xref: /llvm-project/clang/test/Parser/attributes.c (revision 0f1c1be1968076d6f96f8a7bcc4a15cf195ecd97)
1 // RUN: %clang_cc1 -fsyntax-only -verify %s -pedantic -std=c99 -Wno-strict-prototypes
2 
3 int __attribute__(()) x;
4 
5 __inline void __attribute__((__always_inline__, __nodebug__))
foo(void)6 foo(void) {
7 }
8 
9 
10 __attribute__(()) y;   // expected-error {{type specifier missing, defaults to 'int'}}
11 
12 // PR2796
13 int (__attribute__(()) *z)(long y);
14 
15 
16 void f1(__attribute__(()) int x);
17 
18 int f2(y, __attribute__(()) x);     // expected-error {{expected identifier}}
19 
20 // This is parsed as a normal argument list (with two args that are implicit
21 // int) because the __attribute__ is a declspec.
22 void f3(__attribute__(()) x,  // expected-error {{type specifier missing, defaults to 'int'}}
23         y);               // expected-error {{type specifier missing, defaults to 'int'}}
24 
25 void f4(__attribute__(()));   // expected-error {{expected parameter declarator}}
26 
27 
28 // This is ok, the __attribute__ applies to the pointer.
29 int baz(int (__attribute__(()) *x)(long y));
30 
31 void g1(void (*f1)(__attribute__(()) int x));
32 void g2(int (*f2)(y, __attribute__(()) x));    // expected-error {{expected identifier}}
33 void g3(void (*f3)(__attribute__(()) x, int y));  // expected-error {{type specifier missing, defaults to 'int'}}
34 void g4(void (*f4)(__attribute__(())));  // expected-error {{expected parameter declarator}}
35 
36 
37 void (*h1)(void (*f1)(__attribute__(()) int x));
38 void (*h2)(int (*f2)(y, __attribute__(()) x));    // expected-error {{expected identifier}}
39 
40 void (*h3)(void (*f3)(__attribute__(()) x));   // expected-error {{type specifier missing, defaults to 'int'}}
41 void (*h4)(void (*f4)(__attribute__(())));  // expected-error {{expected parameter declarator}}
42 
foo42(void)43 int foo42(void) {
44   int x, __attribute__((unused)) y, z;
45   return 0;
46 }
47 
48 void __attribute__((noreturn)) d0(void), __attribute__((noreturn)) d1(void);
49 
50 void d2(void) __attribute__((noreturn)), d3(void) __attribute__((noreturn));
51 
52 
53 // PR6287
54 void __attribute__((returns_twice)) returns_twice_test(void);
55 
56 int aligned(int);
57 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}}
58 int __attribute__((mode(x aligned(16) )) missing_rparen_2; // expected-error 2{{expected ')'}}
59 int __attribute__((format(printf, 0 aligned(16) )) missing_rparen_3; // expected-error 2{{expected ')'}}
60 
61 
62 
63 int testFundef1(int *a) __attribute__((nonnull(1))) { // \
64     // expected-warning {{GCC does not allow 'nonnull' attribute in this position on a function definition}}
65   return *a;
66 }
67 
68 // noreturn is lifted to type qualifier
69 void testFundef2(void) __attribute__((noreturn)) { // \
70     // expected-warning {{GCC does not allow 'noreturn' attribute in this position on a function definition}}
71   testFundef2();
72 }
73 
74 int testFundef3(int *a) __attribute__((nonnull(1), // \
75     // expected-warning {{GCC does not allow 'nonnull' attribute in this position on a function definition}}
76                                      pure)) { // \
77     // expected-warning {{GCC does not allow 'pure' attribute in this position on a function definition}}
78   return *a;
79 }
80 
81 int testFundef4(int *a) __attribute__((nonnull(1))) // \
82     // expected-warning {{GCC does not allow 'nonnull' attribute in this position on a function definition}}
83                       __attribute((pure)) { // \
84     // expected-warning {{GCC does not allow 'pure' attribute in this position on a function definition}}
85   return *a;
86 }
87 
88 // GCC allows these
89 void testFundef5(void) __attribute__(()) { }
90 
91 __attribute__((pure)) int testFundef6(int a) { return a; }
92 
93 void deprecatedTestFun(void) __attribute__((deprecated()));
94 
95 struct s {
96   int a;
97 };
98 
99 // This test ensure compatibility with parsing GNU-style attributes
100 // where the attribute is on a separate line from the elaborated type
101 // specifier.
102 struct s
103 __attribute__((used)) bar;
104 
105 // Ensure that attributes must be separated by a comma (PR38352).
106 __attribute__((const const)) int PR38352(void); // expected-error {{expected ')'}}
107 // Also ensure that we accept spurious commas.
108 __attribute__((,,,const)) int PR38352_1(void);
109 __attribute__((const,,,)) int PR38352_2(void);
110 __attribute__((const,,,const)) int PR38352_3(void);
111 __attribute__((,,,const,,,const,,,)) int PR38352_4(void);
112 
113 // Test that we allow attributes on free-standing decl-specifier-seqs.
114 // GCC appears to allow this.
115 __attribute__(()) struct t;
116 void f5() {
117   __attribute__(()) struct t;
118 }
119