Mercurial > ~dholland > hg > tradcpp > index.cgi
annotate directive.c @ 182:f7814226906c
very minor changes from openbsd
author | David A. Holland |
---|---|
date | Fri, 12 Jun 2015 03:21:36 -0400 |
parents | a2f047301c15 |
children | 4c3375895c6e |
rev | line source |
---|---|
30 | 1 /*- |
99
60184aa42604
add 2013 to copyrights where it seems warranted
David A. Holland
parents:
90
diff
changeset
|
2 * Copyright (c) 2010, 2013 The NetBSD Foundation, Inc. |
30 | 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> | |
173 | 34 #include <limits.h> |
165 | 35 #include <errno.h> |
15 | 36 |
37 #include "utils.h" | |
38 #include "mode.h" | |
39 #include "place.h" | |
40 #include "files.h" | |
41 #include "directive.h" | |
42 #include "macro.h" | |
43 #include "eval.h" | |
49
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
44 #include "output.h" |
15 | 45 |
46 struct ifstate { | |
47 struct ifstate *prev; | |
48 struct place startplace; | |
49 bool curtrue; | |
50 bool evertrue; | |
51 bool seenelse; | |
52 }; | |
53 | |
54 static struct ifstate *ifstate; | |
55 | |
56 //////////////////////////////////////////////////////////// | |
57 // common parsing bits | |
58 | |
59 static | |
60 void | |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
61 uncomment(char *buf) |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
62 { |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
63 char *s, *t, *u = NULL; |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
64 bool incomment = false; |
81
27c9aafcaca1
Don't recognize comments within double-quote strings.
David A. Holland
parents:
77
diff
changeset
|
65 bool inesc = false; |
27c9aafcaca1
Don't recognize comments within double-quote strings.
David A. Holland
parents:
77
diff
changeset
|
66 bool inquote = false; |
128
1cda505ddc78
Don't expand macros within character constants.
David A. Holland
parents:
127
diff
changeset
|
67 char quote = '\0'; |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
68 |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
69 for (s = t = buf; *s; s++) { |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
70 if (incomment) { |
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 s++; |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
73 incomment = false; |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
74 } |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
75 } else { |
81
27c9aafcaca1
Don't recognize comments within double-quote strings.
David A. Holland
parents:
77
diff
changeset
|
76 if (!inquote && s[0] == '/' && s[1] == '*') { |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
77 incomment = true; |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
78 } else { |
81
27c9aafcaca1
Don't recognize comments within double-quote strings.
David A. Holland
parents:
77
diff
changeset
|
79 if (inesc) { |
27c9aafcaca1
Don't recognize comments within double-quote strings.
David A. Holland
parents:
77
diff
changeset
|
80 inesc = false; |
27c9aafcaca1
Don't recognize comments within double-quote strings.
David A. Holland
parents:
77
diff
changeset
|
81 } else if (s[0] == '\\') { |
27c9aafcaca1
Don't recognize comments within double-quote strings.
David A. Holland
parents:
77
diff
changeset
|
82 inesc = true; |
128
1cda505ddc78
Don't expand macros within character constants.
David A. Holland
parents:
127
diff
changeset
|
83 } else if (!inquote && |
1cda505ddc78
Don't expand macros within character constants.
David A. Holland
parents:
127
diff
changeset
|
84 (s[0] == '"' || s[0] == '\'')) { |
1cda505ddc78
Don't expand macros within character constants.
David A. Holland
parents:
127
diff
changeset
|
85 inquote = true; |
1cda505ddc78
Don't expand macros within character constants.
David A. Holland
parents:
127
diff
changeset
|
86 quote = s[0]; |
1cda505ddc78
Don't expand macros within character constants.
David A. Holland
parents:
127
diff
changeset
|
87 } else if (inquote && s[0] == quote) { |
1cda505ddc78
Don't expand macros within character constants.
David A. Holland
parents:
127
diff
changeset
|
88 inquote = false; |
81
27c9aafcaca1
Don't recognize comments within double-quote strings.
David A. Holland
parents:
77
diff
changeset
|
89 } |
27c9aafcaca1
Don't recognize comments within double-quote strings.
David A. Holland
parents:
77
diff
changeset
|
90 |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
91 if (t != s) { |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
92 *t = *s; |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
93 } |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
94 if (!strchr(ws, *t)) { |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
95 u = t; |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
96 } |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
97 t++; |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
98 } |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
99 } |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
100 } |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
101 if (u) { |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
102 /* end string after last non-whitespace char */ |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
103 u[1] = '\0'; |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
104 } else { |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
105 *t = '\0'; |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
106 } |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
107 } |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
108 |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
109 static |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
110 void |
15 | 111 oneword(const char *what, struct place *p2, char *line) |
112 { | |
113 size_t pos; | |
114 | |
115 pos = strcspn(line, ws); | |
116 if (line[pos] != '\0') { | |
117 p2->column += pos; | |
118 complain(p2, "Garbage after %s argument", what); | |
119 complain_fail(); | |
120 line[pos] = '\0'; | |
121 } | |
122 } | |
123 | |
124 //////////////////////////////////////////////////////////// | |
125 // if handling | |
126 | |
127 static | |
128 struct ifstate * | |
129 ifstate_create(struct ifstate *prev, struct place *p, bool startstate) | |
130 { | |
131 struct ifstate *is; | |
132 | |
133 is = domalloc(sizeof(*is)); | |
134 is->prev = prev; | |
135 if (p != NULL) { | |
136 is->startplace = *p; | |
137 } else { | |
138 place_setbuiltin(&is->startplace, 1); | |
139 } | |
140 is->curtrue = startstate; | |
141 is->evertrue = is->curtrue; | |
142 is->seenelse = false; | |
143 return is; | |
144 } | |
145 | |
146 static | |
147 void | |
148 ifstate_destroy(struct ifstate *is) | |
149 { | |
39
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
150 dofree(is, sizeof(*is)); |
15 | 151 } |
152 | |
153 static | |
154 void | |
155 ifstate_push(struct place *p, bool startstate) | |
156 { | |
72 | 157 struct ifstate *newstate; |
158 | |
159 newstate = ifstate_create(ifstate, p, startstate); | |
160 if (!ifstate->curtrue) { | |
161 newstate->curtrue = false; | |
162 newstate->evertrue = true; | |
163 } | |
164 ifstate = newstate; | |
15 | 165 } |
166 | |
167 static | |
168 void | |
169 ifstate_pop(void) | |
170 { | |
171 struct ifstate *is; | |
172 | |
173 is = ifstate; | |
174 ifstate = ifstate->prev; | |
175 ifstate_destroy(is); | |
176 } | |
177 | |
178 static | |
179 void | |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
180 d_if(struct lineplace *lp, struct place *p2, char *line) |
15 | 181 { |
182 char *expr; | |
183 bool val; | |
16 | 184 struct place p3 = *p2; |
82
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
185 size_t oldlen; |
15 | 186 |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
187 expr = macroexpand(p2, line, strlen(line), true); |
82
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
188 |
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
189 oldlen = strlen(expr); |
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
190 uncomment(expr); |
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
191 /* trim to fit, so the malloc debugging won't complain */ |
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
192 expr = dorealloc(expr, oldlen + 1, strlen(expr) + 1); |
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
193 |
127 | 194 if (ifstate->curtrue) { |
195 val = eval(&p3, expr); | |
196 } else { | |
197 val = 0; | |
198 } | |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
199 ifstate_push(&lp->current, val); |
39
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
200 dostrfree(expr); |
15 | 201 } |
202 | |
203 static | |
204 void | |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
205 d_ifdef(struct lineplace *lp, struct place *p2, char *line) |
15 | 206 { |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
207 uncomment(line); |
15 | 208 oneword("#ifdef", p2, line); |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
209 ifstate_push(&lp->current, macro_isdefined(line)); |
15 | 210 } |
211 | |
212 static | |
213 void | |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
214 d_ifndef(struct lineplace *lp, struct place *p2, char *line) |
15 | 215 { |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
216 uncomment(line); |
15 | 217 oneword("#ifndef", p2, line); |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
218 ifstate_push(&lp->current, !macro_isdefined(line)); |
15 | 219 } |
220 | |
221 static | |
222 void | |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
223 d_elif(struct lineplace *lp, struct place *p2, char *line) |
15 | 224 { |
225 char *expr; | |
16 | 226 struct place p3 = *p2; |
82
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
227 size_t oldlen; |
15 | 228 |
229 if (ifstate->seenelse) { | |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
230 complain(&lp->current, "#elif after #else"); |
15 | 231 complain_fail(); |
232 } | |
233 | |
234 if (ifstate->evertrue) { | |
235 ifstate->curtrue = false; | |
236 } else { | |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
237 expr = macroexpand(p2, line, strlen(line), true); |
82
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
238 |
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
239 oldlen = strlen(expr); |
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
240 uncomment(expr); |
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
241 /* trim to fit, so the malloc debugging won't complain */ |
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
242 expr = dorealloc(expr, oldlen + 1, strlen(expr) + 1); |
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
243 |
16 | 244 ifstate->curtrue = eval(&p3, expr); |
15 | 245 ifstate->evertrue = ifstate->curtrue; |
39
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
246 dostrfree(expr); |
15 | 247 } |
248 } | |
249 | |
250 static | |
251 void | |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
252 d_else(struct lineplace *lp, struct place *p2, char *line) |
15 | 253 { |
103 | 254 (void)p2; |
255 (void)line; | |
256 | |
15 | 257 if (ifstate->seenelse) { |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
258 complain(&lp->current, |
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
259 "Multiple #else directives in one conditional"); |
15 | 260 complain_fail(); |
261 } | |
262 | |
263 ifstate->curtrue = !ifstate->evertrue; | |
264 ifstate->evertrue = true; | |
265 ifstate->seenelse = true; | |
266 } | |
267 | |
268 static | |
269 void | |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
270 d_endif(struct lineplace *lp, struct place *p2, char *line) |
15 | 271 { |
103 | 272 (void)p2; |
273 (void)line; | |
274 | |
15 | 275 if (ifstate->prev == NULL) { |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
276 complain(&lp->current, "Unmatched #endif"); |
15 | 277 complain_fail(); |
278 } else { | |
279 ifstate_pop(); | |
280 } | |
281 } | |
282 | |
283 //////////////////////////////////////////////////////////// | |
284 // macros | |
285 | |
286 static | |
287 void | |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
288 d_define(struct lineplace *lp, struct place *p2, char *line) |
15 | 289 { |
18 | 290 size_t pos, argpos; |
291 struct place p3, p4; | |
15 | 292 |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
293 (void)lp; |
103 | 294 |
15 | 295 /* |
296 * line may be: | |
297 * macro expansion | |
298 * macro(arg, arg, ...) expansion | |
299 */ | |
300 | |
301 pos = strcspn(line, " \t\f\v("); | |
302 if (line[pos] == '(') { | |
18 | 303 line[pos++] = '\0'; |
304 argpos = pos; | |
15 | 305 pos = pos + strcspn(line+pos, "()"); |
306 if (line[pos] == '(') { | |
307 p2->column += pos; | |
308 complain(p2, "Left parenthesis in macro parameters"); | |
309 complain_fail(); | |
310 return; | |
311 } | |
312 if (line[pos] != ')') { | |
313 p2->column += pos; | |
314 complain(p2, "Unclosed macro parameter list"); | |
315 complain_fail(); | |
316 return; | |
317 } | |
18 | 318 line[pos++] = '\0'; |
36
a489cc223483
Don't demand space after the macro argument parenthesis.
David A. Holland
parents:
30
diff
changeset
|
319 #if 0 |
15 | 320 if (!strchr(ws, line[pos])) { |
321 p2->column += pos; | |
322 complain(p2, "Trash after macro parameter list"); | |
323 complain_fail(); | |
324 return; | |
325 } | |
36
a489cc223483
Don't demand space after the macro argument parenthesis.
David A. Holland
parents:
30
diff
changeset
|
326 #endif |
15 | 327 } else if (line[pos] == '\0') { |
18 | 328 argpos = 0; |
15 | 329 } else { |
330 line[pos++] = '\0'; | |
18 | 331 argpos = 0; |
15 | 332 } |
333 | |
334 pos += strspn(line+pos, ws); | |
335 | |
336 p3 = *p2; | |
18 | 337 p3.column += argpos; |
338 | |
339 p4 = *p2; | |
340 p4.column += pos; | |
341 | |
342 if (argpos) { | |
343 macro_define_params(p2, line, &p3, | |
344 line + argpos, &p4, | |
345 line + pos); | |
346 } else { | |
347 macro_define_plain(p2, line, &p4, line + pos); | |
348 } | |
15 | 349 } |
350 | |
351 static | |
352 void | |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
353 d_undef(struct lineplace *lp, struct place *p2, char *line) |
15 | 354 { |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
355 (void)lp; |
103 | 356 |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
357 uncomment(line); |
15 | 358 oneword("#undef", p2, line); |
359 macro_undef(line); | |
360 } | |
361 | |
362 //////////////////////////////////////////////////////////// | |
363 // includes | |
364 | |
365 static | |
366 bool | |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
367 tryinclude(struct place *p, char *line) |
15 | 368 { |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
369 size_t len; |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
370 |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
371 len = strlen(line); |
15 | 372 if (len > 2 && line[0] == '"' && line[len-1] == '"') { |
373 line[len-1] = '\0'; | |
374 file_readquote(p, line+1); | |
62
90c6052410ce
Don't truncate the candidate include path strings.
David A. Holland
parents:
49
diff
changeset
|
375 line[len-1] = '"'; |
15 | 376 return true; |
377 } | |
378 if (len > 2 && line[0] == '<' && line[len-1] == '>') { | |
379 line[len-1] = '\0'; | |
380 file_readbracket(p, line+1); | |
62
90c6052410ce
Don't truncate the candidate include path strings.
David A. Holland
parents:
49
diff
changeset
|
381 line[len-1] = '>'; |
15 | 382 return true; |
383 } | |
384 return false; | |
385 } | |
386 | |
387 static | |
388 void | |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
389 d_include(struct lineplace *lp, struct place *p2, char *line) |
15 | 390 { |
391 char *text; | |
82
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
392 size_t oldlen; |
15 | 393 |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
394 uncomment(line); |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
395 if (tryinclude(&lp->current, line)) { |
15 | 396 return; |
397 } | |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
398 text = macroexpand(p2, line, strlen(line), false); |
82
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
399 |
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
400 oldlen = strlen(text); |
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
401 uncomment(text); |
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
402 /* trim to fit, so the malloc debugging won't complain */ |
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
403 text = dorealloc(text, oldlen + 1, strlen(text) + 1); |
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
404 |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
405 if (tryinclude(&lp->current, text)) { |
39
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
406 dostrfree(text); |
15 | 407 return; |
408 } | |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
409 complain(&lp->current, "Illegal #include directive"); |
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
410 complain(&lp->current, "Before macro expansion: #include %s", line); |
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
411 complain(&lp->current, "After macro expansion: #include %s", text); |
39
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
412 dostrfree(text); |
15 | 413 complain_fail(); |
414 } | |
415 | |
416 static | |
417 void | |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
418 d_line(struct lineplace *lp, struct place *p2, char *line) |
15 | 419 { |
160 | 420 char *text; |
421 size_t oldlen; | |
165 | 422 unsigned long val; |
423 char *moretext; | |
424 size_t moretextlen; | |
425 char *filename; | |
160 | 426 |
427 text = macroexpand(p2, line, strlen(line), true); | |
428 | |
429 oldlen = strlen(text); | |
430 uncomment(text); | |
431 /* trim to fit, so the malloc debugging won't complain */ | |
432 text = dorealloc(text, oldlen + 1, strlen(text) + 1); | |
103 | 433 |
165 | 434 /* |
435 * What we should have here: either 1234 "file.c", | |
436 * or just 1234. | |
437 */ | |
438 | |
439 errno = 0; | |
440 val = strtoul(text, &moretext, 10); | |
441 if (errno) { | |
442 complain(&lp->current, "No line number in #line directive"); | |
443 goto fail; | |
160 | 444 } |
165 | 445 #if UINT_MAX < ULONG_MAX |
446 if (val > UINT_MAX) { | |
447 complain(&lp->current, | |
448 "Line number in #line directive too large"); | |
449 goto fail; | |
160 | 450 } |
165 | 451 #endif |
452 moretext += strspn(moretext, ws); | |
453 moretextlen = strlen(moretext); | |
454 lp->current.column += (moretext - text); | |
455 | |
456 if (moretextlen > 2 && | |
457 moretext[0] == '"' && moretext[moretextlen-1] == '"') { | |
458 filename = dostrndup(moretext+1, moretextlen-2); | |
176
a2f047301c15
Replace Joerg's place_setfile with something that at least sort of works.
David A. Holland
parents:
175
diff
changeset
|
459 place_changefile(&lp->nextline, filename); |
160 | 460 dostrfree(filename); |
461 } | |
165 | 462 else if (moretextlen > 0) { |
463 complain(&lp->current, | |
464 "Invalid file name in #line directive"); | |
465 goto fail; | |
466 } | |
467 | |
468 lp->nextline.line = val; | |
160 | 469 dostrfree(text); |
470 return; | |
471 | |
165 | 472 fail: |
473 complain(&lp->current, "Before macro expansion: #line %s", line); | |
474 complain(&lp->current, "After macro expansion: #line %s", text); | |
475 complain_fail(); | |
160 | 476 dostrfree(text); |
15 | 477 } |
478 | |
479 //////////////////////////////////////////////////////////// | |
480 // messages | |
481 | |
482 static | |
483 void | |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
484 d_warning(struct lineplace *lp, struct place *p2, char *line) |
15 | 485 { |
486 char *msg; | |
487 | |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
488 msg = macroexpand(p2, line, strlen(line), false); |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
489 complain(&lp->current, "#warning: %s", msg); |
15 | 490 if (mode.werror) { |
491 complain_fail(); | |
492 } | |
39
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
493 dostrfree(msg); |
15 | 494 } |
495 | |
496 static | |
497 void | |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
498 d_error(struct lineplace *lp, struct place *p2, char *line) |
15 | 499 { |
500 char *msg; | |
501 | |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
502 msg = macroexpand(p2, line, strlen(line), false); |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
503 complain(&lp->current, "#error: %s", msg); |
15 | 504 complain_fail(); |
39
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
505 dostrfree(msg); |
15 | 506 } |
507 | |
508 //////////////////////////////////////////////////////////// | |
509 // other | |
510 | |
511 static | |
512 void | |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
513 d_pragma(struct lineplace *lp, struct place *p2, char *line) |
15 | 514 { |
103 | 515 (void)p2; |
516 | |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
517 complain(&lp->current, "#pragma %s", line); |
15 | 518 complain_fail(); |
519 } | |
520 | |
521 //////////////////////////////////////////////////////////// | |
522 // directive table | |
523 | |
524 static const struct { | |
525 const char *name; | |
526 bool ifskip; | |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
527 void (*func)(struct lineplace *, struct place *, char *line); |
15 | 528 } directives[] = { |
529 { "define", true, d_define }, | |
530 { "elif", false, d_elif }, | |
531 { "else", false, d_else }, | |
532 { "endif", false, d_endif }, | |
533 { "error", true, d_error }, | |
534 { "if", false, d_if }, | |
535 { "ifdef", false, d_ifdef }, | |
536 { "ifndef", false, d_ifndef }, | |
537 { "include", true, d_include }, | |
538 { "line", true, d_line }, | |
539 { "pragma", true, d_pragma }, | |
540 { "undef", true, d_undef }, | |
541 { "warning", true, d_warning }, | |
542 }; | |
543 static const unsigned numdirectives = HOWMANY(directives); | |
544 | |
545 static | |
546 void | |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
547 directive_gotdirective(struct lineplace *lp, char *line) |
15 | 548 { |
549 struct place p2; | |
550 size_t len, skip; | |
551 unsigned i; | |
552 | |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
553 p2 = lp->current; |
15 | 554 for (i=0; i<numdirectives; i++) { |
555 len = strlen(directives[i].name); | |
556 if (!strncmp(line, directives[i].name, len) && | |
557 strchr(ws, line[len])) { | |
558 if (directives[i].ifskip && !ifstate->curtrue) { | |
559 return; | |
560 } | |
561 skip = len + strspn(line+len, ws); | |
562 p2.column += skip; | |
563 line += skip; | |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
564 |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
565 len = strlen(line); |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
566 len = notrailingws(line, len); |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
567 if (len < strlen(line)) { |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
568 line[len] = '\0'; |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
569 } |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
570 directives[i].func(lp, &p2, line); |
15 | 571 return; |
572 } | |
573 } | |
83
3e505c16b0b0
Accept # by itself, including with a comment after it.
David A. Holland
parents:
82
diff
changeset
|
574 /* ugh. allow # by itself, including with a comment after it */ |
3e505c16b0b0
Accept # by itself, including with a comment after it.
David A. Holland
parents:
82
diff
changeset
|
575 uncomment(line); |
3e505c16b0b0
Accept # by itself, including with a comment after it.
David A. Holland
parents:
82
diff
changeset
|
576 if (line[0] == '\0') { |
3e505c16b0b0
Accept # by itself, including with a comment after it.
David A. Holland
parents:
82
diff
changeset
|
577 return; |
3e505c16b0b0
Accept # by itself, including with a comment after it.
David A. Holland
parents:
82
diff
changeset
|
578 } |
3e505c16b0b0
Accept # by itself, including with a comment after it.
David A. Holland
parents:
82
diff
changeset
|
579 |
15 | 580 skip = strcspn(line, ws); |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
581 complain(&lp->current, "Unknown directive #%.*s", (int)skip, line); |
15 | 582 complain_fail(); |
583 } | |
584 | |
49
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
585 /* |
77
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
586 * 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
|
587 */ |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
588 static |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
589 size_t |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
590 directive_scancomments(const struct lineplace *lp, char *line, size_t len) |
49
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
591 { |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
592 size_t pos; |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
593 bool incomment; |
77
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
594 struct place p2; |
49
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
595 |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
596 p2 = lp->current; |
77
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
597 incomment = 0; |
49
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
598 for (pos = 0; pos+1 < len; pos++) { |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
599 if (line[pos] == '/' && line[pos+1] == '*') { |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
600 if (incomment) { |
77
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
601 complain(&p2, "Warning: %c%c within comment", |
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
602 '/', '*'); |
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
603 if (mode.werror) { |
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
604 complain_failed(); |
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
605 } |
49
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
606 } else { |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
607 incomment = true; |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
608 } |
109 | 609 pos++; |
49
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
610 } else if (line[pos] == '*' && line[pos+1] == '/') { |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
611 if (incomment) { |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
612 incomment = false; |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
613 } else { |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
614 /* stray end-comment; should we care? */ |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
615 } |
109 | 616 pos++; |
49
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
617 } |
77
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
618 if (line[pos] == '\n') { |
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
619 p2.line++; |
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
620 p2.column = 0; |
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
621 } else { |
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
622 p2.column++; |
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
623 } |
49
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
624 } |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
625 |
77
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
626 /* 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
|
627 assert(!incomment); |
49
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
628 return len; |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
629 } |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
630 |
15 | 631 void |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
632 directive_gotline(struct lineplace *lp, char *line, size_t len) |
15 | 633 { |
634 size_t skip; | |
635 | |
77
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
636 if (warns.nestcomment) { |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
637 directive_scancomments(lp, line, len); |
49
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
638 } |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
639 |
66
f8507e5ed84c
Recognize directive lines only when the # is exactly in column 0.
David A. Holland
parents:
64
diff
changeset
|
640 /* check if we have a directive line (# exactly in column 0) */ |
175 | 641 if (len > 0 && line[0] == '#') { |
66
f8507e5ed84c
Recognize directive lines only when the # is exactly in column 0.
David A. Holland
parents:
64
diff
changeset
|
642 skip = 1 + strspn(line + 1, ws); |
77
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
643 assert(skip <= len); |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
644 lp->current.column += skip; |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
645 assert(line[len] == '\0'); |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
646 directive_gotdirective(lp, line+skip /*, length = len-skip */); |
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
647 lp->current.column += len-skip; |
15 | 648 } else if (ifstate->curtrue) { |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
649 macro_sendline(&lp->current, line, len); |
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
650 lp->current.column += len; |
15 | 651 } |
652 } | |
653 | |
654 void | |
655 directive_goteof(struct place *p) | |
656 { | |
657 while (ifstate->prev != NULL) { | |
658 complain(p, "Missing #endif"); | |
659 complain(&ifstate->startplace, "...opened at this point"); | |
660 complain_failed(); | |
661 ifstate_pop(); | |
662 } | |
663 macro_sendeof(p); | |
664 } | |
665 | |
666 //////////////////////////////////////////////////////////// | |
667 // module initialization | |
668 | |
669 void | |
670 directive_init(void) | |
671 { | |
672 ifstate = ifstate_create(NULL, NULL, true); | |
673 } | |
674 | |
675 void | |
676 directive_cleanup(void) | |
677 { | |
678 assert(ifstate->prev == NULL); | |
679 ifstate_destroy(ifstate); | |
680 ifstate = NULL; | |
681 } |