xref: /llvm-project/clang/test/SemaCXX/warn-constant-evaluated-constexpr.cpp (revision 5143a1241362616840af826d18c067025dae1111)
1 // RUN: %clang_cc1 -std=c++2a -fsyntax-only -verify %s
2 // RUN: %clang_cc1 -std=c++2a -fsyntax-only -verify %s -fexperimental-new-constant-interpreter
3 
4 namespace std {
is_constant_evaluated()5 constexpr bool is_constant_evaluated() noexcept {
6   return __builtin_is_constant_evaluated();
7 }
8 } // namespace std
9 
fn1()10 constexpr int fn1() {
11   if constexpr (std::is_constant_evaluated()) // expected-warning {{'std::is_constant_evaluated' will always evaluate to 'true' in a manifestly constant-evaluated expression}}
12     return 0;
13   else
14     return 1;
15 }
16 
fn2()17 constexpr int fn2() {
18   if constexpr (!std::is_constant_evaluated()) // expected-warning {{'std::is_constant_evaluated' will always evaluate to 'true' in a manifestly constant-evaluated expression}}
19     return 0;
20   else
21     return 1;
22 }
23 
fn3()24 constexpr int fn3() {
25   if constexpr (std::is_constant_evaluated() == false) // expected-warning {{'std::is_constant_evaluated' will always evaluate to 'true' in a manifestly constant-evaluated expression}}
26     return 0;
27   else
28     return 1;
29 }
30 
fn4()31 constexpr int fn4() {
32   if constexpr (__builtin_is_constant_evaluated() == true) // expected-warning {{'__builtin_is_constant_evaluated' will always evaluate to 'true' in a manifestly constant-evaluated expression}}
33     return 0;
34   else
35     return 1;
36 }
37 
fn5()38 constexpr int fn5() {
39   if constexpr (__builtin_is_constant_evaluated()) // expected-warning {{'__builtin_is_constant_evaluated' will always evaluate to 'true' in a manifestly constant-evaluated expression}}
40     return 0;
41   else
42     return 1;
43 }
44 
nowarn1()45 constexpr int nowarn1() {
46   if (std::is_constant_evaluated())
47     return 0;
48   else
49     return 1;
50 }
51 
nowarn2()52 constexpr int nowarn2() {
53   if (!__builtin_is_constant_evaluated())
54     return 0;
55   else
56     return 1;
57 }
58