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