Debian Codegraph @

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