clang  16.0.0git
ControlFlowContext.h
Go to the documentation of this file.
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.
32 public:
33  /// Builds a ControlFlowContext from an AST node. `D` is the function in which
34  /// `S` resides and must not be null.
36  ASTContext &C);
37 
38  // DEPRECATED. Use overload above.
40  ASTContext *C);
41 
42  /// Returns the `Decl` containing the statement used to construct the CFG, if
43  /// available.
44  const Decl *getDecl() const { return ContainingDecl; }
45 
46  /// Returns the CFG that is stored in this context.
47  const CFG &getCFG() const { return *Cfg; }
48 
49  /// Returns a mapping from statements to basic blocks that contain them.
50  const llvm::DenseMap<const Stmt *, const CFGBlock *> &getStmtToBlock() const {
51  return StmtToBlock;
52  }
53 
54 private:
55  // FIXME: Once the deprecated `build` method is removed, mark `D` as "must not
56  // be null" and add an assertion.
57  ControlFlowContext(const Decl *D, std::unique_ptr<CFG> Cfg,
58  llvm::DenseMap<const Stmt *, const CFGBlock *> StmtToBlock)
59  : ContainingDecl(D), Cfg(std::move(Cfg)),
60  StmtToBlock(std::move(StmtToBlock)) {}
61 
62  /// The `Decl` containing the statement used to construct the CFG.
63  const Decl *ContainingDecl;
64  std::unique_ptr<CFG> Cfg;
65  llvm::DenseMap<const Stmt *, const CFGBlock *> StmtToBlock;
66 };
67 
68 } // namespace dataflow
69 } // namespace clang
70 
71 #endif // LLVM_CLANG_ANALYSIS_FLOWSENSITIVE_CONTROLFLOWCONTEXT_H
clang::dataflow::ControlFlowContext::getDecl
const Decl * getDecl() const
Returns the Decl containing the statement used to construct the CFG, if available.
Definition: ControlFlowContext.h:44
clang::dataflow::ControlFlowContext::getStmtToBlock
const llvm::DenseMap< const Stmt *, const CFGBlock * > & getStmtToBlock() const
Returns a mapping from statements to basic blocks that contain them.
Definition: ControlFlowContext.h:50
llvm::Expected
Definition: LLVM.h:41
clang::CFG
Represents a source-level, intra-procedural CFG that represents the control-flow of a Stmt.
Definition: CFG.h:1227
Decl.h
clang::ASTContext
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:209
clang::dataflow::ControlFlowContext::build
static llvm::Expected< ControlFlowContext > build(const Decl *D, Stmt &S, ASTContext &C)
Builds a ControlFlowContext from an AST node.
Definition: ControlFlowContext.cpp:48
ASTContext.h
clang::Decl
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:83
clang::dataflow::ControlFlowContext
Holds CFG and other derived context that is needed to perform dataflow analysis.
Definition: ControlFlowContext.h:31
std
Definition: Format.h:4477
clang
Definition: CalledOnceCheck.h:17
CFG.h
clang::Stmt
Stmt - This represents one statement.
Definition: Stmt.h:71
Stmt.h
clang::dataflow::ControlFlowContext::getCFG
const CFG & getCFG() const
Returns the CFG that is stored in this context.
Definition: ControlFlowContext.h:47