| 1 |
// P2809R3 - Trivial infinite loops are not Undefined Behavior |
| 2 |
// { dg-do compile { target c++11 } } |
| 3 |
// { dg-additional-options "-fdump-tree-gimple -fno-inline -Wtautological-compare -O2" } |
| 4 |
// { dg-final { scan-tree-dump-times ".ANNOTATE \\\(\[^\n\r]*, 5, 0\\\)" 32 "gimple" { target c++20 } } } |
| 5 |
// { dg-final { scan-tree-dump-times ".ANNOTATE \\\(\[^\n\r]*, 5, 0\\\)" 16 "gimple" { target c++17_down } } } |
| 6 |
|
| 7 |
volatile int v; |
| 8 |
|
| 9 |
constexpr bool |
| 10 |
foo () |
| 11 |
{ |
| 12 |
return true; |
| 13 |
} |
| 14 |
|
| 15 |
struct S |
| 16 |
{ |
| 17 |
constexpr S () : s (true) {} |
| 18 |
constexpr operator bool () const { return s; } |
| 19 |
bool s; |
| 20 |
}; |
| 21 |
|
| 22 |
#if __cplusplus >= 202002L |
| 23 |
namespace std { |
| 24 |
constexpr inline bool |
| 25 |
is_constant_evaluated () noexcept |
| 26 |
{ |
| 27 |
#if __cpp_if_consteval >= 202106L |
| 28 |
if consteval { return true; } else { return false; } |
| 29 |
#else |
| 30 |
return __builtin_is_constant_evaluated (); |
| 31 |
#endif |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
constexpr bool |
| 36 |
baz () |
| 37 |
{ |
| 38 |
return std::is_constant_evaluated (); |
| 39 |
} |
| 40 |
#endif |
| 41 |
|
| 42 |
void |
| 43 |
bar (int x) |
| 44 |
{ |
| 45 |
switch (x) |
| 46 |
{ |
| 47 |
case 0: |
| 48 |
while (foo ()) ; |
| 49 |
break; |
| 50 |
case 1: |
| 51 |
while (foo ()) {} |
| 52 |
break; |
| 53 |
case 2: |
| 54 |
do ; while (foo ()); |
| 55 |
break; |
| 56 |
case 3: |
| 57 |
do {} while (foo ()); |
| 58 |
break; |
| 59 |
case 4: |
| 60 |
for (v = 42; foo (); ) ; |
| 61 |
break; |
| 62 |
case 5: |
| 63 |
for (v = 42; foo (); ) {} |
| 64 |
break; |
| 65 |
case 6: |
| 66 |
for (int w = 42; foo (); ) ; |
| 67 |
break; |
| 68 |
case 7: |
| 69 |
for (int w = 42; foo (); ) {} |
| 70 |
break; |
| 71 |
case 10: |
| 72 |
while (S {}) ; |
| 73 |
break; |
| 74 |
case 11: |
| 75 |
while (S {}) {} |
| 76 |
break; |
| 77 |
case 12: |
| 78 |
do ; while (S {}); |
| 79 |
break; |
| 80 |
case 13: |
| 81 |
do {} while (S {}); |
| 82 |
break; |
| 83 |
case 14: |
| 84 |
for (v = 42; S {}; ) ; |
| 85 |
break; |
| 86 |
case 15: |
| 87 |
for (v = 42; S {}; ) {} |
| 88 |
break; |
| 89 |
case 16: |
| 90 |
for (int w = 42; S {}; ) ; |
| 91 |
break; |
| 92 |
case 17: |
| 93 |
for (int w = 42; S {}; ) {} |
| 94 |
break; |
| 95 |
#if __cplusplus >= 202002L |
| 96 |
case 20: |
| 97 |
while (baz ()) ; |
| 98 |
break; |
| 99 |
case 21: |
| 100 |
while (baz ()) {} |
| 101 |
break; |
| 102 |
case 22: |
| 103 |
do ; while (baz ()); |
| 104 |
break; |
| 105 |
case 23: |
| 106 |
do {} while (baz ()); |
| 107 |
break; |
| 108 |
case 24: |
| 109 |
for (v = 42; baz (); ) ; |
| 110 |
break; |
| 111 |
case 25: |
| 112 |
for (v = 42; baz (); ) {} |
| 113 |
break; |
| 114 |
case 26: |
| 115 |
for (int w = 42; baz (); ) ; |
| 116 |
break; |
| 117 |
case 27: |
| 118 |
for (int w = 42; baz (); ) {} |
| 119 |
break; |
| 120 |
case 30: |
| 121 |
while (std::is_constant_evaluated ()) ; // { dg-warning "'std::is_constant_evaluated' evaluates to true when checking if trivially empty iteration statement is trivial infinite loop" "" { target c++20 } } |
| 122 |
break; // { dg-message "and evaluates to false when actually evaluating the condition in non-'constexpr' function" "" { target c++20 } .-1 } |
| 123 |
case 31: |
| 124 |
while (std::is_constant_evaluated ()) {} // { dg-warning "'std::is_constant_evaluated' evaluates to true when checking if trivially empty iteration statement is trivial infinite loop" "" { target c++20 } } |
| 125 |
break; // { dg-message "and evaluates to false when actually evaluating the condition in non-'constexpr' function" "" { target c++20 } .-1 } |
| 126 |
case 32: |
| 127 |
do ; while (std::is_constant_evaluated ()); // { dg-warning "'std::is_constant_evaluated' evaluates to true when checking if trivially empty iteration statement is trivial infinite loop" "" { target c++20 } } |
| 128 |
break; // { dg-message "and evaluates to false when actually evaluating the condition in non-'constexpr' function" "" { target c++20 } .-1 } |
| 129 |
case 33: |
| 130 |
do {} while (std::is_constant_evaluated ()); // { dg-warning "'std::is_constant_evaluated' evaluates to true when checking if trivially empty iteration statement is trivial infinite loop" "" { target c++20 } } |
| 131 |
break; // { dg-message "and evaluates to false when actually evaluating the condition in non-'constexpr' function" "" { target c++20 } .-1 } |
| 132 |
case 34: |
| 133 |
for (v = 42; std::is_constant_evaluated (); ) ; // { dg-warning "'std::is_constant_evaluated' evaluates to true when checking if trivially empty iteration statement is trivial infinite loop" "" { target c++20 } } |
| 134 |
break; // { dg-message "and evaluates to false when actually evaluating the condition in non-'constexpr' function" "" { target c++20 } .-1 } |
| 135 |
case 35: |
| 136 |
for (v = 42; std::is_constant_evaluated (); ) {} // { dg-warning "'std::is_constant_evaluated' evaluates to true when checking if trivially empty iteration statement is trivial infinite loop" "" { target c++20 } } |
| 137 |
break; // { dg-message "and evaluates to false when actually evaluating the condition in non-'constexpr' function" "" { target c++20 } .-1 } |
| 138 |
case 36: |
| 139 |
for (int w = 42; std::is_constant_evaluated (); ) ; // { dg-warning "'std::is_constant_evaluated' evaluates to true when checking if trivially empty iteration statement is trivial infinite loop" "" { target c++20 } } |
| 140 |
break; // { dg-message "and evaluates to false when actually evaluating the condition in non-'constexpr' function" "" { target c++20 } .-1 } |
| 141 |
case 37: |
| 142 |
for (int w = 42; std::is_constant_evaluated (); ) {} // { dg-warning "'std::is_constant_evaluated' evaluates to true when checking if trivially empty iteration statement is trivial infinite loop" "" { target c++20 } } |
| 143 |
break; // { dg-message "and evaluates to false when actually evaluating the condition in non-'constexpr' function" "" { target c++20 } .-1 } |
| 144 |
#endif |
| 145 |
default: |
| 146 |
break; |
| 147 |
} |
| 148 |
} |