From: Marek Polacek Date: Wed, 8 May 2024 21:02:49 +0000 (-0400) Subject: c++: failure to suppress -Wsizeof-array-div in template [PR114983] X-Git-Tag: basepoints/gcc-16~9202 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=646db3d30bd071a1b671b4f91c9ea2ab7f2be21c;p=thirdparty%2Fgcc.git 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) : 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 --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index 8787eabb9fd..a7d9fcf930e 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 b8c2bf8771f..71520dcd4fa 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 00000000000..bfd690325e5 --- /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 +constexpr inline size_t fn1() +{ + return ((sizeof(samplesBuffer)) / (sizeof(T))); // { dg-bogus "expression does not compute" } +} + +template +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(); + sz += fn2(); // { dg-message "required from here" } + return sz; +}