Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / Sema / c2x-auto.c
blob7cbd1db31315aef6b458c2f0168ef837ca732a0d
1 // RUN: %clang_cc1 -std=c2x -verify -pedantic -Wno-comments %s
3 void test_basic_types(void) {
4 auto undefined; // expected-error {{declaration of variable 'undefined' with deduced type 'auto' requires an initializer}}
5 auto auto_int = 4;
6 auto auto_long = 4UL;
7 auto int auto_int_ts = 12;
8 signed auto a = 1L; // expected-error {{'auto' cannot be signed or unsigned}}
10 _Static_assert(_Generic(auto_int, int : 1));
11 _Static_assert(_Generic(auto_long, unsigned long : 1));
14 void test_complex_types(void) {
15 _Complex auto i = 12.0; // expected-error {{'_Complex auto' is invalid}}
18 void test_gnu_extensions(void) {
19 auto t = ({ // expected-warning {{use of GNU statement expression extension}}
20 auto b = 12;
22 });
23 _Static_assert(_Generic(t, int : 1));
26 void test_sizeof_typeof(void) {
27 auto auto_size = sizeof(auto); // expected-error {{expected expression}}
28 typeof(auto) tpof = 4; // expected-error {{expected expression}}
31 void test_casts(void) {
32 auto int_cast = (int)(4 + 3);
33 auto double_cast = (double)(1 / 3);
34 auto long_cast = (long)(4UL + 3UL);
35 auto auto_cast = (auto)(4 + 3); // expected-error {{expected expression}}
37 _Static_assert(_Generic(int_cast, int : 1));
38 _Static_assert(_Generic(double_cast, double : 1));
39 _Static_assert(_Generic(long_cast, long : 1));
42 void test_compound_literral(void) {
43 auto int_cl = (int){13};
44 auto double_cl = (double){2.5};
45 auto array[] = { 1, 2, 3 }; // expected-error {{cannot use 'auto' with array in C}}
47 auto auto_cl = (auto){13}; // expected-error {{expected expression}}
49 _Static_assert(_Generic(int_cl, int : 1));
50 _Static_assert(_Generic(double_cl, double : 1));
53 void test_array_pointers(void) {
54 double array[3] = { 0 };
55 auto a = array;
56 auto b = &array;
58 _Static_assert(_Generic(array, double * : 1));
59 _Static_assert(_Generic(a, double * : 1));
60 _Static_assert(_Generic(b, double (*)[3] : 1));
63 void test_typeof() {
64 int typeof_target();
65 auto result = (typeof(typeof_target())){12};
67 _Static_assert(_Generic(result, int : 1));
70 void test_qualifiers(const int y) {
71 const auto a = 12;
72 auto b = y;
73 static auto c = 1UL;
74 int* pa = &a; // expected-warning {{initializing 'int *' with an expression of type 'const int *' discards qualifiers}}
75 const int* pb = &b;
76 int* pc = &c; // expected-warning {{incompatible pointer types initializing 'int *' with an expression of type 'unsigned long *'}}
78 _Static_assert(_Generic(a, int : 1));
79 _Static_assert(_Generic(b, int : 1));
80 _Static_assert(_Generic(c, unsigned long : 1));
81 _Static_assert(_Generic(pa, int * : 1));
82 _Static_assert(_Generic(pb, const int * : 1));
83 _Static_assert(_Generic(pc, int * : 1));
86 void test_strings(void) {
87 auto str = "this is a string";
88 auto str2[] = "this is a string"; // expected-warning {{type inference of a declaration other than a plain identifier with optional trailing attributes is a Clang extension}}
89 auto (str3) = "this is a string";
90 auto (((str4))) = "this is a string";
92 _Static_assert(_Generic(str, char * : 1));
93 _Static_assert(_Generic(str2, char * : 1));
94 _Static_assert(_Generic(str3, char * : 1));
95 _Static_assert(_Generic(str4, char * : 1));
98 void test_pointers(void) {
99 auto a = 12;
100 auto *ptr = &a; // expected-warning {{type inference of a declaration other than a plain identifier with optional trailing attributes is a Clang extension}}
101 auto *str = "this is a string"; // expected-warning {{type inference of a declaration other than a plain identifier with optional trailing attributes is a Clang extension}}
102 const auto *str2 = "this is a string"; // expected-warning {{type inference of a declaration other than a plain identifier with optional trailing attributes is a Clang extension}}
103 auto *b = &a; // expected-warning {{type inference of a declaration other than a plain identifier with optional trailing attributes is a Clang extension}}
104 *b = &a; // expected-error {{incompatible pointer to integer conversion assigning to 'int' from 'int *'; remove &}}
105 auto nptr = nullptr;
107 _Static_assert(_Generic(a, int : 1));
108 _Static_assert(_Generic(ptr, int * : 1));
109 _Static_assert(_Generic(str, char * : 1));
110 _Static_assert(_Generic(str2, const char * : 1));
111 _Static_assert(_Generic(b, int * : 1));
112 _Static_assert(_Generic(nptr, typeof(nullptr) : 1));
115 void test_prototypes(void) {
116 extern void foo(int a, int array[({ auto x = 12; x;})]); // expected-warning {{use of GNU statement expression extension}}
119 void test_scopes(void) {
120 double a = 7;
121 double b = 9;
123 auto a = a * a; // expected-error {{variable 'a' declared with deduced type 'auto' cannot appear in its own initializer}} \
124 expected-error {{variable 'a' declared with deduced type 'auto' cannot appear in its own initializer}}
127 auto b = a * a;
128 auto a = b;
130 _Static_assert(_Generic(b, double : 1));
131 _Static_assert(_Generic(a, double : 1));
135 [[clang::overloadable]] auto test(auto x) { // expected-error {{'auto' not allowed in function prototype}} \
136 expected-error {{'auto' not allowed in function return type}}
137 return x;