changeset 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
parents 27c9aafcaca1
children 3e505c16b0b0
files directive.c
diffstat 1 files changed, 21 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/directive.c	Mon Jun 10 21:30:20 2013 -0400
+++ b/directive.c	Mon Jun 10 21:32:47 2013 -0400
@@ -175,9 +175,15 @@
 	char *expr;
 	bool val;
 	struct place p3 = *p2;
+	size_t oldlen;
 
-	uncomment(line);
 	expr = macroexpand(p2, line, strlen(line), true);
+
+	oldlen = strlen(expr);
+	uncomment(expr);
+	/* trim to fit, so the malloc debugging won't complain */
+	expr = dorealloc(expr, oldlen + 1, strlen(expr) + 1);
+
 	val = eval(&p3, expr);
 	ifstate_push(p, val);
 	dostrfree(expr);
@@ -207,6 +213,7 @@
 {
 	char *expr;
 	struct place p3 = *p2;
+	size_t oldlen;
 
 	if (ifstate->seenelse) {
 		complain(p, "#elif after #else");
@@ -216,8 +223,13 @@
 	if (ifstate->evertrue) {
 		ifstate->curtrue = false;
 	} else {
-		uncomment(line);
 		expr = macroexpand(p2, line, strlen(line), true);
+
+		oldlen = strlen(expr);
+		uncomment(expr);
+		/* trim to fit, so the malloc debugging won't complain */
+		expr = dorealloc(expr, oldlen + 1, strlen(expr) + 1);
+
 		ifstate->curtrue = eval(&p3, expr);
 		ifstate->evertrue = ifstate->curtrue;
 		dostrfree(expr);
@@ -355,12 +367,19 @@
 d_include(struct place *p, struct place *p2, char *line)
 {
 	char *text;
+	size_t oldlen;
 
 	uncomment(line);
 	if (tryinclude(p, line)) {
 		return;
 	}
 	text = macroexpand(p2, line, strlen(line), false);
+
+	oldlen = strlen(text);
+	uncomment(text);
+	/* trim to fit, so the malloc debugging won't complain */
+	text = dorealloc(text, oldlen + 1, strlen(text) + 1);
+
 	if (tryinclude(p, text)) {
 		dostrfree(text);
 		return;