[PATCH] D74374: [clang-tidy] Added check to disable bugprone-infinite-loop on known false condition

Nathan James via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 11 11:37:46 PST 2020


This revision was automatically updated to reflect the committed changes.
Closed by commit rGc69ec6476806: [clang-tidy] Added check to disable bugprone-infinite-loop on known falseā€¦ (authored by njames93).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D74374/new/

https://reviews.llvm.org/D74374

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
@@ -354,3 +354,12 @@
     (*p)++;
   } while (i < Limit);
 }
+
+void evaluatable(bool CondVar) {
+  for (; false && CondVar;) {
+  }
+  while (false && CondVar) {
+  }
+  do {
+  } while (false && CondVar);
+}
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
@@ -152,6 +152,13 @@
   return Result;
 }
 
+static bool isKnownFalse(const Expr &Cond, const ASTContext &Ctx) {
+  bool Result = false;
+  if (Cond.EvaluateAsBooleanCondition(Result, Ctx))
+    return !Result;
+  return false;
+}
+
 void InfiniteLoopCheck::registerMatchers(MatchFinder *Finder) {
   const auto LoopCondition = allOf(
       hasCondition(
@@ -170,6 +177,9 @@
   const auto *LoopStmt = Result.Nodes.getNodeAs<Stmt>("loop-stmt");
   const auto *Func = Result.Nodes.getNodeAs<FunctionDecl>("func");
 
+  if (isKnownFalse(*Cond, *Result.Context))
+    return;
+
   bool ShouldHaveConditionVariables = true;
   if (const auto *While = dyn_cast<WhileStmt>(LoopStmt)) {
     if (const VarDecl *LoopVarDecl = While->getConditionVariable()) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D74374.243938.patch
Type: text/x-patch
Size: 1517 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200211/33a52568/attachment.bin>


More information about the cfe-commits mailing list