| 1 |
// RUN: %clang_cc1 -triple x86_64-linux -verify=both,expected -std=c++11 %s -fexperimental-new-constant-interpreter |
| 2 |
// RUN: %clang_cc1 -triple x86_64-linux -verify=both,ref -std=c++11 %s |
| 3 |
|
| 4 |
namespace IntOrEnum { |
| 5 |
const int k = 0; |
| 6 |
const int &p = k; |
| 7 |
template<int n> struct S {}; |
| 8 |
S<p> s; |
| 9 |
} |
| 10 |
|
| 11 |
const int cval = 2; |
| 12 |
template <int> struct C{}; |
| 13 |
template struct C<cval>; |
| 14 |
|
| 15 |
|
| 16 |
/// FIXME: This example does not get properly diagnosed in the new interpreter. |
| 17 |
extern const int recurse1; |
| 18 |
const int recurse2 = recurse1; // both-note {{here}} |
| 19 |
const int recurse1 = 1; |
| 20 |
int array1[recurse1]; |
| 21 |
int array2[recurse2]; // both-warning {{variable length arrays in C++}} \ |
| 22 |
// both-note {{initializer of 'recurse2' is not a constant expression}} \ |
| 23 |
// expected-error {{variable length array declaration not allowed at file scope}} \ |
| 24 |
// ref-warning {{variable length array folded to constant array as an extension}} |
| 25 |
|
| 26 |
constexpr int b = b; // both-error {{must be initialized by a constant expression}} \ |
| 27 |
// both-note {{read of object outside its lifetime is not allowed in a constant expression}} |
| 28 |
|
| 29 |
|
| 30 |
[[clang::require_constant_initialization]] int c = c; // both-error {{variable does not have a constant initializer}} \ |
| 31 |
// both-note {{attribute here}} \ |
| 32 |
// both-note {{read of non-const variable}} \ |
| 33 |
// both-note {{declared here}} |
| 34 |
|
| 35 |
|
| 36 |
void func(); |
| 37 |
constexpr int (&fp4)() = (int(&)())func; // both-error {{constant expression}} \ |
| 38 |
// both-note {{reinterpret_cast}} |
| 39 |
|
| 40 |
struct S { |
| 41 |
int m; |
| 42 |
}; |
| 43 |
constexpr S s = { 5 }; |
| 44 |
constexpr const int *p = &s.m + 1; |
| 45 |
|
| 46 |
constexpr const int *np2 = &(*(int(*)[4])nullptr)[0]; // both-error {{constexpr variable 'np2' must be initialized by a constant expression}} \ |
| 47 |
// both-note {{dereferencing a null pointer is not allowed in a constant expression}} |
| 48 |
|
| 49 |
constexpr int preDec(int x) { // both-error {{never produces a constant expression}} |
| 50 |
return --x; // both-note {{subexpression}} |
| 51 |
} |
| 52 |
|
| 53 |
constexpr int postDec(int x) { // both-error {{never produces a constant expression}} |
| 54 |
return x--; // both-note {{subexpression}} |
| 55 |
} |
| 56 |
|
| 57 |
constexpr int preInc(int x) { // both-error {{never produces a constant expression}} |
| 58 |
return ++x; // both-note {{subexpression}} |
| 59 |
} |
| 60 |
|
| 61 |
constexpr int postInc(int x) { // both-error {{never produces a constant expression}} |
| 62 |
return x++; // both-note {{subexpression}} |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
namespace ReferenceToConst { |
| 67 |
template<int n> struct S; // both-note 1{{here}} |
| 68 |
struct LiteralType { |
| 69 |
constexpr LiteralType(int n) : n(n) {} |
| 70 |
int n; |
| 71 |
}; |
| 72 |
template<int n> struct T { |
| 73 |
T() { |
| 74 |
static const int ki = 42; |
| 75 |
const int &i2 = ki; |
| 76 |
typename S<i2>::T check5; // both-error {{undefined template}} |
| 77 |
} |
| 78 |
}; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
namespace GH50055 { |
| 84 |
// Enums without fixed underlying type |
| 85 |
enum E1 {e11=-4, e12=4}; |
| 86 |
enum E2 {e21=0, e22=4}; |
| 87 |
enum E3 {e31=-4, e32=1024}; |
| 88 |
enum E4 {e41=0}; |
| 89 |
// Empty but as-if it had a single enumerator with value 0 |
| 90 |
enum EEmpty {}; |
| 91 |
|
| 92 |
// Enum with fixed underlying type because the underlying type is explicitly specified |
| 93 |
enum EFixed : int {efixed1=-4, efixed2=4}; |
| 94 |
// Enum with fixed underlying type because it is scoped |
| 95 |
enum class EScoped {escoped1=-4, escoped2=4}; |
| 96 |
|
| 97 |
enum EMaxInt {emaxint1=-1, emaxint2=__INT_MAX__}; |
| 98 |
|
| 99 |
enum NumberType {}; |
| 100 |
|
| 101 |
E2 testDefaultArgForParam(E2 e2Param = (E2)-1) { // ok, not a constant expression context |
| 102 |
E2 e2LocalInit = e2Param; // ok, not a constant expression context |
| 103 |
return e2LocalInit; |
| 104 |
} |
| 105 |
|
| 106 |
// #include <enum-constexpr-conversion-system-header.h> |
| 107 |
|
| 108 |
void testValueInRangeOfEnumerationValues() { |
| 109 |
constexpr E1 x1 = static_cast<E1>(-8); |
| 110 |
constexpr E1 x2 = static_cast<E1>(8); |
| 111 |
// both-error@-1 {{constexpr variable 'x2' must be initialized by a constant expression}} |
| 112 |
// both-note@-2 {{integer value 8 is outside the valid range of values [-8, 7] for the enumeration type 'E1'}} |
| 113 |
E1 x2b = static_cast<E1>(8); // ok, not a constant expression context |
| 114 |
|
| 115 |
constexpr E2 x3 = static_cast<E2>(-8); |
| 116 |
// both-error@-1 {{constexpr variable 'x3' must be initialized by a constant expression}} |
| 117 |
// both-note@-2 {{integer value -8 is outside the valid range of values [0, 7] for the enumeration type 'E2'}} |
| 118 |
constexpr E2 x4 = static_cast<E2>(0); |
| 119 |
constexpr E2 x5 = static_cast<E2>(8); |
| 120 |
// both-error@-1 {{constexpr variable 'x5' must be initialized by a constant expression}} |
| 121 |
// both-note@-2 {{integer value 8 is outside the valid range of values [0, 7] for the enumeration type 'E2'}} |
| 122 |
|
| 123 |
constexpr E3 x6 = static_cast<E3>(-2048); |
| 124 |
constexpr E3 x7 = static_cast<E3>(-8); |
| 125 |
constexpr E3 x8 = static_cast<E3>(0); |
| 126 |
constexpr E3 x9 = static_cast<E3>(8); |
| 127 |
constexpr E3 x10 = static_cast<E3>(2048); |
| 128 |
// both-error@-1 {{constexpr variable 'x10' must be initialized by a constant expression}} |
| 129 |
// both-note@-2 {{integer value 2048 is outside the valid range of values [-2048, 2047] for the enumeration type 'E3'}} |
| 130 |
|
| 131 |
constexpr E4 x11 = static_cast<E4>(0); |
| 132 |
constexpr E4 x12 = static_cast<E4>(1); |
| 133 |
constexpr E4 x13 = static_cast<E4>(2); |
| 134 |
// both-error@-1 {{constexpr variable 'x13' must be initialized by a constant expression}} |
| 135 |
// both-note@-2 {{integer value 2 is outside the valid range of values [0, 1] for the enumeration type 'E4'}} |
| 136 |
|
| 137 |
constexpr EEmpty x14 = static_cast<EEmpty>(0); |
| 138 |
constexpr EEmpty x15 = static_cast<EEmpty>(1); |
| 139 |
constexpr EEmpty x16 = static_cast<EEmpty>(2); |
| 140 |
// both-error@-1 {{constexpr variable 'x16' must be initialized by a constant expression}} |
| 141 |
// both-note@-2 {{integer value 2 is outside the valid range of values [0, 1] for the enumeration type 'EEmpty'}} |
| 142 |
|
| 143 |
constexpr EFixed x17 = static_cast<EFixed>(100); |
| 144 |
constexpr EScoped x18 = static_cast<EScoped>(100); |
| 145 |
|
| 146 |
constexpr EMaxInt x19 = static_cast<EMaxInt>(__INT_MAX__-1); |
| 147 |
constexpr EMaxInt x20 = static_cast<EMaxInt>((long)__INT_MAX__+1); |
| 148 |
// both-error@-1 {{constexpr variable 'x20' must be initialized by a constant expression}} |
| 149 |
// both-note@-2 {{integer value 2147483648 is outside the valid range of values [-2147483648, 2147483647] for the enumeration type 'EMaxInt'}} |
| 150 |
|
| 151 |
const NumberType neg_one = (NumberType) ((NumberType) 0 - (NumberType) 1); // ok, not a constant expression context |
| 152 |
} |
| 153 |
struct EnumTest { |
| 154 |
enum type { |
| 155 |
Type1, |
| 156 |
BOUND |
| 157 |
}; |
| 158 |
static const type binding_completed = type(BOUND + 1); // both-error {{in-class initializer for static data member is not a constant expression}} \ |
| 159 |
// both-note {{integer value 2 is outside the valid range of values}} |
| 160 |
}; |
| 161 |
|
| 162 |
template<class T, unsigned size> struct Bitfield { |
| 163 |
static constexpr T max = static_cast<T>((1 << size) - 1); |
| 164 |
// both-error@-1 {{constexpr variable 'max' must be initialized by a constant expression}} |
| 165 |
// both-note@-2 {{integer value 15 is outside the valid range of values [0, 7] for the enumeration type 'E2'}} |
| 166 |
}; |
| 167 |
|
| 168 |
void testValueInRangeOfEnumerationValuesViaTemplate() { |
| 169 |
Bitfield<E2, 3> good; |
| 170 |
Bitfield<E2, 4> bad; // both-note {{in instantiation}} |
| 171 |
} |
| 172 |
|
| 173 |
enum SortOrder { |
| 174 |
AscendingOrder, |
| 175 |
DescendingOrder |
| 176 |
}; |
| 177 |
|
| 178 |
class A { |
| 179 |
static void f(SortOrder order); |
| 180 |
}; |
| 181 |
|
| 182 |
void A::f(SortOrder order) { |
| 183 |
if (order == SortOrder(-1)) // ok, not a constant expression context |
| 184 |
return; |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
namespace FinalLtorDiags { |
| 189 |
template<int*> struct A {}; // both-note {{template parameter is declared here}} |
| 190 |
int k; |
| 191 |
int *q = &k; // both-note {{declared here}} |
| 192 |
A<q> c; // both-error {{non-type template argument of type 'int *' is not a constant expression}} \ |
| 193 |
// both-note {{read of non-constexpr variable 'q' is not allowed in a constant expression}} |
| 194 |
} |
| 195 |
|
| 196 |
void lambdas() { |
| 197 |
int d; |
| 198 |
int a9[1] = {[d = 0] = 1}; // both-error {{not an integral constant expression}} |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
namespace InitLinkToRVO { |
| 203 |
struct A { |
| 204 |
int y = 3; |
| 205 |
int z = 1 + y; |
| 206 |
}; |
| 207 |
|
| 208 |
constexpr A make() { return A {}; } |
| 209 |
static_assert(make().z == 4, ""); |
| 210 |
} |
| 211 |
|
| 212 |
namespace DynamicCast { |
| 213 |
struct S { int x, y; } s; |
| 214 |
constexpr S* sptr = &s; |
| 215 |
struct Str { |
| 216 |
int b : reinterpret_cast<S*>(sptr) == reinterpret_cast<S*>(sptr); |
| 217 |
int g : (S*)(void*)(sptr) == sptr; |
| 218 |
}; |
| 219 |
} |
| 220 |
|
| 221 |
namespace GlobalInitializer { |
| 222 |
extern int &g; // both-note {{here}} |
| 223 |
struct S { |
| 224 |
int G : g; // both-error {{constant expression}} \ |
| 225 |
// both-note {{initializer of 'g' is unknown}} |
| 226 |
}; |
| 227 |
} |
| 228 |
|
| 229 |
namespace ExternPointer { |
| 230 |
struct S { int a; }; |
| 231 |
extern const S pu; |
| 232 |
constexpr const int *pua = &pu.a; // Ok. |
| 233 |
} |
| 234 |
|
| 235 |
namespace PseudoDtor { |
| 236 |
typedef int I; |
| 237 |
constexpr int f(int a = 1) { // both-error {{never produces a constant expression}} \ |
| 238 |
// ref-note {{destroying object 'a' whose lifetime has already ended}} |
| 239 |
return ( |
| 240 |
a.~I(), // both-note {{pseudo-destructor call is not permitted}} \ |
| 241 |
// expected-note {{pseudo-destructor call is not permitted}} |
| 242 |
0); |
| 243 |
} |
| 244 |
static_assert(f() == 0, ""); // both-error {{constant expression}} \ |
| 245 |
// expected-note {{in call to}} |
| 246 |
} |
| 247 |
|
| 248 |
namespace IntToPtrCast { |
| 249 |
typedef __INTPTR_TYPE__ intptr_t; |
| 250 |
|
| 251 |
constexpr intptr_t f(intptr_t x) { |
| 252 |
return (((x) >> 21) * 8); |
| 253 |
} |
| 254 |
|
| 255 |
extern "C" int foo; |
| 256 |
constexpr intptr_t i = f((intptr_t)&foo - 10); // both-error{{constexpr variable 'i' must be initialized by a constant expression}} \ |
| 257 |
// both-note{{reinterpret_cast}} |
| 258 |
} |
| 259 |
|
| 260 |
namespace Volatile { |
| 261 |
constexpr int f(volatile int &&r) { |
| 262 |
return r; // both-note {{read of volatile-qualified type 'volatile int'}} |
| 263 |
} |
| 264 |
struct S { |
| 265 |
int j : f(0); // both-error {{constant expression}} \ |
| 266 |
// both-note {{in call to 'f(0)'}} |
| 267 |
}; |
| 268 |
} |
| 269 |
|
| 270 |
namespace ZeroSizeCmp { |
| 271 |
extern void (*start[])(); |
| 272 |
extern void (*end[])(); |
| 273 |
static_assert(&start != &end, ""); // both-error {{constant expression}} \ |
| 274 |
// both-note {{comparison of pointers '&start' and '&end' to unrelated zero-sized objects}} |
| 275 |
} |
| 276 |
|
| 277 |
namespace OverlappingStrings { |
| 278 |
static_assert(+"foo" != +"bar", ""); |
| 279 |
static_assert(&"xfoo"[1] != &"yfoo"[1], ""); |
| 280 |
static_assert(+"foot" != +"foo", ""); |
| 281 |
static_assert(+"foo\0bar" != +"foo\0baz", ""); |
| 282 |
|
| 283 |
|
| 284 |
#define fold(x) (__builtin_constant_p(x) ? (x) : (x)) |
| 285 |
static_assert(fold((const char*)u"A" != (const char*)"\0A\0x"), ""); |
| 286 |
static_assert(fold((const char*)u"A" != (const char*)"A\0\0x"), ""); |
| 287 |
static_assert(fold((const char*)u"AAA" != (const char*)"AAA\0\0x"), ""); |
| 288 |
|
| 289 |
constexpr const char *string = "hello"; |
| 290 |
constexpr const char *also_string = string; |
| 291 |
static_assert(string == string, ""); |
| 292 |
static_assert(string == also_string, ""); |
| 293 |
|
| 294 |
|
| 295 |
// These strings may overlap, and so the result of the comparison is unknown. |
| 296 |
constexpr bool may_overlap_1 = +"foo" == +"foo"; // both-error {{}} both-note {{addresses of potentially overlapping literals}} |
| 297 |
constexpr bool may_overlap_2 = +"foo" == +"foo\0bar"; // both-error {{}} both-note {{addresses of potentially overlapping literals}} |
| 298 |
constexpr bool may_overlap_3 = +"foo" == &"bar\0foo"[4]; // both-error {{}} both-note {{addresses of potentially overlapping literals}} |
| 299 |
constexpr bool may_overlap_4 = &"xfoo"[1] == &"xfoo"[1]; // both-error {{}} both-note {{addresses of potentially overlapping literals}} |
| 300 |
|
| 301 |
|
| 302 |
/// Used to crash. |
| 303 |
const bool x = &"ab"[0] == &"ba"[3]; |
| 304 |
|
| 305 |
} |
| 306 |
|
| 307 |
namespace NonConstLocal { |
| 308 |
int a() { |
| 309 |
const int t=t; // both-note {{declared here}} |
| 310 |
|
| 311 |
switch(1) { |
| 312 |
case t:; // both-note {{initializer of 't' is not a constant expression}} \ |
| 313 |
// both-error {{case value is not a constant expression}} |
| 314 |
} |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
#define ATTR __attribute__((require_constant_initialization)) |
| 319 |
int somefunc() { |
| 320 |
const int non_global = 42; // both-note {{declared here}} |
| 321 |
ATTR static const int &local_init = non_global; // both-error {{variable does not have a constant initializer}} \ |
| 322 |
// both-note {{required by}} \ |
| 323 |
// both-note {{reference to 'non_global' is not a constant expression}} |
| 324 |
} |
| 325 |
|
| 326 |
namespace PR19010 { |
| 327 |
struct Empty {}; |
| 328 |
struct Empty2 : Empty {}; |
| 329 |
struct Test : Empty2 { |
| 330 |
constexpr Test() {} |
| 331 |
Empty2 array[2]; |
| 332 |
}; |
| 333 |
void test() { constexpr Test t; } |
| 334 |
} |
| 335 |
|
| 336 |
namespace ReadMutableInCopyCtor { |
| 337 |
struct G { |
| 338 |
struct X {}; |
| 339 |
union U { X a; }; |
| 340 |
mutable U u; // both-note {{declared here}} |
| 341 |
}; |
| 342 |
constexpr G g1 = {}; |
| 343 |
constexpr G g2 = g1; // both-error {{must be initialized by a constant expression}} \ |
| 344 |
// both-note {{read of mutable member 'u'}} \ |
| 345 |
// both-note {{in call to 'G(g1)'}} |
| 346 |
} |
| 347 |
|
| 348 |
namespace GH150709 { |
| 349 |
struct C { }; |
| 350 |
struct D : C { |
| 351 |
constexpr int f() const { return 1; }; |
| 352 |
}; |
| 353 |
struct E : C { }; |
| 354 |
struct F : D { }; |
| 355 |
struct G : E { }; |
| 356 |
|
| 357 |
constexpr C c1, c2[2]; |
| 358 |
constexpr D d1, d2[2]; |
| 359 |
constexpr E e1, e2[2]; |
| 360 |
constexpr F f; |
| 361 |
constexpr G g; |
| 362 |
|
| 363 |
constexpr auto mp = static_cast<int (C::*)() const>(&D::f); |
| 364 |
|
| 365 |
// sanity checks for fix of GH150709 (unchanged behavior) |
| 366 |
static_assert((c1.*mp)() == 1, ""); // both-error {{constant expression}} |
| 367 |
static_assert((d1.*mp)() == 1, ""); |
| 368 |
static_assert((f.*mp)() == 1, ""); |
| 369 |
static_assert((c2[0].*mp)() == 1, ""); // ref-error {{constant expression}} |
| 370 |
static_assert((d2[0].*mp)() == 1, ""); |
| 371 |
|
| 372 |
// incorrectly undiagnosed before fix of GH150709 |
| 373 |
static_assert((e1.*mp)() == 1, ""); // ref-error {{constant expression}} |
| 374 |
static_assert((e2[0].*mp)() == 1, ""); // ref-error {{constant expression}} |
| 375 |
static_assert((g.*mp)() == 1, ""); // ref-error {{constant expression}} |
| 376 |
} |
| 377 |
|
| 378 |
namespace DiscardedAddrLabel { |
| 379 |
void foo(void) { |
| 380 |
L: |
| 381 |
*&&L; // both-error {{indirection not permitted on operand of type 'void *'}} \ |
| 382 |
// both-warning {{expression result unused}} |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
struct Counter { |
| 387 |
int copies; |
| 388 |
constexpr Counter(int copies) : copies(copies) {} |
| 389 |
constexpr Counter(const Counter& other) : copies(other.copies + 1) {} |
| 390 |
}; |
| 391 |
// Passing an lvalue by value makes a non-elidable copy. |
| 392 |
constexpr int PassByValue(Counter c) { return c.copies; } |
| 393 |
static_assert(PassByValue(Counter(0)) == 0, "expect no copies"); |
| 394 |
|
| 395 |
namespace PointerCast { |
| 396 |
/// The two interpreters disagree here. |
| 397 |
struct S { int x, y; } s; |
| 398 |
constexpr S* sptr = &s; |
| 399 |
struct U {}; |
| 400 |
struct Str { |
| 401 |
int e : (Str*)(sptr) == (Str*)(sptr); // expected-error {{not an integral constant expression}} \ |
| 402 |
// expected-note {{cast that performs the conversions of a reinterpret_cast}} |
| 403 |
}; |
| 404 |
} |
| 405 |
|
| 406 |
namespace DummyToGlobalBlockMove { |
| 407 |
struct Baz { |
| 408 |
unsigned int n; |
| 409 |
}; |
| 410 |
|
| 411 |
struct AP { |
| 412 |
const AP *p; |
| 413 |
const Baz *lp; |
| 414 |
}; |
| 415 |
|
| 416 |
class Bar { |
| 417 |
public: |
| 418 |
static Baz _m[]; |
| 419 |
static const AP m; |
| 420 |
}; |
| 421 |
|
| 422 |
const AP Bar::m = {0, &Bar::_m[0]}; |
| 423 |
Baz Bar::_m[] = {{0}}; |
| 424 |
const AP m = {&Bar ::m}; |
| 425 |
} |