[PATCH] D128833: [clang][dataflow] Handle `for` statements without conditions

Stanislav Gatev via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Jun 29 08:59:29 PDT 2022


sgatev created this revision.
sgatev added reviewers: ymandel, xazax.hun, gribozavr2.
Herald added subscribers: martong, tschuett, rnkovacs.
Herald added a project: All.
sgatev requested review of this revision.
Herald added a project: clang.

Handle `for` statements without conditions.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D128833

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
@@ -3827,4 +3827,30 @@
       });
 }
 
+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
Index: clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp
===================================================================
--- clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp
+++ clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp
@@ -97,8 +97,8 @@
 
   void VisitForStmt(const ForStmt *S) {
     auto *Cond = S->getCond();
-    assert(Cond != nullptr);
-    extendFlowCondition(*Cond);
+    if (Cond != nullptr)
+      extendFlowCondition(*Cond);
   }
 
   void VisitBinaryOperator(const BinaryOperator *S) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D128833.441038.patch
Type: text/x-patch
Size: 1724 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220629/ebec04de/attachment.bin>


More information about the cfe-commits mailing list