157 lines · c
1// RUN: %clang_cc1 -fsyntax-only -verify -triple x86_64-linux %s -Wno-tautological-pointer-compare -Wno-pointer-to-int-cast2// RUN: %clang_cc1 -fsyntax-only -verify -triple powerpc64-ibm-aix-xcoff %s -Wno-tautological-pointer-compare -Wno-pointer-to-int-cast3 4#define EVAL_EXPR(testno, expr) enum { test##testno = (expr) }; struct check_positive##testno { int a[test##testno]; };5int x;6EVAL_EXPR(1, (_Bool)&x)7EVAL_EXPR(2, (int)(1.0+(double)4))8EVAL_EXPR(3, (int)(1.0+(float)4.0))9EVAL_EXPR(4, (_Bool)(1 ? (void*)&x : 0))10EVAL_EXPR(5, (_Bool)(int[]){0})11struct y {int x,y;};12EVAL_EXPR(6, (int)(1+(struct y*)0))13EVAL_EXPR(7, (int)&((struct y*)0)->y)14EVAL_EXPR(8, (_Bool)"asdf")15EVAL_EXPR(9, !!&x)16EVAL_EXPR(10, ((void)1, 12))17void g0(void);18EVAL_EXPR(11, (g0(), 12)) // expected-error {{not an integer constant expression}}19EVAL_EXPR(12, 1.0&&2.0)20EVAL_EXPR(13, x || 3.0) // expected-error {{not an integer constant expression}}21 22unsigned int l_19 = 1;23EVAL_EXPR(14, (1 ^ l_19) && 1); // expected-error {{not an integer constant expression}}24 25void f(void)26{27 int a;28 EVAL_EXPR(15, (_Bool)&a);29}30 31// FIXME: Turn into EVAL_EXPR test once we have more folding.32_Complex float g16 = (1.0f + 1.0fi);33 34// ?: in constant expressions.35int g17[(3?:1) - 2];36 37EVAL_EXPR(18, ((int)((void*)10 + 10)) == 20 ? 1 : -1);38 39struct s {40 int a[(int)-1.0f]; // expected-error {{array size is negative}}41};42 43EVAL_EXPR(19, ((int)&*(char*)10 == 10 ? 1 : -1));44 45EVAL_EXPR(20, __builtin_constant_p(*((int*) 10)));46 47EVAL_EXPR(21, (__imag__ 2i) == 2 ? 1 : -1);48 49EVAL_EXPR(22, (__real__ (2i+3)) == 3 ? 1 : -1);50 51int g23[(int)(1.0 / 1.0)] = { 1 }; // expected-warning {{folded to constant array}}52int g24[(int)(1.0 / 1.0)] = { 1 , 2 }; // expected-warning {{folded to constant array}} expected-warning {{excess elements in array initializer}}53int g25[(int)(1.0 + 1.0)], g26 = sizeof(g25); // expected-warning {{folded to constant array}}54 55EVAL_EXPR(26, (_Complex double)0 ? -1 : 1)56EVAL_EXPR(27, (_Complex int)0 ? -1 : 1)57EVAL_EXPR(28, (_Complex double)1 ? 1 : -1)58EVAL_EXPR(29, (_Complex int)1 ? 1 : -1)59 60 61// PR402762struct a { int x, y; };63static struct a V2 = (struct a)(struct a){ 1, 2};64static const struct a V1 = (struct a){ 1, 2};65 66EVAL_EXPR(30, (int)(_Complex float)((1<<30)-1) == (1<<30) ? 1 : -1)67EVAL_EXPR(31, (int*)0 == (int*)0 ? 1 : -1)68EVAL_EXPR(32, (int*)0 != (int*)0 ? -1 : 1)69EVAL_EXPR(33, (void*)0 - (void*)0 == 0 ? 1 : -1)70void foo(void) {}71EVAL_EXPR(34, (foo == (void *)0) ? -1 : 1)72 73// No PR. Mismatched bitwidths lead to a crash on second evaluation.74const _Bool constbool = 0;75EVAL_EXPR(35, constbool)76EVAL_EXPR(36, constbool)77 78EVAL_EXPR(37, ((void)1,2.0) == 2.0 ? 1 : -1)79EVAL_EXPR(38, __builtin_expect(1,1) == 1 ? 1 : -1)80 81// PR788482EVAL_EXPR(39, __real__(1.f) == 1 ? 1 : -1)83EVAL_EXPR(40, __imag__(1.f) == 0 ? 1 : -1)84 85// From gcc testsuite86EVAL_EXPR(41, (int)(1+(_Complex unsigned)2))87 88void rdar8875946(void) {89 double _Complex P;90 float _Complex P2 = 3.3f + P;91}92 93double d = (d = 0.0); // expected-error {{not a compile-time constant}}94double d2 = ++d; // expected-error {{not a compile-time constant}}95 96int n = 2;97int intLvalue[*(int*)((long)&n ?: 1)] = { 1, 2 }; // expected-error {{variable length array}}98 99union u { int a; char b[4]; };100char c = ((union u)(123456)).b[0]; // expected-error {{not a compile-time constant}}101 102extern const int weak_int __attribute__((weak));103const int weak_int = 42;104int weak_int_test = weak_int; // expected-error {{not a compile-time constant}}105 106int literalVsNull1 = "foo" == 0;107int literalVsNull2 = 0 == "foo";108 109// PR11385.110int castViaInt[*(int*)(unsigned long)"test"]; // expected-error {{variable length array}}111 112// PR11391.113struct PR11391 { _Complex float f; } pr11391;114EVAL_EXPR(42, __builtin_constant_p(pr11391.f = 1))115 116// PR12043117float varfloat;118const float constfloat = 0;119EVAL_EXPR(43, varfloat && constfloat) // expected-error {{not an integer constant expression}}120 121EVAL_EXPR(45, ((char*)-1) + 1 == 0 ? 1 : -1)122EVAL_EXPR(46, ((char*)-1) + 1 < (char*) -1 ? 1 : -1)123EVAL_EXPR(47, &x < &x + 1 ? 1 : -1)124EVAL_EXPR(48, &x != &x - 1 ? 1 : -1)125EVAL_EXPR(49, &x < &x - 100 ? 1 : -1) // expected-error {{not an integer constant expression}}126 127extern struct Test50S Test50;128EVAL_EXPR(50, &Test50 < (struct Test50S*)((unsigned long)&Test50 + 10)) // expected-error {{not an integer constant expression}}129 130EVAL_EXPR(51, 0 != (float)1e99)131 132// PR21945133void PR21945(void) { int i = (({}), 0l); }134 135void PR24622(void);136struct PR24622 {} pr24622;137EVAL_EXPR(52, &pr24622 == (void *)&PR24622);138 139// We evaluate these by providing 2s' complement semantics in constant140// expressions, like we do for integers.141void *PR28739a = (__int128)(unsigned long)-1 + &PR28739a; // expected-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)}}142void *PR28739b = &PR28739b + (__int128)(unsigned long)-1; // expected-warning {{refers past the last possible element}}143__int128 PR28739c = (&PR28739c + (__int128)(unsigned long)-1) - &PR28739c; // expected-warning {{refers past the last possible element}}144void *PR28739d = &(&PR28739d)[(__int128)(unsigned long)-1]; // expected-warning {{refers past the last possible element}}145 146struct PR35214_X {147 int k;148 int arr[];149};150int PR35214_x;151int PR35214_y = ((struct PR35214_X *)&PR35214_x)->arr[1]; // expected-error {{not a compile-time constant}}152int *PR35214_z = &((struct PR35214_X *)&PR35214_x)->arr[1]; // ok, &PR35214_x + 2153 154 155int * GH149500_p = &(*(int *)0x400);156static const void *GH149500_q = &(*(const struct sysrq_key_op *)0);157