clang 17.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 ASTContext &C);
38
39 /// Builds a ControlFlowContext from an AST node. `D` is the function in which
40 /// `S` resides. `D.isTemplated()` must be false.
42 ASTContext &C);
43
44 /// Builds a ControlFlowContext from an AST node. `D` is the function in which
45 /// `S` resides. `D` must not be null and `D->isTemplated()` must be false.
46 LLVM_DEPRECATED("Use the version that takes a const Decl & instead", "")
48 ASTContext &C);
49
50 /// Returns the `Decl` containing the statement used to construct the CFG, if
51 /// available.
52 const Decl *getDecl() const { return ContainingDecl; }
53
54 /// Returns the CFG that is stored in this context.
55 const CFG &getCFG() const { return *Cfg; }
56
57 /// Returns a mapping from statements to basic blocks that contain them.
58 const llvm::DenseMap<const Stmt *, const CFGBlock *> &getStmtToBlock() const {
59 return StmtToBlock;
60 }
61
62 /// Returns whether `B` is reachable from the entry block.
63 bool isBlockReachable(const CFGBlock &B) const {
64 return BlockReachable[B.getBlockID()];
65 }
66
67private:
68 // FIXME: Once the deprecated `build` method is removed, mark `D` as "must not
69 // be null" and add an assertion.
70 ControlFlowContext(const Decl *D, std::unique_ptr<CFG> Cfg,
71 llvm::DenseMap<const Stmt *, const CFGBlock *> StmtToBlock,
72 llvm::BitVector BlockReachable)
73 : ContainingDecl(D), Cfg(std::move(Cfg)),
74 StmtToBlock(std::move(StmtToBlock)),
75 BlockReachable(std::move(BlockReachable)) {}
76
77 /// The `Decl` containing the statement used to construct the CFG.
78 const Decl *ContainingDecl;
79 std::unique_ptr<CFG> Cfg;
80 llvm::DenseMap<const Stmt *, const CFGBlock *> StmtToBlock;
81 llvm::BitVector BlockReachable;
82};
83
84} // namespace dataflow
85} // namespace clang
86
87#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:576
unsigned getBlockID() const
Definition: CFG.h:1074
Represents a source-level, intra-procedural CFG that represents the control-flow of a Stmt.
Definition: CFG.h:1225
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:83
Represents a function declaration or definition.
Definition: Decl.h:1917
Stmt - This represents one statement.
Definition: Stmt.h:72
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.
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.
static llvm::Expected< ControlFlowContext > build(const FunctionDecl &Func, ASTContext &C)
Builds a ControlFlowContext from a FunctionDecl.
const llvm::DenseMap< const Stmt *, const CFGBlock * > & getStmtToBlock() const
Returns a mapping from statements to basic blocks that contain them.
@ C
Languages that the frontend can parse and compile.
Definition: Format.h:4756