[Bug c/101682] gcc incorrectly rejects C2x attributes after declaration-specifiers
joseph at codesourcery dot com
gcc-bugzilla@gcc.gnu.org
Fri Jul 30 14:34:40 GMT 2021
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101682
--- Comment #4 from joseph at codesourcery dot com <joseph at codesourcery dot com> ---
On Thu, 29 Jul 2021, eggert at gnu dot org via Gcc-bugs wrote:
> The grammar at the start of section 6.7 of the current C2x draft (N2596) says
> that attribute specifiers may appear either before or after declaration
> specifiers. Unfortunately, GCC rejects attribution specifiers after declaration
> specifiers. For example, for this this source code:
>
> extern [[nodiscard]] int f (void);
That's not syntactically valid. You can have attribute specifiers before
declaration specifiers in a declaration, or at the end of declaration
specifiers, but you can't have them in the middle; the syntax productions
don't allow it.
> extern int [[nodiscard]] g (void);
> int [[nodiscard]] h (void);
Those violate the constraints on the nodiscard attribute. As per 6.7#8,
"The optional attribute specifier sequence terminating a sequence of
declaration specifiers appertains to the type determined by the preceding
sequence of declaration specifiers.". That is, this syntax is applying
the attribute to the type int, but the constraints for nodiscard
(6.7.11.2#1) say "The nodiscard attribute shall be applied to the
identifier in a function declaration or to the definition of a structure,
union, or enumeration type.".
To apply the nodiscard attribute to a function, either place it before the
declaration specifiers (in which case it applies to all declared entities
in that declaration, so don't so that if you have multiple declarators in
the declaration but only want nodiscard to apply to some of them), or
after the identifier in the declarator.
More information about the Gcc-bugs
mailing list