Skip to content

Commit 7a136d2

Browse files
njames93zmodem
authored andcommitted
[clang-tidy] Added check to disable bugprone-infinite-loop on known false condition
Summary: Addresses [[ https://bugs.llvm.org/show_bug.cgi?id=44816 | bugprone-infinite-loop false positive with CATCH2 ]] by disabling the check on loops where the condition is known to always eval as false, in other words not a loop. Reviewers: aaron.ballman, alexfh, hokein, gribozavr2, JonasToth Reviewed By: gribozavr2 Subscribers: xazax.hun, cfe-commits Tags: #clang, #clang-tools-extra Differential Revision: https://reviews.llvm.org/D74374 (cherry picked from commit c69ec64)
1 parent b33830a commit 7a136d2

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,13 @@ static std::string getCondVarNames(const Stmt *Cond) {
152152
return Result;
153153
}
154154

155+
static bool isKnownFalse(const Expr &Cond, const ASTContext &Ctx) {
156+
bool Result = false;
157+
if (Cond.EvaluateAsBooleanCondition(Result, Ctx))
158+
return !Result;
159+
return false;
160+
}
161+
155162
void InfiniteLoopCheck::registerMatchers(MatchFinder *Finder) {
156163
const auto LoopCondition = allOf(
157164
hasCondition(
@@ -170,6 +177,9 @@ void InfiniteLoopCheck::check(const MatchFinder::MatchResult &Result) {
170177
const auto *LoopStmt = Result.Nodes.getNodeAs<Stmt>("loop-stmt");
171178
const auto *Func = Result.Nodes.getNodeAs<FunctionDecl>("func");
172179

180+
if (isKnownFalse(*Cond, *Result.Context))
181+
return;
182+
173183
bool ShouldHaveConditionVariables = true;
174184
if (const auto *While = dyn_cast<WhileStmt>(LoopStmt)) {
175185
if (const VarDecl *LoopVarDecl = While->getConditionVariable()) {

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,3 +354,12 @@ void lambda_capture() {
354354
(*p)++;
355355
} while (i < Limit);
356356
}
357+
358+
void evaluatable(bool CondVar) {
359+
for (; false && CondVar;) {
360+
}
361+
while (false && CondVar) {
362+
}
363+
do {
364+
} while (false && CondVar);
365+
}

0 commit comments

Comments
 (0)