[clang] Handle __declspec() attributes in using
[llvm-project.git] / clang / test / Sema / const-eval.c
blobbbffc79752fddb9ead0b992892be584a94ac63ec
1 // RUN: %clang_cc1 -fsyntax-only -verify -triple x86_64-linux %s -Wno-tautological-pointer-compare -Wno-pointer-to-int-cast
2 // RUN: %clang_cc1 -fsyntax-only -verify -triple powerpc64-ibm-aix-xcoff %s -Wno-tautological-pointer-compare -Wno-pointer-to-int-cast
4 #define EVAL_EXPR(testno, expr) enum { test##testno = (expr) }; struct check_positive##testno { int a[test##testno]; };
5 int x;
6 EVAL_EXPR(1, (_Bool)&x)
7 EVAL_EXPR(2, (int)(1.0+(double)4))
8 EVAL_EXPR(3, (int)(1.0+(float)4.0))
9 EVAL_EXPR(4, (_Bool)(1 ? (void*)&x : 0))
10 EVAL_EXPR(5, (_Bool)(int[]){0})
11 struct y {int x,y;};
12 EVAL_EXPR(6, (int)(1+(struct y*)0))
13 EVAL_EXPR(7, (int)&((struct y*)0)->y)
14 EVAL_EXPR(8, (_Bool)"asdf")
15 EVAL_EXPR(9, !!&x)
16 EVAL_EXPR(10, ((void)1, 12))
17 void g0(void);
18 EVAL_EXPR(11, (g0(), 12)) // expected-error {{not an integer constant expression}}
19 EVAL_EXPR(12, 1.0&&2.0)
20 EVAL_EXPR(13, x || 3.0) // expected-error {{not an integer constant expression}}
22 unsigned int l_19 = 1;
23 EVAL_EXPR(14, (1 ^ l_19) && 1); // expected-error {{not an integer constant expression}}
25 void f(void)
27 int a;
28 EVAL_EXPR(15, (_Bool)&a);
31 // FIXME: Turn into EVAL_EXPR test once we have more folding.
32 _Complex float g16 = (1.0f + 1.0fi);
34 // ?: in constant expressions.
35 int g17[(3?:1) - 2];
37 EVAL_EXPR(18, ((int)((void*)10 + 10)) == 20 ? 1 : -1);
39 struct s {
40 int a[(int)-1.0f]; // expected-error {{array size is negative}}
43 EVAL_EXPR(19, ((int)&*(char*)10 == 10 ? 1 : -1));
45 EVAL_EXPR(20, __builtin_constant_p(*((int*) 10)));
47 EVAL_EXPR(21, (__imag__ 2i) == 2 ? 1 : -1);
49 EVAL_EXPR(22, (__real__ (2i+3)) == 3 ? 1 : -1);
51 int g23[(int)(1.0 / 1.0)] = { 1 }; // expected-warning {{folded to constant array}}
52 int g24[(int)(1.0 / 1.0)] = { 1 , 2 }; // expected-warning {{folded to constant array}} expected-warning {{excess elements in array initializer}}
53 int g25[(int)(1.0 + 1.0)], g26 = sizeof(g25); // expected-warning {{folded to constant array}}
55 EVAL_EXPR(26, (_Complex double)0 ? -1 : 1)
56 EVAL_EXPR(27, (_Complex int)0 ? -1 : 1)
57 EVAL_EXPR(28, (_Complex double)1 ? 1 : -1)
58 EVAL_EXPR(29, (_Complex int)1 ? 1 : -1)
61 // PR4027 + rdar://6808859
62 struct a { int x, y; };
63 static struct a V2 = (struct a)(struct a){ 1, 2};
64 static const struct a V1 = (struct a){ 1, 2};
66 EVAL_EXPR(30, (int)(_Complex float)((1<<30)-1) == (1<<30) ? 1 : -1)
67 EVAL_EXPR(31, (int*)0 == (int*)0 ? 1 : -1)
68 EVAL_EXPR(32, (int*)0 != (int*)0 ? -1 : 1)
69 EVAL_EXPR(33, (void*)0 - (void*)0 == 0 ? 1 : -1)
70 void foo(void) {}
71 EVAL_EXPR(34, (foo == (void *)0) ? -1 : 1)
73 // No PR. Mismatched bitwidths lead to a crash on second evaluation.
74 const _Bool constbool = 0;
75 EVAL_EXPR(35, constbool)
76 EVAL_EXPR(36, constbool)
78 EVAL_EXPR(37, ((void)1,2.0) == 2.0 ? 1 : -1)
79 EVAL_EXPR(38, __builtin_expect(1,1) == 1 ? 1 : -1)
81 // PR7884
82 EVAL_EXPR(39, __real__(1.f) == 1 ? 1 : -1)
83 EVAL_EXPR(40, __imag__(1.f) == 0 ? 1 : -1)
85 // From gcc testsuite
86 EVAL_EXPR(41, (int)(1+(_Complex unsigned)2))
88 // rdar://8875946
89 void rdar8875946(void) {
90 double _Complex P;
91 float _Complex P2 = 3.3f + P;
94 double d = (d = 0.0); // expected-error {{not a compile-time constant}}
95 double d2 = ++d; // expected-error {{not a compile-time constant}}
97 int n = 2;
98 int intLvalue[*(int*)((long)&n ?: 1)] = { 1, 2 }; // expected-error {{variable length array}}
100 union u { int a; char b[4]; };
101 char c = ((union u)(123456)).b[0]; // expected-error {{not a compile-time constant}}
103 extern const int weak_int __attribute__((weak));
104 const int weak_int = 42;
105 int weak_int_test = weak_int; // expected-error {{not a compile-time constant}}
107 int literalVsNull1 = "foo" == 0;
108 int literalVsNull2 = 0 == "foo";
110 // PR11385.
111 int castViaInt[*(int*)(unsigned long)"test"]; // expected-error {{variable length array}}
113 // PR11391.
114 struct PR11391 { _Complex float f; } pr11391;
115 EVAL_EXPR(42, __builtin_constant_p(pr11391.f = 1))
117 // PR12043
118 float varfloat;
119 const float constfloat = 0;
120 EVAL_EXPR(43, varfloat && constfloat) // expected-error {{not an integer constant expression}}
122 // <rdar://problem/10962435>
123 EVAL_EXPR(45, ((char*)-1) + 1 == 0 ? 1 : -1)
124 EVAL_EXPR(46, ((char*)-1) + 1 < (char*) -1 ? 1 : -1)
125 EVAL_EXPR(47, &x < &x + 1 ? 1 : -1)
126 EVAL_EXPR(48, &x != &x - 1 ? 1 : -1)
127 EVAL_EXPR(49, &x < &x - 100 ? 1 : -1) // expected-error {{not an integer constant expression}}
129 extern struct Test50S Test50;
130 EVAL_EXPR(50, &Test50 < (struct Test50S*)((unsigned long)&Test50 + 10)) // expected-error {{not an integer constant expression}}
132 // <rdar://problem/11874571>
133 EVAL_EXPR(51, 0 != (float)1e99)
135 // PR21945
136 void PR21945(void) { int i = (({}), 0l); }
138 void PR24622(void);
139 struct PR24622 {} pr24622;
140 EVAL_EXPR(52, &pr24622 == (void *)&PR24622); // expected-error {{not an integer constant expression}}
141 // expected-note@-1 {{past the end}}
143 // We evaluate these by providing 2s' complement semantics in constant
144 // expressions, like we do for integers.
145 void *PR28739a = (__int128)(unsigned long)-1 + &PR28739a; // expected-warning {{the pointer incremented by 18446744073709551615 refers past the last possible element for an array in 64-bit address space containing 64-bit (8-byte) elements (max possible 2305843009213693952 elements)}}
146 void *PR28739b = &PR28739b + (__int128)(unsigned long)-1; // expected-warning {{refers past the last possible element}}
147 __int128 PR28739c = (&PR28739c + (__int128)(unsigned long)-1) - &PR28739c; // expected-warning {{refers past the last possible element}}
148 void *PR28739d = &(&PR28739d)[(__int128)(unsigned long)-1]; // expected-warning {{refers past the last possible element}}
150 struct PR35214_X {
151 int k;
152 int arr[];
154 int PR35214_x;
155 int PR35214_y = ((struct PR35214_X *)&PR35214_x)->arr[1]; // expected-error {{not a compile-time constant}}
156 int *PR35214_z = &((struct PR35214_X *)&PR35214_x)->arr[1]; // ok, &PR35214_x + 2