blob: 47dfbfbdc8a7e8baf9e6b9aacf606ff0c0feee3e [file] [log] [blame]
Chris Lattner5af280c2009-04-19 04:46:21 +00001//===--- 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 McCallddb0b4d2010-05-12 00:58:13 +000015#include "llvm/ADT/BitVector.h"
Chris Lattner5af280c2009-04-19 04:46:21 +000016#include "Sema.h"
17#include "clang/AST/Expr.h"
Chris Lattner16f00492009-04-26 01:32:48 +000018#include "clang/AST/StmtObjC.h"
Sebastian Redl972041f2009-04-27 20:27:31 +000019#include "clang/AST/StmtCXX.h"
Chris Lattner5af280c2009-04-19 04:46:21 +000020using namespace clang;
21
22namespace {
23
24/// JumpScopeChecker - This object is used by Sema to diagnose invalid jumps
25/// into VLA and other protected scopes. For example, this rejects:
26/// goto L;
27/// int a[n];
28/// L:
29///
30class JumpScopeChecker {
31 Sema &S;
Mike Stump1eb44332009-09-09 15:08:12 +000032
Chris Lattner5af280c2009-04-19 04:46:21 +000033 /// GotoScope - This is a record that we use to keep track of all of the
34 /// scopes that are introduced by VLAs and other things that scope jumps like
35 /// gotos. This scope tree has nothing to do with the source scope tree,
36 /// because you can have multiple VLA scopes per compound statement, and most
37 /// compound statements don't introduce any scopes.
38 struct GotoScope {
39 /// ParentScope - The index in ScopeMap of the parent scope. This is 0 for
40 /// the parent scope is the function body.
41 unsigned ParentScope;
Mike Stump1eb44332009-09-09 15:08:12 +000042
John McCallddb0b4d2010-05-12 00:58:13 +000043 /// InDiag - The diagnostic to emit if there is a jump into this scope.
44 unsigned InDiag;
45
46 /// OutDiag - The diagnostic to emit if there is an indirect jump out
47 /// of this scope. Direct jumps always clean up their current scope
48 /// in an orderly way.
49 unsigned OutDiag;
Mike Stump1eb44332009-09-09 15:08:12 +000050
Chris Lattner5af280c2009-04-19 04:46:21 +000051 /// Loc - Location to emit the diagnostic.
52 SourceLocation Loc;
Mike Stump1eb44332009-09-09 15:08:12 +000053
John McCallddb0b4d2010-05-12 00:58:13 +000054 GotoScope(unsigned parentScope, unsigned InDiag, unsigned OutDiag,
55 SourceLocation L)
56 : ParentScope(parentScope), InDiag(InDiag), OutDiag(OutDiag), Loc(L) {}
Chris Lattner5af280c2009-04-19 04:46:21 +000057 };
Mike Stump1eb44332009-09-09 15:08:12 +000058
Chris Lattner5af280c2009-04-19 04:46:21 +000059 llvm::SmallVector<GotoScope, 48> Scopes;
60 llvm::DenseMap<Stmt*, unsigned> LabelAndGotoScopes;
61 llvm::SmallVector<Stmt*, 16> Jumps;
John McCallddb0b4d2010-05-12 00:58:13 +000062
63 llvm::SmallVector<IndirectGotoStmt*, 4> IndirectJumps;
64 llvm::SmallVector<LabelStmt*, 4> IndirectJumpTargets;
Chris Lattner5af280c2009-04-19 04:46:21 +000065public:
66 JumpScopeChecker(Stmt *Body, Sema &S);
67private:
Douglas Gregor43dec6b2010-06-21 23:44:13 +000068 void BuildScopeInformation(Decl *D, unsigned &ParentScope);
Chris Lattner5af280c2009-04-19 04:46:21 +000069 void BuildScopeInformation(Stmt *S, unsigned ParentScope);
70 void VerifyJumps();
John McCallddb0b4d2010-05-12 00:58:13 +000071 void VerifyIndirectJumps();
72 void DiagnoseIndirectJump(IndirectGotoStmt *IG, unsigned IGScope,
73 LabelStmt *Target, unsigned TargetScope);
Chris Lattner5af280c2009-04-19 04:46:21 +000074 void CheckJump(Stmt *From, Stmt *To,
75 SourceLocation DiagLoc, unsigned JumpDiag);
John McCall5e2a7ac2010-05-12 02:37:54 +000076
77 unsigned GetDeepestCommonScope(unsigned A, unsigned B);
Chris Lattner5af280c2009-04-19 04:46:21 +000078};
79} // end anonymous namespace
80
81
82JumpScopeChecker::JumpScopeChecker(Stmt *Body, Sema &s) : S(s) {
83 // Add a scope entry for function scope.
John McCallddb0b4d2010-05-12 00:58:13 +000084 Scopes.push_back(GotoScope(~0U, ~0U, ~0U, SourceLocation()));
Mike Stump1eb44332009-09-09 15:08:12 +000085
Chris Lattner5af280c2009-04-19 04:46:21 +000086 // Build information for the top level compound statement, so that we have a
87 // defined scope record for every "goto" and label.
88 BuildScopeInformation(Body, 0);
Mike Stump1eb44332009-09-09 15:08:12 +000089
Chris Lattner5af280c2009-04-19 04:46:21 +000090 // Check that all jumps we saw are kosher.
91 VerifyJumps();
John McCallddb0b4d2010-05-12 00:58:13 +000092 VerifyIndirectJumps();
Chris Lattner5af280c2009-04-19 04:46:21 +000093}
Mike Stump1eb44332009-09-09 15:08:12 +000094
John McCall5e2a7ac2010-05-12 02:37:54 +000095/// GetDeepestCommonScope - Finds the innermost scope enclosing the
96/// two scopes.
97unsigned JumpScopeChecker::GetDeepestCommonScope(unsigned A, unsigned B) {
98 while (A != B) {
99 // Inner scopes are created after outer scopes and therefore have
100 // higher indices.
101 if (A < B) {
102 assert(Scopes[B].ParentScope < B);
103 B = Scopes[B].ParentScope;
104 } else {
105 assert(Scopes[A].ParentScope < A);
106 A = Scopes[A].ParentScope;
107 }
108 }
109 return A;
110}
111
Chris Lattner5af280c2009-04-19 04:46:21 +0000112/// GetDiagForGotoScopeDecl - If this decl induces a new goto scope, return a
113/// diagnostic that should be emitted if control goes over it. If not, return 0.
John McCallddb0b4d2010-05-12 00:58:13 +0000114static std::pair<unsigned,unsigned>
115 GetDiagForGotoScopeDecl(const Decl *D, bool isCPlusPlus) {
Chris Lattner5af280c2009-04-19 04:46:21 +0000116 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
John McCallddb0b4d2010-05-12 00:58:13 +0000117 unsigned InDiag = 0, OutDiag = 0;
Chris Lattner5af280c2009-04-19 04:46:21 +0000118 if (VD->getType()->isVariablyModifiedType())
John McCallddb0b4d2010-05-12 00:58:13 +0000119 InDiag = diag::note_protected_by_vla;
120
121 if (VD->hasAttr<BlocksAttr>()) {
122 InDiag = diag::note_protected_by___block;
123 OutDiag = diag::note_exits___block;
124 } else if (VD->hasAttr<CleanupAttr>()) {
125 InDiag = diag::note_protected_by_cleanup;
126 OutDiag = diag::note_exits_cleanup;
127 } else if (isCPlusPlus) {
128 // FIXME: In C++0x, we have to check more conditions than "did we
129 // just give it an initializer?". See 6.7p3.
130 if (VD->hasLocalStorage() && VD->hasInit())
131 InDiag = diag::note_protected_by_variable_init;
132
133 CanQualType T = VD->getType()->getCanonicalTypeUnqualified();
134 while (CanQual<ArrayType> AT = T->getAs<ArrayType>())
135 T = AT->getElementType();
136 if (CanQual<RecordType> RT = T->getAs<RecordType>())
137 if (!cast<CXXRecordDecl>(RT->getDecl())->hasTrivialDestructor())
138 OutDiag = diag::note_exits_dtor;
139 }
Chris Lattner6d97e5e2010-03-01 20:59:53 +0000140
John McCallddb0b4d2010-05-12 00:58:13 +0000141 return std::make_pair(InDiag, OutDiag);
Chris Lattner5af280c2009-04-19 04:46:21 +0000142 }
Mike Stump1eb44332009-09-09 15:08:12 +0000143
John McCallddb0b4d2010-05-12 00:58:13 +0000144 if (const TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
145 if (TD->getUnderlyingType()->isVariablyModifiedType())
146 return std::make_pair((unsigned) diag::note_protected_by_vla_typedef, 0);
147 }
148
149 return std::make_pair(0U, 0U);
Chris Lattner5af280c2009-04-19 04:46:21 +0000150}
151
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000152/// \brief Build scope information for a declaration that is part of a DeclStmt.
153void JumpScopeChecker::BuildScopeInformation(Decl *D, unsigned &ParentScope) {
154 bool isCPlusPlus = this->S.getLangOptions().CPlusPlus;
155
156 // If this decl causes a new scope, push and switch to it.
157 std::pair<unsigned,unsigned> Diags
158 = GetDiagForGotoScopeDecl(D, isCPlusPlus);
159 if (Diags.first || Diags.second) {
160 Scopes.push_back(GotoScope(ParentScope, Diags.first, Diags.second,
161 D->getLocation()));
162 ParentScope = Scopes.size()-1;
163 }
164
165 // If the decl has an initializer, walk it with the potentially new
166 // scope we just installed.
167 if (VarDecl *VD = dyn_cast<VarDecl>(D))
168 if (Expr *Init = VD->getInit())
169 BuildScopeInformation(Init, ParentScope);
170}
Chris Lattner5af280c2009-04-19 04:46:21 +0000171
172/// BuildScopeInformation - The statements from CI to CE are known to form a
173/// coherent VLA scope with a specified parent node. Walk through the
174/// statements, adding any labels or gotos to LabelAndGotoScopes and recursively
175/// walking the AST as needed.
176void JumpScopeChecker::BuildScopeInformation(Stmt *S, unsigned ParentScope) {
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000177 bool SkipFirstSubStmt = false;
178
Chris Lattner5af280c2009-04-19 04:46:21 +0000179 // If we found a label, remember that it is in ParentScope scope.
John McCallddb0b4d2010-05-12 00:58:13 +0000180 switch (S->getStmtClass()) {
181 case Stmt::LabelStmtClass:
182 case Stmt::DefaultStmtClass:
183 case Stmt::CaseStmtClass:
Chris Lattner5af280c2009-04-19 04:46:21 +0000184 LabelAndGotoScopes[S] = ParentScope;
John McCallddb0b4d2010-05-12 00:58:13 +0000185 break;
186
187 case Stmt::AddrLabelExprClass:
188 IndirectJumpTargets.push_back(cast<AddrLabelExpr>(S)->getLabel());
189 break;
190
191 case Stmt::IndirectGotoStmtClass:
192 LabelAndGotoScopes[S] = ParentScope;
193 IndirectJumps.push_back(cast<IndirectGotoStmt>(S));
194 break;
195
John McCallddb0b4d2010-05-12 00:58:13 +0000196 case Stmt::SwitchStmtClass:
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000197 // Evaluate the condition variable before entering the scope of the switch
198 // statement.
199 if (VarDecl *Var = cast<SwitchStmt>(S)->getConditionVariable()) {
200 BuildScopeInformation(Var, ParentScope);
201 SkipFirstSubStmt = true;
202 }
203 // Fall through
204
205 case Stmt::GotoStmtClass:
Chris Lattner5af280c2009-04-19 04:46:21 +0000206 // Remember both what scope a goto is in as well as the fact that we have
207 // it. This makes the second scan not have to walk the AST again.
208 LabelAndGotoScopes[S] = ParentScope;
209 Jumps.push_back(S);
John McCallddb0b4d2010-05-12 00:58:13 +0000210 break;
211
212 default:
213 break;
Chris Lattner5af280c2009-04-19 04:46:21 +0000214 }
Mike Stump1eb44332009-09-09 15:08:12 +0000215
Chris Lattner5af280c2009-04-19 04:46:21 +0000216 for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); CI != E;
217 ++CI) {
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000218 if (SkipFirstSubStmt) {
219 SkipFirstSubStmt = false;
220 continue;
221 }
222
Chris Lattner5af280c2009-04-19 04:46:21 +0000223 Stmt *SubStmt = *CI;
224 if (SubStmt == 0) continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000225
Chris Lattner5af280c2009-04-19 04:46:21 +0000226 // If this is a declstmt with a VLA definition, it defines a scope from here
227 // to the end of the containing context.
228 if (DeclStmt *DS = dyn_cast<DeclStmt>(SubStmt)) {
Chris Lattner6d97e5e2010-03-01 20:59:53 +0000229 // The decl statement creates a scope if any of the decls in it are VLAs
230 // or have the cleanup attribute.
Chris Lattner5af280c2009-04-19 04:46:21 +0000231 for (DeclStmt::decl_iterator I = DS->decl_begin(), E = DS->decl_end();
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000232 I != E; ++I)
233 BuildScopeInformation(*I, ParentScope);
Chris Lattner5af280c2009-04-19 04:46:21 +0000234 continue;
235 }
236
237 // Disallow jumps into any part of an @try statement by pushing a scope and
238 // walking all sub-stmts in that scope.
239 if (ObjCAtTryStmt *AT = dyn_cast<ObjCAtTryStmt>(SubStmt)) {
240 // Recursively walk the AST for the @try part.
John McCallddb0b4d2010-05-12 00:58:13 +0000241 Scopes.push_back(GotoScope(ParentScope,
242 diag::note_protected_by_objc_try,
243 diag::note_exits_objc_try,
Chris Lattner5af280c2009-04-19 04:46:21 +0000244 AT->getAtTryLoc()));
245 if (Stmt *TryPart = AT->getTryBody())
246 BuildScopeInformation(TryPart, Scopes.size()-1);
247
248 // Jump from the catch to the finally or try is not valid.
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000249 for (unsigned I = 0, N = AT->getNumCatchStmts(); I != N; ++I) {
250 ObjCAtCatchStmt *AC = AT->getCatchStmt(I);
Chris Lattner5af280c2009-04-19 04:46:21 +0000251 Scopes.push_back(GotoScope(ParentScope,
252 diag::note_protected_by_objc_catch,
John McCallddb0b4d2010-05-12 00:58:13 +0000253 diag::note_exits_objc_catch,
Chris Lattner5af280c2009-04-19 04:46:21 +0000254 AC->getAtCatchLoc()));
Mike Stump1eb44332009-09-09 15:08:12 +0000255 // @catches are nested and it isn't
Chris Lattner5af280c2009-04-19 04:46:21 +0000256 BuildScopeInformation(AC->getCatchBody(), Scopes.size()-1);
257 }
Mike Stump1eb44332009-09-09 15:08:12 +0000258
Chris Lattner5af280c2009-04-19 04:46:21 +0000259 // Jump from the finally to the try or catch is not valid.
260 if (ObjCAtFinallyStmt *AF = AT->getFinallyStmt()) {
261 Scopes.push_back(GotoScope(ParentScope,
262 diag::note_protected_by_objc_finally,
John McCallddb0b4d2010-05-12 00:58:13 +0000263 diag::note_exits_objc_finally,
Chris Lattner5af280c2009-04-19 04:46:21 +0000264 AF->getAtFinallyLoc()));
265 BuildScopeInformation(AF, Scopes.size()-1);
266 }
Mike Stump1eb44332009-09-09 15:08:12 +0000267
Chris Lattner5af280c2009-04-19 04:46:21 +0000268 continue;
269 }
Mike Stump1eb44332009-09-09 15:08:12 +0000270
Chris Lattner46c3c4b2009-04-21 06:01:00 +0000271 // Disallow jumps into the protected statement of an @synchronized, but
272 // allow jumps into the object expression it protects.
273 if (ObjCAtSynchronizedStmt *AS = dyn_cast<ObjCAtSynchronizedStmt>(SubStmt)){
274 // Recursively walk the AST for the @synchronized object expr, it is
275 // evaluated in the normal scope.
276 BuildScopeInformation(AS->getSynchExpr(), ParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000277
Chris Lattner46c3c4b2009-04-21 06:01:00 +0000278 // Recursively walk the AST for the @synchronized part, protected by a new
279 // scope.
280 Scopes.push_back(GotoScope(ParentScope,
281 diag::note_protected_by_objc_synchronized,
John McCallddb0b4d2010-05-12 00:58:13 +0000282 diag::note_exits_objc_synchronized,
Chris Lattner46c3c4b2009-04-21 06:01:00 +0000283 AS->getAtSynchronizedLoc()));
284 BuildScopeInformation(AS->getSynchBody(), Scopes.size()-1);
285 continue;
286 }
Sebastian Redl972041f2009-04-27 20:27:31 +0000287
288 // Disallow jumps into any part of a C++ try statement. This is pretty
289 // much the same as for Obj-C.
290 if (CXXTryStmt *TS = dyn_cast<CXXTryStmt>(SubStmt)) {
John McCallddb0b4d2010-05-12 00:58:13 +0000291 Scopes.push_back(GotoScope(ParentScope,
292 diag::note_protected_by_cxx_try,
293 diag::note_exits_cxx_try,
Sebastian Redl972041f2009-04-27 20:27:31 +0000294 TS->getSourceRange().getBegin()));
295 if (Stmt *TryBlock = TS->getTryBlock())
296 BuildScopeInformation(TryBlock, Scopes.size()-1);
297
298 // Jump from the catch into the try is not allowed either.
Mike Stump1eb44332009-09-09 15:08:12 +0000299 for (unsigned I = 0, E = TS->getNumHandlers(); I != E; ++I) {
Sebastian Redl972041f2009-04-27 20:27:31 +0000300 CXXCatchStmt *CS = TS->getHandler(I);
301 Scopes.push_back(GotoScope(ParentScope,
302 diag::note_protected_by_cxx_catch,
John McCallddb0b4d2010-05-12 00:58:13 +0000303 diag::note_exits_cxx_catch,
Sebastian Redl972041f2009-04-27 20:27:31 +0000304 CS->getSourceRange().getBegin()));
305 BuildScopeInformation(CS->getHandlerBlock(), Scopes.size()-1);
306 }
307
308 continue;
309 }
310
Chris Lattner5af280c2009-04-19 04:46:21 +0000311 // Recursively walk the AST.
312 BuildScopeInformation(SubStmt, ParentScope);
313 }
314}
315
316/// VerifyJumps - Verify each element of the Jumps array to see if they are
317/// valid, emitting diagnostics if not.
318void JumpScopeChecker::VerifyJumps() {
319 while (!Jumps.empty()) {
320 Stmt *Jump = Jumps.pop_back_val();
Mike Stump1eb44332009-09-09 15:08:12 +0000321
322 // With a goto,
Chris Lattner5af280c2009-04-19 04:46:21 +0000323 if (GotoStmt *GS = dyn_cast<GotoStmt>(Jump)) {
324 CheckJump(GS, GS->getLabel(), GS->getGotoLoc(),
325 diag::err_goto_into_protected_scope);
326 continue;
327 }
Mike Stump1eb44332009-09-09 15:08:12 +0000328
John McCallddb0b4d2010-05-12 00:58:13 +0000329 SwitchStmt *SS = cast<SwitchStmt>(Jump);
330 for (SwitchCase *SC = SS->getSwitchCaseList(); SC;
331 SC = SC->getNextSwitchCase()) {
332 assert(LabelAndGotoScopes.count(SC) && "Case not visited?");
333 CheckJump(SS, SC, SC->getLocStart(),
334 diag::err_switch_into_protected_scope);
Chris Lattner5af280c2009-04-19 04:46:21 +0000335 }
336 }
337}
338
John McCall5e2a7ac2010-05-12 02:37:54 +0000339/// VerifyIndirectJumps - Verify whether any possible indirect jump
340/// might cross a protection boundary. Unlike direct jumps, indirect
341/// jumps count cleanups as protection boundaries: since there's no
342/// way to know where the jump is going, we can't implicitly run the
343/// right cleanups the way we can with direct jumps.
John McCallddb0b4d2010-05-12 00:58:13 +0000344///
John McCall5e2a7ac2010-05-12 02:37:54 +0000345/// Thus, an indirect jump is "trivial" if it bypasses no
346/// initializations and no teardowns. More formally, an indirect jump
347/// from A to B is trivial if the path out from A to DCA(A,B) is
348/// trivial and the path in from DCA(A,B) to B is trivial, where
349/// DCA(A,B) is the deepest common ancestor of A and B.
350/// Jump-triviality is transitive but asymmetric.
351///
John McCallddb0b4d2010-05-12 00:58:13 +0000352/// A path in is trivial if none of the entered scopes have an InDiag.
353/// A path out is trivial is none of the exited scopes have an OutDiag.
John McCall5e2a7ac2010-05-12 02:37:54 +0000354///
355/// Under these definitions, this function checks that the indirect
356/// jump between A and B is trivial for every indirect goto statement A
357/// and every label B whose address was taken in the function.
John McCallddb0b4d2010-05-12 00:58:13 +0000358void JumpScopeChecker::VerifyIndirectJumps() {
359 if (IndirectJumps.empty()) return;
360
361 // If there aren't any address-of-label expressions in this function,
362 // complain about the first indirect goto.
363 if (IndirectJumpTargets.empty()) {
364 S.Diag(IndirectJumps[0]->getGotoLoc(),
365 diag::err_indirect_goto_without_addrlabel);
366 return;
367 }
368
John McCall5e2a7ac2010-05-12 02:37:54 +0000369 // Collect a single representative of every scope containing an
370 // indirect goto. For most code bases, this substantially cuts
371 // down on the number of jump sites we'll have to consider later.
John McCallddb0b4d2010-05-12 00:58:13 +0000372 typedef std::pair<unsigned, IndirectGotoStmt*> JumpScope;
373 llvm::SmallVector<JumpScope, 32> JumpScopes;
374 {
375 llvm::DenseMap<unsigned, IndirectGotoStmt*> JumpScopesMap;
376 for (llvm::SmallVectorImpl<IndirectGotoStmt*>::iterator
377 I = IndirectJumps.begin(), E = IndirectJumps.end(); I != E; ++I) {
378 IndirectGotoStmt *IG = *I;
379 assert(LabelAndGotoScopes.count(IG) &&
380 "indirect jump didn't get added to scopes?");
381 unsigned IGScope = LabelAndGotoScopes[IG];
382 IndirectGotoStmt *&Entry = JumpScopesMap[IGScope];
383 if (!Entry) Entry = IG;
384 }
385 JumpScopes.reserve(JumpScopesMap.size());
386 for (llvm::DenseMap<unsigned, IndirectGotoStmt*>::iterator
387 I = JumpScopesMap.begin(), E = JumpScopesMap.end(); I != E; ++I)
388 JumpScopes.push_back(*I);
389 }
390
John McCall5e2a7ac2010-05-12 02:37:54 +0000391 // Collect a single representative of every scope containing a
392 // label whose address was taken somewhere in the function.
393 // For most code bases, there will be only one such scope.
John McCallddb0b4d2010-05-12 00:58:13 +0000394 llvm::DenseMap<unsigned, LabelStmt*> TargetScopes;
395 for (llvm::SmallVectorImpl<LabelStmt*>::iterator
396 I = IndirectJumpTargets.begin(), E = IndirectJumpTargets.end();
397 I != E; ++I) {
398 LabelStmt *TheLabel = *I;
399 assert(LabelAndGotoScopes.count(TheLabel) &&
400 "Referenced label didn't get added to scopes?");
401 unsigned LabelScope = LabelAndGotoScopes[TheLabel];
402 LabelStmt *&Target = TargetScopes[LabelScope];
403 if (!Target) Target = TheLabel;
404 }
405
John McCall5e2a7ac2010-05-12 02:37:54 +0000406 // For each target scope, make sure it's trivially reachable from
407 // every scope containing a jump site.
408 //
409 // A path between scopes always consists of exitting zero or more
410 // scopes, then entering zero or more scopes. We build a set of
411 // of scopes S from which the target scope can be trivially
412 // entered, then verify that every jump scope can be trivially
413 // exitted to reach a scope in S.
John McCallddb0b4d2010-05-12 00:58:13 +0000414 llvm::BitVector Reachable(Scopes.size(), false);
415 for (llvm::DenseMap<unsigned,LabelStmt*>::iterator
416 TI = TargetScopes.begin(), TE = TargetScopes.end(); TI != TE; ++TI) {
417 unsigned TargetScope = TI->first;
418 LabelStmt *TargetLabel = TI->second;
419
420 Reachable.reset();
421
422 // Mark all the enclosing scopes from which you can safely jump
John McCall5e2a7ac2010-05-12 02:37:54 +0000423 // into the target scope. 'Min' will end up being the index of
424 // the shallowest such scope.
John McCallddb0b4d2010-05-12 00:58:13 +0000425 unsigned Min = TargetScope;
426 while (true) {
427 Reachable.set(Min);
428
429 // Don't go beyond the outermost scope.
430 if (Min == 0) break;
431
John McCall5e2a7ac2010-05-12 02:37:54 +0000432 // Stop if we can't trivially enter the current scope.
John McCallddb0b4d2010-05-12 00:58:13 +0000433 if (Scopes[Min].InDiag) break;
434
435 Min = Scopes[Min].ParentScope;
436 }
437
438 // Walk through all the jump sites, checking that they can trivially
439 // reach this label scope.
440 for (llvm::SmallVectorImpl<JumpScope>::iterator
441 I = JumpScopes.begin(), E = JumpScopes.end(); I != E; ++I) {
442 unsigned Scope = I->first;
443
444 // Walk out the "scope chain" for this scope, looking for a scope
John McCall5e2a7ac2010-05-12 02:37:54 +0000445 // we've marked reachable. For well-formed code this amortizes
446 // to O(JumpScopes.size() / Scopes.size()): we only iterate
447 // when we see something unmarked, and in well-formed code we
448 // mark everything we iterate past.
John McCallddb0b4d2010-05-12 00:58:13 +0000449 bool IsReachable = false;
450 while (true) {
451 if (Reachable.test(Scope)) {
452 // If we find something reachable, mark all the scopes we just
453 // walked through as reachable.
454 for (unsigned S = I->first; S != Scope; S = Scopes[S].ParentScope)
455 Reachable.set(S);
456 IsReachable = true;
457 break;
458 }
459
460 // Don't walk out if we've reached the top-level scope or we've
461 // gotten shallower than the shallowest reachable scope.
462 if (Scope == 0 || Scope < Min) break;
463
464 // Don't walk out through an out-diagnostic.
465 if (Scopes[Scope].OutDiag) break;
466
467 Scope = Scopes[Scope].ParentScope;
468 }
469
470 // Only diagnose if we didn't find something.
471 if (IsReachable) continue;
472
473 DiagnoseIndirectJump(I->second, I->first, TargetLabel, TargetScope);
474 }
475 }
476}
477
John McCall5e2a7ac2010-05-12 02:37:54 +0000478/// Diagnose an indirect jump which is known to cross scopes.
John McCallddb0b4d2010-05-12 00:58:13 +0000479void JumpScopeChecker::DiagnoseIndirectJump(IndirectGotoStmt *Jump,
480 unsigned JumpScope,
481 LabelStmt *Target,
482 unsigned TargetScope) {
483 assert(JumpScope != TargetScope);
484
485 S.Diag(Jump->getGotoLoc(), diag::warn_indirect_goto_in_protected_scope);
486 S.Diag(Target->getIdentLoc(), diag::note_indirect_goto_target);
487
John McCall5e2a7ac2010-05-12 02:37:54 +0000488 unsigned Common = GetDeepestCommonScope(JumpScope, TargetScope);
John McCallddb0b4d2010-05-12 00:58:13 +0000489
John McCall5e2a7ac2010-05-12 02:37:54 +0000490 // Walk out the scope chain until we reach the common ancestor.
491 for (unsigned I = JumpScope; I != Common; I = Scopes[I].ParentScope)
492 if (Scopes[I].OutDiag)
493 S.Diag(Scopes[I].Loc, Scopes[I].OutDiag);
John McCallddb0b4d2010-05-12 00:58:13 +0000494
495 // Now walk into the scopes containing the label whose address was taken.
John McCall5e2a7ac2010-05-12 02:37:54 +0000496 for (unsigned I = TargetScope; I != Common; I = Scopes[I].ParentScope)
497 if (Scopes[I].InDiag)
498 S.Diag(Scopes[I].Loc, Scopes[I].InDiag);
John McCallddb0b4d2010-05-12 00:58:13 +0000499}
500
Chris Lattner5af280c2009-04-19 04:46:21 +0000501/// CheckJump - Validate that the specified jump statement is valid: that it is
502/// jumping within or out of its current scope, not into a deeper one.
503void JumpScopeChecker::CheckJump(Stmt *From, Stmt *To,
504 SourceLocation DiagLoc, unsigned JumpDiag) {
505 assert(LabelAndGotoScopes.count(From) && "Jump didn't get added to scopes?");
506 unsigned FromScope = LabelAndGotoScopes[From];
507
508 assert(LabelAndGotoScopes.count(To) && "Jump didn't get added to scopes?");
509 unsigned ToScope = LabelAndGotoScopes[To];
Mike Stump1eb44332009-09-09 15:08:12 +0000510
Chris Lattner5af280c2009-04-19 04:46:21 +0000511 // Common case: exactly the same scope, which is fine.
512 if (FromScope == ToScope) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000513
John McCall5e2a7ac2010-05-12 02:37:54 +0000514 unsigned CommonScope = GetDeepestCommonScope(FromScope, ToScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000515
John McCall5e2a7ac2010-05-12 02:37:54 +0000516 // It's okay to jump out from a nested scope.
517 if (CommonScope == ToScope) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000518
John McCall5e2a7ac2010-05-12 02:37:54 +0000519 // Pull out (and reverse) any scopes we might need to diagnose skipping.
520 llvm::SmallVector<unsigned, 10> ToScopes;
521 for (unsigned I = ToScope; I != CommonScope; I = Scopes[I].ParentScope)
522 if (Scopes[I].InDiag)
523 ToScopes.push_back(I);
Chris Lattner5af280c2009-04-19 04:46:21 +0000524
John McCall5e2a7ac2010-05-12 02:37:54 +0000525 // If the only scopes present are cleanup scopes, we're okay.
John McCallddb0b4d2010-05-12 00:58:13 +0000526 if (ToScopes.empty()) return;
527
528 S.Diag(DiagLoc, JumpDiag);
529
Chris Lattner5af280c2009-04-19 04:46:21 +0000530 // Emit diagnostics for whatever is left in ToScopes.
531 for (unsigned i = 0, e = ToScopes.size(); i != e; ++i)
John McCallddb0b4d2010-05-12 00:58:13 +0000532 S.Diag(Scopes[ToScopes[i]].Loc, Scopes[ToScopes[i]].InDiag);
Chris Lattner5af280c2009-04-19 04:46:21 +0000533}
534
535void Sema::DiagnoseInvalidJumps(Stmt *Body) {
Douglas Gregor6490ae52009-11-17 06:14:37 +0000536 (void)JumpScopeChecker(Body, *this);
Chris Lattner5af280c2009-04-19 04:46:21 +0000537}