ControlFlowContext.h 2.6 KB

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