• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   /// Returns the `Decl` containing the statement used to construct the CFG, if
44   /// available.
getDecl()45   const Decl &getDecl() const { return ContainingDecl; }
46 
47   /// Returns the CFG that is stored in this context.
getCFG()48   const CFG &getCFG() const { return *Cfg; }
49 
50   /// Returns a mapping from statements to basic blocks that contain them.
getStmtToBlock()51   const llvm::DenseMap<const Stmt *, const CFGBlock *> &getStmtToBlock() const {
52     return StmtToBlock;
53   }
54 
55   /// Returns whether `B` is reachable from the entry block.
isBlockReachable(const CFGBlock & B)56   bool isBlockReachable(const CFGBlock &B) const {
57     return BlockReachable[B.getBlockID()];
58   }
59 
60 private:
ControlFlowContext(const Decl & D,std::unique_ptr<CFG> Cfg,llvm::DenseMap<const Stmt *,const CFGBlock * > StmtToBlock,llvm::BitVector BlockReachable)61   ControlFlowContext(const Decl &D, std::unique_ptr<CFG> Cfg,
62                      llvm::DenseMap<const Stmt *, const CFGBlock *> StmtToBlock,
63                      llvm::BitVector BlockReachable)
64       : ContainingDecl(D), Cfg(std::move(Cfg)),
65         StmtToBlock(std::move(StmtToBlock)),
66         BlockReachable(std::move(BlockReachable)) {}
67 
68   /// The `Decl` containing the statement used to construct the CFG.
69   const Decl &ContainingDecl;
70   std::unique_ptr<CFG> Cfg;
71   llvm::DenseMap<const Stmt *, const CFGBlock *> StmtToBlock;
72   llvm::BitVector BlockReachable;
73 };
74 
75 } // namespace dataflow
76 } // namespace clang
77 
78 #endif // LLVM_CLANG_ANALYSIS_FLOWSENSITIVE_CONTROLFLOWCONTEXT_H
79