[clang][dataflow] Handle `for` statements without conditions
authorStanislav Gatev <sgatev@google.com>
Wed, 29 Jun 2022 15:56:53 +0000 (15:56 +0000)
committerStanislav Gatev <sgatev@google.com>
Thu, 30 Jun 2022 07:00:35 +0000 (07:00 +0000)
Handle `for` statements without conditions.

Differential Revision: https://reviews.llvm.org/D128833

Reviewed-by: xazax.hun, gribozavr2, li.zhe.hua
clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp
clang/unittests/Analysis/FlowSensitive/TransferTest.cpp

index 4a35c39..6443fc1 100644 (file)
@@ -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) {
index a0b753f..021f8a2 100644 (file)
@@ -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<std::string, DataflowAnalysisState<NoopLattice>>>
+                   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<BoolValue>(LoopBodyEnv.getValue(*FooDecl, SkipPast::None));
+        EXPECT_FALSE(LoopBodyEnv.flowConditionImplies(LoopBodyFooVal));
+      });
+}
+
 } // namespace