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 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 450e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith /// InDiag - The note to emit if there is a jump into this scope. 46ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall unsigned InDiag; 47ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall 480e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith /// OutDiag - The note 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(); 774fe5be028b723fedc28bb33be96cde1ab2574ee6Bill Wendling void NoteJumpIntoScopes(ArrayRef<unsigned> ToScopes); 78ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall void DiagnoseIndirectJump(IndirectGotoStmt *IG, unsigned IGScope, 79ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner LabelDecl *Target, unsigned TargetScope); 80c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet void CheckJump(Stmt *From, Stmt *To, SourceLocation DiagLoc, 810e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith unsigned JumpDiag, unsigned JumpDiagWarning, 820e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith unsigned JumpDiagCXX98Compat); 835e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall 845e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall unsigned GetDeepestCommonScope(unsigned A, unsigned B); 855af280ce21af061f96b5b5b752746871e364ba99Chris Lattner}; 865af280ce21af061f96b5b5b752746871e364ba99Chris Lattner} // end anonymous namespace 875af280ce21af061f96b5b5b752746871e364ba99Chris Lattner 885af280ce21af061f96b5b5b752746871e364ba99Chris Lattner 895af280ce21af061f96b5b5b752746871e364ba99Chris LattnerJumpScopeChecker::JumpScopeChecker(Stmt *Body, Sema &s) : S(s) { 905af280ce21af061f96b5b5b752746871e364ba99Chris Lattner // Add a scope entry for function scope. 91ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall Scopes.push_back(GotoScope(~0U, ~0U, ~0U, SourceLocation())); 921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 935af280ce21af061f96b5b5b752746871e364ba99Chris Lattner // Build information for the top level compound statement, so that we have a 945af280ce21af061f96b5b5b752746871e364ba99Chris Lattner // defined scope record for every "goto" and label. 954e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian unsigned BodyParentScope = 0; 964e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian BuildScopeInformation(Body, BodyParentScope); 971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 985af280ce21af061f96b5b5b752746871e364ba99Chris Lattner // Check that all jumps we saw are kosher. 995af280ce21af061f96b5b5b752746871e364ba99Chris Lattner VerifyJumps(); 100ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall VerifyIndirectJumps(); 1015af280ce21af061f96b5b5b752746871e364ba99Chris Lattner} 1021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1035e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall/// GetDeepestCommonScope - Finds the innermost scope enclosing the 1045e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall/// two scopes. 1055e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCallunsigned JumpScopeChecker::GetDeepestCommonScope(unsigned A, unsigned B) { 1065e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall while (A != B) { 1075e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall // Inner scopes are created after outer scopes and therefore have 1085e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall // higher indices. 1095e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall if (A < B) { 1105e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall assert(Scopes[B].ParentScope < B); 1115e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall B = Scopes[B].ParentScope; 1125e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall } else { 1135e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall assert(Scopes[A].ParentScope < A); 1145e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall A = Scopes[A].ParentScope; 1155e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall } 1165e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall } 1175e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall return A; 1185e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall} 1195e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall 120f85e193739c953358c865005855253af4f68a497John McCalltypedef std::pair<unsigned,unsigned> ScopePair; 121f85e193739c953358c865005855253af4f68a497John McCall 1225af280ce21af061f96b5b5b752746871e364ba99Chris Lattner/// GetDiagForGotoScopeDecl - If this decl induces a new goto scope, return a 1235af280ce21af061f96b5b5b752746871e364ba99Chris Lattner/// diagnostic that should be emitted if control goes over it. If not, return 0. 124f85e193739c953358c865005855253af4f68a497John McCallstatic ScopePair GetDiagForGotoScopeDecl(ASTContext &Context, const Decl *D) { 1255af280ce21af061f96b5b5b752746871e364ba99Chris Lattner if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { 126644e90a17f1bc3dcc2b7b018eb02abb3a1c2022bRafael Espindola unsigned InDiag = 0; 1275af280ce21af061f96b5b5b752746871e364ba99Chris Lattner if (VD->getType()->isVariablyModifiedType()) 128ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall InDiag = diag::note_protected_by_vla; 129ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall 130f85e193739c953358c865005855253af4f68a497John McCall if (VD->hasAttr<BlocksAttr>()) 131f85e193739c953358c865005855253af4f68a497John McCall return ScopePair(diag::note_protected_by___block, 132f85e193739c953358c865005855253af4f68a497John McCall diag::note_exits___block); 133f85e193739c953358c865005855253af4f68a497John McCall 134f85e193739c953358c865005855253af4f68a497John McCall if (VD->hasAttr<CleanupAttr>()) 135f85e193739c953358c865005855253af4f68a497John McCall return ScopePair(diag::note_protected_by_cleanup, 136f85e193739c953358c865005855253af4f68a497John McCall diag::note_exits_cleanup); 137f85e193739c953358c865005855253af4f68a497John McCall 1384e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (Context.getLangOpts().ObjCAutoRefCount && VD->hasLocalStorage()) { 139f85e193739c953358c865005855253af4f68a497John McCall switch (VD->getType().getObjCLifetime()) { 140f85e193739c953358c865005855253af4f68a497John McCall case Qualifiers::OCL_None: 141f85e193739c953358c865005855253af4f68a497John McCall case Qualifiers::OCL_ExplicitNone: 142f85e193739c953358c865005855253af4f68a497John McCall case Qualifiers::OCL_Autoreleasing: 143f85e193739c953358c865005855253af4f68a497John McCall break; 144f85e193739c953358c865005855253af4f68a497John McCall 145f85e193739c953358c865005855253af4f68a497John McCall case Qualifiers::OCL_Strong: 146f85e193739c953358c865005855253af4f68a497John McCall case Qualifiers::OCL_Weak: 147b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidis return ScopePair(diag::note_protected_by_objc_ownership, 148b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidis diag::note_exits_objc_ownership); 149f85e193739c953358c865005855253af4f68a497John McCall } 150f85e193739c953358c865005855253af4f68a497John McCall } 151f85e193739c953358c865005855253af4f68a497John McCall 1524e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (Context.getLangOpts().CPlusPlus && VD->hasLocalStorage()) { 1530e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith // C++11 [stmt.dcl]p3: 154f85e193739c953358c865005855253af4f68a497John McCall // A program that jumps from a point where a variable with automatic 155f85e193739c953358c865005855253af4f68a497John McCall // storage duration is not in scope to a point where it is in scope 156f85e193739c953358c865005855253af4f68a497John McCall // is ill-formed unless the variable has scalar type, class type with 157f85e193739c953358c865005855253af4f68a497John McCall // a trivial default constructor and a trivial destructor, a 158f85e193739c953358c865005855253af4f68a497John McCall // cv-qualified version of one of these types, or an array of one of 159f85e193739c953358c865005855253af4f68a497John McCall // the preceding types and is declared without an initializer. 160f85e193739c953358c865005855253af4f68a497John McCall 161f85e193739c953358c865005855253af4f68a497John McCall // C++03 [stmt.dcl.p3: 162f85e193739c953358c865005855253af4f68a497John McCall // A program that jumps from a point where a local variable 163f85e193739c953358c865005855253af4f68a497John McCall // with automatic storage duration is not in scope to a point 164f85e193739c953358c865005855253af4f68a497John McCall // where it is in scope is ill-formed unless the variable has 165f85e193739c953358c865005855253af4f68a497John McCall // POD type and is declared without an initializer. 166f85e193739c953358c865005855253af4f68a497John McCall 167644e90a17f1bc3dcc2b7b018eb02abb3a1c2022bRafael Espindola const Expr *Init = VD->getInit(); 168644e90a17f1bc3dcc2b7b018eb02abb3a1c2022bRafael Espindola if (!Init) 169644e90a17f1bc3dcc2b7b018eb02abb3a1c2022bRafael Espindola return ScopePair(InDiag, 0); 170644e90a17f1bc3dcc2b7b018eb02abb3a1c2022bRafael Espindola 171644e90a17f1bc3dcc2b7b018eb02abb3a1c2022bRafael Espindola const ExprWithCleanups *EWC = dyn_cast<ExprWithCleanups>(Init); 172644e90a17f1bc3dcc2b7b018eb02abb3a1c2022bRafael Espindola if (EWC) 173644e90a17f1bc3dcc2b7b018eb02abb3a1c2022bRafael Espindola Init = EWC->getSubExpr(); 174644e90a17f1bc3dcc2b7b018eb02abb3a1c2022bRafael Espindola 175644e90a17f1bc3dcc2b7b018eb02abb3a1c2022bRafael Espindola const MaterializeTemporaryExpr *M = NULL; 176644e90a17f1bc3dcc2b7b018eb02abb3a1c2022bRafael Espindola Init = Init->findMaterializedTemporary(M); 177644e90a17f1bc3dcc2b7b018eb02abb3a1c2022bRafael Espindola 178644e90a17f1bc3dcc2b7b018eb02abb3a1c2022bRafael Espindola SmallVector<SubobjectAdjustment, 2> Adjustments; 179644e90a17f1bc3dcc2b7b018eb02abb3a1c2022bRafael Espindola Init = Init->skipRValueSubobjectAdjustments(Adjustments); 180644e90a17f1bc3dcc2b7b018eb02abb3a1c2022bRafael Espindola 181644e90a17f1bc3dcc2b7b018eb02abb3a1c2022bRafael Espindola QualType QT = Init->getType(); 182644e90a17f1bc3dcc2b7b018eb02abb3a1c2022bRafael Espindola if (QT.isNull()) 183644e90a17f1bc3dcc2b7b018eb02abb3a1c2022bRafael Espindola return ScopePair(diag::note_protected_by_variable_init, 0); 184644e90a17f1bc3dcc2b7b018eb02abb3a1c2022bRafael Espindola 185644e90a17f1bc3dcc2b7b018eb02abb3a1c2022bRafael Espindola const Type *T = QT.getTypePtr(); 186644e90a17f1bc3dcc2b7b018eb02abb3a1c2022bRafael Espindola if (T->isArrayType()) 187644e90a17f1bc3dcc2b7b018eb02abb3a1c2022bRafael Espindola T = T->getBaseElementTypeUnsafe(); 188644e90a17f1bc3dcc2b7b018eb02abb3a1c2022bRafael Espindola 189644e90a17f1bc3dcc2b7b018eb02abb3a1c2022bRafael Espindola const CXXRecordDecl *Record = T->getAsCXXRecordDecl(); 190644e90a17f1bc3dcc2b7b018eb02abb3a1c2022bRafael Espindola if (!Record) 191644e90a17f1bc3dcc2b7b018eb02abb3a1c2022bRafael Espindola return ScopePair(diag::note_protected_by_variable_init, 0); 192644e90a17f1bc3dcc2b7b018eb02abb3a1c2022bRafael Espindola 193644e90a17f1bc3dcc2b7b018eb02abb3a1c2022bRafael Espindola // If we need to call a non trivial destructor for this variable, 194644e90a17f1bc3dcc2b7b018eb02abb3a1c2022bRafael Espindola // record an out diagnostic. 195644e90a17f1bc3dcc2b7b018eb02abb3a1c2022bRafael Espindola unsigned OutDiag = 0; 196644e90a17f1bc3dcc2b7b018eb02abb3a1c2022bRafael Espindola if (!Init->isGLValue() && !Record->hasTrivialDestructor()) 197644e90a17f1bc3dcc2b7b018eb02abb3a1c2022bRafael Espindola OutDiag = diag::note_exits_dtor; 198644e90a17f1bc3dcc2b7b018eb02abb3a1c2022bRafael Espindola 199644e90a17f1bc3dcc2b7b018eb02abb3a1c2022bRafael Espindola if (const CXXConstructExpr *cce = dyn_cast<CXXConstructExpr>(Init)) { 200644e90a17f1bc3dcc2b7b018eb02abb3a1c2022bRafael Espindola const CXXConstructorDecl *ctor = cce->getConstructor(); 201644e90a17f1bc3dcc2b7b018eb02abb3a1c2022bRafael Espindola if (ctor->isTrivial() && ctor->isDefaultConstructor()) { 202644e90a17f1bc3dcc2b7b018eb02abb3a1c2022bRafael Espindola if (OutDiag) 203644e90a17f1bc3dcc2b7b018eb02abb3a1c2022bRafael Espindola InDiag = diag::note_protected_by_variable_nontriv_destructor; 204644e90a17f1bc3dcc2b7b018eb02abb3a1c2022bRafael Espindola else if (!Record->isPOD()) 205644e90a17f1bc3dcc2b7b018eb02abb3a1c2022bRafael Espindola InDiag = diag::note_protected_by_variable_non_pod; 206644e90a17f1bc3dcc2b7b018eb02abb3a1c2022bRafael Espindola return ScopePair(InDiag, OutDiag); 207e413516d0ed61ef9e2ff706bcc00480adca947c4Douglas Gregor } 208a92afb611c015196541edd53347aa23dba1949acRafael Espindola } 209644e90a17f1bc3dcc2b7b018eb02abb3a1c2022bRafael Espindola 210644e90a17f1bc3dcc2b7b018eb02abb3a1c2022bRafael Espindola return ScopePair(diag::note_protected_by_variable_init, OutDiag); 211a92afb611c015196541edd53347aa23dba1949acRafael Espindola } 212644e90a17f1bc3dcc2b7b018eb02abb3a1c2022bRafael Espindola 213644e90a17f1bc3dcc2b7b018eb02abb3a1c2022bRafael Espindola return ScopePair(InDiag, 0); 214ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall } 215ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall 216ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall if (const TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) { 2175af280ce21af061f96b5b5b752746871e364ba99Chris Lattner if (TD->getUnderlyingType()->isVariablyModifiedType()) 218f85e193739c953358c865005855253af4f68a497John McCall return ScopePair(diag::note_protected_by_vla_typedef, 0); 2195af280ce21af061f96b5b5b752746871e364ba99Chris Lattner } 2201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 221162e1c1b487352434552147967c3dd296ebee2f7Richard Smith if (const TypeAliasDecl *TD = dyn_cast<TypeAliasDecl>(D)) { 222162e1c1b487352434552147967c3dd296ebee2f7Richard Smith if (TD->getUnderlyingType()->isVariablyModifiedType()) 223f85e193739c953358c865005855253af4f68a497John McCall return ScopePair(diag::note_protected_by_vla_type_alias, 0); 224162e1c1b487352434552147967c3dd296ebee2f7Richard Smith } 225162e1c1b487352434552147967c3dd296ebee2f7Richard Smith 226f85e193739c953358c865005855253af4f68a497John McCall return ScopePair(0U, 0U); 2275af280ce21af061f96b5b5b752746871e364ba99Chris Lattner} 2285af280ce21af061f96b5b5b752746871e364ba99Chris Lattner 22943dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor/// \brief Build scope information for a declaration that is part of a DeclStmt. 23043dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregorvoid JumpScopeChecker::BuildScopeInformation(Decl *D, unsigned &ParentScope) { 23143dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor // If this decl causes a new scope, push and switch to it. 232f85e193739c953358c865005855253af4f68a497John McCall std::pair<unsigned,unsigned> Diags = GetDiagForGotoScopeDecl(S.Context, D); 23343dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor if (Diags.first || Diags.second) { 23443dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor Scopes.push_back(GotoScope(ParentScope, Diags.first, Diags.second, 23543dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor D->getLocation())); 23643dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor ParentScope = Scopes.size()-1; 23743dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor } 23843dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor 23943dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor // If the decl has an initializer, walk it with the potentially new 24043dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor // scope we just installed. 24143dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor if (VarDecl *VD = dyn_cast<VarDecl>(D)) 24243dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor if (Expr *Init = VD->getInit()) 24343dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor BuildScopeInformation(Init, ParentScope); 24443dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor} 2455af280ce21af061f96b5b5b752746871e364ba99Chris Lattner 2464e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian/// \brief Build scope information for a captured block literal variables. 2474e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanianvoid JumpScopeChecker::BuildScopeInformation(VarDecl *D, 2484e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian const BlockDecl *BDecl, 2494e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian unsigned &ParentScope) { 2504e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian // exclude captured __block variables; there's no destructor 2514e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian // associated with the block literal for them. 2524e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian if (D->hasAttr<BlocksAttr>()) 2534e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian return; 2544e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian QualType T = D->getType(); 2554e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian QualType::DestructionKind destructKind = T.isDestructedType(); 2564e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian if (destructKind != QualType::DK_none) { 2574e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian std::pair<unsigned,unsigned> Diags; 2584e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian switch (destructKind) { 2594e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian case QualType::DK_cxx_destructor: 2604e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian Diags = ScopePair(diag::note_enters_block_captures_cxx_obj, 2614e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian diag::note_exits_block_captures_cxx_obj); 2624e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian break; 2634e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian case QualType::DK_objc_strong_lifetime: 2644e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian Diags = ScopePair(diag::note_enters_block_captures_strong, 2654e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian diag::note_exits_block_captures_strong); 2664e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian break; 2674e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian case QualType::DK_objc_weak_lifetime: 2684e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian Diags = ScopePair(diag::note_enters_block_captures_weak, 2694e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian diag::note_exits_block_captures_weak); 2704e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian break; 2714e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian case QualType::DK_none: 2720e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith llvm_unreachable("non-lifetime captured variable"); 2734e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian } 2744e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian SourceLocation Loc = D->getLocation(); 2754e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian if (Loc.isInvalid()) 2764e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian Loc = BDecl->getLocation(); 2774e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian Scopes.push_back(GotoScope(ParentScope, 2784e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian Diags.first, Diags.second, Loc)); 2794e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian ParentScope = Scopes.size()-1; 2804e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian } 2814e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian} 2824e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian 2835af280ce21af061f96b5b5b752746871e364ba99Chris Lattner/// BuildScopeInformation - The statements from CI to CE are known to form a 2845af280ce21af061f96b5b5b752746871e364ba99Chris Lattner/// coherent VLA scope with a specified parent node. Walk through the 2855af280ce21af061f96b5b5b752746871e364ba99Chris Lattner/// statements, adding any labels or gotos to LabelAndGotoScopes and recursively 2865af280ce21af061f96b5b5b752746871e364ba99Chris Lattner/// walking the AST as needed. 2874e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanianvoid JumpScopeChecker::BuildScopeInformation(Stmt *S, unsigned &origParentScope) { 2884e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian // If this is a statement, rather than an expression, scopes within it don't 2894e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian // propagate out into the enclosing scope. Otherwise we have to worry 2904e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian // about block literals, which have the lifetime of their enclosing statement. 2914e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian unsigned independentParentScope = origParentScope; 2924e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian unsigned &ParentScope = ((isa<Expr>(S) && !isa<StmtExpr>(S)) 2934e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian ? origParentScope : independentParentScope); 2944e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian 29543dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor bool SkipFirstSubStmt = false; 29643dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor 2975af280ce21af061f96b5b5b752746871e364ba99Chris Lattner // If we found a label, remember that it is in ParentScope scope. 298ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall switch (S->getStmtClass()) { 299ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall case Stmt::AddrLabelExprClass: 300ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall IndirectJumpTargets.push_back(cast<AddrLabelExpr>(S)->getLabel()); 301ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall break; 302ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall 303ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall case Stmt::IndirectGotoStmtClass: 30495c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall // "goto *&&lbl;" is a special case which we treat as equivalent 30595c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall // to a normal goto. In addition, we don't calculate scope in the 30695c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall // operand (to avoid recording the address-of-label use), which 30795c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall // works only because of the restricted set of expressions which 30895c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall // we detect as constant targets. 30995c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall if (cast<IndirectGotoStmt>(S)->getConstantTarget()) { 31095c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall LabelAndGotoScopes[S] = ParentScope; 31195c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall Jumps.push_back(S); 31295c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall return; 31395c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall } 31495c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall 3155af280ce21af061f96b5b5b752746871e364ba99Chris Lattner LabelAndGotoScopes[S] = ParentScope; 316ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall IndirectJumps.push_back(cast<IndirectGotoStmt>(S)); 317ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall break; 318ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall 319ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall case Stmt::SwitchStmtClass: 32043dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor // Evaluate the condition variable before entering the scope of the switch 32143dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor // statement. 32243dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor if (VarDecl *Var = cast<SwitchStmt>(S)->getConditionVariable()) { 32343dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor BuildScopeInformation(Var, ParentScope); 32443dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor SkipFirstSubStmt = true; 32543dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor } 32643dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor // Fall through 32743dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor 32843dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor case Stmt::GotoStmtClass: 3295af280ce21af061f96b5b5b752746871e364ba99Chris Lattner // Remember both what scope a goto is in as well as the fact that we have 3305af280ce21af061f96b5b5b752746871e364ba99Chris Lattner // it. This makes the second scan not have to walk the AST again. 3315af280ce21af061f96b5b5b752746871e364ba99Chris Lattner LabelAndGotoScopes[S] = ParentScope; 3325af280ce21af061f96b5b5b752746871e364ba99Chris Lattner Jumps.push_back(S); 333ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall break; 334ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall 335889b99e6d5b389c0ed99e7ad8470c28b146a42d3Eli Friedman case Stmt::CXXTryStmtClass: { 336889b99e6d5b389c0ed99e7ad8470c28b146a42d3Eli Friedman CXXTryStmt *TS = cast<CXXTryStmt>(S); 337889b99e6d5b389c0ed99e7ad8470c28b146a42d3Eli Friedman unsigned newParentScope; 338889b99e6d5b389c0ed99e7ad8470c28b146a42d3Eli Friedman Scopes.push_back(GotoScope(ParentScope, 339889b99e6d5b389c0ed99e7ad8470c28b146a42d3Eli Friedman diag::note_protected_by_cxx_try, 340889b99e6d5b389c0ed99e7ad8470c28b146a42d3Eli Friedman diag::note_exits_cxx_try, 341889b99e6d5b389c0ed99e7ad8470c28b146a42d3Eli Friedman TS->getSourceRange().getBegin())); 342889b99e6d5b389c0ed99e7ad8470c28b146a42d3Eli Friedman if (Stmt *TryBlock = TS->getTryBlock()) 343889b99e6d5b389c0ed99e7ad8470c28b146a42d3Eli Friedman BuildScopeInformation(TryBlock, (newParentScope = Scopes.size()-1)); 344889b99e6d5b389c0ed99e7ad8470c28b146a42d3Eli Friedman 345889b99e6d5b389c0ed99e7ad8470c28b146a42d3Eli Friedman // Jump from the catch into the try is not allowed either. 346889b99e6d5b389c0ed99e7ad8470c28b146a42d3Eli Friedman for (unsigned I = 0, E = TS->getNumHandlers(); I != E; ++I) { 347889b99e6d5b389c0ed99e7ad8470c28b146a42d3Eli Friedman CXXCatchStmt *CS = TS->getHandler(I); 348889b99e6d5b389c0ed99e7ad8470c28b146a42d3Eli Friedman Scopes.push_back(GotoScope(ParentScope, 349889b99e6d5b389c0ed99e7ad8470c28b146a42d3Eli Friedman diag::note_protected_by_cxx_catch, 350889b99e6d5b389c0ed99e7ad8470c28b146a42d3Eli Friedman diag::note_exits_cxx_catch, 351889b99e6d5b389c0ed99e7ad8470c28b146a42d3Eli Friedman CS->getSourceRange().getBegin())); 352889b99e6d5b389c0ed99e7ad8470c28b146a42d3Eli Friedman BuildScopeInformation(CS->getHandlerBlock(), 353889b99e6d5b389c0ed99e7ad8470c28b146a42d3Eli Friedman (newParentScope = Scopes.size()-1)); 354889b99e6d5b389c0ed99e7ad8470c28b146a42d3Eli Friedman } 355889b99e6d5b389c0ed99e7ad8470c28b146a42d3Eli Friedman return; 356889b99e6d5b389c0ed99e7ad8470c28b146a42d3Eli Friedman } 357889b99e6d5b389c0ed99e7ad8470c28b146a42d3Eli Friedman 358ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall default: 359ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall break; 3605af280ce21af061f96b5b5b752746871e364ba99Chris Lattner } 3611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 3627502c1d3ce8bb97bcc4f7bebef507040bd93b26fJohn McCall for (Stmt::child_range CI = S->children(); CI; ++CI) { 36343dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor if (SkipFirstSubStmt) { 36443dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor SkipFirstSubStmt = false; 36543dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor continue; 36643dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor } 36743dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor 3685af280ce21af061f96b5b5b752746871e364ba99Chris Lattner Stmt *SubStmt = *CI; 3695af280ce21af061f96b5b5b752746871e364ba99Chris Lattner if (SubStmt == 0) continue; 3701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 37197ba481f3f45e5b63b4a354bfb471ce146b7de57John McCall // Cases, labels, and defaults aren't "scope parents". It's also 37297ba481f3f45e5b63b4a354bfb471ce146b7de57John McCall // important to handle these iteratively instead of recursively in 37397ba481f3f45e5b63b4a354bfb471ce146b7de57John McCall // order to avoid blowing out the stack. 37497ba481f3f45e5b63b4a354bfb471ce146b7de57John McCall while (true) { 37597ba481f3f45e5b63b4a354bfb471ce146b7de57John McCall Stmt *Next; 376ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner if (CaseStmt *CS = dyn_cast<CaseStmt>(SubStmt)) 377ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner Next = CS->getSubStmt(); 378ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner else if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SubStmt)) 379ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner Next = DS->getSubStmt(); 380ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner else if (LabelStmt *LS = dyn_cast<LabelStmt>(SubStmt)) 381ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner Next = LS->getSubStmt(); 38297ba481f3f45e5b63b4a354bfb471ce146b7de57John McCall else 38397ba481f3f45e5b63b4a354bfb471ce146b7de57John McCall break; 38497ba481f3f45e5b63b4a354bfb471ce146b7de57John McCall 38597ba481f3f45e5b63b4a354bfb471ce146b7de57John McCall LabelAndGotoScopes[SubStmt] = ParentScope; 38697ba481f3f45e5b63b4a354bfb471ce146b7de57John McCall SubStmt = Next; 38797ba481f3f45e5b63b4a354bfb471ce146b7de57John McCall } 38897ba481f3f45e5b63b4a354bfb471ce146b7de57John McCall 3895af280ce21af061f96b5b5b752746871e364ba99Chris Lattner // If this is a declstmt with a VLA definition, it defines a scope from here 3905af280ce21af061f96b5b5b752746871e364ba99Chris Lattner // to the end of the containing context. 3915af280ce21af061f96b5b5b752746871e364ba99Chris Lattner if (DeclStmt *DS = dyn_cast<DeclStmt>(SubStmt)) { 3926d97e5e4b7abdae710c2548b51f4ed0298e86d80Chris Lattner // The decl statement creates a scope if any of the decls in it are VLAs 3936d97e5e4b7abdae710c2548b51f4ed0298e86d80Chris Lattner // or have the cleanup attribute. 3945af280ce21af061f96b5b5b752746871e364ba99Chris Lattner for (DeclStmt::decl_iterator I = DS->decl_begin(), E = DS->decl_end(); 39543dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor I != E; ++I) 39643dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor BuildScopeInformation(*I, ParentScope); 3975af280ce21af061f96b5b5b752746871e364ba99Chris Lattner continue; 3985af280ce21af061f96b5b5b752746871e364ba99Chris Lattner } 3995af280ce21af061f96b5b5b752746871e364ba99Chris Lattner // Disallow jumps into any part of an @try statement by pushing a scope and 4005af280ce21af061f96b5b5b752746871e364ba99Chris Lattner // walking all sub-stmts in that scope. 4015af280ce21af061f96b5b5b752746871e364ba99Chris Lattner if (ObjCAtTryStmt *AT = dyn_cast<ObjCAtTryStmt>(SubStmt)) { 4024e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian unsigned newParentScope; 4035af280ce21af061f96b5b5b752746871e364ba99Chris Lattner // Recursively walk the AST for the @try part. 404ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall Scopes.push_back(GotoScope(ParentScope, 405ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall diag::note_protected_by_objc_try, 406ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall diag::note_exits_objc_try, 4075af280ce21af061f96b5b5b752746871e364ba99Chris Lattner AT->getAtTryLoc())); 4085af280ce21af061f96b5b5b752746871e364ba99Chris Lattner if (Stmt *TryPart = AT->getTryBody()) 4094e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian BuildScopeInformation(TryPart, (newParentScope = Scopes.size()-1)); 4105af280ce21af061f96b5b5b752746871e364ba99Chris Lattner 4115af280ce21af061f96b5b5b752746871e364ba99Chris Lattner // Jump from the catch to the finally or try is not valid. 4128f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor for (unsigned I = 0, N = AT->getNumCatchStmts(); I != N; ++I) { 4138f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor ObjCAtCatchStmt *AC = AT->getCatchStmt(I); 4145af280ce21af061f96b5b5b752746871e364ba99Chris Lattner Scopes.push_back(GotoScope(ParentScope, 4155af280ce21af061f96b5b5b752746871e364ba99Chris Lattner diag::note_protected_by_objc_catch, 416ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall diag::note_exits_objc_catch, 4175af280ce21af061f96b5b5b752746871e364ba99Chris Lattner AC->getAtCatchLoc())); 4181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // @catches are nested and it isn't 4194e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian BuildScopeInformation(AC->getCatchBody(), 4204e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian (newParentScope = Scopes.size()-1)); 4215af280ce21af061f96b5b5b752746871e364ba99Chris Lattner } 4221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4235af280ce21af061f96b5b5b752746871e364ba99Chris Lattner // Jump from the finally to the try or catch is not valid. 4245af280ce21af061f96b5b5b752746871e364ba99Chris Lattner if (ObjCAtFinallyStmt *AF = AT->getFinallyStmt()) { 4255af280ce21af061f96b5b5b752746871e364ba99Chris Lattner Scopes.push_back(GotoScope(ParentScope, 4265af280ce21af061f96b5b5b752746871e364ba99Chris Lattner diag::note_protected_by_objc_finally, 427ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall diag::note_exits_objc_finally, 4285af280ce21af061f96b5b5b752746871e364ba99Chris Lattner AF->getAtFinallyLoc())); 4294e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian BuildScopeInformation(AF, (newParentScope = Scopes.size()-1)); 4305af280ce21af061f96b5b5b752746871e364ba99Chris Lattner } 4311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4325af280ce21af061f96b5b5b752746871e364ba99Chris Lattner continue; 4335af280ce21af061f96b5b5b752746871e364ba99Chris Lattner } 4344e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian 4354e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian unsigned newParentScope; 43646c3c4ba78766ac0f1c5ec631b424773e21f5271Chris Lattner // Disallow jumps into the protected statement of an @synchronized, but 43746c3c4ba78766ac0f1c5ec631b424773e21f5271Chris Lattner // allow jumps into the object expression it protects. 43846c3c4ba78766ac0f1c5ec631b424773e21f5271Chris Lattner if (ObjCAtSynchronizedStmt *AS = dyn_cast<ObjCAtSynchronizedStmt>(SubStmt)){ 43946c3c4ba78766ac0f1c5ec631b424773e21f5271Chris Lattner // Recursively walk the AST for the @synchronized object expr, it is 44046c3c4ba78766ac0f1c5ec631b424773e21f5271Chris Lattner // evaluated in the normal scope. 44146c3c4ba78766ac0f1c5ec631b424773e21f5271Chris Lattner BuildScopeInformation(AS->getSynchExpr(), ParentScope); 4421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 44346c3c4ba78766ac0f1c5ec631b424773e21f5271Chris Lattner // Recursively walk the AST for the @synchronized part, protected by a new 44446c3c4ba78766ac0f1c5ec631b424773e21f5271Chris Lattner // scope. 44546c3c4ba78766ac0f1c5ec631b424773e21f5271Chris Lattner Scopes.push_back(GotoScope(ParentScope, 44646c3c4ba78766ac0f1c5ec631b424773e21f5271Chris Lattner diag::note_protected_by_objc_synchronized, 447ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall diag::note_exits_objc_synchronized, 44846c3c4ba78766ac0f1c5ec631b424773e21f5271Chris Lattner AS->getAtSynchronizedLoc())); 4494e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian BuildScopeInformation(AS->getSynchBody(), 4504e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian (newParentScope = Scopes.size()-1)); 45146c3c4ba78766ac0f1c5ec631b424773e21f5271Chris Lattner continue; 45246c3c4ba78766ac0f1c5ec631b424773e21f5271Chris Lattner } 453972041f45bdf8df7ea447221292d7827466ba94bSebastian Redl 454f85e193739c953358c865005855253af4f68a497John McCall // Disallow jumps into the protected statement of an @autoreleasepool. 455f85e193739c953358c865005855253af4f68a497John McCall if (ObjCAutoreleasePoolStmt *AS = dyn_cast<ObjCAutoreleasePoolStmt>(SubStmt)){ 456f85e193739c953358c865005855253af4f68a497John McCall // Recursively walk the AST for the @autoreleasepool part, protected by a new 457f85e193739c953358c865005855253af4f68a497John McCall // scope. 458f85e193739c953358c865005855253af4f68a497John McCall Scopes.push_back(GotoScope(ParentScope, 459f85e193739c953358c865005855253af4f68a497John McCall diag::note_protected_by_objc_autoreleasepool, 460f85e193739c953358c865005855253af4f68a497John McCall diag::note_exits_objc_autoreleasepool, 461f85e193739c953358c865005855253af4f68a497John McCall AS->getAtLoc())); 4624e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian BuildScopeInformation(AS->getSubStmt(), (newParentScope = Scopes.size()-1)); 463f85e193739c953358c865005855253af4f68a497John McCall continue; 464f85e193739c953358c865005855253af4f68a497John McCall } 4659f357de8d5823f9b13cf33ad1f6af1dd69b7669fJohn McCall 4669f357de8d5823f9b13cf33ad1f6af1dd69b7669fJohn McCall // Disallow jumps past full-expressions that use blocks with 4679f357de8d5823f9b13cf33ad1f6af1dd69b7669fJohn McCall // non-trivial cleanups of their captures. This is theoretically 4689f357de8d5823f9b13cf33ad1f6af1dd69b7669fJohn McCall // implementable but a lot of work which we haven't felt up to doing. 4699f357de8d5823f9b13cf33ad1f6af1dd69b7669fJohn McCall if (ExprWithCleanups *EWC = dyn_cast<ExprWithCleanups>(SubStmt)) { 4709f357de8d5823f9b13cf33ad1f6af1dd69b7669fJohn McCall for (unsigned i = 0, e = EWC->getNumObjects(); i != e; ++i) { 4719f357de8d5823f9b13cf33ad1f6af1dd69b7669fJohn McCall const BlockDecl *BDecl = EWC->getObject(i); 4724e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian for (BlockDecl::capture_const_iterator ci = BDecl->capture_begin(), 4734e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian ce = BDecl->capture_end(); ci != ce; ++ci) { 4744e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian VarDecl *variable = ci->getVariable(); 4754e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian BuildScopeInformation(variable, BDecl, ParentScope); 4764e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian } 4779f357de8d5823f9b13cf33ad1f6af1dd69b7669fJohn McCall } 4784e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian } 4794e7c7f2b78ac3930e45f00626ef6acf08b3f80caFariborz Jahanian 4805af280ce21af061f96b5b5b752746871e364ba99Chris Lattner // Recursively walk the AST. 4815af280ce21af061f96b5b5b752746871e364ba99Chris Lattner BuildScopeInformation(SubStmt, ParentScope); 4825af280ce21af061f96b5b5b752746871e364ba99Chris Lattner } 4835af280ce21af061f96b5b5b752746871e364ba99Chris Lattner} 4845af280ce21af061f96b5b5b752746871e364ba99Chris Lattner 4855af280ce21af061f96b5b5b752746871e364ba99Chris Lattner/// VerifyJumps - Verify each element of the Jumps array to see if they are 4865af280ce21af061f96b5b5b752746871e364ba99Chris Lattner/// valid, emitting diagnostics if not. 4875af280ce21af061f96b5b5b752746871e364ba99Chris Lattnervoid JumpScopeChecker::VerifyJumps() { 4885af280ce21af061f96b5b5b752746871e364ba99Chris Lattner while (!Jumps.empty()) { 4895af280ce21af061f96b5b5b752746871e364ba99Chris Lattner Stmt *Jump = Jumps.pop_back_val(); 4901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // With a goto, 4925af280ce21af061f96b5b5b752746871e364ba99Chris Lattner if (GotoStmt *GS = dyn_cast<GotoStmt>(Jump)) { 493ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner CheckJump(GS, GS->getLabel()->getStmt(), GS->getGotoLoc(), 494c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet diag::err_goto_into_protected_scope, 4950e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith diag::warn_goto_into_protected_scope, 4960e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith diag::warn_cxx98_compat_goto_into_protected_scope); 4975af280ce21af061f96b5b5b752746871e364ba99Chris Lattner continue; 4985af280ce21af061f96b5b5b752746871e364ba99Chris Lattner } 4991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 50095c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall // We only get indirect gotos here when they have a constant target. 50195c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall if (IndirectGotoStmt *IGS = dyn_cast<IndirectGotoStmt>(Jump)) { 502ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner LabelDecl *Target = IGS->getConstantTarget(); 503ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner CheckJump(IGS, Target->getStmt(), IGS->getGotoLoc(), 5042e96511773c6a21291dfa5218eb8ba79f04b5318Francois Pichet diag::err_goto_into_protected_scope, 5050e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith diag::warn_goto_into_protected_scope, 5060e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith diag::warn_cxx98_compat_goto_into_protected_scope); 50795c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall continue; 50895c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall } 50995c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall 510ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall SwitchStmt *SS = cast<SwitchStmt>(Jump); 511ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall for (SwitchCase *SC = SS->getSwitchCaseList(); SC; 512ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall SC = SC->getNextSwitchCase()) { 513ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall assert(LabelAndGotoScopes.count(SC) && "Case not visited?"); 51465d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik Verbruggen SourceLocation Loc; 51565d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik Verbruggen if (CaseStmt *CS = dyn_cast<CaseStmt>(SC)) 51665d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik Verbruggen Loc = CS->getLocStart(); 51765d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik Verbruggen else if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) 51865d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik Verbruggen Loc = DS->getLocStart(); 51965d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik Verbruggen else 52065d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik Verbruggen Loc = SC->getLocStart(); 52165d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik Verbruggen CheckJump(SS, SC, Loc, diag::err_switch_into_protected_scope, 0, 5220e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith diag::warn_cxx98_compat_switch_into_protected_scope); 5235af280ce21af061f96b5b5b752746871e364ba99Chris Lattner } 524ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall } 525ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall} 526ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall 5275e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall/// VerifyIndirectJumps - Verify whether any possible indirect jump 5285e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall/// might cross a protection boundary. Unlike direct jumps, indirect 5295e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall/// jumps count cleanups as protection boundaries: since there's no 5305e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall/// way to know where the jump is going, we can't implicitly run the 5315e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall/// right cleanups the way we can with direct jumps. 5325e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall/// 5335e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall/// Thus, an indirect jump is "trivial" if it bypasses no 5345e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall/// initializations and no teardowns. More formally, an indirect jump 5355e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall/// from A to B is trivial if the path out from A to DCA(A,B) is 5365e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall/// trivial and the path in from DCA(A,B) to B is trivial, where 5375e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall/// DCA(A,B) is the deepest common ancestor of A and B. 5385e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall/// Jump-triviality is transitive but asymmetric. 539ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall/// 540ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall/// A path in is trivial if none of the entered scopes have an InDiag. 541ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall/// A path out is trivial is none of the exited scopes have an OutDiag. 5425e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall/// 5435e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall/// Under these definitions, this function checks that the indirect 5445e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall/// jump between A and B is trivial for every indirect goto statement A 5455e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall/// and every label B whose address was taken in the function. 546ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCallvoid JumpScopeChecker::VerifyIndirectJumps() { 547ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall if (IndirectJumps.empty()) return; 548ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall 549ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall // If there aren't any address-of-label expressions in this function, 550ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall // complain about the first indirect goto. 551ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall if (IndirectJumpTargets.empty()) { 552ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall S.Diag(IndirectJumps[0]->getGotoLoc(), 553ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall diag::err_indirect_goto_without_addrlabel); 554ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall return; 555ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall } 556ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall 5575e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall // Collect a single representative of every scope containing an 5585e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall // indirect goto. For most code bases, this substantially cuts 5595e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall // down on the number of jump sites we'll have to consider later. 560ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall typedef std::pair<unsigned, IndirectGotoStmt*> JumpScope; 5615f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVector<JumpScope, 32> JumpScopes; 562ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall { 563ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall llvm::DenseMap<unsigned, IndirectGotoStmt*> JumpScopesMap; 5645f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner for (SmallVectorImpl<IndirectGotoStmt*>::iterator 565ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall I = IndirectJumps.begin(), E = IndirectJumps.end(); I != E; ++I) { 566ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall IndirectGotoStmt *IG = *I; 567ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall assert(LabelAndGotoScopes.count(IG) && 568ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall "indirect jump didn't get added to scopes?"); 569ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall unsigned IGScope = LabelAndGotoScopes[IG]; 570ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall IndirectGotoStmt *&Entry = JumpScopesMap[IGScope]; 571ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall if (!Entry) Entry = IG; 572ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall } 573ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall JumpScopes.reserve(JumpScopesMap.size()); 574ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall for (llvm::DenseMap<unsigned, IndirectGotoStmt*>::iterator 575ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall I = JumpScopesMap.begin(), E = JumpScopesMap.end(); I != E; ++I) 576ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall JumpScopes.push_back(*I); 577ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall } 578ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall 5795e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall // Collect a single representative of every scope containing a 5805e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall // label whose address was taken somewhere in the function. 5815e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall // For most code bases, there will be only one such scope. 582ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner llvm::DenseMap<unsigned, LabelDecl*> TargetScopes; 5835f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner for (SmallVectorImpl<LabelDecl*>::iterator 584ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall I = IndirectJumpTargets.begin(), E = IndirectJumpTargets.end(); 585ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall I != E; ++I) { 586ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner LabelDecl *TheLabel = *I; 587ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner assert(LabelAndGotoScopes.count(TheLabel->getStmt()) && 588ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall "Referenced label didn't get added to scopes?"); 589ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner unsigned LabelScope = LabelAndGotoScopes[TheLabel->getStmt()]; 590ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner LabelDecl *&Target = TargetScopes[LabelScope]; 591ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall if (!Target) Target = TheLabel; 592ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall } 593ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall 5945e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall // For each target scope, make sure it's trivially reachable from 5955e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall // every scope containing a jump site. 5965e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall // 5975e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall // A path between scopes always consists of exitting zero or more 5985e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall // scopes, then entering zero or more scopes. We build a set of 5995e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall // of scopes S from which the target scope can be trivially 6005e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall // entered, then verify that every jump scope can be trivially 6015e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall // exitted to reach a scope in S. 602ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall llvm::BitVector Reachable(Scopes.size(), false); 603ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner for (llvm::DenseMap<unsigned,LabelDecl*>::iterator 604ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall TI = TargetScopes.begin(), TE = TargetScopes.end(); TI != TE; ++TI) { 605ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall unsigned TargetScope = TI->first; 606ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner LabelDecl *TargetLabel = TI->second; 607ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall 608ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall Reachable.reset(); 609ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall 610ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall // Mark all the enclosing scopes from which you can safely jump 6115e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall // into the target scope. 'Min' will end up being the index of 6125e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall // the shallowest such scope. 613ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall unsigned Min = TargetScope; 614ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall while (true) { 615ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall Reachable.set(Min); 616ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall 617ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall // Don't go beyond the outermost scope. 618ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall if (Min == 0) break; 619ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall 6205e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall // Stop if we can't trivially enter the current scope. 621ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall if (Scopes[Min].InDiag) break; 6225af280ce21af061f96b5b5b752746871e364ba99Chris Lattner 623ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall Min = Scopes[Min].ParentScope; 6245af280ce21af061f96b5b5b752746871e364ba99Chris Lattner } 6255af280ce21af061f96b5b5b752746871e364ba99Chris Lattner 626ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall // Walk through all the jump sites, checking that they can trivially 627ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall // reach this label scope. 6285f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner for (SmallVectorImpl<JumpScope>::iterator 629ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall I = JumpScopes.begin(), E = JumpScopes.end(); I != E; ++I) { 630ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall unsigned Scope = I->first; 631ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall 632ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall // Walk out the "scope chain" for this scope, looking for a scope 6335e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall // we've marked reachable. For well-formed code this amortizes 6345e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall // to O(JumpScopes.size() / Scopes.size()): we only iterate 6355e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall // when we see something unmarked, and in well-formed code we 6365e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall // mark everything we iterate past. 637ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall bool IsReachable = false; 638ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall while (true) { 639ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall if (Reachable.test(Scope)) { 640ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall // If we find something reachable, mark all the scopes we just 641ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall // walked through as reachable. 642ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall for (unsigned S = I->first; S != Scope; S = Scopes[S].ParentScope) 643ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall Reachable.set(S); 644ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall IsReachable = true; 645ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall break; 646ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall } 647ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall 648ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall // Don't walk out if we've reached the top-level scope or we've 649ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall // gotten shallower than the shallowest reachable scope. 650ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall if (Scope == 0 || Scope < Min) break; 651ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall 652ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall // Don't walk out through an out-diagnostic. 653ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall if (Scopes[Scope].OutDiag) break; 654ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall 655ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall Scope = Scopes[Scope].ParentScope; 656ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall } 657ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall 658ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall // Only diagnose if we didn't find something. 659ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall if (IsReachable) continue; 660ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall 661ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall DiagnoseIndirectJump(I->second, I->first, TargetLabel, TargetScope); 6625af280ce21af061f96b5b5b752746871e364ba99Chris Lattner } 6635af280ce21af061f96b5b5b752746871e364ba99Chris Lattner } 6645af280ce21af061f96b5b5b752746871e364ba99Chris Lattner} 6655af280ce21af061f96b5b5b752746871e364ba99Chris Lattner 6660e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith/// Return true if a particular error+note combination must be downgraded to a 6670e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith/// warning in Microsoft mode. 6680e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smithstatic bool IsMicrosoftJumpWarning(unsigned JumpDiag, unsigned InDiagNote) { 6690e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith return (JumpDiag == diag::err_goto_into_protected_scope && 6700e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith (InDiagNote == diag::note_protected_by_variable_init || 6710e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith InDiagNote == diag::note_protected_by_variable_nontriv_destructor)); 6720e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith} 6730e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith 6740e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith/// Return true if a particular note should be downgraded to a compatibility 6750e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith/// warning in C++11 mode. 6760e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smithstatic bool IsCXX98CompatWarning(Sema &S, unsigned InDiagNote) { 67780ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith return S.getLangOpts().CPlusPlus11 && 6780e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith InDiagNote == diag::note_protected_by_variable_non_pod; 6790e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith} 6800e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith 6810e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith/// Produce primary diagnostic for an indirect jump statement. 6820e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smithstatic void DiagnoseIndirectJumpStmt(Sema &S, IndirectGotoStmt *Jump, 6830e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith LabelDecl *Target, bool &Diagnosed) { 6840e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith if (Diagnosed) 6850e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith return; 6860e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith S.Diag(Jump->getGotoLoc(), diag::err_indirect_goto_in_protected_scope); 6870e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith S.Diag(Target->getStmt()->getIdentLoc(), diag::note_indirect_goto_target); 6880e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith Diagnosed = true; 6890e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith} 6900e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith 6910e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith/// Produce note diagnostics for a jump into a protected scope. 6924fe5be028b723fedc28bb33be96cde1ab2574ee6Bill Wendlingvoid JumpScopeChecker::NoteJumpIntoScopes(ArrayRef<unsigned> ToScopes) { 6930e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith assert(!ToScopes.empty()); 6940e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith for (unsigned I = 0, E = ToScopes.size(); I != E; ++I) 6950e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith if (Scopes[ToScopes[I]].InDiag) 6960e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith S.Diag(Scopes[ToScopes[I]].Loc, Scopes[ToScopes[I]].InDiag); 6970e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith} 6980e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith 6995e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall/// Diagnose an indirect jump which is known to cross scopes. 700ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCallvoid JumpScopeChecker::DiagnoseIndirectJump(IndirectGotoStmt *Jump, 701ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall unsigned JumpScope, 702ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner LabelDecl *Target, 703ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall unsigned TargetScope) { 704ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall assert(JumpScope != TargetScope); 705ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall 7065e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall unsigned Common = GetDeepestCommonScope(JumpScope, TargetScope); 7070e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith bool Diagnosed = false; 7085e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall 7095e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall // Walk out the scope chain until we reach the common ancestor. 7105e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall for (unsigned I = JumpScope; I != Common; I = Scopes[I].ParentScope) 7110e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith if (Scopes[I].OutDiag) { 7120e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith DiagnoseIndirectJumpStmt(S, Jump, Target, Diagnosed); 7135e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall S.Diag(Scopes[I].Loc, Scopes[I].OutDiag); 7140e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith } 7150e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith 7160e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith SmallVector<unsigned, 10> ToScopesCXX98Compat; 717ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall 718ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall // Now walk into the scopes containing the label whose address was taken. 7195e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall for (unsigned I = TargetScope; I != Common; I = Scopes[I].ParentScope) 7200e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith if (IsCXX98CompatWarning(S, Scopes[I].InDiag)) 7210e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith ToScopesCXX98Compat.push_back(I); 7220e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith else if (Scopes[I].InDiag) { 7230e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith DiagnoseIndirectJumpStmt(S, Jump, Target, Diagnosed); 7245e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall S.Diag(Scopes[I].Loc, Scopes[I].InDiag); 7250e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith } 726ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall 7270e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith // Diagnose this jump if it would be ill-formed in C++98. 7280e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith if (!Diagnosed && !ToScopesCXX98Compat.empty()) { 7290e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith S.Diag(Jump->getGotoLoc(), 7300e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith diag::warn_cxx98_compat_indirect_goto_in_protected_scope); 7310e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith S.Diag(Target->getStmt()->getIdentLoc(), diag::note_indirect_goto_target); 7320e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith NoteJumpIntoScopes(ToScopesCXX98Compat); 7330e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith } 734c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet} 735c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet 7365af280ce21af061f96b5b5b752746871e364ba99Chris Lattner/// CheckJump - Validate that the specified jump statement is valid: that it is 7375af280ce21af061f96b5b5b752746871e364ba99Chris Lattner/// jumping within or out of its current scope, not into a deeper one. 738c985b88efc9188845952ab524fe6d4717705257bFrancois Pichetvoid JumpScopeChecker::CheckJump(Stmt *From, Stmt *To, SourceLocation DiagLoc, 7390e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith unsigned JumpDiagError, unsigned JumpDiagWarning, 7400e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith unsigned JumpDiagCXX98Compat) { 7415af280ce21af061f96b5b5b752746871e364ba99Chris Lattner assert(LabelAndGotoScopes.count(From) && "Jump didn't get added to scopes?"); 7425af280ce21af061f96b5b5b752746871e364ba99Chris Lattner unsigned FromScope = LabelAndGotoScopes[From]; 7435af280ce21af061f96b5b5b752746871e364ba99Chris Lattner 7445af280ce21af061f96b5b5b752746871e364ba99Chris Lattner assert(LabelAndGotoScopes.count(To) && "Jump didn't get added to scopes?"); 7455af280ce21af061f96b5b5b752746871e364ba99Chris Lattner unsigned ToScope = LabelAndGotoScopes[To]; 7461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 7475af280ce21af061f96b5b5b752746871e364ba99Chris Lattner // Common case: exactly the same scope, which is fine. 7485af280ce21af061f96b5b5b752746871e364ba99Chris Lattner if (FromScope == ToScope) return; 7491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 7505e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall unsigned CommonScope = GetDeepestCommonScope(FromScope, ToScope); 7511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 7525e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall // It's okay to jump out from a nested scope. 7535e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall if (CommonScope == ToScope) return; 7541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 7555e2a7acbdc9e4aee4da54e1e2f560a78bf6fb3c1John McCall // Pull out (and reverse) any scopes we might need to diagnose skipping. 7560e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith SmallVector<unsigned, 10> ToScopesCXX98Compat; 757c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet SmallVector<unsigned, 10> ToScopesError; 758c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet SmallVector<unsigned, 10> ToScopesWarning; 759c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet for (unsigned I = ToScope; I != CommonScope; I = Scopes[I].ParentScope) { 7604e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (S.getLangOpts().MicrosoftMode && JumpDiagWarning != 0 && 761c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet IsMicrosoftJumpWarning(JumpDiagError, Scopes[I].InDiag)) 762c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet ToScopesWarning.push_back(I); 7630e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith else if (IsCXX98CompatWarning(S, Scopes[I].InDiag)) 7640e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith ToScopesCXX98Compat.push_back(I); 765c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet else if (Scopes[I].InDiag) 766c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet ToScopesError.push_back(I); 767c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet } 768ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall 769c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet // Handle warnings. 770c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet if (!ToScopesWarning.empty()) { 771c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet S.Diag(DiagLoc, JumpDiagWarning); 7720e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith NoteJumpIntoScopes(ToScopesWarning); 773c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet } 774ddb0b4d5391d3e6bc9dcf93dc42310b20c96b6fcJohn McCall 775c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet // Handle errors. 776c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet if (!ToScopesError.empty()) { 777c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet S.Diag(DiagLoc, JumpDiagError); 7780e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith NoteJumpIntoScopes(ToScopesError); 7790e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith } 7800e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith 7810e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith // Handle -Wc++98-compat warnings if the jump is well-formed. 7820e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith if (ToScopesError.empty() && !ToScopesCXX98Compat.empty()) { 7830e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith S.Diag(DiagLoc, JumpDiagCXX98Compat); 7840e9e9814a7f8313c0c02b6afea71f0e4c411873eRichard Smith NoteJumpIntoScopes(ToScopesCXX98Compat); 785c985b88efc9188845952ab524fe6d4717705257bFrancois Pichet } 7865af280ce21af061f96b5b5b752746871e364ba99Chris Lattner} 7875af280ce21af061f96b5b5b752746871e364ba99Chris Lattner 7885af280ce21af061f96b5b5b752746871e364ba99Chris Lattnervoid Sema::DiagnoseInvalidJumps(Stmt *Body) { 7896490ae5003226cae28f980648948bea8b21a8638Douglas Gregor (void)JumpScopeChecker(Body, *this); 7905af280ce21af061f96b5b5b752746871e364ba99Chris Lattner} 791