JumpDiagnostics.cpp revision c985b88efc9188845952ab524fe6d4717705257b
167bc6071d7abea2d4870bc4fd8f119ebd93c46f3Francois Pichet//===--- JumpDiagnostics.cpp - Protected scope jump analysis ------*- C++ -*-=//
25af280ce21af061f96b5b5b752746871e364ba99Chris Lattner//
35af280ce21af061f96b5b5b752746871e364ba99Chris Lattner//                     The LLVM Compiler Infrastructure
45af280ce21af061f96b5b5b752746871e364ba99Chris Lattner//
55af280ce21af061f96b5b5b752746871e364ba99Chris Lattner// This file is distributed under the University of Illinois Open Source
65af280ce21af061f96b5b5b752746871e364ba99Chris Lattner// License. See LICENSE.TXT for details.
75af280ce21af061f96b5b5b752746871e364ba99Chris Lattner//
85af280ce21af061f96b5b5b752746871e364ba99Chris Lattner//===----------------------------------------------------------------------===//
95af280ce21af061f96b5b5b752746871e364ba99Chris Lattner//
105af280ce21af061f96b5b5b752746871e364ba99Chris Lattner// This file implements the JumpScopeChecker class, which is used to diagnose
1167bc6071d7abea2d4870bc4fd8f119ebd93c46f3Francois Pichet// jumps that enter a protected scope in an invalid way.
125af280ce21af061f96b5b5b752746871e364ba99Chris Lattner//
135af280ce21af061f96b5b5b752746871e364ba99Chris Lattner//===----------------------------------------------------------------------===//
145af280ce21af061f96b5b5b752746871e364ba99Chris Lattner
152d88708cbe4e4ec5e04e4acb6bd7f5be68557379John McCall#include "clang/Sema/SemaInternal.h"
16384aff8b94bb0d1ad6c5667b90621e5699815bb2John McCall#include "clang/AST/DeclCXX.h"
175af280ce21af061f96b5b5b752746871e364ba99Chris Lattner#include "clang/AST/Expr.h"
18e413516d0ed61ef9e2ff706bcc00480adca947c4Douglas Gregor#include "clang/AST/ExprCXX.h"
1916f0049415ec596504891259e2a83e19871c0d52Chris Lattner#include "clang/AST/StmtObjC.h"
20972041f45bdf8df7ea447221292d7827466ba94bSebastian Redl#include "clang/AST/StmtCXX.h"
21e737f5041a36d0befb39ffeed8d50ba15916d3daDouglas Gregor#include "llvm/ADT/BitVector.h"
225af280ce21af061f96b5b5b752746871e364ba99Chris Lattnerusing namespace clang;
235af280ce21af061f96b5b5b752746871e364ba99Chris Lattner
245af280ce21af061f96b5b5b752746871e364ba99Chris Lattnernamespace {
255af280ce21af061f96b5b5b752746871e364ba99Chris Lattner
265af280ce21af061f96b5b5b752746871e364ba99Chris Lattner/// JumpScopeChecker - This object is used by Sema to diagnose invalid jumps
275af280ce21af061f96b5b5b752746871e364ba99Chris Lattner/// into VLA and other protected scopes.  For example, this rejects:
285af280ce21af061f96b5b5b752746871e364ba99Chris Lattner///    goto L;
295af280ce21af061f96b5b5b752746871e364ba99Chris Lattner///    int a[n];
305af280ce21af061f96b5b5b752746871e364ba99Chris Lattner///  L:
315af280ce21af061f96b5b5b752746871e364ba99Chris Lattner///
325af280ce21af061f96b5b5b752746871e364ba99Chris Lattnerclass JumpScopeChecker {
335af280ce21af061f96b5b5b752746871e364ba99Chris Lattner  Sema &S;
341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
355af280ce21af061f96b5b5b752746871e364ba99Chris Lattner  /// GotoScope - This is a record that we use to keep track of all of the
365af280ce21af061f96b5b5b752746871e364ba99Chris Lattner  /// scopes that are introduced by VLAs and other things that scope jumps like
375af280ce21af061f96b5b5b752746871e364ba99Chris Lattner  /// gotos.  This scope tree has nothing to do with the source scope tree,
385af280ce21af061f96b5b5b752746871e364ba99Chris Lattner  /// because you can have multiple VLA scopes per compound statement, and most
395af280ce21af061f96b5b5b752746871e364ba99Chris Lattner  /// compound statements don't introduce any scopes.
405af280ce21af061f96b5b5b752746871e364ba99Chris Lattner  struct GotoScope {
415af280ce21af061f96b5b5b752746871e364ba99Chris Lattner    /// ParentScope - The index in ScopeMap of the parent scope.  This is 0 for
425af280ce21af061f96b5b5b752746871e364ba99Chris Lattner    /// the parent scope is the function body.
435af280ce21af061f96b5b5b752746871e364ba99Chris Lattner    unsigned ParentScope;
441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
45ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall    /// InDiag - The diagnostic to emit if there is a jump into this scope.
46ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall    unsigned InDiag;
47ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall
48ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall    /// OutDiag - The diagnostic to emit if there is an indirect jump out
49ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall    /// of this scope.  Direct jumps always clean up their current scope
50ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall    /// in an orderly way.
51ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall    unsigned OutDiag;
521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
535af280ce21af061f96b5b5b752746871e364ba99Chris Lattner    /// Loc - Location to emit the diagnostic.
545af280ce21af061f96b5b5b752746871e364ba99Chris Lattner    SourceLocation Loc;
551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
56ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall    GotoScope(unsigned parentScope, unsigned InDiag, unsigned OutDiag,
57ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall              SourceLocation L)
58ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall      : ParentScope(parentScope), InDiag(InDiag), OutDiag(OutDiag), Loc(L) {}
595af280ce21af061f96b5b5b752746871e364ba99Chris Lattner  };
601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
615f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<GotoScope, 48> Scopes;
625af280ce21af061f96b5b5b752746871e364ba99Chris Lattner  llvm::DenseMap<Stmt*, unsigned> LabelAndGotoScopes;
635f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Stmt*, 16> Jumps;
64ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall
655f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<IndirectGotoStmt*, 4> IndirectJumps;
665f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<LabelDecl*, 4> IndirectJumpTargets;
675af280ce21af061f96b5b5b752746871e364ba99Chris Lattnerpublic:
685af280ce21af061f96b5b5b752746871e364ba99Chris Lattner  JumpScopeChecker(Stmt *Body, Sema &S);
695af280ce21af061f96b5b5b752746871e364ba99Chris Lattnerprivate:
7043dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor  void BuildScopeInformation(Decl *D, unsigned &ParentScope);
714e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian  void BuildScopeInformation(VarDecl *D, const BlockDecl *BDecl,
724e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian                             unsigned &ParentScope);
734e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian  void BuildScopeInformation(Stmt *S, unsigned &origParentScope);
744e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian
755af280ce21af061f96b5b5b752746871e364ba99Chris Lattner  void VerifyJumps();
76ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall  void VerifyIndirectJumps();
77ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall  void DiagnoseIndirectJump(IndirectGotoStmt *IG, unsigned IGScope,
78ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner                            LabelDecl *Target, unsigned TargetScope);
79c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet  void CheckJump(Stmt *From, Stmt *To, SourceLocation DiagLoc,
80c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet                 unsigned JumpDiag, unsigned JumpDiagWarning);
815e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall
825e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall  unsigned GetDeepestCommonScope(unsigned A, unsigned B);
835af280ce21af061f96b5b5b752746871e364ba99Chris Lattner};
845af280ce21af061f96b5b5b752746871e364ba99Chris Lattner} // end anonymous namespace
855af280ce21af061f96b5b5b752746871e364ba99Chris Lattner
865af280ce21af061f96b5b5b752746871e364ba99Chris Lattner
875af280ce21af061f96b5b5b752746871e364ba99Chris LattnerJumpScopeChecker::JumpScopeChecker(Stmt *Body, Sema &s) : S(s) {
885af280ce21af061f96b5b5b752746871e364ba99Chris Lattner  // Add a scope entry for function scope.
89ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall  Scopes.push_back(GotoScope(~0U, ~0U, ~0U, SourceLocation()));
901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
915af280ce21af061f96b5b5b752746871e364ba99Chris Lattner  // Build information for the top level compound statement, so that we have a
925af280ce21af061f96b5b5b752746871e364ba99Chris Lattner  // defined scope record for every "goto" and label.
934e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian  unsigned BodyParentScope = 0;
944e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian  BuildScopeInformation(Body, BodyParentScope);
951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
965af280ce21af061f96b5b5b752746871e364ba99Chris Lattner  // Check that all jumps we saw are kosher.
975af280ce21af061f96b5b5b752746871e364ba99Chris Lattner  VerifyJumps();
98ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall  VerifyIndirectJumps();
995af280ce21af061f96b5b5b752746871e364ba99Chris Lattner}
1001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1015e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall/// GetDeepestCommonScope - Finds the innermost scope enclosing the
1025e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall/// two scopes.
1035e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCallunsigned JumpScopeChecker::GetDeepestCommonScope(unsigned A, unsigned B) {
1045e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall  while (A != B) {
1055e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall    // Inner scopes are created after outer scopes and therefore have
1065e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall    // higher indices.
1075e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall    if (A < B) {
1085e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall      assert(Scopes[B].ParentScope < B);
1095e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall      B = Scopes[B].ParentScope;
1105e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall    } else {
1115e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall      assert(Scopes[A].ParentScope < A);
1125e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall      A = Scopes[A].ParentScope;
1135e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall    }
1145e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall  }
1155e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall  return A;
1165e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall}
1175e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall
118f85e193739c953358c865005855253af4f68a497John McCalltypedef std::pair<unsigned,unsigned> ScopePair;
119f85e193739c953358c865005855253af4f68a497John McCall
1205af280ce21af061f96b5b5b752746871e364ba99Chris Lattner/// GetDiagForGotoScopeDecl - If this decl induces a new goto scope, return a
1215af280ce21af061f96b5b5b752746871e364ba99Chris Lattner/// diagnostic that should be emitted if control goes over it. If not, return 0.
122f85e193739c953358c865005855253af4f68a497John McCallstatic ScopePair GetDiagForGotoScopeDecl(ASTContext &Context, const Decl *D) {
1235af280ce21af061f96b5b5b752746871e364ba99Chris Lattner  if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
124ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall    unsigned InDiag = 0, OutDiag = 0;
1255af280ce21af061f96b5b5b752746871e364ba99Chris Lattner    if (VD->getType()->isVariablyModifiedType())
126ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall      InDiag = diag::note_protected_by_vla;
127ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall
128f85e193739c953358c865005855253af4f68a497John McCall    if (VD->hasAttr<BlocksAttr>())
129f85e193739c953358c865005855253af4f68a497John McCall      return ScopePair(diag::note_protected_by___block,
130f85e193739c953358c865005855253af4f68a497John McCall                       diag::note_exits___block);
131f85e193739c953358c865005855253af4f68a497John McCall
132f85e193739c953358c865005855253af4f68a497John McCall    if (VD->hasAttr<CleanupAttr>())
133f85e193739c953358c865005855253af4f68a497John McCall      return ScopePair(diag::note_protected_by_cleanup,
134f85e193739c953358c865005855253af4f68a497John McCall                       diag::note_exits_cleanup);
135f85e193739c953358c865005855253af4f68a497John McCall
136f85e193739c953358c865005855253af4f68a497John McCall    if (Context.getLangOptions().ObjCAutoRefCount && VD->hasLocalStorage()) {
137f85e193739c953358c865005855253af4f68a497John McCall      switch (VD->getType().getObjCLifetime()) {
138f85e193739c953358c865005855253af4f68a497John McCall      case Qualifiers::OCL_None:
139f85e193739c953358c865005855253af4f68a497John McCall      case Qualifiers::OCL_ExplicitNone:
140f85e193739c953358c865005855253af4f68a497John McCall      case Qualifiers::OCL_Autoreleasing:
141f85e193739c953358c865005855253af4f68a497John McCall        break;
142f85e193739c953358c865005855253af4f68a497John McCall
143f85e193739c953358c865005855253af4f68a497John McCall      case Qualifiers::OCL_Strong:
144f85e193739c953358c865005855253af4f68a497John McCall      case Qualifiers::OCL_Weak:
145b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidis        return ScopePair(diag::note_protected_by_objc_ownership,
146b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidis                         diag::note_exits_objc_ownership);
147f85e193739c953358c865005855253af4f68a497John McCall      }
148f85e193739c953358c865005855253af4f68a497John McCall    }
149f85e193739c953358c865005855253af4f68a497John McCall
150f85e193739c953358c865005855253af4f68a497John McCall    if (Context.getLangOptions().CPlusPlus && VD->hasLocalStorage()) {
151f85e193739c953358c865005855253af4f68a497John McCall      // C++0x [stmt.dcl]p3:
152f85e193739c953358c865005855253af4f68a497John McCall      //   A program that jumps from a point where a variable with automatic
153f85e193739c953358c865005855253af4f68a497John McCall      //   storage duration is not in scope to a point where it is in scope
154f85e193739c953358c865005855253af4f68a497John McCall      //   is ill-formed unless the variable has scalar type, class type with
155f85e193739c953358c865005855253af4f68a497John McCall      //   a trivial default constructor and a trivial destructor, a
156f85e193739c953358c865005855253af4f68a497John McCall      //   cv-qualified version of one of these types, or an array of one of
157f85e193739c953358c865005855253af4f68a497John McCall      //   the preceding types and is declared without an initializer.
158f85e193739c953358c865005855253af4f68a497John McCall
159f85e193739c953358c865005855253af4f68a497John McCall      // C++03 [stmt.dcl.p3:
160f85e193739c953358c865005855253af4f68a497John McCall      //   A program that jumps from a point where a local variable
161f85e193739c953358c865005855253af4f68a497John McCall      //   with automatic storage duration is not in scope to a point
162f85e193739c953358c865005855253af4f68a497John McCall      //   where it is in scope is ill-formed unless the variable has
163f85e193739c953358c865005855253af4f68a497John McCall      //   POD type and is declared without an initializer.
164f85e193739c953358c865005855253af4f68a497John McCall
165f85e193739c953358c865005855253af4f68a497John McCall      if (const Expr *init = VD->getInit()) {
166f85e193739c953358c865005855253af4f68a497John McCall        // We actually give variables of record type (or array thereof)
167f85e193739c953358c865005855253af4f68a497John McCall        // an initializer even if that initializer only calls a trivial
168f85e193739c953358c865005855253af4f68a497John McCall        // ctor.  Detect that case.
169f85e193739c953358c865005855253af4f68a497John McCall        // FIXME: With generalized initializer lists, this may
170f85e193739c953358c865005855253af4f68a497John McCall        // classify "X x{};" as having no initializer.
171f85e193739c953358c865005855253af4f68a497John McCall        unsigned inDiagToUse = diag::note_protected_by_variable_init;
172f85e193739c953358c865005855253af4f68a497John McCall
173f85e193739c953358c865005855253af4f68a497John McCall        const CXXRecordDecl *record = 0;
174f85e193739c953358c865005855253af4f68a497John McCall
175f85e193739c953358c865005855253af4f68a497John McCall        if (const CXXConstructExpr *cce = dyn_cast<CXXConstructExpr>(init)) {
176f85e193739c953358c865005855253af4f68a497John McCall          const CXXConstructorDecl *ctor = cce->getConstructor();
177f85e193739c953358c865005855253af4f68a497John McCall          record = ctor->getParent();
178f85e193739c953358c865005855253af4f68a497John McCall
179f85e193739c953358c865005855253af4f68a497John McCall          if (ctor->isTrivial() && ctor->isDefaultConstructor()) {
180f85e193739c953358c865005855253af4f68a497John McCall            if (Context.getLangOptions().CPlusPlus0x) {
181f85e193739c953358c865005855253af4f68a497John McCall              inDiagToUse = (record->hasTrivialDestructor() ? 0 :
182f85e193739c953358c865005855253af4f68a497John McCall                        diag::note_protected_by_variable_nontriv_destructor);
183f85e193739c953358c865005855253af4f68a497John McCall            } else {
184f85e193739c953358c865005855253af4f68a497John McCall              if (record->isPOD())
185f85e193739c953358c865005855253af4f68a497John McCall                inDiagToUse = 0;
186f85e193739c953358c865005855253af4f68a497John McCall            }
187e413516d0ed61ef9e2ff706bcc00480adca947c4Douglas Gregor          }
188f85e193739c953358c865005855253af4f68a497John McCall        } else if (VD->getType()->isArrayType()) {
189f85e193739c953358c865005855253af4f68a497John McCall          record = VD->getType()->getBaseElementTypeUnsafe()
190f85e193739c953358c865005855253af4f68a497John McCall                                ->getAsCXXRecordDecl();
191e413516d0ed61ef9e2ff706bcc00480adca947c4Douglas Gregor        }
192f85e193739c953358c865005855253af4f68a497John McCall
193f85e193739c953358c865005855253af4f68a497John McCall        if (inDiagToUse)
194f85e193739c953358c865005855253af4f68a497John McCall          InDiag = inDiagToUse;
195f85e193739c953358c865005855253af4f68a497John McCall
196f85e193739c953358c865005855253af4f68a497John McCall        // Also object to indirect jumps which leave scopes with dtors.
197f85e193739c953358c865005855253af4f68a497John McCall        if (record && !record->hasTrivialDestructor())
198f61103ef335fb273a98c1389e6fddaf796feb4b3Douglas Gregor          OutDiag = diag::note_exits_dtor;
199025291b591a528d8a3f303991f65e19fa1e90a9dDouglas Gregor      }
200ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall    }
2016d97e5e4b7abdae710c2548b51f4ed0298e86d80Chris Lattner
202f85e193739c953358c865005855253af4f68a497John McCall    return ScopePair(InDiag, OutDiag);
203ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall  }
204ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall
205ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall  if (const TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
2065af280ce21af061f96b5b5b752746871e364ba99Chris Lattner    if (TD->getUnderlyingType()->isVariablyModifiedType())
207f85e193739c953358c865005855253af4f68a497John McCall      return ScopePair(diag::note_protected_by_vla_typedef, 0);
2085af280ce21af061f96b5b5b752746871e364ba99Chris Lattner  }
2091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
210162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  if (const TypeAliasDecl *TD = dyn_cast<TypeAliasDecl>(D)) {
211162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    if (TD->getUnderlyingType()->isVariablyModifiedType())
212f85e193739c953358c865005855253af4f68a497John McCall      return ScopePair(diag::note_protected_by_vla_type_alias, 0);
213162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  }
214162e1c1b487352434552147967c3dd296ebee2f7Richard Smith
215f85e193739c953358c865005855253af4f68a497John McCall  return ScopePair(0U, 0U);
2165af280ce21af061f96b5b5b752746871e364ba99Chris Lattner}
2175af280ce21af061f96b5b5b752746871e364ba99Chris Lattner
21843dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor/// \brief Build scope information for a declaration that is part of a DeclStmt.
21943dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregorvoid JumpScopeChecker::BuildScopeInformation(Decl *D, unsigned &ParentScope) {
22043dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor  // If this decl causes a new scope, push and switch to it.
221f85e193739c953358c865005855253af4f68a497John McCall  std::pair<unsigned,unsigned> Diags = GetDiagForGotoScopeDecl(S.Context, D);
22243dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor  if (Diags.first || Diags.second) {
22343dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor    Scopes.push_back(GotoScope(ParentScope, Diags.first, Diags.second,
22443dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor                               D->getLocation()));
22543dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor    ParentScope = Scopes.size()-1;
22643dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor  }
22743dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor
22843dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor  // If the decl has an initializer, walk it with the potentially new
22943dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor  // scope we just installed.
23043dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor  if (VarDecl *VD = dyn_cast<VarDecl>(D))
23143dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor    if (Expr *Init = VD->getInit())
23243dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor      BuildScopeInformation(Init, ParentScope);
23343dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor}
2345af280ce21af061f96b5b5b752746871e364ba99Chris Lattner
2354e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian/// \brief Build scope information for a captured block literal variables.
2364e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanianvoid JumpScopeChecker::BuildScopeInformation(VarDecl *D,
2374e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian                                             const BlockDecl *BDecl,
2384e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian                                             unsigned &ParentScope) {
2394e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian  // exclude captured __block variables; there's no destructor
2404e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian  // associated with the block literal for them.
2414e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian  if (D->hasAttr<BlocksAttr>())
2424e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian    return;
2434e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian  QualType T = D->getType();
2444e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian  QualType::DestructionKind destructKind = T.isDestructedType();
2454e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian  if (destructKind != QualType::DK_none) {
2464e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian    std::pair<unsigned,unsigned> Diags;
2474e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian    switch (destructKind) {
2484e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian      case QualType::DK_cxx_destructor:
2494e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian        Diags = ScopePair(diag::note_enters_block_captures_cxx_obj,
2504e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian                          diag::note_exits_block_captures_cxx_obj);
2514e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian        break;
2524e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian      case QualType::DK_objc_strong_lifetime:
2534e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian        Diags = ScopePair(diag::note_enters_block_captures_strong,
2544e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian                          diag::note_exits_block_captures_strong);
2554e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian        break;
2564e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian      case QualType::DK_objc_weak_lifetime:
2574e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian        Diags = ScopePair(diag::note_enters_block_captures_weak,
2584e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian                          diag::note_exits_block_captures_weak);
2594e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian        break;
2604e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian      case QualType::DK_none:
2614e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian        llvm_unreachable("no-liftime captured variable");
2624e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian    }
2634e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian    SourceLocation Loc = D->getLocation();
2644e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian    if (Loc.isInvalid())
2654e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian      Loc = BDecl->getLocation();
2664e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian    Scopes.push_back(GotoScope(ParentScope,
2674e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian                               Diags.first, Diags.second, Loc));
2684e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian    ParentScope = Scopes.size()-1;
2694e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian  }
2704e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian}
2714e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian
2725af280ce21af061f96b5b5b752746871e364ba99Chris Lattner/// BuildScopeInformation - The statements from CI to CE are known to form a
2735af280ce21af061f96b5b5b752746871e364ba99Chris Lattner/// coherent VLA scope with a specified parent node.  Walk through the
2745af280ce21af061f96b5b5b752746871e364ba99Chris Lattner/// statements, adding any labels or gotos to LabelAndGotoScopes and recursively
2755af280ce21af061f96b5b5b752746871e364ba99Chris Lattner/// walking the AST as needed.
2764e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanianvoid JumpScopeChecker::BuildScopeInformation(Stmt *S, unsigned &origParentScope) {
2774e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian  // If this is a statement, rather than an expression, scopes within it don't
2784e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian  // propagate out into the enclosing scope.  Otherwise we have to worry
2794e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian  // about block literals, which have the lifetime of their enclosing statement.
2804e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian  unsigned independentParentScope = origParentScope;
2814e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian  unsigned &ParentScope = ((isa<Expr>(S) && !isa<StmtExpr>(S))
2824e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian                            ? origParentScope : independentParentScope);
2834e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian
28443dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor  bool SkipFirstSubStmt = false;
28543dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor
2865af280ce21af061f96b5b5b752746871e364ba99Chris Lattner  // If we found a label, remember that it is in ParentScope scope.
287ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall  switch (S->getStmtClass()) {
288ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall  case Stmt::AddrLabelExprClass:
289ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall    IndirectJumpTargets.push_back(cast<AddrLabelExpr>(S)->getLabel());
290ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall    break;
291ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall
292ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall  case Stmt::IndirectGotoStmtClass:
29395c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall    // "goto *&&lbl;" is a special case which we treat as equivalent
29495c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall    // to a normal goto.  In addition, we don't calculate scope in the
29595c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall    // operand (to avoid recording the address-of-label use), which
29695c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall    // works only because of the restricted set of expressions which
29795c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall    // we detect as constant targets.
29895c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall    if (cast<IndirectGotoStmt>(S)->getConstantTarget()) {
29995c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall      LabelAndGotoScopes[S] = ParentScope;
30095c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall      Jumps.push_back(S);
30195c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall      return;
30295c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall    }
30395c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall
3045af280ce21af061f96b5b5b752746871e364ba99Chris Lattner    LabelAndGotoScopes[S] = ParentScope;
305ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall    IndirectJumps.push_back(cast<IndirectGotoStmt>(S));
306ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall    break;
307ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall
308ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall  case Stmt::SwitchStmtClass:
30943dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor    // Evaluate the condition variable before entering the scope of the switch
31043dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor    // statement.
31143dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor    if (VarDecl *Var = cast<SwitchStmt>(S)->getConditionVariable()) {
31243dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor      BuildScopeInformation(Var, ParentScope);
31343dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor      SkipFirstSubStmt = true;
31443dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor    }
31543dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor    // Fall through
31643dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor
31743dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor  case Stmt::GotoStmtClass:
3185af280ce21af061f96b5b5b752746871e364ba99Chris Lattner    // Remember both what scope a goto is in as well as the fact that we have
3195af280ce21af061f96b5b5b752746871e364ba99Chris Lattner    // it.  This makes the second scan not have to walk the AST again.
3205af280ce21af061f96b5b5b752746871e364ba99Chris Lattner    LabelAndGotoScopes[S] = ParentScope;
3215af280ce21af061f96b5b5b752746871e364ba99Chris Lattner    Jumps.push_back(S);
322ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall    break;
323ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall
324ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall  default:
325ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall    break;
3265af280ce21af061f96b5b5b752746871e364ba99Chris Lattner  }
3271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3287502c1d3ce8bb97bcc4f7bebef507040bd93b26fJohn McCall  for (Stmt::child_range CI = S->children(); CI; ++CI) {
32943dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor    if (SkipFirstSubStmt) {
33043dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor      SkipFirstSubStmt = false;
33143dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor      continue;
33243dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor    }
33343dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor
3345af280ce21af061f96b5b5b752746871e364ba99Chris Lattner    Stmt *SubStmt = *CI;
3355af280ce21af061f96b5b5b752746871e364ba99Chris Lattner    if (SubStmt == 0) continue;
3361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
33797ba481f3f45e5b63b4a354bfb471ce146b7de57John McCall    // Cases, labels, and defaults aren't "scope parents".  It's also
33897ba481f3f45e5b63b4a354bfb471ce146b7de57John McCall    // important to handle these iteratively instead of recursively in
33997ba481f3f45e5b63b4a354bfb471ce146b7de57John McCall    // order to avoid blowing out the stack.
34097ba481f3f45e5b63b4a354bfb471ce146b7de57John McCall    while (true) {
34197ba481f3f45e5b63b4a354bfb471ce146b7de57John McCall      Stmt *Next;
342ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner      if (CaseStmt *CS = dyn_cast<CaseStmt>(SubStmt))
343ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner        Next = CS->getSubStmt();
344ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner      else if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SubStmt))
345ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner        Next = DS->getSubStmt();
346ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner      else if (LabelStmt *LS = dyn_cast<LabelStmt>(SubStmt))
347ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner        Next = LS->getSubStmt();
34897ba481f3f45e5b63b4a354bfb471ce146b7de57John McCall      else
34997ba481f3f45e5b63b4a354bfb471ce146b7de57John McCall        break;
35097ba481f3f45e5b63b4a354bfb471ce146b7de57John McCall
35197ba481f3f45e5b63b4a354bfb471ce146b7de57John McCall      LabelAndGotoScopes[SubStmt] = ParentScope;
35297ba481f3f45e5b63b4a354bfb471ce146b7de57John McCall      SubStmt = Next;
35397ba481f3f45e5b63b4a354bfb471ce146b7de57John McCall    }
35497ba481f3f45e5b63b4a354bfb471ce146b7de57John McCall
3555af280ce21af061f96b5b5b752746871e364ba99Chris Lattner    // If this is a declstmt with a VLA definition, it defines a scope from here
3565af280ce21af061f96b5b5b752746871e364ba99Chris Lattner    // to the end of the containing context.
3575af280ce21af061f96b5b5b752746871e364ba99Chris Lattner    if (DeclStmt *DS = dyn_cast<DeclStmt>(SubStmt)) {
3586d97e5e4b7abdae710c2548b51f4ed0298e86d80Chris Lattner      // The decl statement creates a scope if any of the decls in it are VLAs
3596d97e5e4b7abdae710c2548b51f4ed0298e86d80Chris Lattner      // or have the cleanup attribute.
3605af280ce21af061f96b5b5b752746871e364ba99Chris Lattner      for (DeclStmt::decl_iterator I = DS->decl_begin(), E = DS->decl_end();
36143dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor           I != E; ++I)
36243dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor        BuildScopeInformation(*I, ParentScope);
3635af280ce21af061f96b5b5b752746871e364ba99Chris Lattner      continue;
3645af280ce21af061f96b5b5b752746871e364ba99Chris Lattner    }
3655af280ce21af061f96b5b5b752746871e364ba99Chris Lattner    // Disallow jumps into any part of an @try statement by pushing a scope and
3665af280ce21af061f96b5b5b752746871e364ba99Chris Lattner    // walking all sub-stmts in that scope.
3675af280ce21af061f96b5b5b752746871e364ba99Chris Lattner    if (ObjCAtTryStmt *AT = dyn_cast<ObjCAtTryStmt>(SubStmt)) {
3684e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian      unsigned newParentScope;
3695af280ce21af061f96b5b5b752746871e364ba99Chris Lattner      // Recursively walk the AST for the @try part.
370ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall      Scopes.push_back(GotoScope(ParentScope,
371ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall                                 diag::note_protected_by_objc_try,
372ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall                                 diag::note_exits_objc_try,
3735af280ce21af061f96b5b5b752746871e364ba99Chris Lattner                                 AT->getAtTryLoc()));
3745af280ce21af061f96b5b5b752746871e364ba99Chris Lattner      if (Stmt *TryPart = AT->getTryBody())
3754e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian        BuildScopeInformation(TryPart, (newParentScope = Scopes.size()-1));
3765af280ce21af061f96b5b5b752746871e364ba99Chris Lattner
3775af280ce21af061f96b5b5b752746871e364ba99Chris Lattner      // Jump from the catch to the finally or try is not valid.
3788f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor      for (unsigned I = 0, N = AT->getNumCatchStmts(); I != N; ++I) {
3798f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor        ObjCAtCatchStmt *AC = AT->getCatchStmt(I);
3805af280ce21af061f96b5b5b752746871e364ba99Chris Lattner        Scopes.push_back(GotoScope(ParentScope,
3815af280ce21af061f96b5b5b752746871e364ba99Chris Lattner                                   diag::note_protected_by_objc_catch,
382ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall                                   diag::note_exits_objc_catch,
3835af280ce21af061f96b5b5b752746871e364ba99Chris Lattner                                   AC->getAtCatchLoc()));
3841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        // @catches are nested and it isn't
3854e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian        BuildScopeInformation(AC->getCatchBody(),
3864e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian                              (newParentScope = Scopes.size()-1));
3875af280ce21af061f96b5b5b752746871e364ba99Chris Lattner      }
3881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3895af280ce21af061f96b5b5b752746871e364ba99Chris Lattner      // Jump from the finally to the try or catch is not valid.
3905af280ce21af061f96b5b5b752746871e364ba99Chris Lattner      if (ObjCAtFinallyStmt *AF = AT->getFinallyStmt()) {
3915af280ce21af061f96b5b5b752746871e364ba99Chris Lattner        Scopes.push_back(GotoScope(ParentScope,
3925af280ce21af061f96b5b5b752746871e364ba99Chris Lattner                                   diag::note_protected_by_objc_finally,
393ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall                                   diag::note_exits_objc_finally,
3945af280ce21af061f96b5b5b752746871e364ba99Chris Lattner                                   AF->getAtFinallyLoc()));
3954e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian        BuildScopeInformation(AF, (newParentScope = Scopes.size()-1));
3965af280ce21af061f96b5b5b752746871e364ba99Chris Lattner      }
3971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3985af280ce21af061f96b5b5b752746871e364ba99Chris Lattner      continue;
3995af280ce21af061f96b5b5b752746871e364ba99Chris Lattner    }
4004e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian
4014e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian    unsigned newParentScope;
40246c3c4ba78766ac0f1c5ec631b424773e21f5271Chris Lattner    // Disallow jumps into the protected statement of an @synchronized, but
40346c3c4ba78766ac0f1c5ec631b424773e21f5271Chris Lattner    // allow jumps into the object expression it protects.
40446c3c4ba78766ac0f1c5ec631b424773e21f5271Chris Lattner    if (ObjCAtSynchronizedStmt *AS = dyn_cast<ObjCAtSynchronizedStmt>(SubStmt)){
40546c3c4ba78766ac0f1c5ec631b424773e21f5271Chris Lattner      // Recursively walk the AST for the @synchronized object expr, it is
40646c3c4ba78766ac0f1c5ec631b424773e21f5271Chris Lattner      // evaluated in the normal scope.
40746c3c4ba78766ac0f1c5ec631b424773e21f5271Chris Lattner      BuildScopeInformation(AS->getSynchExpr(), ParentScope);
4081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
40946c3c4ba78766ac0f1c5ec631b424773e21f5271Chris Lattner      // Recursively walk the AST for the @synchronized part, protected by a new
41046c3c4ba78766ac0f1c5ec631b424773e21f5271Chris Lattner      // scope.
41146c3c4ba78766ac0f1c5ec631b424773e21f5271Chris Lattner      Scopes.push_back(GotoScope(ParentScope,
41246c3c4ba78766ac0f1c5ec631b424773e21f5271Chris Lattner                                 diag::note_protected_by_objc_synchronized,
413ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall                                 diag::note_exits_objc_synchronized,
41446c3c4ba78766ac0f1c5ec631b424773e21f5271Chris Lattner                                 AS->getAtSynchronizedLoc()));
4154e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian      BuildScopeInformation(AS->getSynchBody(),
4164e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian                            (newParentScope = Scopes.size()-1));
41746c3c4ba78766ac0f1c5ec631b424773e21f5271Chris Lattner      continue;
41846c3c4ba78766ac0f1c5ec631b424773e21f5271Chris Lattner    }
419972041f45bdf8df7ea447221292d7827466ba94bSebastian Redl
420972041f45bdf8df7ea447221292d7827466ba94bSebastian Redl    // Disallow jumps into any part of a C++ try statement. This is pretty
421972041f45bdf8df7ea447221292d7827466ba94bSebastian Redl    // much the same as for Obj-C.
422972041f45bdf8df7ea447221292d7827466ba94bSebastian Redl    if (CXXTryStmt *TS = dyn_cast<CXXTryStmt>(SubStmt)) {
423ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall      Scopes.push_back(GotoScope(ParentScope,
424ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall                                 diag::note_protected_by_cxx_try,
425ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall                                 diag::note_exits_cxx_try,
426972041f45bdf8df7ea447221292d7827466ba94bSebastian Redl                                 TS->getSourceRange().getBegin()));
427972041f45bdf8df7ea447221292d7827466ba94bSebastian Redl      if (Stmt *TryBlock = TS->getTryBlock())
4284e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian        BuildScopeInformation(TryBlock, (newParentScope = Scopes.size()-1));
429972041f45bdf8df7ea447221292d7827466ba94bSebastian Redl
430972041f45bdf8df7ea447221292d7827466ba94bSebastian Redl      // Jump from the catch into the try is not allowed either.
4311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      for (unsigned I = 0, E = TS->getNumHandlers(); I != E; ++I) {
432972041f45bdf8df7ea447221292d7827466ba94bSebastian Redl        CXXCatchStmt *CS = TS->getHandler(I);
433972041f45bdf8df7ea447221292d7827466ba94bSebastian Redl        Scopes.push_back(GotoScope(ParentScope,
434972041f45bdf8df7ea447221292d7827466ba94bSebastian Redl                                   diag::note_protected_by_cxx_catch,
435ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall                                   diag::note_exits_cxx_catch,
436972041f45bdf8df7ea447221292d7827466ba94bSebastian Redl                                   CS->getSourceRange().getBegin()));
4374e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian        BuildScopeInformation(CS->getHandlerBlock(),
4384e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian                              (newParentScope = Scopes.size()-1));
439972041f45bdf8df7ea447221292d7827466ba94bSebastian Redl      }
440972041f45bdf8df7ea447221292d7827466ba94bSebastian Redl
441972041f45bdf8df7ea447221292d7827466ba94bSebastian Redl      continue;
442972041f45bdf8df7ea447221292d7827466ba94bSebastian Redl    }
443972041f45bdf8df7ea447221292d7827466ba94bSebastian Redl
444f85e193739c953358c865005855253af4f68a497John McCall    // Disallow jumps into the protected statement of an @autoreleasepool.
445f85e193739c953358c865005855253af4f68a497John McCall    if (ObjCAutoreleasePoolStmt *AS = dyn_cast<ObjCAutoreleasePoolStmt>(SubStmt)){
446f85e193739c953358c865005855253af4f68a497John McCall      // Recursively walk the AST for the @autoreleasepool part, protected by a new
447f85e193739c953358c865005855253af4f68a497John McCall      // scope.
448f85e193739c953358c865005855253af4f68a497John McCall      Scopes.push_back(GotoScope(ParentScope,
449f85e193739c953358c865005855253af4f68a497John McCall                                 diag::note_protected_by_objc_autoreleasepool,
450f85e193739c953358c865005855253af4f68a497John McCall                                 diag::note_exits_objc_autoreleasepool,
451f85e193739c953358c865005855253af4f68a497John McCall                                 AS->getAtLoc()));
4524e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian      BuildScopeInformation(AS->getSubStmt(), (newParentScope = Scopes.size()-1));
453f85e193739c953358c865005855253af4f68a497John McCall      continue;
454f85e193739c953358c865005855253af4f68a497John McCall    }
4554e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian
4564e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian    if (const BlockExpr *BE = dyn_cast<BlockExpr>(SubStmt)) {
4574e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian        const BlockDecl *BDecl = BE->getBlockDecl();
4584e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian        for (BlockDecl::capture_const_iterator ci = BDecl->capture_begin(),
4594e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian             ce = BDecl->capture_end(); ci != ce; ++ci) {
4604e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian          VarDecl *variable = ci->getVariable();
4614e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian          BuildScopeInformation(variable, BDecl, ParentScope);
4624e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian        }
4634e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian    }
4644e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian
4655af280ce21af061f96b5b5b752746871e364ba99Chris Lattner    // Recursively walk the AST.
4665af280ce21af061f96b5b5b752746871e364ba99Chris Lattner    BuildScopeInformation(SubStmt, ParentScope);
4675af280ce21af061f96b5b5b752746871e364ba99Chris Lattner  }
4685af280ce21af061f96b5b5b752746871e364ba99Chris Lattner}
4695af280ce21af061f96b5b5b752746871e364ba99Chris Lattner
4705af280ce21af061f96b5b5b752746871e364ba99Chris Lattner/// VerifyJumps - Verify each element of the Jumps array to see if they are
4715af280ce21af061f96b5b5b752746871e364ba99Chris Lattner/// valid, emitting diagnostics if not.
4725af280ce21af061f96b5b5b752746871e364ba99Chris Lattnervoid JumpScopeChecker::VerifyJumps() {
4735af280ce21af061f96b5b5b752746871e364ba99Chris Lattner  while (!Jumps.empty()) {
4745af280ce21af061f96b5b5b752746871e364ba99Chris Lattner    Stmt *Jump = Jumps.pop_back_val();
4751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // With a goto,
4775af280ce21af061f96b5b5b752746871e364ba99Chris Lattner    if (GotoStmt *GS = dyn_cast<GotoStmt>(Jump)) {
478ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner      CheckJump(GS, GS->getLabel()->getStmt(), GS->getGotoLoc(),
479c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet                diag::err_goto_into_protected_scope,
480c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet                diag::warn_goto_into_protected_scope);
4815af280ce21af061f96b5b5b752746871e364ba99Chris Lattner      continue;
4825af280ce21af061f96b5b5b752746871e364ba99Chris Lattner    }
4831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
48495c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall    // We only get indirect gotos here when they have a constant target.
48595c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall    if (IndirectGotoStmt *IGS = dyn_cast<IndirectGotoStmt>(Jump)) {
486ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner      LabelDecl *Target = IGS->getConstantTarget();
487ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner      CheckJump(IGS, Target->getStmt(), IGS->getGotoLoc(),
488c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet                diag::err_goto_into_protected_scope, 0);
48995c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall      continue;
49095c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall    }
49195c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall
492ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall    SwitchStmt *SS = cast<SwitchStmt>(Jump);
493ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall    for (SwitchCase *SC = SS->getSwitchCaseList(); SC;
494ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall         SC = SC->getNextSwitchCase()) {
495ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall      assert(LabelAndGotoScopes.count(SC) && "Case not visited?");
496ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall      CheckJump(SS, SC, SC->getLocStart(),
497c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet                diag::err_switch_into_protected_scope, 0);
4985af280ce21af061f96b5b5b752746871e364ba99Chris Lattner    }
499ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall  }
500ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall}
501ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall
5025e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall/// VerifyIndirectJumps - Verify whether any possible indirect jump
5035e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall/// might cross a protection boundary.  Unlike direct jumps, indirect
5045e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall/// jumps count cleanups as protection boundaries:  since there's no
5055e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall/// way to know where the jump is going, we can't implicitly run the
5065e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall/// right cleanups the way we can with direct jumps.
5075e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall///
5085e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall/// Thus, an indirect jump is "trivial" if it bypasses no
5095e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall/// initializations and no teardowns.  More formally, an indirect jump
5105e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall/// from A to B is trivial if the path out from A to DCA(A,B) is
5115e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall/// trivial and the path in from DCA(A,B) to B is trivial, where
5125e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall/// DCA(A,B) is the deepest common ancestor of A and B.
5135e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall/// Jump-triviality is transitive but asymmetric.
514ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall///
515ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall/// A path in is trivial if none of the entered scopes have an InDiag.
516ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall/// A path out is trivial is none of the exited scopes have an OutDiag.
5175e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall///
5185e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall/// Under these definitions, this function checks that the indirect
5195e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall/// jump between A and B is trivial for every indirect goto statement A
5205e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall/// and every label B whose address was taken in the function.
521ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCallvoid JumpScopeChecker::VerifyIndirectJumps() {
522ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall  if (IndirectJumps.empty()) return;
523ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall
524ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall  // If there aren't any address-of-label expressions in this function,
525ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall  // complain about the first indirect goto.
526ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall  if (IndirectJumpTargets.empty()) {
527ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall    S.Diag(IndirectJumps[0]->getGotoLoc(),
528ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall           diag::err_indirect_goto_without_addrlabel);
529ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall    return;
530ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall  }
531ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall
5325e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall  // Collect a single representative of every scope containing an
5335e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall  // indirect goto.  For most code bases, this substantially cuts
5345e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall  // down on the number of jump sites we'll have to consider later.
535ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall  typedef std::pair<unsigned, IndirectGotoStmt*> JumpScope;
5365f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<JumpScope, 32> JumpScopes;
537ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall  {
538ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall    llvm::DenseMap<unsigned, IndirectGotoStmt*> JumpScopesMap;
5395f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    for (SmallVectorImpl<IndirectGotoStmt*>::iterator
540ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall           I = IndirectJumps.begin(), E = IndirectJumps.end(); I != E; ++I) {
541ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall      IndirectGotoStmt *IG = *I;
542ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall      assert(LabelAndGotoScopes.count(IG) &&
543ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall             "indirect jump didn't get added to scopes?");
544ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall      unsigned IGScope = LabelAndGotoScopes[IG];
545ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall      IndirectGotoStmt *&Entry = JumpScopesMap[IGScope];
546ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall      if (!Entry) Entry = IG;
547ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall    }
548ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall    JumpScopes.reserve(JumpScopesMap.size());
549ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall    for (llvm::DenseMap<unsigned, IndirectGotoStmt*>::iterator
550ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall           I = JumpScopesMap.begin(), E = JumpScopesMap.end(); I != E; ++I)
551ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall      JumpScopes.push_back(*I);
552ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall  }
553ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall
5545e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall  // Collect a single representative of every scope containing a
5555e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall  // label whose address was taken somewhere in the function.
5565e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall  // For most code bases, there will be only one such scope.
557ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner  llvm::DenseMap<unsigned, LabelDecl*> TargetScopes;
5585f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  for (SmallVectorImpl<LabelDecl*>::iterator
559ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall         I = IndirectJumpTargets.begin(), E = IndirectJumpTargets.end();
560ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall       I != E; ++I) {
561ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner    LabelDecl *TheLabel = *I;
562ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner    assert(LabelAndGotoScopes.count(TheLabel->getStmt()) &&
563ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall           "Referenced label didn't get added to scopes?");
564ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner    unsigned LabelScope = LabelAndGotoScopes[TheLabel->getStmt()];
565ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner    LabelDecl *&Target = TargetScopes[LabelScope];
566ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall    if (!Target) Target = TheLabel;
567ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall  }
568ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall
5695e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall  // For each target scope, make sure it's trivially reachable from
5705e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall  // every scope containing a jump site.
5715e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall  //
5725e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall  // A path between scopes always consists of exitting zero or more
5735e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall  // scopes, then entering zero or more scopes.  We build a set of
5745e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall  // of scopes S from which the target scope can be trivially
5755e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall  // entered, then verify that every jump scope can be trivially
5765e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall  // exitted to reach a scope in S.
577ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall  llvm::BitVector Reachable(Scopes.size(), false);
578ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner  for (llvm::DenseMap<unsigned,LabelDecl*>::iterator
579ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall         TI = TargetScopes.begin(), TE = TargetScopes.end(); TI != TE; ++TI) {
580ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall    unsigned TargetScope = TI->first;
581ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner    LabelDecl *TargetLabel = TI->second;
582ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall
583ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall    Reachable.reset();
584ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall
585ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall    // Mark all the enclosing scopes from which you can safely jump
5865e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall    // into the target scope.  'Min' will end up being the index of
5875e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall    // the shallowest such scope.
588ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall    unsigned Min = TargetScope;
589ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall    while (true) {
590ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall      Reachable.set(Min);
591ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall
592ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall      // Don't go beyond the outermost scope.
593ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall      if (Min == 0) break;
594ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall
5955e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall      // Stop if we can't trivially enter the current scope.
596ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall      if (Scopes[Min].InDiag) break;
5975af280ce21af061f96b5b5b752746871e364ba99Chris Lattner
598ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall      Min = Scopes[Min].ParentScope;
5995af280ce21af061f96b5b5b752746871e364ba99Chris Lattner    }
6005af280ce21af061f96b5b5b752746871e364ba99Chris Lattner
601ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall    // Walk through all the jump sites, checking that they can trivially
602ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall    // reach this label scope.
6035f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    for (SmallVectorImpl<JumpScope>::iterator
604ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall           I = JumpScopes.begin(), E = JumpScopes.end(); I != E; ++I) {
605ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall      unsigned Scope = I->first;
606ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall
607ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall      // Walk out the "scope chain" for this scope, looking for a scope
6085e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall      // we've marked reachable.  For well-formed code this amortizes
6095e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall      // to O(JumpScopes.size() / Scopes.size()):  we only iterate
6105e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall      // when we see something unmarked, and in well-formed code we
6115e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall      // mark everything we iterate past.
612ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall      bool IsReachable = false;
613ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall      while (true) {
614ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall        if (Reachable.test(Scope)) {
615ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall          // If we find something reachable, mark all the scopes we just
616ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall          // walked through as reachable.
617ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall          for (unsigned S = I->first; S != Scope; S = Scopes[S].ParentScope)
618ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall            Reachable.set(S);
619ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall          IsReachable = true;
620ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall          break;
621ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall        }
622ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall
623ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall        // Don't walk out if we've reached the top-level scope or we've
624ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall        // gotten shallower than the shallowest reachable scope.
625ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall        if (Scope == 0 || Scope < Min) break;
626ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall
627ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall        // Don't walk out through an out-diagnostic.
628ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall        if (Scopes[Scope].OutDiag) break;
629ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall
630ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall        Scope = Scopes[Scope].ParentScope;
631ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall      }
632ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall
633ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall      // Only diagnose if we didn't find something.
634ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall      if (IsReachable) continue;
635ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall
636ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall      DiagnoseIndirectJump(I->second, I->first, TargetLabel, TargetScope);
6375af280ce21af061f96b5b5b752746871e364ba99Chris Lattner    }
6385af280ce21af061f96b5b5b752746871e364ba99Chris Lattner  }
6395af280ce21af061f96b5b5b752746871e364ba99Chris Lattner}
6405af280ce21af061f96b5b5b752746871e364ba99Chris Lattner
6415e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall/// Diagnose an indirect jump which is known to cross scopes.
642ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCallvoid JumpScopeChecker::DiagnoseIndirectJump(IndirectGotoStmt *Jump,
643ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall                                            unsigned JumpScope,
644ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner                                            LabelDecl *Target,
645ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall                                            unsigned TargetScope) {
646ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall  assert(JumpScope != TargetScope);
647ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall
64895c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall  S.Diag(Jump->getGotoLoc(), diag::err_indirect_goto_in_protected_scope);
649ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner  S.Diag(Target->getStmt()->getIdentLoc(), diag::note_indirect_goto_target);
650ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall
6515e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall  unsigned Common = GetDeepestCommonScope(JumpScope, TargetScope);
6525e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall
6535e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall  // Walk out the scope chain until we reach the common ancestor.
6545e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall  for (unsigned I = JumpScope; I != Common; I = Scopes[I].ParentScope)
6555e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall    if (Scopes[I].OutDiag)
6565e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall      S.Diag(Scopes[I].Loc, Scopes[I].OutDiag);
657ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall
658ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall  // Now walk into the scopes containing the label whose address was taken.
6595e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall  for (unsigned I = TargetScope; I != Common; I = Scopes[I].ParentScope)
6605e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall    if (Scopes[I].InDiag)
6615e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall      S.Diag(Scopes[I].Loc, Scopes[I].InDiag);
662ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall}
663ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall
664c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet/// Return true if a particular error+note combination must be downgraded
665c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet/// to a warning in Microsoft mode.
666c985b88efc9188845952ab524fe6d4717705257bFrancois Pichetstatic bool IsMicrosoftJumpWarning(unsigned JumpDiag, unsigned InDiagNote)
667c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet{
668c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet    return (JumpDiag == diag::err_goto_into_protected_scope &&
669c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet           (InDiagNote == diag::note_protected_by_variable_init ||
670c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet            InDiagNote == diag::note_protected_by_variable_nontriv_destructor));
671c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet}
672c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet
673c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet
6745af280ce21af061f96b5b5b752746871e364ba99Chris Lattner/// CheckJump - Validate that the specified jump statement is valid: that it is
6755af280ce21af061f96b5b5b752746871e364ba99Chris Lattner/// jumping within or out of its current scope, not into a deeper one.
676c985b88efc9188845952ab524fe6d4717705257bFrancois Pichetvoid JumpScopeChecker::CheckJump(Stmt *From, Stmt *To, SourceLocation DiagLoc,
677c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet                             unsigned JumpDiagError, unsigned JumpDiagWarning) {
6785af280ce21af061f96b5b5b752746871e364ba99Chris Lattner  assert(LabelAndGotoScopes.count(From) && "Jump didn't get added to scopes?");
6795af280ce21af061f96b5b5b752746871e364ba99Chris Lattner  unsigned FromScope = LabelAndGotoScopes[From];
6805af280ce21af061f96b5b5b752746871e364ba99Chris Lattner
6815af280ce21af061f96b5b5b752746871e364ba99Chris Lattner  assert(LabelAndGotoScopes.count(To) && "Jump didn't get added to scopes?");
6825af280ce21af061f96b5b5b752746871e364ba99Chris Lattner  unsigned ToScope = LabelAndGotoScopes[To];
6831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6845af280ce21af061f96b5b5b752746871e364ba99Chris Lattner  // Common case: exactly the same scope, which is fine.
6855af280ce21af061f96b5b5b752746871e364ba99Chris Lattner  if (FromScope == ToScope) return;
6861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6875e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall  unsigned CommonScope = GetDeepestCommonScope(FromScope, ToScope);
6881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6895e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall  // It's okay to jump out from a nested scope.
6905e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall  if (CommonScope == ToScope) return;
6911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6925e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall  // Pull out (and reverse) any scopes we might need to diagnose skipping.
693c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet  SmallVector<unsigned, 10> ToScopesError;
694c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet  SmallVector<unsigned, 10> ToScopesWarning;
695c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet  for (unsigned I = ToScope; I != CommonScope; I = Scopes[I].ParentScope) {
696c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet    if (S.getLangOptions().Microsoft &&
697c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet        IsMicrosoftJumpWarning(JumpDiagError, Scopes[I].InDiag))
698c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet      ToScopesWarning.push_back(I);
699c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet    else if (Scopes[I].InDiag)
700c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet      ToScopesError.push_back(I);
701c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet  }
702ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall
703c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet  // Handle warnings.
704c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet  if (!ToScopesWarning.empty()) {
705c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet    S.Diag(DiagLoc, JumpDiagWarning);
706c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet    for (unsigned i = 0, e = ToScopesWarning.size(); i != e; ++i)
707c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet      S.Diag(Scopes[ToScopesWarning[i]].Loc, Scopes[ToScopesWarning[i]].InDiag);
708c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet  }
709ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall
710c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet  // Handle errors.
711c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet  if (!ToScopesError.empty()) {
712c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet    S.Diag(DiagLoc, JumpDiagError);
713c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet    // Emit diagnostics note for whatever is left in ToScopesError.
714c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet    for (unsigned i = 0, e = ToScopesError.size(); i != e; ++i)
715c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet      S.Diag(Scopes[ToScopesError[i]].Loc, Scopes[ToScopesError[i]].InDiag);
716c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet  }
7175af280ce21af061f96b5b5b752746871e364ba99Chris Lattner}
7185af280ce21af061f96b5b5b752746871e364ba99Chris Lattner
7195af280ce21af061f96b5b5b752746871e364ba99Chris Lattnervoid Sema::DiagnoseInvalidJumps(Stmt *Body) {
7206490ae5003226cae28f980648948bea8b21a8638Douglas Gregor  (void)JumpScopeChecker(Body, *this);
7215af280ce21af061f96b5b5b752746871e364ba99Chris Lattner}
722