|
| 1 | +// RUN: %clang_cc1 -std=c2x -verify -pedantic -Wno-comments %s |
| 2 | + |
| 3 | +/* WG14 N3007: Yes |
| 4 | + * Type Inference for object definitions |
| 5 | + */ |
| 6 | +void test_qualifiers(int x, const int y, int * restrict z) { |
| 7 | + const auto a = x; |
| 8 | + auto b = y; |
| 9 | + static auto c = 1UL; |
| 10 | + int* pa = &a; // expected-warning {{initializing 'int *' with an expression of type 'const int *' discards qualifiers}} |
| 11 | + const int* pb = &b; |
| 12 | + int* pc = &c; // expected-warning {{incompatible pointer types initializing 'int *' with an expression of type 'unsigned long *'}} |
| 13 | + |
| 14 | + const int ci = 12; |
| 15 | + auto yup = ci; |
| 16 | + yup = 12; |
| 17 | + |
| 18 | + auto r_test = z; |
| 19 | + |
| 20 | + _Static_assert(_Generic(a, int : 1)); |
| 21 | + _Static_assert(_Generic(c, unsigned long : 1)); |
| 22 | + _Static_assert(_Generic(pa, int * : 1)); |
| 23 | + _Static_assert(_Generic(pb, const int * : 1)); |
| 24 | + _Static_assert(_Generic(r_test, int * : 1)); |
| 25 | +} |
| 26 | + |
| 27 | +void test_atomic(void) { |
| 28 | + _Atomic auto i = 12; // expected-error {{_Atomic cannot be applied to type 'auto' in C23}} |
| 29 | + _Atomic(auto) j = 12; // expected-error {{'auto' not allowed here}} \ |
| 30 | + expected-error {{a type specifier is required for all declarations}} |
| 31 | + |
| 32 | + _Atomic(int) foo(void); |
| 33 | + auto k = foo(); |
| 34 | + |
| 35 | + _Static_assert(_Generic(&i, _Atomic auto *: 1)); // expected-error {{_Atomic cannot be applied to type 'auto' in C23}} \ |
| 36 | + expected-error {{'auto' not allowed here}} |
| 37 | + _Static_assert(_Generic(k, int: 1)); |
| 38 | +} |
| 39 | + |
| 40 | +void test_double(void) { |
| 41 | + double A[3] = { 0 }; |
| 42 | + auto pA = A; |
| 43 | + auto qA = &A; |
| 44 | + auto pi = 3.14; |
| 45 | + |
| 46 | + _Static_assert(_Generic(A, double * : 1)); |
| 47 | + _Static_assert(_Generic(pA, double * : 1)); |
| 48 | + _Static_assert(_Generic(qA, double (*)[3] : 1)); |
| 49 | + _Static_assert(_Generic(pi, double : 1)); |
| 50 | +} |
| 51 | + |
| 52 | +int test_auto_param(auto a) { // expected-error {{'auto' not allowed in function prototype}} |
| 53 | + return (int)(a * 2); |
| 54 | +} |
| 55 | + |
| 56 | +auto test_auto_return(float a, int b) { // expected-error {{'auto' not allowed in function return type}} |
| 57 | + return ((a * b) * (a / b)); |
| 58 | +} |
| 59 | + |
| 60 | +[[clang::overloadable]] auto test(auto x) { // expected-error {{'auto' not allowed in function prototype}} \ |
| 61 | + expected-error {{'auto' not allowed in function return type}} |
| 62 | + return x; |
| 63 | +} |
| 64 | + |
| 65 | +void test_sizeof_alignas(void) { |
| 66 | + (void)sizeof(auto); // expected-error {{expected expression}} |
| 67 | + _Alignas(auto) int a[4]; // expected-error {{expected expression}} |
| 68 | +} |
| 69 | + |
| 70 | +void test_arrary(void) { |
| 71 | + auto a[4]; // expected-error {{'auto' not allowed in array declaration}} |
| 72 | + auto b[] = {1, 2}; // expected-error {{cannot use 'auto' with array in C}} |
| 73 | +} |
| 74 | + |
| 75 | +void test_initializer_list(void) { |
| 76 | + auto a = {}; // expected-error {{cannot use 'auto' with array in C}} |
| 77 | + auto b = { 0 }; // expected-error {{cannot use 'auto' with array in C}} |
| 78 | + auto c = { 1, }; // expected-error {{cannot use 'auto' with array in C}} |
| 79 | + auto d = { 1 , 2 }; // expected-error {{cannot use 'auto' with array in C}} |
| 80 | + auto e = (int [3]){ 1, 2, 3 }; |
| 81 | +} |
| 82 | + |
| 83 | +void test_structs(void) { |
| 84 | + // FIXME: Both of these should be diagnosed as invalid underspecified |
| 85 | + // declarations as described in N3006. |
| 86 | + auto p1 = (struct { int a; } *)0; |
| 87 | + struct s; |
| 88 | + auto p2 = (struct s { int a; } *)0; |
| 89 | + |
| 90 | + struct B { auto b; }; // expected-error {{'auto' not allowed in struct member}} |
| 91 | +} |
| 92 | + |
| 93 | +void test_typedefs(void) { |
| 94 | + typedef auto auto_type; // expected-error {{'auto' not allowed in typedef}} |
| 95 | + |
| 96 | + typedef auto (*fp)(void); // expected-error {{'auto' not allowed in typedef}} |
| 97 | + typedef void (*fp)(auto); // expected-error {{'auto' not allowed in function prototype}} |
| 98 | + |
| 99 | + _Generic(0, auto : 1); // expected-error {{'auto' not allowed here}} |
| 100 | +} |
| 101 | + |
| 102 | +void test_misc(void) { |
| 103 | + auto something; // expected-error {{declaration of variable 'something' with deduced type 'auto' requires an initializer}} |
| 104 | + auto test_char = 'A'; |
| 105 | + auto test_char_ptr = "test"; |
| 106 | + auto test_char_ptr2[] = "another test"; // expected-warning {{type inference of a declaration other than a plain identifier with optional trailing attributes is a Clang extension}} |
| 107 | + auto auto_size = sizeof(auto); // expected-error {{expected expression}} |
| 108 | + |
| 109 | + _Static_assert(_Generic(test_char, int : 1)); |
| 110 | + _Static_assert(_Generic(test_char_ptr, char * : 1)); |
| 111 | + _Static_assert(_Generic(test_char_ptr2, char * : 1)); |
| 112 | +} |
| 113 | + |
| 114 | +void test_no_integer_promotions(void) { |
| 115 | + short s; |
| 116 | + auto a = s; |
| 117 | + _Generic(a, int : 1); // expected-error {{controlling expression type 'short' not compatible with any generic association type}} |
| 118 | +} |
| 119 | + |
| 120 | +void test_compound_literals(void) { |
| 121 | + auto a = (int){}; |
| 122 | + auto b = (int){ 0 }; |
| 123 | + auto c = (int){ 0, }; |
| 124 | + auto d = (int){ 0, 1 }; // expected-warning {{excess elements in scalar initializer}} |
| 125 | + |
| 126 | + auto auto_cl = (auto){13}; // expected-error {{expected expression}} |
| 127 | + |
| 128 | + _Static_assert(_Generic(a, int : 1)); |
| 129 | + _Static_assert(_Generic(b, int : 1)); |
| 130 | + _Static_assert(_Generic(c, int : 1)); |
| 131 | +} |
| 132 | + |
| 133 | +void test_pointers(void) { |
| 134 | + int a; |
| 135 | + auto *ptr = &a; // expected-warning {{type inference of a declaration other than a plain identifier with optional trailing attributes is a Clang extension}} |
| 136 | + auto *ptr2 = a; // expected-error {{variable 'ptr2' with type 'auto *' has incompatible initializer of type 'int'}} \ |
| 137 | + expected-warning {{type inference of a declaration other than a plain identifier with optional trailing attributes is a Clang extension}} |
| 138 | + auto nptr = nullptr; |
| 139 | + |
| 140 | + _Static_assert(_Generic(ptr, int * : 1)); |
| 141 | + _Static_assert(_Generic(ptr2, int * : 1)); |
| 142 | +} |
| 143 | + |
| 144 | +void test_scopes(void) { |
| 145 | + double a = 7; |
| 146 | + double b = 9; |
| 147 | + { |
| 148 | + auto a = a * a; // expected-error {{variable 'a' declared with deduced type 'auto' cannot appear in its own initializer}} \ |
| 149 | + expected-error {{variable 'a' declared with deduced type 'auto' cannot appear in its own initializer}} |
| 150 | + } |
| 151 | + { |
| 152 | + auto b = a * a; |
| 153 | + auto a = b; |
| 154 | + |
| 155 | + _Static_assert(_Generic(a, double : 1)); |
| 156 | + _Static_assert(_Generic(b, double : 1)); |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +void test_loop(void) { |
| 161 | + auto j = 4; |
| 162 | + for (auto i = j; i < 2 * j; i++); |
| 163 | + |
| 164 | + _Static_assert(_Generic(j, int : 1)); |
| 165 | +} |
| 166 | + |
| 167 | +#define AUTO_MACRO(_NAME, ARG, ARG2, ARG3) \ |
| 168 | +auto _NAME = ARG + (ARG2 / ARG3); |
| 169 | + |
| 170 | +// This macro should only work with integers due to the usage of binary operators |
| 171 | +#define AUTO_INT_MACRO(_NAME, ARG, ARG2, ARG3) \ |
| 172 | +auto _NAME = (ARG ^ ARG2) & ARG3; |
| 173 | + |
| 174 | +void test_macros(int in_int) { |
| 175 | + auto a = in_int + 1; |
| 176 | + AUTO_MACRO(b, 1.3, 2.5f, 3); |
| 177 | + AUTO_INT_MACRO(c, 64, 23, 0xff); |
| 178 | + AUTO_INT_MACRO(not_valid, 51.5, 25, 0xff); // expected-error {{invalid operands to binary expression ('double' and 'int')}} |
| 179 | + |
| 180 | + auto result = (a + (int)b) - c; |
| 181 | + |
| 182 | + _Static_assert(_Generic(a, int : 1)); |
| 183 | + _Static_assert(_Generic(b, double : 1)); |
| 184 | + _Static_assert(_Generic(c, int : 1)); |
| 185 | + _Static_assert(_Generic(result, int : 1)); |
| 186 | +} |
0 commit comments