Debian Codegraph @

gcc/testsuite/g++.dg/cpp26/trivial-infinite-loop3.C

raw
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" }
4 // { dg-final { scan-tree-dump-not ".ANNOTATE \\\(\[^\n\r]*, 5, 0\\\)" "gimple" } }
5
6 volatile int v;
7 int y;
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 ()) ++y;
49 break;
50 case 1:
51 while (foo ()) { ++y; }
52 break;
53 case 2:
54 do ++y; while (foo ());
55 break;
56 case 3:
57 do { ++y; } while (foo ());
58 break;
59 case 4:
60 for (v = 42; foo (); ) ++y;
61 break;
62 case 5:
63 for (v = 42; foo (); ) { ++y; }
64 break;
65 case 6:
66 for (int w = 42; foo (); ) ++y;
67 break;
68 case 7:
69 for (int w = 42; foo (); ) { ++y; }
70 break;
71 case 10:
72 while (S {}) ++y;
73 break;
74 case 11:
75 while (S {}) { ++y; }
76 break;
77 case 12:
78 do ++y; while (S {});
79 break;
80 case 13:
81 do { ++y; } while (S {});
82 break;
83 case 14:
84 for (v = 42; S {}; ) ++y;
85 break;
86 case 15:
87 for (v = 42; S {}; ) { ++y; }
88 break;
89 case 16:
90 for (int w = 42; S {}; ) ++y;
91 break;
92 case 17:
93 for (int w = 42; S {}; ) { ++y; }
94 break;
95 #if __cplusplus >= 202002L
96 case 20:
97 while (baz ()) ++y;
98 break;
99 case 21:
100 while (baz ()) { ++y; }
101 break;
102 case 22:
103 do ++y; while (baz ());
104 break;
105 case 23:
106 do { ++y; } while (baz ());
107 break;
108 case 24:
109 for (v = 42; baz (); ) ++y;
110 break;
111 case 25:
112 for (v = 42; baz (); ) { ++y; }
113 break;
114 case 26:
115 for (int w = 42; baz (); ) ++y;
116 break;
117 case 27:
118 for (int w = 42; baz (); ) { ++y; }
119 break;
120 case 30:
121 while (std::is_constant_evaluated ()) ++y; // { dg-warning "'std::is_constant_evaluated' always evaluates to false in a non-'constexpr' function" "" { target c++20 } }
122 break;
123 case 31:
124 while (std::is_constant_evaluated ()) { ++y; } // { dg-warning "'std::is_constant_evaluated' always evaluates to false in a non-'constexpr' function" "" { target c++20 } }
125 break;
126 case 32:
127 do ++y; while (std::is_constant_evaluated ()); // { dg-warning "'std::is_constant_evaluated' always evaluates to false in a non-'constexpr' function" "" { target c++20 } }
128 break;
129 case 33:
130 do { ++y; } while (std::is_constant_evaluated ()); // { dg-warning "'std::is_constant_evaluated' always evaluates to false in a non-'constexpr' function" "" { target c++20 } }
131 break;
132 case 34:
133 for (v = 42; std::is_constant_evaluated (); ) ++y; // { dg-warning "'std::is_constant_evaluated' always evaluates to false in a non-'constexpr' function" "" { target c++20 } }
134 break;
135 case 35:
136 for (v = 42; std::is_constant_evaluated (); ) { ++y; } // { dg-warning "'std::is_constant_evaluated' always evaluates to false in a non-'constexpr' function" "" { target c++20 } }
137 break;
138 case 36:
139 for (int w = 42; std::is_constant_evaluated (); ) ++y; // { dg-warning "'std::is_constant_evaluated' always evaluates to false in a non-'constexpr' function" "" { target c++20 } }
140 break;
141 case 37:
142 for (int w = 42; std::is_constant_evaluated (); ) { ++y; } // { dg-warning "'std::is_constant_evaluated' always evaluates to false in a non-'constexpr' function" "" { target c++20 } }
143 break;
144 #endif
145 default:
146 break;
147 }
148 }