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/BitVector.h"
22 #include "llvm/ADT/DenseMap.h"
23 #include "llvm/Support/Error.h"
24 #include <memory>
25 #include <utility>
26 
27 namespace clang {
28 namespace dataflow {
29 
30 /// Holds CFG and other derived context that is needed to perform dataflow
31 /// analysis.
32 class ControlFlowContext {
33 public:
34   /// Builds a ControlFlowContext from a `FunctionDecl`.
35   /// `Func.hasBody()` must be true, and `Func.isTemplated()` must be false.
36   static llvm::Expected<ControlFlowContext> build(const FunctionDecl &Func);
37 
38   /// Builds a ControlFlowContext from an AST node. `D` is the function in which
39   /// `S` resides. `D.isTemplated()` must be false.
40   static llvm::Expected<ControlFlowContext> build(const Decl &D, Stmt &S,
41                                                   ASTContext &C);
42 
43   /// Builds a ControlFlowContext from an AST node. `D` is the function in which
44   /// `S` resides. `D` must not be null and `D->isTemplated()` must be false.
45   LLVM_DEPRECATED("Use the version that takes a const Decl & instead", "")
46   static llvm::Expected<ControlFlowContext> build(const Decl *D, Stmt &S,
47                                                   ASTContext &C);
48 
49   /// Returns the `Decl` containing the statement used to construct the CFG, if
50   /// available.
51   const Decl *getDecl() const { return ContainingDecl; }
52 
53   /// Returns the CFG that is stored in this context.
54   const CFG &getCFG() const { return *Cfg; }
55 
56   /// Returns a mapping from statements to basic blocks that contain them.
57   const llvm::DenseMap<const Stmt *, const CFGBlock *> &getStmtToBlock() const {
58     return StmtToBlock;
59   }
60 
61   /// Returns whether `B` is reachable from the entry block.
62   bool isBlockReachable(const CFGBlock &B) const {
63     return BlockReachable[B.getBlockID()];
64   }
65 
66 private:
67   // FIXME: Once the deprecated `build` method is removed, mark `D` as "must not
68   // be null" and add an assertion.
69   ControlFlowContext(const Decl *D, std::unique_ptr<CFG> Cfg,
70                      llvm::DenseMap<const Stmt *, const CFGBlock *> StmtToBlock,
71                      llvm::BitVector BlockReachable)
72       : ContainingDecl(D), Cfg(std::move(Cfg)),
73         StmtToBlock(std::move(StmtToBlock)),
74         BlockReachable(std::move(BlockReachable)) {}
75 
76   /// The `Decl` containing the statement used to construct the CFG.
77   const Decl *ContainingDecl;
78   std::unique_ptr<CFG> Cfg;
79   llvm::DenseMap<const Stmt *, const CFGBlock *> StmtToBlock;
80   llvm::BitVector BlockReachable;
81 };
82 
83 } // namespace dataflow
84 } // namespace clang
85 
86 #endif // LLVM_CLANG_ANALYSIS_FLOWSENSITIVE_CONTROLFLOWCONTEXT_H
87