aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJason Merrill <jason@redhat.com>2025-04-16 11:15:14 -0400
committerJason Merrill <jason@redhat.com>2025-04-16 14:48:35 -0400
commitb0d7d644f3c25af9bf60c948ab26aa7b09a68787 (patch)
tree2f26ad016a180f5a55fdadb16fdff8173cd1b8e3
parent6b4569a3ebdd0df44d87d67a18272ec0b878f2ee (diff)
downloadgcc-b0d7d644f3c25af9bf60c948ab26aa7b09a68787.zip
gcc-b0d7d644f3c25af9bf60c948ab26aa7b09a68787.tar.gz
gcc-b0d7d644f3c25af9bf60c948ab26aa7b09a68787.tar.bz2
c++: format attribute redeclaration [PR116954]
Here when merging the two decls, remove_contract_attributes loses ATTR_IS_DEPENDENT on the format attribute, so apply_late_template_attributes just returns, so the attribute doesn't get propagated to the type where the warning looks for it. Fixed by using copy_node instead of tree_cons to preserve flags. PR c++/116954 gcc/cp/ChangeLog: * contracts.cc (remove_contract_attributes): Preserve flags on the attribute list. gcc/testsuite/ChangeLog: * g++.dg/warn/Wformat-3.C: New test.
-rw-r--r--gcc/cp/contracts.cc6
-rw-r--r--gcc/testsuite/g++.dg/warn/Wformat-3.C19
2 files changed, 24 insertions, 1 deletions
diff --git a/gcc/cp/contracts.cc b/gcc/cp/contracts.cc
index f2b126c..3ca2102 100644
--- a/gcc/cp/contracts.cc
+++ b/gcc/cp/contracts.cc
@@ -863,7 +863,11 @@ remove_contract_attributes (tree fndecl)
tree list = NULL_TREE;
for (tree p = DECL_ATTRIBUTES (fndecl); p; p = TREE_CHAIN (p))
if (!cxx_contract_attribute_p (p))
- list = tree_cons (TREE_PURPOSE (p), TREE_VALUE (p), list);
+ {
+ tree nl = copy_node (p);
+ TREE_CHAIN (nl) = list;
+ list = nl;
+ }
DECL_ATTRIBUTES (fndecl) = nreverse (list);
}
diff --git a/gcc/testsuite/g++.dg/warn/Wformat-3.C b/gcc/testsuite/g++.dg/warn/Wformat-3.C
new file mode 100644
index 0000000..e308530
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Wformat-3.C
@@ -0,0 +1,19 @@
+// PR c++/116954
+// { dg-additional-options -Wformat }
+
+#ifndef WORKS
+template<int N>
+int fn(char (&buf)[N], const char fmt[], ...)
+ __attribute__ ((__format__ (__printf__, 2, 3)));
+#endif
+
+template<int N>
+__attribute__ ((__format__ (__printf__, 2, 3)))
+int fn(char (&)[N], const char [], ...)
+{ return 0; }
+
+int main()
+{
+ char buf[20];
+ return fn(buf, "%s", 42); /* { dg-warning "Wformat" } */
+}