aboutsummaryrefslogtreecommitdiff
path: root/gcc/c/c-decl.c
diff options
context:
space:
mode:
authorJoseph Myers <joseph@codesourcery.com>2019-11-25 13:42:49 +0000
committerJoseph Myers <jsm28@gcc.gnu.org>2019-11-25 13:42:49 +0000
commit1723e1bedb9545c45619c4023729755243524584 (patch)
tree3338a8b65ec9bfb47d4bf0a79a04d572e4162ea0 /gcc/c/c-decl.c
parent6cebc6cbbb801183090dbb2752aa6b698331a31c (diff)
downloadgcc-1723e1bedb9545c45619c4023729755243524584.zip
gcc-1723e1bedb9545c45619c4023729755243524584.tar.gz
gcc-1723e1bedb9545c45619c4023729755243524584.tar.bz2
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
Diffstat (limited to 'gcc/c/c-decl.c')
-rw-r--r--gcc/c/c-decl.c55
1 files changed, 28 insertions, 27 deletions
diff --git a/gcc/c/c-decl.c b/gcc/c/c-decl.c
index 7fd3cc2..a14f782 100644
--- a/gcc/c/c-decl.c
+++ b/gcc/c/c-decl.c
@@ -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);