clang  7.0.0svn
CFG.h
Go to the documentation of this file.
1 //===- CFG.h - Classes for representing and building CFGs -------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the CFG and CFGBuilder classes for representing and
11 // building Control-Flow Graphs (CFGs) from ASTs.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_CLANG_ANALYSIS_CFG_H
16 #define LLVM_CLANG_ANALYSIS_CFG_H
17 
18 #include "clang/AST/ExprCXX.h"
21 #include "clang/Basic/LLVM.h"
22 #include "llvm/ADT/DenseMap.h"
23 #include "llvm/ADT/GraphTraits.h"
24 #include "llvm/ADT/None.h"
25 #include "llvm/ADT/Optional.h"
26 #include "llvm/ADT/PointerIntPair.h"
27 #include "llvm/ADT/iterator_range.h"
28 #include "llvm/Support/Allocator.h"
29 #include "llvm/Support/raw_ostream.h"
30 #include <bitset>
31 #include <cassert>
32 #include <cstddef>
33 #include <iterator>
34 #include <memory>
35 #include <vector>
36 
37 namespace clang {
38 
39 class ASTContext;
40 class BinaryOperator;
41 class CFG;
42 class CXXBaseSpecifier;
43 class CXXBindTemporaryExpr;
44 class CXXCtorInitializer;
45 class CXXDeleteExpr;
46 class CXXDestructorDecl;
47 class CXXNewExpr;
48 class CXXRecordDecl;
49 class Decl;
50 class FieldDecl;
51 class LangOptions;
52 class VarDecl;
53 
54 /// CFGElement - Represents a top-level expression in a basic block.
55 class CFGElement {
56 public:
57  enum Kind {
58  // main kind
65  // stmt kind
71  // dtor kind
79  };
80 
81 protected:
82  // The int bits are used to mark the kind.
83  llvm::PointerIntPair<void *, 2> Data1;
84  llvm::PointerIntPair<void *, 2> Data2;
85 
86  CFGElement(Kind kind, const void *Ptr1, const void *Ptr2 = nullptr)
87  : Data1(const_cast<void*>(Ptr1), ((unsigned) kind) & 0x3),
88  Data2(const_cast<void*>(Ptr2), (((unsigned) kind) >> 2) & 0x3) {
89  assert(getKind() == kind);
90  }
91 
92  CFGElement() = default;
93 
94 public:
95  /// Convert to the specified CFGElement type, asserting that this
96  /// CFGElement is of the desired type.
97  template<typename T>
98  T castAs() const {
99  assert(T::isKind(*this));
100  T t;
101  CFGElement& e = t;
102  e = *this;
103  return t;
104  }
105 
106  /// Convert to the specified CFGElement type, returning None if this
107  /// CFGElement is not of the desired type.
108  template<typename T>
109  Optional<T> getAs() const {
110  if (!T::isKind(*this))
111  return None;
112  T t;
113  CFGElement& e = t;
114  e = *this;
115  return t;
116  }
117 
118  Kind getKind() const {
119  unsigned x = Data2.getInt();
120  x <<= 2;
121  x |= Data1.getInt();
122  return (Kind) x;
123  }
124 };
125 
126 class CFGStmt : public CFGElement {
127 public:
128  explicit CFGStmt(Stmt *S, Kind K = Statement) : CFGElement(K, S) {
129  assert(isKind(*this));
130  }
131 
132  const Stmt *getStmt() const {
133  return static_cast<const Stmt *>(Data1.getPointer());
134  }
135 
136 private:
137  friend class CFGElement;
138 
139  static bool isKind(const CFGElement &E) {
140  return E.getKind() >= STMT_BEGIN && E.getKind() <= STMT_END;
141  }
142 
143 protected:
144  CFGStmt() = default;
145 };
146 
147 /// CFGConstructor - Represents C++ constructor call. Maintains information
148 /// necessary to figure out what memory is being initialized by the
149 /// constructor expression. For now this is only used by the analyzer's CFG.
150 class CFGConstructor : public CFGStmt {
151 public:
153  : CFGStmt(CE, Constructor) {
154  assert(C);
155  Data2.setPointer(const_cast<ConstructionContext *>(C));
156  }
157 
159  return static_cast<ConstructionContext *>(Data2.getPointer());
160  }
161 
162 private:
163  friend class CFGElement;
164 
165  CFGConstructor() = default;
166 
167  static bool isKind(const CFGElement &E) {
168  return E.getKind() == Constructor;
169  }
170 };
171 
172 /// CFGCXXRecordTypedCall - Represents a function call that returns a C++ object
173 /// by value. This, like constructor, requires a construction context in order
174 /// to understand the storage of the returned object . In C such tracking is not
175 /// necessary because no additional effort is required for destroying the object
176 /// or modeling copy elision. Like CFGConstructor, this element is for now only
177 /// used by the analyzer's CFG.
179 public:
180  /// Returns true when call expression \p CE needs to be represented
181  /// by CFGCXXRecordTypedCall, as opposed to a regular CFGStmt.
182  static bool isCXXRecordTypedCall(CallExpr *CE, const ASTContext &ACtx) {
184  }
185 
187  : CFGStmt(CE, CXXRecordTypedCall) {
188  // FIXME: This is not protected against squeezing a non-record-typed-call
189  // into the constructor. An assertion would require passing an ASTContext
190  // which would mean paying for something we don't use.
191  assert(C && (isa<TemporaryObjectConstructionContext>(C) ||
192  // These are possible in C++17 due to mandatory copy elision.
193  isa<ReturnedValueConstructionContext>(C) ||
194  isa<VariableConstructionContext>(C) ||
195  isa<ConstructorInitializerConstructionContext>(C)));
196  Data2.setPointer(const_cast<ConstructionContext *>(C));
197  }
198 
200  return static_cast<ConstructionContext *>(Data2.getPointer());
201  }
202 
203 private:
204  friend class CFGElement;
205 
206  CFGCXXRecordTypedCall() = default;
207 
208  static bool isKind(const CFGElement &E) {
209  return E.getKind() == CXXRecordTypedCall;
210  }
211 };
212 
213 /// CFGInitializer - Represents C++ base or member initializer from
214 /// constructor's initialization list.
215 class CFGInitializer : public CFGElement {
216 public:
217  explicit CFGInitializer(CXXCtorInitializer *initializer)
218  : CFGElement(Initializer, initializer) {}
219 
221  return static_cast<CXXCtorInitializer*>(Data1.getPointer());
222  }
223 
224 private:
225  friend class CFGElement;
226 
227  CFGInitializer() = default;
228 
229  static bool isKind(const CFGElement &E) {
230  return E.getKind() == Initializer;
231  }
232 };
233 
234 /// CFGNewAllocator - Represents C++ allocator call.
235 class CFGNewAllocator : public CFGElement {
236 public:
237  explicit CFGNewAllocator(const CXXNewExpr *S)
238  : CFGElement(NewAllocator, S) {}
239 
240  // Get the new expression.
241  const CXXNewExpr *getAllocatorExpr() const {
242  return static_cast<CXXNewExpr *>(Data1.getPointer());
243  }
244 
245 private:
246  friend class CFGElement;
247 
248  CFGNewAllocator() = default;
249 
250  static bool isKind(const CFGElement &elem) {
251  return elem.getKind() == NewAllocator;
252  }
253 };
254 
255 /// Represents the point where a loop ends.
256 /// This element is is only produced when building the CFG for the static
257 /// analyzer and hidden behind the 'cfg-loopexit' analyzer config flag.
258 ///
259 /// Note: a loop exit element can be reached even when the loop body was never
260 /// entered.
261 class CFGLoopExit : public CFGElement {
262 public:
263  explicit CFGLoopExit(const Stmt *stmt) : CFGElement(LoopExit, stmt) {}
264 
265  const Stmt *getLoopStmt() const {
266  return static_cast<Stmt *>(Data1.getPointer());
267  }
268 
269 private:
270  friend class CFGElement;
271 
272  CFGLoopExit() = default;
273 
274  static bool isKind(const CFGElement &elem) {
275  return elem.getKind() == LoopExit;
276  }
277 };
278 
279 /// Represents the point where the lifetime of an automatic object ends
280 class CFGLifetimeEnds : public CFGElement {
281 public:
282  explicit CFGLifetimeEnds(const VarDecl *var, const Stmt *stmt)
283  : CFGElement(LifetimeEnds, var, stmt) {}
284 
285  const VarDecl *getVarDecl() const {
286  return static_cast<VarDecl *>(Data1.getPointer());
287  }
288 
289  const Stmt *getTriggerStmt() const {
290  return static_cast<Stmt *>(Data2.getPointer());
291  }
292 
293 private:
294  friend class CFGElement;
295 
296  CFGLifetimeEnds() = default;
297 
298  static bool isKind(const CFGElement &elem) {
299  return elem.getKind() == LifetimeEnds;
300  }
301 };
302 
303 /// Represents beginning of a scope implicitly generated
304 /// by the compiler on encountering a CompoundStmt
305 class CFGScopeBegin : public CFGElement {
306 public:
308  CFGScopeBegin(const VarDecl *VD, const Stmt *S)
309  : CFGElement(ScopeBegin, VD, S) {}
310 
311  // Get statement that triggered a new scope.
312  const Stmt *getTriggerStmt() const {
313  return static_cast<Stmt*>(Data2.getPointer());
314  }
315 
316  // Get VD that triggered a new scope.
317  const VarDecl *getVarDecl() const {
318  return static_cast<VarDecl *>(Data1.getPointer());
319  }
320 
321 private:
322  friend class CFGElement;
323  static bool isKind(const CFGElement &E) {
324  Kind kind = E.getKind();
325  return kind == ScopeBegin;
326  }
327 };
328 
329 /// Represents end of a scope implicitly generated by
330 /// the compiler after the last Stmt in a CompoundStmt's body
331 class CFGScopeEnd : public CFGElement {
332 public:
334  CFGScopeEnd(const VarDecl *VD, const Stmt *S) : CFGElement(ScopeEnd, VD, S) {}
335 
336  const VarDecl *getVarDecl() const {
337  return static_cast<VarDecl *>(Data1.getPointer());
338  }
339 
340  const Stmt *getTriggerStmt() const {
341  return static_cast<Stmt *>(Data2.getPointer());
342  }
343 
344 private:
345  friend class CFGElement;
346  static bool isKind(const CFGElement &E) {
347  Kind kind = E.getKind();
348  return kind == ScopeEnd;
349  }
350 };
351 
352 /// CFGImplicitDtor - Represents C++ object destructor implicitly generated
353 /// by compiler on various occasions.
354 class CFGImplicitDtor : public CFGElement {
355 protected:
356  CFGImplicitDtor() = default;
357 
358  CFGImplicitDtor(Kind kind, const void *data1, const void *data2 = nullptr)
359  : CFGElement(kind, data1, data2) {
360  assert(kind >= DTOR_BEGIN && kind <= DTOR_END);
361  }
362 
363 public:
364  const CXXDestructorDecl *getDestructorDecl(ASTContext &astContext) const;
365  bool isNoReturn(ASTContext &astContext) const;
366 
367 private:
368  friend class CFGElement;
369 
370  static bool isKind(const CFGElement &E) {
371  Kind kind = E.getKind();
372  return kind >= DTOR_BEGIN && kind <= DTOR_END;
373  }
374 };
375 
376 /// CFGAutomaticObjDtor - Represents C++ object destructor implicitly generated
377 /// for automatic object or temporary bound to const reference at the point
378 /// of leaving its local scope.
380 public:
381  CFGAutomaticObjDtor(const VarDecl *var, const Stmt *stmt)
382  : CFGImplicitDtor(AutomaticObjectDtor, var, stmt) {}
383 
384  const VarDecl *getVarDecl() const {
385  return static_cast<VarDecl*>(Data1.getPointer());
386  }
387 
388  // Get statement end of which triggered the destructor call.
389  const Stmt *getTriggerStmt() const {
390  return static_cast<Stmt*>(Data2.getPointer());
391  }
392 
393 private:
394  friend class CFGElement;
395 
396  CFGAutomaticObjDtor() = default;
397 
398  static bool isKind(const CFGElement &elem) {
399  return elem.getKind() == AutomaticObjectDtor;
400  }
401 };
402 
403 /// CFGDeleteDtor - Represents C++ object destructor generated
404 /// from a call to delete.
406 public:
408  : CFGImplicitDtor(DeleteDtor, RD, DE) {}
409 
411  return static_cast<CXXRecordDecl*>(Data1.getPointer());
412  }
413 
414  // Get Delete expression which triggered the destructor call.
415  const CXXDeleteExpr *getDeleteExpr() const {
416  return static_cast<CXXDeleteExpr *>(Data2.getPointer());
417  }
418 
419 private:
420  friend class CFGElement;
421 
422  CFGDeleteDtor() = default;
423 
424  static bool isKind(const CFGElement &elem) {
425  return elem.getKind() == DeleteDtor;
426  }
427 };
428 
429 /// CFGBaseDtor - Represents C++ object destructor implicitly generated for
430 /// base object in destructor.
431 class CFGBaseDtor : public CFGImplicitDtor {
432 public:
434  : CFGImplicitDtor(BaseDtor, base) {}
435 
437  return static_cast<const CXXBaseSpecifier*>(Data1.getPointer());
438  }
439 
440 private:
441  friend class CFGElement;
442 
443  CFGBaseDtor() = default;
444 
445  static bool isKind(const CFGElement &E) {
446  return E.getKind() == BaseDtor;
447  }
448 };
449 
450 /// CFGMemberDtor - Represents C++ object destructor implicitly generated for
451 /// member object in destructor.
453 public:
454  CFGMemberDtor(const FieldDecl *field)
455  : CFGImplicitDtor(MemberDtor, field, nullptr) {}
456 
457  const FieldDecl *getFieldDecl() const {
458  return static_cast<const FieldDecl*>(Data1.getPointer());
459  }
460 
461 private:
462  friend class CFGElement;
463 
464  CFGMemberDtor() = default;
465 
466  static bool isKind(const CFGElement &E) {
467  return E.getKind() == MemberDtor;
468  }
469 };
470 
471 /// CFGTemporaryDtor - Represents C++ object destructor implicitly generated
472 /// at the end of full expression for temporary object.
474 public:
476  : CFGImplicitDtor(TemporaryDtor, expr, nullptr) {}
477 
479  return static_cast<const CXXBindTemporaryExpr *>(Data1.getPointer());
480  }
481 
482 private:
483  friend class CFGElement;
484 
485  CFGTemporaryDtor() = default;
486 
487  static bool isKind(const CFGElement &E) {
488  return E.getKind() == TemporaryDtor;
489  }
490 };
491 
492 /// CFGTerminator - Represents CFGBlock terminator statement.
493 ///
494 /// TemporaryDtorsBranch bit is set to true if the terminator marks a branch
495 /// in control flow of destructors of temporaries. In this case terminator
496 /// statement is the same statement that branches control flow in evaluation
497 /// of matching full expression.
499  llvm::PointerIntPair<Stmt *, 1> Data;
500 
501 public:
502  CFGTerminator() = default;
503  CFGTerminator(Stmt *S, bool TemporaryDtorsBranch = false)
504  : Data(S, TemporaryDtorsBranch) {}
505 
506  Stmt *getStmt() { return Data.getPointer(); }
507  const Stmt *getStmt() const { return Data.getPointer(); }
508 
509  bool isTemporaryDtorsBranch() const { return Data.getInt(); }
510 
511  operator Stmt *() { return getStmt(); }
512  operator const Stmt *() const { return getStmt(); }
513 
514  Stmt *operator->() { return getStmt(); }
515  const Stmt *operator->() const { return getStmt(); }
516 
517  Stmt &operator*() { return *getStmt(); }
518  const Stmt &operator*() const { return *getStmt(); }
519 
520  explicit operator bool() const { return getStmt(); }
521 };
522 
523 /// CFGBlock - Represents a single basic block in a source-level CFG.
524 /// It consists of:
525 ///
526 /// (1) A set of statements/expressions (which may contain subexpressions).
527 /// (2) A "terminator" statement (not in the set of statements).
528 /// (3) A list of successors and predecessors.
529 ///
530 /// Terminator: The terminator represents the type of control-flow that occurs
531 /// at the end of the basic block. The terminator is a Stmt* referring to an
532 /// AST node that has control-flow: if-statements, breaks, loops, etc.
533 /// If the control-flow is conditional, the condition expression will appear
534 /// within the set of statements in the block (usually the last statement).
535 ///
536 /// Predecessors: the order in the set of predecessors is arbitrary.
537 ///
538 /// Successors: the order in the set of successors is NOT arbitrary. We
539 /// currently have the following orderings based on the terminator:
540 ///
541 /// Terminator Successor Ordering
542 /// -----------------------------------------------------
543 /// if Then Block; Else Block
544 /// ? operator LHS expression; RHS expression
545 /// &&, || expression that uses result of && or ||, RHS
546 ///
547 /// But note that any of that may be NULL in case of optimized-out edges.
548 class CFGBlock {
549  class ElementList {
550  using ImplTy = BumpVector<CFGElement>;
551 
552  ImplTy Impl;
553 
554  public:
555  ElementList(BumpVectorContext &C) : Impl(C, 4) {}
556 
557  using iterator = std::reverse_iterator<ImplTy::iterator>;
558  using const_iterator = std::reverse_iterator<ImplTy::const_iterator>;
559  using reverse_iterator = ImplTy::iterator;
560  using const_reverse_iterator = ImplTy::const_iterator;
561  using const_reference = ImplTy::const_reference;
562 
563  void push_back(CFGElement e, BumpVectorContext &C) { Impl.push_back(e, C); }
564 
565  reverse_iterator insert(reverse_iterator I, size_t Cnt, CFGElement E,
566  BumpVectorContext &C) {
567  return Impl.insert(I, Cnt, E, C);
568  }
569 
570  const_reference front() const { return Impl.back(); }
571  const_reference back() const { return Impl.front(); }
572 
573  iterator begin() { return Impl.rbegin(); }
574  iterator end() { return Impl.rend(); }
575  const_iterator begin() const { return Impl.rbegin(); }
576  const_iterator end() const { return Impl.rend(); }
577  reverse_iterator rbegin() { return Impl.begin(); }
578  reverse_iterator rend() { return Impl.end(); }
579  const_reverse_iterator rbegin() const { return Impl.begin(); }
580  const_reverse_iterator rend() const { return Impl.end(); }
581 
582  CFGElement operator[](size_t i) const {
583  assert(i < Impl.size());
584  return Impl[Impl.size() - 1 - i];
585  }
586 
587  size_t size() const { return Impl.size(); }
588  bool empty() const { return Impl.empty(); }
589  };
590 
591  /// Stmts - The set of statements in the basic block.
592  ElementList Elements;
593 
594  /// Label - An (optional) label that prefixes the executable
595  /// statements in the block. When this variable is non-NULL, it is
596  /// either an instance of LabelStmt, SwitchCase or CXXCatchStmt.
597  Stmt *Label = nullptr;
598 
599  /// Terminator - The terminator for a basic block that
600  /// indicates the type of control-flow that occurs between a block
601  /// and its successors.
602  CFGTerminator Terminator;
603 
604  /// LoopTarget - Some blocks are used to represent the "loop edge" to
605  /// the start of a loop from within the loop body. This Stmt* will be
606  /// refer to the loop statement for such blocks (and be null otherwise).
607  const Stmt *LoopTarget = nullptr;
608 
609  /// BlockID - A numerical ID assigned to a CFGBlock during construction
610  /// of the CFG.
611  unsigned BlockID;
612 
613 public:
614  /// This class represents a potential adjacent block in the CFG. It encodes
615  /// whether or not the block is actually reachable, or can be proved to be
616  /// trivially unreachable. For some cases it allows one to encode scenarios
617  /// where a block was substituted because the original (now alternate) block
618  /// is unreachable.
620  enum Kind {
621  AB_Normal,
622  AB_Unreachable,
623  AB_Alternate
624  };
625 
626  CFGBlock *ReachableBlock;
627  llvm::PointerIntPair<CFGBlock *, 2> UnreachableBlock;
628 
629  public:
630  /// Construct an AdjacentBlock with a possibly unreachable block.
631  AdjacentBlock(CFGBlock *B, bool IsReachable);
632 
633  /// Construct an AdjacentBlock with a reachable block and an alternate
634  /// unreachable block.
635  AdjacentBlock(CFGBlock *B, CFGBlock *AlternateBlock);
636 
637  /// Get the reachable block, if one exists.
639  return ReachableBlock;
640  }
641 
642  /// Get the potentially unreachable block.
644  return UnreachableBlock.getPointer();
645  }
646 
647  /// Provide an implicit conversion to CFGBlock* so that
648  /// AdjacentBlock can be substituted for CFGBlock*.
649  operator CFGBlock*() const {
650  return getReachableBlock();
651  }
652 
653  CFGBlock& operator *() const {
654  return *getReachableBlock();
655  }
656 
657  CFGBlock* operator ->() const {
658  return getReachableBlock();
659  }
660 
661  bool isReachable() const {
662  Kind K = (Kind) UnreachableBlock.getInt();
663  return K == AB_Normal || K == AB_Alternate;
664  }
665  };
666 
667 private:
668  /// Predecessors/Successors - Keep track of the predecessor / successor
669  /// CFG blocks.
671  AdjacentBlocks Preds;
672  AdjacentBlocks Succs;
673 
674  /// NoReturn - This bit is set when the basic block contains a function call
675  /// or implicit destructor that is attributed as 'noreturn'. In that case,
676  /// control cannot technically ever proceed past this block. All such blocks
677  /// will have a single immediate successor: the exit block. This allows them
678  /// to be easily reached from the exit block and using this bit quickly
679  /// recognized without scanning the contents of the block.
680  ///
681  /// Optimization Note: This bit could be profitably folded with Terminator's
682  /// storage if the memory usage of CFGBlock becomes an issue.
683  unsigned HasNoReturnElement : 1;
684 
685  /// Parent - The parent CFG that owns this CFGBlock.
686  CFG *Parent;
687 
688 public:
689  explicit CFGBlock(unsigned blockid, BumpVectorContext &C, CFG *parent)
690  : Elements(C), Terminator(nullptr), BlockID(blockid), Preds(C, 1),
691  Succs(C, 1), HasNoReturnElement(false), Parent(parent) {}
692 
693  // Statement iterators
694  using iterator = ElementList::iterator;
695  using const_iterator = ElementList::const_iterator;
698 
699  CFGElement front() const { return Elements.front(); }
700  CFGElement back() const { return Elements.back(); }
701 
702  iterator begin() { return Elements.begin(); }
703  iterator end() { return Elements.end(); }
704  const_iterator begin() const { return Elements.begin(); }
705  const_iterator end() const { return Elements.end(); }
706 
707  reverse_iterator rbegin() { return Elements.rbegin(); }
708  reverse_iterator rend() { return Elements.rend(); }
709  const_reverse_iterator rbegin() const { return Elements.rbegin(); }
710  const_reverse_iterator rend() const { return Elements.rend(); }
711 
712  unsigned size() const { return Elements.size(); }
713  bool empty() const { return Elements.empty(); }
714 
715  CFGElement operator[](size_t i) const { return Elements[i]; }
716 
717  // CFG iterators
722  using pred_range = llvm::iterator_range<pred_iterator>;
723  using pred_const_range = llvm::iterator_range<const_pred_iterator>;
724 
729  using succ_range = llvm::iterator_range<succ_iterator>;
730  using succ_const_range = llvm::iterator_range<const_succ_iterator>;
731 
732  pred_iterator pred_begin() { return Preds.begin(); }
733  pred_iterator pred_end() { return Preds.end(); }
734  const_pred_iterator pred_begin() const { return Preds.begin(); }
735  const_pred_iterator pred_end() const { return Preds.end(); }
736 
738  pred_reverse_iterator pred_rend() { return Preds.rend(); }
739  const_pred_reverse_iterator pred_rbegin() const { return Preds.rbegin(); }
740  const_pred_reverse_iterator pred_rend() const { return Preds.rend(); }
741 
743  return pred_range(pred_begin(), pred_end());
744  }
745 
747  return pred_const_range(pred_begin(), pred_end());
748  }
749 
750  succ_iterator succ_begin() { return Succs.begin(); }
751  succ_iterator succ_end() { return Succs.end(); }
752  const_succ_iterator succ_begin() const { return Succs.begin(); }
753  const_succ_iterator succ_end() const { return Succs.end(); }
754 
756  succ_reverse_iterator succ_rend() { return Succs.rend(); }
757  const_succ_reverse_iterator succ_rbegin() const { return Succs.rbegin(); }
758  const_succ_reverse_iterator succ_rend() const { return Succs.rend(); }
759 
761  return succ_range(succ_begin(), succ_end());
762  }
763 
765  return succ_const_range(succ_begin(), succ_end());
766  }
767 
768  unsigned succ_size() const { return Succs.size(); }
769  bool succ_empty() const { return Succs.empty(); }
770 
771  unsigned pred_size() const { return Preds.size(); }
772  bool pred_empty() const { return Preds.empty(); }
773 
774 
776  public:
779 
781  : IgnoreNullPredecessors(1), IgnoreDefaultsWithCoveredEnums(0) {}
782  };
783 
784  static bool FilterEdge(const FilterOptions &F, const CFGBlock *Src,
785  const CFGBlock *Dst);
786 
787  template <typename IMPL, bool IsPred>
789  private:
790  IMPL I, E;
791  const FilterOptions F;
792  const CFGBlock *From;
793 
794  public:
795  explicit FilteredCFGBlockIterator(const IMPL &i, const IMPL &e,
796  const CFGBlock *from,
797  const FilterOptions &f)
798  : I(i), E(e), F(f), From(from) {
799  while (hasMore() && Filter(*I))
800  ++I;
801  }
802 
803  bool hasMore() const { return I != E; }
804 
806  do { ++I; } while (hasMore() && Filter(*I));
807  return *this;
808  }
809 
810  const CFGBlock *operator*() const { return *I; }
811 
812  private:
813  bool Filter(const CFGBlock *To) {
814  return IsPred ? FilterEdge(F, To, From) : FilterEdge(F, From, To);
815  }
816  };
817 
818  using filtered_pred_iterator =
820 
821  using filtered_succ_iterator =
823 
825  return filtered_pred_iterator(pred_begin(), pred_end(), this, f);
826  }
827 
829  return filtered_succ_iterator(succ_begin(), succ_end(), this, f);
830  }
831 
832  // Manipulation of block contents
833 
834  void setTerminator(CFGTerminator Term) { Terminator = Term; }
835  void setLabel(Stmt *Statement) { Label = Statement; }
836  void setLoopTarget(const Stmt *loopTarget) { LoopTarget = loopTarget; }
837  void setHasNoReturnElement() { HasNoReturnElement = true; }
838 
839  CFGTerminator getTerminator() { return Terminator; }
840  const CFGTerminator getTerminator() const { return Terminator; }
841 
842  Stmt *getTerminatorCondition(bool StripParens = true);
843 
844  const Stmt *getTerminatorCondition(bool StripParens = true) const {
845  return const_cast<CFGBlock*>(this)->getTerminatorCondition(StripParens);
846  }
847 
848  const Stmt *getLoopTarget() const { return LoopTarget; }
849 
850  Stmt *getLabel() { return Label; }
851  const Stmt *getLabel() const { return Label; }
852 
853  bool hasNoReturnElement() const { return HasNoReturnElement; }
854 
855  unsigned getBlockID() const { return BlockID; }
856 
857  CFG *getParent() const { return Parent; }
858 
859  void dump() const;
860 
861  void dump(const CFG *cfg, const LangOptions &LO, bool ShowColors = false) const;
862  void print(raw_ostream &OS, const CFG* cfg, const LangOptions &LO,
863  bool ShowColors) const;
864  void printTerminator(raw_ostream &OS, const LangOptions &LO) const;
865  void printAsOperand(raw_ostream &OS, bool /*PrintType*/) {
866  OS << "BB#" << getBlockID();
867  }
868 
869  /// Adds a (potentially unreachable) successor block to the current block.
870  void addSuccessor(AdjacentBlock Succ, BumpVectorContext &C);
871 
872  void appendStmt(Stmt *statement, BumpVectorContext &C) {
873  Elements.push_back(CFGStmt(statement), C);
874  }
875 
877  BumpVectorContext &C) {
878  Elements.push_back(CFGConstructor(CE, CC), C);
879  }
880 
882  const ConstructionContext *CC,
883  BumpVectorContext &C) {
884  Elements.push_back(CFGCXXRecordTypedCall(CE, CC), C);
885  }
886 
888  BumpVectorContext &C) {
889  Elements.push_back(CFGInitializer(initializer), C);
890  }
891 
893  BumpVectorContext &C) {
894  Elements.push_back(CFGNewAllocator(NE), C);
895  }
896 
897  void appendScopeBegin(const VarDecl *VD, const Stmt *S,
898  BumpVectorContext &C) {
899  Elements.push_back(CFGScopeBegin(VD, S), C);
900  }
901 
902  void prependScopeBegin(const VarDecl *VD, const Stmt *S,
903  BumpVectorContext &C) {
904  Elements.insert(Elements.rbegin(), 1, CFGScopeBegin(VD, S), C);
905  }
906 
907  void appendScopeEnd(const VarDecl *VD, const Stmt *S, BumpVectorContext &C) {
908  Elements.push_back(CFGScopeEnd(VD, S), C);
909  }
910 
911  void prependScopeEnd(const VarDecl *VD, const Stmt *S, BumpVectorContext &C) {
912  Elements.insert(Elements.rbegin(), 1, CFGScopeEnd(VD, S), C);
913  }
914 
916  Elements.push_back(CFGBaseDtor(BS), C);
917  }
918 
920  Elements.push_back(CFGMemberDtor(FD), C);
921  }
922 
924  Elements.push_back(CFGTemporaryDtor(E), C);
925  }
926 
928  Elements.push_back(CFGAutomaticObjDtor(VD, S), C);
929  }
930 
932  Elements.push_back(CFGLifetimeEnds(VD, S), C);
933  }
934 
935  void appendLoopExit(const Stmt *LoopStmt, BumpVectorContext &C) {
936  Elements.push_back(CFGLoopExit(LoopStmt), C);
937  }
938 
940  Elements.push_back(CFGDeleteDtor(RD, DE), C);
941  }
942 
943  // Destructors must be inserted in reversed order. So insertion is in two
944  // steps. First we prepare space for some number of elements, then we insert
945  // the elements beginning at the last position in prepared space.
947  BumpVectorContext &C) {
948  return iterator(Elements.insert(I.base(), Cnt,
949  CFGAutomaticObjDtor(nullptr, nullptr), C));
950  }
952  *I = CFGAutomaticObjDtor(VD, S);
953  return ++I;
954  }
955 
956  // Scope leaving must be performed in reversed order. So insertion is in two
957  // steps. First we prepare space for some number of elements, then we insert
958  // the elements beginning at the last position in prepared space.
960  BumpVectorContext &C) {
961  return iterator(
962  Elements.insert(I.base(), Cnt, CFGLifetimeEnds(nullptr, nullptr), C));
963  }
965  *I = CFGLifetimeEnds(VD, S);
966  return ++I;
967  }
968 
969  // Scope leaving must be performed in reversed order. So insertion is in two
970  // steps. First we prepare space for some number of elements, then we insert
971  // the elements beginning at the last position in prepared space.
973  return iterator(
974  Elements.insert(I.base(), Cnt, CFGScopeEnd(nullptr, nullptr), C));
975  }
977  *I = CFGScopeEnd(VD, S);
978  return ++I;
979  }
980 
981 };
982 
983 /// CFGCallback defines methods that should be called when a logical
984 /// operator error is found when building the CFG.
985 class CFGCallback {
986 public:
987  CFGCallback() = default;
988  virtual ~CFGCallback() = default;
989 
990  virtual void compareAlwaysTrue(const BinaryOperator *B, bool isAlwaysTrue) {}
991  virtual void compareBitwiseEquality(const BinaryOperator *B,
992  bool isAlwaysTrue) {}
993 };
994 
995 /// CFG - Represents a source-level, intra-procedural CFG that represents the
996 /// control-flow of a Stmt. The Stmt can represent an entire function body,
997 /// or a single expression. A CFG will always contain one empty block that
998 /// represents the Exit point of the CFG. A CFG will also contain a designated
999 /// Entry block. The CFG solely represents control-flow; it consists of
1000 /// CFGBlocks which are simply containers of Stmt*'s in the AST the CFG
1001 /// was constructed from.
1002 class CFG {
1003 public:
1004  //===--------------------------------------------------------------------===//
1005  // CFG Construction & Manipulation.
1006  //===--------------------------------------------------------------------===//
1007 
1009  std::bitset<Stmt::lastStmtConstant> alwaysAddMask;
1010 
1011  public:
1012  using ForcedBlkExprs = llvm::DenseMap<const Stmt *, const CFGBlock *>;
1013 
1014  ForcedBlkExprs **forcedBlkExprs = nullptr;
1015  CFGCallback *Observer = nullptr;
1016  bool PruneTriviallyFalseEdges = true;
1017  bool AddEHEdges = false;
1018  bool AddInitializers = false;
1019  bool AddImplicitDtors = false;
1020  bool AddLifetime = false;
1021  bool AddLoopExit = false;
1022  bool AddTemporaryDtors = false;
1023  bool AddScopes = false;
1024  bool AddStaticInitBranches = false;
1025  bool AddCXXNewAllocator = false;
1026  bool AddCXXDefaultInitExprInCtors = false;
1027  bool AddRichCXXConstructors = false;
1028  bool MarkElidedCXXConstructors = false;
1029 
1030  BuildOptions() = default;
1031 
1032  bool alwaysAdd(const Stmt *stmt) const {
1033  return alwaysAddMask[stmt->getStmtClass()];
1034  }
1035 
1036  BuildOptions &setAlwaysAdd(Stmt::StmtClass stmtClass, bool val = true) {
1037  alwaysAddMask[stmtClass] = val;
1038  return *this;
1039  }
1040 
1042  alwaysAddMask.set();
1043  return *this;
1044  }
1045  };
1046 
1047  /// buildCFG - Builds a CFG from an AST.
1048  static std::unique_ptr<CFG> buildCFG(const Decl *D, Stmt *AST, ASTContext *C,
1049  const BuildOptions &BO);
1050 
1051  /// createBlock - Create a new block in the CFG. The CFG owns the block;
1052  /// the caller should not directly free it.
1053  CFGBlock *createBlock();
1054 
1055  /// setEntry - Set the entry block of the CFG. This is typically used
1056  /// only during CFG construction. Most CFG clients expect that the
1057  /// entry block has no predecessors and contains no statements.
1058  void setEntry(CFGBlock *B) { Entry = B; }
1059 
1060  /// setIndirectGotoBlock - Set the block used for indirect goto jumps.
1061  /// This is typically used only during CFG construction.
1062  void setIndirectGotoBlock(CFGBlock *B) { IndirectGotoBlock = B; }
1063 
1064  //===--------------------------------------------------------------------===//
1065  // Block Iterators
1066  //===--------------------------------------------------------------------===//
1067 
1071  using reverse_iterator = std::reverse_iterator<iterator>;
1072  using const_reverse_iterator = std::reverse_iterator<const_iterator>;
1073 
1074  CFGBlock & front() { return *Blocks.front(); }
1075  CFGBlock & back() { return *Blocks.back(); }
1076 
1077  iterator begin() { return Blocks.begin(); }
1078  iterator end() { return Blocks.end(); }
1079  const_iterator begin() const { return Blocks.begin(); }
1080  const_iterator end() const { return Blocks.end(); }
1081 
1082  iterator nodes_begin() { return iterator(Blocks.begin()); }
1083  iterator nodes_end() { return iterator(Blocks.end()); }
1084  const_iterator nodes_begin() const { return const_iterator(Blocks.begin()); }
1085  const_iterator nodes_end() const { return const_iterator(Blocks.end()); }
1086 
1087  reverse_iterator rbegin() { return Blocks.rbegin(); }
1088  reverse_iterator rend() { return Blocks.rend(); }
1089  const_reverse_iterator rbegin() const { return Blocks.rbegin(); }
1090  const_reverse_iterator rend() const { return Blocks.rend(); }
1091 
1092  CFGBlock & getEntry() { return *Entry; }
1093  const CFGBlock & getEntry() const { return *Entry; }
1094  CFGBlock & getExit() { return *Exit; }
1095  const CFGBlock & getExit() const { return *Exit; }
1096 
1097  CFGBlock * getIndirectGotoBlock() { return IndirectGotoBlock; }
1098  const CFGBlock * getIndirectGotoBlock() const { return IndirectGotoBlock; }
1099 
1100  using try_block_iterator = std::vector<const CFGBlock *>::const_iterator;
1101 
1103  return TryDispatchBlocks.begin();
1104  }
1105 
1107  return TryDispatchBlocks.end();
1108  }
1109 
1110  void addTryDispatchBlock(const CFGBlock *block) {
1111  TryDispatchBlocks.push_back(block);
1112  }
1113 
1114  /// Records a synthetic DeclStmt and the DeclStmt it was constructed from.
1115  ///
1116  /// The CFG uses synthetic DeclStmts when a single AST DeclStmt contains
1117  /// multiple decls.
1118  void addSyntheticDeclStmt(const DeclStmt *Synthetic,
1119  const DeclStmt *Source) {
1120  assert(Synthetic->isSingleDecl() && "Can handle single declarations only");
1121  assert(Synthetic != Source && "Don't include original DeclStmts in map");
1122  assert(!SyntheticDeclStmts.count(Synthetic) && "Already in map");
1123  SyntheticDeclStmts[Synthetic] = Source;
1124  }
1125 
1126  using synthetic_stmt_iterator =
1127  llvm::DenseMap<const DeclStmt *, const DeclStmt *>::const_iterator;
1128  using synthetic_stmt_range = llvm::iterator_range<synthetic_stmt_iterator>;
1129 
1130  /// Iterates over synthetic DeclStmts in the CFG.
1131  ///
1132  /// Each element is a (synthetic statement, source statement) pair.
1133  ///
1134  /// \sa addSyntheticDeclStmt
1136  return SyntheticDeclStmts.begin();
1137  }
1138 
1139  /// \sa synthetic_stmt_begin
1141  return SyntheticDeclStmts.end();
1142  }
1143 
1144  /// \sa synthetic_stmt_begin
1146  return synthetic_stmt_range(synthetic_stmt_begin(), synthetic_stmt_end());
1147  }
1148 
1149  //===--------------------------------------------------------------------===//
1150  // Member templates useful for various batch operations over CFGs.
1151  //===--------------------------------------------------------------------===//
1152 
1153  template <typename CALLBACK>
1154  void VisitBlockStmts(CALLBACK& O) const {
1155  for (const_iterator I=begin(), E=end(); I != E; ++I)
1156  for (CFGBlock::const_iterator BI=(*I)->begin(), BE=(*I)->end();
1157  BI != BE; ++BI) {
1158  if (Optional<CFGStmt> stmt = BI->getAs<CFGStmt>())
1159  O(const_cast<Stmt*>(stmt->getStmt()));
1160  }
1161  }
1162 
1163  //===--------------------------------------------------------------------===//
1164  // CFG Introspection.
1165  //===--------------------------------------------------------------------===//
1166 
1167  /// getNumBlockIDs - Returns the total number of BlockIDs allocated (which
1168  /// start at 0).
1169  unsigned getNumBlockIDs() const { return NumBlockIDs; }
1170 
1171  /// size - Return the total number of CFGBlocks within the CFG
1172  /// This is simply a renaming of the getNumBlockIDs(). This is necessary
1173  /// because the dominator implementation needs such an interface.
1174  unsigned size() const { return NumBlockIDs; }
1175 
1176  //===--------------------------------------------------------------------===//
1177  // CFG Debugging: Pretty-Printing and Visualization.
1178  //===--------------------------------------------------------------------===//
1179 
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;
1183 
1184  //===--------------------------------------------------------------------===//
1185  // Internal: constructors and data.
1186  //===--------------------------------------------------------------------===//
1187 
1188  CFG() : Blocks(BlkBVC, 10) {}
1189 
1190  llvm::BumpPtrAllocator& getAllocator() {
1191  return BlkBVC.getAllocator();
1192  }
1193 
1195  return BlkBVC;
1196  }
1197 
1198 private:
1199  CFGBlock *Entry = nullptr;
1200  CFGBlock *Exit = nullptr;
1201 
1202  // Special block to contain collective dispatch for indirect gotos
1203  CFGBlock* IndirectGotoBlock = nullptr;
1204 
1205  unsigned NumBlockIDs = 0;
1206 
1207  BumpVectorContext BlkBVC;
1208 
1209  CFGBlockListTy Blocks;
1210 
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;
1214 
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;
1218 };
1219 
1220 } // namespace clang
1221 
1222 //===----------------------------------------------------------------------===//
1223 // GraphTraits specializations for CFG basic block graphs (source-level CFGs)
1224 //===----------------------------------------------------------------------===//
1225 
1226 namespace llvm {
1227 
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> {
1232 
1234  return Val.getStmt();
1235  }
1236 };
1237 
1238 // Traits for: CFGBlock
1239 
1240 template <> struct GraphTraits< ::clang::CFGBlock *> {
1243 
1244  static NodeRef getEntryNode(::clang::CFGBlock *BB) { return BB; }
1246  static ChildIteratorType child_end(NodeRef N) { return N->succ_end(); }
1247 };
1248 
1249 template <> struct GraphTraits< const ::clang::CFGBlock *> {
1250  using NodeRef = const ::clang::CFGBlock *;
1252 
1253  static NodeRef getEntryNode(const clang::CFGBlock *BB) { return BB; }
1255  static ChildIteratorType child_end(NodeRef N) { return N->succ_end(); }
1256 };
1257 
1258 template <> struct GraphTraits<Inverse< ::clang::CFGBlock *>> {
1261 
1262  static NodeRef getEntryNode(Inverse<::clang::CFGBlock *> G) {
1263  return G.Graph;
1264  }
1265 
1267  static ChildIteratorType child_end(NodeRef N) { return N->pred_end(); }
1268 };
1269 
1270 template <> struct GraphTraits<Inverse<const ::clang::CFGBlock *>> {
1271  using NodeRef = const ::clang::CFGBlock *;
1273 
1274  static NodeRef getEntryNode(Inverse<const ::clang::CFGBlock *> G) {
1275  return G.Graph;
1276  }
1277 
1279  static ChildIteratorType child_end(NodeRef N) { return N->pred_end(); }
1280 };
1281 
1282 // Traits for: CFG
1283 
1284 template <> struct GraphTraits< ::clang::CFG* >
1285  : public GraphTraits< ::clang::CFGBlock *> {
1287 
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(); }
1292 };
1293 
1294 template <> struct GraphTraits<const ::clang::CFG* >
1295  : public GraphTraits<const ::clang::CFGBlock *> {
1297 
1298  static NodeRef getEntryNode(const ::clang::CFG *F) { return &F->getEntry(); }
1299 
1300  static nodes_iterator nodes_begin( const ::clang::CFG* F) {
1301  return F->nodes_begin();
1302  }
1303 
1304  static nodes_iterator nodes_end( const ::clang::CFG* F) {
1305  return F->nodes_end();
1306  }
1307 
1308  static unsigned size(const ::clang::CFG* F) {
1309  return F->size();
1310  }
1311 };
1312 
1313 template <> struct GraphTraits<Inverse< ::clang::CFG *>>
1314  : public GraphTraits<Inverse< ::clang::CFGBlock *>> {
1316 
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(); }
1320 };
1321 
1322 template <> struct GraphTraits<Inverse<const ::clang::CFG *>>
1323  : public GraphTraits<Inverse<const ::clang::CFGBlock *>> {
1325 
1326  static NodeRef getEntryNode(const ::clang::CFG *F) { return &F->getExit(); }
1327 
1328  static nodes_iterator nodes_begin(const ::clang::CFG* F) {
1329  return F->nodes_begin();
1330  }
1331 
1332  static nodes_iterator nodes_end(const ::clang::CFG* F) {
1333  return F->nodes_end();
1334  }
1335 };
1336 
1337 } // namespace llvm
1338 
1339 #endif // LLVM_CLANG_ANALYSIS_CFG_H
void setIndirectGotoBlock(CFGBlock *B)
setIndirectGotoBlock - Set the block used for indirect goto jumps.
Definition: CFG.h:1062
CFGNewAllocator - Represents C++ allocator call.
Definition: CFG.h:235
static NodeRef getEntryNode(::clang::CFG *F)
Definition: CFG.h:1288
const_iterator end() const
Definition: CFG.h:1080
succ_reverse_iterator succ_rbegin()
Definition: CFG.h:755
static unsigned size(const ::clang::CFG *F)
Definition: CFG.h:1308
bool empty() const
Definition: CFG.h:713
pred_iterator pred_end()
Definition: CFG.h:733
Stmt * operator->()
Definition: CFG.h:514
CFGBlock(unsigned blockid, BumpVectorContext &C, CFG *parent)
Definition: CFG.h:689
iterator end()
Definition: BumpVector.h:95
AdjacentBlocks::const_iterator const_pred_iterator
Definition: CFG.h:719
iterator beginScopeEndInsert(iterator I, size_t Cnt, BumpVectorContext &C)
Definition: CFG.h:972
const internal::VariadicAllOfMatcher< Stmt > stmt
Matches statements.
const Stmt * getStmt() const
Definition: CFG.h:132
iterator begin()
Definition: BumpVector.h:93
ElementList::iterator iterator
Definition: CFG.h:694
succ_iterator succ_begin()
Definition: CFG.h:750
const_pred_reverse_iterator pred_rend() const
Definition: CFG.h:740
DominatorTree GraphTraits specialization so the DominatorTree can be iterable by generic graph iterat...
Definition: Dominators.h:30
bool isReachable() const
Definition: CFG.h:661
CFGLifetimeEnds(const VarDecl *var, const Stmt *stmt)
Definition: CFG.h:282
Stmt - This represents one statement.
Definition: Stmt.h:66
const_iterator nodes_end() const
Definition: CFG.h:1085
CFGElement front() const
Definition: CFG.h:699
CFGBlock & getEntry()
Definition: CFG.h:1092
unsigned size() const
size - Return the total number of CFGBlocks within the CFG This is simply a renaming of the getNumBlo...
Definition: CFG.h:1174
void appendCXXRecordTypedCall(CallExpr *CE, const ConstructionContext *CC, BumpVectorContext &C)
Definition: CFG.h:881
void appendScopeEnd(const VarDecl *VD, const Stmt *S, BumpVectorContext &C)
Definition: CFG.h:907
CFGDeleteDtor(const CXXRecordDecl *RD, const CXXDeleteExpr *DE)
Definition: CFG.h:407
size_type size() const
Definition: BumpVector.h:107
void prependScopeBegin(const VarDecl *VD, const Stmt *S, BumpVectorContext &C)
Definition: CFG.h:902
unsigned getBlockID() const
Definition: CFG.h:855
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
AdjacentBlocks::iterator succ_iterator
Definition: CFG.h:725
void appendNewAllocator(CXXNewExpr *NE, BumpVectorContext &C)
Definition: CFG.h:892
std::reverse_iterator< iterator > reverse_iterator
Definition: CFG.h:1071
CFGNewAllocator(const CXXNewExpr *S)
Definition: CFG.h:237
CFGElement operator[](size_t i) const
Definition: CFG.h:715
void appendLifetimeEnds(VarDecl *VD, Stmt *S, BumpVectorContext &C)
Definition: CFG.h:931
llvm::BumpPtrAllocator & getAllocator()
Definition: CFG.h:1190
static ChildIteratorType child_end(NodeRef N)
Definition: CFG.h:1246
::clang::CFGBlock::succ_iterator ChildIteratorType
Definition: CFG.h:1242
CFGDeleteDtor - Represents C++ object destructor generated from a call to delete. ...
Definition: CFG.h:405
const Stmt * getLoopTarget() const
Definition: CFG.h:848
iterator begin()
Definition: CFG.h:702
Represents a call to a C++ constructor.
Definition: ExprCXX.h:1240
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
const Stmt * getTriggerStmt() const
Definition: CFG.h:340
T castAs() const
Convert to the specified CFGElement type, asserting that this CFGElement is of the desired type...
Definition: CFG.h:98
AdjacentBlocks::const_reverse_iterator const_pred_reverse_iterator
Definition: CFG.h:721
unsigned IgnoreDefaultsWithCoveredEnums
Definition: CFG.h:778
void prependScopeEnd(const VarDecl *VD, const Stmt *S, BumpVectorContext &C)
Definition: CFG.h:911
Represents a point when we exit a loop.
Definition: ProgramPoint.h:687
unsigned succ_size() const
Definition: CFG.h:768
static ChildIteratorType child_begin(NodeRef N)
Definition: CFG.h:1278
reverse_iterator rbegin()
Definition: BumpVector.h:99
Represents a variable declaration or definition.
Definition: Decl.h:812
const internal::VariadicDynCastAllOfMatcher< Stmt, Expr > expr
Matches expressions.
static ChildIteratorType child_end(NodeRef N)
Definition: CFG.h:1255
const ConstructionContext * getConstructionContext() const
Definition: CFG.h:158
synthetic_stmt_range synthetic_stmts() const
Definition: CFG.h:1145
const_reverse_iterator rbegin() const
Definition: CFG.h:1089
CFGBlock * getReachableBlock() const
Get the reachable block, if one exists.
Definition: CFG.h:638
const CFGBlock & getEntry() const
Definition: CFG.h:1093
const Stmt * getTriggerStmt() const
Definition: CFG.h:389
const_succ_reverse_iterator succ_rbegin() const
Definition: CFG.h:757
Defines the clang::Expr interface and subclasses for C++ expressions.
const_pred_iterator pred_begin() const
Definition: CFG.h:734
std::reverse_iterator< iterator > reverse_iterator
Definition: BumpVector.h:85
CFGCXXRecordTypedCall - Represents a function call that returns a C++ object by value.
Definition: CFG.h:178
const ConstructionContext * getConstructionContext() const
Definition: CFG.h:199
bool pred_empty() const
Definition: CFG.h:772
pred_const_range preds() const
Definition: CFG.h:746
const_reverse_iterator rend() const
Definition: CFG.h:710
std::vector< const CFGBlock * >::const_iterator try_block_iterator
Definition: CFG.h:1100
CFGBlock * getPossiblyUnreachableBlock() const
Get the potentially unreachable block.
Definition: CFG.h:643
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:150
void setLoopTarget(const Stmt *loopTarget)
Definition: CFG.h:836
::clang::CFGBlock::const_pred_iterator ChildIteratorType
Definition: CFG.h:1260
CFGScopeBegin(const VarDecl *VD, const Stmt *S)
Definition: CFG.h:308
static NodeRef getEntryNode(const clang::CFGBlock *BB)
Definition: CFG.h:1253
Represents a member of a struct/union/class.
Definition: Decl.h:2525
static ChildIteratorType child_end(NodeRef N)
Definition: CFG.h:1279
bool empty() const
Definition: BumpVector.h:106
iterator insertAutomaticObjDtor(iterator I, VarDecl *VD, Stmt *S)
Definition: CFG.h:951
iterator nodes_end()
Definition: CFG.h:1083
CFGAutomaticObjDtor - Represents C++ object destructor implicitly generated for automatic object or t...
Definition: CFG.h:379
void appendAutomaticObjDtor(VarDecl *VD, Stmt *S, BumpVectorContext &C)
Definition: CFG.h:927
const CXXRecordDecl * getCXXRecordDecl() const
Definition: CFG.h:410
const_iterator nodes_begin() const
Definition: CFG.h:1084
succ_range succs()
Definition: CFG.h:760
clang::CharUnits operator*(clang::CharUnits::QuantityType Scale, const clang::CharUnits &CU)
Definition: CharUnits.h:208
CFGBlock & back()
Definition: CFG.h:1075
void setTerminator(CFGTerminator Term)
Definition: CFG.h:834
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:50
llvm::iterator_range< succ_iterator > succ_range
Definition: CFG.h:729
const_iterator begin() const
Definition: CFG.h:1079
iterator end()
Definition: CFG.h:1078
AdjacentBlocks::const_iterator const_succ_iterator
Definition: CFG.h:726
const_iterator end() const
Definition: CFG.h:705
static void dump(llvm::raw_ostream &OS, StringRef FunctionName, ArrayRef< CounterExpression > Expressions, ArrayRef< CounterMappingRegion > Regions)
AdjacentBlocks::iterator pred_iterator
Definition: CFG.h:718
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
CFGElement()=default
A builtin binary operation expression such as "x + y" or "x <= y".
Definition: Expr.h:3069
static NodeRef getEntryNode(::clang::CFGBlock *BB)
Definition: CFG.h:1244
CFGBlockListTy::iterator iterator
Definition: CFG.h:1069
const VarDecl * getVarDecl() const
Definition: CFG.h:384
unsigned size() const
Definition: CFG.h:712
const CXXBaseSpecifier * getBaseSpecifier() const
Definition: CFG.h:436
void appendLoopExit(const Stmt *LoopStmt, BumpVectorContext &C)
Definition: CFG.h:935
const_succ_iterator succ_end() const
Definition: CFG.h:753
FilteredCFGBlockIterator(const IMPL &i, const IMPL &e, const CFGBlock *from, const FilterOptions &f)
Definition: CFG.h:795
Represents binding an expression to a temporary.
Definition: ExprCXX.h:1197
CFGScopeEnd(const VarDecl *VD, const Stmt *S)
Definition: CFG.h:334
llvm::DenseMap< const DeclStmt *, const DeclStmt * >::const_iterator synthetic_stmt_iterator
Definition: CFG.h:1127
StmtClass
Definition: Stmt.h:68
reverse_iterator rend()
Definition: CFG.h:708
static ChildIteratorType child_end(NodeRef N)
Definition: CFG.h:1267
llvm::DenseMap< const Stmt *, const CFGBlock * > ForcedBlkExprs
Definition: CFG.h:1012
CFGBlockListTy::const_iterator const_iterator
Definition: CFG.h:1070
CFG * getParent() const
Definition: CFG.h:857
NodeId Parent
Definition: ASTDiff.cpp:192
CXXCtorInitializer * getInitializer() const
Definition: CFG.h:220
void appendConstructor(CXXConstructExpr *CE, const ConstructionContext *CC, BumpVectorContext &C)
Definition: CFG.h:876
static nodes_iterator nodes_begin(::clang::CFG *F)
Definition: CFG.h:1318
static ChildIteratorType child_begin(NodeRef N)
Definition: CFG.h:1266
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Definition: Type.cpp:1621
const FieldDecl * getFieldDecl() const
Definition: CFG.h:457
const_iterator begin() const
Definition: CFG.h:704
const CXXBindTemporaryExpr * getBindTemporaryExpr() const
Definition: CFG.h:478
reverse_iterator rend()
Definition: BumpVector.h:101
void appendBaseDtor(const CXXBaseSpecifier *BS, BumpVectorContext &C)
Definition: CFG.h:915
CFGBlock - Represents a single basic block in a source-level CFG.
Definition: CFG.h:548
CFG()
Definition: CFG.h:1188
const VarDecl * getVarDecl() const
Definition: CFG.h:317
::clang::CFG::const_iterator nodes_iterator
Definition: CFG.h:1296
const_succ_iterator succ_begin() const
Definition: CFG.h:752
std::string Label
CFG - Represents a source-level, intra-procedural CFG that represents the control-flow of a Stmt...
Definition: CFG.h:1002
::clang::CFGBlock::const_succ_iterator ChildIteratorType
Definition: CFG.h:1251
Represents a C++ destructor within a class.
Definition: DeclCXX.h:2698
bool succ_empty() const
Definition: CFG.h:769
CFGLoopExit(const Stmt *stmt)
Definition: CFG.h:263
CFGConstructor - Represents C++ constructor call.
Definition: CFG.h:150
#define bool
Definition: stdbool.h:31
const_reverse_iterator rend() const
Definition: CFG.h:1090
ElementList::const_iterator const_iterator
Definition: CFG.h:695
bool isTemporaryDtorsBranch() const
Definition: CFG.h:509
static SimpleType getSimplifiedValue(::clang::CFGTerminator Val)
Definition: CFG.h:1233
filtered_pred_iterator filtered_pred_start_end(const FilterOptions &f) const
Definition: CFG.h:824
const_pred_iterator pred_end() const
Definition: CFG.h:735
succ_const_range succs() const
Definition: CFG.h:764
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: BumpVector.h:84
static NodeRef getEntryNode(Inverse< const ::clang::CFGBlock *> G)
Definition: CFG.h:1274
CFGBaseDtor - Represents C++ object destructor implicitly generated for base object in destructor...
Definition: CFG.h:431
CFGElement back() const
Definition: CFG.h:700
void addTryDispatchBlock(const CFGBlock *block)
Definition: CFG.h:1110
Stmt & operator*()
Definition: CFG.h:517
reverse_iterator rbegin()
Definition: CFG.h:707
try_block_iterator try_blocks_begin() const
Definition: CFG.h:1102
const_succ_reverse_iterator succ_rend() const
Definition: CFG.h:758
void appendStmt(Stmt *statement, BumpVectorContext &C)
Definition: CFG.h:872
CFGBlock & front()
Definition: CFG.h:1074
CFGTerminator getTerminator()
Definition: CFG.h:839
Kind getKind() const
Definition: CFG.h:118
#define false
Definition: stdbool.h:33
QualType getCanonicalType() const
Definition: Type.h:5921
CFGImplicitDtor(Kind kind, const void *data1, const void *data2=nullptr)
Definition: CFG.h:358
pred_reverse_iterator pred_rend()
Definition: CFG.h:738
BuildOptions & setAlwaysAdd(Stmt::StmtClass stmtClass, bool val=true)
Definition: CFG.h:1036
const Stmt * operator->() const
Definition: CFG.h:515
Stmt * getLabel()
Definition: CFG.h:850
reverse_iterator rbegin()
Definition: CFG.h:1087
::clang::CFGBlock::const_pred_iterator ChildIteratorType
Definition: CFG.h:1272
Represents a new-expression for memory allocation and constructor calls, e.g: "new CXXNewExpr(foo)"...
Definition: ExprCXX.h:1846
void setLabel(Stmt *Statement)
Definition: CFG.h:835
iterator insertScopeEnd(iterator I, VarDecl *VD, Stmt *S)
Definition: CFG.h:976
DeclStmt - Adaptor class for mixing declarations with statements and expressions. ...
Definition: Stmt.h:499
llvm::iterator_range< const_succ_iterator > succ_const_range
Definition: CFG.h:730
virtual void compareAlwaysTrue(const BinaryOperator *B, bool isAlwaysTrue)
Definition: CFG.h:990
succ_reverse_iterator succ_rend()
Definition: CFG.h:756
static NodeRef getEntryNode(::clang::CFG *F)
Definition: CFG.h:1317
llvm::PointerIntPair< void *, 2 > Data1
Definition: CFG.h:83
unsigned getNumBlockIDs() const
getNumBlockIDs - Returns the total number of BlockIDs allocated (which start at 0).
Definition: CFG.h:1169
CFGConstructor(CXXConstructExpr *CE, const ConstructionContext *C)
Definition: CFG.h:152
BumpVectorContext & getBumpVectorContext()
Definition: CFG.h:1194
void appendScopeBegin(const VarDecl *VD, const Stmt *S, BumpVectorContext &C)
Definition: CFG.h:897
iterator begin()
Definition: CFG.h:1077
static ChildIteratorType child_begin(NodeRef N)
Definition: CFG.h:1245
succ_iterator succ_end()
Definition: CFG.h:751
BuildOptions & setAllAlwaysAdd()
Definition: CFG.h:1041
const_pred_reverse_iterator pred_rbegin() const
Definition: CFG.h:739
Represents end of a scope implicitly generated by the compiler after the last Stmt in a CompoundStmt&#39;...
Definition: CFG.h:331
static bool isCXXRecordTypedCall(CallExpr *CE, const ASTContext &ACtx)
Returns true when call expression CE needs to be represented by CFGCXXRecordTypedCall, as opposed to a regular CFGStmt.
Definition: CFG.h:182
Optional< T > getAs() const
Convert to the specified CFGElement type, returning None if this CFGElement is not of the desired typ...
Definition: CFG.h:109
FilteredCFGBlockIterator & operator++()
Definition: CFG.h:805
const Stmt * getTerminatorCondition(bool StripParens=true) const
Definition: CFG.h:844
static NodeRef getEntryNode(const ::clang::CFG *F)
Definition: CFG.h:1326
const Stmt & operator*() const
Definition: CFG.h:518
pred_iterator pred_begin()
Definition: CFG.h:732
static NodeRef getEntryNode(Inverse<::clang::CFGBlock *> G)
Definition: CFG.h:1262
const VarDecl * getVarDecl() const
Definition: CFG.h:285
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: CFG.h:1072
Dataflow Directional Tag Classes.
llvm::PointerIntPair< void *, 2 > Data2
Definition: CFG.h:84
void VisitBlockStmts(CALLBACK &O) const
Definition: CFG.h:1154
Represents a delete expression for memory deallocation and destructor calls, e.g. ...
Definition: ExprCXX.h:2075
static nodes_iterator nodes_begin(const ::clang::CFG *F)
Definition: CFG.h:1328
CFGTerminator(Stmt *S, bool TemporaryDtorsBranch=false)
Definition: CFG.h:503
iterator beginAutomaticObjDtorsInsert(iterator I, size_t Cnt, BumpVectorContext &C)
Definition: CFG.h:946
CFGAutomaticObjDtor(const VarDecl *var, const Stmt *stmt)
Definition: CFG.h:381
virtual void compareBitwiseEquality(const BinaryOperator *B, bool isAlwaysTrue)
Definition: CFG.h:991
const VarDecl * getVarDecl() const
Definition: CFG.h:336
llvm::iterator_range< const_pred_iterator > pred_const_range
Definition: CFG.h:723
static nodes_iterator nodes_begin(const ::clang::CFG *F)
Definition: CFG.h:1300
static unsigned size(::clang::CFG *F)
Definition: CFG.h:1291
unsigned pred_size() const
Definition: CFG.h:771
StmtClass getStmtClass() const
Definition: Stmt.h:389
void appendDeleteDtor(CXXRecordDecl *RD, CXXDeleteExpr *DE, BumpVectorContext &C)
Definition: CFG.h:939
static nodes_iterator nodes_begin(::clang::CFG *F)
Definition: CFG.h:1289
void appendMemberDtor(FieldDecl *FD, BumpVectorContext &C)
Definition: CFG.h:919
void appendInitializer(CXXCtorInitializer *initializer, BumpVectorContext &C)
Definition: CFG.h:887
static const Stmt * getTerminatorCondition(const CFGBlock *B)
A customized wrapper for CFGBlock::getTerminatorCondition() which returns the element for ObjCForColl...
This class represents a potential adjacent block in the CFG.
Definition: CFG.h:619
reverse_iterator rend()
Definition: CFG.h:1088
CFGInitializer(CXXCtorInitializer *initializer)
Definition: CFG.h:217
QualType getCallReturnType(const ASTContext &Ctx) const
getCallReturnType - Get the return type of the call expr.
Definition: Expr.cpp:1338
bool isSingleDecl() const
isSingleDecl - This method returns true if this DeclStmt refers to a single Decl. ...
Definition: Stmt.h:512
const Stmt * getLoopStmt() const
Definition: CFG.h:265
static nodes_iterator nodes_end(const ::clang::CFG *F)
Definition: CFG.h:1304
Represents the point where the lifetime of an automatic object ends.
Definition: CFG.h:280
::clang::CFG::iterator nodes_iterator
Definition: CFG.h:1286
Stmt * getStmt()
Definition: CFG.h:506
const CXXDeleteExpr * getDeleteExpr() const
Definition: CFG.h:415
pred_range preds()
Definition: CFG.h:742
pred_reverse_iterator pred_rbegin()
Definition: CFG.h:737
CFGBlock * getIndirectGotoBlock()
Definition: CFG.h:1097
const CFGBlock * operator*() const
Definition: CFG.h:810
bool alwaysAdd(const Stmt *stmt) const
Definition: CFG.h:1032
try_block_iterator try_blocks_end() const
Definition: CFG.h:1106
Represents a C++ base or member initializer.
Definition: DeclCXX.h:2250
const CFGBlock * getIndirectGotoBlock() const
Definition: CFG.h:1098
AdjacentBlocks::reverse_iterator succ_reverse_iterator
Definition: CFG.h:727
iterator insertLifetimeEnds(iterator I, VarDecl *VD, Stmt *S)
Definition: CFG.h:964
AdjacentBlocks::const_reverse_iterator const_succ_reverse_iterator
Definition: CFG.h:728
Represents a base class of a C++ class.
Definition: DeclCXX.h:192
::clang::CFG::iterator nodes_iterator
Definition: CFG.h:1315
CFGElement(Kind kind, const void *Ptr1, const void *Ptr2=nullptr)
Definition: CFG.h:86
CFGStmt(Stmt *S, Kind K=Statement)
Definition: CFG.h:128
unsigned IgnoreNullPredecessors
Definition: CFG.h:777
bool hasNoReturnElement() const
Definition: CFG.h:853
void setHasNoReturnElement()
Definition: CFG.h:837
CFGMemberDtor(const FieldDecl *field)
Definition: CFG.h:454
const CXXNewExpr * getAllocatorExpr() const
Definition: CFG.h:241
static nodes_iterator nodes_end(::clang::CFG *F)
Definition: CFG.h:1290
ConstructionContext&#39;s subclasses describe different ways of constructing an object in C++...
void printAsOperand(raw_ostream &OS, bool)
Definition: CFG.h:865
Represents a C++ struct/union/class.
Definition: DeclCXX.h:300
llvm::iterator_range< synthetic_stmt_iterator > synthetic_stmt_range
Definition: CFG.h:1128
CFGCallback defines methods that should be called when a logical operator error is found when buildin...
Definition: CFG.h:985
static nodes_iterator nodes_end(const ::clang::CFG *F)
Definition: CFG.h:1332
llvm::iterator_range< pred_iterator > pred_range
Definition: CFG.h:722
CFGImplicitDtor - Represents C++ object destructor implicitly generated by compiler on various occasi...
Definition: CFG.h:354
CFGElement - Represents a top-level expression in a basic block.
Definition: CFG.h:55
const CFGBlock & getExit() const
Definition: CFG.h:1095
const Stmt * getStmt() const
Definition: CFG.h:507
static NodeRef getEntryNode(const ::clang::CFG *F)
Definition: CFG.h:1298
unsigned kind
All of the diagnostics that can be emitted by the frontend.
Definition: DiagnosticIDs.h:61
synthetic_stmt_iterator synthetic_stmt_end() const
Definition: CFG.h:1140
CFGTerminator - Represents CFGBlock terminator statement.
Definition: CFG.h:498
CFGMemberDtor - Represents C++ object destructor implicitly generated for member object in destructor...
Definition: CFG.h:452
AdjacentBlocks::reverse_iterator pred_reverse_iterator
Definition: CFG.h:720
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition: Expr.h:2283
CFGCXXRecordTypedCall(CallExpr *CE, const ConstructionContext *C)
Definition: CFG.h:186
void appendTemporaryDtor(CXXBindTemporaryExpr *E, BumpVectorContext &C)
Definition: CFG.h:923
synthetic_stmt_iterator synthetic_stmt_begin() const
Iterates over synthetic DeclStmts in the CFG.
Definition: CFG.h:1135
const Stmt * getTriggerStmt() const
Definition: CFG.h:312
const Stmt * getTriggerStmt() const
Definition: CFG.h:289
CFGBaseDtor(const CXXBaseSpecifier *base)
Definition: CFG.h:433
const AdjacentBlock * const_iterator
Definition: BumpVector.h:82
CFGInitializer - Represents C++ base or member initializer from constructor&#39;s initialization list...
Definition: CFG.h:215
const_reverse_iterator rbegin() const
Definition: CFG.h:709
filtered_succ_iterator filtered_succ_start_end(const FilterOptions &f) const
Definition: CFG.h:828
CFGTemporaryDtor(CXXBindTemporaryExpr *expr)
Definition: CFG.h:475
iterator nodes_begin()
Definition: CFG.h:1082
iterator end()
Definition: CFG.h:703
const Stmt * getLabel() const
Definition: CFG.h:851
void setEntry(CFGBlock *B)
setEntry - Set the entry block of the CFG.
Definition: CFG.h:1058
iterator beginLifetimeEndsInsert(iterator I, size_t Cnt, BumpVectorContext &C)
Definition: CFG.h:959
void addSyntheticDeclStmt(const DeclStmt *Synthetic, const DeclStmt *Source)
Records a synthetic DeclStmt and the DeclStmt it was constructed from.
Definition: CFG.h:1118
::clang::CFG::const_iterator nodes_iterator
Definition: CFG.h:1324
CFGTemporaryDtor - Represents C++ object destructor implicitly generated at the end of full expressio...
Definition: CFG.h:473
static nodes_iterator nodes_end(::clang::CFG *F)
Definition: CFG.h:1319
const CFGTerminator getTerminator() const
Definition: CFG.h:840
static ChildIteratorType child_begin(NodeRef N)
Definition: CFG.h:1254
Represents beginning of a scope implicitly generated by the compiler on encountering a CompoundStmt...
Definition: CFG.h:305
CFGBlock & getExit()
Definition: CFG.h:1094
Represents the point where a loop ends.
Definition: CFG.h:261