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