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