From: Stanislav Gatev Date: Wed, 29 Jun 2022 15:56:53 +0000 (+0000) Subject: [clang][dataflow] Handle `for` statements without conditions X-Git-Tag: upstream/15.0.7~3061 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=8207c2a660303272ad8ecb1807407f029466ff49;p=platform%2Fupstream%2Fllvm.git [clang][dataflow] Handle `for` statements without conditions Handle `for` statements without conditions. Differential Revision: https://reviews.llvm.org/D128833 Reviewed-by: xazax.hun, gribozavr2, li.zhe.hua --- diff --git a/clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp b/clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp index 4a35c39..6443fc1 100644 --- a/clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp +++ b/clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp @@ -97,8 +97,8 @@ public: void VisitForStmt(const ForStmt *S) { auto *Cond = S->getCond(); - assert(Cond != nullptr); - extendFlowCondition(*Cond); + if (Cond != nullptr) + extendFlowCondition(*Cond); } void VisitBinaryOperator(const BinaryOperator *S) { diff --git a/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp b/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp index a0b753f..021f8a2 100644 --- a/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp +++ b/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp @@ -3827,4 +3827,30 @@ TEST_F(TransferTest, ForStmtBranchExtendsFlowCondition) { }); } +TEST_F(TransferTest, ForStmtBranchWithoutConditionDoesNotExtendFlowCondition) { + std::string Code = R"( + void target(bool Foo) { + for (;;) { + (void)0; + // [[loop_body]] + } + } + )"; + runDataflow( + Code, [](llvm::ArrayRef< + std::pair>> + Results, + ASTContext &ASTCtx) { + ASSERT_THAT(Results, ElementsAre(Pair("loop_body", _))); + const Environment &LoopBodyEnv = Results[0].second.Env; + + const ValueDecl *FooDecl = findValueDecl(ASTCtx, "Foo"); + ASSERT_THAT(FooDecl, NotNull()); + + BoolValue &LoopBodyFooVal = + *cast(LoopBodyEnv.getValue(*FooDecl, SkipPast::None)); + EXPECT_FALSE(LoopBodyEnv.flowConditionImplies(LoopBodyFooVal)); + }); +} + } // namespace