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()) {
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()) {
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits