Add more pedwarns for [[]] C attributes on types

Programming / Compilers / GCC - jsm28 [138bc75d-0d04-0410-961f-82ee72b054a4] - 20 November 2019 00:13 UTC

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.

- 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.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.

ed7dd26d6f9 Add more pedwarns for [[]] C attributes on types.
gcc/c/ChangeLog | 7 ++++++
gcc/c/c-decl.c | 33 ++++++++++++++++++++++----
gcc/c/c-tree.h | 1 +
gcc/testsuite/ChangeLog | 7 ++++++
gcc/testsuite/gcc.dg/c2x-attr-deprecated-2.c | 11 ++++-----
gcc/testsuite/gcc.dg/c2x-attr-fallthrough-2.c | 10 ++++----
gcc/testsuite/gcc.dg/c2x-attr-maybe_unused-2.c | 11 ++++-----
7 files changed, 60 insertions(+), 20 deletions(-)


  • Share