blob: 2234d6ba9b11f577994fc54583ec5f717dc43070 [file] [log] [blame]
Francois Pichet2731b7d2011-09-09 11:02:57 +00001//===--- JumpDiagnostics.cpp - Protected scope jump analysis ------*- C++ -*-=//
Chris Lattner1a1fdbd2009-04-19 04:46:21 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Lattner1a1fdbd2009-04-19 04:46:21 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the JumpScopeChecker class, which is used to diagnose
Francois Pichet2731b7d2011-09-09 11:02:57 +000010// jumps that enter a protected scope in an invalid way.
Chris Lattner1a1fdbd2009-04-19 04:46:21 +000011//
12//===----------------------------------------------------------------------===//
13
John McCall83024632010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
John McCall28a0cf72010-08-25 07:42:41 +000015#include "clang/AST/DeclCXX.h"
Chris Lattner1a1fdbd2009-04-19 04:46:21 +000016#include "clang/AST/Expr.h"
Douglas Gregor27d0c442011-05-27 16:05:29 +000017#include "clang/AST/ExprCXX.h"
Sebastian Redl4de47b42009-04-27 20:27:31 +000018#include "clang/AST/StmtCXX.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000019#include "clang/AST/StmtObjC.h"
Douglas Gregorc3a6ade2010-08-12 20:07:10 +000020#include "llvm/ADT/BitVector.h"
Chris Lattner1a1fdbd2009-04-19 04:46:21 +000021using namespace clang;
22
23namespace {
24
25/// JumpScopeChecker - This object is used by Sema to diagnose invalid jumps
26/// into VLA and other protected scopes. For example, this rejects:
27/// goto L;
28/// int a[n];
29/// L:
30///
31class JumpScopeChecker {
32 Sema &S;
Mike Stump11289f42009-09-09 15:08:12 +000033
Alp Tokere265cf12014-05-09 08:40:10 +000034 /// Permissive - True when recovering from errors, in which case precautions
35 /// are taken to handle incomplete scope information.
36 const bool Permissive;
37
Chris Lattner1a1fdbd2009-04-19 04:46:21 +000038 /// GotoScope - This is a record that we use to keep track of all of the
39 /// scopes that are introduced by VLAs and other things that scope jumps like
40 /// gotos. This scope tree has nothing to do with the source scope tree,
41 /// because you can have multiple VLA scopes per compound statement, and most
42 /// compound statements don't introduce any scopes.
43 struct GotoScope {
44 /// ParentScope - The index in ScopeMap of the parent scope. This is 0 for
45 /// the parent scope is the function body.
46 unsigned ParentScope;
Mike Stump11289f42009-09-09 15:08:12 +000047
Richard Smithfe2750d2011-10-20 21:42:12 +000048 /// InDiag - The note to emit if there is a jump into this scope.
John McCallcf819ab2010-05-12 00:58:13 +000049 unsigned InDiag;
50
Richard Smithfe2750d2011-10-20 21:42:12 +000051 /// OutDiag - The note to emit if there is an indirect jump out
John McCallcf819ab2010-05-12 00:58:13 +000052 /// of this scope. Direct jumps always clean up their current scope
53 /// in an orderly way.
54 unsigned OutDiag;
Mike Stump11289f42009-09-09 15:08:12 +000055
Chris Lattner1a1fdbd2009-04-19 04:46:21 +000056 /// Loc - Location to emit the diagnostic.
57 SourceLocation Loc;
Mike Stump11289f42009-09-09 15:08:12 +000058
John McCallcf819ab2010-05-12 00:58:13 +000059 GotoScope(unsigned parentScope, unsigned InDiag, unsigned OutDiag,
60 SourceLocation L)
61 : ParentScope(parentScope), InDiag(InDiag), OutDiag(OutDiag), Loc(L) {}
Chris Lattner1a1fdbd2009-04-19 04:46:21 +000062 };
Mike Stump11289f42009-09-09 15:08:12 +000063
Chris Lattner0e62c1c2011-07-23 10:55:15 +000064 SmallVector<GotoScope, 48> Scopes;
Chris Lattner1a1fdbd2009-04-19 04:46:21 +000065 llvm::DenseMap<Stmt*, unsigned> LabelAndGotoScopes;
Chris Lattner0e62c1c2011-07-23 10:55:15 +000066 SmallVector<Stmt*, 16> Jumps;
John McCallcf819ab2010-05-12 00:58:13 +000067
Chris Lattner0e62c1c2011-07-23 10:55:15 +000068 SmallVector<IndirectGotoStmt*, 4> IndirectJumps;
69 SmallVector<LabelDecl*, 4> IndirectJumpTargets;
Chris Lattner1a1fdbd2009-04-19 04:46:21 +000070public:
71 JumpScopeChecker(Stmt *Body, Sema &S);
72private:
Douglas Gregor27b98ea2010-06-21 23:44:13 +000073 void BuildScopeInformation(Decl *D, unsigned &ParentScope);
Hubert Tong64c2f5a2015-06-04 22:53:21 +000074 void BuildScopeInformation(VarDecl *D, const BlockDecl *BDecl,
Fariborz Jahanian256d39d2011-07-11 18:04:54 +000075 unsigned &ParentScope);
76 void BuildScopeInformation(Stmt *S, unsigned &origParentScope);
Hubert Tong64c2f5a2015-06-04 22:53:21 +000077
Chris Lattner1a1fdbd2009-04-19 04:46:21 +000078 void VerifyJumps();
John McCallcf819ab2010-05-12 00:58:13 +000079 void VerifyIndirectJumps();
Bill Wendling8ac06af2012-02-22 09:38:11 +000080 void NoteJumpIntoScopes(ArrayRef<unsigned> ToScopes);
John McCallcf819ab2010-05-12 00:58:13 +000081 void DiagnoseIndirectJump(IndirectGotoStmt *IG, unsigned IGScope,
Chris Lattnerc8e630e2011-02-17 07:39:24 +000082 LabelDecl *Target, unsigned TargetScope);
Francois Pichet051f5e52011-09-13 10:26:51 +000083 void CheckJump(Stmt *From, Stmt *To, SourceLocation DiagLoc,
Richard Smithfe2750d2011-10-20 21:42:12 +000084 unsigned JumpDiag, unsigned JumpDiagWarning,
85 unsigned JumpDiagCXX98Compat);
Ehsan Akhgari31097582014-09-22 02:21:54 +000086 void CheckGotoStmt(GotoStmt *GS);
John McCall42f9f1f2010-05-12 02:37:54 +000087
88 unsigned GetDeepestCommonScope(unsigned A, unsigned B);
Chris Lattner1a1fdbd2009-04-19 04:46:21 +000089};
90} // end anonymous namespace
91
Alp Tokere265cf12014-05-09 08:40:10 +000092#define CHECK_PERMISSIVE(x) (assert(Permissive || !(x)), (Permissive && (x)))
Chris Lattner1a1fdbd2009-04-19 04:46:21 +000093
Alp Tokere265cf12014-05-09 08:40:10 +000094JumpScopeChecker::JumpScopeChecker(Stmt *Body, Sema &s)
95 : S(s), Permissive(s.hasAnyUnrecoverableErrorsInThisFunction()) {
Chris Lattner1a1fdbd2009-04-19 04:46:21 +000096 // Add a scope entry for function scope.
John McCallcf819ab2010-05-12 00:58:13 +000097 Scopes.push_back(GotoScope(~0U, ~0U, ~0U, SourceLocation()));
Mike Stump11289f42009-09-09 15:08:12 +000098
Chris Lattner1a1fdbd2009-04-19 04:46:21 +000099 // Build information for the top level compound statement, so that we have a
100 // defined scope record for every "goto" and label.
Fariborz Jahanian256d39d2011-07-11 18:04:54 +0000101 unsigned BodyParentScope = 0;
102 BuildScopeInformation(Body, BodyParentScope);
Mike Stump11289f42009-09-09 15:08:12 +0000103
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000104 // Check that all jumps we saw are kosher.
105 VerifyJumps();
John McCallcf819ab2010-05-12 00:58:13 +0000106 VerifyIndirectJumps();
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000107}
Mike Stump11289f42009-09-09 15:08:12 +0000108
John McCall42f9f1f2010-05-12 02:37:54 +0000109/// GetDeepestCommonScope - Finds the innermost scope enclosing the
110/// two scopes.
111unsigned JumpScopeChecker::GetDeepestCommonScope(unsigned A, unsigned B) {
112 while (A != B) {
113 // Inner scopes are created after outer scopes and therefore have
114 // higher indices.
115 if (A < B) {
116 assert(Scopes[B].ParentScope < B);
117 B = Scopes[B].ParentScope;
118 } else {
119 assert(Scopes[A].ParentScope < A);
120 A = Scopes[A].ParentScope;
121 }
122 }
123 return A;
124}
125
John McCall31168b02011-06-15 23:02:42 +0000126typedef std::pair<unsigned,unsigned> ScopePair;
127
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000128/// GetDiagForGotoScopeDecl - If this decl induces a new goto scope, return a
129/// diagnostic that should be emitted if control goes over it. If not, return 0.
Richard Smithc934e4f2013-12-12 01:27:02 +0000130static ScopePair GetDiagForGotoScopeDecl(Sema &S, const Decl *D) {
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000131 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola44938a72012-10-28 02:44:03 +0000132 unsigned InDiag = 0;
Richard Smithc934e4f2013-12-12 01:27:02 +0000133 unsigned OutDiag = 0;
134
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000135 if (VD->getType()->isVariablyModifiedType())
John McCallcf819ab2010-05-12 00:58:13 +0000136 InDiag = diag::note_protected_by_vla;
137
John McCall31168b02011-06-15 23:02:42 +0000138 if (VD->hasAttr<BlocksAttr>())
139 return ScopePair(diag::note_protected_by___block,
140 diag::note_exits___block);
141
142 if (VD->hasAttr<CleanupAttr>())
143 return ScopePair(diag::note_protected_by_cleanup,
144 diag::note_exits_cleanup);
145
Richard Smithc934e4f2013-12-12 01:27:02 +0000146 if (VD->hasLocalStorage()) {
147 switch (VD->getType().isDestructedType()) {
148 case QualType::DK_objc_strong_lifetime:
John McCall039f2bb2015-10-21 18:06:38 +0000149 return ScopePair(diag::note_protected_by_objc_strong_init,
150 diag::note_exits_objc_strong);
151
Richard Smithc934e4f2013-12-12 01:27:02 +0000152 case QualType::DK_objc_weak_lifetime:
John McCall039f2bb2015-10-21 18:06:38 +0000153 return ScopePair(diag::note_protected_by_objc_weak_init,
154 diag::note_exits_objc_weak);
Richard Smithc934e4f2013-12-12 01:27:02 +0000155
Akira Hatanaka7275da02018-02-28 07:15:55 +0000156 case QualType::DK_nontrivial_c_struct:
157 return ScopePair(diag::note_protected_by_non_trivial_c_struct_init,
158 diag::note_exits_dtor);
159
Richard Smithc934e4f2013-12-12 01:27:02 +0000160 case QualType::DK_cxx_destructor:
161 OutDiag = diag::note_exits_dtor;
162 break;
163
164 case QualType::DK_none:
165 break;
John McCall31168b02011-06-15 23:02:42 +0000166 }
167 }
168
Richard Smithc934e4f2013-12-12 01:27:02 +0000169 const Expr *Init = VD->getInit();
170 if (S.Context.getLangOpts().CPlusPlus && VD->hasLocalStorage() && Init) {
Richard Smithfe2750d2011-10-20 21:42:12 +0000171 // C++11 [stmt.dcl]p3:
John McCall31168b02011-06-15 23:02:42 +0000172 // A program that jumps from a point where a variable with automatic
173 // storage duration is not in scope to a point where it is in scope
174 // is ill-formed unless the variable has scalar type, class type with
Hubert Tong64c2f5a2015-06-04 22:53:21 +0000175 // a trivial default constructor and a trivial destructor, a
John McCall31168b02011-06-15 23:02:42 +0000176 // cv-qualified version of one of these types, or an array of one of
177 // the preceding types and is declared without an initializer.
178
179 // C++03 [stmt.dcl.p3:
180 // A program that jumps from a point where a local variable
181 // with automatic storage duration is not in scope to a point
182 // where it is in scope is ill-formed unless the variable has
183 // POD type and is declared without an initializer.
184
Richard Smithc934e4f2013-12-12 01:27:02 +0000185 InDiag = diag::note_protected_by_variable_init;
John McCall31168b02011-06-15 23:02:42 +0000186
Richard Smithc934e4f2013-12-12 01:27:02 +0000187 // For a variable of (array of) class type declared without an
188 // initializer, we will have call-style initialization and the initializer
189 // will be the CXXConstructExpr with no intervening nodes.
190 if (const CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(Init)) {
191 const CXXConstructorDecl *Ctor = CCE->getConstructor();
192 if (Ctor->isTrivial() && Ctor->isDefaultConstructor() &&
193 VD->getInitStyle() == VarDecl::CallInit) {
Rafael Espindola44938a72012-10-28 02:44:03 +0000194 if (OutDiag)
195 InDiag = diag::note_protected_by_variable_nontriv_destructor;
Richard Smithc934e4f2013-12-12 01:27:02 +0000196 else if (!Ctor->getParent()->isPOD())
Rafael Espindola44938a72012-10-28 02:44:03 +0000197 InDiag = diag::note_protected_by_variable_non_pod;
Richard Smithc934e4f2013-12-12 01:27:02 +0000198 else
199 InDiag = 0;
Douglas Gregor27d0c442011-05-27 16:05:29 +0000200 }
Douglas Gregor5a5fcd82010-07-01 00:21:21 +0000201 }
John McCallcf819ab2010-05-12 00:58:13 +0000202 }
Rafael Espindola44938a72012-10-28 02:44:03 +0000203
Richard Smithc934e4f2013-12-12 01:27:02 +0000204 return ScopePair(InDiag, OutDiag);
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000205 }
Mike Stump11289f42009-09-09 15:08:12 +0000206
Richard Smithc934e4f2013-12-12 01:27:02 +0000207 if (const TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCallcf819ab2010-05-12 00:58:13 +0000208 if (TD->getUnderlyingType()->isVariablyModifiedType())
Richard Smithc934e4f2013-12-12 01:27:02 +0000209 return ScopePair(isa<TypedefDecl>(TD)
210 ? diag::note_protected_by_vla_typedef
211 : diag::note_protected_by_vla_type_alias,
212 0);
Richard Smithdda56e42011-04-15 14:24:37 +0000213 }
214
John McCall31168b02011-06-15 23:02:42 +0000215 return ScopePair(0U, 0U);
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000216}
217
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000218/// Build scope information for a declaration that is part of a DeclStmt.
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000219void JumpScopeChecker::BuildScopeInformation(Decl *D, unsigned &ParentScope) {
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000220 // If this decl causes a new scope, push and switch to it.
Richard Smithc934e4f2013-12-12 01:27:02 +0000221 std::pair<unsigned,unsigned> Diags = GetDiagForGotoScopeDecl(S, D);
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000222 if (Diags.first || Diags.second) {
223 Scopes.push_back(GotoScope(ParentScope, Diags.first, Diags.second,
224 D->getLocation()));
225 ParentScope = Scopes.size()-1;
226 }
Hubert Tong64c2f5a2015-06-04 22:53:21 +0000227
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000228 // If the decl has an initializer, walk it with the potentially new
229 // scope we just installed.
230 if (VarDecl *VD = dyn_cast<VarDecl>(D))
231 if (Expr *Init = VD->getInit())
232 BuildScopeInformation(Init, ParentScope);
233}
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000234
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000235/// Build scope information for a captured block literal variables.
Hubert Tong64c2f5a2015-06-04 22:53:21 +0000236void JumpScopeChecker::BuildScopeInformation(VarDecl *D,
237 const BlockDecl *BDecl,
Fariborz Jahanian256d39d2011-07-11 18:04:54 +0000238 unsigned &ParentScope) {
239 // exclude captured __block variables; there's no destructor
240 // associated with the block literal for them.
241 if (D->hasAttr<BlocksAttr>())
242 return;
243 QualType T = D->getType();
244 QualType::DestructionKind destructKind = T.isDestructedType();
245 if (destructKind != QualType::DK_none) {
246 std::pair<unsigned,unsigned> Diags;
247 switch (destructKind) {
248 case QualType::DK_cxx_destructor:
249 Diags = ScopePair(diag::note_enters_block_captures_cxx_obj,
250 diag::note_exits_block_captures_cxx_obj);
251 break;
252 case QualType::DK_objc_strong_lifetime:
253 Diags = ScopePair(diag::note_enters_block_captures_strong,
254 diag::note_exits_block_captures_strong);
255 break;
256 case QualType::DK_objc_weak_lifetime:
257 Diags = ScopePair(diag::note_enters_block_captures_weak,
258 diag::note_exits_block_captures_weak);
259 break;
Akira Hatanaka7275da02018-02-28 07:15:55 +0000260 case QualType::DK_nontrivial_c_struct:
261 Diags = ScopePair(diag::note_enters_block_captures_non_trivial_c_struct,
262 diag::note_exits_block_captures_non_trivial_c_struct);
263 break;
Fariborz Jahanian256d39d2011-07-11 18:04:54 +0000264 case QualType::DK_none:
Richard Smithfe2750d2011-10-20 21:42:12 +0000265 llvm_unreachable("non-lifetime captured variable");
Fariborz Jahanian256d39d2011-07-11 18:04:54 +0000266 }
267 SourceLocation Loc = D->getLocation();
268 if (Loc.isInvalid())
269 Loc = BDecl->getLocation();
Hubert Tong64c2f5a2015-06-04 22:53:21 +0000270 Scopes.push_back(GotoScope(ParentScope,
Fariborz Jahanian256d39d2011-07-11 18:04:54 +0000271 Diags.first, Diags.second, Loc));
272 ParentScope = Scopes.size()-1;
273 }
274}
275
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000276/// BuildScopeInformation - The statements from CI to CE are known to form a
277/// coherent VLA scope with a specified parent node. Walk through the
278/// statements, adding any labels or gotos to LabelAndGotoScopes and recursively
279/// walking the AST as needed.
Richard Smith8b65f102016-06-21 20:10:11 +0000280void JumpScopeChecker::BuildScopeInformation(Stmt *S,
281 unsigned &origParentScope) {
Fariborz Jahanian256d39d2011-07-11 18:04:54 +0000282 // If this is a statement, rather than an expression, scopes within it don't
283 // propagate out into the enclosing scope. Otherwise we have to worry
284 // about block literals, which have the lifetime of their enclosing statement.
285 unsigned independentParentScope = origParentScope;
Hubert Tong64c2f5a2015-06-04 22:53:21 +0000286 unsigned &ParentScope = ((isa<Expr>(S) && !isa<StmtExpr>(S))
Fariborz Jahanian256d39d2011-07-11 18:04:54 +0000287 ? origParentScope : independentParentScope);
288
Richard Smitha547eb22016-07-14 00:11:03 +0000289 unsigned StmtsToSkip = 0u;
Hubert Tong64c2f5a2015-06-04 22:53:21 +0000290
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000291 // If we found a label, remember that it is in ParentScope scope.
John McCallcf819ab2010-05-12 00:58:13 +0000292 switch (S->getStmtClass()) {
John McCallcf819ab2010-05-12 00:58:13 +0000293 case Stmt::AddrLabelExprClass:
294 IndirectJumpTargets.push_back(cast<AddrLabelExpr>(S)->getLabel());
295 break;
296
Akira Hatanaka39013772017-04-19 17:54:08 +0000297 case Stmt::ObjCForCollectionStmtClass: {
298 auto *CS = cast<ObjCForCollectionStmt>(S);
299 unsigned Diag = diag::note_protected_by_objc_fast_enumeration;
300 unsigned NewParentScope = Scopes.size();
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000301 Scopes.push_back(GotoScope(ParentScope, Diag, 0, S->getBeginLoc()));
Akira Hatanaka39013772017-04-19 17:54:08 +0000302 BuildScopeInformation(CS->getBody(), NewParentScope);
303 return;
304 }
305
John McCallcf819ab2010-05-12 00:58:13 +0000306 case Stmt::IndirectGotoStmtClass:
John McCall9de91602010-10-28 08:53:48 +0000307 // "goto *&&lbl;" is a special case which we treat as equivalent
308 // to a normal goto. In addition, we don't calculate scope in the
309 // operand (to avoid recording the address-of-label use), which
310 // works only because of the restricted set of expressions which
311 // we detect as constant targets.
312 if (cast<IndirectGotoStmt>(S)->getConstantTarget()) {
313 LabelAndGotoScopes[S] = ParentScope;
314 Jumps.push_back(S);
315 return;
316 }
317
John McCallcf819ab2010-05-12 00:58:13 +0000318 LabelAndGotoScopes[S] = ParentScope;
319 IndirectJumps.push_back(cast<IndirectGotoStmt>(S));
320 break;
321
John McCallcf819ab2010-05-12 00:58:13 +0000322 case Stmt::SwitchStmtClass:
Richard Smitha547eb22016-07-14 00:11:03 +0000323 // Evaluate the C++17 init stmt and condition variable
324 // before entering the scope of the switch statement.
325 if (Stmt *Init = cast<SwitchStmt>(S)->getInit()) {
326 BuildScopeInformation(Init, ParentScope);
327 ++StmtsToSkip;
328 }
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000329 if (VarDecl *Var = cast<SwitchStmt>(S)->getConditionVariable()) {
330 BuildScopeInformation(Var, ParentScope);
Richard Smitha547eb22016-07-14 00:11:03 +0000331 ++StmtsToSkip;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000332 }
Adrian Prantlf3b3ccd2017-12-19 22:06:11 +0000333 LLVM_FALLTHROUGH;
Hubert Tong64c2f5a2015-06-04 22:53:21 +0000334
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000335 case Stmt::GotoStmtClass:
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000336 // Remember both what scope a goto is in as well as the fact that we have
337 // it. This makes the second scan not have to walk the AST again.
338 LabelAndGotoScopes[S] = ParentScope;
339 Jumps.push_back(S);
John McCallcf819ab2010-05-12 00:58:13 +0000340 break;
341
Richard Smithb130fe72016-06-23 19:16:49 +0000342 case Stmt::IfStmtClass: {
343 IfStmt *IS = cast<IfStmt>(S);
Erik Pilkington5cd57172016-08-16 17:44:11 +0000344 if (!(IS->isConstexpr() || IS->isObjCAvailabilityCheck()))
Richard Smithb130fe72016-06-23 19:16:49 +0000345 break;
346
Erik Pilkington5cd57172016-08-16 17:44:11 +0000347 unsigned Diag = IS->isConstexpr() ? diag::note_protected_by_constexpr_if
348 : diag::note_protected_by_if_available;
349
Richard Smithb130fe72016-06-23 19:16:49 +0000350 if (VarDecl *Var = IS->getConditionVariable())
351 BuildScopeInformation(Var, ParentScope);
352
353 // Cannot jump into the middle of the condition.
354 unsigned NewParentScope = Scopes.size();
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000355 Scopes.push_back(GotoScope(ParentScope, Diag, 0, IS->getBeginLoc()));
Richard Smithb130fe72016-06-23 19:16:49 +0000356 BuildScopeInformation(IS->getCond(), NewParentScope);
357
358 // Jumps into either arm of an 'if constexpr' are not allowed.
359 NewParentScope = Scopes.size();
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000360 Scopes.push_back(GotoScope(ParentScope, Diag, 0, IS->getBeginLoc()));
Richard Smithb130fe72016-06-23 19:16:49 +0000361 BuildScopeInformation(IS->getThen(), NewParentScope);
362 if (Stmt *Else = IS->getElse()) {
363 NewParentScope = Scopes.size();
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000364 Scopes.push_back(GotoScope(ParentScope, Diag, 0, IS->getBeginLoc()));
Richard Smithb130fe72016-06-23 19:16:49 +0000365 BuildScopeInformation(Else, NewParentScope);
366 }
367 return;
368 }
369
Eli Friedman1e95d4b2012-10-31 23:55:28 +0000370 case Stmt::CXXTryStmtClass: {
371 CXXTryStmt *TS = cast<CXXTryStmt>(S);
Richard Smith8b65f102016-06-21 20:10:11 +0000372 {
373 unsigned NewParentScope = Scopes.size();
374 Scopes.push_back(GotoScope(ParentScope,
375 diag::note_protected_by_cxx_try,
376 diag::note_exits_cxx_try,
377 TS->getSourceRange().getBegin()));
378 if (Stmt *TryBlock = TS->getTryBlock())
379 BuildScopeInformation(TryBlock, NewParentScope);
380 }
Eli Friedman1e95d4b2012-10-31 23:55:28 +0000381
382 // Jump from the catch into the try is not allowed either.
383 for (unsigned I = 0, E = TS->getNumHandlers(); I != E; ++I) {
384 CXXCatchStmt *CS = TS->getHandler(I);
Richard Smith8b65f102016-06-21 20:10:11 +0000385 unsigned NewParentScope = Scopes.size();
Eli Friedman1e95d4b2012-10-31 23:55:28 +0000386 Scopes.push_back(GotoScope(ParentScope,
387 diag::note_protected_by_cxx_catch,
388 diag::note_exits_cxx_catch,
389 CS->getSourceRange().getBegin()));
Richard Smith8b65f102016-06-21 20:10:11 +0000390 BuildScopeInformation(CS->getHandlerBlock(), NewParentScope);
Eli Friedman1e95d4b2012-10-31 23:55:28 +0000391 }
392 return;
393 }
394
Nico Weberb14f8722015-02-03 17:06:08 +0000395 case Stmt::SEHTryStmtClass: {
396 SEHTryStmt *TS = cast<SEHTryStmt>(S);
Richard Smith8b65f102016-06-21 20:10:11 +0000397 {
398 unsigned NewParentScope = Scopes.size();
399 Scopes.push_back(GotoScope(ParentScope,
400 diag::note_protected_by_seh_try,
401 diag::note_exits_seh_try,
402 TS->getSourceRange().getBegin()));
403 if (Stmt *TryBlock = TS->getTryBlock())
404 BuildScopeInformation(TryBlock, NewParentScope);
405 }
Nico Weberb14f8722015-02-03 17:06:08 +0000406
407 // Jump from __except or __finally into the __try are not allowed either.
408 if (SEHExceptStmt *Except = TS->getExceptHandler()) {
Richard Smith8b65f102016-06-21 20:10:11 +0000409 unsigned NewParentScope = Scopes.size();
Nico Weberb14f8722015-02-03 17:06:08 +0000410 Scopes.push_back(GotoScope(ParentScope,
411 diag::note_protected_by_seh_except,
412 diag::note_exits_seh_except,
413 Except->getSourceRange().getBegin()));
Richard Smith8b65f102016-06-21 20:10:11 +0000414 BuildScopeInformation(Except->getBlock(), NewParentScope);
Nico Weberb14f8722015-02-03 17:06:08 +0000415 } else if (SEHFinallyStmt *Finally = TS->getFinallyHandler()) {
Richard Smith8b65f102016-06-21 20:10:11 +0000416 unsigned NewParentScope = Scopes.size();
Nico Weberb14f8722015-02-03 17:06:08 +0000417 Scopes.push_back(GotoScope(ParentScope,
418 diag::note_protected_by_seh_finally,
419 diag::note_exits_seh_finally,
420 Finally->getSourceRange().getBegin()));
Richard Smith8b65f102016-06-21 20:10:11 +0000421 BuildScopeInformation(Finally->getBlock(), NewParentScope);
Nico Weberb14f8722015-02-03 17:06:08 +0000422 }
423
424 return;
425 }
Reid Kleckner1d59f992015-01-22 01:36:17 +0000426
Richard Smith8b65f102016-06-21 20:10:11 +0000427 case Stmt::DeclStmtClass: {
428 // If this is a declstmt with a VLA definition, it defines a scope from here
429 // to the end of the containing context.
430 DeclStmt *DS = cast<DeclStmt>(S);
431 // The decl statement creates a scope if any of the decls in it are VLAs
432 // or have the cleanup attribute.
433 for (auto *I : DS->decls())
434 BuildScopeInformation(I, origParentScope);
435 return;
436 }
437
438 case Stmt::ObjCAtTryStmtClass: {
439 // Disallow jumps into any part of an @try statement by pushing a scope and
440 // walking all sub-stmts in that scope.
441 ObjCAtTryStmt *AT = cast<ObjCAtTryStmt>(S);
442 // Recursively walk the AST for the @try part.
443 {
444 unsigned NewParentScope = Scopes.size();
445 Scopes.push_back(GotoScope(ParentScope,
446 diag::note_protected_by_objc_try,
447 diag::note_exits_objc_try,
448 AT->getAtTryLoc()));
449 if (Stmt *TryPart = AT->getTryBody())
450 BuildScopeInformation(TryPart, NewParentScope);
451 }
452
453 // Jump from the catch to the finally or try is not valid.
454 for (unsigned I = 0, N = AT->getNumCatchStmts(); I != N; ++I) {
455 ObjCAtCatchStmt *AC = AT->getCatchStmt(I);
456 unsigned NewParentScope = Scopes.size();
457 Scopes.push_back(GotoScope(ParentScope,
458 diag::note_protected_by_objc_catch,
459 diag::note_exits_objc_catch,
460 AC->getAtCatchLoc()));
461 // @catches are nested and it isn't
462 BuildScopeInformation(AC->getCatchBody(), NewParentScope);
463 }
464
465 // Jump from the finally to the try or catch is not valid.
466 if (ObjCAtFinallyStmt *AF = AT->getFinallyStmt()) {
467 unsigned NewParentScope = Scopes.size();
468 Scopes.push_back(GotoScope(ParentScope,
469 diag::note_protected_by_objc_finally,
470 diag::note_exits_objc_finally,
471 AF->getAtFinallyLoc()));
472 BuildScopeInformation(AF, NewParentScope);
473 }
474
475 return;
476 }
477
478 case Stmt::ObjCAtSynchronizedStmtClass: {
479 // Disallow jumps into the protected statement of an @synchronized, but
480 // allow jumps into the object expression it protects.
481 ObjCAtSynchronizedStmt *AS = cast<ObjCAtSynchronizedStmt>(S);
482 // Recursively walk the AST for the @synchronized object expr, it is
483 // evaluated in the normal scope.
484 BuildScopeInformation(AS->getSynchExpr(), ParentScope);
485
486 // Recursively walk the AST for the @synchronized part, protected by a new
487 // scope.
488 unsigned NewParentScope = Scopes.size();
489 Scopes.push_back(GotoScope(ParentScope,
490 diag::note_protected_by_objc_synchronized,
491 diag::note_exits_objc_synchronized,
492 AS->getAtSynchronizedLoc()));
493 BuildScopeInformation(AS->getSynchBody(), NewParentScope);
494 return;
495 }
496
497 case Stmt::ObjCAutoreleasePoolStmtClass: {
498 // Disallow jumps into the protected statement of an @autoreleasepool.
499 ObjCAutoreleasePoolStmt *AS = cast<ObjCAutoreleasePoolStmt>(S);
500 // Recursively walk the AST for the @autoreleasepool part, protected by a
501 // new scope.
502 unsigned NewParentScope = Scopes.size();
503 Scopes.push_back(GotoScope(ParentScope,
504 diag::note_protected_by_objc_autoreleasepool,
505 diag::note_exits_objc_autoreleasepool,
506 AS->getAtLoc()));
507 BuildScopeInformation(AS->getSubStmt(), NewParentScope);
508 return;
509 }
510
511 case Stmt::ExprWithCleanupsClass: {
512 // Disallow jumps past full-expressions that use blocks with
513 // non-trivial cleanups of their captures. This is theoretically
514 // implementable but a lot of work which we haven't felt up to doing.
515 ExprWithCleanups *EWC = cast<ExprWithCleanups>(S);
516 for (unsigned i = 0, e = EWC->getNumObjects(); i != e; ++i) {
517 const BlockDecl *BDecl = EWC->getObject(i);
518 for (const auto &CI : BDecl->captures()) {
519 VarDecl *variable = CI.getVariable();
520 BuildScopeInformation(variable, BDecl, origParentScope);
521 }
522 }
523 break;
524 }
525
526 case Stmt::MaterializeTemporaryExprClass: {
527 // Disallow jumps out of scopes containing temporaries lifetime-extended to
528 // automatic storage duration.
529 MaterializeTemporaryExpr *MTE = cast<MaterializeTemporaryExpr>(S);
530 if (MTE->getStorageDuration() == SD_Automatic) {
531 SmallVector<const Expr *, 4> CommaLHS;
532 SmallVector<SubobjectAdjustment, 4> Adjustments;
533 const Expr *ExtendedObject =
534 MTE->GetTemporaryExpr()->skipRValueSubobjectAdjustments(
535 CommaLHS, Adjustments);
536 if (ExtendedObject->getType().isDestructedType()) {
537 Scopes.push_back(GotoScope(ParentScope, 0,
538 diag::note_exits_temporary_dtor,
539 ExtendedObject->getExprLoc()));
540 origParentScope = Scopes.size()-1;
541 }
542 }
543 break;
544 }
545
546 case Stmt::CaseStmtClass:
547 case Stmt::DefaultStmtClass:
548 case Stmt::LabelStmtClass:
549 LabelAndGotoScopes[S] = ParentScope;
550 break;
551
John McCallcf819ab2010-05-12 00:58:13 +0000552 default:
553 break;
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000554 }
Mike Stump11289f42009-09-09 15:08:12 +0000555
Benjamin Kramer642f1732015-07-02 21:03:14 +0000556 for (Stmt *SubStmt : S->children()) {
Richard Smitha547eb22016-07-14 00:11:03 +0000557 if (!SubStmt)
558 continue;
559 if (StmtsToSkip) {
560 --StmtsToSkip;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000561 continue;
562 }
Hubert Tong64c2f5a2015-06-04 22:53:21 +0000563
John McCall4a33fa92010-08-02 23:33:14 +0000564 // Cases, labels, and defaults aren't "scope parents". It's also
565 // important to handle these iteratively instead of recursively in
566 // order to avoid blowing out the stack.
567 while (true) {
568 Stmt *Next;
Vitaly Buka28a1b8c2016-10-26 02:00:00 +0000569 if (SwitchCase *SC = dyn_cast<SwitchCase>(SubStmt))
570 Next = SC->getSubStmt();
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000571 else if (LabelStmt *LS = dyn_cast<LabelStmt>(SubStmt))
572 Next = LS->getSubStmt();
John McCall4a33fa92010-08-02 23:33:14 +0000573 else
574 break;
575
576 LabelAndGotoScopes[SubStmt] = ParentScope;
577 SubStmt = Next;
578 }
579
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000580 // Recursively walk the AST.
581 BuildScopeInformation(SubStmt, ParentScope);
582 }
583}
584
585/// VerifyJumps - Verify each element of the Jumps array to see if they are
586/// valid, emitting diagnostics if not.
587void JumpScopeChecker::VerifyJumps() {
588 while (!Jumps.empty()) {
589 Stmt *Jump = Jumps.pop_back_val();
Mike Stump11289f42009-09-09 15:08:12 +0000590
591 // With a goto,
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000592 if (GotoStmt *GS = dyn_cast<GotoStmt>(Jump)) {
Ehsan Akhgari31097582014-09-22 02:21:54 +0000593 // The label may not have a statement if it's coming from inline MS ASM.
594 if (GS->getLabel()->getStmt()) {
595 CheckJump(GS, GS->getLabel()->getStmt(), GS->getGotoLoc(),
596 diag::err_goto_into_protected_scope,
597 diag::ext_goto_into_protected_scope,
598 diag::warn_cxx98_compat_goto_into_protected_scope);
599 }
600 CheckGotoStmt(GS);
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000601 continue;
602 }
Mike Stump11289f42009-09-09 15:08:12 +0000603
John McCall9de91602010-10-28 08:53:48 +0000604 // We only get indirect gotos here when they have a constant target.
605 if (IndirectGotoStmt *IGS = dyn_cast<IndirectGotoStmt>(Jump)) {
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000606 LabelDecl *Target = IGS->getConstantTarget();
607 CheckJump(IGS, Target->getStmt(), IGS->getGotoLoc(),
Francois Pichet2f550192011-09-16 23:15:32 +0000608 diag::err_goto_into_protected_scope,
Richard Smith1b98ccc2014-07-19 01:39:17 +0000609 diag::ext_goto_into_protected_scope,
Richard Smithfe2750d2011-10-20 21:42:12 +0000610 diag::warn_cxx98_compat_goto_into_protected_scope);
John McCall9de91602010-10-28 08:53:48 +0000611 continue;
612 }
613
John McCallcf819ab2010-05-12 00:58:13 +0000614 SwitchStmt *SS = cast<SwitchStmt>(Jump);
615 for (SwitchCase *SC = SS->getSwitchCaseList(); SC;
616 SC = SC->getNextSwitchCase()) {
Alp Tokere265cf12014-05-09 08:40:10 +0000617 if (CHECK_PERMISSIVE(!LabelAndGotoScopes.count(SC)))
618 continue;
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000619 SourceLocation Loc;
620 if (CaseStmt *CS = dyn_cast<CaseStmt>(SC))
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000621 Loc = CS->getBeginLoc();
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000622 else if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC))
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000623 Loc = DS->getBeginLoc();
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000624 else
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000625 Loc = SC->getBeginLoc();
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000626 CheckJump(SS, SC, Loc, diag::err_switch_into_protected_scope, 0,
Richard Smithfe2750d2011-10-20 21:42:12 +0000627 diag::warn_cxx98_compat_switch_into_protected_scope);
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000628 }
629 }
630}
631
John McCall42f9f1f2010-05-12 02:37:54 +0000632/// VerifyIndirectJumps - Verify whether any possible indirect jump
633/// might cross a protection boundary. Unlike direct jumps, indirect
634/// jumps count cleanups as protection boundaries: since there's no
635/// way to know where the jump is going, we can't implicitly run the
636/// right cleanups the way we can with direct jumps.
John McCallcf819ab2010-05-12 00:58:13 +0000637///
John McCall42f9f1f2010-05-12 02:37:54 +0000638/// Thus, an indirect jump is "trivial" if it bypasses no
639/// initializations and no teardowns. More formally, an indirect jump
640/// from A to B is trivial if the path out from A to DCA(A,B) is
641/// trivial and the path in from DCA(A,B) to B is trivial, where
642/// DCA(A,B) is the deepest common ancestor of A and B.
643/// Jump-triviality is transitive but asymmetric.
644///
John McCallcf819ab2010-05-12 00:58:13 +0000645/// A path in is trivial if none of the entered scopes have an InDiag.
646/// A path out is trivial is none of the exited scopes have an OutDiag.
John McCall42f9f1f2010-05-12 02:37:54 +0000647///
648/// Under these definitions, this function checks that the indirect
649/// jump between A and B is trivial for every indirect goto statement A
650/// and every label B whose address was taken in the function.
John McCallcf819ab2010-05-12 00:58:13 +0000651void JumpScopeChecker::VerifyIndirectJumps() {
652 if (IndirectJumps.empty()) return;
653
654 // If there aren't any address-of-label expressions in this function,
655 // complain about the first indirect goto.
656 if (IndirectJumpTargets.empty()) {
657 S.Diag(IndirectJumps[0]->getGotoLoc(),
658 diag::err_indirect_goto_without_addrlabel);
659 return;
660 }
661
John McCall42f9f1f2010-05-12 02:37:54 +0000662 // Collect a single representative of every scope containing an
663 // indirect goto. For most code bases, this substantially cuts
664 // down on the number of jump sites we'll have to consider later.
John McCallcf819ab2010-05-12 00:58:13 +0000665 typedef std::pair<unsigned, IndirectGotoStmt*> JumpScope;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000666 SmallVector<JumpScope, 32> JumpScopes;
John McCallcf819ab2010-05-12 00:58:13 +0000667 {
668 llvm::DenseMap<unsigned, IndirectGotoStmt*> JumpScopesMap;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000669 for (SmallVectorImpl<IndirectGotoStmt*>::iterator
John McCallcf819ab2010-05-12 00:58:13 +0000670 I = IndirectJumps.begin(), E = IndirectJumps.end(); I != E; ++I) {
671 IndirectGotoStmt *IG = *I;
Alp Tokere265cf12014-05-09 08:40:10 +0000672 if (CHECK_PERMISSIVE(!LabelAndGotoScopes.count(IG)))
673 continue;
John McCallcf819ab2010-05-12 00:58:13 +0000674 unsigned IGScope = LabelAndGotoScopes[IG];
675 IndirectGotoStmt *&Entry = JumpScopesMap[IGScope];
676 if (!Entry) Entry = IG;
677 }
678 JumpScopes.reserve(JumpScopesMap.size());
679 for (llvm::DenseMap<unsigned, IndirectGotoStmt*>::iterator
680 I = JumpScopesMap.begin(), E = JumpScopesMap.end(); I != E; ++I)
681 JumpScopes.push_back(*I);
682 }
683
John McCall42f9f1f2010-05-12 02:37:54 +0000684 // Collect a single representative of every scope containing a
685 // label whose address was taken somewhere in the function.
686 // For most code bases, there will be only one such scope.
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000687 llvm::DenseMap<unsigned, LabelDecl*> TargetScopes;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000688 for (SmallVectorImpl<LabelDecl*>::iterator
John McCallcf819ab2010-05-12 00:58:13 +0000689 I = IndirectJumpTargets.begin(), E = IndirectJumpTargets.end();
690 I != E; ++I) {
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000691 LabelDecl *TheLabel = *I;
Alp Tokere265cf12014-05-09 08:40:10 +0000692 if (CHECK_PERMISSIVE(!LabelAndGotoScopes.count(TheLabel->getStmt())))
693 continue;
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000694 unsigned LabelScope = LabelAndGotoScopes[TheLabel->getStmt()];
695 LabelDecl *&Target = TargetScopes[LabelScope];
John McCallcf819ab2010-05-12 00:58:13 +0000696 if (!Target) Target = TheLabel;
697 }
698
John McCall42f9f1f2010-05-12 02:37:54 +0000699 // For each target scope, make sure it's trivially reachable from
700 // every scope containing a jump site.
701 //
702 // A path between scopes always consists of exitting zero or more
703 // scopes, then entering zero or more scopes. We build a set of
704 // of scopes S from which the target scope can be trivially
705 // entered, then verify that every jump scope can be trivially
706 // exitted to reach a scope in S.
John McCallcf819ab2010-05-12 00:58:13 +0000707 llvm::BitVector Reachable(Scopes.size(), false);
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000708 for (llvm::DenseMap<unsigned,LabelDecl*>::iterator
John McCallcf819ab2010-05-12 00:58:13 +0000709 TI = TargetScopes.begin(), TE = TargetScopes.end(); TI != TE; ++TI) {
710 unsigned TargetScope = TI->first;
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000711 LabelDecl *TargetLabel = TI->second;
John McCallcf819ab2010-05-12 00:58:13 +0000712
713 Reachable.reset();
714
715 // Mark all the enclosing scopes from which you can safely jump
John McCall42f9f1f2010-05-12 02:37:54 +0000716 // into the target scope. 'Min' will end up being the index of
717 // the shallowest such scope.
John McCallcf819ab2010-05-12 00:58:13 +0000718 unsigned Min = TargetScope;
719 while (true) {
720 Reachable.set(Min);
721
722 // Don't go beyond the outermost scope.
723 if (Min == 0) break;
724
John McCall42f9f1f2010-05-12 02:37:54 +0000725 // Stop if we can't trivially enter the current scope.
John McCallcf819ab2010-05-12 00:58:13 +0000726 if (Scopes[Min].InDiag) break;
727
728 Min = Scopes[Min].ParentScope;
729 }
730
731 // Walk through all the jump sites, checking that they can trivially
732 // reach this label scope.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000733 for (SmallVectorImpl<JumpScope>::iterator
John McCallcf819ab2010-05-12 00:58:13 +0000734 I = JumpScopes.begin(), E = JumpScopes.end(); I != E; ++I) {
735 unsigned Scope = I->first;
736
737 // Walk out the "scope chain" for this scope, looking for a scope
John McCall42f9f1f2010-05-12 02:37:54 +0000738 // we've marked reachable. For well-formed code this amortizes
739 // to O(JumpScopes.size() / Scopes.size()): we only iterate
740 // when we see something unmarked, and in well-formed code we
741 // mark everything we iterate past.
John McCallcf819ab2010-05-12 00:58:13 +0000742 bool IsReachable = false;
743 while (true) {
744 if (Reachable.test(Scope)) {
745 // If we find something reachable, mark all the scopes we just
746 // walked through as reachable.
747 for (unsigned S = I->first; S != Scope; S = Scopes[S].ParentScope)
748 Reachable.set(S);
749 IsReachable = true;
750 break;
751 }
752
753 // Don't walk out if we've reached the top-level scope or we've
754 // gotten shallower than the shallowest reachable scope.
755 if (Scope == 0 || Scope < Min) break;
756
757 // Don't walk out through an out-diagnostic.
758 if (Scopes[Scope].OutDiag) break;
759
760 Scope = Scopes[Scope].ParentScope;
761 }
762
763 // Only diagnose if we didn't find something.
764 if (IsReachable) continue;
765
766 DiagnoseIndirectJump(I->second, I->first, TargetLabel, TargetScope);
767 }
768 }
769}
770
Richard Smithfe2750d2011-10-20 21:42:12 +0000771/// Return true if a particular error+note combination must be downgraded to a
772/// warning in Microsoft mode.
773static bool IsMicrosoftJumpWarning(unsigned JumpDiag, unsigned InDiagNote) {
774 return (JumpDiag == diag::err_goto_into_protected_scope &&
775 (InDiagNote == diag::note_protected_by_variable_init ||
776 InDiagNote == diag::note_protected_by_variable_nontriv_destructor));
777}
778
779/// Return true if a particular note should be downgraded to a compatibility
780/// warning in C++11 mode.
781static bool IsCXX98CompatWarning(Sema &S, unsigned InDiagNote) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000782 return S.getLangOpts().CPlusPlus11 &&
Richard Smithfe2750d2011-10-20 21:42:12 +0000783 InDiagNote == diag::note_protected_by_variable_non_pod;
784}
785
786/// Produce primary diagnostic for an indirect jump statement.
787static void DiagnoseIndirectJumpStmt(Sema &S, IndirectGotoStmt *Jump,
788 LabelDecl *Target, bool &Diagnosed) {
789 if (Diagnosed)
790 return;
791 S.Diag(Jump->getGotoLoc(), diag::err_indirect_goto_in_protected_scope);
792 S.Diag(Target->getStmt()->getIdentLoc(), diag::note_indirect_goto_target);
793 Diagnosed = true;
794}
795
796/// Produce note diagnostics for a jump into a protected scope.
Bill Wendling8ac06af2012-02-22 09:38:11 +0000797void JumpScopeChecker::NoteJumpIntoScopes(ArrayRef<unsigned> ToScopes) {
Alp Tokere265cf12014-05-09 08:40:10 +0000798 if (CHECK_PERMISSIVE(ToScopes.empty()))
799 return;
Richard Smithfe2750d2011-10-20 21:42:12 +0000800 for (unsigned I = 0, E = ToScopes.size(); I != E; ++I)
801 if (Scopes[ToScopes[I]].InDiag)
802 S.Diag(Scopes[ToScopes[I]].Loc, Scopes[ToScopes[I]].InDiag);
803}
804
John McCall42f9f1f2010-05-12 02:37:54 +0000805/// Diagnose an indirect jump which is known to cross scopes.
John McCallcf819ab2010-05-12 00:58:13 +0000806void JumpScopeChecker::DiagnoseIndirectJump(IndirectGotoStmt *Jump,
807 unsigned JumpScope,
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000808 LabelDecl *Target,
John McCallcf819ab2010-05-12 00:58:13 +0000809 unsigned TargetScope) {
Alp Tokere265cf12014-05-09 08:40:10 +0000810 if (CHECK_PERMISSIVE(JumpScope == TargetScope))
811 return;
John McCallcf819ab2010-05-12 00:58:13 +0000812
John McCall42f9f1f2010-05-12 02:37:54 +0000813 unsigned Common = GetDeepestCommonScope(JumpScope, TargetScope);
Richard Smithfe2750d2011-10-20 21:42:12 +0000814 bool Diagnosed = false;
John McCallcf819ab2010-05-12 00:58:13 +0000815
John McCall42f9f1f2010-05-12 02:37:54 +0000816 // Walk out the scope chain until we reach the common ancestor.
817 for (unsigned I = JumpScope; I != Common; I = Scopes[I].ParentScope)
Richard Smithfe2750d2011-10-20 21:42:12 +0000818 if (Scopes[I].OutDiag) {
819 DiagnoseIndirectJumpStmt(S, Jump, Target, Diagnosed);
John McCall42f9f1f2010-05-12 02:37:54 +0000820 S.Diag(Scopes[I].Loc, Scopes[I].OutDiag);
Richard Smithfe2750d2011-10-20 21:42:12 +0000821 }
822
823 SmallVector<unsigned, 10> ToScopesCXX98Compat;
John McCallcf819ab2010-05-12 00:58:13 +0000824
825 // Now walk into the scopes containing the label whose address was taken.
John McCall42f9f1f2010-05-12 02:37:54 +0000826 for (unsigned I = TargetScope; I != Common; I = Scopes[I].ParentScope)
Richard Smithfe2750d2011-10-20 21:42:12 +0000827 if (IsCXX98CompatWarning(S, Scopes[I].InDiag))
828 ToScopesCXX98Compat.push_back(I);
829 else if (Scopes[I].InDiag) {
830 DiagnoseIndirectJumpStmt(S, Jump, Target, Diagnosed);
John McCall42f9f1f2010-05-12 02:37:54 +0000831 S.Diag(Scopes[I].Loc, Scopes[I].InDiag);
Richard Smithfe2750d2011-10-20 21:42:12 +0000832 }
John McCallcf819ab2010-05-12 00:58:13 +0000833
Richard Smithfe2750d2011-10-20 21:42:12 +0000834 // Diagnose this jump if it would be ill-formed in C++98.
835 if (!Diagnosed && !ToScopesCXX98Compat.empty()) {
836 S.Diag(Jump->getGotoLoc(),
837 diag::warn_cxx98_compat_indirect_goto_in_protected_scope);
838 S.Diag(Target->getStmt()->getIdentLoc(), diag::note_indirect_goto_target);
839 NoteJumpIntoScopes(ToScopesCXX98Compat);
840 }
Francois Pichet051f5e52011-09-13 10:26:51 +0000841}
842
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000843/// CheckJump - Validate that the specified jump statement is valid: that it is
844/// jumping within or out of its current scope, not into a deeper one.
Francois Pichet051f5e52011-09-13 10:26:51 +0000845void JumpScopeChecker::CheckJump(Stmt *From, Stmt *To, SourceLocation DiagLoc,
Richard Smithfe2750d2011-10-20 21:42:12 +0000846 unsigned JumpDiagError, unsigned JumpDiagWarning,
847 unsigned JumpDiagCXX98Compat) {
Alp Tokere265cf12014-05-09 08:40:10 +0000848 if (CHECK_PERMISSIVE(!LabelAndGotoScopes.count(From)))
849 return;
850 if (CHECK_PERMISSIVE(!LabelAndGotoScopes.count(To)))
851 return;
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000852
Alp Tokere265cf12014-05-09 08:40:10 +0000853 unsigned FromScope = LabelAndGotoScopes[From];
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000854 unsigned ToScope = LabelAndGotoScopes[To];
Mike Stump11289f42009-09-09 15:08:12 +0000855
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000856 // Common case: exactly the same scope, which is fine.
857 if (FromScope == ToScope) return;
Mike Stump11289f42009-09-09 15:08:12 +0000858
Nico Webereb0cfb52015-03-09 04:27:56 +0000859 // Warn on gotos out of __finally blocks.
860 if (isa<GotoStmt>(From) || isa<IndirectGotoStmt>(From)) {
861 // If FromScope > ToScope, FromScope is more nested and the jump goes to a
862 // less nested scope. Check if it crosses a __finally along the way.
863 for (unsigned I = FromScope; I > ToScope; I = Scopes[I].ParentScope) {
864 if (Scopes[I].InDiag == diag::note_protected_by_seh_finally) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000865 S.Diag(From->getBeginLoc(), diag::warn_jump_out_of_seh_finally);
Nico Webereb0cfb52015-03-09 04:27:56 +0000866 break;
867 }
868 }
869 }
870
John McCall42f9f1f2010-05-12 02:37:54 +0000871 unsigned CommonScope = GetDeepestCommonScope(FromScope, ToScope);
Mike Stump11289f42009-09-09 15:08:12 +0000872
John McCall42f9f1f2010-05-12 02:37:54 +0000873 // It's okay to jump out from a nested scope.
874 if (CommonScope == ToScope) return;
Mike Stump11289f42009-09-09 15:08:12 +0000875
John McCall42f9f1f2010-05-12 02:37:54 +0000876 // Pull out (and reverse) any scopes we might need to diagnose skipping.
Richard Smithfe2750d2011-10-20 21:42:12 +0000877 SmallVector<unsigned, 10> ToScopesCXX98Compat;
Francois Pichet051f5e52011-09-13 10:26:51 +0000878 SmallVector<unsigned, 10> ToScopesError;
879 SmallVector<unsigned, 10> ToScopesWarning;
880 for (unsigned I = ToScope; I != CommonScope; I = Scopes[I].ParentScope) {
Alp Tokerbfa39342014-01-14 12:51:41 +0000881 if (S.getLangOpts().MSVCCompat && JumpDiagWarning != 0 &&
Francois Pichet051f5e52011-09-13 10:26:51 +0000882 IsMicrosoftJumpWarning(JumpDiagError, Scopes[I].InDiag))
883 ToScopesWarning.push_back(I);
Richard Smithfe2750d2011-10-20 21:42:12 +0000884 else if (IsCXX98CompatWarning(S, Scopes[I].InDiag))
885 ToScopesCXX98Compat.push_back(I);
Francois Pichet051f5e52011-09-13 10:26:51 +0000886 else if (Scopes[I].InDiag)
887 ToScopesError.push_back(I);
888 }
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000889
Francois Pichet051f5e52011-09-13 10:26:51 +0000890 // Handle warnings.
891 if (!ToScopesWarning.empty()) {
892 S.Diag(DiagLoc, JumpDiagWarning);
Richard Smithfe2750d2011-10-20 21:42:12 +0000893 NoteJumpIntoScopes(ToScopesWarning);
Francois Pichet051f5e52011-09-13 10:26:51 +0000894 }
John McCallcf819ab2010-05-12 00:58:13 +0000895
Francois Pichet051f5e52011-09-13 10:26:51 +0000896 // Handle errors.
897 if (!ToScopesError.empty()) {
898 S.Diag(DiagLoc, JumpDiagError);
Richard Smithfe2750d2011-10-20 21:42:12 +0000899 NoteJumpIntoScopes(ToScopesError);
900 }
901
902 // Handle -Wc++98-compat warnings if the jump is well-formed.
903 if (ToScopesError.empty() && !ToScopesCXX98Compat.empty()) {
904 S.Diag(DiagLoc, JumpDiagCXX98Compat);
905 NoteJumpIntoScopes(ToScopesCXX98Compat);
Francois Pichet051f5e52011-09-13 10:26:51 +0000906 }
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000907}
908
Ehsan Akhgari31097582014-09-22 02:21:54 +0000909void JumpScopeChecker::CheckGotoStmt(GotoStmt *GS) {
910 if (GS->getLabel()->isMSAsmLabel()) {
911 S.Diag(GS->getGotoLoc(), diag::err_goto_ms_asm_label)
912 << GS->getLabel()->getIdentifier();
913 S.Diag(GS->getLabel()->getLocation(), diag::note_goto_ms_asm_label)
914 << GS->getLabel()->getIdentifier();
915 }
916}
917
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000918void Sema::DiagnoseInvalidJumps(Stmt *Body) {
Douglas Gregor120f6a62009-11-17 06:14:37 +0000919 (void)JumpScopeChecker(Body, *this);
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000920}