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