1 //===-- ControlFlowContext.h ------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 //  This file defines a ControlFlowContext class that is used by dataflow
10 //  analyses that run over Control-Flow Graphs (CFGs).
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_CLANG_ANALYSIS_FLOWSENSITIVE_CONTROLFLOWCONTEXT_H
15 #define LLVM_CLANG_ANALYSIS_FLOWSENSITIVE_CONTROLFLOWCONTEXT_H
16 
17 #include "clang/AST/ASTContext.h"
18 #include "clang/AST/Decl.h"
19 #include "clang/AST/Stmt.h"
20 #include "clang/Analysis/CFG.h"
21 #include "llvm/ADT/DenseMap.h"
22 #include "llvm/Support/Error.h"
23 #include <memory>
24 #include <utility>
25 
26 namespace clang {
27 namespace dataflow {
28 
29 /// Holds CFG and other derived context that is needed to perform dataflow
30 /// analysis.
31 class ControlFlowContext {
32 public:
33   /// Builds a ControlFlowContext from an AST node. `D` is the function in which
34   /// `S` resides and must not be null.
35   static llvm::Expected<ControlFlowContext> build(const Decl *D, Stmt &S,
36                                                   ASTContext &C);
37 
38   /// Returns the `Decl` containing the statement used to construct the CFG, if
39   /// available.
40   const Decl *getDecl() const { return ContainingDecl; }
41 
42   /// Returns the CFG that is stored in this context.
43   const CFG &getCFG() const { return *Cfg; }
44 
45   /// Returns a mapping from statements to basic blocks that contain them.
46   const llvm::DenseMap<const Stmt *, const CFGBlock *> &getStmtToBlock() const {
47     return StmtToBlock;
48   }
49 
50 private:
51   // FIXME: Once the deprecated `build` method is removed, mark `D` as "must not
52   // be null" and add an assertion.
53   ControlFlowContext(const Decl *D, std::unique_ptr<CFG> Cfg,
54                      llvm::DenseMap<const Stmt *, const CFGBlock *> StmtToBlock)
55       : ContainingDecl(D), Cfg(std::move(Cfg)),
56         StmtToBlock(std::move(StmtToBlock)) {}
57 
58   /// The `Decl` containing the statement used to construct the CFG.
59   const Decl *ContainingDecl;
60   std::unique_ptr<CFG> Cfg;
61   llvm::DenseMap<const Stmt *, const CFGBlock *> StmtToBlock;
62 };
63 
64 } // namespace dataflow
65 } // namespace clang
66 
67 #endif // LLVM_CLANG_ANALYSIS_FLOWSENSITIVE_CONTROLFLOWCONTEXT_H
68