public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jason Merrill <jason@redhat.com>
To: Patrick Palka <ppalka@redhat.com>, gcc-patches@gcc.gnu.org
Cc: libstdc++@gcc.gnu.org
Subject: Re: [PATCH 1/2] c++: correct __has_attribute(init_priority)
Date: Fri, 4 Nov 2022 11:55:59 -0400	[thread overview]
Message-ID: <9952e423-4e25-4ba5-8236-5fbb3b027c9d@redhat.com> (raw)
In-Reply-To: <20221104150525.2968778-1-ppalka@redhat.com>

On 11/4/22 11:05, Patrick Palka wrote:
> 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 issue a hard error that init
> priorities are unsupported.  This makes it impossible to conditionally
> use the attribute by querying __has_attribute.
> 
> This patch fixes this by adding the attribute to the attribute table
> only if the target supports init priorities, so that __has_attribute
> returns false appropriately.  Thus on such targets we'll now treat it as
> just another unrecognized attribute, so using it gives a -Wattribute
> warning instead of an error.

OK.

> gcc/cp/ChangeLog:
> 
> 	* tree.cc (cxx_attribute_table): Add entry for init_priority
> 	only if SUPPORTS_INIT_PRIORITY.
> 	(handle_init_priority_attribute): Assert SUPPORTS_INIT_PRIORITY
> 	is true.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/special/initpri3.C: New test.
> ---
>   gcc/cp/tree.cc                          | 20 +++++++-------------
>   gcc/testsuite/g++.dg/special/initpri3.C |  9 +++++++++
>   2 files changed, 16 insertions(+), 13 deletions(-)
>   create mode 100644 gcc/testsuite/g++.dg/special/initpri3.C
> 
> 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..a181abdd0b1
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/special/initpri3.C
> @@ -0,0 +1,9 @@
> +// Verify __has_attribute(init_priority) is false whenever the platform
> +// doesn't support it, and is treated as an unrecognized attribute.
> +
> +#if !__has_attribute(init_priority)
> +#error init_priority /* { dg-error "" "" { target { ! init_priority } } } */
> +#endif
> +
> +struct A { A(); } a __attribute__((init_priority(500)));
> +// { dg-warning "attribute directive ignored" "" { target { ! init_priority } } .-1 }


      parent reply	other threads:[~2022-11-04 15:56 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-04 15:05 Patrick Palka
2022-11-04 15:05 ` [PATCH 2/2] libstdc++: Move stream initialization into compiled library [PR44952] Patrick Palka
2022-11-04 15:24   ` Jonathan Wakely
2022-11-04 16:31     ` Patrick Palka
2022-11-04 16:42       ` Jonathan Wakely
2022-11-04 16:57   ` Florian Weimer
2022-11-04 17:47     ` Patrick Palka
2022-11-04 18:16       ` Florian Weimer
2022-11-15 16:10   ` Hans-Peter Nilsson
2022-11-04 15:55 ` Jason Merrill [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=9952e423-4e25-4ba5-8236-5fbb3b027c9d@redhat.com \
    --to=jason@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=libstdc++@gcc.gnu.org \
    --cc=ppalka@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).