Mesa (master): glsl/glcpp: Add explicit error for " #define without macro name"

Ian Romanick idr at kemper.freedesktop.org
Thu Aug 7 23:08:46 UTC 2014


Module: Mesa
Branch: master
Commit: a196ab1f8aacf657e591a0a15c80c3f37b9496b0
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=a196ab1f8aacf657e591a0a15c80c3f37b9496b0

Author: Carl Worth <cworth at cworth.org>
Date:   Tue Jul  1 17:40:28 2014 -0700

glsl/glcpp: Add explicit error for "#define without macro name"

Previously, glcpp would emit an error like this if <EOF> happened to occur
immediately after the "#define", but in general would just get confused,
(leading to un-helpful error messages).

To fix things to generate a clean error message, we do a few things:

	1. Don't require horizontal whitespace immediately after #define

	2. Add a production for the error case, (DEFINE_TOKEN followed
	   immediately by a NEWLINE token).

	3. Make the lexer reset to the <INITIAL> state after every NEWLINE.

This 3rd point prevents the lexer from getting so confused and generating
further spurious errors in the file because it was stuck in the <DEFINE> start
condition.

We also drop the similar error message from the <EOF> rule since the
newly-added rule will have already printed the error message.

Reviewed-by: Ian Romanick <ian.d.romanick at intel.com>

---

 src/glsl/glcpp/glcpp-lex.l                                     |    6 +++---
 src/glsl/glcpp/glcpp-parse.y                                   |    3 +++
 src/glsl/glcpp/tests/132-eof-without-newline-define.c.expected |    3 +--
 3 files changed, 7 insertions(+), 5 deletions(-)

diff --git a/src/glsl/glcpp/glcpp-lex.l b/src/glsl/glcpp/glcpp-lex.l
index c126850..6e197d1 100644
--- a/src/glsl/glcpp/glcpp-lex.l
+++ b/src/glsl/glcpp/glcpp-lex.l
@@ -371,7 +371,7 @@ HEXADECIMAL_INTEGER	0[xX][0-9a-fA-F]+[uU]?
 	 *	* Anything else, (not an identifier, not a comment,
 	 *	  and not whitespace). This will generate an error.
 	 */
-<HASH>define{HSPACE}+ {
+<HASH>define{HSPACE}* {
 	if (! parser->skipping) {
 		BEGIN DEFINE;
 		yyextra->space_tokens = 0;
@@ -520,6 +520,8 @@ HEXADECIMAL_INTEGER	0[xX][0-9a-fA-F]+[uU]?
 <*>[\r\n] {
 	if (parser->commented_newlines) {
 		BEGIN NEWLINE_CATCHUP;
+	} else {
+		BEGIN INITIAL;
 	}
 	yyextra->space_tokens = 1;
 	yyextra->lexing_directive = 0;
@@ -531,8 +533,6 @@ HEXADECIMAL_INTEGER	0[xX][0-9a-fA-F]+[uU]?
 <INITIAL,COMMENT,DEFINE,HASH><<EOF>> {
 	if (YY_START == COMMENT)
 		glcpp_error(yylloc, yyextra, "Unterminated comment");
-	if (YY_START == DEFINE)
-		glcpp_error(yylloc, yyextra, "#define without macro name");
 	BEGIN DONE; /* Don't keep matching this rule forever. */
 	yyextra->lexing_directive = 0;
 	if (! parser->last_token_was_newline)
diff --git a/src/glsl/glcpp/glcpp-parse.y b/src/glsl/glcpp/glcpp-parse.y
index e2e8aca..25da2a5 100644
--- a/src/glsl/glcpp/glcpp-parse.y
+++ b/src/glsl/glcpp/glcpp-parse.y
@@ -424,6 +424,9 @@ control_line_error:
 	HASH_TOKEN ERROR_TOKEN NEWLINE {
 		glcpp_error(& @1, parser, "#%s", $2);
 	}
+|	HASH_TOKEN DEFINE_TOKEN NEWLINE {
+		glcpp_error (& @1, parser, "#define without macro name");
+	}
 |	HASH_TOKEN GARBAGE pp_tokens NEWLINE  {
 		glcpp_error (& @1, parser, "Illegal non-directive after #");
 	}
diff --git a/src/glsl/glcpp/tests/132-eof-without-newline-define.c.expected b/src/glsl/glcpp/tests/132-eof-without-newline-define.c.expected
index a3ace0f..341e5e2 100644
--- a/src/glsl/glcpp/tests/132-eof-without-newline-define.c.expected
+++ b/src/glsl/glcpp/tests/132-eof-without-newline-define.c.expected
@@ -1,2 +1 @@
-0:1(2): preprocessor error: #define without macro name
-0:1(2): preprocessor error: syntax error, unexpected NEWLINE, expecting FUNC_IDENTIFIER or OBJ_IDENTIFIER
+0:1(1): preprocessor error: #define without macro name




More information about the mesa-commit mailing list