Francois Pichet | 2731b7d | 2011-09-09 11:02:57 +0000 | [diff] [blame] | 1 | //===--- JumpDiagnostics.cpp - Protected scope jump analysis ------*- C++ -*-=// |
Chris Lattner | 1a1fdbd | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the JumpScopeChecker class, which is used to diagnose |
Francois Pichet | 2731b7d | 2011-09-09 11:02:57 +0000 | [diff] [blame] | 11 | // jumps that enter a protected scope in an invalid way. |
Chris Lattner | 1a1fdbd | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
John McCall | 8302463 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 15 | #include "clang/Sema/SemaInternal.h" |
John McCall | 28a0cf7 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclCXX.h" |
Chris Lattner | 1a1fdbd | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 17 | #include "clang/AST/Expr.h" |
Douglas Gregor | 27d0c44 | 2011-05-27 16:05:29 +0000 | [diff] [blame] | 18 | #include "clang/AST/ExprCXX.h" |
Sebastian Redl | 4de47b4 | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 19 | #include "clang/AST/StmtCXX.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 20 | #include "clang/AST/StmtObjC.h" |
Douglas Gregor | c3a6ade | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/BitVector.h" |
Chris Lattner | 1a1fdbd | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 22 | using namespace clang; |
| 23 | |
| 24 | namespace { |
| 25 | |
| 26 | /// JumpScopeChecker - This object is used by Sema to diagnose invalid jumps |
| 27 | /// into VLA and other protected scopes. For example, this rejects: |
| 28 | /// goto L; |
| 29 | /// int a[n]; |
| 30 | /// L: |
| 31 | /// |
| 32 | class JumpScopeChecker { |
| 33 | Sema &S; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 34 | |
Alp Toker | e265cf1 | 2014-05-09 08:40:10 +0000 | [diff] [blame] | 35 | /// Permissive - True when recovering from errors, in which case precautions |
| 36 | /// are taken to handle incomplete scope information. |
| 37 | const bool Permissive; |
| 38 | |
Chris Lattner | 1a1fdbd | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 39 | /// GotoScope - This is a record that we use to keep track of all of the |
| 40 | /// scopes that are introduced by VLAs and other things that scope jumps like |
| 41 | /// gotos. This scope tree has nothing to do with the source scope tree, |
| 42 | /// because you can have multiple VLA scopes per compound statement, and most |
| 43 | /// compound statements don't introduce any scopes. |
| 44 | struct GotoScope { |
| 45 | /// ParentScope - The index in ScopeMap of the parent scope. This is 0 for |
| 46 | /// the parent scope is the function body. |
| 47 | unsigned ParentScope; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 48 | |
Richard Smith | fe2750d | 2011-10-20 21:42:12 +0000 | [diff] [blame] | 49 | /// InDiag - The note to emit if there is a jump into this scope. |
John McCall | cf819ab | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 50 | unsigned InDiag; |
| 51 | |
Richard Smith | fe2750d | 2011-10-20 21:42:12 +0000 | [diff] [blame] | 52 | /// OutDiag - The note to emit if there is an indirect jump out |
John McCall | cf819ab | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 53 | /// of this scope. Direct jumps always clean up their current scope |
| 54 | /// in an orderly way. |
| 55 | unsigned OutDiag; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 56 | |
Chris Lattner | 1a1fdbd | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 57 | /// Loc - Location to emit the diagnostic. |
| 58 | SourceLocation Loc; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 59 | |
John McCall | cf819ab | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 60 | GotoScope(unsigned parentScope, unsigned InDiag, unsigned OutDiag, |
| 61 | SourceLocation L) |
| 62 | : ParentScope(parentScope), InDiag(InDiag), OutDiag(OutDiag), Loc(L) {} |
Chris Lattner | 1a1fdbd | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 63 | }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 64 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 65 | SmallVector<GotoScope, 48> Scopes; |
Chris Lattner | 1a1fdbd | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 66 | llvm::DenseMap<Stmt*, unsigned> LabelAndGotoScopes; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 67 | SmallVector<Stmt*, 16> Jumps; |
John McCall | cf819ab | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 68 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 69 | SmallVector<IndirectGotoStmt*, 4> IndirectJumps; |
| 70 | SmallVector<LabelDecl*, 4> IndirectJumpTargets; |
Chris Lattner | 1a1fdbd | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 71 | public: |
| 72 | JumpScopeChecker(Stmt *Body, Sema &S); |
| 73 | private: |
Douglas Gregor | 27b98ea | 2010-06-21 23:44:13 +0000 | [diff] [blame] | 74 | void BuildScopeInformation(Decl *D, unsigned &ParentScope); |
Hubert Tong | 64c2f5a | 2015-06-04 22:53:21 +0000 | [diff] [blame] | 75 | void BuildScopeInformation(VarDecl *D, const BlockDecl *BDecl, |
Fariborz Jahanian | 256d39d | 2011-07-11 18:04:54 +0000 | [diff] [blame] | 76 | unsigned &ParentScope); |
| 77 | void BuildScopeInformation(Stmt *S, unsigned &origParentScope); |
Hubert Tong | 64c2f5a | 2015-06-04 22:53:21 +0000 | [diff] [blame] | 78 | |
Chris Lattner | 1a1fdbd | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 79 | void VerifyJumps(); |
John McCall | cf819ab | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 80 | void VerifyIndirectJumps(); |
Bill Wendling | 8ac06af | 2012-02-22 09:38:11 +0000 | [diff] [blame] | 81 | void NoteJumpIntoScopes(ArrayRef<unsigned> ToScopes); |
John McCall | cf819ab | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 82 | void DiagnoseIndirectJump(IndirectGotoStmt *IG, unsigned IGScope, |
Chris Lattner | c8e630e | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 83 | LabelDecl *Target, unsigned TargetScope); |
Francois Pichet | 051f5e5 | 2011-09-13 10:26:51 +0000 | [diff] [blame] | 84 | void CheckJump(Stmt *From, Stmt *To, SourceLocation DiagLoc, |
Richard Smith | fe2750d | 2011-10-20 21:42:12 +0000 | [diff] [blame] | 85 | unsigned JumpDiag, unsigned JumpDiagWarning, |
| 86 | unsigned JumpDiagCXX98Compat); |
Ehsan Akhgari | 3109758 | 2014-09-22 02:21:54 +0000 | [diff] [blame] | 87 | void CheckGotoStmt(GotoStmt *GS); |
John McCall | 42f9f1f | 2010-05-12 02:37:54 +0000 | [diff] [blame] | 88 | |
| 89 | unsigned GetDeepestCommonScope(unsigned A, unsigned B); |
Chris Lattner | 1a1fdbd | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 90 | }; |
| 91 | } // end anonymous namespace |
| 92 | |
Alp Toker | e265cf1 | 2014-05-09 08:40:10 +0000 | [diff] [blame] | 93 | #define CHECK_PERMISSIVE(x) (assert(Permissive || !(x)), (Permissive && (x))) |
Chris Lattner | 1a1fdbd | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 94 | |
Alp Toker | e265cf1 | 2014-05-09 08:40:10 +0000 | [diff] [blame] | 95 | JumpScopeChecker::JumpScopeChecker(Stmt *Body, Sema &s) |
| 96 | : S(s), Permissive(s.hasAnyUnrecoverableErrorsInThisFunction()) { |
Chris Lattner | 1a1fdbd | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 97 | // Add a scope entry for function scope. |
John McCall | cf819ab | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 98 | Scopes.push_back(GotoScope(~0U, ~0U, ~0U, SourceLocation())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 99 | |
Chris Lattner | 1a1fdbd | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 100 | // Build information for the top level compound statement, so that we have a |
| 101 | // defined scope record for every "goto" and label. |
Fariborz Jahanian | 256d39d | 2011-07-11 18:04:54 +0000 | [diff] [blame] | 102 | unsigned BodyParentScope = 0; |
| 103 | BuildScopeInformation(Body, BodyParentScope); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 104 | |
Chris Lattner | 1a1fdbd | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 105 | // Check that all jumps we saw are kosher. |
| 106 | VerifyJumps(); |
John McCall | cf819ab | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 107 | VerifyIndirectJumps(); |
Chris Lattner | 1a1fdbd | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 108 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 109 | |
John McCall | 42f9f1f | 2010-05-12 02:37:54 +0000 | [diff] [blame] | 110 | /// GetDeepestCommonScope - Finds the innermost scope enclosing the |
| 111 | /// two scopes. |
| 112 | unsigned JumpScopeChecker::GetDeepestCommonScope(unsigned A, unsigned B) { |
| 113 | while (A != B) { |
| 114 | // Inner scopes are created after outer scopes and therefore have |
| 115 | // higher indices. |
| 116 | if (A < B) { |
| 117 | assert(Scopes[B].ParentScope < B); |
| 118 | B = Scopes[B].ParentScope; |
| 119 | } else { |
| 120 | assert(Scopes[A].ParentScope < A); |
| 121 | A = Scopes[A].ParentScope; |
| 122 | } |
| 123 | } |
| 124 | return A; |
| 125 | } |
| 126 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 127 | typedef std::pair<unsigned,unsigned> ScopePair; |
| 128 | |
Chris Lattner | 1a1fdbd | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 129 | /// GetDiagForGotoScopeDecl - If this decl induces a new goto scope, return a |
| 130 | /// diagnostic that should be emitted if control goes over it. If not, return 0. |
Richard Smith | c934e4f | 2013-12-12 01:27:02 +0000 | [diff] [blame] | 131 | static ScopePair GetDiagForGotoScopeDecl(Sema &S, const Decl *D) { |
Chris Lattner | 1a1fdbd | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 132 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Rafael Espindola | 44938a7 | 2012-10-28 02:44:03 +0000 | [diff] [blame] | 133 | unsigned InDiag = 0; |
Richard Smith | c934e4f | 2013-12-12 01:27:02 +0000 | [diff] [blame] | 134 | unsigned OutDiag = 0; |
| 135 | |
Chris Lattner | 1a1fdbd | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 136 | if (VD->getType()->isVariablyModifiedType()) |
John McCall | cf819ab | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 137 | InDiag = diag::note_protected_by_vla; |
| 138 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 139 | if (VD->hasAttr<BlocksAttr>()) |
| 140 | return ScopePair(diag::note_protected_by___block, |
| 141 | diag::note_exits___block); |
| 142 | |
| 143 | if (VD->hasAttr<CleanupAttr>()) |
| 144 | return ScopePair(diag::note_protected_by_cleanup, |
| 145 | diag::note_exits_cleanup); |
| 146 | |
Richard Smith | c934e4f | 2013-12-12 01:27:02 +0000 | [diff] [blame] | 147 | if (VD->hasLocalStorage()) { |
| 148 | switch (VD->getType().isDestructedType()) { |
| 149 | case QualType::DK_objc_strong_lifetime: |
John McCall | 039f2bb | 2015-10-21 18:06:38 +0000 | [diff] [blame] | 150 | return ScopePair(diag::note_protected_by_objc_strong_init, |
| 151 | diag::note_exits_objc_strong); |
| 152 | |
Richard Smith | c934e4f | 2013-12-12 01:27:02 +0000 | [diff] [blame] | 153 | case QualType::DK_objc_weak_lifetime: |
John McCall | 039f2bb | 2015-10-21 18:06:38 +0000 | [diff] [blame] | 154 | return ScopePair(diag::note_protected_by_objc_weak_init, |
| 155 | diag::note_exits_objc_weak); |
Richard Smith | c934e4f | 2013-12-12 01:27:02 +0000 | [diff] [blame] | 156 | |
| 157 | case QualType::DK_cxx_destructor: |
| 158 | OutDiag = diag::note_exits_dtor; |
| 159 | break; |
| 160 | |
| 161 | case QualType::DK_none: |
| 162 | break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
Richard Smith | c934e4f | 2013-12-12 01:27:02 +0000 | [diff] [blame] | 166 | const Expr *Init = VD->getInit(); |
| 167 | if (S.Context.getLangOpts().CPlusPlus && VD->hasLocalStorage() && Init) { |
Richard Smith | fe2750d | 2011-10-20 21:42:12 +0000 | [diff] [blame] | 168 | // C++11 [stmt.dcl]p3: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 169 | // A program that jumps from a point where a variable with automatic |
| 170 | // storage duration is not in scope to a point where it is in scope |
| 171 | // is ill-formed unless the variable has scalar type, class type with |
Hubert Tong | 64c2f5a | 2015-06-04 22:53:21 +0000 | [diff] [blame] | 172 | // a trivial default constructor and a trivial destructor, a |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 173 | // cv-qualified version of one of these types, or an array of one of |
| 174 | // the preceding types and is declared without an initializer. |
| 175 | |
| 176 | // C++03 [stmt.dcl.p3: |
| 177 | // A program that jumps from a point where a local variable |
| 178 | // with automatic storage duration is not in scope to a point |
| 179 | // where it is in scope is ill-formed unless the variable has |
| 180 | // POD type and is declared without an initializer. |
| 181 | |
Richard Smith | c934e4f | 2013-12-12 01:27:02 +0000 | [diff] [blame] | 182 | InDiag = diag::note_protected_by_variable_init; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 183 | |
Richard Smith | c934e4f | 2013-12-12 01:27:02 +0000 | [diff] [blame] | 184 | // For a variable of (array of) class type declared without an |
| 185 | // initializer, we will have call-style initialization and the initializer |
| 186 | // will be the CXXConstructExpr with no intervening nodes. |
| 187 | if (const CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(Init)) { |
| 188 | const CXXConstructorDecl *Ctor = CCE->getConstructor(); |
| 189 | if (Ctor->isTrivial() && Ctor->isDefaultConstructor() && |
| 190 | VD->getInitStyle() == VarDecl::CallInit) { |
Rafael Espindola | 44938a7 | 2012-10-28 02:44:03 +0000 | [diff] [blame] | 191 | if (OutDiag) |
| 192 | InDiag = diag::note_protected_by_variable_nontriv_destructor; |
Richard Smith | c934e4f | 2013-12-12 01:27:02 +0000 | [diff] [blame] | 193 | else if (!Ctor->getParent()->isPOD()) |
Rafael Espindola | 44938a7 | 2012-10-28 02:44:03 +0000 | [diff] [blame] | 194 | InDiag = diag::note_protected_by_variable_non_pod; |
Richard Smith | c934e4f | 2013-12-12 01:27:02 +0000 | [diff] [blame] | 195 | else |
| 196 | InDiag = 0; |
Douglas Gregor | 27d0c44 | 2011-05-27 16:05:29 +0000 | [diff] [blame] | 197 | } |
Douglas Gregor | 5a5fcd8 | 2010-07-01 00:21:21 +0000 | [diff] [blame] | 198 | } |
John McCall | cf819ab | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 199 | } |
Rafael Espindola | 44938a7 | 2012-10-28 02:44:03 +0000 | [diff] [blame] | 200 | |
Richard Smith | c934e4f | 2013-12-12 01:27:02 +0000 | [diff] [blame] | 201 | return ScopePair(InDiag, OutDiag); |
Chris Lattner | 1a1fdbd | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 202 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 203 | |
Richard Smith | c934e4f | 2013-12-12 01:27:02 +0000 | [diff] [blame] | 204 | if (const TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
John McCall | cf819ab | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 205 | if (TD->getUnderlyingType()->isVariablyModifiedType()) |
Richard Smith | c934e4f | 2013-12-12 01:27:02 +0000 | [diff] [blame] | 206 | return ScopePair(isa<TypedefDecl>(TD) |
| 207 | ? diag::note_protected_by_vla_typedef |
| 208 | : diag::note_protected_by_vla_type_alias, |
| 209 | 0); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 210 | } |
| 211 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 212 | return ScopePair(0U, 0U); |
Chris Lattner | 1a1fdbd | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Douglas Gregor | 27b98ea | 2010-06-21 23:44:13 +0000 | [diff] [blame] | 215 | /// \brief Build scope information for a declaration that is part of a DeclStmt. |
| 216 | void JumpScopeChecker::BuildScopeInformation(Decl *D, unsigned &ParentScope) { |
Douglas Gregor | 27b98ea | 2010-06-21 23:44:13 +0000 | [diff] [blame] | 217 | // If this decl causes a new scope, push and switch to it. |
Richard Smith | c934e4f | 2013-12-12 01:27:02 +0000 | [diff] [blame] | 218 | std::pair<unsigned,unsigned> Diags = GetDiagForGotoScopeDecl(S, D); |
Douglas Gregor | 27b98ea | 2010-06-21 23:44:13 +0000 | [diff] [blame] | 219 | if (Diags.first || Diags.second) { |
| 220 | Scopes.push_back(GotoScope(ParentScope, Diags.first, Diags.second, |
| 221 | D->getLocation())); |
| 222 | ParentScope = Scopes.size()-1; |
| 223 | } |
Hubert Tong | 64c2f5a | 2015-06-04 22:53:21 +0000 | [diff] [blame] | 224 | |
Douglas Gregor | 27b98ea | 2010-06-21 23:44:13 +0000 | [diff] [blame] | 225 | // If the decl has an initializer, walk it with the potentially new |
| 226 | // scope we just installed. |
| 227 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 228 | if (Expr *Init = VD->getInit()) |
| 229 | BuildScopeInformation(Init, ParentScope); |
| 230 | } |
Chris Lattner | 1a1fdbd | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 231 | |
Fariborz Jahanian | 256d39d | 2011-07-11 18:04:54 +0000 | [diff] [blame] | 232 | /// \brief Build scope information for a captured block literal variables. |
Hubert Tong | 64c2f5a | 2015-06-04 22:53:21 +0000 | [diff] [blame] | 233 | void JumpScopeChecker::BuildScopeInformation(VarDecl *D, |
| 234 | const BlockDecl *BDecl, |
Fariborz Jahanian | 256d39d | 2011-07-11 18:04:54 +0000 | [diff] [blame] | 235 | unsigned &ParentScope) { |
| 236 | // exclude captured __block variables; there's no destructor |
| 237 | // associated with the block literal for them. |
| 238 | if (D->hasAttr<BlocksAttr>()) |
| 239 | return; |
| 240 | QualType T = D->getType(); |
| 241 | QualType::DestructionKind destructKind = T.isDestructedType(); |
| 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); |
| 248 | break; |
| 249 | case QualType::DK_objc_strong_lifetime: |
| 250 | Diags = ScopePair(diag::note_enters_block_captures_strong, |
| 251 | diag::note_exits_block_captures_strong); |
| 252 | break; |
| 253 | case QualType::DK_objc_weak_lifetime: |
| 254 | Diags = ScopePair(diag::note_enters_block_captures_weak, |
| 255 | diag::note_exits_block_captures_weak); |
| 256 | break; |
| 257 | case QualType::DK_none: |
Richard Smith | fe2750d | 2011-10-20 21:42:12 +0000 | [diff] [blame] | 258 | llvm_unreachable("non-lifetime captured variable"); |
Fariborz Jahanian | 256d39d | 2011-07-11 18:04:54 +0000 | [diff] [blame] | 259 | } |
| 260 | SourceLocation Loc = D->getLocation(); |
| 261 | if (Loc.isInvalid()) |
| 262 | Loc = BDecl->getLocation(); |
Hubert Tong | 64c2f5a | 2015-06-04 22:53:21 +0000 | [diff] [blame] | 263 | Scopes.push_back(GotoScope(ParentScope, |
Fariborz Jahanian | 256d39d | 2011-07-11 18:04:54 +0000 | [diff] [blame] | 264 | Diags.first, Diags.second, Loc)); |
| 265 | ParentScope = Scopes.size()-1; |
| 266 | } |
| 267 | } |
| 268 | |
Chris Lattner | 1a1fdbd | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 269 | /// BuildScopeInformation - The statements from CI to CE are known to form a |
| 270 | /// coherent VLA scope with a specified parent node. Walk through the |
| 271 | /// statements, adding any labels or gotos to LabelAndGotoScopes and recursively |
| 272 | /// walking the AST as needed. |
Richard Smith | 8b65f10 | 2016-06-21 20:10:11 +0000 | [diff] [blame] | 273 | void JumpScopeChecker::BuildScopeInformation(Stmt *S, |
| 274 | unsigned &origParentScope) { |
Fariborz Jahanian | 256d39d | 2011-07-11 18:04:54 +0000 | [diff] [blame] | 275 | // If this is a statement, rather than an expression, scopes within it don't |
| 276 | // propagate out into the enclosing scope. Otherwise we have to worry |
| 277 | // about block literals, which have the lifetime of their enclosing statement. |
| 278 | unsigned independentParentScope = origParentScope; |
Hubert Tong | 64c2f5a | 2015-06-04 22:53:21 +0000 | [diff] [blame] | 279 | unsigned &ParentScope = ((isa<Expr>(S) && !isa<StmtExpr>(S)) |
Fariborz Jahanian | 256d39d | 2011-07-11 18:04:54 +0000 | [diff] [blame] | 280 | ? origParentScope : independentParentScope); |
| 281 | |
Richard Smith | a547eb2 | 2016-07-14 00:11:03 +0000 | [diff] [blame^] | 282 | unsigned StmtsToSkip = 0u; |
Hubert Tong | 64c2f5a | 2015-06-04 22:53:21 +0000 | [diff] [blame] | 283 | |
Chris Lattner | 1a1fdbd | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 284 | // If we found a label, remember that it is in ParentScope scope. |
John McCall | cf819ab | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 285 | switch (S->getStmtClass()) { |
John McCall | cf819ab | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 286 | case Stmt::AddrLabelExprClass: |
| 287 | IndirectJumpTargets.push_back(cast<AddrLabelExpr>(S)->getLabel()); |
| 288 | break; |
| 289 | |
| 290 | case Stmt::IndirectGotoStmtClass: |
John McCall | 9de9160 | 2010-10-28 08:53:48 +0000 | [diff] [blame] | 291 | // "goto *&&lbl;" is a special case which we treat as equivalent |
| 292 | // to a normal goto. In addition, we don't calculate scope in the |
| 293 | // operand (to avoid recording the address-of-label use), which |
| 294 | // works only because of the restricted set of expressions which |
| 295 | // we detect as constant targets. |
| 296 | if (cast<IndirectGotoStmt>(S)->getConstantTarget()) { |
| 297 | LabelAndGotoScopes[S] = ParentScope; |
| 298 | Jumps.push_back(S); |
| 299 | return; |
| 300 | } |
| 301 | |
John McCall | cf819ab | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 302 | LabelAndGotoScopes[S] = ParentScope; |
| 303 | IndirectJumps.push_back(cast<IndirectGotoStmt>(S)); |
| 304 | break; |
| 305 | |
John McCall | cf819ab | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 306 | case Stmt::SwitchStmtClass: |
Richard Smith | a547eb2 | 2016-07-14 00:11:03 +0000 | [diff] [blame^] | 307 | // Evaluate the C++17 init stmt and condition variable |
| 308 | // before entering the scope of the switch statement. |
| 309 | if (Stmt *Init = cast<SwitchStmt>(S)->getInit()) { |
| 310 | BuildScopeInformation(Init, ParentScope); |
| 311 | ++StmtsToSkip; |
| 312 | } |
Douglas Gregor | 27b98ea | 2010-06-21 23:44:13 +0000 | [diff] [blame] | 313 | if (VarDecl *Var = cast<SwitchStmt>(S)->getConditionVariable()) { |
| 314 | BuildScopeInformation(Var, ParentScope); |
Richard Smith | a547eb2 | 2016-07-14 00:11:03 +0000 | [diff] [blame^] | 315 | ++StmtsToSkip; |
Douglas Gregor | 27b98ea | 2010-06-21 23:44:13 +0000 | [diff] [blame] | 316 | } |
| 317 | // Fall through |
Hubert Tong | 64c2f5a | 2015-06-04 22:53:21 +0000 | [diff] [blame] | 318 | |
Douglas Gregor | 27b98ea | 2010-06-21 23:44:13 +0000 | [diff] [blame] | 319 | case Stmt::GotoStmtClass: |
Chris Lattner | 1a1fdbd | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 320 | // Remember both what scope a goto is in as well as the fact that we have |
| 321 | // it. This makes the second scan not have to walk the AST again. |
| 322 | LabelAndGotoScopes[S] = ParentScope; |
| 323 | Jumps.push_back(S); |
John McCall | cf819ab | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 324 | break; |
| 325 | |
Richard Smith | b130fe7 | 2016-06-23 19:16:49 +0000 | [diff] [blame] | 326 | case Stmt::IfStmtClass: { |
| 327 | IfStmt *IS = cast<IfStmt>(S); |
| 328 | if (!IS->isConstexpr()) |
| 329 | break; |
| 330 | |
| 331 | if (VarDecl *Var = IS->getConditionVariable()) |
| 332 | BuildScopeInformation(Var, ParentScope); |
| 333 | |
| 334 | // Cannot jump into the middle of the condition. |
| 335 | unsigned NewParentScope = Scopes.size(); |
| 336 | Scopes.push_back(GotoScope(ParentScope, |
| 337 | diag::note_protected_by_constexpr_if, 0, |
| 338 | IS->getLocStart())); |
| 339 | BuildScopeInformation(IS->getCond(), NewParentScope); |
| 340 | |
| 341 | // Jumps into either arm of an 'if constexpr' are not allowed. |
| 342 | NewParentScope = Scopes.size(); |
| 343 | Scopes.push_back(GotoScope(ParentScope, |
| 344 | diag::note_protected_by_constexpr_if, 0, |
| 345 | IS->getLocStart())); |
| 346 | BuildScopeInformation(IS->getThen(), NewParentScope); |
| 347 | if (Stmt *Else = IS->getElse()) { |
| 348 | NewParentScope = Scopes.size(); |
| 349 | Scopes.push_back(GotoScope(ParentScope, |
| 350 | diag::note_protected_by_constexpr_if, 0, |
| 351 | IS->getLocStart())); |
| 352 | BuildScopeInformation(Else, NewParentScope); |
| 353 | } |
| 354 | return; |
| 355 | } |
| 356 | |
Eli Friedman | 1e95d4b | 2012-10-31 23:55:28 +0000 | [diff] [blame] | 357 | case Stmt::CXXTryStmtClass: { |
| 358 | CXXTryStmt *TS = cast<CXXTryStmt>(S); |
Richard Smith | 8b65f10 | 2016-06-21 20:10:11 +0000 | [diff] [blame] | 359 | { |
| 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())); |
| 365 | if (Stmt *TryBlock = TS->getTryBlock()) |
| 366 | BuildScopeInformation(TryBlock, NewParentScope); |
| 367 | } |
Eli Friedman | 1e95d4b | 2012-10-31 23:55:28 +0000 | [diff] [blame] | 368 | |
| 369 | // Jump from the catch into the try is not allowed either. |
| 370 | for (unsigned I = 0, E = TS->getNumHandlers(); I != E; ++I) { |
| 371 | CXXCatchStmt *CS = TS->getHandler(I); |
Richard Smith | 8b65f10 | 2016-06-21 20:10:11 +0000 | [diff] [blame] | 372 | unsigned NewParentScope = Scopes.size(); |
Eli Friedman | 1e95d4b | 2012-10-31 23:55:28 +0000 | [diff] [blame] | 373 | Scopes.push_back(GotoScope(ParentScope, |
| 374 | diag::note_protected_by_cxx_catch, |
| 375 | diag::note_exits_cxx_catch, |
| 376 | CS->getSourceRange().getBegin())); |
Richard Smith | 8b65f10 | 2016-06-21 20:10:11 +0000 | [diff] [blame] | 377 | BuildScopeInformation(CS->getHandlerBlock(), NewParentScope); |
Eli Friedman | 1e95d4b | 2012-10-31 23:55:28 +0000 | [diff] [blame] | 378 | } |
| 379 | return; |
| 380 | } |
| 381 | |
Nico Weber | b14f872 | 2015-02-03 17:06:08 +0000 | [diff] [blame] | 382 | case Stmt::SEHTryStmtClass: { |
| 383 | SEHTryStmt *TS = cast<SEHTryStmt>(S); |
Richard Smith | 8b65f10 | 2016-06-21 20:10:11 +0000 | [diff] [blame] | 384 | { |
| 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())); |
| 390 | if (Stmt *TryBlock = TS->getTryBlock()) |
| 391 | BuildScopeInformation(TryBlock, NewParentScope); |
| 392 | } |
Nico Weber | b14f872 | 2015-02-03 17:06:08 +0000 | [diff] [blame] | 393 | |
| 394 | // Jump from __except or __finally into the __try are not allowed either. |
| 395 | if (SEHExceptStmt *Except = TS->getExceptHandler()) { |
Richard Smith | 8b65f10 | 2016-06-21 20:10:11 +0000 | [diff] [blame] | 396 | unsigned NewParentScope = Scopes.size(); |
Nico Weber | b14f872 | 2015-02-03 17:06:08 +0000 | [diff] [blame] | 397 | Scopes.push_back(GotoScope(ParentScope, |
| 398 | diag::note_protected_by_seh_except, |
| 399 | diag::note_exits_seh_except, |
| 400 | Except->getSourceRange().getBegin())); |
Richard Smith | 8b65f10 | 2016-06-21 20:10:11 +0000 | [diff] [blame] | 401 | BuildScopeInformation(Except->getBlock(), NewParentScope); |
Nico Weber | b14f872 | 2015-02-03 17:06:08 +0000 | [diff] [blame] | 402 | } else if (SEHFinallyStmt *Finally = TS->getFinallyHandler()) { |
Richard Smith | 8b65f10 | 2016-06-21 20:10:11 +0000 | [diff] [blame] | 403 | unsigned NewParentScope = Scopes.size(); |
Nico Weber | b14f872 | 2015-02-03 17:06:08 +0000 | [diff] [blame] | 404 | Scopes.push_back(GotoScope(ParentScope, |
| 405 | diag::note_protected_by_seh_finally, |
| 406 | diag::note_exits_seh_finally, |
| 407 | Finally->getSourceRange().getBegin())); |
Richard Smith | 8b65f10 | 2016-06-21 20:10:11 +0000 | [diff] [blame] | 408 | BuildScopeInformation(Finally->getBlock(), NewParentScope); |
Nico Weber | b14f872 | 2015-02-03 17:06:08 +0000 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | return; |
| 412 | } |
Reid Kleckner | 1d59f99 | 2015-01-22 01:36:17 +0000 | [diff] [blame] | 413 | |
Richard Smith | 8b65f10 | 2016-06-21 20:10:11 +0000 | [diff] [blame] | 414 | case Stmt::DeclStmtClass: { |
| 415 | // If this is a declstmt with a VLA definition, it defines a scope from here |
| 416 | // to the end of the containing context. |
| 417 | DeclStmt *DS = cast<DeclStmt>(S); |
| 418 | // The decl statement creates a scope if any of the decls in it are VLAs |
| 419 | // or have the cleanup attribute. |
| 420 | for (auto *I : DS->decls()) |
| 421 | BuildScopeInformation(I, origParentScope); |
| 422 | return; |
| 423 | } |
| 424 | |
| 425 | case Stmt::ObjCAtTryStmtClass: { |
| 426 | // Disallow jumps into any part of an @try statement by pushing a scope and |
| 427 | // walking all sub-stmts in that scope. |
| 428 | ObjCAtTryStmt *AT = cast<ObjCAtTryStmt>(S); |
| 429 | // Recursively walk the AST for the @try part. |
| 430 | { |
| 431 | unsigned NewParentScope = Scopes.size(); |
| 432 | Scopes.push_back(GotoScope(ParentScope, |
| 433 | diag::note_protected_by_objc_try, |
| 434 | diag::note_exits_objc_try, |
| 435 | AT->getAtTryLoc())); |
| 436 | if (Stmt *TryPart = AT->getTryBody()) |
| 437 | BuildScopeInformation(TryPart, NewParentScope); |
| 438 | } |
| 439 | |
| 440 | // Jump from the catch to the finally or try is not valid. |
| 441 | for (unsigned I = 0, N = AT->getNumCatchStmts(); I != N; ++I) { |
| 442 | ObjCAtCatchStmt *AC = AT->getCatchStmt(I); |
| 443 | unsigned NewParentScope = Scopes.size(); |
| 444 | Scopes.push_back(GotoScope(ParentScope, |
| 445 | diag::note_protected_by_objc_catch, |
| 446 | diag::note_exits_objc_catch, |
| 447 | AC->getAtCatchLoc())); |
| 448 | // @catches are nested and it isn't |
| 449 | BuildScopeInformation(AC->getCatchBody(), NewParentScope); |
| 450 | } |
| 451 | |
| 452 | // Jump from the finally to the try or catch is not valid. |
| 453 | if (ObjCAtFinallyStmt *AF = AT->getFinallyStmt()) { |
| 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); |
| 460 | } |
| 461 | |
| 462 | return; |
| 463 | } |
| 464 | |
| 465 | case Stmt::ObjCAtSynchronizedStmtClass: { |
| 466 | // Disallow jumps into the protected statement of an @synchronized, but |
| 467 | // allow jumps into the object expression it protects. |
| 468 | ObjCAtSynchronizedStmt *AS = cast<ObjCAtSynchronizedStmt>(S); |
| 469 | // Recursively walk the AST for the @synchronized object expr, it is |
| 470 | // evaluated in the normal scope. |
| 471 | BuildScopeInformation(AS->getSynchExpr(), ParentScope); |
| 472 | |
| 473 | // Recursively walk the AST for the @synchronized part, protected by a new |
| 474 | // scope. |
| 475 | unsigned NewParentScope = Scopes.size(); |
| 476 | Scopes.push_back(GotoScope(ParentScope, |
| 477 | diag::note_protected_by_objc_synchronized, |
| 478 | diag::note_exits_objc_synchronized, |
| 479 | AS->getAtSynchronizedLoc())); |
| 480 | BuildScopeInformation(AS->getSynchBody(), NewParentScope); |
| 481 | return; |
| 482 | } |
| 483 | |
| 484 | case Stmt::ObjCAutoreleasePoolStmtClass: { |
| 485 | // Disallow jumps into the protected statement of an @autoreleasepool. |
| 486 | ObjCAutoreleasePoolStmt *AS = cast<ObjCAutoreleasePoolStmt>(S); |
| 487 | // Recursively walk the AST for the @autoreleasepool part, protected by a |
| 488 | // new scope. |
| 489 | unsigned NewParentScope = Scopes.size(); |
| 490 | Scopes.push_back(GotoScope(ParentScope, |
| 491 | diag::note_protected_by_objc_autoreleasepool, |
| 492 | diag::note_exits_objc_autoreleasepool, |
| 493 | AS->getAtLoc())); |
| 494 | BuildScopeInformation(AS->getSubStmt(), NewParentScope); |
| 495 | return; |
| 496 | } |
| 497 | |
| 498 | case Stmt::ExprWithCleanupsClass: { |
| 499 | // Disallow jumps past full-expressions that use blocks with |
| 500 | // non-trivial cleanups of their captures. This is theoretically |
| 501 | // implementable but a lot of work which we haven't felt up to doing. |
| 502 | ExprWithCleanups *EWC = cast<ExprWithCleanups>(S); |
| 503 | for (unsigned i = 0, e = EWC->getNumObjects(); i != e; ++i) { |
| 504 | const BlockDecl *BDecl = EWC->getObject(i); |
| 505 | for (const auto &CI : BDecl->captures()) { |
| 506 | VarDecl *variable = CI.getVariable(); |
| 507 | BuildScopeInformation(variable, BDecl, origParentScope); |
| 508 | } |
| 509 | } |
| 510 | break; |
| 511 | } |
| 512 | |
| 513 | case Stmt::MaterializeTemporaryExprClass: { |
| 514 | // Disallow jumps out of scopes containing temporaries lifetime-extended to |
| 515 | // automatic storage duration. |
| 516 | MaterializeTemporaryExpr *MTE = cast<MaterializeTemporaryExpr>(S); |
| 517 | if (MTE->getStorageDuration() == SD_Automatic) { |
| 518 | SmallVector<const Expr *, 4> CommaLHS; |
| 519 | SmallVector<SubobjectAdjustment, 4> Adjustments; |
| 520 | const Expr *ExtendedObject = |
| 521 | MTE->GetTemporaryExpr()->skipRValueSubobjectAdjustments( |
| 522 | CommaLHS, Adjustments); |
| 523 | if (ExtendedObject->getType().isDestructedType()) { |
| 524 | Scopes.push_back(GotoScope(ParentScope, 0, |
| 525 | diag::note_exits_temporary_dtor, |
| 526 | ExtendedObject->getExprLoc())); |
| 527 | origParentScope = Scopes.size()-1; |
| 528 | } |
| 529 | } |
| 530 | break; |
| 531 | } |
| 532 | |
| 533 | case Stmt::CaseStmtClass: |
| 534 | case Stmt::DefaultStmtClass: |
| 535 | case Stmt::LabelStmtClass: |
| 536 | LabelAndGotoScopes[S] = ParentScope; |
| 537 | break; |
| 538 | |
John McCall | cf819ab | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 539 | default: |
| 540 | break; |
Chris Lattner | 1a1fdbd | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 541 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 542 | |
Benjamin Kramer | 642f173 | 2015-07-02 21:03:14 +0000 | [diff] [blame] | 543 | for (Stmt *SubStmt : S->children()) { |
Richard Smith | a547eb2 | 2016-07-14 00:11:03 +0000 | [diff] [blame^] | 544 | if (!SubStmt) |
| 545 | continue; |
| 546 | if (StmtsToSkip) { |
| 547 | --StmtsToSkip; |
Douglas Gregor | 27b98ea | 2010-06-21 23:44:13 +0000 | [diff] [blame] | 548 | continue; |
| 549 | } |
Hubert Tong | 64c2f5a | 2015-06-04 22:53:21 +0000 | [diff] [blame] | 550 | |
John McCall | 4a33fa9 | 2010-08-02 23:33:14 +0000 | [diff] [blame] | 551 | // Cases, labels, and defaults aren't "scope parents". It's also |
| 552 | // important to handle these iteratively instead of recursively in |
| 553 | // order to avoid blowing out the stack. |
| 554 | while (true) { |
| 555 | Stmt *Next; |
Chris Lattner | c8e630e | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 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(); |
John McCall | 4a33fa9 | 2010-08-02 23:33:14 +0000 | [diff] [blame] | 562 | else |
| 563 | break; |
| 564 | |
| 565 | LabelAndGotoScopes[SubStmt] = ParentScope; |
| 566 | SubStmt = Next; |
| 567 | } |
| 568 | |
Chris Lattner | 1a1fdbd | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 569 | // Recursively walk the AST. |
| 570 | BuildScopeInformation(SubStmt, ParentScope); |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | /// VerifyJumps - Verify each element of the Jumps array to see if they are |
| 575 | /// valid, emitting diagnostics if not. |
| 576 | void JumpScopeChecker::VerifyJumps() { |
| 577 | while (!Jumps.empty()) { |
| 578 | Stmt *Jump = Jumps.pop_back_val(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 579 | |
| 580 | // With a goto, |
Chris Lattner | 1a1fdbd | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 581 | if (GotoStmt *GS = dyn_cast<GotoStmt>(Jump)) { |
Ehsan Akhgari | 3109758 | 2014-09-22 02:21:54 +0000 | [diff] [blame] | 582 | // The label may not have a statement if it's coming from inline MS ASM. |
| 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); |
| 588 | } |
| 589 | CheckGotoStmt(GS); |
Chris Lattner | 1a1fdbd | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 590 | continue; |
| 591 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 592 | |
John McCall | 9de9160 | 2010-10-28 08:53:48 +0000 | [diff] [blame] | 593 | // We only get indirect gotos here when they have a constant target. |
| 594 | if (IndirectGotoStmt *IGS = dyn_cast<IndirectGotoStmt>(Jump)) { |
Chris Lattner | c8e630e | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 595 | LabelDecl *Target = IGS->getConstantTarget(); |
| 596 | CheckJump(IGS, Target->getStmt(), IGS->getGotoLoc(), |
Francois Pichet | 2f55019 | 2011-09-16 23:15:32 +0000 | [diff] [blame] | 597 | diag::err_goto_into_protected_scope, |
Richard Smith | 1b98ccc | 2014-07-19 01:39:17 +0000 | [diff] [blame] | 598 | diag::ext_goto_into_protected_scope, |
Richard Smith | fe2750d | 2011-10-20 21:42:12 +0000 | [diff] [blame] | 599 | diag::warn_cxx98_compat_goto_into_protected_scope); |
John McCall | 9de9160 | 2010-10-28 08:53:48 +0000 | [diff] [blame] | 600 | continue; |
| 601 | } |
| 602 | |
John McCall | cf819ab | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 603 | SwitchStmt *SS = cast<SwitchStmt>(Jump); |
| 604 | for (SwitchCase *SC = SS->getSwitchCaseList(); SC; |
| 605 | SC = SC->getNextSwitchCase()) { |
Alp Toker | e265cf1 | 2014-05-09 08:40:10 +0000 | [diff] [blame] | 606 | if (CHECK_PERMISSIVE(!LabelAndGotoScopes.count(SC))) |
| 607 | continue; |
Erik Verbruggen | 11a2ecc | 2012-12-25 14:51:39 +0000 | [diff] [blame] | 608 | SourceLocation Loc; |
| 609 | if (CaseStmt *CS = dyn_cast<CaseStmt>(SC)) |
| 610 | Loc = CS->getLocStart(); |
| 611 | else if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) |
| 612 | Loc = DS->getLocStart(); |
| 613 | else |
| 614 | Loc = SC->getLocStart(); |
| 615 | CheckJump(SS, SC, Loc, diag::err_switch_into_protected_scope, 0, |
Richard Smith | fe2750d | 2011-10-20 21:42:12 +0000 | [diff] [blame] | 616 | diag::warn_cxx98_compat_switch_into_protected_scope); |
Chris Lattner | 1a1fdbd | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 617 | } |
| 618 | } |
| 619 | } |
| 620 | |
John McCall | 42f9f1f | 2010-05-12 02:37:54 +0000 | [diff] [blame] | 621 | /// VerifyIndirectJumps - Verify whether any possible indirect jump |
| 622 | /// might cross a protection boundary. Unlike direct jumps, indirect |
| 623 | /// jumps count cleanups as protection boundaries: since there's no |
| 624 | /// way to know where the jump is going, we can't implicitly run the |
| 625 | /// right cleanups the way we can with direct jumps. |
John McCall | cf819ab | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 626 | /// |
John McCall | 42f9f1f | 2010-05-12 02:37:54 +0000 | [diff] [blame] | 627 | /// Thus, an indirect jump is "trivial" if it bypasses no |
| 628 | /// initializations and no teardowns. More formally, an indirect jump |
| 629 | /// from A to B is trivial if the path out from A to DCA(A,B) is |
| 630 | /// trivial and the path in from DCA(A,B) to B is trivial, where |
| 631 | /// DCA(A,B) is the deepest common ancestor of A and B. |
| 632 | /// Jump-triviality is transitive but asymmetric. |
| 633 | /// |
John McCall | cf819ab | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 634 | /// A path in is trivial if none of the entered scopes have an InDiag. |
| 635 | /// A path out is trivial is none of the exited scopes have an OutDiag. |
John McCall | 42f9f1f | 2010-05-12 02:37:54 +0000 | [diff] [blame] | 636 | /// |
| 637 | /// Under these definitions, this function checks that the indirect |
| 638 | /// jump between A and B is trivial for every indirect goto statement A |
| 639 | /// and every label B whose address was taken in the function. |
John McCall | cf819ab | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 640 | void JumpScopeChecker::VerifyIndirectJumps() { |
| 641 | if (IndirectJumps.empty()) return; |
| 642 | |
| 643 | // If there aren't any address-of-label expressions in this function, |
| 644 | // complain about the first indirect goto. |
| 645 | if (IndirectJumpTargets.empty()) { |
| 646 | S.Diag(IndirectJumps[0]->getGotoLoc(), |
| 647 | diag::err_indirect_goto_without_addrlabel); |
| 648 | return; |
| 649 | } |
| 650 | |
John McCall | 42f9f1f | 2010-05-12 02:37:54 +0000 | [diff] [blame] | 651 | // Collect a single representative of every scope containing an |
| 652 | // indirect goto. For most code bases, this substantially cuts |
| 653 | // down on the number of jump sites we'll have to consider later. |
John McCall | cf819ab | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 654 | typedef std::pair<unsigned, IndirectGotoStmt*> JumpScope; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 655 | SmallVector<JumpScope, 32> JumpScopes; |
John McCall | cf819ab | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 656 | { |
| 657 | llvm::DenseMap<unsigned, IndirectGotoStmt*> JumpScopesMap; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 658 | for (SmallVectorImpl<IndirectGotoStmt*>::iterator |
John McCall | cf819ab | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 659 | I = IndirectJumps.begin(), E = IndirectJumps.end(); I != E; ++I) { |
| 660 | IndirectGotoStmt *IG = *I; |
Alp Toker | e265cf1 | 2014-05-09 08:40:10 +0000 | [diff] [blame] | 661 | if (CHECK_PERMISSIVE(!LabelAndGotoScopes.count(IG))) |
| 662 | continue; |
John McCall | cf819ab | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 663 | unsigned IGScope = LabelAndGotoScopes[IG]; |
| 664 | IndirectGotoStmt *&Entry = JumpScopesMap[IGScope]; |
| 665 | if (!Entry) Entry = IG; |
| 666 | } |
| 667 | JumpScopes.reserve(JumpScopesMap.size()); |
| 668 | for (llvm::DenseMap<unsigned, IndirectGotoStmt*>::iterator |
| 669 | I = JumpScopesMap.begin(), E = JumpScopesMap.end(); I != E; ++I) |
| 670 | JumpScopes.push_back(*I); |
| 671 | } |
| 672 | |
John McCall | 42f9f1f | 2010-05-12 02:37:54 +0000 | [diff] [blame] | 673 | // Collect a single representative of every scope containing a |
| 674 | // label whose address was taken somewhere in the function. |
| 675 | // For most code bases, there will be only one such scope. |
Chris Lattner | c8e630e | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 676 | llvm::DenseMap<unsigned, LabelDecl*> TargetScopes; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 677 | for (SmallVectorImpl<LabelDecl*>::iterator |
John McCall | cf819ab | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 678 | I = IndirectJumpTargets.begin(), E = IndirectJumpTargets.end(); |
| 679 | I != E; ++I) { |
Chris Lattner | c8e630e | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 680 | LabelDecl *TheLabel = *I; |
Alp Toker | e265cf1 | 2014-05-09 08:40:10 +0000 | [diff] [blame] | 681 | if (CHECK_PERMISSIVE(!LabelAndGotoScopes.count(TheLabel->getStmt()))) |
| 682 | continue; |
Chris Lattner | c8e630e | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 683 | unsigned LabelScope = LabelAndGotoScopes[TheLabel->getStmt()]; |
| 684 | LabelDecl *&Target = TargetScopes[LabelScope]; |
John McCall | cf819ab | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 685 | if (!Target) Target = TheLabel; |
| 686 | } |
| 687 | |
John McCall | 42f9f1f | 2010-05-12 02:37:54 +0000 | [diff] [blame] | 688 | // For each target scope, make sure it's trivially reachable from |
| 689 | // every scope containing a jump site. |
| 690 | // |
| 691 | // A path between scopes always consists of exitting zero or more |
| 692 | // scopes, then entering zero or more scopes. We build a set of |
| 693 | // of scopes S from which the target scope can be trivially |
| 694 | // entered, then verify that every jump scope can be trivially |
| 695 | // exitted to reach a scope in S. |
John McCall | cf819ab | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 696 | llvm::BitVector Reachable(Scopes.size(), false); |
Chris Lattner | c8e630e | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 697 | for (llvm::DenseMap<unsigned,LabelDecl*>::iterator |
John McCall | cf819ab | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 698 | TI = TargetScopes.begin(), TE = TargetScopes.end(); TI != TE; ++TI) { |
| 699 | unsigned TargetScope = TI->first; |
Chris Lattner | c8e630e | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 700 | LabelDecl *TargetLabel = TI->second; |
John McCall | cf819ab | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 701 | |
| 702 | Reachable.reset(); |
| 703 | |
| 704 | // Mark all the enclosing scopes from which you can safely jump |
John McCall | 42f9f1f | 2010-05-12 02:37:54 +0000 | [diff] [blame] | 705 | // into the target scope. 'Min' will end up being the index of |
| 706 | // the shallowest such scope. |
John McCall | cf819ab | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 707 | unsigned Min = TargetScope; |
| 708 | while (true) { |
| 709 | Reachable.set(Min); |
| 710 | |
| 711 | // Don't go beyond the outermost scope. |
| 712 | if (Min == 0) break; |
| 713 | |
John McCall | 42f9f1f | 2010-05-12 02:37:54 +0000 | [diff] [blame] | 714 | // Stop if we can't trivially enter the current scope. |
John McCall | cf819ab | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 715 | if (Scopes[Min].InDiag) break; |
| 716 | |
| 717 | Min = Scopes[Min].ParentScope; |
| 718 | } |
| 719 | |
| 720 | // Walk through all the jump sites, checking that they can trivially |
| 721 | // reach this label scope. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 722 | for (SmallVectorImpl<JumpScope>::iterator |
John McCall | cf819ab | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 723 | I = JumpScopes.begin(), E = JumpScopes.end(); I != E; ++I) { |
| 724 | unsigned Scope = I->first; |
| 725 | |
| 726 | // Walk out the "scope chain" for this scope, looking for a scope |
John McCall | 42f9f1f | 2010-05-12 02:37:54 +0000 | [diff] [blame] | 727 | // we've marked reachable. For well-formed code this amortizes |
| 728 | // to O(JumpScopes.size() / Scopes.size()): we only iterate |
| 729 | // when we see something unmarked, and in well-formed code we |
| 730 | // mark everything we iterate past. |
John McCall | cf819ab | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 731 | bool IsReachable = false; |
| 732 | while (true) { |
| 733 | if (Reachable.test(Scope)) { |
| 734 | // If we find something reachable, mark all the scopes we just |
| 735 | // walked through as reachable. |
| 736 | for (unsigned S = I->first; S != Scope; S = Scopes[S].ParentScope) |
| 737 | Reachable.set(S); |
| 738 | IsReachable = true; |
| 739 | break; |
| 740 | } |
| 741 | |
| 742 | // Don't walk out if we've reached the top-level scope or we've |
| 743 | // gotten shallower than the shallowest reachable scope. |
| 744 | if (Scope == 0 || Scope < Min) break; |
| 745 | |
| 746 | // Don't walk out through an out-diagnostic. |
| 747 | if (Scopes[Scope].OutDiag) break; |
| 748 | |
| 749 | Scope = Scopes[Scope].ParentScope; |
| 750 | } |
| 751 | |
| 752 | // Only diagnose if we didn't find something. |
| 753 | if (IsReachable) continue; |
| 754 | |
| 755 | DiagnoseIndirectJump(I->second, I->first, TargetLabel, TargetScope); |
| 756 | } |
| 757 | } |
| 758 | } |
| 759 | |
Richard Smith | fe2750d | 2011-10-20 21:42:12 +0000 | [diff] [blame] | 760 | /// Return true if a particular error+note combination must be downgraded to a |
| 761 | /// warning in Microsoft mode. |
| 762 | static bool IsMicrosoftJumpWarning(unsigned JumpDiag, unsigned InDiagNote) { |
| 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)); |
| 766 | } |
| 767 | |
| 768 | /// Return true if a particular note should be downgraded to a compatibility |
| 769 | /// warning in C++11 mode. |
| 770 | static bool IsCXX98CompatWarning(Sema &S, unsigned InDiagNote) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 771 | return S.getLangOpts().CPlusPlus11 && |
Richard Smith | fe2750d | 2011-10-20 21:42:12 +0000 | [diff] [blame] | 772 | InDiagNote == diag::note_protected_by_variable_non_pod; |
| 773 | } |
| 774 | |
| 775 | /// Produce primary diagnostic for an indirect jump statement. |
| 776 | static void DiagnoseIndirectJumpStmt(Sema &S, IndirectGotoStmt *Jump, |
| 777 | LabelDecl *Target, bool &Diagnosed) { |
| 778 | if (Diagnosed) |
| 779 | return; |
| 780 | S.Diag(Jump->getGotoLoc(), diag::err_indirect_goto_in_protected_scope); |
| 781 | S.Diag(Target->getStmt()->getIdentLoc(), diag::note_indirect_goto_target); |
| 782 | Diagnosed = true; |
| 783 | } |
| 784 | |
| 785 | /// Produce note diagnostics for a jump into a protected scope. |
Bill Wendling | 8ac06af | 2012-02-22 09:38:11 +0000 | [diff] [blame] | 786 | void JumpScopeChecker::NoteJumpIntoScopes(ArrayRef<unsigned> ToScopes) { |
Alp Toker | e265cf1 | 2014-05-09 08:40:10 +0000 | [diff] [blame] | 787 | if (CHECK_PERMISSIVE(ToScopes.empty())) |
| 788 | return; |
Richard Smith | fe2750d | 2011-10-20 21:42:12 +0000 | [diff] [blame] | 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); |
| 792 | } |
| 793 | |
John McCall | 42f9f1f | 2010-05-12 02:37:54 +0000 | [diff] [blame] | 794 | /// Diagnose an indirect jump which is known to cross scopes. |
John McCall | cf819ab | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 795 | void JumpScopeChecker::DiagnoseIndirectJump(IndirectGotoStmt *Jump, |
| 796 | unsigned JumpScope, |
Chris Lattner | c8e630e | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 797 | LabelDecl *Target, |
John McCall | cf819ab | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 798 | unsigned TargetScope) { |
Alp Toker | e265cf1 | 2014-05-09 08:40:10 +0000 | [diff] [blame] | 799 | if (CHECK_PERMISSIVE(JumpScope == TargetScope)) |
| 800 | return; |
John McCall | cf819ab | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 801 | |
John McCall | 42f9f1f | 2010-05-12 02:37:54 +0000 | [diff] [blame] | 802 | unsigned Common = GetDeepestCommonScope(JumpScope, TargetScope); |
Richard Smith | fe2750d | 2011-10-20 21:42:12 +0000 | [diff] [blame] | 803 | bool Diagnosed = false; |
John McCall | cf819ab | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 804 | |
John McCall | 42f9f1f | 2010-05-12 02:37:54 +0000 | [diff] [blame] | 805 | // Walk out the scope chain until we reach the common ancestor. |
| 806 | for (unsigned I = JumpScope; I != Common; I = Scopes[I].ParentScope) |
Richard Smith | fe2750d | 2011-10-20 21:42:12 +0000 | [diff] [blame] | 807 | if (Scopes[I].OutDiag) { |
| 808 | DiagnoseIndirectJumpStmt(S, Jump, Target, Diagnosed); |
John McCall | 42f9f1f | 2010-05-12 02:37:54 +0000 | [diff] [blame] | 809 | S.Diag(Scopes[I].Loc, Scopes[I].OutDiag); |
Richard Smith | fe2750d | 2011-10-20 21:42:12 +0000 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | SmallVector<unsigned, 10> ToScopesCXX98Compat; |
John McCall | cf819ab | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 813 | |
| 814 | // Now walk into the scopes containing the label whose address was taken. |
John McCall | 42f9f1f | 2010-05-12 02:37:54 +0000 | [diff] [blame] | 815 | for (unsigned I = TargetScope; I != Common; I = Scopes[I].ParentScope) |
Richard Smith | fe2750d | 2011-10-20 21:42:12 +0000 | [diff] [blame] | 816 | if (IsCXX98CompatWarning(S, Scopes[I].InDiag)) |
| 817 | ToScopesCXX98Compat.push_back(I); |
| 818 | else if (Scopes[I].InDiag) { |
| 819 | DiagnoseIndirectJumpStmt(S, Jump, Target, Diagnosed); |
John McCall | 42f9f1f | 2010-05-12 02:37:54 +0000 | [diff] [blame] | 820 | S.Diag(Scopes[I].Loc, Scopes[I].InDiag); |
Richard Smith | fe2750d | 2011-10-20 21:42:12 +0000 | [diff] [blame] | 821 | } |
John McCall | cf819ab | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 822 | |
Richard Smith | fe2750d | 2011-10-20 21:42:12 +0000 | [diff] [blame] | 823 | // Diagnose this jump if it would be ill-formed in C++98. |
| 824 | if (!Diagnosed && !ToScopesCXX98Compat.empty()) { |
| 825 | S.Diag(Jump->getGotoLoc(), |
| 826 | diag::warn_cxx98_compat_indirect_goto_in_protected_scope); |
| 827 | S.Diag(Target->getStmt()->getIdentLoc(), diag::note_indirect_goto_target); |
| 828 | NoteJumpIntoScopes(ToScopesCXX98Compat); |
| 829 | } |
Francois Pichet | 051f5e5 | 2011-09-13 10:26:51 +0000 | [diff] [blame] | 830 | } |
| 831 | |
Chris Lattner | 1a1fdbd | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 832 | /// CheckJump - Validate that the specified jump statement is valid: that it is |
| 833 | /// jumping within or out of its current scope, not into a deeper one. |
Francois Pichet | 051f5e5 | 2011-09-13 10:26:51 +0000 | [diff] [blame] | 834 | void JumpScopeChecker::CheckJump(Stmt *From, Stmt *To, SourceLocation DiagLoc, |
Richard Smith | fe2750d | 2011-10-20 21:42:12 +0000 | [diff] [blame] | 835 | unsigned JumpDiagError, unsigned JumpDiagWarning, |
| 836 | unsigned JumpDiagCXX98Compat) { |
Alp Toker | e265cf1 | 2014-05-09 08:40:10 +0000 | [diff] [blame] | 837 | if (CHECK_PERMISSIVE(!LabelAndGotoScopes.count(From))) |
| 838 | return; |
| 839 | if (CHECK_PERMISSIVE(!LabelAndGotoScopes.count(To))) |
| 840 | return; |
Chris Lattner | 1a1fdbd | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 841 | |
Alp Toker | e265cf1 | 2014-05-09 08:40:10 +0000 | [diff] [blame] | 842 | unsigned FromScope = LabelAndGotoScopes[From]; |
Chris Lattner | 1a1fdbd | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 843 | unsigned ToScope = LabelAndGotoScopes[To]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 844 | |
Chris Lattner | 1a1fdbd | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 845 | // Common case: exactly the same scope, which is fine. |
| 846 | if (FromScope == ToScope) return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 847 | |
Nico Weber | eb0cfb5 | 2015-03-09 04:27:56 +0000 | [diff] [blame] | 848 | // Warn on gotos out of __finally blocks. |
| 849 | if (isa<GotoStmt>(From) || isa<IndirectGotoStmt>(From)) { |
| 850 | // If FromScope > ToScope, FromScope is more nested and the jump goes to a |
| 851 | // less nested scope. Check if it crosses a __finally along the way. |
| 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); |
| 855 | break; |
| 856 | } |
| 857 | } |
| 858 | } |
| 859 | |
John McCall | 42f9f1f | 2010-05-12 02:37:54 +0000 | [diff] [blame] | 860 | unsigned CommonScope = GetDeepestCommonScope(FromScope, ToScope); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 861 | |
John McCall | 42f9f1f | 2010-05-12 02:37:54 +0000 | [diff] [blame] | 862 | // It's okay to jump out from a nested scope. |
| 863 | if (CommonScope == ToScope) return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 864 | |
John McCall | 42f9f1f | 2010-05-12 02:37:54 +0000 | [diff] [blame] | 865 | // Pull out (and reverse) any scopes we might need to diagnose skipping. |
Richard Smith | fe2750d | 2011-10-20 21:42:12 +0000 | [diff] [blame] | 866 | SmallVector<unsigned, 10> ToScopesCXX98Compat; |
Francois Pichet | 051f5e5 | 2011-09-13 10:26:51 +0000 | [diff] [blame] | 867 | SmallVector<unsigned, 10> ToScopesError; |
| 868 | SmallVector<unsigned, 10> ToScopesWarning; |
| 869 | for (unsigned I = ToScope; I != CommonScope; I = Scopes[I].ParentScope) { |
Alp Toker | bfa3934 | 2014-01-14 12:51:41 +0000 | [diff] [blame] | 870 | if (S.getLangOpts().MSVCCompat && JumpDiagWarning != 0 && |
Francois Pichet | 051f5e5 | 2011-09-13 10:26:51 +0000 | [diff] [blame] | 871 | IsMicrosoftJumpWarning(JumpDiagError, Scopes[I].InDiag)) |
| 872 | ToScopesWarning.push_back(I); |
Richard Smith | fe2750d | 2011-10-20 21:42:12 +0000 | [diff] [blame] | 873 | else if (IsCXX98CompatWarning(S, Scopes[I].InDiag)) |
| 874 | ToScopesCXX98Compat.push_back(I); |
Francois Pichet | 051f5e5 | 2011-09-13 10:26:51 +0000 | [diff] [blame] | 875 | else if (Scopes[I].InDiag) |
| 876 | ToScopesError.push_back(I); |
| 877 | } |
Chris Lattner | 1a1fdbd | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 878 | |
Francois Pichet | 051f5e5 | 2011-09-13 10:26:51 +0000 | [diff] [blame] | 879 | // Handle warnings. |
| 880 | if (!ToScopesWarning.empty()) { |
| 881 | S.Diag(DiagLoc, JumpDiagWarning); |
Richard Smith | fe2750d | 2011-10-20 21:42:12 +0000 | [diff] [blame] | 882 | NoteJumpIntoScopes(ToScopesWarning); |
Francois Pichet | 051f5e5 | 2011-09-13 10:26:51 +0000 | [diff] [blame] | 883 | } |
John McCall | cf819ab | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 884 | |
Francois Pichet | 051f5e5 | 2011-09-13 10:26:51 +0000 | [diff] [blame] | 885 | // Handle errors. |
| 886 | if (!ToScopesError.empty()) { |
| 887 | S.Diag(DiagLoc, JumpDiagError); |
Richard Smith | fe2750d | 2011-10-20 21:42:12 +0000 | [diff] [blame] | 888 | NoteJumpIntoScopes(ToScopesError); |
| 889 | } |
| 890 | |
| 891 | // Handle -Wc++98-compat warnings if the jump is well-formed. |
| 892 | if (ToScopesError.empty() && !ToScopesCXX98Compat.empty()) { |
| 893 | S.Diag(DiagLoc, JumpDiagCXX98Compat); |
| 894 | NoteJumpIntoScopes(ToScopesCXX98Compat); |
Francois Pichet | 051f5e5 | 2011-09-13 10:26:51 +0000 | [diff] [blame] | 895 | } |
Chris Lattner | 1a1fdbd | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 896 | } |
| 897 | |
Ehsan Akhgari | 3109758 | 2014-09-22 02:21:54 +0000 | [diff] [blame] | 898 | void JumpScopeChecker::CheckGotoStmt(GotoStmt *GS) { |
| 899 | if (GS->getLabel()->isMSAsmLabel()) { |
| 900 | S.Diag(GS->getGotoLoc(), diag::err_goto_ms_asm_label) |
| 901 | << GS->getLabel()->getIdentifier(); |
| 902 | S.Diag(GS->getLabel()->getLocation(), diag::note_goto_ms_asm_label) |
| 903 | << GS->getLabel()->getIdentifier(); |
| 904 | } |
| 905 | } |
| 906 | |
Chris Lattner | 1a1fdbd | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 907 | void Sema::DiagnoseInvalidJumps(Stmt *Body) { |
Douglas Gregor | 120f6a6 | 2009-11-17 06:14:37 +0000 | [diff] [blame] | 908 | (void)JumpScopeChecker(Body, *this); |
Chris Lattner | 1a1fdbd | 2009-04-19 04:46:21 +0000 | [diff] [blame] | 909 | } |