Richard Smith | b755a9d | 2011-11-16 07:18:12 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -verify -triple i686-linux %s |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2 | |
| 3 | #define EVAL_EXPR(testno, expr) int test##testno = sizeof(struct{char qq[expr];}); |
| 4 | int x; |
| 5 | EVAL_EXPR(1, (_Bool)&x) |
| 6 | EVAL_EXPR(2, (int)(1.0+(double)4)) |
| 7 | EVAL_EXPR(3, (int)(1.0+(float)4.0)) |
| 8 | EVAL_EXPR(4, (_Bool)(1 ? (void*)&x : 0)) |
| 9 | EVAL_EXPR(5, (_Bool)(int[]){0}) |
| 10 | struct y {int x,y;}; |
| 11 | EVAL_EXPR(6, (int)(1+(struct y*)0)) |
| 12 | EVAL_EXPR(7, (int)&((struct y*)0)->y) |
| 13 | EVAL_EXPR(8, (_Bool)"asdf") |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 14 | EVAL_EXPR(9, !!&x) |
| 15 | EVAL_EXPR(10, ((void)1, 12)) |
| 16 | void g0(void); |
Richard Smith | aa9c350 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 17 | EVAL_EXPR(11, (g0(), 12)) // expected-error {{must have a constant size}} |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 18 | EVAL_EXPR(12, 1.0&&2.0) |
Richard Smith | aa9c350 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 19 | EVAL_EXPR(13, x || 3.0) // expected-error {{must have a constant size}} |
Anders Carlsson | 4bbc0e0 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 20 | |
| 21 | unsigned int l_19 = 1; |
| 22 | EVAL_EXPR(14, (1 ^ l_19) && 1); // expected-error {{fields must have a constant size}} |
Anders Carlsson | 35873c4 | 2008-11-24 04:41:22 +0000 | [diff] [blame] | 23 | |
| 24 | void f() |
| 25 | { |
| 26 | int a; |
Richard Smith | f2e4cd7 | 2012-01-26 04:47:34 +0000 | [diff] [blame] | 27 | EVAL_EXPR(15, (_Bool)&a); |
Anders Carlsson | 35873c4 | 2008-11-24 04:41:22 +0000 | [diff] [blame] | 28 | } |
Daniel Dunbar | 8f826f0 | 2009-01-24 19:08:01 +0000 | [diff] [blame] | 29 | |
| 30 | // FIXME: Turn into EVAL_EXPR test once we have more folding. |
| 31 | _Complex float g16 = (1.0f + 1.0fi); |
Daniel Dunbar | 2d6744f | 2009-02-18 00:47:45 +0000 | [diff] [blame] | 32 | |
| 33 | // ?: in constant expressions. |
| 34 | int g17[(3?:1) - 2]; |
Anders Carlsson | 4d4c50d | 2009-02-19 04:55:58 +0000 | [diff] [blame] | 35 | |
Anders Carlsson | e2f0e96 | 2009-02-19 06:19:15 +0000 | [diff] [blame] | 36 | EVAL_EXPR(18, ((int)((void*)10 + 10)) == 20 ? 1 : -1); |
Anders Carlsson | 1c17689 | 2009-02-19 06:30:50 +0000 | [diff] [blame] | 37 | |
| 38 | struct s { |
Chris Lattner | e1eed38 | 2011-06-14 06:38:10 +0000 | [diff] [blame] | 39 | int a[(int)-1.0f]; // expected-error {{'a' declared as an array with a negative size}} |
Anders Carlsson | 1c17689 | 2009-02-19 06:30:50 +0000 | [diff] [blame] | 40 | }; |
Eli Friedman | e8761c8 | 2009-02-20 01:57:15 +0000 | [diff] [blame] | 41 | |
| 42 | EVAL_EXPR(19, ((int)&*(char*)10 == 10 ? 1 : -1)); |
Daniel Dunbar | 4fff481 | 2009-02-21 18:14:20 +0000 | [diff] [blame] | 43 | |
Chris Lattner | 75c29a0 | 2010-10-12 17:47:42 +0000 | [diff] [blame] | 44 | EVAL_EXPR(20, __builtin_constant_p(*((int*) 10))); |
Eli Friedman | 722c717 | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 45 | |
| 46 | EVAL_EXPR(21, (__imag__ 2i) == 2 ? 1 : -1); |
| 47 | |
| 48 | EVAL_EXPR(22, (__real__ (2i+3)) == 3 ? 1 : -1); |
| 49 | |
Anders Carlsson | 1a7acfa | 2009-02-28 21:56:50 +0000 | [diff] [blame] | 50 | int g23[(int)(1.0 / 1.0)] = { 1 }; |
| 51 | int g24[(int)(1.0 / 1.0)] = { 1 , 2 }; // expected-warning {{excess elements in array initializer}} |
| 52 | int g25[(int)(1.0 + 1.0)], g26 = sizeof(g25); |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 53 | |
| 54 | EVAL_EXPR(26, (_Complex double)0 ? -1 : 1) |
| 55 | EVAL_EXPR(27, (_Complex int)0 ? -1 : 1) |
| 56 | EVAL_EXPR(28, (_Complex double)1 ? 1 : -1) |
| 57 | EVAL_EXPR(29, (_Complex int)1 ? 1 : -1) |
Chris Lattner | 81045d8 | 2009-04-21 05:19:11 +0000 | [diff] [blame] | 58 | |
| 59 | |
| 60 | // PR4027 + rdar://6808859 |
Carl Norum | e224ba7 | 2011-03-07 22:57:45 +0000 | [diff] [blame] | 61 | struct a { int x, y; }; |
Chris Lattner | 81045d8 | 2009-04-21 05:19:11 +0000 | [diff] [blame] | 62 | static struct a V2 = (struct a)(struct a){ 1, 2}; |
| 63 | static const struct a V1 = (struct a){ 1, 2}; |
Eli Friedman | 1725f68 | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 64 | |
| 65 | EVAL_EXPR(30, (int)(_Complex float)((1<<30)-1) == (1<<30) ? 1 : -1) |
Eli Friedman | ad02d7d | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 66 | EVAL_EXPR(31, (int*)0 == (int*)0 ? 1 : -1) |
| 67 | EVAL_EXPR(32, (int*)0 != (int*)0 ? -1 : 1) |
Eli Friedman | 147bd64 | 2009-06-04 20:04:03 +0000 | [diff] [blame] | 68 | EVAL_EXPR(33, (void*)0 - (void*)0 == 0 ? 1 : -1) |
Eli Friedman | 5bc8610 | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 69 | void foo(void) {} |
| 70 | EVAL_EXPR(34, (foo == (void *)0) ? -1 : 1) |
Sebastian Redl | 632d772 | 2009-11-05 21:10:57 +0000 | [diff] [blame] | 71 | |
| 72 | // No PR. Mismatched bitwidths lead to a crash on second evaluation. |
| 73 | const _Bool constbool = 0; |
| 74 | EVAL_EXPR(35, constbool) |
| 75 | EVAL_EXPR(36, constbool) |
Eli Friedman | 7f92f03 | 2009-11-16 04:25:37 +0000 | [diff] [blame] | 76 | |
Eli Friedman | 43efa31 | 2010-08-14 20:52:13 +0000 | [diff] [blame] | 77 | EVAL_EXPR(37, (1,2.0) == 2.0 ? 1 : -1) |
| 78 | EVAL_EXPR(38, __builtin_expect(1,1) == 1 ? 1 : -1) |
| 79 | |
| 80 | // PR7884 |
| 81 | EVAL_EXPR(39, __real__(1.f) == 1 ? 1 : -1) |
| 82 | EVAL_EXPR(40, __imag__(1.f) == 0 ? 1 : -1) |
Argyrios Kyrtzidis | e188933 | 2011-01-18 18:49:33 +0000 | [diff] [blame] | 83 | |
Eli Friedman | ddadaa4 | 2011-11-12 03:56:23 +0000 | [diff] [blame] | 84 | // From gcc testsuite |
| 85 | EVAL_EXPR(41, (int)(1+(_Complex unsigned)2)) |
| 86 | |
Argyrios Kyrtzidis | e188933 | 2011-01-18 18:49:33 +0000 | [diff] [blame] | 87 | // rdar://8875946 |
| 88 | void rdar8875946() { |
| 89 | double _Complex P; |
| 90 | float _Complex P2 = 3.3f + P; |
| 91 | } |
Richard Smith | ee591a9 | 2011-10-28 23:26:52 +0000 | [diff] [blame] | 92 | |
| 93 | double d = (d = 0.0); // expected-error {{not a compile-time constant}} |
Richard Smith | 7993e8a | 2011-10-30 23:17:09 +0000 | [diff] [blame] | 94 | double d2 = ++d; // expected-error {{not a compile-time constant}} |
Richard Smith | 342f1f8 | 2011-10-29 22:55:55 +0000 | [diff] [blame] | 95 | |
| 96 | int n = 2; |
| 97 | int intLvalue[*(int*)((long)&n ?: 1)] = { 1, 2 }; // expected-error {{variable length array}} |
Richard Smith | 6a7c94a | 2011-10-31 20:57:44 +0000 | [diff] [blame] | 98 | |
| 99 | union u { int a; char b[4]; }; |
| 100 | char c = ((union u)(123456)).b[0]; // expected-error {{not a compile-time constant}} |
Richard Smith | 65ac598 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 101 | |
| 102 | extern const int weak_int __attribute__((weak)); |
| 103 | const int weak_int = 42; |
| 104 | int weak_int_test = weak_int; // expected-error {{not a compile-time constant}} |
Richard Smith | 74f4634 | 2011-11-04 01:10:57 +0000 | [diff] [blame] | 105 | |
| 106 | int literalVsNull1 = "foo" == 0; |
| 107 | int literalVsNull2 = 0 == "foo"; |
Richard Smith | b755a9d | 2011-11-16 07:18:12 +0000 | [diff] [blame] | 108 | |
| 109 | // PR11385. |
| 110 | int castViaInt[*(int*)(unsigned long)"test"]; // expected-error {{variable length array}} |
Richard Smith | 2ad226b | 2011-11-16 17:22:48 +0000 | [diff] [blame] | 111 | |
| 112 | // PR11391. |
| 113 | struct PR11391 { _Complex float f; } pr11391; |
| 114 | EVAL_EXPR(42, __builtin_constant_p(pr11391.f = 1)) |
Eli Friedman | 51e47df | 2012-02-21 22:41:33 +0000 | [diff] [blame] | 115 | |
| 116 | // PR12043 |
| 117 | float varfloat; |
| 118 | const float constfloat = 0; |
| 119 | EVAL_EXPR(43, varfloat && constfloat) // expected-error {{must have a constant size}} |
Eli Friedman | 16aac6f | 2012-04-11 23:32:29 +0000 | [diff] [blame] | 120 | |
| 121 | // <rdar://problem/11205586> |
| 122 | // (Make sure we continue to reject this.) |
| 123 | EVAL_EXPR(44, "x"[0]); // expected-error {{variable length array}} |
Eli Friedman | a316988 | 2012-04-16 04:30:08 +0000 | [diff] [blame] | 124 | |
| 125 | // <rdar://problem/10962435> |
| 126 | EVAL_EXPR(45, ((char*)-1) + 1 == 0 ? 1 : -1) |
| 127 | EVAL_EXPR(46, ((char*)-1) + 1 < (char*) -1 ? 1 : -1) |
Eli Friedman | 2850376 | 2012-04-16 19:23:57 +0000 | [diff] [blame] | 128 | EVAL_EXPR(47, &x < &x + 1 ? 1 : -1) |
| 129 | EVAL_EXPR(48, &x != &x - 1 ? 1 : -1) |
| 130 | EVAL_EXPR(49, &x < &x - 100 ? 1 : -1) // expected-error {{must have a constant size}} |
| 131 | |
| 132 | extern struct Test50S Test50; |
| 133 | EVAL_EXPR(50, &Test50 < (struct Test50S*)((unsigned)&Test50 + 10)) // expected-error {{must have a constant size}} |