From 0c25b3e4b00d6418c0d9c40071debe7727c1b512 Mon Sep 17 00:00:00 2001 From: Ian Romanick Date: Tue, 9 Aug 2016 14:32:24 -0700 Subject: [PATCH] glcpp: Only disallow #undef of pre-defined macros on GLSL ES >= 3.00 shaders Section 3.4 (Preprocessor) of the GLSL ES 3.00 spec says: It is an error to undefine or to redefine a built-in (pre-defined) macro name. The GLSL ES 1.00 spec does not contain this text. Section 3.3 (Preprocessor) of the GLSL 1.30 spec says: #define and #undef functionality are defined as is standard for C++ preprocessors for macro definitions both with and without macro parameters. At least as far as I can tell GCC allow '#undef __FILE__'. Furthermore, there are desktop OpenGL conformance tests that expect '#undef __VERSION__' and '#undef GL_core_profile' to work. Fixes: GL45-CTS.shaders.preprocessor.definitions.undefine_version_vertex GL45-CTS.shaders.preprocessor.definitions.undefine_version_fragment GL45-CTS.shaders.preprocessor.definitions.undefine_core_profile_vertex GL45-CTS.shaders.preprocessor.definitions.undefine_core_profile_fragment Signed-off-by: Ian Romanick Reviewed-by: Timothy Arceri Cc: mesa-stable@lists.freedesktop.org (cherry picked from commit 50b49d242d702e4728329cc59f87d929963e7c53) Squashed with commit glcpp: Update tests for new #undef of built-in macro rules. Ian recently changed the preprocessor to allow this in most GLSL versions, but not GLSL ES 3.00+. This patch converts the existing test that expects a failure to a #version 300 es shader, and adds a #version 110 shader to make sure that it's allowed. Fixes 'make check'. Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=97307 Signed-off-by: Kenneth Graunke Reviewed-by: Timothy Arceri Tested-by: Vinson Lee (cherry picked from commit 1f47f78fc3193ecff13799305a8e35af82e68a06) --- src/compiler/glsl/glcpp/glcpp-parse.y | 32 +++++++++++++++++++--- src/compiler/glsl/glcpp/tests/120-undef-builtin.c | 1 + .../glsl/glcpp/tests/120-undef-builtin.c.expected | 3 +- .../glsl/glcpp/tests/147-undef-builtin-allowed.c | 4 +++ .../tests/147-undef-builtin-allowed.c.expected | 4 +++ 5 files changed, 39 insertions(+), 5 deletions(-) create mode 100644 src/compiler/glsl/glcpp/tests/147-undef-builtin-allowed.c create mode 100644 src/compiler/glsl/glcpp/tests/147-undef-builtin-allowed.c.expected diff --git a/src/compiler/glsl/glcpp/glcpp-parse.y b/src/compiler/glsl/glcpp/glcpp-parse.y index bdd48f66685..68544ae88c2 100644 --- a/src/compiler/glsl/glcpp/glcpp-parse.y +++ b/src/compiler/glsl/glcpp/glcpp-parse.y @@ -278,10 +278,34 @@ control_line_success: HASH_TOKEN DEFINE_TOKEN define | HASH_TOKEN UNDEF IDENTIFIER NEWLINE { macro_t *macro; - if (strcmp("__LINE__", $3) == 0 - || strcmp("__FILE__", $3) == 0 - || strcmp("__VERSION__", $3) == 0 - || strncmp("GL_", $3, 3) == 0) + + /* Section 3.4 (Preprocessor) of the GLSL ES 3.00 spec says: + * + * It is an error to undefine or to redefine a built-in + * (pre-defined) macro name. + * + * The GLSL ES 1.00 spec does not contain this text. + * + * Section 3.3 (Preprocessor) of the GLSL 1.30 spec says: + * + * #define and #undef functionality are defined as is + * standard for C++ preprocessors for macro definitions + * both with and without macro parameters. + * + * At least as far as I can tell GCC allow '#undef __FILE__'. + * Furthermore, there are desktop OpenGL conformance tests + * that expect '#undef __VERSION__' and '#undef + * GL_core_profile' to work. + * + * Only disallow #undef of pre-defined macros on GLSL ES >= + * 3.00 shaders. + */ + if (parser->is_gles && + parser->version >= 300 && + (strcmp("__LINE__", $3) == 0 + || strcmp("__FILE__", $3) == 0 + || strcmp("__VERSION__", $3) == 0 + || strncmp("GL_", $3, 3) == 0)) glcpp_error(& @1, parser, "Built-in (pre-defined)" " macro names cannot be undefined."); diff --git a/src/compiler/glsl/glcpp/tests/120-undef-builtin.c b/src/compiler/glsl/glcpp/tests/120-undef-builtin.c index 49e7696613e..f8ade19bd59 100644 --- a/src/compiler/glsl/glcpp/tests/120-undef-builtin.c +++ b/src/compiler/glsl/glcpp/tests/120-undef-builtin.c @@ -1,3 +1,4 @@ +#version 300 es #undef __LINE__ #undef __FILE__ #undef __VERSION__ diff --git a/src/compiler/glsl/glcpp/tests/120-undef-builtin.c.expected b/src/compiler/glsl/glcpp/tests/120-undef-builtin.c.expected index 3b736df378e..498dc0f98d1 100644 --- a/src/compiler/glsl/glcpp/tests/120-undef-builtin.c.expected +++ b/src/compiler/glsl/glcpp/tests/120-undef-builtin.c.expected @@ -1,6 +1,7 @@ -0:1(1): preprocessor error: Built-in (pre-defined) macro names cannot be undefined. 0:2(1): preprocessor error: Built-in (pre-defined) macro names cannot be undefined. 0:3(1): preprocessor error: Built-in (pre-defined) macro names cannot be undefined. +0:4(1): preprocessor error: Built-in (pre-defined) macro names cannot be undefined. +#version 300 es diff --git a/src/compiler/glsl/glcpp/tests/147-undef-builtin-allowed.c b/src/compiler/glsl/glcpp/tests/147-undef-builtin-allowed.c new file mode 100644 index 00000000000..e3af10d7da2 --- /dev/null +++ b/src/compiler/glsl/glcpp/tests/147-undef-builtin-allowed.c @@ -0,0 +1,4 @@ +#version 110 +#undef __LINE__ +#undef __FILE__ +#undef __VERSION__ diff --git a/src/compiler/glsl/glcpp/tests/147-undef-builtin-allowed.c.expected b/src/compiler/glsl/glcpp/tests/147-undef-builtin-allowed.c.expected new file mode 100644 index 00000000000..cd0071ff4d4 --- /dev/null +++ b/src/compiler/glsl/glcpp/tests/147-undef-builtin-allowed.c.expected @@ -0,0 +1,4 @@ +#version 110 + + + -- 2.11.0