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) {
238 if (D->hasAttr<BlocksAttr>())
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");
262 Loc = BDecl->getLocation();
263 Scopes.push_back(GotoScope(ParentScope,
264 Diags.first, Diags.second, Loc));
265 ParentScope = Scopes.size()-1;
273 void JumpScopeChecker::BuildScopeInformation(
Stmt *
S,
274 unsigned &origParentScope) {
278 unsigned independentParentScope = origParentScope;
279 unsigned &ParentScope = ((isa<Expr>(
S) && !isa<StmtExpr>(S))
280 ? origParentScope : independentParentScope);
282 unsigned StmtsToSkip = 0u;
285 switch (S->getStmtClass()) {
286 case Stmt::AddrLabelExprClass:
287 IndirectJumpTargets.push_back(cast<AddrLabelExpr>(S)->getLabel());
290 case Stmt::IndirectGotoStmtClass:
296 if (cast<IndirectGotoStmt>(S)->getConstantTarget()) {
297 LabelAndGotoScopes[
S] = ParentScope;
302 LabelAndGotoScopes[
S] = ParentScope;
303 IndirectJumps.push_back(cast<IndirectGotoStmt>(S));
306 case Stmt::SwitchStmtClass:
309 if (
Stmt *Init = cast<SwitchStmt>(S)->getInit()) {
310 BuildScopeInformation(Init, ParentScope);
313 if (
VarDecl *Var = cast<SwitchStmt>(S)->getConditionVariable()) {
314 BuildScopeInformation(Var, ParentScope);
319 case Stmt::GotoStmtClass:
322 LabelAndGotoScopes[
S] = ParentScope;
326 case Stmt::IfStmtClass: {
332 BuildScopeInformation(Var, ParentScope);
335 unsigned NewParentScope = Scopes.size();
336 Scopes.push_back(GotoScope(ParentScope,
337 diag::note_protected_by_constexpr_if, 0,
339 BuildScopeInformation(IS->
getCond(), NewParentScope);
342 NewParentScope = Scopes.size();
343 Scopes.push_back(GotoScope(ParentScope,
344 diag::note_protected_by_constexpr_if, 0,
346 BuildScopeInformation(IS->
getThen(), NewParentScope);
348 NewParentScope = Scopes.size();
349 Scopes.push_back(GotoScope(ParentScope,
350 diag::note_protected_by_constexpr_if, 0,
352 BuildScopeInformation(Else, NewParentScope);
357 case Stmt::CXXTryStmtClass: {
360 unsigned NewParentScope = Scopes.size();
361 Scopes.push_back(GotoScope(ParentScope,
362 diag::note_protected_by_cxx_try,
363 diag::note_exits_cxx_try,
364 TS->getSourceRange().getBegin()));
366 BuildScopeInformation(TryBlock, NewParentScope);
372 unsigned NewParentScope = Scopes.size();
373 Scopes.push_back(GotoScope(ParentScope,
374 diag::note_protected_by_cxx_catch,
375 diag::note_exits_cxx_catch,
376 CS->getSourceRange().getBegin()));
382 case Stmt::SEHTryStmtClass: {
385 unsigned NewParentScope = Scopes.size();
386 Scopes.push_back(GotoScope(ParentScope,
387 diag::note_protected_by_seh_try,
388 diag::note_exits_seh_try,
389 TS->getSourceRange().getBegin()));
391 BuildScopeInformation(TryBlock, NewParentScope);
396 unsigned NewParentScope = Scopes.size();
397 Scopes.push_back(GotoScope(ParentScope,
398 diag::note_protected_by_seh_except,
399 diag::note_exits_seh_except,
400 Except->getSourceRange().getBegin()));
401 BuildScopeInformation(Except->getBlock(), NewParentScope);
403 unsigned NewParentScope = Scopes.size();
404 Scopes.push_back(GotoScope(ParentScope,
405 diag::note_protected_by_seh_finally,
406 diag::note_exits_seh_finally,
407 Finally->getSourceRange().getBegin()));
408 BuildScopeInformation(Finally->getBlock(), NewParentScope);
414 case Stmt::DeclStmtClass: {
420 for (
auto *
I : DS->
decls())
421 BuildScopeInformation(
I, origParentScope);
425 case Stmt::ObjCAtTryStmtClass: {
431 unsigned NewParentScope = Scopes.size();
432 Scopes.push_back(GotoScope(ParentScope,
433 diag::note_protected_by_objc_try,
434 diag::note_exits_objc_try,
437 BuildScopeInformation(TryPart, NewParentScope);
443 unsigned NewParentScope = Scopes.size();
444 Scopes.push_back(GotoScope(ParentScope,
445 diag::note_protected_by_objc_catch,
446 diag::note_exits_objc_catch,
449 BuildScopeInformation(AC->
getCatchBody(), NewParentScope);
454 unsigned NewParentScope = Scopes.size();
455 Scopes.push_back(GotoScope(ParentScope,
456 diag::note_protected_by_objc_finally,
457 diag::note_exits_objc_finally,
458 AF->getAtFinallyLoc()));
459 BuildScopeInformation(AF, NewParentScope);
465 case Stmt::ObjCAtSynchronizedStmtClass: {
475 unsigned NewParentScope = Scopes.size();
476 Scopes.push_back(GotoScope(ParentScope,
477 diag::note_protected_by_objc_synchronized,
478 diag::note_exits_objc_synchronized,
480 BuildScopeInformation(AS->
getSynchBody(), NewParentScope);
484 case Stmt::ObjCAutoreleasePoolStmtClass: {
489 unsigned NewParentScope = Scopes.size();
490 Scopes.push_back(GotoScope(ParentScope,
491 diag::note_protected_by_objc_autoreleasepool,
492 diag::note_exits_objc_autoreleasepool,
494 BuildScopeInformation(AS->
getSubStmt(), NewParentScope);
498 case Stmt::ExprWithCleanupsClass: {
503 for (
unsigned i = 0, e = EWC->
getNumObjects(); i != e; ++i) {
505 for (
const auto &CI : BDecl->
captures()) {
506 VarDecl *variable = CI.getVariable();
507 BuildScopeInformation(variable, BDecl, origParentScope);
513 case Stmt::MaterializeTemporaryExprClass: {
520 const Expr *ExtendedObject =
522 CommaLHS, Adjustments);
524 Scopes.push_back(GotoScope(ParentScope, 0,
525 diag::note_exits_temporary_dtor,
527 origParentScope = Scopes.size()-1;
533 case Stmt::CaseStmtClass:
534 case Stmt::DefaultStmtClass:
535 case Stmt::LabelStmtClass:
536 LabelAndGotoScopes[
S] = ParentScope;
543 for (
Stmt *SubStmt : S->children()) {
556 if (
CaseStmt *CS = dyn_cast<CaseStmt>(SubStmt))
557 Next = CS->getSubStmt();
558 else if (
DefaultStmt *DS = dyn_cast<DefaultStmt>(SubStmt))
559 Next = DS->getSubStmt();
560 else if (
LabelStmt *LS = dyn_cast<LabelStmt>(SubStmt))
561 Next = LS->getSubStmt();
565 LabelAndGotoScopes[SubStmt] = ParentScope;
570 BuildScopeInformation(SubStmt, ParentScope);
576 void JumpScopeChecker::VerifyJumps() {
577 while (!Jumps.empty()) {
578 Stmt *Jump = Jumps.pop_back_val();
581 if (
GotoStmt *GS = dyn_cast<GotoStmt>(Jump)) {
583 if (GS->getLabel()->getStmt()) {
584 CheckJump(GS, GS->getLabel()->getStmt(), GS->getGotoLoc(),
585 diag::err_goto_into_protected_scope,
586 diag::ext_goto_into_protected_scope,
587 diag::warn_cxx98_compat_goto_into_protected_scope);
595 LabelDecl *Target = IGS->getConstantTarget();
596 CheckJump(IGS, Target->
getStmt(), IGS->getGotoLoc(),
597 diag::err_goto_into_protected_scope,
598 diag::ext_goto_into_protected_scope,
599 diag::warn_cxx98_compat_goto_into_protected_scope);
609 if (
CaseStmt *CS = dyn_cast<CaseStmt>(SC))
611 else if (
DefaultStmt *DS = dyn_cast<DefaultStmt>(SC))
614 Loc = SC->getLocStart();
615 CheckJump(SS, SC, Loc, diag::err_switch_into_protected_scope, 0,
616 diag::warn_cxx98_compat_switch_into_protected_scope);
640 void JumpScopeChecker::VerifyIndirectJumps() {
641 if (IndirectJumps.empty())
return;
645 if (IndirectJumpTargets.empty()) {
646 S.Diag(IndirectJumps[0]->getGotoLoc(),
647 diag::err_indirect_goto_without_addrlabel);
654 typedef std::pair<unsigned, IndirectGotoStmt*> JumpScope;
657 llvm::DenseMap<unsigned, IndirectGotoStmt*> JumpScopesMap;
659 I = IndirectJumps.begin(),
E = IndirectJumps.end();
I !=
E; ++
I) {
663 unsigned IGScope = LabelAndGotoScopes[IG];
665 if (!Entry) Entry = IG;
667 JumpScopes.reserve(JumpScopesMap.size());
669 I = JumpScopesMap.begin(),
E = JumpScopesMap.end();
I !=
E; ++
I)
670 JumpScopes.push_back(*
I);
676 llvm::DenseMap<unsigned, LabelDecl*> TargetScopes;
678 I = IndirectJumpTargets.begin(),
E = IndirectJumpTargets.end();
683 unsigned LabelScope = LabelAndGotoScopes[TheLabel->
getStmt()];
684 LabelDecl *&Target = TargetScopes[LabelScope];
685 if (!Target) Target = TheLabel;
696 llvm::BitVector Reachable(Scopes.size(),
false);
698 TI = TargetScopes.begin(), TE = TargetScopes.end(); TI != TE; ++TI) {
699 unsigned TargetScope = TI->first;
707 unsigned Min = TargetScope;
715 if (Scopes[Min].InDiag)
break;
717 Min = Scopes[Min].ParentScope;
723 I = JumpScopes.begin(),
E = JumpScopes.end();
I !=
E; ++
I) {
724 unsigned Scope =
I->first;
731 bool IsReachable =
false;
733 if (Reachable.test(Scope)) {
736 for (
unsigned S =
I->first; S != Scope; S = Scopes[S].ParentScope)
744 if (Scope == 0 || Scope < Min)
break;
747 if (Scopes[Scope].OutDiag)
break;
749 Scope = Scopes[Scope].ParentScope;
753 if (IsReachable)
continue;
755 DiagnoseIndirectJump(
I->second,
I->first, TargetLabel, TargetScope);
763 return (JumpDiag == diag::err_goto_into_protected_scope &&
764 (InDiagNote == diag::note_protected_by_variable_init ||
765 InDiagNote == diag::note_protected_by_variable_nontriv_destructor));
772 InDiagNote == diag::note_protected_by_variable_non_pod;
780 S.
Diag(Jump->
getGotoLoc(), diag::err_indirect_goto_in_protected_scope);
789 for (
unsigned I = 0,
E = ToScopes.size();
I !=
E; ++
I)
790 if (Scopes[ToScopes[
I]].InDiag)
791 S.Diag(Scopes[ToScopes[
I]].Loc, Scopes[ToScopes[
I]].InDiag);
798 unsigned TargetScope) {
802 unsigned Common = GetDeepestCommonScope(JumpScope, TargetScope);
803 bool Diagnosed =
false;
806 for (
unsigned I = JumpScope;
I != Common;
I = Scopes[
I].ParentScope)
807 if (Scopes[
I].OutDiag) {
809 S.Diag(Scopes[
I].Loc, Scopes[
I].OutDiag);
815 for (
unsigned I = TargetScope;
I != Common;
I = Scopes[
I].ParentScope)
817 ToScopesCXX98Compat.push_back(
I);
818 else if (Scopes[
I].InDiag) {
820 S.Diag(Scopes[
I].Loc, Scopes[
I].InDiag);
824 if (!Diagnosed && !ToScopesCXX98Compat.empty()) {
826 diag::warn_cxx98_compat_indirect_goto_in_protected_scope);
828 NoteJumpIntoScopes(ToScopesCXX98Compat);
835 unsigned JumpDiagError,
unsigned JumpDiagWarning,
836 unsigned JumpDiagCXX98Compat) {
842 unsigned FromScope = LabelAndGotoScopes[From];
843 unsigned ToScope = LabelAndGotoScopes[To];
846 if (FromScope == ToScope)
return;
849 if (isa<GotoStmt>(From) || isa<IndirectGotoStmt>(From)) {
852 for (
unsigned I = FromScope;
I > ToScope;
I = Scopes[
I].ParentScope) {
853 if (Scopes[
I].InDiag == diag::note_protected_by_seh_finally) {
854 S.Diag(From->getLocStart(), diag::warn_jump_out_of_seh_finally);
860 unsigned CommonScope = GetDeepestCommonScope(FromScope, ToScope);
863 if (CommonScope == ToScope)
return;
869 for (
unsigned I = ToScope;
I != CommonScope;
I = Scopes[
I].ParentScope) {
870 if (S.getLangOpts().MSVCCompat && JumpDiagWarning != 0 &&
872 ToScopesWarning.push_back(
I);
874 ToScopesCXX98Compat.push_back(
I);
875 else if (Scopes[
I].InDiag)
876 ToScopesError.push_back(
I);
880 if (!ToScopesWarning.empty()) {
881 S.Diag(DiagLoc, JumpDiagWarning);
882 NoteJumpIntoScopes(ToScopesWarning);
886 if (!ToScopesError.empty()) {
887 S.Diag(DiagLoc, JumpDiagError);
888 NoteJumpIntoScopes(ToScopesError);
892 if (ToScopesError.empty() && !ToScopesCXX98Compat.empty()) {
893 S.Diag(DiagLoc, JumpDiagCXX98Compat);
894 NoteJumpIntoScopes(ToScopesCXX98Compat);
898 void JumpScopeChecker::CheckGotoStmt(
GotoStmt *GS) {
900 S.Diag(GS->
getGotoLoc(), diag::err_goto_ms_asm_label)
902 S.Diag(GS->
getLabel()->getLocation(), diag::note_goto_ms_asm_label)
907 void Sema::DiagnoseInvalidJumps(
Stmt *Body) {
908 (void)JumpScopeChecker(Body, *
this);
const SwitchCase * getNextSwitchCase() const
const ObjCAtFinallyStmt * getFinallyStmt() const
Retrieve the @finally statement, if any.
A (possibly-)qualified type.
ArrayRef< Capture > captures() const
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)
IfStmt - This represents an if/then/else.
Expr * GetTemporaryExpr() const
Retrieve the temporary-generating subexpression whose value will be materialized into a glvalue...
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.
Represents a call to a C++ constructor.
const Stmt * getElse() const
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.
const CompoundStmt * getSynchBody() const
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
const ObjCAtCatchStmt * getCatchStmt(unsigned I) const
Retrieve a @catch statement.
CleanupObject getObject(unsigned i) const
detail::InMemoryDirectory::const_iterator I
Sema - This implements semantic analysis and AST building for C.
SourceLocation getLocStart() const LLVM_READONLY
unsigned getNumObjects() const
BlockDecl - This represents a block literal declaration, which is like an unnamed FunctionDecl...
Expr - This represents one expression.
StorageDuration getStorageDuration() const
Retrieve the storage duration for the materialized temporary.
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
SourceLocation getAtLoc() const
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 getAtSynchronizedLoc() const
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.
VarDecl * getConditionVariable() const
Retrieve the variable declared in this "if" statement, if any.
SourceLocation getLocStart() const LLVM_READONLY
const Expr * getSynchExpr() const
unsigned getNumHandlers() const
detail::InMemoryDirectory::const_iterator E
const Stmt * getThen() const
SwitchStmt - This represents a 'switch' stmt.
SourceLocation getAtCatchLoc() const
const Stmt * getSubStmt() const
SourceLocation getAtTryLoc() const
Retrieve the location of the @ in the @try.
bool isTrivial() const
Whether this function is "trivial" in some specialized C++ senses.
Represents Objective-C's @finally statement.
unsigned getNumCatchStmts() const
Retrieve the number of @catch statements in this try-catch-finally block.
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.
const Expr * getCond() const
CompoundStmt * getTryBlock()
Represents Objective-C's @try ... @catch ... @finally statement.
const Stmt * getTryBody() const
Retrieve the @try body.
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)