[clang] 824b136 - [clang][dataflow] Support range-for loops in fixpoint algorithm.

Yitzhak Mandelbaum via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 28 10:36:06 PDT 2023


Author: Yitzhak Mandelbaum
Date: 2023-08-28T17:35:34Z
New Revision: 824b136591303a2bec62cc752ec89843fbbc0ca0

URL: https://github.com/llvm/llvm-project/commit/824b136591303a2bec62cc752ec89843fbbc0ca0
DIFF: https://github.com/llvm/llvm-project/commit/824b136591303a2bec62cc752ec89843fbbc0ca0.diff

LOG: [clang][dataflow] Support range-for loops in fixpoint algorithm.

Adds support for recognizing range-for loops in the main algorithm for computing
the model fixpoint.

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

Added: 
    

Modified: 
    clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp
    clang/unittests/Analysis/FlowSensitive/TypeErasedDataflowAnalysisTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp b/clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp
index ae108a702c86aa..626b57b43755ec 100644
--- a/clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp
+++ b/clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp
@@ -21,6 +21,7 @@
 #include "clang/AST/ASTDumper.h"
 #include "clang/AST/DeclCXX.h"
 #include "clang/AST/OperationKinds.h"
+#include "clang/AST/StmtCXX.h"
 #include "clang/AST/StmtVisitor.h"
 #include "clang/Analysis/Analyses/PostOrderCFGView.h"
 #include "clang/Analysis/CFG.h"
@@ -58,6 +59,7 @@ static bool isLoopHead(const CFGBlock &B) {
       case Stmt::WhileStmtClass:
       case Stmt::DoStmtClass:
       case Stmt::ForStmtClass:
+      case Stmt::CXXForRangeStmtClass:
         return true;
       default:
         return false;
@@ -108,6 +110,12 @@ class TerminatorVisitor
     return {nullptr, false};
   }
 
+  TerminatorVisitorRetTy VisitCXXForRangeStmt(const CXXForRangeStmt *) {
+    // Don't do anything special for CXXForRangeStmt, because the condition
+    // (being implicitly generated) isn't visible from the loop body.
+    return {nullptr, false};
+  }
+
   TerminatorVisitorRetTy VisitBinaryOperator(const BinaryOperator *S) {
     assert(S->getOpcode() == BO_LAnd || S->getOpcode() == BO_LOr);
     auto *LHS = S->getLHS();

diff  --git a/clang/unittests/Analysis/FlowSensitive/TypeErasedDataflowAnalysisTest.cpp b/clang/unittests/Analysis/FlowSensitive/TypeErasedDataflowAnalysisTest.cpp
index 8e43ef73c6b78b..de0b5c7b239055 100644
--- a/clang/unittests/Analysis/FlowSensitive/TypeErasedDataflowAnalysisTest.cpp
+++ b/clang/unittests/Analysis/FlowSensitive/TypeErasedDataflowAnalysisTest.cpp
@@ -1612,4 +1612,21 @@ TEST_F(TopTest, TopUnusedBeforeLoopHeadJoinsToTop) {
 
       });
 }
+
+TEST_F(TopTest, ForRangeStmtConverges) {
+  std::string Code = R"(
+    void target(bool Foo) {
+      int Ints[10];
+      bool B = false;
+      for (int I : Ints)
+        B = true;
+    }
+  )";
+  runDataflow(Code,
+              [](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &,
+                 const AnalysisOutputs &) {
+                // No additional expectations. We're only checking that the
+                // analysis converged.
+              });
+}
 } // namespace


        


More information about the cfe-commits mailing list