diff options
author | Joseph Myers <joseph@codesourcery.com> | 2020-11-12 21:13:51 +0000 |
---|---|---|
committer | Joseph Myers <joseph@codesourcery.com> | 2020-11-12 21:13:51 +0000 |
commit | 1d00f8c86324c40ab2ba7933366d380e32c0a94a (patch) | |
tree | d58b866de3af6d6321b2e66f9f6ea2091d5cb475 /gcc/doc/cpp.texi | |
parent | 6fcc3cac42998081dbbb1d3389bd42833ad9fdf4 (diff) | |
download | gcc-1d00f8c86324c40ab2ba7933366d380e32c0a94a.zip gcc-1d00f8c86324c40ab2ba7933366d380e32c0a94a.tar.gz gcc-1d00f8c86324c40ab2ba7933366d380e32c0a94a.tar.bz2 |
c: C2x __has_c_attribute
C2x adds the __has_c_attribute preprocessor operator, similar to C++
__has_cpp_attribute.
GCC implements __has_cpp_attribute as exactly equivalent to
__has_attribute. (The documentation says they differ regarding the
values returned for standard attributes, but that's actually only a
matter of the particular nonzero value returned not being specified in
the documentation for __has_attribute; the implementation makes no
distinction between the two.)
I don't think having them exactly equivalent is actually correct,
either for __has_cpp_attribute or for __has_c_attribute.
Specifically, I think it is only correct for __has_cpp_attribute or
__has_c_attribute to return nonzero if the given attribute is
supported, with the particular pp-tokens passed to __has_cpp_attribute
or __has_c_attribute, with [[]] syntax, not if it's only accepted in
__attribute__ or with gnu:: added in [[]]. For example, they should
return nonzero for gnu::packed, but zero for plain packed, because
[[gnu::packed]] is accepted but [[packed]] is ignored as not a
standard attribute.
This patch implements that for __has_c_attribute, leaving any changes
to __has_cpp_attribute for the C++ maintainers. A new
BT_HAS_STD_ATTRIBUTE is added for __has_c_attribute (which I think,
based on the above, would actually be correct to use for
__has_cpp_attribute as well). The code in c_common_has_attribute that
deals with scopes has its C++ conditional removed; instead, whether
the language is C or C++ is used only to determine the numeric values
returned for standard attributes (and which standard attributes are
handled there at all). A new argument is passed to
c_common_has_attribute to distinguish BT_HAS_STD_ATTRIBUTE from
BT_HAS_ATTRIBUTE, and that argument is used to stop attributes with no
scope specified from being accepted with __has_c_attribute unless they
are one of the known standard attributes and so handled specially.
Although the standard specify constants ending with 'L' as the values
for the standard attributes, there is no correctness issue with the
lack of code in GCC to add that 'L' to the expansion:
__has_c_attribute and __has_cpp_attribute are expanded in #if after
other macro expansion has occurred, with no semantics being specified
if they occur outside #if, so there is no way for a conforming program
to inspect the exact text of the expansion of those macros, only to
use the resulting pp-number in a #if expression, where long and int
have the same set of values.
Bootstrapped with no regressions for x86_64-pc-linux-gnu.
gcc/
2020-11-12 Joseph Myers <joseph@codesourcery.com>
* doc/cpp.texi (__has_attribute): Document when scopes are allowed
for C.
(__has_c_attribute): New.
gcc/c-family/
2020-11-12 Joseph Myers <joseph@codesourcery.com>
* c-lex.c (c_common_has_attribute): Take argument std_syntax.
Allow scope for C. Handle standard attributes for C. Do not
accept unscoped attributes if std_syntax and not handled as
standard attributes.
* c-common.h (c_common_has_attribute): Update prototype.
gcc/testsuite/
2020-11-12 Joseph Myers <joseph@codesourcery.com>
* gcc.dg/c2x-has-c-attribute-1.c, gcc.dg/c2x-has-c-attribute-2.c,
gcc.dg/c2x-has-c-attribute-3.c, gcc.dg/c2x-has-c-attribute-4.c:
New tests.
libcpp/
2020-11-12 Joseph Myers <joseph@codesourcery.com>
* include/cpplib.h (struct cpp_callbacks): Add bool argument to
has_attribute.
(enum cpp_builtin_type): Add BT_HAS_STD_ATTRIBUTE.
* init.c (builtin_array): Add __has_c_attribute.
(cpp_init_special_builtins): Handle BT_HAS_STD_ATTRIBUTE.
* macro.c (_cpp_builtin_macro_text): Handle BT_HAS_STD_ATTRIBUTE.
Update call to has_attribute for BT_HAS_ATTRIBUTE.
* traditional.c (fun_like_macro): Handle BT_HAS_STD_ATTRIBUTE.
Diffstat (limited to 'gcc/doc/cpp.texi')
-rw-r--r-- | gcc/doc/cpp.texi | 21 |
1 files changed, 19 insertions, 2 deletions
diff --git a/gcc/doc/cpp.texi b/gcc/doc/cpp.texi index 33f876a..291e146 100644 --- a/gcc/doc/cpp.texi +++ b/gcc/doc/cpp.texi @@ -3159,6 +3159,7 @@ directive}: @samp{#if}, @samp{#ifdef} or @samp{#ifndef}. * Elif:: * @code{__has_attribute}:: * @code{__has_cpp_attribute}:: +* @code{__has_c_attribute}:: * @code{__has_builtin}:: * @code{__has_include}:: @end menu @@ -3432,8 +3433,9 @@ condition succeeds after the original @samp{#if} and all previous The special operator @code{__has_attribute (@var{operand})} may be used in @samp{#if} and @samp{#elif} expressions to test whether the attribute referenced by its @var{operand} is recognized by GCC. Using the operator -in other contexts is not valid. In C code, @var{operand} must be -a valid identifier. In C++ code, @var{operand} may be optionally +in other contexts is not valid. In C code, if compiling for strict +conformance to standards before C2x, @var{operand} must be +a valid identifier. Otherwise, @var{operand} may be optionally introduced by the @code{@var{attribute-scope}::} prefix. The @var{attribute-scope} prefix identifies the ``namespace'' within which the attribute is recognized. The scope of GCC attributes is @@ -3479,6 +3481,21 @@ information including the dates of the introduction of current standard attributes, see @w{@uref{https://isocpp.org/std/standing-documents/sd-6-sg10-feature-test-recommendations/, SD-6: SG10 Feature Test Recommendations}}. +@node @code{__has_c_attribute} +@subsection @code{__has_c_attribute} +@cindex @code{__has_c_attribute} + +The special operator @code{__has_c_attribute (@var{operand})} may be +used in @samp{#if} and @samp{#elif} expressions in C code to test +whether the attribute referenced by its @var{operand} is recognized by +GCC in attributes using the @samp{[[]]} syntax. GNU attributes must +be specified with the scope @samp{gnu} or @samp{__gnu__} with +@code{__has_c_attribute}. When @var{operand} designates a supported +standard attribute it evaluates to an integer constant of the form +@code{YYYYMM} indicating the year and month when the attribute was +first introduced into the C standard, or when the syntax of operands +to the attribute was extended in the C standard. + @node @code{__has_builtin} @subsection @code{__has_builtin} @cindex @code{__has_builtin} |