https://gcc.gnu.org/g:646db3d30bd071a1b671b4f91c9ea2ab7f2be21c

commit r15-351-g646db3d30bd071a1b671b4f91c9ea2ab7f2be21c
Author: Marek Polacek <pola...@redhat.com>
Date:   Wed May 8 17:02:49 2024 -0400

    c++: failure to suppress -Wsizeof-array-div in template [PR114983]
    
    -Wsizeof-array-div offers a way to suppress the warning by wrapping
    the second operand of the division in parens:
    
      sizeof (samplesBuffer) / (sizeof(unsigned char))
    
    but this doesn't work in a template, because we fail to propagate
    the suppression bits.  Do it, then.
    
    The finish_parenthesized_expr hunk is not needed because suppress_warning
    isn't very fine-grained.  But I think it makes sense to be explicit and
    not rely on OPT_Wparentheses also suppressing OPT_Wsizeof_array_div.
    
            PR c++/114983
    
    gcc/cp/ChangeLog:
    
            * pt.cc (tsubst_expr) <case SIZEOF_EXPR>: Use copy_warning.
            * semantics.cc (finish_parenthesized_expr): Also suppress
            -Wsizeof-array-div.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/warn/Wsizeof-array-div3.C: New test.

Diff:
---
 gcc/cp/pt.cc                                   |  1 +
 gcc/cp/semantics.cc                            |  2 ++
 gcc/testsuite/g++.dg/warn/Wsizeof-array-div3.C | 27 ++++++++++++++++++++++++++
 3 files changed, 30 insertions(+)

diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index 8787eabb9fdb..a7d9fcf930e2 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -20570,6 +20570,7 @@ tsubst_expr (tree t, tree args, tsubst_flags_t 
complain, tree in_decl)
                TREE_READONLY (r) = 1;
              }
            SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
+           copy_warning (r, t);
          }
        RETURN (r);
       }
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index b8c2bf8771fc..71520dcd4fa9 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -2306,6 +2306,8 @@ finish_parenthesized_expr (cp_expr expr)
       /* This inhibits warnings in maybe_warn_unparenthesized_assignment
         and c_common_truthvalue_conversion.  */
       suppress_warning (STRIP_REFERENCE_REF (*expr), OPT_Wparentheses);
+      /* And maybe_warn_sizeof_array_div.  */
+      suppress_warning (STRIP_REFERENCE_REF (*expr), OPT_Wsizeof_array_div);
     }
 
   if (TREE_CODE (expr) == OFFSET_REF
diff --git a/gcc/testsuite/g++.dg/warn/Wsizeof-array-div3.C 
b/gcc/testsuite/g++.dg/warn/Wsizeof-array-div3.C
new file mode 100644
index 000000000000..bfd690325e51
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Wsizeof-array-div3.C
@@ -0,0 +1,27 @@
+// PR c++/114983
+// { dg-do compile { target c++11 } }
+// { dg-options "-Wsizeof-array-div" }
+
+using size_t = decltype (sizeof (0));
+unsigned int samplesBuffer[40];
+
+template <typename T>
+constexpr inline size_t fn1()
+{
+  return ((sizeof(samplesBuffer)) / (sizeof(T))); // { dg-bogus "expression 
does not compute" }
+}
+
+template <typename T>
+constexpr inline size_t fn2()
+{
+  return ((sizeof(samplesBuffer)) / sizeof(T)); // { dg-warning "expression 
does not compute" }
+}
+
+size_t
+g ()
+{
+  auto sz = sizeof (samplesBuffer) / (sizeof(unsigned char));
+  sz += fn1<unsigned char>();
+  sz += fn2<unsigned char>(); // { dg-message "required from here" }
+  return sz;
+}

Reply via email to