From: Joseph Myers Date: Wed, 20 Nov 2019 00:13:51 +0000 (+0000) Subject: Add more pedwarns for [[]] C attributes on types. X-Git-Tag: misc/cutover-git~1062 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8c5b727acc19d3ee415e0d0e5ba84af7d1843dab;p=thirdparty%2Fgcc.git Add more pedwarns for [[]] C attributes on types. The standard [[]] attributes currently defined in C2x are all not valid on types not being defined at the time. Use on such types results in a warning from attribs.c about attributes appertaining to types (the warning that I think is bogus in general for both C and C++, applying as it does to all [[]] attributes including gnu:: ones that are perfectly meaningful on types not being defined and work fine when __attribute__ syntax is used instead). If that warning is removed (as I intend to do in a subsequent patch), warnings may or may not result from the attribute handlers, depending on whether those particular attribute handlers consider the attributes meaningful in such a context. In C, however, the rules about where each [[]] attribute is valid are constraints, so a pedwarn, not a warning, is required. Because some handlers are shared between standard and gnu:: attributes, there can be cases that are valid for the GNU attribute variant but not for the standard one. So in general it is not correct to rely on the attribute handlers to give all required pedwarns (although in some cases, a pedwarn in the attribute handler is in appropriate way of diagnosing an invalid use); they not have the information about whether the attribute was a gnu:: one and can legitimately accept a wider range of uses for the gnu:: attributes. This patch ensures appropriate diagnostics for invalid uses of C2x standard attributes on types, and so helps pave the way for the subsequent removal of the bogus check in attribs.c, by adding a check run in the front end before calling decl_attributes; this check removes the attributes from the list after calling pedwarn to avoid subsequent duplicate warnings. Bootstrapped with no regressions for x86_64-pc-linux-gnu. gcc/c: * c-decl.c (c_warn_type_attributes): New function. (groktypename, grokdeclarator, finish_declspecs): Call c_warn_type_attributes before applying attributes to types. * c-tree.h (c_warn_type_attributes): Declare. gcc/testsuite: * gcc.dg/c2x-attr-deprecated-2.c, gcc.dg/c2x-attr-fallthrough-2.c, gcc.dg/c2x-attr-maybe_unused-2.c: Expect errors for invalid uses of standard attributes on types. Add more tests of invalid uses on types. From-SVN: r278471 --- diff --git a/gcc/c/ChangeLog b/gcc/c/ChangeLog index fff7dc6328ab..3f42e40896a5 100644 --- a/gcc/c/ChangeLog +++ b/gcc/c/ChangeLog @@ -1,3 +1,10 @@ +2019-11-20 Joseph Myers + + * c-decl.c (c_warn_type_attributes): New function. + (groktypename, grokdeclarator, finish_declspecs): Call + c_warn_type_attributes before applying attributes to types. + * c-tree.h (c_warn_type_attributes): Declare. + 2019-11-19 Joseph Myers * c-decl.c (c_warn_unused_attributes): Use pedwarn not warning for diff --git a/gcc/c/c-decl.c b/gcc/c/c-decl.c index 746339d12edc..d153de2e2ad0 100644 --- a/gcc/c/c-decl.c +++ b/gcc/c/c-decl.c @@ -4525,6 +4525,26 @@ c_warn_unused_attributes (tree attrs) warning (OPT_Wattributes, "%qE attribute ignored", get_attribute_name (t)); } + +/* Warn for standard attributes being applied to a type that is not + being defined, where that is a constraint violation, and return a + list of attributes with them removed. */ + +tree +c_warn_type_attributes (tree attrs) +{ + tree *attr_ptr = &attrs; + while (*attr_ptr) + if (get_attribute_namespace (*attr_ptr) == NULL_TREE) + { + pedwarn (input_location, OPT_Wattributes, "%qE attribute ignored", + get_attribute_name (*attr_ptr)); + *attr_ptr = TREE_CHAIN (*attr_ptr); + } + else + attr_ptr = &TREE_CHAIN (*attr_ptr); + return attrs; +} /* Called when a declaration is seen that contains no names to declare. If its type is a reference to a structure, union or enum inherited @@ -4883,6 +4903,7 @@ groktypename (struct c_type_name *type_name, tree *expr, DEPRECATED_NORMAL); /* Apply attributes. */ + attrs = c_warn_type_attributes (attrs); decl_attributes (&type, attrs, 0); return type; @@ -6295,10 +6316,13 @@ grokdeclarator (const struct c_declarator *declarator, if (cxx11_attribute_p (attrs) && inner_decl->kind == cdk_id) returned_attrs = chainon (returned_attrs, attrs); else - 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: @@ -11676,6 +11700,7 @@ finish_declspecs (struct c_declspecs *specs) } if (specs->type != NULL) { + specs->postfix_attrs = c_warn_type_attributes (specs->postfix_attrs); decl_attributes (&specs->type, specs->postfix_attrs, 0); specs->postfix_attrs = NULL_TREE; } diff --git a/gcc/c/c-tree.h b/gcc/c/c-tree.h index 67c8f45af450..dffefa3fd1a5 100644 --- a/gcc/c/c-tree.h +++ b/gcc/c/c-tree.h @@ -596,6 +596,7 @@ extern tree c_builtin_function (tree); extern tree c_builtin_function_ext_scope (tree); extern tree c_simulate_builtin_function_decl (tree); extern void c_warn_unused_attributes (tree); +extern tree c_warn_type_attributes (tree); extern void shadow_tag (const struct c_declspecs *); extern void shadow_tag_warned (const struct c_declspecs *, int); extern tree start_enum (location_t, struct c_enum_contents *, tree); diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 2cc8db571709..37fbda183dec 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,10 @@ +2019-11-20 Joseph Myers + + * gcc.dg/c2x-attr-deprecated-2.c, gcc.dg/c2x-attr-fallthrough-2.c, + gcc.dg/c2x-attr-maybe_unused-2.c: Expect errors for invalid uses + of standard attributes on types. Add more tests of invalid uses + on types. + 2019-11-19 Jakub Jelinek PR c++/92414 diff --git a/gcc/testsuite/gcc.dg/c2x-attr-deprecated-2.c b/gcc/testsuite/gcc.dg/c2x-attr-deprecated-2.c index 0f0b1749ae4f..44f2cc9bd138 100644 --- a/gcc/testsuite/gcc.dg/c2x-attr-deprecated-2.c +++ b/gcc/testsuite/gcc.dg/c2x-attr-deprecated-2.c @@ -7,14 +7,13 @@ [[deprecated]]; /* { dg-error "ignored" } */ -int [[deprecated]] var; /* { dg-warning "ignored" } */ -/* { dg-message "that appertains to a type-specifier" "appertains" { target *-*-* } .-1 } */ +int [[deprecated]] var; /* { dg-error "ignored" } */ -int array_with_dep_type[2] [[deprecated]]; /* { dg-warning "ignored" } */ -/* { dg-message "that appertains to a type-specifier" "appertains" { target *-*-* } .-1 } */ +int array_with_dep_type[2] [[deprecated]]; /* { dg-error "ignored" } */ -void fn_with_dep_type () [[deprecated]]; /* { dg-warning "ignored" } */ -/* { dg-message "that appertains to a type-specifier" "appertains" { target *-*-* } .-1 } */ +void fn_with_dep_type () [[deprecated]]; /* { dg-error "ignored" } */ + +int z = sizeof (int [[__deprecated__]]); /* { dg-error "ignored" } */ void f (void) diff --git a/gcc/testsuite/gcc.dg/c2x-attr-fallthrough-2.c b/gcc/testsuite/gcc.dg/c2x-attr-fallthrough-2.c index 33e3ec282279..0e36adccc9ee 100644 --- a/gcc/testsuite/gcc.dg/c2x-attr-fallthrough-2.c +++ b/gcc/testsuite/gcc.dg/c2x-attr-fallthrough-2.c @@ -4,11 +4,13 @@ [[fallthrough]]; /* { dg-error "'fallthrough' attribute at top level" } */ -int [[fallthrough]] x; /* { dg-warning "ignored" } */ -/* { dg-message "that appertains to a type-specifier" "appertains" { target *-*-* } .-1 } */ +int [[fallthrough]] x; /* { dg-error "ignored" } */ -int g () [[fallthrough]]; /* { dg-warning "ignored" } */ -/* { dg-message "that appertains to a type-specifier" "appertains" { target *-*-* } .-1 } */ +int g () [[fallthrough]]; /* { dg-error "ignored" } */ + +int array[2] [[fallthrough]]; /* { dg-error "ignored" } */ + +int z = sizeof (int [[fallthrough]]); /* { dg-error "ignored" } */ int f (int a) diff --git a/gcc/testsuite/gcc.dg/c2x-attr-maybe_unused-2.c b/gcc/testsuite/gcc.dg/c2x-attr-maybe_unused-2.c index 7f06581f949a..a749639192e8 100644 --- a/gcc/testsuite/gcc.dg/c2x-attr-maybe_unused-2.c +++ b/gcc/testsuite/gcc.dg/c2x-attr-maybe_unused-2.c @@ -7,14 +7,13 @@ [[maybe_unused]]; /* { dg-error "ignored" } */ -int [[maybe_unused]] var; /* { dg-warning "ignored" } */ -/* { dg-message "that appertains to a type-specifier" "appertains" { target *-*-* } .-1 } */ +int [[maybe_unused]] var; /* { dg-error "ignored" } */ -int array_with_dep_type[2] [[maybe_unused]]; /* { dg-warning "ignored" } */ -/* { dg-message "that appertains to a type-specifier" "appertains" { target *-*-* } .-1 } */ +int array_with_dep_type[2] [[maybe_unused]]; /* { dg-error "ignored" } */ -void fn_with_dep_type () [[maybe_unused]]; /* { dg-warning "ignored" } */ -/* { dg-message "that appertains to a type-specifier" "appertains" { target *-*-* } .-1 } */ +void fn_with_dep_type () [[maybe_unused]]; /* { dg-error "ignored" } */ + +int z = sizeof (int [[__maybe_unused__]]); /* { dg-error "ignored" } */ void f (void)