Mercurial > ~dholland > hg > tradcpp > index.cgi
annotate directive.c @ 73:d122b4553150
another multiline comment case
author | David A. Holland |
---|---|
date | Mon, 10 Jun 2013 18:40:26 -0400 |
parents | b1d0f10e8d36 |
children | 123168887da8 |
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; | |
49
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
53 static bool in_multiline_comment; |
15 | 54 |
55 //////////////////////////////////////////////////////////// | |
56 // common parsing bits | |
57 | |
58 static | |
59 void | |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
60 uncomment(char *buf) |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
61 { |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
62 char *s, *t, *u = NULL; |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
63 bool incomment = false; |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
64 |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
65 for (s = t = buf; *s; s++) { |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
66 if (incomment) { |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
67 if (s[0] == '*' && s[1] == '/') { |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
68 s++; |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
69 incomment = false; |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
70 } |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
71 } else { |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
72 if (s[0] == '/' && s[1] == '*') { |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
73 incomment = true; |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
74 } else { |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
75 if (t != s) { |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
76 *t = *s; |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
77 } |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
78 if (!strchr(ws, *t)) { |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
79 u = t; |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
80 } |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
81 t++; |
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 } |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
85 if (u) { |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
86 /* end string after last non-whitespace char */ |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
87 u[1] = '\0'; |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
88 } else { |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
89 *t = '\0'; |
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 |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
93 static |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
94 void |
15 | 95 oneword(const char *what, struct place *p2, char *line) |
96 { | |
97 size_t pos; | |
98 | |
99 pos = strcspn(line, ws); | |
100 if (line[pos] != '\0') { | |
101 p2->column += pos; | |
102 complain(p2, "Garbage after %s argument", what); | |
103 complain_fail(); | |
104 line[pos] = '\0'; | |
105 } | |
106 } | |
107 | |
108 //////////////////////////////////////////////////////////// | |
109 // if handling | |
110 | |
111 static | |
112 struct ifstate * | |
113 ifstate_create(struct ifstate *prev, struct place *p, bool startstate) | |
114 { | |
115 struct ifstate *is; | |
116 | |
117 is = domalloc(sizeof(*is)); | |
118 is->prev = prev; | |
119 if (p != NULL) { | |
120 is->startplace = *p; | |
121 } else { | |
122 place_setbuiltin(&is->startplace, 1); | |
123 } | |
124 is->curtrue = startstate; | |
125 is->evertrue = is->curtrue; | |
126 is->seenelse = false; | |
127 return is; | |
128 } | |
129 | |
130 static | |
131 void | |
132 ifstate_destroy(struct ifstate *is) | |
133 { | |
39
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
134 dofree(is, sizeof(*is)); |
15 | 135 } |
136 | |
137 static | |
138 void | |
139 ifstate_push(struct place *p, bool startstate) | |
140 { | |
72 | 141 struct ifstate *newstate; |
142 | |
143 newstate = ifstate_create(ifstate, p, startstate); | |
144 if (!ifstate->curtrue) { | |
145 newstate->curtrue = false; | |
146 newstate->evertrue = true; | |
147 } | |
148 ifstate = newstate; | |
15 | 149 } |
150 | |
151 static | |
152 void | |
153 ifstate_pop(void) | |
154 { | |
155 struct ifstate *is; | |
156 | |
157 is = ifstate; | |
158 ifstate = ifstate->prev; | |
159 ifstate_destroy(is); | |
160 } | |
161 | |
162 static | |
163 void | |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
164 d_if(struct place *p, struct place *p2, char *line) |
15 | 165 { |
166 char *expr; | |
167 bool val; | |
16 | 168 struct place p3 = *p2; |
15 | 169 |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
170 uncomment(line); |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
171 expr = macroexpand(p2, line, strlen(line), true); |
16 | 172 val = eval(&p3, expr); |
15 | 173 ifstate_push(p, val); |
39
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
174 dostrfree(expr); |
15 | 175 } |
176 | |
177 static | |
178 void | |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
179 d_ifdef(struct place *p, struct place *p2, char *line) |
15 | 180 { |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
181 uncomment(line); |
15 | 182 oneword("#ifdef", p2, line); |
183 ifstate_push(p, macro_isdefined(line)); | |
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_ifndef(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("#ifndef", 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_elif(struct place *p, struct place *p2, char *line) |
15 | 198 { |
199 char *expr; | |
16 | 200 struct place p3 = *p2; |
15 | 201 |
202 if (ifstate->seenelse) { | |
203 complain(p, "#elif after #else"); | |
204 complain_fail(); | |
205 } | |
206 | |
207 if (ifstate->evertrue) { | |
208 ifstate->curtrue = false; | |
209 } else { | |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
210 uncomment(line); |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
211 expr = macroexpand(p2, line, strlen(line), true); |
16 | 212 ifstate->curtrue = eval(&p3, expr); |
15 | 213 ifstate->evertrue = ifstate->curtrue; |
39
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
214 dostrfree(expr); |
15 | 215 } |
216 } | |
217 | |
218 static | |
219 void | |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
220 d_else(struct place *p, struct place *p2, char *line) |
15 | 221 { |
222 if (ifstate->seenelse) { | |
223 complain(p, "Multiple #else directives in one conditional"); | |
224 complain_fail(); | |
225 } | |
226 | |
227 ifstate->curtrue = !ifstate->evertrue; | |
228 ifstate->evertrue = true; | |
229 ifstate->seenelse = true; | |
230 } | |
231 | |
232 static | |
233 void | |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
234 d_endif(struct place *p, struct place *p2, char *line) |
15 | 235 { |
236 if (ifstate->prev == NULL) { | |
237 complain(p, "Unmatched #endif"); | |
238 complain_fail(); | |
239 } else { | |
240 ifstate_pop(); | |
241 } | |
242 } | |
243 | |
244 //////////////////////////////////////////////////////////// | |
245 // macros | |
246 | |
247 static | |
248 void | |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
249 d_define(struct place *p, struct place *p2, char *line) |
15 | 250 { |
18 | 251 size_t pos, argpos; |
252 struct place p3, p4; | |
15 | 253 |
254 /* | |
255 * line may be: | |
256 * macro expansion | |
257 * macro(arg, arg, ...) expansion | |
258 */ | |
259 | |
260 pos = strcspn(line, " \t\f\v("); | |
261 if (line[pos] == '(') { | |
18 | 262 line[pos++] = '\0'; |
263 argpos = pos; | |
15 | 264 pos = pos + strcspn(line+pos, "()"); |
265 if (line[pos] == '(') { | |
266 p2->column += pos; | |
267 complain(p2, "Left parenthesis in macro parameters"); | |
268 complain_fail(); | |
269 return; | |
270 } | |
271 if (line[pos] != ')') { | |
272 p2->column += pos; | |
273 complain(p2, "Unclosed macro parameter list"); | |
274 complain_fail(); | |
275 return; | |
276 } | |
18 | 277 line[pos++] = '\0'; |
36
a489cc223483
Don't demand space after the macro argument parenthesis.
David A. Holland
parents:
30
diff
changeset
|
278 #if 0 |
15 | 279 if (!strchr(ws, line[pos])) { |
280 p2->column += pos; | |
281 complain(p2, "Trash after macro parameter list"); | |
282 complain_fail(); | |
283 return; | |
284 } | |
36
a489cc223483
Don't demand space after the macro argument parenthesis.
David A. Holland
parents:
30
diff
changeset
|
285 #endif |
15 | 286 } else if (line[pos] == '\0') { |
18 | 287 argpos = 0; |
15 | 288 } else { |
289 line[pos++] = '\0'; | |
18 | 290 argpos = 0; |
15 | 291 } |
292 | |
293 pos += strspn(line+pos, ws); | |
294 | |
295 p3 = *p2; | |
18 | 296 p3.column += argpos; |
297 | |
298 p4 = *p2; | |
299 p4.column += pos; | |
300 | |
301 if (argpos) { | |
302 macro_define_params(p2, line, &p3, | |
303 line + argpos, &p4, | |
304 line + pos); | |
305 } else { | |
306 macro_define_plain(p2, line, &p4, line + pos); | |
307 } | |
15 | 308 } |
309 | |
310 static | |
311 void | |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
312 d_undef(struct place *p, struct place *p2, char *line) |
15 | 313 { |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
314 uncomment(line); |
15 | 315 oneword("#undef", p2, line); |
316 macro_undef(line); | |
317 } | |
318 | |
319 //////////////////////////////////////////////////////////// | |
320 // includes | |
321 | |
322 static | |
323 bool | |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
324 tryinclude(struct place *p, char *line) |
15 | 325 { |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
326 size_t len; |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
327 |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
328 len = strlen(line); |
15 | 329 if (len > 2 && line[0] == '"' && line[len-1] == '"') { |
330 line[len-1] = '\0'; | |
331 file_readquote(p, line+1); | |
62
90c6052410ce
Don't truncate the candidate include path strings.
David A. Holland
parents:
49
diff
changeset
|
332 line[len-1] = '"'; |
15 | 333 return true; |
334 } | |
335 if (len > 2 && line[0] == '<' && line[len-1] == '>') { | |
336 line[len-1] = '\0'; | |
337 file_readbracket(p, line+1); | |
62
90c6052410ce
Don't truncate the candidate include path strings.
David A. Holland
parents:
49
diff
changeset
|
338 line[len-1] = '>'; |
15 | 339 return true; |
340 } | |
341 return false; | |
342 } | |
343 | |
344 static | |
345 void | |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
346 d_include(struct place *p, struct place *p2, char *line) |
15 | 347 { |
348 char *text; | |
349 | |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
350 uncomment(line); |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
351 if (tryinclude(p, line)) { |
15 | 352 return; |
353 } | |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
354 text = macroexpand(p2, line, strlen(line), false); |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
355 if (tryinclude(p, text)) { |
39
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
356 dostrfree(text); |
15 | 357 return; |
358 } | |
39
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
359 dostrfree(text); |
15 | 360 complain(p, "Illegal #include directive"); |
361 complain_fail(); | |
362 } | |
363 | |
364 static | |
365 void | |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
366 d_line(struct place *p, struct place *p2, char *line) |
15 | 367 { |
368 /* XXX */ | |
369 complain(p, "Sorry, no #line yet"); | |
370 } | |
371 | |
372 //////////////////////////////////////////////////////////// | |
373 // messages | |
374 | |
375 static | |
376 void | |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
377 d_warning(struct place *p, struct place *p2, char *line) |
15 | 378 { |
379 char *msg; | |
380 | |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
381 msg = macroexpand(p2, line, strlen(line), false); |
15 | 382 complain(p, "#warning: %s", msg); |
383 if (mode.werror) { | |
384 complain_fail(); | |
385 } | |
39
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
386 dostrfree(msg); |
15 | 387 } |
388 | |
389 static | |
390 void | |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
391 d_error(struct place *p, struct place *p2, char *line) |
15 | 392 { |
393 char *msg; | |
394 | |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
395 msg = macroexpand(p2, line, strlen(line), false); |
15 | 396 complain(p, "#error: %s", msg); |
397 complain_fail(); | |
39
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
398 dostrfree(msg); |
15 | 399 } |
400 | |
401 //////////////////////////////////////////////////////////// | |
402 // other | |
403 | |
404 static | |
405 void | |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
406 d_pragma(struct place *p, struct place *p2, char *line) |
15 | 407 { |
408 complain(p, "#pragma %s", line); | |
409 complain_fail(); | |
410 } | |
411 | |
412 //////////////////////////////////////////////////////////// | |
413 // directive table | |
414 | |
415 static const struct { | |
416 const char *name; | |
417 bool ifskip; | |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
418 void (*func)(struct place *, struct place *, char *line); |
15 | 419 } directives[] = { |
420 { "define", true, d_define }, | |
421 { "elif", false, d_elif }, | |
422 { "else", false, d_else }, | |
423 { "endif", false, d_endif }, | |
424 { "error", true, d_error }, | |
425 { "if", false, d_if }, | |
426 { "ifdef", false, d_ifdef }, | |
427 { "ifndef", false, d_ifndef }, | |
428 { "include", true, d_include }, | |
429 { "line", true, d_line }, | |
430 { "pragma", true, d_pragma }, | |
431 { "undef", true, d_undef }, | |
432 { "warning", true, d_warning }, | |
433 }; | |
434 static const unsigned numdirectives = HOWMANY(directives); | |
435 | |
436 static | |
437 void | |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
438 directive_gotdirective(struct place *p, char *line) |
15 | 439 { |
440 struct place p2; | |
441 size_t len, skip; | |
442 unsigned i; | |
443 | |
444 p2 = *p; | |
445 for (i=0; i<numdirectives; i++) { | |
446 len = strlen(directives[i].name); | |
447 if (!strncmp(line, directives[i].name, len) && | |
448 strchr(ws, line[len])) { | |
449 if (directives[i].ifskip && !ifstate->curtrue) { | |
450 return; | |
451 } | |
452 skip = len + strspn(line+len, ws); | |
453 p2.column += skip; | |
454 line += skip; | |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
455 |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
456 len = strlen(line); |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
457 len = notrailingws(line, len); |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
458 if (len < strlen(line)) { |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
459 line[len] = '\0'; |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
460 } |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
461 directives[i].func(p, &p2, line); |
15 | 462 return; |
463 } | |
464 } | |
465 skip = strcspn(line, ws); | |
466 complain(p, "Unknown directive #%.*s", (int)skip, line); | |
467 complain_fail(); | |
468 } | |
469 | |
49
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
470 /* |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
471 * If desired, warn about a nested comment. The comment begins at |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
472 * offset POS from the place P. |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
473 */ |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
474 static |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
475 void |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
476 warn_nestcomment(const struct place *p, size_t pos) |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
477 { |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
478 struct place p2; |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
479 |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
480 if (warns.nestcomment) { |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
481 p2 = *p; |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
482 p2.column += pos; |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
483 complain(p, "Warning: %c%c within comment", |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
484 '/', '*'); |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
485 if (mode.werror) { |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
486 complain_failed(); |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
487 } |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
488 } |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
489 } |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
490 |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
491 /* |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
492 * Check for comment delimiters in LINE. If a multi-line comment is |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
493 * continuing or ending, set ACOMM to its length. If a multi-line |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
494 * comment is starting, set BCOMM to its length. Set TEXT to the |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
495 * length of text that is not commented out, or that contains comments |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
496 * that both begin and end on this line. ACOMM + TEXT + BCOMM == LEN. |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
497 * |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
498 * Updates in_multiline_comment to the appropriate state for after |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
499 * this line is handled. |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
500 */ |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
501 static |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
502 size_t |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
503 directive_scancomments(const struct place *p, char *line, size_t len, |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
504 size_t *acomm, size_t *text, size_t *bcomm) |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
505 { |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
506 size_t pos; |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
507 size_t first_commentend; |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
508 size_t last_commentstart; |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
509 bool incomment; |
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 first_commentend = len; |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
512 last_commentstart = len; |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
513 incomment = in_multiline_comment; |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
514 for (pos = 0; pos+1 < len; pos++) { |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
515 if (line[pos] == '/' && line[pos+1] == '*') { |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
516 if (incomment) { |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
517 warn_nestcomment(p, pos); |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
518 } else { |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
519 incomment = true; |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
520 last_commentstart = pos; |
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 } else if (line[pos] == '*' && line[pos+1] == '/') { |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
523 if (incomment) { |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
524 incomment = false; |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
525 if (first_commentend == len) { |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
526 first_commentend = pos; |
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 last_commentstart = len; |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
529 } else { |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
530 /* stray end-comment; should we care? */ |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
531 } |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
532 } |
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 |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
535 if (in_multiline_comment && first_commentend < last_commentstart) { |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
536 /* multiline comment ends */ |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
537 /* first_commentend points to the star, adjust */ |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
538 *acomm = first_commentend + 2; |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
539 *text = len - *acomm; |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
540 } else if (in_multiline_comment) { |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
541 /* comment did not end, so another one cannot have started */ |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
542 assert(last_commentstart == len); |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
543 *acomm = len; |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
544 *text = 0; |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
545 } else { |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
546 *acomm = 0; |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
547 *text = len; |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
548 } |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
549 |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
550 *bcomm = len - last_commentstart; |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
551 *text -= *bcomm; |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
552 |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
553 in_multiline_comment = incomment; |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
554 return len; |
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 |
15 | 557 void |
558 directive_gotline(struct place *p, char *line, size_t len) | |
559 { | |
49
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
560 size_t acomm; /* length of comment ending on this line */ |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
561 size_t text; /* length of non-multi-line-comment text */ |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
562 size_t bcomm; /* length of comment beginning on this line */ |
15 | 563 size_t skip; |
564 | |
49
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
565 directive_scancomments(p, line, len, &acomm, &text, &bcomm); |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
566 |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
567 if (acomm > 0) { |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
568 if (mode.output_retain_comments && ifstate->curtrue) { |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
569 /* |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
570 * Do not expand the comment; send it straight |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
571 * to the output. This will cause it to appear |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
572 * first if we're partway through collecting a |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
573 * macro argument. Too bad. This isn't a |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
574 * standard mode anyway. |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
575 */ |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
576 output(p, line, acomm); |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
577 } |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
578 p->column += acomm; |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
579 } |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
580 |
66
f8507e5ed84c
Recognize directive lines only when the # is exactly in column 0.
David A. Holland
parents:
64
diff
changeset
|
581 /* check if we have a directive line (# exactly in column 0) */ |
f8507e5ed84c
Recognize directive lines only when the # is exactly in column 0.
David A. Holland
parents:
64
diff
changeset
|
582 if (acomm == 0 && line[0] == '#') { |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
583 char ch; |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
584 |
66
f8507e5ed84c
Recognize directive lines only when the # is exactly in column 0.
David A. Holland
parents:
64
diff
changeset
|
585 skip = 1 + strspn(line + 1, ws); |
49
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
586 assert(skip <= text); |
15 | 587 p->column += skip; |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
588 assert(line[len] == '\0'); |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
589 /* ensure null termination for directives */ |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
590 ch = line[text]; |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
591 if (ch != '\0') { |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
592 line[text] = '\0'; |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
593 } |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
594 directive_gotdirective(p, line+skip /*, length = text-skip */); |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
595 line[text] = ch; |
49
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
596 p->column += text-skip; |
15 | 597 } else if (ifstate->curtrue) { |
49
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
598 macro_sendline(p, line + acomm, text); |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
599 p->column += text; |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
600 } |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
601 |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
602 if (bcomm > 0) { |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
603 if (mode.output_retain_comments && ifstate->curtrue) { |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
604 output(p, line + acomm + text, bcomm); |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
605 } |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
606 p->column += bcomm; |
15 | 607 } |
608 } | |
609 | |
610 void | |
611 directive_goteof(struct place *p) | |
612 { | |
613 while (ifstate->prev != NULL) { | |
614 complain(p, "Missing #endif"); | |
615 complain(&ifstate->startplace, "...opened at this point"); | |
616 complain_failed(); | |
617 ifstate_pop(); | |
618 } | |
619 macro_sendeof(p); | |
620 } | |
621 | |
622 //////////////////////////////////////////////////////////// | |
623 // module initialization | |
624 | |
625 void | |
626 directive_init(void) | |
627 { | |
628 ifstate = ifstate_create(NULL, NULL, true); | |
629 } | |
630 | |
631 void | |
632 directive_cleanup(void) | |
633 { | |
634 assert(ifstate->prev == NULL); | |
635 ifstate_destroy(ifstate); | |
636 ifstate = NULL; | |
637 } |