| 1 |
// RUN: %clang_cc1 -fsyntax-only -verify=both,ref -triple x86_64-linux %s -Wno-tautological-pointer-compare -Wno-pointer-to-int-cast |
| 2 |
// RUN: %clang_cc1 -fsyntax-only -verify=both,expected -triple x86_64-linux %s -Wno-tautological-pointer-compare -Wno-pointer-to-int-cast -fexperimental-new-constant-interpreter -DNEW_INTERP |
| 3 |
// RUN: %clang_cc1 -fsyntax-only -verify=both,ref -triple powerpc64-ibm-aix-xcoff %s -Wno-tautological-pointer-compare -Wno-pointer-to-int-cast |
| 4 |
// RUN: %clang_cc1 -fsyntax-only -verify=both,expected -triple powerpc64-ibm-aix-xcoff %s -Wno-tautological-pointer-compare -Wno-pointer-to-int-cast -fexperimental-new-constant-interpreter -DNEW_INTERP |
| 5 |
|
| 6 |
/// This is a version of test/Sema/const-eval.c with the |
| 7 |
/// tests commented out that the new constant expression interpreter does |
| 8 |
/// not support yet. They are all marked with the NEW_INTERP define. |
| 9 |
|
| 10 |
#define EVAL_EXPR(testno, expr) enum { test##testno = (expr) }; struct check_positive##testno { int a[test##testno]; }; |
| 11 |
int x; |
| 12 |
EVAL_EXPR(1, (_Bool)&x) |
| 13 |
EVAL_EXPR(2, (int)(1.0+(double)4)) |
| 14 |
EVAL_EXPR(3, (int)(1.0+(float)4.0)) |
| 15 |
EVAL_EXPR(4, (_Bool)(1 ? (void*)&x : 0)) |
| 16 |
EVAL_EXPR(5, (_Bool)(int[]){0}) |
| 17 |
struct y {int x,y;}; |
| 18 |
EVAL_EXPR(6, (int)(1+(struct y*)0)) |
| 19 |
_Static_assert((long)&((struct y*)0)->y > 0, ""); |
| 20 |
EVAL_EXPR(7, (int)&((struct y*)0)->y) |
| 21 |
EVAL_EXPR(8, (_Bool)"asdf") |
| 22 |
EVAL_EXPR(9, !!&x) |
| 23 |
EVAL_EXPR(10, ((void)1, 12)) |
| 24 |
void g0(void); |
| 25 |
EVAL_EXPR(11, (g0(), 12)) // both-error {{not an integer constant expression}} |
| 26 |
EVAL_EXPR(12, 1.0&&2.0) |
| 27 |
EVAL_EXPR(13, x || 3.0) // both-error {{not an integer constant expression}} |
| 28 |
|
| 29 |
unsigned int l_19 = 1; |
| 30 |
EVAL_EXPR(14, (1 ^ l_19) && 1); // both-error {{not an integer constant expression}} |
| 31 |
|
| 32 |
void f(void) |
| 33 |
{ |
| 34 |
int a; |
| 35 |
EVAL_EXPR(15, (_Bool)&a); |
| 36 |
} |
| 37 |
|
| 38 |
_Complex float g16 = (1.0f + 1.0fi); |
| 39 |
|
| 40 |
// ?: in constant expressions. |
| 41 |
int g17[(3?:1) - 2]; |
| 42 |
|
| 43 |
EVAL_EXPR(18, ((int)((void*)10 + 10)) == 20 ? 1 : -1); |
| 44 |
|
| 45 |
struct s { |
| 46 |
int a[(int)-1.0f]; // both-error {{array size is negative}} |
| 47 |
}; |
| 48 |
|
| 49 |
EVAL_EXPR(19, ((int)&*(char*)10 == 10 ? 1 : -1)); |
| 50 |
|
| 51 |
EVAL_EXPR(20, __builtin_constant_p(*((int*) 10))); |
| 52 |
|
| 53 |
EVAL_EXPR(21, (__imag__ 2i) == 2 ? 1 : -1); |
| 54 |
|
| 55 |
EVAL_EXPR(22, (__real__ (2i+3)) == 3 ? 1 : -1); |
| 56 |
|
| 57 |
int g23[(int)(1.0 / 1.0)] = { 1 }; // both-warning {{folded to constant array}} |
| 58 |
int g24[(int)(1.0 / 1.0)] = { 1 , 2 }; // both-warning {{folded to constant array}} \ |
| 59 |
// both-warning {{excess elements in array initializer}} |
| 60 |
int g25[(int)(1.0 + 1.0)], g26 = sizeof(g25); // both-warning {{folded to constant array}} |
| 61 |
|
| 62 |
EVAL_EXPR(26, (_Complex double)0 ? -1 : 1) |
| 63 |
EVAL_EXPR(27, (_Complex int)0 ? -1 : 1) |
| 64 |
EVAL_EXPR(28, (_Complex double)1 ? 1 : -1) |
| 65 |
EVAL_EXPR(29, (_Complex int)1 ? 1 : -1) |
| 66 |
|
| 67 |
// PR4027 |
| 68 |
struct a { int x, y; }; |
| 69 |
static struct a V2 = (struct a)(struct a){ 1, 2}; |
| 70 |
static const struct a V1 = (struct a){ 1, 2}; |
| 71 |
|
| 72 |
EVAL_EXPR(30, (int)(_Complex float)((1<<30)-1) == (1<<30) ? 1 : -1) |
| 73 |
EVAL_EXPR(31, (int*)0 == (int*)0 ? 1 : -1) |
| 74 |
EVAL_EXPR(32, (int*)0 != (int*)0 ? -1 : 1) |
| 75 |
EVAL_EXPR(33, (void*)0 - (void*)0 == 0 ? 1 : -1) |
| 76 |
|
| 77 |
void foo(void) {} |
| 78 |
EVAL_EXPR(34, (foo == (void *)0) ? -1 : 1) |
| 79 |
|
| 80 |
// No PR. Mismatched bitwidths lead to a crash on second evaluation. |
| 81 |
const _Bool constbool = 0; |
| 82 |
EVAL_EXPR(35, constbool) |
| 83 |
EVAL_EXPR(36, constbool) |
| 84 |
|
| 85 |
EVAL_EXPR(37, ((void)1,2.0) == 2.0 ? 1 : -1) |
| 86 |
EVAL_EXPR(38, __builtin_expect(1,1) == 1 ? 1 : -1) |
| 87 |
|
| 88 |
// PR7884 |
| 89 |
EVAL_EXPR(39, __real__(1.f) == 1 ? 1 : -1) |
| 90 |
EVAL_EXPR(40, __imag__(1.f) == 0 ? 1 : -1) |
| 91 |
|
| 92 |
// From gcc testsuite |
| 93 |
EVAL_EXPR(41, (int)(1+(_Complex unsigned)2)) |
| 94 |
|
| 95 |
void rdar8875946(void) { |
| 96 |
double _Complex P; |
| 97 |
float _Complex P2 = 3.3f + P; |
| 98 |
} |
| 99 |
|
| 100 |
double d = (d = 0.0); // both-error {{not a compile-time constant}} |
| 101 |
double d2 = ++d; // both-error {{not a compile-time constant}} |
| 102 |
|
| 103 |
int n = 2; |
| 104 |
int intLvalue[*(int*)((long)&n ?: 1)] = { 1, 2 }; // both-error {{variable length array}} |
| 105 |
|
| 106 |
union u { int a; char b[4]; }; |
| 107 |
char c = ((union u)(123456)).b[0]; // both-error {{not a compile-time constant}} |
| 108 |
|
| 109 |
extern const int weak_int __attribute__((weak)); |
| 110 |
const int weak_int = 42; |
| 111 |
int weak_int_test = weak_int; // both-error {{not a compile-time constant}} |
| 112 |
|
| 113 |
int literalVsNull1 = "foo" == 0; |
| 114 |
int literalVsNull2 = 0 == "foo"; |
| 115 |
|
| 116 |
// PR11385. |
| 117 |
int castViaInt[*(int*)(unsigned long)"test"]; // both-error {{variable length array}} |
| 118 |
|
| 119 |
// PR11391. |
| 120 |
struct PR11391 { _Complex float f; } pr11391; |
| 121 |
EVAL_EXPR(42, __builtin_constant_p(pr11391.f = 1)) |
| 122 |
|
| 123 |
// PR12043 |
| 124 |
float varfloat; |
| 125 |
const float constfloat = 0; |
| 126 |
EVAL_EXPR(43, varfloat && constfloat) // both-error {{not an integer constant expression}} |
| 127 |
EVAL_EXPR(45, ((char*)-1) + 1 == 0 ? 1 : -1) |
| 128 |
EVAL_EXPR(46, ((char*)-1) + 1 < (char*) -1 ? 1 : -1) |
| 129 |
EVAL_EXPR(47, &x < &x + 1 ? 1 : -1) |
| 130 |
EVAL_EXPR(48, &x != &x - 1 ? 1 : -1) // expected-error {{declared as an array with a negative size}} |
| 131 |
EVAL_EXPR(49, &x < &x - 100 ? 1 : -1) // ref-error {{not an integer constant expression}} |
| 132 |
|
| 133 |
extern struct Test50S Test50; |
| 134 |
EVAL_EXPR(50, &Test50 < (struct Test50S*)((unsigned long)&Test50 + 10)) // both-error {{not an integer constant expression}} |
| 135 |
|
| 136 |
EVAL_EXPR(51, 0 != (float)1e99) |
| 137 |
|
| 138 |
// PR21945 |
| 139 |
void PR21945(void) { int i = (({}), 0l); } |
| 140 |
|
| 141 |
void PR24622(void); |
| 142 |
struct PR24622 {} pr24622; |
| 143 |
EVAL_EXPR(52, &pr24622 == (void *)&PR24622); |
| 144 |
|
| 145 |
// We evaluate these by providing 2s' complement semantics in constant |
| 146 |
// expressions, like we do for integers. |
| 147 |
void *PR28739a = (__int128)(unsigned long)-1 + &PR28739a; // both-warning {{the pointer incremented by 18'446'744'073'709'551'615 refers past the last possible element for an array in 64-bit address space containing 64-bit (8-byte) elements (max possible 2'305'843'009'213'693'952 elements)}} |
| 148 |
|
| 149 |
void *PR28739b = &PR28739b + (__int128)(unsigned long)-1; // both-warning {{refers past the last possible element}} |
| 150 |
__int128 PR28739c = (&PR28739c + (__int128)(unsigned long)-1) - &PR28739c; // both-warning {{refers past the last possible element}} |
| 151 |
void *PR28739d = &(&PR28739d)[(__int128)(unsigned long)-1]; // both-warning {{refers past the last possible element}} |
| 152 |
|
| 153 |
struct PR35214_X { |
| 154 |
int k; |
| 155 |
int arr[]; |
| 156 |
}; |
| 157 |
int PR35214_x; |
| 158 |
int PR35214_y = ((struct PR35214_X *)&PR35214_x)->arr[1]; // both-error {{not a compile-time constant}} |
| 159 |
#ifndef NEW_INTERP |
| 160 |
int *PR35214_z = &((struct PR35214_X *)&PR35214_x)->arr[1]; // ok, &PR35214_x + 2 |
| 161 |
#endif |
| 162 |
|
| 163 |
/// From const-eval-64.c |
| 164 |
EVAL_EXPR(53, ((char*)-1LL) + 1 == 0 ? 1 : -1) |
| 165 |
EVAL_EXPR(54, ((char*)-1LL) + 1 < (char*) -1 ? 1 : -1) |
| 166 |
|
| 167 |
/// === Additions === |
| 168 |
#if __SIZEOF_INT__ == 4 |
| 169 |
typedef __INTPTR_TYPE__ intptr_t; |
| 170 |
const intptr_t A = (intptr_t)(((int*) 0) + 1); |
| 171 |
const intptr_t B = (intptr_t)(((char*)0) + 3); |
| 172 |
_Static_assert(A > B, ""); |
| 173 |
int * GH149500_p = &(*(int *)0x400); |
| 174 |
static const void *GH149500_q = &(*(const struct sysrq_key_op *)0); |
| 175 |
|
| 176 |
#else |
| 177 |
#error :( |
| 178 |
#endif |