1 //===- CFG.h - Classes for representing and building CFGs -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the CFG and CFGBuilder classes for representing and
11 // building Control-Flow Graphs (CFGs) from ASTs.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_ANALYSIS_CFG_H
16 #define LLVM_CLANG_ANALYSIS_CFG_H
18 #include "clang/Analysis/Support/BumpVector.h"
19 #include "clang/Analysis/ConstructionContext.h"
20 #include "clang/AST/ExprCXX.h"
21 #include "clang/AST/ExprObjC.h"
22 #include "clang/Basic/LLVM.h"
23 #include "llvm/ADT/DenseMap.h"
24 #include "llvm/ADT/GraphTraits.h"
25 #include "llvm/ADT/None.h"
26 #include "llvm/ADT/Optional.h"
27 #include "llvm/ADT/PointerIntPair.h"
28 #include "llvm/ADT/iterator_range.h"
29 #include "llvm/Support/Allocator.h"
30 #include "llvm/Support/raw_ostream.h"
43 class CXXBaseSpecifier;
44 class CXXBindTemporaryExpr;
45 class CXXCtorInitializer;
47 class CXXDestructorDecl;
55 /// Represents a top-level expression in a basic block.
70 STMT_BEGIN = Statement,
71 STMT_END = CXXRecordTypedCall,
78 DTOR_BEGIN = AutomaticObjectDtor,
79 DTOR_END = TemporaryDtor
83 // The int bits are used to mark the kind.
84 llvm::PointerIntPair<void *, 2> Data1;
85 llvm::PointerIntPair<void *, 2> Data2;
87 CFGElement(Kind kind, const void *Ptr1, const void *Ptr2 = nullptr)
88 : Data1(const_cast<void*>(Ptr1), ((unsigned) kind) & 0x3),
89 Data2(const_cast<void*>(Ptr2), (((unsigned) kind) >> 2) & 0x3) {
90 assert(getKind() == kind);
93 CFGElement() = default;
96 /// Convert to the specified CFGElement type, asserting that this
97 /// CFGElement is of the desired type.
100 assert(T::isKind(*this));
107 /// Convert to the specified CFGElement type, returning None if this
108 /// CFGElement is not of the desired type.
110 Optional<T> getAs() const {
111 if (!T::isKind(*this))
119 Kind getKind() const {
120 unsigned x = Data2.getInt();
127 class CFGStmt : public CFGElement {
129 explicit CFGStmt(Stmt *S, Kind K = Statement) : CFGElement(K, S) {
130 assert(isKind(*this));
133 const Stmt *getStmt() const {
134 return static_cast<const Stmt *>(Data1.getPointer());
138 friend class CFGElement;
140 static bool isKind(const CFGElement &E) {
141 return E.getKind() >= STMT_BEGIN && E.getKind() <= STMT_END;
148 /// Represents C++ constructor call. Maintains information necessary to figure
149 /// out what memory is being initialized by the constructor expression. For now
150 /// this is only used by the analyzer's CFG.
151 class CFGConstructor : public CFGStmt {
153 explicit CFGConstructor(CXXConstructExpr *CE, const ConstructionContext *C)
154 : CFGStmt(CE, Constructor) {
156 Data2.setPointer(const_cast<ConstructionContext *>(C));
159 const ConstructionContext *getConstructionContext() const {
160 return static_cast<ConstructionContext *>(Data2.getPointer());
164 friend class CFGElement;
166 CFGConstructor() = default;
168 static bool isKind(const CFGElement &E) {
169 return E.getKind() == Constructor;
173 /// Represents a function call that returns a C++ object by value. This, like
174 /// constructor, requires a construction context in order to understand the
175 /// storage of the returned object . In C such tracking is not necessary because
176 /// no additional effort is required for destroying the object or modeling copy
177 /// elision. Like CFGConstructor, this element is for now only used by the
179 class CFGCXXRecordTypedCall : public CFGStmt {
181 /// Returns true when call expression \p CE needs to be represented
182 /// by CFGCXXRecordTypedCall, as opposed to a regular CFGStmt.
183 static bool isCXXRecordTypedCall(Expr *E) {
184 assert(isa<CallExpr>(E) || isa<ObjCMessageExpr>(E));
185 // There is no such thing as reference-type expression. If the function
186 // returns a reference, it'll return the respective lvalue or xvalue
187 // instead, and we're only interested in objects.
188 return !E->isGLValue() &&
189 E->getType().getCanonicalType()->getAsCXXRecordDecl();
192 explicit CFGCXXRecordTypedCall(Expr *E, const ConstructionContext *C)
193 : CFGStmt(E, CXXRecordTypedCall) {
194 assert(isCXXRecordTypedCall(E));
195 assert(C && (isa<TemporaryObjectConstructionContext>(C) ||
196 // These are possible in C++17 due to mandatory copy elision.
197 isa<ReturnedValueConstructionContext>(C) ||
198 isa<VariableConstructionContext>(C) ||
199 isa<ConstructorInitializerConstructionContext>(C) ||
200 isa<ArgumentConstructionContext>(C)));
201 Data2.setPointer(const_cast<ConstructionContext *>(C));
204 const ConstructionContext *getConstructionContext() const {
205 return static_cast<ConstructionContext *>(Data2.getPointer());
209 friend class CFGElement;
211 CFGCXXRecordTypedCall() = default;
213 static bool isKind(const CFGElement &E) {
214 return E.getKind() == CXXRecordTypedCall;
218 /// Represents C++ base or member initializer from constructor's initialization
220 class CFGInitializer : public CFGElement {
222 explicit CFGInitializer(CXXCtorInitializer *initializer)
223 : CFGElement(Initializer, initializer) {}
225 CXXCtorInitializer* getInitializer() const {
226 return static_cast<CXXCtorInitializer*>(Data1.getPointer());
230 friend class CFGElement;
232 CFGInitializer() = default;
234 static bool isKind(const CFGElement &E) {
235 return E.getKind() == Initializer;
239 /// Represents C++ allocator call.
240 class CFGNewAllocator : public CFGElement {
242 explicit CFGNewAllocator(const CXXNewExpr *S)
243 : CFGElement(NewAllocator, S) {}
245 // Get the new expression.
246 const CXXNewExpr *getAllocatorExpr() const {
247 return static_cast<CXXNewExpr *>(Data1.getPointer());
251 friend class CFGElement;
253 CFGNewAllocator() = default;
255 static bool isKind(const CFGElement &elem) {
256 return elem.getKind() == NewAllocator;
260 /// Represents the point where a loop ends.
261 /// This element is is only produced when building the CFG for the static
262 /// analyzer and hidden behind the 'cfg-loopexit' analyzer config flag.
264 /// Note: a loop exit element can be reached even when the loop body was never
266 class CFGLoopExit : public CFGElement {
268 explicit CFGLoopExit(const Stmt *stmt) : CFGElement(LoopExit, stmt) {}
270 const Stmt *getLoopStmt() const {
271 return static_cast<Stmt *>(Data1.getPointer());
275 friend class CFGElement;
277 CFGLoopExit() = default;
279 static bool isKind(const CFGElement &elem) {
280 return elem.getKind() == LoopExit;
284 /// Represents the point where the lifetime of an automatic object ends
285 class CFGLifetimeEnds : public CFGElement {
287 explicit CFGLifetimeEnds(const VarDecl *var, const Stmt *stmt)
288 : CFGElement(LifetimeEnds, var, stmt) {}
290 const VarDecl *getVarDecl() const {
291 return static_cast<VarDecl *>(Data1.getPointer());
294 const Stmt *getTriggerStmt() const {
295 return static_cast<Stmt *>(Data2.getPointer());
299 friend class CFGElement;
301 CFGLifetimeEnds() = default;
303 static bool isKind(const CFGElement &elem) {
304 return elem.getKind() == LifetimeEnds;
308 /// Represents beginning of a scope implicitly generated
309 /// by the compiler on encountering a CompoundStmt
310 class CFGScopeBegin : public CFGElement {
313 CFGScopeBegin(const VarDecl *VD, const Stmt *S)
314 : CFGElement(ScopeBegin, VD, S) {}
316 // Get statement that triggered a new scope.
317 const Stmt *getTriggerStmt() const {
318 return static_cast<Stmt*>(Data2.getPointer());
321 // Get VD that triggered a new scope.
322 const VarDecl *getVarDecl() const {
323 return static_cast<VarDecl *>(Data1.getPointer());
327 friend class CFGElement;
328 static bool isKind(const CFGElement &E) {
329 Kind kind = E.getKind();
330 return kind == ScopeBegin;
334 /// Represents end of a scope implicitly generated by
335 /// the compiler after the last Stmt in a CompoundStmt's body
336 class CFGScopeEnd : public CFGElement {
339 CFGScopeEnd(const VarDecl *VD, const Stmt *S) : CFGElement(ScopeEnd, VD, S) {}
341 const VarDecl *getVarDecl() const {
342 return static_cast<VarDecl *>(Data1.getPointer());
345 const Stmt *getTriggerStmt() const {
346 return static_cast<Stmt *>(Data2.getPointer());
350 friend class CFGElement;
351 static bool isKind(const CFGElement &E) {
352 Kind kind = E.getKind();
353 return kind == ScopeEnd;
357 /// Represents C++ object destructor implicitly generated by compiler on various
359 class CFGImplicitDtor : public CFGElement {
361 CFGImplicitDtor() = default;
363 CFGImplicitDtor(Kind kind, const void *data1, const void *data2 = nullptr)
364 : CFGElement(kind, data1, data2) {
365 assert(kind >= DTOR_BEGIN && kind <= DTOR_END);
369 const CXXDestructorDecl *getDestructorDecl(ASTContext &astContext) const;
370 bool isNoReturn(ASTContext &astContext) const;
373 friend class CFGElement;
375 static bool isKind(const CFGElement &E) {
376 Kind kind = E.getKind();
377 return kind >= DTOR_BEGIN && kind <= DTOR_END;
381 /// Represents C++ object destructor implicitly generated for automatic object
382 /// or temporary bound to const reference at the point of leaving its local
384 class CFGAutomaticObjDtor: public CFGImplicitDtor {
386 CFGAutomaticObjDtor(const VarDecl *var, const Stmt *stmt)
387 : CFGImplicitDtor(AutomaticObjectDtor, var, stmt) {}
389 const VarDecl *getVarDecl() const {
390 return static_cast<VarDecl*>(Data1.getPointer());
393 // Get statement end of which triggered the destructor call.
394 const Stmt *getTriggerStmt() const {
395 return static_cast<Stmt*>(Data2.getPointer());
399 friend class CFGElement;
401 CFGAutomaticObjDtor() = default;
403 static bool isKind(const CFGElement &elem) {
404 return elem.getKind() == AutomaticObjectDtor;
408 /// Represents C++ object destructor generated from a call to delete.
409 class CFGDeleteDtor : public CFGImplicitDtor {
411 CFGDeleteDtor(const CXXRecordDecl *RD, const CXXDeleteExpr *DE)
412 : CFGImplicitDtor(DeleteDtor, RD, DE) {}
414 const CXXRecordDecl *getCXXRecordDecl() const {
415 return static_cast<CXXRecordDecl*>(Data1.getPointer());
418 // Get Delete expression which triggered the destructor call.
419 const CXXDeleteExpr *getDeleteExpr() const {
420 return static_cast<CXXDeleteExpr *>(Data2.getPointer());
424 friend class CFGElement;
426 CFGDeleteDtor() = default;
428 static bool isKind(const CFGElement &elem) {
429 return elem.getKind() == DeleteDtor;
433 /// Represents C++ object destructor implicitly generated for base object in
435 class CFGBaseDtor : public CFGImplicitDtor {
437 CFGBaseDtor(const CXXBaseSpecifier *base)
438 : CFGImplicitDtor(BaseDtor, base) {}
440 const CXXBaseSpecifier *getBaseSpecifier() const {
441 return static_cast<const CXXBaseSpecifier*>(Data1.getPointer());
445 friend class CFGElement;
447 CFGBaseDtor() = default;
449 static bool isKind(const CFGElement &E) {
450 return E.getKind() == BaseDtor;
454 /// Represents C++ object destructor implicitly generated for member object in
456 class CFGMemberDtor : public CFGImplicitDtor {
458 CFGMemberDtor(const FieldDecl *field)
459 : CFGImplicitDtor(MemberDtor, field, nullptr) {}
461 const FieldDecl *getFieldDecl() const {
462 return static_cast<const FieldDecl*>(Data1.getPointer());
466 friend class CFGElement;
468 CFGMemberDtor() = default;
470 static bool isKind(const CFGElement &E) {
471 return E.getKind() == MemberDtor;
475 /// Represents C++ object destructor implicitly generated at the end of full
476 /// expression for temporary object.
477 class CFGTemporaryDtor : public CFGImplicitDtor {
479 CFGTemporaryDtor(CXXBindTemporaryExpr *expr)
480 : CFGImplicitDtor(TemporaryDtor, expr, nullptr) {}
482 const CXXBindTemporaryExpr *getBindTemporaryExpr() const {
483 return static_cast<const CXXBindTemporaryExpr *>(Data1.getPointer());
487 friend class CFGElement;
489 CFGTemporaryDtor() = default;
491 static bool isKind(const CFGElement &E) {
492 return E.getKind() == TemporaryDtor;
496 /// Represents CFGBlock terminator statement.
498 /// TemporaryDtorsBranch bit is set to true if the terminator marks a branch
499 /// in control flow of destructors of temporaries. In this case terminator
500 /// statement is the same statement that branches control flow in evaluation
501 /// of matching full expression.
502 class CFGTerminator {
503 llvm::PointerIntPair<Stmt *, 1> Data;
506 CFGTerminator() = default;
507 CFGTerminator(Stmt *S, bool TemporaryDtorsBranch = false)
508 : Data(S, TemporaryDtorsBranch) {}
510 Stmt *getStmt() { return Data.getPointer(); }
511 const Stmt *getStmt() const { return Data.getPointer(); }
513 bool isTemporaryDtorsBranch() const { return Data.getInt(); }
515 operator Stmt *() { return getStmt(); }
516 operator const Stmt *() const { return getStmt(); }
518 Stmt *operator->() { return getStmt(); }
519 const Stmt *operator->() const { return getStmt(); }
521 Stmt &operator*() { return *getStmt(); }
522 const Stmt &operator*() const { return *getStmt(); }
524 explicit operator bool() const { return getStmt(); }
527 /// Represents a single basic block in a source-level CFG.
530 /// (1) A set of statements/expressions (which may contain subexpressions).
531 /// (2) A "terminator" statement (not in the set of statements).
532 /// (3) A list of successors and predecessors.
534 /// Terminator: The terminator represents the type of control-flow that occurs
535 /// at the end of the basic block. The terminator is a Stmt* referring to an
536 /// AST node that has control-flow: if-statements, breaks, loops, etc.
537 /// If the control-flow is conditional, the condition expression will appear
538 /// within the set of statements in the block (usually the last statement).
540 /// Predecessors: the order in the set of predecessors is arbitrary.
542 /// Successors: the order in the set of successors is NOT arbitrary. We
543 /// currently have the following orderings based on the terminator:
545 /// Terminator Successor Ordering
546 /// -----------------------------------------------------
547 /// if Then Block; Else Block
548 /// ? operator LHS expression; RHS expression
549 /// &&, || expression that uses result of && or ||, RHS
551 /// But note that any of that may be NULL in case of optimized-out edges.
554 using ImplTy = BumpVector<CFGElement>;
559 ElementList(BumpVectorContext &C) : Impl(C, 4) {}
561 using iterator = std::reverse_iterator<ImplTy::iterator>;
562 using const_iterator = std::reverse_iterator<ImplTy::const_iterator>;
563 using reverse_iterator = ImplTy::iterator;
564 using const_reverse_iterator = ImplTy::const_iterator;
565 using const_reference = ImplTy::const_reference;
567 void push_back(CFGElement e, BumpVectorContext &C) { Impl.push_back(e, C); }
569 reverse_iterator insert(reverse_iterator I, size_t Cnt, CFGElement E,
570 BumpVectorContext &C) {
571 return Impl.insert(I, Cnt, E, C);
574 const_reference front() const { return Impl.back(); }
575 const_reference back() const { return Impl.front(); }
577 iterator begin() { return Impl.rbegin(); }
578 iterator end() { return Impl.rend(); }
579 const_iterator begin() const { return Impl.rbegin(); }
580 const_iterator end() const { return Impl.rend(); }
581 reverse_iterator rbegin() { return Impl.begin(); }
582 reverse_iterator rend() { return Impl.end(); }
583 const_reverse_iterator rbegin() const { return Impl.begin(); }
584 const_reverse_iterator rend() const { return Impl.end(); }
586 CFGElement operator[](size_t i) const {
587 assert(i < Impl.size());
588 return Impl[Impl.size() - 1 - i];
591 size_t size() const { return Impl.size(); }
592 bool empty() const { return Impl.empty(); }
595 /// The set of statements in the basic block.
596 ElementList Elements;
598 /// An (optional) label that prefixes the executable statements in the block.
599 /// When this variable is non-NULL, it is either an instance of LabelStmt,
600 /// SwitchCase or CXXCatchStmt.
601 Stmt *Label = nullptr;
603 /// The terminator for a basic block that indicates the type of control-flow
604 /// that occurs between a block and its successors.
605 CFGTerminator Terminator;
607 /// Some blocks are used to represent the "loop edge" to the start of a loop
608 /// from within the loop body. This Stmt* will be refer to the loop statement
609 /// for such blocks (and be null otherwise).
610 const Stmt *LoopTarget = nullptr;
612 /// A numerical ID assigned to a CFGBlock during construction of the CFG.
616 /// This class represents a potential adjacent block in the CFG. It encodes
617 /// whether or not the block is actually reachable, or can be proved to be
618 /// trivially unreachable. For some cases it allows one to encode scenarios
619 /// where a block was substituted because the original (now alternate) block
621 class AdjacentBlock {
628 CFGBlock *ReachableBlock;
629 llvm::PointerIntPair<CFGBlock *, 2> UnreachableBlock;
632 /// Construct an AdjacentBlock with a possibly unreachable block.
633 AdjacentBlock(CFGBlock *B, bool IsReachable);
635 /// Construct an AdjacentBlock with a reachable block and an alternate
636 /// unreachable block.
637 AdjacentBlock(CFGBlock *B, CFGBlock *AlternateBlock);
639 /// Get the reachable block, if one exists.
640 CFGBlock *getReachableBlock() const {
641 return ReachableBlock;
644 /// Get the potentially unreachable block.
645 CFGBlock *getPossiblyUnreachableBlock() const {
646 return UnreachableBlock.getPointer();
649 /// Provide an implicit conversion to CFGBlock* so that
650 /// AdjacentBlock can be substituted for CFGBlock*.
651 operator CFGBlock*() const {
652 return getReachableBlock();
655 CFGBlock& operator *() const {
656 return *getReachableBlock();
659 CFGBlock* operator ->() const {
660 return getReachableBlock();
663 bool isReachable() const {
664 Kind K = (Kind) UnreachableBlock.getInt();
665 return K == AB_Normal || K == AB_Alternate;
670 /// Keep track of the predecessor / successor CFG blocks.
671 using AdjacentBlocks = BumpVector<AdjacentBlock>;
672 AdjacentBlocks Preds;
673 AdjacentBlocks Succs;
675 /// This bit is set when the basic block contains a function call
676 /// or implicit destructor that is attributed as 'noreturn'. In that case,
677 /// control cannot technically ever proceed past this block. All such blocks
678 /// will have a single immediate successor: the exit block. This allows them
679 /// to be easily reached from the exit block and using this bit quickly
680 /// recognized without scanning the contents of the block.
682 /// Optimization Note: This bit could be profitably folded with Terminator's
683 /// storage if the memory usage of CFGBlock becomes an issue.
684 unsigned HasNoReturnElement : 1;
686 /// The parent CFG that owns this CFGBlock.
690 explicit CFGBlock(unsigned blockid, BumpVectorContext &C, CFG *parent)
691 : Elements(C), Terminator(nullptr), BlockID(blockid), Preds(C, 1),
692 Succs(C, 1), HasNoReturnElement(false), Parent(parent) {}
694 // Statement iterators
695 using iterator = ElementList::iterator;
696 using const_iterator = ElementList::const_iterator;
697 using reverse_iterator = ElementList::reverse_iterator;
698 using const_reverse_iterator = ElementList::const_reverse_iterator;
700 CFGElement front() const { return Elements.front(); }
701 CFGElement back() const { return Elements.back(); }
703 iterator begin() { return Elements.begin(); }
704 iterator end() { return Elements.end(); }
705 const_iterator begin() const { return Elements.begin(); }
706 const_iterator end() const { return Elements.end(); }
708 reverse_iterator rbegin() { return Elements.rbegin(); }
709 reverse_iterator rend() { return Elements.rend(); }
710 const_reverse_iterator rbegin() const { return Elements.rbegin(); }
711 const_reverse_iterator rend() const { return Elements.rend(); }
713 unsigned size() const { return Elements.size(); }
714 bool empty() const { return Elements.empty(); }
716 CFGElement operator[](size_t i) const { return Elements[i]; }
719 using pred_iterator = AdjacentBlocks::iterator;
720 using const_pred_iterator = AdjacentBlocks::const_iterator;
721 using pred_reverse_iterator = AdjacentBlocks::reverse_iterator;
722 using const_pred_reverse_iterator = AdjacentBlocks::const_reverse_iterator;
723 using pred_range = llvm::iterator_range<pred_iterator>;
724 using pred_const_range = llvm::iterator_range<const_pred_iterator>;
726 using succ_iterator = AdjacentBlocks::iterator;
727 using const_succ_iterator = AdjacentBlocks::const_iterator;
728 using succ_reverse_iterator = AdjacentBlocks::reverse_iterator;
729 using const_succ_reverse_iterator = AdjacentBlocks::const_reverse_iterator;
730 using succ_range = llvm::iterator_range<succ_iterator>;
731 using succ_const_range = llvm::iterator_range<const_succ_iterator>;
733 pred_iterator pred_begin() { return Preds.begin(); }
734 pred_iterator pred_end() { return Preds.end(); }
735 const_pred_iterator pred_begin() const { return Preds.begin(); }
736 const_pred_iterator pred_end() const { return Preds.end(); }
738 pred_reverse_iterator pred_rbegin() { return Preds.rbegin(); }
739 pred_reverse_iterator pred_rend() { return Preds.rend(); }
740 const_pred_reverse_iterator pred_rbegin() const { return Preds.rbegin(); }
741 const_pred_reverse_iterator pred_rend() const { return Preds.rend(); }
744 return pred_range(pred_begin(), pred_end());
747 pred_const_range preds() const {
748 return pred_const_range(pred_begin(), pred_end());
751 succ_iterator succ_begin() { return Succs.begin(); }
752 succ_iterator succ_end() { return Succs.end(); }
753 const_succ_iterator succ_begin() const { return Succs.begin(); }
754 const_succ_iterator succ_end() const { return Succs.end(); }
756 succ_reverse_iterator succ_rbegin() { return Succs.rbegin(); }
757 succ_reverse_iterator succ_rend() { return Succs.rend(); }
758 const_succ_reverse_iterator succ_rbegin() const { return Succs.rbegin(); }
759 const_succ_reverse_iterator succ_rend() const { return Succs.rend(); }
762 return succ_range(succ_begin(), succ_end());
765 succ_const_range succs() const {
766 return succ_const_range(succ_begin(), succ_end());
769 unsigned succ_size() const { return Succs.size(); }
770 bool succ_empty() const { return Succs.empty(); }
772 unsigned pred_size() const { return Preds.size(); }
773 bool pred_empty() const { return Preds.empty(); }
776 class FilterOptions {
778 unsigned IgnoreNullPredecessors : 1;
779 unsigned IgnoreDefaultsWithCoveredEnums : 1;
782 : IgnoreNullPredecessors(1), IgnoreDefaultsWithCoveredEnums(0) {}
785 static bool FilterEdge(const FilterOptions &F, const CFGBlock *Src,
786 const CFGBlock *Dst);
788 template <typename IMPL, bool IsPred>
789 class FilteredCFGBlockIterator {
792 const FilterOptions F;
793 const CFGBlock *From;
796 explicit FilteredCFGBlockIterator(const IMPL &i, const IMPL &e,
797 const CFGBlock *from,
798 const FilterOptions &f)
799 : I(i), E(e), F(f), From(from) {
800 while (hasMore() && Filter(*I))
804 bool hasMore() const { return I != E; }
806 FilteredCFGBlockIterator &operator++() {
807 do { ++I; } while (hasMore() && Filter(*I));
811 const CFGBlock *operator*() const { return *I; }
814 bool Filter(const CFGBlock *To) {
815 return IsPred ? FilterEdge(F, To, From) : FilterEdge(F, From, To);
819 using filtered_pred_iterator =
820 FilteredCFGBlockIterator<const_pred_iterator, true>;
822 using filtered_succ_iterator =
823 FilteredCFGBlockIterator<const_succ_iterator, false>;
825 filtered_pred_iterator filtered_pred_start_end(const FilterOptions &f) const {
826 return filtered_pred_iterator(pred_begin(), pred_end(), this, f);
829 filtered_succ_iterator filtered_succ_start_end(const FilterOptions &f) const {
830 return filtered_succ_iterator(succ_begin(), succ_end(), this, f);
833 // Manipulation of block contents
835 void setTerminator(CFGTerminator Term) { Terminator = Term; }
836 void setLabel(Stmt *Statement) { Label = Statement; }
837 void setLoopTarget(const Stmt *loopTarget) { LoopTarget = loopTarget; }
838 void setHasNoReturnElement() { HasNoReturnElement = true; }
840 CFGTerminator getTerminator() { return Terminator; }
841 const CFGTerminator getTerminator() const { return Terminator; }
843 Stmt *getTerminatorCondition(bool StripParens = true);
845 const Stmt *getTerminatorCondition(bool StripParens = true) const {
846 return const_cast<CFGBlock*>(this)->getTerminatorCondition(StripParens);
849 const Stmt *getLoopTarget() const { return LoopTarget; }
851 Stmt *getLabel() { return Label; }
852 const Stmt *getLabel() const { return Label; }
854 bool hasNoReturnElement() const { return HasNoReturnElement; }
856 unsigned getBlockID() const { return BlockID; }
858 CFG *getParent() const { return Parent; }
862 void dump(const CFG *cfg, const LangOptions &LO, bool ShowColors = false) const;
863 void print(raw_ostream &OS, const CFG* cfg, const LangOptions &LO,
864 bool ShowColors) const;
865 void printTerminator(raw_ostream &OS, const LangOptions &LO) const;
866 void printAsOperand(raw_ostream &OS, bool /*PrintType*/) {
867 OS << "BB#" << getBlockID();
870 /// Adds a (potentially unreachable) successor block to the current block.
871 void addSuccessor(AdjacentBlock Succ, BumpVectorContext &C);
873 void appendStmt(Stmt *statement, BumpVectorContext &C) {
874 Elements.push_back(CFGStmt(statement), C);
877 void appendConstructor(CXXConstructExpr *CE, const ConstructionContext *CC,
878 BumpVectorContext &C) {
879 Elements.push_back(CFGConstructor(CE, CC), C);
882 void appendCXXRecordTypedCall(Expr *E,
883 const ConstructionContext *CC,
884 BumpVectorContext &C) {
885 Elements.push_back(CFGCXXRecordTypedCall(E, CC), C);
888 void appendInitializer(CXXCtorInitializer *initializer,
889 BumpVectorContext &C) {
890 Elements.push_back(CFGInitializer(initializer), C);
893 void appendNewAllocator(CXXNewExpr *NE,
894 BumpVectorContext &C) {
895 Elements.push_back(CFGNewAllocator(NE), C);
898 void appendScopeBegin(const VarDecl *VD, const Stmt *S,
899 BumpVectorContext &C) {
900 Elements.push_back(CFGScopeBegin(VD, S), C);
903 void prependScopeBegin(const VarDecl *VD, const Stmt *S,
904 BumpVectorContext &C) {
905 Elements.insert(Elements.rbegin(), 1, CFGScopeBegin(VD, S), C);
908 void appendScopeEnd(const VarDecl *VD, const Stmt *S, BumpVectorContext &C) {
909 Elements.push_back(CFGScopeEnd(VD, S), C);
912 void prependScopeEnd(const VarDecl *VD, const Stmt *S, BumpVectorContext &C) {
913 Elements.insert(Elements.rbegin(), 1, CFGScopeEnd(VD, S), C);
916 void appendBaseDtor(const CXXBaseSpecifier *BS, BumpVectorContext &C) {
917 Elements.push_back(CFGBaseDtor(BS), C);
920 void appendMemberDtor(FieldDecl *FD, BumpVectorContext &C) {
921 Elements.push_back(CFGMemberDtor(FD), C);
924 void appendTemporaryDtor(CXXBindTemporaryExpr *E, BumpVectorContext &C) {
925 Elements.push_back(CFGTemporaryDtor(E), C);
928 void appendAutomaticObjDtor(VarDecl *VD, Stmt *S, BumpVectorContext &C) {
929 Elements.push_back(CFGAutomaticObjDtor(VD, S), C);
932 void appendLifetimeEnds(VarDecl *VD, Stmt *S, BumpVectorContext &C) {
933 Elements.push_back(CFGLifetimeEnds(VD, S), C);
936 void appendLoopExit(const Stmt *LoopStmt, BumpVectorContext &C) {
937 Elements.push_back(CFGLoopExit(LoopStmt), C);
940 void appendDeleteDtor(CXXRecordDecl *RD, CXXDeleteExpr *DE, BumpVectorContext &C) {
941 Elements.push_back(CFGDeleteDtor(RD, DE), C);
944 // Destructors must be inserted in reversed order. So insertion is in two
945 // steps. First we prepare space for some number of elements, then we insert
946 // the elements beginning at the last position in prepared space.
947 iterator beginAutomaticObjDtorsInsert(iterator I, size_t Cnt,
948 BumpVectorContext &C) {
949 return iterator(Elements.insert(I.base(), Cnt,
950 CFGAutomaticObjDtor(nullptr, nullptr), C));
952 iterator insertAutomaticObjDtor(iterator I, VarDecl *VD, Stmt *S) {
953 *I = CFGAutomaticObjDtor(VD, S);
957 // Scope leaving must be performed in reversed order. So insertion is in two
958 // steps. First we prepare space for some number of elements, then we insert
959 // the elements beginning at the last position in prepared space.
960 iterator beginLifetimeEndsInsert(iterator I, size_t Cnt,
961 BumpVectorContext &C) {
963 Elements.insert(I.base(), Cnt, CFGLifetimeEnds(nullptr, nullptr), C));
965 iterator insertLifetimeEnds(iterator I, VarDecl *VD, Stmt *S) {
966 *I = CFGLifetimeEnds(VD, S);
970 // Scope leaving must be performed in reversed order. So insertion is in two
971 // steps. First we prepare space for some number of elements, then we insert
972 // the elements beginning at the last position in prepared space.
973 iterator beginScopeEndInsert(iterator I, size_t Cnt, BumpVectorContext &C) {
975 Elements.insert(I.base(), Cnt, CFGScopeEnd(nullptr, nullptr), C));
977 iterator insertScopeEnd(iterator I, VarDecl *VD, Stmt *S) {
978 *I = CFGScopeEnd(VD, S);
984 /// CFGCallback defines methods that should be called when a logical
985 /// operator error is found when building the CFG.
988 CFGCallback() = default;
989 virtual ~CFGCallback() = default;
991 virtual void compareAlwaysTrue(const BinaryOperator *B, bool isAlwaysTrue) {}
992 virtual void compareBitwiseEquality(const BinaryOperator *B,
993 bool isAlwaysTrue) {}
996 /// Represents a source-level, intra-procedural CFG that represents the
997 /// control-flow of a Stmt. The Stmt can represent an entire function body,
998 /// or a single expression. A CFG will always contain one empty block that
999 /// represents the Exit point of the CFG. A CFG will also contain a designated
1000 /// Entry block. The CFG solely represents control-flow; it consists of
1001 /// CFGBlocks which are simply containers of Stmt*'s in the AST the CFG
1002 /// was constructed from.
1005 //===--------------------------------------------------------------------===//
1006 // CFG Construction & Manipulation.
1007 //===--------------------------------------------------------------------===//
1009 class BuildOptions {
1010 std::bitset<Stmt::lastStmtConstant> alwaysAddMask;
1013 using ForcedBlkExprs = llvm::DenseMap<const Stmt *, const CFGBlock *>;
1015 ForcedBlkExprs **forcedBlkExprs = nullptr;
1016 CFGCallback *Observer = nullptr;
1017 bool PruneTriviallyFalseEdges = true;
1018 bool AddEHEdges = false;
1019 bool AddInitializers = false;
1020 bool AddImplicitDtors = false;
1021 bool AddLifetime = false;
1022 bool AddLoopExit = false;
1023 bool AddTemporaryDtors = false;
1024 bool AddScopes = false;
1025 bool AddStaticInitBranches = false;
1026 bool AddCXXNewAllocator = false;
1027 bool AddCXXDefaultInitExprInCtors = false;
1028 bool AddRichCXXConstructors = false;
1029 bool MarkElidedCXXConstructors = false;
1031 BuildOptions() = default;
1033 bool alwaysAdd(const Stmt *stmt) const {
1034 return alwaysAddMask[stmt->getStmtClass()];
1037 BuildOptions &setAlwaysAdd(Stmt::StmtClass stmtClass, bool val = true) {
1038 alwaysAddMask[stmtClass] = val;
1042 BuildOptions &setAllAlwaysAdd() {
1043 alwaysAddMask.set();
1048 /// Builds a CFG from an AST.
1049 static std::unique_ptr<CFG> buildCFG(const Decl *D, Stmt *AST, ASTContext *C,
1050 const BuildOptions &BO);
1052 /// Create a new block in the CFG. The CFG owns the block; the caller should
1053 /// not directly free it.
1054 CFGBlock *createBlock();
1056 /// Set the entry block of the CFG. This is typically used only during CFG
1057 /// construction. Most CFG clients expect that the entry block has no
1058 /// predecessors and contains no statements.
1059 void setEntry(CFGBlock *B) { Entry = B; }
1061 /// Set the block used for indirect goto jumps. This is typically used only
1062 /// during CFG construction.
1063 void setIndirectGotoBlock(CFGBlock *B) { IndirectGotoBlock = B; }
1065 //===--------------------------------------------------------------------===//
1067 //===--------------------------------------------------------------------===//
1069 using CFGBlockListTy = BumpVector<CFGBlock *>;
1070 using iterator = CFGBlockListTy::iterator;
1071 using const_iterator = CFGBlockListTy::const_iterator;
1072 using reverse_iterator = std::reverse_iterator<iterator>;
1073 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
1075 CFGBlock & front() { return *Blocks.front(); }
1076 CFGBlock & back() { return *Blocks.back(); }
1078 iterator begin() { return Blocks.begin(); }
1079 iterator end() { return Blocks.end(); }
1080 const_iterator begin() const { return Blocks.begin(); }
1081 const_iterator end() const { return Blocks.end(); }
1083 iterator nodes_begin() { return iterator(Blocks.begin()); }
1084 iterator nodes_end() { return iterator(Blocks.end()); }
1085 const_iterator nodes_begin() const { return const_iterator(Blocks.begin()); }
1086 const_iterator nodes_end() const { return const_iterator(Blocks.end()); }
1088 reverse_iterator rbegin() { return Blocks.rbegin(); }
1089 reverse_iterator rend() { return Blocks.rend(); }
1090 const_reverse_iterator rbegin() const { return Blocks.rbegin(); }
1091 const_reverse_iterator rend() const { return Blocks.rend(); }
1093 CFGBlock & getEntry() { return *Entry; }
1094 const CFGBlock & getEntry() const { return *Entry; }
1095 CFGBlock & getExit() { return *Exit; }
1096 const CFGBlock & getExit() const { return *Exit; }
1098 CFGBlock * getIndirectGotoBlock() { return IndirectGotoBlock; }
1099 const CFGBlock * getIndirectGotoBlock() const { return IndirectGotoBlock; }
1101 using try_block_iterator = std::vector<const CFGBlock *>::const_iterator;
1103 try_block_iterator try_blocks_begin() const {
1104 return TryDispatchBlocks.begin();
1107 try_block_iterator try_blocks_end() const {
1108 return TryDispatchBlocks.end();
1111 void addTryDispatchBlock(const CFGBlock *block) {
1112 TryDispatchBlocks.push_back(block);
1115 /// Records a synthetic DeclStmt and the DeclStmt it was constructed from.
1117 /// The CFG uses synthetic DeclStmts when a single AST DeclStmt contains
1119 void addSyntheticDeclStmt(const DeclStmt *Synthetic,
1120 const DeclStmt *Source) {
1121 assert(Synthetic->isSingleDecl() && "Can handle single declarations only");
1122 assert(Synthetic != Source && "Don't include original DeclStmts in map");
1123 assert(!SyntheticDeclStmts.count(Synthetic) && "Already in map");
1124 SyntheticDeclStmts[Synthetic] = Source;
1127 using synthetic_stmt_iterator =
1128 llvm::DenseMap<const DeclStmt *, const DeclStmt *>::const_iterator;
1129 using synthetic_stmt_range = llvm::iterator_range<synthetic_stmt_iterator>;
1131 /// Iterates over synthetic DeclStmts in the CFG.
1133 /// Each element is a (synthetic statement, source statement) pair.
1135 /// \sa addSyntheticDeclStmt
1136 synthetic_stmt_iterator synthetic_stmt_begin() const {
1137 return SyntheticDeclStmts.begin();
1140 /// \sa synthetic_stmt_begin
1141 synthetic_stmt_iterator synthetic_stmt_end() const {
1142 return SyntheticDeclStmts.end();
1145 /// \sa synthetic_stmt_begin
1146 synthetic_stmt_range synthetic_stmts() const {
1147 return synthetic_stmt_range(synthetic_stmt_begin(), synthetic_stmt_end());
1150 //===--------------------------------------------------------------------===//
1151 // Member templates useful for various batch operations over CFGs.
1152 //===--------------------------------------------------------------------===//
1154 template <typename CALLBACK>
1155 void VisitBlockStmts(CALLBACK& O) const {
1156 for (const_iterator I = begin(), E = end(); I != E; ++I)
1157 for (CFGBlock::const_iterator BI = (*I)->begin(), BE = (*I)->end();
1159 if (Optional<CFGStmt> stmt = BI->getAs<CFGStmt>())
1160 O(const_cast<Stmt*>(stmt->getStmt()));
1164 //===--------------------------------------------------------------------===//
1165 // CFG Introspection.
1166 //===--------------------------------------------------------------------===//
1168 /// Returns the total number of BlockIDs allocated (which start at 0).
1169 unsigned getNumBlockIDs() const { return NumBlockIDs; }
1171 /// Return the total number of CFGBlocks within the CFG This is simply a
1172 /// renaming of the getNumBlockIDs(). This is necessary because the dominator
1173 /// implementation needs such an interface.
1174 unsigned size() const { return NumBlockIDs; }
1176 //===--------------------------------------------------------------------===//
1177 // CFG Debugging: Pretty-Printing and Visualization.
1178 //===--------------------------------------------------------------------===//
1180 void viewCFG(const LangOptions &LO) const;
1181 void print(raw_ostream &OS, const LangOptions &LO, bool ShowColors) const;
1182 void dump(const LangOptions &LO, bool ShowColors) const;
1184 //===--------------------------------------------------------------------===//
1185 // Internal: constructors and data.
1186 //===--------------------------------------------------------------------===//
1188 CFG() : Blocks(BlkBVC, 10) {}
1190 llvm::BumpPtrAllocator& getAllocator() {
1191 return BlkBVC.getAllocator();
1194 BumpVectorContext &getBumpVectorContext() {
1199 CFGBlock *Entry = nullptr;
1200 CFGBlock *Exit = nullptr;
1202 // Special block to contain collective dispatch for indirect gotos
1203 CFGBlock* IndirectGotoBlock = nullptr;
1205 unsigned NumBlockIDs = 0;
1207 BumpVectorContext BlkBVC;
1209 CFGBlockListTy Blocks;
1211 /// C++ 'try' statements are modeled with an indirect dispatch block.
1212 /// This is the collection of such blocks present in the CFG.
1213 std::vector<const CFGBlock *> TryDispatchBlocks;
1215 /// Collects DeclStmts synthesized for this CFG and maps each one back to its
1216 /// source DeclStmt.
1217 llvm::DenseMap<const DeclStmt *, const DeclStmt *> SyntheticDeclStmts;
1220 } // namespace clang
1222 //===----------------------------------------------------------------------===//
1223 // GraphTraits specializations for CFG basic block graphs (source-level CFGs)
1224 //===----------------------------------------------------------------------===//
1228 /// Implement simplify_type for CFGTerminator, so that we can dyn_cast from
1229 /// CFGTerminator to a specific Stmt class.
1230 template <> struct simplify_type< ::clang::CFGTerminator> {
1231 using SimpleType = ::clang::Stmt *;
1233 static SimpleType getSimplifiedValue(::clang::CFGTerminator Val) {
1234 return Val.getStmt();
1238 // Traits for: CFGBlock
1240 template <> struct GraphTraits< ::clang::CFGBlock *> {
1241 using NodeRef = ::clang::CFGBlock *;
1242 using ChildIteratorType = ::clang::CFGBlock::succ_iterator;
1244 static NodeRef getEntryNode(::clang::CFGBlock *BB) { return BB; }
1245 static ChildIteratorType child_begin(NodeRef N) { return N->succ_begin(); }
1246 static ChildIteratorType child_end(NodeRef N) { return N->succ_end(); }
1249 template <> struct GraphTraits< const ::clang::CFGBlock *> {
1250 using NodeRef = const ::clang::CFGBlock *;
1251 using ChildIteratorType = ::clang::CFGBlock::const_succ_iterator;
1253 static NodeRef getEntryNode(const clang::CFGBlock *BB) { return BB; }
1254 static ChildIteratorType child_begin(NodeRef N) { return N->succ_begin(); }
1255 static ChildIteratorType child_end(NodeRef N) { return N->succ_end(); }
1258 template <> struct GraphTraits<Inverse< ::clang::CFGBlock *>> {
1259 using NodeRef = ::clang::CFGBlock *;
1260 using ChildIteratorType = ::clang::CFGBlock::const_pred_iterator;
1262 static NodeRef getEntryNode(Inverse<::clang::CFGBlock *> G) {
1266 static ChildIteratorType child_begin(NodeRef N) { return N->pred_begin(); }
1267 static ChildIteratorType child_end(NodeRef N) { return N->pred_end(); }
1270 template <> struct GraphTraits<Inverse<const ::clang::CFGBlock *>> {
1271 using NodeRef = const ::clang::CFGBlock *;
1272 using ChildIteratorType = ::clang::CFGBlock::const_pred_iterator;
1274 static NodeRef getEntryNode(Inverse<const ::clang::CFGBlock *> G) {
1278 static ChildIteratorType child_begin(NodeRef N) { return N->pred_begin(); }
1279 static ChildIteratorType child_end(NodeRef N) { return N->pred_end(); }
1284 template <> struct GraphTraits< ::clang::CFG* >
1285 : public GraphTraits< ::clang::CFGBlock *> {
1286 using nodes_iterator = ::clang::CFG::iterator;
1288 static NodeRef getEntryNode(::clang::CFG *F) { return &F->getEntry(); }
1289 static nodes_iterator nodes_begin(::clang::CFG* F) { return F->nodes_begin();}
1290 static nodes_iterator nodes_end(::clang::CFG* F) { return F->nodes_end(); }
1291 static unsigned size(::clang::CFG* F) { return F->size(); }
1294 template <> struct GraphTraits<const ::clang::CFG* >
1295 : public GraphTraits<const ::clang::CFGBlock *> {
1296 using nodes_iterator = ::clang::CFG::const_iterator;
1298 static NodeRef getEntryNode(const ::clang::CFG *F) { return &F->getEntry(); }
1300 static nodes_iterator nodes_begin( const ::clang::CFG* F) {
1301 return F->nodes_begin();
1304 static nodes_iterator nodes_end( const ::clang::CFG* F) {
1305 return F->nodes_end();
1308 static unsigned size(const ::clang::CFG* F) {
1313 template <> struct GraphTraits<Inverse< ::clang::CFG *>>
1314 : public GraphTraits<Inverse< ::clang::CFGBlock *>> {
1315 using nodes_iterator = ::clang::CFG::iterator;
1317 static NodeRef getEntryNode(::clang::CFG *F) { return &F->getExit(); }
1318 static nodes_iterator nodes_begin( ::clang::CFG* F) {return F->nodes_begin();}
1319 static nodes_iterator nodes_end( ::clang::CFG* F) { return F->nodes_end(); }
1322 template <> struct GraphTraits<Inverse<const ::clang::CFG *>>
1323 : public GraphTraits<Inverse<const ::clang::CFGBlock *>> {
1324 using nodes_iterator = ::clang::CFG::const_iterator;
1326 static NodeRef getEntryNode(const ::clang::CFG *F) { return &F->getExit(); }
1328 static nodes_iterator nodes_begin(const ::clang::CFG* F) {
1329 return F->nodes_begin();
1332 static nodes_iterator nodes_end(const ::clang::CFG* F) {
1333 return F->nodes_end();
1339 #endif // LLVM_CLANG_ANALYSIS_CFG_H