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