[PATCH] D73270: [clan-tidy] Fix false positive in bugprone-infinite-loop

Balogh, Ádám via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Jan 23 06:12:33 PST 2020


baloghadamsoftware created this revision.
baloghadamsoftware added a reviewer: gribozavr2.
baloghadamsoftware added a project: clang-tools-extra.
Herald added subscribers: mgehre, gamesh411, Szelethus, rnkovacs, whisperity.
Herald added a project: clang.

The checker `bugprone-infinite-loop` does not track changes of variables in the initialization expression of a variable declared inside the condition of the `while` statement. This leads to false positives, similarly to the one in the bug report 44618 <https://bugs.llvm.org/show_bug.cgi?id=44618>. This patch fixes this issue by enabling tracking of the variables of this expression as well.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D73270

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
@@ -8,6 +8,11 @@
     j++;
   }
 
+  while (int k = 10) {
+    // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: this loop is infinite; none of its condition variables (k) are updated in the loop body [bugprone-infinite-loop]
+    j--;
+  }
+
   do {
   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: this loop is infinite; none of its condition variables (i) are updated in the loop body [bugprone-infinite-loop]
     j++;
@@ -24,6 +29,11 @@
   int Limit = 10;
   while (i < Limit) {
     // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: this loop is infinite; none of its condition variables (i, Limit) are updated in the loop body [bugprone-infinite-loop]
+    j--;
+  }
+
+  while (int k = Limit) {
+    // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: this loop is infinite; none of its condition variables (k, Limit) are updated in the loop body [bugprone-infinite-loop]
     j++;
   }
 
@@ -44,6 +54,12 @@
     // Not an error since 'Limit' is updated.
     Limit--;
   }
+
+  while (int k = Limit) {
+    // Not an error since 'Limit' is updated.
+    Limit--;
+  }
+
   do {
     Limit--;
   } while (i < Limit);
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
@@ -129,6 +129,7 @@
     if (isAtLeastOneCondVarChanged(Func, LoopStmt, Child, Context))
       return true;
   }
+
   return false;
 }
 
@@ -149,6 +150,7 @@
       Result += ", ";
     Result += NewNames;
   }
+
   return Result;
 }
 
@@ -173,10 +175,32 @@
   if (isAtLeastOneCondVarChanged(Func, LoopStmt, Cond, Result.Context))
     return;
 
+  if (const auto *While = dyn_cast<WhileStmt>(LoopStmt)) {
+    if (const auto *LoopVarDecl = While->getConditionVariable()) {
+      if (const Expr *Init = LoopVarDecl->getInit()) {
+        if (isAtLeastOneCondVarChanged(Func, LoopStmt, Init,
+                                       Result.Context))
+          return;
+      }
+    }
+  }
+
   std::string CondVarNames = getCondVarNames(Cond);
+
   if (CondVarNames.empty())
     return;
 
+  if (const auto *While = dyn_cast<WhileStmt>(LoopStmt)) {
+    if (const auto *LoopVarDecl = While->getConditionVariable()) {
+      if (const Expr *Init = LoopVarDecl->getInit()) {
+        std::string AdditionalVarNames = getCondVarNames(Init);
+        if (!AdditionalVarNames.empty()) {
+          CondVarNames += ", " + AdditionalVarNames;
+        }
+      }
+    }
+  }
+
   diag(LoopStmt->getBeginLoc(),
        "this loop is infinite; none of its condition variables (%0)"
        " are updated in the loop body")


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D73270.239876.patch
Type: text/x-patch
Size: 2974 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200123/636c047c/attachment-0001.bin>


More information about the cfe-commits mailing list