diff --git a/clang/lib/Parse/ParseStmt.cpp b/clang/lib/Parse/ParseStmt.cpp --- a/clang/lib/Parse/ParseStmt.cpp +++ b/clang/lib/Parse/ParseStmt.cpp @@ -210,7 +210,8 @@ if ((getLangOpts().CPlusPlus || getLangOpts().MicrosoftExt || (StmtCtx & ParsedStmtContext::AllowDeclarationsInC) != ParsedStmtContext()) && - (GNUAttributeLoc.isValid() || isDeclarationStatement())) { + ((GNUAttributeLoc.isValid() && !Attrs.back().isStmtAttr()) || + isDeclarationStatement())) { SourceLocation DeclStart = Tok.getLocation(), DeclEnd; DeclGroupPtrTy Decl; if (GNUAttributeLoc.isValid()) { diff --git a/clang/test/Parser/stmt-attributes.c b/clang/test/Parser/stmt-attributes.c new file mode 100644 --- /dev/null +++ b/clang/test/Parser/stmt-attributes.c @@ -0,0 +1,86 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s + +void foo(int i) { + + __attribute__((unknown_attribute)); // expected-warning {{unknown attribute 'unknown_attribute' ignored}} + __attribute__((unknown_attribute)) {} // expected-warning {{unknown attribute 'unknown_attribute' ignored}} + __attribute__((unknown_attribute)) if (0) {} // expected-warning {{unknown attribute 'unknown_attribute' ignored}} + __attribute__((unknown_attribute)) for (;;); // expected-warning {{unknown attribute 'unknown_attribute' ignored}} + __attribute__((unknown_attribute)) do { // expected-warning {{unknown attribute 'unknown_attribute' ignored}} + __attribute__((unknown_attribute)) continue; // expected-warning {{unknown attribute 'unknown_attribute' ignored}} + } + while (0) + ; + __attribute__((unknown_attribute)) while (0); // expected-warning {{unknown attribute 'unknown_attribute' ignored}} + + __attribute__((unknown_attribute)) switch (i) { // expected-warning {{unknown attribute 'unknown_attribute' ignored}} + __attribute__((unknown_attribute)) case 0 : // expected-warning {{unknown attribute 'unknown_attribute' ignored}} + __attribute__((unknown_attribute)) default : // expected-warning {{unknown attribute 'unknown_attribute' ignored}} + __attribute__((unknown_attribute)) break; // expected-warning {{unknown attribute 'unknown_attribute' ignored}} + } + + __attribute__((unknown_attribute)) goto here; // expected-warning {{unknown attribute 'unknown_attribute' ignored}} + __attribute__((unknown_attribute)) here : // expected-warning {{unknown attribute 'unknown_attribute' ignored}} + + __attribute__((unknown_attribute)) return; // expected-warning {{unknown attribute 'unknown_attribute' ignored}} + + __attribute__((noreturn)) {} // expected-error {{'noreturn' attribute cannot be applied to a statement}} + __attribute__((noreturn)) if (0) {} // expected-error {{'noreturn' attribute cannot be applied to a statement}} + __attribute__((noreturn)) for (;;); // expected-error {{'noreturn' attribute cannot be applied to a statement}} + __attribute__((noreturn)) do { // expected-error {{'noreturn' attribute cannot be applied to a statement}} + __attribute__((unavailable)) continue; // expected-error {{'unavailable' attribute cannot be applied to a statement}} + } + while (0) + ; + __attribute__((unknown_attributqqq)) while (0); // expected-warning {{unknown attribute 'unknown_attributqqq' ignored}} + // TODO: remove 'qqq' part and enjoy 'empty loop body' warning here (DiagnoseEmptyLoopBody) + + __attribute__((unknown_attribute)) while (0); // expected-warning {{unknown attribute 'unknown_attribute' ignored}} + + __attribute__((unused)) switch (i) { // expected-error {{'unused' attribute cannot be applied to a statement}} + __attribute__((uuid)) case 0: // expected-warning {{unknown attribute 'uuid' ignored}} + __attribute__((visibility)) default: // expected-error {{'visibility' attribute cannot be applied to a statement}} + __attribute__((carries_dependency)) break; // expected-error {{'carries_dependency' attribute cannot be applied to a statement}} + } + + __attribute__((fastcall)) goto there; // expected-error {{'fastcall' attribute cannot be applied to a statement}} + __attribute__((noinline)) there : // expected-warning {{'noinline' attribute only applies to functions}} + + __attribute__((weakref)) return; // expected-error {{'weakref' attribute only applies to variables and functions}} + + __attribute__((carries_dependency)); // expected-error {{'carries_dependency' attribute cannot be applied to a statement}} + __attribute__((carries_dependency)) {} // expected-error {{'carries_dependency' attribute cannot be applied to a statement}} + __attribute__((carries_dependency)) if (0) {} // expected-error {{'carries_dependency' attribute cannot be applied to a statement}} + __attribute__((carries_dependency)) for (;;); // expected-error {{'carries_dependency' attribute cannot be applied to a statement}} + __attribute__((carries_dependency)) do { // expected-error {{'carries_dependency' attribute cannot be applied to a statement}} + __attribute__((carries_dependency)) continue; // expected-error {{'carries_dependency' attribute cannot be applied to a statement}} ignored}} + } + while (0) + ; + __attribute__((carries_dependency)) while (0); // expected-error {{'carries_dependency' attribute cannot be applied to a statement}} + + __attribute__((carries_dependency)) switch (i) { // expected-error {{'carries_dependency' attribute cannot be applied to a statement}} ignored}} + __attribute__((carries_dependency)) case 0: // expected-error {{'carries_dependency' attribute cannot be applied to a statement}} + __attribute__((carries_dependency)) default: // expected-error {{'carries_dependency' attribute cannot be applied to a statement}} + __attribute__((carries_dependency)) break; // expected-error {{'carries_dependency' attribute cannot be applied to a statement}} + } + + __attribute__((carries_dependency)) goto here; // expected-error {{'carries_dependency' attribute cannot be applied to a statement}} + + __attribute__((carries_dependency)) return; // expected-error {{'carries_dependency' attribute cannot be applied to a statement}} +} + +void bar(); + +void foobar() { + __attribute__((nomerge)) bar(); + __attribute__((nomerge(1, 2))) bar(); // expected-error {{'nomerge' attribute takes no arguments}} + int x; + __attribute__((nomerge)) x = 10; // expected-warning {{nomerge attribute is ignored because there exists no call expression inside the statement}} + + __attribute__((nomerge)) label : bar(); // expected-error {{'nomerge' attribute cannot be applied to a declaration}} +} + +int f(); + +__attribute__((nomerge)) static int i; // expected-error {{'nomerge' attribute cannot be applied to a declaration}}