blob: 16d028eaf60e8d90b66ebccf4d44cce706b76d6c [file] [log] [blame]
Richard Smithcf74da72011-11-16 07:18:12 +00001// RUN: %clang_cc1 -fsyntax-only -verify -triple i686-linux %s
Eli Friedman9a156e52008-11-12 09:44:48 +00002
3#define EVAL_EXPR(testno, expr) int test##testno = sizeof(struct{char qq[expr];});
4int x;
5EVAL_EXPR(1, (_Bool)&x)
6EVAL_EXPR(2, (int)(1.0+(double)4))
7EVAL_EXPR(3, (int)(1.0+(float)4.0))
8EVAL_EXPR(4, (_Bool)(1 ? (void*)&x : 0))
9EVAL_EXPR(5, (_Bool)(int[]){0})
10struct y {int x,y;};
11EVAL_EXPR(6, (int)(1+(struct y*)0))
12EVAL_EXPR(7, (int)&((struct y*)0)->y)
13EVAL_EXPR(8, (_Bool)"asdf")
Eli Friedman5a332ea2008-11-13 06:09:17 +000014EVAL_EXPR(9, !!&x)
15EVAL_EXPR(10, ((void)1, 12))
16void g0(void);
Richard Smith42d3af92011-12-07 00:43:50 +000017EVAL_EXPR(11, (g0(), 12)) // expected-error {{must have a constant size}}
Eli Friedman5a332ea2008-11-13 06:09:17 +000018EVAL_EXPR(12, 1.0&&2.0)
Richard Smith42d3af92011-12-07 00:43:50 +000019EVAL_EXPR(13, x || 3.0) // expected-error {{must have a constant size}}
Anders Carlsson4c76e932008-11-24 04:21:33 +000020
21unsigned int l_19 = 1;
22EVAL_EXPR(14, (1 ^ l_19) && 1); // expected-error {{fields must have a constant size}}
Anders Carlssona42ee442008-11-24 04:41:22 +000023
24void f()
25{
26 int a;
Richard Smitha07ed4a2012-01-26 04:47:34 +000027 EVAL_EXPR(15, (_Bool)&a);
Anders Carlssona42ee442008-11-24 04:41:22 +000028}
Daniel Dunbar134a0252009-01-24 19:08:01 +000029
30// FIXME: Turn into EVAL_EXPR test once we have more folding.
31_Complex float g16 = (1.0f + 1.0fi);
Daniel Dunbar4750e632009-02-18 00:47:45 +000032
33// ?: in constant expressions.
34int g17[(3?:1) - 2];
Anders Carlssonef56fba2009-02-19 04:55:58 +000035
Anders Carlsson0bbcfa62009-02-19 06:19:15 +000036EVAL_EXPR(18, ((int)((void*)10 + 10)) == 20 ? 1 : -1);
Anders Carlsson0756c972009-02-19 06:30:50 +000037
38struct s {
Chris Lattnerf35de482011-06-14 06:38:10 +000039 int a[(int)-1.0f]; // expected-error {{'a' declared as an array with a negative size}}
Anders Carlsson0756c972009-02-19 06:30:50 +000040};
Eli Friedman0b8337c2009-02-20 01:57:15 +000041
42EVAL_EXPR(19, ((int)&*(char*)10 == 10 ? 1 : -1));
Daniel Dunbar79e042a2009-02-21 18:14:20 +000043
Chris Lattner17c0eac2010-10-12 17:47:42 +000044EVAL_EXPR(20, __builtin_constant_p(*((int*) 10)));
Eli Friedmana1c7b6c2009-02-28 03:59:05 +000045
46EVAL_EXPR(21, (__imag__ 2i) == 2 ? 1 : -1);
47
48EVAL_EXPR(22, (__real__ (2i+3)) == 3 ? 1 : -1);
49
Anders Carlsson6c885802009-02-28 21:56:50 +000050int g23[(int)(1.0 / 1.0)] = { 1 };
51int g24[(int)(1.0 / 1.0)] = { 1 , 2 }; // expected-warning {{excess elements in array initializer}}
52int g25[(int)(1.0 + 1.0)], g26 = sizeof(g25);
Eli Friedman64004332009-03-23 04:38:34 +000053
54EVAL_EXPR(26, (_Complex double)0 ? -1 : 1)
55EVAL_EXPR(27, (_Complex int)0 ? -1 : 1)
56EVAL_EXPR(28, (_Complex double)1 ? 1 : -1)
57EVAL_EXPR(29, (_Complex int)1 ? 1 : -1)
Chris Lattner1f02e052009-04-21 05:19:11 +000058
59
60// PR4027 + rdar://6808859
Carl Norum58d489f2011-03-07 22:57:45 +000061struct a { int x, y; };
Chris Lattner1f02e052009-04-21 05:19:11 +000062static struct a V2 = (struct a)(struct a){ 1, 2};
63static const struct a V1 = (struct a){ 1, 2};
Eli Friedmand3a5a9d2009-04-22 19:23:09 +000064
65EVAL_EXPR(30, (int)(_Complex float)((1<<30)-1) == (1<<30) ? 1 : -1)
Eli Friedmana38da572009-04-28 19:17:36 +000066EVAL_EXPR(31, (int*)0 == (int*)0 ? 1 : -1)
67EVAL_EXPR(32, (int*)0 != (int*)0 ? -1 : 1)
Eli Friedman4a4fefc2009-06-04 20:04:03 +000068EVAL_EXPR(33, (void*)0 - (void*)0 == 0 ? 1 : -1)
Eli Friedman334046a2009-06-14 02:17:33 +000069void foo(void) {}
70EVAL_EXPR(34, (foo == (void *)0) ? -1 : 1)
Sebastian Redl87869bc2009-11-05 21:10:57 +000071
72// No PR. Mismatched bitwidths lead to a crash on second evaluation.
73const _Bool constbool = 0;
74EVAL_EXPR(35, constbool)
75EVAL_EXPR(36, constbool)
Eli Friedman141fbf32009-11-16 04:25:37 +000076
Eli Friedman95719532010-08-14 20:52:13 +000077EVAL_EXPR(37, (1,2.0) == 2.0 ? 1 : -1)
78EVAL_EXPR(38, __builtin_expect(1,1) == 1 ? 1 : -1)
79
80// PR7884
81EVAL_EXPR(39, __real__(1.f) == 1 ? 1 : -1)
82EVAL_EXPR(40, __imag__(1.f) == 0 ? 1 : -1)
Argyrios Kyrtzidise84389b2011-01-18 18:49:33 +000083
Eli Friedman47133be2011-11-12 03:56:23 +000084// From gcc testsuite
85EVAL_EXPR(41, (int)(1+(_Complex unsigned)2))
86
Argyrios Kyrtzidise84389b2011-01-18 18:49:33 +000087// rdar://8875946
88void rdar8875946() {
89 double _Complex P;
90 float _Complex P2 = 3.3f + P;
91}
Richard Smith472d4952011-10-28 23:26:52 +000092
93double d = (d = 0.0); // expected-error {{not a compile-time constant}}
Richard Smith390cd492011-10-30 23:17:09 +000094double d2 = ++d; // expected-error {{not a compile-time constant}}
Richard Smith9c8d1c52011-10-29 22:55:55 +000095
96int n = 2;
97int intLvalue[*(int*)((long)&n ?: 1)] = { 1, 2 }; // expected-error {{variable length array}}
Richard Smithdd785442011-10-31 20:57:44 +000098
99union u { int a; char b[4]; };
100char c = ((union u)(123456)).b[0]; // expected-error {{not a compile-time constant}}
Richard Smithcecf1842011-11-01 21:06:14 +0000101
102extern const int weak_int __attribute__((weak));
103const int weak_int = 42;
104int weak_int_test = weak_int; // expected-error {{not a compile-time constant}}
Richard Smithe9e20dd32011-11-04 01:10:57 +0000105
106int literalVsNull1 = "foo" == 0;
107int literalVsNull2 = 0 == "foo";
Richard Smithcf74da72011-11-16 07:18:12 +0000108
109// PR11385.
110int castViaInt[*(int*)(unsigned long)"test"]; // expected-error {{variable length array}}
Richard Smith10f4d062011-11-16 17:22:48 +0000111
112// PR11391.
113struct PR11391 { _Complex float f; } pr11391;
114EVAL_EXPR(42, __builtin_constant_p(pr11391.f = 1))
Eli Friedmanebea9af2012-02-21 22:41:33 +0000115
116// PR12043
117float varfloat;
118const float constfloat = 0;
119EVAL_EXPR(43, varfloat && constfloat) // expected-error {{must have a constant size}}
Eli Friedmane9c64d12012-04-11 23:32:29 +0000120
121// <rdar://problem/11205586>
122// (Make sure we continue to reject this.)
123EVAL_EXPR(44, "x"[0]); // expected-error {{variable length array}}
Eli Friedman6c31cb42012-04-16 04:30:08 +0000124
125// <rdar://problem/10962435>
126EVAL_EXPR(45, ((char*)-1) + 1 == 0 ? 1 : -1)
127EVAL_EXPR(46, ((char*)-1) + 1 < (char*) -1 ? 1 : -1)
Eli Friedman2f5b7c52012-04-16 19:23:57 +0000128EVAL_EXPR(47, &x < &x + 1 ? 1 : -1)
129EVAL_EXPR(48, &x != &x - 1 ? 1 : -1)
130EVAL_EXPR(49, &x < &x - 100 ? 1 : -1) // expected-error {{must have a constant size}}
131
132extern struct Test50S Test50;
133EVAL_EXPR(50, &Test50 < (struct Test50S*)((unsigned)&Test50 + 10)) // expected-error {{must have a constant size}}
Eli Friedman4eafb6b2012-07-17 21:03:05 +0000134
135// <rdar://problem/11874571>
136EVAL_EXPR(51, 0 != (float)1e99)