xref: /llvm-project/clang/test/Sema/auto-type.c (revision 2147a2a4f3ef344a561677b55444ce4d028ec59f)
1 // RUN: %clang_cc1 %s -fsyntax-only -Wno-strict-prototypes -verify -pedantic -std=c11
2 // RUN: %clang_cc1 %s -fsyntax-only -Wno-strict-prototypes -verify -pedantic -std=c11 -fexperimental-new-constant-interpreter
3 
4 __auto_type a = 5; // expected-warning {{'__auto_type' is a GNU extension}}
5 __extension__ __auto_type a1 = 5;
6 #pragma clang diagnostic ignored "-Wgnu-auto-type"
7 __auto_type b = 5.0;
8 __auto_type c = &b;
9 __auto_type d = (struct {int a;}) {5};
10 _Static_assert(__builtin_types_compatible_p(__typeof(a), int), "");
11 __auto_type e = e; // expected-error {{variable 'e' declared with deduced type '__auto_type' cannot appear in its own initializer}}
12 
13 struct s { __auto_type a; }; // expected-error {{'__auto_type' not allowed in struct member}}
14 
15 __auto_type f = 1, g = 1.0; // expected-error {{'__auto_type' deduced as 'int' in declaration of 'f' and deduced as 'double' in declaration of 'g'}}
16 
h()17 __auto_type h() {} // expected-error {{'__auto_type' not allowed in function return type}}
18 
i()19 int i() {
20   struct bitfield { int field:2; };
21   __auto_type j = (struct bitfield){1}.field; // expected-error {{cannot pass bit-field as __auto_type initializer in C}}
22 
23 }
24 
k(l)25 int k(l)
26 __auto_type l; // expected-error {{'__auto_type' not allowed in K&R-style function parameter}}
27 {}
28 
Issue53652(void)29 void Issue53652(void) {
30   // Ensure that qualifiers all work the same way as GCC.
31   const __auto_type cat = a;
32   const __auto_type pcat = &a;
33   volatile __auto_type vat = a;
34   volatile __auto_type pvat = &a;
35   restrict __auto_type rat = &a;
36   _Atomic __auto_type aat1 = a;
37   _Atomic __auto_type paat = &a;
38 
39   // GCC does not accept this either, for the same reason.
40   _Atomic(__auto_type) aat2 = a; // expected-error {{'__auto_type' not allowed here}} \
41                                  // expected-error {{type specifier missing, defaults to 'int'}}
42 
43   // Ensure the types are what we expect them to be, regardless of order we
44   // pass the types.
45   _Static_assert(__builtin_types_compatible_p(__typeof(cat), const int), "");
46   _Static_assert(__builtin_types_compatible_p(const int, __typeof(cat)), "");
47   _Static_assert(__builtin_types_compatible_p(__typeof(pcat), int *const), "");
48   _Static_assert(__builtin_types_compatible_p(int *const, __typeof(pcat)), "");
49   _Static_assert(__builtin_types_compatible_p(__typeof(vat), volatile int), "");
50   _Static_assert(__builtin_types_compatible_p(volatile int, __typeof(vat)), "");
51   _Static_assert(__builtin_types_compatible_p(__typeof(pvat), int *volatile), "");
52   _Static_assert(__builtin_types_compatible_p(int *volatile, __typeof(pvat)), "");
53   _Static_assert(__builtin_types_compatible_p(__typeof(rat), int *restrict), "");
54   _Static_assert(__builtin_types_compatible_p(int *restrict, __typeof(rat)), "");
55   _Static_assert(__builtin_types_compatible_p(__typeof(aat1), _Atomic int), "");
56   _Static_assert(__builtin_types_compatible_p(_Atomic int, __typeof(aat1)), "");
57   _Static_assert(__builtin_types_compatible_p(__typeof(paat), _Atomic(int *)), "");
58   _Static_assert(__builtin_types_compatible_p(_Atomic(int *), __typeof(paat)), "");
59 
60   // Ensure the types also work in generic selection expressions. Remember, the
61   // type of the expression argument to _Generic is treated as-if it undergoes
62   // lvalue to rvalue conversion, which drops qualifiers. We're making sure the
63   // use of __auto_type doesn't impact that.
64   (void)_Generic(cat, int : 0);
65   (void)_Generic(pcat, int * : 0);
66   (void)_Generic(vat, int : 0);
67   (void)_Generic(pvat, int * : 0);
68   (void)_Generic(rat, int * : 0);
69   (void)_Generic(aat1, int : 0);
70   (void)_Generic(paat, int * : 0);
71 
72   // Ensure that trying to merge two different __auto_type types does not
73   // decide that they are both the same type when they're actually different,
74   // and that we reject when the types are the same.
75   __auto_type i = 12;
76   __auto_type f = 1.2f;
77   (void)_Generic(a, __typeof__(i) : 0, __typeof__(f) : 1);
78   (void)_Generic(a,
79                  __typeof__(i) : 0,   // expected-note {{compatible type 'typeof (i)' (aka 'int') specified here}}
80                  __typeof__(a) : 1);  // expected-error {{type 'typeof (a)' (aka 'int') in generic association compatible with previously specified type 'typeof (i)' (aka 'int')}}
81 }
82 
Issue55702(void)83 void Issue55702(void) {
84   // A controlling expression which uses __auto_type should not be
85   // automatically compatible with every association; we should be using the
86   // canonical type for that comparison.
87   void *ptr = 0;
88   __auto_type v = ptr;
89   (void)_Generic(v, long double : 0, double : 0, default : 1); // OK
90   _Static_assert(_Generic(v, long double : 0, default : 1) == 1, "fail");
91 }
92