diff options
author | Joseph Myers <joseph@codesourcery.com> | 2019-11-25 13:42:49 +0000 |
---|---|---|
committer | Joseph Myers <jsm28@gcc.gnu.org> | 2019-11-25 13:42:49 +0000 |
commit | 1723e1bedb9545c45619c4023729755243524584 (patch) | |
tree | 3338a8b65ec9bfb47d4bf0a79a04d572e4162ea0 /gcc/c | |
parent | 6cebc6cbbb801183090dbb2752aa6b698331a31c (diff) | |
download | gcc-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')
-rw-r--r-- | gcc/c/ChangeLog | 18 | ||||
-rw-r--r-- | gcc/c/c-decl.c | 55 | ||||
-rw-r--r-- | gcc/c/c-parser.c | 17 | ||||
-rw-r--r-- | gcc/c/c-tree.h | 12 | ||||
-rw-r--r-- | gcc/c/gimple-parser.c | 4 |
5 files changed, 63 insertions, 43 deletions
diff --git a/gcc/c/ChangeLog b/gcc/c/ChangeLog index cb1ea09..9f0b8ce 100644 --- a/gcc/c/ChangeLog +++ b/gcc/c/ChangeLog @@ -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 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); diff --git a/gcc/c/c-parser.c b/gcc/c/c-parser.c index 03194b4..5aa42e2 100644 --- a/gcc/c/c-parser.c +++ b/gcc/c/c-parser.c @@ -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); diff --git a/gcc/c/c-tree.h b/gcc/c/c-tree.h index dffefa3..cdfb908 100644 --- a/gcc/c/c-tree.h +++ b/gcc/c/c-tree.h @@ -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. */ diff --git a/gcc/c/gimple-parser.c b/gcc/c/gimple-parser.c index c16d0df..43e9437 100644 --- a/gcc/c/gimple-parser.c +++ b/gcc/c/gimple-parser.c @@ -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 { |