]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Make Warray-bounds alias to Warray-bounds= [PR107787]
authorIskander Shakirzyanov <iskander@ispras.ru>
Thu, 24 Nov 2022 14:26:59 +0000 (14:26 +0000)
committerAlexander Monakov <amonakov@ispras.ru>
Wed, 30 Nov 2022 13:30:48 +0000 (16:30 +0300)
According to the documentation, the -Werror= option makes the specified
warning into an error and also automatically implies that option. Then
it seems that the behavior of the compiler when specifying
-Werror=array-bounds=X should be the same as specifying
"-Werror=array-bounds -Warray-bounds=X", so we expect to receive
array-bounds pass diagnostics and they must be processed as errors.

In practice, we observe that the array-bounds pass is indeed invoked,
but its diagnostics are processed as warnings, not errors.

This happens because Warray-bounds and Warray-bounds= are
declared as two different options in common.opt, so when
diagnostic_classify_diagnostic is called, DK_ERROR is set for
the Warray-bounds= option, but diagnostic_report_diagnostic called from
warning_at receives opt_index of Warray-bounds, so information about
DK_ERROR is lost. Fix this by using Alias in declaration of
Warray-bounds (similar to Wattribute-alias).

Co-authored-by: Franz Sirl <Franz.Sirl-kernel@lauterbach.com>
gcc/ChangeLog:

PR driver/107787
* common.opt (Warray-bounds): Turn into alias of
-Warray-bounds=1.
* builtins.cc (c_strlen): Use OPT_Warray_bounds_
instead of OPT_Warray_bounds.
* diagnostic-spec.cc (nowarn_spec_t::nowarn_spec_t): Ditto.
* gimple-array-bounds.cc (array_bounds_checker::check_array_ref,
array_bounds_checker::check_mem_ref,
array_bounds_checker::check_addr_expr,
array_bounds_checker::check_array_bounds): Ditto.
* gimple-ssa-warn-restrict.cc (maybe_diag_access_bounds): Ditto.

gcc/c-family/ChangeLog:

PR driver/107787
* c-common.cc (fold_offsetof,
convert_vector_to_array_for_subscript): Use OPT_Warray_bounds_
instead of OPT_Warray_bounds.

gcc/testsuite/ChangeLog:

PR driver/107787
* gcc.dg/Warray-bounds-34.c: Correct the regular expression
for -Warray-bounds=.
* gcc.dg/Warray-bounds-43.c: Likewise.
* gcc.dg/pr107787.c: New test.

gcc/builtins.cc
gcc/c-family/c-common.cc
gcc/common.opt
gcc/diagnostic-spec.cc
gcc/gimple-array-bounds.cc
gcc/gimple-ssa-warn-restrict.cc
gcc/testsuite/gcc.dg/Warray-bounds-34.c
gcc/testsuite/gcc.dg/Warray-bounds-43.c
gcc/testsuite/gcc.dg/pr107787.c [new file with mode: 0644]

index 4dc1ca672b23ce157e294cc16d3af3168f88720f..02c4fefa86f486fc977f5be993e436f1cda72c49 100644 (file)
@@ -696,14 +696,14 @@ c_strlen (tree arg, int only_value, c_strlen_data *data, unsigned eltsize)
     {
       /* Suppress multiple warnings for propagated constant strings.  */
       if (only_value != 2
-         && !warning_suppressed_p (arg, OPT_Warray_bounds)
-         && warning_at (loc, OPT_Warray_bounds,
+         && !warning_suppressed_p (arg, OPT_Warray_bounds_)
+         && warning_at (loc, OPT_Warray_bounds_,
                         "offset %qwi outside bounds of constant string",
                         eltoff))
        {
          if (decl)
            inform (DECL_SOURCE_LOCATION (decl), "%qE declared here", decl);
-         suppress_warning (arg, OPT_Warray_bounds);
+         suppress_warning (arg, OPT_Warray_bounds_);
        }
       return NULL_TREE;
     }
index 6f1f21bc4c173e15dfee6e7196ad97e4fe12d2d4..f08c89142e502d7cdfdfe4037011a10e4960bf8b 100644 (file)
@@ -6811,7 +6811,7 @@ fold_offsetof (tree expr, tree type, enum tree_code ctx)
                     definition thereof.  */
                  if (TREE_CODE (v) == ARRAY_REF
                      || TREE_CODE (v) == COMPONENT_REF)
-                   warning (OPT_Warray_bounds,
+                   warning (OPT_Warray_bounds_,
                             "index %E denotes an offset "
                             "greater than size of %qT",
                             t, TREE_TYPE (TREE_OPERAND (expr, 0)));
@@ -8534,7 +8534,7 @@ convert_vector_to_array_for_subscript (location_t loc,
       if (TREE_CODE (index) == INTEGER_CST)
         if (!tree_fits_uhwi_p (index)
            || maybe_ge (tree_to_uhwi (index), TYPE_VECTOR_SUBPARTS (type)))
-          warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
+         warning_at (loc, OPT_Warray_bounds_, "index value is out of bound");
 
       /* We are building an ARRAY_REF so mark the vector as addressable
          to not run into the gimplifiers premature setting of DECL_GIMPLE_REG_P
index c458b71680c773ad64c5bcbee3c375c3f988b8f6..562d73d7f552a13d30c1ce360d058edf72fda7c3 100644 (file)
@@ -539,8 +539,7 @@ Common Var(warn_aggressive_loop_optimizations) Init(1) Warning
 Warn if a loop with constant number of iterations triggers undefined behavior.
 
 Warray-bounds
-Common Var(warn_array_bounds) Warning
-Warn if an array is accessed out of bounds.
+Common Alias(Warray-bounds=, 1, 0) Warning
 
 Warray-bounds=
 Common Joined RejectNegative UInteger Var(warn_array_bounds) Warning IntegerRange(0, 2)
index aece89619e77cddf08ef3fd44331f264808161f4..7a03fc493e6b60cf7825bc1ca6b1085141fd303d 100644 (file)
@@ -79,7 +79,6 @@ nowarn_spec_t::nowarn_spec_t (opt_code opt)
       break;
 
       /* Access warning group.  */
-    case OPT_Warray_bounds:
     case OPT_Warray_bounds_:
     case OPT_Wformat_overflow_:
     case OPT_Wformat_truncation_:
index eae49ab3910ad57288d772e63af10fd610527a84..972e25fdb3154ca25e319346c1475a14d9732e41 100644 (file)
@@ -172,7 +172,7 @@ bool
 array_bounds_checker::check_array_ref (location_t location, tree ref,
                                       gimple *stmt, bool ignore_off_by_one)
 {
-  if (warning_suppressed_p (ref, OPT_Warray_bounds))
+  if (warning_suppressed_p (ref, OPT_Warray_bounds_))
     /* Return true to have the caller prevent warnings for enclosing
        refs.  */
     return true;
@@ -277,7 +277,7 @@ array_bounds_checker::check_array_ref (location_t location, tree ref,
 
   /* Empty array.  */
   if (up_bound && tree_int_cst_equal (low_bound, up_bound_p1))
-    warned = warning_at (location, OPT_Warray_bounds,
+    warned = warning_at (location, OPT_Warray_bounds_,
                         "array subscript %E is outside array bounds of %qT",
                         low_sub, artype);
 
@@ -303,7 +303,7 @@ array_bounds_checker::check_array_ref (location_t location, tree ref,
              : tree_int_cst_le (up_bound, up_sub))
          && TREE_CODE (low_sub) == INTEGER_CST
          && tree_int_cst_le (low_sub, low_bound))
-       warned = warning_at (location, OPT_Warray_bounds,
+       warned = warning_at (location, OPT_Warray_bounds_,
                             "array subscript [%E, %E] is outside "
                             "array bounds of %qT",
                             low_sub, up_sub, artype);
@@ -313,12 +313,12 @@ array_bounds_checker::check_array_ref (location_t location, tree ref,
           && (ignore_off_by_one
               ? !tree_int_cst_le (up_sub, up_bound_p1)
               : !tree_int_cst_le (up_sub, up_bound)))
-    warned = warning_at (location, OPT_Warray_bounds,
+    warned = warning_at (location, OPT_Warray_bounds_,
                         "array subscript %E is above array bounds of %qT",
                         up_sub, artype);
   else if (TREE_CODE (low_sub) == INTEGER_CST
           && tree_int_cst_lt (low_sub, low_bound))
-    warned = warning_at (location, OPT_Warray_bounds,
+    warned = warning_at (location, OPT_Warray_bounds_,
                         "array subscript %E is below array bounds of %qT",
                         low_sub, artype);
 
@@ -343,7 +343,7 @@ array_bounds_checker::check_array_ref (location_t location, tree ref,
       /* Avoid more warnings when checking more significant subscripts
         of the same expression.  */
       ref = TREE_OPERAND (ref, 0);
-      suppress_warning (ref, OPT_Warray_bounds);
+      suppress_warning (ref, OPT_Warray_bounds_);
 
       if (decl)
        ref = decl;
@@ -383,7 +383,7 @@ bool
 array_bounds_checker::check_mem_ref (location_t location, tree ref,
                                     bool ignore_off_by_one)
 {
-  if (warning_suppressed_p (ref, OPT_Warray_bounds))
+  if (warning_suppressed_p (ref, OPT_Warray_bounds_))
     return false;
 
   /* The statement used to allocate the array or null.  */
@@ -483,12 +483,12 @@ array_bounds_checker::check_mem_ref (location_t location, tree ref,
   if (lboob)
     {
       if (offrange[0] == offrange[1])
-       warned = warning_at (location, OPT_Warray_bounds,
+       warned = warning_at (location, OPT_Warray_bounds_,
                             "array subscript %wi is outside array bounds "
                             "of %qT",
                             offrange[0].to_shwi (), reftype);
       else
-       warned = warning_at (location, OPT_Warray_bounds,
+       warned = warning_at (location, OPT_Warray_bounds_,
                             "array subscript [%wi, %wi] is outside "
                             "array bounds of %qT",
                             offrange[0].to_shwi (),
@@ -503,7 +503,7 @@ array_bounds_checker::check_mem_ref (location_t location, tree ref,
        backtype = build_array_type_nelts (unsigned_char_type_node,
                                           aref.sizrng[1].to_uhwi ());
 
-      warned = warning_at (location, OPT_Warray_bounds,
+      warned = warning_at (location, OPT_Warray_bounds_,
                           "array subscript %<%T[%wi]%> is partly "
                           "outside array bounds of %qT",
                           axstype, offrange[0].to_shwi (), backtype);
@@ -513,7 +513,7 @@ array_bounds_checker::check_mem_ref (location_t location, tree ref,
     {
       /* TODO: Determine the access from the statement and use it.  */
       aref.inform_access (access_none);
-      suppress_warning (ref, OPT_Warray_bounds);
+      suppress_warning (ref, OPT_Warray_bounds_);
       return true;
     }
 
@@ -526,11 +526,11 @@ array_bounds_checker::check_mem_ref (location_t location, tree ref,
     {
       HOST_WIDE_INT tmpidx = (aref.offmax[i] / eltsize).to_shwi ();
 
-      if (warning_at (location, OPT_Warray_bounds,
+      if (warning_at (location, OPT_Warray_bounds_,
                      "intermediate array offset %wi is outside array bounds "
                      "of %qT", tmpidx, reftype))
        {
-         suppress_warning (ref, OPT_Warray_bounds);
+         suppress_warning (ref, OPT_Warray_bounds_);
          return true;
        }
     }
@@ -562,7 +562,7 @@ array_bounds_checker::check_addr_expr (location_t location, tree t,
        warned = check_mem_ref (location, t, ignore_off_by_one);
 
       if (warned)
-       suppress_warning (t, OPT_Warray_bounds);
+       suppress_warning (t, OPT_Warray_bounds_);
 
       t = TREE_OPERAND (t, 0);
     }
@@ -570,7 +570,7 @@ array_bounds_checker::check_addr_expr (location_t location, tree t,
 
   if (TREE_CODE (t) != MEM_REF
       || TREE_CODE (TREE_OPERAND (t, 0)) != ADDR_EXPR
-      || warning_suppressed_p (t, OPT_Warray_bounds))
+      || warning_suppressed_p (t, OPT_Warray_bounds_))
     return;
 
   tree tem = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
@@ -605,7 +605,7 @@ array_bounds_checker::check_addr_expr (location_t location, tree t,
          dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
          fprintf (dump_file, "\n");
        }
-      warned = warning_at (location, OPT_Warray_bounds,
+      warned = warning_at (location, OPT_Warray_bounds_,
                           "array subscript %wi is below "
                           "array bounds of %qT",
                           idx.to_shwi (), TREE_TYPE (tem));
@@ -619,7 +619,7 @@ array_bounds_checker::check_addr_expr (location_t location, tree t,
          dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
          fprintf (dump_file, "\n");
        }
-      warned = warning_at (location, OPT_Warray_bounds,
+      warned = warning_at (location, OPT_Warray_bounds_,
                           "array subscript %wu is above "
                           "array bounds of %qT",
                           idx.to_uhwi (), TREE_TYPE (tem));
@@ -630,7 +630,7 @@ array_bounds_checker::check_addr_expr (location_t location, tree t,
       if (DECL_P (t))
        inform (DECL_SOURCE_LOCATION (t), "while referencing %qD", t);
 
-      suppress_warning (t, OPT_Warray_bounds);
+      suppress_warning (t, OPT_Warray_bounds_);
     }
 }
 
@@ -730,7 +730,7 @@ array_bounds_checker::check_array_bounds (tree *tp, int *walk_subtree,
   /* Propagate the no-warning bit to the outer statement to avoid also
      issuing -Wstringop-overflow/-overread for the out-of-bounds accesses.  */
   if (warned)
-    suppress_warning (wi->stmt, OPT_Warray_bounds);
+    suppress_warning (wi->stmt, OPT_Warray_bounds_);
 
   return NULL_TREE;
 }
index 832456ba6fce2f9e614e1bdf1c28612b6ddfe221..107ba278798d26ffddff9265b67d5442b5a2707c 100644 (file)
@@ -1734,7 +1734,7 @@ maybe_diag_access_bounds (gimple *call, tree func, int strict,
   if (!oobref)
     return no_warning;
 
-  const opt_code opt = OPT_Warray_bounds;
+  const opt_code opt = OPT_Warray_bounds_;
   /* Return true without issuing a warning.  */
   if (!do_warn)
     return opt;
index cea7c4b32a220a60b6a8d6c59a17cc7bee2395ae..1b2a098ad86c082671cd68e468917cba1507b415 100644 (file)
@@ -7,7 +7,7 @@ int x;
 
 inline void foo (int i)
 {
-  a[i + 1] = 123;   /* { dg-warning "\\\[-Warray-bounds]" } */
+  a[i + 1] = 123;   /* { dg-warning "\\\[-Warray-bounds" } */
 }
 
 int bar (void)
index 0f521a7250d79b29afcd8ab4058f6730510d2bb3..43a83315787c7f219354ad59bca4fc247cc2d4bb 100644 (file)
@@ -19,7 +19,7 @@ NOIPA int g2 (int i)
 
   sink (p0, p1, p2);
 
-  return p2[8];     // { dg-warning "\\\[-Warray-bounds]" }
+  return p2[8];     // { dg-warning "\\\[-Warray-bounds" }
 }
 
 NOIPA int g3 (int i)
@@ -33,7 +33,7 @@ NOIPA int g3 (int i)
 
   sink (p0, p1, p2, p3);
 
-  return p3[7];     // { dg-warning "\\\[-Warray-bounds]" }
+  return p3[7];     // { dg-warning "\\\[-Warray-bounds" }
 }
 
 NOIPA int g4 (int i)
@@ -48,7 +48,7 @@ NOIPA int g4 (int i)
 
   sink (p0, p1, p2, p3, p4);
 
-  return p4[6];     // { dg-warning "\\\[-Warray-bounds]" }
+  return p4[6];     // { dg-warning "\\\[-Warray-bounds" }
 }
 
 NOIPA int g5 (int i)
diff --git a/gcc/testsuite/gcc.dg/pr107787.c b/gcc/testsuite/gcc.dg/pr107787.c
new file mode 100644 (file)
index 0000000..922dbff
--- /dev/null
@@ -0,0 +1,13 @@
+/* PR driver/107787 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -Werror=array-bounds=1" } */
+/* { dg-message "some warnings being treated as errors" "" {target "*-*-*"} 0 } */
+
+int a[10];     /* { dg-note "while referencing" } */
+
+int* f(void) {
+
+    a[-1] = 0; /* { dg-error "is below array bounds" } */
+
+    return a;
+}