19#include "llvm/ADT/BitVector.h"
20#include "llvm/ADT/DenseMap.h"
21#include "llvm/Support/Error.h"
28static llvm::DenseMap<const Stmt *, const CFGBlock *>
30 llvm::DenseMap<const Stmt *, const CFGBlock *> StmtToBlock;
52 if (
const Stmt *TerminatorCond =
Block->getTerminatorCondition())
53 StmtToBlock.insert({TerminatorCond,
Block});
69 if (
const Stmt *TerminatorStmt =
Block->getTerminatorStmt())
70 StmtToBlock.insert({TerminatorStmt,
Block});
79 BlocksToVisit.push_back(&Cfg.
getEntry());
80 while (!BlocksToVisit.empty()) {
82 BlocksToVisit.pop_back();
84 if (BlockReachable[
Block->getBlockID()])
87 BlockReachable[
Block->getBlockID()] =
true;
91 BlocksToVisit.push_back(Succ);
94 return BlockReachable;
100 const llvm::DenseMap<const Stmt *, const CFGBlock *> &StmtToBlock) {
103 auto CheckChildExprs = [&Result, &StmtToBlock](
const Stmt *S,
105 for (
const Stmt *Child : S->children()) {
106 if (!isa_and_nonnull<Expr>(Child))
108 const CFGBlock *ChildBlock = StmtToBlock.lookup(Child);
109 if (ChildBlock !=
Block)
110 Result.insert(ChildBlock);
115 if (
Block ==
nullptr)
119 if (
auto S = Element.getAs<
CFGStmt>())
120 CheckChildExprs(S->getStmt(),
Block);
122 if (
const Stmt *TerminatorCond =
Block->getTerminatorCondition())
123 CheckChildExprs(TerminatorCond,
Block);
130 if (!
Func.doesThisDeclarationHaveABody())
131 return llvm::createStringError(
132 std::make_error_code(std::errc::invalid_argument),
133 "Cannot analyze function without a body");
135 return build(
Func, *
Func.getBody(),
Func.getASTContext());
141 return llvm::createStringError(
142 std::make_error_code(std::errc::invalid_argument),
143 "Cannot analyze templated declarations");
147 if (!C.getLangOpts().CPlusPlus || C.getLangOpts().ObjC)
148 return llvm::createStringError(
149 std::make_error_code(std::errc::invalid_argument),
150 "Can only analyze C++");
153 Options.PruneTriviallyFalseEdges =
true;
154 Options.AddImplicitDtors =
true;
155 Options.AddTemporaryDtors =
true;
156 Options.AddInitializers =
true;
157 Options.AddCXXDefaultInitExprInCtors =
true;
158 Options.AddLifetime =
true;
161 Options.setAllAlwaysAdd();
163 auto Cfg = CFG::buildCFG(&D, &S, &C, Options);
165 return llvm::createStringError(
166 std::make_error_code(std::errc::invalid_argument),
167 "CFG::buildCFG failed");
169 llvm::DenseMap<const Stmt *, const CFGBlock *> StmtToBlock =
177 return AdornedCFG(D, std::move(Cfg), std::move(StmtToBlock),
178 std::move(BlockReachable),
179 std::move(ContainsExprConsumedInDifferentBlock));
Defines the clang::ASTContext interface.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Represents a single basic block in a source-level CFG.
Represents a top-level expression in a basic block.
Represents a source-level, intra-procedural CFG that represents the control-flow of a Stmt.
unsigned getNumBlockIDs() const
Returns the total number of BlockIDs allocated (which start at 0).
Decl - This represents one declaration (or definition), e.g.
bool isTemplated() const
Determine whether this declaration is a templated entity (whether it is.
Represents a function declaration or definition.
Stmt - This represents one statement.
Holds CFG with additional information derived from it that is needed to perform dataflow analysis.
static llvm::DenseSet< const CFGBlock * > buildContainsExprConsumedInDifferentBlock(const CFG &Cfg, const llvm::DenseMap< const Stmt *, const CFGBlock * > &StmtToBlock)
static llvm::DenseMap< const Stmt *, const CFGBlock * > buildStmtToBasicBlockMap(const CFG &Cfg)
Returns a map from statements to basic blocks that contain them.
static llvm::BitVector findReachableBlocks(const CFG &Cfg)
The JSON file list parser is used to communicate input to InstallAPI.