This fixes PR61564 by diagnosing (and ignoring) options not marked with
'Optimization' being applied to #pragma GCC optimize or via the
optimize attribute.

The reason is that while we save/restore option state for 'Optimize'
marked options we don't do that for other options.  Thus while such
options do not end up in the per-function optimize state applying them
still clobbers the global state.

Bootstrap and regtest running on x86_64-unknown-linux-gnu.

Ok for trunk?

Note this also points to the unfortunate ways the FEs apply
the pragma to functions which seems to be via adding optimize
attributes literally rather than copying a tree optimization
node constructable by parse_optimize_options.  That results
in excessive diagnostics also reproducable without the patch
if you use sth like #pragma GCC optimize ("-Xhost").

Thanks,
Richard.

2016-06-07  Richard Biener  <rguent...@suse.de>

        PR c/61564
        * c-common.c (parse_optimize_options): Only apply CL_OPTIMIZATION
        options and warn about others.

        * gcc.dg/Wpragmas-1.c: New testcase.
        * gcc.dg/Wattributes-4.c: Likewise.

Index: gcc/c-family/c-common.c
===================================================================
*** gcc/c-family/c-common.c     (revision 237167)
--- gcc/c-family/c-common.c     (working copy)
*************** parse_optimize_options (tree args, bool
*** 9580,9585 ****
--- 9580,9608 ----
    decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
                                                &decoded_options,
                                                &decoded_options_count);
+   /* Drop non-Optimization options.  */
+   unsigned j = 1;
+   for (i = 1; i < decoded_options_count; ++i)
+     {
+       if (! (cl_options[decoded_options[i].opt_index].flags & 
CL_OPTIMIZATION))
+       {
+         ret = false;
+         if (attr_p)
+           warning (OPT_Wattributes,
+                    "bad option %s to optimize attribute",
+                    decoded_options[i].orig_option_with_args_text);
+         else
+           warning (OPT_Wpragmas,
+                    "bad option %s to pragma attribute",
+                    decoded_options[i].orig_option_with_args_text);
+         continue;
+       }
+       if (i != j)
+       decoded_options[j] = decoded_options[i];
+       j++;
+     }
+   decoded_options_count = j;
+   /* And apply them.  */
    decode_options (&global_options, &global_options_set,
                  decoded_options, decoded_options_count,
                  input_location, global_dc);
Index: gcc/testsuite/gcc.dg/Wpragmas-1.c
===================================================================
*** gcc/testsuite/gcc.dg/Wpragmas-1.c   (revision 0)
--- gcc/testsuite/gcc.dg/Wpragmas-1.c   (working copy)
***************
*** 0 ****
--- 1,11 ----
+ /* { dg-do compile } */
+ 
+ #pragma GCC push_options
+ #pragma GCC optimize ("-fno-lto") /* { dg-warning "bad option" } */
+ int main(void){return 0;}
+ #pragma GCC pop_options
+ 
+ /* ???  The FEs still apply the pragma string as optimize attribute to
+    all functions thus the diagnostic will be repeated for each function
+    affected.  */
+ /* { dg-message "bad option" "" { target *-*-* } 0 } */
Index: gcc/testsuite/gcc.dg/Wattributes-4.c
===================================================================
*** gcc/testsuite/gcc.dg/Wattributes-4.c        (revision 0)
--- gcc/testsuite/gcc.dg/Wattributes-4.c        (working copy)
***************
*** 0 ****
--- 1,3 ----
+ /* { dg-do compile } */
+ 
+ int __attribute__((optimize("no-lto"))) main(void){return 0;} /* { dg-warning 
"bad option" } */

Reply via email to