xref: /aosp_15_r20/external/clang/test/SemaCXX/i-c-e-cxx.cpp (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li // RUN: %clang_cc1 -fsyntax-only -verify -pedantic %s
2*67e74705SXin Li 
3*67e74705SXin Li // C++-specific tests for integral constant expressions.
4*67e74705SXin Li 
5*67e74705SXin Li const int c = 10;
6*67e74705SXin Li int ar[c];
7*67e74705SXin Li 
8*67e74705SXin Li struct X0 {
9*67e74705SXin Li   static const int value = static_cast<int>(4.0);
10*67e74705SXin Li };
11*67e74705SXin Li 
f()12*67e74705SXin Li void f() {
13*67e74705SXin Li   if (const int value = 17) {
14*67e74705SXin Li     int array[value];
15*67e74705SXin Li   }
16*67e74705SXin Li }
17*67e74705SXin Li 
a()18*67e74705SXin Li int a() {
19*67e74705SXin Li   const int t=t; // expected-note {{declared here}} expected-note {{read of object outside its lifetime}}
20*67e74705SXin Li   switch(1) { // expected-warning {{no case matching constant switch condition '1'}}
21*67e74705SXin Li     case t:; // expected-error {{not an integral constant expression}} expected-note {{initializer of 't' is not a constant expression}}
22*67e74705SXin Li   }
23*67e74705SXin Li }
24*67e74705SXin Li 
25*67e74705SXin Li // PR6206:  out-of-line definitions are legit
26*67e74705SXin Li namespace pr6206 {
27*67e74705SXin Li   class Foo {
28*67e74705SXin Li   public:
29*67e74705SXin Li     static const int kBar;
30*67e74705SXin Li   };
31*67e74705SXin Li 
32*67e74705SXin Li   const int Foo::kBar = 20;
33*67e74705SXin Li 
Test()34*67e74705SXin Li   char Test() {
35*67e74705SXin Li     char str[Foo::kBar];
36*67e74705SXin Li     str[0] = '0';
37*67e74705SXin Li     return str[0];
38*67e74705SXin Li   }
39*67e74705SXin Li }
40*67e74705SXin Li 
41*67e74705SXin Li // PR6373:  default arguments don't count.
pr6373(const unsigned x=0)42*67e74705SXin Li void pr6373(const unsigned x = 0) {
43*67e74705SXin Li   unsigned max = 80 / x;
44*67e74705SXin Li }
45*67e74705SXin Li 
46*67e74705SXin Li 
47*67e74705SXin Li // rdar://9204520
48*67e74705SXin Li namespace rdar9204520 {
49*67e74705SXin Li 
50*67e74705SXin Li struct A {
51*67e74705SXin Li   static const int B = int(0.75 * 1000 * 1000); // expected-warning {{not a constant expression; folding it to a constant is a GNU extension}}
52*67e74705SXin Li };
53*67e74705SXin Li 
foo()54*67e74705SXin Li int foo() { return A::B; }
55*67e74705SXin Li }
56*67e74705SXin Li 
57*67e74705SXin Li // PR11040
58*67e74705SXin Li const int x = 10;
59*67e74705SXin Li int* y = reinterpret_cast<const char&>(x); // expected-error {{cannot initialize}}
60*67e74705SXin Li 
61*67e74705SXin Li // This isn't an integral constant expression, but make sure it folds anyway.
62*67e74705SXin Li struct PR8836 { char _; long long a; }; // expected-warning {{long long}}
63*67e74705SXin Li int PR8836test[(__typeof(sizeof(int)))&reinterpret_cast<const volatile char&>((((PR8836*)0)->a))]; // expected-warning {{folded to constant array as an extension}} expected-note {{cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression}}
64*67e74705SXin Li 
65*67e74705SXin Li const int nonconst = 1.0; // expected-note {{declared here}}
66*67e74705SXin Li int arr[nonconst]; // expected-warning {{folded to constant array as an extension}} expected-note {{initializer of 'nonconst' is not a constant expression}}
67*67e74705SXin Li const int castfloat = static_cast<int>(1.0);
68*67e74705SXin Li int arr2[castfloat]; // ok
69