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