tcc -MD: drop system includes and duplicates
[tinycc.git] / tests / tests2 / 94_generic.c
blob6e85c0253e91b1b1041aa935cd7ab43c1e8080e8
1 #include <stdio.h>
3 const int a = 0;
5 struct a {
6 int a;
7 };
9 struct b {
10 int a;
13 int a_f()
15 return 20;
18 int b_f()
20 return 10;
23 typedef int (*fptr)(int);
24 int foo(int i)
26 return i;
29 typedef int int_type1;
31 #define gen_sw(a) _Generic(a, const char *: 1, default: 8, int: 123);
33 int main()
35 int i = 0;
36 signed long int l = 2;
37 struct b titi;
38 const int * const ptr;
39 const char *ti;
40 int_type1 i2;
42 i = _Generic(a, int: a_f, const int: b_f)();
43 printf("%d\n", i);
44 i = _Generic(a, int: a_f() / 2, const int: b_f() / 2);
45 printf("%d\n", i);
46 i = _Generic(ptr, int *:1, int * const:2, default:20);
47 printf("%d\n", i);
48 i = gen_sw(a);
49 printf("%d\n", i);
50 i = _Generic(titi, struct a:1, struct b:2, default:20);
51 printf("%d\n", i);
52 i = _Generic(i2, char: 1, int : 0);
53 printf("%d\n", i);
54 i = _Generic(a, char:1, int[4]:2, default:5);
55 printf("%d\n", i);
56 i = _Generic(17, int :1, int **:2);
57 printf("%d\n", i);
58 i = _Generic(17L, int :1, long :2, long long : 3);
59 printf("%d\n", i);
60 i = _Generic("17, io", char *: 3, const char *: 1);
61 printf("%d\n", i);
62 i = _Generic(ti, const unsigned char *:1, const char *:4, char *:3,
63 const signed char *:2);
64 printf("%d\n", i);
65 printf("%s\n", _Generic(i + 2L, long: "long", int: "int",
66 long long: "long long"));
67 i = _Generic(l, long: 1, int: 2);
68 printf("%d\n", i);
69 i = _Generic(foo, fptr: 3, int: 4);
70 printf("%d\n", i);
72 (void)_Generic((int(*)[2]){0}, int(*)[2]:0, int(*)[4]:0); //shouldn't match twice
74 //should accept ({ }) in the controlling expr of _Generic even in const_wanted contexts
75 struct { _Bool x_0: _Generic(({0;}),default:1); } my_x;
77 _Generic((__typeof((float const)((float const){42}))*){0}, float*: 0); //casts lose top-level qualifiers
78 int const x = 42; __typeof((__typeof(x))x) *xp = 0; (void)_Generic(xp, int*: 0); //casts lose top-level qualifiers
80 //TEST TERNARY:
81 //Same type
82 _Generic( 0?(long*)0:(long*)0, long*: (void)0);
83 //combining of qualifiers
84 _Generic( 0?(long volatile*)0:(long const*)0, long const volatile*: (void)0);
85 //nul-ptr constant selects other type
86 _Generic( 0?(long*)0:0, long*: (void)0);
87 _Generic( 0?(long*)0:(void*)0, long*: (void)0);
89 //void ptrs get chosen preferentially; qualifs still combine
90 _Generic( 0?(int volatile*)0: (void const*)1, void volatile const*: (void)0);
91 //like gcc but not clang, don't treat (void* const as the null-ptr constant)
92 _Generic( 0?(int volatile*)0: (void const*)0, void volatile const*: (void)0);
94 //ptrs to incomplete types get completed
95 (void)(sizeof(struct { int x:_Generic( 0?(int (*)[4])0 : (int (*)[])0, int (*)[4]:+1, int (*)[5]:(void)0); }));
96 (void)(sizeof(struct { int x:_Generic( 0?(int (*)[])0 : (int (*)[4])0, int (*)[4]:+1, int (*)[5]:(void)0); }));
99 /* completion shouldn't affect the type of decl */
100 char **argv;
101 _Generic(argv, char**: (void)0);
102 _Generic(0?(char const*)0:argv[0], char const*: (void)0);
103 _Generic(argv, char**: (void)0);
106 extern int (*ar)[];
107 (void)(sizeof(struct { int x:_Generic( 0?(int (*)[4])0 : (int (*)[])0, int (*)[4]:+1, int (*)[5]:(void)0); }));
108 (void)(sizeof(struct { int x:_Generic( 0?(int (*)[])0 : (int (*)[4])0, int (*)[4]:+1, int (*)[5]:(void)0); }));
109 (void)(sizeof(struct { int x:_Generic( 0?ar : (int (*)[4])0, int (*)[4]:+1, int (*)[5]:(void)0); }));
110 (void)(sizeof(struct { int x:_Generic( 0?(int (*)[4])0 : ar, int (*)[4]:+1, int (*)[5]:(void)0); }));
111 (void)(sizeof(struct { int x:_Generic( 0?(int (*)[5])0 : ar, int (*)[5]:+1, int (*)[4]:(void)0); }));
114 return 0;