1fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek//===--- CFG.h - Classes for representing and building CFGs------*- C++ -*-===//
2fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek//
3fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek//                     The LLVM Compiler Infrastructure
4fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek//
50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source
60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details.
7fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek//
8fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek//===----------------------------------------------------------------------===//
9fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek//
10fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek//  This file defines the CFG and CFGBuilder classes for representing and
11fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek//  building Control-Flow Graphs (CFGs) from ASTs.
12fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek//
13fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek//===----------------------------------------------------------------------===//
14fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek
15cd881d534517f09a2fae10445f9b865f49ccc6c8Ted Kremenek#ifndef LLVM_CLANG_CFG_H
16cd881d534517f09a2fae10445f9b865f49ccc6c8Ted Kremenek#define LLVM_CLANG_CFG_H
17cd881d534517f09a2fae10445f9b865f49ccc6c8Ted Kremenek
1874fb1a493cf5d2dd0fb51a4eadf74e85e10a3457Ted Kremenek#include "clang/AST/Stmt.h"
19ee82d9bdc5025b82de8ce2a4ad4685e0a8b79da9Ted Kremenek#include "clang/Analysis/Support/BumpVector.h"
20079bd72439448b78629a28da6b1f8abe2cdeaf4dMike Stump#include "clang/Basic/SourceLocation.h"
2130a2e16f6c27f888dd11eba6bbbae1e980078fcbChandler Carruth#include "llvm/ADT/DenseMap.h"
2230a2e16f6c27f888dd11eba6bbbae1e980078fcbChandler Carruth#include "llvm/ADT/GraphTraits.h"
23fdf6a279c9a75c778eba382d9a156697092982a1David Blaikie#include "llvm/ADT/Optional.h"
2430a2e16f6c27f888dd11eba6bbbae1e980078fcbChandler Carruth#include "llvm/ADT/PointerIntPair.h"
2530a2e16f6c27f888dd11eba6bbbae1e980078fcbChandler Carruth#include "llvm/Support/Allocator.h"
2630a2e16f6c27f888dd11eba6bbbae1e980078fcbChandler Carruth#include "llvm/Support/Casting.h"
27651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#include "llvm/Support/raw_ostream.h"
28681ab8998793899076bae9cd6383a5d78b8ee1acBenjamin Kramer#include <bitset>
29c1581a0d64b0ee4f822ed2fca4442a111d03569aHartmut Kaiser#include <cassert>
30403ba3522d1b1c97ae5fad81c1a2c4b3a754e1c1Nick Lewycky#include <iterator>
31651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#include <memory>
32fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek
33fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremeneknamespace clang {
343c0349e87cdbd7316d06d2411d86ee1086e717a5Ted Kremenek  class CXXDestructorDecl;
35b978a441c7d8bf59e7fede938e1f3b672573b443Mike Stump  class Decl;
3642a509f6a4f71bb805cc4abbb26722a34dffdddeTed Kremenek  class Stmt;
3763f5887f316fb52d243fcbb3631c039de6c4b993Ted Kremenek  class Expr;
387c625d8ffc20b92fff9e1690cd2484fcb6498183Marcin Swiderski  class FieldDecl;
391cff132e48e0ccc253c34e5a2fb12718bd4e7d2eMarcin Swiderski  class VarDecl;
40cbb67480094b3bcb5b715acd827cbad55e2a204cSean Hunt  class CXXCtorInitializer;
417c625d8ffc20b92fff9e1690cd2484fcb6498183Marcin Swiderski  class CXXBaseSpecifier;
428599e7677e067fd01d3b2ee4c0875747d367fd8eMarcin Swiderski  class CXXBindTemporaryExpr;
4342a509f6a4f71bb805cc4abbb26722a34dffdddeTed Kremenek  class CFG;
4442a509f6a4f71bb805cc4abbb26722a34dffdddeTed Kremenek  class PrinterHelper;
45e4f2142d00fa5fdb580c4e2413da91882d955381Chris Lattner  class LangOptions;
46e5af3ce53ec58995b09381ba645ab2117a46647bMike Stump  class ASTContext;
4736d558d85653315edb389677e995ec9ccdbfbf3dJordan Rose  class CXXRecordDecl;
4836d558d85653315edb389677e995ec9ccdbfbf3dJordan Rose  class CXXDeleteExpr;
49651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  class CXXNewExpr;
506bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  class BinaryOperator;
51e5af3ce53ec58995b09381ba645ab2117a46647bMike Stump
52852274d4257134906995cb252fb3dfd2d71deae8Ted Kremenek/// CFGElement - Represents a top-level expression in a basic block.
53852274d4257134906995cb252fb3dfd2d71deae8Ted Kremenekclass CFGElement {
54852274d4257134906995cb252fb3dfd2d71deae8Ted Kremenekpublic:
55b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu  enum Kind {
56b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu    // main kind
57b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu    Statement,
58b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu    Initializer,
59651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    NewAllocator,
60b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu    // dtor kind
61b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu    AutomaticObjectDtor,
6236d558d85653315edb389677e995ec9ccdbfbf3dJordan Rose    DeleteDtor,
63b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu    BaseDtor,
64b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu    MemberDtor,
65b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu    TemporaryDtor,
663c0349e87cdbd7316d06d2411d86ee1086e717a5Ted Kremenek    DTOR_BEGIN = AutomaticObjectDtor,
673c0349e87cdbd7316d06d2411d86ee1086e717a5Ted Kremenek    DTOR_END = TemporaryDtor
68b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu  };
69b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu
70b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xuprotected:
713c0349e87cdbd7316d06d2411d86ee1086e717a5Ted Kremenek  // The int bits are used to mark the kind.
72b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu  llvm::PointerIntPair<void *, 2> Data1;
73b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu  llvm::PointerIntPair<void *, 2> Data2;
74b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu
756bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  CFGElement(Kind kind, const void *Ptr1, const void *Ptr2 = nullptr)
763c0349e87cdbd7316d06d2411d86ee1086e717a5Ted Kremenek    : Data1(const_cast<void*>(Ptr1), ((unsigned) kind) & 0x3),
77651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      Data2(const_cast<void*>(Ptr2), (((unsigned) kind) >> 2) & 0x3) {
78651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    assert(getKind() == kind);
79651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  }
801cff132e48e0ccc253c34e5a2fb12718bd4e7d2eMarcin Swiderski
81b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu  CFGElement() {}
82b07805485c603be3d8011f72611465324c9e664bDavid Blaikiepublic:
83b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu
84fdf6a279c9a75c778eba382d9a156697092982a1David Blaikie  /// \brief Convert to the specified CFGElement type, asserting that this
85fdf6a279c9a75c778eba382d9a156697092982a1David Blaikie  /// CFGElement is of the desired type.
86fdf6a279c9a75c778eba382d9a156697092982a1David Blaikie  template<typename T>
87fdf6a279c9a75c778eba382d9a156697092982a1David Blaikie  T castAs() const {
88fdf6a279c9a75c778eba382d9a156697092982a1David Blaikie    assert(T::isKind(*this));
89fdf6a279c9a75c778eba382d9a156697092982a1David Blaikie    T t;
90fdf6a279c9a75c778eba382d9a156697092982a1David Blaikie    CFGElement& e = t;
91fdf6a279c9a75c778eba382d9a156697092982a1David Blaikie    e = *this;
92fdf6a279c9a75c778eba382d9a156697092982a1David Blaikie    return t;
93fdf6a279c9a75c778eba382d9a156697092982a1David Blaikie  }
94fdf6a279c9a75c778eba382d9a156697092982a1David Blaikie
95b07805485c603be3d8011f72611465324c9e664bDavid Blaikie  /// \brief Convert to the specified CFGElement type, returning None if this
96b07805485c603be3d8011f72611465324c9e664bDavid Blaikie  /// CFGElement is not of the desired type.
97fdf6a279c9a75c778eba382d9a156697092982a1David Blaikie  template<typename T>
98b07805485c603be3d8011f72611465324c9e664bDavid Blaikie  Optional<T> getAs() const {
99fdf6a279c9a75c778eba382d9a156697092982a1David Blaikie    if (!T::isKind(*this))
100b07805485c603be3d8011f72611465324c9e664bDavid Blaikie      return None;
101fdf6a279c9a75c778eba382d9a156697092982a1David Blaikie    T t;
102fdf6a279c9a75c778eba382d9a156697092982a1David Blaikie    CFGElement& e = t;
103fdf6a279c9a75c778eba382d9a156697092982a1David Blaikie    e = *this;
104fdf6a279c9a75c778eba382d9a156697092982a1David Blaikie    return t;
105fdf6a279c9a75c778eba382d9a156697092982a1David Blaikie  }
106fdf6a279c9a75c778eba382d9a156697092982a1David Blaikie
107ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  Kind getKind() const {
1083c0349e87cdbd7316d06d2411d86ee1086e717a5Ted Kremenek    unsigned x = Data2.getInt();
1093c0349e87cdbd7316d06d2411d86ee1086e717a5Ted Kremenek    x <<= 2;
1103c0349e87cdbd7316d06d2411d86ee1086e717a5Ted Kremenek    x |= Data1.getInt();
1113c0349e87cdbd7316d06d2411d86ee1086e717a5Ted Kremenek    return (Kind) x;
112b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu  }
113b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu};
114b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu
115b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xuclass CFGStmt : public CFGElement {
116b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xupublic:
1173c0349e87cdbd7316d06d2411d86ee1086e717a5Ted Kremenek  CFGStmt(Stmt *S) : CFGElement(Statement, S) {}
118b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu
119ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  const Stmt *getStmt() const {
120f1d10d939739f1a4544926d807e4f0f9fb64be61Ted Kremenek    return static_cast<const Stmt *>(Data1.getPointer());
121f1d10d939739f1a4544926d807e4f0f9fb64be61Ted Kremenek  }
122b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu
123fdf6a279c9a75c778eba382d9a156697092982a1David Blaikieprivate:
124fdf6a279c9a75c778eba382d9a156697092982a1David Blaikie  friend class CFGElement;
125fdf6a279c9a75c778eba382d9a156697092982a1David Blaikie  CFGStmt() {}
126fdf6a279c9a75c778eba382d9a156697092982a1David Blaikie  static bool isKind(const CFGElement &E) {
127fdf6a279c9a75c778eba382d9a156697092982a1David Blaikie    return E.getKind() == Statement;
128b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu  }
129b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu};
130b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu
1311cff132e48e0ccc253c34e5a2fb12718bd4e7d2eMarcin Swiderski/// CFGInitializer - Represents C++ base or member initializer from
1321cff132e48e0ccc253c34e5a2fb12718bd4e7d2eMarcin Swiderski/// constructor's initialization list.
133b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xuclass CFGInitializer : public CFGElement {
134b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xupublic:
1353c0349e87cdbd7316d06d2411d86ee1086e717a5Ted Kremenek  CFGInitializer(CXXCtorInitializer *initializer)
1363c0349e87cdbd7316d06d2411d86ee1086e717a5Ted Kremenek      : CFGElement(Initializer, initializer) {}
1371cff132e48e0ccc253c34e5a2fb12718bd4e7d2eMarcin Swiderski
138cbb67480094b3bcb5b715acd827cbad55e2a204cSean Hunt  CXXCtorInitializer* getInitializer() const {
139cbb67480094b3bcb5b715acd827cbad55e2a204cSean Hunt    return static_cast<CXXCtorInitializer*>(Data1.getPointer());
1401cff132e48e0ccc253c34e5a2fb12718bd4e7d2eMarcin Swiderski  }
1411cff132e48e0ccc253c34e5a2fb12718bd4e7d2eMarcin Swiderski
142fdf6a279c9a75c778eba382d9a156697092982a1David Blaikieprivate:
143fdf6a279c9a75c778eba382d9a156697092982a1David Blaikie  friend class CFGElement;
144fdf6a279c9a75c778eba382d9a156697092982a1David Blaikie  CFGInitializer() {}
145fdf6a279c9a75c778eba382d9a156697092982a1David Blaikie  static bool isKind(const CFGElement &E) {
146fdf6a279c9a75c778eba382d9a156697092982a1David Blaikie    return E.getKind() == Initializer;
147b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu  }
148b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu};
149b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu
150651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines/// CFGNewAllocator - Represents C++ allocator call.
151651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesclass CFGNewAllocator : public CFGElement {
152651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinespublic:
153651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  explicit CFGNewAllocator(const CXXNewExpr *S)
154651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    : CFGElement(NewAllocator, S) {}
155651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
156651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  // Get the new expression.
157651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  const CXXNewExpr *getAllocatorExpr() const {
158651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    return static_cast<CXXNewExpr *>(Data1.getPointer());
159651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  }
160651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
161651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesprivate:
162651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  friend class CFGElement;
163651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  CFGNewAllocator() {}
164651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  static bool isKind(const CFGElement &elem) {
165651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    return elem.getKind() == NewAllocator;
166651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  }
167651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines};
168651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
16953de134e7b4686eed40bc031438d8a4560a2cda4Marcin Swiderski/// CFGImplicitDtor - Represents C++ object destructor implicitly generated
1701cff132e48e0ccc253c34e5a2fb12718bd4e7d2eMarcin Swiderski/// by compiler on various occasions.
171b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xuclass CFGImplicitDtor : public CFGElement {
1721cff132e48e0ccc253c34e5a2fb12718bd4e7d2eMarcin Swiderskiprotected:
173fdf6a279c9a75c778eba382d9a156697092982a1David Blaikie  CFGImplicitDtor() {}
1746bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  CFGImplicitDtor(Kind kind, const void *data1, const void *data2 = nullptr)
1753c0349e87cdbd7316d06d2411d86ee1086e717a5Ted Kremenek    : CFGElement(kind, data1, data2) {
176ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie    assert(kind >= DTOR_BEGIN && kind <= DTOR_END);
1773c0349e87cdbd7316d06d2411d86ee1086e717a5Ted Kremenek  }
1781cff132e48e0ccc253c34e5a2fb12718bd4e7d2eMarcin Swiderski
179b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xupublic:
180c5aff4497e5bfd7523e00b87560c1a5aa65136ccTed Kremenek  const CXXDestructorDecl *getDestructorDecl(ASTContext &astContext) const;
181c5aff4497e5bfd7523e00b87560c1a5aa65136ccTed Kremenek  bool isNoReturn(ASTContext &astContext) const;
1821cff132e48e0ccc253c34e5a2fb12718bd4e7d2eMarcin Swiderski
183fdf6a279c9a75c778eba382d9a156697092982a1David Blaikieprivate:
184fdf6a279c9a75c778eba382d9a156697092982a1David Blaikie  friend class CFGElement;
185fdf6a279c9a75c778eba382d9a156697092982a1David Blaikie  static bool isKind(const CFGElement &E) {
186fdf6a279c9a75c778eba382d9a156697092982a1David Blaikie    Kind kind = E.getKind();
1873c0349e87cdbd7316d06d2411d86ee1086e717a5Ted Kremenek    return kind >= DTOR_BEGIN && kind <= DTOR_END;
188b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu  }
189b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu};
190b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu
1917c625d8ffc20b92fff9e1690cd2484fcb6498183Marcin Swiderski/// CFGAutomaticObjDtor - Represents C++ object destructor implicitly generated
1921cff132e48e0ccc253c34e5a2fb12718bd4e7d2eMarcin Swiderski/// for automatic object or temporary bound to const reference at the point
1931cff132e48e0ccc253c34e5a2fb12718bd4e7d2eMarcin Swiderski/// of leaving its local scope.
194b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xuclass CFGAutomaticObjDtor: public CFGImplicitDtor {
195b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xupublic:
1963c0349e87cdbd7316d06d2411d86ee1086e717a5Ted Kremenek  CFGAutomaticObjDtor(const VarDecl *var, const Stmt *stmt)
1973c0349e87cdbd7316d06d2411d86ee1086e717a5Ted Kremenek      : CFGImplicitDtor(AutomaticObjectDtor, var, stmt) {}
1981cff132e48e0ccc253c34e5a2fb12718bd4e7d2eMarcin Swiderski
1993c0349e87cdbd7316d06d2411d86ee1086e717a5Ted Kremenek  const VarDecl *getVarDecl() const {
2001cff132e48e0ccc253c34e5a2fb12718bd4e7d2eMarcin Swiderski    return static_cast<VarDecl*>(Data1.getPointer());
2011cff132e48e0ccc253c34e5a2fb12718bd4e7d2eMarcin Swiderski  }
2021cff132e48e0ccc253c34e5a2fb12718bd4e7d2eMarcin Swiderski
2031cff132e48e0ccc253c34e5a2fb12718bd4e7d2eMarcin Swiderski  // Get statement end of which triggered the destructor call.
2043c0349e87cdbd7316d06d2411d86ee1086e717a5Ted Kremenek  const Stmt *getTriggerStmt() const {
2051cff132e48e0ccc253c34e5a2fb12718bd4e7d2eMarcin Swiderski    return static_cast<Stmt*>(Data2.getPointer());
2061cff132e48e0ccc253c34e5a2fb12718bd4e7d2eMarcin Swiderski  }
2071cff132e48e0ccc253c34e5a2fb12718bd4e7d2eMarcin Swiderski
208fdf6a279c9a75c778eba382d9a156697092982a1David Blaikieprivate:
209fdf6a279c9a75c778eba382d9a156697092982a1David Blaikie  friend class CFGElement;
210fdf6a279c9a75c778eba382d9a156697092982a1David Blaikie  CFGAutomaticObjDtor() {}
211fdf6a279c9a75c778eba382d9a156697092982a1David Blaikie  static bool isKind(const CFGElement &elem) {
212fdf6a279c9a75c778eba382d9a156697092982a1David Blaikie    return elem.getKind() == AutomaticObjectDtor;
213b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu  }
214b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu};
215b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu
21636d558d85653315edb389677e995ec9ccdbfbf3dJordan Rose/// CFGDeleteDtor - Represents C++ object destructor generated
21736d558d85653315edb389677e995ec9ccdbfbf3dJordan Rose/// from a call to delete.
21836d558d85653315edb389677e995ec9ccdbfbf3dJordan Roseclass CFGDeleteDtor : public CFGImplicitDtor {
21936d558d85653315edb389677e995ec9ccdbfbf3dJordan Rosepublic:
22036d558d85653315edb389677e995ec9ccdbfbf3dJordan Rose  CFGDeleteDtor(const CXXRecordDecl *RD, const CXXDeleteExpr *DE)
22136d558d85653315edb389677e995ec9ccdbfbf3dJordan Rose      : CFGImplicitDtor(DeleteDtor, RD, DE) {}
22236d558d85653315edb389677e995ec9ccdbfbf3dJordan Rose
22336d558d85653315edb389677e995ec9ccdbfbf3dJordan Rose  const CXXRecordDecl *getCXXRecordDecl() const {
22436d558d85653315edb389677e995ec9ccdbfbf3dJordan Rose    return static_cast<CXXRecordDecl*>(Data1.getPointer());
22536d558d85653315edb389677e995ec9ccdbfbf3dJordan Rose  }
22636d558d85653315edb389677e995ec9ccdbfbf3dJordan Rose
22736d558d85653315edb389677e995ec9ccdbfbf3dJordan Rose  // Get Delete expression which triggered the destructor call.
22881557223ba8d7ef8b0468a6e1dc8fc79f2de46f2Jordan Rose  const CXXDeleteExpr *getDeleteExpr() const {
22936d558d85653315edb389677e995ec9ccdbfbf3dJordan Rose    return static_cast<CXXDeleteExpr *>(Data2.getPointer());
23036d558d85653315edb389677e995ec9ccdbfbf3dJordan Rose  }
23136d558d85653315edb389677e995ec9ccdbfbf3dJordan Rose
23236d558d85653315edb389677e995ec9ccdbfbf3dJordan Rose
23336d558d85653315edb389677e995ec9ccdbfbf3dJordan Roseprivate:
23436d558d85653315edb389677e995ec9ccdbfbf3dJordan Rose  friend class CFGElement;
23536d558d85653315edb389677e995ec9ccdbfbf3dJordan Rose  CFGDeleteDtor() {}
23636d558d85653315edb389677e995ec9ccdbfbf3dJordan Rose  static bool isKind(const CFGElement &elem) {
23736d558d85653315edb389677e995ec9ccdbfbf3dJordan Rose    return elem.getKind() == DeleteDtor;
23836d558d85653315edb389677e995ec9ccdbfbf3dJordan Rose  }
23936d558d85653315edb389677e995ec9ccdbfbf3dJordan Rose};
24036d558d85653315edb389677e995ec9ccdbfbf3dJordan Rose
2417c625d8ffc20b92fff9e1690cd2484fcb6498183Marcin Swiderski/// CFGBaseDtor - Represents C++ object destructor implicitly generated for
2427c625d8ffc20b92fff9e1690cd2484fcb6498183Marcin Swiderski/// base object in destructor.
243b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xuclass CFGBaseDtor : public CFGImplicitDtor {
244b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xupublic:
2453c0349e87cdbd7316d06d2411d86ee1086e717a5Ted Kremenek  CFGBaseDtor(const CXXBaseSpecifier *base)
2463c0349e87cdbd7316d06d2411d86ee1086e717a5Ted Kremenek      : CFGImplicitDtor(BaseDtor, base) {}
2477c625d8ffc20b92fff9e1690cd2484fcb6498183Marcin Swiderski
2487c625d8ffc20b92fff9e1690cd2484fcb6498183Marcin Swiderski  const CXXBaseSpecifier *getBaseSpecifier() const {
2497c625d8ffc20b92fff9e1690cd2484fcb6498183Marcin Swiderski    return static_cast<const CXXBaseSpecifier*>(Data1.getPointer());
2507c625d8ffc20b92fff9e1690cd2484fcb6498183Marcin Swiderski  }
2517c625d8ffc20b92fff9e1690cd2484fcb6498183Marcin Swiderski
252fdf6a279c9a75c778eba382d9a156697092982a1David Blaikieprivate:
253fdf6a279c9a75c778eba382d9a156697092982a1David Blaikie  friend class CFGElement;
254fdf6a279c9a75c778eba382d9a156697092982a1David Blaikie  CFGBaseDtor() {}
255fdf6a279c9a75c778eba382d9a156697092982a1David Blaikie  static bool isKind(const CFGElement &E) {
256fdf6a279c9a75c778eba382d9a156697092982a1David Blaikie    return E.getKind() == BaseDtor;
257b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu  }
258b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu};
259b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu
2607c625d8ffc20b92fff9e1690cd2484fcb6498183Marcin Swiderski/// CFGMemberDtor - Represents C++ object destructor implicitly generated for
2617c625d8ffc20b92fff9e1690cd2484fcb6498183Marcin Swiderski/// member object in destructor.
262b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xuclass CFGMemberDtor : public CFGImplicitDtor {
263b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xupublic:
2643c0349e87cdbd7316d06d2411d86ee1086e717a5Ted Kremenek  CFGMemberDtor(const FieldDecl *field)
2656bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      : CFGImplicitDtor(MemberDtor, field, nullptr) {}
2667c625d8ffc20b92fff9e1690cd2484fcb6498183Marcin Swiderski
2673c0349e87cdbd7316d06d2411d86ee1086e717a5Ted Kremenek  const FieldDecl *getFieldDecl() const {
2683c0349e87cdbd7316d06d2411d86ee1086e717a5Ted Kremenek    return static_cast<const FieldDecl*>(Data1.getPointer());
2697c625d8ffc20b92fff9e1690cd2484fcb6498183Marcin Swiderski  }
2707c625d8ffc20b92fff9e1690cd2484fcb6498183Marcin Swiderski
271fdf6a279c9a75c778eba382d9a156697092982a1David Blaikieprivate:
272fdf6a279c9a75c778eba382d9a156697092982a1David Blaikie  friend class CFGElement;
273fdf6a279c9a75c778eba382d9a156697092982a1David Blaikie  CFGMemberDtor() {}
274fdf6a279c9a75c778eba382d9a156697092982a1David Blaikie  static bool isKind(const CFGElement &E) {
275fdf6a279c9a75c778eba382d9a156697092982a1David Blaikie    return E.getKind() == MemberDtor;
276b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu  }
277b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu};
278b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu
2798599e7677e067fd01d3b2ee4c0875747d367fd8eMarcin Swiderski/// CFGTemporaryDtor - Represents C++ object destructor implicitly generated
2808599e7677e067fd01d3b2ee4c0875747d367fd8eMarcin Swiderski/// at the end of full expression for temporary object.
281b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xuclass CFGTemporaryDtor : public CFGImplicitDtor {
282b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xupublic:
2833c0349e87cdbd7316d06d2411d86ee1086e717a5Ted Kremenek  CFGTemporaryDtor(CXXBindTemporaryExpr *expr)
2846bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      : CFGImplicitDtor(TemporaryDtor, expr, nullptr) {}
2858599e7677e067fd01d3b2ee4c0875747d367fd8eMarcin Swiderski
2863c0349e87cdbd7316d06d2411d86ee1086e717a5Ted Kremenek  const CXXBindTemporaryExpr *getBindTemporaryExpr() const {
2873c0349e87cdbd7316d06d2411d86ee1086e717a5Ted Kremenek    return static_cast<const CXXBindTemporaryExpr *>(Data1.getPointer());
2888599e7677e067fd01d3b2ee4c0875747d367fd8eMarcin Swiderski  }
2898599e7677e067fd01d3b2ee4c0875747d367fd8eMarcin Swiderski
290fdf6a279c9a75c778eba382d9a156697092982a1David Blaikieprivate:
291fdf6a279c9a75c778eba382d9a156697092982a1David Blaikie  friend class CFGElement;
292fdf6a279c9a75c778eba382d9a156697092982a1David Blaikie  CFGTemporaryDtor() {}
293fdf6a279c9a75c778eba382d9a156697092982a1David Blaikie  static bool isKind(const CFGElement &E) {
294fdf6a279c9a75c778eba382d9a156697092982a1David Blaikie    return E.getKind() == TemporaryDtor;
295b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu  }
296852274d4257134906995cb252fb3dfd2d71deae8Ted Kremenek};
297852274d4257134906995cb252fb3dfd2d71deae8Ted Kremenek
2984ba72a0b28135209c435630682febe1f854ccfa6Marcin Swiderski/// CFGTerminator - Represents CFGBlock terminator statement.
2994ba72a0b28135209c435630682febe1f854ccfa6Marcin Swiderski///
3004ba72a0b28135209c435630682febe1f854ccfa6Marcin Swiderski/// TemporaryDtorsBranch bit is set to true if the terminator marks a branch
3014ba72a0b28135209c435630682febe1f854ccfa6Marcin Swiderski/// in control flow of destructors of temporaries. In this case terminator
3024ba72a0b28135209c435630682febe1f854ccfa6Marcin Swiderski/// statement is the same statement that branches control flow in evaluation
3034ba72a0b28135209c435630682febe1f854ccfa6Marcin Swiderski/// of matching full expression.
3044ba72a0b28135209c435630682febe1f854ccfa6Marcin Swiderskiclass CFGTerminator {
3054ba72a0b28135209c435630682febe1f854ccfa6Marcin Swiderski  llvm::PointerIntPair<Stmt *, 1> Data;
3064ba72a0b28135209c435630682febe1f854ccfa6Marcin Swiderskipublic:
3074ba72a0b28135209c435630682febe1f854ccfa6Marcin Swiderski  CFGTerminator() {}
3084ba72a0b28135209c435630682febe1f854ccfa6Marcin Swiderski  CFGTerminator(Stmt *S, bool TemporaryDtorsBranch = false)
3094ba72a0b28135209c435630682febe1f854ccfa6Marcin Swiderski      : Data(S, TemporaryDtorsBranch) {}
3104ba72a0b28135209c435630682febe1f854ccfa6Marcin Swiderski
3114ba72a0b28135209c435630682febe1f854ccfa6Marcin Swiderski  Stmt *getStmt() { return Data.getPointer(); }
3124ba72a0b28135209c435630682febe1f854ccfa6Marcin Swiderski  const Stmt *getStmt() const { return Data.getPointer(); }
3134ba72a0b28135209c435630682febe1f854ccfa6Marcin Swiderski
3144ba72a0b28135209c435630682febe1f854ccfa6Marcin Swiderski  bool isTemporaryDtorsBranch() const { return Data.getInt(); }
3154ba72a0b28135209c435630682febe1f854ccfa6Marcin Swiderski
3164ba72a0b28135209c435630682febe1f854ccfa6Marcin Swiderski  operator Stmt *() { return getStmt(); }
3174ba72a0b28135209c435630682febe1f854ccfa6Marcin Swiderski  operator const Stmt *() const { return getStmt(); }
3184ba72a0b28135209c435630682febe1f854ccfa6Marcin Swiderski
3194ba72a0b28135209c435630682febe1f854ccfa6Marcin Swiderski  Stmt *operator->() { return getStmt(); }
3204ba72a0b28135209c435630682febe1f854ccfa6Marcin Swiderski  const Stmt *operator->() const { return getStmt(); }
3214ba72a0b28135209c435630682febe1f854ccfa6Marcin Swiderski
3224ba72a0b28135209c435630682febe1f854ccfa6Marcin Swiderski  Stmt &operator*() { return *getStmt(); }
3234ba72a0b28135209c435630682febe1f854ccfa6Marcin Swiderski  const Stmt &operator*() const { return *getStmt(); }
3244ba72a0b28135209c435630682febe1f854ccfa6Marcin Swiderski
3257247c88d1e41514a41085f83ebf03dd5220e054aDavid Blaikie  LLVM_EXPLICIT operator bool() const { return getStmt(); }
3264ba72a0b28135209c435630682febe1f854ccfa6Marcin Swiderski};
3274ba72a0b28135209c435630682febe1f854ccfa6Marcin Swiderski
328fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek/// CFGBlock - Represents a single basic block in a source-level CFG.
329fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek///  It consists of:
330fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek///
331fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek///  (1) A set of statements/expressions (which may contain subexpressions).
332fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek///  (2) A "terminator" statement (not in the set of statements).
333fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek///  (3) A list of successors and predecessors.
334fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek///
335fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek/// Terminator: The terminator represents the type of control-flow that occurs
336fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek/// at the end of the basic block.  The terminator is a Stmt* referring to an
337fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek/// AST node that has control-flow: if-statements, breaks, loops, etc.
338fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek/// If the control-flow is conditional, the condition expression will appear
339fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek/// within the set of statements in the block (usually the last statement).
340fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek///
341fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek/// Predecessors: the order in the set of predecessors is arbitrary.
342fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek///
343fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek/// Successors: the order in the set of successors is NOT arbitrary.  We
344fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek///  currently have the following orderings based on the terminator:
345fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek///
346fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek///     Terminator       Successor Ordering
347fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek///  -----------------------------------------------------
348fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek///       if            Then Block;  Else Block
3499cffe7366ea3beb33c2d58f43a8c3a04c1873e11Ted Kremenek///     ? operator      LHS expression;  RHS expression
3509cffe7366ea3beb33c2d58f43a8c3a04c1873e11Ted Kremenek///     &&, ||          expression that uses result of && or ||, RHS
351fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek///
3523cda937d4c015ff6070f1117024621a452cfadddTed Kremenek/// But note that any of that may be NULL in case of optimized-out edges.
3533cda937d4c015ff6070f1117024621a452cfadddTed Kremenek///
354fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenekclass CFGBlock {
355b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu  class ElementList {
356852274d4257134906995cb252fb3dfd2d71deae8Ted Kremenek    typedef BumpVector<CFGElement> ImplTy;
3576c2497248bc4f7fd8e5fb0a206d20abbf0e16645Ted Kremenek    ImplTy Impl;
3586c2497248bc4f7fd8e5fb0a206d20abbf0e16645Ted Kremenek  public:
359b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu    ElementList(BumpVectorContext &C) : Impl(C, 4) {}
360ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
3616c2497248bc4f7fd8e5fb0a206d20abbf0e16645Ted Kremenek    typedef std::reverse_iterator<ImplTy::iterator>       iterator;
3626c2497248bc4f7fd8e5fb0a206d20abbf0e16645Ted Kremenek    typedef std::reverse_iterator<ImplTy::const_iterator> const_iterator;
3636c2497248bc4f7fd8e5fb0a206d20abbf0e16645Ted Kremenek    typedef ImplTy::iterator                              reverse_iterator;
364ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie    typedef ImplTy::const_iterator                       const_reverse_iterator;
365298038352b34c5503db418201f3ddea6e56fd0e1David Blaikie    typedef ImplTy::const_reference                       const_reference;
366ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
367852274d4257134906995cb252fb3dfd2d71deae8Ted Kremenek    void push_back(CFGElement e, BumpVectorContext &C) { Impl.push_back(e, C); }
36853de134e7b4686eed40bc031438d8a4560a2cda4Marcin Swiderski    reverse_iterator insert(reverse_iterator I, size_t Cnt, CFGElement E,
3699c378f705405d37f49795d5e915989de774fe11fTed Kremenek        BumpVectorContext &C) {
37053de134e7b4686eed40bc031438d8a4560a2cda4Marcin Swiderski      return Impl.insert(I, Cnt, E, C);
37153de134e7b4686eed40bc031438d8a4560a2cda4Marcin Swiderski    }
37253de134e7b4686eed40bc031438d8a4560a2cda4Marcin Swiderski
373298038352b34c5503db418201f3ddea6e56fd0e1David Blaikie    const_reference front() const { return Impl.back(); }
374298038352b34c5503db418201f3ddea6e56fd0e1David Blaikie    const_reference back() const { return Impl.front(); }
375ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
3766c2497248bc4f7fd8e5fb0a206d20abbf0e16645Ted Kremenek    iterator begin() { return Impl.rbegin(); }
3776c2497248bc4f7fd8e5fb0a206d20abbf0e16645Ted Kremenek    iterator end() { return Impl.rend(); }
3786c2497248bc4f7fd8e5fb0a206d20abbf0e16645Ted Kremenek    const_iterator begin() const { return Impl.rbegin(); }
3796c2497248bc4f7fd8e5fb0a206d20abbf0e16645Ted Kremenek    const_iterator end() const { return Impl.rend(); }
3806c2497248bc4f7fd8e5fb0a206d20abbf0e16645Ted Kremenek    reverse_iterator rbegin() { return Impl.begin(); }
3816c2497248bc4f7fd8e5fb0a206d20abbf0e16645Ted Kremenek    reverse_iterator rend() { return Impl.end(); }
3826c2497248bc4f7fd8e5fb0a206d20abbf0e16645Ted Kremenek    const_reverse_iterator rbegin() const { return Impl.begin(); }
3836c2497248bc4f7fd8e5fb0a206d20abbf0e16645Ted Kremenek    const_reverse_iterator rend() const { return Impl.end(); }
3846c2497248bc4f7fd8e5fb0a206d20abbf0e16645Ted Kremenek
385852274d4257134906995cb252fb3dfd2d71deae8Ted Kremenek   CFGElement operator[](size_t i) const  {
3866c2497248bc4f7fd8e5fb0a206d20abbf0e16645Ted Kremenek     assert(i < Impl.size());
3876c2497248bc4f7fd8e5fb0a206d20abbf0e16645Ted Kremenek     return Impl[Impl.size() - 1 - i];
3886c2497248bc4f7fd8e5fb0a206d20abbf0e16645Ted Kremenek   }
389ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
3906c2497248bc4f7fd8e5fb0a206d20abbf0e16645Ted Kremenek    size_t size() const { return Impl.size(); }
3916c2497248bc4f7fd8e5fb0a206d20abbf0e16645Ted Kremenek    bool empty() const { return Impl.empty(); }
3926c2497248bc4f7fd8e5fb0a206d20abbf0e16645Ted Kremenek  };
3936c2497248bc4f7fd8e5fb0a206d20abbf0e16645Ted Kremenek
394fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek  /// Stmts - The set of statements in the basic block.
395b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu  ElementList Elements;
3969cffe7366ea3beb33c2d58f43a8c3a04c1873e11Ted Kremenek
3979cffe7366ea3beb33c2d58f43a8c3a04c1873e11Ted Kremenek  /// Label - An (optional) label that prefixes the executable
3989cffe7366ea3beb33c2d58f43a8c3a04c1873e11Ted Kremenek  ///  statements in the block.  When this variable is non-NULL, it is
3994c45aa1b00b91847acfb082acfaced3ffa294d1dMike Stump  ///  either an instance of LabelStmt, SwitchCase or CXXCatchStmt.
4003575f84e459033d6427b84b4b795b22c85c4d27dTed Kremenek  Stmt *Label;
4011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4029cffe7366ea3beb33c2d58f43a8c3a04c1873e11Ted Kremenek  /// Terminator - The terminator for a basic block that
403fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek  ///  indicates the type of control-flow that occurs between a block
404fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek  ///  and its successors.
4054ba72a0b28135209c435630682febe1f854ccfa6Marcin Swiderski  CFGTerminator Terminator;
4061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4073575f84e459033d6427b84b4b795b22c85c4d27dTed Kremenek  /// LoopTarget - Some blocks are used to represent the "loop edge" to
4083575f84e459033d6427b84b4b795b22c85c4d27dTed Kremenek  ///  the start of a loop from within the loop body.  This Stmt* will be
4093575f84e459033d6427b84b4b795b22c85c4d27dTed Kremenek  ///  refer to the loop statement for such blocks (and be null otherwise).
4101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  const Stmt *LoopTarget;
4111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
412fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek  /// BlockID - A numerical ID assigned to a CFGBlock during construction
413fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek  ///   of the CFG.
414fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek  unsigned BlockID;
4151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
416651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinespublic:
417651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  /// This class represents a potential adjacent block in the CFG.  It encodes
418651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  /// whether or not the block is actually reachable, or can be proved to be
419651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  /// trivially unreachable.  For some cases it allows one to encode scenarios
420651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  /// where a block was substituted because the original (now alternate) block
421651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  /// is unreachable.
422651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  class AdjacentBlock {
423651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    enum Kind {
424651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      AB_Normal,
425651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      AB_Unreachable,
426651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      AB_Alternate
427651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    };
428651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
429651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    CFGBlock *ReachableBlock;
430651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    llvm::PointerIntPair<CFGBlock*, 2> UnreachableBlock;
431651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
432651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  public:
433651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    /// Construct an AdjacentBlock with a possibly unreachable block.
434651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    AdjacentBlock(CFGBlock *B, bool IsReachable);
435651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
436651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    /// Construct an AdjacentBlock with a reachable block and an alternate
437651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    /// unreachable block.
438651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    AdjacentBlock(CFGBlock *B, CFGBlock *AlternateBlock);
439651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
440651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    /// Get the reachable block, if one exists.
441651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    CFGBlock *getReachableBlock() const {
442651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      return ReachableBlock;
443651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    }
444651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
445651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    /// Get the potentially unreachable block.
446651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    CFGBlock *getPossiblyUnreachableBlock() const {
447651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      return UnreachableBlock.getPointer();
448651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    }
449651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
450651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    /// Provide an implicit conversion to CFGBlock* so that
451651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    /// AdjacentBlock can be substituted for CFGBlock*.
452651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    operator CFGBlock*() const {
453651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      return getReachableBlock();
454651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    }
455651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
456651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    CFGBlock& operator *() const {
457651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      return *getReachableBlock();
458651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    }
459651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
460651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    CFGBlock* operator ->() const {
461651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      return getReachableBlock();
462651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    }
463651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
464651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    bool isReachable() const {
465651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      Kind K = (Kind) UnreachableBlock.getInt();
466651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      return K == AB_Normal || K == AB_Alternate;
467651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    }
468651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  };
469651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
470651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesprivate:
471fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek  /// Predecessors/Successors - Keep track of the predecessor / successor
472fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek  /// CFG blocks.
473651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  typedef BumpVector<AdjacentBlock> AdjacentBlocks;
474fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek  AdjacentBlocks Preds;
475fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek  AdjacentBlocks Succs;
4761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
47783754162f698a5dafad93fb89be0953651a604d0Chandler Carruth  /// NoReturn - This bit is set when the basic block contains a function call
47883754162f698a5dafad93fb89be0953651a604d0Chandler Carruth  /// or implicit destructor that is attributed as 'noreturn'. In that case,
47983754162f698a5dafad93fb89be0953651a604d0Chandler Carruth  /// control cannot technically ever proceed past this block. All such blocks
48083754162f698a5dafad93fb89be0953651a604d0Chandler Carruth  /// will have a single immediate successor: the exit block. This allows them
48183754162f698a5dafad93fb89be0953651a604d0Chandler Carruth  /// to be easily reached from the exit block and using this bit quickly
48283754162f698a5dafad93fb89be0953651a604d0Chandler Carruth  /// recognized without scanning the contents of the block.
48383754162f698a5dafad93fb89be0953651a604d0Chandler Carruth  ///
48483754162f698a5dafad93fb89be0953651a604d0Chandler Carruth  /// Optimization Note: This bit could be profitably folded with Terminator's
48583754162f698a5dafad93fb89be0953651a604d0Chandler Carruth  /// storage if the memory usage of CFGBlock becomes an issue.
48683754162f698a5dafad93fb89be0953651a604d0Chandler Carruth  unsigned HasNoReturnElement : 1;
48783754162f698a5dafad93fb89be0953651a604d0Chandler Carruth
48802f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks  /// Parent - The parent CFG that owns this CFGBlock.
48902f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks  CFG *Parent;
49002f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks
491fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenekpublic:
49202f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks  explicit CFGBlock(unsigned blockid, BumpVectorContext &C, CFG *parent)
4936bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    : Elements(C), Label(nullptr), Terminator(nullptr), LoopTarget(nullptr),
49402f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks      BlockID(blockid), Preds(C, 1), Succs(C, 1), HasNoReturnElement(false),
49502f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks      Parent(parent) {}
4967177dee8aee4b432911c91f1b788963bec0cac9fDaniel Dunbar  ~CFGBlock() {}
497fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek
498fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek  // Statement iterators
499b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu  typedef ElementList::iterator                      iterator;
500b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu  typedef ElementList::const_iterator                const_iterator;
501b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu  typedef ElementList::reverse_iterator              reverse_iterator;
502b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu  typedef ElementList::const_reverse_iterator        const_reverse_iterator;
5031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
504b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu  CFGElement                 front()       const { return Elements.front();   }
505b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu  CFGElement                 back()        const { return Elements.back();    }
5061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
507b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu  iterator                   begin()             { return Elements.begin();   }
508b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu  iterator                   end()               { return Elements.end();     }
509b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu  const_iterator             begin()       const { return Elements.begin();   }
510b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu  const_iterator             end()         const { return Elements.end();     }
511fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek
512b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu  reverse_iterator           rbegin()            { return Elements.rbegin();  }
513b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu  reverse_iterator           rend()              { return Elements.rend();    }
514b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu  const_reverse_iterator     rbegin()      const { return Elements.rbegin();  }
515b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu  const_reverse_iterator     rend()        const { return Elements.rend();    }
5161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
517b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu  unsigned                   size()        const { return Elements.size();    }
518b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu  bool                       empty()       const { return Elements.empty();   }
519be7a7d6f2e5b86807afd08a568863df973baaa38Ted Kremenek
520b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu  CFGElement operator[](size_t i) const  { return Elements[i]; }
5211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
522fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek  // CFG iterators
523fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek  typedef AdjacentBlocks::iterator                              pred_iterator;
524fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek  typedef AdjacentBlocks::const_iterator                  const_pred_iterator;
525fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek  typedef AdjacentBlocks::reverse_iterator              pred_reverse_iterator;
526fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek  typedef AdjacentBlocks::const_reverse_iterator  const_pred_reverse_iterator;
527fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek
528fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek  typedef AdjacentBlocks::iterator                              succ_iterator;
529fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek  typedef AdjacentBlocks::const_iterator                  const_succ_iterator;
530fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek  typedef AdjacentBlocks::reverse_iterator              succ_reverse_iterator;
531fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek  typedef AdjacentBlocks::const_reverse_iterator  const_succ_reverse_iterator;
5321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
533fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek  pred_iterator                pred_begin()        { return Preds.begin();   }
534fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek  pred_iterator                pred_end()          { return Preds.end();     }
535fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek  const_pred_iterator          pred_begin()  const { return Preds.begin();   }
536fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek  const_pred_iterator          pred_end()    const { return Preds.end();     }
5371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
538fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek  pred_reverse_iterator        pred_rbegin()       { return Preds.rbegin();  }
5391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  pred_reverse_iterator        pred_rend()         { return Preds.rend();    }
540fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek  const_pred_reverse_iterator  pred_rbegin() const { return Preds.rbegin();  }
541fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek  const_pred_reverse_iterator  pred_rend()   const { return Preds.rend();    }
542fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek
5431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  succ_iterator                succ_begin()        { return Succs.begin();   }
544fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek  succ_iterator                succ_end()          { return Succs.end();     }
545fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek  const_succ_iterator          succ_begin()  const { return Succs.begin();   }
5461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  const_succ_iterator          succ_end()    const { return Succs.end();     }
5471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
548fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek  succ_reverse_iterator        succ_rbegin()       { return Succs.rbegin();  }
549fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek  succ_reverse_iterator        succ_rend()         { return Succs.rend();    }
550fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek  const_succ_reverse_iterator  succ_rbegin() const { return Succs.rbegin();  }
551fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek  const_succ_reverse_iterator  succ_rend()   const { return Succs.rend();    }
552fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek
553fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek  unsigned                     succ_size()   const { return Succs.size();    }
554fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek  bool                         succ_empty()  const { return Succs.empty();   }
555fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek
556fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek  unsigned                     pred_size()   const { return Preds.size();    }
557fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek  bool                         pred_empty()  const { return Preds.empty();   }
5581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
559ee7f84d509c6382491673883598eb9ed2d3a6a8bTed Kremenek
560ee7f84d509c6382491673883598eb9ed2d3a6a8bTed Kremenek  class FilterOptions {
561ee7f84d509c6382491673883598eb9ed2d3a6a8bTed Kremenek  public:
562ee7f84d509c6382491673883598eb9ed2d3a6a8bTed Kremenek    FilterOptions() {
563651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      IgnoreNullPredecessors = 1;
564ee7f84d509c6382491673883598eb9ed2d3a6a8bTed Kremenek      IgnoreDefaultsWithCoveredEnums = 0;
565be39a566a914df8561d7a1e9654708297f0908c1Ted Kremenek    }
566ee7f84d509c6382491673883598eb9ed2d3a6a8bTed Kremenek
567651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    unsigned IgnoreNullPredecessors : 1;
568ee7f84d509c6382491673883598eb9ed2d3a6a8bTed Kremenek    unsigned IgnoreDefaultsWithCoveredEnums : 1;
569ee7f84d509c6382491673883598eb9ed2d3a6a8bTed Kremenek  };
570ee7f84d509c6382491673883598eb9ed2d3a6a8bTed Kremenek
571ee7f84d509c6382491673883598eb9ed2d3a6a8bTed Kremenek  static bool FilterEdge(const FilterOptions &F, const CFGBlock *Src,
572be39a566a914df8561d7a1e9654708297f0908c1Ted Kremenek       const CFGBlock *Dst);
573ee7f84d509c6382491673883598eb9ed2d3a6a8bTed Kremenek
574ee7f84d509c6382491673883598eb9ed2d3a6a8bTed Kremenek  template <typename IMPL, bool IsPred>
575ee7f84d509c6382491673883598eb9ed2d3a6a8bTed Kremenek  class FilteredCFGBlockIterator {
576ee7f84d509c6382491673883598eb9ed2d3a6a8bTed Kremenek  private:
577ee7f84d509c6382491673883598eb9ed2d3a6a8bTed Kremenek    IMPL I, E;
578ee7f84d509c6382491673883598eb9ed2d3a6a8bTed Kremenek    const FilterOptions F;
579ee7f84d509c6382491673883598eb9ed2d3a6a8bTed Kremenek    const CFGBlock *From;
580651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  public:
581ee7f84d509c6382491673883598eb9ed2d3a6a8bTed Kremenek    explicit FilteredCFGBlockIterator(const IMPL &i, const IMPL &e,
582651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                      const CFGBlock *from,
583651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                      const FilterOptions &f)
584651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        : I(i), E(e), F(f), From(from) {
585651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      while (hasMore() && Filter(*I))
586651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        ++I;
587651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    }
588ee7f84d509c6382491673883598eb9ed2d3a6a8bTed Kremenek
589ee7f84d509c6382491673883598eb9ed2d3a6a8bTed Kremenek    bool hasMore() const { return I != E; }
590ee7f84d509c6382491673883598eb9ed2d3a6a8bTed Kremenek
591ee7f84d509c6382491673883598eb9ed2d3a6a8bTed Kremenek    FilteredCFGBlockIterator &operator++() {
592ee7f84d509c6382491673883598eb9ed2d3a6a8bTed Kremenek      do { ++I; } while (hasMore() && Filter(*I));
593ee7f84d509c6382491673883598eb9ed2d3a6a8bTed Kremenek      return *this;
594ee7f84d509c6382491673883598eb9ed2d3a6a8bTed Kremenek    }
595ee7f84d509c6382491673883598eb9ed2d3a6a8bTed Kremenek
596ee7f84d509c6382491673883598eb9ed2d3a6a8bTed Kremenek    const CFGBlock *operator*() const { return *I; }
597ee7f84d509c6382491673883598eb9ed2d3a6a8bTed Kremenek  private:
598ee7f84d509c6382491673883598eb9ed2d3a6a8bTed Kremenek    bool Filter(const CFGBlock *To) {
599ee7f84d509c6382491673883598eb9ed2d3a6a8bTed Kremenek      return IsPred ? FilterEdge(F, To, From) : FilterEdge(F, From, To);
600ee7f84d509c6382491673883598eb9ed2d3a6a8bTed Kremenek    }
601ee7f84d509c6382491673883598eb9ed2d3a6a8bTed Kremenek  };
602ee7f84d509c6382491673883598eb9ed2d3a6a8bTed Kremenek
603ee7f84d509c6382491673883598eb9ed2d3a6a8bTed Kremenek  typedef FilteredCFGBlockIterator<const_pred_iterator, true>
604ee7f84d509c6382491673883598eb9ed2d3a6a8bTed Kremenek          filtered_pred_iterator;
605ee7f84d509c6382491673883598eb9ed2d3a6a8bTed Kremenek
606ee7f84d509c6382491673883598eb9ed2d3a6a8bTed Kremenek  typedef FilteredCFGBlockIterator<const_succ_iterator, false>
607ee7f84d509c6382491673883598eb9ed2d3a6a8bTed Kremenek          filtered_succ_iterator;
608ee7f84d509c6382491673883598eb9ed2d3a6a8bTed Kremenek
609ee7f84d509c6382491673883598eb9ed2d3a6a8bTed Kremenek  filtered_pred_iterator filtered_pred_start_end(const FilterOptions &f) const {
610ee7f84d509c6382491673883598eb9ed2d3a6a8bTed Kremenek    return filtered_pred_iterator(pred_begin(), pred_end(), this, f);
611ee7f84d509c6382491673883598eb9ed2d3a6a8bTed Kremenek  }
612ee7f84d509c6382491673883598eb9ed2d3a6a8bTed Kremenek
613ee7f84d509c6382491673883598eb9ed2d3a6a8bTed Kremenek  filtered_succ_iterator filtered_succ_start_end(const FilterOptions &f) const {
614ee7f84d509c6382491673883598eb9ed2d3a6a8bTed Kremenek    return filtered_succ_iterator(succ_begin(), succ_end(), this, f);
615ee7f84d509c6382491673883598eb9ed2d3a6a8bTed Kremenek  }
616ee7f84d509c6382491673883598eb9ed2d3a6a8bTed Kremenek
617fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek  // Manipulation of block contents
6181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
619651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void setTerminator(CFGTerminator Term) { Terminator = Term; }
6209c378f705405d37f49795d5e915989de774fe11fTed Kremenek  void setLabel(Stmt *Statement) { Label = Statement; }
6213575f84e459033d6427b84b4b795b22c85c4d27dTed Kremenek  void setLoopTarget(const Stmt *loopTarget) { LoopTarget = loopTarget; }
62283754162f698a5dafad93fb89be0953651a604d0Chandler Carruth  void setHasNoReturnElement() { HasNoReturnElement = true; }
6239cffe7366ea3beb33c2d58f43a8c3a04c1873e11Ted Kremenek
6244ba72a0b28135209c435630682febe1f854ccfa6Marcin Swiderski  CFGTerminator getTerminator() { return Terminator; }
6254ba72a0b28135209c435630682febe1f854ccfa6Marcin Swiderski  const CFGTerminator getTerminator() const { return Terminator; }
6261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
627651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  Stmt *getTerminatorCondition(bool StripParens = true);
6281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
629651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  const Stmt *getTerminatorCondition(bool StripParens = true) const {
630651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    return const_cast<CFGBlock*>(this)->getTerminatorCondition(StripParens);
631411cdee0b490f79428c9eb977f25199eb7d21cd8Ted Kremenek  }
6321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6333575f84e459033d6427b84b4b795b22c85c4d27dTed Kremenek  const Stmt *getLoopTarget() const { return LoopTarget; }
6341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6359c378f705405d37f49795d5e915989de774fe11fTed Kremenek  Stmt *getLabel() { return Label; }
6369c378f705405d37f49795d5e915989de774fe11fTed Kremenek  const Stmt *getLabel() const { return Label; }
6371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
63883754162f698a5dafad93fb89be0953651a604d0Chandler Carruth  bool hasNoReturnElement() const { return HasNoReturnElement; }
63983754162f698a5dafad93fb89be0953651a604d0Chandler Carruth
640fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek  unsigned getBlockID() const { return BlockID; }
6411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
64202f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks  CFG *getParent() const { return Parent; }
64302f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks
644ef8225444452a1486bd721f3285301fe84643b00Stephen Hines  void dump() const;
645ef8225444452a1486bd721f3285301fe84643b00Stephen Hines
646682060c5d95f6e4f79536013781ab0870cdd3850Ted Kremenek  void dump(const CFG *cfg, const LangOptions &LO, bool ShowColors = false) const;
647682060c5d95f6e4f79536013781ab0870cdd3850Ted Kremenek  void print(raw_ostream &OS, const CFG* cfg, const LangOptions &LO,
648682060c5d95f6e4f79536013781ab0870cdd3850Ted Kremenek             bool ShowColors) const;
6498cc488fefb2fb04bc8d5398da29f0182f97934cfChris Lattner  void printTerminator(raw_ostream &OS, const LangOptions &LO) const;
650651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void printAsOperand(raw_ostream &OS, bool /*PrintType*/) {
651651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    OS << "BB#" << getBlockID();
652ee82d9bdc5025b82de8ce2a4ad4685e0a8b79da9Ted Kremenek  }
653ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
654651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  /// Adds a (potentially unreachable) successor block to the current block.
655651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void addSuccessor(AdjacentBlock Succ, BumpVectorContext &C);
656651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
6579c378f705405d37f49795d5e915989de774fe11fTed Kremenek  void appendStmt(Stmt *statement, BumpVectorContext &C) {
658892697dd2287caf7c29aaaa82909b0e90b8b63feTed Kremenek    Elements.push_back(CFGStmt(statement), C);
659079bd72439448b78629a28da6b1f8abe2cdeaf4dMike Stump  }
66082bc3fd823d85ee3ef9a641c0975b6ad25f55047Marcin Swiderski
661cbb67480094b3bcb5b715acd827cbad55e2a204cSean Hunt  void appendInitializer(CXXCtorInitializer *initializer,
6629c378f705405d37f49795d5e915989de774fe11fTed Kremenek                        BumpVectorContext &C) {
663892697dd2287caf7c29aaaa82909b0e90b8b63feTed Kremenek    Elements.push_back(CFGInitializer(initializer), C);
66482bc3fd823d85ee3ef9a641c0975b6ad25f55047Marcin Swiderski  }
66582bc3fd823d85ee3ef9a641c0975b6ad25f55047Marcin Swiderski
666651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void appendNewAllocator(CXXNewExpr *NE,
667651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                          BumpVectorContext &C) {
668651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    Elements.push_back(CFGNewAllocator(NE), C);
669651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  }
670651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
6717c625d8ffc20b92fff9e1690cd2484fcb6498183Marcin Swiderski  void appendBaseDtor(const CXXBaseSpecifier *BS, BumpVectorContext &C) {
6727c625d8ffc20b92fff9e1690cd2484fcb6498183Marcin Swiderski    Elements.push_back(CFGBaseDtor(BS), C);
6737c625d8ffc20b92fff9e1690cd2484fcb6498183Marcin Swiderski  }
6747c625d8ffc20b92fff9e1690cd2484fcb6498183Marcin Swiderski
6757c625d8ffc20b92fff9e1690cd2484fcb6498183Marcin Swiderski  void appendMemberDtor(FieldDecl *FD, BumpVectorContext &C) {
6767c625d8ffc20b92fff9e1690cd2484fcb6498183Marcin Swiderski    Elements.push_back(CFGMemberDtor(FD), C);
6777c625d8ffc20b92fff9e1690cd2484fcb6498183Marcin Swiderski  }
678ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
6798599e7677e067fd01d3b2ee4c0875747d367fd8eMarcin Swiderski  void appendTemporaryDtor(CXXBindTemporaryExpr *E, BumpVectorContext &C) {
6808599e7677e067fd01d3b2ee4c0875747d367fd8eMarcin Swiderski    Elements.push_back(CFGTemporaryDtor(E), C);
6818599e7677e067fd01d3b2ee4c0875747d367fd8eMarcin Swiderski  }
6827c625d8ffc20b92fff9e1690cd2484fcb6498183Marcin Swiderski
683c8cfc74bdcc999828bc232294d937fb191940d5bChandler Carruth  void appendAutomaticObjDtor(VarDecl *VD, Stmt *S, BumpVectorContext &C) {
684c8cfc74bdcc999828bc232294d937fb191940d5bChandler Carruth    Elements.push_back(CFGAutomaticObjDtor(VD, S), C);
685c8cfc74bdcc999828bc232294d937fb191940d5bChandler Carruth  }
686c8cfc74bdcc999828bc232294d937fb191940d5bChandler Carruth
68736d558d85653315edb389677e995ec9ccdbfbf3dJordan Rose  void appendDeleteDtor(CXXRecordDecl *RD, CXXDeleteExpr *DE, BumpVectorContext &C) {
68836d558d85653315edb389677e995ec9ccdbfbf3dJordan Rose    Elements.push_back(CFGDeleteDtor(RD, DE), C);
68936d558d85653315edb389677e995ec9ccdbfbf3dJordan Rose  }
69036d558d85653315edb389677e995ec9ccdbfbf3dJordan Rose
69153de134e7b4686eed40bc031438d8a4560a2cda4Marcin Swiderski  // Destructors must be inserted in reversed order. So insertion is in two
69253de134e7b4686eed40bc031438d8a4560a2cda4Marcin Swiderski  // steps. First we prepare space for some number of elements, then we insert
69353de134e7b4686eed40bc031438d8a4560a2cda4Marcin Swiderski  // the elements beginning at the last position in prepared space.
69453de134e7b4686eed40bc031438d8a4560a2cda4Marcin Swiderski  iterator beginAutomaticObjDtorsInsert(iterator I, size_t Cnt,
6959c378f705405d37f49795d5e915989de774fe11fTed Kremenek      BumpVectorContext &C) {
6966bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return iterator(Elements.insert(I.base(), Cnt,
6976bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                    CFGAutomaticObjDtor(nullptr, 0), C));
69853de134e7b4686eed40bc031438d8a4560a2cda4Marcin Swiderski  }
6999c378f705405d37f49795d5e915989de774fe11fTed Kremenek  iterator insertAutomaticObjDtor(iterator I, VarDecl *VD, Stmt *S) {
70053de134e7b4686eed40bc031438d8a4560a2cda4Marcin Swiderski    *I = CFGAutomaticObjDtor(VD, S);
70153de134e7b4686eed40bc031438d8a4560a2cda4Marcin Swiderski    return ++I;
70253de134e7b4686eed40bc031438d8a4560a2cda4Marcin Swiderski  }
703fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek};
7041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7056bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines/// \brief CFGCallback defines methods that should be called when a logical
7066bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines/// operator error is found when building the CFG.
7076bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesclass CFGCallback {
7086bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinespublic:
7096bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  CFGCallback() {}
7106bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  virtual void compareAlwaysTrue(const BinaryOperator *B, bool isAlwaysTrue) {}
7116bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  virtual void compareBitwiseEquality(const BinaryOperator *B,
7126bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                      bool isAlwaysTrue) {}
7136bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  virtual ~CFGCallback() {}
7146bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines};
7156bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
716fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek/// CFG - Represents a source-level, intra-procedural CFG that represents the
717fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek///  control-flow of a Stmt.  The Stmt can represent an entire function body,
718fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek///  or a single expression.  A CFG will always contain one empty block that
719fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek///  represents the Exit point of the CFG.  A CFG will also contain a designated
720fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek///  Entry block.  The CFG solely represents control-flow; it consists of
721fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek///  CFGBlocks which are simply containers of Stmt*'s in the AST the CFG
722fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek///  was constructed from.
723fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenekclass CFG {
724fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenekpublic:
725a36c6540346266f570b2fc7457950dea45d89988Ted Kremenek  //===--------------------------------------------------------------------===//
726a36c6540346266f570b2fc7457950dea45d89988Ted Kremenek  // CFG Construction & Manipulation.
727a36c6540346266f570b2fc7457950dea45d89988Ted Kremenek  //===--------------------------------------------------------------------===//
728a36c6540346266f570b2fc7457950dea45d89988Ted Kremenek
7296c52c7850bccb6991470668429a1e1edf01b0873Ted Kremenek  class BuildOptions {
730681ab8998793899076bae9cd6383a5d78b8ee1acBenjamin Kramer    std::bitset<Stmt::lastStmtConstant> alwaysAddMask;
7316c52c7850bccb6991470668429a1e1edf01b0873Ted Kremenek  public:
7320d28d360b5559abda755e50b855ba5e59727d9cdTed Kremenek    typedef llvm::DenseMap<const Stmt *, const CFGBlock*> ForcedBlkExprs;
733ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie    ForcedBlkExprs **forcedBlkExprs;
7346bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    CFGCallback *Observer;
735bc5cb8a5fe2b88f917d47ceb58b53696a121e57eTed Kremenek    bool PruneTriviallyFalseEdges;
736bc5cb8a5fe2b88f917d47ceb58b53696a121e57eTed Kremenek    bool AddEHEdges;
737bc5cb8a5fe2b88f917d47ceb58b53696a121e57eTed Kremenek    bool AddInitializers;
738bc5cb8a5fe2b88f917d47ceb58b53696a121e57eTed Kremenek    bool AddImplicitDtors;
7395a1ffe98b04120846a15f7105905b5f363b08635Jordan Rose    bool AddTemporaryDtors;
740fbd4b5dc7febd1d8b4fa64ab00a29d72b44bec7bTed Kremenek    bool AddStaticInitBranches;
741651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    bool AddCXXNewAllocator;
742ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
74374fb1a493cf5d2dd0fb51a4eadf74e85e10a3457Ted Kremenek    bool alwaysAdd(const Stmt *stmt) const {
74474fb1a493cf5d2dd0fb51a4eadf74e85e10a3457Ted Kremenek      return alwaysAddMask[stmt->getStmtClass()];
74574fb1a493cf5d2dd0fb51a4eadf74e85e10a3457Ted Kremenek    }
746ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
747882998923889a2fcce9b49696506c499e22cf38fTed Kremenek    BuildOptions &setAlwaysAdd(Stmt::StmtClass stmtClass, bool val = true) {
748882998923889a2fcce9b49696506c499e22cf38fTed Kremenek      alwaysAddMask[stmtClass] = val;
749882998923889a2fcce9b49696506c499e22cf38fTed Kremenek      return *this;
750882998923889a2fcce9b49696506c499e22cf38fTed Kremenek    }
751ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
752882998923889a2fcce9b49696506c499e22cf38fTed Kremenek    BuildOptions &setAllAlwaysAdd() {
753882998923889a2fcce9b49696506c499e22cf38fTed Kremenek      alwaysAddMask.set();
754bc5cb8a5fe2b88f917d47ceb58b53696a121e57eTed Kremenek      return *this;
75574fb1a493cf5d2dd0fb51a4eadf74e85e10a3457Ted Kremenek    }
7566c52c7850bccb6991470668429a1e1edf01b0873Ted Kremenek
7576c52c7850bccb6991470668429a1e1edf01b0873Ted Kremenek    BuildOptions()
7586bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      : forcedBlkExprs(nullptr), Observer(nullptr),
7596bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        PruneTriviallyFalseEdges(true), AddEHEdges(false),
7606bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        AddInitializers(false), AddImplicitDtors(false),
7616bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        AddTemporaryDtors(false), AddStaticInitBranches(false),
7626bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        AddCXXNewAllocator(false) {}
7636c52c7850bccb6991470668429a1e1edf01b0873Ted Kremenek  };
7646c52c7850bccb6991470668429a1e1edf01b0873Ted Kremenek
76502f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks  /// \brief Provides a custom implementation of the iterator class to have the
76602f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks  /// same interface as Function::iterator - iterator returns CFGBlock
76702f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks  /// (not a pointer to CFGBlock).
76802f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks  class graph_iterator {
76902f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks  public:
77002f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks    typedef const CFGBlock                  value_type;
77102f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks    typedef value_type&                     reference;
77202f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks    typedef value_type*                     pointer;
77302f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks    typedef BumpVector<CFGBlock*>::iterator ImplTy;
77402f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks
77502f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks    graph_iterator(const ImplTy &i) : I(i) {}
77602f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks
77702f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks    bool operator==(const graph_iterator &X) const { return I == X.I; }
77802f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks    bool operator!=(const graph_iterator &X) const { return I != X.I; }
77902f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks
78002f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks    reference operator*()    const { return **I; }
78102f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks    pointer operator->()     const { return  *I; }
78202f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks    operator CFGBlock* ()          { return  *I; }
78302f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks
78402f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks    graph_iterator &operator++() { ++I; return *this; }
78502f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks    graph_iterator &operator--() { --I; return *this; }
78602f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks
78702f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks  private:
78802f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks    ImplTy I;
78902f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks  };
79002f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks
79102f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks  class const_graph_iterator {
79202f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks  public:
79302f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks    typedef const CFGBlock                  value_type;
79402f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks    typedef value_type&                     reference;
79502f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks    typedef value_type*                     pointer;
79602f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks    typedef BumpVector<CFGBlock*>::const_iterator ImplTy;
79702f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks
79802f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks    const_graph_iterator(const ImplTy &i) : I(i) {}
79902f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks
80002f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks    bool operator==(const const_graph_iterator &X) const { return I == X.I; }
80102f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks    bool operator!=(const const_graph_iterator &X) const { return I != X.I; }
80202f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks
80302f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks    reference operator*() const { return **I; }
80402f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks    pointer operator->()  const { return  *I; }
80502f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks    operator CFGBlock* () const { return  *I; }
80602f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks
80702f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks    const_graph_iterator &operator++() { ++I; return *this; }
80802f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks    const_graph_iterator &operator--() { --I; return *this; }
80902f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks
81002f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks  private:
81102f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks    ImplTy I;
81202f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks  };
81302f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks
814a36c6540346266f570b2fc7457950dea45d89988Ted Kremenek  /// buildCFG - Builds a CFG from an AST.  The responsibility to free the
8151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  ///   constructed CFG belongs to the caller.
8169c378f705405d37f49795d5e915989de774fe11fTed Kremenek  static CFG* buildCFG(const Decl *D, Stmt *AST, ASTContext *C,
817b8ad5ee345fa1fdd1fa9253f2d01f69becc88a04Ted Kremenek                       const BuildOptions &BO);
8181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
819a36c6540346266f570b2fc7457950dea45d89988Ted Kremenek  /// createBlock - Create a new block in the CFG.  The CFG owns the block;
820a36c6540346266f570b2fc7457950dea45d89988Ted Kremenek  ///  the caller should not directly free it.
8219c378f705405d37f49795d5e915989de774fe11fTed Kremenek  CFGBlock *createBlock();
8221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
823a36c6540346266f570b2fc7457950dea45d89988Ted Kremenek  /// setEntry - Set the entry block of the CFG.  This is typically used
824a36c6540346266f570b2fc7457950dea45d89988Ted Kremenek  ///  only during CFG construction.  Most CFG clients expect that the
825a36c6540346266f570b2fc7457950dea45d89988Ted Kremenek  ///  entry block has no predecessors and contains no statements.
826a36c6540346266f570b2fc7457950dea45d89988Ted Kremenek  void setEntry(CFGBlock *B) { Entry = B; }
8271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
828e38158de9236dfcded7da56126134e5e3e49cb01Ted Kremenek  /// setIndirectGotoBlock - Set the block used for indirect goto jumps.
829e38158de9236dfcded7da56126134e5e3e49cb01Ted Kremenek  ///  This is typically used only during CFG construction.
8309c378f705405d37f49795d5e915989de774fe11fTed Kremenek  void setIndirectGotoBlock(CFGBlock *B) { IndirectGotoBlock = B; }
8311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
832a36c6540346266f570b2fc7457950dea45d89988Ted Kremenek  //===--------------------------------------------------------------------===//
833a36c6540346266f570b2fc7457950dea45d89988Ted Kremenek  // Block Iterators
834a36c6540346266f570b2fc7457950dea45d89988Ted Kremenek  //===--------------------------------------------------------------------===//
835a36c6540346266f570b2fc7457950dea45d89988Ted Kremenek
836ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  typedef BumpVector<CFGBlock*>                    CFGBlockListTy;
837a36c6540346266f570b2fc7457950dea45d89988Ted Kremenek  typedef CFGBlockListTy::iterator                 iterator;
838a36c6540346266f570b2fc7457950dea45d89988Ted Kremenek  typedef CFGBlockListTy::const_iterator           const_iterator;
839a36c6540346266f570b2fc7457950dea45d89988Ted Kremenek  typedef std::reverse_iterator<iterator>          reverse_iterator;
840a36c6540346266f570b2fc7457950dea45d89988Ted Kremenek  typedef std::reverse_iterator<const_iterator>    const_reverse_iterator;
841fddd51853f8ccaa1df2476376e6fd74d2f315c73Ted Kremenek
8429c378f705405d37f49795d5e915989de774fe11fTed Kremenek  CFGBlock &                front()                { return *Blocks.front(); }
8439c378f705405d37f49795d5e915989de774fe11fTed Kremenek  CFGBlock &                back()                 { return *Blocks.back(); }
8441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
84519bb356317952445b03ee341c02f6147083c9eeaTed Kremenek  iterator                  begin()                { return Blocks.begin(); }
84619bb356317952445b03ee341c02f6147083c9eeaTed Kremenek  iterator                  end()                  { return Blocks.end(); }
84719bb356317952445b03ee341c02f6147083c9eeaTed Kremenek  const_iterator            begin()       const    { return Blocks.begin(); }
8481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  const_iterator            end()         const    { return Blocks.end(); }
8491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
85002f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks  graph_iterator nodes_begin() { return graph_iterator(Blocks.begin()); }
85102f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks  graph_iterator nodes_end() { return graph_iterator(Blocks.end()); }
85202f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks  const_graph_iterator nodes_begin() const {
85302f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks    return const_graph_iterator(Blocks.begin());
85402f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks  }
85502f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks  const_graph_iterator nodes_end() const {
85602f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks    return const_graph_iterator(Blocks.end());
85702f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks  }
85802f34c5003b2c5067675f89ffce0a84c28faf722Anna Zaks
85919bb356317952445b03ee341c02f6147083c9eeaTed Kremenek  reverse_iterator          rbegin()               { return Blocks.rbegin(); }
86019bb356317952445b03ee341c02f6147083c9eeaTed Kremenek  reverse_iterator          rend()                 { return Blocks.rend(); }
86119bb356317952445b03ee341c02f6147083c9eeaTed Kremenek  const_reverse_iterator    rbegin()      const    { return Blocks.rbegin(); }
86219bb356317952445b03ee341c02f6147083c9eeaTed Kremenek  const_reverse_iterator    rend()        const    { return Blocks.rend(); }
8631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8649c378f705405d37f49795d5e915989de774fe11fTed Kremenek  CFGBlock &                getEntry()             { return *Entry; }
8659c378f705405d37f49795d5e915989de774fe11fTed Kremenek  const CFGBlock &          getEntry()    const    { return *Entry; }
8669c378f705405d37f49795d5e915989de774fe11fTed Kremenek  CFGBlock &                getExit()              { return *Exit; }
8679c378f705405d37f49795d5e915989de774fe11fTed Kremenek  const CFGBlock &          getExit()     const    { return *Exit; }
8687dba8607e59096014b7139ff20ef00870041d518Ted Kremenek
8699c378f705405d37f49795d5e915989de774fe11fTed Kremenek  CFGBlock *       getIndirectGotoBlock() { return IndirectGotoBlock; }
8709c378f705405d37f49795d5e915989de774fe11fTed Kremenek  const CFGBlock * getIndirectGotoBlock() const { return IndirectGotoBlock; }
871ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
872f0e71aede7ccf3e311feac6a414c431f7a0fc3c8Ted Kremenek  typedef std::vector<const CFGBlock*>::const_iterator try_block_iterator;
873f0e71aede7ccf3e311feac6a414c431f7a0fc3c8Ted Kremenek  try_block_iterator try_blocks_begin() const {
874f0e71aede7ccf3e311feac6a414c431f7a0fc3c8Ted Kremenek    return TryDispatchBlocks.begin();
875f0e71aede7ccf3e311feac6a414c431f7a0fc3c8Ted Kremenek  }
876f0e71aede7ccf3e311feac6a414c431f7a0fc3c8Ted Kremenek  try_block_iterator try_blocks_end() const {
877f0e71aede7ccf3e311feac6a414c431f7a0fc3c8Ted Kremenek    return TryDispatchBlocks.end();
878f0e71aede7ccf3e311feac6a414c431f7a0fc3c8Ted Kremenek  }
879ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
880f0e71aede7ccf3e311feac6a414c431f7a0fc3c8Ted Kremenek  void addTryDispatchBlock(const CFGBlock *block) {
881f0e71aede7ccf3e311feac6a414c431f7a0fc3c8Ted Kremenek    TryDispatchBlocks.push_back(block);
882f0e71aede7ccf3e311feac6a414c431f7a0fc3c8Ted Kremenek  }
8831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
88449a246f4fad959888bb0164c624c3c2b03078e91Jordan Rose  /// Records a synthetic DeclStmt and the DeclStmt it was constructed from.
88549a246f4fad959888bb0164c624c3c2b03078e91Jordan Rose  ///
88649a246f4fad959888bb0164c624c3c2b03078e91Jordan Rose  /// The CFG uses synthetic DeclStmts when a single AST DeclStmt contains
88749a246f4fad959888bb0164c624c3c2b03078e91Jordan Rose  /// multiple decls.
88849a246f4fad959888bb0164c624c3c2b03078e91Jordan Rose  void addSyntheticDeclStmt(const DeclStmt *Synthetic,
88949a246f4fad959888bb0164c624c3c2b03078e91Jordan Rose                            const DeclStmt *Source) {
89049a246f4fad959888bb0164c624c3c2b03078e91Jordan Rose    assert(Synthetic->isSingleDecl() && "Can handle single declarations only");
89149a246f4fad959888bb0164c624c3c2b03078e91Jordan Rose    assert(Synthetic != Source && "Don't include original DeclStmts in map");
89249a246f4fad959888bb0164c624c3c2b03078e91Jordan Rose    assert(!SyntheticDeclStmts.count(Synthetic) && "Already in map");
89349a246f4fad959888bb0164c624c3c2b03078e91Jordan Rose    SyntheticDeclStmts[Synthetic] = Source;
89449a246f4fad959888bb0164c624c3c2b03078e91Jordan Rose  }
89549a246f4fad959888bb0164c624c3c2b03078e91Jordan Rose
89649a246f4fad959888bb0164c624c3c2b03078e91Jordan Rose  typedef llvm::DenseMap<const DeclStmt *, const DeclStmt *>::const_iterator
89749a246f4fad959888bb0164c624c3c2b03078e91Jordan Rose    synthetic_stmt_iterator;
89849a246f4fad959888bb0164c624c3c2b03078e91Jordan Rose
89949a246f4fad959888bb0164c624c3c2b03078e91Jordan Rose  /// Iterates over synthetic DeclStmts in the CFG.
90049a246f4fad959888bb0164c624c3c2b03078e91Jordan Rose  ///
901