408 lines · c
1// RUN: %clang_cc1 -triple x86_64-linux -verify=expected,all -std=c11 -Wcast-qual %s -fexperimental-new-constant-interpreter2// RUN: %clang_cc1 -triple x86_64-linux -verify=pedantic,pedantic-expected,all -std=c11 -Wcast-qual -pedantic %s -fexperimental-new-constant-interpreter3// RUN: %clang_cc1 -triple x86_64-linux -verify=ref,all -std=c11 -Wcast-qual %s4// RUN: %clang_cc1 -triple x86_64-linux -verify=pedantic,pedantic-ref,all -std=c11 -Wcast-qual -pedantic %s5 6typedef __INTPTR_TYPE__ intptr_t;7typedef __PTRDIFF_TYPE__ ptrdiff_t;8 9_Static_assert(1, "");10 11_Static_assert(__objc_yes, "");12_Static_assert(!__objc_no, "");13 14_Static_assert(0 != 1, "");15_Static_assert(1.0 == 1.0, ""); // pedantic-ref-warning {{not an integer constant expression}} \16 // pedantic-expected-warning {{not an integer constant expression}}17_Static_assert(1 && 1.0, ""); // pedantic-ref-warning {{not an integer constant expression}} \18 // pedantic-expected-warning {{not an integer constant expression}}19_Static_assert( (5 > 4) + (3 > 2) == 2, "");20_Static_assert(!!1.0, ""); // pedantic-ref-warning {{not an integer constant expression}} \21 // pedantic-expected-warning {{not an integer constant expression}}22_Static_assert(!!1, "");23 24_Static_assert(!(_Bool){(void*)0}, ""); // pedantic-ref-warning {{not an integer constant expression}} \25 // pedantic-expected-warning {{not an integer constant expression}}26 27int a = (1 == 1 ? 5 : 3);28_Static_assert(a == 5, ""); // all-error {{not an integral constant expression}}29 30const int DiscardedPtrToIntCast = ((intptr_t)((void*)0), 0); // all-warning {{left operand of comma operator has no effect}}31 32const int b = 3;33_Static_assert(b == 3, ""); // pedantic-ref-warning {{not an integer constant expression}} \34 // pedantic-expected-warning {{not an integer constant expression}}35 36const int c; // all-note {{declared here}}37_Static_assert(c == 0, ""); // ref-error {{not an integral constant expression}} \38 // ref-note {{initializer of 'c' is unknown}} \39 // pedantic-ref-error {{not an integral constant expression}} \40 // pedantic-ref-note {{initializer of 'c' is unknown}} \41 // expected-error {{not an integral constant expression}} \42 // expected-note {{initializer of 'c' is unknown}} \43 // pedantic-expected-error {{not an integral constant expression}} \44 // pedantic-expected-note {{initializer of 'c' is unknown}}45 46_Static_assert(&c != 0, ""); // ref-warning {{always true}} \47 // pedantic-ref-warning {{always true}} \48 // pedantic-ref-warning {{is a GNU extension}} \49 // expected-warning {{always true}} \50 // pedantic-expected-warning {{always true}} \51 // pedantic-expected-warning {{is a GNU extension}}52_Static_assert(&a != 0, ""); // ref-warning {{always true}} \53 // pedantic-ref-warning {{always true}} \54 // pedantic-ref-warning {{is a GNU extension}} \55 // expected-warning {{always true}} \56 // pedantic-expected-warning {{always true}} \57 // pedantic-expected-warning {{is a GNU extension}}58_Static_assert((&c + 1) != 0, ""); // pedantic-ref-warning {{is a GNU extension}} \59 // pedantic-expected-warning {{is a GNU extension}}60_Static_assert((&a + 100) != 0, ""); // pedantic-ref-warning {{is a GNU extension}} \61 // pedantic-ref-note {{100 of non-array}} \62 // pedantic-expected-note {{100 of non-array}} \63 // pedantic-expected-warning {{is a GNU extension}}64_Static_assert((&a - 100) != 0, ""); // pedantic-ref-warning {{is a GNU extension}} \65 // pedantic-expected-warning {{is a GNU extension}} \66 // pedantic-ref-note {{-100 of non-array}} \67 // pedantic-expected-note {{-100 of non-array}}68/// extern variable of a composite type.69extern struct Test50S Test50;70_Static_assert(&Test50 != (void*)0, ""); // all-warning {{always true}} \71 // pedantic-warning {{is a GNU extension}} \72 // pedantic-note {{this conversion is not allowed in a constant expression}}73 74struct y {int x,y;};75int a2[(intptr_t)&((struct y*)0)->y]; // all-warning {{folded to constant array}}76 77const struct y *yy = (struct y*)0;78const intptr_t L = (intptr_t)(&(yy->y)); // all-error {{not a compile-time constant}}79 80_Static_assert((long)&((struct y*)0)->y > 0, ""); // pedantic-ref-warning {{GNU extension}} \81 // pedantic-ref-note {{this conversion is not allowed in a constant expression}} \82 // pedantic-expected-warning {{GNU extension}} \83 // pedantic-expected-note {{this conversion is not allowed in a constant expression}}84 85const ptrdiff_t m = &m + 137 - &m;86_Static_assert(m == 137, ""); // pedantic-ref-warning {{GNU extension}} \87 // pedantic-expected-warning {{GNU extension}}88 89/// from test/Sema/switch.c, used to cause an assertion failure.90void f (int z) {91 while (z) {92 default: z--; // all-error {{'default' statement not in switch}}93 }94}95 96int expr;97int chooseexpr[__builtin_choose_expr(1, 1, expr)];98 99int somefunc(int i) {100 return (i, 65537) * 65537; // all-warning {{left operand of comma operator has no effect}} \101 // all-warning {{overflow in expression; result is 131'073 with type 'int'}}102}103 104#pragma clang diagnostic ignored "-Wpointer-to-int-cast"105struct ArrayStruct {106 char n[1];107};108char name2[(int)&((struct ArrayStruct*)0)->n]; // expected-warning {{folded to constant array}} \109 // pedantic-expected-warning {{folded to constant array}} \110 // ref-warning {{folded to constant array}} \111 // pedantic-ref-warning {{folded to constant array}}112_Static_assert(sizeof(name2) == 0, "");113 114#ifdef __SIZEOF_INT128__115void *PR28739d = &(&PR28739d)[(__int128)(unsigned long)-1]; // all-warning {{refers past the last possible element}}116#endif117 118extern float global_float;119struct XX { int a, *b; };120struct XY { int before; struct XX xx, *xp; float* after; } xy[] = {121 0, 0, &xy[0].xx.a, &xy[0].xx, &global_float,122 [1].xx = 0, &xy[1].xx.a, &xy[1].xx, &global_float,123 0, // all-note {{previous initialization is here}}124 0, // all-note {{previous initialization is here}}125 [2].before = 0, // all-warning {{initializer overrides prior initialization of this subobject}}126 0, // all-warning {{initializer overrides prior initialization of this subobject}}127 &xy[2].xx.a, &xy[2].xx, &global_float128};129 130void t14(void) {131 int array[256] = { 0 }; // expected-note {{array 'array' declared here}} \132 // pedantic-expected-note {{array 'array' declared here}} \133 // ref-note {{array 'array' declared here}} \134 // pedantic-ref-note {{array 'array' declared here}}135 const char b = -1;136 int val = array[b]; // expected-warning {{array index -1 is before the beginning of the array}} \137 // pedantic-expected-warning {{array index -1 is before the beginning of the array}} \138 // ref-warning {{array index -1 is before the beginning of the array}} \139 // pedantic-ref-warning {{array index -1 is before the beginning of the array}}140 141}142 143void bar_0(void) {144 struct C {145 const int a;146 int b;147 };148 149 const struct C S = {0, 0};150 151 *(int *)(&S.a) = 0; // all-warning {{cast from 'const int *' to 'int *' drops const qualifier}}152 *(int *)(&S.b) = 0; // all-warning {{cast from 'const int *' to 'int *' drops const qualifier}}153}154 155/// Complex-to-bool casts.156const int A = ((_Complex double)1.0 ? 21 : 1);157_Static_assert(A == 21, ""); // pedantic-ref-warning {{GNU extension}} \158 // pedantic-expected-warning {{GNU extension}}159 160const int CTI1 = ((_Complex double){0.0, 1.0}); // pedantic-ref-warning {{extension}} \161 // pedantic-expected-warning {{extension}}162_Static_assert(CTI1 == 0, ""); // pedantic-ref-warning {{GNU extension}} \163 // pedantic-expected-warning {{GNU extension}}164 165const _Bool CTB2 = (_Bool)(_Complex double){0.0, 1.0}; // pedantic-ref-warning {{extension}} \166 // pedantic-expected-warning {{extension}}167_Static_assert(CTB2, ""); // pedantic-ref-warning {{GNU extension}} \168 // pedantic-expected-warning {{GNU extension}}169 170const _Bool CTB3 = (_Complex double){0.0, 1.0}; // pedantic-ref-warning {{extension}} \171 // pedantic-expected-warning {{extension}}172_Static_assert(CTB3, ""); // pedantic-ref-warning {{GNU extension}} \173 // pedantic-expected-warning {{GNU extension}}174 175 176void nonComplexToComplexCast(void) {177 _Complex double z = *(_Complex double *)&(struct { double r, i; }){0.0, 1.0};178}179 180int t1 = sizeof(int);181void test4(void) {182 t1 = sizeof(int);183}184 185void localCompoundLiteral(void) {186 struct S { int x, y; } s = {}; // pedantic-expected-warning {{use of an empty initializer}} \187 // pedantic-ref-warning {{use of an empty initializer}}188 struct T {189 int i;190 struct S s;191 } t1 = { 1, {} }; // pedantic-expected-warning {{use of an empty initializer}} \192 // pedantic-ref-warning {{use of an empty initializer}}193 194 struct T t3 = {195 (int){}, // pedantic-expected-warning {{use of an empty initializer}} \196 // pedantic-ref-warning {{use of an empty initializer}}197 {} // pedantic-expected-warning {{use of an empty initializer}} \198 // pedantic-ref-warning {{use of an empty initializer}}199 };200}201 202/// struct copy203struct StrA {int a; };204const struct StrA sa = { 12 };205const struct StrA * const sb = &sa;206const struct StrA sc = *sb;207_Static_assert(sc.a == 12, ""); // pedantic-ref-warning {{GNU extension}} \208 // pedantic-expected-warning {{GNU extension}}209 210struct ComplexS {211 int a;212 float b;213 struct StrA sa[2];214};215const struct ComplexS CS = {12, 23.0f, {{1}, {2}}};216const struct ComplexS CS2 = CS;217_Static_assert(CS2.sa[0].a == 1, ""); // pedantic-ref-warning {{GNU extension}} \218 // pedantic-expected-warning {{GNU extension}}219 220_Static_assert(((void*)0 + 1) != (void*)0, ""); // pedantic-expected-warning {{arithmetic on a pointer to void is a GNU extension}} \221 // pedantic-expected-warning {{not an integer constant expression}} \222 // pedantic-expected-note {{cannot perform pointer arithmetic on null pointer}} \223 // pedantic-ref-warning {{arithmetic on a pointer to void is a GNU extension}} \224 // pedantic-ref-warning {{not an integer constant expression}} \225 // pedantic-ref-note {{cannot perform pointer arithmetic on null pointer}}226 227typedef __INTPTR_TYPE__ intptr_t;228int array[(intptr_t)(int*)1]; // ref-warning {{variable length array folded to constant array}} \229 // pedantic-ref-warning {{variable length array folded to constant array}} \230 // expected-warning {{variable length array folded to constant array}} \231 // pedantic-expected-warning {{variable length array folded to constant array}}232 233int castViaInt[*(int*)(unsigned long)"test"]; // ref-error {{variable length array}} \234 // pedantic-ref-error {{variable length array}} \235 // expected-error {{variable length array}} \236 // pedantic-expected-error {{variable length array}}237 238const void (*const funcp)(void) = (void*)123; // pedantic-warning {{converts between void pointer and function pointer}} \239 // pedantic-expected-note {{this conversion is not allowed in a constant expression}}240_Static_assert(funcp == (void*)0, ""); // all-error {{failed due to requirement 'funcp == (void *)0'}} \241 // pedantic-warning {{expression is not an integer constant expression}}242_Static_assert(funcp == (void*)123, ""); // pedantic-warning {{equality comparison between function pointer and void pointer}} \243 // pedantic-warning {{expression is not an integer constant expression}}244 245void unaryops(void) {246 (void)(++(struct x {unsigned x;}){3}.x);247 (void)(--(struct y {unsigned x;}){3}.x);248 (void)(++(struct z {float x;}){3}.x);249 (void)(--(struct w {float x;}){3}.x);250 251 (void)((struct xx {unsigned x;}){3}.x++);252 (void)((struct yy {unsigned x;}){3}.x--);253 (void)((struct zz {float x;}){3}.x++);254 (void)((struct ww {float x;}){3}.x--);255}256 257/// This used to fail because we didn't properly mark the struct258/// initialized through a CompoundLiteralExpr as initialized.259struct TestStruct {260 int a;261 int b;262};263int Y __attribute__((annotate(264 "GlobalValAnnotationWithArgs",265 42,266 (struct TestStruct) { .a = 1, .b = 2 }267)));268 269#ifdef __SIZEOF_INT128__270const int *p = &b;271const __int128 K = (__int128)(int*)0;272const unsigned __int128 KU = (unsigned __int128)(int*)0;273#endif274 275 276int test3(void) {277 int a[2];278 a[0] = test3; // all-error {{incompatible pointer to integer conversion assigning to 'int' from 'int (void)'}}279 return 0;280}281/// This tests that we have full type info, even for values we cannot read.282int dummyarray[5];283_Static_assert(&dummyarray[0] < &dummyarray[1], ""); // pedantic-warning {{GNU extension}}284 285void addrlabelexpr(void) {286 a0: ;287 static void *ps[] = { &&a0 }; // pedantic-warning {{use of GNU address-of-label extension}}288}289 290extern void cv2;291void *foo5 (void)292{293 return &cv2; // pedantic-warning{{address of an expression of type 'void'}}294}295 296__attribute__((weak)) const unsigned int test10_bound = 10;297char test10_global[test10_bound]; // all-error {{variable length array declaration not allowed at file scope}}298void test10(void) {299 char test10_local[test10_bound] = "help"; // all-error {{variable-sized object may not be initialized}}300}301 302void SuperSpecialFunc(void) {303const int SuperSpecialCase = 10;304_Static_assert((sizeof(SuperSpecialCase) == 12 && SuperSpecialCase == 3) || SuperSpecialCase == 10, ""); // pedantic-warning {{GNU extension}}305}306 307 308void T1(void) {309 static int *y[1] = {({ static int _x = 20; (void*)0;})}; // all-error {{initializer element is not a compile-time constant}} \310 // pedantic-warning {{use of GNU statement expression extension}}311}312 313enum teste1 test1f(void), (*test1)(void) = test1f; // pedantic-warning {{ISO C forbids forward references to 'enum' types}}314enum teste1 { TEST1 };315 316void func(void) {317 _Static_assert(func + 1 - func == 1, ""); // pedantic-warning {{arithmetic on a pointer to the function type}} \318 // pedantic-warning {{arithmetic on pointers to the function type}} \319 // pedantic-warning {{not an integer constant expression}}320 _Static_assert(func + 0xdead000000000000UL - 0xdead000000000000UL == func, ""); // pedantic-warning 2{{arithmetic on a pointer to the function type}} \321 // pedantic-warning {{not an integer constant expression}} \322 // pedantic-note {{cannot refer to element 16045481047390945280 of non-array object in a constant expression}}323 _Static_assert(func + 1 != func, ""); // pedantic-warning {{arithmetic on a pointer to the function type}} \324 // pedantic-warning {{expression is not an integer constant expression}}325 func + 0xdead000000000000UL; // all-warning {{expression result unused}} \326 // pedantic-warning {{arithmetic on a pointer to the function type}}327 func - 0xdead000000000000UL; // all-warning {{expression result unused}} \328 // pedantic-warning {{arithmetic on a pointer to the function type}}329}330 331void foo3 (void)332{333 void* x = 0;334 void* y = &*x;335}336 337static void *FooTable[1] = {338 [0] = (void *[1]) { // 1339 [0] = (void *[1]) { // 2340 [0] = (void *[1]) {} // pedantic-warning {{use of an empty initializer}}341 },342 }343};344 345int strcmp(const char *, const char *); // all-note {{passing argument to parameter here}}346#define S "\x01\x02\x03\x04\x05\x06\x07\x08"347const char _str[] = {S[0], S[1], S[2], S[3], S[4], S[5], S[6], S[7]};348const unsigned char _str2[] = {S[0], S[1], S[2], S[3], S[4], S[5], S[6], S[7]};349const int compared = strcmp(_str, (const char *)_str2); // all-error {{initializer element is not a compile-time constant}}350 351 352const int compared2 = strcmp(strcmp, _str); // all-error {{incompatible pointer types}} \353 // all-error {{initializer element is not a compile-time constant}}354 355int foo(x) // all-warning {{a function definition without a prototype is deprecated in all versions of C}}356int x;357{358 return x;359}360 361void bar() { // pedantic-warning {{a function declaration without a prototype}}362 int x;363 x = foo(); // all-warning {{too few arguments}}364}365 366int *_b = &a;367void discardedCmp(void)368{369 (*_b) = ((&a == &a) , a); // all-warning {{left operand of comma operator has no effect}}370}371 372/// ArraySubscriptExpr that's not an lvalue373typedef unsigned char U __attribute__((vector_size(1)));374void nonLValueASE(U f) { f[0] = f[((U)(U){0})[0]]; }375 376static char foo_(a) // all-warning {{definition without a prototype}}377 char a;378{379 return 'a';380}381static void bar_(void) {382 foo_(foo_(1));383}384 385void foo2(void*);386void bar2(void) {387 int a[2][3][4][5]; // all-note {{array 'a' declared here}}388 foo2(&a[0][4]); // all-warning {{array index 4 is past the end of the array}}389}390 391void plainComplex(void) {392 _Complex cd; // all-warning {{_Complex double}}393 cd = *(_Complex *)&(struct { double r, i; }){0.0, 0.0}; // all-warning {{_Complex double}}394}395 396/// This test results in an ImplicitValueInitExpr with DiscardResult set.397struct M{398 char c;399};400typedef struct S64 {401 struct M m;402 char a[64];403} I64;404 405_Static_assert((((I64){}, 1)), ""); // all-warning {{left operand of comma operator has no effect}} \406 // pedantic-warning {{use of an empty initializer is a C23 extension}} \407 // pedantic-warning {{expression is not an integer constant expression; folding it to a constant is a GNU extension}}408