public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r13-3706] c++: correct __has_attribute(init_priority)
@ 2022-11-06 16:16 Patrick Palka
  0 siblings, 0 replies; only message in thread
From: Patrick Palka @ 2022-11-06 16:16 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:d0a492faa6478c99d325fa4a7ed2e5633cef7529

commit r13-3706-gd0a492faa6478c99d325fa4a7ed2e5633cef7529
Author: Patrick Palka <ppalka@redhat.com>
Date:   Sun Nov 6 11:09:26 2022 -0500

    c++: correct __has_attribute(init_priority)
    
    Currently __has_attribute(init_priority) always returns true, even on
    targets that don't actually support init priorities, and when using the
    attribute on such targets we just get a hard error about them being
    unsupported.  This makes it impossible to conditionally use the attribute
    by querying __has_attribute.
    
    This patch fixes this by including init_priority in the attribute table
    only if the target supports init priorities.  Thus on such targets
    __has_attribute(init_priority) will now return false and we'll treat it
    as just another unrecognized attribute (e.g. using it gives a -Wattribute
    warning instead of a hard error).
    
    gcc/cp/ChangeLog:
    
            * tree.cc (cxx_attribute_table): Include init_priority entry
            only if SUPPORTS_INIT_PRIORITY.
            (handle_init_priority_attribute): Add ATTRIBUTE_UNUSED.  Assert
            SUPPORTS_INIT_PRIORITY is true.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/special/initpri3.C: New test.

Diff:
---
 gcc/cp/tree.cc                          | 20 +++++++-------------
 gcc/testsuite/g++.dg/special/initpri3.C | 10 ++++++++++
 2 files changed, 17 insertions(+), 13 deletions(-)

diff --git a/gcc/cp/tree.cc b/gcc/cp/tree.cc
index 45348c58bb6..c30bbeb0839 100644
--- a/gcc/cp/tree.cc
+++ b/gcc/cp/tree.cc
@@ -5010,8 +5010,10 @@ const struct attribute_spec cxx_attribute_table[] =
 {
   /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
        affects_type_identity, handler, exclude } */
+#if SUPPORTS_INIT_PRIORITY
   { "init_priority",  1, 1, true,  false, false, false,
     handle_init_priority_attribute, NULL },
+#endif
   { "abi_tag", 1, -1, false, false, false, true,
     handle_abi_tag_attribute, NULL },
   { NULL, 0, 0, false, false, false, false, NULL, NULL }
@@ -5039,7 +5041,7 @@ const struct attribute_spec std_attribute_table[] =
 
 /* Handle an "init_priority" attribute; arguments as in
    struct attribute_spec.handler.  */
-static tree
+ATTRIBUTE_UNUSED static tree
 handle_init_priority_attribute (tree* node,
 				tree name,
 				tree args,
@@ -5103,18 +5105,10 @@ handle_init_priority_attribute (tree* node,
 	 pri);
     }
 
-  if (SUPPORTS_INIT_PRIORITY)
-    {
-      SET_DECL_INIT_PRIORITY (decl, pri);
-      DECL_HAS_INIT_PRIORITY_P (decl) = 1;
-      return NULL_TREE;
-    }
-  else
-    {
-      error ("%qE attribute is not supported on this platform", name);
-      *no_add_attrs = true;
-      return NULL_TREE;
-    }
+  gcc_assert (SUPPORTS_INIT_PRIORITY);
+  SET_DECL_INIT_PRIORITY (decl, pri);
+  DECL_HAS_INIT_PRIORITY_P (decl) = 1;
+  return NULL_TREE;
 }
 
 /* DECL is being redeclared; the old declaration had the abi tags in OLD,
diff --git a/gcc/testsuite/g++.dg/special/initpri3.C b/gcc/testsuite/g++.dg/special/initpri3.C
new file mode 100644
index 00000000000..3d745a42a6a
--- /dev/null
+++ b/gcc/testsuite/g++.dg/special/initpri3.C
@@ -0,0 +1,10 @@
+// Verify __has_attribute(init_priority) is false on targets that
+// don't support init priorities, and is treated as an unrecognized
+// attribute in that case.
+
+#if !__has_attribute(init_priority)
+#error unsupported // { dg-error "" "" { target { ! init_priority } } }
+#endif
+
+struct A { A(); } a __attribute__((init_priority(500)));
+// { dg-warning "attribute directive ignored" "" { target { ! init_priority } } .-1 }

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-11-06 16:16 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-06 16:16 [gcc r13-3706] c++: correct __has_attribute(init_priority) Patrick Palka

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).