Mercurial > ~dholland > hg > tradcpp > index.cgi
annotate directive.c @ 83:3e505c16b0b0
Accept # by itself, including with a comment after it.
author | David A. Holland |
---|---|
date | Mon, 10 Jun 2013 21:33:31 -0400 |
parents | 05a94332f08b |
children | 594495750d84 |
rev | line source |
---|---|
30 | 1 /*- |
2 * Copyright (c) 2010 The NetBSD Foundation, Inc. | |
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 { |
243 if (ifstate->seenelse) { | |
244 complain(p, "Multiple #else directives in one conditional"); | |
245 complain_fail(); | |
246 } | |
247 | |
248 ifstate->curtrue = !ifstate->evertrue; | |
249 ifstate->evertrue = true; | |
250 ifstate->seenelse = true; | |
251 } | |
252 | |
253 static | |
254 void | |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
255 d_endif(struct place *p, struct place *p2, char *line) |
15 | 256 { |
257 if (ifstate->prev == NULL) { | |
258 complain(p, "Unmatched #endif"); | |
259 complain_fail(); | |
260 } else { | |
261 ifstate_pop(); | |
262 } | |
263 } | |
264 | |
265 //////////////////////////////////////////////////////////// | |
266 // macros | |
267 | |
268 static | |
269 void | |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
270 d_define(struct place *p, struct place *p2, char *line) |
15 | 271 { |
18 | 272 size_t pos, argpos; |
273 struct place p3, p4; | |
15 | 274 |
275 /* | |
276 * line may be: | |
277 * macro expansion | |
278 * macro(arg, arg, ...) expansion | |
279 */ | |
280 | |
281 pos = strcspn(line, " \t\f\v("); | |
282 if (line[pos] == '(') { | |
18 | 283 line[pos++] = '\0'; |
284 argpos = pos; | |
15 | 285 pos = pos + strcspn(line+pos, "()"); |
286 if (line[pos] == '(') { | |
287 p2->column += pos; | |
288 complain(p2, "Left parenthesis in macro parameters"); | |
289 complain_fail(); | |
290 return; | |
291 } | |
292 if (line[pos] != ')') { | |
293 p2->column += pos; | |
294 complain(p2, "Unclosed macro parameter list"); | |
295 complain_fail(); | |
296 return; | |
297 } | |
18 | 298 line[pos++] = '\0'; |
36
a489cc223483
Don't demand space after the macro argument parenthesis.
David A. Holland
parents:
30
diff
changeset
|
299 #if 0 |
15 | 300 if (!strchr(ws, line[pos])) { |
301 p2->column += pos; | |
302 complain(p2, "Trash after macro parameter list"); | |
303 complain_fail(); | |
304 return; | |
305 } | |
36
a489cc223483
Don't demand space after the macro argument parenthesis.
David A. Holland
parents:
30
diff
changeset
|
306 #endif |
15 | 307 } else if (line[pos] == '\0') { |
18 | 308 argpos = 0; |
15 | 309 } else { |
310 line[pos++] = '\0'; | |
18 | 311 argpos = 0; |
15 | 312 } |
313 | |
314 pos += strspn(line+pos, ws); | |
315 | |
316 p3 = *p2; | |
18 | 317 p3.column += argpos; |
318 | |
319 p4 = *p2; | |
320 p4.column += pos; | |
321 | |
322 if (argpos) { | |
323 macro_define_params(p2, line, &p3, | |
324 line + argpos, &p4, | |
325 line + pos); | |
326 } else { | |
327 macro_define_plain(p2, line, &p4, line + pos); | |
328 } | |
15 | 329 } |
330 | |
331 static | |
332 void | |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
333 d_undef(struct place *p, struct place *p2, char *line) |
15 | 334 { |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
335 uncomment(line); |
15 | 336 oneword("#undef", p2, line); |
337 macro_undef(line); | |
338 } | |
339 | |
340 //////////////////////////////////////////////////////////// | |
341 // includes | |
342 | |
343 static | |
344 bool | |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
345 tryinclude(struct place *p, char *line) |
15 | 346 { |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
347 size_t len; |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
348 |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
349 len = strlen(line); |
15 | 350 if (len > 2 && line[0] == '"' && line[len-1] == '"') { |
351 line[len-1] = '\0'; | |
352 file_readquote(p, line+1); | |
62
90c6052410ce
Don't truncate the candidate include path strings.
David A. Holland
parents:
49
diff
changeset
|
353 line[len-1] = '"'; |
15 | 354 return true; |
355 } | |
356 if (len > 2 && line[0] == '<' && line[len-1] == '>') { | |
357 line[len-1] = '\0'; | |
358 file_readbracket(p, line+1); | |
62
90c6052410ce
Don't truncate the candidate include path strings.
David A. Holland
parents:
49
diff
changeset
|
359 line[len-1] = '>'; |
15 | 360 return true; |
361 } | |
362 return false; | |
363 } | |
364 | |
365 static | |
366 void | |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
367 d_include(struct place *p, struct place *p2, char *line) |
15 | 368 { |
369 char *text; | |
82
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
370 size_t oldlen; |
15 | 371 |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
372 uncomment(line); |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
373 if (tryinclude(p, line)) { |
15 | 374 return; |
375 } | |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
376 text = macroexpand(p2, line, strlen(line), false); |
82
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
377 |
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
378 oldlen = strlen(text); |
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
379 uncomment(text); |
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
380 /* 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
|
381 text = dorealloc(text, oldlen + 1, strlen(text) + 1); |
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
382 |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
383 if (tryinclude(p, text)) { |
39
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
384 dostrfree(text); |
15 | 385 return; |
386 } | |
39
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
387 dostrfree(text); |
15 | 388 complain(p, "Illegal #include directive"); |
389 complain_fail(); | |
390 } | |
391 | |
392 static | |
393 void | |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
394 d_line(struct place *p, struct place *p2, char *line) |
15 | 395 { |
396 /* XXX */ | |
397 complain(p, "Sorry, no #line yet"); | |
398 } | |
399 | |
400 //////////////////////////////////////////////////////////// | |
401 // messages | |
402 | |
403 static | |
404 void | |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
405 d_warning(struct place *p, struct place *p2, char *line) |
15 | 406 { |
407 char *msg; | |
408 | |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
409 msg = macroexpand(p2, line, strlen(line), false); |
15 | 410 complain(p, "#warning: %s", msg); |
411 if (mode.werror) { | |
412 complain_fail(); | |
413 } | |
39
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
414 dostrfree(msg); |
15 | 415 } |
416 | |
417 static | |
418 void | |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
419 d_error(struct place *p, struct place *p2, char *line) |
15 | 420 { |
421 char *msg; | |
422 | |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
423 msg = macroexpand(p2, line, strlen(line), false); |
15 | 424 complain(p, "#error: %s", msg); |
425 complain_fail(); | |
39
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
426 dostrfree(msg); |
15 | 427 } |
428 | |
429 //////////////////////////////////////////////////////////// | |
430 // other | |
431 | |
432 static | |
433 void | |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
434 d_pragma(struct place *p, struct place *p2, char *line) |
15 | 435 { |
436 complain(p, "#pragma %s", line); | |
437 complain_fail(); | |
438 } | |
439 | |
440 //////////////////////////////////////////////////////////// | |
441 // directive table | |
442 | |
443 static const struct { | |
444 const char *name; | |
445 bool ifskip; | |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
446 void (*func)(struct place *, struct place *, char *line); |
15 | 447 } directives[] = { |
448 { "define", true, d_define }, | |
449 { "elif", false, d_elif }, | |
450 { "else", false, d_else }, | |
451 { "endif", false, d_endif }, | |
452 { "error", true, d_error }, | |
453 { "if", false, d_if }, | |
454 { "ifdef", false, d_ifdef }, | |
455 { "ifndef", false, d_ifndef }, | |
456 { "include", true, d_include }, | |
457 { "line", true, d_line }, | |
458 { "pragma", true, d_pragma }, | |
459 { "undef", true, d_undef }, | |
460 { "warning", true, d_warning }, | |
461 }; | |
462 static const unsigned numdirectives = HOWMANY(directives); | |
463 | |
464 static | |
465 void | |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
466 directive_gotdirective(struct place *p, char *line) |
15 | 467 { |
468 struct place p2; | |
469 size_t len, skip; | |
470 unsigned i; | |
471 | |
472 p2 = *p; | |
473 for (i=0; i<numdirectives; i++) { | |
474 len = strlen(directives[i].name); | |
475 if (!strncmp(line, directives[i].name, len) && | |
476 strchr(ws, line[len])) { | |
477 if (directives[i].ifskip && !ifstate->curtrue) { | |
478 return; | |
479 } | |
480 skip = len + strspn(line+len, ws); | |
481 p2.column += skip; | |
482 line += skip; | |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
483 |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
484 len = strlen(line); |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
485 len = notrailingws(line, len); |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
486 if (len < strlen(line)) { |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
487 line[len] = '\0'; |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
488 } |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
489 directives[i].func(p, &p2, line); |
15 | 490 return; |
491 } | |
492 } | |
83
3e505c16b0b0
Accept # by itself, including with a comment after it.
David A. Holland
parents:
82
diff
changeset
|
493 /* 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
|
494 uncomment(line); |
3e505c16b0b0
Accept # by itself, including with a comment after it.
David A. Holland
parents:
82
diff
changeset
|
495 if (line[0] == '\0') { |
3e505c16b0b0
Accept # by itself, including with a comment after it.
David A. Holland
parents:
82
diff
changeset
|
496 return; |
3e505c16b0b0
Accept # by itself, including with a comment after it.
David A. Holland
parents:
82
diff
changeset
|
497 } |
3e505c16b0b0
Accept # by itself, including with a comment after it.
David A. Holland
parents:
82
diff
changeset
|
498 |
15 | 499 skip = strcspn(line, ws); |
500 complain(p, "Unknown directive #%.*s", (int)skip, line); | |
501 complain_fail(); | |
502 } | |
503 | |
49
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
504 /* |
77
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
505 * 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
|
506 */ |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
507 static |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
508 size_t |
77
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
509 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
|
510 { |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
511 size_t pos; |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
512 bool incomment; |
77
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
513 struct place p2; |
49
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
514 |
77
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
515 p2 = *p; |
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
516 incomment = 0; |
49
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
517 for (pos = 0; pos+1 < len; pos++) { |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
518 if (line[pos] == '/' && line[pos+1] == '*') { |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
519 if (incomment) { |
77
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
520 complain(&p2, "Warning: %c%c within comment", |
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
521 '/', '*'); |
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
522 if (mode.werror) { |
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
523 complain_failed(); |
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
524 } |
49
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
525 } else { |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
526 incomment = true; |
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 } else if (line[pos] == '*' && line[pos+1] == '/') { |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
529 if (incomment) { |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
530 incomment = false; |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
531 } else { |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
532 /* stray end-comment; should we care? */ |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
533 } |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
534 } |
77
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
535 if (line[pos] == '\n') { |
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
536 p2.line++; |
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
537 p2.column = 0; |
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
538 } else { |
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
539 p2.column++; |
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
540 } |
49
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
541 } |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
542 |
77
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
543 /* 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
|
544 assert(!incomment); |
49
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
545 return len; |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
546 } |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
547 |
15 | 548 void |
549 directive_gotline(struct place *p, char *line, size_t len) | |
550 { | |
551 size_t skip; | |
552 | |
77
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
553 if (warns.nestcomment) { |
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
554 directive_scancomments(p, line, len); |
49
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
555 } |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
556 |
66
f8507e5ed84c
Recognize directive lines only when the # is exactly in column 0.
David A. Holland
parents:
64
diff
changeset
|
557 /* 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
|
558 if (line[0] == '#') { |
66
f8507e5ed84c
Recognize directive lines only when the # is exactly in column 0.
David A. Holland
parents:
64
diff
changeset
|
559 skip = 1 + strspn(line + 1, ws); |
77
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
560 assert(skip <= len); |
15 | 561 p->column += skip; |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
562 assert(line[len] == '\0'); |
77
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
563 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
|
564 p->column += len-skip; |
15 | 565 } else if (ifstate->curtrue) { |
77
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
566 macro_sendline(p, line, len); |
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
567 p->column += len; |
15 | 568 } |
569 } | |
570 | |
571 void | |
572 directive_goteof(struct place *p) | |
573 { | |
574 while (ifstate->prev != NULL) { | |
575 complain(p, "Missing #endif"); | |
576 complain(&ifstate->startplace, "...opened at this point"); | |
577 complain_failed(); | |
578 ifstate_pop(); | |
579 } | |
580 macro_sendeof(p); | |
581 } | |
582 | |
583 //////////////////////////////////////////////////////////// | |
584 // module initialization | |
585 | |
586 void | |
587 directive_init(void) | |
588 { | |
589 ifstate = ifstate_create(NULL, NULL, true); | |
590 } | |
591 | |
592 void | |
593 directive_cleanup(void) | |
594 { | |
595 assert(ifstate->prev == NULL); | |
596 ifstate_destroy(ifstate); | |
597 ifstate = NULL; | |
598 } |