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"
19972041f45bdf8df7ea447221292d7827466ba94bSebastian Redl#include "clang/AST/StmtCXX.h"
2055fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/AST/StmtObjC.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
356bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  /// Permissive - True when recovering from errors, in which case precautions
366bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  /// are taken to handle incomplete scope information.
376bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  const bool Permissive;
386bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
395af280ce21af061f96b5b5b752746871e364ba99Chris Lattner  /// GotoScope - This is a record that we use to keep track of all of the
405af280ce21af061f96b5b5b752746871e364ba99Chris Lattner  /// scopes that are introduced by VLAs and other things that scope jumps like
415af280ce21af061f96b5b5b752746871e364ba99Chris Lattner  /// gotos.  This scope tree has nothing to do with the source scope tree,
425af280ce21af061f96b5b5b752746871e364ba99Chris Lattner  /// because you can have multiple VLA scopes per compound statement, and most
435af280ce21af061f96b5b5b752746871e364ba99Chris Lattner  /// compound statements don't introduce any scopes.
445af280ce21af061f96b5b5b752746871e364ba99Chris Lattner  struct GotoScope {
455af280ce21af061f96b5b5b752746871e364ba99Chris Lattner    /// ParentScope - The index in ScopeMap of the parent scope.  This is 0 for
465af280ce21af061f96b5b5b752746871e364ba99Chris Lattner    /// the parent scope is the function body.
475af280ce21af061f96b5b5b752746871e364ba99Chris Lattner    unsigned ParentScope;
481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
490e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith    /// InDiag - The note to emit if there is a jump into this scope.
50ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall    unsigned InDiag;
51ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall
520e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith    /// OutDiag - The note to emit if there is an indirect jump out
53ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall    /// of this scope.  Direct jumps always clean up their current scope
54ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall    /// in an orderly way.
55ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall    unsigned OutDiag;
561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
575af280ce21af061f96b5b5b752746871e364ba99Chris Lattner    /// Loc - Location to emit the diagnostic.
585af280ce21af061f96b5b5b752746871e364ba99Chris Lattner    SourceLocation Loc;
591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
60ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall    GotoScope(unsigned parentScope, unsigned InDiag, unsigned OutDiag,
61ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall              SourceLocation L)
62ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall      : ParentScope(parentScope), InDiag(InDiag), OutDiag(OutDiag), Loc(L) {}
635af280ce21af061f96b5b5b752746871e364ba99Chris Lattner  };
641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
655f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<GotoScope, 48> Scopes;
665af280ce21af061f96b5b5b752746871e364ba99Chris Lattner  llvm::DenseMap<Stmt*, unsigned> LabelAndGotoScopes;
675f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Stmt*, 16> Jumps;
68ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall
695f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<IndirectGotoStmt*, 4> IndirectJumps;
705f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<LabelDecl*, 4> IndirectJumpTargets;
715af280ce21af061f96b5b5b752746871e364ba99Chris Lattnerpublic:
725af280ce21af061f96b5b5b752746871e364ba99Chris Lattner  JumpScopeChecker(Stmt *Body, Sema &S);
735af280ce21af061f96b5b5b752746871e364ba99Chris Lattnerprivate:
7443dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor  void BuildScopeInformation(Decl *D, unsigned &ParentScope);
75b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar  void BuildScopeInformation(VarDecl *D, const BlockDecl *BDecl,
764e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian                             unsigned &ParentScope);
774e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian  void BuildScopeInformation(Stmt *S, unsigned &origParentScope);
78b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar
795af280ce21af061f96b5b5b752746871e364ba99Chris Lattner  void VerifyJumps();
80ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall  void VerifyIndirectJumps();
814fe5be028b723fedc28bb33be96cde1ab2574ee6Bill Wendling  void NoteJumpIntoScopes(ArrayRef<unsigned> ToScopes);
82ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall  void DiagnoseIndirectJump(IndirectGotoStmt *IG, unsigned IGScope,
83ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner                            LabelDecl *Target, unsigned TargetScope);
84c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet  void CheckJump(Stmt *From, Stmt *To, SourceLocation DiagLoc,
850e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith                 unsigned JumpDiag, unsigned JumpDiagWarning,
860e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith                 unsigned JumpDiagCXX98Compat);
87176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  void CheckGotoStmt(GotoStmt *GS);
885e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall
895e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall  unsigned GetDeepestCommonScope(unsigned A, unsigned B);
905af280ce21af061f96b5b5b752746871e364ba99Chris Lattner};
915af280ce21af061f96b5b5b752746871e364ba99Chris Lattner} // end anonymous namespace
925af280ce21af061f96b5b5b752746871e364ba99Chris Lattner
936bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines#define CHECK_PERMISSIVE(x) (assert(Permissive || !(x)), (Permissive && (x)))
945af280ce21af061f96b5b5b752746871e364ba99Chris Lattner
956bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen HinesJumpScopeChecker::JumpScopeChecker(Stmt *Body, Sema &s)
966bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    : S(s), Permissive(s.hasAnyUnrecoverableErrorsInThisFunction()) {
975af280ce21af061f96b5b5b752746871e364ba99Chris Lattner  // Add a scope entry for function scope.
98ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall  Scopes.push_back(GotoScope(~0U, ~0U, ~0U, SourceLocation()));
991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1005af280ce21af061f96b5b5b752746871e364ba99Chris Lattner  // Build information for the top level compound statement, so that we have a
1015af280ce21af061f96b5b5b752746871e364ba99Chris Lattner  // defined scope record for every "goto" and label.
1024e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian  unsigned BodyParentScope = 0;
1034e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian  BuildScopeInformation(Body, BodyParentScope);
1041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1055af280ce21af061f96b5b5b752746871e364ba99Chris Lattner  // Check that all jumps we saw are kosher.
1065af280ce21af061f96b5b5b752746871e364ba99Chris Lattner  VerifyJumps();
107ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall  VerifyIndirectJumps();
1085af280ce21af061f96b5b5b752746871e364ba99Chris Lattner}
1091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1105e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall/// GetDeepestCommonScope - Finds the innermost scope enclosing the
1115e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall/// two scopes.
1125e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCallunsigned JumpScopeChecker::GetDeepestCommonScope(unsigned A, unsigned B) {
1135e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall  while (A != B) {
1145e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall    // Inner scopes are created after outer scopes and therefore have
1155e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall    // higher indices.
1165e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall    if (A < B) {
1175e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall      assert(Scopes[B].ParentScope < B);
1185e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall      B = Scopes[B].ParentScope;
1195e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall    } else {
1205e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall      assert(Scopes[A].ParentScope < A);
1215e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall      A = Scopes[A].ParentScope;
1225e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall    }
1235e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall  }
1245e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall  return A;
1255e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall}
1265e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall
127f85e193739c953358c865005855253af4f68a497John McCalltypedef std::pair<unsigned,unsigned> ScopePair;
128f85e193739c953358c865005855253af4f68a497John McCall
1295af280ce21af061f96b5b5b752746871e364ba99Chris Lattner/// GetDiagForGotoScopeDecl - If this decl induces a new goto scope, return a
1305af280ce21af061f96b5b5b752746871e364ba99Chris Lattner/// diagnostic that should be emitted if control goes over it. If not, return 0.
131651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesstatic ScopePair GetDiagForGotoScopeDecl(Sema &S, const Decl *D) {
1325af280ce21af061f96b5b5b752746871e364ba99Chris Lattner  if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
133644e90a17f1bc3dcc2b7b018eb02abb3a1c2022bRafael Espindola    unsigned InDiag = 0;
134651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    unsigned OutDiag = 0;
135651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
1365af280ce21af061f96b5b5b752746871e364ba99Chris Lattner    if (VD->getType()->isVariablyModifiedType())
137ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall      InDiag = diag::note_protected_by_vla;
138ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall
139f85e193739c953358c865005855253af4f68a497John McCall    if (VD->hasAttr<BlocksAttr>())
140f85e193739c953358c865005855253af4f68a497John McCall      return ScopePair(diag::note_protected_by___block,
141f85e193739c953358c865005855253af4f68a497John McCall                       diag::note_exits___block);
142f85e193739c953358c865005855253af4f68a497John McCall
143f85e193739c953358c865005855253af4f68a497John McCall    if (VD->hasAttr<CleanupAttr>())
144f85e193739c953358c865005855253af4f68a497John McCall      return ScopePair(diag::note_protected_by_cleanup,
145f85e193739c953358c865005855253af4f68a497John McCall                       diag::note_exits_cleanup);
146f85e193739c953358c865005855253af4f68a497John McCall
147651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    if (VD->hasLocalStorage()) {
148651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      switch (VD->getType().isDestructedType()) {
149651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      case QualType::DK_objc_strong_lifetime:
15087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        return ScopePair(diag::note_protected_by_objc_strong_init,
15187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                         diag::note_exits_objc_strong);
15287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
153651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      case QualType::DK_objc_weak_lifetime:
15487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        return ScopePair(diag::note_protected_by_objc_weak_init,
15587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                         diag::note_exits_objc_weak);
156651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
157651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      case QualType::DK_cxx_destructor:
158651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        OutDiag = diag::note_exits_dtor;
159651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        break;
160651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
161651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      case QualType::DK_none:
162651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        break;
163f85e193739c953358c865005855253af4f68a497John McCall      }
164f85e193739c953358c865005855253af4f68a497John McCall    }
165f85e193739c953358c865005855253af4f68a497John McCall
166651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    const Expr *Init = VD->getInit();
167651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    if (S.Context.getLangOpts().CPlusPlus && VD->hasLocalStorage() && Init) {
1680e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith      // C++11 [stmt.dcl]p3:
169f85e193739c953358c865005855253af4f68a497John McCall      //   A program that jumps from a point where a variable with automatic
170f85e193739c953358c865005855253af4f68a497John McCall      //   storage duration is not in scope to a point where it is in scope
171f85e193739c953358c865005855253af4f68a497John McCall      //   is ill-formed unless the variable has scalar type, class type with
172b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar      //   a trivial default constructor and a trivial destructor, a
173f85e193739c953358c865005855253af4f68a497John McCall      //   cv-qualified version of one of these types, or an array of one of
174f85e193739c953358c865005855253af4f68a497John McCall      //   the preceding types and is declared without an initializer.
175f85e193739c953358c865005855253af4f68a497John McCall
176f85e193739c953358c865005855253af4f68a497John McCall      // C++03 [stmt.dcl.p3:
177f85e193739c953358c865005855253af4f68a497John McCall      //   A program that jumps from a point where a local variable
178f85e193739c953358c865005855253af4f68a497John McCall      //   with automatic storage duration is not in scope to a point
179f85e193739c953358c865005855253af4f68a497John McCall      //   where it is in scope is ill-formed unless the variable has
180f85e193739c953358c865005855253af4f68a497John McCall      //   POD type and is declared without an initializer.
181f85e193739c953358c865005855253af4f68a497John McCall
182651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      InDiag = diag::note_protected_by_variable_init;
183644e90a17f1bc3dcc2b7b018eb02abb3a1c2022bRafael Espindola
184651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      // For a variable of (array of) class type declared without an
185651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      // initializer, we will have call-style initialization and the initializer
186651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      // will be the CXXConstructExpr with no intervening nodes.
187651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      if (const CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(Init)) {
188651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        const CXXConstructorDecl *Ctor = CCE->getConstructor();
189651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        if (Ctor->isTrivial() && Ctor->isDefaultConstructor() &&
190651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines            VD->getInitStyle() == VarDecl::CallInit) {
191644e90a17f1bc3dcc2b7b018eb02abb3a1c2022bRafael Espindola          if (OutDiag)
192644e90a17f1bc3dcc2b7b018eb02abb3a1c2022bRafael Espindola            InDiag = diag::note_protected_by_variable_nontriv_destructor;
193651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines          else if (!Ctor->getParent()->isPOD())
194644e90a17f1bc3dcc2b7b018eb02abb3a1c2022bRafael Espindola            InDiag = diag::note_protected_by_variable_non_pod;
195651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines          else
196651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines            InDiag = 0;
197e413516d0ed61ef9e2ff706bcc00480adca947c4Douglas Gregor        }
198a92afb611c015196541edd53347aa23dba1949acRafael Espindola      }
199a92afb611c015196541edd53347aa23dba1949acRafael Espindola    }
200644e90a17f1bc3dcc2b7b018eb02abb3a1c2022bRafael Espindola
201651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    return ScopePair(InDiag, OutDiag);
202ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall  }
203ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall
204651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (const TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
2055af280ce21af061f96b5b5b752746871e364ba99Chris Lattner    if (TD->getUnderlyingType()->isVariablyModifiedType())
206651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      return ScopePair(isa<TypedefDecl>(TD)
207651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                           ? diag::note_protected_by_vla_typedef
208651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                           : diag::note_protected_by_vla_type_alias,
209651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                       0);
210162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  }
211162e1c1b487352434552147967c3dd296ebee2f7Richard Smith
212f85e193739c953358c865005855253af4f68a497John McCall  return ScopePair(0U, 0U);
2135af280ce21af061f96b5b5b752746871e364ba99Chris Lattner}
2145af280ce21af061f96b5b5b752746871e364ba99Chris Lattner
21543dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor/// \brief Build scope information for a declaration that is part of a DeclStmt.
21643dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregorvoid JumpScopeChecker::BuildScopeInformation(Decl *D, unsigned &ParentScope) {
21743dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor  // If this decl causes a new scope, push and switch to it.
218651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  std::pair<unsigned,unsigned> Diags = GetDiagForGotoScopeDecl(S, D);
21943dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor  if (Diags.first || Diags.second) {
22043dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor    Scopes.push_back(GotoScope(ParentScope, Diags.first, Diags.second,
22143dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor                               D->getLocation()));
22243dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor    ParentScope = Scopes.size()-1;
22343dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor  }
224b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar
22543dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor  // If the decl has an initializer, walk it with the potentially new
22643dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor  // scope we just installed.
22743dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor  if (VarDecl *VD = dyn_cast<VarDecl>(D))
22843dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor    if (Expr *Init = VD->getInit())
22943dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor      BuildScopeInformation(Init, ParentScope);
23043dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor}
2315af280ce21af061f96b5b5b752746871e364ba99Chris Lattner
2324e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian/// \brief Build scope information for a captured block literal variables.
233b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainarvoid JumpScopeChecker::BuildScopeInformation(VarDecl *D,
234b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar                                             const BlockDecl *BDecl,
2354e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian                                             unsigned &ParentScope) {
2364e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian  // exclude captured __block variables; there's no destructor
2374e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian  // associated with the block literal for them.
2384e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian  if (D->hasAttr<BlocksAttr>())
2394e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian    return;
2404e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian  QualType T = D->getType();
2414e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian  QualType::DestructionKind destructKind = T.isDestructedType();
2424e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian  if (destructKind != QualType::DK_none) {
2434e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian    std::pair<unsigned,unsigned> Diags;
2444e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian    switch (destructKind) {
2454e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian      case QualType::DK_cxx_destructor:
2464e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian        Diags = ScopePair(diag::note_enters_block_captures_cxx_obj,
2474e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian                          diag::note_exits_block_captures_cxx_obj);
2484e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian        break;
2494e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian      case QualType::DK_objc_strong_lifetime:
2504e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian        Diags = ScopePair(diag::note_enters_block_captures_strong,
2514e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian                          diag::note_exits_block_captures_strong);
2524e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian        break;
2534e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian      case QualType::DK_objc_weak_lifetime:
2544e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian        Diags = ScopePair(diag::note_enters_block_captures_weak,
2554e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian                          diag::note_exits_block_captures_weak);
2564e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian        break;
2574e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian      case QualType::DK_none:
2580e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith        llvm_unreachable("non-lifetime captured variable");
2594e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian    }
2604e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian    SourceLocation Loc = D->getLocation();
2614e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian    if (Loc.isInvalid())
2624e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian      Loc = BDecl->getLocation();
263b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar    Scopes.push_back(GotoScope(ParentScope,
2644e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian                               Diags.first, Diags.second, Loc));
2654e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian    ParentScope = Scopes.size()-1;
2664e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian  }
2674e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian}
2684e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian
2695af280ce21af061f96b5b5b752746871e364ba99Chris Lattner/// BuildScopeInformation - The statements from CI to CE are known to form a
2705af280ce21af061f96b5b5b752746871e364ba99Chris Lattner/// coherent VLA scope with a specified parent node.  Walk through the
2715af280ce21af061f96b5b5b752746871e364ba99Chris Lattner/// statements, adding any labels or gotos to LabelAndGotoScopes and recursively
2725af280ce21af061f96b5b5b752746871e364ba99Chris Lattner/// walking the AST as needed.
2734967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainarvoid JumpScopeChecker::BuildScopeInformation(Stmt *S,
2744967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar                                             unsigned &origParentScope) {
2754e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian  // If this is a statement, rather than an expression, scopes within it don't
2764e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian  // propagate out into the enclosing scope.  Otherwise we have to worry
2774e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian  // about block literals, which have the lifetime of their enclosing statement.
2784e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian  unsigned independentParentScope = origParentScope;
279b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar  unsigned &ParentScope = ((isa<Expr>(S) && !isa<StmtExpr>(S))
2804e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian                            ? origParentScope : independentParentScope);
2814e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian
2824967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  unsigned StmtsToSkip = 0u;
283b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar
2845af280ce21af061f96b5b5b752746871e364ba99Chris Lattner  // If we found a label, remember that it is in ParentScope scope.
285ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall  switch (S->getStmtClass()) {
286ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall  case Stmt::AddrLabelExprClass:
287ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall    IndirectJumpTargets.push_back(cast<AddrLabelExpr>(S)->getLabel());
288ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall    break;
289ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall
290ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall  case Stmt::IndirectGotoStmtClass:
29195c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall    // "goto *&&lbl;" is a special case which we treat as equivalent
29295c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall    // to a normal goto.  In addition, we don't calculate scope in the
29395c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall    // operand (to avoid recording the address-of-label use), which
29495c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall    // works only because of the restricted set of expressions which
29595c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall    // we detect as constant targets.
29695c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall    if (cast<IndirectGotoStmt>(S)->getConstantTarget()) {
29795c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall      LabelAndGotoScopes[S] = ParentScope;
29895c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall      Jumps.push_back(S);
29995c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall      return;
30095c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall    }
30195c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall
3025af280ce21af061f96b5b5b752746871e364ba99Chris Lattner    LabelAndGotoScopes[S] = ParentScope;
303ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall    IndirectJumps.push_back(cast<IndirectGotoStmt>(S));
304ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall    break;
305ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall
306ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall  case Stmt::SwitchStmtClass:
3074967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    // Evaluate the C++17 init stmt and condition variable
3084967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    // before entering the scope of the switch statement.
3094967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    if (Stmt *Init = cast<SwitchStmt>(S)->getInit()) {
3104967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      BuildScopeInformation(Init, ParentScope);
3114967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      ++StmtsToSkip;
3124967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    }
31343dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor    if (VarDecl *Var = cast<SwitchStmt>(S)->getConditionVariable()) {
31443dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor      BuildScopeInformation(Var, ParentScope);
3154967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      ++StmtsToSkip;
31643dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor    }
31743dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor    // Fall through
318b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar
31943dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor  case Stmt::GotoStmtClass:
3205af280ce21af061f96b5b5b752746871e364ba99Chris Lattner    // Remember both what scope a goto is in as well as the fact that we have
3215af280ce21af061f96b5b5b752746871e364ba99Chris Lattner    // it.  This makes the second scan not have to walk the AST again.
3225af280ce21af061f96b5b5b752746871e364ba99Chris Lattner    LabelAndGotoScopes[S] = ParentScope;
3235af280ce21af061f96b5b5b752746871e364ba99Chris Lattner    Jumps.push_back(S);
324ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall    break;
325ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall
3264967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  case Stmt::IfStmtClass: {
3274967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    IfStmt *IS = cast<IfStmt>(S);
3284967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    if (!IS->isConstexpr())
3294967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      break;
3304967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar
3314967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    if (VarDecl *Var = IS->getConditionVariable())
3324967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      BuildScopeInformation(Var, ParentScope);
3334967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar
3344967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    // Cannot jump into the middle of the condition.
3354967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    unsigned NewParentScope = Scopes.size();
3364967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    Scopes.push_back(GotoScope(ParentScope,
3374967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar                               diag::note_protected_by_constexpr_if, 0,
3384967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar                               IS->getLocStart()));
3394967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    BuildScopeInformation(IS->getCond(), NewParentScope);
3404967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar
3414967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    // Jumps into either arm of an 'if constexpr' are not allowed.
3424967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    NewParentScope = Scopes.size();
3434967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    Scopes.push_back(GotoScope(ParentScope,
3444967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar                               diag::note_protected_by_constexpr_if, 0,
3454967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar                               IS->getLocStart()));
3464967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    BuildScopeInformation(IS->getThen(), NewParentScope);
3474967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    if (Stmt *Else = IS->getElse()) {
3484967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      NewParentScope = Scopes.size();
3494967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      Scopes.push_back(GotoScope(ParentScope,
3504967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar                                 diag::note_protected_by_constexpr_if, 0,
3514967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar                                 IS->getLocStart()));
3524967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      BuildScopeInformation(Else, NewParentScope);
3534967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    }
3544967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    return;
3554967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  }
3564967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar
357889b99e6d5b389c0ed99e7ad8470c28b146a42d3Eli Friedman  case Stmt::CXXTryStmtClass: {
358889b99e6d5b389c0ed99e7ad8470c28b146a42d3Eli Friedman    CXXTryStmt *TS = cast<CXXTryStmt>(S);
3594967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    {
3604967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      unsigned NewParentScope = Scopes.size();
3614967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      Scopes.push_back(GotoScope(ParentScope,
3624967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar                                 diag::note_protected_by_cxx_try,
3634967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar                                 diag::note_exits_cxx_try,
3644967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar                                 TS->getSourceRange().getBegin()));
3654967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      if (Stmt *TryBlock = TS->getTryBlock())
3664967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar        BuildScopeInformation(TryBlock, NewParentScope);
3674967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    }
368889b99e6d5b389c0ed99e7ad8470c28b146a42d3Eli Friedman
369889b99e6d5b389c0ed99e7ad8470c28b146a42d3Eli Friedman    // Jump from the catch into the try is not allowed either.
370889b99e6d5b389c0ed99e7ad8470c28b146a42d3Eli Friedman    for (unsigned I = 0, E = TS->getNumHandlers(); I != E; ++I) {
371889b99e6d5b389c0ed99e7ad8470c28b146a42d3Eli Friedman      CXXCatchStmt *CS = TS->getHandler(I);
3724967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      unsigned NewParentScope = Scopes.size();
373889b99e6d5b389c0ed99e7ad8470c28b146a42d3Eli Friedman      Scopes.push_back(GotoScope(ParentScope,
374889b99e6d5b389c0ed99e7ad8470c28b146a42d3Eli Friedman                                 diag::note_protected_by_cxx_catch,
375889b99e6d5b389c0ed99e7ad8470c28b146a42d3Eli Friedman                                 diag::note_exits_cxx_catch,
376889b99e6d5b389c0ed99e7ad8470c28b146a42d3Eli Friedman                                 CS->getSourceRange().getBegin()));
3774967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      BuildScopeInformation(CS->getHandlerBlock(), NewParentScope);
378889b99e6d5b389c0ed99e7ad8470c28b146a42d3Eli Friedman    }
379889b99e6d5b389c0ed99e7ad8470c28b146a42d3Eli Friedman    return;
380889b99e6d5b389c0ed99e7ad8470c28b146a42d3Eli Friedman  }
381889b99e6d5b389c0ed99e7ad8470c28b146a42d3Eli Friedman
3820e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  case Stmt::SEHTryStmtClass: {
3830e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    SEHTryStmt *TS = cast<SEHTryStmt>(S);
3844967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    {
3854967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      unsigned NewParentScope = Scopes.size();
3864967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      Scopes.push_back(GotoScope(ParentScope,
3874967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar                                 diag::note_protected_by_seh_try,
3884967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar                                 diag::note_exits_seh_try,
3894967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar                                 TS->getSourceRange().getBegin()));
3904967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      if (Stmt *TryBlock = TS->getTryBlock())
3914967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar        BuildScopeInformation(TryBlock, NewParentScope);
3924967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    }
3930e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
3940e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    // Jump from __except or __finally into the __try are not allowed either.
3950e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    if (SEHExceptStmt *Except = TS->getExceptHandler()) {
3964967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      unsigned NewParentScope = Scopes.size();
3970e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      Scopes.push_back(GotoScope(ParentScope,
3980e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                                 diag::note_protected_by_seh_except,
3990e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                                 diag::note_exits_seh_except,
4000e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                                 Except->getSourceRange().getBegin()));
4014967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      BuildScopeInformation(Except->getBlock(), NewParentScope);
4020e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    } else if (SEHFinallyStmt *Finally = TS->getFinallyHandler()) {
4034967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      unsigned NewParentScope = Scopes.size();
4040e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      Scopes.push_back(GotoScope(ParentScope,
4050e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                                 diag::note_protected_by_seh_finally,
4060e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                                 diag::note_exits_seh_finally,
4070e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                                 Finally->getSourceRange().getBegin()));
4084967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      BuildScopeInformation(Finally->getBlock(), NewParentScope);
4090e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    }
4100e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
4110e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    return;
4120e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  }
4130e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
4144967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  case Stmt::DeclStmtClass: {
4155af280ce21af061f96b5b5b752746871e364ba99Chris Lattner    // If this is a declstmt with a VLA definition, it defines a scope from here
4165af280ce21af061f96b5b5b752746871e364ba99Chris Lattner    // to the end of the containing context.
4174967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    DeclStmt *DS = cast<DeclStmt>(S);
4184967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    // The decl statement creates a scope if any of the decls in it are VLAs
4194967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    // or have the cleanup attribute.
4204967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    for (auto *I : DS->decls())
4214967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      BuildScopeInformation(I, origParentScope);
4224967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    return;
4234967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  }
4244967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar
4254967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  case Stmt::ObjCAtTryStmtClass: {
4265af280ce21af061f96b5b5b752746871e364ba99Chris Lattner    // Disallow jumps into any part of an @try statement by pushing a scope and
4275af280ce21af061f96b5b5b752746871e364ba99Chris Lattner    // walking all sub-stmts in that scope.
4284967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    ObjCAtTryStmt *AT = cast<ObjCAtTryStmt>(S);
4294967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    // Recursively walk the AST for the @try part.
4304967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    {
4314967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      unsigned NewParentScope = Scopes.size();
432ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall      Scopes.push_back(GotoScope(ParentScope,
433ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall                                 diag::note_protected_by_objc_try,
434ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall                                 diag::note_exits_objc_try,
4355af280ce21af061f96b5b5b752746871e364ba99Chris Lattner                                 AT->getAtTryLoc()));
4365af280ce21af061f96b5b5b752746871e364ba99Chris Lattner      if (Stmt *TryPart = AT->getTryBody())
4374967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar        BuildScopeInformation(TryPart, NewParentScope);
4384967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    }
4391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4404967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    // Jump from the catch to the finally or try is not valid.
4414967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    for (unsigned I = 0, N = AT->getNumCatchStmts(); I != N; ++I) {
4424967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      ObjCAtCatchStmt *AC = AT->getCatchStmt(I);
4434967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      unsigned NewParentScope = Scopes.size();
4444967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      Scopes.push_back(GotoScope(ParentScope,
4454967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar                                 diag::note_protected_by_objc_catch,
4464967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar                                 diag::note_exits_objc_catch,
4474967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar                                 AC->getAtCatchLoc()));
4484967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      // @catches are nested and it isn't
4494967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      BuildScopeInformation(AC->getCatchBody(), NewParentScope);
4504967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    }
4511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4524967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    // Jump from the finally to the try or catch is not valid.
4534967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    if (ObjCAtFinallyStmt *AF = AT->getFinallyStmt()) {
4544967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      unsigned NewParentScope = Scopes.size();
4554967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      Scopes.push_back(GotoScope(ParentScope,
4564967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar                                 diag::note_protected_by_objc_finally,
4574967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar                                 diag::note_exits_objc_finally,
4584967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar                                 AF->getAtFinallyLoc()));
4594967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      BuildScopeInformation(AF, NewParentScope);
4605af280ce21af061f96b5b5b752746871e364ba99Chris Lattner    }
461b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar
4624967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    return;
4634967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  }
4644967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar
4654967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  case Stmt::ObjCAtSynchronizedStmtClass: {
46646c3c4ba78766ac0f1c5ec631b424773e21f5271Chris Lattner    // Disallow jumps into the protected statement of an @synchronized, but
46746c3c4ba78766ac0f1c5ec631b424773e21f5271Chris Lattner    // allow jumps into the object expression it protects.
4684967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    ObjCAtSynchronizedStmt *AS = cast<ObjCAtSynchronizedStmt>(S);
4694967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    // Recursively walk the AST for the @synchronized object expr, it is
4704967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    // evaluated in the normal scope.
4714967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    BuildScopeInformation(AS->getSynchExpr(), ParentScope);
4724967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar
4734967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    // Recursively walk the AST for the @synchronized part, protected by a new
4744967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    // scope.
4754967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    unsigned NewParentScope = Scopes.size();
4764967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    Scopes.push_back(GotoScope(ParentScope,
4774967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar                               diag::note_protected_by_objc_synchronized,
4784967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar                               diag::note_exits_objc_synchronized,
4794967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar                               AS->getAtSynchronizedLoc()));
4804967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    BuildScopeInformation(AS->getSynchBody(), NewParentScope);
4814967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    return;
4824967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  }
483972041f45bdf8df7ea447221292d7827466ba94bSebastian Redl
4844967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  case Stmt::ObjCAutoreleasePoolStmtClass: {
485f85e193739c953358c865005855253af4f68a497John McCall    // Disallow jumps into the protected statement of an @autoreleasepool.
4864967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    ObjCAutoreleasePoolStmt *AS = cast<ObjCAutoreleasePoolStmt>(S);
4874967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    // Recursively walk the AST for the @autoreleasepool part, protected by a
4884967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    // new scope.
4894967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    unsigned NewParentScope = Scopes.size();
4904967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    Scopes.push_back(GotoScope(ParentScope,
4914967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar                               diag::note_protected_by_objc_autoreleasepool,
4924967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar                               diag::note_exits_objc_autoreleasepool,
4934967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar                               AS->getAtLoc()));
4944967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    BuildScopeInformation(AS->getSubStmt(), NewParentScope);
4954967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    return;
4964967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  }
4979f357de8d5823f9b13cf33ad1f6af1dd69b7669fJohn McCall
4984967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  case Stmt::ExprWithCleanupsClass: {
4999f357de8d5823f9b13cf33ad1f6af1dd69b7669fJohn McCall    // Disallow jumps past full-expressions that use blocks with
5009f357de8d5823f9b13cf33ad1f6af1dd69b7669fJohn McCall    // non-trivial cleanups of their captures.  This is theoretically
5019f357de8d5823f9b13cf33ad1f6af1dd69b7669fJohn McCall    // implementable but a lot of work which we haven't felt up to doing.
5024967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    ExprWithCleanups *EWC = cast<ExprWithCleanups>(S);
5034967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    for (unsigned i = 0, e = EWC->getNumObjects(); i != e; ++i) {
5044967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      const BlockDecl *BDecl = EWC->getObject(i);
5054967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      for (const auto &CI : BDecl->captures()) {
5064967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar        VarDecl *variable = CI.getVariable();
5074967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar        BuildScopeInformation(variable, BDecl, origParentScope);
5089f357de8d5823f9b13cf33ad1f6af1dd69b7669fJohn McCall      }
5094e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian    }
5104967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    break;
5114967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  }
512651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
5134967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  case Stmt::MaterializeTemporaryExprClass: {
514651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    // Disallow jumps out of scopes containing temporaries lifetime-extended to
515651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    // automatic storage duration.
5164967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    MaterializeTemporaryExpr *MTE = cast<MaterializeTemporaryExpr>(S);
5174967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    if (MTE->getStorageDuration() == SD_Automatic) {
5184967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      SmallVector<const Expr *, 4> CommaLHS;
5194967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      SmallVector<SubobjectAdjustment, 4> Adjustments;
5204967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      const Expr *ExtendedObject =
5214967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar          MTE->GetTemporaryExpr()->skipRValueSubobjectAdjustments(
5224967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar              CommaLHS, Adjustments);
5234967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      if (ExtendedObject->getType().isDestructedType()) {
5244967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar        Scopes.push_back(GotoScope(ParentScope, 0,
5254967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar                                   diag::note_exits_temporary_dtor,
5264967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar                                   ExtendedObject->getExprLoc()));
5274967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar        origParentScope = Scopes.size()-1;
528651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      }
529651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    }
5304967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    break;
5314967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  }
5324967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar
5334967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  case Stmt::CaseStmtClass:
5344967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  case Stmt::DefaultStmtClass:
5354967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  case Stmt::LabelStmtClass:
5364967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    LabelAndGotoScopes[S] = ParentScope;
5374967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    break;
5384967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar
5394967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  default:
5404967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    break;
5414967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  }
5424967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar
5434967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  for (Stmt *SubStmt : S->children()) {
5444967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    if (!SubStmt)
5454967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar        continue;
5464967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    if (StmtsToSkip) {
5474967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      --StmtsToSkip;
5484967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      continue;
5494967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    }
5504967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar
5514967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    // Cases, labels, and defaults aren't "scope parents".  It's also
5524967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    // important to handle these iteratively instead of recursively in
5534967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    // order to avoid blowing out the stack.
5544967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    while (true) {
5554967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      Stmt *Next;
5564967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      if (CaseStmt *CS = dyn_cast<CaseStmt>(SubStmt))
5574967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar        Next = CS->getSubStmt();
5584967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      else if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SubStmt))
5594967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar        Next = DS->getSubStmt();
5604967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      else if (LabelStmt *LS = dyn_cast<LabelStmt>(SubStmt))
5614967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar        Next = LS->getSubStmt();
5624967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      else
5634967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar        break;
5644967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar
5654967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      LabelAndGotoScopes[SubStmt] = ParentScope;
5664967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      SubStmt = Next;
5674967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    }
568651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
5695af280ce21af061f96b5b5b752746871e364ba99Chris Lattner    // Recursively walk the AST.
5705af280ce21af061f96b5b5b752746871e364ba99Chris Lattner    BuildScopeInformation(SubStmt, ParentScope);
5715af280ce21af061f96b5b5b752746871e364ba99Chris Lattner  }
5725af280ce21af061f96b5b5b752746871e364ba99Chris Lattner}
5735af280ce21af061f96b5b5b752746871e364ba99Chris Lattner
5745af280ce21af061f96b5b5b752746871e364ba99Chris Lattner/// VerifyJumps - Verify each element of the Jumps array to see if they are
5755af280ce21af061f96b5b5b752746871e364ba99Chris Lattner/// valid, emitting diagnostics if not.
5765af280ce21af061f96b5b5b752746871e364ba99Chris Lattnervoid JumpScopeChecker::VerifyJumps() {
5775af280ce21af061f96b5b5b752746871e364ba99Chris Lattner  while (!Jumps.empty()) {
5785af280ce21af061f96b5b5b752746871e364ba99Chris Lattner    Stmt *Jump = Jumps.pop_back_val();
5791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // With a goto,
5815af280ce21af061f96b5b5b752746871e364ba99Chris Lattner    if (GotoStmt *GS = dyn_cast<GotoStmt>(Jump)) {
582176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      // The label may not have a statement if it's coming from inline MS ASM.
583176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      if (GS->getLabel()->getStmt()) {
584176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        CheckJump(GS, GS->getLabel()->getStmt(), GS->getGotoLoc(),
585176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                  diag::err_goto_into_protected_scope,
586176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                  diag::ext_goto_into_protected_scope,
587176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                  diag::warn_cxx98_compat_goto_into_protected_scope);
588176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      }
589176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      CheckGotoStmt(GS);
5905af280ce21af061f96b5b5b752746871e364ba99Chris Lattner      continue;
5915af280ce21af061f96b5b5b752746871e364ba99Chris Lattner    }
5921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
59395c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall    // We only get indirect gotos here when they have a constant target.
59495c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall    if (IndirectGotoStmt *IGS = dyn_cast<IndirectGotoStmt>(Jump)) {
595ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner      LabelDecl *Target = IGS->getConstantTarget();
596ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner      CheckJump(IGS, Target->getStmt(), IGS->getGotoLoc(),
5972e96511773c6a21291dfa5218eb8ba79f04b5318Francois Pichet                diag::err_goto_into_protected_scope,
598176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                diag::ext_goto_into_protected_scope,
5990e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith                diag::warn_cxx98_compat_goto_into_protected_scope);
60095c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall      continue;
60195c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall    }
60295c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall
603ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall    SwitchStmt *SS = cast<SwitchStmt>(Jump);
604ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall    for (SwitchCase *SC = SS->getSwitchCaseList(); SC;
605ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall         SC = SC->getNextSwitchCase()) {
6066bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      if (CHECK_PERMISSIVE(!LabelAndGotoScopes.count(SC)))
6076bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        continue;
60865d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik Verbruggen      SourceLocation Loc;
60965d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik Verbruggen      if (CaseStmt *CS = dyn_cast<CaseStmt>(SC))
61065d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik Verbruggen        Loc = CS->getLocStart();
61165d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik Verbruggen      else if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC))
61265d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik Verbruggen        Loc = DS->getLocStart();
61365d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik Verbruggen      else
61465d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik Verbruggen        Loc = SC->getLocStart();
61565d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik Verbruggen      CheckJump(SS, SC, Loc, diag::err_switch_into_protected_scope, 0,
6160e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith                diag::warn_cxx98_compat_switch_into_protected_scope);
6175af280ce21af061f96b5b5b752746871e364ba99Chris Lattner    }
618ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall  }
619ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall}
620ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall
6215e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall/// VerifyIndirectJumps - Verify whether any possible indirect jump
6225e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall/// might cross a protection boundary.  Unlike direct jumps, indirect
6235e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall/// jumps count cleanups as protection boundaries:  since there's no
6245e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall/// way to know where the jump is going, we can't implicitly run the
6255e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall/// right cleanups the way we can with direct jumps.
6265e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall///
6275e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall/// Thus, an indirect jump is "trivial" if it bypasses no
6285e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall/// initializations and no teardowns.  More formally, an indirect jump
6295e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall/// from A to B is trivial if the path out from A to DCA(A,B) is
6305e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall/// trivial and the path in from DCA(A,B) to B is trivial, where
6315e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall/// DCA(A,B) is the deepest common ancestor of A and B.
6325e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall/// Jump-triviality is transitive but asymmetric.
633ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall///
634ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall/// A path in is trivial if none of the entered scopes have an InDiag.
635ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall/// A path out is trivial is none of the exited scopes have an OutDiag.
6365e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall///
6375e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall/// Under these definitions, this function checks that the indirect
6385e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall/// jump between A and B is trivial for every indirect goto statement A
6395e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall/// and every label B whose address was taken in the function.
640ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCallvoid JumpScopeChecker::VerifyIndirectJumps() {
641ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall  if (IndirectJumps.empty()) return;
642ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall
643ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall  // If there aren't any address-of-label expressions in this function,
644ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall  // complain about the first indirect goto.
645ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall  if (IndirectJumpTargets.empty()) {
646ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall    S.Diag(IndirectJumps[0]->getGotoLoc(),
647ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall           diag::err_indirect_goto_without_addrlabel);
648ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall    return;
649ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall  }
650ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall
6515e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall  // Collect a single representative of every scope containing an
6525e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall  // indirect goto.  For most code bases, this substantially cuts
6535e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall  // down on the number of jump sites we'll have to consider later.
654ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall  typedef std::pair<unsigned, IndirectGotoStmt*> JumpScope;
6555f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<JumpScope, 32> JumpScopes;
656ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall  {
657ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall    llvm::DenseMap<unsigned, IndirectGotoStmt*> JumpScopesMap;
6585f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    for (SmallVectorImpl<IndirectGotoStmt*>::iterator
659ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall           I = IndirectJumps.begin(), E = IndirectJumps.end(); I != E; ++I) {
660ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall      IndirectGotoStmt *IG = *I;
6616bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      if (CHECK_PERMISSIVE(!LabelAndGotoScopes.count(IG)))
6626bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        continue;
663ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall      unsigned IGScope = LabelAndGotoScopes[IG];
664ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall      IndirectGotoStmt *&Entry = JumpScopesMap[IGScope];
665ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall      if (!Entry) Entry = IG;
666ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall    }
667ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall    JumpScopes.reserve(JumpScopesMap.size());
668ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall    for (llvm::DenseMap<unsigned, IndirectGotoStmt*>::iterator
669ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall           I = JumpScopesMap.begin(), E = JumpScopesMap.end(); I != E; ++I)
670ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall      JumpScopes.push_back(*I);
671ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall  }
672ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall
6735e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall  // Collect a single representative of every scope containing a
6745e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall  // label whose address was taken somewhere in the function.
6755e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall  // For most code bases, there will be only one such scope.
676ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner  llvm::DenseMap<unsigned, LabelDecl*> TargetScopes;
6775f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  for (SmallVectorImpl<LabelDecl*>::iterator
678ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall         I = IndirectJumpTargets.begin(), E = IndirectJumpTargets.end();
679ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall       I != E; ++I) {
680ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner    LabelDecl *TheLabel = *I;
6816bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    if (CHECK_PERMISSIVE(!LabelAndGotoScopes.count(TheLabel->getStmt())))
6826bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      continue;
683ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner    unsigned LabelScope = LabelAndGotoScopes[TheLabel->getStmt()];
684ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner    LabelDecl *&Target = TargetScopes[LabelScope];
685ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall    if (!Target) Target = TheLabel;
686ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall  }
687ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall
6885e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall  // For each target scope, make sure it's trivially reachable from
6895e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall  // every scope containing a jump site.
6905e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall  //
6915e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall  // A path between scopes always consists of exitting zero or more
6925e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall  // scopes, then entering zero or more scopes.  We build a set of
6935e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall  // of scopes S from which the target scope can be trivially
6945e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall  // entered, then verify that every jump scope can be trivially
6955e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall  // exitted to reach a scope in S.
696ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall  llvm::BitVector Reachable(Scopes.size(), false);
697ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner  for (llvm::DenseMap<unsigned,LabelDecl*>::iterator
698ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall         TI = TargetScopes.begin(), TE = TargetScopes.end(); TI != TE; ++TI) {
699ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall    unsigned TargetScope = TI->first;
700ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner    LabelDecl *TargetLabel = TI->second;
701ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall
702ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall    Reachable.reset();
703ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall
704ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall    // Mark all the enclosing scopes from which you can safely jump
7055e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall    // into the target scope.  'Min' will end up being the index of
7065e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall    // the shallowest such scope.
707ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall    unsigned Min = TargetScope;
708ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall    while (true) {
709ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall      Reachable.set(Min);
710ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall
711ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall      // Don't go beyond the outermost scope.
712ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall      if (Min == 0) break;
713ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall
7145e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall      // Stop if we can't trivially enter the current scope.
715ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall      if (Scopes[Min].InDiag) break;
7165af280ce21af061f96b5b5b752746871e364ba99Chris Lattner
717ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall      Min = Scopes[Min].ParentScope;
7185af280ce21af061f96b5b5b752746871e364ba99Chris Lattner    }
7195af280ce21af061f96b5b5b752746871e364ba99Chris Lattner
720ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall    // Walk through all the jump sites, checking that they can trivially
721ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall    // reach this label scope.
7225f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    for (SmallVectorImpl<JumpScope>::iterator
723ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall           I = JumpScopes.begin(), E = JumpScopes.end(); I != E; ++I) {
724ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall      unsigned Scope = I->first;
725ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall
726ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall      // Walk out the "scope chain" for this scope, looking for a scope
7275e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall      // we've marked reachable.  For well-formed code this amortizes
7285e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall      // to O(JumpScopes.size() / Scopes.size()):  we only iterate
7295e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall      // when we see something unmarked, and in well-formed code we
7305e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall      // mark everything we iterate past.
731ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall      bool IsReachable = false;
732ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall      while (true) {
733ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall        if (Reachable.test(Scope)) {
734ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall          // If we find something reachable, mark all the scopes we just
735ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall          // walked through as reachable.
736ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall          for (unsigned S = I->first; S != Scope; S = Scopes[S].ParentScope)
737ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall            Reachable.set(S);
738ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall          IsReachable = true;
739ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall          break;
740ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall        }
741ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall
742ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall        // Don't walk out if we've reached the top-level scope or we've
743ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall        // gotten shallower than the shallowest reachable scope.
744ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall        if (Scope == 0 || Scope < Min) break;
745ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall
746ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall        // Don't walk out through an out-diagnostic.
747ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall        if (Scopes[Scope].OutDiag) break;
748ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall
749ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall        Scope = Scopes[Scope].ParentScope;
750ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall      }
751ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall
752ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall      // Only diagnose if we didn't find something.
753ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall      if (IsReachable) continue;
754ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall
755ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall      DiagnoseIndirectJump(I->second, I->first, TargetLabel, TargetScope);
7565af280ce21af061f96b5b5b752746871e364ba99Chris Lattner    }
7575af280ce21af061f96b5b5b752746871e364ba99Chris Lattner  }
7585af280ce21af061f96b5b5b752746871e364ba99Chris Lattner}
7595af280ce21af061f96b5b5b752746871e364ba99Chris Lattner
7600e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith/// Return true if a particular error+note combination must be downgraded to a
7610e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith/// warning in Microsoft mode.
7620e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smithstatic bool IsMicrosoftJumpWarning(unsigned JumpDiag, unsigned InDiagNote) {
7630e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith  return (JumpDiag == diag::err_goto_into_protected_scope &&
7640e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith         (InDiagNote == diag::note_protected_by_variable_init ||
7650e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith          InDiagNote == diag::note_protected_by_variable_nontriv_destructor));
7660e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith}
7670e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith
7680e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith/// Return true if a particular note should be downgraded to a compatibility
7690e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith/// warning in C++11 mode.
7700e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smithstatic bool IsCXX98CompatWarning(Sema &S, unsigned InDiagNote) {
77180ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith  return S.getLangOpts().CPlusPlus11 &&
7720e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith         InDiagNote == diag::note_protected_by_variable_non_pod;
7730e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith}
7740e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith
7750e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith/// Produce primary diagnostic for an indirect jump statement.
7760e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smithstatic void DiagnoseIndirectJumpStmt(Sema &S, IndirectGotoStmt *Jump,
7770e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith                                     LabelDecl *Target, bool &Diagnosed) {
7780e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith  if (Diagnosed)
7790e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith    return;
7800e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith  S.Diag(Jump->getGotoLoc(), diag::err_indirect_goto_in_protected_scope);
7810e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith  S.Diag(Target->getStmt()->getIdentLoc(), diag::note_indirect_goto_target);
7820e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith  Diagnosed = true;
7830e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith}
7840e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith
7850e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith/// Produce note diagnostics for a jump into a protected scope.
7864fe5be028b723fedc28bb33be96cde1ab2574ee6Bill Wendlingvoid JumpScopeChecker::NoteJumpIntoScopes(ArrayRef<unsigned> ToScopes) {
7876bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  if (CHECK_PERMISSIVE(ToScopes.empty()))
7886bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return;
7890e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith  for (unsigned I = 0, E = ToScopes.size(); I != E; ++I)
7900e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith    if (Scopes[ToScopes[I]].InDiag)
7910e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith      S.Diag(Scopes[ToScopes[I]].Loc, Scopes[ToScopes[I]].InDiag);
7920e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith}
7930e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith
7945e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall/// Diagnose an indirect jump which is known to cross scopes.
795ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCallvoid JumpScopeChecker::DiagnoseIndirectJump(IndirectGotoStmt *Jump,
796ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall                                            unsigned JumpScope,
797ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner                                            LabelDecl *Target,
798ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall                                            unsigned TargetScope) {
7996bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  if (CHECK_PERMISSIVE(JumpScope == TargetScope))
8006bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return;
801ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall
8025e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall  unsigned Common = GetDeepestCommonScope(JumpScope, TargetScope);
8030e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith  bool Diagnosed = false;
8045e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall
8055e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall  // Walk out the scope chain until we reach the common ancestor.
8065e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall  for (unsigned I = JumpScope; I != Common; I = Scopes[I].ParentScope)
8070e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith    if (Scopes[I].OutDiag) {
8080e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith      DiagnoseIndirectJumpStmt(S, Jump, Target, Diagnosed);
8095e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall      S.Diag(Scopes[I].Loc, Scopes[I].OutDiag);
8100e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith    }
8110e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith
8120e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith  SmallVector<unsigned, 10> ToScopesCXX98Compat;
813ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall
814ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall  // Now walk into the scopes containing the label whose address was taken.
8155e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall  for (unsigned I = TargetScope; I != Common; I = Scopes[I].ParentScope)
8160e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith    if (IsCXX98CompatWarning(S, Scopes[I].InDiag))
8170e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith      ToScopesCXX98Compat.push_back(I);
8180e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith    else if (Scopes[I].InDiag) {
8190e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith      DiagnoseIndirectJumpStmt(S, Jump, Target, Diagnosed);
8205e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall      S.Diag(Scopes[I].Loc, Scopes[I].InDiag);
8210e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith    }
822ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall
8230e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith  // Diagnose this jump if it would be ill-formed in C++98.
8240e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith  if (!Diagnosed && !ToScopesCXX98Compat.empty()) {
8250e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith    S.Diag(Jump->getGotoLoc(),
8260e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith           diag::warn_cxx98_compat_indirect_goto_in_protected_scope);
8270e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith    S.Diag(Target->getStmt()->getIdentLoc(), diag::note_indirect_goto_target);
8280e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith    NoteJumpIntoScopes(ToScopesCXX98Compat);
8290e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith  }
830c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet}
831c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet
8325af280ce21af061f96b5b5b752746871e364ba99Chris Lattner/// CheckJump - Validate that the specified jump statement is valid: that it is
8335af280ce21af061f96b5b5b752746871e364ba99Chris Lattner/// jumping within or out of its current scope, not into a deeper one.
834c985b88efc9188845952ab524fe6d4717705257bFrancois Pichetvoid JumpScopeChecker::CheckJump(Stmt *From, Stmt *To, SourceLocation DiagLoc,
8350e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith                               unsigned JumpDiagError, unsigned JumpDiagWarning,
8360e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith                                 unsigned JumpDiagCXX98Compat) {
8376bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  if (CHECK_PERMISSIVE(!LabelAndGotoScopes.count(From)))
8386bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return;
8396bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  if (CHECK_PERMISSIVE(!LabelAndGotoScopes.count(To)))
8406bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return;
8415af280ce21af061f96b5b5b752746871e364ba99Chris Lattner
8426bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  unsigned FromScope = LabelAndGotoScopes[From];
8435af280ce21af061f96b5b5b752746871e364ba99Chris Lattner  unsigned ToScope = LabelAndGotoScopes[To];
8441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8455af280ce21af061f96b5b5b752746871e364ba99Chris Lattner  // Common case: exactly the same scope, which is fine.
8465af280ce21af061f96b5b5b752746871e364ba99Chris Lattner  if (FromScope == ToScope) return;
8471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8483ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar  // Warn on gotos out of __finally blocks.
8493ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar  if (isa<GotoStmt>(From) || isa<IndirectGotoStmt>(From)) {
8503ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar    // If FromScope > ToScope, FromScope is more nested and the jump goes to a
8513ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar    // less nested scope.  Check if it crosses a __finally along the way.
8523ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar    for (unsigned I = FromScope; I > ToScope; I = Scopes[I].ParentScope) {
8533ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar      if (Scopes[I].InDiag == diag::note_protected_by_seh_finally) {
8543ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar        S.Diag(From->getLocStart(), diag::warn_jump_out_of_seh_finally);
8553ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar        break;
8563ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar      }
8573ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar    }
8583ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar  }
8593ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar
8605e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall  unsigned CommonScope = GetDeepestCommonScope(FromScope, ToScope);
8611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8625e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall  // It's okay to jump out from a nested scope.
8635e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall  if (CommonScope == ToScope) return;
8641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8655e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall  // Pull out (and reverse) any scopes we might need to diagnose skipping.
8660e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith  SmallVector<unsigned, 10> ToScopesCXX98Compat;
867c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet  SmallVector<unsigned, 10> ToScopesError;
868c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet  SmallVector<unsigned, 10> ToScopesWarning;
869c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet  for (unsigned I = ToScope; I != CommonScope; I = Scopes[I].ParentScope) {
870651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    if (S.getLangOpts().MSVCCompat && JumpDiagWarning != 0 &&
871c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet        IsMicrosoftJumpWarning(JumpDiagError, Scopes[I].InDiag))
872c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet      ToScopesWarning.push_back(I);
8730e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith    else if (IsCXX98CompatWarning(S, Scopes[I].InDiag))
8740e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith      ToScopesCXX98Compat.push_back(I);
875c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet    else if (Scopes[I].InDiag)
876c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet      ToScopesError.push_back(I);
877c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet  }
878ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall
879c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet  // Handle warnings.
880c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet  if (!ToScopesWarning.empty()) {
881c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet    S.Diag(DiagLoc, JumpDiagWarning);
8820e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith    NoteJumpIntoScopes(ToScopesWarning);
883c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet  }
884ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall
885c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet  // Handle errors.
886c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet  if (!ToScopesError.empty()) {
887c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet    S.Diag(DiagLoc, JumpDiagError);
8880e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith    NoteJumpIntoScopes(ToScopesError);
8890e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith  }
8900e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith
8910e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith  // Handle -Wc++98-compat warnings if the jump is well-formed.
8920e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith  if (ToScopesError.empty() && !ToScopesCXX98Compat.empty()) {
8930e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith    S.Diag(DiagLoc, JumpDiagCXX98Compat);
8940e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith    NoteJumpIntoScopes(ToScopesCXX98Compat);
895c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet  }
8965af280ce21af061f96b5b5b752746871e364ba99Chris Lattner}
8975af280ce21af061f96b5b5b752746871e364ba99Chris Lattner
898176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hinesvoid JumpScopeChecker::CheckGotoStmt(GotoStmt *GS) {
899176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  if (GS->getLabel()->isMSAsmLabel()) {
900176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    S.Diag(GS->getGotoLoc(), diag::err_goto_ms_asm_label)
901176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        << GS->getLabel()->getIdentifier();
902176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    S.Diag(GS->getLabel()->getLocation(), diag::note_goto_ms_asm_label)
903176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        << GS->getLabel()->getIdentifier();
904176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  }
905176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines}
906176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
9075af280ce21af061f96b5b5b752746871e364ba99Chris Lattnervoid Sema::DiagnoseInvalidJumps(Stmt *Body) {
9086490ae5003226cae28f980648948bea8b21a8638Douglas Gregor  (void)JumpScopeChecker(Body, *this);
9095af280ce21af061f96b5b5b752746871e364ba99Chris Lattner}
910