[Mesa-dev] [PATCH] glsl: Properly lex extra tokens when handling # directives.

Carl Worth cworth at cworth.org
Wed Jun 25 11:39:45 PDT 2014

Kenneth Graunke <kenneth at whitecape.org> writes:
> That was the strangest bug I've tracked down in a while.

A truly heroic debug effort, Ken!

I recently ran into a bug or two in glcpp which were also running afoul
of the annoying default lex rule.

I'm about to publish my latest series of glcpp patches, but below is a
preview of one of them that tries to prevent that annoyingly silent rule
From ever firing.

It's not a perfect solution because it requires maintenance to keep the
list of start conditions up to date. (If we add a new start condition
and don't update this rule, then the default rule can still take effect
in that start condition.)

But we'll probably want something like this in glsl_lexer.ll as well.

I've added a note to myself to fix this along with the other bugs I'm
addressing in my current work to fix up glcpp.


From c431eb1586ea6b724c09df17beab191ceefddc83 Mon Sep 17 00:00:00 2001
From: Carl Worth <cworth at cworth.org>
Date: Wed, 18 Jun 2014 17:03:19 -0700
Subject: [PATCH] glsl/glcpp: Add a catch-all rule for unexpected characters.

In some of the recent glcpp bug-fixing, we found that glcpp was emitting
unrecognized characters from the input source file to stdout, and dropping
them from the source passed onto the compiler proper.

This was obviously confusing, and totally undesired.

The bogus behavior comes from an implicit default rule in flex, which is
that any unmatched character is implicitly matched and printed to stdout.

To avoid this implicit matching and printing, here we add an explicit
catch-all rule. If this rule ever matches it prints an internal compiler
error. The correct response for anyt such error is fixing glcpp to handle
the unexpected character in the correct way.
 src/glsl/glcpp/glcpp-lex.l | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/src/glsl/glcpp/glcpp-lex.l b/src/glsl/glcpp/glcpp-lex.l
index e4065c6..45b113b 100644
--- a/src/glsl/glcpp/glcpp-lex.l
+++ b/src/glsl/glcpp/glcpp-lex.l
@@ -374,6 +374,15 @@ HEXADECIMAL_INTEGER	0[xX][0-9a-fA-F]+[uU]?
 	return NEWLINE;
+	/* This is a catch-all to avoid the annoying default flex action which
+	 * matches any character and prints it. If any input ever matches this
+	 * rule, then we have made a mistake above and need to fix one or more
+	 * of the preceding patterns to match that input. */
+	glcpp_error(yylloc, yyextra, "Internal compiler error: Unexpected character: %s", yytext);
 	/* We don't actually use the UNREACHABLE start condition. We
 	only have this action here so that we can pretend to call some
 	generated functions, (to avoid "defined but not used"

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 818 bytes
Desc: not available
URL: <http://lists.freedesktop.org/archives/mesa-dev/attachments/20140625/e3367362/attachment.sig>

More information about the mesa-dev mailing list