1 //===--- JumpDiagnostics.cpp - Protected scope jump analysis ------*- C++ -*-=//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the JumpScopeChecker class, which is used to diagnose
10 // jumps that enter a protected scope in an invalid way.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "clang/Sema/SemaInternal.h"
15 #include "clang/AST/DeclCXX.h"
16 #include "clang/AST/Expr.h"
17 #include "clang/AST/ExprCXX.h"
18 #include "clang/AST/StmtCXX.h"
19 #include "clang/AST/StmtObjC.h"
20 #include "llvm/ADT/BitVector.h"
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;
33 
34   /// Permissive - True when recovering from errors, in which case precautions
35   /// are taken to handle incomplete scope information.
36   const bool Permissive;
37 
38   /// GotoScope - This is a record that we use to keep track of all of the
39   /// scopes that are introduced by VLAs and other things that scope jumps like
40   /// gotos.  This scope tree has nothing to do with the source scope tree,
41   /// because you can have multiple VLA scopes per compound statement, and most
42   /// compound statements don't introduce any scopes.
43   struct GotoScope {
44     /// ParentScope - The index in ScopeMap of the parent scope.  This is 0 for
45     /// the parent scope is the function body.
46     unsigned ParentScope;
47 
48     /// InDiag - The note to emit if there is a jump into this scope.
49     unsigned InDiag;
50 
51     /// OutDiag - The note to emit if there is an indirect jump out
52     /// of this scope.  Direct jumps always clean up their current scope
53     /// in an orderly way.
54     unsigned OutDiag;
55 
56     /// Loc - Location to emit the diagnostic.
57     SourceLocation Loc;
58 
59     GotoScope(unsigned parentScope, unsigned InDiag, unsigned OutDiag,
60               SourceLocation L)
61       : ParentScope(parentScope), InDiag(InDiag), OutDiag(OutDiag), Loc(L) {}
62   };
63 
64   SmallVector<GotoScope, 48> Scopes;
65   llvm::DenseMap<Stmt*, unsigned> LabelAndGotoScopes;
66   SmallVector<Stmt*, 16> Jumps;
67 
68   SmallVector<IndirectGotoStmt*, 4> IndirectJumps;
69   SmallVector<LabelDecl*, 4> IndirectJumpTargets;
70 public:
71   JumpScopeChecker(Stmt *Body, Sema &S);
72 private:
73   void BuildScopeInformation(Decl *D, unsigned &ParentScope);
74   void BuildScopeInformation(VarDecl *D, const BlockDecl *BDecl,
75                              unsigned &ParentScope);
76   void BuildScopeInformation(Stmt *S, unsigned &origParentScope);
77 
78   void VerifyJumps();
79   void VerifyIndirectJumps();
80   void NoteJumpIntoScopes(ArrayRef<unsigned> ToScopes);
81   void DiagnoseIndirectJump(IndirectGotoStmt *IG, unsigned IGScope,
82                             LabelDecl *Target, unsigned TargetScope);
83   void CheckJump(Stmt *From, Stmt *To, SourceLocation DiagLoc,
84                  unsigned JumpDiag, unsigned JumpDiagWarning,
85                  unsigned JumpDiagCXX98Compat);
86   void CheckGotoStmt(GotoStmt *GS);
87 
88   unsigned GetDeepestCommonScope(unsigned A, unsigned B);
89 };
90 } // end anonymous namespace
91 
92 #define CHECK_PERMISSIVE(x) (assert(Permissive || !(x)), (Permissive && (x)))
93 
94 JumpScopeChecker::JumpScopeChecker(Stmt *Body, Sema &s)
95     : S(s), Permissive(s.hasAnyUnrecoverableErrorsInThisFunction()) {
96   // Add a scope entry for function scope.
97   Scopes.push_back(GotoScope(~0U, ~0U, ~0U, SourceLocation()));
98 
99   // Build information for the top level compound statement, so that we have a
100   // defined scope record for every "goto" and label.
101   unsigned BodyParentScope = 0;
102   BuildScopeInformation(Body, BodyParentScope);
103 
104   // Check that all jumps we saw are kosher.
105   VerifyJumps();
106   VerifyIndirectJumps();
107 }
108 
109 /// GetDeepestCommonScope - Finds the innermost scope enclosing the
110 /// two scopes.
111 unsigned JumpScopeChecker::GetDeepestCommonScope(unsigned A, unsigned B) {
112   while (A != B) {
113     // Inner scopes are created after outer scopes and therefore have
114     // higher indices.
115     if (A < B) {
116       assert(Scopes[B].ParentScope < B);
117       B = Scopes[B].ParentScope;
118     } else {
119       assert(Scopes[A].ParentScope < A);
120       A = Scopes[A].ParentScope;
121     }
122   }
123   return A;
124 }
125 
126 typedef std::pair<unsigned,unsigned> ScopePair;
127 
128 /// GetDiagForGotoScopeDecl - If this decl induces a new goto scope, return a
129 /// diagnostic that should be emitted if control goes over it. If not, return 0.
130 static ScopePair GetDiagForGotoScopeDecl(Sema &S, const Decl *D) {
131   if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
132     unsigned InDiag = 0;
133     unsigned OutDiag = 0;
134 
135     if (VD->getType()->isVariablyModifiedType())
136       InDiag = diag::note_protected_by_vla;
137 
138     if (VD->hasAttr<BlocksAttr>())
139       return ScopePair(diag::note_protected_by___block,
140                        diag::note_exits___block);
141 
142     if (VD->hasAttr<CleanupAttr>())
143       return ScopePair(diag::note_protected_by_cleanup,
144                        diag::note_exits_cleanup);
145 
146     if (VD->hasLocalStorage()) {
147       switch (VD->getType().isDestructedType()) {
148       case QualType::DK_objc_strong_lifetime:
149         return ScopePair(diag::note_protected_by_objc_strong_init,
150                          diag::note_exits_objc_strong);
151 
152       case QualType::DK_objc_weak_lifetime:
153         return ScopePair(diag::note_protected_by_objc_weak_init,
154                          diag::note_exits_objc_weak);
155 
156       case QualType::DK_nontrivial_c_struct:
157         return ScopePair(diag::note_protected_by_non_trivial_c_struct_init,
158                          diag::note_exits_dtor);
159 
160       case QualType::DK_cxx_destructor:
161         OutDiag = diag::note_exits_dtor;
162         break;
163 
164       case QualType::DK_none:
165         break;
166       }
167     }
168 
169     const Expr *Init = VD->getInit();
170     if (S.Context.getLangOpts().CPlusPlus && VD->hasLocalStorage() && Init) {
171       // C++11 [stmt.dcl]p3:
172       //   A program that jumps from a point where a variable with automatic
173       //   storage duration is not in scope to a point where it is in scope
174       //   is ill-formed unless the variable has scalar type, class type with
175       //   a trivial default constructor and a trivial destructor, a
176       //   cv-qualified version of one of these types, or an array of one of
177       //   the preceding types and is declared without an initializer.
178 
179       // C++03 [stmt.dcl.p3:
180       //   A program that jumps from a point where a local variable
181       //   with automatic storage duration is not in scope to a point
182       //   where it is in scope is ill-formed unless the variable has
183       //   POD type and is declared without an initializer.
184 
185       InDiag = diag::note_protected_by_variable_init;
186 
187       // For a variable of (array of) class type declared without an
188       // initializer, we will have call-style initialization and the initializer
189       // will be the CXXConstructExpr with no intervening nodes.
190       if (const CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(Init)) {
191         const CXXConstructorDecl *Ctor = CCE->getConstructor();
192         if (Ctor->isTrivial() && Ctor->isDefaultConstructor() &&
193             VD->getInitStyle() == VarDecl::CallInit) {
194           if (OutDiag)
195             InDiag = diag::note_protected_by_variable_nontriv_destructor;
196           else if (!Ctor->getParent()->isPOD())
197             InDiag = diag::note_protected_by_variable_non_pod;
198           else
199             InDiag = 0;
200         }
201       }
202     }
203 
204     return ScopePair(InDiag, OutDiag);
205   }
206 
207   if (const TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
208     if (TD->getUnderlyingType()->isVariablyModifiedType())
209       return ScopePair(isa<TypedefDecl>(TD)
210                            ? diag::note_protected_by_vla_typedef
211                            : diag::note_protected_by_vla_type_alias,
212                        0);
213   }
214 
215   return ScopePair(0U, 0U);
216 }
217 
218 /// Build scope information for a declaration that is part of a DeclStmt.
219 void JumpScopeChecker::BuildScopeInformation(Decl *D, unsigned &ParentScope) {
220   // If this decl causes a new scope, push and switch to it.
221   std::pair<unsigned,unsigned> Diags = GetDiagForGotoScopeDecl(S, D);
222   if (Diags.first || Diags.second) {
223     Scopes.push_back(GotoScope(ParentScope, Diags.first, Diags.second,
224                                D->getLocation()));
225     ParentScope = Scopes.size()-1;
226   }
227 
228   // If the decl has an initializer, walk it with the potentially new
229   // scope we just installed.
230   if (VarDecl *VD = dyn_cast<VarDecl>(D))
231     if (Expr *Init = VD->getInit())
232       BuildScopeInformation(Init, ParentScope);
233 }
234 
235 /// Build scope information for a captured block literal variables.
236 void JumpScopeChecker::BuildScopeInformation(VarDecl *D,
237                                              const BlockDecl *BDecl,
238                                              unsigned &ParentScope) {
239   // exclude captured __block variables; there's no destructor
240   // associated with the block literal for them.
241   if (D->hasAttr<BlocksAttr>())
242     return;
243   QualType T = D->getType();
244   QualType::DestructionKind destructKind = T.isDestructedType();
245   if (destructKind != QualType::DK_none) {
246     std::pair<unsigned,unsigned> Diags;
247     switch (destructKind) {
248       case QualType::DK_cxx_destructor:
249         Diags = ScopePair(diag::note_enters_block_captures_cxx_obj,
250                           diag::note_exits_block_captures_cxx_obj);
251         break;
252       case QualType::DK_objc_strong_lifetime:
253         Diags = ScopePair(diag::note_enters_block_captures_strong,
254                           diag::note_exits_block_captures_strong);
255         break;
256       case QualType::DK_objc_weak_lifetime:
257         Diags = ScopePair(diag::note_enters_block_captures_weak,
258                           diag::note_exits_block_captures_weak);
259         break;
260       case QualType::DK_nontrivial_c_struct:
261         Diags = ScopePair(diag::note_enters_block_captures_non_trivial_c_struct,
262                           diag::note_exits_block_captures_non_trivial_c_struct);
263         break;
264       case QualType::DK_none:
265         llvm_unreachable("non-lifetime captured variable");
266     }
267     SourceLocation Loc = D->getLocation();
268     if (Loc.isInvalid())
269       Loc = BDecl->getLocation();
270     Scopes.push_back(GotoScope(ParentScope,
271                                Diags.first, Diags.second, Loc));
272     ParentScope = Scopes.size()-1;
273   }
274 }
275 
276 /// BuildScopeInformation - The statements from CI to CE are known to form a
277 /// coherent VLA scope with a specified parent node.  Walk through the
278 /// statements, adding any labels or gotos to LabelAndGotoScopes and recursively
279 /// walking the AST as needed.
280 void JumpScopeChecker::BuildScopeInformation(Stmt *S,
281                                              unsigned &origParentScope) {
282   // If this is a statement, rather than an expression, scopes within it don't
283   // propagate out into the enclosing scope.  Otherwise we have to worry
284   // about block literals, which have the lifetime of their enclosing statement.
285   unsigned independentParentScope = origParentScope;
286   unsigned &ParentScope = ((isa<Expr>(S) && !isa<StmtExpr>(S))
287                             ? origParentScope : independentParentScope);
288 
289   unsigned StmtsToSkip = 0u;
290 
291   // If we found a label, remember that it is in ParentScope scope.
292   switch (S->getStmtClass()) {
293   case Stmt::AddrLabelExprClass:
294     IndirectJumpTargets.push_back(cast<AddrLabelExpr>(S)->getLabel());
295     break;
296 
297   case Stmt::ObjCForCollectionStmtClass: {
298     auto *CS = cast<ObjCForCollectionStmt>(S);
299     unsigned Diag = diag::note_protected_by_objc_fast_enumeration;
300     unsigned NewParentScope = Scopes.size();
301     Scopes.push_back(GotoScope(ParentScope, Diag, 0, S->getBeginLoc()));
302     BuildScopeInformation(CS->getBody(), NewParentScope);
303     return;
304   }
305 
306   case Stmt::IndirectGotoStmtClass:
307     // "goto *&&lbl;" is a special case which we treat as equivalent
308     // to a normal goto.  In addition, we don't calculate scope in the
309     // operand (to avoid recording the address-of-label use), which
310     // works only because of the restricted set of expressions which
311     // we detect as constant targets.
312     if (cast<IndirectGotoStmt>(S)->getConstantTarget()) {
313       LabelAndGotoScopes[S] = ParentScope;
314       Jumps.push_back(S);
315       return;
316     }
317 
318     LabelAndGotoScopes[S] = ParentScope;
319     IndirectJumps.push_back(cast<IndirectGotoStmt>(S));
320     break;
321 
322   case Stmt::SwitchStmtClass:
323     // Evaluate the C++17 init stmt and condition variable
324     // before entering the scope of the switch statement.
325     if (Stmt *Init = cast<SwitchStmt>(S)->getInit()) {
326       BuildScopeInformation(Init, ParentScope);
327       ++StmtsToSkip;
328     }
329     if (VarDecl *Var = cast<SwitchStmt>(S)->getConditionVariable()) {
330       BuildScopeInformation(Var, ParentScope);
331       ++StmtsToSkip;
332     }
333     LLVM_FALLTHROUGH;
334 
335   case Stmt::GotoStmtClass:
336     // Remember both what scope a goto is in as well as the fact that we have
337     // it.  This makes the second scan not have to walk the AST again.
338     LabelAndGotoScopes[S] = ParentScope;
339     Jumps.push_back(S);
340     break;
341 
342   case Stmt::IfStmtClass: {
343     IfStmt *IS = cast<IfStmt>(S);
344     if (!(IS->isConstexpr() || IS->isObjCAvailabilityCheck()))
345       break;
346 
347     unsigned Diag = IS->isConstexpr() ? diag::note_protected_by_constexpr_if
348                                       : diag::note_protected_by_if_available;
349 
350     if (VarDecl *Var = IS->getConditionVariable())
351       BuildScopeInformation(Var, ParentScope);
352 
353     // Cannot jump into the middle of the condition.
354     unsigned NewParentScope = Scopes.size();
355     Scopes.push_back(GotoScope(ParentScope, Diag, 0, IS->getBeginLoc()));
356     BuildScopeInformation(IS->getCond(), NewParentScope);
357 
358     // Jumps into either arm of an 'if constexpr' are not allowed.
359     NewParentScope = Scopes.size();
360     Scopes.push_back(GotoScope(ParentScope, Diag, 0, IS->getBeginLoc()));
361     BuildScopeInformation(IS->getThen(), NewParentScope);
362     if (Stmt *Else = IS->getElse()) {
363       NewParentScope = Scopes.size();
364       Scopes.push_back(GotoScope(ParentScope, Diag, 0, IS->getBeginLoc()));
365       BuildScopeInformation(Else, NewParentScope);
366     }
367     return;
368   }
369 
370   case Stmt::CXXTryStmtClass: {
371     CXXTryStmt *TS = cast<CXXTryStmt>(S);
372     {
373       unsigned NewParentScope = Scopes.size();
374       Scopes.push_back(GotoScope(ParentScope,
375                                  diag::note_protected_by_cxx_try,
376                                  diag::note_exits_cxx_try,
377                                  TS->getSourceRange().getBegin()));
378       if (Stmt *TryBlock = TS->getTryBlock())
379         BuildScopeInformation(TryBlock, NewParentScope);
380     }
381 
382     // Jump from the catch into the try is not allowed either.
383     for (unsigned I = 0, E = TS->getNumHandlers(); I != E; ++I) {
384       CXXCatchStmt *CS = TS->getHandler(I);
385       unsigned NewParentScope = Scopes.size();
386       Scopes.push_back(GotoScope(ParentScope,
387                                  diag::note_protected_by_cxx_catch,
388                                  diag::note_exits_cxx_catch,
389                                  CS->getSourceRange().getBegin()));
390       BuildScopeInformation(CS->getHandlerBlock(), NewParentScope);
391     }
392     return;
393   }
394 
395   case Stmt::SEHTryStmtClass: {
396     SEHTryStmt *TS = cast<SEHTryStmt>(S);
397     {
398       unsigned NewParentScope = Scopes.size();
399       Scopes.push_back(GotoScope(ParentScope,
400                                  diag::note_protected_by_seh_try,
401                                  diag::note_exits_seh_try,
402                                  TS->getSourceRange().getBegin()));
403       if (Stmt *TryBlock = TS->getTryBlock())
404         BuildScopeInformation(TryBlock, NewParentScope);
405     }
406 
407     // Jump from __except or __finally into the __try are not allowed either.
408     if (SEHExceptStmt *Except = TS->getExceptHandler()) {
409       unsigned NewParentScope = Scopes.size();
410       Scopes.push_back(GotoScope(ParentScope,
411                                  diag::note_protected_by_seh_except,
412                                  diag::note_exits_seh_except,
413                                  Except->getSourceRange().getBegin()));
414       BuildScopeInformation(Except->getBlock(), NewParentScope);
415     } else if (SEHFinallyStmt *Finally = TS->getFinallyHandler()) {
416       unsigned NewParentScope = Scopes.size();
417       Scopes.push_back(GotoScope(ParentScope,
418                                  diag::note_protected_by_seh_finally,
419                                  diag::note_exits_seh_finally,
420                                  Finally->getSourceRange().getBegin()));
421       BuildScopeInformation(Finally->getBlock(), NewParentScope);
422     }
423 
424     return;
425   }
426 
427   case Stmt::DeclStmtClass: {
428     // If this is a declstmt with a VLA definition, it defines a scope from here
429     // to the end of the containing context.
430     DeclStmt *DS = cast<DeclStmt>(S);
431     // The decl statement creates a scope if any of the decls in it are VLAs
432     // or have the cleanup attribute.
433     for (auto *I : DS->decls())
434       BuildScopeInformation(I, origParentScope);
435     return;
436   }
437 
438   case Stmt::ObjCAtTryStmtClass: {
439     // Disallow jumps into any part of an @try statement by pushing a scope and
440     // walking all sub-stmts in that scope.
441     ObjCAtTryStmt *AT = cast<ObjCAtTryStmt>(S);
442     // Recursively walk the AST for the @try part.
443     {
444       unsigned NewParentScope = Scopes.size();
445       Scopes.push_back(GotoScope(ParentScope,
446                                  diag::note_protected_by_objc_try,
447                                  diag::note_exits_objc_try,
448                                  AT->getAtTryLoc()));
449       if (Stmt *TryPart = AT->getTryBody())
450         BuildScopeInformation(TryPart, NewParentScope);
451     }
452 
453     // Jump from the catch to the finally or try is not valid.
454     for (unsigned I = 0, N = AT->getNumCatchStmts(); I != N; ++I) {
455       ObjCAtCatchStmt *AC = AT->getCatchStmt(I);
456       unsigned NewParentScope = Scopes.size();
457       Scopes.push_back(GotoScope(ParentScope,
458                                  diag::note_protected_by_objc_catch,
459                                  diag::note_exits_objc_catch,
460                                  AC->getAtCatchLoc()));
461       // @catches are nested and it isn't
462       BuildScopeInformation(AC->getCatchBody(), NewParentScope);
463     }
464 
465     // Jump from the finally to the try or catch is not valid.
466     if (ObjCAtFinallyStmt *AF = AT->getFinallyStmt()) {
467       unsigned NewParentScope = Scopes.size();
468       Scopes.push_back(GotoScope(ParentScope,
469                                  diag::note_protected_by_objc_finally,
470                                  diag::note_exits_objc_finally,
471                                  AF->getAtFinallyLoc()));
472       BuildScopeInformation(AF, NewParentScope);
473     }
474 
475     return;
476   }
477 
478   case Stmt::ObjCAtSynchronizedStmtClass: {
479     // Disallow jumps into the protected statement of an @synchronized, but
480     // allow jumps into the object expression it protects.
481     ObjCAtSynchronizedStmt *AS = cast<ObjCAtSynchronizedStmt>(S);
482     // Recursively walk the AST for the @synchronized object expr, it is
483     // evaluated in the normal scope.
484     BuildScopeInformation(AS->getSynchExpr(), ParentScope);
485 
486     // Recursively walk the AST for the @synchronized part, protected by a new
487     // scope.
488     unsigned NewParentScope = Scopes.size();
489     Scopes.push_back(GotoScope(ParentScope,
490                                diag::note_protected_by_objc_synchronized,
491                                diag::note_exits_objc_synchronized,
492                                AS->getAtSynchronizedLoc()));
493     BuildScopeInformation(AS->getSynchBody(), NewParentScope);
494     return;
495   }
496 
497   case Stmt::ObjCAutoreleasePoolStmtClass: {
498     // Disallow jumps into the protected statement of an @autoreleasepool.
499     ObjCAutoreleasePoolStmt *AS = cast<ObjCAutoreleasePoolStmt>(S);
500     // Recursively walk the AST for the @autoreleasepool part, protected by a
501     // new scope.
502     unsigned NewParentScope = Scopes.size();
503     Scopes.push_back(GotoScope(ParentScope,
504                                diag::note_protected_by_objc_autoreleasepool,
505                                diag::note_exits_objc_autoreleasepool,
506                                AS->getAtLoc()));
507     BuildScopeInformation(AS->getSubStmt(), NewParentScope);
508     return;
509   }
510 
511   case Stmt::ExprWithCleanupsClass: {
512     // Disallow jumps past full-expressions that use blocks with
513     // non-trivial cleanups of their captures.  This is theoretically
514     // implementable but a lot of work which we haven't felt up to doing.
515     ExprWithCleanups *EWC = cast<ExprWithCleanups>(S);
516     for (unsigned i = 0, e = EWC->getNumObjects(); i != e; ++i) {
517       const BlockDecl *BDecl = EWC->getObject(i);
518       for (const auto &CI : BDecl->captures()) {
519         VarDecl *variable = CI.getVariable();
520         BuildScopeInformation(variable, BDecl, origParentScope);
521       }
522     }
523     break;
524   }
525 
526   case Stmt::MaterializeTemporaryExprClass: {
527     // Disallow jumps out of scopes containing temporaries lifetime-extended to
528     // automatic storage duration.
529     MaterializeTemporaryExpr *MTE = cast<MaterializeTemporaryExpr>(S);
530     if (MTE->getStorageDuration() == SD_Automatic) {
531       SmallVector<const Expr *, 4> CommaLHS;
532       SmallVector<SubobjectAdjustment, 4> Adjustments;
533       const Expr *ExtendedObject =
534           MTE->GetTemporaryExpr()->skipRValueSubobjectAdjustments(
535               CommaLHS, Adjustments);
536       if (ExtendedObject->getType().isDestructedType()) {
537         Scopes.push_back(GotoScope(ParentScope, 0,
538                                    diag::note_exits_temporary_dtor,
539                                    ExtendedObject->getExprLoc()));
540         origParentScope = Scopes.size()-1;
541       }
542     }
543     break;
544   }
545 
546   case Stmt::CaseStmtClass:
547   case Stmt::DefaultStmtClass:
548   case Stmt::LabelStmtClass:
549     LabelAndGotoScopes[S] = ParentScope;
550     break;
551 
552   default:
553     break;
554   }
555 
556   for (Stmt *SubStmt : S->children()) {
557     if (!SubStmt)
558         continue;
559     if (StmtsToSkip) {
560       --StmtsToSkip;
561       continue;
562     }
563 
564     // Cases, labels, and defaults aren't "scope parents".  It's also
565     // important to handle these iteratively instead of recursively in
566     // order to avoid blowing out the stack.
567     while (true) {
568       Stmt *Next;
569       if (SwitchCase *SC = dyn_cast<SwitchCase>(SubStmt))
570         Next = SC->getSubStmt();
571       else if (LabelStmt *LS = dyn_cast<LabelStmt>(SubStmt))
572         Next = LS->getSubStmt();
573       else
574         break;
575 
576       LabelAndGotoScopes[SubStmt] = ParentScope;
577       SubStmt = Next;
578     }
579 
580     // Recursively walk the AST.
581     BuildScopeInformation(SubStmt, ParentScope);
582   }
583 }
584 
585 /// VerifyJumps - Verify each element of the Jumps array to see if they are
586 /// valid, emitting diagnostics if not.
587 void JumpScopeChecker::VerifyJumps() {
588   while (!Jumps.empty()) {
589     Stmt *Jump = Jumps.pop_back_val();
590 
591     // With a goto,
592     if (GotoStmt *GS = dyn_cast<GotoStmt>(Jump)) {
593       // The label may not have a statement if it's coming from inline MS ASM.
594       if (GS->getLabel()->getStmt()) {
595         CheckJump(GS, GS->getLabel()->getStmt(), GS->getGotoLoc(),
596                   diag::err_goto_into_protected_scope,
597                   diag::ext_goto_into_protected_scope,
598                   diag::warn_cxx98_compat_goto_into_protected_scope);
599       }
600       CheckGotoStmt(GS);
601       continue;
602     }
603 
604     // We only get indirect gotos here when they have a constant target.
605     if (IndirectGotoStmt *IGS = dyn_cast<IndirectGotoStmt>(Jump)) {
606       LabelDecl *Target = IGS->getConstantTarget();
607       CheckJump(IGS, Target->getStmt(), IGS->getGotoLoc(),
608                 diag::err_goto_into_protected_scope,
609                 diag::ext_goto_into_protected_scope,
610                 diag::warn_cxx98_compat_goto_into_protected_scope);
611       continue;
612     }
613 
614     SwitchStmt *SS = cast<SwitchStmt>(Jump);
615     for (SwitchCase *SC = SS->getSwitchCaseList(); SC;
616          SC = SC->getNextSwitchCase()) {
617       if (CHECK_PERMISSIVE(!LabelAndGotoScopes.count(SC)))
618         continue;
619       SourceLocation Loc;
620       if (CaseStmt *CS = dyn_cast<CaseStmt>(SC))
621         Loc = CS->getBeginLoc();
622       else if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC))
623         Loc = DS->getBeginLoc();
624       else
625         Loc = SC->getBeginLoc();
626       CheckJump(SS, SC, Loc, diag::err_switch_into_protected_scope, 0,
627                 diag::warn_cxx98_compat_switch_into_protected_scope);
628     }
629   }
630 }
631 
632 /// VerifyIndirectJumps - Verify whether any possible indirect jump
633 /// might cross a protection boundary.  Unlike direct jumps, indirect
634 /// jumps count cleanups as protection boundaries:  since there's no
635 /// way to know where the jump is going, we can't implicitly run the
636 /// right cleanups the way we can with direct jumps.
637 ///
638 /// Thus, an indirect jump is "trivial" if it bypasses no
639 /// initializations and no teardowns.  More formally, an indirect jump
640 /// from A to B is trivial if the path out from A to DCA(A,B) is
641 /// trivial and the path in from DCA(A,B) to B is trivial, where
642 /// DCA(A,B) is the deepest common ancestor of A and B.
643 /// Jump-triviality is transitive but asymmetric.
644 ///
645 /// A path in is trivial if none of the entered scopes have an InDiag.
646 /// A path out is trivial is none of the exited scopes have an OutDiag.
647 ///
648 /// Under these definitions, this function checks that the indirect
649 /// jump between A and B is trivial for every indirect goto statement A
650 /// and every label B whose address was taken in the function.
651 void JumpScopeChecker::VerifyIndirectJumps() {
652   if (IndirectJumps.empty()) return;
653 
654   // If there aren't any address-of-label expressions in this function,
655   // complain about the first indirect goto.
656   if (IndirectJumpTargets.empty()) {
657     S.Diag(IndirectJumps[0]->getGotoLoc(),
658            diag::err_indirect_goto_without_addrlabel);
659     return;
660   }
661 
662   // Collect a single representative of every scope containing an
663   // indirect goto.  For most code bases, this substantially cuts
664   // down on the number of jump sites we'll have to consider later.
665   typedef std::pair<unsigned, IndirectGotoStmt*> JumpScope;
666   SmallVector<JumpScope, 32> JumpScopes;
667   {
668     llvm::DenseMap<unsigned, IndirectGotoStmt*> JumpScopesMap;
669     for (SmallVectorImpl<IndirectGotoStmt*>::iterator
670            I = IndirectJumps.begin(), E = IndirectJumps.end(); I != E; ++I) {
671       IndirectGotoStmt *IG = *I;
672       if (CHECK_PERMISSIVE(!LabelAndGotoScopes.count(IG)))
673         continue;
674       unsigned IGScope = LabelAndGotoScopes[IG];
675       IndirectGotoStmt *&Entry = JumpScopesMap[IGScope];
676       if (!Entry) Entry = IG;
677     }
678     JumpScopes.reserve(JumpScopesMap.size());
679     for (llvm::DenseMap<unsigned, IndirectGotoStmt*>::iterator
680            I = JumpScopesMap.begin(), E = JumpScopesMap.end(); I != E; ++I)
681       JumpScopes.push_back(*I);
682   }
683 
684   // Collect a single representative of every scope containing a
685   // label whose address was taken somewhere in the function.
686   // For most code bases, there will be only one such scope.
687   llvm::DenseMap<unsigned, LabelDecl*> TargetScopes;
688   for (SmallVectorImpl<LabelDecl*>::iterator
689          I = IndirectJumpTargets.begin(), E = IndirectJumpTargets.end();
690        I != E; ++I) {
691     LabelDecl *TheLabel = *I;
692     if (CHECK_PERMISSIVE(!LabelAndGotoScopes.count(TheLabel->getStmt())))
693       continue;
694     unsigned LabelScope = LabelAndGotoScopes[TheLabel->getStmt()];
695     LabelDecl *&Target = TargetScopes[LabelScope];
696     if (!Target) Target = TheLabel;
697   }
698 
699   // For each target scope, make sure it's trivially reachable from
700   // every scope containing a jump site.
701   //
702   // A path between scopes always consists of exitting zero or more
703   // scopes, then entering zero or more scopes.  We build a set of
704   // of scopes S from which the target scope can be trivially
705   // entered, then verify that every jump scope can be trivially
706   // exitted to reach a scope in S.
707   llvm::BitVector Reachable(Scopes.size(), false);
708   for (llvm::DenseMap<unsigned,LabelDecl*>::iterator
709          TI = TargetScopes.begin(), TE = TargetScopes.end(); TI != TE; ++TI) {
710     unsigned TargetScope = TI->first;
711     LabelDecl *TargetLabel = TI->second;
712 
713     Reachable.reset();
714 
715     // Mark all the enclosing scopes from which you can safely jump
716     // into the target scope.  'Min' will end up being the index of
717     // the shallowest such scope.
718     unsigned Min = TargetScope;
719     while (true) {
720       Reachable.set(Min);
721 
722       // Don't go beyond the outermost scope.
723       if (Min == 0) break;
724 
725       // Stop if we can't trivially enter the current scope.
726       if (Scopes[Min].InDiag) break;
727 
728       Min = Scopes[Min].ParentScope;
729     }
730 
731     // Walk through all the jump sites, checking that they can trivially
732     // reach this label scope.
733     for (SmallVectorImpl<JumpScope>::iterator
734            I = JumpScopes.begin(), E = JumpScopes.end(); I != E; ++I) {
735       unsigned Scope = I->first;
736 
737       // Walk out the "scope chain" for this scope, looking for a scope
738       // we've marked reachable.  For well-formed code this amortizes
739       // to O(JumpScopes.size() / Scopes.size()):  we only iterate
740       // when we see something unmarked, and in well-formed code we
741       // mark everything we iterate past.
742       bool IsReachable = false;
743       while (true) {
744         if (Reachable.test(Scope)) {
745           // If we find something reachable, mark all the scopes we just
746           // walked through as reachable.
747           for (unsigned S = I->first; S != Scope; S = Scopes[S].ParentScope)
748             Reachable.set(S);
749           IsReachable = true;
750           break;
751         }
752 
753         // Don't walk out if we've reached the top-level scope or we've
754         // gotten shallower than the shallowest reachable scope.
755         if (Scope == 0 || Scope < Min) break;
756 
757         // Don't walk out through an out-diagnostic.
758         if (Scopes[Scope].OutDiag) break;
759 
760         Scope = Scopes[Scope].ParentScope;
761       }
762 
763       // Only diagnose if we didn't find something.
764       if (IsReachable) continue;
765 
766       DiagnoseIndirectJump(I->second, I->first, TargetLabel, TargetScope);
767     }
768   }
769 }
770 
771 /// Return true if a particular error+note combination must be downgraded to a
772 /// warning in Microsoft mode.
773 static bool IsMicrosoftJumpWarning(unsigned JumpDiag, unsigned InDiagNote) {
774   return (JumpDiag == diag::err_goto_into_protected_scope &&
775          (InDiagNote == diag::note_protected_by_variable_init ||
776           InDiagNote == diag::note_protected_by_variable_nontriv_destructor));
777 }
778 
779 /// Return true if a particular note should be downgraded to a compatibility
780 /// warning in C++11 mode.
781 static bool IsCXX98CompatWarning(Sema &S, unsigned InDiagNote) {
782   return S.getLangOpts().CPlusPlus11 &&
783          InDiagNote == diag::note_protected_by_variable_non_pod;
784 }
785 
786 /// Produce primary diagnostic for an indirect jump statement.
787 static void DiagnoseIndirectJumpStmt(Sema &S, IndirectGotoStmt *Jump,
788                                      LabelDecl *Target, bool &Diagnosed) {
789   if (Diagnosed)
790     return;
791   S.Diag(Jump->getGotoLoc(), diag::err_indirect_goto_in_protected_scope);
792   S.Diag(Target->getStmt()->getIdentLoc(), diag::note_indirect_goto_target);
793   Diagnosed = true;
794 }
795 
796 /// Produce note diagnostics for a jump into a protected scope.
797 void JumpScopeChecker::NoteJumpIntoScopes(ArrayRef<unsigned> ToScopes) {
798   if (CHECK_PERMISSIVE(ToScopes.empty()))
799     return;
800   for (unsigned I = 0, E = ToScopes.size(); I != E; ++I)
801     if (Scopes[ToScopes[I]].InDiag)
802       S.Diag(Scopes[ToScopes[I]].Loc, Scopes[ToScopes[I]].InDiag);
803 }
804 
805 /// Diagnose an indirect jump which is known to cross scopes.
806 void JumpScopeChecker::DiagnoseIndirectJump(IndirectGotoStmt *Jump,
807                                             unsigned JumpScope,
808                                             LabelDecl *Target,
809                                             unsigned TargetScope) {
810   if (CHECK_PERMISSIVE(JumpScope == TargetScope))
811     return;
812 
813   unsigned Common = GetDeepestCommonScope(JumpScope, TargetScope);
814   bool Diagnosed = false;
815 
816   // Walk out the scope chain until we reach the common ancestor.
817   for (unsigned I = JumpScope; I != Common; I = Scopes[I].ParentScope)
818     if (Scopes[I].OutDiag) {
819       DiagnoseIndirectJumpStmt(S, Jump, Target, Diagnosed);
820       S.Diag(Scopes[I].Loc, Scopes[I].OutDiag);
821     }
822 
823   SmallVector<unsigned, 10> ToScopesCXX98Compat;
824 
825   // Now walk into the scopes containing the label whose address was taken.
826   for (unsigned I = TargetScope; I != Common; I = Scopes[I].ParentScope)
827     if (IsCXX98CompatWarning(S, Scopes[I].InDiag))
828       ToScopesCXX98Compat.push_back(I);
829     else if (Scopes[I].InDiag) {
830       DiagnoseIndirectJumpStmt(S, Jump, Target, Diagnosed);
831       S.Diag(Scopes[I].Loc, Scopes[I].InDiag);
832     }
833 
834   // Diagnose this jump if it would be ill-formed in C++98.
835   if (!Diagnosed && !ToScopesCXX98Compat.empty()) {
836     S.Diag(Jump->getGotoLoc(),
837            diag::warn_cxx98_compat_indirect_goto_in_protected_scope);
838     S.Diag(Target->getStmt()->getIdentLoc(), diag::note_indirect_goto_target);
839     NoteJumpIntoScopes(ToScopesCXX98Compat);
840   }
841 }
842 
843 /// CheckJump - Validate that the specified jump statement is valid: that it is
844 /// jumping within or out of its current scope, not into a deeper one.
845 void JumpScopeChecker::CheckJump(Stmt *From, Stmt *To, SourceLocation DiagLoc,
846                                unsigned JumpDiagError, unsigned JumpDiagWarning,
847                                  unsigned JumpDiagCXX98Compat) {
848   if (CHECK_PERMISSIVE(!LabelAndGotoScopes.count(From)))
849     return;
850   if (CHECK_PERMISSIVE(!LabelAndGotoScopes.count(To)))
851     return;
852 
853   unsigned FromScope = LabelAndGotoScopes[From];
854   unsigned ToScope = LabelAndGotoScopes[To];
855 
856   // Common case: exactly the same scope, which is fine.
857   if (FromScope == ToScope) return;
858 
859   // Warn on gotos out of __finally blocks.
860   if (isa<GotoStmt>(From) || isa<IndirectGotoStmt>(From)) {
861     // If FromScope > ToScope, FromScope is more nested and the jump goes to a
862     // less nested scope.  Check if it crosses a __finally along the way.
863     for (unsigned I = FromScope; I > ToScope; I = Scopes[I].ParentScope) {
864       if (Scopes[I].InDiag == diag::note_protected_by_seh_finally) {
865         S.Diag(From->getBeginLoc(), diag::warn_jump_out_of_seh_finally);
866         break;
867       }
868     }
869   }
870 
871   unsigned CommonScope = GetDeepestCommonScope(FromScope, ToScope);
872 
873   // It's okay to jump out from a nested scope.
874   if (CommonScope == ToScope) return;
875 
876   // Pull out (and reverse) any scopes we might need to diagnose skipping.
877   SmallVector<unsigned, 10> ToScopesCXX98Compat;
878   SmallVector<unsigned, 10> ToScopesError;
879   SmallVector<unsigned, 10> ToScopesWarning;
880   for (unsigned I = ToScope; I != CommonScope; I = Scopes[I].ParentScope) {
881     if (S.getLangOpts().MSVCCompat && JumpDiagWarning != 0 &&
882         IsMicrosoftJumpWarning(JumpDiagError, Scopes[I].InDiag))
883       ToScopesWarning.push_back(I);
884     else if (IsCXX98CompatWarning(S, Scopes[I].InDiag))
885       ToScopesCXX98Compat.push_back(I);
886     else if (Scopes[I].InDiag)
887       ToScopesError.push_back(I);
888   }
889 
890   // Handle warnings.
891   if (!ToScopesWarning.empty()) {
892     S.Diag(DiagLoc, JumpDiagWarning);
893     NoteJumpIntoScopes(ToScopesWarning);
894   }
895 
896   // Handle errors.
897   if (!ToScopesError.empty()) {
898     S.Diag(DiagLoc, JumpDiagError);
899     NoteJumpIntoScopes(ToScopesError);
900   }
901 
902   // Handle -Wc++98-compat warnings if the jump is well-formed.
903   if (ToScopesError.empty() && !ToScopesCXX98Compat.empty()) {
904     S.Diag(DiagLoc, JumpDiagCXX98Compat);
905     NoteJumpIntoScopes(ToScopesCXX98Compat);
906   }
907 }
908 
909 void JumpScopeChecker::CheckGotoStmt(GotoStmt *GS) {
910   if (GS->getLabel()->isMSAsmLabel()) {
911     S.Diag(GS->getGotoLoc(), diag::err_goto_ms_asm_label)
912         << GS->getLabel()->getIdentifier();
913     S.Diag(GS->getLabel()->getLocation(), diag::note_goto_ms_asm_label)
914         << GS->getLabel()->getIdentifier();
915   }
916 }
917 
918 void Sema::DiagnoseInvalidJumps(Stmt *Body) {
919   (void)JumpScopeChecker(Body, *this);
920 }
921