98 lines · c
1// RUN: %clang_cc1 %s -ffreestanding -Wno-int-to-pointer-cast -fsyntax-only -verify -pedantic -fpascal-strings -std=c992 3#include <stdint.h>4#include <limits.h>5 6int a(void) {int p; *(1 ? &p : (void*)(0 && (a(),1))) = 10;} /* expected-error {{incomplete type 'void' is not assignable}}7 expected-warning {{ISO C does not allow indirection on operand of type 'void *'}} */8 9// ?: with __builtin_constant_p as the operand is an i-c-e.10int expr;11char w[__builtin_constant_p(expr) ? expr : 1];12 13char v[sizeof(__builtin_constant_p(0)) == sizeof(int) ? 1 : -1];14 15int implicitConversion = 1.0;16char floatArith[(int)(1.0+2.0)]; // expected-warning {{variable length array folded to constant array as an extension}}17 18// __builtin_constant_p as the condition of ?: allows arbitrary foldable19// constants to be transmogrified into i-c-e's.20char b[__builtin_constant_p((int)(1.0+2.0)) ? (int)(1.0+2.0) : -1];21 22struct c {23 int a : (24 __builtin_constant_p((int)(1.0+2.0)) ? (int)(1.0+25 expr // expected-error {{expression is not an integer constant expression}}26 ) : -1);27};28 29// Check that we can evaluate statement-expressions properly when30// constant-folding inside an ICE.31void PR49239(void) {32 goto check_not_vla;33 char not_vla[__builtin_constant_p(1) ? ({ 42; }) : -1]; // expected-warning {{statement expression}}34check_not_vla:;35 _Static_assert(sizeof(not_vla) == 42, ""); // expected-warning {{C11 extension}}36 37 // It's not clear that this should be valid: __builtin_expect(expr1, expr2)38 // should probably be an ICE if and only if expr1 is an ICE, but we roughly39 // follow GCC in treating it as an ICE if and only if we can evaluate expr140 // regardless of whether it's an ICE.41 goto check_also_not_vla;42 char also_not_vla[__builtin_expect(({ 76; }), 0)]; // expected-warning {{statement expression}}43check_also_not_vla:;44 _Static_assert(sizeof(also_not_vla) == 76, ""); // expected-warning {{C11 extension}}45}46 47 48void test1(int n, int* p) { *(n ? p : (void *)(7-7)) = 1; }49void test2(int n, int* p) { *(n ? p : (void *)0) = 1; }50 51 52 53char array[1024/(sizeof (long))];54 55int x['\xBb' == (char) 187 ? 1: -1];56 57// PR199258void func(int x)59{60 switch (x) {61 case sizeof("abc"): break;62 case sizeof("loooong"): func(4);63 case sizeof("\ploooong"): func(4);64 }65}66 67int expr;68char y[__builtin_constant_p(expr) ? -1 : 1];69char z[__builtin_constant_p(4) ? 1 : -1];70 71// Comma tests72int comma1[0?1,2:3]; // expected-warning {{left operand of comma operator has no effect}}73int comma2[1 || (1, 2)]; // expected-warning {{use of logical '||' with constant operand}} \74 // expected-note {{use '|' for a bitwise operation}} \75 // expected-warning {{left operand of comma operator has no effect}}76int comma3[(1, 2)]; // expected-warning {{variable length array folded to constant array as an extension}} \77 // expected-warning {{left operand of comma operator has no effect}}78 79// Pointer + __builtin_constant_p80char pbcp[__builtin_constant_p(4) ? (intptr_t)&expr : 0]; // expected-error {{variable length array declaration not allowed at file scope}}81 82int illegaldiv1a[1 || 1/0];83int illegaldiv1b[1 && 1/0]; //expected-error{{variable length array declaration not allowed at file scope}}84 85int illegaldiv2[1/0]; // expected-error {{variable length array declaration not allowed at file scope}}86int illegaldiv3[INT_MIN / -1]; // expected-error {{variable length array declaration not allowed at file scope}}87// PR926288int illegaldiv4[0 / (1 / 0)]; // expected-error {{variable length array declaration not allowed at file scope}}89 90int chooseexpr[__builtin_choose_expr(1, 1, expr)];91int realop[(__real__ 4) == 4 ? 1 : -1];92int imagop[(__imag__ 4) == 0 ? 1 : -1];93 94int *PR14729 = 0 ?: 1/0; // expected-error {{not a compile-time constant}} expected-warning 2{{}} expected-error {{incompatible integer to pointer conversion initializing 'int *' with an expression of type 'int'}}95 96int bcp_call_v;97int bcp_call_a[] = {__builtin_constant_p(bcp_call_v && 0) ? bcp_call_v && 0 : -1};98