[PATCH] D113499: [clang-tidy] Reduce false positives for `bugprone-infinite-loop` with dependent expressions

Fabian Wolff via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 9 10:44:03 PST 2021


fwolff created this revision.
fwolff added reviewers: alexfh, klimek, djasper.
fwolff added a project: clang-tools-extra.
Herald added subscribers: carlosgalvezp, xazax.hun.
fwolff requested review of this revision.
Herald added a subscriber: cfe-commits.

Fixes PR#52081 by attempting to detect some of the "obvious" cases where a loop condition is known to be false despite depending on template parameters.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D113499

Files:
  clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp
  clang-tools-extra/test/clang-tidy/checkers/bugprone-infinite-loop.cpp


Index: clang-tools-extra/test/clang-tidy/checkers/bugprone-infinite-loop.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/bugprone-infinite-loop.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/bugprone-infinite-loop.cpp
@@ -622,3 +622,31 @@
     // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: this loop is infinite; none of its condition variables (size) are updated in the loop body [bugprone-infinite-loop]
   }
 }
+
+template <typename T>
+int some_template_fn() { return 1; }
+
+template <typename T>
+void test_dependent_condition() {
+  const int error = some_template_fn<T>();
+  do {
+  } while (false && error == 0);
+
+  const int val = some_template_fn<T>();
+  for (; !(val == 0 || true);) {
+  }
+
+  const int val2 = some_template_fn<T>();
+  for (; !(val2 == 0 || false);) {
+    // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: this loop is infinite; none of its condition variables (val2) are updated in the loop body [bugprone-infinite-loop]
+  }
+
+  const int val3 = some_template_fn<T>();
+  do {
+    // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: this loop is infinite; none of its condition variables (val3) are updated in the loop body [bugprone-infinite-loop]
+  } while (1, (true) && val3 == 1);
+
+  const int val4 = some_template_fn<T>();
+  do {
+  } while (1, (false) && val4 == 1);
+}
Index: clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp
+++ clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp
@@ -117,12 +117,31 @@
   return Result;
 }
 
-static bool isKnownFalse(const Expr &Cond, const ASTContext &Ctx) {
-  if (Cond.isValueDependent())
+static bool isKnown(const Expr &Cond, const ASTContext &Ctx, const bool Value) {
+  if (Cond.isValueDependent()) {
+    if (const auto *BinOp = dyn_cast<BinaryOperator>(&Cond)) {
+      // Conjunctions (disjunctions) can still be handled if at least one
+      // conjunct (disjunct) is known to be false (true).
+      if (!Value && BinOp->getOpcode() == BO_LAnd)
+        return isKnown(*BinOp->getLHS(), Ctx, false) ||
+               isKnown(*BinOp->getRHS(), Ctx, false);
+      if (Value && BinOp->getOpcode() == BO_LOr)
+        return isKnown(*BinOp->getLHS(), Ctx, true) ||
+               isKnown(*BinOp->getRHS(), Ctx, true);
+      if (BinOp->getOpcode() == BO_Comma)
+        return isKnown(*BinOp->getRHS(), Ctx, Value);
+    } else if (const auto *UnOp = dyn_cast<UnaryOperator>(&Cond)) {
+      if (UnOp->getOpcode() == UO_LNot)
+        return isKnown(*UnOp->getSubExpr(), Ctx, !Value);
+    } else if (const auto *Paren = dyn_cast<ParenExpr>(&Cond))
+      return isKnown(*Paren->getSubExpr(), Ctx, Value);
+    else if (const auto *ImplCast = dyn_cast<ImplicitCastExpr>(&Cond))
+      return isKnown(*ImplCast->getSubExpr(), Ctx, Value);
     return false;
+  }
   bool Result = false;
   if (Cond.EvaluateAsBooleanCondition(Result, Ctx))
-    return !Result;
+    return Result == Value;
   return false;
 }
 
@@ -144,7 +163,7 @@
   const auto *LoopStmt = Result.Nodes.getNodeAs<Stmt>("loop-stmt");
   const auto *Func = Result.Nodes.getNodeAs<Decl>("func");
 
-  if (isKnownFalse(*Cond, *Result.Context))
+  if (isKnown(*Cond, *Result.Context, false))
     return;
 
   bool ShouldHaveConditionVariables = true;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D113499.385888.patch
Type: text/x-patch
Size: 3422 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20211109/2c860f04/attachment.bin>


More information about the cfe-commits mailing list