Coverage Report

Created: 2024-01-17 10:31

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