Properly handle C2x attributes on types.

attribs.c has code to ignore all scoped attributes appertaining to
types except when they are part of the definition of that type.

I think the premise of that code is incorrect, and its presence is a
bug; such attributes are clearly valid in both C and C++, which
explicitly specify that attributes in certain syntactic positions
appertain to a particular type, only for that use of that type and not
for other uses of the same type specifiers without that attribute
specified, and while the standard attributes in C2x aren't relevant in
such contexts, some gnu:: attributes certainly are.  Where some
attributes are invalid on some types in such contexts, that's a matter
for the individual attribute handlers to diagnose (or the front end if
the requirements on a standard attribute in the standard are more
strict than those of a handler shared with a GNU attribute).

Thus, this patch removes the bogus code to allow such attributes to be
used.  Doing so (and adding tests for attributes in such positions)
shows up that the logic in the C front end for creating the
c_declarator structures for such attributes put them in the wrong
place relative to the structures for function and array types, and the
logic for postfix attributes on a list of declaration specifiers
failed to handle some cases, so those bugs are also fixed in this
patch.

Bootstrapped with no regressions for x86_64-pc-linux-gnu.

gcc:
	* attribs.c (decl_attributes): Do not ignore C++11 attributes on
	types.

gcc/c:
	* c-tree.h (struct c_declarator): Use a structure for id member.
	* c-decl.c (grokdeclarator): Extract attributes from cdk_id
	declarators at the start, not when handling individual declarators
	later.  Use u.id.id instead of u.id.
	(grokfield): Use u.id.id instead of u.id.
	(build_id_declarator): Set u.id.id and u.id.attrs.
	(finish_declspecs): Handle postfix attributes in case of typedef
	name or typeof used.
	* c-parser.c (c_parser_direct_declarator)
	(c_parser_direct_declarator_inner): Place declarator for
	attributes inside that for function or array, not outside.  Set
	u.id.attrs for identifiers.
	(c_parser_parameter_declaration): Use u.id.id instead of u.id.
	* gimple-parser.c (c_parser_gimple_declaration): Use u.id.id
	instead of u.id.

gcc/testsuite:
	* gcc.dg/gnu2x-attrs-1.c: Do not expect message about attributes
	appertaining to types.
	* gcc.dg/gnu2x-attrs-2.c: New test.
	* g++.dg/cpp0x/gen-attrs-1.C, g++.dg/cpp0x/gen-attrs-22.C,
	g++.dg/cpp0x/gen-attrs-4.C, g++.dg/cpp0x/lambda/lambda-attr1.C:
	Update expected diagnostics.

From-SVN: r278683
This commit is contained in:
Joseph Myers 2019-11-25 13:42:49 +00:00 committed by Joseph Myers
parent 6cebc6cbbb
commit 1723e1bedb
14 changed files with 115 additions and 64 deletions

View file

@ -1,3 +1,8 @@
2019-11-25 Joseph Myers <joseph@codesourcery.com>
* attribs.c (decl_attributes): Do not ignore C++11 attributes on
types.
2019-11-25 Bernd Schmidt <bernds_cb1@t-online.de>
* config/m68k/m68k.c (output_move_himode, output_move_qimode):

View file

@ -593,21 +593,6 @@ decl_attributes (tree *node, tree attributes, int flags,
}
gcc_assert (is_attribute_p (spec->name, name));
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;
}
if (spec->decl_required && !DECL_P (*anode))
{
if (flags & ((int) ATTR_FLAG_DECL_NEXT

View file

@ -1,3 +1,21 @@
2019-11-25 Joseph Myers <joseph@codesourcery.com>
* c-tree.h (struct c_declarator): Use a structure for id member.
* c-decl.c (grokdeclarator): Extract attributes from cdk_id
declarators at the start, not when handling individual declarators
later. Use u.id.id instead of u.id.
(grokfield): Use u.id.id instead of u.id.
(build_id_declarator): Set u.id.id and u.id.attrs.
(finish_declspecs): Handle postfix attributes in case of typedef
name or typeof used.
* c-parser.c (c_parser_direct_declarator)
(c_parser_direct_declarator_inner): Place declarator for
attributes inside that for function or array, not outside. Set
u.id.attrs for identifiers.
(c_parser_parameter_declaration): Use u.id.id instead of u.id.
* gimple-parser.c (c_parser_gimple_declaration): Use u.id.id
instead of u.id.
2019-11-22 Jakub Jelinek <jakub@redhat.com>
PR c/90677

View file

@ -5951,6 +5951,7 @@ grokdeclarator (const struct c_declarator *declarator,
bool array_parm_static = false;
bool array_parm_vla_unspec_p = false;
tree returned_attrs = NULL_TREE;
tree decl_id_attrs = NULL_TREE;
bool bitfield = width != NULL;
tree element_type;
tree orig_qual_type = NULL;
@ -6013,8 +6014,9 @@ grokdeclarator (const struct c_declarator *declarator,
case cdk_id:
loc = decl->id_loc;
if (decl->u.id)
name = decl->u.id;
if (decl->u.id.id)
name = decl->u.id.id;
decl_id_attrs = decl->u.id.attrs;
if (first_non_attr_kind == cdk_attrs)
first_non_attr_kind = decl->kind;
decl = 0;
@ -6300,7 +6302,9 @@ grokdeclarator (const struct c_declarator *declarator,
Standard attributes applied to a function or array
declarator apply exactly to that type; standard
attributes applied to the identifier apply to the
declaration rather than to the type. */
declaration rather than to the type, and are specified
using a cdk_id declarator rather than using
cdk_attrs. */
inner_decl = declarator;
while (inner_decl->kind == cdk_attrs)
inner_decl = inner_decl->declarator;
@ -6313,16 +6317,10 @@ grokdeclarator (const struct c_declarator *declarator,
else if (inner_decl->kind == cdk_array)
attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
}
if (cxx11_attribute_p (attrs) && inner_decl->kind == cdk_id)
returned_attrs = chainon (returned_attrs, attrs);
else
{
attrs = c_warn_type_attributes (attrs);
returned_attrs = decl_attributes (&type,
chainon (returned_attrs,
attrs),
attr_flags);
}
attrs = c_warn_type_attributes (attrs);
returned_attrs = decl_attributes (&type,
chainon (returned_attrs, attrs),
attr_flags);
break;
}
case cdk_array:
@ -6883,6 +6881,7 @@ grokdeclarator (const struct c_declarator *declarator,
}
}
*decl_attrs = chainon (returned_attrs, *decl_attrs);
*decl_attrs = chainon (decl_id_attrs, *decl_attrs);
/* Now TYPE has the actual type, apart from any qualifiers in
TYPE_QUALS. */
@ -7017,7 +7016,7 @@ grokdeclarator (const struct c_declarator *declarator,
type = c_build_qualified_type (type, type_quals, orig_qual_type,
orig_qual_indirect);
decl = build_decl (declarator->id_loc,
TYPE_DECL, declarator->u.id, type);
TYPE_DECL, declarator->u.id.id, type);
if (declspecs->explicit_signed_p)
C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
if (declspecs->inline_p)
@ -7025,9 +7024,9 @@ grokdeclarator (const struct c_declarator *declarator,
if (declspecs->noreturn_p)
pedwarn (loc, 0,"typedef %q+D declared %<_Noreturn%>", decl);
if (warn_cxx_compat && declarator->u.id != NULL_TREE)
if (warn_cxx_compat && declarator->u.id.id != NULL_TREE)
{
struct c_binding *b = I_TAG_BINDING (declarator->u.id);
struct c_binding *b = I_TAG_BINDING (declarator->u.id.id);
if (b != NULL
&& b->decl != NULL_TREE
@ -7159,7 +7158,7 @@ grokdeclarator (const struct c_declarator *declarator,
type = c_build_qualified_type (type, type_quals);
decl = build_decl (declarator->id_loc,
PARM_DECL, declarator->u.id, type);
PARM_DECL, declarator->u.id.id, type);
if (size_varies)
C_DECL_VARIABLE_SIZE (decl) = 1;
C_ARRAY_PARAMETER (decl) = array_parameter_p;
@ -7221,9 +7220,9 @@ grokdeclarator (const struct c_declarator *declarator,
type = c_build_qualified_type (type, type_quals, orig_qual_type,
orig_qual_indirect);
decl = build_decl (declarator->id_loc,
FIELD_DECL, declarator->u.id, type);
FIELD_DECL, declarator->u.id.id, type);
DECL_NONADDRESSABLE_P (decl) = bitfield;
if (bitfield && !declarator->u.id)
if (bitfield && !declarator->u.id.id)
{
TREE_NO_WARNING (decl) = 1;
DECL_PADDING_P (decl) = 1;
@ -7259,7 +7258,7 @@ grokdeclarator (const struct c_declarator *declarator,
}
decl = build_decl (declarator->id_loc,
FUNCTION_DECL, declarator->u.id, type);
FUNCTION_DECL, declarator->u.id.id, type);
decl = build_decl_attribute_variant (decl, decl_attr);
if (type_quals & TYPE_QUAL_ATOMIC)
@ -7304,7 +7303,7 @@ grokdeclarator (const struct c_declarator *declarator,
/* Record presence of `inline' and `_Noreturn', if it is
reasonable. */
if (flag_hosted && MAIN_NAME_P (declarator->u.id))
if (flag_hosted && MAIN_NAME_P (declarator->u.id.id))
{
if (declspecs->inline_p)
pedwarn (loc, 0, "cannot inline function %<main%>");
@ -7345,8 +7344,8 @@ grokdeclarator (const struct c_declarator *declarator,
the 'extern' declaration is taken to refer to that decl.) */
if (extern_ref && current_scope != file_scope)
{
tree global_decl = identifier_global_value (declarator->u.id);
tree visible_decl = lookup_name (declarator->u.id);
tree global_decl = identifier_global_value (declarator->u.id.id);
tree visible_decl = lookup_name (declarator->u.id.id);
if (global_decl
&& global_decl != visible_decl
@ -7357,7 +7356,7 @@ grokdeclarator (const struct c_declarator *declarator,
}
decl = build_decl (declarator->id_loc,
VAR_DECL, declarator->u.id, type);
VAR_DECL, declarator->u.id.id, type);
if (size_varies)
C_DECL_VARIABLE_SIZE (decl) = 1;
@ -8009,7 +8008,7 @@ grokfield (location_t loc,
{
tree value;
if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
if (declarator->kind == cdk_id && declarator->u.id.id == NULL_TREE
&& width == NULL_TREE)
{
/* This is an unnamed decl.
@ -10295,7 +10294,8 @@ build_id_declarator (tree ident)
struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
ret->kind = cdk_id;
ret->declarator = 0;
ret->u.id = ident;
ret->u.id.id = ident;
ret->u.id.attrs = NULL_TREE;
/* Default value - may get reset to a more precise location. */
ret->id_loc = input_location;
return ret;
@ -11468,7 +11468,7 @@ finish_declspecs (struct c_declspecs *specs)
/* Set a dummy type. */
if (TREE_CODE (specs->type) == ERROR_MARK)
specs->type = integer_type_node;
return specs;
goto handle_postfix_attrs;
}
/* If none of "void", "_Bool", "char", "int", "float" or "double"
@ -11729,6 +11729,7 @@ finish_declspecs (struct c_declspecs *specs)
default:
gcc_unreachable ();
}
handle_postfix_attrs:
if (specs->type != NULL)
{
specs->postfix_attrs = c_warn_type_attributes (specs->postfix_attrs);

View file

@ -3857,11 +3857,7 @@ c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
inner->id_loc = c_parser_peek_token (parser)->location;
c_parser_consume_token (parser);
if (c_parser_nth_token_starts_std_attributes (parser, 1))
{
tree std_attrs = c_parser_std_attribute_specifier_sequence (parser);
if (std_attrs)
inner = build_attrs_declarator (std_attrs, inner);
}
inner->u.id.attrs = c_parser_std_attribute_specifier_sequence (parser);
return c_parser_direct_declarator_inner (parser, *seen_id, inner);
}
@ -3898,9 +3894,7 @@ c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
return NULL;
else
{
inner
= build_function_declarator (args,
build_id_declarator (NULL_TREE));
inner = build_id_declarator (NULL_TREE);
if (!(args->types
&& args->types != error_mark_node
&& TREE_CODE (TREE_VALUE (args->types)) == IDENTIFIER_NODE)
@ -3911,6 +3905,7 @@ c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
if (std_attrs)
inner = build_attrs_declarator (std_attrs, inner);
}
inner = build_function_declarator (args, inner);
return c_parser_direct_declarator_inner (parser, *seen_id,
inner);
}
@ -4028,7 +4023,6 @@ c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
static_seen, star_seen);
if (declarator == NULL)
return NULL;
inner = set_array_declarator_inner (declarator, inner);
if (c_parser_nth_token_starts_std_attributes (parser, 1))
{
tree std_attrs
@ -4036,6 +4030,7 @@ c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
if (std_attrs)
inner = build_attrs_declarator (std_attrs, inner);
}
inner = set_array_declarator_inner (declarator, inner);
return c_parser_direct_declarator_inner (parser, id_present, inner);
}
else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
@ -4052,7 +4047,6 @@ c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
return NULL;
else
{
inner = build_function_declarator (args, inner);
if (!(args->types
&& args->types != error_mark_node
&& TREE_CODE (TREE_VALUE (args->types)) == IDENTIFIER_NODE)
@ -4063,6 +4057,7 @@ c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
if (std_attrs)
inner = build_attrs_declarator (std_attrs, inner);
}
inner = build_function_declarator (args, inner);
return c_parser_direct_declarator_inner (parser, id_present, inner);
}
}
@ -4352,7 +4347,7 @@ c_parser_parameter_declaration (c_parser *parser, tree attrs,
c_declarator *id_declarator = declarator;
while (id_declarator && id_declarator->kind != cdk_id)
id_declarator = id_declarator->declarator;
location_t caret_loc = (id_declarator->u.id
location_t caret_loc = (id_declarator->u.id.id
? id_declarator->id_loc
: start_loc);
location_t param_loc = make_location (caret_loc, start_loc, end_loc);

View file

@ -460,9 +460,15 @@ struct c_declarator {
/* Except for cdk_id, the contained declarator. For cdk_id, NULL. */
struct c_declarator *declarator;
union {
/* For identifiers, an IDENTIFIER_NODE or NULL_TREE if an abstract
declarator. */
tree id;
/* For identifiers. */
struct {
/* An IDENTIFIER_NODE, or NULL_TREE if an abstract
declarator. */
tree id;
/* Any attributes (which apply to the declaration rather than to
the type described by the outer declarators). */
tree attrs;
} id;
/* For functions. */
struct c_arg_info *arg_info;
/* For arrays. */

View file

@ -2040,13 +2040,13 @@ c_parser_gimple_declaration (gimple_parser &parser)
unsigned version, ver_offset;
if (declarator->kind == cdk_id
&& is_gimple_reg_type (specs->type)
&& c_parser_parse_ssa_name_id (declarator->u.id,
&& c_parser_parse_ssa_name_id (declarator->u.id.id,
&version, &ver_offset)
/* The following restricts it to unnamed anonymous SSA names
which fails parsing of named ones in dumps (we could
decide to not dump their name for -gimple). */
&& ver_offset == 0)
c_parser_parse_ssa_name (parser, declarator->u.id, specs->type,
c_parser_parse_ssa_name (parser, declarator->u.id.id, specs->type,
version, ver_offset);
else
{

View file

@ -1,3 +1,12 @@
2019-11-25 Joseph Myers <joseph@codesourcery.com>
* gcc.dg/gnu2x-attrs-1.c: Do not expect message about attributes
appertaining to types.
* gcc.dg/gnu2x-attrs-2.c: New test.
* g++.dg/cpp0x/gen-attrs-1.C, g++.dg/cpp0x/gen-attrs-22.C,
g++.dg/cpp0x/gen-attrs-4.C, g++.dg/cpp0x/lambda/lambda-attr1.C:
Update expected diagnostics.
2019-11-25 Tamar Christina <tamar.christina@arm.com>
* gcc.dg/torture/fp-int-convert-timode-1.c: Always run if int128.

View file

@ -1,3 +1,3 @@
// { dg-do compile { target c++11 } }
int **** [[gnu::format(printf, 1, 2)]] foo(const char *, ...); // { dg-warning "ignored" }
int **** [[gnu::format(printf, 1, 2)]] foo(const char *, ...); // { dg-warning "only applies to function types" }

View file

@ -3,5 +3,5 @@
void f()
{
static_cast<float *[[gnu::unused]]>(0); // { dg-warning "ignored" }
static_cast<float *[[gnu::unused]]>(0);
}

View file

@ -21,7 +21,7 @@ void two [[gnu::unused]] (void) {}
[[gnu::unused]]
int
five(void)
[[noreturn]] // { dg-warning "ignored" }
[[noreturn]] // { dg-warning "does not apply to types" }
{ return 0; }
[[noreturn]]

View file

@ -1,3 +1,3 @@
// { dg-do compile { target c++11 } }
auto l = []() [[noreturn]] {}; // { dg-warning "ignored" }
auto l = []() [[noreturn]] {}; // { dg-warning "does not apply to types" }

View file

@ -7,8 +7,7 @@ void f (void) {};
[[gnu::alias("f")]] void g (void); /* { dg-error "only weak" "" { target *-*-darwin* } } */
void [[gnu::alias("f")]] h (void); /* { dg-warning "ignored" } */
/* { dg-message "that appertains to a type-specifier" "appertains" { target *-*-* } .-1 } */
void [[gnu::alias("f")]] h (void); /* { dg-warning "does not apply to types" } */
struct [[gnu::packed]] s { int a; char b; };
_Static_assert (sizeof (struct s) == (sizeof (int) + sizeof (char)));

View file

@ -0,0 +1,33 @@
/* Test C2x attribute syntax. Test GNU attributes appertain to
appropriate constructs. Attributes on types not being defined at
the time. */
/* { dg-do compile } */
/* { dg-options "-std=gnu2x -Wformat" } */
typedef void va_type (const char *, ...);
typedef va_type [[gnu::format (printf, 1, 2)]] printf_like_1;
typedef void printf_like_2 (const char *, ...) [[gnu::format (printf, 1, 2)]];
typedef __typeof__ (void (const char *, ...) [[gnu::format (printf, 1, 2)]])
printf_like_3;
va_type func1;
printf_like_1 func2;
printf_like_2 func3;
printf_like_3 func4;
va_type [[gnu::format (printf, 1, 2)]] *func5 (void);
void
func_test (void)
{
func1 ("%s", 1);
func2 ("%s", 1); /* { dg-warning "expects argument" } */
func3 ("%s", 1); /* { dg-warning "expects argument" } */
func4 ("%s", 1); /* { dg-warning "expects argument" } */
func5 () ("%s", 1); /* { dg-warning "expects argument" } */
}
typedef int A[2];
__typeof__ (int [[gnu::deprecated]]) var1; /* { dg-warning "deprecated" } */
__typeof__ (A [[gnu::deprecated]]) var2; /* { dg-warning "deprecated" } */
__typeof__ (int [3] [[gnu::deprecated]]) var3; /* { dg-warning "deprecated" } */