[Mesa-dev] [PATCH] glcpp: error on multiple #else directives
Erik Faye-Lund
kusmabite at gmail.com
Mon Sep 23 14:02:28 PDT 2013
On Mon, Sep 23, 2013 at 10:35 PM, Erik Faye-Lund <kusmabite at gmail.com> wrote:
> The preprocessor currently eats multiple #else directives
> int the same #if(def) ... #endif block. While I haven't been able
> to find anything that explicitly disallows it, it's nonsensical
> and should probably not be allowed.
>
> Add checks to reject the code.
> ---
>
> I'm not entirely sure why parser->skip_stack can be NULL after
> _glcpp_parser_skip_stack_change_if, but apparently it is for at
> least one test.
>
> I'm also not entirely sure if this should be an error or a warning.
> Thoughts?
>
> src/glsl/glcpp/glcpp-parse.y | 13 ++++++++++++-
> src/glsl/glcpp/glcpp.h | 1 +
> src/glsl/glcpp/tests/118-multiple-else.c | 6 ++++++
> src/glsl/glcpp/tests/118-multiple-else.c.expected | 8 ++++++++
> 4 files changed, 27 insertions(+), 1 deletion(-)
> create mode 100644 src/glsl/glcpp/tests/118-multiple-else.c
> create mode 100644 src/glsl/glcpp/tests/118-multiple-else.c.expected
>
> diff --git a/src/glsl/glcpp/glcpp-parse.y b/src/glsl/glcpp/glcpp-parse.y
> index 6eaa5f9..885c64d 100644
> --- a/src/glsl/glcpp/glcpp-parse.y
> +++ b/src/glsl/glcpp/glcpp-parse.y
> @@ -332,7 +332,17 @@ control_line:
> }
> }
> | HASH_ELSE {
> - _glcpp_parser_skip_stack_change_if (parser, & @1, "else", 1);
> + if (parser->skip_stack &&
> + parser->skip_stack->has_else)
> + {
> + glcpp_error(& @1, parser, "multiple #else");
> + }
> + else
> + {
> + _glcpp_parser_skip_stack_change_if (parser, & @1, "else", 1);
> + if (parser->skip_stack)
> + parser->skip_stack->has_else = true;
> + }
Seems the HASH_ELIF variants needs the same treatment (sorry for the
b0rked white-space, gmail is not playing nice today):
diff --git a/src/glsl/glcpp/glcpp-parse.y b/src/glsl/glcpp/glcpp-parse.y
index 885c64d..690f2da 100644
--- a/src/glsl/glcpp/glcpp-parse.y
+++ b/src/glsl/glcpp/glcpp-parse.y
@@ -310,6 +310,11 @@ control_line:
_glcpp_parser_expand_and_lex_from (parser,
ELIF_EXPANDED, $2);
}
+ else if (parser->skip_stack &&
+ parser->skip_stack->has_else)
+ {
+ glcpp_error(& @1, parser, "#elif after #else");
+ }
else
{
_glcpp_parser_skip_stack_change_if (parser, & @1,
@@ -324,6 +329,11 @@ control_line:
{
glcpp_error(& @1, parser, "#elif with no expression");
}
+ else if (parser->skip_stack &&
+ parser->skip_stack->has_else)
+ {
+ glcpp_error(& @1, parser, "#elif after #else");
+ }
else
{
_glcpp_parser_skip_stack_change_if (parser, & @1,
diff --git a/src/glsl/glcpp/tests/119-elif-after-else.c
b/src/glsl/glcpp/tests/119-elif-after-else.c
new file mode 100644
index 0000000..9b9e923
--- /dev/null
+++ b/src/glsl/glcpp/tests/119-elif-after-else.c
@@ -0,0 +1,6 @@
+#if 0
+#else
+int foo;
+#elif 0
+int bar;
+#endif
diff --git a/src/glsl/glcpp/tests/119-elif-after-else.c.expected
b/src/glsl/glcpp/tests/119-elif-after-else.c.expected
new file mode 100644
index 0000000..33f0513
--- /dev/null
+++ b/src/glsl/glcpp/tests/119-elif-after-else.c.expected
@@ -0,0 +1,8 @@
+0:4(1): preprocessor error: #elif after #else
+
+
+int foo;
+
+int bar;
+
+
More information about the mesa-dev
mailing list