[clang] e6cd409 - [clang][dataflow] In `ControlFlowContext`, handle `Decl` by reference instead of pointer.

Martin Braenne via cfe-commits cfe-commits at lists.llvm.org
Wed Aug 2 23:59:38 PDT 2023


Author: Martin Braenne
Date: 2023-08-03T06:59:29Z
New Revision: e6cd409fc6396cb13c59b4a5190abc4b856f22a5

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

LOG: [clang][dataflow] In `ControlFlowContext`, handle `Decl` by reference instead of pointer.

`build()` guarantees that we'll always have a `Decl`, so we can simplify the code.

Reviewed By: ymandel, xazax.hun

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

Added: 
    

Modified: 
    clang/include/clang/Analysis/FlowSensitive/ControlFlowContext.h
    clang/lib/Analysis/FlowSensitive/ControlFlowContext.cpp
    clang/lib/Analysis/FlowSensitive/HTMLLogger.cpp
    clang/lib/Analysis/FlowSensitive/Logger.cpp
    clang/lib/Analysis/FlowSensitive/Transfer.cpp
    clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Analysis/FlowSensitive/ControlFlowContext.h b/clang/include/clang/Analysis/FlowSensitive/ControlFlowContext.h
index bb36ed237c1e34..a45bb0635a2f36 100644
--- a/clang/include/clang/Analysis/FlowSensitive/ControlFlowContext.h
+++ b/clang/include/clang/Analysis/FlowSensitive/ControlFlowContext.h
@@ -48,7 +48,7 @@ class ControlFlowContext {
 
   /// Returns the `Decl` containing the statement used to construct the CFG, if
   /// available.
-  const Decl *getDecl() const { return ContainingDecl; }
+  const Decl &getDecl() const { return ContainingDecl; }
 
   /// Returns the CFG that is stored in this context.
   const CFG &getCFG() const { return *Cfg; }
@@ -64,9 +64,7 @@ class ControlFlowContext {
   }
 
 private:
-  // FIXME: Once the deprecated `build` method is removed, mark `D` as "must not
-  // be null" and add an assertion.
-  ControlFlowContext(const Decl *D, std::unique_ptr<CFG> Cfg,
+  ControlFlowContext(const Decl &D, std::unique_ptr<CFG> Cfg,
                      llvm::DenseMap<const Stmt *, const CFGBlock *> StmtToBlock,
                      llvm::BitVector BlockReachable)
       : ContainingDecl(D), Cfg(std::move(Cfg)),
@@ -74,7 +72,7 @@ class ControlFlowContext {
         BlockReachable(std::move(BlockReachable)) {}
 
   /// The `Decl` containing the statement used to construct the CFG.
-  const Decl *ContainingDecl;
+  const Decl &ContainingDecl;
   std::unique_ptr<CFG> Cfg;
   llvm::DenseMap<const Stmt *, const CFGBlock *> StmtToBlock;
   llvm::BitVector BlockReachable;

diff  --git a/clang/lib/Analysis/FlowSensitive/ControlFlowContext.cpp b/clang/lib/Analysis/FlowSensitive/ControlFlowContext.cpp
index c80525dc4f34f2..d5e0b443caf301 100644
--- a/clang/lib/Analysis/FlowSensitive/ControlFlowContext.cpp
+++ b/clang/lib/Analysis/FlowSensitive/ControlFlowContext.cpp
@@ -105,7 +105,7 @@ ControlFlowContext::build(const Decl &D, Stmt &S, ASTContext &C) {
 
   llvm::BitVector BlockReachable = findReachableBlocks(*Cfg);
 
-  return ControlFlowContext(&D, std::move(Cfg), std::move(StmtToBlock),
+  return ControlFlowContext(D, std::move(Cfg), std::move(StmtToBlock),
                             std::move(BlockReachable));
 }
 

diff  --git a/clang/lib/Analysis/FlowSensitive/HTMLLogger.cpp b/clang/lib/Analysis/FlowSensitive/HTMLLogger.cpp
index 9ec0160b1e3fe4..b1bfe10db20243 100644
--- a/clang/lib/Analysis/FlowSensitive/HTMLLogger.cpp
+++ b/clang/lib/Analysis/FlowSensitive/HTMLLogger.cpp
@@ -166,15 +166,14 @@ class HTMLLogger : public Logger {
     this->CFG = &CFG;
     *OS << llvm::StringRef(HTMLLogger_html).split("<?INJECT?>").first;
 
-    if (const auto *D = CFG.getDecl()) {
-      const auto &SM = A.getASTContext().getSourceManager();
-      *OS << "<title>";
-      if (const auto *ND = dyn_cast<NamedDecl>(D))
-        *OS << ND->getNameAsString() << " at ";
-      *OS << SM.getFilename(D->getLocation()) << ":"
-          << SM.getSpellingLineNumber(D->getLocation());
-      *OS << "</title>\n";
-    };
+    const auto &D = CFG.getDecl();
+    const auto &SM = A.getASTContext().getSourceManager();
+    *OS << "<title>";
+    if (const auto *ND = dyn_cast<NamedDecl>(&D))
+      *OS << ND->getNameAsString() << " at ";
+    *OS << SM.getFilename(D.getLocation()) << ":"
+        << SM.getSpellingLineNumber(D.getLocation());
+    *OS << "</title>\n";
 
     *OS << "<style>" << HTMLLogger_css << "</style>\n";
     *OS << "<script>" << HTMLLogger_js << "</script>\n";
@@ -307,9 +306,7 @@ class HTMLLogger : public Logger {
   // tokens are associated with, and even which BB element (so that clicking
   // can select the right element).
   void writeCode() {
-    if (!CFG->getDecl())
-      return;
-    const auto &AST = CFG->getDecl()->getASTContext();
+    const auto &AST = CFG->getDecl().getASTContext();
     bool Invalid = false;
 
     // Extract the source code from the original file.
@@ -317,7 +314,7 @@ class HTMLLogger : public Logger {
     // indentation to worry about), but we need the boundaries of particular
     // AST nodes and the printer doesn't provide this.
     auto Range = clang::Lexer::makeFileCharRange(
-        CharSourceRange::getTokenRange(CFG->getDecl()->getSourceRange()),
+        CharSourceRange::getTokenRange(CFG->getDecl().getSourceRange()),
         AST.getSourceManager(), AST.getLangOpts());
     if (Range.isInvalid())
       return;

diff  --git a/clang/lib/Analysis/FlowSensitive/Logger.cpp b/clang/lib/Analysis/FlowSensitive/Logger.cpp
index 469fea338e451d..28557638522e8f 100644
--- a/clang/lib/Analysis/FlowSensitive/Logger.cpp
+++ b/clang/lib/Analysis/FlowSensitive/Logger.cpp
@@ -39,11 +39,10 @@ struct TextualLogger final : Logger {
       llvm::WithColor Header(OS, llvm::raw_ostream::Colors::RED, /*Bold=*/true);
       OS << "=== Beginning data flow analysis ===\n";
     }
-    if (auto *D = CFG.getDecl()) {
-      D->print(OS);
-      OS << "\n";
-      D->dump(OS);
-    }
+    auto &D = CFG.getDecl();
+    D.print(OS);
+    OS << "\n";
+    D.dump(OS);
     CurrentCFG = &CFG.getCFG();
     CurrentCFG->print(OS, Analysis.getASTContext().getLangOpts(), ShowColors);
     CurrentAnalysis = &Analysis;

diff  --git a/clang/lib/Analysis/FlowSensitive/Transfer.cpp b/clang/lib/Analysis/FlowSensitive/Transfer.cpp
index d5da879d1bed2a..8617cf4c8ca4a2 100644
--- a/clang/lib/Analysis/FlowSensitive/Transfer.cpp
+++ b/clang/lib/Analysis/FlowSensitive/Transfer.cpp
@@ -725,9 +725,7 @@ class TransferVisitor : public ConstStmtVisitor<TransferVisitor> {
     // FIXME: Use the same analysis as the caller for the callee. Note,
     // though, that doing so would require support for changing the analysis's
     // ASTContext.
-    assert(CFCtx->getDecl() != nullptr &&
-           "ControlFlowContexts in the environment should always carry a decl");
-    auto Analysis = NoopAnalysis(CFCtx->getDecl()->getASTContext(),
+    auto Analysis = NoopAnalysis(CFCtx->getDecl().getASTContext(),
                                  DataflowAnalysisOptions{Options});
 
     auto BlockToOutputState =

diff  --git a/clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp b/clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp
index e0c4991ca1a45d..e37d86cb6ba4ee 100644
--- a/clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp
+++ b/clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp
@@ -189,7 +189,7 @@ class PrettyStackTraceAnalysis : public llvm::PrettyStackTraceEntry {
   void print(raw_ostream &OS) const override {
     OS << Message << "\n";
     OS << "Decl:\n";
-    CFCtx.getDecl()->dump(OS);
+    CFCtx.getDecl().dump(OS);
     OS << "CFG:\n";
     CFCtx.getCFG().print(OS, LangOptions(), false);
   }


        


More information about the cfe-commits mailing list