xref: /llvm-project/clang/test/C/C23/n2927.c (revision 50c81128de8616117118564eff22cf508cba7848)
1 // RUN: %clang_cc1 -verify -std=c2x %s
2 
3 /* WG14 N2927: yes
4  * Not-so-magic: typeof
5  */
6 
7 // These examples originated in WG14 N2927 but were modified to test particular
8 // compiler behaviors. Each of these examples come from C2x 6.7.2.5.
9 
10 // EXAMPLE 1
11 typeof(1 + 1) func();
12 int func();
13 
14 // EXAMPLE 2
15 const _Atomic int purr = 0;
16 const int meow = 1;
17 const char *const mew[] = {
18 	"aardvark",
19 	"bluejay",
20 	"catte",
21 };
22 
23 extern typeof_unqual(purr) plain_purr;
24 extern int plain_purr;
25 
26 extern typeof(_Atomic typeof(meow)) atomic_meow;
27 extern const _Atomic int atomic_meow;
28 
29 extern typeof(mew) mew_array;
30 extern const char *const mew_array[3];
31 
32 extern typeof_unqual(mew) mew2_array;
33 extern const char *mew2_array[3];
34 
35 // EXAMPLE 3
foo(int argc,char * argv[])36 void foo(int argc, char *argv[]) { // expected-note 2 {{declared here}}
37   _Static_assert(sizeof(typeof('p')) == sizeof(int));
38   _Static_assert(sizeof(typeof('p')) == sizeof('p'));
39   _Static_assert(sizeof(typeof((char)'p')) == sizeof(char));
40   _Static_assert(sizeof(typeof((char)'p')) == sizeof((char)'p'));
41   _Static_assert(sizeof(typeof("meow")) == sizeof(char[5]));
42   _Static_assert(sizeof(typeof("meow")) == sizeof("meow"));
43   _Static_assert(sizeof(typeof(argc)) == sizeof(int));
44   _Static_assert(sizeof(typeof(argc)) == sizeof(argc));
45   _Static_assert(sizeof(typeof(argv)) == sizeof(char**));
46   _Static_assert(sizeof(typeof(argv)) == sizeof(argv)); // expected-warning {{sizeof on array function parameter will return size of 'char **' instead of 'char *[]'}}
47 
48   _Static_assert(sizeof(typeof_unqual('p')) == sizeof(int));
49   _Static_assert(sizeof(typeof_unqual('p')) == sizeof('p'));
50   _Static_assert(sizeof(typeof_unqual((char)'p')) == sizeof(char));
51   _Static_assert(sizeof(typeof_unqual((char)'p')) == sizeof((char)'p'));
52   _Static_assert(sizeof(typeof_unqual("meow")) == sizeof(char[5]));
53   _Static_assert(sizeof(typeof_unqual("meow")) == sizeof("meow"));
54   _Static_assert(sizeof(typeof_unqual(argc)) == sizeof(int));
55   _Static_assert(sizeof(typeof_unqual(argc)) == sizeof(argc));
56   _Static_assert(sizeof(typeof_unqual(argv)) == sizeof(char**));
57   _Static_assert(sizeof(typeof_unqual(argv)) == sizeof(argv)); // expected-warning {{sizeof on array function parameter will return size of 'char **' instead of 'char *[]'}}
58 }
59 
60 // EXAMPLE 4
bar(int argc)61 void bar(int argc) {
62   extern int val;
63   extern typeof(typeof_unqual(typeof(argc)))val;
64 }
65 
66 // EXAMPLE 5 is tested by n2927_2.c because it is a codegen test.
67 
68 // EXAMPLE 6
69 extern const char *y[4];
70 extern typeof(typeof(const char*)[4]) y;
71 
72 // EXAMPLE 7
73 void f(int);
74 
75 void g(double);
76 typeof(f(5)) g(double x);          // g has type "void(double)"
77 
78 extern void (*h)(double);
79 extern typeof(g)* h;               // h has type "void(*)(double)"
80 extern typeof(true ? g : 0) h;  // h has type "void(*)(double)"
81 
82 void j(double *, double **);
83 void j(double A[5], typeof(A)* B); // j has type "void(double*, double**)"
84 
85 extern typeof(double[]) D;         // D has an incomplete type
86 
87 extern double C[2];
88 extern typeof(D) C;                // C has type "double[2]"
89 
90 typeof(D) D = { 5, 8.9, 0.1, 99 }; // D is now completed to "double[4]"
91 extern double E[4];
92 extern typeof(D) E;                // E has type "double[4]" from D�s completed type
93 
94 // GH64713 -- this used to trigger an infinite loop when creating the function
95 // declaration for F from the function designator specified by typeof.
96 typeof(int(int)) F;                // F has type "int(int)"
97