15 #ifndef LLVM_CLANG_ANALYSIS_CFG_H
16 #define LLVM_CLANG_ANALYSIS_CFG_H
21 #include "llvm/ADT/DenseMap.h"
22 #include "llvm/ADT/GraphTraits.h"
23 #include "llvm/ADT/Optional.h"
24 #include "llvm/ADT/PointerIntPair.h"
25 #include "llvm/Support/Allocator.h"
26 #include "llvm/Support/Casting.h"
27 #include "llvm/Support/raw_ostream.h"
34 class CXXDestructorDecl;
40 class CXXCtorInitializer;
41 class CXXBaseSpecifier;
42 class CXXBindTemporaryExpr;
72 llvm::PointerIntPair<void *, 2>
Data1;
73 llvm::PointerIntPair<void *, 2>
Data2;
77 Data2(const_cast<void*>(Ptr2), (((
unsigned) kind) >> 2) & 0x3) {
88 assert(T::isKind(*
this));
99 if (!T::isKind(*
this))
108 unsigned x =
Data2.getInt();
120 return static_cast<const Stmt *
>(
Data1.getPointer());
205 return static_cast<Stmt*
>(
Data2.getPointer());
304 llvm::PointerIntPair<Stmt *, 1> Data;
308 : Data(S, TemporaryDtorsBranch) {}
360 typedef std::reverse_iterator<ImplTy::iterator>
iterator;
361 typedef std::reverse_iterator<ImplTy::const_iterator>
const_iterator;
369 return Impl.insert(I, Cnt, E, C);
372 const_reference
front()
const {
return Impl.back(); }
373 const_reference
back()
const {
return Impl.front(); }
385 assert(i < Impl.size());
386 return Impl[Impl.size() - 1 - i];
389 size_t size()
const {
return Impl.size(); }
390 bool empty()
const {
return Impl.empty(); }
394 ElementList Elements;
409 const Stmt *LoopTarget;
429 llvm::PointerIntPair<CFGBlock*, 2> UnreachableBlock;
441 return ReachableBlock;
446 return UnreachableBlock.getPointer();
464 Kind K = (Kind) UnreachableBlock.getInt();
465 return K == AB_Normal || K == AB_Alternate;
473 AdjacentBlocks Preds;
474 AdjacentBlocks Succs;
485 unsigned HasNoReturnElement : 1;
492 : Elements(C), Label(nullptr), Terminator(nullptr), LoopTarget(nullptr),
493 BlockID(blockid), Preds(C, 1), Succs(C, 1), HasNoReturnElement(
false),
515 unsigned size()
const {
return Elements.size(); }
516 bool empty()
const {
return Elements.empty(); }
572 template <
typename IMPL,
bool IsPred>
582 : I(i), E(e), F(f), From(from) {
583 while (
hasMore() && Filter(*I))
590 do { ++I; }
while (
hasMore() && Filter(*I));
597 return IsPred ?
FilterEdge(F, To, From) : FilterEdge(F, From, To);
601 typedef FilteredCFGBlockIterator<const_pred_iterator, true>
646 bool ShowColors)
const;
656 Elements.push_back(
CFGStmt(statement), C);
694 return iterator(Elements.insert(I.base(), Cnt,
710 bool isAlwaysTrue) {}
728 std::bitset<Stmt::lastStmtConstant> alwaysAddMask;
743 return alwaysAddMask[stmt->getStmtClass()];
747 alwaysAddMask[stmtClass] = val;
815 const BuildOptions &BO);
872 return TryDispatchBlocks.begin();
875 return TryDispatchBlocks.end();
879 TryDispatchBlocks.push_back(block);
888 assert(Synthetic->
isSingleDecl() &&
"Can handle single declarations only");
889 assert(Synthetic != Source &&
"Don't include original DeclStmts in map");
890 assert(!SyntheticDeclStmts.count(Synthetic) &&
"Already in map");
891 SyntheticDeclStmts[Synthetic] = Source;
894 typedef llvm::DenseMap<const DeclStmt *, const DeclStmt *>::const_iterator
903 return SyntheticDeclStmts.begin();
908 return SyntheticDeclStmts.end();
915 template <
typename CALLBACK>
921 O(const_cast<Stmt*>(
stmt->getStmt()));
936 unsigned size()
const {
return NumBlockIDs; }
951 : Entry(nullptr), Exit(nullptr), IndirectGotoBlock(nullptr), NumBlockIDs(0),
952 Blocks(BlkBVC, 10) {}
967 unsigned NumBlockIDs;
975 std::vector<const CFGBlock *> TryDispatchBlocks;
979 llvm::DenseMap<const DeclStmt *, const DeclStmt *> SyntheticDeclStmts;
1022 {
return N->succ_begin(); }
1025 {
return N->succ_end(); }
1050 {
return N->pred_begin(); }
1053 {
return N->pred_end(); }
1059 :
public GraphTraits< ::clang::CFGBlock *> {
1070 :
public GraphTraits<const ::clang::CFGBlock *> {
1075 return &F->getEntry();
1078 return F->nodes_begin();
1081 return F->nodes_end();
1083 static unsigned size(const ::clang::CFG* F) {
1089 :
public GraphTraits<Inverse< ::clang::CFGBlock*> > {
1099 :
public GraphTraits<Inverse<const ::clang::CFGBlock*> > {
1103 static NodeType *
getEntryNode(const ::clang::CFG* F) {
return &F->getExit(); }
1105 return F->nodes_begin();
1108 return F->nodes_end();
1113 #endif // LLVM_CLANG_ANALYSIS_CFG_H
FilteredCFGBlockIterator< const_pred_iterator, true > filtered_pred_iterator
void setIndirectGotoBlock(CFGBlock *B)
setIndirectGotoBlock - Set the block used for indirect goto jumps.
const_pred_reverse_iterator pred_rend() const
CFGNewAllocator - Represents C++ allocator call.
succ_reverse_iterator succ_rbegin()
const internal::VariadicDynCastAllOfMatcher< Stmt, Expr > expr
Matches expressions.
const CXXNewExpr * getAllocatorExpr() const
CFGBlock(unsigned blockid, BumpVectorContext &C, CFG *parent)
CFGElement operator[](size_t i) const
ElementList::const_reverse_iterator const_reverse_iterator
succ_iterator succ_begin()
ElementList::reverse_iterator reverse_iterator
const internal::VariadicAllOfMatcher< Stmt > stmt
Matches statements.
static NodeType * getEntryNode(Inverse< const ::clang::CFGBlock * > G)
CFGDeleteDtor(const CXXRecordDecl *RD, const CXXDeleteExpr *DE)
reference operator*() const
void appendNewAllocator(CXXNewExpr *NE, BumpVectorContext &C)
CFGNewAllocator(const CXXNewExpr *S)
graph_iterator & operator++()
const CFGBlock value_type
CFGBlockListTy::iterator iterator
llvm::BumpPtrAllocator & getAllocator()
const CFGBlock * operator*() const
CFGDeleteDtor - Represents C++ object destructor generated from a call to delete. ...
static unsigned size(const ::clang::CFG *F)
const CXXDeleteExpr * getDeleteExpr() const
const CFGBlock & getEntry() const
BumpVector< CFGBlock * >::const_iterator ImplTy
std::reverse_iterator< iterator > reverse_iterator
unsigned IgnoreDefaultsWithCoveredEnums
bool AddStaticInitBranches
const_pred_iterator pred_end() const
::clang::CFGBlock::const_pred_iterator ChildIteratorType
void print(raw_ostream &OS, const LangOptions &LO, bool ShowColors) const
print - A simple pretty printer of a CFG that outputs to an ostream.
llvm::BumpPtrAllocator & getAllocator()
typedefconst::clang::CFGBlock NodeType
const FieldDecl * getFieldDecl() const
reverse_iterator rbegin()
CFGBlock * getReachableBlock() const
Get the reachable block, if one exists.
VarDecl - An instance of this class is created to represent a variable declaration or definition...
graph_iterator(const ImplTy &i)
void printTerminator(raw_ostream &OS, const LangOptions &LO) const
printTerminator - A simple pretty printer of the terminator of a CFGBlock.
typedefconst::clang::CFGBlock NodeType
static ChildIteratorType child_end(NodeType *N)
unsigned succ_size() const
graph_iterator nodes_begin()
AdjacentBlocks::iterator succ_iterator
reference operator*() const
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
void setLoopTarget(const Stmt *loopTarget)
::clang::CFG::graph_iterator nodes_iterator
synthetic_stmt_iterator synthetic_stmt_end() const
const_graph_iterator nodes_end() const
FieldDecl - An instance of this class is created by Sema::ActOnField to represent a member of a struc...
const Stmt * getLabel() const
iterator insertAutomaticObjDtor(iterator I, VarDecl *VD, Stmt *S)
CFGAutomaticObjDtor - Represents C++ object destructor implicitly generated for automatic object or t...
void appendAutomaticObjDtor(VarDecl *VD, Stmt *S, BumpVectorContext &C)
T castAs() const
Convert to the specified CFGElement type, asserting that this CFGElement is of the desired type...
CFGBlock * operator->() const
void setTerminator(CFGTerminator Term)
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
static unsigned size(::clang::CFG *F)
const Stmt & operator*() const
const CXXBindTemporaryExpr * getBindTemporaryExpr() const
static nodes_iterator nodes_end(const ::clang::CFG *F)
const Stmt * getLoopTarget() const
std::vector< const CFGBlock * >::const_iterator try_block_iterator
CFGBlock * getPossiblyUnreachableBlock() const
Get the potentially unreachable block.
BumpVector< CFGBlock * > CFGBlockListTy
const_graph_iterator & operator++()
static NodeType * getEntryNode(const clang::CFGBlock *BB)
const_iterator begin() const
bool AddCXXDefaultInitExprInCtors
const VarDecl * getVarDecl() const
unsigned size() const
size - Return the total number of CFGBlocks within the CFG This is simply a renaming of the getNumBlo...
unsigned pred_size() const
static nodes_iterator nodes_begin(const ::clang::CFG *F)
ElementList::const_iterator const_iterator
A builtin binary operation expression such as "x + y" or "x <= y".
Provides a custom implementation of the iterator class to have the same interface as Function::iterat...
graph_iterator nodes_end()
FilteredCFGBlockIterator(const IMPL &i, const IMPL &e, const CFGBlock *from, const FilterOptions &f)
::clang::CFGBlock::succ_iterator ChildIteratorType
Represents binding an expression to a temporary.
static ChildIteratorType child_begin(NodeType *N)
static NodeType * getEntryNode(const ::clang::CFG *F)
detail::InMemoryDirectory::const_iterator I
const Stmt * getTriggerStmt() const
filtered_pred_iterator filtered_pred_start_end(const FilterOptions &f) const
AdjacentBlocks::reverse_iterator succ_reverse_iterator
const_succ_iterator succ_begin() const
static nodes_iterator nodes_begin(::clang::CFG *F)
static nodes_iterator nodes_begin(::clang::CFG *F)
::clang::CFGBlock NodeType
ForcedBlkExprs ** forcedBlkExprs
const Stmt * getStmt() const
AdjacentBlocks::iterator pred_iterator
static ChildIteratorType child_end(NodeType *N)
void appendBaseDtor(const CXXBaseSpecifier *BS, BumpVectorContext &C)
CFGBlock - Represents a single basic block in a source-level CFG.
static ChildIteratorType child_begin(NodeType *N)
bool alwaysAdd(const Stmt *stmt) const
bool operator==(const const_graph_iterator &X) const
Stmt * getTerminatorCondition(bool StripParens=true)
bool isNoReturn(ASTContext &astContext) const
void VisitBlockStmts(CALLBACK &O) const
static NodeType * getEntryNode(Inverse< ::clang::CFGBlock * > G)
CFG - Represents a source-level, intra-procedural CFG that represents the control-flow of a Stmt...
static NodeType * getEntryNode(::clang::CFG *F)
Represents a C++ destructor within a class.
CXXCtorInitializer * getInitializer() const
const CXXRecordDecl * getCXXRecordDecl() const
static NodeType * getEntryNode(const ::clang::CFG *F)
static ChildIteratorType child_begin(NodeType *N)
static SimpleType getSimplifiedValue(::clang::CFGTerminator Val)
::clang::CFG::graph_iterator nodes_iterator
bool operator==(const graph_iterator &X) const
AdjacentBlocks::const_iterator const_pred_iterator
unsigned getBlockID() const
const_iterator end() const
const_iterator begin() const
CFGBaseDtor - Represents C++ object destructor implicitly generated for base object in destructor...
std::reverse_iterator< const_iterator > const_reverse_iterator
const_graph_iterator(const ImplTy &i)
void viewCFG(const LangOptions &LO) const
void addTryDispatchBlock(const CFGBlock *block)
reverse_iterator rbegin()
ElementList::iterator iterator
const_pred_iterator pred_begin() const
llvm::DenseMap< const DeclStmt *, const DeclStmt * >::const_iterator synthetic_stmt_iterator
static ChildIteratorType child_begin(NodeType *N)
static ChildIteratorType child_end(NodeType *N)
::clang::CFGBlock NodeType
const_reverse_iterator rbegin() const
void appendStmt(Stmt *statement, BumpVectorContext &C)
bool hasNoReturnElement() const
CFGTerminator getTerminator()
CFGImplicitDtor(Kind kind, const void *data1, const void *data2=nullptr)
pred_reverse_iterator pred_rend()
BuildOptions & setAlwaysAdd(Stmt::StmtClass stmtClass, bool val=true)
::clang::CFG::const_graph_iterator nodes_iterator
reverse_iterator rbegin()
::clang::CFG::const_graph_iterator nodes_iterator
BumpVector< CFGBlock * >::iterator ImplTy
const Stmt * operator->() const
Represents a new-expression for memory allocation and constructor calls, e.g: "new CXXNewExpr(foo)"...
bool isSingleDecl() const
isSingleDecl - This method returns true if this DeclStmt refers to a single Decl. ...
void setLabel(Stmt *Statement)
static std::unique_ptr< CFG > buildCFG(const Decl *D, Stmt *AST, ASTContext *C, const BuildOptions &BO)
buildCFG - Builds a CFG from an AST.
DeclStmt - Adaptor class for mixing declarations with statements and expressions. ...
bool PruneTriviallyFalseEdges
bool isTemporaryDtorsBranch() const
std::reverse_iterator< const_iterator > const_reverse_iterator
virtual void compareAlwaysTrue(const BinaryOperator *B, bool isAlwaysTrue)
const Stmt * getStmt() const
succ_reverse_iterator succ_rend()
const CXXDestructorDecl * getDestructorDecl(ASTContext &astContext) const
llvm::PointerIntPair< void *, 2 > Data1
BumpVectorContext & getBumpVectorContext()
BuildOptions & setAllAlwaysAdd()
const_graph_iterator nodes_begin() const
void print(raw_ostream &OS, const CFG *cfg, const LangOptions &LO, bool ShowColors) const
print - A simple pretty printer of a CFGBlock that outputs to an ostream.
AdjacentBlocks::const_iterator const_succ_iterator
FilteredCFGBlockIterator & operator++()
static nodes_iterator nodes_end(::clang::CFG *F)
const_pred_reverse_iterator pred_rbegin() const
::clang::CFGBlock::const_pred_iterator ChildIteratorType
pred_iterator pred_begin()
AdjacentBlocks::reverse_iterator pred_reverse_iterator
llvm::PointerIntPair< void *, 2 > Data2
CFGBlockListTy::const_iterator const_iterator
Represents a delete expression for memory deallocation and destructor calls, e.g. ...
static nodes_iterator nodes_begin(const ::clang::CFG *F)
CFGTerminator(Stmt *S, bool TemporaryDtorsBranch=false)
iterator beginAutomaticObjDtorsInsert(iterator I, size_t Cnt, BumpVectorContext &C)
CFGAutomaticObjDtor(const VarDecl *var, const Stmt *stmt)
virtual void compareBitwiseEquality(const BinaryOperator *B, bool isAlwaysTrue)
const_iterator end() const
pointer operator->() const
void appendDeleteDtor(CXXRecordDecl *RD, CXXDeleteExpr *DE, BumpVectorContext &C)
const_succ_reverse_iterator succ_rend() const
detail::InMemoryDirectory::const_iterator E
void appendMemberDtor(FieldDecl *FD, BumpVectorContext &C)
try_block_iterator try_blocks_end() const
void appendInitializer(CXXCtorInitializer *initializer, BumpVectorContext &C)
This class represents a potential adjacent block in the CFG.
CFGInitializer(CXXCtorInitializer *initializer)
bool operator!=(const graph_iterator &X) const
pred_reverse_iterator pred_rbegin()
llvm::DenseMap< const Stmt *, const CFGBlock * > ForcedBlkExprs
CFGBlock * getIndirectGotoBlock()
Represents a C++ base or member initializer.
bool operator!=(const const_graph_iterator &X) const
::clang::CFGBlock::const_succ_iterator ChildIteratorType
try_block_iterator try_blocks_begin() const
const_reverse_iterator rbegin() const
static ChildIteratorType child_end(NodeType *N)
Represents a base class of a C++ class.
static NodeType * getEntryNode(::clang::CFG *F)
CFGElement(Kind kind, const void *Ptr1, const void *Ptr2=nullptr)
unsigned IgnoreNullPredecessors
X
Add a minimal nested name specifier fixit hint to allow lookup of a tag name from an outer enclosing ...
void setHasNoReturnElement()
CFGMemberDtor(const FieldDecl *field)
const_graph_iterator & operator--()
Defines the clang::SourceLocation class and associated facilities.
AdjacentBlocks::const_reverse_iterator const_pred_reverse_iterator
const_reverse_iterator rend() const
void printAsOperand(raw_ostream &OS, bool)
Represents a C++ struct/union/class.
pointer operator->() const
CFGCallback defines methods that should be called when a logical operator error is found when buildin...
static nodes_iterator nodes_end(const ::clang::CFG *F)
CFGImplicitDtor - Represents C++ object destructor implicitly generated by compiler on various occasi...
static NodeType * getEntryNode(::clang::CFGBlock *BB)
CFGElement - Represents a top-level expression in a basic block.
void addSuccessor(AdjacentBlock Succ, BumpVectorContext &C)
Adds a (potentially unreachable) successor block to the current block.
const_reverse_iterator rend() const
AdjacentBlocks::const_reverse_iterator const_succ_reverse_iterator
unsigned kind
All of the diagnostics that can be emitted by the frontend.
CFGTerminator - Represents CFGBlock terminator statement.
CFGMemberDtor - Represents C++ object destructor implicitly generated for member object in destructor...
const CFGElement * const_iterator
::clang::Stmt * SimpleType
AdjacentBlock(CFGBlock *B, bool IsReachable)
Construct an AdjacentBlock with a possibly unreachable block.
graph_iterator & operator--()
void appendTemporaryDtor(CXXBindTemporaryExpr *E, BumpVectorContext &C)
FilteredCFGBlockIterator< const_succ_iterator, false > filtered_succ_iterator
CFGBlock & operator*() const
const Stmt * getTerminatorCondition(bool StripParens=true) const
const_succ_iterator succ_end() const
const CFGElement & const_reference
synthetic_stmt_iterator synthetic_stmt_begin() const
Iterates over synthetic DeclStmts in the CFG.
CFGBaseDtor(const CXXBaseSpecifier *base)
CFGInitializer - Represents C++ base or member initializer from constructor's initialization list...
const CFGTerminator getTerminator() const
CFGTemporaryDtor(CXXBindTemporaryExpr *expr)
CFGBlock * createBlock()
createBlock - Create a new block in the CFG.
const CXXBaseSpecifier * getBaseSpecifier() const
const CFGBlock & getExit() const
unsigned getNumBlockIDs() const
getNumBlockIDs - Returns the total number of BlockIDs allocated (which start at 0).
void setEntry(CFGBlock *B)
setEntry - Set the entry block of the CFG.
std::reverse_iterator< iterator > reverse_iterator
void addSyntheticDeclStmt(const DeclStmt *Synthetic, const DeclStmt *Source)
Records a synthetic DeclStmt and the DeclStmt it was constructed from.
void dump(const LangOptions &LO, bool ShowColors) const
dump - A simple pretty printer of a CFG that outputs to stderr.
CFGTemporaryDtor - Represents C++ object destructor implicitly generated at the end of full expressio...
static bool FilterEdge(const FilterOptions &F, const CFGBlock *Src, const CFGBlock *Dst)
static nodes_iterator nodes_end(::clang::CFG *F)
const_succ_reverse_iterator succ_rbegin() const
Optional< T > getAs() const
Convert to the specified CFGElement type, returning None if this CFGElement is not of the desired typ...
const CFGBlock * getIndirectGotoBlock() const
filtered_succ_iterator filtered_succ_start_end(const FilterOptions &f) const