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