Chris Lattner | 5af280c | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 1 | //===--- JumpDiagnostics.cpp - Analyze Jump Targets for VLA issues --------===// |
| 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 |
| 11 | // jumps that enter a VLA scope in an invalid way. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
John McCall | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame^] | 15 | #include "clang/Sema/SemaInternal.h" |
John McCall | 384aff8 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclCXX.h" |
Chris Lattner | 5af280c | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 17 | #include "clang/AST/Expr.h" |
Chris Lattner | 16f0049 | 2009-04-26 01:32:48 +0000 | [diff] [blame] | 18 | #include "clang/AST/StmtObjC.h" |
Sebastian Redl | 972041f | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 19 | #include "clang/AST/StmtCXX.h" |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/BitVector.h" |
Chris Lattner | 5af280c | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 21 | using namespace clang; |
| 22 | |
| 23 | namespace { |
| 24 | |
| 25 | /// JumpScopeChecker - This object is used by Sema to diagnose invalid jumps |
| 26 | /// into VLA and other protected scopes. For example, this rejects: |
| 27 | /// goto L; |
| 28 | /// int a[n]; |
| 29 | /// L: |
| 30 | /// |
| 31 | class JumpScopeChecker { |
| 32 | Sema &S; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 33 | |
Chris Lattner | 5af280c | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 34 | /// GotoScope - This is a record that we use to keep track of all of the |
| 35 | /// scopes that are introduced by VLAs and other things that scope jumps like |
| 36 | /// gotos. This scope tree has nothing to do with the source scope tree, |
| 37 | /// because you can have multiple VLA scopes per compound statement, and most |
| 38 | /// compound statements don't introduce any scopes. |
| 39 | struct GotoScope { |
| 40 | /// ParentScope - The index in ScopeMap of the parent scope. This is 0 for |
| 41 | /// the parent scope is the function body. |
| 42 | unsigned ParentScope; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 43 | |
John McCall | ddb0b4d | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 44 | /// InDiag - The diagnostic to emit if there is a jump into this scope. |
| 45 | unsigned InDiag; |
| 46 | |
| 47 | /// OutDiag - The diagnostic to emit if there is an indirect jump out |
| 48 | /// of this scope. Direct jumps always clean up their current scope |
| 49 | /// in an orderly way. |
| 50 | unsigned OutDiag; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 51 | |
Chris Lattner | 5af280c | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 52 | /// Loc - Location to emit the diagnostic. |
| 53 | SourceLocation Loc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 54 | |
John McCall | ddb0b4d | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 55 | GotoScope(unsigned parentScope, unsigned InDiag, unsigned OutDiag, |
| 56 | SourceLocation L) |
| 57 | : ParentScope(parentScope), InDiag(InDiag), OutDiag(OutDiag), Loc(L) {} |
Chris Lattner | 5af280c | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 58 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 59 | |
Chris Lattner | 5af280c | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 60 | llvm::SmallVector<GotoScope, 48> Scopes; |
| 61 | llvm::DenseMap<Stmt*, unsigned> LabelAndGotoScopes; |
| 62 | llvm::SmallVector<Stmt*, 16> Jumps; |
John McCall | ddb0b4d | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 63 | |
| 64 | llvm::SmallVector<IndirectGotoStmt*, 4> IndirectJumps; |
| 65 | llvm::SmallVector<LabelStmt*, 4> IndirectJumpTargets; |
Chris Lattner | 5af280c | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 66 | public: |
| 67 | JumpScopeChecker(Stmt *Body, Sema &S); |
| 68 | private: |
Douglas Gregor | 43dec6b | 2010-06-21 23:44:13 +0000 | [diff] [blame] | 69 | void BuildScopeInformation(Decl *D, unsigned &ParentScope); |
Chris Lattner | 5af280c | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 70 | void BuildScopeInformation(Stmt *S, unsigned ParentScope); |
| 71 | void VerifyJumps(); |
John McCall | ddb0b4d | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 72 | void VerifyIndirectJumps(); |
| 73 | void DiagnoseIndirectJump(IndirectGotoStmt *IG, unsigned IGScope, |
| 74 | LabelStmt *Target, unsigned TargetScope); |
Chris Lattner | 5af280c | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 75 | void CheckJump(Stmt *From, Stmt *To, |
| 76 | SourceLocation DiagLoc, unsigned JumpDiag); |
John McCall | 5e2a7ac | 2010-05-12 02:37:54 +0000 | [diff] [blame] | 77 | |
| 78 | unsigned GetDeepestCommonScope(unsigned A, unsigned B); |
Chris Lattner | 5af280c | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 79 | }; |
| 80 | } // end anonymous namespace |
| 81 | |
| 82 | |
| 83 | JumpScopeChecker::JumpScopeChecker(Stmt *Body, Sema &s) : S(s) { |
| 84 | // Add a scope entry for function scope. |
John McCall | ddb0b4d | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 85 | Scopes.push_back(GotoScope(~0U, ~0U, ~0U, SourceLocation())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 86 | |
Chris Lattner | 5af280c | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 87 | // Build information for the top level compound statement, so that we have a |
| 88 | // defined scope record for every "goto" and label. |
| 89 | BuildScopeInformation(Body, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 90 | |
Chris Lattner | 5af280c | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 91 | // Check that all jumps we saw are kosher. |
| 92 | VerifyJumps(); |
John McCall | ddb0b4d | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 93 | VerifyIndirectJumps(); |
Chris Lattner | 5af280c | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 94 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 95 | |
John McCall | 5e2a7ac | 2010-05-12 02:37:54 +0000 | [diff] [blame] | 96 | /// GetDeepestCommonScope - Finds the innermost scope enclosing the |
| 97 | /// two scopes. |
| 98 | unsigned JumpScopeChecker::GetDeepestCommonScope(unsigned A, unsigned B) { |
| 99 | while (A != B) { |
| 100 | // Inner scopes are created after outer scopes and therefore have |
| 101 | // higher indices. |
| 102 | if (A < B) { |
| 103 | assert(Scopes[B].ParentScope < B); |
| 104 | B = Scopes[B].ParentScope; |
| 105 | } else { |
| 106 | assert(Scopes[A].ParentScope < A); |
| 107 | A = Scopes[A].ParentScope; |
| 108 | } |
| 109 | } |
| 110 | return A; |
| 111 | } |
| 112 | |
Chris Lattner | 5af280c | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 113 | /// GetDiagForGotoScopeDecl - If this decl induces a new goto scope, return a |
| 114 | /// diagnostic that should be emitted if control goes over it. If not, return 0. |
John McCall | ddb0b4d | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 115 | static std::pair<unsigned,unsigned> |
| 116 | GetDiagForGotoScopeDecl(const Decl *D, bool isCPlusPlus) { |
Chris Lattner | 5af280c | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 117 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
John McCall | ddb0b4d | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 118 | unsigned InDiag = 0, OutDiag = 0; |
Chris Lattner | 5af280c | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 119 | if (VD->getType()->isVariablyModifiedType()) |
John McCall | ddb0b4d | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 120 | InDiag = diag::note_protected_by_vla; |
| 121 | |
| 122 | if (VD->hasAttr<BlocksAttr>()) { |
| 123 | InDiag = diag::note_protected_by___block; |
| 124 | OutDiag = diag::note_exits___block; |
| 125 | } else if (VD->hasAttr<CleanupAttr>()) { |
| 126 | InDiag = diag::note_protected_by_cleanup; |
| 127 | OutDiag = diag::note_exits_cleanup; |
| 128 | } else if (isCPlusPlus) { |
| 129 | // FIXME: In C++0x, we have to check more conditions than "did we |
| 130 | // just give it an initializer?". See 6.7p3. |
| 131 | if (VD->hasLocalStorage() && VD->hasInit()) |
| 132 | InDiag = diag::note_protected_by_variable_init; |
| 133 | |
| 134 | CanQualType T = VD->getType()->getCanonicalTypeUnqualified(); |
Douglas Gregor | 025291b | 2010-07-01 00:21:21 +0000 | [diff] [blame] | 135 | if (!T->isDependentType()) { |
| 136 | while (CanQual<ArrayType> AT = T->getAs<ArrayType>()) |
| 137 | T = AT->getElementType(); |
| 138 | if (CanQual<RecordType> RT = T->getAs<RecordType>()) |
| 139 | if (!cast<CXXRecordDecl>(RT->getDecl())->hasTrivialDestructor()) |
| 140 | OutDiag = diag::note_exits_dtor; |
| 141 | } |
John McCall | ddb0b4d | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 142 | } |
Chris Lattner | 6d97e5e | 2010-03-01 20:59:53 +0000 | [diff] [blame] | 143 | |
John McCall | ddb0b4d | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 144 | return std::make_pair(InDiag, OutDiag); |
Chris Lattner | 5af280c | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 145 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 146 | |
John McCall | ddb0b4d | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 147 | if (const TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) { |
| 148 | if (TD->getUnderlyingType()->isVariablyModifiedType()) |
| 149 | return std::make_pair((unsigned) diag::note_protected_by_vla_typedef, 0); |
| 150 | } |
| 151 | |
| 152 | return std::make_pair(0U, 0U); |
Chris Lattner | 5af280c | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Douglas Gregor | 43dec6b | 2010-06-21 23:44:13 +0000 | [diff] [blame] | 155 | /// \brief Build scope information for a declaration that is part of a DeclStmt. |
| 156 | void JumpScopeChecker::BuildScopeInformation(Decl *D, unsigned &ParentScope) { |
| 157 | bool isCPlusPlus = this->S.getLangOptions().CPlusPlus; |
| 158 | |
| 159 | // If this decl causes a new scope, push and switch to it. |
| 160 | std::pair<unsigned,unsigned> Diags |
| 161 | = GetDiagForGotoScopeDecl(D, isCPlusPlus); |
| 162 | if (Diags.first || Diags.second) { |
| 163 | Scopes.push_back(GotoScope(ParentScope, Diags.first, Diags.second, |
| 164 | D->getLocation())); |
| 165 | ParentScope = Scopes.size()-1; |
| 166 | } |
| 167 | |
| 168 | // If the decl has an initializer, walk it with the potentially new |
| 169 | // scope we just installed. |
| 170 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 171 | if (Expr *Init = VD->getInit()) |
| 172 | BuildScopeInformation(Init, ParentScope); |
| 173 | } |
Chris Lattner | 5af280c | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 174 | |
| 175 | /// BuildScopeInformation - The statements from CI to CE are known to form a |
| 176 | /// coherent VLA scope with a specified parent node. Walk through the |
| 177 | /// statements, adding any labels or gotos to LabelAndGotoScopes and recursively |
| 178 | /// walking the AST as needed. |
| 179 | void JumpScopeChecker::BuildScopeInformation(Stmt *S, unsigned ParentScope) { |
Douglas Gregor | 43dec6b | 2010-06-21 23:44:13 +0000 | [diff] [blame] | 180 | bool SkipFirstSubStmt = false; |
| 181 | |
Chris Lattner | 5af280c | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 182 | // If we found a label, remember that it is in ParentScope scope. |
John McCall | ddb0b4d | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 183 | switch (S->getStmtClass()) { |
John McCall | ddb0b4d | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 184 | case Stmt::AddrLabelExprClass: |
| 185 | IndirectJumpTargets.push_back(cast<AddrLabelExpr>(S)->getLabel()); |
| 186 | break; |
| 187 | |
| 188 | case Stmt::IndirectGotoStmtClass: |
| 189 | LabelAndGotoScopes[S] = ParentScope; |
| 190 | IndirectJumps.push_back(cast<IndirectGotoStmt>(S)); |
| 191 | break; |
| 192 | |
John McCall | ddb0b4d | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 193 | case Stmt::SwitchStmtClass: |
Douglas Gregor | 43dec6b | 2010-06-21 23:44:13 +0000 | [diff] [blame] | 194 | // Evaluate the condition variable before entering the scope of the switch |
| 195 | // statement. |
| 196 | if (VarDecl *Var = cast<SwitchStmt>(S)->getConditionVariable()) { |
| 197 | BuildScopeInformation(Var, ParentScope); |
| 198 | SkipFirstSubStmt = true; |
| 199 | } |
| 200 | // Fall through |
| 201 | |
| 202 | case Stmt::GotoStmtClass: |
Chris Lattner | 5af280c | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 203 | // Remember both what scope a goto is in as well as the fact that we have |
| 204 | // it. This makes the second scan not have to walk the AST again. |
| 205 | LabelAndGotoScopes[S] = ParentScope; |
| 206 | Jumps.push_back(S); |
John McCall | ddb0b4d | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 207 | break; |
| 208 | |
| 209 | default: |
| 210 | break; |
Chris Lattner | 5af280c | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 211 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 212 | |
Chris Lattner | 5af280c | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 213 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); CI != E; |
| 214 | ++CI) { |
Douglas Gregor | 43dec6b | 2010-06-21 23:44:13 +0000 | [diff] [blame] | 215 | if (SkipFirstSubStmt) { |
| 216 | SkipFirstSubStmt = false; |
| 217 | continue; |
| 218 | } |
| 219 | |
Chris Lattner | 5af280c | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 220 | Stmt *SubStmt = *CI; |
| 221 | if (SubStmt == 0) continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 222 | |
John McCall | 97ba481 | 2010-08-02 23:33:14 +0000 | [diff] [blame] | 223 | // Cases, labels, and defaults aren't "scope parents". It's also |
| 224 | // important to handle these iteratively instead of recursively in |
| 225 | // order to avoid blowing out the stack. |
| 226 | while (true) { |
| 227 | Stmt *Next; |
| 228 | if (isa<CaseStmt>(SubStmt)) |
| 229 | Next = cast<CaseStmt>(SubStmt)->getSubStmt(); |
| 230 | else if (isa<DefaultStmt>(SubStmt)) |
| 231 | Next = cast<DefaultStmt>(SubStmt)->getSubStmt(); |
| 232 | else if (isa<LabelStmt>(SubStmt)) |
| 233 | Next = cast<LabelStmt>(SubStmt)->getSubStmt(); |
| 234 | else |
| 235 | break; |
| 236 | |
| 237 | LabelAndGotoScopes[SubStmt] = ParentScope; |
| 238 | SubStmt = Next; |
| 239 | } |
| 240 | |
Chris Lattner | 5af280c | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 241 | // If this is a declstmt with a VLA definition, it defines a scope from here |
| 242 | // to the end of the containing context. |
| 243 | if (DeclStmt *DS = dyn_cast<DeclStmt>(SubStmt)) { |
Chris Lattner | 6d97e5e | 2010-03-01 20:59:53 +0000 | [diff] [blame] | 244 | // The decl statement creates a scope if any of the decls in it are VLAs |
| 245 | // or have the cleanup attribute. |
Chris Lattner | 5af280c | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 246 | for (DeclStmt::decl_iterator I = DS->decl_begin(), E = DS->decl_end(); |
Douglas Gregor | 43dec6b | 2010-06-21 23:44:13 +0000 | [diff] [blame] | 247 | I != E; ++I) |
| 248 | BuildScopeInformation(*I, ParentScope); |
Chris Lattner | 5af280c | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 249 | continue; |
| 250 | } |
| 251 | |
| 252 | // Disallow jumps into any part of an @try statement by pushing a scope and |
| 253 | // walking all sub-stmts in that scope. |
| 254 | if (ObjCAtTryStmt *AT = dyn_cast<ObjCAtTryStmt>(SubStmt)) { |
| 255 | // Recursively walk the AST for the @try part. |
John McCall | ddb0b4d | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 256 | Scopes.push_back(GotoScope(ParentScope, |
| 257 | diag::note_protected_by_objc_try, |
| 258 | diag::note_exits_objc_try, |
Chris Lattner | 5af280c | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 259 | AT->getAtTryLoc())); |
| 260 | if (Stmt *TryPart = AT->getTryBody()) |
| 261 | BuildScopeInformation(TryPart, Scopes.size()-1); |
| 262 | |
| 263 | // Jump from the catch to the finally or try is not valid. |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 264 | for (unsigned I = 0, N = AT->getNumCatchStmts(); I != N; ++I) { |
| 265 | ObjCAtCatchStmt *AC = AT->getCatchStmt(I); |
Chris Lattner | 5af280c | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 266 | Scopes.push_back(GotoScope(ParentScope, |
| 267 | diag::note_protected_by_objc_catch, |
John McCall | ddb0b4d | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 268 | diag::note_exits_objc_catch, |
Chris Lattner | 5af280c | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 269 | AC->getAtCatchLoc())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 270 | // @catches are nested and it isn't |
Chris Lattner | 5af280c | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 271 | BuildScopeInformation(AC->getCatchBody(), Scopes.size()-1); |
| 272 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 273 | |
Chris Lattner | 5af280c | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 274 | // Jump from the finally to the try or catch is not valid. |
| 275 | if (ObjCAtFinallyStmt *AF = AT->getFinallyStmt()) { |
| 276 | Scopes.push_back(GotoScope(ParentScope, |
| 277 | diag::note_protected_by_objc_finally, |
John McCall | ddb0b4d | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 278 | diag::note_exits_objc_finally, |
Chris Lattner | 5af280c | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 279 | AF->getAtFinallyLoc())); |
| 280 | BuildScopeInformation(AF, Scopes.size()-1); |
| 281 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 282 | |
Chris Lattner | 5af280c | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 283 | continue; |
| 284 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 285 | |
Chris Lattner | 46c3c4b | 2009-04-21 06:01:00 +0000 | [diff] [blame] | 286 | // Disallow jumps into the protected statement of an @synchronized, but |
| 287 | // allow jumps into the object expression it protects. |
| 288 | if (ObjCAtSynchronizedStmt *AS = dyn_cast<ObjCAtSynchronizedStmt>(SubStmt)){ |
| 289 | // Recursively walk the AST for the @synchronized object expr, it is |
| 290 | // evaluated in the normal scope. |
| 291 | BuildScopeInformation(AS->getSynchExpr(), ParentScope); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 292 | |
Chris Lattner | 46c3c4b | 2009-04-21 06:01:00 +0000 | [diff] [blame] | 293 | // Recursively walk the AST for the @synchronized part, protected by a new |
| 294 | // scope. |
| 295 | Scopes.push_back(GotoScope(ParentScope, |
| 296 | diag::note_protected_by_objc_synchronized, |
John McCall | ddb0b4d | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 297 | diag::note_exits_objc_synchronized, |
Chris Lattner | 46c3c4b | 2009-04-21 06:01:00 +0000 | [diff] [blame] | 298 | AS->getAtSynchronizedLoc())); |
| 299 | BuildScopeInformation(AS->getSynchBody(), Scopes.size()-1); |
| 300 | continue; |
| 301 | } |
Sebastian Redl | 972041f | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 302 | |
| 303 | // Disallow jumps into any part of a C++ try statement. This is pretty |
| 304 | // much the same as for Obj-C. |
| 305 | if (CXXTryStmt *TS = dyn_cast<CXXTryStmt>(SubStmt)) { |
John McCall | ddb0b4d | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 306 | Scopes.push_back(GotoScope(ParentScope, |
| 307 | diag::note_protected_by_cxx_try, |
| 308 | diag::note_exits_cxx_try, |
Sebastian Redl | 972041f | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 309 | TS->getSourceRange().getBegin())); |
| 310 | if (Stmt *TryBlock = TS->getTryBlock()) |
| 311 | BuildScopeInformation(TryBlock, Scopes.size()-1); |
| 312 | |
| 313 | // Jump from the catch into the try is not allowed either. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 314 | for (unsigned I = 0, E = TS->getNumHandlers(); I != E; ++I) { |
Sebastian Redl | 972041f | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 315 | CXXCatchStmt *CS = TS->getHandler(I); |
| 316 | Scopes.push_back(GotoScope(ParentScope, |
| 317 | diag::note_protected_by_cxx_catch, |
John McCall | ddb0b4d | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 318 | diag::note_exits_cxx_catch, |
Sebastian Redl | 972041f | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 319 | CS->getSourceRange().getBegin())); |
| 320 | BuildScopeInformation(CS->getHandlerBlock(), Scopes.size()-1); |
| 321 | } |
| 322 | |
| 323 | continue; |
| 324 | } |
| 325 | |
Chris Lattner | 5af280c | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 326 | // Recursively walk the AST. |
| 327 | BuildScopeInformation(SubStmt, ParentScope); |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | /// VerifyJumps - Verify each element of the Jumps array to see if they are |
| 332 | /// valid, emitting diagnostics if not. |
| 333 | void JumpScopeChecker::VerifyJumps() { |
| 334 | while (!Jumps.empty()) { |
| 335 | Stmt *Jump = Jumps.pop_back_val(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 336 | |
| 337 | // With a goto, |
Chris Lattner | 5af280c | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 338 | if (GotoStmt *GS = dyn_cast<GotoStmt>(Jump)) { |
| 339 | CheckJump(GS, GS->getLabel(), GS->getGotoLoc(), |
| 340 | diag::err_goto_into_protected_scope); |
| 341 | continue; |
| 342 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 343 | |
John McCall | ddb0b4d | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 344 | SwitchStmt *SS = cast<SwitchStmt>(Jump); |
| 345 | for (SwitchCase *SC = SS->getSwitchCaseList(); SC; |
| 346 | SC = SC->getNextSwitchCase()) { |
| 347 | assert(LabelAndGotoScopes.count(SC) && "Case not visited?"); |
| 348 | CheckJump(SS, SC, SC->getLocStart(), |
| 349 | diag::err_switch_into_protected_scope); |
Chris Lattner | 5af280c | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | } |
| 353 | |
John McCall | 5e2a7ac | 2010-05-12 02:37:54 +0000 | [diff] [blame] | 354 | /// VerifyIndirectJumps - Verify whether any possible indirect jump |
| 355 | /// might cross a protection boundary. Unlike direct jumps, indirect |
| 356 | /// jumps count cleanups as protection boundaries: since there's no |
| 357 | /// way to know where the jump is going, we can't implicitly run the |
| 358 | /// right cleanups the way we can with direct jumps. |
John McCall | ddb0b4d | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 359 | /// |
John McCall | 5e2a7ac | 2010-05-12 02:37:54 +0000 | [diff] [blame] | 360 | /// Thus, an indirect jump is "trivial" if it bypasses no |
| 361 | /// initializations and no teardowns. More formally, an indirect jump |
| 362 | /// from A to B is trivial if the path out from A to DCA(A,B) is |
| 363 | /// trivial and the path in from DCA(A,B) to B is trivial, where |
| 364 | /// DCA(A,B) is the deepest common ancestor of A and B. |
| 365 | /// Jump-triviality is transitive but asymmetric. |
| 366 | /// |
John McCall | ddb0b4d | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 367 | /// A path in is trivial if none of the entered scopes have an InDiag. |
| 368 | /// A path out is trivial is none of the exited scopes have an OutDiag. |
John McCall | 5e2a7ac | 2010-05-12 02:37:54 +0000 | [diff] [blame] | 369 | /// |
| 370 | /// Under these definitions, this function checks that the indirect |
| 371 | /// jump between A and B is trivial for every indirect goto statement A |
| 372 | /// and every label B whose address was taken in the function. |
John McCall | ddb0b4d | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 373 | void JumpScopeChecker::VerifyIndirectJumps() { |
| 374 | if (IndirectJumps.empty()) return; |
| 375 | |
| 376 | // If there aren't any address-of-label expressions in this function, |
| 377 | // complain about the first indirect goto. |
| 378 | if (IndirectJumpTargets.empty()) { |
| 379 | S.Diag(IndirectJumps[0]->getGotoLoc(), |
| 380 | diag::err_indirect_goto_without_addrlabel); |
| 381 | return; |
| 382 | } |
| 383 | |
John McCall | 5e2a7ac | 2010-05-12 02:37:54 +0000 | [diff] [blame] | 384 | // Collect a single representative of every scope containing an |
| 385 | // indirect goto. For most code bases, this substantially cuts |
| 386 | // down on the number of jump sites we'll have to consider later. |
John McCall | ddb0b4d | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 387 | typedef std::pair<unsigned, IndirectGotoStmt*> JumpScope; |
| 388 | llvm::SmallVector<JumpScope, 32> JumpScopes; |
| 389 | { |
| 390 | llvm::DenseMap<unsigned, IndirectGotoStmt*> JumpScopesMap; |
| 391 | for (llvm::SmallVectorImpl<IndirectGotoStmt*>::iterator |
| 392 | I = IndirectJumps.begin(), E = IndirectJumps.end(); I != E; ++I) { |
| 393 | IndirectGotoStmt *IG = *I; |
| 394 | assert(LabelAndGotoScopes.count(IG) && |
| 395 | "indirect jump didn't get added to scopes?"); |
| 396 | unsigned IGScope = LabelAndGotoScopes[IG]; |
| 397 | IndirectGotoStmt *&Entry = JumpScopesMap[IGScope]; |
| 398 | if (!Entry) Entry = IG; |
| 399 | } |
| 400 | JumpScopes.reserve(JumpScopesMap.size()); |
| 401 | for (llvm::DenseMap<unsigned, IndirectGotoStmt*>::iterator |
| 402 | I = JumpScopesMap.begin(), E = JumpScopesMap.end(); I != E; ++I) |
| 403 | JumpScopes.push_back(*I); |
| 404 | } |
| 405 | |
John McCall | 5e2a7ac | 2010-05-12 02:37:54 +0000 | [diff] [blame] | 406 | // Collect a single representative of every scope containing a |
| 407 | // label whose address was taken somewhere in the function. |
| 408 | // For most code bases, there will be only one such scope. |
John McCall | ddb0b4d | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 409 | llvm::DenseMap<unsigned, LabelStmt*> TargetScopes; |
| 410 | for (llvm::SmallVectorImpl<LabelStmt*>::iterator |
| 411 | I = IndirectJumpTargets.begin(), E = IndirectJumpTargets.end(); |
| 412 | I != E; ++I) { |
| 413 | LabelStmt *TheLabel = *I; |
| 414 | assert(LabelAndGotoScopes.count(TheLabel) && |
| 415 | "Referenced label didn't get added to scopes?"); |
| 416 | unsigned LabelScope = LabelAndGotoScopes[TheLabel]; |
| 417 | LabelStmt *&Target = TargetScopes[LabelScope]; |
| 418 | if (!Target) Target = TheLabel; |
| 419 | } |
| 420 | |
John McCall | 5e2a7ac | 2010-05-12 02:37:54 +0000 | [diff] [blame] | 421 | // For each target scope, make sure it's trivially reachable from |
| 422 | // every scope containing a jump site. |
| 423 | // |
| 424 | // A path between scopes always consists of exitting zero or more |
| 425 | // scopes, then entering zero or more scopes. We build a set of |
| 426 | // of scopes S from which the target scope can be trivially |
| 427 | // entered, then verify that every jump scope can be trivially |
| 428 | // exitted to reach a scope in S. |
John McCall | ddb0b4d | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 429 | llvm::BitVector Reachable(Scopes.size(), false); |
| 430 | for (llvm::DenseMap<unsigned,LabelStmt*>::iterator |
| 431 | TI = TargetScopes.begin(), TE = TargetScopes.end(); TI != TE; ++TI) { |
| 432 | unsigned TargetScope = TI->first; |
| 433 | LabelStmt *TargetLabel = TI->second; |
| 434 | |
| 435 | Reachable.reset(); |
| 436 | |
| 437 | // Mark all the enclosing scopes from which you can safely jump |
John McCall | 5e2a7ac | 2010-05-12 02:37:54 +0000 | [diff] [blame] | 438 | // into the target scope. 'Min' will end up being the index of |
| 439 | // the shallowest such scope. |
John McCall | ddb0b4d | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 440 | unsigned Min = TargetScope; |
| 441 | while (true) { |
| 442 | Reachable.set(Min); |
| 443 | |
| 444 | // Don't go beyond the outermost scope. |
| 445 | if (Min == 0) break; |
| 446 | |
John McCall | 5e2a7ac | 2010-05-12 02:37:54 +0000 | [diff] [blame] | 447 | // Stop if we can't trivially enter the current scope. |
John McCall | ddb0b4d | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 448 | if (Scopes[Min].InDiag) break; |
| 449 | |
| 450 | Min = Scopes[Min].ParentScope; |
| 451 | } |
| 452 | |
| 453 | // Walk through all the jump sites, checking that they can trivially |
| 454 | // reach this label scope. |
| 455 | for (llvm::SmallVectorImpl<JumpScope>::iterator |
| 456 | I = JumpScopes.begin(), E = JumpScopes.end(); I != E; ++I) { |
| 457 | unsigned Scope = I->first; |
| 458 | |
| 459 | // Walk out the "scope chain" for this scope, looking for a scope |
John McCall | 5e2a7ac | 2010-05-12 02:37:54 +0000 | [diff] [blame] | 460 | // we've marked reachable. For well-formed code this amortizes |
| 461 | // to O(JumpScopes.size() / Scopes.size()): we only iterate |
| 462 | // when we see something unmarked, and in well-formed code we |
| 463 | // mark everything we iterate past. |
John McCall | ddb0b4d | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 464 | bool IsReachable = false; |
| 465 | while (true) { |
| 466 | if (Reachable.test(Scope)) { |
| 467 | // If we find something reachable, mark all the scopes we just |
| 468 | // walked through as reachable. |
| 469 | for (unsigned S = I->first; S != Scope; S = Scopes[S].ParentScope) |
| 470 | Reachable.set(S); |
| 471 | IsReachable = true; |
| 472 | break; |
| 473 | } |
| 474 | |
| 475 | // Don't walk out if we've reached the top-level scope or we've |
| 476 | // gotten shallower than the shallowest reachable scope. |
| 477 | if (Scope == 0 || Scope < Min) break; |
| 478 | |
| 479 | // Don't walk out through an out-diagnostic. |
| 480 | if (Scopes[Scope].OutDiag) break; |
| 481 | |
| 482 | Scope = Scopes[Scope].ParentScope; |
| 483 | } |
| 484 | |
| 485 | // Only diagnose if we didn't find something. |
| 486 | if (IsReachable) continue; |
| 487 | |
| 488 | DiagnoseIndirectJump(I->second, I->first, TargetLabel, TargetScope); |
| 489 | } |
| 490 | } |
| 491 | } |
| 492 | |
John McCall | 5e2a7ac | 2010-05-12 02:37:54 +0000 | [diff] [blame] | 493 | /// Diagnose an indirect jump which is known to cross scopes. |
John McCall | ddb0b4d | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 494 | void JumpScopeChecker::DiagnoseIndirectJump(IndirectGotoStmt *Jump, |
| 495 | unsigned JumpScope, |
| 496 | LabelStmt *Target, |
| 497 | unsigned TargetScope) { |
| 498 | assert(JumpScope != TargetScope); |
| 499 | |
| 500 | S.Diag(Jump->getGotoLoc(), diag::warn_indirect_goto_in_protected_scope); |
| 501 | S.Diag(Target->getIdentLoc(), diag::note_indirect_goto_target); |
| 502 | |
John McCall | 5e2a7ac | 2010-05-12 02:37:54 +0000 | [diff] [blame] | 503 | unsigned Common = GetDeepestCommonScope(JumpScope, TargetScope); |
John McCall | ddb0b4d | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 504 | |
John McCall | 5e2a7ac | 2010-05-12 02:37:54 +0000 | [diff] [blame] | 505 | // Walk out the scope chain until we reach the common ancestor. |
| 506 | for (unsigned I = JumpScope; I != Common; I = Scopes[I].ParentScope) |
| 507 | if (Scopes[I].OutDiag) |
| 508 | S.Diag(Scopes[I].Loc, Scopes[I].OutDiag); |
John McCall | ddb0b4d | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 509 | |
| 510 | // Now walk into the scopes containing the label whose address was taken. |
John McCall | 5e2a7ac | 2010-05-12 02:37:54 +0000 | [diff] [blame] | 511 | for (unsigned I = TargetScope; I != Common; I = Scopes[I].ParentScope) |
| 512 | if (Scopes[I].InDiag) |
| 513 | S.Diag(Scopes[I].Loc, Scopes[I].InDiag); |
John McCall | ddb0b4d | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 514 | } |
| 515 | |
Chris Lattner | 5af280c | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 516 | /// CheckJump - Validate that the specified jump statement is valid: that it is |
| 517 | /// jumping within or out of its current scope, not into a deeper one. |
| 518 | void JumpScopeChecker::CheckJump(Stmt *From, Stmt *To, |
| 519 | SourceLocation DiagLoc, unsigned JumpDiag) { |
| 520 | assert(LabelAndGotoScopes.count(From) && "Jump didn't get added to scopes?"); |
| 521 | unsigned FromScope = LabelAndGotoScopes[From]; |
| 522 | |
| 523 | assert(LabelAndGotoScopes.count(To) && "Jump didn't get added to scopes?"); |
| 524 | unsigned ToScope = LabelAndGotoScopes[To]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 525 | |
Chris Lattner | 5af280c | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 526 | // Common case: exactly the same scope, which is fine. |
| 527 | if (FromScope == ToScope) return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 528 | |
John McCall | 5e2a7ac | 2010-05-12 02:37:54 +0000 | [diff] [blame] | 529 | unsigned CommonScope = GetDeepestCommonScope(FromScope, ToScope); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 530 | |
John McCall | 5e2a7ac | 2010-05-12 02:37:54 +0000 | [diff] [blame] | 531 | // It's okay to jump out from a nested scope. |
| 532 | if (CommonScope == ToScope) return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 533 | |
John McCall | 5e2a7ac | 2010-05-12 02:37:54 +0000 | [diff] [blame] | 534 | // Pull out (and reverse) any scopes we might need to diagnose skipping. |
| 535 | llvm::SmallVector<unsigned, 10> ToScopes; |
| 536 | for (unsigned I = ToScope; I != CommonScope; I = Scopes[I].ParentScope) |
| 537 | if (Scopes[I].InDiag) |
| 538 | ToScopes.push_back(I); |
Chris Lattner | 5af280c | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 539 | |
John McCall | 5e2a7ac | 2010-05-12 02:37:54 +0000 | [diff] [blame] | 540 | // If the only scopes present are cleanup scopes, we're okay. |
John McCall | ddb0b4d | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 541 | if (ToScopes.empty()) return; |
| 542 | |
| 543 | S.Diag(DiagLoc, JumpDiag); |
| 544 | |
Chris Lattner | 5af280c | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 545 | // Emit diagnostics for whatever is left in ToScopes. |
| 546 | for (unsigned i = 0, e = ToScopes.size(); i != e; ++i) |
John McCall | ddb0b4d | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 547 | S.Diag(Scopes[ToScopes[i]].Loc, Scopes[ToScopes[i]].InDiag); |
Chris Lattner | 5af280c | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | void Sema::DiagnoseInvalidJumps(Stmt *Body) { |
Douglas Gregor | 6490ae5 | 2009-11-17 06:14:37 +0000 | [diff] [blame] | 551 | (void)JumpScopeChecker(Body, *this); |
Chris Lattner | 5af280c | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 552 | } |