1 | // RUN: %clang_cc1 -fsyntax-only -verify -pedantic %s |
2 | // RUN: %clang_cc1 -fsyntax-only -verify -pedantic -std=gnu++98 %s |
3 | // RUN: %clang_cc1 -fsyntax-only -verify -pedantic -std=gnu++11 %s |
4 | |
5 | // C++-specific tests for integral constant expressions. |
6 | |
7 | const int c = 10; |
8 | int ar[c]; |
9 | |
10 | struct X0 { |
11 | static const int value = static_cast<int>(4.0); |
12 | }; |
13 | |
14 | void f() { |
15 | if (const int value = 17) { |
16 | int array[value]; |
17 | } |
18 | } |
19 | |
20 | int a() { |
21 | const int t=t; // expected-note {{declared here}} |
22 | #if __cplusplus <= 199711L |
23 | // expected-note@-2 {{read of object outside its lifetime}} |
24 | #endif |
25 | |
26 | switch(1) { // do not warn that 1 is not a case value; |
27 | // 't' might have been expected to evalaute to 1 |
28 | case t:; // expected-note {{initializer of 't' is not a constant expression}} |
29 | #if __cplusplus <= 199711L |
30 | // expected-error@-2 {{not an integral constant expression}} |
31 | #else |
32 | // expected-error@-4 {{case value is not a constant expression}} |
33 | #endif |
34 | } |
35 | } |
36 | |
37 | // PR6206: out-of-line definitions are legit |
38 | namespace pr6206 { |
39 | class Foo { |
40 | public: |
41 | static const int kBar; |
42 | }; |
43 | |
44 | const int Foo::kBar = 20; |
45 | |
46 | char Test() { |
47 | char str[Foo::kBar]; |
48 | str[0] = '0'; |
49 | return str[0]; |
50 | } |
51 | } |
52 | |
53 | // PR6373: default arguments don't count. |
54 | void pr6373(const unsigned x = 0) { |
55 | unsigned max = 80 / x; |
56 | } |
57 | |
58 | |
59 | // rdar://9204520 |
60 | namespace rdar9204520 { |
61 | |
62 | struct A { |
63 | static const int B = int(0.75 * 1000 * 1000); |
64 | #if __cplusplus <= 199711L |
65 | // expected-warning@-2 {{not a constant expression; folding it to a constant is a GNU extension}} |
66 | #endif |
67 | }; |
68 | |
69 | int foo() { return A::B; } |
70 | } |
71 | |
72 | // PR11040 |
73 | const int x = 10; |
74 | int* y = reinterpret_cast<const char&>(x); // expected-error {{cannot initialize}} |
75 | |
76 | // This isn't an integral constant expression, but make sure it folds anyway. |
77 | struct PR8836 { char _; long long a; }; |
78 | #if __cplusplus <= 199711L |
79 | // expected-warning@-2 {{'long long' is a C++11 extension}} |
80 | #endif |
81 | |
82 | int PR8836test[(__typeof(sizeof(int)))&reinterpret_cast<const volatile char&>((((PR8836*)0)->a))]; |
83 | // expected-warning@-1 {{folded to constant array as an extension}} |
84 | // expected-note@-2 {{cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression}} |
85 | |
86 | const int nonconst = 1.0; |
87 | #if __cplusplus <= 199711L |
88 | // expected-note@-2 {{declared here}} |
89 | #endif |
90 | int arr[nonconst]; |
91 | #if __cplusplus <= 199711L |
92 | // expected-warning@-2 {{folded to constant array as an extension}} |
93 | // expected-note@-3 {{initializer of 'nonconst' is not a constant expression}} |
94 | #endif |
95 | |
96 | const int castfloat = static_cast<int>(1.0); |
97 | int arr2[castfloat]; // ok |
98 | |