2011-10-25 02:26:35 +08:00
|
|
|
// RUN: %clang_cc1 -fsyntax-only -verify -pedantic %s
|
2017-02-25 06:22:05 +08:00
|
|
|
// RUN: %clang_cc1 -fsyntax-only -verify -pedantic -std=gnu++98 %s
|
|
|
|
// RUN: %clang_cc1 -fsyntax-only -verify -pedantic -std=gnu++11 %s
|
2009-02-07 21:06:23 +08:00
|
|
|
|
|
|
|
// C++-specific tests for integral constant expressions.
|
|
|
|
|
|
|
|
const int c = 10;
|
|
|
|
int ar[c];
|
2009-09-11 01:44:23 +08:00
|
|
|
|
|
|
|
struct X0 {
|
|
|
|
static const int value = static_cast<int>(4.0);
|
|
|
|
};
|
2009-09-11 07:31:45 +08:00
|
|
|
|
|
|
|
void f() {
|
|
|
|
if (const int value = 17) {
|
|
|
|
int array[value];
|
|
|
|
}
|
|
|
|
}
|
2009-12-04 04:31:57 +08:00
|
|
|
|
|
|
|
int a() {
|
2017-02-25 06:22:05 +08:00
|
|
|
const int t=t; // expected-note {{declared here}}
|
|
|
|
#if __cplusplus <= 199711L
|
|
|
|
// expected-note@-2 {{read of object outside its lifetime}}
|
|
|
|
#endif
|
|
|
|
|
2018-07-27 02:41:30 +08:00
|
|
|
switch(1) { // do not warn that 1 is not a case value;
|
|
|
|
// 't' might have been expected to evalaute to 1
|
2017-02-25 06:22:05 +08:00
|
|
|
case t:; // expected-note {{initializer of 't' is not a constant expression}}
|
|
|
|
#if __cplusplus <= 199711L
|
|
|
|
// expected-error@-2 {{not an integral constant expression}}
|
|
|
|
#else
|
|
|
|
// expected-error@-4 {{case value is not a constant expression}}
|
|
|
|
#endif
|
2009-12-04 04:31:57 +08:00
|
|
|
}
|
|
|
|
}
|
2010-02-06 09:07:37 +08:00
|
|
|
|
|
|
|
// PR6206: out-of-line definitions are legit
|
|
|
|
namespace pr6206 {
|
|
|
|
class Foo {
|
|
|
|
public:
|
|
|
|
static const int kBar;
|
|
|
|
};
|
|
|
|
|
|
|
|
const int Foo::kBar = 20;
|
|
|
|
|
|
|
|
char Test() {
|
|
|
|
char str[Foo::kBar];
|
|
|
|
str[0] = '0';
|
|
|
|
return str[0];
|
|
|
|
}
|
|
|
|
}
|
2010-02-24 17:03:18 +08:00
|
|
|
|
|
|
|
// PR6373: default arguments don't count.
|
|
|
|
void pr6373(const unsigned x = 0) {
|
|
|
|
unsigned max = 80 / x;
|
|
|
|
}
|
2011-06-14 13:46:29 +08:00
|
|
|
|
|
|
|
|
|
|
|
// rdar://9204520
|
|
|
|
namespace rdar9204520 {
|
|
|
|
|
|
|
|
struct A {
|
2017-02-25 06:22:05 +08:00
|
|
|
static const int B = int(0.75 * 1000 * 1000);
|
|
|
|
#if __cplusplus <= 199711L
|
|
|
|
// expected-warning@-2 {{not a constant expression; folding it to a constant is a GNU extension}}
|
|
|
|
#endif
|
2011-06-14 13:46:29 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
int foo() { return A::B; }
|
|
|
|
}
|
|
|
|
|
2011-09-30 05:49:34 +08:00
|
|
|
// PR11040
|
|
|
|
const int x = 10;
|
|
|
|
int* y = reinterpret_cast<const char&>(x); // expected-error {{cannot initialize}}
|
2011-10-11 08:13:24 +08:00
|
|
|
|
|
|
|
// This isn't an integral constant expression, but make sure it folds anyway.
|
2017-02-25 06:22:05 +08:00
|
|
|
struct PR8836 { char _; long long a; };
|
|
|
|
#if __cplusplus <= 199711L
|
|
|
|
// expected-warning@-2 {{'long long' is a C++11 extension}}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int PR8836test[(__typeof(sizeof(int)))&reinterpret_cast<const volatile char&>((((PR8836*)0)->a))];
|
|
|
|
// expected-warning@-1 {{folded to constant array as an extension}}
|
|
|
|
// expected-note@-2 {{cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression}}
|
|
|
|
|
|
|
|
const int nonconst = 1.0;
|
|
|
|
#if __cplusplus <= 199711L
|
|
|
|
// expected-note@-2 {{declared here}}
|
|
|
|
#endif
|
|
|
|
int arr[nonconst];
|
|
|
|
#if __cplusplus <= 199711L
|
|
|
|
// expected-warning@-2 {{folded to constant array as an extension}}
|
|
|
|
// expected-note@-3 {{initializer of 'nonconst' is not a constant expression}}
|
|
|
|
#endif
|
2011-10-25 02:26:35 +08:00
|
|
|
|
|
|
|
const int castfloat = static_cast<int>(1.0);
|
|
|
|
int arr2[castfloat]; // ok
|