diff options
author | Marek Polacek <polacek@redhat.com> | 2024-05-08 17:02:49 -0400 |
---|---|---|
committer | Marek Polacek <polacek@redhat.com> | 2024-05-09 15:25:06 -0400 |
commit | 646db3d30bd071a1b671b4f91c9ea2ab7f2be21c (patch) | |
tree | f34b59a7cbc524be0c68f64d914ea9897b21b8d0 /gcc | |
parent | e02b5683e77c2b4317b23be72e43b6e6cc6c8e5b (diff) | |
download | gcc-646db3d30bd071a1b671b4f91c9ea2ab7f2be21c.zip gcc-646db3d30bd071a1b671b4f91c9ea2ab7f2be21c.tar.gz gcc-646db3d30bd071a1b671b4f91c9ea2ab7f2be21c.tar.bz2 |
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.
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/cp/pt.cc | 1 | ||||
-rw-r--r-- | gcc/cp/semantics.cc | 2 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/warn/Wsizeof-array-div3.C | 27 |
3 files changed, 30 insertions, 0 deletions
diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index 8787eab..a7d9fcf 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 b8c2bf8..71520dc 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 0000000..bfd6903 --- /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; +} |