blob: 3a8ac464b4d8405b0f31349f1d2b918c7cf8e4a4 [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:
68 void BuildScopeInformation(Stmt *S, unsigned ParentScope);
69 void VerifyJumps();
John McCallddb0b4d2010-05-12 00:58:13 +000070 void VerifyIndirectJumps();
71 void DiagnoseIndirectJump(IndirectGotoStmt *IG, unsigned IGScope,
72 LabelStmt *Target, unsigned TargetScope);
Chris Lattner5af280c2009-04-19 04:46:21 +000073 void CheckJump(Stmt *From, Stmt *To,
74 SourceLocation DiagLoc, unsigned JumpDiag);
75};
76} // end anonymous namespace
77
78
79JumpScopeChecker::JumpScopeChecker(Stmt *Body, Sema &s) : S(s) {
80 // Add a scope entry for function scope.
John McCallddb0b4d2010-05-12 00:58:13 +000081 Scopes.push_back(GotoScope(~0U, ~0U, ~0U, SourceLocation()));
Mike Stump1eb44332009-09-09 15:08:12 +000082
Chris Lattner5af280c2009-04-19 04:46:21 +000083 // Build information for the top level compound statement, so that we have a
84 // defined scope record for every "goto" and label.
85 BuildScopeInformation(Body, 0);
Mike Stump1eb44332009-09-09 15:08:12 +000086
Chris Lattner5af280c2009-04-19 04:46:21 +000087 // Check that all jumps we saw are kosher.
88 VerifyJumps();
John McCallddb0b4d2010-05-12 00:58:13 +000089 VerifyIndirectJumps();
Chris Lattner5af280c2009-04-19 04:46:21 +000090}
Mike Stump1eb44332009-09-09 15:08:12 +000091
Chris Lattner5af280c2009-04-19 04:46:21 +000092/// GetDiagForGotoScopeDecl - If this decl induces a new goto scope, return a
93/// diagnostic that should be emitted if control goes over it. If not, return 0.
John McCallddb0b4d2010-05-12 00:58:13 +000094static std::pair<unsigned,unsigned>
95 GetDiagForGotoScopeDecl(const Decl *D, bool isCPlusPlus) {
Chris Lattner5af280c2009-04-19 04:46:21 +000096 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
John McCallddb0b4d2010-05-12 00:58:13 +000097 unsigned InDiag = 0, OutDiag = 0;
Chris Lattner5af280c2009-04-19 04:46:21 +000098 if (VD->getType()->isVariablyModifiedType())
John McCallddb0b4d2010-05-12 00:58:13 +000099 InDiag = diag::note_protected_by_vla;
100
101 if (VD->hasAttr<BlocksAttr>()) {
102 InDiag = diag::note_protected_by___block;
103 OutDiag = diag::note_exits___block;
104 } else if (VD->hasAttr<CleanupAttr>()) {
105 InDiag = diag::note_protected_by_cleanup;
106 OutDiag = diag::note_exits_cleanup;
107 } else if (isCPlusPlus) {
108 // FIXME: In C++0x, we have to check more conditions than "did we
109 // just give it an initializer?". See 6.7p3.
110 if (VD->hasLocalStorage() && VD->hasInit())
111 InDiag = diag::note_protected_by_variable_init;
112
113 CanQualType T = VD->getType()->getCanonicalTypeUnqualified();
114 while (CanQual<ArrayType> AT = T->getAs<ArrayType>())
115 T = AT->getElementType();
116 if (CanQual<RecordType> RT = T->getAs<RecordType>())
117 if (!cast<CXXRecordDecl>(RT->getDecl())->hasTrivialDestructor())
118 OutDiag = diag::note_exits_dtor;
119 }
Chris Lattner6d97e5e2010-03-01 20:59:53 +0000120
John McCallddb0b4d2010-05-12 00:58:13 +0000121 return std::make_pair(InDiag, OutDiag);
Chris Lattner5af280c2009-04-19 04:46:21 +0000122 }
Mike Stump1eb44332009-09-09 15:08:12 +0000123
John McCallddb0b4d2010-05-12 00:58:13 +0000124 if (const TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
125 if (TD->getUnderlyingType()->isVariablyModifiedType())
126 return std::make_pair((unsigned) diag::note_protected_by_vla_typedef, 0);
127 }
128
129 return std::make_pair(0U, 0U);
Chris Lattner5af280c2009-04-19 04:46:21 +0000130}
131
132
133/// BuildScopeInformation - The statements from CI to CE are known to form a
134/// coherent VLA scope with a specified parent node. Walk through the
135/// statements, adding any labels or gotos to LabelAndGotoScopes and recursively
136/// walking the AST as needed.
137void JumpScopeChecker::BuildScopeInformation(Stmt *S, unsigned ParentScope) {
Mike Stump1eb44332009-09-09 15:08:12 +0000138
Chris Lattner5af280c2009-04-19 04:46:21 +0000139 // If we found a label, remember that it is in ParentScope scope.
John McCallddb0b4d2010-05-12 00:58:13 +0000140 switch (S->getStmtClass()) {
141 case Stmt::LabelStmtClass:
142 case Stmt::DefaultStmtClass:
143 case Stmt::CaseStmtClass:
Chris Lattner5af280c2009-04-19 04:46:21 +0000144 LabelAndGotoScopes[S] = ParentScope;
John McCallddb0b4d2010-05-12 00:58:13 +0000145 break;
146
147 case Stmt::AddrLabelExprClass:
148 IndirectJumpTargets.push_back(cast<AddrLabelExpr>(S)->getLabel());
149 break;
150
151 case Stmt::IndirectGotoStmtClass:
152 LabelAndGotoScopes[S] = ParentScope;
153 IndirectJumps.push_back(cast<IndirectGotoStmt>(S));
154 break;
155
156 case Stmt::GotoStmtClass:
157 case Stmt::SwitchStmtClass:
Chris Lattner5af280c2009-04-19 04:46:21 +0000158 // Remember both what scope a goto is in as well as the fact that we have
159 // it. This makes the second scan not have to walk the AST again.
160 LabelAndGotoScopes[S] = ParentScope;
161 Jumps.push_back(S);
John McCallddb0b4d2010-05-12 00:58:13 +0000162 break;
163
164 default:
165 break;
Chris Lattner5af280c2009-04-19 04:46:21 +0000166 }
Mike Stump1eb44332009-09-09 15:08:12 +0000167
Chris Lattner5af280c2009-04-19 04:46:21 +0000168 for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); CI != E;
169 ++CI) {
170 Stmt *SubStmt = *CI;
171 if (SubStmt == 0) continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000172
Chris Lattner6d97e5e2010-03-01 20:59:53 +0000173 bool isCPlusPlus = this->S.getLangOptions().CPlusPlus;
Chris Lattner5af280c2009-04-19 04:46:21 +0000174
175 // If this is a declstmt with a VLA definition, it defines a scope from here
176 // to the end of the containing context.
177 if (DeclStmt *DS = dyn_cast<DeclStmt>(SubStmt)) {
Chris Lattner6d97e5e2010-03-01 20:59:53 +0000178 // The decl statement creates a scope if any of the decls in it are VLAs
179 // or have the cleanup attribute.
Chris Lattner5af280c2009-04-19 04:46:21 +0000180 for (DeclStmt::decl_iterator I = DS->decl_begin(), E = DS->decl_end();
181 I != E; ++I) {
182 // If this decl causes a new scope, push and switch to it.
John McCallddb0b4d2010-05-12 00:58:13 +0000183 std::pair<unsigned,unsigned> Diags
184 = GetDiagForGotoScopeDecl(*I, isCPlusPlus);
185 if (Diags.first || Diags.second) {
186 Scopes.push_back(GotoScope(ParentScope, Diags.first, Diags.second,
187 (*I)->getLocation()));
Chris Lattner5af280c2009-04-19 04:46:21 +0000188 ParentScope = Scopes.size()-1;
189 }
Mike Stump1eb44332009-09-09 15:08:12 +0000190
Chris Lattner5af280c2009-04-19 04:46:21 +0000191 // If the decl has an initializer, walk it with the potentially new
192 // scope we just installed.
193 if (VarDecl *VD = dyn_cast<VarDecl>(*I))
194 if (Expr *Init = VD->getInit())
195 BuildScopeInformation(Init, ParentScope);
196 }
197 continue;
198 }
199
200 // Disallow jumps into any part of an @try statement by pushing a scope and
201 // walking all sub-stmts in that scope.
202 if (ObjCAtTryStmt *AT = dyn_cast<ObjCAtTryStmt>(SubStmt)) {
203 // Recursively walk the AST for the @try part.
John McCallddb0b4d2010-05-12 00:58:13 +0000204 Scopes.push_back(GotoScope(ParentScope,
205 diag::note_protected_by_objc_try,
206 diag::note_exits_objc_try,
Chris Lattner5af280c2009-04-19 04:46:21 +0000207 AT->getAtTryLoc()));
208 if (Stmt *TryPart = AT->getTryBody())
209 BuildScopeInformation(TryPart, Scopes.size()-1);
210
211 // Jump from the catch to the finally or try is not valid.
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000212 for (unsigned I = 0, N = AT->getNumCatchStmts(); I != N; ++I) {
213 ObjCAtCatchStmt *AC = AT->getCatchStmt(I);
Chris Lattner5af280c2009-04-19 04:46:21 +0000214 Scopes.push_back(GotoScope(ParentScope,
215 diag::note_protected_by_objc_catch,
John McCallddb0b4d2010-05-12 00:58:13 +0000216 diag::note_exits_objc_catch,
Chris Lattner5af280c2009-04-19 04:46:21 +0000217 AC->getAtCatchLoc()));
Mike Stump1eb44332009-09-09 15:08:12 +0000218 // @catches are nested and it isn't
Chris Lattner5af280c2009-04-19 04:46:21 +0000219 BuildScopeInformation(AC->getCatchBody(), Scopes.size()-1);
220 }
Mike Stump1eb44332009-09-09 15:08:12 +0000221
Chris Lattner5af280c2009-04-19 04:46:21 +0000222 // Jump from the finally to the try or catch is not valid.
223 if (ObjCAtFinallyStmt *AF = AT->getFinallyStmt()) {
224 Scopes.push_back(GotoScope(ParentScope,
225 diag::note_protected_by_objc_finally,
John McCallddb0b4d2010-05-12 00:58:13 +0000226 diag::note_exits_objc_finally,
Chris Lattner5af280c2009-04-19 04:46:21 +0000227 AF->getAtFinallyLoc()));
228 BuildScopeInformation(AF, Scopes.size()-1);
229 }
Mike Stump1eb44332009-09-09 15:08:12 +0000230
Chris Lattner5af280c2009-04-19 04:46:21 +0000231 continue;
232 }
Mike Stump1eb44332009-09-09 15:08:12 +0000233
Chris Lattner46c3c4b2009-04-21 06:01:00 +0000234 // Disallow jumps into the protected statement of an @synchronized, but
235 // allow jumps into the object expression it protects.
236 if (ObjCAtSynchronizedStmt *AS = dyn_cast<ObjCAtSynchronizedStmt>(SubStmt)){
237 // Recursively walk the AST for the @synchronized object expr, it is
238 // evaluated in the normal scope.
239 BuildScopeInformation(AS->getSynchExpr(), ParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000240
Chris Lattner46c3c4b2009-04-21 06:01:00 +0000241 // Recursively walk the AST for the @synchronized part, protected by a new
242 // scope.
243 Scopes.push_back(GotoScope(ParentScope,
244 diag::note_protected_by_objc_synchronized,
John McCallddb0b4d2010-05-12 00:58:13 +0000245 diag::note_exits_objc_synchronized,
Chris Lattner46c3c4b2009-04-21 06:01:00 +0000246 AS->getAtSynchronizedLoc()));
247 BuildScopeInformation(AS->getSynchBody(), Scopes.size()-1);
248 continue;
249 }
Sebastian Redl972041f2009-04-27 20:27:31 +0000250
251 // Disallow jumps into any part of a C++ try statement. This is pretty
252 // much the same as for Obj-C.
253 if (CXXTryStmt *TS = dyn_cast<CXXTryStmt>(SubStmt)) {
John McCallddb0b4d2010-05-12 00:58:13 +0000254 Scopes.push_back(GotoScope(ParentScope,
255 diag::note_protected_by_cxx_try,
256 diag::note_exits_cxx_try,
Sebastian Redl972041f2009-04-27 20:27:31 +0000257 TS->getSourceRange().getBegin()));
258 if (Stmt *TryBlock = TS->getTryBlock())
259 BuildScopeInformation(TryBlock, Scopes.size()-1);
260
261 // Jump from the catch into the try is not allowed either.
Mike Stump1eb44332009-09-09 15:08:12 +0000262 for (unsigned I = 0, E = TS->getNumHandlers(); I != E; ++I) {
Sebastian Redl972041f2009-04-27 20:27:31 +0000263 CXXCatchStmt *CS = TS->getHandler(I);
264 Scopes.push_back(GotoScope(ParentScope,
265 diag::note_protected_by_cxx_catch,
John McCallddb0b4d2010-05-12 00:58:13 +0000266 diag::note_exits_cxx_catch,
Sebastian Redl972041f2009-04-27 20:27:31 +0000267 CS->getSourceRange().getBegin()));
268 BuildScopeInformation(CS->getHandlerBlock(), Scopes.size()-1);
269 }
270
271 continue;
272 }
273
Chris Lattner5af280c2009-04-19 04:46:21 +0000274 // Recursively walk the AST.
275 BuildScopeInformation(SubStmt, ParentScope);
276 }
277}
278
279/// VerifyJumps - Verify each element of the Jumps array to see if they are
280/// valid, emitting diagnostics if not.
281void JumpScopeChecker::VerifyJumps() {
282 while (!Jumps.empty()) {
283 Stmt *Jump = Jumps.pop_back_val();
Mike Stump1eb44332009-09-09 15:08:12 +0000284
285 // With a goto,
Chris Lattner5af280c2009-04-19 04:46:21 +0000286 if (GotoStmt *GS = dyn_cast<GotoStmt>(Jump)) {
287 CheckJump(GS, GS->getLabel(), GS->getGotoLoc(),
288 diag::err_goto_into_protected_scope);
289 continue;
290 }
Mike Stump1eb44332009-09-09 15:08:12 +0000291
John McCallddb0b4d2010-05-12 00:58:13 +0000292 SwitchStmt *SS = cast<SwitchStmt>(Jump);
293 for (SwitchCase *SC = SS->getSwitchCaseList(); SC;
294 SC = SC->getNextSwitchCase()) {
295 assert(LabelAndGotoScopes.count(SC) && "Case not visited?");
296 CheckJump(SS, SC, SC->getLocStart(),
297 diag::err_switch_into_protected_scope);
Chris Lattner5af280c2009-04-19 04:46:21 +0000298 }
299 }
300}
301
John McCallddb0b4d2010-05-12 00:58:13 +0000302/// VerifyIndirectJumps - Verify whether any possible indirect jump might
303/// cross a protection boundary.
304///
305/// An indirect jump is "trivial" if it bypasses no initializations
306/// and no teardowns. More formally, the jump from A to B is trivial
307/// if the path out from A to DCA(A,B) is trivial and the path in from
308/// DCA(A,B) to B is trivial, where DCA(A,B) is the deepest common
309/// ancestor of A and B.
310/// A path in is trivial if none of the entered scopes have an InDiag.
311/// A path out is trivial is none of the exited scopes have an OutDiag.
312/// Jump-triviality is transitive but asymmetric.
313void JumpScopeChecker::VerifyIndirectJumps() {
314 if (IndirectJumps.empty()) return;
315
316 // If there aren't any address-of-label expressions in this function,
317 // complain about the first indirect goto.
318 if (IndirectJumpTargets.empty()) {
319 S.Diag(IndirectJumps[0]->getGotoLoc(),
320 diag::err_indirect_goto_without_addrlabel);
321 return;
322 }
323
324 // Build a vector of source scopes. This serves to unique source
325 // scopes as well as to eliminate redundant lookups into
326 // LabelAndGotoScopes.
327 typedef std::pair<unsigned, IndirectGotoStmt*> JumpScope;
328 llvm::SmallVector<JumpScope, 32> JumpScopes;
329 {
330 llvm::DenseMap<unsigned, IndirectGotoStmt*> JumpScopesMap;
331 for (llvm::SmallVectorImpl<IndirectGotoStmt*>::iterator
332 I = IndirectJumps.begin(), E = IndirectJumps.end(); I != E; ++I) {
333 IndirectGotoStmt *IG = *I;
334 assert(LabelAndGotoScopes.count(IG) &&
335 "indirect jump didn't get added to scopes?");
336 unsigned IGScope = LabelAndGotoScopes[IG];
337 IndirectGotoStmt *&Entry = JumpScopesMap[IGScope];
338 if (!Entry) Entry = IG;
339 }
340 JumpScopes.reserve(JumpScopesMap.size());
341 for (llvm::DenseMap<unsigned, IndirectGotoStmt*>::iterator
342 I = JumpScopesMap.begin(), E = JumpScopesMap.end(); I != E; ++I)
343 JumpScopes.push_back(*I);
344 }
345
346 // Find a representative label from each protection scope.
347 llvm::DenseMap<unsigned, LabelStmt*> TargetScopes;
348 for (llvm::SmallVectorImpl<LabelStmt*>::iterator
349 I = IndirectJumpTargets.begin(), E = IndirectJumpTargets.end();
350 I != E; ++I) {
351 LabelStmt *TheLabel = *I;
352 assert(LabelAndGotoScopes.count(TheLabel) &&
353 "Referenced label didn't get added to scopes?");
354 unsigned LabelScope = LabelAndGotoScopes[TheLabel];
355 LabelStmt *&Target = TargetScopes[LabelScope];
356 if (!Target) Target = TheLabel;
357 }
358
359 llvm::BitVector Reachable(Scopes.size(), false);
360 for (llvm::DenseMap<unsigned,LabelStmt*>::iterator
361 TI = TargetScopes.begin(), TE = TargetScopes.end(); TI != TE; ++TI) {
362 unsigned TargetScope = TI->first;
363 LabelStmt *TargetLabel = TI->second;
364
365 Reachable.reset();
366
367 // Mark all the enclosing scopes from which you can safely jump
368 // into the target scope.
369 unsigned Min = TargetScope;
370 while (true) {
371 Reachable.set(Min);
372
373 // Don't go beyond the outermost scope.
374 if (Min == 0) break;
375
376 // Don't go further if we couldn't trivially enter this scope.
377 if (Scopes[Min].InDiag) break;
378
379 Min = Scopes[Min].ParentScope;
380 }
381
382 // Walk through all the jump sites, checking that they can trivially
383 // reach this label scope.
384 for (llvm::SmallVectorImpl<JumpScope>::iterator
385 I = JumpScopes.begin(), E = JumpScopes.end(); I != E; ++I) {
386 unsigned Scope = I->first;
387
388 // Walk out the "scope chain" for this scope, looking for a scope
389 // we've marked reachable.
390 bool IsReachable = false;
391 while (true) {
392 if (Reachable.test(Scope)) {
393 // If we find something reachable, mark all the scopes we just
394 // walked through as reachable.
395 for (unsigned S = I->first; S != Scope; S = Scopes[S].ParentScope)
396 Reachable.set(S);
397 IsReachable = true;
398 break;
399 }
400
401 // Don't walk out if we've reached the top-level scope or we've
402 // gotten shallower than the shallowest reachable scope.
403 if (Scope == 0 || Scope < Min) break;
404
405 // Don't walk out through an out-diagnostic.
406 if (Scopes[Scope].OutDiag) break;
407
408 Scope = Scopes[Scope].ParentScope;
409 }
410
411 // Only diagnose if we didn't find something.
412 if (IsReachable) continue;
413
414 DiagnoseIndirectJump(I->second, I->first, TargetLabel, TargetScope);
415 }
416 }
417}
418
419void JumpScopeChecker::DiagnoseIndirectJump(IndirectGotoStmt *Jump,
420 unsigned JumpScope,
421 LabelStmt *Target,
422 unsigned TargetScope) {
423 assert(JumpScope != TargetScope);
424
425 S.Diag(Jump->getGotoLoc(), diag::warn_indirect_goto_in_protected_scope);
426 S.Diag(Target->getIdentLoc(), diag::note_indirect_goto_target);
427
428 // Collect everything in the target scope chain.
429 llvm::DenseSet<unsigned> TargetScopeChain;
430 for (unsigned SI = TargetScope; SI != 0; SI = Scopes[SI].ParentScope)
431 TargetScopeChain.insert(SI);
432 TargetScopeChain.insert(0);
433
434 // Walk out the scopes containing the indirect goto until we find a
435 // common ancestor with the target label.
436 unsigned Common = JumpScope;
437 while (!TargetScopeChain.count(Common)) {
438 // FIXME: this isn't necessarily a problem! Not every protected
439 // scope requires destruction.
440 S.Diag(Scopes[Common].Loc, Scopes[Common].OutDiag);
441 Common = Scopes[Common].ParentScope;
442 }
443
444 // Now walk into the scopes containing the label whose address was taken.
445 for (unsigned SI = TargetScope; SI != Common; SI = Scopes[SI].ParentScope)
446 S.Diag(Scopes[SI].Loc, Scopes[SI].InDiag);
447}
448
Chris Lattner5af280c2009-04-19 04:46:21 +0000449/// CheckJump - Validate that the specified jump statement is valid: that it is
450/// jumping within or out of its current scope, not into a deeper one.
451void JumpScopeChecker::CheckJump(Stmt *From, Stmt *To,
452 SourceLocation DiagLoc, unsigned JumpDiag) {
453 assert(LabelAndGotoScopes.count(From) && "Jump didn't get added to scopes?");
454 unsigned FromScope = LabelAndGotoScopes[From];
455
456 assert(LabelAndGotoScopes.count(To) && "Jump didn't get added to scopes?");
457 unsigned ToScope = LabelAndGotoScopes[To];
Mike Stump1eb44332009-09-09 15:08:12 +0000458
Chris Lattner5af280c2009-04-19 04:46:21 +0000459 // Common case: exactly the same scope, which is fine.
460 if (FromScope == ToScope) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000461
Chris Lattner5af280c2009-04-19 04:46:21 +0000462 // The only valid mismatch jump case happens when the jump is more deeply
463 // nested inside the jump target. Do a quick scan to see if the jump is valid
464 // because valid code is more common than invalid code.
465 unsigned TestScope = Scopes[FromScope].ParentScope;
466 while (TestScope != ~0U) {
467 // If we found the jump target, then we're jumping out of our current scope,
468 // which is perfectly fine.
469 if (TestScope == ToScope) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000470
Chris Lattner5af280c2009-04-19 04:46:21 +0000471 // Otherwise, scan up the hierarchy.
472 TestScope = Scopes[TestScope].ParentScope;
473 }
Mike Stump1eb44332009-09-09 15:08:12 +0000474
John McCallddb0b4d2010-05-12 00:58:13 +0000475 // If we get here, then either we have invalid code or we're jumping in
476 // past some cleanup blocks. It may seem strange to have a declaration
477 // with a trivial constructor and a non-trivial destructor, but it's
478 // possible.
Chris Lattner5af280c2009-04-19 04:46:21 +0000479
480 // Eliminate the common prefix of the jump and the target. Start by
481 // linearizing both scopes, reversing them as we go.
482 std::vector<unsigned> FromScopes, ToScopes;
483 for (TestScope = FromScope; TestScope != ~0U;
484 TestScope = Scopes[TestScope].ParentScope)
485 FromScopes.push_back(TestScope);
486 for (TestScope = ToScope; TestScope != ~0U;
487 TestScope = Scopes[TestScope].ParentScope)
488 ToScopes.push_back(TestScope);
489
490 // Remove any common entries (such as the top-level function scope).
John McCallddb0b4d2010-05-12 00:58:13 +0000491 while (!FromScopes.empty() && (FromScopes.back() == ToScopes.back())) {
Chris Lattner5af280c2009-04-19 04:46:21 +0000492 FromScopes.pop_back();
493 ToScopes.pop_back();
494 }
Mike Stump1eb44332009-09-09 15:08:12 +0000495
John McCallddb0b4d2010-05-12 00:58:13 +0000496 // Ignore any cleanup blocks on the way in.
497 while (!ToScopes.empty()) {
498 if (Scopes[ToScopes.back()].InDiag) break;
499 ToScopes.pop_back();
500 }
501 if (ToScopes.empty()) return;
502
503 S.Diag(DiagLoc, JumpDiag);
504
Chris Lattner5af280c2009-04-19 04:46:21 +0000505 // Emit diagnostics for whatever is left in ToScopes.
506 for (unsigned i = 0, e = ToScopes.size(); i != e; ++i)
John McCallddb0b4d2010-05-12 00:58:13 +0000507 S.Diag(Scopes[ToScopes[i]].Loc, Scopes[ToScopes[i]].InDiag);
Chris Lattner5af280c2009-04-19 04:46:21 +0000508}
509
510void Sema::DiagnoseInvalidJumps(Stmt *Body) {
Douglas Gregor6490ae52009-11-17 06:14:37 +0000511 (void)JumpScopeChecker(Body, *this);
Chris Lattner5af280c2009-04-19 04:46:21 +0000512}