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.
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);
}
--- /dev/null
+// 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" } */
+}