Mercurial > ~dholland > hg > tradcpp > index.cgi
annotate directive.c @ 172:3e7e696fe558
Clean up the mess Joerg made of apply_builtin_macros().
author | David A. Holland |
---|---|
date | Fri, 12 Jun 2015 02:14:45 -0400 |
parents | cc6d6f27d6ee |
children | 6ff17ab68b16 |
rev | line source |
---|---|
30 | 1 /*- |
99
60184aa42604
add 2013 to copyrights where it seems warranted
David A. Holland
parents:
90
diff
changeset
|
2 * Copyright (c) 2010, 2013 The NetBSD Foundation, Inc. |
30 | 3 * All rights reserved. |
4 * | |
5 * This code is derived from software contributed to The NetBSD Foundation | |
6 * by David A. Holland. | |
7 * | |
8 * Redistribution and use in source and binary forms, with or without | |
9 * modification, are permitted provided that the following conditions | |
10 * are met: | |
11 * 1. Redistributions of source code must retain the above copyright | |
12 * notice, this list of conditions and the following disclaimer. | |
13 * 2. Redistributions in binary form must reproduce the above copyright | |
14 * notice, this list of conditions and the following disclaimer in the | |
15 * documentation and/or other materials provided with the distribution. | |
16 * | |
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS | |
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | |
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS | |
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
27 * POSSIBILITY OF SUCH DAMAGE. | |
28 */ | |
29 | |
15 | 30 #include <assert.h> |
31 #include <stdbool.h> | |
32 #include <stdlib.h> | |
33 #include <string.h> | |
165 | 34 #include <errno.h> |
15 | 35 |
36 #include "utils.h" | |
37 #include "mode.h" | |
38 #include "place.h" | |
39 #include "files.h" | |
40 #include "directive.h" | |
41 #include "macro.h" | |
42 #include "eval.h" | |
49
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
43 #include "output.h" |
15 | 44 |
45 struct ifstate { | |
46 struct ifstate *prev; | |
47 struct place startplace; | |
48 bool curtrue; | |
49 bool evertrue; | |
50 bool seenelse; | |
51 }; | |
52 | |
53 static struct ifstate *ifstate; | |
54 | |
55 //////////////////////////////////////////////////////////// | |
56 // common parsing bits | |
57 | |
58 static | |
59 void | |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
60 uncomment(char *buf) |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
61 { |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
62 char *s, *t, *u = NULL; |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
63 bool incomment = false; |
81
27c9aafcaca1
Don't recognize comments within double-quote strings.
David A. Holland
parents:
77
diff
changeset
|
64 bool inesc = false; |
27c9aafcaca1
Don't recognize comments within double-quote strings.
David A. Holland
parents:
77
diff
changeset
|
65 bool inquote = false; |
128
1cda505ddc78
Don't expand macros within character constants.
David A. Holland
parents:
127
diff
changeset
|
66 char quote = '\0'; |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
67 |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
68 for (s = t = buf; *s; s++) { |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
69 if (incomment) { |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
70 if (s[0] == '*' && s[1] == '/') { |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
71 s++; |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
72 incomment = false; |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
73 } |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
74 } else { |
81
27c9aafcaca1
Don't recognize comments within double-quote strings.
David A. Holland
parents:
77
diff
changeset
|
75 if (!inquote && s[0] == '/' && s[1] == '*') { |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
76 incomment = true; |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
77 } else { |
81
27c9aafcaca1
Don't recognize comments within double-quote strings.
David A. Holland
parents:
77
diff
changeset
|
78 if (inesc) { |
27c9aafcaca1
Don't recognize comments within double-quote strings.
David A. Holland
parents:
77
diff
changeset
|
79 inesc = false; |
27c9aafcaca1
Don't recognize comments within double-quote strings.
David A. Holland
parents:
77
diff
changeset
|
80 } else if (s[0] == '\\') { |
27c9aafcaca1
Don't recognize comments within double-quote strings.
David A. Holland
parents:
77
diff
changeset
|
81 inesc = true; |
128
1cda505ddc78
Don't expand macros within character constants.
David A. Holland
parents:
127
diff
changeset
|
82 } else if (!inquote && |
1cda505ddc78
Don't expand macros within character constants.
David A. Holland
parents:
127
diff
changeset
|
83 (s[0] == '"' || s[0] == '\'')) { |
1cda505ddc78
Don't expand macros within character constants.
David A. Holland
parents:
127
diff
changeset
|
84 inquote = true; |
1cda505ddc78
Don't expand macros within character constants.
David A. Holland
parents:
127
diff
changeset
|
85 quote = s[0]; |
1cda505ddc78
Don't expand macros within character constants.
David A. Holland
parents:
127
diff
changeset
|
86 } else if (inquote && s[0] == quote) { |
1cda505ddc78
Don't expand macros within character constants.
David A. Holland
parents:
127
diff
changeset
|
87 inquote = false; |
81
27c9aafcaca1
Don't recognize comments within double-quote strings.
David A. Holland
parents:
77
diff
changeset
|
88 } |
27c9aafcaca1
Don't recognize comments within double-quote strings.
David A. Holland
parents:
77
diff
changeset
|
89 |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
90 if (t != s) { |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
91 *t = *s; |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
92 } |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
93 if (!strchr(ws, *t)) { |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
94 u = t; |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
95 } |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
96 t++; |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
97 } |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
98 } |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
99 } |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
100 if (u) { |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
101 /* end string after last non-whitespace char */ |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
102 u[1] = '\0'; |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
103 } else { |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
104 *t = '\0'; |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
105 } |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
106 } |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
107 |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
108 static |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
109 void |
15 | 110 oneword(const char *what, struct place *p2, char *line) |
111 { | |
112 size_t pos; | |
113 | |
114 pos = strcspn(line, ws); | |
115 if (line[pos] != '\0') { | |
116 p2->column += pos; | |
117 complain(p2, "Garbage after %s argument", what); | |
118 complain_fail(); | |
119 line[pos] = '\0'; | |
120 } | |
121 } | |
122 | |
123 //////////////////////////////////////////////////////////// | |
124 // if handling | |
125 | |
126 static | |
127 struct ifstate * | |
128 ifstate_create(struct ifstate *prev, struct place *p, bool startstate) | |
129 { | |
130 struct ifstate *is; | |
131 | |
132 is = domalloc(sizeof(*is)); | |
133 is->prev = prev; | |
134 if (p != NULL) { | |
135 is->startplace = *p; | |
136 } else { | |
137 place_setbuiltin(&is->startplace, 1); | |
138 } | |
139 is->curtrue = startstate; | |
140 is->evertrue = is->curtrue; | |
141 is->seenelse = false; | |
142 return is; | |
143 } | |
144 | |
145 static | |
146 void | |
147 ifstate_destroy(struct ifstate *is) | |
148 { | |
39
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
149 dofree(is, sizeof(*is)); |
15 | 150 } |
151 | |
152 static | |
153 void | |
154 ifstate_push(struct place *p, bool startstate) | |
155 { | |
72 | 156 struct ifstate *newstate; |
157 | |
158 newstate = ifstate_create(ifstate, p, startstate); | |
159 if (!ifstate->curtrue) { | |
160 newstate->curtrue = false; | |
161 newstate->evertrue = true; | |
162 } | |
163 ifstate = newstate; | |
15 | 164 } |
165 | |
166 static | |
167 void | |
168 ifstate_pop(void) | |
169 { | |
170 struct ifstate *is; | |
171 | |
172 is = ifstate; | |
173 ifstate = ifstate->prev; | |
174 ifstate_destroy(is); | |
175 } | |
176 | |
177 static | |
178 void | |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
179 d_if(struct lineplace *lp, struct place *p2, char *line) |
15 | 180 { |
181 char *expr; | |
182 bool val; | |
16 | 183 struct place p3 = *p2; |
82
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
184 size_t oldlen; |
15 | 185 |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
186 expr = macroexpand(p2, line, strlen(line), true); |
82
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
187 |
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
188 oldlen = strlen(expr); |
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
189 uncomment(expr); |
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
190 /* trim to fit, so the malloc debugging won't complain */ |
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
191 expr = dorealloc(expr, oldlen + 1, strlen(expr) + 1); |
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
192 |
127 | 193 if (ifstate->curtrue) { |
194 val = eval(&p3, expr); | |
195 } else { | |
196 val = 0; | |
197 } | |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
198 ifstate_push(&lp->current, val); |
39
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
199 dostrfree(expr); |
15 | 200 } |
201 | |
202 static | |
203 void | |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
204 d_ifdef(struct lineplace *lp, struct place *p2, char *line) |
15 | 205 { |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
206 uncomment(line); |
15 | 207 oneword("#ifdef", p2, line); |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
208 ifstate_push(&lp->current, macro_isdefined(line)); |
15 | 209 } |
210 | |
211 static | |
212 void | |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
213 d_ifndef(struct lineplace *lp, struct place *p2, char *line) |
15 | 214 { |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
215 uncomment(line); |
15 | 216 oneword("#ifndef", p2, line); |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
217 ifstate_push(&lp->current, !macro_isdefined(line)); |
15 | 218 } |
219 | |
220 static | |
221 void | |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
222 d_elif(struct lineplace *lp, struct place *p2, char *line) |
15 | 223 { |
224 char *expr; | |
16 | 225 struct place p3 = *p2; |
82
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
226 size_t oldlen; |
15 | 227 |
228 if (ifstate->seenelse) { | |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
229 complain(&lp->current, "#elif after #else"); |
15 | 230 complain_fail(); |
231 } | |
232 | |
233 if (ifstate->evertrue) { | |
234 ifstate->curtrue = false; | |
235 } else { | |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
236 expr = macroexpand(p2, line, strlen(line), true); |
82
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
237 |
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
238 oldlen = strlen(expr); |
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
239 uncomment(expr); |
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
240 /* trim to fit, so the malloc debugging won't complain */ |
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
241 expr = dorealloc(expr, oldlen + 1, strlen(expr) + 1); |
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
242 |
16 | 243 ifstate->curtrue = eval(&p3, expr); |
15 | 244 ifstate->evertrue = ifstate->curtrue; |
39
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
245 dostrfree(expr); |
15 | 246 } |
247 } | |
248 | |
249 static | |
250 void | |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
251 d_else(struct lineplace *lp, struct place *p2, char *line) |
15 | 252 { |
103 | 253 (void)p2; |
254 (void)line; | |
255 | |
15 | 256 if (ifstate->seenelse) { |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
257 complain(&lp->current, |
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
258 "Multiple #else directives in one conditional"); |
15 | 259 complain_fail(); |
260 } | |
261 | |
262 ifstate->curtrue = !ifstate->evertrue; | |
263 ifstate->evertrue = true; | |
264 ifstate->seenelse = true; | |
265 } | |
266 | |
267 static | |
268 void | |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
269 d_endif(struct lineplace *lp, struct place *p2, char *line) |
15 | 270 { |
103 | 271 (void)p2; |
272 (void)line; | |
273 | |
15 | 274 if (ifstate->prev == NULL) { |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
275 complain(&lp->current, "Unmatched #endif"); |
15 | 276 complain_fail(); |
277 } else { | |
278 ifstate_pop(); | |
279 } | |
280 } | |
281 | |
282 //////////////////////////////////////////////////////////// | |
283 // macros | |
284 | |
285 static | |
286 void | |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
287 d_define(struct lineplace *lp, struct place *p2, char *line) |
15 | 288 { |
18 | 289 size_t pos, argpos; |
290 struct place p3, p4; | |
15 | 291 |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
292 (void)lp; |
103 | 293 |
15 | 294 /* |
295 * line may be: | |
296 * macro expansion | |
297 * macro(arg, arg, ...) expansion | |
298 */ | |
299 | |
300 pos = strcspn(line, " \t\f\v("); | |
301 if (line[pos] == '(') { | |
18 | 302 line[pos++] = '\0'; |
303 argpos = pos; | |
15 | 304 pos = pos + strcspn(line+pos, "()"); |
305 if (line[pos] == '(') { | |
306 p2->column += pos; | |
307 complain(p2, "Left parenthesis in macro parameters"); | |
308 complain_fail(); | |
309 return; | |
310 } | |
311 if (line[pos] != ')') { | |
312 p2->column += pos; | |
313 complain(p2, "Unclosed macro parameter list"); | |
314 complain_fail(); | |
315 return; | |
316 } | |
18 | 317 line[pos++] = '\0'; |
36
a489cc223483
Don't demand space after the macro argument parenthesis.
David A. Holland
parents:
30
diff
changeset
|
318 #if 0 |
15 | 319 if (!strchr(ws, line[pos])) { |
320 p2->column += pos; | |
321 complain(p2, "Trash after macro parameter list"); | |
322 complain_fail(); | |
323 return; | |
324 } | |
36
a489cc223483
Don't demand space after the macro argument parenthesis.
David A. Holland
parents:
30
diff
changeset
|
325 #endif |
15 | 326 } else if (line[pos] == '\0') { |
18 | 327 argpos = 0; |
15 | 328 } else { |
329 line[pos++] = '\0'; | |
18 | 330 argpos = 0; |
15 | 331 } |
332 | |
333 pos += strspn(line+pos, ws); | |
334 | |
335 p3 = *p2; | |
18 | 336 p3.column += argpos; |
337 | |
338 p4 = *p2; | |
339 p4.column += pos; | |
340 | |
341 if (argpos) { | |
342 macro_define_params(p2, line, &p3, | |
343 line + argpos, &p4, | |
344 line + pos); | |
345 } else { | |
346 macro_define_plain(p2, line, &p4, line + pos); | |
347 } | |
15 | 348 } |
349 | |
350 static | |
351 void | |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
352 d_undef(struct lineplace *lp, struct place *p2, char *line) |
15 | 353 { |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
354 (void)lp; |
103 | 355 |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
356 uncomment(line); |
15 | 357 oneword("#undef", p2, line); |
358 macro_undef(line); | |
359 } | |
360 | |
361 //////////////////////////////////////////////////////////// | |
362 // includes | |
363 | |
364 static | |
365 bool | |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
366 tryinclude(struct place *p, char *line) |
15 | 367 { |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
368 size_t len; |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
369 |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
370 len = strlen(line); |
15 | 371 if (len > 2 && line[0] == '"' && line[len-1] == '"') { |
372 line[len-1] = '\0'; | |
373 file_readquote(p, line+1); | |
62
90c6052410ce
Don't truncate the candidate include path strings.
David A. Holland
parents:
49
diff
changeset
|
374 line[len-1] = '"'; |
15 | 375 return true; |
376 } | |
377 if (len > 2 && line[0] == '<' && line[len-1] == '>') { | |
378 line[len-1] = '\0'; | |
379 file_readbracket(p, line+1); | |
62
90c6052410ce
Don't truncate the candidate include path strings.
David A. Holland
parents:
49
diff
changeset
|
380 line[len-1] = '>'; |
15 | 381 return true; |
382 } | |
383 return false; | |
384 } | |
385 | |
386 static | |
387 void | |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
388 d_include(struct lineplace *lp, struct place *p2, char *line) |
15 | 389 { |
390 char *text; | |
82
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
391 size_t oldlen; |
15 | 392 |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
393 uncomment(line); |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
394 if (tryinclude(&lp->current, line)) { |
15 | 395 return; |
396 } | |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
397 text = macroexpand(p2, line, strlen(line), false); |
82
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
398 |
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
399 oldlen = strlen(text); |
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
400 uncomment(text); |
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
401 /* trim to fit, so the malloc debugging won't complain */ |
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
402 text = dorealloc(text, oldlen + 1, strlen(text) + 1); |
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
403 |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
404 if (tryinclude(&lp->current, text)) { |
39
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
405 dostrfree(text); |
15 | 406 return; |
407 } | |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
408 complain(&lp->current, "Illegal #include directive"); |
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
409 complain(&lp->current, "Before macro expansion: #include %s", line); |
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
410 complain(&lp->current, "After macro expansion: #include %s", text); |
39
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
411 dostrfree(text); |
15 | 412 complain_fail(); |
413 } | |
414 | |
415 static | |
416 void | |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
417 d_line(struct lineplace *lp, struct place *p2, char *line) |
15 | 418 { |
160 | 419 char *text; |
420 size_t oldlen; | |
165 | 421 unsigned long val; |
422 char *moretext; | |
423 size_t moretextlen; | |
424 char *filename; | |
160 | 425 |
426 text = macroexpand(p2, line, strlen(line), true); | |
427 | |
428 oldlen = strlen(text); | |
429 uncomment(text); | |
430 /* trim to fit, so the malloc debugging won't complain */ | |
431 text = dorealloc(text, oldlen + 1, strlen(text) + 1); | |
103 | 432 |
165 | 433 /* |
434 * What we should have here: either 1234 "file.c", | |
435 * or just 1234. | |
436 */ | |
437 | |
438 errno = 0; | |
439 val = strtoul(text, &moretext, 10); | |
440 if (errno) { | |
441 complain(&lp->current, "No line number in #line directive"); | |
442 goto fail; | |
160 | 443 } |
165 | 444 #if UINT_MAX < ULONG_MAX |
445 if (val > UINT_MAX) { | |
446 complain(&lp->current, | |
447 "Line number in #line directive too large"); | |
448 goto fail; | |
160 | 449 } |
165 | 450 #endif |
451 moretext += strspn(moretext, ws); | |
452 moretextlen = strlen(moretext); | |
453 lp->current.column += (moretext - text); | |
454 | |
455 if (moretextlen > 2 && | |
456 moretext[0] == '"' && moretext[moretextlen-1] == '"') { | |
457 filename = dostrndup(moretext+1, moretextlen-2); | |
164 | 458 place_setfile(&lp->nextline, filename); |
160 | 459 dostrfree(filename); |
460 } | |
165 | 461 else if (moretextlen > 0) { |
462 complain(&lp->current, | |
463 "Invalid file name in #line directive"); | |
464 goto fail; | |
465 } | |
466 | |
467 lp->nextline.line = val; | |
160 | 468 dostrfree(text); |
469 return; | |
470 | |
165 | 471 fail: |
472 complain(&lp->current, "Before macro expansion: #line %s", line); | |
473 complain(&lp->current, "After macro expansion: #line %s", text); | |
474 complain_fail(); | |
160 | 475 dostrfree(text); |
15 | 476 } |
477 | |
478 //////////////////////////////////////////////////////////// | |
479 // messages | |
480 | |
481 static | |
482 void | |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
483 d_warning(struct lineplace *lp, struct place *p2, char *line) |
15 | 484 { |
485 char *msg; | |
486 | |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
487 msg = macroexpand(p2, line, strlen(line), false); |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
488 complain(&lp->current, "#warning: %s", msg); |
15 | 489 if (mode.werror) { |
490 complain_fail(); | |
491 } | |
39
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
492 dostrfree(msg); |
15 | 493 } |
494 | |
495 static | |
496 void | |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
497 d_error(struct lineplace *lp, struct place *p2, char *line) |
15 | 498 { |
499 char *msg; | |
500 | |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
501 msg = macroexpand(p2, line, strlen(line), false); |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
502 complain(&lp->current, "#error: %s", msg); |
15 | 503 complain_fail(); |
39
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
504 dostrfree(msg); |
15 | 505 } |
506 | |
507 //////////////////////////////////////////////////////////// | |
508 // other | |
509 | |
510 static | |
511 void | |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
512 d_pragma(struct lineplace *lp, struct place *p2, char *line) |
15 | 513 { |
103 | 514 (void)p2; |
515 | |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
516 complain(&lp->current, "#pragma %s", line); |
15 | 517 complain_fail(); |
518 } | |
519 | |
520 //////////////////////////////////////////////////////////// | |
521 // directive table | |
522 | |
523 static const struct { | |
524 const char *name; | |
525 bool ifskip; | |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
526 void (*func)(struct lineplace *, struct place *, char *line); |
15 | 527 } directives[] = { |
528 { "define", true, d_define }, | |
529 { "elif", false, d_elif }, | |
530 { "else", false, d_else }, | |
531 { "endif", false, d_endif }, | |
532 { "error", true, d_error }, | |
533 { "if", false, d_if }, | |
534 { "ifdef", false, d_ifdef }, | |
535 { "ifndef", false, d_ifndef }, | |
536 { "include", true, d_include }, | |
537 { "line", true, d_line }, | |
538 { "pragma", true, d_pragma }, | |
539 { "undef", true, d_undef }, | |
540 { "warning", true, d_warning }, | |
541 }; | |
542 static const unsigned numdirectives = HOWMANY(directives); | |
543 | |
544 static | |
545 void | |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
546 directive_gotdirective(struct lineplace *lp, char *line) |
15 | 547 { |
548 struct place p2; | |
549 size_t len, skip; | |
550 unsigned i; | |
551 | |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
552 p2 = lp->current; |
15 | 553 for (i=0; i<numdirectives; i++) { |
554 len = strlen(directives[i].name); | |
555 if (!strncmp(line, directives[i].name, len) && | |
556 strchr(ws, line[len])) { | |
557 if (directives[i].ifskip && !ifstate->curtrue) { | |
558 return; | |
559 } | |
560 skip = len + strspn(line+len, ws); | |
561 p2.column += skip; | |
562 line += skip; | |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
563 |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
564 len = strlen(line); |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
565 len = notrailingws(line, len); |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
566 if (len < strlen(line)) { |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
567 line[len] = '\0'; |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
568 } |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
569 directives[i].func(lp, &p2, line); |
15 | 570 return; |
571 } | |
572 } | |
83
3e505c16b0b0
Accept # by itself, including with a comment after it.
David A. Holland
parents:
82
diff
changeset
|
573 /* ugh. allow # by itself, including with a comment after it */ |
3e505c16b0b0
Accept # by itself, including with a comment after it.
David A. Holland
parents:
82
diff
changeset
|
574 uncomment(line); |
3e505c16b0b0
Accept # by itself, including with a comment after it.
David A. Holland
parents:
82
diff
changeset
|
575 if (line[0] == '\0') { |
3e505c16b0b0
Accept # by itself, including with a comment after it.
David A. Holland
parents:
82
diff
changeset
|
576 return; |
3e505c16b0b0
Accept # by itself, including with a comment after it.
David A. Holland
parents:
82
diff
changeset
|
577 } |
3e505c16b0b0
Accept # by itself, including with a comment after it.
David A. Holland
parents:
82
diff
changeset
|
578 |
15 | 579 skip = strcspn(line, ws); |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
580 complain(&lp->current, "Unknown directive #%.*s", (int)skip, line); |
15 | 581 complain_fail(); |
582 } | |
583 | |
49
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
584 /* |
77
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
585 * Check for nested comment delimiters in LINE. |
49
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
586 */ |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
587 static |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
588 size_t |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
589 directive_scancomments(const struct lineplace *lp, char *line, size_t len) |
49
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
590 { |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
591 size_t pos; |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
592 bool incomment; |
77
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
593 struct place p2; |
49
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
594 |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
595 p2 = lp->current; |
77
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
596 incomment = 0; |
49
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
597 for (pos = 0; pos+1 < len; pos++) { |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
598 if (line[pos] == '/' && line[pos+1] == '*') { |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
599 if (incomment) { |
77
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
600 complain(&p2, "Warning: %c%c within comment", |
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
601 '/', '*'); |
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
602 if (mode.werror) { |
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
603 complain_failed(); |
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
604 } |
49
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
605 } else { |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
606 incomment = true; |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
607 } |
109 | 608 pos++; |
49
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
609 } else if (line[pos] == '*' && line[pos+1] == '/') { |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
610 if (incomment) { |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
611 incomment = false; |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
612 } else { |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
613 /* stray end-comment; should we care? */ |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
614 } |
109 | 615 pos++; |
49
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
616 } |
77
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
617 if (line[pos] == '\n') { |
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
618 p2.line++; |
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
619 p2.column = 0; |
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
620 } else { |
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
621 p2.column++; |
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
622 } |
49
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
623 } |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
624 |
77
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
625 /* multiline comments are supposed to arrive in a single buffer */ |
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
626 assert(!incomment); |
49
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
627 return len; |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
628 } |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
629 |
15 | 630 void |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
631 directive_gotline(struct lineplace *lp, char *line, size_t len) |
15 | 632 { |
633 size_t skip; | |
634 | |
77
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
635 if (warns.nestcomment) { |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
636 directive_scancomments(lp, line, len); |
49
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
637 } |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
638 |
66
f8507e5ed84c
Recognize directive lines only when the # is exactly in column 0.
David A. Holland
parents:
64
diff
changeset
|
639 /* check if we have a directive line (# exactly in column 0) */ |
77
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
640 if (line[0] == '#') { |
66
f8507e5ed84c
Recognize directive lines only when the # is exactly in column 0.
David A. Holland
parents:
64
diff
changeset
|
641 skip = 1 + strspn(line + 1, ws); |
77
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
642 assert(skip <= len); |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
643 lp->current.column += skip; |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
644 assert(line[len] == '\0'); |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
645 directive_gotdirective(lp, line+skip /*, length = len-skip */); |
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
646 lp->current.column += len-skip; |
15 | 647 } else if (ifstate->curtrue) { |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
648 macro_sendline(&lp->current, line, len); |
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
649 lp->current.column += len; |
15 | 650 } |
651 } | |
652 | |
653 void | |
654 directive_goteof(struct place *p) | |
655 { | |
656 while (ifstate->prev != NULL) { | |
657 complain(p, "Missing #endif"); | |
658 complain(&ifstate->startplace, "...opened at this point"); | |
659 complain_failed(); | |
660 ifstate_pop(); | |
661 } | |
662 macro_sendeof(p); | |
663 } | |
664 | |
665 //////////////////////////////////////////////////////////// | |
666 // module initialization | |
667 | |
668 void | |
669 directive_init(void) | |
670 { | |
671 ifstate = ifstate_create(NULL, NULL, true); | |
672 } | |
673 | |
674 void | |
675 directive_cleanup(void) | |
676 { | |
677 assert(ifstate->prev == NULL); | |
678 ifstate_destroy(ifstate); | |
679 ifstate = NULL; | |
680 } |