1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
|
// RUN: %clang_cc1 -triple x86_64-linux -fexperimental-new-constant-interpreter -verify=expected,all -std=c11 -Wcast-qual %s
// RUN: %clang_cc1 -triple x86_64-linux -fexperimental-new-constant-interpreter -pedantic -verify=pedantic,pedantic-expected,all -std=c11 -Wcast-qual %s
// RUN: %clang_cc1 -triple x86_64-linux -verify=ref,all -std=c11 -Wcast-qual %s
// RUN: %clang_cc1 -triple x86_64-linux -pedantic -verify=pedantic,pedantic-ref,all -std=c11 -Wcast-qual %s
typedef __INTPTR_TYPE__ intptr_t;
typedef __PTRDIFF_TYPE__ ptrdiff_t;
_Static_assert(1, "");
_Static_assert(__objc_yes, "");
_Static_assert(!__objc_no, "");
_Static_assert(0 != 1, "");
_Static_assert(1.0 == 1.0, ""); // pedantic-ref-warning {{not an integer constant expression}} \
// pedantic-expected-warning {{not an integer constant expression}}
_Static_assert(1 && 1.0, ""); // pedantic-ref-warning {{not an integer constant expression}} \
// pedantic-expected-warning {{not an integer constant expression}}
_Static_assert( (5 > 4) + (3 > 2) == 2, "");
_Static_assert(!!1.0, ""); // pedantic-ref-warning {{not an integer constant expression}} \
// pedantic-expected-warning {{not an integer constant expression}}
_Static_assert(!!1, "");
_Static_assert(!(_Bool){(void*)0}, ""); // pedantic-ref-warning {{not an integer constant expression}} \
// pedantic-expected-warning {{not an integer constant expression}}
int a = (1 == 1 ? 5 : 3);
_Static_assert(a == 5, ""); // all-error {{not an integral constant expression}}
const int DiscardedPtrToIntCast = ((intptr_t)((void*)0), 0); // all-warning {{left operand of comma operator has no effect}}
const int b = 3;
_Static_assert(b == 3, ""); // pedantic-ref-warning {{not an integer constant expression}} \
// pedantic-expected-warning {{not an integer constant expression}}
const int c; // all-note {{declared here}}
_Static_assert(c == 0, ""); // ref-error {{not an integral constant expression}} \
// ref-note {{initializer of 'c' is unknown}} \
// pedantic-ref-error {{not an integral constant expression}} \
// pedantic-ref-note {{initializer of 'c' is unknown}} \
// expected-error {{not an integral constant expression}} \
// expected-note {{initializer of 'c' is unknown}} \
// pedantic-expected-error {{not an integral constant expression}} \
// pedantic-expected-note {{initializer of 'c' is unknown}}
_Static_assert(&c != 0, ""); // ref-warning {{always true}} \
// pedantic-ref-warning {{always true}} \
// pedantic-ref-warning {{is a GNU extension}} \
// expected-warning {{always true}} \
// pedantic-expected-warning {{always true}} \
// pedantic-expected-warning {{is a GNU extension}}
_Static_assert(&a != 0, ""); // ref-warning {{always true}} \
// pedantic-ref-warning {{always true}} \
// pedantic-ref-warning {{is a GNU extension}} \
// expected-warning {{always true}} \
// pedantic-expected-warning {{always true}} \
// pedantic-expected-warning {{is a GNU extension}}
_Static_assert((&c + 1) != 0, ""); // pedantic-ref-warning {{is a GNU extension}} \
// pedantic-expected-warning {{is a GNU extension}}
_Static_assert((&a + 100) != 0, ""); // pedantic-ref-warning {{is a GNU extension}} \
// pedantic-ref-note {{100 of non-array}} \
// pedantic-expected-note {{100 of non-array}} \
// pedantic-expected-warning {{is a GNU extension}}
_Static_assert((&a - 100) != 0, ""); // pedantic-ref-warning {{is a GNU extension}} \
// pedantic-expected-warning {{is a GNU extension}} \
// pedantic-ref-note {{-100 of non-array}} \
// pedantic-expected-note {{-100 of non-array}}
/// extern variable of a composite type.
extern struct Test50S Test50;
_Static_assert(&Test50 != (void*)0, ""); // all-warning {{always true}} \
// pedantic-warning {{is a GNU extension}} \
// pedantic-note {{this conversion is not allowed in a constant expression}}
struct y {int x,y;};
int a2[(intptr_t)&((struct y*)0)->y]; // all-warning {{folded to constant array}}
const struct y *yy = (struct y*)0;
const intptr_t L = (intptr_t)(&(yy->y)); // all-error {{not a compile-time constant}}
_Static_assert((long)&((struct y*)0)->y > 0, ""); // pedantic-ref-warning {{GNU extension}} \
// pedantic-ref-note {{this conversion is not allowed in a constant expression}} \
// pedantic-expected-warning {{GNU extension}} \
// pedantic-expected-note {{this conversion is not allowed in a constant expression}}
const ptrdiff_t m = &m + 137 - &m;
_Static_assert(m == 137, ""); // pedantic-ref-warning {{GNU extension}} \
// pedantic-expected-warning {{GNU extension}}
/// from test/Sema/switch.c, used to cause an assertion failure.
void f (int z) {
while (z) {
default: z--; // all-error {{'default' statement not in switch}}
}
}
int expr;
int chooseexpr[__builtin_choose_expr(1, 1, expr)];
int somefunc(int i) {
return (i, 65537) * 65537; // all-warning {{left operand of comma operator has no effect}} \
// all-warning {{overflow in expression; result is 131'073 with type 'int'}}
}
/// FIXME: The following test is incorrect in the new interpreter.
/// The null pointer returns 16 from its getIntegerRepresentation().
#pragma clang diagnostic ignored "-Wpointer-to-int-cast"
struct ArrayStruct {
char n[1];
};
char name2[(int)&((struct ArrayStruct*)0)->n]; // expected-warning {{folded to constant array}} \
// pedantic-expected-warning {{folded to constant array}} \
// ref-warning {{folded to constant array}} \
// pedantic-ref-warning {{folded to constant array}}
_Static_assert(sizeof(name2) == 0, ""); // expected-error {{failed}} \
// expected-note {{evaluates to}} \
// pedantic-expected-error {{failed}} \
// pedantic-expected-note {{evaluates to}}
#ifdef __SIZEOF_INT128__
void *PR28739d = &(&PR28739d)[(__int128)(unsigned long)-1]; // all-warning {{refers past the last possible element}}
#endif
extern float global_float;
struct XX { int a, *b; };
struct XY { int before; struct XX xx, *xp; float* after; } xy[] = {
0, 0, &xy[0].xx.a, &xy[0].xx, &global_float,
[1].xx = 0, &xy[1].xx.a, &xy[1].xx, &global_float,
0, // all-note {{previous initialization is here}}
0, // all-note {{previous initialization is here}}
[2].before = 0, // all-warning {{initializer overrides prior initialization of this subobject}}
0, // all-warning {{initializer overrides prior initialization of this subobject}}
&xy[2].xx.a, &xy[2].xx, &global_float
};
void t14(void) {
int array[256] = { 0 }; // expected-note {{array 'array' declared here}} \
// pedantic-expected-note {{array 'array' declared here}} \
// ref-note {{array 'array' declared here}} \
// pedantic-ref-note {{array 'array' declared here}}
const char b = -1;
int val = array[b]; // expected-warning {{array index -1 is before the beginning of the array}} \
// pedantic-expected-warning {{array index -1 is before the beginning of the array}} \
// ref-warning {{array index -1 is before the beginning of the array}} \
// pedantic-ref-warning {{array index -1 is before the beginning of the array}}
}
void bar_0(void) {
struct C {
const int a;
int b;
};
const struct C S = {0, 0};
*(int *)(&S.a) = 0; // all-warning {{cast from 'const int *' to 'int *' drops const qualifier}}
*(int *)(&S.b) = 0; // all-warning {{cast from 'const int *' to 'int *' drops const qualifier}}
}
/// Complex-to-bool casts.
const int A = ((_Complex double)1.0 ? 21 : 1);
_Static_assert(A == 21, ""); // pedantic-ref-warning {{GNU extension}} \
// pedantic-expected-warning {{GNU extension}}
const int CTI1 = ((_Complex double){0.0, 1.0}); // pedantic-ref-warning {{extension}} \
// pedantic-expected-warning {{extension}}
_Static_assert(CTI1 == 0, ""); // pedantic-ref-warning {{GNU extension}} \
// pedantic-expected-warning {{GNU extension}}
const _Bool CTB2 = (_Bool)(_Complex double){0.0, 1.0}; // pedantic-ref-warning {{extension}} \
// pedantic-expected-warning {{extension}}
_Static_assert(CTB2, ""); // pedantic-ref-warning {{GNU extension}} \
// pedantic-expected-warning {{GNU extension}}
const _Bool CTB3 = (_Complex double){0.0, 1.0}; // pedantic-ref-warning {{extension}} \
// pedantic-expected-warning {{extension}}
_Static_assert(CTB3, ""); // pedantic-ref-warning {{GNU extension}} \
// pedantic-expected-warning {{GNU extension}}
int t1 = sizeof(int);
void test4(void) {
t1 = sizeof(int);
}
void localCompoundLiteral(void) {
struct S { int x, y; } s = {}; // pedantic-expected-warning {{use of an empty initializer}} \
// pedantic-ref-warning {{use of an empty initializer}}
struct T {
int i;
struct S s;
} t1 = { 1, {} }; // pedantic-expected-warning {{use of an empty initializer}} \
// pedantic-ref-warning {{use of an empty initializer}}
struct T t3 = {
(int){}, // pedantic-expected-warning {{use of an empty initializer}} \
// pedantic-ref-warning {{use of an empty initializer}}
{} // pedantic-expected-warning {{use of an empty initializer}} \
// pedantic-ref-warning {{use of an empty initializer}}
};
}
/// struct copy
struct StrA {int a; };
const struct StrA sa = { 12 };
const struct StrA * const sb = &sa;
const struct StrA sc = *sb;
_Static_assert(sc.a == 12, ""); // pedantic-ref-warning {{GNU extension}} \
// pedantic-expected-warning {{GNU extension}}
_Static_assert(((void*)0 + 1) != (void*)0, ""); // pedantic-expected-warning {{arithmetic on a pointer to void is a GNU extension}} \
// pedantic-expected-warning {{not an integer constant expression}} \
// pedantic-expected-note {{cannot perform pointer arithmetic on null pointer}} \
// pedantic-ref-warning {{arithmetic on a pointer to void is a GNU extension}} \
// pedantic-ref-warning {{not an integer constant expression}} \
// pedantic-ref-note {{cannot perform pointer arithmetic on null pointer}}
typedef __INTPTR_TYPE__ intptr_t;
int array[(intptr_t)(int*)1]; // ref-warning {{variable length array folded to constant array}} \
// pedantic-ref-warning {{variable length array folded to constant array}} \
// expected-warning {{variable length array folded to constant array}} \
// pedantic-expected-warning {{variable length array folded to constant array}}
int castViaInt[*(int*)(unsigned long)"test"]; // ref-error {{variable length array}} \
// pedantic-ref-error {{variable length array}} \
// expected-error {{variable length array}} \
// pedantic-expected-error {{variable length array}}
const void (*const funcp)(void) = (void*)123; // pedantic-warning {{converts between void pointer and function pointer}}
_Static_assert(funcp == (void*)0, ""); // all-error {{failed due to requirement 'funcp == (void *)0'}} \
// pedantic-warning {{expression is not an integer constant expression}}
_Static_assert(funcp == (void*)123, ""); // pedantic-warning {{equality comparison between function pointer and void pointer}} \
// pedantic-warning {{expression is not an integer constant expression}}
void unaryops(void) {
(void)(++(struct x {unsigned x;}){3}.x);
(void)(--(struct y {unsigned x;}){3}.x);
(void)(++(struct z {float x;}){3}.x);
(void)(--(struct w {float x;}){3}.x);
(void)((struct xx {unsigned x;}){3}.x++);
(void)((struct yy {unsigned x;}){3}.x--);
(void)((struct zz {float x;}){3}.x++);
(void)((struct ww {float x;}){3}.x--);
}
/// This used to fail because we didn't properly mark the struct
/// initialized through a CompoundLiteralExpr as initialized.
struct TestStruct {
int a;
int b;
};
int Y __attribute__((annotate(
"GlobalValAnnotationWithArgs",
42,
(struct TestStruct) { .a = 1, .b = 2 }
)));
#ifdef __SIZEOF_INT128__
const int *p = &b;
const __int128 K = (__int128)(int*)0;
const unsigned __int128 KU = (unsigned __int128)(int*)0;
#endif
int test3(void) {
int a[2];
a[0] = test3; // all-error {{incompatible pointer to integer conversion assigning to 'int' from 'int (void)'}}
return 0;
}
/// This tests that we have full type info, even for values we cannot read.
int dummyarray[5];
_Static_assert(&dummyarray[0] < &dummyarray[1], ""); // pedantic-warning {{GNU extension}}
void addrlabelexpr(void) {
a0: ;
static void *ps[] = { &&a0 }; // pedantic-warning {{use of GNU address-of-label extension}}
}
extern void cv2;
void *foo5 (void)
{
return &cv2; // pedantic-warning{{address of an expression of type 'void'}}
}
__attribute__((weak)) const unsigned int test10_bound = 10;
char test10_global[test10_bound]; // all-error {{variable length array declaration not allowed at file scope}}
void test10(void) {
char test10_local[test10_bound] = "help"; // all-error {{variable-sized object may not be initialized}}
}
void SuperSpecialFunc(void) {
const int SuperSpecialCase = 10;
_Static_assert((sizeof(SuperSpecialCase) == 12 && SuperSpecialCase == 3) || SuperSpecialCase == 10, ""); // pedantic-warning {{GNU extension}}
}
void T1(void) {
static int *y[1] = {({ static int _x = 20; (void*)0;})}; // all-error {{initializer element is not a compile-time constant}} \
// pedantic-warning {{use of GNU statement expression extension}}
}
|