The unavailable attribute is the error form of deprecated, and
handle_unavailable_attribute stores it the same way. Without a message
it only sets TREE_UNAVAILABLE. With one it stays in the attribute list,
the copy attribute copies it, and any use of the new declaration is an
error. The new declaration usually exists to replace the old one, so
skip unavailable in both loops of handle_copy_attribute, as the previous
patch does for deprecated
gcc/c-family/ChangeLog:
* c-attribs.cc (handle_copy_attribute): Don't copy the unavailable
attribute from the referenced declaration or type.
gcc/testsuite/ChangeLog:
* c-c++-common/attr-copy-unavailable.c: New test.
Signed-off-by: Hugo Sales <[email protected]>
---
gcc/c-family/c-attribs.cc | 10 +++++--
gcc/testsuite/c-c++-common/attr-copy-unavailable.c | 32 ++++++++++++++++++++++
2 files changed, 39 insertions(+), 3 deletions(-)
diff --git a/gcc/c-family/c-attribs.cc b/gcc/c-family/c-attribs.cc
index 90f1a422ada..4bc4a81d40d 100644
--- a/gcc/c-family/c-attribs.cc
+++ b/gcc/c-family/c-attribs.cc
@@ -2894,8 +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))
+ /* As documented, the deprecated and unavailable attributes are not
+ copied either. */
+ if (is_attribute_p ("deprecated", atname)
+ || is_attribute_p ("unavailable", atname))
continue;
/* Attribute leaf only applies to extern functions.
@@ -2944,7 +2946,9 @@ handle_copy_attribute (tree *node, tree name, tree args,
since the attributes' arguments may be in their internal form. */
for (tree at = attrs; at; at = TREE_CHAIN (at))
{
- if (is_attribute_p ("deprecated", get_attribute_name (at)))
+ tree atname = get_attribute_name (at);
+ if (is_attribute_p ("deprecated", atname)
+ || is_attribute_p ("unavailable", atname))
continue;
tree attr = tree_cons (TREE_PURPOSE (at), TREE_VALUE (at), NULL_TREE);
diff --git a/gcc/testsuite/c-c++-common/attr-copy-unavailable.c
b/gcc/testsuite/c-c++-common/attr-copy-unavailable.c
new file mode 100644
index 00000000000..7f62bbf41ce
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/attr-copy-unavailable.c
@@ -0,0 +1,32 @@
+/* The copy attribute does not copy unavailable when it has a message
+ { dg-do compile } */
+
+__attribute__ ((unavailable)) void un (void);
+__attribute__ ((unavailable ("message"))) void un_message (void);
+
+/* C rejects naming an unavailable function in copy, C++ accepts it. */
+__attribute__ ((copy (un))) void copy (void);
+/* { dg-error "unavailable" "" { target c } .-1 } */
+__attribute__ ((copy (un_message))) void copy_message (void);
+/* { dg-error "unavailable" "" { target c } .-1 } */
+
+__attribute__ ((unavailable ("message"))) extern int un_var;
+__attribute__ ((copy (un_var))) extern int copy_var;
+/* { dg-error "unavailable" "" { target *-*-* } .-1 } */
+
+struct __attribute__ ((unavailable)) A { int i; };
+struct __attribute__ ((unavailable ("message"))) A_message { int i; };
+struct __attribute__ ((copy ((struct A *) 0))) B { int i; };
+/* { dg-error "unavailable" "" { target *-*-* } .-1 } */
+struct __attribute__ ((copy ((struct A_message *) 0))) B_message { int i; };
+/* { dg-error "unavailable" "" { target *-*-* } .-1 } */
+
+int
+f (void)
+{
+ struct B b = { 0 }; /* { dg-bogus "unavailable" } */
+ struct B_message bm = { 0 }; /* { dg-bogus "unavailable" } */
+ copy (); /* { dg-bogus "unavailable" } */
+ copy_message (); /* { dg-bogus "unavailable" } */
+ return copy_var + b.i + bm.i; /* { dg-bogus "unavailable" } */
+}
--
2.56.0