[clang] [Clang] Implement P2809: Trivial infinite loops are not Undefined Behavior (PR #90066)

Hubert Tong via cfe-commits cfe-commits at lists.llvm.org
Sat May 4 19:27:23 PDT 2024


================
@@ -908,6 +908,69 @@ void CodeGenFunction::EmitIfStmt(const IfStmt &S) {
     incrementProfileCounter(&S);
 }
 
+bool CodeGenFunction::checkIfLoopMustProgress(const Expr *ControllingExpression,
+                                              bool HasEmptyBody) {
+  if (CGM.getCodeGenOpts().getFiniteLoops() ==
+      CodeGenOptions::FiniteLoopsKind::Never)
+    return false;
+
+  // Now apply rules for plain C (see  6.8.5.6 in C11).
+  // Loops with constant conditions do not have to make progress in any C
+  // version.
+  // As an extension, we consisider loops whose constant expression
+  // can be constant-folded.
+  Expr::EvalResult Result;
+  bool CondIsConstInt =
----------------
hubert-reinterpretcast wrote:

> As a standards-wording issue, the standard has to do something here

This was extensively discussed in CWG. The wording makes the evaluation for the purposes of determining whether the loop is trivially infinite a manifestly constant-evaluated context. It leaves the normal evaluation as not a manifestly constant-evaluated context.

The operative words are "interpreted as a _constant-expression_" in https://eel.is/c++draft/stmt.iter.general#3.

https://github.com/llvm/llvm-project/pull/90066


More information about the cfe-commits mailing list