baloghadamsoftware updated this revision to Diff 239927. baloghadamsoftware added a comment.
Updated according to the comments. CHANGES SINCE LAST ACTION https://reviews.llvm.org/D73270/new/ 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++; @@ -27,6 +32,11 @@ 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--; + } + do { // 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++; @@ -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 @@ -173,10 +173,30 @@ if (isAtLeastOneCondVarChanged(Func, LoopStmt, Cond, Result.Context)) return; + if (const auto *While = dyn_cast<WhileStmt>(LoopStmt)) { + if (const VarDecl *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 VarDecl *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")
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++; @@ -27,6 +32,11 @@ 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--; + } + do { // 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++; @@ -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 @@ -173,10 +173,30 @@ if (isAtLeastOneCondVarChanged(Func, LoopStmt, Cond, Result.Context)) return; + if (const auto *While = dyn_cast<WhileStmt>(LoopStmt)) { + if (const VarDecl *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 VarDecl *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")
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits