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