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 return ScopePair(diag::note_protected_by_objc_strong_init,
151 diag::note_exits_objc_strong);
153 case QualType::DK_objc_weak_lifetime:
154 return ScopePair(diag::note_protected_by_objc_weak_init,
155 diag::note_exits_objc_weak);
157 case QualType::DK_cxx_destructor:
158 OutDiag = diag::note_exits_dtor;
161 case QualType::DK_none:
166 const Expr *Init = VD->getInit();
182 InDiag = diag::note_protected_by_variable_init;
190 VD->getInitStyle() == VarDecl::CallInit) {
192 InDiag = diag::note_protected_by_variable_nontriv_destructor;
194 InDiag = diag::note_protected_by_variable_non_pod;
205 if (TD->getUnderlyingType()->isVariablyModifiedType())
207 ? diag::note_protected_by_vla_typedef
208 : diag::note_protected_by_vla_type_alias,
216 void JumpScopeChecker::BuildScopeInformation(
Decl *D,
unsigned &ParentScope) {
219 if (Diags.first || Diags.second) {
220 Scopes.push_back(GotoScope(ParentScope, Diags.first, Diags.second,
222 ParentScope = Scopes.size()-1;
227 if (
VarDecl *VD = dyn_cast<VarDecl>(D))
228 if (
Expr *Init = VD->getInit())
229 BuildScopeInformation(Init, ParentScope);
233 void JumpScopeChecker::BuildScopeInformation(
VarDecl *D,
235 unsigned &ParentScope) {
242 if (destructKind != QualType::DK_none) {
243 std::pair<unsigned,unsigned> Diags;
244 switch (destructKind) {
245 case QualType::DK_cxx_destructor:
246 Diags =
ScopePair(diag::note_enters_block_captures_cxx_obj,
247 diag::note_exits_block_captures_cxx_obj);
249 case QualType::DK_objc_strong_lifetime:
250 Diags =
ScopePair(diag::note_enters_block_captures_strong,
251 diag::note_exits_block_captures_strong);
253 case QualType::DK_objc_weak_lifetime:
254 Diags =
ScopePair(diag::note_enters_block_captures_weak,
255 diag::note_exits_block_captures_weak);
257 case QualType::DK_none:
258 llvm_unreachable(
"non-lifetime captured variable");
263 Scopes.push_back(GotoScope(ParentScope,
264 Diags.first, Diags.second, Loc));
265 ParentScope = Scopes.size()-1;
273 void JumpScopeChecker::BuildScopeInformation(
Stmt *
S,
unsigned &origParentScope) {
277 unsigned independentParentScope = origParentScope;
278 unsigned &ParentScope = ((isa<Expr>(
S) && !isa<StmtExpr>(S))
279 ? origParentScope : independentParentScope);
281 bool SkipFirstSubStmt =
false;
284 switch (S->getStmtClass()) {
285 case Stmt::AddrLabelExprClass:
286 IndirectJumpTargets.push_back(cast<AddrLabelExpr>(S)->getLabel());
289 case Stmt::IndirectGotoStmtClass:
295 if (cast<IndirectGotoStmt>(S)->getConstantTarget()) {
296 LabelAndGotoScopes[
S] = ParentScope;
301 LabelAndGotoScopes[
S] = ParentScope;
302 IndirectJumps.push_back(cast<IndirectGotoStmt>(S));
305 case Stmt::SwitchStmtClass:
308 if (
VarDecl *Var = cast<SwitchStmt>(S)->getConditionVariable()) {
309 BuildScopeInformation(Var, ParentScope);
310 SkipFirstSubStmt =
true;
314 case Stmt::GotoStmtClass:
317 LabelAndGotoScopes[
S] = ParentScope;
321 case Stmt::CXXTryStmtClass: {
323 unsigned newParentScope;
324 Scopes.push_back(GotoScope(ParentScope,
325 diag::note_protected_by_cxx_try,
326 diag::note_exits_cxx_try,
327 TS->getSourceRange().getBegin()));
329 BuildScopeInformation(TryBlock, (newParentScope = Scopes.size()-1));
334 Scopes.push_back(GotoScope(ParentScope,
335 diag::note_protected_by_cxx_catch,
336 diag::note_exits_cxx_catch,
337 CS->getSourceRange().getBegin()));
339 (newParentScope = Scopes.size()-1));
344 case Stmt::SEHTryStmtClass: {
346 unsigned newParentScope;
347 Scopes.push_back(GotoScope(ParentScope,
348 diag::note_protected_by_seh_try,
349 diag::note_exits_seh_try,
350 TS->getSourceRange().getBegin()));
352 BuildScopeInformation(TryBlock, (newParentScope = Scopes.size()-1));
356 Scopes.push_back(GotoScope(ParentScope,
357 diag::note_protected_by_seh_except,
358 diag::note_exits_seh_except,
359 Except->getSourceRange().getBegin()));
360 BuildScopeInformation(Except->getBlock(),
361 (newParentScope = Scopes.size()-1));
363 Scopes.push_back(GotoScope(ParentScope,
364 diag::note_protected_by_seh_finally,
365 diag::note_exits_seh_finally,
366 Finally->getSourceRange().getBegin()));
367 BuildScopeInformation(Finally->getBlock(),
368 (newParentScope = Scopes.size()-1));
378 for (
Stmt *SubStmt : S->children()) {
379 if (SkipFirstSubStmt) {
380 SkipFirstSubStmt =
false;
384 if (!SubStmt)
continue;
391 if (
CaseStmt *CS = dyn_cast<CaseStmt>(SubStmt))
392 Next = CS->getSubStmt();
393 else if (
DefaultStmt *DS = dyn_cast<DefaultStmt>(SubStmt))
394 Next = DS->getSubStmt();
395 else if (
LabelStmt *LS = dyn_cast<LabelStmt>(SubStmt))
396 Next = LS->getSubStmt();
400 LabelAndGotoScopes[SubStmt] = ParentScope;
406 if (
DeclStmt *DS = dyn_cast<DeclStmt>(SubStmt)) {
409 for (
auto *
I : DS->decls())
410 BuildScopeInformation(
I, ParentScope);
416 unsigned newParentScope;
418 Scopes.push_back(GotoScope(ParentScope,
419 diag::note_protected_by_objc_try,
420 diag::note_exits_objc_try,
422 if (
Stmt *TryPart = AT->getTryBody())
423 BuildScopeInformation(TryPart, (newParentScope = Scopes.size()-1));
426 for (
unsigned I = 0, N = AT->getNumCatchStmts();
I != N; ++
I) {
428 Scopes.push_back(GotoScope(ParentScope,
429 diag::note_protected_by_objc_catch,
430 diag::note_exits_objc_catch,
434 (newParentScope = Scopes.size()-1));
439 Scopes.push_back(GotoScope(ParentScope,
440 diag::note_protected_by_objc_finally,
441 diag::note_exits_objc_finally,
442 AF->getAtFinallyLoc()));
443 BuildScopeInformation(AF, (newParentScope = Scopes.size()-1));
449 unsigned newParentScope;
453 dyn_cast<ObjCAtSynchronizedStmt>(SubStmt)) {
456 BuildScopeInformation(AS->getSynchExpr(), ParentScope);
460 Scopes.push_back(GotoScope(ParentScope,
461 diag::note_protected_by_objc_synchronized,
462 diag::note_exits_objc_synchronized,
463 AS->getAtSynchronizedLoc()));
464 BuildScopeInformation(AS->getSynchBody(),
465 (newParentScope = Scopes.size()-1));
471 dyn_cast<ObjCAutoreleasePoolStmt>(SubStmt)) {
474 Scopes.push_back(GotoScope(ParentScope,
475 diag::note_protected_by_objc_autoreleasepool,
476 diag::note_exits_objc_autoreleasepool,
478 BuildScopeInformation(AS->getSubStmt(),
479 (newParentScope = Scopes.size() - 1));
487 for (
unsigned i = 0, e = EWC->getNumObjects(); i != e; ++i) {
488 const BlockDecl *BDecl = EWC->getObject(i);
489 for (
const auto &CI : BDecl->
captures()) {
490 VarDecl *variable = CI.getVariable();
491 BuildScopeInformation(variable, BDecl, ParentScope);
499 dyn_cast<MaterializeTemporaryExpr>(SubStmt)) {
503 const Expr *ExtendedObject =
505 CommaLHS, Adjustments);
507 Scopes.push_back(GotoScope(ParentScope, 0,
508 diag::note_exits_temporary_dtor,
510 ParentScope = Scopes.size()-1;
516 BuildScopeInformation(SubStmt, ParentScope);
522 void JumpScopeChecker::VerifyJumps() {
523 while (!Jumps.empty()) {
524 Stmt *Jump = Jumps.pop_back_val();
527 if (
GotoStmt *GS = dyn_cast<GotoStmt>(Jump)) {
529 if (GS->getLabel()->getStmt()) {
530 CheckJump(GS, GS->getLabel()->getStmt(), GS->getGotoLoc(),
531 diag::err_goto_into_protected_scope,
532 diag::ext_goto_into_protected_scope,
533 diag::warn_cxx98_compat_goto_into_protected_scope);
541 LabelDecl *Target = IGS->getConstantTarget();
542 CheckJump(IGS, Target->
getStmt(), IGS->getGotoLoc(),
543 diag::err_goto_into_protected_scope,
544 diag::ext_goto_into_protected_scope,
545 diag::warn_cxx98_compat_goto_into_protected_scope);
555 if (
CaseStmt *CS = dyn_cast<CaseStmt>(SC))
557 else if (
DefaultStmt *DS = dyn_cast<DefaultStmt>(SC))
558 Loc = DS->getLocStart();
560 Loc = SC->getLocStart();
561 CheckJump(SS, SC, Loc, diag::err_switch_into_protected_scope, 0,
562 diag::warn_cxx98_compat_switch_into_protected_scope);
586 void JumpScopeChecker::VerifyIndirectJumps() {
587 if (IndirectJumps.empty())
return;
591 if (IndirectJumpTargets.empty()) {
592 S.Diag(IndirectJumps[0]->getGotoLoc(),
593 diag::err_indirect_goto_without_addrlabel);
600 typedef std::pair<unsigned, IndirectGotoStmt*> JumpScope;
603 llvm::DenseMap<unsigned, IndirectGotoStmt*> JumpScopesMap;
605 I = IndirectJumps.begin(),
E = IndirectJumps.end();
I !=
E; ++
I) {
609 unsigned IGScope = LabelAndGotoScopes[IG];
611 if (!Entry) Entry = IG;
613 JumpScopes.reserve(JumpScopesMap.size());
615 I = JumpScopesMap.begin(),
E = JumpScopesMap.end();
I !=
E; ++
I)
616 JumpScopes.push_back(*
I);
622 llvm::DenseMap<unsigned, LabelDecl*> TargetScopes;
624 I = IndirectJumpTargets.begin(),
E = IndirectJumpTargets.end();
629 unsigned LabelScope = LabelAndGotoScopes[TheLabel->
getStmt()];
630 LabelDecl *&Target = TargetScopes[LabelScope];
631 if (!Target) Target = TheLabel;
642 llvm::BitVector Reachable(Scopes.size(),
false);
644 TI = TargetScopes.begin(), TE = TargetScopes.end(); TI != TE; ++TI) {
645 unsigned TargetScope = TI->first;
653 unsigned Min = TargetScope;
661 if (Scopes[Min].InDiag)
break;
663 Min = Scopes[Min].ParentScope;
669 I = JumpScopes.begin(),
E = JumpScopes.end();
I !=
E; ++
I) {
670 unsigned Scope =
I->first;
677 bool IsReachable =
false;
679 if (Reachable.test(Scope)) {
682 for (
unsigned S =
I->first; S != Scope; S = Scopes[S].ParentScope)
690 if (Scope == 0 || Scope < Min)
break;
693 if (Scopes[Scope].OutDiag)
break;
695 Scope = Scopes[Scope].ParentScope;
699 if (IsReachable)
continue;
701 DiagnoseIndirectJump(
I->second,
I->first, TargetLabel, TargetScope);
709 return (JumpDiag == diag::err_goto_into_protected_scope &&
710 (InDiagNote == diag::note_protected_by_variable_init ||
711 InDiagNote == diag::note_protected_by_variable_nontriv_destructor));
718 InDiagNote == diag::note_protected_by_variable_non_pod;
726 S.
Diag(Jump->
getGotoLoc(), diag::err_indirect_goto_in_protected_scope);
735 for (
unsigned I = 0,
E = ToScopes.size();
I !=
E; ++
I)
736 if (Scopes[ToScopes[
I]].InDiag)
737 S.Diag(Scopes[ToScopes[
I]].Loc, Scopes[ToScopes[
I]].InDiag);
744 unsigned TargetScope) {
748 unsigned Common = GetDeepestCommonScope(JumpScope, TargetScope);
749 bool Diagnosed =
false;
752 for (
unsigned I = JumpScope;
I != Common;
I = Scopes[
I].ParentScope)
753 if (Scopes[
I].OutDiag) {
755 S.Diag(Scopes[
I].Loc, Scopes[
I].OutDiag);
761 for (
unsigned I = TargetScope;
I != Common;
I = Scopes[
I].ParentScope)
763 ToScopesCXX98Compat.push_back(
I);
764 else if (Scopes[
I].InDiag) {
766 S.Diag(Scopes[
I].Loc, Scopes[
I].InDiag);
770 if (!Diagnosed && !ToScopesCXX98Compat.empty()) {
772 diag::warn_cxx98_compat_indirect_goto_in_protected_scope);
774 NoteJumpIntoScopes(ToScopesCXX98Compat);
781 unsigned JumpDiagError,
unsigned JumpDiagWarning,
782 unsigned JumpDiagCXX98Compat) {
788 unsigned FromScope = LabelAndGotoScopes[From];
789 unsigned ToScope = LabelAndGotoScopes[To];
792 if (FromScope == ToScope)
return;
795 if (isa<GotoStmt>(From) || isa<IndirectGotoStmt>(From)) {
798 for (
unsigned I = FromScope;
I > ToScope;
I = Scopes[
I].ParentScope) {
799 if (Scopes[
I].InDiag == diag::note_protected_by_seh_finally) {
800 S.Diag(From->getLocStart(), diag::warn_jump_out_of_seh_finally);
806 unsigned CommonScope = GetDeepestCommonScope(FromScope, ToScope);
809 if (CommonScope == ToScope)
return;
815 for (
unsigned I = ToScope;
I != CommonScope;
I = Scopes[
I].ParentScope) {
816 if (S.getLangOpts().MSVCCompat && JumpDiagWarning != 0 &&
818 ToScopesWarning.push_back(
I);
820 ToScopesCXX98Compat.push_back(
I);
821 else if (Scopes[
I].InDiag)
822 ToScopesError.push_back(
I);
826 if (!ToScopesWarning.empty()) {
827 S.Diag(DiagLoc, JumpDiagWarning);
828 NoteJumpIntoScopes(ToScopesWarning);
832 if (!ToScopesError.empty()) {
833 S.Diag(DiagLoc, JumpDiagError);
834 NoteJumpIntoScopes(ToScopesError);
838 if (ToScopesError.empty() && !ToScopesCXX98Compat.empty()) {
839 S.Diag(DiagLoc, JumpDiagCXX98Compat);
840 NoteJumpIntoScopes(ToScopesCXX98Compat);
844 void JumpScopeChecker::CheckGotoStmt(
GotoStmt *GS) {
846 S.Diag(GS->
getGotoLoc(), diag::err_goto_ms_asm_label)
853 void Sema::DiagnoseInvalidJumps(
Stmt *Body) {
854 (void)JumpScopeChecker(Body, *
this);
const SwitchCase * getNextSwitchCase() const
A (possibly-)qualified type.
DestructionKind isDestructedType() const
Returns a nonzero value if objects of this type require non-trivial work to clean up after...
IdentifierInfo * getIdentifier() const
getIdentifier - Get the identifier that names this declaration, if there is one.
const LangOptions & getLangOpts() const
CXXCatchStmt * getHandler(unsigned i)
static bool IsMicrosoftJumpWarning(unsigned JumpDiag, unsigned InDiagNote)
Return true if a particular error+note combination must be downgraded to a warning in Microsoft mode...
SemaDiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID)
Emit a diagnostic.
Decl - This represents one declaration (or definition), e.g.
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...
VarDecl - An instance of this class is created to represent a variable declaration or definition...
SourceLocation getLocStart() const LLVM_READONLY
#define CHECK_PERMISSIVE(x)
Defines the Objective-C statement AST node classes.
Represents an expression – generally a full-expression – that introduces cleanups to be run at the en...
Defines the clang::Expr interface and subclasses for C++ expressions.
LabelStmt - Represents a label, which has a substatement.
Represents Objective-C's @catch statement.
IndirectGotoStmt - This represents an indirect goto.
const LangOptions & getLangOpts() const
const CXXRecordDecl * getParent() const
Returns the parent of this method declaration, which is the class in which this method is defined...
Stmt * getHandlerBlock() const
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...
LabelStmt * getStmt() const
Scope - A scope is a transient data structure that is used while parsing the program.
const Stmt * getCatchBody() const
detail::InMemoryDirectory::const_iterator I
Sema - This implements semantic analysis and AST building for C.
BlockDecl - This represents a block literal declaration, which is like an unnamed FunctionDecl...
Expr - This represents one expression.
SourceLocation getGotoLoc() const
Represents Objective-C's @synchronized statement.
CXXTryStmt - A C++ try block, including all handlers.
bool isMSAsmLabel() const
const SwitchCase * getSwitchCaseList() const
std::pair< unsigned, unsigned > ScopePair
LabelDecl * getLabel() const
SourceLocation getGotoLoc() const
Encodes a location in the source.
const TemplateArgument * iterator
DeclStmt - Adaptor class for mixing declarations with statements and expressions. ...
LabelDecl - Represents the declaration of a label.
SourceLocation getIdentLoc() const
SEHExceptStmt * getExceptHandler() const
Returns 0 if not defined.
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
Base class for declarations which introduce a typedef-name.
unsigned getNumHandlers() const
detail::InMemoryDirectory::const_iterator E
SwitchStmt - This represents a 'switch' stmt.
SourceLocation getAtCatchLoc() const
bool isTrivial() const
Whether this function is "trivial" in some specialized C++ senses.
Represents Objective-C's @finally statement.
bool isDefaultConstructor() const
Whether this constructor is a default constructor (C++ [class.ctor]p5), which can be used to default-...
GotoStmt - This represents a direct goto.
static ScopePair GetDiagForGotoScopeDecl(Sema &S, const Decl *D)
GetDiagForGotoScopeDecl - If this decl induces a new goto scope, return a diagnostic that should be e...
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.
CXXCatchStmt - This represents a C++ catch block.
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)
Return true if a particular note should be downgraded to a compatibility warning in C++11 mode...
bool isPOD() const
Whether this class is a POD-type (C++ [class]p4)