xref: /llvm-project/clang/test/Parser/stmt-attributes.c (revision 06eee734c1ea9de754c1e7e8c79c0897b9e01350)
1 // RUN: %clang_cc1 -fsyntax-only -verify %s
2 
3 #if !__has_extension(statement_attributes_with_gnu_syntax)
4 #error "We should have statement attributes with GNU syntax support"
5 #endif
6 
foo(int i)7 void foo(int i) {
8 
9   __attribute__((unknown_attribute)); // expected-warning {{unknown attribute 'unknown_attribute' ignored}}
10   __attribute__(()) {}
11   __attribute__(()) if (0) {}
12   __attribute__(()) for (;;);
13   __attribute__(()) do {
14     __attribute__(()) continue;
15   }
16   while (0)
17     ;
18   __attribute__(()) while (0);
19 
20   __attribute__(()) switch (i) {
21     __attribute__(()) case 0 :
22     __attribute__(()) default :
23     __attribute__(()) break;
24   }
25 
26   __attribute__(()) goto here;
27   __attribute__(()) here :
28 
29   __attribute__(()) return;
30 
31   __attribute__((noreturn)) {}             // expected-error {{'noreturn' attribute cannot be applied to a statement}}
32   __attribute__((noreturn)) if (0) {}      // expected-error {{'noreturn' attribute cannot be applied to a statement}}
33   __attribute__((noreturn)) for (;;);      // expected-error {{'noreturn' attribute cannot be applied to a statement}}
34   __attribute__((noreturn)) do {           // expected-error {{'noreturn' attribute cannot be applied to a statement}}
35     __attribute__((unavailable)) continue; // expected-error {{'unavailable' attribute cannot be applied to a statement}}
36   }
37   while (0)
38     ;
39   __attribute__((unknown_attribute)) while (0); // expected-warning {{unknown attribute 'unknown_attribute' ignored}}
40 
41   __attribute__((unused)) switch (i) {         // expected-error {{'unused' attribute cannot be applied to a statement}}
42   __attribute__((uuid)) case 0:                // expected-warning {{unknown attribute 'uuid' ignored}}
43   __attribute__((visibility(""))) default:         // expected-error {{'visibility' attribute cannot be applied to a statement}}
44     __attribute__((carries_dependency)) break; // expected-error {{'carries_dependency' attribute cannot be applied to a statement}}
45   }
46 
47   __attribute__((fastcall)) goto there; // expected-error {{'fastcall' attribute cannot be applied to a statement}}
48   __attribute__((noinline)) there :     // expected-warning {{'noinline' attribute only applies to functions and statements}}
49 
50                                     __attribute__((weakref)) return; // expected-error {{'weakref' attribute only applies to variables and functions}}
51 
52   __attribute__((carries_dependency));            // expected-error {{'carries_dependency' attribute cannot be applied to a statement}}
53   __attribute__((carries_dependency)) {}          // expected-error {{'carries_dependency' attribute cannot be applied to a statement}}
54   __attribute__((carries_dependency)) if (0) {}   // expected-error {{'carries_dependency' attribute cannot be applied to a statement}}
55   __attribute__((carries_dependency)) for (;;);   // expected-error {{'carries_dependency' attribute cannot be applied to a statement}}
56   __attribute__((carries_dependency)) do {        // expected-error {{'carries_dependency' attribute cannot be applied to a statement}}
57     __attribute__((carries_dependency)) continue; // expected-error {{'carries_dependency' attribute cannot be applied to a statement}} ignored}}
58   }
59   while (0)
60     ;
61   __attribute__((carries_dependency)) while (0); // expected-error {{'carries_dependency' attribute cannot be applied to a statement}}
62 
63   __attribute__((carries_dependency)) switch (i) { // expected-error {{'carries_dependency' attribute cannot be applied to a statement}} ignored}}
64   __attribute__((carries_dependency)) case 0:      // expected-error {{'carries_dependency' attribute cannot be applied to a statement}}
65   __attribute__((carries_dependency)) default:     // expected-error {{'carries_dependency' attribute cannot be applied to a statement}}
66     __attribute__((carries_dependency)) break;     // expected-error {{'carries_dependency' attribute cannot be applied to a statement}}
67   }
68 
69   __attribute__((carries_dependency)) goto here; // expected-error {{'carries_dependency' attribute cannot be applied to a statement}}
70 
71   __attribute__((carries_dependency)) return; // expected-error {{'carries_dependency' attribute cannot be applied to a statement}}
72 }
73 
74 void bar(void);
75 
foobar(void)76 void foobar(void) {
77   __attribute__((nomerge)) bar();
78   __attribute__(()) bar();                // expected-error {{expected identifier or '('}}
79   __attribute__((unused, nomerge)) bar(); // expected-error {{expected identifier or '('}}
80   __attribute__((nomerge, unused)) bar(); // expected-error {{expected identifier or '('}}
81   __attribute__((nomerge(1, 2))) bar();   // expected-error {{'nomerge' attribute takes no arguments}}
82   int x;
83   __attribute__((nomerge)) x = 10; // expected-warning {{'nomerge' attribute is ignored because there exists no call expression inside the statement}}
84 
85   __attribute__((nomerge)) label : bar(); // expected-error {{'nomerge' attribute only applies to functions, statements and variables}}
86 }
87 
88 int f(void);
89 
90 __attribute__((nomerge)) static int (*fptr)(void);
91 __attribute__((nomerge)) static int i; // expected-warning {{'nomerge' attribute is ignored because 'i' is not a function pointer}}
92 struct buz {} __attribute__((nomerge)); // expected-error {{'nomerge' attribute only applies to functions, statements and variables}}
93