summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakub Jelinek <jakub@redhat.com>2025-11-22 12:39:09 +0100
committerJakub Jelinek <jakub@gcc.gnu.org>2025-11-22 12:39:09 +0100
commitcf865c811be1b83e960b3e661123cb19105d3181 (patch)
tree32268e6a077f409048e478460189d206ee693f67
parentfb22d7f3b665555cfb55673ca2b954f860d28b50 (diff)
c++: Fix up [[maybe_unused]] handling on expansion stmts [PR122788]
This PR complains that [[maybe_unused]] attribute is ignored on the range-for-declaration of expansion-statement. We copy DECL_ATTRIBUTES and apply late attributes, but early attributes don't have their handlers called again, so some extra flags need to be copied as well. This copies TREE_USED and DECL_READ_P flags. 2025-11-22 Jakub Jelinek <jakub@redhat.com> PR c++/122788 * pt.cc (finish_expansion_stmt): Or in TREE_USED and DECL_READ_P flags from range_decl to decl or from corresponding structured binding to this_decl. * g++.dg/cpp26/expansion-stmt27.C: New test.
-rw-r--r--gcc/cp/pt.cc4
-rw-r--r--gcc/testsuite/g++.dg/cpp26/expansion-stmt27.C17
2 files changed, 21 insertions, 0 deletions
diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index 27d786d0e77..c418edc6931 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -32900,6 +32900,8 @@ finish_expansion_stmt (tree expansion_stmt, tree args,
type = tsubst (type, args, complain | tf_tst_ok, in_decl);
tree decl = build_decl (loc, VAR_DECL, DECL_NAME (range_decl), type);
DECL_ATTRIBUTES (decl) = DECL_ATTRIBUTES (range_decl);
+ TREE_USED (decl) |= TREE_USED (range_decl);
+ DECL_READ_P (decl) |= DECL_READ_P (range_decl);
if (args)
apply_late_template_attributes (&decl, DECL_ATTRIBUTES (decl),
/*flags=*/0, args, complain,
@@ -32958,6 +32960,8 @@ finish_expansion_stmt (tree expansion_stmt, tree args,
DECL_ARTIFICIAL (this_decl) = 1;
DECL_ATTRIBUTES (this_decl)
= DECL_ATTRIBUTES (TREE_VEC_ELT (v, i + 1));
+ TREE_USED (this_decl) |= TREE_USED (TREE_VEC_ELT (v, i + 1));
+ DECL_READ_P (this_decl) |= DECL_READ_P (TREE_VEC_ELT (v, i + 1));
if (DECL_PACK_P (TREE_VEC_ELT (v, i + 1)))
{
tree dtype = cxx_make_type (DECLTYPE_TYPE);
diff --git a/gcc/testsuite/g++.dg/cpp26/expansion-stmt27.C b/gcc/testsuite/g++.dg/cpp26/expansion-stmt27.C
new file mode 100644
index 00000000000..2a0d325e7a5
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp26/expansion-stmt27.C
@@ -0,0 +1,17 @@
+// PR c++/122788
+// { dg-do compile { target c++14 } }
+// { dg-options "-Wunused" }
+
+void
+foo ()
+{
+ template for ([[maybe_unused]] auto i : { 42 }) // { dg-warning "'template for' only available with" "" { target c++23_down } }
+ ; // { dg-bogus "unused variable 'i'" "" { target *-*-* } .-1 }
+ template for ([[maybe_unused]] auto j : { 1, 2, 3 }) // { dg-warning "'template for' only available with" "" { target c++23_down } }
+ { // { dg-bogus "unused variable 'j'" "" { target *-*-* } .-1 }
+#if __cpp_if_constexpr >= 201606
+ if constexpr (false)
+ (void) j;
+#endif
+ }
+}