[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
Mon Feb 10 18:44:13 PST 2020
njames93 created this revision.
Herald added subscribers: cfe-commits, xazax.hun.
Herald added a project: clang.
njames93 edited the summary of this revision.
njames93 added reviewers: aaron.ballman, alexfh, hokein, gribozavr2, JonasToth.
njames93 added a project: clang-tools-extra.
Addresses bugprone-infinite-loop false positive with CATCH2 <https://bugs.llvm.org/show_bug.cgi?id=44816> by disabling the check on loops where the condition is known to always eval as false, in other words not a loop.
Repository:
rG LLVM Github Monorepo
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.243712.patch
Type: text/x-patch
Size: 1517 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200211/f0dd7eb2/attachment.bin>
More information about the cfe-commits
mailing list