Coverage Report

Created: 2021-08-24 07:12

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