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