21 #include "llvm/ADT/BitVector.h"
22 using namespace clang;
32 class JumpScopeChecker {
37 const bool Permissive;
60 GotoScope(
unsigned parentScope,
unsigned InDiag,
unsigned OutDiag,
62 : ParentScope(parentScope), InDiag(InDiag), OutDiag(OutDiag), Loc(L) {}
66 llvm::DenseMap<Stmt*, unsigned> LabelAndGotoScopes;
74 void BuildScopeInformation(
Decl *D,
unsigned &ParentScope);
76 unsigned &ParentScope);
77 void BuildScopeInformation(
Stmt *
S,
unsigned &origParentScope);
80 void VerifyIndirectJumps();
85 unsigned JumpDiag,
unsigned JumpDiagWarning,
86 unsigned JumpDiagCXX98Compat);
89 unsigned GetDeepestCommonScope(
unsigned A,
unsigned B);
93 #define CHECK_PERMISSIVE(x) (assert(Permissive || !(x)), (Permissive && (x)))
95 JumpScopeChecker::JumpScopeChecker(
Stmt *Body,
Sema &s)
96 :
S(s), Permissive(s.hasAnyUnrecoverableErrorsInThisFunction()) {
102 unsigned BodyParentScope = 0;
103 BuildScopeInformation(Body, BodyParentScope);
107 VerifyIndirectJumps();
112 unsigned JumpScopeChecker::GetDeepestCommonScope(
unsigned A,
unsigned B) {
117 assert(Scopes[B].ParentScope < B);
118 B = Scopes[B].ParentScope;
120 assert(Scopes[A].ParentScope < A);
121 A = Scopes[A].ParentScope;
132 if (
const VarDecl *VD = dyn_cast<VarDecl>(D)) {
134 unsigned OutDiag = 0;
136 if (VD->getType()->isVariablyModifiedType())
137 InDiag = diag::note_protected_by_vla;
139 if (VD->hasAttr<BlocksAttr>())
140 return ScopePair(diag::note_protected_by___block,
141 diag::note_exits___block);
143 if (VD->hasAttr<CleanupAttr>())
144 return ScopePair(diag::note_protected_by_cleanup,
145 diag::note_exits_cleanup);
147 if (VD->hasLocalStorage()) {
148 switch (VD->getType().isDestructedType()) {
149 case QualType::DK_objc_strong_lifetime:
150 case QualType::DK_objc_weak_lifetime:
151 return ScopePair(diag::note_protected_by_objc_ownership,
152 diag::note_exits_objc_ownership);
154 case QualType::DK_cxx_destructor:
155 OutDiag = diag::note_exits_dtor;
158 case QualType::DK_none:
163 const Expr *Init = VD->getInit();
179 InDiag = diag::note_protected_by_variable_init;
187 VD->getInitStyle() == VarDecl::CallInit) {
189 InDiag = diag::note_protected_by_variable_nontriv_destructor;
191 InDiag = diag::note_protected_by_variable_non_pod;
202 if (TD->getUnderlyingType()->isVariablyModifiedType())
204 ? diag::note_protected_by_vla_typedef
205 : diag::note_protected_by_vla_type_alias,
213 void JumpScopeChecker::BuildScopeInformation(
Decl *D,
unsigned &ParentScope) {
216 if (Diags.first || Diags.second) {
217 Scopes.push_back(GotoScope(ParentScope, Diags.first, Diags.second,
219 ParentScope = Scopes.size()-1;
224 if (
VarDecl *VD = dyn_cast<VarDecl>(D))
225 if (
Expr *Init = VD->getInit())
226 BuildScopeInformation(Init, ParentScope);
230 void JumpScopeChecker::BuildScopeInformation(
VarDecl *D,
232 unsigned &ParentScope) {
239 if (destructKind != QualType::DK_none) {
240 std::pair<unsigned,unsigned> Diags;
241 switch (destructKind) {
242 case QualType::DK_cxx_destructor:
243 Diags =
ScopePair(diag::note_enters_block_captures_cxx_obj,
244 diag::note_exits_block_captures_cxx_obj);
246 case QualType::DK_objc_strong_lifetime:
247 Diags =
ScopePair(diag::note_enters_block_captures_strong,
248 diag::note_exits_block_captures_strong);
250 case QualType::DK_objc_weak_lifetime:
251 Diags =
ScopePair(diag::note_enters_block_captures_weak,
252 diag::note_exits_block_captures_weak);
254 case QualType::DK_none:
255 llvm_unreachable(
"non-lifetime captured variable");
260 Scopes.push_back(GotoScope(ParentScope,
261 Diags.first, Diags.second, Loc));
262 ParentScope = Scopes.size()-1;
270 void JumpScopeChecker::BuildScopeInformation(
Stmt *
S,
unsigned &origParentScope) {
274 unsigned independentParentScope = origParentScope;
275 unsigned &ParentScope = ((isa<Expr>(
S) && !isa<StmtExpr>(S))
276 ? origParentScope : independentParentScope);
278 bool SkipFirstSubStmt =
false;
281 switch (S->getStmtClass()) {
282 case Stmt::AddrLabelExprClass:
283 IndirectJumpTargets.push_back(cast<AddrLabelExpr>(S)->getLabel());
286 case Stmt::IndirectGotoStmtClass:
292 if (cast<IndirectGotoStmt>(S)->getConstantTarget()) {
293 LabelAndGotoScopes[
S] = ParentScope;
298 LabelAndGotoScopes[
S] = ParentScope;
299 IndirectJumps.push_back(cast<IndirectGotoStmt>(S));
302 case Stmt::SwitchStmtClass:
305 if (
VarDecl *Var = cast<SwitchStmt>(S)->getConditionVariable()) {
306 BuildScopeInformation(Var, ParentScope);
307 SkipFirstSubStmt =
true;
311 case Stmt::GotoStmtClass:
314 LabelAndGotoScopes[
S] = ParentScope;
318 case Stmt::CXXTryStmtClass: {
320 unsigned newParentScope;
321 Scopes.push_back(GotoScope(ParentScope,
322 diag::note_protected_by_cxx_try,
323 diag::note_exits_cxx_try,
324 TS->getSourceRange().getBegin()));
326 BuildScopeInformation(TryBlock, (newParentScope = Scopes.size()-1));
331 Scopes.push_back(GotoScope(ParentScope,
332 diag::note_protected_by_cxx_catch,
333 diag::note_exits_cxx_catch,
334 CS->getSourceRange().getBegin()));
336 (newParentScope = Scopes.size()-1));
341 case Stmt::SEHTryStmtClass: {
343 unsigned newParentScope;
344 Scopes.push_back(GotoScope(ParentScope,
345 diag::note_protected_by_seh_try,
346 diag::note_exits_seh_try,
347 TS->getSourceRange().getBegin()));
349 BuildScopeInformation(TryBlock, (newParentScope = Scopes.size()-1));
353 Scopes.push_back(GotoScope(ParentScope,
354 diag::note_protected_by_seh_except,
355 diag::note_exits_seh_except,
356 Except->getSourceRange().getBegin()));
357 BuildScopeInformation(Except->getBlock(),
358 (newParentScope = Scopes.size()-1));
360 Scopes.push_back(GotoScope(ParentScope,
361 diag::note_protected_by_seh_finally,
362 diag::note_exits_seh_finally,
363 Finally->getSourceRange().getBegin()));
364 BuildScopeInformation(Finally->getBlock(),
365 (newParentScope = Scopes.size()-1));
375 for (
Stmt *SubStmt : S->children()) {
376 if (SkipFirstSubStmt) {
377 SkipFirstSubStmt =
false;
381 if (!SubStmt)
continue;
388 if (
CaseStmt *CS = dyn_cast<CaseStmt>(SubStmt))
389 Next = CS->getSubStmt();
390 else if (
DefaultStmt *DS = dyn_cast<DefaultStmt>(SubStmt))
391 Next = DS->getSubStmt();
392 else if (
LabelStmt *LS = dyn_cast<LabelStmt>(SubStmt))
393 Next = LS->getSubStmt();
397 LabelAndGotoScopes[SubStmt] = ParentScope;
403 if (
DeclStmt *DS = dyn_cast<DeclStmt>(SubStmt)) {
406 for (
auto *I : DS->decls())
407 BuildScopeInformation(I, ParentScope);
413 unsigned newParentScope;
415 Scopes.push_back(GotoScope(ParentScope,
416 diag::note_protected_by_objc_try,
417 diag::note_exits_objc_try,
419 if (
Stmt *TryPart = AT->getTryBody())
420 BuildScopeInformation(TryPart, (newParentScope = Scopes.size()-1));
423 for (
unsigned I = 0, N = AT->getNumCatchStmts(); I != N; ++I) {
425 Scopes.push_back(GotoScope(ParentScope,
426 diag::note_protected_by_objc_catch,
427 diag::note_exits_objc_catch,
431 (newParentScope = Scopes.size()-1));
436 Scopes.push_back(GotoScope(ParentScope,
437 diag::note_protected_by_objc_finally,
438 diag::note_exits_objc_finally,
439 AF->getAtFinallyLoc()));
440 BuildScopeInformation(AF, (newParentScope = Scopes.size()-1));
446 unsigned newParentScope;
450 dyn_cast<ObjCAtSynchronizedStmt>(SubStmt)) {
453 BuildScopeInformation(AS->getSynchExpr(), ParentScope);
457 Scopes.push_back(GotoScope(ParentScope,
458 diag::note_protected_by_objc_synchronized,
459 diag::note_exits_objc_synchronized,
460 AS->getAtSynchronizedLoc()));
461 BuildScopeInformation(AS->getSynchBody(),
462 (newParentScope = Scopes.size()-1));
468 dyn_cast<ObjCAutoreleasePoolStmt>(SubStmt)) {
471 Scopes.push_back(GotoScope(ParentScope,
472 diag::note_protected_by_objc_autoreleasepool,
473 diag::note_exits_objc_autoreleasepool,
475 BuildScopeInformation(AS->getSubStmt(),
476 (newParentScope = Scopes.size() - 1));
484 for (
unsigned i = 0, e = EWC->getNumObjects(); i != e; ++i) {
485 const BlockDecl *BDecl = EWC->getObject(i);
486 for (
const auto &CI : BDecl->
captures()) {
487 VarDecl *variable = CI.getVariable();
488 BuildScopeInformation(variable, BDecl, ParentScope);
496 dyn_cast<MaterializeTemporaryExpr>(SubStmt)) {
500 const Expr *ExtendedObject =
502 CommaLHS, Adjustments);
504 Scopes.push_back(GotoScope(ParentScope, 0,
505 diag::note_exits_temporary_dtor,
507 ParentScope = Scopes.size()-1;
513 BuildScopeInformation(SubStmt, ParentScope);
519 void JumpScopeChecker::VerifyJumps() {
520 while (!Jumps.empty()) {
521 Stmt *Jump = Jumps.pop_back_val();
524 if (
GotoStmt *GS = dyn_cast<GotoStmt>(Jump)) {
526 if (GS->getLabel()->getStmt()) {
527 CheckJump(GS, GS->getLabel()->getStmt(), GS->getGotoLoc(),
528 diag::err_goto_into_protected_scope,
529 diag::ext_goto_into_protected_scope,
530 diag::warn_cxx98_compat_goto_into_protected_scope);
538 LabelDecl *Target = IGS->getConstantTarget();
539 CheckJump(IGS, Target->
getStmt(), IGS->getGotoLoc(),
540 diag::err_goto_into_protected_scope,
541 diag::ext_goto_into_protected_scope,
542 diag::warn_cxx98_compat_goto_into_protected_scope);
552 if (
CaseStmt *CS = dyn_cast<CaseStmt>(SC))
554 else if (
DefaultStmt *DS = dyn_cast<DefaultStmt>(SC))
555 Loc = DS->getLocStart();
557 Loc = SC->getLocStart();
558 CheckJump(SS, SC, Loc, diag::err_switch_into_protected_scope, 0,
559 diag::warn_cxx98_compat_switch_into_protected_scope);
583 void JumpScopeChecker::VerifyIndirectJumps() {
584 if (IndirectJumps.empty())
return;
588 if (IndirectJumpTargets.empty()) {
589 S.Diag(IndirectJumps[0]->getGotoLoc(),
590 diag::err_indirect_goto_without_addrlabel);
597 typedef std::pair<unsigned, IndirectGotoStmt*> JumpScope;
600 llvm::DenseMap<unsigned, IndirectGotoStmt*> JumpScopesMap;
602 I = IndirectJumps.begin(), E = IndirectJumps.end(); I != E; ++I) {
606 unsigned IGScope = LabelAndGotoScopes[IG];
608 if (!Entry) Entry = IG;
610 JumpScopes.reserve(JumpScopesMap.size());
611 for (llvm::DenseMap<unsigned, IndirectGotoStmt*>::iterator
612 I = JumpScopesMap.begin(), E = JumpScopesMap.end(); I != E; ++I)
613 JumpScopes.push_back(*I);
619 llvm::DenseMap<unsigned, LabelDecl*> TargetScopes;
621 I = IndirectJumpTargets.begin(), E = IndirectJumpTargets.end();
626 unsigned LabelScope = LabelAndGotoScopes[TheLabel->
getStmt()];
627 LabelDecl *&Target = TargetScopes[LabelScope];
628 if (!Target) Target = TheLabel;
639 llvm::BitVector Reachable(Scopes.size(),
false);
640 for (llvm::DenseMap<unsigned,LabelDecl*>::iterator
641 TI = TargetScopes.begin(), TE = TargetScopes.end(); TI != TE; ++TI) {
642 unsigned TargetScope = TI->first;
650 unsigned Min = TargetScope;
658 if (Scopes[Min].InDiag)
break;
660 Min = Scopes[Min].ParentScope;
666 I = JumpScopes.begin(), E = JumpScopes.end(); I != E; ++I) {
667 unsigned Scope = I->first;
674 bool IsReachable =
false;
676 if (Reachable.test(Scope)) {
679 for (
unsigned S = I->first; S != Scope; S = Scopes[S].ParentScope)
687 if (Scope == 0 || Scope < Min)
break;
690 if (Scopes[Scope].OutDiag)
break;
692 Scope = Scopes[Scope].ParentScope;
696 if (IsReachable)
continue;
698 DiagnoseIndirectJump(I->second, I->first, TargetLabel, TargetScope);
706 return (JumpDiag == diag::err_goto_into_protected_scope &&
707 (InDiagNote == diag::note_protected_by_variable_init ||
708 InDiagNote == diag::note_protected_by_variable_nontriv_destructor));
715 InDiagNote == diag::note_protected_by_variable_non_pod;
723 S.
Diag(Jump->
getGotoLoc(), diag::err_indirect_goto_in_protected_scope);
732 for (
unsigned I = 0, E = ToScopes.size(); I != E; ++I)
733 if (Scopes[ToScopes[I]].InDiag)
734 S.Diag(Scopes[ToScopes[I]].Loc, Scopes[ToScopes[I]].InDiag);
741 unsigned TargetScope) {
745 unsigned Common = GetDeepestCommonScope(JumpScope, TargetScope);
746 bool Diagnosed =
false;
749 for (
unsigned I = JumpScope; I != Common; I = Scopes[I].ParentScope)
750 if (Scopes[I].OutDiag) {
752 S.Diag(Scopes[I].Loc, Scopes[I].OutDiag);
758 for (
unsigned I = TargetScope; I != Common; I = Scopes[I].ParentScope)
760 ToScopesCXX98Compat.push_back(I);
761 else if (Scopes[I].InDiag) {
763 S.Diag(Scopes[I].Loc, Scopes[I].InDiag);
767 if (!Diagnosed && !ToScopesCXX98Compat.empty()) {
769 diag::warn_cxx98_compat_indirect_goto_in_protected_scope);
771 NoteJumpIntoScopes(ToScopesCXX98Compat);
778 unsigned JumpDiagError,
unsigned JumpDiagWarning,
779 unsigned JumpDiagCXX98Compat) {
785 unsigned FromScope = LabelAndGotoScopes[From];
786 unsigned ToScope = LabelAndGotoScopes[To];
789 if (FromScope == ToScope)
return;
792 if (isa<GotoStmt>(From) || isa<IndirectGotoStmt>(From)) {
795 for (
unsigned I = FromScope; I > ToScope; I = Scopes[I].ParentScope) {
796 if (Scopes[I].InDiag == diag::note_protected_by_seh_finally) {
797 S.Diag(From->getLocStart(), diag::warn_jump_out_of_seh_finally);
803 unsigned CommonScope = GetDeepestCommonScope(FromScope, ToScope);
806 if (CommonScope == ToScope)
return;
812 for (
unsigned I = ToScope; I != CommonScope; I = Scopes[I].ParentScope) {
813 if (S.getLangOpts().MSVCCompat && JumpDiagWarning != 0 &&
815 ToScopesWarning.push_back(I);
817 ToScopesCXX98Compat.push_back(I);
818 else if (Scopes[I].InDiag)
819 ToScopesError.push_back(I);
823 if (!ToScopesWarning.empty()) {
824 S.Diag(DiagLoc, JumpDiagWarning);
825 NoteJumpIntoScopes(ToScopesWarning);
829 if (!ToScopesError.empty()) {
830 S.Diag(DiagLoc, JumpDiagError);
831 NoteJumpIntoScopes(ToScopesError);
835 if (ToScopesError.empty() && !ToScopesCXX98Compat.empty()) {
836 S.Diag(DiagLoc, JumpDiagCXX98Compat);
837 NoteJumpIntoScopes(ToScopesCXX98Compat);
841 void JumpScopeChecker::CheckGotoStmt(
GotoStmt *GS) {
843 S.Diag(GS->
getGotoLoc(), diag::err_goto_ms_asm_label)
850 void Sema::DiagnoseInvalidJumps(
Stmt *Body) {
851 (void)JumpScopeChecker(Body, *
this);
const SwitchCase * getNextSwitchCase() const
DestructionKind isDestructedType() const
IdentifierInfo * getIdentifier() const
const LangOptions & getLangOpts() const
CXXCatchStmt * getHandler(unsigned i)
static bool IsMicrosoftJumpWarning(unsigned JumpDiag, unsigned InDiagNote)
SemaDiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID)
Emit a diagnostic.
Represents a call to a C++ constructor.
Represents a C++ constructor within a class.
Represents a prvalue temporary that is written into memory so that a reference can bind to it...
SourceLocation getLocStart() const LLVM_READONLY
#define CHECK_PERMISSIVE(x)
Defines the Objective-C statement AST node classes.
Defines the clang::Expr interface and subclasses for C++ expressions.
Represents Objective-C's @catch statement.
const LangOptions & getLangOpts() const
const CXXRecordDecl * getParent() const
Stmt * getHandlerBlock() const
const Expr * skipRValueSubobjectAdjustments(SmallVectorImpl< const Expr * > &CommaLHS, SmallVectorImpl< SubobjectAdjustment > &Adjustments) const
LabelStmt * getStmt() const
const Stmt * getCatchBody() const
Sema - This implements semantic analysis and AST building for C.
SourceLocation getGotoLoc() const
Represents Objective-C's @synchronized statement.
bool isMSAsmLabel() const
const SwitchCase * getSwitchCaseList() const
std::pair< unsigned, unsigned > ScopePair
LabelDecl * getLabel() const
SourceLocation getGotoLoc() const
Encodes a location in the source. The SourceManager can decode this to get at the full include stack...
SourceLocation getIdentLoc() const
SEHExceptStmt * getExceptHandler() const
Returns 0 if not defined.
SourceLocation getExprLoc() const LLVM_READONLY
Base class for declarations which introduce a typedef-name.
unsigned getNumHandlers() const
SourceLocation getAtCatchLoc() const
Represents Objective-C's @finally statement.
bool isDefaultConstructor() const
static ScopePair GetDiagForGotoScopeDecl(Sema &S, const Decl *D)
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate.h) and friends (in DeclFriend.h).
static void DiagnoseIndirectJumpStmt(Sema &S, IndirectGotoStmt *Jump, LabelDecl *Target, bool &Diagnosed)
Produce primary diagnostic for an indirect jump statement.
CompoundStmt * getTryBlock()
Represents Objective-C's @try ... @catch ... @finally statement.
SourceLocation getLocation() const
CompoundStmt * getTryBlock() const
Automatic storage duration (most local variables).
Represents Objective-C's @autoreleasepool Statement.
SEHFinallyStmt * getFinallyHandler() const
static bool IsCXX98CompatWarning(Sema &S, unsigned InDiagNote)
bool isPOD() const
Whether this class is a POD-type (C++ [class]p4)