Without a message, handle_deprecated_attribute only sets TREE_DEPRECATED
and drops the attribute, so the copy attribute has nothing to copy.
With a message the attribute stays in DECL_ATTRIBUTES, and
handle_copy_attribute copies it, so calls to the copy warn that it is
deprecated
Skip it in both loops of handle_copy_attribute. The type loop passed
the rest of the chain to decl_attributes on each iteration, which would
still let a later deprecated through, so pass one attribute at a time,
as the declaration loop does
gcc/c-family/ChangeLog:
PR c/128072
* c-attribs.cc (handle_copy_attribute): Don't copy the deprecated
attribute from the referenced declaration or type. Apply the type
attributes one at a time.
gcc/testsuite/ChangeLog:
PR c/128072
* c-c++-common/attr-copy-pr128072.c: New test.
Signed-off-by: Hugo Sales <[email protected]>
---
gcc/c-family/c-attribs.cc | 14 +++++++++--
gcc/testsuite/c-c++-common/attr-copy-pr128072.c | 33 +++++++++++++++++++++++++
2 files changed, 45 insertions(+), 2 deletions(-)
diff --git a/gcc/c-family/c-attribs.cc b/gcc/c-family/c-attribs.cc
index 5d45a345d26..90f1a422ada 100644
--- a/gcc/c-family/c-attribs.cc
+++ b/gcc/c-family/c-attribs.cc
@@ -2894,6 +2894,10 @@ handle_copy_attribute (tree *node, tree name, tree args,
|| is_attribute_p ("target_clones", atname))
continue;
+ /* As documented, the deprecated attribute is not copied either. */
+ if (is_attribute_p ("deprecated", atname))
+ continue;
+
/* Attribute leaf only applies to extern functions.
Avoid copying it to static ones. */
if (!TREE_PUBLIC (decl)
@@ -2939,8 +2943,14 @@ handle_copy_attribute (tree *node, tree name, tree args,
or a type but not if it's an expression. Set ATTR_FLAG_INTERNAL
since the attributes' arguments may be in their internal form. */
for (tree at = attrs; at; at = TREE_CHAIN (at))
- decl_attributes (node, at, flags | ATTR_FLAG_INTERNAL,
- EXPR_P (ref) ? NULL_TREE : ref);
+ {
+ if (is_attribute_p ("deprecated", get_attribute_name (at)))
+ continue;
+
+ tree attr = tree_cons (TREE_PURPOSE (at), TREE_VALUE (at), NULL_TREE);
+ decl_attributes (node, attr, flags | ATTR_FLAG_INTERNAL,
+ EXPR_P (ref) ? NULL_TREE : ref);
+ }
return NULL_TREE;
}
diff --git a/gcc/testsuite/c-c++-common/attr-copy-pr128072.c
b/gcc/testsuite/c-c++-common/attr-copy-pr128072.c
new file mode 100644
index 00000000000..2fea2f84f3b
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/attr-copy-pr128072.c
@@ -0,0 +1,33 @@
+/* PR c/128072 - the copy attribute copies deprecated when it has a message
+ { dg-do compile } */
+
+__attribute__ ((deprecated)) void dep (void);
+__attribute__ ((deprecated ("message"))) void dep_message (void);
+
+/* C warns about naming a deprecated function in copy, C++ does not. */
+__attribute__ ((copy (dep))) void copy (void);
+/* { dg-warning "deprecated" "" { target c } .-1 } */
+__attribute__ ((copy (dep_message))) void copy_message (void);
+/* { dg-warning "deprecated" "" { target c } .-1 } */
+
+__attribute__ ((deprecated ("message"))) extern int dep_var;
+__attribute__ ((copy (dep_var))) extern int copy_var;
+/* { dg-warning "deprecated" "" { target *-*-* } .-1 } */
+
+struct __attribute__ ((deprecated)) A { int i; };
+struct __attribute__ ((deprecated ("message"))) A_message { int i; };
+struct __attribute__ ((copy ((struct A *) 0))) B { int i; };
+/* { dg-warning "deprecated" "" { target *-*-* } .-1 } */
+struct __attribute__ ((copy ((struct A_message *) 0))) B_message { int i; };
+/* { dg-warning "deprecated" "" { target *-*-* } .-1 } */
+
+int
+f (void)
+{
+ struct B b = { 0 }; /* { dg-bogus "deprecated" } */
+ struct B_message bm = { 0 }; /* { dg-bogus "deprecated" } */
+ copy (); /* { dg-bogus "deprecated" } */
+ copy_message (); /* { dg-bogus "deprecated" } */
+ dep_message (); /* { dg-warning "deprecated" } */
+ return copy_var + b.i + bm.i; /* { dg-bogus "deprecated" } */
+}
--
2.56.0