Mercurial > ~dholland > hg > tradcpp > index.cgi
comparison directive.c @ 82:05a94332f08b
In #if/#elif, prune comments *after* macro expansion.
Also prune comments again in #include if we get as far as trying macro
expansion.
author | David A. Holland |
---|---|
date | Mon, 10 Jun 2013 21:32:47 -0400 (2013-06-11) |
parents | 27c9aafcaca1 |
children | 3e505c16b0b0 |
comparison
equal
deleted
inserted
replaced
81:27c9aafcaca1 | 82:05a94332f08b |
---|---|
173 d_if(struct place *p, struct place *p2, char *line) | 173 d_if(struct place *p, struct place *p2, char *line) |
174 { | 174 { |
175 char *expr; | 175 char *expr; |
176 bool val; | 176 bool val; |
177 struct place p3 = *p2; | 177 struct place p3 = *p2; |
178 | 178 size_t oldlen; |
179 uncomment(line); | 179 |
180 expr = macroexpand(p2, line, strlen(line), true); | 180 expr = macroexpand(p2, line, strlen(line), true); |
181 | |
182 oldlen = strlen(expr); | |
183 uncomment(expr); | |
184 /* trim to fit, so the malloc debugging won't complain */ | |
185 expr = dorealloc(expr, oldlen + 1, strlen(expr) + 1); | |
186 | |
181 val = eval(&p3, expr); | 187 val = eval(&p3, expr); |
182 ifstate_push(p, val); | 188 ifstate_push(p, val); |
183 dostrfree(expr); | 189 dostrfree(expr); |
184 } | 190 } |
185 | 191 |
205 void | 211 void |
206 d_elif(struct place *p, struct place *p2, char *line) | 212 d_elif(struct place *p, struct place *p2, char *line) |
207 { | 213 { |
208 char *expr; | 214 char *expr; |
209 struct place p3 = *p2; | 215 struct place p3 = *p2; |
216 size_t oldlen; | |
210 | 217 |
211 if (ifstate->seenelse) { | 218 if (ifstate->seenelse) { |
212 complain(p, "#elif after #else"); | 219 complain(p, "#elif after #else"); |
213 complain_fail(); | 220 complain_fail(); |
214 } | 221 } |
215 | 222 |
216 if (ifstate->evertrue) { | 223 if (ifstate->evertrue) { |
217 ifstate->curtrue = false; | 224 ifstate->curtrue = false; |
218 } else { | 225 } else { |
219 uncomment(line); | |
220 expr = macroexpand(p2, line, strlen(line), true); | 226 expr = macroexpand(p2, line, strlen(line), true); |
227 | |
228 oldlen = strlen(expr); | |
229 uncomment(expr); | |
230 /* trim to fit, so the malloc debugging won't complain */ | |
231 expr = dorealloc(expr, oldlen + 1, strlen(expr) + 1); | |
232 | |
221 ifstate->curtrue = eval(&p3, expr); | 233 ifstate->curtrue = eval(&p3, expr); |
222 ifstate->evertrue = ifstate->curtrue; | 234 ifstate->evertrue = ifstate->curtrue; |
223 dostrfree(expr); | 235 dostrfree(expr); |
224 } | 236 } |
225 } | 237 } |
353 static | 365 static |
354 void | 366 void |
355 d_include(struct place *p, struct place *p2, char *line) | 367 d_include(struct place *p, struct place *p2, char *line) |
356 { | 368 { |
357 char *text; | 369 char *text; |
370 size_t oldlen; | |
358 | 371 |
359 uncomment(line); | 372 uncomment(line); |
360 if (tryinclude(p, line)) { | 373 if (tryinclude(p, line)) { |
361 return; | 374 return; |
362 } | 375 } |
363 text = macroexpand(p2, line, strlen(line), false); | 376 text = macroexpand(p2, line, strlen(line), false); |
377 | |
378 oldlen = strlen(text); | |
379 uncomment(text); | |
380 /* trim to fit, so the malloc debugging won't complain */ | |
381 text = dorealloc(text, oldlen + 1, strlen(text) + 1); | |
382 | |
364 if (tryinclude(p, text)) { | 383 if (tryinclude(p, text)) { |
365 dostrfree(text); | 384 dostrfree(text); |
366 return; | 385 return; |
367 } | 386 } |
368 dostrfree(text); | 387 dostrfree(text); |