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