Mesa (glsl2): glcpp: Additional fixes for not evaluating skipped #if/ #elif expressions.

Carl Worth cworth at kemper.freedesktop.org
Wed Aug 11 19:47:32 UTC 2010


Module: Mesa
Branch: glsl2
Commit: 48ba058e7a4b808271ca987b1553efd7e9792da9
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=48ba058e7a4b808271ca987b1553efd7e9792da9

Author: Carl Worth <cworth at cworth.org>
Date:   Wed Aug 11 12:43:44 2010 -0700

glcpp: Additional fixes for not evaluating skipped #if/#elif expressions.

This adds a couple of test cases to expand our coverage of invalid #if and
being skipped, (either by being nested inside an #if/#elif that evaluates to
zero or by being after an #if/#elif that evaluates to non-zero).

---

 src/glsl/glcpp/glcpp-parse.y                       |   60 +++++++++++++------
 src/glsl/glcpp/tests/075-elif-elif-undef.c         |    4 +
 .../glcpp/tests/075-elif-elif-undef.c.expected     |    5 ++
 src/glsl/glcpp/tests/076-elif-undef-nested.c       |    5 ++
 .../glcpp/tests/076-elif-undef-nested.c.expected   |    6 ++
 5 files changed, 61 insertions(+), 19 deletions(-)

diff --git a/src/glsl/glcpp/glcpp-parse.y b/src/glsl/glcpp/glcpp-parse.y
index a438357..643c449 100644
--- a/src/glsl/glcpp/glcpp-parse.y
+++ b/src/glsl/glcpp/glcpp-parse.y
@@ -223,14 +223,22 @@ control_line:
 		talloc_free ($2);
 	}
 |	HASH_IF conditional_tokens NEWLINE {
-		/* If we're skipping to the next #elif/#else case or to #endif,
-		 * don't bother expanding or parsing the expression.
-		 */
-		if (parser->skip_stack != NULL && parser->skip_stack->type != SKIP_NO_SKIP) {
+		/* Be careful to only evaluate the 'if' expression if
+		 * we are not skipping. When we are skipping, we
+		 * simply push a new 0-valued 'if' onto the skip
+		 * stack.
+		 *
+		 * This avoids generating diagnostics for invalid
+		 * expressions that are being skipped. */
+		if (parser->skip_stack == NULL ||
+		    parser->skip_stack->type == SKIP_NO_SKIP)
+		{
+			_glcpp_parser_expand_if (parser, IF_EXPANDED, $2);
+		}	
+		else
+		{
 			_glcpp_parser_skip_stack_push_if (parser, & @1, 0);
 			parser->skip_stack->type = SKIP_TO_ENDIF;
-		} else {
-			_glcpp_parser_expand_if (parser, IF_EXPANDED, $2);
 		}
 	}
 |	HASH_IFDEF IDENTIFIER junk NEWLINE {
@@ -244,24 +252,38 @@ control_line:
 		_glcpp_parser_skip_stack_push_if (parser, & @1, macro == NULL);
 	}
 |	HASH_ELIF conditional_tokens NEWLINE {
-		/* If we just finished a non-skipped #if/#ifdef/#ifndef block,
-		 * don't bother expanding or parsing the expression.
-		 */
-		if (parser->skip_stack != NULL && parser->skip_stack->type == SKIP_NO_SKIP)
-			parser->skip_stack->type = SKIP_TO_ENDIF;
-		else
+		/* Be careful to only evaluate the 'elif' expression
+		 * if we are not skipping. When we are skipping, we
+		 * simply change to a 0-valued 'elif' on the skip
+		 * stack.
+		 *
+		 * This avoids generating diagnostics for invalid
+		 * expressions that are being skipped. */
+		if (parser->skip_stack &&
+		    parser->skip_stack->type == SKIP_TO_ELSE)
+		{
 			_glcpp_parser_expand_if (parser, ELIF_EXPANDED, $2);
+		}
+		else
+		{
+			_glcpp_parser_skip_stack_change_if (parser, & @1,
+							    "elif", 0);
+		}
 	}
 |	HASH_ELIF NEWLINE {
-		/* #elif without an expression results in a warning if the
-		 * condition doesn't matter (we just handled #if 1 or such)
-		 * but an error otherwise. */
-		if (parser->skip_stack != NULL && parser->skip_stack->type == SKIP_NO_SKIP) {
-			parser->skip_stack->type = SKIP_TO_ENDIF;
-			glcpp_warning(& @1, parser, "ignoring illegal #elif without expression");
-		} else {
+		/* #elif without an expression is an error unless we
+		 * are skipping. */
+		if (parser->skip_stack &&
+		    parser->skip_stack->type == SKIP_TO_ELSE)
+		{
 			glcpp_error(& @1, parser, "#elif needs an expression");
 		}
+		else
+		{
+			_glcpp_parser_skip_stack_change_if (parser, & @1,
+							    "elif", 0);
+			glcpp_warning(& @1, parser, "ignoring illegal #elif without expression");
+		}
 	}
 |	HASH_ELSE NEWLINE {
 		_glcpp_parser_skip_stack_change_if (parser, & @1, "else", 1);
diff --git a/src/glsl/glcpp/tests/075-elif-elif-undef.c b/src/glsl/glcpp/tests/075-elif-elif-undef.c
new file mode 100644
index 0000000..264bc4f
--- /dev/null
+++ b/src/glsl/glcpp/tests/075-elif-elif-undef.c
@@ -0,0 +1,4 @@
+#ifndef UNDEF
+#elif UNDEF < 0
+#elif UNDEF == 3
+#endif
diff --git a/src/glsl/glcpp/tests/075-elif-elif-undef.c.expected b/src/glsl/glcpp/tests/075-elif-elif-undef.c.expected
new file mode 100644
index 0000000..3f2ff2d
--- /dev/null
+++ b/src/glsl/glcpp/tests/075-elif-elif-undef.c.expected
@@ -0,0 +1,5 @@
+
+
+
+
+
diff --git a/src/glsl/glcpp/tests/076-elif-undef-nested.c b/src/glsl/glcpp/tests/076-elif-undef-nested.c
new file mode 100644
index 0000000..ebd550e
--- /dev/null
+++ b/src/glsl/glcpp/tests/076-elif-undef-nested.c
@@ -0,0 +1,5 @@
+#ifdef UNDEF
+#if UNDEF == 4
+#elif UNDEF == 5
+#endif
+#endif
diff --git a/src/glsl/glcpp/tests/076-elif-undef-nested.c.expected b/src/glsl/glcpp/tests/076-elif-undef-nested.c.expected
new file mode 100644
index 0000000..6fb66a5
--- /dev/null
+++ b/src/glsl/glcpp/tests/076-elif-undef-nested.c.expected
@@ -0,0 +1,6 @@
+
+
+
+
+
+




More information about the mesa-commit mailing list