| 1 |
// RUN: %clang_cc1 -Wno-vla -fms-extensions -std=c++11 -fexperimental-new-constant-interpreter -verify=expected,both %s |
| 2 |
// RUN: %clang_cc1 -Wno-vla -fms-extensions -std=c++20 -fexperimental-new-constant-interpreter -verify=expected,both %s |
| 3 |
// RUN: %clang_cc1 -Wno-vla -fms-extensions -std=c++11 -verify=ref,both %s |
| 4 |
// RUN: %clang_cc1 -Wno-vla -fms-extensions -std=c++20 -verify=ref,both %s |
| 5 |
|
| 6 |
#define INT_MIN (~__INT_MAX__) |
| 7 |
#define INT_MAX __INT_MAX__ |
| 8 |
|
| 9 |
typedef __INTPTR_TYPE__ intptr_t; |
| 10 |
typedef __PTRDIFF_TYPE__ ptrdiff_t; |
| 11 |
|
| 12 |
|
| 13 |
static_assert(true, ""); |
| 14 |
static_assert(false, ""); // both-error{{failed}} |
| 15 |
static_assert(nullptr == nullptr, ""); |
| 16 |
static_assert(__null == __null, ""); |
| 17 |
static_assert(1 == 1, ""); |
| 18 |
static_assert(1 == 3, ""); // both-error{{failed}} |
| 19 |
|
| 20 |
constexpr void* v = nullptr; |
| 21 |
static_assert(__null == v, ""); |
| 22 |
|
| 23 |
constexpr int number = 10; |
| 24 |
static_assert(number == 10, ""); |
| 25 |
static_assert(number != 10, ""); // both-error{{failed}} \ |
| 26 |
// both-note{{evaluates to}} |
| 27 |
|
| 28 |
static_assert(__objc_yes, ""); |
| 29 |
static_assert(!__objc_no, ""); |
| 30 |
|
| 31 |
static_assert((long long)0x00000000FFFF0000 == 4294901760, ""); |
| 32 |
|
| 33 |
constexpr bool b = number; |
| 34 |
static_assert(b, ""); |
| 35 |
constexpr int one = true; |
| 36 |
static_assert(one == 1, ""); |
| 37 |
|
| 38 |
constexpr bool b2 = bool(); |
| 39 |
static_assert(!b2, ""); |
| 40 |
|
| 41 |
constexpr int Failed1 = 1 / 0; // both-error {{must be initialized by a constant expression}} \ |
| 42 |
// both-note {{division by zero}} \ |
| 43 |
// both-note {{declared here}} |
| 44 |
constexpr int Failed2 = Failed1 + 1; // both-error {{must be initialized by a constant expression}} \ |
| 45 |
// both-note {{declared here}} \ |
| 46 |
// both-note {{initializer of 'Failed1' is not a constant expression}} |
| 47 |
static_assert(Failed2 == 0, ""); // both-error {{not an integral constant expression}} \ |
| 48 |
// both-note {{initializer of 'Failed2' is not a constant expression}} |
| 49 |
|
| 50 |
const int x = *(volatile int*)0x1234; |
| 51 |
static_assert((void{}, true), ""); |
| 52 |
|
| 53 |
namespace ScalarTypes { |
| 54 |
constexpr int ScalarInitInt = int(); |
| 55 |
static_assert(ScalarInitInt == 0, ""); |
| 56 |
constexpr float ScalarInitFloat = float(); |
| 57 |
static_assert(ScalarInitFloat == 0.0f, ""); |
| 58 |
|
| 59 |
static_assert(decltype(nullptr)() == nullptr, ""); |
| 60 |
|
| 61 |
template<typename T> |
| 62 |
constexpr T getScalar() { return T(); } |
| 63 |
|
| 64 |
static_assert(getScalar<const int>() == 0, ""); |
| 65 |
static_assert(getScalar<const double>() == 0.0, ""); |
| 66 |
|
| 67 |
static_assert(getScalar<void*>() == nullptr, ""); |
| 68 |
static_assert(getScalar<void(*)(void)>() == nullptr, ""); |
| 69 |
|
| 70 |
enum E { |
| 71 |
First = 0, |
| 72 |
}; |
| 73 |
static_assert(getScalar<E>() == First, ""); |
| 74 |
|
| 75 |
struct S { |
| 76 |
int v; |
| 77 |
}; |
| 78 |
constexpr int S::* MemberPtr = &S::v; |
| 79 |
static_assert(getScalar<decltype(MemberPtr)>() == nullptr, ""); |
| 80 |
|
| 81 |
#if __cplusplus >= 201402L |
| 82 |
constexpr void Void(int n) { |
| 83 |
void(n + 1); |
| 84 |
void(); |
| 85 |
} |
| 86 |
constexpr int void_test = (Void(0), 1); |
| 87 |
static_assert(void_test == 1, ""); |
| 88 |
#endif |
| 89 |
} |
| 90 |
|
| 91 |
namespace IntegralCasts { |
| 92 |
constexpr int i = 12; |
| 93 |
constexpr unsigned int ui = i; |
| 94 |
static_assert(ui == 12, ""); |
| 95 |
constexpr unsigned int ub = !false; |
| 96 |
static_assert(ub == 1, ""); |
| 97 |
|
| 98 |
constexpr int si = ui; |
| 99 |
static_assert(si == 12, ""); |
| 100 |
constexpr int sb = true; |
| 101 |
static_assert(sb == 1, ""); |
| 102 |
|
| 103 |
constexpr int zero = 0; |
| 104 |
constexpr unsigned int uzero = 0; |
| 105 |
constexpr bool bs = i; |
| 106 |
static_assert(bs, ""); |
| 107 |
constexpr bool bu = ui; |
| 108 |
static_assert(bu, ""); |
| 109 |
constexpr bool ns = zero; |
| 110 |
static_assert(!ns, ""); |
| 111 |
constexpr bool nu = uzero; |
| 112 |
static_assert(!nu, ""); |
| 113 |
}; |
| 114 |
|
| 115 |
constexpr int UninitI; // both-error {{must be initialized by a constant expression}} |
| 116 |
constexpr int *UninitPtr; // both-error {{must be initialized by a constant expression}} |
| 117 |
|
| 118 |
constexpr bool getTrue() { return true; } |
| 119 |
constexpr bool getFalse() { return false; } |
| 120 |
constexpr void* getNull() { return nullptr; } |
| 121 |
|
| 122 |
constexpr int neg(int m) { return -m; } |
| 123 |
constexpr bool inv(bool b) { return !b; } |
| 124 |
|
| 125 |
static_assert(12, ""); |
| 126 |
static_assert(12 == -(-(12)), ""); |
| 127 |
static_assert(!false, ""); |
| 128 |
static_assert(!!true, ""); |
| 129 |
static_assert(!!true == !false, ""); |
| 130 |
static_assert(true == 1, ""); |
| 131 |
static_assert(false == 0, ""); |
| 132 |
static_assert(!5 == false, ""); |
| 133 |
static_assert(!0, ""); |
| 134 |
static_assert(-true, ""); |
| 135 |
static_assert(-false, ""); //both-error{{failed}} |
| 136 |
|
| 137 |
static_assert(~0 == -1, ""); |
| 138 |
static_assert(~1 == -2, ""); |
| 139 |
static_assert(~-1 == 0, ""); |
| 140 |
static_assert(~255 == -256, ""); |
| 141 |
static_assert(~INT_MIN == INT_MAX, ""); |
| 142 |
static_assert(~INT_MAX == INT_MIN, ""); |
| 143 |
|
| 144 |
static_assert(-(1 << 31), ""); // both-error {{not an integral constant expression}} \ |
| 145 |
// both-note {{outside the range of representable values}} |
| 146 |
|
| 147 |
namespace PrimitiveEmptyInitList { |
| 148 |
constexpr int a = {}; |
| 149 |
static_assert(a == 0, ""); |
| 150 |
constexpr bool b = {}; |
| 151 |
static_assert(!b, ""); |
| 152 |
constexpr double d = {}; |
| 153 |
static_assert(d == 0.0, ""); |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
enum E {}; |
| 158 |
constexpr E e = static_cast<E>(0); |
| 159 |
static_assert(~e == -1, ""); |
| 160 |
|
| 161 |
|
| 162 |
constexpr int m = 10; |
| 163 |
constexpr const int *p = &m; |
| 164 |
static_assert(p != nullptr, ""); |
| 165 |
static_assert(*p == 10, ""); |
| 166 |
|
| 167 |
constexpr const int* getIntPointer() { |
| 168 |
return &m; |
| 169 |
} |
| 170 |
static_assert(getIntPointer() == &m, ""); |
| 171 |
static_assert(*getIntPointer() == 10, ""); |
| 172 |
|
| 173 |
constexpr int gimme(int k) { |
| 174 |
return k; |
| 175 |
} |
| 176 |
static_assert(gimme(5) == 5, ""); |
| 177 |
|
| 178 |
namespace PointerToBool { |
| 179 |
|
| 180 |
constexpr void *N = nullptr; |
| 181 |
constexpr bool B = N; |
| 182 |
static_assert(!B, ""); |
| 183 |
static_assert(!N, ""); |
| 184 |
|
| 185 |
constexpr float F = 1.0; |
| 186 |
constexpr const float *FP = &F; |
| 187 |
static_assert(FP, ""); |
| 188 |
static_assert(!!FP, ""); |
| 189 |
} |
| 190 |
|
| 191 |
namespace PointerComparison { |
| 192 |
|
| 193 |
struct S { int a, b; } s; |
| 194 |
constexpr void *null = 0; |
| 195 |
constexpr void *pv = (void*)&s.a; |
| 196 |
constexpr void *qv = (void*)&s.b; |
| 197 |
constexpr bool v1 = null < (int*)0; |
| 198 |
constexpr bool v2 = null < pv; // both-error {{must be initialized by a constant expression}} \ |
| 199 |
// both-note {{comparison between pointers to unrelated objects 'nullptr' and '&s.a' has unspecified value}} |
| 200 |
|
| 201 |
constexpr bool v3 = null == pv; // ok |
| 202 |
constexpr bool v4 = qv == pv; // ok |
| 203 |
|
| 204 |
constexpr bool v5 = qv >= pv; |
| 205 |
constexpr bool v8 = qv > (void*)&s.a; |
| 206 |
constexpr bool v6 = qv > null; // both-error {{must be initialized by a constant expression}} \ |
| 207 |
// both-note {{comparison between pointers to unrelated objects '&s.b' and 'nullptr' has unspecified value}} |
| 208 |
|
| 209 |
constexpr bool v7 = qv <= (void*)&s.b; // ok |
| 210 |
|
| 211 |
constexpr ptrdiff_t m = &m - &m; |
| 212 |
static_assert(m == 0, ""); |
| 213 |
|
| 214 |
constexpr ptrdiff_t m2 = (&m2 + 1) - (&m2 + 1); |
| 215 |
static_assert(m2 == 0, ""); |
| 216 |
|
| 217 |
constexpr long m3 = (&m3 + 1) - (&m3); |
| 218 |
static_assert(m3 == 1, ""); |
| 219 |
|
| 220 |
constexpr long m4 = &m4 + 2 - &m4; // both-error {{must be initialized by a constant expression}} \ |
| 221 |
// both-note {{cannot refer to element 2 of non-array object}} |
| 222 |
} |
| 223 |
|
| 224 |
namespace SizeOf { |
| 225 |
static_assert(alignof(char&) == 1, ""); |
| 226 |
|
| 227 |
constexpr int soint = sizeof(int); |
| 228 |
constexpr int souint = sizeof(unsigned int); |
| 229 |
static_assert(soint == souint, ""); |
| 230 |
|
| 231 |
static_assert(sizeof(&soint) == sizeof(void*), ""); |
| 232 |
static_assert(sizeof(&soint) == sizeof(nullptr), ""); |
| 233 |
|
| 234 |
static_assert(sizeof(long) == sizeof(unsigned long), ""); |
| 235 |
static_assert(sizeof(char) == sizeof(unsigned char), ""); |
| 236 |
|
| 237 |
constexpr int N = 4; |
| 238 |
constexpr int arr[N] = {1,2,3,4}; |
| 239 |
static_assert(sizeof(arr) == N * sizeof(int), ""); |
| 240 |
static_assert(sizeof(arr) == N * sizeof(arr[0]), ""); |
| 241 |
|
| 242 |
constexpr bool arrB[N] = {true, true, true, true}; |
| 243 |
static_assert(sizeof(arrB) == N * sizeof(bool), ""); |
| 244 |
|
| 245 |
static_assert(sizeof(bool) == 1, ""); |
| 246 |
static_assert(sizeof(char) == 1, ""); |
| 247 |
|
| 248 |
constexpr int F = sizeof(void); // both-error{{incomplete type 'void'}} |
| 249 |
|
| 250 |
constexpr int F2 = sizeof(gimme); // both-error{{to a function type}} |
| 251 |
|
| 252 |
|
| 253 |
struct S { |
| 254 |
void func(); |
| 255 |
}; |
| 256 |
constexpr void (S::*Func)() = &S::func; |
| 257 |
static_assert(sizeof(Func) == sizeof(&S::func), ""); |
| 258 |
|
| 259 |
|
| 260 |
void func() { |
| 261 |
int n = 12; |
| 262 |
constexpr int oofda = sizeof(int[n++]); // both-error {{must be initialized by a constant expression}} |
| 263 |
} |
| 264 |
|
| 265 |
#if __cplusplus >= 201402L |
| 266 |
constexpr int IgnoredRejected() { // both-error {{never produces a constant expression}} |
| 267 |
int n = 0; |
| 268 |
sizeof(int[n++]); // both-warning {{expression result unused}} \ |
| 269 |
// both-note 2{{subexpression not valid in a constant expression}} |
| 270 |
return n; |
| 271 |
} |
| 272 |
static_assert(IgnoredRejected() == 0, ""); // both-error {{not an integral constant expression}} \ |
| 273 |
// both-note {{in call to 'IgnoredRejected()'}} |
| 274 |
#endif |
| 275 |
|
| 276 |
|
| 277 |
#if __cplusplus >= 202002L |
| 278 |
/// FIXME: The following code should be accepted. |
| 279 |
consteval int foo(int n) { // both-error {{consteval function never produces a constant expression}} |
| 280 |
return sizeof(int[n]); // both-note 3{{not valid in a constant expression}} |
| 281 |
} |
| 282 |
constinit int var = foo(5); // both-error {{not a constant expression}} \ |
| 283 |
// both-note 2{{in call to}} \ |
| 284 |
// both-error {{does not have a constant initializer}} \ |
| 285 |
// both-note {{required by 'constinit' specifier}} |
| 286 |
|
| 287 |
#endif |
| 288 |
}; |
| 289 |
|
| 290 |
namespace rem { |
| 291 |
static_assert(2 % 2 == 0, ""); |
| 292 |
static_assert(2 % 1 == 0, ""); |
| 293 |
static_assert(-3 % 4 == -3, ""); |
| 294 |
static_assert(4 % -2 == 0, ""); |
| 295 |
static_assert(-3 % -4 == -3, ""); |
| 296 |
|
| 297 |
constexpr int zero() { return 0; } |
| 298 |
static_assert(10 % zero() == 20, ""); // both-error {{not an integral constant expression}} \ |
| 299 |
// both-note {{division by zero}} |
| 300 |
|
| 301 |
static_assert(true % true == 0, ""); |
| 302 |
static_assert(false % true == 0, ""); |
| 303 |
static_assert(true % false == 10, ""); // both-error {{not an integral constant expression}} \ |
| 304 |
// both-note {{division by zero}} |
| 305 |
constexpr int x = INT_MIN % - 1; // both-error {{must be initialized by a constant expression}} \ |
| 306 |
// both-note {{value 2147483648 is outside the range}} |
| 307 |
}; |
| 308 |
|
| 309 |
namespace div { |
| 310 |
constexpr int zero() { return 0; } |
| 311 |
static_assert(12 / 3 == 4, ""); |
| 312 |
static_assert(12 / zero() == 12, ""); // both-error {{not an integral constant expression}} \ |
| 313 |
// both-note {{division by zero}} |
| 314 |
static_assert(12 / -3 == -4, ""); |
| 315 |
static_assert(-12 / 3 == -4, ""); |
| 316 |
|
| 317 |
|
| 318 |
constexpr int LHS = 12; |
| 319 |
constexpr long unsigned RHS = 3; |
| 320 |
static_assert(LHS / RHS == 4, ""); |
| 321 |
|
| 322 |
constexpr int x = INT_MIN / - 1; // both-error {{must be initialized by a constant expression}} \ |
| 323 |
// both-note {{value 2147483648 is outside the range}} |
| 324 |
}; |
| 325 |
|
| 326 |
namespace cond { |
| 327 |
constexpr bool isEven(int n) { |
| 328 |
return n % 2 == 0 ? true : false; |
| 329 |
} |
| 330 |
static_assert(isEven(2), ""); |
| 331 |
static_assert(!isEven(3), ""); |
| 332 |
static_assert(isEven(100), ""); |
| 333 |
|
| 334 |
constexpr int M = 5 ? 10 : 20; |
| 335 |
static_assert(M == 10, ""); |
| 336 |
|
| 337 |
static_assert(5 ? 13 : 16 == 13, ""); |
| 338 |
static_assert(0 ? 13 : 16 == 16, ""); |
| 339 |
|
| 340 |
static_assert(number ?: -15 == number, ""); |
| 341 |
static_assert(0 ?: 100 == 100 , ""); |
| 342 |
|
| 343 |
#if __cplusplus >= 201402L |
| 344 |
constexpr int N = 20; |
| 345 |
constexpr int foo() { |
| 346 |
int m = N > 0 ? 5 : 10; |
| 347 |
|
| 348 |
return m == 5 ? isEven(m) : true; |
| 349 |
} |
| 350 |
static_assert(foo() == false, ""); |
| 351 |
|
| 352 |
constexpr int dontCallMe(unsigned m) { |
| 353 |
if (m == 0) return 0; |
| 354 |
return dontCallMe(m - 2); |
| 355 |
} |
| 356 |
|
| 357 |
// Can't call this because it will run into infinite recursion. |
| 358 |
constexpr int assertNotReached() { |
| 359 |
return dontCallMe(3); |
| 360 |
} |
| 361 |
|
| 362 |
constexpr int testCond() { |
| 363 |
return true ? 5 : assertNotReached(); |
| 364 |
} |
| 365 |
|
| 366 |
constexpr int testCond2() { |
| 367 |
return false ? assertNotReached() : 10; |
| 368 |
} |
| 369 |
|
| 370 |
static_assert(testCond() == 5, ""); |
| 371 |
static_assert(testCond2() == 10, ""); |
| 372 |
|
| 373 |
#endif |
| 374 |
|
| 375 |
}; |
| 376 |
|
| 377 |
namespace band { |
| 378 |
static_assert((10 & 1) == 0, ""); |
| 379 |
static_assert((10 & 10) == 10, ""); |
| 380 |
|
| 381 |
static_assert((1337 & -1) == 1337, ""); |
| 382 |
static_assert((0 & gimme(12)) == 0, ""); |
| 383 |
}; |
| 384 |
|
| 385 |
namespace bitOr { |
| 386 |
static_assert((10 | 1) == 11, ""); |
| 387 |
static_assert((10 | 10) == 10, ""); |
| 388 |
|
| 389 |
static_assert((1337 | -1) == -1, ""); |
| 390 |
static_assert((0 | gimme(12)) == 12, ""); |
| 391 |
static_assert((12 | true) == 13, ""); |
| 392 |
}; |
| 393 |
|
| 394 |
namespace bitXor { |
| 395 |
#pragma clang diagnostic push |
| 396 |
#pragma clang diagnostic ignored "-Wxor-used-as-pow" |
| 397 |
static_assert((10 ^ 1) == 11, ""); |
| 398 |
static_assert((10 ^ 10) == 0, ""); |
| 399 |
|
| 400 |
enum { |
| 401 |
ONE = 1, |
| 402 |
}; |
| 403 |
|
| 404 |
static_assert((1337 ^ -1) == -1338, ""); |
| 405 |
static_assert((0 | gimme(12)) == 12, ""); |
| 406 |
static_assert((12 ^ true) == 13, ""); |
| 407 |
static_assert((12 ^ ONE) == 13, ""); |
| 408 |
#pragma clang diagnostic pop |
| 409 |
}; |
| 410 |
|
| 411 |
#if __cplusplus >= 201402L |
| 412 |
constexpr bool IgnoredUnary() { |
| 413 |
bool bo = true; |
| 414 |
!bo; // both-warning {{expression result unused}} |
| 415 |
return bo; |
| 416 |
} |
| 417 |
static_assert(IgnoredUnary(), ""); |
| 418 |
#endif |
| 419 |
|
| 420 |
namespace strings { |
| 421 |
constexpr const char *S = "abc"; |
| 422 |
static_assert(S[0] == 97, ""); |
| 423 |
static_assert(S[1] == 98, ""); |
| 424 |
static_assert(S[2] == 99, ""); |
| 425 |
static_assert(S[3] == 0, ""); |
| 426 |
|
| 427 |
static_assert("foobar"[2] == 'o', ""); |
| 428 |
static_assert(2["foobar"] == 'o', ""); |
| 429 |
|
| 430 |
constexpr const wchar_t *wide = L"bar"; |
| 431 |
static_assert(wide[0] == L'b', ""); |
| 432 |
|
| 433 |
constexpr const char32_t *u32 = U"abc"; |
| 434 |
static_assert(u32[1] == U'b', ""); |
| 435 |
|
| 436 |
constexpr char32_t c = U'\U0001F60E'; |
| 437 |
static_assert(c == 0x0001F60EL, ""); |
| 438 |
|
| 439 |
constexpr char k = -1; |
| 440 |
static_assert(k == -1, ""); |
| 441 |
|
| 442 |
static_assert('\N{LATIN CAPITAL LETTER E}' == 'E', ""); |
| 443 |
static_assert('\t' == 9, ""); |
| 444 |
|
| 445 |
#pragma clang diagnostic push |
| 446 |
#pragma clang diagnostic ignored "-Wmultichar" |
| 447 |
constexpr int mc = 'abc'; |
| 448 |
static_assert(mc == 'abc', ""); |
| 449 |
__WCHAR_TYPE__ wm = L'abc'; // both-error{{wide character literals may not contain multiple characters}} |
| 450 |
__WCHAR_TYPE__ wu = u'abc'; // both-error{{Unicode character literals may not contain multiple characters}} |
| 451 |
__WCHAR_TYPE__ wU = U'abc'; // both-error{{Unicode character literals may not contain multiple characters}} |
| 452 |
#if __cplusplus > 201103L |
| 453 |
__WCHAR_TYPE__ wu8 = u8'abc'; // both-error{{Unicode character literals may not contain multiple characters}} |
| 454 |
#endif |
| 455 |
|
| 456 |
#pragma clang diagnostic pop |
| 457 |
|
| 458 |
constexpr char foo[12] = "abc"; |
| 459 |
static_assert(foo[0] == 'a', ""); |
| 460 |
static_assert(foo[1] == 'b', ""); |
| 461 |
static_assert(foo[2] == 'c', ""); |
| 462 |
static_assert(foo[3] == 0, ""); |
| 463 |
static_assert(foo[11] == 0, ""); |
| 464 |
|
| 465 |
constexpr char foo2[] = "abc\0def"; |
| 466 |
static_assert(foo2[0] == 'a', ""); |
| 467 |
static_assert(foo2[3] == '\0', ""); |
| 468 |
static_assert(foo2[6] == 'f', ""); |
| 469 |
static_assert(foo2[7] == '\0', ""); |
| 470 |
static_assert(foo2[8] == '\0', ""); // both-error {{not an integral constant expression}} \ |
| 471 |
// both-note {{read of dereferenced one-past-the-end pointer}} |
| 472 |
|
| 473 |
constexpr char foo3[4] = "abc"; |
| 474 |
static_assert(foo3[3] == '\0', ""); |
| 475 |
static_assert(foo3[4] == '\0', ""); // both-error {{not an integral constant expression}} \ |
| 476 |
// both-note {{read of dereferenced one-past-the-end pointer}} |
| 477 |
|
| 478 |
constexpr char foo4[2] = "abcd"; // both-error {{initializer-string for char array is too long}} |
| 479 |
static_assert(foo4[0] == 'a', ""); |
| 480 |
static_assert(foo4[1] == 'b', ""); |
| 481 |
static_assert(foo4[2] == '\0', ""); // both-error {{not an integral constant expression}} \ |
| 482 |
// both-note {{read of dereferenced one-past-the-end pointer}} |
| 483 |
|
| 484 |
constexpr char foo5[12] = "abc\xff"; |
| 485 |
#if defined(__CHAR_UNSIGNED__) || __CHAR_BIT__ > 8 |
| 486 |
static_assert(foo5[3] == 255, ""); |
| 487 |
#else |
| 488 |
static_assert(foo5[3] == -1, ""); |
| 489 |
#endif |
| 490 |
}; |
| 491 |
|
| 492 |
#if __cplusplus > 201402L |
| 493 |
namespace IncDec { |
| 494 |
constexpr int zero() { |
| 495 |
int a = 0; |
| 496 |
a++; |
| 497 |
++a; |
| 498 |
a--; |
| 499 |
--a; |
| 500 |
return a; |
| 501 |
} |
| 502 |
static_assert(zero() == 0, ""); |
| 503 |
|
| 504 |
constexpr int preInc() { |
| 505 |
int a = 0; |
| 506 |
return ++a; |
| 507 |
} |
| 508 |
static_assert(preInc() == 1, ""); |
| 509 |
|
| 510 |
constexpr int postInc() { |
| 511 |
int a = 0; |
| 512 |
return a++; |
| 513 |
} |
| 514 |
static_assert(postInc() == 0, ""); |
| 515 |
|
| 516 |
constexpr int preDec() { |
| 517 |
int a = 0; |
| 518 |
return --a; |
| 519 |
} |
| 520 |
static_assert(preDec() == -1, ""); |
| 521 |
|
| 522 |
constexpr int postDec() { |
| 523 |
int a = 0; |
| 524 |
return a--; |
| 525 |
} |
| 526 |
static_assert(postDec() == 0, ""); |
| 527 |
|
| 528 |
constexpr int three() { |
| 529 |
int a = 0; |
| 530 |
return ++a + ++a; // both-warning {{multiple unsequenced modifications to 'a'}} |
| 531 |
} |
| 532 |
static_assert(three() == 3, ""); |
| 533 |
|
| 534 |
constexpr bool incBool() { |
| 535 |
bool b = false; |
| 536 |
return ++b; // both-error {{ISO C++17 does not allow incrementing expression of type bool}} |
| 537 |
} |
| 538 |
static_assert(incBool(), ""); |
| 539 |
|
| 540 |
/// FIXME: The diagnostics for pre-inc/dec of pointers doesn't match the |
| 541 |
/// current interpreter. But they are stil OK. |
| 542 |
template<typename T, bool Inc, bool Pre> |
| 543 |
constexpr int uninit() { |
| 544 |
T a; |
| 545 |
if constexpr (Inc) { |
| 546 |
if (Pre) |
| 547 |
++a; // ref-note 3{{increment of uninitialized}} \ |
| 548 |
// expected-note 2{{increment of uninitialized}} \ |
| 549 |
// expected-note {{read of uninitialized}} |
| 550 |
else |
| 551 |
a++; // both-note 2{{increment of uninitialized}} |
| 552 |
} else { |
| 553 |
if (Pre) |
| 554 |
--a; // ref-note 3{{decrement of uninitialized}} \ |
| 555 |
// expected-note 2{{decrement of uninitialized}} \ |
| 556 |
// expected-note {{read of uninitialized}} |
| 557 |
else |
| 558 |
a--; // both-note 2{{decrement of uninitialized}} |
| 559 |
} |
| 560 |
return 1; |
| 561 |
} |
| 562 |
static_assert(uninit<int, true, true>(), ""); // both-error {{not an integral constant expression}} \ |
| 563 |
// both-note {{in call to 'uninit<int, true, true>()'}} |
| 564 |
static_assert(uninit<int, false, true>(), ""); // both-error {{not an integral constant expression}} \ |
| 565 |
// both-note {{in call to 'uninit<int, false, true>()'}} |
| 566 |
|
| 567 |
static_assert(uninit<float, true, true>(), ""); // both-error {{not an integral constant expression}} \ |
| 568 |
// both-note {{in call to 'uninit<float, true, true>()'}} |
| 569 |
static_assert(uninit<float, false, true>(), ""); // both-error {{not an integral constant expression}} \ |
| 570 |
// both-note {{in call to 'uninit<float, false, true>()'}} |
| 571 |
static_assert(uninit<float, true, false>(), ""); // both-error {{not an integral constant expression}} \ |
| 572 |
// both-note {{in call to 'uninit<float, true, false>()'}} |
| 573 |
static_assert(uninit<float, false, false>(), ""); // both-error {{not an integral constant expression}} \ |
| 574 |
// both-note {{in call to 'uninit<float, false, false>()'}} |
| 575 |
|
| 576 |
static_assert(uninit<int*, true, true>(), ""); // both-error {{not an integral constant expression}} \ |
| 577 |
// both-note {{in call to 'uninit<int *, true, true>()'}} |
| 578 |
static_assert(uninit<int*, false, true>(), ""); // both-error {{not an integral constant expression}} \ |
| 579 |
// both-note {{in call to 'uninit<int *, false, true>()'}} |
| 580 |
static_assert(uninit<int*, true, false>(), ""); // both-error {{not an integral constant expression}} \ |
| 581 |
// both-note {{in call to 'uninit<int *, true, false>()'}} |
| 582 |
static_assert(uninit<int*, false, false>(), ""); // both-error {{not an integral constant expression}} \ |
| 583 |
// both-note {{in call to 'uninit<int *, false, false>()'}} |
| 584 |
|
| 585 |
constexpr int OverFlow() { // both-error {{never produces a constant expression}} |
| 586 |
int a = INT_MAX; |
| 587 |
++a; // both-note 2{{is outside the range}} |
| 588 |
return -1; |
| 589 |
} |
| 590 |
static_assert(OverFlow() == -1, ""); // both-error {{not an integral constant expression}} \ |
| 591 |
// both-note {{in call to 'OverFlow()'}} |
| 592 |
|
| 593 |
constexpr int UnderFlow() { // both-error {{never produces a constant expression}} |
| 594 |
int a = INT_MIN; |
| 595 |
--a; // both-note 2{{is outside the range}} |
| 596 |
return -1; |
| 597 |
} |
| 598 |
static_assert(UnderFlow() == -1, ""); // both-error {{not an integral constant expression}} \ |
| 599 |
// both-note {{in call to 'UnderFlow()'}} |
| 600 |
|
| 601 |
/// This UnaryOperator can't overflow, so we shouldn't diagnose any overflow. |
| 602 |
constexpr int CanOverflow() { |
| 603 |
char c = 127; |
| 604 |
char p; |
| 605 |
++c; |
| 606 |
c++; |
| 607 |
p = ++c; |
| 608 |
p = c++; |
| 609 |
|
| 610 |
c = -128; |
| 611 |
--c; |
| 612 |
c--; |
| 613 |
p = --c; |
| 614 |
p = ++c; |
| 615 |
|
| 616 |
return 0; |
| 617 |
} |
| 618 |
static_assert(CanOverflow() == 0, ""); |
| 619 |
|
| 620 |
constexpr char OverflownChar() { |
| 621 |
char c = 127; |
| 622 |
c++; |
| 623 |
return c; |
| 624 |
} |
| 625 |
static_assert(OverflownChar() == -128, ""); |
| 626 |
|
| 627 |
constexpr int getTwo() { |
| 628 |
int i = 1; |
| 629 |
return (i += 1); |
| 630 |
} |
| 631 |
static_assert(getTwo() == 2, ""); |
| 632 |
|
| 633 |
constexpr int sub(int a) { |
| 634 |
return (a -= 2); |
| 635 |
} |
| 636 |
static_assert(sub(7) == 5, ""); |
| 637 |
|
| 638 |
constexpr int add(int a, int b) { |
| 639 |
a += b; // both-note {{is outside the range of representable values}} |
| 640 |
return a; |
| 641 |
} |
| 642 |
static_assert(add(1, 2) == 3, ""); |
| 643 |
static_assert(add(INT_MAX, 1) == 0, ""); // both-error {{not an integral constant expression}} \ |
| 644 |
// both-note {{in call to 'add}} |
| 645 |
|
| 646 |
constexpr int sub(int a, int b) { |
| 647 |
a -= b; // both-note {{is outside the range of representable values}} |
| 648 |
return a; |
| 649 |
} |
| 650 |
static_assert(sub(10, 20) == -10, ""); |
| 651 |
static_assert(sub(INT_MIN, 1) == 0, ""); // both-error {{not an integral constant expression}} \ |
| 652 |
// both-note {{in call to 'sub}} |
| 653 |
|
| 654 |
constexpr int subAll(int a) { |
| 655 |
return (a -= a); |
| 656 |
} |
| 657 |
static_assert(subAll(213) == 0, ""); |
| 658 |
|
| 659 |
constexpr bool BoolOr(bool b1, bool b2) { |
| 660 |
bool a; |
| 661 |
a = b1; |
| 662 |
a |= b2; |
| 663 |
return a; |
| 664 |
} |
| 665 |
static_assert(BoolOr(true, true), ""); |
| 666 |
static_assert(BoolOr(true, false), ""); |
| 667 |
static_assert(BoolOr(false, true), ""); |
| 668 |
static_assert(!BoolOr(false, false), ""); |
| 669 |
|
| 670 |
constexpr int IntOr(unsigned a, unsigned b) { |
| 671 |
unsigned r; |
| 672 |
r = a; |
| 673 |
r |= b; |
| 674 |
return r; |
| 675 |
} |
| 676 |
static_assert(IntOr(10, 1) == 11, ""); |
| 677 |
static_assert(IntOr(1337, -1) == -1, ""); |
| 678 |
static_assert(IntOr(0, 12) == 12, ""); |
| 679 |
|
| 680 |
constexpr bool BoolAnd(bool b1, bool b2) { |
| 681 |
bool a; |
| 682 |
a = b1; |
| 683 |
a &= b2; |
| 684 |
return a; |
| 685 |
} |
| 686 |
static_assert(BoolAnd(true, true), ""); |
| 687 |
static_assert(!BoolAnd(true, false), ""); |
| 688 |
static_assert(!BoolAnd(false, true), ""); |
| 689 |
static_assert(!BoolAnd(false, false), ""); |
| 690 |
|
| 691 |
constexpr int IntAnd(unsigned a, unsigned b) { |
| 692 |
unsigned r; |
| 693 |
r = a; |
| 694 |
r &= b; |
| 695 |
return r; |
| 696 |
} |
| 697 |
static_assert(IntAnd(10, 1) == 0, ""); |
| 698 |
static_assert(IntAnd(1337, -1) == 1337, ""); |
| 699 |
static_assert(IntAnd(0, 12) == 0, ""); |
| 700 |
|
| 701 |
constexpr bool BoolXor(bool b1, bool b2) { |
| 702 |
bool a; |
| 703 |
a = b1; |
| 704 |
a ^= b2; |
| 705 |
return a; |
| 706 |
} |
| 707 |
static_assert(!BoolXor(true, true), ""); |
| 708 |
static_assert(BoolXor(true, false), ""); |
| 709 |
static_assert(BoolXor(false, true), ""); |
| 710 |
static_assert(!BoolXor(false, false), ""); |
| 711 |
|
| 712 |
constexpr int IntXor(unsigned a, unsigned b) { |
| 713 |
unsigned r; |
| 714 |
r = a; |
| 715 |
r ^= b; |
| 716 |
return r; |
| 717 |
} |
| 718 |
static_assert(IntXor(10, 1) == 11, ""); |
| 719 |
static_assert(IntXor(10, 10) == 0, ""); |
| 720 |
static_assert(IntXor(12, true) == 13, ""); |
| 721 |
|
| 722 |
constexpr bool BoolRem(bool b1, bool b2) { |
| 723 |
bool a; |
| 724 |
a = b1; |
| 725 |
a %= b2; |
| 726 |
return a; |
| 727 |
} |
| 728 |
static_assert(!BoolRem(true, true), ""); |
| 729 |
static_assert(!BoolRem(false, true), ""); |
| 730 |
|
| 731 |
constexpr int IntRem(int a, int b) { |
| 732 |
int r; |
| 733 |
r = a; |
| 734 |
r %= b; // both-note {{division by zero}} \ |
| 735 |
// both-note {{outside the range of representable values}} |
| 736 |
return r; |
| 737 |
} |
| 738 |
static_assert(IntRem(2, 2) == 0, ""); |
| 739 |
static_assert(IntRem(2, 1) == 0, ""); |
| 740 |
static_assert(IntRem(9, 7) == 2, ""); |
| 741 |
static_assert(IntRem(5, 0) == 0, ""); // both-error {{not an integral constant expression}} \ |
| 742 |
// both-note {{in call to 'IntRem(5, 0)'}} |
| 743 |
|
| 744 |
static_assert(IntRem(INT_MIN, -1) == 0, ""); // both-error {{not an integral constant expression}} \ |
| 745 |
// both-note {{in call to 'IntRem}} |
| 746 |
|
| 747 |
constexpr bool BoolDiv(bool b1, bool b2) { |
| 748 |
bool a; |
| 749 |
a = b1; |
| 750 |
a /= b2; |
| 751 |
return a; |
| 752 |
} |
| 753 |
static_assert(BoolDiv(true, true), ""); |
| 754 |
static_assert(!BoolDiv(false, true), ""); |
| 755 |
|
| 756 |
constexpr int IntDiv(int a, int b) { |
| 757 |
int r; |
| 758 |
r = a; |
| 759 |
r /= b; // both-note {{division by zero}} \ |
| 760 |
// both-note {{outside the range of representable values}} |
| 761 |
return r; |
| 762 |
} |
| 763 |
static_assert(IntDiv(2, 2) == 1, ""); |
| 764 |
static_assert(IntDiv(12, 20) == 0, ""); |
| 765 |
static_assert(IntDiv(2, 1) == 2, ""); |
| 766 |
static_assert(IntDiv(9, 7) == 1, ""); |
| 767 |
static_assert(IntDiv(5, 0) == 0, ""); // both-error {{not an integral constant expression}} \ |
| 768 |
// both-note {{in call to 'IntDiv(5, 0)'}} |
| 769 |
|
| 770 |
static_assert(IntDiv(INT_MIN, -1) == 0, ""); // both-error {{not an integral constant expression}} \ |
| 771 |
// both-note {{in call to 'IntDiv}} |
| 772 |
|
| 773 |
constexpr bool BoolMul(bool b1, bool b2) { |
| 774 |
bool a; |
| 775 |
a = b1; |
| 776 |
a *= b2; |
| 777 |
return a; |
| 778 |
} |
| 779 |
static_assert(BoolMul(true, true), ""); |
| 780 |
static_assert(!BoolMul(true, false), ""); |
| 781 |
static_assert(!BoolMul(false, true), ""); |
| 782 |
static_assert(!BoolMul(false, false), ""); |
| 783 |
|
| 784 |
constexpr int IntMul(int a, int b) { |
| 785 |
int r; |
| 786 |
r = a; |
| 787 |
r *= b; // both-note {{is outside the range of representable values of type 'int'}} |
| 788 |
return r; |
| 789 |
} |
| 790 |
static_assert(IntMul(2, 2) == 4, ""); |
| 791 |
static_assert(IntMul(12, 20) == 240, ""); |
| 792 |
static_assert(IntMul(2, 1) == 2, ""); |
| 793 |
static_assert(IntMul(9, 7) == 63, ""); |
| 794 |
static_assert(IntMul(INT_MAX, 2) == 0, ""); // both-error {{not an integral constant expression}} \ |
| 795 |
// both-note {{in call to 'IntMul}} |
| 796 |
constexpr int arr[] = {1,2,3}; |
| 797 |
constexpr int ptrInc1() { |
| 798 |
const int *p = arr; |
| 799 |
p += 2; |
| 800 |
return *p; |
| 801 |
} |
| 802 |
static_assert(ptrInc1() == 3, ""); |
| 803 |
|
| 804 |
constexpr int ptrInc2() { |
| 805 |
const int *p = arr; |
| 806 |
return *(p += 1); |
| 807 |
} |
| 808 |
static_assert(ptrInc2() == 2, ""); |
| 809 |
|
| 810 |
constexpr int ptrInc3() { // both-error {{never produces a constant expression}} |
| 811 |
const int *p = arr; |
| 812 |
p += 12; // both-note {{cannot refer to element 12 of array of 3 elements}} |
| 813 |
return *p; |
| 814 |
} |
| 815 |
|
| 816 |
constexpr int ptrIncDec1() { |
| 817 |
const int *p = arr; |
| 818 |
p += 2; |
| 819 |
p -= 1; |
| 820 |
return *p; |
| 821 |
} |
| 822 |
static_assert(ptrIncDec1() == 2, ""); |
| 823 |
|
| 824 |
constexpr int ptrDec1() { // both-error {{never produces a constant expression}} |
| 825 |
const int *p = arr; |
| 826 |
p -= 1; // both-note {{cannot refer to element -1 of array of 3 elements}} |
| 827 |
return *p; |
| 828 |
} |
| 829 |
|
| 830 |
/// This used to leave a 0 on the stack instead of the previous |
| 831 |
/// value of a. |
| 832 |
constexpr int bug1Inc() { |
| 833 |
int a = 3; |
| 834 |
int b = a++; |
| 835 |
return b; |
| 836 |
} |
| 837 |
static_assert(bug1Inc() == 3); |
| 838 |
|
| 839 |
constexpr int bug1Dec() { |
| 840 |
int a = 3; |
| 841 |
int b = a--; |
| 842 |
return b; |
| 843 |
} |
| 844 |
static_assert(bug1Dec() == 3); |
| 845 |
|
| 846 |
constexpr int f() { |
| 847 |
int a[] = {1,2}; |
| 848 |
int i = 0; |
| 849 |
|
| 850 |
// RHS should be evaluated before LHS, so this should |
| 851 |
// write to a[1]; |
| 852 |
a[i++] += ++i; |
| 853 |
|
| 854 |
return a[1]; |
| 855 |
} |
| 856 |
static_assert(f() == 3, ""); |
| 857 |
|
| 858 |
int nonconst(int a) { // both-note 4{{declared here}} |
| 859 |
static_assert(a++, ""); // both-error {{not an integral constant expression}} \ |
| 860 |
// both-note {{function parameter 'a' with unknown value cannot be used in a constant expression}} |
| 861 |
static_assert(a--, ""); // both-error {{not an integral constant expression}} \ |
| 862 |
// both-note {{function parameter 'a' with unknown value cannot be used in a constant expression}} |
| 863 |
static_assert(++a, ""); // both-error {{not an integral constant expression}} \ |
| 864 |
// both-note {{function parameter 'a' with unknown value cannot be used in a constant expression}} |
| 865 |
static_assert(--a, ""); // both-error {{not an integral constant expression}} \ |
| 866 |
// both-note {{function parameter 'a' with unknown value cannot be used in a constant expression}} |
| 867 |
} |
| 868 |
|
| 869 |
namespace Const { |
| 870 |
constexpr int test1(const int a) { |
| 871 |
((int&)a)++; // both-note {{modification of object of const-qualified type 'const int'}} |
| 872 |
return a; |
| 873 |
} |
| 874 |
static_assert(test1(12) == 10); // both-error {{not an integral constant expression}} \ |
| 875 |
// both-note {{in call to}} |
| 876 |
|
| 877 |
constexpr int test2(const int a) { |
| 878 |
++((int&)a); // both-note {{modification of object of const-qualified type 'const int'}} |
| 879 |
return a; |
| 880 |
} |
| 881 |
static_assert(test2(12) == 10); // both-error {{not an integral constant expression}} \ |
| 882 |
// both-note {{in call to}} |
| 883 |
constexpr int test3(const int a) { |
| 884 |
((int&)a)--; // both-note {{modification of object of const-qualified type 'const int'}} |
| 885 |
return a; |
| 886 |
} |
| 887 |
static_assert(test3(12) == 10); // both-error {{not an integral constant expression}} \ |
| 888 |
// both-note {{in call to}} |
| 889 |
|
| 890 |
constexpr int test4(const int a) { |
| 891 |
--((int&)a); // both-note {{modification of object of const-qualified type 'const int'}} |
| 892 |
return a; |
| 893 |
} |
| 894 |
static_assert(test4(12) == 10); // both-error {{not an integral constant expression}} \ |
| 895 |
// both-note {{in call to}} |
| 896 |
|
| 897 |
constexpr int test5(const int a) { |
| 898 |
int b = ((int&)a)++; // both-note {{modification of object of const-qualified type 'const int'}} |
| 899 |
return a; |
| 900 |
} |
| 901 |
static_assert(test5(12) == 10); // both-error {{not an integral constant expression}} \ |
| 902 |
// both-note {{in call to}} |
| 903 |
|
| 904 |
constexpr int test6(const int a) { |
| 905 |
int b = ++((int&)a); // both-note {{modification of object of const-qualified type 'const int'}} |
| 906 |
return a; |
| 907 |
} |
| 908 |
static_assert(test6(12) == 10); // both-error {{not an integral constant expression}} \ |
| 909 |
// both-note {{in call to}} |
| 910 |
constexpr int test7(const int a) { |
| 911 |
int b = ((int&)a)--; // both-note {{modification of object of const-qualified type 'const int'}} |
| 912 |
return a; |
| 913 |
} |
| 914 |
static_assert(test7(12) == 10); // both-error {{not an integral constant expression}} \ |
| 915 |
// both-note {{in call to}} |
| 916 |
|
| 917 |
constexpr int test8(const int a) { |
| 918 |
int b = --((int&)a); // both-note {{modification of object of const-qualified type 'const int'}} |
| 919 |
return a; |
| 920 |
} |
| 921 |
static_assert(test8(12) == 10); // both-error {{not an integral constant expression}} \ |
| 922 |
// both-note {{in call to}} |
| 923 |
} |
| 924 |
} |
| 925 |
#endif |
| 926 |
|
| 927 |
namespace CompoundLiterals { |
| 928 |
constexpr int get5() { |
| 929 |
return (int[]){1,2,3,4,5}[4]; |
| 930 |
} |
| 931 |
static_assert(get5() == 5, ""); |
| 932 |
|
| 933 |
constexpr int get6(int f = (int[]){1,2,6}[2]) { |
| 934 |
return f; |
| 935 |
} |
| 936 |
static_assert(get6(6) == 6, ""); |
| 937 |
static_assert(get6() == 6, ""); |
| 938 |
|
| 939 |
constexpr int x = (int){3}; |
| 940 |
static_assert(x == 3, ""); |
| 941 |
#if __cplusplus >= 201402L |
| 942 |
constexpr int getX() { |
| 943 |
int x = (int){3}; |
| 944 |
x = (int){5}; |
| 945 |
return x; |
| 946 |
} |
| 947 |
static_assert(getX() == 5, ""); |
| 948 |
#endif |
| 949 |
|
| 950 |
#if __cplusplus >= 202002L |
| 951 |
constexpr int get3() { |
| 952 |
int m; |
| 953 |
m = (int){3}; |
| 954 |
return m; |
| 955 |
} |
| 956 |
static_assert(get3() == 3, ""); |
| 957 |
|
| 958 |
constexpr int *f(int *a=(int[]){1,2,3}) { return a; } // both-note {{temporary created here}} |
| 959 |
constinit int *a1 = f(); // both-error {{variable does not have a constant initializer}} \ |
| 960 |
both-note {{required by 'constinit' specifier here}} \ |
| 961 |
both-note {{pointer to subobject of temporary is not a constant expression}} |
| 962 |
static_assert(f()[0] == 1); // Ok |
| 963 |
#endif |
| 964 |
|
| 965 |
constexpr int f2(int *x =(int[]){1,2,3}) { |
| 966 |
return x[0]; |
| 967 |
} |
| 968 |
// Should evaluate to 1? |
| 969 |
constexpr int g = f2(); // #g_decl |
| 970 |
static_assert(g == 1, ""); |
| 971 |
|
| 972 |
// This example should be rejected because the lifetime of the compound |
| 973 |
// literal assigned into x is that of the full expression, which is the |
| 974 |
// parenthesized assignment operator. So the return statement is using a |
| 975 |
// dangling pointer. FIXME: the note saying it's a read of a dereferenced |
| 976 |
// null pointer suggests we're doing something odd during constant expression |
| 977 |
// evaluation: I think it's still taking 'x' as being null from the call to |
| 978 |
// f3() rather than tracking the assignment happening in the VLA. |
| 979 |
constexpr int f3(int *x, int (*y)[*(x=(int[]){1,2,3})]) { // both-warning {{object backing the pointer 'x' will be destroyed at the end of the full-expression}} |
| 980 |
return x[0]; // both-note {{read of dereferenced null pointer is not allowed in a constant expression}} |
| 981 |
} |
| 982 |
constexpr int h = f3(0,0); // both-error {{constexpr variable 'h' must be initialized by a constant expression}} \ |
| 983 |
both-note {{in call to 'f3(nullptr, nullptr)'}} |
| 984 |
} |
| 985 |
|
| 986 |
namespace TypeTraits { |
| 987 |
static_assert(__is_trivial(int), ""); |
| 988 |
static_assert(__is_trivial(float), ""); |
| 989 |
static_assert(__is_trivial(E), ""); |
| 990 |
struct S{}; |
| 991 |
static_assert(__is_trivial(S), ""); |
| 992 |
struct S2 { |
| 993 |
S2() {} |
| 994 |
}; |
| 995 |
static_assert(!__is_trivial(S2), ""); |
| 996 |
|
| 997 |
template <typename T> |
| 998 |
struct S3 { |
| 999 |
constexpr bool foo() const { return __is_trivial(T); } |
| 1000 |
}; |
| 1001 |
struct T { |
| 1002 |
~T() {} |
| 1003 |
}; |
| 1004 |
struct U {}; |
| 1005 |
static_assert(S3<U>{}.foo(), ""); |
| 1006 |
static_assert(!S3<T>{}.foo(), ""); |
| 1007 |
|
| 1008 |
typedef int Int; |
| 1009 |
typedef Int IntAr[10]; |
| 1010 |
typedef const IntAr ConstIntAr; |
| 1011 |
typedef ConstIntAr ConstIntArAr[4]; |
| 1012 |
|
| 1013 |
static_assert(__array_rank(IntAr) == 1, ""); |
| 1014 |
static_assert(__array_rank(ConstIntArAr) == 2, ""); |
| 1015 |
|
| 1016 |
static_assert(__array_extent(IntAr, 0) == 10, ""); |
| 1017 |
static_assert(__array_extent(ConstIntArAr, 0) == 4, ""); |
| 1018 |
static_assert(__array_extent(ConstIntArAr, 1) == 10, ""); |
| 1019 |
} |
| 1020 |
|
| 1021 |
#if __cplusplus >= 201402L |
| 1022 |
namespace SomeNS { |
| 1023 |
using MyInt = int; |
| 1024 |
} |
| 1025 |
|
| 1026 |
constexpr int ignoredDecls() { |
| 1027 |
static_assert(true, ""); |
| 1028 |
struct F { int a; }; |
| 1029 |
enum E { b }; |
| 1030 |
using A = int; |
| 1031 |
typedef int Z; |
| 1032 |
namespace NewNS = SomeNS; |
| 1033 |
using NewNS::MyInt; |
| 1034 |
|
| 1035 |
return F{12}.a; |
| 1036 |
} |
| 1037 |
static_assert(ignoredDecls() == 12, ""); |
| 1038 |
|
| 1039 |
namespace DiscardExprs { |
| 1040 |
#pragma clang diagnostic push |
| 1041 |
#pragma clang diagnostic ignored "-Wunused-value" |
| 1042 |
typedef struct _GUID { |
| 1043 |
__UINT32_TYPE__ Data1; |
| 1044 |
__UINT16_TYPE__ Data2; |
| 1045 |
__UINT16_TYPE__ Data3; |
| 1046 |
__UINT8_TYPE__ Data4[8]; |
| 1047 |
} GUID; |
| 1048 |
class __declspec(uuid("000000A0-0000-0000-C000-000000000049")) GuidType; |
| 1049 |
|
| 1050 |
struct A{ int a; }; |
| 1051 |
constexpr int ignoredExprs() { |
| 1052 |
(void)(1 / 2); |
| 1053 |
A a{12}; |
| 1054 |
a; |
| 1055 |
(void)a; |
| 1056 |
(a); |
| 1057 |
|
| 1058 |
/// Ignored MaterializeTemporaryExpr. |
| 1059 |
struct B{ const int &a; }; |
| 1060 |
(void)B{12}; |
| 1061 |
|
| 1062 |
(void)5, (void)6; |
| 1063 |
|
| 1064 |
1 ? 0 : 1; |
| 1065 |
__is_trivial(int); |
| 1066 |
|
| 1067 |
(int){1}; |
| 1068 |
(int[]){1,2,3}; |
| 1069 |
int arr[] = {1,2,3}; |
| 1070 |
arr[0]; |
| 1071 |
"a"; |
| 1072 |
'b'; |
| 1073 |
sizeof(int); |
| 1074 |
alignof(int); |
| 1075 |
|
| 1076 |
(short)5; |
| 1077 |
(bool)1; |
| 1078 |
__null; |
| 1079 |
__builtin_offsetof(A, a); |
| 1080 |
1,2; |
| 1081 |
(int)1.0; |
| 1082 |
(float)1; |
| 1083 |
(double)1.0f; |
| 1084 |
(signed)4u; |
| 1085 |
__uuidof(GuidType); |
| 1086 |
__uuidof(number); // both-error {{cannot call operator __uuidof on a type with no GUID}} |
| 1087 |
|
| 1088 |
requires{false;}; |
| 1089 |
constexpr int *p = nullptr; |
| 1090 |
p - p; |
| 1091 |
|
| 1092 |
return 0; |
| 1093 |
} |
| 1094 |
static_assert(ignoredExprs() == 0, ""); |
| 1095 |
|
| 1096 |
constexpr int oh_my(int x) { |
| 1097 |
(int){ x++ }; |
| 1098 |
return x; |
| 1099 |
} |
| 1100 |
static_assert(oh_my(0) == 1, ""); |
| 1101 |
|
| 1102 |
constexpr int oh_my2(int x) { |
| 1103 |
int y{x++}; |
| 1104 |
return x; |
| 1105 |
} |
| 1106 |
|
| 1107 |
static_assert(oh_my2(0) == 1, ""); |
| 1108 |
|
| 1109 |
|
| 1110 |
/// Ignored comma expressions still have their |
| 1111 |
/// expressions evaluated. |
| 1112 |
constexpr int Comma(int start) { |
| 1113 |
int i = start; |
| 1114 |
|
| 1115 |
(void)i++; |
| 1116 |
(void)i++,(void)i++; |
| 1117 |
return i; |
| 1118 |
} |
| 1119 |
constexpr int Value = Comma(5); |
| 1120 |
static_assert(Value == 8, ""); |
| 1121 |
|
| 1122 |
/// Ignored MemberExprs need to still evaluate the Base |
| 1123 |
/// expr. |
| 1124 |
constexpr A callme(int &i) { |
| 1125 |
++i; |
| 1126 |
return A{}; |
| 1127 |
} |
| 1128 |
constexpr int ignoredMemberExpr() { |
| 1129 |
int i = 0; |
| 1130 |
callme(i).a; |
| 1131 |
return i; |
| 1132 |
} |
| 1133 |
static_assert(ignoredMemberExpr() == 1, ""); |
| 1134 |
|
| 1135 |
template <int I> |
| 1136 |
constexpr int foo() { |
| 1137 |
I; |
| 1138 |
return I; |
| 1139 |
} |
| 1140 |
static_assert(foo<3>() == 3, ""); |
| 1141 |
|
| 1142 |
struct ATemp { |
| 1143 |
consteval ATemp ret_a() const { return ATemp{}; } |
| 1144 |
}; |
| 1145 |
|
| 1146 |
void test() { |
| 1147 |
int k = (ATemp().ret_a(), 0); |
| 1148 |
} |
| 1149 |
|
| 1150 |
#pragma clang diagnostic pop |
| 1151 |
} |
| 1152 |
#endif |
| 1153 |
|
| 1154 |
namespace PredefinedExprs { |
| 1155 |
#if __cplusplus >= 201402L |
| 1156 |
template<typename CharT> |
| 1157 |
constexpr bool strings_match(const CharT *str1, const CharT *str2) { |
| 1158 |
while (*str1 && *str2) { |
| 1159 |
if (*str1++ != *str2++) |
| 1160 |
return false; |
| 1161 |
}; |
| 1162 |
|
| 1163 |
return *str1 == *str2; |
| 1164 |
} |
| 1165 |
|
| 1166 |
void foo() { |
| 1167 |
static_assert(strings_match(__FUNCSIG__, "void __cdecl PredefinedExprs::foo(void)"), ""); |
| 1168 |
static_assert(strings_match(L__FUNCSIG__, L"void __cdecl PredefinedExprs::foo(void)"), ""); |
| 1169 |
static_assert(strings_match(L__FUNCTION__, L"foo"), ""); |
| 1170 |
static_assert(strings_match(__FUNCTION__, "foo"), ""); |
| 1171 |
static_assert(strings_match(__func__, "foo"), ""); |
| 1172 |
static_assert(strings_match(__PRETTY_FUNCTION__, "void PredefinedExprs::foo()"), ""); |
| 1173 |
} |
| 1174 |
|
| 1175 |
constexpr char heh(unsigned index) { |
| 1176 |
__FUNCTION__; // both-warning {{result unused}} |
| 1177 |
__extension__ __FUNCTION__; // both-warning {{result unused}} |
| 1178 |
return __FUNCTION__[index]; |
| 1179 |
} |
| 1180 |
static_assert(heh(0) == 'h', ""); |
| 1181 |
static_assert(heh(1) == 'e', ""); |
| 1182 |
static_assert(heh(2) == 'h', ""); |
| 1183 |
#endif |
| 1184 |
} |
| 1185 |
|
| 1186 |
namespace NE { |
| 1187 |
constexpr int foo() noexcept { |
| 1188 |
return 1; |
| 1189 |
} |
| 1190 |
static_assert(noexcept(foo()), ""); |
| 1191 |
constexpr int foo2() { |
| 1192 |
return 1; |
| 1193 |
} |
| 1194 |
static_assert(!noexcept(foo2()), ""); |
| 1195 |
|
| 1196 |
#if __cplusplus > 201402L |
| 1197 |
constexpr int a() { |
| 1198 |
int b = 0; |
| 1199 |
(void)noexcept(++b); // both-warning {{expression with side effects has no effect in an unevaluated context}} |
| 1200 |
|
| 1201 |
return b; |
| 1202 |
} |
| 1203 |
static_assert(a() == 0, ""); |
| 1204 |
#endif |
| 1205 |
} |
| 1206 |
|
| 1207 |
namespace PointerCasts { |
| 1208 |
constexpr int M = 10; |
| 1209 |
constexpr const int *P = &M; |
| 1210 |
constexpr intptr_t A = (intptr_t)P; // both-error {{must be initialized by a constant expression}} \ |
| 1211 |
// both-note {{cast that performs the conversions of a reinterpret_cast}} |
| 1212 |
|
| 1213 |
int array[(intptr_t)(char*)0]; // both-warning {{variable length array folded to constant array}} |
| 1214 |
} |
| 1215 |
|
| 1216 |
namespace InvalidDeclRefs { |
| 1217 |
bool b00; // both-note {{declared here}} |
| 1218 |
static_assert(b00, ""); // both-error {{not an integral constant expression}} \ |
| 1219 |
// both-note {{read of non-const variable}} |
| 1220 |
|
| 1221 |
float b01; // both-note {{declared here}} |
| 1222 |
static_assert(b01, ""); // both-error {{not an integral constant expression}} \ |
| 1223 |
// both-note {{read of non-constexpr variable}} |
| 1224 |
|
| 1225 |
extern const int b02; // both-note {{declared here}} |
| 1226 |
static_assert(b02, ""); // both-error {{not an integral constant expression}} \ |
| 1227 |
// both-note {{initializer of 'b02' is unknown}} |
| 1228 |
|
| 1229 |
int b03 = 3; // both-note {{declared here}} |
| 1230 |
static_assert(b03, ""); // both-error {{not an integral constant expression}} \ |
| 1231 |
// both-note {{read of non-const variable}} |
| 1232 |
|
| 1233 |
extern int var; |
| 1234 |
constexpr int *varp = &var; // Ok. |
| 1235 |
} |
| 1236 |
|
| 1237 |
namespace NonConstReads { |
| 1238 |
void *p = nullptr; // both-note {{declared here}} |
| 1239 |
static_assert(!p, ""); // both-error {{not an integral constant expression}} \ |
| 1240 |
// both-note {{read of non-constexpr variable 'p'}} |
| 1241 |
|
| 1242 |
int arr[!p]; // both-error {{variable length array}} |
| 1243 |
|
| 1244 |
int z; // both-note {{declared here}} |
| 1245 |
static_assert(z == 0, ""); // both-error {{not an integral constant expression}} \ |
| 1246 |
// both-note {{read of non-const variable 'z'}} |
| 1247 |
} |
| 1248 |
|
| 1249 |
/// This test passes a MaterializedTemporaryExpr to evaluateAsRValue. |
| 1250 |
/// That needs to return a null pointer after the lvalue-to-rvalue conversion. |
| 1251 |
/// We used to fail to do that. |
| 1252 |
namespace rdar8769025 { |
| 1253 |
__attribute__((nonnull)) void f1(int * const &p); |
| 1254 |
void test_f1() { |
| 1255 |
f1(0); // both-warning{{null passed to a callee that requires a non-null argument}} |
| 1256 |
} |
| 1257 |
} |
| 1258 |
|
| 1259 |
namespace nullptrsub { |
| 1260 |
void a() { |
| 1261 |
char *f = (char *)0; |
| 1262 |
f = (char *)((char *)0 - (char *)0); |
| 1263 |
} |
| 1264 |
} |
| 1265 |
|
| 1266 |
namespace incdecbool { |
| 1267 |
#if __cplusplus >= 201402L |
| 1268 |
constexpr bool incb(bool c) { |
| 1269 |
if (!c) |
| 1270 |
++c; |
| 1271 |
else {++c; c++; } |
| 1272 |
#if __cplusplus >= 202002L |
| 1273 |
// both-error@-3 {{ISO C++17 does not allow incrementing expression of type bool}} |
| 1274 |
// both-error@-3 2{{ISO C++17 does not allow incrementing expression of type bool}} |
| 1275 |
#else |
| 1276 |
// both-warning@-6 {{incrementing expression of type bool is deprecated and incompatible with C++17}} |
| 1277 |
#endif |
| 1278 |
return c; |
| 1279 |
} |
| 1280 |
static_assert(incb(false), ""); |
| 1281 |
static_assert(incb(true), ""); |
| 1282 |
static_assert(incb(true) == 1, ""); |
| 1283 |
#endif |
| 1284 |
|
| 1285 |
|
| 1286 |
#if __cplusplus == 201103L |
| 1287 |
constexpr bool foo() { // both-error {{never produces a constant expression}} |
| 1288 |
bool b = true; // both-warning {{variable declaration in a constexpr function is a C++14 extension}} |
| 1289 |
b++; // both-warning {{incrementing expression of type bool is deprecated and incompatible with C++17}} \ |
| 1290 |
// both-warning {{use of this statement in a constexpr function is a C++14 extension}} \ |
| 1291 |
// both-note 2{{subexpression not valid in a constant expression}} |
| 1292 |
|
| 1293 |
return b; |
| 1294 |
} |
| 1295 |
static_assert(foo() == 1, ""); // both-error {{not an integral constant expression}} \ |
| 1296 |
// both-note {{in call to}} |
| 1297 |
#endif |
| 1298 |
|
| 1299 |
|
| 1300 |
|
| 1301 |
} |
| 1302 |
|
| 1303 |
#if __cplusplus >= 201402L |
| 1304 |
constexpr int externvar1() { // both-error {{never produces a constant expression}} |
| 1305 |
extern char arr[]; // both-note {{declared here}} |
| 1306 |
return arr[0]; // both-note {{read of non-constexpr variable 'arr'}} |
| 1307 |
} |
| 1308 |
namespace externarr { |
| 1309 |
extern int arr[]; |
| 1310 |
constexpr int *externarrindex = &arr[0]; /// No diagnostic. |
| 1311 |
} |
| 1312 |
|
| 1313 |
|
| 1314 |
namespace StmtExprs { |
| 1315 |
constexpr int foo() { |
| 1316 |
({ |
| 1317 |
int i; |
| 1318 |
for (i = 0; i < 76; i++) {} |
| 1319 |
i; // both-warning {{expression result unused}} |
| 1320 |
}); |
| 1321 |
return 76; |
| 1322 |
} |
| 1323 |
static_assert(foo() == 76, ""); |
| 1324 |
|
| 1325 |
namespace CrossFuncLabelDiff { |
| 1326 |
constexpr long a(bool x) { return x ? 0 : (intptr_t)&&lbl + (0 && ({lbl: 0;})); } |
| 1327 |
} |
| 1328 |
|
| 1329 |
/// GCC agrees with the bytecode interpreter here. |
| 1330 |
void switchInSE() { |
| 1331 |
static_assert(({ // ref-error {{not an integral constant expression}} |
| 1332 |
int i = 20; |
| 1333 |
switch(10) { |
| 1334 |
case 10: i = 300; // ref-note {{a constant expression cannot modify an object that is visible outside that expression}} |
| 1335 |
} |
| 1336 |
i; |
| 1337 |
}) == 300); |
| 1338 |
} |
| 1339 |
} |
| 1340 |
#endif |
| 1341 |
|
| 1342 |
namespace Extern { |
| 1343 |
constexpr extern char Oops = 1; |
| 1344 |
static_assert(Oops == 1, ""); |
| 1345 |
|
| 1346 |
#if __cplusplus >= 201402L |
| 1347 |
struct NonLiteral { |
| 1348 |
NonLiteral() {} |
| 1349 |
}; |
| 1350 |
NonLiteral nl; |
| 1351 |
constexpr NonLiteral &ExternNonLiteralVarDecl() { |
| 1352 |
extern NonLiteral nl; |
| 1353 |
return nl; |
| 1354 |
} |
| 1355 |
static_assert(&ExternNonLiteralVarDecl() == &nl, ""); |
| 1356 |
#endif |
| 1357 |
|
| 1358 |
struct A { |
| 1359 |
int b; |
| 1360 |
}; |
| 1361 |
|
| 1362 |
extern constexpr A a{12}; |
| 1363 |
static_assert(a.b == 12, ""); |
| 1364 |
} |
| 1365 |
|
| 1366 |
#if __cplusplus >= 201402L |
| 1367 |
constexpr int StmtExprEval() { |
| 1368 |
if (({ |
| 1369 |
while (0); |
| 1370 |
true; |
| 1371 |
})) { |
| 1372 |
return 2; |
| 1373 |
} |
| 1374 |
return 1; |
| 1375 |
} |
| 1376 |
static_assert(StmtExprEval() == 2, ""); |
| 1377 |
|
| 1378 |
constexpr int ReturnInStmtExpr() { // both-error {{never produces a constant expression}} |
| 1379 |
return ({ |
| 1380 |
return 1; // both-note 2{{this use of statement expressions is not supported in a constant expression}} |
| 1381 |
2; |
| 1382 |
}); |
| 1383 |
} |
| 1384 |
static_assert(ReturnInStmtExpr() == 1, ""); // both-error {{not an integral constant expression}} \ |
| 1385 |
// both-note {{in call to}} |
| 1386 |
|
| 1387 |
#endif |
| 1388 |
|
| 1389 |
namespace ComparisonAgainstOnePastEnd { |
| 1390 |
int a, b; |
| 1391 |
static_assert(&a + 1 == &b, ""); // both-error {{not an integral constant expression}} \ |
| 1392 |
// both-note {{comparison against pointer '&a + 1' that points past the end of a complete object has unspecified value}} |
| 1393 |
static_assert(&a == &b + 1, ""); // both-error {{not an integral constant expression}} \ |
| 1394 |
// both-note {{comparison against pointer '&b + 1' that points past the end of a complete object has unspecified value}} |
| 1395 |
|
| 1396 |
static_assert(&a + 1 == &b + 1, ""); // both-error {{static assertion failed}} |
| 1397 |
}; |
| 1398 |
|
| 1399 |
namespace NTTP { |
| 1400 |
template <typename _Tp, unsigned _Nm> |
| 1401 |
constexpr unsigned |
| 1402 |
size(const _Tp (&)[_Nm]) noexcept |
| 1403 |
{ return _Nm; } |
| 1404 |
|
| 1405 |
template <char C> |
| 1406 |
static int write_padding() { |
| 1407 |
static const char Chars[] = {C}; |
| 1408 |
|
| 1409 |
return size(Chars); |
| 1410 |
} |
| 1411 |
} |
| 1412 |
|
| 1413 |
#if __cplusplus >= 201402L |
| 1414 |
namespace UnaryOpError { |
| 1415 |
constexpr int foo() { |
| 1416 |
int f = 0; |
| 1417 |
++g; // both-error {{use of undeclared identifier 'g'}} \ |
| 1418 |
both-error {{cannot assign to variable 'g' with const-qualified type 'const int'}} \ |
| 1419 |
both-note@#g_decl {{'CompoundLiterals::g' declared here}} \ |
| 1420 |
both-note@#g_decl {{variable 'g' declared const here}} |
| 1421 |
return f; |
| 1422 |
} |
| 1423 |
} |
| 1424 |
#endif |
| 1425 |
|
| 1426 |
namespace VolatileReads { |
| 1427 |
const volatile int b = 1; |
| 1428 |
static_assert(b, ""); // both-error {{not an integral constant expression}} \ |
| 1429 |
// both-note {{read of volatile-qualified type 'const volatile int' is not allowed in a constant expression}} |
| 1430 |
|
| 1431 |
|
| 1432 |
constexpr int a = 12; |
| 1433 |
constexpr volatile int c = (volatile int&)a; // both-error {{must be initialized by a constant expression}} \ |
| 1434 |
// both-note {{read of volatile-qualified type 'volatile int'}} |
| 1435 |
|
| 1436 |
volatile constexpr int n1 = 0; // both-note {{here}} |
| 1437 |
volatile const int n2 = 0; // both-note {{here}} |
| 1438 |
constexpr int m1 = n1; // both-error {{constant expression}} \ |
| 1439 |
// both-note {{read of volatile-qualified type 'const volatile int'}} |
| 1440 |
constexpr int m2 = n2; // both-error {{constant expression}} \ |
| 1441 |
// both-note {{read of volatile-qualified type 'const volatile int'}} |
| 1442 |
constexpr int m1b = const_cast<const int&>(n1); // both-error {{constant expression}} \ |
| 1443 |
// both-note {{read of volatile object 'n1'}} |
| 1444 |
constexpr int m2b = const_cast<const int&>(n2); // both-error {{constant expression}} \ |
| 1445 |
// both-note {{read of volatile object 'n2'}} |
| 1446 |
|
| 1447 |
struct S { |
| 1448 |
constexpr S(int=0) : i(1) {} |
| 1449 |
int i; |
| 1450 |
}; |
| 1451 |
constexpr volatile S vs; // both-note {{here}} |
| 1452 |
static_assert(const_cast<int&>(vs.i), ""); // both-error {{constant expression}} \ |
| 1453 |
// both-note {{read of volatile object 'vs'}} |
| 1454 |
} |
| 1455 |
#if __cplusplus >= 201703L |
| 1456 |
namespace { |
| 1457 |
struct C { |
| 1458 |
int x; |
| 1459 |
}; |
| 1460 |
|
| 1461 |
template <const C *p> void f() { |
| 1462 |
const auto &[c] = *p; |
| 1463 |
&c; // both-warning {{expression result unused}} |
| 1464 |
} |
| 1465 |
} |
| 1466 |
#endif |
| 1467 |
|
| 1468 |
void localConstexpr() { |
| 1469 |
constexpr int a = 1/0; // both-error {{must be initialized by a constant expression}} \ |
| 1470 |
// both-note {{division by zero}} \ |
| 1471 |
// both-warning {{division by zero is undefined}} \ |
| 1472 |
// both-note {{declared here}} |
| 1473 |
static_assert(a == 0, ""); // both-error {{not an integral constant expression}} \ |
| 1474 |
// both-note {{initializer of 'a' is not a constant expression}} |
| 1475 |
} |
| 1476 |
|
| 1477 |
namespace Foo { |
| 1478 |
namespace Bar { |
| 1479 |
constexpr int FB = 10; |
| 1480 |
} |
| 1481 |
} |
| 1482 |
constexpr int usingDirectiveDecl() { |
| 1483 |
using namespace Foo::Bar; |
| 1484 |
return FB; |
| 1485 |
} |
| 1486 |
static_assert(usingDirectiveDecl() == 10, ""); |
| 1487 |
|
| 1488 |
namespace OnePastEndCmp { |
| 1489 |
struct S { |
| 1490 |
int a; |
| 1491 |
int b; |
| 1492 |
}; |
| 1493 |
|
| 1494 |
constexpr S s{12,13}; |
| 1495 |
constexpr const int *p = &s.a; |
| 1496 |
constexpr const int *q = &s.a + 1; |
| 1497 |
static_assert(p != q, ""); |
| 1498 |
} |
| 1499 |
|
| 1500 |
namespace ExternRedecl { |
| 1501 |
extern const int a; |
| 1502 |
constexpr const int *p = &a; |
| 1503 |
constexpr int a = 10; |
| 1504 |
static_assert(*p == 10, ""); |
| 1505 |
} |