[Bug c/101682] gcc incorrectly rejects C2x attributes after declaration-specifiers

2023-02-14 Thread sam at gentoo dot org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101682

Sam James  changed:

   What|Removed |Added

 CC||sam at gentoo dot org

--- Comment #7 from Sam James  ---
*** Bug 108796 has been marked as a duplicate of this bug. ***

[Bug c/101682] gcc incorrectly rejects C2x attributes after declaration-specifiers

2021-07-30 Thread jakub at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101682

Jakub Jelinek  changed:

   What|Removed |Added

 CC||jakub at gcc dot gnu.org

--- Comment #6 from Jakub Jelinek  ---
(In reply to Paul Eggert from comment #5)
> PS. It is a bit annoying that one can't do this:
> 
> int f (int) [[nodiscard]];

This appertains to the function type, not the function declaration.

[Bug c/101682] gcc incorrectly rejects C2x attributes after declaration-specifiers

2021-07-30 Thread eggert at gnu dot org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101682

Paul Eggert  changed:

   What|Removed |Added

 Status|UNCONFIRMED |RESOLVED
 Resolution|--- |INVALID

--- Comment #5 from Paul Eggert  ---
Thanks for the clarification, Joseph. I'll fix Gnulib accordingly. Marking the
bug as RESOLVED INVALID.

FWIW the comment 2 code works even with bleeding-edge clang because
__has_c_attribute (nodiscard) is still 0 in there.


PS. It is a bit annoying that one can't do this:

int f (int) [[nodiscard]];

as that would match the existing Gnulib style of putting attributes at the end
of the function declarations. I don't know whether such a thing could be added
as a GCC extension, but I suppose it's not worth the trouble since it wouldn't
be portable as per draft C2x.

[Bug c/101682] gcc incorrectly rejects C2x attributes after declaration-specifiers

2021-07-30 Thread joseph at codesourcery dot com via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101682

--- Comment #4 from 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.

[Bug c/101682] gcc incorrectly rejects C2x attributes after declaration-specifiers

2021-07-29 Thread pinskia at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101682

--- Comment #3 from Andrew Pinski  ---
(In reply to Paul Eggert from comment #2)
> Clang's warnings are not a problem here, because in clang 12.0.0 (the
> current release) __has_c_attribute(nodiscard) is false, so code like this
> works:

I was testing the trunk clang git commit
ef8c6849a235e97b8b981e0f998d430fdbd7bc2a  not the release.

[Bug c/101682] gcc incorrectly rejects C2x attributes after declaration-specifiers

2021-07-29 Thread eggert at gnu dot org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101682

--- Comment #2 from Paul Eggert  ---
Clang's warnings are not a problem here, because in clang 12.0.0 (the current
release) __has_c_attribute(nodiscard) is false, so code like this works:

#ifndef __has_c_attribute
# define __has_c_attribute(x) 0
#endif
#if __has_c_attribute (nodiscard)
# define NODISCARD [[nodiscard]]
#else
# define NODISCARD
#endif
extern NODISCARD int f (void);
extern int NODISCARD g (void);
int NODISCARD h (void);
NODISCARD extern int i (void);
NODISCARD int j (void);

Although draft C2x recommends this sort of macro preprocessing and it works
with Clang 12.0.0, this code doesn't work with GCC 11.2.0 due to its
incompatibility with draft C2x.

This is more what the original code looked like, by the way; I removed the
macro preprocessing to simplify the original bug report.

[Bug c/101682] gcc incorrectly rejects C2x attributes after declaration-specifiers

2021-07-29 Thread pinskia at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101682

--- Comment #1 from Andrew Pinski  ---
clang rejects/warnings similar to gcc:
:2:8: error: an attribute list cannot appear here
extern [[nodiscard]] int f (void);
   ^
:3:14: error: 'nodiscard' attribute cannot be applied to types
extern int [[nodiscard]] g (void);
 ^
:4:7: error: 'nodiscard' attribute cannot be applied to types
int [[nodiscard]] h (void);
  ^