glsl: Add pragma to disable all warnings
authorIan Romanick <ian.d.romanick@intel.com>
Thu, 1 Nov 2018 20:50:14 +0000 (13:50 -0700)
committerIan Romanick <ian.d.romanick@intel.com>
Thu, 8 Nov 2018 19:00:00 +0000 (11:00 -0800)
Use #pragma warning(off) and #pragma warning(on) to disable or enable
all 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
'    #pragma warning(off)
'    __foo
'    #pragma warning(on)
'    (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.

v2: Fix the #pragma lines in the commit message that git-commit ate.

Signed-off-by: Ian Romanick <ian.d.romanick@intel.com>
Reviewed-by: Matt Turner <mattst88@gmail.com>
src/compiler/glsl/ast.h
src/compiler/glsl/ast_to_hir.cpp
src/compiler/glsl/glsl_lexer.ll
src/compiler/glsl/glsl_parser.yy
src/compiler/glsl/glsl_parser_extras.cpp
src/compiler/glsl/glsl_parser_extras.h
src/compiler/glsl/tests/warnings/032-__-in-function-name-pragma-disable.vert [new file with mode: 0644]
src/compiler/glsl/tests/warnings/032-__-in-function-name-pragma-disable.vert.expected [new file with mode: 0644]

index 4d5e045b82c048df32bfdae177ace4e601b32c7d..94bcf644a26953291239104cdbbe710ec2e580cc 100644 (file)
@@ -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
index 7671041a4544c535fc808132ac254a5962bdb012..95cef626ac3c338af2648d826fe0e5074d76f061 100644 (file)
@@ -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;
+}
index 0a2bba6d38fe204151136dd8d481f4760791d018..224418ed7cebb606ca4f3256dce1e5e02143e8f5 100644 (file)
@@ -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;
index fd1592beca0e3ddc02f4d8d74c7f6f2cfe0ce1f6..ddb54f4a4d6e9d718816aa3656ba481a6e78d83f 100644 (file)
@@ -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; }
    ;
index 2a58908c2261c0a380db855aab2cd851bac69bb3..21ed34d79d078e450c8675a365c9d42295cd0b4f 100644 (file)
@@ -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);
+   }
 }
 
 
index 985200ecab575aab3324b7ae0d9da84d4026e96c..da8b2fa3ab51ee51e2cf4e5fb6cc21bc0dd37a67 100644 (file)
@@ -614,6 +614,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 (file)
index 0000000..d0d8d91
--- /dev/null
@@ -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 (file)
index 0000000..4b490a9
--- /dev/null
@@ -0,0 +1,2 @@
+0:3(7): warning: identifier `__foo' uses reserved `__' string
+0:15(7): warning: identifier `__blat' uses reserved `__' string