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