[PATCH] D128848: Fix assertion when analyzing a for-loop with no condition expression
Eric Li via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Wed Jun 29 12:47:16 PDT 2022
li.zhe.hua created this revision.
li.zhe.hua added a reviewer: ymandel.
Herald added a project: All.
li.zhe.hua requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
For loops are allowed to have an empty conditional expression; we
should not assert that it must have one.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D128848
Files:
clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp
clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
Index: clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
===================================================================
--- clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
+++ clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
@@ -10,7 +10,6 @@
#include "TestingSupport.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/Decl.h"
-#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/Analysis/FlowSensitive/DataflowEnvironment.h"
#include "clang/Analysis/FlowSensitive/StorageLocation.h"
@@ -3827,4 +3826,42 @@
});
}
+TEST_F(TransferTest, ForStmtEmptyBranchExtendsFlowCondition) {
+ std::string Code = R"cc(
+ void target(bool Foo) {
+ for (;;) {
+ if (!Foo) {
+ break;
+ }
+ (void)0;
+ // [[loop_body]]
+ }
+ (void)0;
+ // [[after_loop]]
+ }
+ )cc";
+ runDataflow(
+ Code, [](llvm::ArrayRef<
+ std::pair<std::string, DataflowAnalysisState<NoopLattice>>>
+ Results,
+ ASTContext &ASTCtx) {
+ ASSERT_THAT(Results,
+ ElementsAre(Pair("after_loop", _), Pair("loop_body", _)));
+ const Environment &LoopBodyEnv = Results[1].second.Env;
+ const Environment &AfterLoopEnv = Results[0].second.Env;
+
+ const ValueDecl *FooDecl = findValueDecl(ASTCtx, "Foo");
+ ASSERT_THAT(FooDecl, NotNull());
+
+ BoolValue &LoopBodyFooVal =
+ *cast<BoolValue>(LoopBodyEnv.getValue(*FooDecl, SkipPast::None));
+ EXPECT_TRUE(LoopBodyEnv.flowConditionImplies(LoopBodyFooVal));
+
+ BoolValue &AfterLoopFooVal =
+ *cast<BoolValue>(AfterLoopEnv.getValue(*FooDecl, SkipPast::None));
+ EXPECT_TRUE(AfterLoopEnv.flowConditionImplies(
+ AfterLoopEnv.makeNot(AfterLoopFooVal)));
+ });
+}
+
} // namespace
Index: clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp
===================================================================
--- clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp
+++ clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp
@@ -97,7 +97,8 @@
void VisitForStmt(const ForStmt *S) {
auto *Cond = S->getCond();
- assert(Cond != nullptr);
+ if (Cond == nullptr)
+ return;
extendFlowCondition(*Cond);
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D128848.441133.patch
Type: text/x-patch
Size: 2390 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220629/9f82b1f6/attachment.bin>
More information about the cfe-commits
mailing list