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

source code of clang/lib/Sema/JumpDiagnostics.cpp