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. 34 static llvm::Expected<ControlFlowContext> build(const Decl *D, Stmt *S, 35 ASTContext *C); 36 37 /// Returns the CFG that is stored in this context. 38 const CFG &getCFG() const { return *Cfg; } 39 40 /// Returns a mapping from statements to basic blocks that contain them. 41 const llvm::DenseMap<const Stmt *, const CFGBlock *> &getStmtToBlock() const { 42 return StmtToBlock; 43 } 44 45 private: 46 ControlFlowContext(std::unique_ptr<CFG> Cfg, 47 llvm::DenseMap<const Stmt *, const CFGBlock *> StmtToBlock) 48 : Cfg(std::move(Cfg)), StmtToBlock(std::move(StmtToBlock)) {} 49 50 std::unique_ptr<CFG> Cfg; 51 llvm::DenseMap<const Stmt *, const CFGBlock *> StmtToBlock; 52 }; 53 54 } // namespace dataflow 55 } // namespace clang 56 57 #endif // LLVM_CLANG_ANALYSIS_FLOWSENSITIVE_CONTROLFLOWCONTEXT_H 58