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.doesThisDeclarationHaveABody()` must be true, and
36   /// `Func.isTemplated()` must be false.
37   static llvm::Expected<ControlFlowContext> build(const FunctionDecl &Func);
38 
39   /// Builds a ControlFlowContext from an AST node. `D` is the function in which
40   /// `S` resides. `D.isTemplated()` must be false.
41   static llvm::Expected<ControlFlowContext> build(const Decl &D, Stmt &S,
42                                                   ASTContext &C);
43 
44   /// Returns the `Decl` containing the statement used to construct the CFG, if
45   /// available.
getDecl()46   const Decl &getDecl() const { return ContainingDecl; }
47 
48   /// Returns the CFG that is stored in this context.
getCFG()49   const CFG &getCFG() const { return *Cfg; }
50 
51   /// Returns a mapping from statements to basic blocks that contain them.
getStmtToBlock()52   const llvm::DenseMap<const Stmt *, const CFGBlock *> &getStmtToBlock() const {
53     return StmtToBlock;
54   }
55 
56   /// Returns whether `B` is reachable from the entry block.
isBlockReachable(const CFGBlock & B)57   bool isBlockReachable(const CFGBlock &B) const {
58     return BlockReachable[B.getBlockID()];
59   }
60 
61 private:
ControlFlowContext(const Decl & D,std::unique_ptr<CFG> Cfg,llvm::DenseMap<const Stmt *,const CFGBlock * > StmtToBlock,llvm::BitVector BlockReachable)62   ControlFlowContext(const Decl &D, std::unique_ptr<CFG> Cfg,
63                      llvm::DenseMap<const Stmt *, const CFGBlock *> StmtToBlock,
64                      llvm::BitVector BlockReachable)
65       : ContainingDecl(D), Cfg(std::move(Cfg)),
66         StmtToBlock(std::move(StmtToBlock)),
67         BlockReachable(std::move(BlockReachable)) {}
68 
69   /// The `Decl` containing the statement used to construct the CFG.
70   const Decl &ContainingDecl;
71   std::unique_ptr<CFG> Cfg;
72   llvm::DenseMap<const Stmt *, const CFGBlock *> StmtToBlock;
73   llvm::BitVector BlockReachable;
74 };
75 
76 } // namespace dataflow
77 } // namespace clang
78 
79 #endif // LLVM_CLANG_ANALYSIS_FLOWSENSITIVE_CONTROLFLOWCONTEXT_H
80