From: Ian Romanick <ian.d.roman...@intel.com>

warnings.  This is a big hammer.  If we ever need a smaller hammer, we
can enhance this functionality.

There is one lame thing about this.  Because we parse everything, create
an AST, then convert the AST to GLSL IR, we have to treat the #pragma
like a statment.  This means that you can't do something like

void
__foo
(float param0);

Fixing that would, as far as I can tell, require a huge amount of work.

I did try just handling the #pragma during parsing (like we do for
state for the whole shader.

Signed-off-by: Ian Romanick <ian.d.roman...@intel.com>
Cc: Matt Turner <matts...@gmail.com>
---
 src/compiler/glsl/ast.h                            | 14 +++++++++++++
 src/compiler/glsl/ast_to_hir.cpp                   |  8 ++++++++
 src/compiler/glsl/glsl_lexer.ll                    |  8 ++++++++
 src/compiler/glsl/glsl_parser.yy                   | 24 +++++++++++++++++-----
 src/compiler/glsl/glsl_parser_extras.cpp           | 12 ++++++-----
 src/compiler/glsl/glsl_parser_extras.h             |  7 +++++++
 .../032-__-in-function-name-pragma-disable.vert    | 24 ++++++++++++++++++++++
 ..._-in-function-name-pragma-disable.vert.expected |  2 ++
 8 files changed, 89 insertions(+), 10 deletions(-)
 create mode 100644 
src/compiler/glsl/tests/warnings/032-__-in-function-name-pragma-disable.vert
 create mode 100644 
src/compiler/glsl/tests/warnings/032-__-in-function-name-pragma-disable.vert.expected

diff --git a/src/compiler/glsl/ast.h b/src/compiler/glsl/ast.h
index 4d5e045b82c..94bcf644a26 100644
--- a/src/compiler/glsl/ast.h
+++ b/src/compiler/glsl/ast.h
@@ -1315,6 +1315,20 @@ private:
    ast_layout_expression *local_size[3];
 };
 
+class ast_warnings_toggle : public ast_node {
+public:
+   ast_warnings_toggle(bool _enable)
+      : enable(_enable)
+   {
+      /* empty */
+   }
+
+   virtual ir_rvalue *hir(exec_list *instructions,
+                          struct _mesa_glsl_parse_state *state);
+
+private:
+   bool enable;
+};
 /*@}*/
 
 extern void
diff --git a/src/compiler/glsl/ast_to_hir.cpp b/src/compiler/glsl/ast_to_hir.cpp
index 084b7021a9f..fa9f61da2cd 100644
--- a/src/compiler/glsl/ast_to_hir.cpp
+++ b/src/compiler/glsl/ast_to_hir.cpp
@@ -8766,3 +8766,11 @@ remove_per_vertex_blocks(exec_list *instructions,
       }
    }
 }
+
+ir_rvalue *
+ast_warnings_toggle::hir(exec_list *,
+                         struct _mesa_glsl_parse_state *state)
+{
+   state->warnings_enabled = enable;
+   return NULL;
+}
diff --git a/src/compiler/glsl/glsl_lexer.ll b/src/compiler/glsl/glsl_lexer.ll
index 0a2bba6d38f..224418ed7ce 100644
--- a/src/compiler/glsl/glsl_lexer.ll
+++ b/src/compiler/glsl/glsl_lexer.ll
@@ -295,6 +295,14 @@ HASH               ^{SPC}#{SPC}
                                  BEGIN PP;
                                  return PRAGMA_OPTIMIZE_OFF;
                                }
+^{SPC}#{SPC}pragma{SPCP}warning{SPC}\({SPC}on{SPC}\) {
+                                 BEGIN PP;
+                                 return PRAGMA_WARNING_ON;
+                               }
+^{SPC}#{SPC}pragma{SPCP}warning{SPC}\({SPC}off{SPC}\) {
+                                 BEGIN PP;
+                                 return PRAGMA_WARNING_OFF;
+                               }
 ^{SPC}#{SPC}pragma{SPCP}STDGL{SPCP}invariant{SPC}\({SPC}all{SPC}\) {
                                  BEGIN PP;
                                  return PRAGMA_INVARIANT_ALL;
diff --git a/src/compiler/glsl/glsl_parser.yy b/src/compiler/glsl/glsl_parser.yy
index fd1592beca0..ddb54f4a4d6 100644
--- a/src/compiler/glsl/glsl_parser.yy
+++ b/src/compiler/glsl/glsl_parser.yy
@@ -164,6 +164,7 @@ static bool match_layout_qualifier(const char *s1, const 
char *s2,
 %token VERSION_TOK EXTENSION LINE COLON EOL INTERFACE OUTPUT
 %token PRAGMA_DEBUG_ON PRAGMA_DEBUG_OFF
 %token PRAGMA_OPTIMIZE_ON PRAGMA_OPTIMIZE_OFF
+%token PRAGMA_WARNING_ON PRAGMA_WARNING_OFF
 %token PRAGMA_INVARIANT_ALL
 %token LAYOUT_TOK
 %token DOT_TOK
@@ -246,6 +247,7 @@ static bool match_layout_qualifier(const char *s1, const 
char *s2,
 %type <n> unary_operator
 %type <expression> function_identifier
 %type <node> external_declaration
+%type <node> pragma_statement
 %type <declarator_list> init_declarator_list
 %type <declarator_list> single_declaration
 %type <expression> initializer
@@ -328,10 +330,10 @@ version_statement:
    ;
 
 pragma_statement:
-   PRAGMA_DEBUG_ON EOL
-   | PRAGMA_DEBUG_OFF EOL
-   | PRAGMA_OPTIMIZE_ON EOL
-   | PRAGMA_OPTIMIZE_OFF EOL
+   PRAGMA_DEBUG_ON EOL { $$ = NULL; }
+   | PRAGMA_DEBUG_OFF EOL { $$ = NULL; }
+   | PRAGMA_OPTIMIZE_ON EOL { $$ = NULL; }
+   | PRAGMA_OPTIMIZE_OFF EOL { $$ = NULL; }
    | PRAGMA_INVARIANT_ALL EOL
    {
       /* Pragma invariant(all) cannot be used in a fragment shader.
@@ -353,6 +355,18 @@ pragma_statement:
       } else {
          state->all_invariant = true;
       }
+
+      $$ = NULL;
+   }
+   | PRAGMA_WARNING_ON EOL
+   {
+      void *mem_ctx = state->linalloc;
+      $$ = new(mem_ctx) ast_warnings_toggle(true);
+   }
+   | PRAGMA_WARNING_OFF EOL
+   {
+      void *mem_ctx = state->linalloc;
+      $$ = new(mem_ctx) ast_warnings_toggle(false);
    }
    ;
 
@@ -2723,7 +2737,7 @@ jump_statement:
 external_declaration:
    function_definition      { $$ = $1; }
    | declaration            { $$ = $1; }
-   | pragma_statement       { $$ = NULL; }
+   | pragma_statement       { $$ = $1; }
    | layout_defaults        { $$ = $1; }
    | ';'                    { $$ = NULL; }
    ;
diff --git a/src/compiler/glsl/glsl_parser_extras.cpp 
b/src/compiler/glsl/glsl_parser_extras.cpp
index 1bdd7c4bf17..72d351909fc 100644
--- a/src/compiler/glsl/glsl_parser_extras.cpp
+++ b/src/compiler/glsl/glsl_parser_extras.cpp
@@ -62,7 +62,7 @@ _mesa_glsl_parse_state::_mesa_glsl_parse_state(struct 
gl_context *_ctx,
                                               gl_shader_stage stage,
                                                void *mem_ctx)
    : ctx(_ctx), cs_input_local_size_specified(false), cs_input_local_size(),
-     switch_state()
+     switch_state(), warnings_enabled(true)
 {
    assert(stage < MESA_SHADER_STAGES);
    this->stage = stage;
@@ -527,11 +527,13 @@ void
 _mesa_glsl_warning(const YYLTYPE *locp, _mesa_glsl_parse_state *state,
                   const char *fmt, ...)
 {
-   va_list ap;
+   if (state->warnings_enabled) {
+      va_list ap;
 
-   va_start(ap, fmt);
-   _mesa_glsl_msg(locp, state, MESA_DEBUG_TYPE_OTHER, fmt, ap);
-   va_end(ap);
+      va_start(ap, fmt);
+      _mesa_glsl_msg(locp, state, MESA_DEBUG_TYPE_OTHER, fmt, ap);
+      va_end(ap);
+   }
 }
 
 
diff --git a/src/compiler/glsl/glsl_parser_extras.h 
b/src/compiler/glsl/glsl_parser_extras.h
index 966d848509c..2768e4bb377 100644
--- a/src/compiler/glsl/glsl_parser_extras.h
+++ b/src/compiler/glsl/glsl_parser_extras.h
@@ -601,6 +601,13 @@ struct _mesa_glsl_parse_state {
 
    char *info_log;
 
+   /**
+    * Are warnings enabled?
+    *
+    * Emission of warngins is controlled by '#pragma warning(...)'.
+    */
+   bool warnings_enabled;
+
    /**
     * \name Enable bits for GLSL extensions
     */
diff --git 
a/src/compiler/glsl/tests/warnings/032-__-in-function-name-pragma-disable.vert 
b/src/compiler/glsl/tests/warnings/032-__-in-function-name-pragma-disable.vert
new file mode 100644
index 00000000000..d0d8d915a40
--- /dev/null
+++ 
b/src/compiler/glsl/tests/warnings/032-__-in-function-name-pragma-disable.vert
@@ -0,0 +1,24 @@
+#version 130
+
+float __foo(float x)
+{
+   return 6.0 * x;
+}
+
+#pragma warning(off)
+float __bar(float x)
+{
+   return 3.0 * x;
+}
+#pragma warning(on)
+
+float __blat(float x)
+{
+   return 2.0 * x;
+}
+
+void main()
+{
+   gl_Position = vec4(__foo(gl_Vertex.x), __bar(gl_Vertex.y), 
__blat(gl_Vertex.z), 1.0);
+}
+
diff --git 
a/src/compiler/glsl/tests/warnings/032-__-in-function-name-pragma-disable.vert.expected
 
b/src/compiler/glsl/tests/warnings/032-__-in-function-name-pragma-disable.vert.expected
new file mode 100644
index 00000000000..4b490a9c879
--- /dev/null
+++ 
b/src/compiler/glsl/tests/warnings/032-__-in-function-name-pragma-disable.vert.expected
@@ -0,0 +1,2 @@
+0:3(7): warning: identifier `__foo' uses reserved `__' string
+0:15(7): warning: identifier `__blat' uses reserved `__' string
-- 
2.14.4

_______________________________________________
mesa-dev mailing list
mesa-dev@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/mesa-dev

Reply via email to