clang 18.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
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
27namespace clang {
28namespace dataflow {
29
30/// Holds CFG and other derived context that is needed to perform dataflow
31/// analysis.
33public:
34 /// Builds a ControlFlowContext from a `FunctionDecl`.
35 /// `Func.hasBody()` must be true, and `Func.isTemplated()` must be false.
37
38 /// Builds a ControlFlowContext from an AST node. `D` is the function in which
39 /// `S` resides. `D.isTemplated()` must be false.
41 ASTContext &C);
42
43 /// Returns the `Decl` containing the statement used to construct the CFG, if
44 /// available.
45 const Decl &getDecl() const { return ContainingDecl; }
46
47 /// Returns the CFG that is stored in this context.
48 const CFG &getCFG() const { return *Cfg; }
49
50 /// Returns a mapping from statements to basic blocks that contain them.
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.
56 bool isBlockReachable(const CFGBlock &B) const {
57 return BlockReachable[B.getBlockID()];
58 }
59
60private:
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
Defines the clang::ASTContext interface.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:182
Represents a single basic block in a source-level CFG.
Definition: CFG.h:604
unsigned getBlockID() const
Definition: CFG.h:1102
Represents a source-level, intra-procedural CFG that represents the control-flow of a Stmt.
Definition: CFG.h:1211
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:85
Represents a function declaration or definition.
Definition: Decl.h:1957
Stmt - This represents one statement.
Definition: Stmt.h:84
Holds CFG and other derived context that is needed to perform dataflow analysis.
bool isBlockReachable(const CFGBlock &B) const
Returns whether B is reachable from the entry block.
static llvm::Expected< ControlFlowContext > build(const FunctionDecl &Func)
Builds a ControlFlowContext from a FunctionDecl.
const Decl & getDecl() const
Returns the Decl containing the statement used to construct the CFG, if available.
const CFG & getCFG() const
Returns the CFG that is stored in this context.
const llvm::DenseMap< const Stmt *, const CFGBlock * > & getStmtToBlock() const
Returns a mapping from statements to basic blocks that contain them.
Definition: Format.h:5200