blob: 4d02ca8f174e9ef7a518e655f64e56eb839b19cf [file] [log] [blame]
Daniel Dunbara5728872009-12-15 20:14:24 +00001// RUN: %clang_cc1 -fsyntax-only -verify %s
Sebastian Redl4a4251b2009-02-07 13:06:23 +00002
3// C++-specific tests for integral constant expressions.
4
5const int c = 10;
6int ar[c];
Douglas Gregor59600d82009-09-10 17:44:23 +00007
8struct X0 {
9 static const int value = static_cast<int>(4.0);
10};
Douglas Gregorf2991242009-09-10 23:31:45 +000011
12void f() {
13 if (const int value = 17) {
14 int array[value];
15 }
16}
Eli Friedmanc0131182009-12-03 20:31:57 +000017
18int a() {
Eli Friedman60b4d042010-09-06 00:30:50 +000019 const int t=t;
John McCall0fb97082010-05-18 03:19:21 +000020 switch(1) { // expected-warning {{no case matching constant switch condition '1'}}
Eli Friedmanc0131182009-12-03 20:31:57 +000021 case t:; // expected-error {{not an integer constant expression}}
22 }
23}
John McCall1f1b3b32010-02-06 01:07:37 +000024
25// PR6206: out-of-line definitions are legit
26namespace pr6206 {
27 class Foo {
28 public:
29 static const int kBar;
30 };
31
32 const int Foo::kBar = 20;
33
34 char Test() {
35 char str[Foo::kBar];
36 str[0] = '0';
37 return str[0];
38 }
39}
John McCallf604a562010-02-24 09:03:18 +000040
41// PR6373: default arguments don't count.
42void pr6373(const unsigned x = 0) {
43 unsigned max = 80 / x;
44}
Chris Lattner24c38e12011-06-14 05:46:29 +000045
46
47// rdar://9204520
48namespace rdar9204520 {
49
50struct A {
51 static const int B = int(0.75 * 1000 * 1000);
52};
53
54int foo() { return A::B; }
55}
56
Eli Friedmaneea0e812011-09-29 21:49:34 +000057// PR11040
58const int x = 10;
59int* y = reinterpret_cast<const char&>(x); // expected-error {{cannot initialize}}
Eli Friedmandb924222011-10-11 00:13:24 +000060
61// This isn't an integral constant expression, but make sure it folds anyway.
62struct PR8836 { char _; long long a; };
63int PR8836test[(__typeof(sizeof(int)))&reinterpret_cast<const volatile char&>((((PR8836*)0)->a))];