Mercurial > ~dholland > hg > tradcpp > index.cgi
annotate directive.c @ 208:f6f2c7a9d7d8
also recognize __riscv128__, may as well.
author | David A. Holland |
---|---|
date | Mon, 21 Jan 2019 21:41:38 -0500 |
parents | 3a25180d3a5c |
children |
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 <stdlib.h> | |
32 #include <string.h> | |
173 | 33 #include <limits.h> |
165 | 34 #include <errno.h> |
15 | 35 |
183
4c3375895c6e
Don't use <stdbool.h> unless __STDC__ is large enough.
David A. Holland
parents:
176
diff
changeset
|
36 #include "bool.h" |
15 | 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') { | |
203
3a25180d3a5c
Abort on line numbering or column numbering overflow.
David A. Holland
parents:
199
diff
changeset
|
117 place_addcolumns(p2, pos); |
15 | 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 { |
199
1d2bad7151f9
Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents:
183
diff
changeset
|
182 bool doprint; |
15 | 183 char *expr; |
184 bool val; | |
16 | 185 struct place p3 = *p2; |
82
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
186 size_t oldlen; |
15 | 187 |
199
1d2bad7151f9
Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents:
183
diff
changeset
|
188 doprint = ifstate->curtrue; |
1d2bad7151f9
Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents:
183
diff
changeset
|
189 |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
190 expr = macroexpand(p2, line, strlen(line), true); |
82
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
191 |
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
192 oldlen = strlen(expr); |
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
193 uncomment(expr); |
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
194 /* 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
|
195 expr = dorealloc(expr, oldlen + 1, strlen(expr) + 1); |
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
196 |
127 | 197 if (ifstate->curtrue) { |
198 val = eval(&p3, expr); | |
199 } else { | |
200 val = 0; | |
201 } | |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
202 ifstate_push(&lp->current, val); |
39
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
203 dostrfree(expr); |
199
1d2bad7151f9
Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents:
183
diff
changeset
|
204 |
1d2bad7151f9
Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents:
183
diff
changeset
|
205 if (doprint) { |
1d2bad7151f9
Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents:
183
diff
changeset
|
206 debuglog(&lp->current, "#if: %s", |
1d2bad7151f9
Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents:
183
diff
changeset
|
207 ifstate->curtrue ? "taken" : "not taken"); |
1d2bad7151f9
Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents:
183
diff
changeset
|
208 } |
15 | 209 } |
210 | |
211 static | |
212 void | |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
213 d_ifdef(struct lineplace *lp, struct place *p2, char *line) |
15 | 214 { |
199
1d2bad7151f9
Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents:
183
diff
changeset
|
215 bool doprint; |
1d2bad7151f9
Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents:
183
diff
changeset
|
216 |
1d2bad7151f9
Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents:
183
diff
changeset
|
217 doprint = ifstate->curtrue; |
1d2bad7151f9
Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents:
183
diff
changeset
|
218 |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
219 uncomment(line); |
15 | 220 oneword("#ifdef", p2, line); |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
221 ifstate_push(&lp->current, macro_isdefined(line)); |
199
1d2bad7151f9
Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents:
183
diff
changeset
|
222 |
1d2bad7151f9
Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents:
183
diff
changeset
|
223 if (doprint) { |
1d2bad7151f9
Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents:
183
diff
changeset
|
224 debuglog(&lp->current, "#ifdef %s: %s", |
1d2bad7151f9
Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents:
183
diff
changeset
|
225 line, ifstate->curtrue ? "taken" : "not taken"); |
1d2bad7151f9
Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents:
183
diff
changeset
|
226 } |
15 | 227 } |
228 | |
229 static | |
230 void | |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
231 d_ifndef(struct lineplace *lp, struct place *p2, char *line) |
15 | 232 { |
199
1d2bad7151f9
Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents:
183
diff
changeset
|
233 bool doprint; |
1d2bad7151f9
Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents:
183
diff
changeset
|
234 |
1d2bad7151f9
Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents:
183
diff
changeset
|
235 doprint = ifstate->curtrue; |
1d2bad7151f9
Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents:
183
diff
changeset
|
236 |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
237 uncomment(line); |
15 | 238 oneword("#ifndef", p2, line); |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
239 ifstate_push(&lp->current, !macro_isdefined(line)); |
199
1d2bad7151f9
Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents:
183
diff
changeset
|
240 |
1d2bad7151f9
Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents:
183
diff
changeset
|
241 if (doprint) { |
1d2bad7151f9
Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents:
183
diff
changeset
|
242 debuglog(&lp->current, "#ifndef %s: %s", |
1d2bad7151f9
Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents:
183
diff
changeset
|
243 line, ifstate->curtrue ? "taken" : "not taken"); |
1d2bad7151f9
Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents:
183
diff
changeset
|
244 } |
15 | 245 } |
246 | |
247 static | |
248 void | |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
249 d_elif(struct lineplace *lp, struct place *p2, char *line) |
15 | 250 { |
199
1d2bad7151f9
Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents:
183
diff
changeset
|
251 bool doprint; |
15 | 252 char *expr; |
16 | 253 struct place p3 = *p2; |
82
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
254 size_t oldlen; |
15 | 255 |
256 if (ifstate->seenelse) { | |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
257 complain(&lp->current, "#elif after #else"); |
15 | 258 complain_fail(); |
259 } | |
260 | |
199
1d2bad7151f9
Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents:
183
diff
changeset
|
261 doprint = ifstate->curtrue; |
1d2bad7151f9
Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents:
183
diff
changeset
|
262 |
15 | 263 if (ifstate->evertrue) { |
264 ifstate->curtrue = false; | |
265 } else { | |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
266 expr = macroexpand(p2, line, strlen(line), true); |
82
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
267 |
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
268 oldlen = strlen(expr); |
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
269 uncomment(expr); |
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
270 /* 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
|
271 expr = dorealloc(expr, oldlen + 1, strlen(expr) + 1); |
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
272 |
16 | 273 ifstate->curtrue = eval(&p3, expr); |
15 | 274 ifstate->evertrue = ifstate->curtrue; |
39
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
275 dostrfree(expr); |
15 | 276 } |
199
1d2bad7151f9
Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents:
183
diff
changeset
|
277 |
1d2bad7151f9
Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents:
183
diff
changeset
|
278 if (doprint) { |
1d2bad7151f9
Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents:
183
diff
changeset
|
279 debuglog2(&lp->current, &ifstate->startplace, "#elif: %s", |
1d2bad7151f9
Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents:
183
diff
changeset
|
280 ifstate->curtrue ? "taken" : "not taken"); |
1d2bad7151f9
Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents:
183
diff
changeset
|
281 } |
15 | 282 } |
283 | |
284 static | |
285 void | |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
286 d_else(struct lineplace *lp, struct place *p2, char *line) |
15 | 287 { |
199
1d2bad7151f9
Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents:
183
diff
changeset
|
288 bool doprint; |
1d2bad7151f9
Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents:
183
diff
changeset
|
289 |
103 | 290 (void)p2; |
291 (void)line; | |
292 | |
15 | 293 if (ifstate->seenelse) { |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
294 complain(&lp->current, |
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
295 "Multiple #else directives in one conditional"); |
15 | 296 complain_fail(); |
297 } | |
298 | |
199
1d2bad7151f9
Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents:
183
diff
changeset
|
299 doprint = ifstate->curtrue; |
1d2bad7151f9
Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents:
183
diff
changeset
|
300 |
15 | 301 ifstate->curtrue = !ifstate->evertrue; |
302 ifstate->evertrue = true; | |
303 ifstate->seenelse = true; | |
199
1d2bad7151f9
Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents:
183
diff
changeset
|
304 |
1d2bad7151f9
Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents:
183
diff
changeset
|
305 if (doprint) { |
1d2bad7151f9
Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents:
183
diff
changeset
|
306 debuglog2(&lp->current, &ifstate->startplace, "#else: %s", |
1d2bad7151f9
Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents:
183
diff
changeset
|
307 ifstate->curtrue ? "taken" : "not taken"); |
1d2bad7151f9
Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents:
183
diff
changeset
|
308 } |
15 | 309 } |
310 | |
311 static | |
312 void | |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
313 d_endif(struct lineplace *lp, struct place *p2, char *line) |
15 | 314 { |
103 | 315 (void)p2; |
316 (void)line; | |
317 | |
15 | 318 if (ifstate->prev == NULL) { |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
319 complain(&lp->current, "Unmatched #endif"); |
15 | 320 complain_fail(); |
321 } else { | |
199
1d2bad7151f9
Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents:
183
diff
changeset
|
322 debuglog2(&lp->current, &ifstate->startplace, "#endif"); |
15 | 323 ifstate_pop(); |
324 } | |
325 } | |
326 | |
327 //////////////////////////////////////////////////////////// | |
328 // macros | |
329 | |
330 static | |
331 void | |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
332 d_define(struct lineplace *lp, struct place *p2, char *line) |
15 | 333 { |
18 | 334 size_t pos, argpos; |
335 struct place p3, p4; | |
15 | 336 |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
337 (void)lp; |
103 | 338 |
15 | 339 /* |
340 * line may be: | |
341 * macro expansion | |
342 * macro(arg, arg, ...) expansion | |
343 */ | |
344 | |
345 pos = strcspn(line, " \t\f\v("); | |
346 if (line[pos] == '(') { | |
18 | 347 line[pos++] = '\0'; |
348 argpos = pos; | |
15 | 349 pos = pos + strcspn(line+pos, "()"); |
350 if (line[pos] == '(') { | |
203
3a25180d3a5c
Abort on line numbering or column numbering overflow.
David A. Holland
parents:
199
diff
changeset
|
351 place_addcolumns(p2, pos); |
15 | 352 complain(p2, "Left parenthesis in macro parameters"); |
353 complain_fail(); | |
354 return; | |
355 } | |
356 if (line[pos] != ')') { | |
203
3a25180d3a5c
Abort on line numbering or column numbering overflow.
David A. Holland
parents:
199
diff
changeset
|
357 place_addcolumns(p2, pos); |
15 | 358 complain(p2, "Unclosed macro parameter list"); |
359 complain_fail(); | |
360 return; | |
361 } | |
18 | 362 line[pos++] = '\0'; |
36
a489cc223483
Don't demand space after the macro argument parenthesis.
David A. Holland
parents:
30
diff
changeset
|
363 #if 0 |
15 | 364 if (!strchr(ws, line[pos])) { |
365 p2->column += pos; | |
366 complain(p2, "Trash after macro parameter list"); | |
367 complain_fail(); | |
368 return; | |
369 } | |
36
a489cc223483
Don't demand space after the macro argument parenthesis.
David A. Holland
parents:
30
diff
changeset
|
370 #endif |
15 | 371 } else if (line[pos] == '\0') { |
18 | 372 argpos = 0; |
15 | 373 } else { |
374 line[pos++] = '\0'; | |
18 | 375 argpos = 0; |
15 | 376 } |
377 | |
378 pos += strspn(line+pos, ws); | |
379 | |
380 p3 = *p2; | |
203
3a25180d3a5c
Abort on line numbering or column numbering overflow.
David A. Holland
parents:
199
diff
changeset
|
381 place_addcolumns(&p3, argpos); |
18 | 382 |
383 p4 = *p2; | |
203
3a25180d3a5c
Abort on line numbering or column numbering overflow.
David A. Holland
parents:
199
diff
changeset
|
384 place_addcolumns(&p4, pos); |
18 | 385 |
386 if (argpos) { | |
199
1d2bad7151f9
Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents:
183
diff
changeset
|
387 debuglog(&lp->current, "Defining %s()", line); |
18 | 388 macro_define_params(p2, line, &p3, |
389 line + argpos, &p4, | |
390 line + pos); | |
391 } else { | |
199
1d2bad7151f9
Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents:
183
diff
changeset
|
392 debuglog(&lp->current, "Defining %s", line); |
18 | 393 macro_define_plain(p2, line, &p4, line + pos); |
394 } | |
15 | 395 } |
396 | |
397 static | |
398 void | |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
399 d_undef(struct lineplace *lp, struct place *p2, char *line) |
15 | 400 { |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
401 (void)lp; |
103 | 402 |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
403 uncomment(line); |
15 | 404 oneword("#undef", p2, line); |
199
1d2bad7151f9
Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents:
183
diff
changeset
|
405 debuglog(&lp->current, "Undef %s", line); |
15 | 406 macro_undef(line); |
407 } | |
408 | |
409 //////////////////////////////////////////////////////////// | |
410 // includes | |
411 | |
412 static | |
413 bool | |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
414 tryinclude(struct place *p, char *line) |
15 | 415 { |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
416 size_t len; |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
417 |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
418 len = strlen(line); |
15 | 419 if (len > 2 && line[0] == '"' && line[len-1] == '"') { |
420 line[len-1] = '\0'; | |
199
1d2bad7151f9
Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents:
183
diff
changeset
|
421 debuglog(p, "Entering include file \"%s\"", line+1); |
15 | 422 file_readquote(p, line+1); |
199
1d2bad7151f9
Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents:
183
diff
changeset
|
423 debuglog(p, "Leaving include file \"%s\"", line+1); |
62
90c6052410ce
Don't truncate the candidate include path strings.
David A. Holland
parents:
49
diff
changeset
|
424 line[len-1] = '"'; |
15 | 425 return true; |
426 } | |
427 if (len > 2 && line[0] == '<' && line[len-1] == '>') { | |
428 line[len-1] = '\0'; | |
199
1d2bad7151f9
Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents:
183
diff
changeset
|
429 debuglog(p, "Entering include file <%s>", line+1); |
15 | 430 file_readbracket(p, line+1); |
199
1d2bad7151f9
Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents:
183
diff
changeset
|
431 debuglog(p, "Leaving include file <%s>", line+1); |
62
90c6052410ce
Don't truncate the candidate include path strings.
David A. Holland
parents:
49
diff
changeset
|
432 line[len-1] = '>'; |
15 | 433 return true; |
434 } | |
435 return false; | |
436 } | |
437 | |
438 static | |
439 void | |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
440 d_include(struct lineplace *lp, struct place *p2, char *line) |
15 | 441 { |
442 char *text; | |
82
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
443 size_t oldlen; |
15 | 444 |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
445 uncomment(line); |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
446 if (tryinclude(&lp->current, line)) { |
15 | 447 return; |
448 } | |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
449 text = macroexpand(p2, line, strlen(line), false); |
82
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
450 |
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
451 oldlen = strlen(text); |
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
452 uncomment(text); |
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
453 /* 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
|
454 text = dorealloc(text, oldlen + 1, strlen(text) + 1); |
05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents:
81
diff
changeset
|
455 |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
456 if (tryinclude(&lp->current, text)) { |
39
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
457 dostrfree(text); |
15 | 458 return; |
459 } | |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
460 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
|
461 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
|
462 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
|
463 dostrfree(text); |
15 | 464 complain_fail(); |
465 } | |
466 | |
467 static | |
468 void | |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
469 d_line(struct lineplace *lp, struct place *p2, char *line) |
15 | 470 { |
160 | 471 char *text; |
472 size_t oldlen; | |
165 | 473 unsigned long val; |
474 char *moretext; | |
475 size_t moretextlen; | |
476 char *filename; | |
160 | 477 |
478 text = macroexpand(p2, line, strlen(line), true); | |
479 | |
480 oldlen = strlen(text); | |
481 uncomment(text); | |
482 /* trim to fit, so the malloc debugging won't complain */ | |
483 text = dorealloc(text, oldlen + 1, strlen(text) + 1); | |
103 | 484 |
165 | 485 /* |
486 * What we should have here: either 1234 "file.c", | |
487 * or just 1234. | |
488 */ | |
489 | |
490 errno = 0; | |
491 val = strtoul(text, &moretext, 10); | |
492 if (errno) { | |
203
3a25180d3a5c
Abort on line numbering or column numbering overflow.
David A. Holland
parents:
199
diff
changeset
|
493 complain(&lp->current, |
3a25180d3a5c
Abort on line numbering or column numbering overflow.
David A. Holland
parents:
199
diff
changeset
|
494 "Invalid line number in #line directive"); |
165 | 495 goto fail; |
160 | 496 } |
165 | 497 #if UINT_MAX < ULONG_MAX |
498 if (val > UINT_MAX) { | |
499 complain(&lp->current, | |
500 "Line number in #line directive too large"); | |
501 goto fail; | |
160 | 502 } |
165 | 503 #endif |
504 moretext += strspn(moretext, ws); | |
505 moretextlen = strlen(moretext); | |
203
3a25180d3a5c
Abort on line numbering or column numbering overflow.
David A. Holland
parents:
199
diff
changeset
|
506 place_addcolumns(&lp->current, moretext - text); |
165 | 507 |
508 if (moretextlen > 2 && | |
509 moretext[0] == '"' && moretext[moretextlen-1] == '"') { | |
510 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
|
511 place_changefile(&lp->nextline, filename); |
160 | 512 dostrfree(filename); |
513 } | |
165 | 514 else if (moretextlen > 0) { |
515 complain(&lp->current, | |
516 "Invalid file name in #line directive"); | |
517 goto fail; | |
518 } | |
519 | |
520 lp->nextline.line = val; | |
160 | 521 dostrfree(text); |
522 return; | |
523 | |
165 | 524 fail: |
525 complain(&lp->current, "Before macro expansion: #line %s", line); | |
526 complain(&lp->current, "After macro expansion: #line %s", text); | |
527 complain_fail(); | |
160 | 528 dostrfree(text); |
15 | 529 } |
530 | |
531 //////////////////////////////////////////////////////////// | |
532 // messages | |
533 | |
534 static | |
535 void | |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
536 d_warning(struct lineplace *lp, struct place *p2, char *line) |
15 | 537 { |
538 char *msg; | |
539 | |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
540 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
|
541 complain(&lp->current, "#warning: %s", msg); |
15 | 542 if (mode.werror) { |
543 complain_fail(); | |
544 } | |
39
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
545 dostrfree(msg); |
15 | 546 } |
547 | |
548 static | |
549 void | |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
550 d_error(struct lineplace *lp, struct place *p2, char *line) |
15 | 551 { |
552 char *msg; | |
553 | |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
554 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
|
555 complain(&lp->current, "#error: %s", msg); |
15 | 556 complain_fail(); |
39
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
557 dostrfree(msg); |
15 | 558 } |
559 | |
560 //////////////////////////////////////////////////////////// | |
561 // other | |
562 | |
563 static | |
564 void | |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
565 d_pragma(struct lineplace *lp, struct place *p2, char *line) |
15 | 566 { |
103 | 567 (void)p2; |
568 | |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
569 complain(&lp->current, "#pragma %s", line); |
15 | 570 complain_fail(); |
571 } | |
572 | |
573 //////////////////////////////////////////////////////////// | |
574 // directive table | |
575 | |
576 static const struct { | |
577 const char *name; | |
578 bool ifskip; | |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
579 void (*func)(struct lineplace *, struct place *, char *line); |
15 | 580 } directives[] = { |
581 { "define", true, d_define }, | |
582 { "elif", false, d_elif }, | |
583 { "else", false, d_else }, | |
584 { "endif", false, d_endif }, | |
585 { "error", true, d_error }, | |
586 { "if", false, d_if }, | |
587 { "ifdef", false, d_ifdef }, | |
588 { "ifndef", false, d_ifndef }, | |
589 { "include", true, d_include }, | |
590 { "line", true, d_line }, | |
591 { "pragma", true, d_pragma }, | |
592 { "undef", true, d_undef }, | |
593 { "warning", true, d_warning }, | |
594 }; | |
595 static const unsigned numdirectives = HOWMANY(directives); | |
596 | |
597 static | |
598 void | |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
599 directive_gotdirective(struct lineplace *lp, char *line) |
15 | 600 { |
601 struct place p2; | |
602 size_t len, skip; | |
603 unsigned i; | |
604 | |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
605 p2 = lp->current; |
15 | 606 for (i=0; i<numdirectives; i++) { |
607 len = strlen(directives[i].name); | |
608 if (!strncmp(line, directives[i].name, len) && | |
609 strchr(ws, line[len])) { | |
610 if (directives[i].ifskip && !ifstate->curtrue) { | |
611 return; | |
612 } | |
613 skip = len + strspn(line+len, ws); | |
203
3a25180d3a5c
Abort on line numbering or column numbering overflow.
David A. Holland
parents:
199
diff
changeset
|
614 place_addcolumns(&p2, skip); |
15 | 615 line += skip; |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
616 |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
617 len = strlen(line); |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
618 len = notrailingws(line, len); |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
619 if (len < strlen(line)) { |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
620 line[len] = '\0'; |
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
621 } |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
622 directives[i].func(lp, &p2, line); |
15 | 623 return; |
624 } | |
625 } | |
83
3e505c16b0b0
Accept # by itself, including with a comment after it.
David A. Holland
parents:
82
diff
changeset
|
626 /* 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
|
627 uncomment(line); |
3e505c16b0b0
Accept # by itself, including with a comment after it.
David A. Holland
parents:
82
diff
changeset
|
628 if (line[0] == '\0') { |
3e505c16b0b0
Accept # by itself, including with a comment after it.
David A. Holland
parents:
82
diff
changeset
|
629 return; |
3e505c16b0b0
Accept # by itself, including with a comment after it.
David A. Holland
parents:
82
diff
changeset
|
630 } |
3e505c16b0b0
Accept # by itself, including with a comment after it.
David A. Holland
parents:
82
diff
changeset
|
631 |
15 | 632 skip = strcspn(line, ws); |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
633 complain(&lp->current, "Unknown directive #%.*s", (int)skip, line); |
15 | 634 complain_fail(); |
635 } | |
636 | |
49
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
637 /* |
77
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
638 * 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
|
639 */ |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
640 static |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
641 size_t |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
642 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
|
643 { |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
644 size_t pos; |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
645 bool incomment; |
77
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
646 struct place p2; |
49
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
647 |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
648 p2 = lp->current; |
77
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
649 incomment = 0; |
49
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
650 for (pos = 0; pos+1 < len; pos++) { |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
651 if (line[pos] == '/' && line[pos+1] == '*') { |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
652 if (incomment) { |
77
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
653 complain(&p2, "Warning: %c%c within comment", |
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
654 '/', '*'); |
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
655 if (mode.werror) { |
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
656 complain_failed(); |
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
657 } |
49
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
658 } else { |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
659 incomment = true; |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
660 } |
109 | 661 pos++; |
49
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
662 } else if (line[pos] == '*' && line[pos+1] == '/') { |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
663 if (incomment) { |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
664 incomment = false; |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
665 } else { |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
666 /* stray end-comment; should we care? */ |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
667 } |
109 | 668 pos++; |
49
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
669 } |
77
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
670 if (line[pos] == '\n') { |
203
3a25180d3a5c
Abort on line numbering or column numbering overflow.
David A. Holland
parents:
199
diff
changeset
|
671 place_addlines(&p2, 1); |
77
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
672 p2.column = 0; |
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
673 } else { |
203
3a25180d3a5c
Abort on line numbering or column numbering overflow.
David A. Holland
parents:
199
diff
changeset
|
674 place_addcolumns(&p2, 1); |
77
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
675 } |
49
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
676 } |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
677 |
77
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
678 /* 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
|
679 assert(!incomment); |
49
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
680 return len; |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
681 } |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
682 |
15 | 683 void |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
684 directive_gotline(struct lineplace *lp, char *line, size_t len) |
15 | 685 { |
686 size_t skip; | |
687 | |
77
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
688 if (warns.nestcomment) { |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
689 directive_scancomments(lp, line, len); |
49
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
690 } |
8a204d153398
Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents:
39
diff
changeset
|
691 |
66
f8507e5ed84c
Recognize directive lines only when the # is exactly in column 0.
David A. Holland
parents:
64
diff
changeset
|
692 /* check if we have a directive line (# exactly in column 0) */ |
175 | 693 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
|
694 skip = 1 + strspn(line + 1, ws); |
77
123168887da8
Clean out old not-really-working nested comment handling.
David A. Holland
parents:
72
diff
changeset
|
695 assert(skip <= len); |
203
3a25180d3a5c
Abort on line numbering or column numbering overflow.
David A. Holland
parents:
199
diff
changeset
|
696 place_addcolumns(&lp->current, skip); |
64
f50b4ea6cbfe
Prune single-line comments from (most) directive lines.
David A. Holland
parents:
62
diff
changeset
|
697 assert(line[len] == '\0'); |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
698 directive_gotdirective(lp, line+skip /*, length = len-skip */); |
203
3a25180d3a5c
Abort on line numbering or column numbering overflow.
David A. Holland
parents:
199
diff
changeset
|
699 place_addcolumns(&lp->current, len-skip); |
15 | 700 } else if (ifstate->curtrue) { |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
128
diff
changeset
|
701 macro_sendline(&lp->current, line, len); |
203
3a25180d3a5c
Abort on line numbering or column numbering overflow.
David A. Holland
parents:
199
diff
changeset
|
702 place_addcolumns(&lp->current, len); |
15 | 703 } |
704 } | |
705 | |
706 void | |
707 directive_goteof(struct place *p) | |
708 { | |
709 while (ifstate->prev != NULL) { | |
710 complain(p, "Missing #endif"); | |
711 complain(&ifstate->startplace, "...opened at this point"); | |
712 complain_failed(); | |
713 ifstate_pop(); | |
714 } | |
715 macro_sendeof(p); | |
716 } | |
717 | |
718 //////////////////////////////////////////////////////////// | |
719 // module initialization | |
720 | |
721 void | |
722 directive_init(void) | |
723 { | |
724 ifstate = ifstate_create(NULL, NULL, true); | |
725 } | |
726 | |
727 void | |
728 directive_cleanup(void) | |
729 { | |
730 assert(ifstate->prev == NULL); | |
731 ifstate_destroy(ifstate); | |
732 ifstate = NULL; | |
733 } |