xref: /llvm-project/clang/test/Sema/generic-selection.c (revision 881125ad9178120acef186f579e36ced0888dfdb)
1 // RUN: %clang_cc1 -std=c11 -fsyntax-only -Wno-strict-prototypes -Wno-implicit-function-declaration -verify %s
2 // RUN: %clang_cc1 -std=c99 -pedantic -fsyntax-only -Wno-strict-prototypes -Wno-implicit-function-declaration -verify=expected,ext %s
3 
4 void g(void);
5 
foo(int n)6 void foo(int n) {
7   (void) _Generic(0, // ext-warning {{'_Generic' is a C11 extension}}
8       struct A: 0, // expected-error {{type 'struct A' in generic association incomplete}}
9       void(): 0,   // expected-error {{type 'void ()' in generic association not an object type}}
10       int[n]: 0);  // expected-error {{type 'int[n]' in generic association is a variably modified type}}
11 
12   (void) _Generic(0, // ext-warning {{'_Generic' is a C11 extension}}
13       void (*)():     0,  // expected-note {{compatible type 'void (*)()' specified here}}
14       void (*)(void): 0); // expected-error {{type 'void (*)(void)' in generic association compatible with previously specified type 'void (*)()'}}
15 
16   (void) _Generic((void (*)()) 0, // expected-error {{controlling expression type 'void (*)()' compatible with 2 generic association types}} \
17                                   // ext-warning {{'_Generic' is a C11 extension}}
18       void (*)(int):  0,  // expected-note {{compatible type 'void (*)(int)' specified here}}
19       void (*)(void): 0); // expected-note {{compatible type 'void (*)(void)' specified here}}
20 
21   (void) _Generic(0, // expected-error {{controlling expression type 'int' not compatible with any generic association type}} \
22                      // ext-warning {{'_Generic' is a C11 extension}}
23       char: 0, short: 0, long: 0);
24 
25   int a1[_Generic(0, int: 1, short: 2, float: 3, default: 4) == 1 ? 1 : -1]; // ext-warning {{'_Generic' is a C11 extension}}
26   int a2[_Generic(0, default: 1, short: 2, float: 3, int: 4) == 4 ? 1 : -1]; // ext-warning {{'_Generic' is a C11 extension}}
27   int a3[_Generic(0L, int: 1, short: 2, float: 3, default: 4) == 4 ? 1 : -1]; // ext-warning {{'_Generic' is a C11 extension}}
28   int a4[_Generic(0L, default: 1, short: 2, float: 3, int: 4) == 1 ? 1 : -1]; // ext-warning {{'_Generic' is a C11 extension}}
29   int a5[_Generic(0, int: 1, short: 2, float: 3) == 1 ? 1 : -1]; // ext-warning {{'_Generic' is a C11 extension}}
30   int a6[_Generic(0, short: 1, float: 2, int: 3) == 3 ? 1 : -1]; // ext-warning {{'_Generic' is a C11 extension}}
31 
32   int a7[_Generic("test", char *: 1, default: 2) == 1 ? 1 : -1]; // ext-warning {{'_Generic' is a C11 extension}}
33   int a8[_Generic(g, void (*)(void): 1, default: 2) == 1 ? 1 : -1]; // ext-warning {{'_Generic' is a C11 extension}}
34 
35   const int i = 12;
36   int a9[_Generic(i, int: 1, default: 2) == 1 ? 1 : -1]; // ext-warning {{'_Generic' is a C11 extension}}
37 
38   // This is expected to not trigger any diagnostics because the controlling
39   // expression is not evaluated.
40   (void)_Generic(*(int *)0, int: 1); // ext-warning {{'_Generic' is a C11 extension}}
41 }
42 
43 int __attribute__((overloadable)) test (int);
44 double __attribute__((overloadable)) test (double);
45 char testc(char);
46 
PR30201(void)47 void PR30201(void) {
48   _Generic(4, char:testc, default:test)(4); // ext-warning {{'_Generic' is a C11 extension}}
49 }
50 
GH50227(void)51 void GH50227(void) {
52   // Previously, the controlling expression for the outer _Generic makes it
53   // result dependent, and testing whether that controlling expression has side
54   // effects would cause a crash.
55   _Generic( // ext-warning {{'_Generic' is a C11 extension}}
56     n(
57       _Generic(n++, int : 0) // expected-error {{cannot increment value of type 'int ()'}} ext-warning {{'_Generic' is a C11 extension}}
58     ), int : 0);
59 }
60 
61 struct Test {
62   int i;
63 };
64 
unreachable_associations(const int i,const struct Test t)65 void unreachable_associations(const int i, const struct Test t) {
66   _Static_assert( // ext-warning {{'_Static_assert' is a C11 extension}}
67     _Generic(i, // ext-warning {{'_Generic' is a C11 extension}}
68       const int : 1,    // expected-warning {{due to lvalue conversion of the controlling expression, association of type 'const int' will never be selected because it is qualified}}
69       volatile int : 2, // expected-warning {{due to lvalue conversion of the controlling expression, association of type 'volatile int' will never be selected because it is qualified}}
70       int[12] : 3,      // expected-warning {{due to lvalue conversion of the controlling expression, association of type 'int[12]' will never be selected because it is of array type}}
71       int : 4,
72       default : 5
73     ) == 4, "we had better pick int!");
74   _Static_assert( // ext-warning {{'_Static_assert' is a C11 extension}}
75     _Generic(t, // ext-warning {{'_Generic' is a C11 extension}}
76       struct Test : 1,
77       const struct Test : 2, // expected-warning {{due to lvalue conversion of the controlling expression, association of type 'const struct Test' will never be selected because it is qualified}}
78       default : 3
79     ) == 1, "we had better pick struct Test, not const struct Test!"); // C-specific result
80 }
81 
GH55562(void)82 void GH55562(void) {
83   // Ensure that you can still define a type within a generic selection
84   // association (despite it not being particularly useful).
85   (void)_Generic(1, struct S { int a; } : 0, default : 0); // ext-warning {{'_Generic' is a C11 extension}}
86   struct S s = { 0 };
87   int i = s.a;
88 }
89