Coverage Report

Created: 2022-01-18 06:27

/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
40.3k
      : 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
63.4k
#define CHECK_PERMISSIVE(x) (assert(Permissive || !(x)), (Permissive && 
(x)1.20k
))
105
106
JumpScopeChecker::JumpScopeChecker(Stmt *Body, Sema &s)
107
5.63k
    : S(s), Permissive(s.hasAnyUnrecoverableErrorsInThisFunction()) {
108
  // Add a scope entry for function scope.
109
5.63k
  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.63k
  unsigned BodyParentScope = 0;
114
5.63k
  BuildScopeInformation(Body, BodyParentScope);
115
116
  // Check that all jumps we saw are kosher.
117
5.63k
  VerifyJumps();
118
5.63k
  VerifyIndirectOrAsmJumps(false);
119
5.63k
  VerifyIndirectOrAsmJumps(true);
120
5.63k
  VerifyMustTailStmts();
121
5.63k
}
122
123
/// GetDeepestCommonScope - Finds the innermost scope enclosing the
124
/// two scopes.
125
909
unsigned JumpScopeChecker::GetDeepestCommonScope(unsigned A, unsigned B) {
126
3.75k
  while (A != B) {
127
    // Inner scopes are created after outer scopes and therefore have
128
    // higher indices.
129
2.84k
    if (A < B) {
130
279
      assert(Scopes[B].ParentScope < B);
131
0
      B = Scopes[B].ParentScope;
132
2.56k
    } else {
133
2.56k
      assert(Scopes[A].ParentScope < A);
134
0
      A = Scopes[A].ParentScope;
135
2.56k
    }
136
2.84k
  }
137
909
  return A;
138
909
}
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
40.8k
static ScopePair GetDiagForGotoScopeDecl(Sema &S, const Decl *D) {
145
40.8k
  if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
146
36.2k
    unsigned InDiag = 0;
147
36.2k
    unsigned OutDiag = 0;
148
149
36.2k
    if (VD->getType()->isVariablyModifiedType())
150
39
      InDiag = diag::note_protected_by_vla;
151
152
36.2k
    if (VD->hasAttr<BlocksAttr>())
153
3
      return ScopePair(diag::note_protected_by___block,
154
3
                       diag::note_exits___block);
155
156
36.2k
    if (VD->hasAttr<CleanupAttr>())
157
1
      return ScopePair(diag::note_protected_by_cleanup,
158
1
                       diag::note_exits_cleanup);
159
160
36.2k
    if (VD->hasLocalStorage()) {
161
35.8k
      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
495
      case QualType::DK_cxx_destructor:
175
495
        OutDiag = diag::note_exits_dtor;
176
495
        break;
177
178
35.3k
      case QualType::DK_none:
179
35.3k
        break;
180
35.8k
      }
181
35.8k
    }
182
183
36.2k
    const Expr *Init = VD->getInit();
184
36.2k
    if (S.Context.getLangOpts().CPlusPlus && 
VD->hasLocalStorage()35.9k
&&
Init35.5k
) {
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
32.4k
      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
32.4k
      if (const CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(Init)) {
205
2.47k
        const CXXConstructorDecl *Ctor = CCE->getConstructor();
206
2.47k
        if (Ctor->isTrivial() && 
Ctor->isDefaultConstructor()397
&&
207
2.47k
            
VD->getInitStyle() == VarDecl::CallInit268
) {
208
257
          if (OutDiag)
209
85
            InDiag = diag::note_protected_by_variable_nontriv_destructor;
210
172
          else if (!Ctor->getParent()->isPOD())
211
25
            InDiag = diag::note_protected_by_variable_non_pod;
212
147
          else
213
147
            InDiag = 0;
214
257
        }
215
2.47k
      }
216
32.4k
    }
217
218
36.2k
    return ScopePair(InDiag, OutDiag);
219
36.2k
  }
220
221
4.55k
  if (const TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
222
4.13k
    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
4.13k
  }
228
229
4.55k
  return ScopePair(0U, 0U);
230
4.55k
}
231
232
/// Build scope information for a declaration that is part of a DeclStmt.
233
40.8k
void JumpScopeChecker::BuildScopeInformation(Decl *D, unsigned &ParentScope) {
234
  // If this decl causes a new scope, push and switch to it.
235
40.8k
  std::pair<unsigned,unsigned> Diags = GetDiagForGotoScopeDecl(S, D);
236
40.8k
  if (Diags.first || 
Diags.second8.42k
) {
237
32.4k
    Scopes.push_back(GotoScope(ParentScope, Diags.first, Diags.second,
238
32.4k
                               D->getLocation()));
239
32.4k
    ParentScope = Scopes.size()-1;
240
32.4k
  }
241
242
  // If the decl has an initializer, walk it with the potentially new
243
  // scope we just installed.
244
40.8k
  if (VarDecl *VD = dyn_cast<VarDecl>(D))
245
36.2k
    if (Expr *Init = VD->getInit())
246
33.0k
      BuildScopeInformation(Init, ParentScope);
247
40.8k
}
248
249
/// Build scope information for a captured block literal variables.
250
void JumpScopeChecker::BuildScopeInformation(VarDecl *D,
251
                                             const BlockDecl *BDecl,
252
111
                                             unsigned &ParentScope) {
253
  // exclude captured __block variables; there's no destructor
254
  // associated with the block literal for them.
255
111
  if (D->hasAttr<BlocksAttr>())
256
2
    return;
257
109
  QualType T = D->getType();
258
109
  QualType::DestructionKind destructKind = T.isDestructedType();
259
109
  if (destructKind != QualType::DK_none) {
260
19
    std::pair<unsigned,unsigned> Diags;
261
19
    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
16
      case QualType::DK_objc_strong_lifetime:
267
16
        Diags = ScopePair(diag::note_enters_block_captures_strong,
268
16
                          diag::note_exits_block_captures_strong);
269
16
        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
19
    }
281
19
    SourceLocation Loc = D->getLocation();
282
19
    if (Loc.isInvalid())
283
9
      Loc = BDecl->getLocation();
284
19
    Scopes.push_back(GotoScope(ParentScope,
285
19
                               Diags.first, Diags.second, Loc));
286
19
    ParentScope = Scopes.size()-1;
287
19
  }
288
109
}
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.11M
                                             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.11M
  unsigned independentParentScope = origParentScope;
310
1.11M
  unsigned &ParentScope = ((isa<Expr>(S) && 
!isa<StmtExpr>(S)923k
)
311
1.11M
                            ? 
origParentScope922k
:
independentParentScope195k
);
312
313
1.11M
  unsigned StmtsToSkip = 0u;
314
315
  // If we found a label, remember that it is in ParentScope scope.
316
1.11M
  switch (S->getStmtClass()) {
317
190
  case Stmt::AddrLabelExprClass:
318
190
    IndirectJumpTargets.push_back(cast<AddrLabelExpr>(S)->getLabel());
319
190
    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
130
  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
130
    if (cast<IndirectGotoStmt>(S)->getConstantTarget()) {
337
10
      LabelAndGotoScopes[S] = ParentScope;
338
10
      Jumps.push_back(S);
339
10
      return;
340
10
    }
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
68
      BuildScopeInformation(Init, ParentScope);
351
68
      ++StmtsToSkip;
352
68
    }
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.17k
  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.17k
    LabelAndGotoScopes[S] = ParentScope;
363
7.17k
    Jumps.push_back(S);
364
7.17k
    break;
365
366
29
  case Stmt::GCCAsmStmtClass:
367
29
    if (auto *GS = dyn_cast<GCCAsmStmt>(S))
368
29
      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
18
        LabelAndGotoScopes[S] = ParentScope;
372
18
        AsmJumps.push_back(GS);
373
18
        for (auto *E : GS->labels())
374
21
          AsmJumpTargets.push_back(E->getLabel());
375
18
      }
376
29
    break;
377
378
36.3k
  case Stmt::IfStmtClass: {
379
36.3k
    IfStmt *IS = cast<IfStmt>(S);
380
36.3k
    if (!(IS->isConstexpr() || 
IS->isConsteval()36.3k
||
381
36.3k
          
IS->isObjCAvailabilityCheck()36.3k
))
382
36.3k
      break;
383
384
19
    unsigned Diag = diag::note_protected_by_if_available;
385
19
    if (IS->isConstexpr())
386
14
      Diag = diag::note_protected_by_constexpr_if;
387
5
    else if (IS->isConsteval())
388
3
      Diag = diag::note_protected_by_consteval_if;
389
390
19
    if (VarDecl *Var = IS->getConditionVariable())
391
0
      BuildScopeInformation(Var, ParentScope);
392
393
    // Cannot jump into the middle of the condition.
394
19
    unsigned NewParentScope = Scopes.size();
395
19
    Scopes.push_back(GotoScope(ParentScope, Diag, 0, IS->getBeginLoc()));
396
397
19
    if (!IS->isConsteval())
398
16
      BuildScopeInformation(IS->getCond(), NewParentScope);
399
400
    // Jumps into either arm of an 'if constexpr' are not allowed.
401
19
    NewParentScope = Scopes.size();
402
19
    Scopes.push_back(GotoScope(ParentScope, Diag, 0, IS->getBeginLoc()));
403
19
    BuildScopeInformation(IS->getThen(), NewParentScope);
404
19
    if (Stmt *Else = IS->getElse()) {
405
12
      NewParentScope = Scopes.size();
406
12
      Scopes.push_back(GotoScope(ParentScope, Diag, 0, IS->getBeginLoc()));
407
12
      BuildScopeInformation(Else, NewParentScope);
408
12
    }
409
19
    return;
410
36.3k
  }
411
412
128
  case Stmt::CXXTryStmtClass: {
413
128
    CXXTryStmt *TS = cast<CXXTryStmt>(S);
414
128
    {
415
128
      unsigned NewParentScope = Scopes.size();
416
128
      Scopes.push_back(GotoScope(ParentScope,
417
128
                                 diag::note_protected_by_cxx_try,
418
128
                                 diag::note_exits_cxx_try,
419
128
                                 TS->getSourceRange().getBegin()));
420
128
      if (Stmt *TryBlock = TS->getTryBlock())
421
128
        BuildScopeInformation(TryBlock, NewParentScope);
422
128
    }
423
424
    // Jump from the catch into the try is not allowed either.
425
260
    for (unsigned I = 0, E = TS->getNumHandlers(); I != E; 
++I132
) {
426
132
      CXXCatchStmt *CS = TS->getHandler(I);
427
132
      unsigned NewParentScope = Scopes.size();
428
132
      Scopes.push_back(GotoScope(ParentScope,
429
132
                                 diag::note_protected_by_cxx_catch,
430
132
                                 diag::note_exits_cxx_catch,
431
132
                                 CS->getSourceRange().getBegin()));
432
132
      BuildScopeInformation(CS->getHandlerBlock(), NewParentScope);
433
132
    }
434
128
    return;
435
36.3k
  }
436
437
78
  case Stmt::SEHTryStmtClass: {
438
78
    SEHTryStmt *TS = cast<SEHTryStmt>(S);
439
78
    {
440
78
      unsigned NewParentScope = Scopes.size();
441
78
      Scopes.push_back(GotoScope(ParentScope,
442
78
                                 diag::note_protected_by_seh_try,
443
78
                                 diag::note_exits_seh_try,
444
78
                                 TS->getSourceRange().getBegin()));
445
78
      if (Stmt *TryBlock = TS->getTryBlock())
446
78
        BuildScopeInformation(TryBlock, NewParentScope);
447
78
    }
448
449
    // Jump from __except or __finally into the __try are not allowed either.
450
78
    if (SEHExceptStmt *Except = TS->getExceptHandler()) {
451
24
      unsigned NewParentScope = Scopes.size();
452
24
      Scopes.push_back(GotoScope(ParentScope,
453
24
                                 diag::note_protected_by_seh_except,
454
24
                                 diag::note_exits_seh_except,
455
24
                                 Except->getSourceRange().getBegin()));
456
24
      BuildScopeInformation(Except->getBlock(), NewParentScope);
457
54
    } else if (SEHFinallyStmt *Finally = TS->getFinallyHandler()) {
458
54
      unsigned NewParentScope = Scopes.size();
459
54
      Scopes.push_back(GotoScope(ParentScope,
460
54
                                 diag::note_protected_by_seh_finally,
461
54
                                 diag::note_exits_seh_finally,
462
54
                                 Finally->getSourceRange().getBegin()));
463
54
      BuildScopeInformation(Finally->getBlock(), NewParentScope);
464
54
    }
465
466
78
    return;
467
36.3k
  }
468
469
40.6k
  case Stmt::DeclStmtClass: {
470
    // If this is a declstmt with a VLA definition, it defines a scope from here
471
    // to the end of the containing context.
472
40.6k
    DeclStmt *DS = cast<DeclStmt>(S);
473
    // The decl statement creates a scope if any of the decls in it are VLAs
474
    // or have the cleanup attribute.
475
40.6k
    for (auto *I : DS->decls())
476
40.7k
      BuildScopeInformation(I, origParentScope);
477
40.6k
    return;
478
36.3k
  }
479
480
7
  case Stmt::ObjCAtTryStmtClass: {
481
    // Disallow jumps into any part of an @try statement by pushing a scope and
482
    // walking all sub-stmts in that scope.
483
7
    ObjCAtTryStmt *AT = cast<ObjCAtTryStmt>(S);
484
    // Recursively walk the AST for the @try part.
485
7
    {
486
7
      unsigned NewParentScope = Scopes.size();
487
7
      Scopes.push_back(GotoScope(ParentScope,
488
7
                                 diag::note_protected_by_objc_try,
489
7
                                 diag::note_exits_objc_try,
490
7
                                 AT->getAtTryLoc()));
491
7
      if (Stmt *TryPart = AT->getTryBody())
492
7
        BuildScopeInformation(TryPart, NewParentScope);
493
7
    }
494
495
    // Jump from the catch to the finally or try is not valid.
496
11
    for (ObjCAtCatchStmt *AC : AT->catch_stmts()) {
497
11
      unsigned NewParentScope = Scopes.size();
498
11
      Scopes.push_back(GotoScope(ParentScope,
499
11
                                 diag::note_protected_by_objc_catch,
500
11
                                 diag::note_exits_objc_catch,
501
11
                                 AC->getAtCatchLoc()));
502
      // @catches are nested and it isn't
503
11
      BuildScopeInformation(AC->getCatchBody(), NewParentScope);
504
11
    }
505
506
    // Jump from the finally to the try or catch is not valid.
507
7
    if (ObjCAtFinallyStmt *AF = AT->getFinallyStmt()) {
508
4
      unsigned NewParentScope = Scopes.size();
509
4
      Scopes.push_back(GotoScope(ParentScope,
510
4
                                 diag::note_protected_by_objc_finally,
511
4
                                 diag::note_exits_objc_finally,
512
4
                                 AF->getAtFinallyLoc()));
513
4
      BuildScopeInformation(AF, NewParentScope);
514
4
    }
515
516
7
    return;
517
36.3k
  }
518
519
1
  case Stmt::ObjCAtSynchronizedStmtClass: {
520
    // Disallow jumps into the protected statement of an @synchronized, but
521
    // allow jumps into the object expression it protects.
522
1
    ObjCAtSynchronizedStmt *AS = cast<ObjCAtSynchronizedStmt>(S);
523
    // Recursively walk the AST for the @synchronized object expr, it is
524
    // evaluated in the normal scope.
525
1
    BuildScopeInformation(AS->getSynchExpr(), ParentScope);
526
527
    // Recursively walk the AST for the @synchronized part, protected by a new
528
    // scope.
529
1
    unsigned NewParentScope = Scopes.size();
530
1
    Scopes.push_back(GotoScope(ParentScope,
531
1
                               diag::note_protected_by_objc_synchronized,
532
1
                               diag::note_exits_objc_synchronized,
533
1
                               AS->getAtSynchronizedLoc()));
534
1
    BuildScopeInformation(AS->getSynchBody(), NewParentScope);
535
1
    return;
536
36.3k
  }
537
538
2
  case Stmt::ObjCAutoreleasePoolStmtClass: {
539
    // Disallow jumps into the protected statement of an @autoreleasepool.
540
2
    ObjCAutoreleasePoolStmt *AS = cast<ObjCAutoreleasePoolStmt>(S);
541
    // Recursively walk the AST for the @autoreleasepool part, protected by a
542
    // new scope.
543
2
    unsigned NewParentScope = Scopes.size();
544
2
    Scopes.push_back(GotoScope(ParentScope,
545
2
                               diag::note_protected_by_objc_autoreleasepool,
546
2
                               diag::note_exits_objc_autoreleasepool,
547
2
                               AS->getAtLoc()));
548
2
    BuildScopeInformation(AS->getSubStmt(), NewParentScope);
549
2
    return;
550
36.3k
  }
551
552
1.86k
  case Stmt::ExprWithCleanupsClass: {
553
    // Disallow jumps past full-expressions that use blocks with
554
    // non-trivial cleanups of their captures.  This is theoretically
555
    // implementable but a lot of work which we haven't felt up to doing.
556
1.86k
    ExprWithCleanups *EWC = cast<ExprWithCleanups>(S);
557
1.97k
    for (unsigned i = 0, e = EWC->getNumObjects(); i != e; 
++i113
) {
558
113
      if (auto *BDecl = EWC->getObject(i).dyn_cast<BlockDecl *>())
559
111
        for (const auto &CI : BDecl->captures()) {
560
111
          VarDecl *variable = CI.getVariable();
561
111
          BuildScopeInformation(variable, BDecl, origParentScope);
562
111
        }
563
2
      else if (auto *CLE = EWC->getObject(i).dyn_cast<CompoundLiteralExpr *>())
564
2
        BuildScopeInformation(CLE, origParentScope);
565
0
      else
566
0
        llvm_unreachable("unexpected cleanup object type");
567
113
    }
568
1.86k
    break;
569
36.3k
  }
570
571
1.12k
  case Stmt::MaterializeTemporaryExprClass: {
572
    // Disallow jumps out of scopes containing temporaries lifetime-extended to
573
    // automatic storage duration.
574
1.12k
    MaterializeTemporaryExpr *MTE = cast<MaterializeTemporaryExpr>(S);
575
1.12k
    if (MTE->getStorageDuration() == SD_Automatic) {
576
96
      SmallVector<const Expr *, 4> CommaLHS;
577
96
      SmallVector<SubobjectAdjustment, 4> Adjustments;
578
96
      const Expr *ExtendedObject =
579
96
          MTE->getSubExpr()->skipRValueSubobjectAdjustments(CommaLHS,
580
96
                                                            Adjustments);
581
96
      if (ExtendedObject->getType().isDestructedType()) {
582
11
        Scopes.push_back(GotoScope(ParentScope, 0,
583
11
                                   diag::note_exits_temporary_dtor,
584
11
                                   ExtendedObject->getExprLoc()));
585
11
        origParentScope = Scopes.size()-1;
586
11
      }
587
96
    }
588
1.12k
    break;
589
36.3k
  }
590
591
2
  case Stmt::CaseStmtClass:
592
4
  case Stmt::DefaultStmtClass:
593
76
  case Stmt::LabelStmtClass:
594
76
    LabelAndGotoScopes[S] = ParentScope;
595
76
    break;
596
597
1.05k
  case Stmt::AttributedStmtClass: {
598
1.05k
    AttributedStmt *AS = cast<AttributedStmt>(S);
599
1.05k
    if (GetMustTailAttr(AS)) {
600
69
      LabelAndGotoScopes[AS] = ParentScope;
601
69
      MustTailStmts.push_back(AS);
602
69
    }
603
1.05k
    break;
604
4
  }
605
606
1.02M
  default:
607
1.02M
    if (auto *ED = dyn_cast<OMPExecutableDirective>(S)) {
608
2.30k
      if (!ED->isStandaloneDirective()) {
609
1.77k
        unsigned NewParentScope = Scopes.size();
610
1.77k
        Scopes.emplace_back(ParentScope,
611
1.77k
                            diag::note_omp_protected_structured_block,
612
1.77k
                            diag::note_omp_exits_structured_block,
613
1.77k
                            ED->getStructuredBlock()->getBeginLoc());
614
1.77k
        BuildScopeInformation(ED->getStructuredBlock(), NewParentScope);
615
1.77k
        return;
616
1.77k
      }
617
2.30k
    }
618
1.02M
    break;
619
1.11M
  }
620
621
1.08M
  
for (Stmt *SubStmt : S->children())1.07M
{
622
1.08M
    if (!SubStmt)
623
3.84k
        continue;
624
1.07M
    if (StmtsToSkip) {
625
121
      --StmtsToSkip;
626
121
      continue;
627
121
    }
628
629
    // Cases, labels, and defaults aren't "scope parents".  It's also
630
    // important to handle these iteratively instead of recursively in
631
    // order to avoid blowing out the stack.
632
1.09M
    
while (1.07M
true) {
633
1.09M
      Stmt *Next;
634
1.09M
      if (SwitchCase *SC = dyn_cast<SwitchCase>(SubStmt))
635
19.4k
        Next = SC->getSubStmt();
636
1.07M
      else if (LabelStmt *LS = dyn_cast<LabelStmt>(SubStmt))
637
2.45k
        Next = LS->getSubStmt();
638
1.07M
      else
639
1.07M
        break;
640
641
21.8k
      LabelAndGotoScopes[SubStmt] = ParentScope;
642
21.8k
      SubStmt = Next;
643
21.8k
    }
644
645
    // Recursively walk the AST.
646
1.07M
    BuildScopeInformation(SubStmt, ParentScope);
647
1.07M
  }
648
1.07M
}
649
650
/// VerifyJumps - Verify each element of the Jumps array to see if they are
651
/// valid, emitting diagnostics if not.
652
5.63k
void JumpScopeChecker::VerifyJumps() {
653
12.8k
  while (!Jumps.empty()) {
654
7.18k
    Stmt *Jump = Jumps.pop_back_val();
655
656
    // With a goto,
657
7.18k
    if (GotoStmt *GS = dyn_cast<GotoStmt>(Jump)) {
658
      // The label may not have a statement if it's coming from inline MS ASM.
659
2.57k
      if (GS->getLabel()->getStmt()) {
660
2.27k
        CheckJump(GS, GS->getLabel()->getStmt(), GS->getGotoLoc(),
661
2.27k
                  diag::err_goto_into_protected_scope,
662
2.27k
                  diag::ext_goto_into_protected_scope,
663
2.27k
                  diag::warn_cxx98_compat_goto_into_protected_scope);
664
2.27k
      }
665
2.57k
      CheckGotoStmt(GS);
666
2.57k
      continue;
667
2.57k
    }
668
669
    // We only get indirect gotos here when they have a constant target.
670
4.60k
    if (IndirectGotoStmt *IGS = dyn_cast<IndirectGotoStmt>(Jump)) {
671
10
      LabelDecl *Target = IGS->getConstantTarget();
672
10
      CheckJump(IGS, Target->getStmt(), IGS->getGotoLoc(),
673
10
                diag::err_goto_into_protected_scope,
674
10
                diag::ext_goto_into_protected_scope,
675
10
                diag::warn_cxx98_compat_goto_into_protected_scope);
676
10
      continue;
677
10
    }
678
679
4.59k
    SwitchStmt *SS = cast<SwitchStmt>(Jump);
680
24.0k
    for (SwitchCase *SC = SS->getSwitchCaseList(); SC;
681
19.4k
         SC = SC->getNextSwitchCase()) {
682
19.4k
      if (CHECK_PERMISSIVE(!LabelAndGotoScopes.count(SC)))
683
0
        continue;
684
19.4k
      SourceLocation Loc;
685
19.4k
      if (CaseStmt *CS = dyn_cast<CaseStmt>(SC))
686
18.2k
        Loc = CS->getBeginLoc();
687
1.13k
      else if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC))
688
1.13k
        Loc = DS->getBeginLoc();
689
0
      else
690
0
        Loc = SC->getBeginLoc();
691
19.4k
      CheckJump(SS, SC, Loc, diag::err_switch_into_protected_scope, 0,
692
19.4k
                diag::warn_cxx98_compat_switch_into_protected_scope);
693
19.4k
    }
694
4.59k
  }
695
5.63k
}
696
697
/// VerifyIndirectOrAsmJumps - Verify whether any possible indirect goto or
698
/// asm goto jump might cross a protection boundary.  Unlike direct jumps,
699
/// indirect or asm goto jumps count cleanups as protection boundaries:
700
/// since there's no way to know where the jump is going, we can't implicitly
701
/// run the right cleanups the way we can with direct jumps.
702
/// Thus, an indirect/asm jump is "trivial" if it bypasses no
703
/// initializations and no teardowns.  More formally, an indirect/asm jump
704
/// from A to B is trivial if the path out from A to DCA(A,B) is
705
/// trivial and the path in from DCA(A,B) to B is trivial, where
706
/// DCA(A,B) is the deepest common ancestor of A and B.
707
/// Jump-triviality is transitive but asymmetric.
708
///
709
/// A path in is trivial if none of the entered scopes have an InDiag.
710
/// A path out is trivial is none of the exited scopes have an OutDiag.
711
///
712
/// Under these definitions, this function checks that the indirect
713
/// jump between A and B is trivial for every indirect goto statement A
714
/// and every label B whose address was taken in the function.
715
11.2k
void JumpScopeChecker::VerifyIndirectOrAsmJumps(bool IsAsmGoto) {
716
11.2k
  SmallVector<Stmt*, 4> GotoJumps = IsAsmGoto ? 
AsmJumps5.63k
:
IndirectJumps5.63k
;
717
11.2k
  if (GotoJumps.empty())
718
11.1k
    return;
719
116
  SmallVector<LabelDecl *, 4> JumpTargets =
720
116
      IsAsmGoto ? 
AsmJumpTargets18
:
IndirectJumpTargets98
;
721
  // If there aren't any address-of-label expressions in this function,
722
  // complain about the first indirect goto.
723
116
  if (JumpTargets.empty()) {
724
0
    assert(!IsAsmGoto &&"only indirect goto can get here");
725
0
    S.Diag(GotoJumps[0]->getBeginLoc(),
726
0
           diag::err_indirect_goto_without_addrlabel);
727
0
    return;
728
0
  }
729
  // Collect a single representative of every scope containing an
730
  // indirect or asm goto.  For most code bases, this substantially cuts
731
  // down on the number of jump sites we'll have to consider later.
732
116
  typedef std::pair<unsigned, Stmt*> JumpScope;
733
116
  SmallVector<JumpScope, 32> JumpScopes;
734
116
  {
735
116
    llvm::DenseMap<unsigned, Stmt*> JumpScopesMap;
736
116
    for (SmallVectorImpl<Stmt *>::iterator I = GotoJumps.begin(),
737
116
                                           E = GotoJumps.end();
738
254
         I != E; 
++I138
) {
739
138
      Stmt *IG = *I;
740
138
      if (CHECK_PERMISSIVE(!LabelAndGotoScopes.count(IG)))
741
0
        continue;
742
138
      unsigned IGScope = LabelAndGotoScopes[IG];
743
138
      Stmt *&Entry = JumpScopesMap[IGScope];
744
138
      if (!Entry) 
Entry = IG127
;
745
138
    }
746
116
    JumpScopes.reserve(JumpScopesMap.size());
747
116
    for (llvm::DenseMap<unsigned, Stmt *>::iterator I = JumpScopesMap.begin(),
748
116
                                                    E = JumpScopesMap.end();
749
243
         I != E; 
++I127
)
750
127
      JumpScopes.push_back(*I);
751
116
  }
752
753
  // Collect a single representative of every scope containing a
754
  // label whose address was taken somewhere in the function.
755
  // For most code bases, there will be only one such scope.
756
116
  llvm::DenseMap<unsigned, LabelDecl*> TargetScopes;
757
116
  for (SmallVectorImpl<LabelDecl *>::iterator I = JumpTargets.begin(),
758
116
                                              E = JumpTargets.end();
759
314
       I != E; 
++I198
) {
760
198
    LabelDecl *TheLabel = *I;
761
198
    if (CHECK_PERMISSIVE(!LabelAndGotoScopes.count(TheLabel->getStmt())))
762
0
      continue;
763
198
    unsigned LabelScope = LabelAndGotoScopes[TheLabel->getStmt()];
764
198
    LabelDecl *&Target = TargetScopes[LabelScope];
765
198
    if (!Target) 
Target = TheLabel123
;
766
198
  }
767
768
  // For each target scope, make sure it's trivially reachable from
769
  // every scope containing a jump site.
770
  //
771
  // A path between scopes always consists of exitting zero or more
772
  // scopes, then entering zero or more scopes.  We build a set of
773
  // of scopes S from which the target scope can be trivially
774
  // entered, then verify that every jump scope can be trivially
775
  // exitted to reach a scope in S.
776
116
  llvm::BitVector Reachable(Scopes.size(), false);
777
116
  for (llvm::DenseMap<unsigned,LabelDecl*>::iterator
778
239
         TI = TargetScopes.begin(), TE = TargetScopes.end(); TI != TE; 
++TI123
) {
779
123
    unsigned TargetScope = TI->first;
780
123
    LabelDecl *TargetLabel = TI->second;
781
782
123
    Reachable.reset();
783
784
    // Mark all the enclosing scopes from which you can safely jump
785
    // into the target scope.  'Min' will end up being the index of
786
    // the shallowest such scope.
787
123
    unsigned Min = TargetScope;
788
123
    while (true) {
789
123
      Reachable.set(Min);
790
791
      // Don't go beyond the outermost scope.
792
123
      if (Min == 0) 
break86
;
793
794
      // Stop if we can't trivially enter the current scope.
795
37
      if (Scopes[Min].InDiag) break;
796
797
0
      Min = Scopes[Min].ParentScope;
798
0
    }
799
800
    // Walk through all the jump sites, checking that they can trivially
801
    // reach this label scope.
802
123
    for (SmallVectorImpl<JumpScope>::iterator
803
258
           I = JumpScopes.begin(), E = JumpScopes.end(); I != E; 
++I135
) {
804
135
      unsigned Scope = I->first;
805
806
      // Walk out the "scope chain" for this scope, looking for a scope
807
      // we've marked reachable.  For well-formed code this amortizes
808
      // to O(JumpScopes.size() / Scopes.size()):  we only iterate
809
      // when we see something unmarked, and in well-formed code we
810
      // mark everything we iterate past.
811
135
      bool IsReachable = false;
812
164
      while (true) {
813
164
        if (Reachable.test(Scope)) {
814
          // If we find something reachable, mark all the scopes we just
815
          // walked through as reachable.
816
116
          for (unsigned S = I->first; S != Scope; 
S = Scopes[S].ParentScope23
)
817
23
            Reachable.set(S);
818
93
          IsReachable = true;
819
93
          break;
820
93
        }
821
822
        // Don't walk out if we've reached the top-level scope or we've
823
        // gotten shallower than the shallowest reachable scope.
824
71
        if (Scope == 0 || 
Scope < Min65
)
break13
;
825
826
        // Don't walk out through an out-diagnostic.
827
58
        if (Scopes[Scope].OutDiag) 
break29
;
828
829
29
        Scope = Scopes[Scope].ParentScope;
830
29
      }
831
832
      // Only diagnose if we didn't find something.
833
135
      if (IsReachable) 
continue93
;
834
835
42
      DiagnoseIndirectOrAsmJump(I->second, I->first, TargetLabel, TargetScope);
836
42
    }
837
123
  }
838
116
}
839
840
/// Return true if a particular error+note combination must be downgraded to a
841
/// warning in Microsoft mode.
842
20
static bool IsMicrosoftJumpWarning(unsigned JumpDiag, unsigned InDiagNote) {
843
20
  return (JumpDiag == diag::err_goto_into_protected_scope &&
844
20
         (InDiagNote == diag::note_protected_by_variable_init ||
845
20
          
InDiagNote == diag::note_protected_by_variable_nontriv_destructor10
));
846
20
}
847
848
/// Return true if a particular note should be downgraded to a compatibility
849
/// warning in C++11 mode.
850
264
static bool IsCXX98CompatWarning(Sema &S, unsigned InDiagNote) {
851
264
  return S.getLangOpts().CPlusPlus11 &&
852
264
         
InDiagNote == diag::note_protected_by_variable_non_pod155
;
853
264
}
854
855
/// Produce primary diagnostic for an indirect jump statement.
856
static void DiagnoseIndirectOrAsmJumpStmt(Sema &S, Stmt *Jump,
857
40
                                          LabelDecl *Target, bool &Diagnosed) {
858
40
  if (Diagnosed)
859
1
    return;
860
39
  bool IsAsmGoto = isa<GCCAsmStmt>(Jump);
861
39
  S.Diag(Jump->getBeginLoc(), diag::err_indirect_goto_in_protected_scope)
862
39
      << IsAsmGoto;
863
39
  S.Diag(Target->getStmt()->getIdentLoc(), diag::note_indirect_goto_target)
864
39
      << IsAsmGoto;
865
39
  Diagnosed = true;
866
39
}
867
868
/// Produce note diagnostics for a jump into a protected scope.
869
248
void JumpScopeChecker::NoteJumpIntoScopes(ArrayRef<unsigned> ToScopes) {
870
248
  if (CHECK_PERMISSIVE(ToScopes.empty()))
871
0
    return;
872
517
  
for (unsigned I = 0, E = ToScopes.size(); 248
I != E;
++I269
)
873
269
    if (Scopes[ToScopes[I]].InDiag)
874
269
      S.Diag(Scopes[ToScopes[I]].Loc, Scopes[ToScopes[I]].InDiag);
875
248
}
876
877
/// Diagnose an indirect jump which is known to cross scopes.
878
void JumpScopeChecker::DiagnoseIndirectOrAsmJump(Stmt *Jump, unsigned JumpScope,
879
                                                 LabelDecl *Target,
880
42
                                                 unsigned TargetScope) {
881
42
  if (CHECK_PERMISSIVE(JumpScope == TargetScope))
882
0
    return;
883
884
42
  unsigned Common = GetDeepestCommonScope(JumpScope, TargetScope);
885
42
  bool Diagnosed = false;
886
887
  // Walk out the scope chain until we reach the common ancestor.
888
88
  for (unsigned I = JumpScope; I != Common; 
I = Scopes[I].ParentScope46
)
889
46
    if (Scopes[I].OutDiag) {
890
30
      DiagnoseIndirectOrAsmJumpStmt(S, Jump, Target, Diagnosed);
891
30
      S.Diag(Scopes[I].Loc, Scopes[I].OutDiag);
892
30
    }
893
894
42
  SmallVector<unsigned, 10> ToScopesCXX98Compat;
895
896
  // Now walk into the scopes containing the label whose address was taken.
897
55
  for (unsigned I = TargetScope; I != Common; 
I = Scopes[I].ParentScope13
)
898
13
    if (IsCXX98CompatWarning(S, Scopes[I].InDiag))
899
3
      ToScopesCXX98Compat.push_back(I);
900
10
    else if (Scopes[I].InDiag) {
901
10
      DiagnoseIndirectOrAsmJumpStmt(S, Jump, Target, Diagnosed);
902
10
      S.Diag(Scopes[I].Loc, Scopes[I].InDiag);
903
10
    }
904
905
  // Diagnose this jump if it would be ill-formed in C++98.
906
42
  if (!Diagnosed && 
!ToScopesCXX98Compat.empty()3
) {
907
3
    bool IsAsmGoto = isa<GCCAsmStmt>(Jump);
908
3
    S.Diag(Jump->getBeginLoc(),
909
3
           diag::warn_cxx98_compat_indirect_goto_in_protected_scope)
910
3
        << IsAsmGoto;
911
3
    S.Diag(Target->getStmt()->getIdentLoc(), diag::note_indirect_goto_target)
912
3
        << IsAsmGoto;
913
3
    NoteJumpIntoScopes(ToScopesCXX98Compat);
914
3
  }
915
42
}
916
917
/// CheckJump - Validate that the specified jump statement is valid: that it is
918
/// jumping within or out of its current scope, not into a deeper one.
919
void JumpScopeChecker::CheckJump(Stmt *From, Stmt *To, SourceLocation DiagLoc,
920
                               unsigned JumpDiagError, unsigned JumpDiagWarning,
921
21.7k
                                 unsigned JumpDiagCXX98Compat) {
922
21.7k
  if (CHECK_PERMISSIVE(!LabelAndGotoScopes.count(From)))
923
0
    return;
924
21.7k
  if (CHECK_PERMISSIVE(!LabelAndGotoScopes.count(To)))
925
0
    return;
926
927
21.7k
  unsigned FromScope = LabelAndGotoScopes[From];
928
21.7k
  unsigned ToScope = LabelAndGotoScopes[To];
929
930
  // Common case: exactly the same scope, which is fine.
931
21.7k
  if (FromScope == ToScope) 
return20.8k
;
932
933
  // Warn on gotos out of __finally blocks.
934
867
  if (isa<GotoStmt>(From) || 
isa<IndirectGotoStmt>(From)62
) {
935
    // If FromScope > ToScope, FromScope is more nested and the jump goes to a
936
    // less nested scope.  Check if it crosses a __finally along the way.
937
3.29k
    for (unsigned I = FromScope; I > ToScope; 
I = Scopes[I].ParentScope2.47k
) {
938
2.49k
      if (Scopes[I].InDiag == diag::note_protected_by_seh_finally) {
939
8
        S.Diag(From->getBeginLoc(), diag::warn_jump_out_of_seh_finally);
940
8
        break;
941
8
      }
942
2.48k
      if (Scopes[I].InDiag == diag::note_omp_protected_structured_block) {
943
8
        S.Diag(From->getBeginLoc(), diag::err_goto_into_protected_scope);
944
8
        S.Diag(To->getBeginLoc(), diag::note_omp_exits_structured_block);
945
8
        break;
946
8
      }
947
2.48k
    }
948
814
  }
949
950
867
  unsigned CommonScope = GetDeepestCommonScope(FromScope, ToScope);
951
952
  // It's okay to jump out from a nested scope.
953
867
  if (CommonScope == ToScope) 
return622
;
954
955
  // Pull out (and reverse) any scopes we might need to diagnose skipping.
956
245
  SmallVector<unsigned, 10> ToScopesCXX98Compat;
957
245
  SmallVector<unsigned, 10> ToScopesError;
958
245
  SmallVector<unsigned, 10> ToScopesWarning;
959
511
  for (unsigned I = ToScope; I != CommonScope; 
I = Scopes[I].ParentScope266
) {
960
266
    if (S.getLangOpts().MSVCCompat && 
JumpDiagWarning != 025
&&
961
266
        
IsMicrosoftJumpWarning(JumpDiagError, Scopes[I].InDiag)20
)
962
15
      ToScopesWarning.push_back(I);
963
251
    else if (IsCXX98CompatWarning(S, Scopes[I].InDiag))
964
12
      ToScopesCXX98Compat.push_back(I);
965
239
    else if (Scopes[I].InDiag)
966
239
      ToScopesError.push_back(I);
967
266
  }
968
969
  // Handle warnings.
970
245
  if (!ToScopesWarning.empty()) {
971
15
    S.Diag(DiagLoc, JumpDiagWarning);
972
15
    NoteJumpIntoScopes(ToScopesWarning);
973
15
    assert(isa<LabelStmt>(To));
974
0
    LabelStmt *Label = cast<LabelStmt>(To);
975
15
    Label->setSideEntry(true);
976
15
  }
977
978
  // Handle errors.
979
245
  if (!ToScopesError.empty()) {
980
218
    S.Diag(DiagLoc, JumpDiagError);
981
218
    NoteJumpIntoScopes(ToScopesError);
982
218
  }
983
984
  // Handle -Wc++98-compat warnings if the jump is well-formed.
985
245
  if (ToScopesError.empty() && 
!ToScopesCXX98Compat.empty()27
) {
986
12
    S.Diag(DiagLoc, JumpDiagCXX98Compat);
987
12
    NoteJumpIntoScopes(ToScopesCXX98Compat);
988
12
  }
989
245
}
990
991
2.57k
void JumpScopeChecker::CheckGotoStmt(GotoStmt *GS) {
992
2.57k
  if (GS->getLabel()->isMSAsmLabel()) {
993
4
    S.Diag(GS->getGotoLoc(), diag::err_goto_ms_asm_label)
994
4
        << GS->getLabel()->getIdentifier();
995
4
    S.Diag(GS->getLabel()->getLocation(), diag::note_goto_ms_asm_label)
996
4
        << GS->getLabel()->getIdentifier();
997
4
  }
998
2.57k
}
999
1000
5.63k
void JumpScopeChecker::VerifyMustTailStmts() {
1001
5.63k
  for (AttributedStmt *AS : MustTailStmts) {
1002
84
    for (unsigned I = LabelAndGotoScopes[AS]; I; 
I = Scopes[I].ParentScope15
) {
1003
15
      if (Scopes[I].OutDiag) {
1004
5
        S.Diag(AS->getBeginLoc(), diag::err_musttail_scope);
1005
5
        S.Diag(Scopes[I].Loc, Scopes[I].OutDiag);
1006
5
      }
1007
15
    }
1008
69
  }
1009
5.63k
}
1010
1011
1.05k
const Attr *JumpScopeChecker::GetMustTailAttr(AttributedStmt *AS) {
1012
1.05k
  ArrayRef<const Attr *> Attrs = AS->getAttrs();
1013
1.05k
  const auto *Iter =
1014
1.06k
      llvm::find_if(Attrs, [](const Attr *A) { return isa<MustTailAttr>(A); });
1015
1.05k
  return Iter != Attrs.end() ? 
*Iter69
:
nullptr986
;
1016
1.05k
}
1017
1018
5.63k
void Sema::DiagnoseInvalidJumps(Stmt *Body) {
1019
5.63k
  (void)JumpScopeChecker(Body, *this);
1020
5.63k
}