Re: Support C2x [[]] attributes for C

2019-11-21 Thread Iain Sandoe

Joseph Myers  wrote:


On Mon, 18 Nov 2019, Joseph Myers wrote:


On Sat, 16 Nov 2019, Iain Sandoe wrote:


Joseph Myers  wrote:


This patch adds support for the C2x [[]] attribute syntax to the C
front end.



gcc.dg/gnu2x-attrs-1.c: New tests.


This test fails on targets without symbol alias support, but it would
be most unfortunate to skip it entirely with the usual dg-requires.

In this instance, I propose to expect the error for Darwin as per the  
patch

below, but open to other suggestions, if you prefer something different.

OK / something else?


This patch is OK.


Actually, the committed change has incorrect syntax so causes

FAIL: gcc.dg/gnu2x-attrs-1.c *-*-darwin* (test for errors, line 8)

everywhere that's not Darwin.  You're putting "*-*-darwin*" in the "string
that forms part of the test name" slot, when you need to put something
else in that slot and then have { target *-*-darwin* } in the following
slot.


yes, the goof was spotted, and will be fixed shortly.

sorry,
Iain



Re: Support C2x [[]] attributes for C

2019-11-21 Thread Joseph Myers
On Mon, 18 Nov 2019, Joseph Myers wrote:

> On Sat, 16 Nov 2019, Iain Sandoe wrote:
> 
> > Joseph Myers  wrote:
> > 
> > > This patch adds support for the C2x [[]] attribute syntax to the C
> > > front end.  
> > 
> > >   gcc.dg/gnu2x-attrs-1.c: New tests.
> > 
> > This test fails on targets without symbol alias support, but it would
> > be most unfortunate to skip it entirely with the usual dg-requires.
> > 
> > In this instance, I propose to expect the error for Darwin as per the patch
> > below, but open to other suggestions, if you prefer something different.
> > 
> > OK / something else?
> 
> This patch is OK.

Actually, the committed change has incorrect syntax so causes

FAIL: gcc.dg/gnu2x-attrs-1.c *-*-darwin* (test for errors, line 8)

everywhere that's not Darwin.  You're putting "*-*-darwin*" in the "string 
that forms part of the test name" slot, when you need to put something 
else in that slot and then have { target *-*-darwin* } in the following 
slot.

-- 
Joseph S. Myers
jos...@codesourcery.com


Re: Support C2x [[]] attributes for C

2019-11-18 Thread Joseph Myers
On Sat, 16 Nov 2019, Iain Sandoe wrote:

> Joseph Myers  wrote:
> 
> > This patch adds support for the C2x [[]] attribute syntax to the C
> > front end.  
> 
> > gcc.dg/gnu2x-attrs-1.c: New tests.
> 
> This test fails on targets without symbol alias support, but it would
> be most unfortunate to skip it entirely with the usual dg-requires.
> 
> In this instance, I propose to expect the error for Darwin as per the patch
> below, but open to other suggestions, if you prefer something different.
> 
> OK / something else?

This patch is OK.

The point of this test is to verify GNU attributes work in C2x syntax, as 
applied to various constructs, so "alias" is being used as an example of 
an attribute that applies to declarations but clearly does not make sense 
on types.  It's probably hard to be 100% portable for all such tests.

-- 
Joseph S. Myers
jos...@codesourcery.com


Re: Support C2x [[]] attributes for C

2019-11-16 Thread Iain Sandoe
Joseph Myers  wrote:

> This patch adds support for the C2x [[]] attribute syntax to the C
> front end.  

>   gcc.dg/gnu2x-attrs-1.c: New tests.

This test fails on targets without symbol alias support, but it would
be most unfortunate to skip it entirely with the usual dg-requires.

In this instance, I propose to expect the error for Darwin as per the patch
below, but open to other suggestions, if you prefer something different.

OK / something else?

thanks
Iain

gcc/testsuite/ChangeLog:

2019-11-16  Iain Sandoe  

* gcc.dg/gnu2x-attrs-1.c: Expect the alias case to produce
an error on Darwin platforms.


diff --git a/gcc/testsuite/gcc.dg/gnu2x-attrs-1.c 
b/gcc/testsuite/gcc.dg/gnu2x-attrs-1.c
index df22fb3..fe7e95b 100644
--- a/gcc/testsuite/gcc.dg/gnu2x-attrs-1.c
+++ b/gcc/testsuite/gcc.dg/gnu2x-attrs-1.c
@@ -5,7 +5,7 @@
 
 void f (void) {};
 
-[[gnu::alias("f")]] void g (void);
+[[gnu::alias("f")]] void g (void); /* { dg-error "only weak" *-*-darwin* } */
 
 void [[gnu::alias("f")]] h (void); /* { dg-warning "ignored" } */
 /* { dg-message "that appertains to a type-specifier" "appertains" { target 
*-*-* } .-1 } */



Support C2x [[]] attributes for C

2019-11-13 Thread Joseph Myers
This patch adds support for the C2x [[]] attribute syntax to the C
front end.  Support is only added for C at this point, not for
Objective-C; I intend to add the unbounded lookahead required to
support it for Objective-C in a followup patch, but maybe not in
development stage 1.

The syntax is supported in all relevant places where the standard says
it is supported, but support is not added for the individual
attributes specified in C2x (all of which are optional to support).  I
expect to add support for some of them in followup patches; all except
nodiscard can be mapped directly to the semantics of an existing GNU
attribute (subject to extra checks for invalid usages such as the same
attribute being used more than once inside a single [[]]), and the
fallthrough attribute already works after this patch because of
existing special-case code handling it (but without some of the checks
for invalid usage being present).

Note that the four functions c_token_starts_declspecs,
c_token_starts_declaration, c_parser_next_token_starts_declspecs and
c_parser_next_tokens_start_declaration do *not* accept "[[".  This is
analogous with the handling of __extension__: both cases have the
property that they can start either a declaration or some other
statements and so need an unbounded number of tokens to be parsed in
the caller before it can find out what kind of syntactic construct
follows.  Note also that, while I updated all places calling those
functions for standard C syntax to handle attributes if applicable, I
did not do anything regarding calls to such functions for OpenMP or
OpenACC constructs.  Thus, if there are such constructs using such
functions where "[[" *should* be accepted as a possible start to a
declaration, the code for parsing those constructs should be updated
accordingly.

Although all cases of the syntax are handled, and attributes applied
to the constructs the standard says they should be (with less laxity
than there is for GNU attributes to allow an attribute applied to one
construct to be moved automatically to another one), there is a major
limitation in the existing language-independent code in attribs.c
preventing most cases of type attributes from working.  The following
code has been present with minor changes since the first support for
[[]] attributes for C++ was added:

  if (TYPE_P (*node)
  && cxx11_attr_p
  && !(flags & ATTR_FLAG_TYPE_IN_PLACE))
{
  /* This is a c++11 attribute that appertains to a
 type-specifier, outside of the definition of, a class
 type.  Ignore it.  */
  auto_diagnostic_group d;
  if (warning (OPT_Wattributes, "attribute ignored"))
inform (input_location,
"an attribute that appertains to a type-specifier "
"is ignored");
  continue;
}

I see no justification for this in general for either C or C++ and so
propose to remove or restrict it in a followup bug-fix patch.  Both C
and C++ are clear about attributes in certain places (at the end of
declaration specifiers, or after function or array declarators)
appertaining to a specific type (and explicitly say, in the case of
attributes at the end of declaration specifiers, that they only apply
for that particular use of that type, not for subsequent uses of the
same type without the attributes).  Thus it seems clear to me that,
for example,

int [[gnu::mode(DI)]] x;

ought to be accepted as an analogue in [[]] syntax for

int __attribute__((mode(DI))) x;

(or strictly as an analogue for a version of that with extra
parentheses to make the GNU attribute bind properly to the type rather
than being automatically moved from the declaration to the type).
There are certain cases where an attribute *does* only make sense for
the definition of a type (e.g. "packed" on structure types), but those
should already be handled in the individual attribute handlers (such
as handle_packed_attribute, which already has code to deal with that
issue).  So my inclination is that the above-quoted check in attribs.c
should simply be removed, but failing that it should be restricted to
structure and union types (and such a change would be a bug-fix).
That would then allow various cases of [[]] attributes on types to
work properly.

Bootstrapped with no regressions on x86_64-pc-linux-gnu.  Applied to 
mainline.

gcc/c:
2019-11-14  Joseph Myers  

* c-tree.h (enum c_typespec_kind): Add ctsk_tagref_attrs and
ctsk_tagfirstref_attrs.
(struct c_declspecs): Update description of attrs.  Add
postfix_attrs and non_std_attrs_seen_p.  Increase size of
typespec_kind bit-field.
(c_warn_unused_attributes): New declaration.
(parser_xref_tag): Update prototype.
* c-decl.c (c_warn_unused_attributes): New function.
(shadow_tag_warned): Handle ctsk_tagfirstref_attrs and
ctsk_tagref_attrs.  Handle attribute declarations.