xref: /llvm-project/clang/test/Parser/c2x-auto.c (revision 13ffc61a4224012ab1abb4e806d03fc149aec91f)
1 // RUN: %clang_cc1 -fsyntax-only -verify=expected,c23 -std=c23 %s
2 // RUN: %clang_cc1 -fsyntax-only -verify=expected,c17 -std=c17 %s
3 
4 #define AUTO_MACRO(_NAME, ARG, ARG2, ARG3) \
5 auto _NAME = ARG + (ARG2 / ARG3);
6 
7 struct S {
8   int a;
9   auto b;       // c23-error {{'auto' not allowed in struct member}} \
10                    c17-error {{type name does not allow storage class to be specified}} \
11                    c17-error {{type specifier missing, defaults to 'int'; ISO C99 and later do not support implicit int}}
12   union {
13     char c;
14     auto smth;  // c23-error {{'auto' not allowed in union member}} \
15                    c17-error {{type name does not allow storage class to be specified}} \
16                    c17-error {{type specifier missing, defaults to 'int'; ISO C99 and later do not support implicit int}}
17   } u;
18 };
19 
20 enum E : auto { // c23-error {{'auto' not allowed here}} \
21                    c17-error {{expected a type}} \
22                    c17-error {{type name does not allow storage class to be specified}}
23   One,
24   Two,
25   Tree,
26 };
27 
basic_usage(auto auto)28 auto basic_usage(auto auto) {   // c23-error {{'auto' not allowed in function prototype}} \
29                                    c23-error {{'auto' not allowed in function return type}} \
30                                    c23-error {{cannot combine with previous 'auto' declaration specifier}} \
31                                    c17-error {{invalid storage class specifier in function declarator}} \
32                                    c17-error {{illegal storage class on function}} \
33                                    c17-warning {{duplicate 'auto' declaration specifier}} \
34                                    c17-warning {{omitting the parameter name in a function definition is a C23 extension}} \
35                                    c17-error {{type specifier missing, defaults to 'int'; ISO C99 and later do not support implicit int}} \
36                                    c17-error {{type specifier missing, defaults to 'int'; ISO C99 and later do not support implicit int}}
37 
38   auto = 4;                     // expected-error {{expected identifier or '('}}
39 
40   auto a = 4;                   // c17-error {{type specifier missing, defaults to 'int'; ISO C99 and later do not support implicit int}}
41 
42   auto auto aa = 12;            // c23-error {{cannot combine with previous 'auto' declaration specifier}} \
43                                    c17-warning {{duplicate 'auto' declaration specifier}} \
44                                    c17-error {{type specifier missing, defaults to 'int'; ISO C99 and later do not support implicit int}}
45 
46   auto b[4];                    // c23-error {{'auto' not allowed in array declaration}} \
47                                    c17-error {{type specifier missing, defaults to 'int'; ISO C99 and later do not support implicit int}}
48 
49   auto array[auto];             // expected-error {{expected expression}} \
50                                    c23-error {{declaration of variable 'array' with deduced type 'auto' requires an initializer}} \
51                                    c17-error {{type specifier missing, defaults to 'int'; ISO C99 and later do not support implicit int}}
52 
53   AUTO_MACRO(auto, 1, 2, 3);    // c23-error {{cannot combine with previous 'auto' declaration specifier}} \
54                                    expected-error {{expected identifier or '('}} \
55                                    c17-warning {{duplicate 'auto' declaration specifier}}
56 
57   auto c = (auto)a;             // expected-error {{expected expression}} \
58                                    c17-error {{type specifier missing, defaults to 'int'; ISO C99 and later do not support implicit int}}
59 
60   auto ci = (auto){12};         // expected-error {{expected expression}} \
61                                    c17-error {{type specifier missing, defaults to 'int'; ISO C99 and later do not support implicit int}}
62 
63   int auto_cxx_decl = auto(0);  // expected-error {{expected expression}}
64 
65   return c;
66 }
67 
structs(void)68 void structs(void) {
69   struct s_auto { auto a; };            // c23-error {{'auto' not allowed in struct member}} \
70                                            c17-error {{type name does not allow storage class to be specified}} \
71                                            c17-error {{type specifier missing, defaults to 'int'; ISO C99 and later do not support implicit int}}
72 
73   // FIXME: this should end up being rejected when we implement underspecified
74   // declarations in N3006.
75   auto s_int = (struct { int a; } *)0;  // c17-error {{incompatible pointer to integer conversion initializing 'int' with an expression of type}} \
76                                            c17-error {{type specifier missing, defaults to 'int'; ISO C99 and later do not support implicit int}}
77 
78   typedef auto auto_type;               // c23-error {{'auto' not allowed in typedef}} \
79                                            c17-error {{cannot combine with previous 'typedef' declaration specifier}} \
80                                            c17-error {{type specifier missing, defaults to 'int'; ISO C99 and later do not support implicit int}}
81 }
82 
sizeof_alignas(void)83 void sizeof_alignas(void) {
84   auto auto_size = sizeof(auto);  // expected-error {{expected expression}} \
85                                      c17-error {{type specifier missing, defaults to 'int'; ISO C99 and later do not support implicit int}}
86 }
87 
generic_alignof_alignas(void)88 void generic_alignof_alignas(void) {
89   int g;
90   _Generic(g, auto : 0);  // c23-error {{'auto' not allowed here}} \
91                              c17-error {{expected a type}} \
92                              c17-error {{type name does not allow storage class to be specified}}
93 
94   _Alignof(auto);         // expected-error {{expected expression}} \
95                              expected-warning {{'_Alignof' applied to an expression is a GNU extension}}
96 
97   _Alignas(auto);         // expected-error {{expected expression}} \
98                              expected-warning {{declaration does not declare anything}}
99 }
100 
function_designators(void)101 void function_designators(void) {
102   extern auto auto_ret_func(void);    // c23-error {{'auto' not allowed in function return type}} \
103                                          c17-error {{cannot combine with previous 'extern' declaration specifier}} \
104                                          c17-error {{type specifier missing, defaults to 'int'; ISO C99 and later do not support implicit int}}
105 
106   extern void auto_param_func(auto);  // c23-error {{'auto' not allowed in function prototype}} \
107                                          c17-error {{invalid storage class specifier in function declarator}} \
108                                          c17-error {{type specifier missing, defaults to 'int'; ISO C99 and later do not support implicit int}}
109 
110   auto (auto_ret_func)(void);         // c23-error {{'auto' not allowed in function return type}} \
111                                          c17-error {{illegal storage class on function}} \
112                                          c17-error {{type specifier missing, defaults to 'int'; ISO C99 and later do not support implicit int}}
113 
114   void (auto_param_func)(auto);       // c23-error {{'auto' not allowed in function prototype}} \
115                                          c17-error {{invalid storage class specifier in function declarator}} \
116                                          c17-error {{type specifier missing, defaults to 'int'; ISO C99 and later do not support implicit int}}
117 }
118 
atomic(void)119 void atomic(void) {
120   _Atomic(auto) atom1 = 12; // c23-error {{'auto' not allowed here}} \
121                                c23-error {{a type specifier is required for all declarations}} \
122                                c17-error {{expected a type}} \
123                                c17-error {{type name does not allow storage class to be specified}} \
124                                c17-error {{type specifier missing, defaults to 'int'; ISO C99 and later do not support implicit int}}
125 
126   _Atomic auto atom2 = 12;  // c23-error {{_Atomic cannot be applied to type 'auto' in C23}} \
127                                c17-error {{type specifier missing, defaults to 'int'; ISO C99 and later do not support implicit int}}
128 }
129 
attributes(void)130 void attributes(void) {
131   auto ident [[clang::annotate("this works")]] = 12;  // c17-error {{type specifier missing, defaults to 'int'; ISO C99 and later do not support implicit int}}
132 }
133