| 1 |
// RUN: %clang_cc1 -std=c2x -verify -pedantic -Wno-comments %s |
| 2 |
// RUN: %clang_cc1 -std=c2x -verify -pedantic -Wno-comments %s -fexperimental-new-constant-interpreter |
| 3 |
|
| 4 |
void test_basic_types(void) { |
| 5 |
auto undefined; // expected-error {{declaration of variable 'undefined' with deduced type 'auto' requires an initializer}} |
| 6 |
auto auto_int = 4; |
| 7 |
auto auto_long = 4UL; |
| 8 |
auto int auto_int_ts = 12; |
| 9 |
signed auto a = 1L; // expected-error {{'auto' cannot be signed or unsigned}} |
| 10 |
|
| 11 |
_Static_assert(_Generic(auto_int, int : 1)); |
| 12 |
_Static_assert(_Generic(auto_long, unsigned long : 1)); |
| 13 |
} |
| 14 |
|
| 15 |
void test_complex_types(void) { |
| 16 |
_Complex auto i = 12.0; // expected-error {{'_Complex auto' is invalid}} |
| 17 |
} |
| 18 |
|
| 19 |
void test_gnu_extensions(void) { |
| 20 |
auto t = ({ // expected-warning {{use of GNU statement expression extension}} |
| 21 |
auto b = 12; |
| 22 |
b; |
| 23 |
}); |
| 24 |
_Static_assert(_Generic(t, int : 1)); |
| 25 |
} |
| 26 |
|
| 27 |
void test_sizeof_typeof(void) { |
| 28 |
auto auto_size = sizeof(auto); // expected-error {{expected expression}} |
| 29 |
typeof(auto) tpof = 4; // expected-error {{expected expression}} |
| 30 |
} |
| 31 |
|
| 32 |
void test_casts(void) { |
| 33 |
auto int_cast = (int)(4 + 3); |
| 34 |
auto double_cast = (double)(1 / 3); |
| 35 |
auto long_cast = (long)(4UL + 3UL); |
| 36 |
auto auto_cast = (auto)(4 + 3); // expected-error {{expected expression}} |
| 37 |
|
| 38 |
_Static_assert(_Generic(int_cast, int : 1)); |
| 39 |
_Static_assert(_Generic(double_cast, double : 1)); |
| 40 |
_Static_assert(_Generic(long_cast, long : 1)); |
| 41 |
} |
| 42 |
|
| 43 |
void test_compound_literral(void) { |
| 44 |
auto int_cl = (int){13}; |
| 45 |
auto double_cl = (double){2.5}; |
| 46 |
auto array[] = { 1, 2, 3 }; // expected-error {{cannot use 'auto' with array in C}} |
| 47 |
|
| 48 |
auto auto_cl = (auto){13}; // expected-error {{expected expression}} |
| 49 |
|
| 50 |
_Static_assert(_Generic(int_cl, int : 1)); |
| 51 |
_Static_assert(_Generic(double_cl, double : 1)); |
| 52 |
} |
| 53 |
|
| 54 |
void test_array_pointers(void) { |
| 55 |
double array[3] = { 0 }; |
| 56 |
auto a = array; |
| 57 |
auto b = &array; |
| 58 |
|
| 59 |
_Static_assert(_Generic(array, double * : 1)); |
| 60 |
_Static_assert(_Generic(a, double * : 1)); |
| 61 |
_Static_assert(_Generic(b, double (*)[3] : 1)); |
| 62 |
} |
| 63 |
|
| 64 |
void test_typeof() { |
| 65 |
int typeof_target(); |
| 66 |
auto result = (typeof(typeof_target())){12}; |
| 67 |
|
| 68 |
_Static_assert(_Generic(result, int : 1)); |
| 69 |
} |
| 70 |
|
| 71 |
void test_qualifiers(const int y) { |
| 72 |
const auto a = 12; |
| 73 |
auto b = y; |
| 74 |
static auto c = 1UL; |
| 75 |
int* pa = &a; // expected-warning {{initializing 'int *' with an expression of type 'const int *' discards qualifiers}} |
| 76 |
const int* pb = &b; |
| 77 |
int* pc = &c; // expected-error {{incompatible pointer types initializing 'int *' with an expression of type 'unsigned long *'}} |
| 78 |
|
| 79 |
_Static_assert(_Generic(a, int : 1)); |
| 80 |
_Static_assert(_Generic(b, int : 1)); |
| 81 |
_Static_assert(_Generic(c, unsigned long : 1)); |
| 82 |
_Static_assert(_Generic(pa, int * : 1)); |
| 83 |
_Static_assert(_Generic(pb, const int * : 1)); |
| 84 |
_Static_assert(_Generic(pc, int * : 1)); |
| 85 |
} |
| 86 |
|
| 87 |
void test_strings(void) { |
| 88 |
auto str = "this is a string"; |
| 89 |
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}} |
| 90 |
auto (str3) = "this is a string"; |
| 91 |
auto (((str4))) = "this is a string"; |
| 92 |
|
| 93 |
_Static_assert(_Generic(str, char * : 1)); |
| 94 |
_Static_assert(_Generic(str2, char * : 1)); |
| 95 |
_Static_assert(_Generic(str3, char * : 1)); |
| 96 |
_Static_assert(_Generic(str4, char * : 1)); |
| 97 |
} |
| 98 |
|
| 99 |
void test_pointers(void) { |
| 100 |
auto a = 12; |
| 101 |
auto *ptr = &a; // expected-warning {{type inference of a declaration other than a plain identifier with optional trailing attributes is a Clang extension}} |
| 102 |
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}} |
| 103 |
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}} |
| 104 |
auto *b = &a; // expected-warning {{type inference of a declaration other than a plain identifier with optional trailing attributes is a Clang extension}} |
| 105 |
*b = &a; // expected-error {{incompatible pointer to integer conversion assigning to 'int' from 'int *'; remove &}} |
| 106 |
auto nptr = nullptr; |
| 107 |
|
| 108 |
_Static_assert(_Generic(a, int : 1)); |
| 109 |
_Static_assert(_Generic(ptr, int * : 1)); |
| 110 |
_Static_assert(_Generic(str, char * : 1)); |
| 111 |
_Static_assert(_Generic(str2, const char * : 1)); |
| 112 |
_Static_assert(_Generic(b, int * : 1)); |
| 113 |
_Static_assert(_Generic(nptr, typeof(nullptr) : 1)); |
| 114 |
} |
| 115 |
|
| 116 |
void test_prototypes(void) { |
| 117 |
extern void foo(int a, int array[({ auto x = 12; x;})]); // expected-warning {{use of GNU statement expression extension}} |
| 118 |
} |
| 119 |
|
| 120 |
void test_scopes(void) { |
| 121 |
double a = 7; |
| 122 |
double b = 9; |
| 123 |
{ |
| 124 |
auto a = a * a; // expected-error {{variable 'a' declared with deduced type 'auto' cannot appear in its own initializer}} \ |
| 125 |
expected-error {{variable 'a' declared with deduced type 'auto' cannot appear in its own initializer}} |
| 126 |
} |
| 127 |
{ |
| 128 |
auto b = a * a; |
| 129 |
auto a = b; |
| 130 |
|
| 131 |
_Static_assert(_Generic(b, double : 1)); |
| 132 |
_Static_assert(_Generic(a, double : 1)); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
[[clang::overloadable]] auto test(auto x) { // expected-error {{'auto' not allowed in function prototype}} \ |
| 137 |
expected-error {{'auto' not allowed in function return type}} |
| 138 |
return x; |
| 139 |
} |