llvm-project/clang/test/Parser/attributes.c
Martin Boehme 8c7b64b5ae [clang] Reject non-declaration C++11 attributes on declarations
For backwards compatiblity, we emit only a warning instead of an error if the
attribute is one of the existing type attributes that we have historically
allowed to "slide" to the `DeclSpec` just as if it had been specified in GNU
syntax. (We will call these "legacy type attributes" below.)

The high-level changes that achieve this are:

- We introduce a new field `Declarator::DeclarationAttrs` (with appropriate
  accessors) to store C++11 attributes occurring in the attribute-specifier-seq
  at the beginning of a simple-declaration (and other similar declarations).
  Previously, these attributes were placed on the `DeclSpec`, which made it
  impossible to reconstruct later on whether the attributes had in fact been
  placed on the decl-specifier-seq or ahead of the declaration.

- In the parser, we propgate declaration attributes and decl-specifier-seq
  attributes separately until we can place them in
  `Declarator::DeclarationAttrs` or `DeclSpec::Attrs`, respectively.

- In `ProcessDeclAttributes()`, in addition to processing declarator attributes,
  we now also process the attributes from `Declarator::DeclarationAttrs` (except
  if they are legacy type attributes).

- In `ConvertDeclSpecToType()`, in addition to processing `DeclSpec` attributes,
  we also process any legacy type attributes that occur in
  `Declarator::DeclarationAttrs` (and emit a warning).

- We make `ProcessDeclAttribute` emit an error if it sees any non-declaration
  attributes in C++11 syntax, except in the following cases:
  - If it is being called for attributes on a `DeclSpec` or `DeclaratorChunk`
  - If the attribute is a legacy type attribute (in which case we only emit
    a warning)

The standard justifies treating attributes at the beginning of a
simple-declaration and attributes after a declarator-id the same. Here are some
relevant parts of the standard:

- The attribute-specifier-seq at the beginning of a simple-declaration
  "appertains to each of the entities declared by the declarators of the
  init-declarator-list" (https://eel.is/c++draft/dcl.dcl#dcl.pre-3)

- "In the declaration for an entity, attributes appertaining to that entity can
  appear at the start of the declaration and after the declarator-id for that
  declaration." (https://eel.is/c++draft/dcl.dcl#dcl.pre-note-2)

- "The optional attribute-specifier-seq following a declarator-id appertains to
  the entity that is declared."
  (https://eel.is/c++draft/dcl.dcl#dcl.meaning.general-1)

The standard contains similar wording to that for a simple-declaration in other
similar types of declarations, for example:

- "The optional attribute-specifier-seq in a parameter-declaration appertains to
  the parameter." (https://eel.is/c++draft/dcl.fct#3)

- "The optional attribute-specifier-seq in an exception-declaration appertains
  to the parameter of the catch clause" (https://eel.is/c++draft/except.pre#1)

The new behavior is tested both on the newly added type attribute
`annotate_type`, for which we emit errors, and for the legacy type attribute
`address_space` (chosen somewhat randomly from the various legacy type
attributes), for which we emit warnings.

Depends On D111548

Reviewed By: aaron.ballman, rsmith

Differential Revision: https://reviews.llvm.org/D126061
2022-06-15 11:58:26 +02:00

123 lines
4.3 KiB
C

// RUN: %clang_cc1 -fsyntax-only -verify %s -pedantic -std=c99 -Wno-strict-prototypes
int __attribute__(()) x;
__inline void __attribute__((__always_inline__, __nodebug__))
foo(void) {
}
__attribute__(()) y; // expected-error {{type specifier missing, defaults to 'int'}}
// PR2796
int (__attribute__(()) *z)(long y);
void f1(__attribute__(()) int x);
int f2(y, __attribute__(()) x); // expected-error {{expected identifier}}
// This is parsed as a normal argument list (with two args that are implicit
// int) because the __attribute__ is a declspec.
void f3(__attribute__(()) x, // expected-error {{type specifier missing, defaults to 'int'}}
y); // expected-error {{type specifier missing, defaults to 'int'}}
void f4(__attribute__(())); // expected-error {{expected parameter declarator}}
// This is ok, the __attribute__ applies to the pointer.
int baz(int (__attribute__(()) *x)(long y));
void g1(void (*f1)(__attribute__(()) int x));
void g2(int (*f2)(y, __attribute__(()) x)); // expected-error {{expected identifier}}
void g3(void (*f3)(__attribute__(()) x, int y)); // expected-error {{type specifier missing, defaults to 'int'}}
void g4(void (*f4)(__attribute__(()))); // expected-error {{expected parameter declarator}}
void (*h1)(void (*f1)(__attribute__(()) int x));
void (*h2)(int (*f2)(y, __attribute__(()) x)); // expected-error {{expected identifier}}
void (*h3)(void (*f3)(__attribute__(()) x)); // expected-error {{type specifier missing, defaults to 'int'}}
void (*h4)(void (*f4)(__attribute__(()))); // expected-error {{expected parameter declarator}}
// rdar://6131260
int foo42(void) {
int x, __attribute__((unused)) y, z;
return 0;
}
// rdar://6096491
void __attribute__((noreturn)) d0(void), __attribute__((noreturn)) d1(void);
void d2(void) __attribute__((noreturn)), d3(void) __attribute__((noreturn));
// PR6287
void __attribute__((returns_twice)) returns_twice_test(void);
int aligned(int);
int __attribute__((vec_type_hint(char, aligned(16) )) missing_rparen_1; // expected-error 2{{expected ')'}} expected-note {{to match}} expected-warning {{does not declare anything}}
int __attribute__((mode(x aligned(16) )) missing_rparen_2; // expected-error 2{{expected ')'}}
int __attribute__((format(printf, 0 aligned(16) )) missing_rparen_3; // expected-error 2{{expected ')'}}
int testFundef1(int *a) __attribute__((nonnull(1))) { // \
// expected-warning {{GCC does not allow 'nonnull' attribute in this position on a function definition}}
return *a;
}
// noreturn is lifted to type qualifier
void testFundef2(void) __attribute__((noreturn)) { // \
// expected-warning {{GCC does not allow 'noreturn' attribute in this position on a function definition}}
testFundef2();
}
int testFundef3(int *a) __attribute__((nonnull(1), // \
// expected-warning {{GCC does not allow 'nonnull' attribute in this position on a function definition}}
pure)) { // \
// expected-warning {{GCC does not allow 'pure' attribute in this position on a function definition}}
return *a;
}
int testFundef4(int *a) __attribute__((nonnull(1))) // \
// expected-warning {{GCC does not allow 'nonnull' attribute in this position on a function definition}}
__attribute((pure)) { // \
// expected-warning {{GCC does not allow 'pure' attribute in this position on a function definition}}
return *a;
}
// GCC allows these
void testFundef5(void) __attribute__(()) { }
__attribute__((pure)) int testFundef6(int a) { return a; }
void deprecatedTestFun(void) __attribute__((deprecated()));
struct s {
int a;
};
// This test ensure compatibility with parsing GNU-style attributes
// where the attribute is on a separate line from the elaborated type
// specifier.
struct s
__attribute__((used)) bar;
// Ensure that attributes must be separated by a comma (PR38352).
__attribute__((const const)) int PR38352(void); // expected-error {{expected ')'}}
// Also ensure that we accept spurious commas.
__attribute__((,,,const)) int PR38352_1(void);
__attribute__((const,,,)) int PR38352_2(void);
__attribute__((const,,,const)) int PR38352_3(void);
__attribute__((,,,const,,,const,,,)) int PR38352_4(void);
// Test that we allow attributes on free-standing decl-specifier-seqs.
// GCC appears to allow this.
__attribute__(()) struct t;
void f5() {
__attribute__(()) struct t;
}