annotate place.c @ 9:1fbcbd58742e

move destroyall to array.h
author David A. Holland
date Sun, 19 Dec 2010 19:19:02 -0500
parents 97243badae69
children 800f3a560a3b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
1 #include <assert.h>
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
2 #include <stdarg.h>
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
3 #include <stdio.h>
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
4 #include <stdlib.h>
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
5
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
6 #include "utils.h"
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
7 #include "place.h"
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
8
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
9 static bool overall_failure;
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
10
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
11 #define NOWHERE_LINE 0
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
12 #define BUILTIN_LINE 1
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
13 #define COMMANDLINE_LINE 2
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
14
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
15 static struct place scratchplace;
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
16 static bool scratchplace_inuse;
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
17
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
18 static
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
19 bool
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
20 place_isnowhere(const struct place *p)
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
21 {
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
22 return p->file == NULL && p->line == NOWHERE_LINE;
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
23 }
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
24
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
25 static
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
26 bool
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
27 place_isbuiltin(const struct place *p)
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
28 {
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
29 return p->file == NULL && p->line == BUILTIN_LINE;
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
30 }
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
31
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
32 static
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
33 bool
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
34 place_iscommandline(const struct place *p)
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
35 {
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
36 return p->file == NULL && p->line == COMMANDLINE_LINE;
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
37 }
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
38
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
39 struct place *
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
40 place_gettemporary(void)
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
41 {
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
42 assert(!scratchplace_inuse);
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
43 scratchplace_inuse = true;
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
44 return &scratchplace;
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
45 }
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
46
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
47 void
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
48 place_puttemporary(struct place *p)
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
49 {
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
50 assert(scratchplace_inuse);
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
51 assert(p == &scratchplace);
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
52 scratchplace_inuse = false;
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
53 }
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
54
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
55 struct place *
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
56 place_create(void)
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
57 {
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
58 struct place *p;
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
59
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
60 p = domalloc(sizeof(*p));
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
61 place_setnowhere(p);
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
62 return p;
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
63 }
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
64
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
65 struct place *
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
66 place_clone(const struct place *op)
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
67 {
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
68 struct place *p;
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
69
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
70 p = domalloc(sizeof(*p));
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
71 *p = *op;
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
72 return p;
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
73 }
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
74
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
75 void
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
76 place_destroy(struct place *p)
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
77 {
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
78 free(p);
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
79 }
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
80
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
81 void
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
82 place_setnowhere(struct place *p)
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
83 {
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
84 p->file = NULL;
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
85 p->line = NOWHERE_LINE;
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
86 p->column = 0;
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
87 }
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
88
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
89 void
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
90 place_setbuiltin(struct place *p, unsigned num)
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
91 {
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
92 p->file = NULL;
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
93 p->line = BUILTIN_LINE;
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
94 p->column = num;
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
95 }
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
96
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
97 void
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
98 place_setcommandline(struct place *p, unsigned column)
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
99 {
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
100 p->file = NULL;
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
101 p->line = COMMANDLINE_LINE;
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
102 p->column = column;
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
103 }
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
104
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
105 static
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
106 void
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
107 place_print(const struct place *p)
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
108 {
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
109 if (place_iscommandline(p)) {
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
110 fprintf(stderr, "<command-line>:1:%u", p->column);
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
111 } else if (place_isbuiltin(p)) {
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
112 fprintf(stderr, "<built-in>:%u:1", p->column);
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
113 } else {
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
114 fprintf(stderr, "%s:%u:%u", seenfile_getname(p->file),
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
115 p->line, p->column);
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
116 }
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
117 }
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
118
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
119 static
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
120 void
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
121 place_printfrom(const struct place *p)
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
122 {
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
123 const struct place *from;
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
124
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
125 from = seenfile_getincludeplace(p->file);
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
126 if (!place_isnowhere(from)) {
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
127 place_printfrom(from);
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
128 }
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
129 fprintf(stderr, "In file included from ");
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
130 place_print(p);
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
131 fprintf(stderr, ":\n");
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
132 }
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
133
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
134 void
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
135 complain(const struct place *p, const char *fmt, ...)
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
136 {
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
137 va_list ap;
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
138 const struct place *from;
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
139
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
140 from = seenfile_getincludeplace(p->file);
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
141 if (!place_isnowhere(from)) {
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
142 place_printfrom(from);
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
143 }
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
144 place_print(p);
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
145 fprintf(stderr, ": ");
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
146 va_start(ap, fmt);
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
147 vfprintf(stderr, fmt, ap);
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
148 va_end(ap);
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
149 fprintf(stderr, "\n");
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
150 }
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
151
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
152 void
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
153 complain_fail(void)
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
154 {
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
155 overall_failure = true;
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
156 }
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
157
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
158 bool
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
159 complain_failed(void)
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
160 {
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
161 return overall_failure;
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
162 }
97243badae69 split place stuff to its own file
David A. Holland
parents:
diff changeset
163