clang 19.0.0git
JumpDiagnostics.cpp
Go to the documentation of this file.
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"
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.
65
66 GotoScope(unsigned parentScope, unsigned InDiag, unsigned OutDiag,
68 : ParentScope(parentScope), InDiag(InDiag), OutDiag(OutDiag), Loc(L) {}
69 };
70
72 llvm::DenseMap<Stmt*, unsigned> LabelAndGotoScopes;
74
75 SmallVector<Stmt*, 4> IndirectJumps;
76 SmallVector<LabelDecl *, 4> IndirectJumpTargets;
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(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(Body, 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.
144 if (const VarDecl *VD = dyn_cast<VarDecl>(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()) {
162 return ScopePair(diag::note_protected_by_objc_strong_init,
163 diag::note_exits_objc_strong);
164
166 return ScopePair(diag::note_protected_by_objc_weak_init,
167 diag::note_exits_objc_weak);
168
170 return ScopePair(diag::note_protected_by_non_trivial_c_struct_init,
171 diag::note_exits_dtor);
172
174 OutDiag = diag::note_exits_dtor;
175 break;
176
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>(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>(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(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>(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) {
262 Diags = ScopePair(diag::note_enters_block_captures_cxx_obj,
263 diag::note_exits_block_captures_cxx_obj);
264 break;
266 Diags = ScopePair(diag::note_enters_block_captures_strong,
267 diag::note_exits_block_captures_strong);
268 break;
270 Diags = ScopePair(diag::note_enters_block_captures_weak,
271 diag::note_exits_block_captures_weak);
272 break;
274 Diags = ScopePair(diag::note_enters_block_captures_non_trivial_c_struct,
275 diag::note_exits_block_captures_non_trivial_c_struct);
276 break;
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(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(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>(S) && !isa<StmtExpr>(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(cast<AddrLabelExpr>(S)->getLabel());
318 break;
319
320 case Stmt::ObjCForCollectionStmtClass: {
321 auto *CS = cast<ObjCForCollectionStmt>(S);
322 unsigned Diag = diag::note_protected_by_objc_fast_enumeration;
323 unsigned NewParentScope = Scopes.size();
324 Scopes.push_back(GotoScope(ParentScope, Diag, 0, S->getBeginLoc()));
325 BuildScopeInformation(CS->getBody(), 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>(S)->getConstantTarget())
336 goto RecordJumpScope;
337
338 LabelAndGotoScopes[S] = ParentScope;
339 IndirectJumps.push_back(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>(S)->getInit()) {
346 BuildScopeInformation(Init, ParentScope);
347 ++StmtsToSkip;
348 }
349 if (VarDecl *Var = cast<SwitchStmt>(S)->getConditionVariable()) {
350 BuildScopeInformation(Var, ParentScope);
351 ++StmtsToSkip;
352 }
353 goto RecordJumpScope;
354
355 case Stmt::GCCAsmStmtClass:
356 if (!cast<GCCAsmStmt>(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(S);
366 break;
367
368 case Stmt::IfStmtClass: {
369 IfStmt *IS = cast<IfStmt>(S);
370 if (!(IS->isConstexpr() || IS->isConsteval() ||
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(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(GotoScope(ParentScope, Diag, 0, IS->getBeginLoc()));
393 BuildScopeInformation(IS->getThen(), NewParentScope);
394 if (Stmt *Else = IS->getElse()) {
395 NewParentScope = Scopes.size();
396 Scopes.push_back(GotoScope(ParentScope, Diag, 0, IS->getBeginLoc()));
397 BuildScopeInformation(Else, NewParentScope);
398 }
399 return;
400 }
401
402 case Stmt::CXXTryStmtClass: {
403 CXXTryStmt *TS = cast<CXXTryStmt>(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(TryBlock, 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);
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(CS->getHandlerBlock(), NewParentScope);
423 }
424 return;
425 }
426
427 case Stmt::SEHTryStmtClass: {
428 SEHTryStmt *TS = cast<SEHTryStmt>(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(TryBlock, 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>(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(I, 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>(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>(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(TryPart, 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(AF, 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>(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>(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(AS->getSubStmt(), 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>(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(variable, BDecl, origParentScope);
567 }
568 else if (auto *CLE = EWC->getObject(i).dyn_cast<CompoundLiteralExpr *>())
569 BuildScopeInformation(CLE, 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>(S);
580 if (MTE->getStorageDuration() == SD_Automatic) {
581 const Expr *ExtendedObject =
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>(S);
601 if (GetMustTailAttr(AS)) {
602 LabelAndGotoScopes[AS] = ParentScope;
603 MustTailStmts.push_back(AS);
604 }
605 break;
606 }
607
608 case Stmt::OpenACCComputeConstructClass: {
609 unsigned NewParentScope = Scopes.size();
610 OpenACCComputeConstruct *CC = cast<OpenACCComputeConstruct>(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(CC->getStructuredBlock(), NewParentScope);
615 return;
616 }
617
618 default:
619 if (auto *ED = dyn_cast<OMPExecutableDirective>(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(ED->getStructuredBlock(), 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>(SubStmt))
647 Next = SC->getSubStmt();
648 else if (LabelStmt *LS = dyn_cast<LabelStmt>(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(SubStmt, 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>(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>(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>(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>(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>(SC))
714 Loc = CS->getBeginLoc();
715 else if (DefaultStmt *DS = dyn_cast<DefaultStmt>(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 *>;
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(IGScope))
764 JumpScopesMap[IGScope] = IG;
765 }
766 JumpScopes.reserve(JumpScopesMap.size());
767 for (auto &Pair : JumpScopesMap)
768 JumpScopes.emplace_back(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(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(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(JumpStmt, JumpScope, 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.
866 LabelDecl *Target, bool &Diagnosed) {
867 if (Diagnosed)
868 return;
869 bool IsAsmGoto = isa<GCCAsmStmt>(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,
889 unsigned TargetScope) {
890 if (CHECK_PERMISSIVE(JumpScope == TargetScope))
891 return;
892
893 unsigned Common = GetDeepestCommonScope(JumpScope, 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, Scopes[I].InDiag))
908 ToScopesCXX98Compat.push_back(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>(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(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>(From) || isa<IndirectGotoStmt>(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(FromScope, 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(JumpDiagError, Scopes[I].InDiag))
976 ToScopesWarning.push_back(I);
977 else if (IsCXX98CompatWarning(S, Scopes[I].InDiag))
978 ToScopesCXX98Compat.push_back(I);
979 else if (Scopes[I].InDiag)
980 ToScopesError.push_back(I);
981 }
982
983 // Handle warnings.
984 if (!ToScopesWarning.empty()) {
985 S.Diag(DiagLoc, JumpDiagWarning);
986 NoteJumpIntoScopes(ToScopesWarning);
987 assert(isa<LabelStmt>(To));
988 LabelStmt *Label = cast<LabelStmt>(To);
989 Label->setSideEntry(true);
990 }
991
992 // Handle errors.
993 if (!ToScopesError.empty()) {
994 S.Diag(DiagLoc, JumpDiagError);
995 NoteJumpIntoScopes(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(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(Attrs, [](const Attr *A) { return isa<MustTailAttr>(A); });
1029 return Iter != Attrs.end() ? *Iter : nullptr;
1030}
1031
1033 (void)JumpScopeChecker(Body, *this);
1034}
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
Defines the clang::Expr interface and subclasses for C++ expressions.
unsigned Iter
Definition: HTMLLogger.cpp:154
std::pair< unsigned, unsigned > ScopePair
static ScopePair GetDiagForGotoScopeDecl(Sema &S, const Decl *D)
GetDiagForGotoScopeDecl - If this decl induces a new goto scope, return a diagnostic that should be e...
static bool IsCXX98CompatWarning(Sema &S, unsigned InDiagNote)
Return true if a particular note should be downgraded to a compatibility warning in C++11 mode.
static bool IsMicrosoftJumpWarning(unsigned JumpDiag, unsigned InDiagNote)
Return true if a particular error+note combination must be downgraded to a warning in Microsoft mode.
#define CHECK_PERMISSIVE(x)
static void DiagnoseIndirectOrAsmJumpStmt(Sema &S, Stmt *Jump, LabelDecl *Target, bool &Diagnosed)
Produce primary diagnostic for an indirect jump statement.
static DiagnosticBuilder Diag(DiagnosticsEngine *Diags, const LangOptions &Features, FullSourceLoc TokLoc, const char *TokBegin, const char *TokRangeBegin, const char *TokRangeEnd, unsigned DiagID)
Produce a diagnostic highlighting some portion of a literal.
llvm::MachO::Target Target
Definition: MachO.h:47
Defines the clang::SourceLocation class and associated facilities.
Defines the Objective-C statement AST node classes.
This file defines OpenACC AST classes for statement-level contructs.
This file defines OpenMP AST classes for executable directives and clauses.
std::string Label
__device__ __2f16 float __ockl_bool s
const LangOptions & getLangOpts() const
Definition: ASTContext.h:775
AddrLabelExpr - The GNU address of label extension, representing &&label.
Definition: Expr.h:4338
Attr - This represents one attribute.
Definition: Attr.h:42
Represents an attribute applied to a statement.
Definition: Stmt.h:2085
ArrayRef< const Attr * > getAttrs() const
Definition: Stmt.h:2119
SourceLocation getBeginLoc() const
Definition: Stmt.h:2126
Represents a block literal declaration, which is like an unnamed FunctionDecl.
Definition: Decl.h:4495
ArrayRef< Capture > captures() const
Definition: Decl.h:4622
CXXCatchStmt - This represents a C++ catch block.
Definition: StmtCXX.h:28
Stmt * getHandlerBlock() const
Definition: StmtCXX.h:51
SourceLocation getBeginLoc() const LLVM_READONLY
Definition: StmtCXX.h:43
Represents a call to a C++ constructor.
Definition: ExprCXX.h:1540
Represents a C++ constructor within a class.
Definition: DeclCXX.h:2532
bool isDefaultConstructor() const
Whether this constructor is a default constructor (C++ [class.ctor]p5), which can be used to default-...
Definition: DeclCXX.cpp:2751
const CXXRecordDecl * getParent() const
Return the parent of this method declaration, which is the class in which this method is defined.
Definition: DeclCXX.h:2183
bool isPOD() const
Whether this class is a POD-type (C++ [class]p4)
Definition: DeclCXX.h:1174
CXXTryStmt - A C++ try block, including all handlers.
Definition: StmtCXX.h:69
CXXCatchStmt * getHandler(unsigned i)
Definition: StmtCXX.h:108
unsigned getNumHandlers() const
Definition: StmtCXX.h:107
CompoundStmt * getTryBlock()
Definition: StmtCXX.h:100
CaseStmt - Represent a case statement.
Definition: Stmt.h:1806
CompoundLiteralExpr - [C99 6.5.2.5].
Definition: Expr.h:3413
DeclStmt - Adaptor class for mixing declarations with statements and expressions.
Definition: Stmt.h:1502
decl_range decls()
Definition: Stmt.h:1550
SourceLocation getBeginLoc() const LLVM_READONLY
Definition: Stmt.h:1528
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:85
SourceLocation getLocation() const
Definition: DeclBase.h:444
bool hasAttr() const
Definition: DeclBase.h:582
Represents an expression – generally a full-expression – that introduces cleanups to be run at the en...
Definition: ExprCXX.h:3446
CleanupObject getObject(unsigned i) const
Definition: ExprCXX.h:3476
unsigned getNumObjects() const
Definition: ExprCXX.h:3474
This represents one expression.
Definition: Expr.h:110
const Expr * skipRValueSubobjectAdjustments(SmallVectorImpl< const Expr * > &CommaLHS, SmallVectorImpl< SubobjectAdjustment > &Adjustments) const
Walk outwards from an expression we want to bind a reference to and find the expression whose lifetim...
Definition: Expr.cpp:82
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
Definition: Expr.cpp:277
QualType getType() const
Definition: Expr.h:142
bool isTrivial() const
Whether this function is "trivial" in some specialized C++ senses.
Definition: Decl.h:2340
GotoStmt - This represents a direct goto.
Definition: Stmt.h:2867
SourceLocation getGotoLoc() const
Definition: Stmt.h:2883
LabelDecl * getLabel() const
Definition: Stmt.h:2880
IfStmt - This represents an if/then/else.
Definition: Stmt.h:2143
Stmt * getThen()
Definition: Stmt.h:2232
Expr * getCond()
Definition: Stmt.h:2220
bool isConstexpr() const
Definition: Stmt.h:2336
bool isObjCAvailabilityCheck() const
Definition: Stmt.cpp:1003
Stmt * getElse()
Definition: Stmt.h:2241
SourceLocation getBeginLoc() const
Definition: Stmt.h:2355
bool isConsteval() const
Definition: Stmt.h:2323
VarDecl * getConditionVariable()
Retrieve the variable declared in this "if" statement, if any.
Definition: Stmt.cpp:982
IndirectGotoStmt - This represents an indirect goto.
Definition: Stmt.h:2906
Represents the declaration of a label.
Definition: Decl.h:499
LabelStmt * getStmt() const
Definition: Decl.h:523
bool isMSAsmLabel() const
Definition: Decl.h:533
LabelStmt - Represents a label, which has a substatement.
Definition: Stmt.h:2036
Represents a prvalue temporary that is written into memory so that a reference can bind to it.
Definition: ExprCXX.h:4689
StorageDuration getStorageDuration() const
Retrieve the storage duration for the materialized temporary.
Definition: ExprCXX.h:4714
Expr * getSubExpr() const
Retrieve the temporary-generating subexpression whose value will be materialized into a glvalue.
Definition: ExprCXX.h:4706
IdentifierInfo * getIdentifier() const
Get the identifier that names this declaration, if there is one.
Definition: Decl.h:270
Represents Objective-C's @catch statement.
Definition: StmtObjC.h:77
Represents Objective-C's @finally statement.
Definition: StmtObjC.h:127
Represents Objective-C's @synchronized statement.
Definition: StmtObjC.h:303
const Expr * getSynchExpr() const
Definition: StmtObjC.h:331
const CompoundStmt * getSynchBody() const
Definition: StmtObjC.h:323
SourceLocation getAtSynchronizedLoc() const
Definition: StmtObjC.h:320
Represents Objective-C's @try ... @catch ... @finally statement.
Definition: StmtObjC.h:167
const ObjCAtFinallyStmt * getFinallyStmt() const
Retrieve the @finally statement, if any.
Definition: StmtObjC.h:240
const Stmt * getTryBody() const
Retrieve the @try body.
Definition: StmtObjC.h:213
SourceLocation getAtTryLoc() const
Retrieve the location of the @ in the @try.
Definition: StmtObjC.h:209
catch_range catch_stmts()
Definition: StmtObjC.h:282
Represents Objective-C's @autoreleasepool Statement.
Definition: StmtObjC.h:394
SourceLocation getAtLoc() const
Definition: StmtObjC.h:414
const Stmt * getSubStmt() const
Definition: StmtObjC.h:405
This class represents a compute construct, representing a 'Kind' of ‘parallel’, 'serial',...
Definition: StmtOpenACC.h:120
SourceLocation getBeginLoc() const
Definition: StmtOpenACC.h:60
A (possibly-)qualified type.
Definition: Type.h:738
@ DK_cxx_destructor
Definition: Type.h:1314
@ DK_nontrivial_c_struct
Definition: Type.h:1317
@ DK_objc_weak_lifetime
Definition: Type.h:1316
@ DK_objc_strong_lifetime
Definition: Type.h:1315
DestructionKind isDestructedType() const
Returns a nonzero value if objects of this type require non-trivial work to clean up after.
Definition: Type.h:1324
CompoundStmt * getTryBlock() const
Definition: Stmt.h:3699
SEHFinallyStmt * getFinallyHandler() const
Definition: Stmt.cpp:1257
SEHExceptStmt * getExceptHandler() const
Returns 0 if not defined.
Definition: Stmt.cpp:1253
Scope - A scope is a transient data structure that is used while parsing the program.
Definition: Scope.h:41
SemaDiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID, bool DeferHint=false)
Emit a diagnostic.
Definition: SemaBase.cpp:56
Sema - This implements semantic analysis and AST building for C.
Definition: Sema.h:434
ASTContext & Context
Definition: Sema.h:836
const LangOptions & getLangOpts() const
Definition: Sema.h:497
void DiagnoseInvalidJumps(Stmt *Body)
Encodes a location in the source.
SourceLocation getBegin() const
StmtExpr - This is the GNU Statement Expression extension: ({int X=4; X;}).
Definition: Expr.h:4383
CompoundStmt * getSubStmt()
Definition: Expr.h:4400
SourceLocation getBeginLoc() const LLVM_READONLY
Definition: Expr.h:4404
Stmt - This represents one statement.
Definition: Stmt.h:84
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
Definition: Stmt.cpp:326
SourceLocation getBeginLoc() const LLVM_READONLY
Definition: Stmt.cpp:338
const SwitchCase * getNextSwitchCase() const
Definition: Stmt.h:1779
SwitchStmt - This represents a 'switch' stmt.
Definition: Stmt.h:2393
SwitchCase * getSwitchCaseList()
Definition: Stmt.h:2530
Base class for declarations which introduce a typedef-name.
Definition: Decl.h:3433
QualType getType() const
Definition: Decl.h:717
Represents a variable declaration or definition.
Definition: Decl.h:918
@ CallInit
Call-style initialization (C++98)
Definition: Decl.h:926
The JSON file list parser is used to communicate input to InstallAPI.
@ SD_Automatic
Automatic storage duration (most local variables).
Definition: Specifiers.h:326
const FunctionProtoType * T