1*f4a2713aSLionel Sambuc //===--- JumpDiagnostics.cpp - Protected scope jump analysis ------*- C++ -*-=// 2*f4a2713aSLionel Sambuc // 3*f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure 4*f4a2713aSLionel Sambuc // 5*f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source 6*f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details. 7*f4a2713aSLionel Sambuc // 8*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===// 9*f4a2713aSLionel Sambuc // 10*f4a2713aSLionel Sambuc // This file implements the JumpScopeChecker class, which is used to diagnose 11*f4a2713aSLionel Sambuc // jumps that enter a protected scope in an invalid way. 12*f4a2713aSLionel Sambuc // 13*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===// 14*f4a2713aSLionel Sambuc 15*f4a2713aSLionel Sambuc #include "clang/Sema/SemaInternal.h" 16*f4a2713aSLionel Sambuc #include "clang/AST/DeclCXX.h" 17*f4a2713aSLionel Sambuc #include "clang/AST/Expr.h" 18*f4a2713aSLionel Sambuc #include "clang/AST/ExprCXX.h" 19*f4a2713aSLionel Sambuc #include "clang/AST/StmtCXX.h" 20*f4a2713aSLionel Sambuc #include "clang/AST/StmtObjC.h" 21*f4a2713aSLionel Sambuc #include "llvm/ADT/BitVector.h" 22*f4a2713aSLionel Sambuc using namespace clang; 23*f4a2713aSLionel Sambuc 24*f4a2713aSLionel Sambuc namespace { 25*f4a2713aSLionel Sambuc 26*f4a2713aSLionel Sambuc /// JumpScopeChecker - This object is used by Sema to diagnose invalid jumps 27*f4a2713aSLionel Sambuc /// into VLA and other protected scopes. For example, this rejects: 28*f4a2713aSLionel Sambuc /// goto L; 29*f4a2713aSLionel Sambuc /// int a[n]; 30*f4a2713aSLionel Sambuc /// L: 31*f4a2713aSLionel Sambuc /// 32*f4a2713aSLionel Sambuc class JumpScopeChecker { 33*f4a2713aSLionel Sambuc Sema &S; 34*f4a2713aSLionel Sambuc 35*f4a2713aSLionel Sambuc /// GotoScope - This is a record that we use to keep track of all of the 36*f4a2713aSLionel Sambuc /// scopes that are introduced by VLAs and other things that scope jumps like 37*f4a2713aSLionel Sambuc /// gotos. This scope tree has nothing to do with the source scope tree, 38*f4a2713aSLionel Sambuc /// because you can have multiple VLA scopes per compound statement, and most 39*f4a2713aSLionel Sambuc /// compound statements don't introduce any scopes. 40*f4a2713aSLionel Sambuc struct GotoScope { 41*f4a2713aSLionel Sambuc /// ParentScope - The index in ScopeMap of the parent scope. This is 0 for 42*f4a2713aSLionel Sambuc /// the parent scope is the function body. 43*f4a2713aSLionel Sambuc unsigned ParentScope; 44*f4a2713aSLionel Sambuc 45*f4a2713aSLionel Sambuc /// InDiag - The note to emit if there is a jump into this scope. 46*f4a2713aSLionel Sambuc unsigned InDiag; 47*f4a2713aSLionel Sambuc 48*f4a2713aSLionel Sambuc /// OutDiag - The note to emit if there is an indirect jump out 49*f4a2713aSLionel Sambuc /// of this scope. Direct jumps always clean up their current scope 50*f4a2713aSLionel Sambuc /// in an orderly way. 51*f4a2713aSLionel Sambuc unsigned OutDiag; 52*f4a2713aSLionel Sambuc 53*f4a2713aSLionel Sambuc /// Loc - Location to emit the diagnostic. 54*f4a2713aSLionel Sambuc SourceLocation Loc; 55*f4a2713aSLionel Sambuc 56*f4a2713aSLionel Sambuc GotoScope(unsigned parentScope, unsigned InDiag, unsigned OutDiag, 57*f4a2713aSLionel Sambuc SourceLocation L) 58*f4a2713aSLionel Sambuc : ParentScope(parentScope), InDiag(InDiag), OutDiag(OutDiag), Loc(L) {} 59*f4a2713aSLionel Sambuc }; 60*f4a2713aSLionel Sambuc 61*f4a2713aSLionel Sambuc SmallVector<GotoScope, 48> Scopes; 62*f4a2713aSLionel Sambuc llvm::DenseMap<Stmt*, unsigned> LabelAndGotoScopes; 63*f4a2713aSLionel Sambuc SmallVector<Stmt*, 16> Jumps; 64*f4a2713aSLionel Sambuc 65*f4a2713aSLionel Sambuc SmallVector<IndirectGotoStmt*, 4> IndirectJumps; 66*f4a2713aSLionel Sambuc SmallVector<LabelDecl*, 4> IndirectJumpTargets; 67*f4a2713aSLionel Sambuc public: 68*f4a2713aSLionel Sambuc JumpScopeChecker(Stmt *Body, Sema &S); 69*f4a2713aSLionel Sambuc private: 70*f4a2713aSLionel Sambuc void BuildScopeInformation(Decl *D, unsigned &ParentScope); 71*f4a2713aSLionel Sambuc void BuildScopeInformation(VarDecl *D, const BlockDecl *BDecl, 72*f4a2713aSLionel Sambuc unsigned &ParentScope); 73*f4a2713aSLionel Sambuc void BuildScopeInformation(Stmt *S, unsigned &origParentScope); 74*f4a2713aSLionel Sambuc 75*f4a2713aSLionel Sambuc void VerifyJumps(); 76*f4a2713aSLionel Sambuc void VerifyIndirectJumps(); 77*f4a2713aSLionel Sambuc void NoteJumpIntoScopes(ArrayRef<unsigned> ToScopes); 78*f4a2713aSLionel Sambuc void DiagnoseIndirectJump(IndirectGotoStmt *IG, unsigned IGScope, 79*f4a2713aSLionel Sambuc LabelDecl *Target, unsigned TargetScope); 80*f4a2713aSLionel Sambuc void CheckJump(Stmt *From, Stmt *To, SourceLocation DiagLoc, 81*f4a2713aSLionel Sambuc unsigned JumpDiag, unsigned JumpDiagWarning, 82*f4a2713aSLionel Sambuc unsigned JumpDiagCXX98Compat); 83*f4a2713aSLionel Sambuc 84*f4a2713aSLionel Sambuc unsigned GetDeepestCommonScope(unsigned A, unsigned B); 85*f4a2713aSLionel Sambuc }; 86*f4a2713aSLionel Sambuc } // end anonymous namespace 87*f4a2713aSLionel Sambuc 88*f4a2713aSLionel Sambuc 89*f4a2713aSLionel Sambuc JumpScopeChecker::JumpScopeChecker(Stmt *Body, Sema &s) : S(s) { 90*f4a2713aSLionel Sambuc // Add a scope entry for function scope. 91*f4a2713aSLionel Sambuc Scopes.push_back(GotoScope(~0U, ~0U, ~0U, SourceLocation())); 92*f4a2713aSLionel Sambuc 93*f4a2713aSLionel Sambuc // Build information for the top level compound statement, so that we have a 94*f4a2713aSLionel Sambuc // defined scope record for every "goto" and label. 95*f4a2713aSLionel Sambuc unsigned BodyParentScope = 0; 96*f4a2713aSLionel Sambuc BuildScopeInformation(Body, BodyParentScope); 97*f4a2713aSLionel Sambuc 98*f4a2713aSLionel Sambuc // Check that all jumps we saw are kosher. 99*f4a2713aSLionel Sambuc VerifyJumps(); 100*f4a2713aSLionel Sambuc VerifyIndirectJumps(); 101*f4a2713aSLionel Sambuc } 102*f4a2713aSLionel Sambuc 103*f4a2713aSLionel Sambuc /// GetDeepestCommonScope - Finds the innermost scope enclosing the 104*f4a2713aSLionel Sambuc /// two scopes. 105*f4a2713aSLionel Sambuc unsigned JumpScopeChecker::GetDeepestCommonScope(unsigned A, unsigned B) { 106*f4a2713aSLionel Sambuc while (A != B) { 107*f4a2713aSLionel Sambuc // Inner scopes are created after outer scopes and therefore have 108*f4a2713aSLionel Sambuc // higher indices. 109*f4a2713aSLionel Sambuc if (A < B) { 110*f4a2713aSLionel Sambuc assert(Scopes[B].ParentScope < B); 111*f4a2713aSLionel Sambuc B = Scopes[B].ParentScope; 112*f4a2713aSLionel Sambuc } else { 113*f4a2713aSLionel Sambuc assert(Scopes[A].ParentScope < A); 114*f4a2713aSLionel Sambuc A = Scopes[A].ParentScope; 115*f4a2713aSLionel Sambuc } 116*f4a2713aSLionel Sambuc } 117*f4a2713aSLionel Sambuc return A; 118*f4a2713aSLionel Sambuc } 119*f4a2713aSLionel Sambuc 120*f4a2713aSLionel Sambuc typedef std::pair<unsigned,unsigned> ScopePair; 121*f4a2713aSLionel Sambuc 122*f4a2713aSLionel Sambuc /// GetDiagForGotoScopeDecl - If this decl induces a new goto scope, return a 123*f4a2713aSLionel Sambuc /// diagnostic that should be emitted if control goes over it. If not, return 0. 124*f4a2713aSLionel Sambuc static ScopePair GetDiagForGotoScopeDecl(ASTContext &Context, const Decl *D) { 125*f4a2713aSLionel Sambuc if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { 126*f4a2713aSLionel Sambuc unsigned InDiag = 0; 127*f4a2713aSLionel Sambuc if (VD->getType()->isVariablyModifiedType()) 128*f4a2713aSLionel Sambuc InDiag = diag::note_protected_by_vla; 129*f4a2713aSLionel Sambuc 130*f4a2713aSLionel Sambuc if (VD->hasAttr<BlocksAttr>()) 131*f4a2713aSLionel Sambuc return ScopePair(diag::note_protected_by___block, 132*f4a2713aSLionel Sambuc diag::note_exits___block); 133*f4a2713aSLionel Sambuc 134*f4a2713aSLionel Sambuc if (VD->hasAttr<CleanupAttr>()) 135*f4a2713aSLionel Sambuc return ScopePair(diag::note_protected_by_cleanup, 136*f4a2713aSLionel Sambuc diag::note_exits_cleanup); 137*f4a2713aSLionel Sambuc 138*f4a2713aSLionel Sambuc if (Context.getLangOpts().ObjCAutoRefCount && VD->hasLocalStorage()) { 139*f4a2713aSLionel Sambuc switch (VD->getType().getObjCLifetime()) { 140*f4a2713aSLionel Sambuc case Qualifiers::OCL_None: 141*f4a2713aSLionel Sambuc case Qualifiers::OCL_ExplicitNone: 142*f4a2713aSLionel Sambuc case Qualifiers::OCL_Autoreleasing: 143*f4a2713aSLionel Sambuc break; 144*f4a2713aSLionel Sambuc 145*f4a2713aSLionel Sambuc case Qualifiers::OCL_Strong: 146*f4a2713aSLionel Sambuc case Qualifiers::OCL_Weak: 147*f4a2713aSLionel Sambuc return ScopePair(diag::note_protected_by_objc_ownership, 148*f4a2713aSLionel Sambuc diag::note_exits_objc_ownership); 149*f4a2713aSLionel Sambuc } 150*f4a2713aSLionel Sambuc } 151*f4a2713aSLionel Sambuc 152*f4a2713aSLionel Sambuc if (Context.getLangOpts().CPlusPlus && VD->hasLocalStorage()) { 153*f4a2713aSLionel Sambuc // C++11 [stmt.dcl]p3: 154*f4a2713aSLionel Sambuc // A program that jumps from a point where a variable with automatic 155*f4a2713aSLionel Sambuc // storage duration is not in scope to a point where it is in scope 156*f4a2713aSLionel Sambuc // is ill-formed unless the variable has scalar type, class type with 157*f4a2713aSLionel Sambuc // a trivial default constructor and a trivial destructor, a 158*f4a2713aSLionel Sambuc // cv-qualified version of one of these types, or an array of one of 159*f4a2713aSLionel Sambuc // the preceding types and is declared without an initializer. 160*f4a2713aSLionel Sambuc 161*f4a2713aSLionel Sambuc // C++03 [stmt.dcl.p3: 162*f4a2713aSLionel Sambuc // A program that jumps from a point where a local variable 163*f4a2713aSLionel Sambuc // with automatic storage duration is not in scope to a point 164*f4a2713aSLionel Sambuc // where it is in scope is ill-formed unless the variable has 165*f4a2713aSLionel Sambuc // POD type and is declared without an initializer. 166*f4a2713aSLionel Sambuc 167*f4a2713aSLionel Sambuc const Expr *Init = VD->getInit(); 168*f4a2713aSLionel Sambuc if (!Init) 169*f4a2713aSLionel Sambuc return ScopePair(InDiag, 0); 170*f4a2713aSLionel Sambuc 171*f4a2713aSLionel Sambuc const ExprWithCleanups *EWC = dyn_cast<ExprWithCleanups>(Init); 172*f4a2713aSLionel Sambuc if (EWC) 173*f4a2713aSLionel Sambuc Init = EWC->getSubExpr(); 174*f4a2713aSLionel Sambuc 175*f4a2713aSLionel Sambuc const MaterializeTemporaryExpr *M = NULL; 176*f4a2713aSLionel Sambuc Init = Init->findMaterializedTemporary(M); 177*f4a2713aSLionel Sambuc 178*f4a2713aSLionel Sambuc SmallVector<const Expr *, 2> CommaLHSs; 179*f4a2713aSLionel Sambuc SmallVector<SubobjectAdjustment, 2> Adjustments; 180*f4a2713aSLionel Sambuc Init = Init->skipRValueSubobjectAdjustments(CommaLHSs, Adjustments); 181*f4a2713aSLionel Sambuc 182*f4a2713aSLionel Sambuc QualType QT = Init->getType(); 183*f4a2713aSLionel Sambuc if (QT.isNull()) 184*f4a2713aSLionel Sambuc return ScopePair(diag::note_protected_by_variable_init, 0); 185*f4a2713aSLionel Sambuc 186*f4a2713aSLionel Sambuc const Type *T = QT.getTypePtr(); 187*f4a2713aSLionel Sambuc if (T->isArrayType()) 188*f4a2713aSLionel Sambuc T = T->getBaseElementTypeUnsafe(); 189*f4a2713aSLionel Sambuc 190*f4a2713aSLionel Sambuc const CXXRecordDecl *Record = T->getAsCXXRecordDecl(); 191*f4a2713aSLionel Sambuc if (!Record) 192*f4a2713aSLionel Sambuc return ScopePair(diag::note_protected_by_variable_init, 0); 193*f4a2713aSLionel Sambuc 194*f4a2713aSLionel Sambuc // If we need to call a non trivial destructor for this variable, 195*f4a2713aSLionel Sambuc // record an out diagnostic. 196*f4a2713aSLionel Sambuc unsigned OutDiag = 0; 197*f4a2713aSLionel Sambuc if (!Init->isGLValue() && !Record->hasTrivialDestructor()) 198*f4a2713aSLionel Sambuc OutDiag = diag::note_exits_dtor; 199*f4a2713aSLionel Sambuc 200*f4a2713aSLionel Sambuc if (const CXXConstructExpr *cce = dyn_cast<CXXConstructExpr>(Init)) { 201*f4a2713aSLionel Sambuc const CXXConstructorDecl *ctor = cce->getConstructor(); 202*f4a2713aSLionel Sambuc // For a variable declared without an initializer, we will have 203*f4a2713aSLionel Sambuc // call-style initialization and the initializer will be the 204*f4a2713aSLionel Sambuc // CXXConstructExpr with no intervening nodes. 205*f4a2713aSLionel Sambuc if (ctor->isTrivial() && ctor->isDefaultConstructor() && 206*f4a2713aSLionel Sambuc VD->getInit() == Init && VD->getInitStyle() == VarDecl::CallInit) { 207*f4a2713aSLionel Sambuc if (OutDiag) 208*f4a2713aSLionel Sambuc InDiag = diag::note_protected_by_variable_nontriv_destructor; 209*f4a2713aSLionel Sambuc else if (!Record->isPOD()) 210*f4a2713aSLionel Sambuc InDiag = diag::note_protected_by_variable_non_pod; 211*f4a2713aSLionel Sambuc return ScopePair(InDiag, OutDiag); 212*f4a2713aSLionel Sambuc } 213*f4a2713aSLionel Sambuc } 214*f4a2713aSLionel Sambuc 215*f4a2713aSLionel Sambuc return ScopePair(diag::note_protected_by_variable_init, OutDiag); 216*f4a2713aSLionel Sambuc } 217*f4a2713aSLionel Sambuc 218*f4a2713aSLionel Sambuc return ScopePair(InDiag, 0); 219*f4a2713aSLionel Sambuc } 220*f4a2713aSLionel Sambuc 221*f4a2713aSLionel Sambuc if (const TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) { 222*f4a2713aSLionel Sambuc if (TD->getUnderlyingType()->isVariablyModifiedType()) 223*f4a2713aSLionel Sambuc return ScopePair(diag::note_protected_by_vla_typedef, 0); 224*f4a2713aSLionel Sambuc } 225*f4a2713aSLionel Sambuc 226*f4a2713aSLionel Sambuc if (const TypeAliasDecl *TD = dyn_cast<TypeAliasDecl>(D)) { 227*f4a2713aSLionel Sambuc if (TD->getUnderlyingType()->isVariablyModifiedType()) 228*f4a2713aSLionel Sambuc return ScopePair(diag::note_protected_by_vla_type_alias, 0); 229*f4a2713aSLionel Sambuc } 230*f4a2713aSLionel Sambuc 231*f4a2713aSLionel Sambuc return ScopePair(0U, 0U); 232*f4a2713aSLionel Sambuc } 233*f4a2713aSLionel Sambuc 234*f4a2713aSLionel Sambuc /// \brief Build scope information for a declaration that is part of a DeclStmt. 235*f4a2713aSLionel Sambuc void JumpScopeChecker::BuildScopeInformation(Decl *D, unsigned &ParentScope) { 236*f4a2713aSLionel Sambuc // If this decl causes a new scope, push and switch to it. 237*f4a2713aSLionel Sambuc std::pair<unsigned,unsigned> Diags = GetDiagForGotoScopeDecl(S.Context, D); 238*f4a2713aSLionel Sambuc if (Diags.first || Diags.second) { 239*f4a2713aSLionel Sambuc Scopes.push_back(GotoScope(ParentScope, Diags.first, Diags.second, 240*f4a2713aSLionel Sambuc D->getLocation())); 241*f4a2713aSLionel Sambuc ParentScope = Scopes.size()-1; 242*f4a2713aSLionel Sambuc } 243*f4a2713aSLionel Sambuc 244*f4a2713aSLionel Sambuc // If the decl has an initializer, walk it with the potentially new 245*f4a2713aSLionel Sambuc // scope we just installed. 246*f4a2713aSLionel Sambuc if (VarDecl *VD = dyn_cast<VarDecl>(D)) 247*f4a2713aSLionel Sambuc if (Expr *Init = VD->getInit()) 248*f4a2713aSLionel Sambuc BuildScopeInformation(Init, ParentScope); 249*f4a2713aSLionel Sambuc } 250*f4a2713aSLionel Sambuc 251*f4a2713aSLionel Sambuc /// \brief Build scope information for a captured block literal variables. 252*f4a2713aSLionel Sambuc void JumpScopeChecker::BuildScopeInformation(VarDecl *D, 253*f4a2713aSLionel Sambuc const BlockDecl *BDecl, 254*f4a2713aSLionel Sambuc unsigned &ParentScope) { 255*f4a2713aSLionel Sambuc // exclude captured __block variables; there's no destructor 256*f4a2713aSLionel Sambuc // associated with the block literal for them. 257*f4a2713aSLionel Sambuc if (D->hasAttr<BlocksAttr>()) 258*f4a2713aSLionel Sambuc return; 259*f4a2713aSLionel Sambuc QualType T = D->getType(); 260*f4a2713aSLionel Sambuc QualType::DestructionKind destructKind = T.isDestructedType(); 261*f4a2713aSLionel Sambuc if (destructKind != QualType::DK_none) { 262*f4a2713aSLionel Sambuc std::pair<unsigned,unsigned> Diags; 263*f4a2713aSLionel Sambuc switch (destructKind) { 264*f4a2713aSLionel Sambuc case QualType::DK_cxx_destructor: 265*f4a2713aSLionel Sambuc Diags = ScopePair(diag::note_enters_block_captures_cxx_obj, 266*f4a2713aSLionel Sambuc diag::note_exits_block_captures_cxx_obj); 267*f4a2713aSLionel Sambuc break; 268*f4a2713aSLionel Sambuc case QualType::DK_objc_strong_lifetime: 269*f4a2713aSLionel Sambuc Diags = ScopePair(diag::note_enters_block_captures_strong, 270*f4a2713aSLionel Sambuc diag::note_exits_block_captures_strong); 271*f4a2713aSLionel Sambuc break; 272*f4a2713aSLionel Sambuc case QualType::DK_objc_weak_lifetime: 273*f4a2713aSLionel Sambuc Diags = ScopePair(diag::note_enters_block_captures_weak, 274*f4a2713aSLionel Sambuc diag::note_exits_block_captures_weak); 275*f4a2713aSLionel Sambuc break; 276*f4a2713aSLionel Sambuc case QualType::DK_none: 277*f4a2713aSLionel Sambuc llvm_unreachable("non-lifetime captured variable"); 278*f4a2713aSLionel Sambuc } 279*f4a2713aSLionel Sambuc SourceLocation Loc = D->getLocation(); 280*f4a2713aSLionel Sambuc if (Loc.isInvalid()) 281*f4a2713aSLionel Sambuc Loc = BDecl->getLocation(); 282*f4a2713aSLionel Sambuc Scopes.push_back(GotoScope(ParentScope, 283*f4a2713aSLionel Sambuc Diags.first, Diags.second, Loc)); 284*f4a2713aSLionel Sambuc ParentScope = Scopes.size()-1; 285*f4a2713aSLionel Sambuc } 286*f4a2713aSLionel Sambuc } 287*f4a2713aSLionel Sambuc 288*f4a2713aSLionel Sambuc /// BuildScopeInformation - The statements from CI to CE are known to form a 289*f4a2713aSLionel Sambuc /// coherent VLA scope with a specified parent node. Walk through the 290*f4a2713aSLionel Sambuc /// statements, adding any labels or gotos to LabelAndGotoScopes and recursively 291*f4a2713aSLionel Sambuc /// walking the AST as needed. 292*f4a2713aSLionel Sambuc void JumpScopeChecker::BuildScopeInformation(Stmt *S, unsigned &origParentScope) { 293*f4a2713aSLionel Sambuc // If this is a statement, rather than an expression, scopes within it don't 294*f4a2713aSLionel Sambuc // propagate out into the enclosing scope. Otherwise we have to worry 295*f4a2713aSLionel Sambuc // about block literals, which have the lifetime of their enclosing statement. 296*f4a2713aSLionel Sambuc unsigned independentParentScope = origParentScope; 297*f4a2713aSLionel Sambuc unsigned &ParentScope = ((isa<Expr>(S) && !isa<StmtExpr>(S)) 298*f4a2713aSLionel Sambuc ? origParentScope : independentParentScope); 299*f4a2713aSLionel Sambuc 300*f4a2713aSLionel Sambuc bool SkipFirstSubStmt = false; 301*f4a2713aSLionel Sambuc 302*f4a2713aSLionel Sambuc // If we found a label, remember that it is in ParentScope scope. 303*f4a2713aSLionel Sambuc switch (S->getStmtClass()) { 304*f4a2713aSLionel Sambuc case Stmt::AddrLabelExprClass: 305*f4a2713aSLionel Sambuc IndirectJumpTargets.push_back(cast<AddrLabelExpr>(S)->getLabel()); 306*f4a2713aSLionel Sambuc break; 307*f4a2713aSLionel Sambuc 308*f4a2713aSLionel Sambuc case Stmt::IndirectGotoStmtClass: 309*f4a2713aSLionel Sambuc // "goto *&&lbl;" is a special case which we treat as equivalent 310*f4a2713aSLionel Sambuc // to a normal goto. In addition, we don't calculate scope in the 311*f4a2713aSLionel Sambuc // operand (to avoid recording the address-of-label use), which 312*f4a2713aSLionel Sambuc // works only because of the restricted set of expressions which 313*f4a2713aSLionel Sambuc // we detect as constant targets. 314*f4a2713aSLionel Sambuc if (cast<IndirectGotoStmt>(S)->getConstantTarget()) { 315*f4a2713aSLionel Sambuc LabelAndGotoScopes[S] = ParentScope; 316*f4a2713aSLionel Sambuc Jumps.push_back(S); 317*f4a2713aSLionel Sambuc return; 318*f4a2713aSLionel Sambuc } 319*f4a2713aSLionel Sambuc 320*f4a2713aSLionel Sambuc LabelAndGotoScopes[S] = ParentScope; 321*f4a2713aSLionel Sambuc IndirectJumps.push_back(cast<IndirectGotoStmt>(S)); 322*f4a2713aSLionel Sambuc break; 323*f4a2713aSLionel Sambuc 324*f4a2713aSLionel Sambuc case Stmt::SwitchStmtClass: 325*f4a2713aSLionel Sambuc // Evaluate the condition variable before entering the scope of the switch 326*f4a2713aSLionel Sambuc // statement. 327*f4a2713aSLionel Sambuc if (VarDecl *Var = cast<SwitchStmt>(S)->getConditionVariable()) { 328*f4a2713aSLionel Sambuc BuildScopeInformation(Var, ParentScope); 329*f4a2713aSLionel Sambuc SkipFirstSubStmt = true; 330*f4a2713aSLionel Sambuc } 331*f4a2713aSLionel Sambuc // Fall through 332*f4a2713aSLionel Sambuc 333*f4a2713aSLionel Sambuc case Stmt::GotoStmtClass: 334*f4a2713aSLionel Sambuc // Remember both what scope a goto is in as well as the fact that we have 335*f4a2713aSLionel Sambuc // it. This makes the second scan not have to walk the AST again. 336*f4a2713aSLionel Sambuc LabelAndGotoScopes[S] = ParentScope; 337*f4a2713aSLionel Sambuc Jumps.push_back(S); 338*f4a2713aSLionel Sambuc break; 339*f4a2713aSLionel Sambuc 340*f4a2713aSLionel Sambuc case Stmt::CXXTryStmtClass: { 341*f4a2713aSLionel Sambuc CXXTryStmt *TS = cast<CXXTryStmt>(S); 342*f4a2713aSLionel Sambuc unsigned newParentScope; 343*f4a2713aSLionel Sambuc Scopes.push_back(GotoScope(ParentScope, 344*f4a2713aSLionel Sambuc diag::note_protected_by_cxx_try, 345*f4a2713aSLionel Sambuc diag::note_exits_cxx_try, 346*f4a2713aSLionel Sambuc TS->getSourceRange().getBegin())); 347*f4a2713aSLionel Sambuc if (Stmt *TryBlock = TS->getTryBlock()) 348*f4a2713aSLionel Sambuc BuildScopeInformation(TryBlock, (newParentScope = Scopes.size()-1)); 349*f4a2713aSLionel Sambuc 350*f4a2713aSLionel Sambuc // Jump from the catch into the try is not allowed either. 351*f4a2713aSLionel Sambuc for (unsigned I = 0, E = TS->getNumHandlers(); I != E; ++I) { 352*f4a2713aSLionel Sambuc CXXCatchStmt *CS = TS->getHandler(I); 353*f4a2713aSLionel Sambuc Scopes.push_back(GotoScope(ParentScope, 354*f4a2713aSLionel Sambuc diag::note_protected_by_cxx_catch, 355*f4a2713aSLionel Sambuc diag::note_exits_cxx_catch, 356*f4a2713aSLionel Sambuc CS->getSourceRange().getBegin())); 357*f4a2713aSLionel Sambuc BuildScopeInformation(CS->getHandlerBlock(), 358*f4a2713aSLionel Sambuc (newParentScope = Scopes.size()-1)); 359*f4a2713aSLionel Sambuc } 360*f4a2713aSLionel Sambuc return; 361*f4a2713aSLionel Sambuc } 362*f4a2713aSLionel Sambuc 363*f4a2713aSLionel Sambuc default: 364*f4a2713aSLionel Sambuc break; 365*f4a2713aSLionel Sambuc } 366*f4a2713aSLionel Sambuc 367*f4a2713aSLionel Sambuc for (Stmt::child_range CI = S->children(); CI; ++CI) { 368*f4a2713aSLionel Sambuc if (SkipFirstSubStmt) { 369*f4a2713aSLionel Sambuc SkipFirstSubStmt = false; 370*f4a2713aSLionel Sambuc continue; 371*f4a2713aSLionel Sambuc } 372*f4a2713aSLionel Sambuc 373*f4a2713aSLionel Sambuc Stmt *SubStmt = *CI; 374*f4a2713aSLionel Sambuc if (SubStmt == 0) continue; 375*f4a2713aSLionel Sambuc 376*f4a2713aSLionel Sambuc // Cases, labels, and defaults aren't "scope parents". It's also 377*f4a2713aSLionel Sambuc // important to handle these iteratively instead of recursively in 378*f4a2713aSLionel Sambuc // order to avoid blowing out the stack. 379*f4a2713aSLionel Sambuc while (true) { 380*f4a2713aSLionel Sambuc Stmt *Next; 381*f4a2713aSLionel Sambuc if (CaseStmt *CS = dyn_cast<CaseStmt>(SubStmt)) 382*f4a2713aSLionel Sambuc Next = CS->getSubStmt(); 383*f4a2713aSLionel Sambuc else if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SubStmt)) 384*f4a2713aSLionel Sambuc Next = DS->getSubStmt(); 385*f4a2713aSLionel Sambuc else if (LabelStmt *LS = dyn_cast<LabelStmt>(SubStmt)) 386*f4a2713aSLionel Sambuc Next = LS->getSubStmt(); 387*f4a2713aSLionel Sambuc else 388*f4a2713aSLionel Sambuc break; 389*f4a2713aSLionel Sambuc 390*f4a2713aSLionel Sambuc LabelAndGotoScopes[SubStmt] = ParentScope; 391*f4a2713aSLionel Sambuc SubStmt = Next; 392*f4a2713aSLionel Sambuc } 393*f4a2713aSLionel Sambuc 394*f4a2713aSLionel Sambuc // If this is a declstmt with a VLA definition, it defines a scope from here 395*f4a2713aSLionel Sambuc // to the end of the containing context. 396*f4a2713aSLionel Sambuc if (DeclStmt *DS = dyn_cast<DeclStmt>(SubStmt)) { 397*f4a2713aSLionel Sambuc // The decl statement creates a scope if any of the decls in it are VLAs 398*f4a2713aSLionel Sambuc // or have the cleanup attribute. 399*f4a2713aSLionel Sambuc for (DeclStmt::decl_iterator I = DS->decl_begin(), E = DS->decl_end(); 400*f4a2713aSLionel Sambuc I != E; ++I) 401*f4a2713aSLionel Sambuc BuildScopeInformation(*I, ParentScope); 402*f4a2713aSLionel Sambuc continue; 403*f4a2713aSLionel Sambuc } 404*f4a2713aSLionel Sambuc // Disallow jumps into any part of an @try statement by pushing a scope and 405*f4a2713aSLionel Sambuc // walking all sub-stmts in that scope. 406*f4a2713aSLionel Sambuc if (ObjCAtTryStmt *AT = dyn_cast<ObjCAtTryStmt>(SubStmt)) { 407*f4a2713aSLionel Sambuc unsigned newParentScope; 408*f4a2713aSLionel Sambuc // Recursively walk the AST for the @try part. 409*f4a2713aSLionel Sambuc Scopes.push_back(GotoScope(ParentScope, 410*f4a2713aSLionel Sambuc diag::note_protected_by_objc_try, 411*f4a2713aSLionel Sambuc diag::note_exits_objc_try, 412*f4a2713aSLionel Sambuc AT->getAtTryLoc())); 413*f4a2713aSLionel Sambuc if (Stmt *TryPart = AT->getTryBody()) 414*f4a2713aSLionel Sambuc BuildScopeInformation(TryPart, (newParentScope = Scopes.size()-1)); 415*f4a2713aSLionel Sambuc 416*f4a2713aSLionel Sambuc // Jump from the catch to the finally or try is not valid. 417*f4a2713aSLionel Sambuc for (unsigned I = 0, N = AT->getNumCatchStmts(); I != N; ++I) { 418*f4a2713aSLionel Sambuc ObjCAtCatchStmt *AC = AT->getCatchStmt(I); 419*f4a2713aSLionel Sambuc Scopes.push_back(GotoScope(ParentScope, 420*f4a2713aSLionel Sambuc diag::note_protected_by_objc_catch, 421*f4a2713aSLionel Sambuc diag::note_exits_objc_catch, 422*f4a2713aSLionel Sambuc AC->getAtCatchLoc())); 423*f4a2713aSLionel Sambuc // @catches are nested and it isn't 424*f4a2713aSLionel Sambuc BuildScopeInformation(AC->getCatchBody(), 425*f4a2713aSLionel Sambuc (newParentScope = Scopes.size()-1)); 426*f4a2713aSLionel Sambuc } 427*f4a2713aSLionel Sambuc 428*f4a2713aSLionel Sambuc // Jump from the finally to the try or catch is not valid. 429*f4a2713aSLionel Sambuc if (ObjCAtFinallyStmt *AF = AT->getFinallyStmt()) { 430*f4a2713aSLionel Sambuc Scopes.push_back(GotoScope(ParentScope, 431*f4a2713aSLionel Sambuc diag::note_protected_by_objc_finally, 432*f4a2713aSLionel Sambuc diag::note_exits_objc_finally, 433*f4a2713aSLionel Sambuc AF->getAtFinallyLoc())); 434*f4a2713aSLionel Sambuc BuildScopeInformation(AF, (newParentScope = Scopes.size()-1)); 435*f4a2713aSLionel Sambuc } 436*f4a2713aSLionel Sambuc 437*f4a2713aSLionel Sambuc continue; 438*f4a2713aSLionel Sambuc } 439*f4a2713aSLionel Sambuc 440*f4a2713aSLionel Sambuc unsigned newParentScope; 441*f4a2713aSLionel Sambuc // Disallow jumps into the protected statement of an @synchronized, but 442*f4a2713aSLionel Sambuc // allow jumps into the object expression it protects. 443*f4a2713aSLionel Sambuc if (ObjCAtSynchronizedStmt *AS = dyn_cast<ObjCAtSynchronizedStmt>(SubStmt)){ 444*f4a2713aSLionel Sambuc // Recursively walk the AST for the @synchronized object expr, it is 445*f4a2713aSLionel Sambuc // evaluated in the normal scope. 446*f4a2713aSLionel Sambuc BuildScopeInformation(AS->getSynchExpr(), ParentScope); 447*f4a2713aSLionel Sambuc 448*f4a2713aSLionel Sambuc // Recursively walk the AST for the @synchronized part, protected by a new 449*f4a2713aSLionel Sambuc // scope. 450*f4a2713aSLionel Sambuc Scopes.push_back(GotoScope(ParentScope, 451*f4a2713aSLionel Sambuc diag::note_protected_by_objc_synchronized, 452*f4a2713aSLionel Sambuc diag::note_exits_objc_synchronized, 453*f4a2713aSLionel Sambuc AS->getAtSynchronizedLoc())); 454*f4a2713aSLionel Sambuc BuildScopeInformation(AS->getSynchBody(), 455*f4a2713aSLionel Sambuc (newParentScope = Scopes.size()-1)); 456*f4a2713aSLionel Sambuc continue; 457*f4a2713aSLionel Sambuc } 458*f4a2713aSLionel Sambuc 459*f4a2713aSLionel Sambuc // Disallow jumps into the protected statement of an @autoreleasepool. 460*f4a2713aSLionel Sambuc if (ObjCAutoreleasePoolStmt *AS = dyn_cast<ObjCAutoreleasePoolStmt>(SubStmt)){ 461*f4a2713aSLionel Sambuc // Recursively walk the AST for the @autoreleasepool part, protected by a new 462*f4a2713aSLionel Sambuc // scope. 463*f4a2713aSLionel Sambuc Scopes.push_back(GotoScope(ParentScope, 464*f4a2713aSLionel Sambuc diag::note_protected_by_objc_autoreleasepool, 465*f4a2713aSLionel Sambuc diag::note_exits_objc_autoreleasepool, 466*f4a2713aSLionel Sambuc AS->getAtLoc())); 467*f4a2713aSLionel Sambuc BuildScopeInformation(AS->getSubStmt(), (newParentScope = Scopes.size()-1)); 468*f4a2713aSLionel Sambuc continue; 469*f4a2713aSLionel Sambuc } 470*f4a2713aSLionel Sambuc 471*f4a2713aSLionel Sambuc // Disallow jumps past full-expressions that use blocks with 472*f4a2713aSLionel Sambuc // non-trivial cleanups of their captures. This is theoretically 473*f4a2713aSLionel Sambuc // implementable but a lot of work which we haven't felt up to doing. 474*f4a2713aSLionel Sambuc if (ExprWithCleanups *EWC = dyn_cast<ExprWithCleanups>(SubStmt)) { 475*f4a2713aSLionel Sambuc for (unsigned i = 0, e = EWC->getNumObjects(); i != e; ++i) { 476*f4a2713aSLionel Sambuc const BlockDecl *BDecl = EWC->getObject(i); 477*f4a2713aSLionel Sambuc for (BlockDecl::capture_const_iterator ci = BDecl->capture_begin(), 478*f4a2713aSLionel Sambuc ce = BDecl->capture_end(); ci != ce; ++ci) { 479*f4a2713aSLionel Sambuc VarDecl *variable = ci->getVariable(); 480*f4a2713aSLionel Sambuc BuildScopeInformation(variable, BDecl, ParentScope); 481*f4a2713aSLionel Sambuc } 482*f4a2713aSLionel Sambuc } 483*f4a2713aSLionel Sambuc } 484*f4a2713aSLionel Sambuc 485*f4a2713aSLionel Sambuc // Recursively walk the AST. 486*f4a2713aSLionel Sambuc BuildScopeInformation(SubStmt, ParentScope); 487*f4a2713aSLionel Sambuc } 488*f4a2713aSLionel Sambuc } 489*f4a2713aSLionel Sambuc 490*f4a2713aSLionel Sambuc /// VerifyJumps - Verify each element of the Jumps array to see if they are 491*f4a2713aSLionel Sambuc /// valid, emitting diagnostics if not. 492*f4a2713aSLionel Sambuc void JumpScopeChecker::VerifyJumps() { 493*f4a2713aSLionel Sambuc while (!Jumps.empty()) { 494*f4a2713aSLionel Sambuc Stmt *Jump = Jumps.pop_back_val(); 495*f4a2713aSLionel Sambuc 496*f4a2713aSLionel Sambuc // With a goto, 497*f4a2713aSLionel Sambuc if (GotoStmt *GS = dyn_cast<GotoStmt>(Jump)) { 498*f4a2713aSLionel Sambuc CheckJump(GS, GS->getLabel()->getStmt(), GS->getGotoLoc(), 499*f4a2713aSLionel Sambuc diag::err_goto_into_protected_scope, 500*f4a2713aSLionel Sambuc diag::warn_goto_into_protected_scope, 501*f4a2713aSLionel Sambuc diag::warn_cxx98_compat_goto_into_protected_scope); 502*f4a2713aSLionel Sambuc continue; 503*f4a2713aSLionel Sambuc } 504*f4a2713aSLionel Sambuc 505*f4a2713aSLionel Sambuc // We only get indirect gotos here when they have a constant target. 506*f4a2713aSLionel Sambuc if (IndirectGotoStmt *IGS = dyn_cast<IndirectGotoStmt>(Jump)) { 507*f4a2713aSLionel Sambuc LabelDecl *Target = IGS->getConstantTarget(); 508*f4a2713aSLionel Sambuc CheckJump(IGS, Target->getStmt(), IGS->getGotoLoc(), 509*f4a2713aSLionel Sambuc diag::err_goto_into_protected_scope, 510*f4a2713aSLionel Sambuc diag::warn_goto_into_protected_scope, 511*f4a2713aSLionel Sambuc diag::warn_cxx98_compat_goto_into_protected_scope); 512*f4a2713aSLionel Sambuc continue; 513*f4a2713aSLionel Sambuc } 514*f4a2713aSLionel Sambuc 515*f4a2713aSLionel Sambuc SwitchStmt *SS = cast<SwitchStmt>(Jump); 516*f4a2713aSLionel Sambuc for (SwitchCase *SC = SS->getSwitchCaseList(); SC; 517*f4a2713aSLionel Sambuc SC = SC->getNextSwitchCase()) { 518*f4a2713aSLionel Sambuc assert(LabelAndGotoScopes.count(SC) && "Case not visited?"); 519*f4a2713aSLionel Sambuc SourceLocation Loc; 520*f4a2713aSLionel Sambuc if (CaseStmt *CS = dyn_cast<CaseStmt>(SC)) 521*f4a2713aSLionel Sambuc Loc = CS->getLocStart(); 522*f4a2713aSLionel Sambuc else if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) 523*f4a2713aSLionel Sambuc Loc = DS->getLocStart(); 524*f4a2713aSLionel Sambuc else 525*f4a2713aSLionel Sambuc Loc = SC->getLocStart(); 526*f4a2713aSLionel Sambuc CheckJump(SS, SC, Loc, diag::err_switch_into_protected_scope, 0, 527*f4a2713aSLionel Sambuc diag::warn_cxx98_compat_switch_into_protected_scope); 528*f4a2713aSLionel Sambuc } 529*f4a2713aSLionel Sambuc } 530*f4a2713aSLionel Sambuc } 531*f4a2713aSLionel Sambuc 532*f4a2713aSLionel Sambuc /// VerifyIndirectJumps - Verify whether any possible indirect jump 533*f4a2713aSLionel Sambuc /// might cross a protection boundary. Unlike direct jumps, indirect 534*f4a2713aSLionel Sambuc /// jumps count cleanups as protection boundaries: since there's no 535*f4a2713aSLionel Sambuc /// way to know where the jump is going, we can't implicitly run the 536*f4a2713aSLionel Sambuc /// right cleanups the way we can with direct jumps. 537*f4a2713aSLionel Sambuc /// 538*f4a2713aSLionel Sambuc /// Thus, an indirect jump is "trivial" if it bypasses no 539*f4a2713aSLionel Sambuc /// initializations and no teardowns. More formally, an indirect jump 540*f4a2713aSLionel Sambuc /// from A to B is trivial if the path out from A to DCA(A,B) is 541*f4a2713aSLionel Sambuc /// trivial and the path in from DCA(A,B) to B is trivial, where 542*f4a2713aSLionel Sambuc /// DCA(A,B) is the deepest common ancestor of A and B. 543*f4a2713aSLionel Sambuc /// Jump-triviality is transitive but asymmetric. 544*f4a2713aSLionel Sambuc /// 545*f4a2713aSLionel Sambuc /// A path in is trivial if none of the entered scopes have an InDiag. 546*f4a2713aSLionel Sambuc /// A path out is trivial is none of the exited scopes have an OutDiag. 547*f4a2713aSLionel Sambuc /// 548*f4a2713aSLionel Sambuc /// Under these definitions, this function checks that the indirect 549*f4a2713aSLionel Sambuc /// jump between A and B is trivial for every indirect goto statement A 550*f4a2713aSLionel Sambuc /// and every label B whose address was taken in the function. 551*f4a2713aSLionel Sambuc void JumpScopeChecker::VerifyIndirectJumps() { 552*f4a2713aSLionel Sambuc if (IndirectJumps.empty()) return; 553*f4a2713aSLionel Sambuc 554*f4a2713aSLionel Sambuc // If there aren't any address-of-label expressions in this function, 555*f4a2713aSLionel Sambuc // complain about the first indirect goto. 556*f4a2713aSLionel Sambuc if (IndirectJumpTargets.empty()) { 557*f4a2713aSLionel Sambuc S.Diag(IndirectJumps[0]->getGotoLoc(), 558*f4a2713aSLionel Sambuc diag::err_indirect_goto_without_addrlabel); 559*f4a2713aSLionel Sambuc return; 560*f4a2713aSLionel Sambuc } 561*f4a2713aSLionel Sambuc 562*f4a2713aSLionel Sambuc // Collect a single representative of every scope containing an 563*f4a2713aSLionel Sambuc // indirect goto. For most code bases, this substantially cuts 564*f4a2713aSLionel Sambuc // down on the number of jump sites we'll have to consider later. 565*f4a2713aSLionel Sambuc typedef std::pair<unsigned, IndirectGotoStmt*> JumpScope; 566*f4a2713aSLionel Sambuc SmallVector<JumpScope, 32> JumpScopes; 567*f4a2713aSLionel Sambuc { 568*f4a2713aSLionel Sambuc llvm::DenseMap<unsigned, IndirectGotoStmt*> JumpScopesMap; 569*f4a2713aSLionel Sambuc for (SmallVectorImpl<IndirectGotoStmt*>::iterator 570*f4a2713aSLionel Sambuc I = IndirectJumps.begin(), E = IndirectJumps.end(); I != E; ++I) { 571*f4a2713aSLionel Sambuc IndirectGotoStmt *IG = *I; 572*f4a2713aSLionel Sambuc assert(LabelAndGotoScopes.count(IG) && 573*f4a2713aSLionel Sambuc "indirect jump didn't get added to scopes?"); 574*f4a2713aSLionel Sambuc unsigned IGScope = LabelAndGotoScopes[IG]; 575*f4a2713aSLionel Sambuc IndirectGotoStmt *&Entry = JumpScopesMap[IGScope]; 576*f4a2713aSLionel Sambuc if (!Entry) Entry = IG; 577*f4a2713aSLionel Sambuc } 578*f4a2713aSLionel Sambuc JumpScopes.reserve(JumpScopesMap.size()); 579*f4a2713aSLionel Sambuc for (llvm::DenseMap<unsigned, IndirectGotoStmt*>::iterator 580*f4a2713aSLionel Sambuc I = JumpScopesMap.begin(), E = JumpScopesMap.end(); I != E; ++I) 581*f4a2713aSLionel Sambuc JumpScopes.push_back(*I); 582*f4a2713aSLionel Sambuc } 583*f4a2713aSLionel Sambuc 584*f4a2713aSLionel Sambuc // Collect a single representative of every scope containing a 585*f4a2713aSLionel Sambuc // label whose address was taken somewhere in the function. 586*f4a2713aSLionel Sambuc // For most code bases, there will be only one such scope. 587*f4a2713aSLionel Sambuc llvm::DenseMap<unsigned, LabelDecl*> TargetScopes; 588*f4a2713aSLionel Sambuc for (SmallVectorImpl<LabelDecl*>::iterator 589*f4a2713aSLionel Sambuc I = IndirectJumpTargets.begin(), E = IndirectJumpTargets.end(); 590*f4a2713aSLionel Sambuc I != E; ++I) { 591*f4a2713aSLionel Sambuc LabelDecl *TheLabel = *I; 592*f4a2713aSLionel Sambuc assert(LabelAndGotoScopes.count(TheLabel->getStmt()) && 593*f4a2713aSLionel Sambuc "Referenced label didn't get added to scopes?"); 594*f4a2713aSLionel Sambuc unsigned LabelScope = LabelAndGotoScopes[TheLabel->getStmt()]; 595*f4a2713aSLionel Sambuc LabelDecl *&Target = TargetScopes[LabelScope]; 596*f4a2713aSLionel Sambuc if (!Target) Target = TheLabel; 597*f4a2713aSLionel Sambuc } 598*f4a2713aSLionel Sambuc 599*f4a2713aSLionel Sambuc // For each target scope, make sure it's trivially reachable from 600*f4a2713aSLionel Sambuc // every scope containing a jump site. 601*f4a2713aSLionel Sambuc // 602*f4a2713aSLionel Sambuc // A path between scopes always consists of exitting zero or more 603*f4a2713aSLionel Sambuc // scopes, then entering zero or more scopes. We build a set of 604*f4a2713aSLionel Sambuc // of scopes S from which the target scope can be trivially 605*f4a2713aSLionel Sambuc // entered, then verify that every jump scope can be trivially 606*f4a2713aSLionel Sambuc // exitted to reach a scope in S. 607*f4a2713aSLionel Sambuc llvm::BitVector Reachable(Scopes.size(), false); 608*f4a2713aSLionel Sambuc for (llvm::DenseMap<unsigned,LabelDecl*>::iterator 609*f4a2713aSLionel Sambuc TI = TargetScopes.begin(), TE = TargetScopes.end(); TI != TE; ++TI) { 610*f4a2713aSLionel Sambuc unsigned TargetScope = TI->first; 611*f4a2713aSLionel Sambuc LabelDecl *TargetLabel = TI->second; 612*f4a2713aSLionel Sambuc 613*f4a2713aSLionel Sambuc Reachable.reset(); 614*f4a2713aSLionel Sambuc 615*f4a2713aSLionel Sambuc // Mark all the enclosing scopes from which you can safely jump 616*f4a2713aSLionel Sambuc // into the target scope. 'Min' will end up being the index of 617*f4a2713aSLionel Sambuc // the shallowest such scope. 618*f4a2713aSLionel Sambuc unsigned Min = TargetScope; 619*f4a2713aSLionel Sambuc while (true) { 620*f4a2713aSLionel Sambuc Reachable.set(Min); 621*f4a2713aSLionel Sambuc 622*f4a2713aSLionel Sambuc // Don't go beyond the outermost scope. 623*f4a2713aSLionel Sambuc if (Min == 0) break; 624*f4a2713aSLionel Sambuc 625*f4a2713aSLionel Sambuc // Stop if we can't trivially enter the current scope. 626*f4a2713aSLionel Sambuc if (Scopes[Min].InDiag) break; 627*f4a2713aSLionel Sambuc 628*f4a2713aSLionel Sambuc Min = Scopes[Min].ParentScope; 629*f4a2713aSLionel Sambuc } 630*f4a2713aSLionel Sambuc 631*f4a2713aSLionel Sambuc // Walk through all the jump sites, checking that they can trivially 632*f4a2713aSLionel Sambuc // reach this label scope. 633*f4a2713aSLionel Sambuc for (SmallVectorImpl<JumpScope>::iterator 634*f4a2713aSLionel Sambuc I = JumpScopes.begin(), E = JumpScopes.end(); I != E; ++I) { 635*f4a2713aSLionel Sambuc unsigned Scope = I->first; 636*f4a2713aSLionel Sambuc 637*f4a2713aSLionel Sambuc // Walk out the "scope chain" for this scope, looking for a scope 638*f4a2713aSLionel Sambuc // we've marked reachable. For well-formed code this amortizes 639*f4a2713aSLionel Sambuc // to O(JumpScopes.size() / Scopes.size()): we only iterate 640*f4a2713aSLionel Sambuc // when we see something unmarked, and in well-formed code we 641*f4a2713aSLionel Sambuc // mark everything we iterate past. 642*f4a2713aSLionel Sambuc bool IsReachable = false; 643*f4a2713aSLionel Sambuc while (true) { 644*f4a2713aSLionel Sambuc if (Reachable.test(Scope)) { 645*f4a2713aSLionel Sambuc // If we find something reachable, mark all the scopes we just 646*f4a2713aSLionel Sambuc // walked through as reachable. 647*f4a2713aSLionel Sambuc for (unsigned S = I->first; S != Scope; S = Scopes[S].ParentScope) 648*f4a2713aSLionel Sambuc Reachable.set(S); 649*f4a2713aSLionel Sambuc IsReachable = true; 650*f4a2713aSLionel Sambuc break; 651*f4a2713aSLionel Sambuc } 652*f4a2713aSLionel Sambuc 653*f4a2713aSLionel Sambuc // Don't walk out if we've reached the top-level scope or we've 654*f4a2713aSLionel Sambuc // gotten shallower than the shallowest reachable scope. 655*f4a2713aSLionel Sambuc if (Scope == 0 || Scope < Min) break; 656*f4a2713aSLionel Sambuc 657*f4a2713aSLionel Sambuc // Don't walk out through an out-diagnostic. 658*f4a2713aSLionel Sambuc if (Scopes[Scope].OutDiag) break; 659*f4a2713aSLionel Sambuc 660*f4a2713aSLionel Sambuc Scope = Scopes[Scope].ParentScope; 661*f4a2713aSLionel Sambuc } 662*f4a2713aSLionel Sambuc 663*f4a2713aSLionel Sambuc // Only diagnose if we didn't find something. 664*f4a2713aSLionel Sambuc if (IsReachable) continue; 665*f4a2713aSLionel Sambuc 666*f4a2713aSLionel Sambuc DiagnoseIndirectJump(I->second, I->first, TargetLabel, TargetScope); 667*f4a2713aSLionel Sambuc } 668*f4a2713aSLionel Sambuc } 669*f4a2713aSLionel Sambuc } 670*f4a2713aSLionel Sambuc 671*f4a2713aSLionel Sambuc /// Return true if a particular error+note combination must be downgraded to a 672*f4a2713aSLionel Sambuc /// warning in Microsoft mode. 673*f4a2713aSLionel Sambuc static bool IsMicrosoftJumpWarning(unsigned JumpDiag, unsigned InDiagNote) { 674*f4a2713aSLionel Sambuc return (JumpDiag == diag::err_goto_into_protected_scope && 675*f4a2713aSLionel Sambuc (InDiagNote == diag::note_protected_by_variable_init || 676*f4a2713aSLionel Sambuc InDiagNote == diag::note_protected_by_variable_nontriv_destructor)); 677*f4a2713aSLionel Sambuc } 678*f4a2713aSLionel Sambuc 679*f4a2713aSLionel Sambuc /// Return true if a particular note should be downgraded to a compatibility 680*f4a2713aSLionel Sambuc /// warning in C++11 mode. 681*f4a2713aSLionel Sambuc static bool IsCXX98CompatWarning(Sema &S, unsigned InDiagNote) { 682*f4a2713aSLionel Sambuc return S.getLangOpts().CPlusPlus11 && 683*f4a2713aSLionel Sambuc InDiagNote == diag::note_protected_by_variable_non_pod; 684*f4a2713aSLionel Sambuc } 685*f4a2713aSLionel Sambuc 686*f4a2713aSLionel Sambuc /// Produce primary diagnostic for an indirect jump statement. 687*f4a2713aSLionel Sambuc static void DiagnoseIndirectJumpStmt(Sema &S, IndirectGotoStmt *Jump, 688*f4a2713aSLionel Sambuc LabelDecl *Target, bool &Diagnosed) { 689*f4a2713aSLionel Sambuc if (Diagnosed) 690*f4a2713aSLionel Sambuc return; 691*f4a2713aSLionel Sambuc S.Diag(Jump->getGotoLoc(), diag::err_indirect_goto_in_protected_scope); 692*f4a2713aSLionel Sambuc S.Diag(Target->getStmt()->getIdentLoc(), diag::note_indirect_goto_target); 693*f4a2713aSLionel Sambuc Diagnosed = true; 694*f4a2713aSLionel Sambuc } 695*f4a2713aSLionel Sambuc 696*f4a2713aSLionel Sambuc /// Produce note diagnostics for a jump into a protected scope. 697*f4a2713aSLionel Sambuc void JumpScopeChecker::NoteJumpIntoScopes(ArrayRef<unsigned> ToScopes) { 698*f4a2713aSLionel Sambuc assert(!ToScopes.empty()); 699*f4a2713aSLionel Sambuc for (unsigned I = 0, E = ToScopes.size(); I != E; ++I) 700*f4a2713aSLionel Sambuc if (Scopes[ToScopes[I]].InDiag) 701*f4a2713aSLionel Sambuc S.Diag(Scopes[ToScopes[I]].Loc, Scopes[ToScopes[I]].InDiag); 702*f4a2713aSLionel Sambuc } 703*f4a2713aSLionel Sambuc 704*f4a2713aSLionel Sambuc /// Diagnose an indirect jump which is known to cross scopes. 705*f4a2713aSLionel Sambuc void JumpScopeChecker::DiagnoseIndirectJump(IndirectGotoStmt *Jump, 706*f4a2713aSLionel Sambuc unsigned JumpScope, 707*f4a2713aSLionel Sambuc LabelDecl *Target, 708*f4a2713aSLionel Sambuc unsigned TargetScope) { 709*f4a2713aSLionel Sambuc assert(JumpScope != TargetScope); 710*f4a2713aSLionel Sambuc 711*f4a2713aSLionel Sambuc unsigned Common = GetDeepestCommonScope(JumpScope, TargetScope); 712*f4a2713aSLionel Sambuc bool Diagnosed = false; 713*f4a2713aSLionel Sambuc 714*f4a2713aSLionel Sambuc // Walk out the scope chain until we reach the common ancestor. 715*f4a2713aSLionel Sambuc for (unsigned I = JumpScope; I != Common; I = Scopes[I].ParentScope) 716*f4a2713aSLionel Sambuc if (Scopes[I].OutDiag) { 717*f4a2713aSLionel Sambuc DiagnoseIndirectJumpStmt(S, Jump, Target, Diagnosed); 718*f4a2713aSLionel Sambuc S.Diag(Scopes[I].Loc, Scopes[I].OutDiag); 719*f4a2713aSLionel Sambuc } 720*f4a2713aSLionel Sambuc 721*f4a2713aSLionel Sambuc SmallVector<unsigned, 10> ToScopesCXX98Compat; 722*f4a2713aSLionel Sambuc 723*f4a2713aSLionel Sambuc // Now walk into the scopes containing the label whose address was taken. 724*f4a2713aSLionel Sambuc for (unsigned I = TargetScope; I != Common; I = Scopes[I].ParentScope) 725*f4a2713aSLionel Sambuc if (IsCXX98CompatWarning(S, Scopes[I].InDiag)) 726*f4a2713aSLionel Sambuc ToScopesCXX98Compat.push_back(I); 727*f4a2713aSLionel Sambuc else if (Scopes[I].InDiag) { 728*f4a2713aSLionel Sambuc DiagnoseIndirectJumpStmt(S, Jump, Target, Diagnosed); 729*f4a2713aSLionel Sambuc S.Diag(Scopes[I].Loc, Scopes[I].InDiag); 730*f4a2713aSLionel Sambuc } 731*f4a2713aSLionel Sambuc 732*f4a2713aSLionel Sambuc // Diagnose this jump if it would be ill-formed in C++98. 733*f4a2713aSLionel Sambuc if (!Diagnosed && !ToScopesCXX98Compat.empty()) { 734*f4a2713aSLionel Sambuc S.Diag(Jump->getGotoLoc(), 735*f4a2713aSLionel Sambuc diag::warn_cxx98_compat_indirect_goto_in_protected_scope); 736*f4a2713aSLionel Sambuc S.Diag(Target->getStmt()->getIdentLoc(), diag::note_indirect_goto_target); 737*f4a2713aSLionel Sambuc NoteJumpIntoScopes(ToScopesCXX98Compat); 738*f4a2713aSLionel Sambuc } 739*f4a2713aSLionel Sambuc } 740*f4a2713aSLionel Sambuc 741*f4a2713aSLionel Sambuc /// CheckJump - Validate that the specified jump statement is valid: that it is 742*f4a2713aSLionel Sambuc /// jumping within or out of its current scope, not into a deeper one. 743*f4a2713aSLionel Sambuc void JumpScopeChecker::CheckJump(Stmt *From, Stmt *To, SourceLocation DiagLoc, 744*f4a2713aSLionel Sambuc unsigned JumpDiagError, unsigned JumpDiagWarning, 745*f4a2713aSLionel Sambuc unsigned JumpDiagCXX98Compat) { 746*f4a2713aSLionel Sambuc assert(LabelAndGotoScopes.count(From) && "Jump didn't get added to scopes?"); 747*f4a2713aSLionel Sambuc unsigned FromScope = LabelAndGotoScopes[From]; 748*f4a2713aSLionel Sambuc 749*f4a2713aSLionel Sambuc assert(LabelAndGotoScopes.count(To) && "Jump didn't get added to scopes?"); 750*f4a2713aSLionel Sambuc unsigned ToScope = LabelAndGotoScopes[To]; 751*f4a2713aSLionel Sambuc 752*f4a2713aSLionel Sambuc // Common case: exactly the same scope, which is fine. 753*f4a2713aSLionel Sambuc if (FromScope == ToScope) return; 754*f4a2713aSLionel Sambuc 755*f4a2713aSLionel Sambuc unsigned CommonScope = GetDeepestCommonScope(FromScope, ToScope); 756*f4a2713aSLionel Sambuc 757*f4a2713aSLionel Sambuc // It's okay to jump out from a nested scope. 758*f4a2713aSLionel Sambuc if (CommonScope == ToScope) return; 759*f4a2713aSLionel Sambuc 760*f4a2713aSLionel Sambuc // Pull out (and reverse) any scopes we might need to diagnose skipping. 761*f4a2713aSLionel Sambuc SmallVector<unsigned, 10> ToScopesCXX98Compat; 762*f4a2713aSLionel Sambuc SmallVector<unsigned, 10> ToScopesError; 763*f4a2713aSLionel Sambuc SmallVector<unsigned, 10> ToScopesWarning; 764*f4a2713aSLionel Sambuc for (unsigned I = ToScope; I != CommonScope; I = Scopes[I].ParentScope) { 765*f4a2713aSLionel Sambuc if (S.getLangOpts().MicrosoftMode && JumpDiagWarning != 0 && 766*f4a2713aSLionel Sambuc IsMicrosoftJumpWarning(JumpDiagError, Scopes[I].InDiag)) 767*f4a2713aSLionel Sambuc ToScopesWarning.push_back(I); 768*f4a2713aSLionel Sambuc else if (IsCXX98CompatWarning(S, Scopes[I].InDiag)) 769*f4a2713aSLionel Sambuc ToScopesCXX98Compat.push_back(I); 770*f4a2713aSLionel Sambuc else if (Scopes[I].InDiag) 771*f4a2713aSLionel Sambuc ToScopesError.push_back(I); 772*f4a2713aSLionel Sambuc } 773*f4a2713aSLionel Sambuc 774*f4a2713aSLionel Sambuc // Handle warnings. 775*f4a2713aSLionel Sambuc if (!ToScopesWarning.empty()) { 776*f4a2713aSLionel Sambuc S.Diag(DiagLoc, JumpDiagWarning); 777*f4a2713aSLionel Sambuc NoteJumpIntoScopes(ToScopesWarning); 778*f4a2713aSLionel Sambuc } 779*f4a2713aSLionel Sambuc 780*f4a2713aSLionel Sambuc // Handle errors. 781*f4a2713aSLionel Sambuc if (!ToScopesError.empty()) { 782*f4a2713aSLionel Sambuc S.Diag(DiagLoc, JumpDiagError); 783*f4a2713aSLionel Sambuc NoteJumpIntoScopes(ToScopesError); 784*f4a2713aSLionel Sambuc } 785*f4a2713aSLionel Sambuc 786*f4a2713aSLionel Sambuc // Handle -Wc++98-compat warnings if the jump is well-formed. 787*f4a2713aSLionel Sambuc if (ToScopesError.empty() && !ToScopesCXX98Compat.empty()) { 788*f4a2713aSLionel Sambuc S.Diag(DiagLoc, JumpDiagCXX98Compat); 789*f4a2713aSLionel Sambuc NoteJumpIntoScopes(ToScopesCXX98Compat); 790*f4a2713aSLionel Sambuc } 791*f4a2713aSLionel Sambuc } 792*f4a2713aSLionel Sambuc 793*f4a2713aSLionel Sambuc void Sema::DiagnoseInvalidJumps(Stmt *Body) { 794*f4a2713aSLionel Sambuc (void)JumpScopeChecker(Body, *this); 795*f4a2713aSLionel Sambuc } 796