Chris Lattner | e1eed38 | 2011-06-14 06:38:10 +0000 | [diff] [blame] | 1 | // RUN: %clang %s -ffreestanding -fsyntax-only -Xclang -verify -pedantic -fpascal-strings -std=c99 |
Chris Lattner | 04de993 | 2008-12-12 07:01:24 +0000 | [diff] [blame] | 2 | |
Eli Friedman | f04ec67 | 2009-02-27 04:46:32 +0000 | [diff] [blame] | 3 | #include <stdint.h> |
| 4 | #include <limits.h> |
| 5 | |
Eli Friedman | 09de176 | 2009-04-25 22:37:12 +0000 | [diff] [blame] | 6 | int a() {int p; *(1 ? &p : (void*)(0 && (a(),1))) = 10;} // expected-error {{incomplete type 'void' is not assignable}} |
Chris Lattner | 04de993 | 2008-12-12 07:01:24 +0000 | [diff] [blame] | 7 | |
| 8 | // rdar://6091492 - ?: with __builtin_constant_p as the operand is an i-c-e. |
| 9 | int expr; |
| 10 | char w[__builtin_constant_p(expr) ? expr : 1]; |
| 11 | |
Abramo Bagnara | 83a1c5c | 2010-10-09 04:51:06 +0000 | [diff] [blame] | 12 | char v[sizeof(__builtin_constant_p(0)) == sizeof(int) ? 1 : -1]; |
Chris Lattner | 04de993 | 2008-12-12 07:01:24 +0000 | [diff] [blame] | 13 | |
Richard Smith | 32cb471 | 2011-10-24 18:26:35 +0000 | [diff] [blame] | 14 | int implicitConversion = 1.0; |
| 15 | char floatArith[(int)(1.0+2.0)]; // expected-warning {{must be an integer constant expression}} |
| 16 | |
Chris Lattner | 42b83dd | 2008-12-12 18:00:51 +0000 | [diff] [blame] | 17 | // __builtin_constant_p as the condition of ?: allows arbitrary foldable |
| 18 | // constants to be transmogrified into i-c-e's. |
| 19 | char b[__builtin_constant_p((int)(1.0+2.0)) ? (int)(1.0+2.0) : -1]; |
| 20 | |
| 21 | struct c { |
| 22 | int a : ( // expected-error {{expression is not an integer constant expression}} |
| 23 | __builtin_constant_p((int)(1.0+2.0)) ? (int)(1.0+ |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 24 | expr // expected-note {{subexpression not valid in a constant expression}} |
Chris Lattner | 42b83dd | 2008-12-12 18:00:51 +0000 | [diff] [blame] | 25 | ) : -1); |
| 26 | }; |
| 27 | |
| 28 | |
| 29 | |
Chris Lattner | 04de993 | 2008-12-12 07:01:24 +0000 | [diff] [blame] | 30 | |
| 31 | void test1(int n, int* p) { *(n ? p : (void *)(7-7)) = 1; } |
| 32 | void test2(int n, int* p) { *(n ? p : (void *)0) = 1; } |
| 33 | |
| 34 | |
| 35 | |
| 36 | char array[1024/(sizeof (long))]; |
| 37 | |
| 38 | int x['\xBb' == (char) 187 ? 1: -1]; |
| 39 | |
| 40 | // PR1992 |
| 41 | void func(int x) |
| 42 | { |
| 43 | switch (x) { |
| 44 | case sizeof("abc"): break; |
| 45 | case sizeof("loooong"): func(4); |
| 46 | case sizeof("\ploooong"): func(4); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | |
| 51 | // rdar://4213768 |
| 52 | int expr; |
| 53 | char y[__builtin_constant_p(expr) ? -1 : 1]; |
| 54 | char z[__builtin_constant_p(4) ? 1 : -1]; |
| 55 | |
Eli Friedman | f04ec67 | 2009-02-27 04:46:32 +0000 | [diff] [blame] | 56 | // Comma tests |
Argyrios Kyrtzidis | 1b2ad2f | 2010-09-19 23:03:35 +0000 | [diff] [blame] | 57 | int comma1[0?1,2:3]; // expected-warning {{expression result unused}} |
| 58 | int comma2[1||(1,2)]; // expected-warning {{expression result unused}} \ |
Matt Beaumont-Gay | 9b127f3 | 2011-08-15 17:50:06 +0000 | [diff] [blame] | 59 | // expected-warning {{use of logical '||' with constant operand}} \ |
| 60 | // expected-note {{use '|' for a bitwise operation}} |
Argyrios Kyrtzidis | 1b2ad2f | 2010-09-19 23:03:35 +0000 | [diff] [blame] | 61 | int comma3[(1,2)]; // expected-warning {{size of static array must be an integer constant expression}} \ |
| 62 | // expected-warning {{expression result unused}} |
Eli Friedman | f04ec67 | 2009-02-27 04:46:32 +0000 | [diff] [blame] | 63 | |
| 64 | // Pointer + __builtin_constant_p |
| 65 | char pbcp[__builtin_constant_p(4) ? (intptr_t)&expr : 0]; // expected-error {{variable length array declaration not allowed at file scope}} |
| 66 | |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 67 | int illegaldiv1a[1 || 1/0]; // expected-warning {{division by zero is undefined}} |
| 68 | int illegaldiv1b[1 && 1/0]; // expected-warning {{division by zero is undefined}} expected-error{{variable length array declaration not allowed at file scope}} |
| 69 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 70 | int illegaldiv2[1/0]; // expected-error {{variable length array declaration not allowed at file scope}} \ |
| 71 | // expected-warning {{division by zero is undefined}} |
Eli Friedman | f04ec67 | 2009-02-27 04:46:32 +0000 | [diff] [blame] | 72 | int illegaldiv3[INT_MIN / -1]; // expected-error {{variable length array declaration not allowed at file scope}} |
John McCall | 3b332ab | 2011-02-26 08:27:17 +0000 | [diff] [blame] | 73 | // PR9262 |
| 74 | int illegaldiv4[0 / (1 / 0)]; // expected-warning {{division by zero is undefined}} expected-error {{variable length array declaration not allowed at file scope}} |
Eli Friedman | f04ec67 | 2009-02-27 04:46:32 +0000 | [diff] [blame] | 75 | |
Eli Friedman | eb32fde | 2009-04-28 03:13:54 +0000 | [diff] [blame] | 76 | int chooseexpr[__builtin_choose_expr(1, 1, expr)]; |
| 77 | int realop[(__real__ 4) == 4 ? 1 : -1]; |
| 78 | int imagop[(__imag__ 4) == 0 ? 1 : -1]; |