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());
305 llvm::PointerIntPair<Stmt *, 1> Data;
309 : Data(S, TemporaryDtorsBranch) {}
361 typedef std::reverse_iterator<ImplTy::iterator>
iterator;
362 typedef std::reverse_iterator<ImplTy::const_iterator>
const_iterator;
370 return Impl.insert(I, Cnt, E, C);
373 const_reference
front()
const {
return Impl.back(); }
374 const_reference
back()
const {
return Impl.front(); }
386 assert(i < Impl.size());
387 return Impl[Impl.size() - 1 - i];
390 size_t size()
const {
return Impl.size(); }
391 bool empty()
const {
return Impl.empty(); }
395 ElementList Elements;
410 const Stmt *LoopTarget;
430 llvm::PointerIntPair<CFGBlock*, 2> UnreachableBlock;
442 return ReachableBlock;
447 return UnreachableBlock.getPointer();
465 Kind K = (Kind) UnreachableBlock.getInt();
466 return K == AB_Normal || K == AB_Alternate;
474 AdjacentBlocks Preds;
475 AdjacentBlocks Succs;
486 unsigned HasNoReturnElement : 1;
493 : Elements(C), Label(nullptr), Terminator(nullptr), LoopTarget(nullptr),
494 BlockID(blockid), Preds(C, 1), Succs(C, 1), HasNoReturnElement(
false),
516 unsigned size()
const {
return Elements.size(); }
517 bool empty()
const {
return Elements.empty(); }
573 template <
typename IMPL,
bool IsPred>
583 : I(i), E(e), F(f), From(from) {
584 while (
hasMore() && Filter(*I))
591 do { ++I; }
while (
hasMore() && Filter(*I));
598 return IsPred ?
FilterEdge(F, To, From) : FilterEdge(F, From, To);
602 typedef FilteredCFGBlockIterator<const_pred_iterator, true>
647 bool ShowColors)
const;
657 Elements.push_back(
CFGStmt(statement), C);
695 return iterator(Elements.insert(I.base(), Cnt,
711 bool isAlwaysTrue) {}
729 std::bitset<Stmt::lastStmtConstant> alwaysAddMask;
744 return alwaysAddMask[stmt->getStmtClass()];
748 alwaysAddMask[stmtClass] = val;
816 const BuildOptions &BO);
873 return TryDispatchBlocks.begin();
876 return TryDispatchBlocks.end();
880 TryDispatchBlocks.push_back(block);
889 assert(Synthetic->
isSingleDecl() &&
"Can handle single declarations only");
890 assert(Synthetic != Source &&
"Don't include original DeclStmts in map");
891 assert(!SyntheticDeclStmts.count(Synthetic) &&
"Already in map");
892 SyntheticDeclStmts[Synthetic] = Source;
895 typedef llvm::DenseMap<const DeclStmt *, const DeclStmt *>::const_iterator
904 return SyntheticDeclStmts.begin();
909 return SyntheticDeclStmts.end();
916 template <
typename CALLBACK>
922 O(const_cast<Stmt*>(
stmt->getStmt()));
937 unsigned size()
const {
return NumBlockIDs; }
952 : Entry(nullptr), Exit(nullptr), IndirectGotoBlock(nullptr), NumBlockIDs(0),
953 Blocks(BlkBVC, 10) {}
968 unsigned NumBlockIDs;
976 std::vector<const CFGBlock *> TryDispatchBlocks;
980 llvm::DenseMap<const DeclStmt *, const DeclStmt *> SyntheticDeclStmts;
1023 {
return N->succ_begin(); }
1026 {
return N->succ_end(); }
1051 {
return N->pred_begin(); }
1054 {
return N->pred_end(); }
1060 :
public GraphTraits< ::clang::CFGBlock *> {
1071 :
public GraphTraits<const ::clang::CFGBlock *> {
1076 return &F->getEntry();
1079 return F->nodes_begin();
1082 return F->nodes_end();
1084 static unsigned size(const ::clang::CFG* F) {
1090 :
public GraphTraits<Inverse< ::clang::CFGBlock*> > {
1100 :
public GraphTraits<Inverse<const ::clang::CFGBlock*> > {
1104 static NodeType *
getEntryNode(const ::clang::CFG* F) {
return &F->getExit(); }
1106 return F->nodes_begin();
1109 return F->nodes_end();
FilteredCFGBlockIterator< const_pred_iterator, true > filtered_pred_iterator
void setIndirectGotoBlock(CFGBlock *B)
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
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.
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
const Stmt * getLabel() const
iterator insertAutomaticObjDtor(iterator I, VarDecl *VD, Stmt *S)
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 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)
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)
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)
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
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
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.
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
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. "delete[] pArray"...
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
void appendMemberDtor(FieldDecl *FD, BumpVectorContext &C)
try_block_iterator try_blocks_end() const
void appendInitializer(CXXCtorInitializer *initializer, BumpVectorContext &C)
CFGInitializer(CXXCtorInitializer *initializer)
const CFGBlock value_type
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
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)
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.
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
CFGBaseDtor(const CXXBaseSpecifier *base)
const CFGTerminator getTerminator() const
CFGTemporaryDtor(CXXBindTemporaryExpr *expr)
const CXXBaseSpecifier * getBaseSpecifier() const
const CFGBlock & getExit() const
unsigned getNumBlockIDs() const
void setEntry(CFGBlock *B)
std::reverse_iterator< iterator > reverse_iterator
void addSyntheticDeclStmt(const DeclStmt *Synthetic, const DeclStmt *Source)
void dump(const LangOptions &LO, bool ShowColors) const
dump - A simple pretty printer of a CFG that outputs to stderr.
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