blob: 55582f8c61b84daee5ccef1f8a2635638dac7fe7 [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//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the JumpScopeChecker class, which is used to diagnose
Francois Pichet2731b7d2011-09-09 11:02:57 +000011// jumps that enter a protected scope in an invalid way.
Chris Lattner1a1fdbd2009-04-19 04:46:21 +000012//
13//===----------------------------------------------------------------------===//
14
John McCall83024632010-08-25 22:03:47 +000015#include "clang/Sema/SemaInternal.h"
John McCall28a0cf72010-08-25 07:42:41 +000016#include "clang/AST/DeclCXX.h"
Chris Lattner1a1fdbd2009-04-19 04:46:21 +000017#include "clang/AST/Expr.h"
Douglas Gregor27d0c442011-05-27 16:05:29 +000018#include "clang/AST/ExprCXX.h"
Sebastian Redl4de47b42009-04-27 20:27:31 +000019#include "clang/AST/StmtCXX.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000020#include "clang/AST/StmtObjC.h"
Douglas Gregorc3a6ade2010-08-12 20:07:10 +000021#include "llvm/ADT/BitVector.h"
Chris Lattner1a1fdbd2009-04-19 04:46:21 +000022using namespace clang;
23
24namespace {
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///
32class JumpScopeChecker {
33 Sema &S;
Mike Stump11289f42009-09-09 15:08:12 +000034
Alp Tokere265cf12014-05-09 08:40:10 +000035 /// Permissive - True when recovering from errors, in which case precautions
36 /// are taken to handle incomplete scope information.
37 const bool Permissive;
38
Chris Lattner1a1fdbd2009-04-19 04:46:21 +000039 /// 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 Stump11289f42009-09-09 15:08:12 +000048
Richard Smithfe2750d2011-10-20 21:42:12 +000049 /// InDiag - The note to emit if there is a jump into this scope.
John McCallcf819ab2010-05-12 00:58:13 +000050 unsigned InDiag;
51
Richard Smithfe2750d2011-10-20 21:42:12 +000052 /// OutDiag - The note to emit if there is an indirect jump out
John McCallcf819ab2010-05-12 00:58:13 +000053 /// of this scope. Direct jumps always clean up their current scope
54 /// in an orderly way.
55 unsigned OutDiag;
Mike Stump11289f42009-09-09 15:08:12 +000056
Chris Lattner1a1fdbd2009-04-19 04:46:21 +000057 /// Loc - Location to emit the diagnostic.
58 SourceLocation Loc;
Mike Stump11289f42009-09-09 15:08:12 +000059
John McCallcf819ab2010-05-12 00:58:13 +000060 GotoScope(unsigned parentScope, unsigned InDiag, unsigned OutDiag,
61 SourceLocation L)
62 : ParentScope(parentScope), InDiag(InDiag), OutDiag(OutDiag), Loc(L) {}
Chris Lattner1a1fdbd2009-04-19 04:46:21 +000063 };
Mike Stump11289f42009-09-09 15:08:12 +000064
Chris Lattner0e62c1c2011-07-23 10:55:15 +000065 SmallVector<GotoScope, 48> Scopes;
Chris Lattner1a1fdbd2009-04-19 04:46:21 +000066 llvm::DenseMap<Stmt*, unsigned> LabelAndGotoScopes;
Chris Lattner0e62c1c2011-07-23 10:55:15 +000067 SmallVector<Stmt*, 16> Jumps;
John McCallcf819ab2010-05-12 00:58:13 +000068
Chris Lattner0e62c1c2011-07-23 10:55:15 +000069 SmallVector<IndirectGotoStmt*, 4> IndirectJumps;
70 SmallVector<LabelDecl*, 4> IndirectJumpTargets;
Chris Lattner1a1fdbd2009-04-19 04:46:21 +000071public:
72 JumpScopeChecker(Stmt *Body, Sema &S);
73private:
Douglas Gregor27b98ea2010-06-21 23:44:13 +000074 void BuildScopeInformation(Decl *D, unsigned &ParentScope);
Hubert Tong64c2f5a2015-06-04 22:53:21 +000075 void BuildScopeInformation(VarDecl *D, const BlockDecl *BDecl,
Fariborz Jahanian256d39d2011-07-11 18:04:54 +000076 unsigned &ParentScope);
77 void BuildScopeInformation(Stmt *S, unsigned &origParentScope);
Hubert Tong64c2f5a2015-06-04 22:53:21 +000078
Chris Lattner1a1fdbd2009-04-19 04:46:21 +000079 void VerifyJumps();
John McCallcf819ab2010-05-12 00:58:13 +000080 void VerifyIndirectJumps();
Bill Wendling8ac06af2012-02-22 09:38:11 +000081 void NoteJumpIntoScopes(ArrayRef<unsigned> ToScopes);
John McCallcf819ab2010-05-12 00:58:13 +000082 void DiagnoseIndirectJump(IndirectGotoStmt *IG, unsigned IGScope,
Chris Lattnerc8e630e2011-02-17 07:39:24 +000083 LabelDecl *Target, unsigned TargetScope);
Francois Pichet051f5e52011-09-13 10:26:51 +000084 void CheckJump(Stmt *From, Stmt *To, SourceLocation DiagLoc,
Richard Smithfe2750d2011-10-20 21:42:12 +000085 unsigned JumpDiag, unsigned JumpDiagWarning,
86 unsigned JumpDiagCXX98Compat);
Ehsan Akhgari31097582014-09-22 02:21:54 +000087 void CheckGotoStmt(GotoStmt *GS);
John McCall42f9f1f2010-05-12 02:37:54 +000088
89 unsigned GetDeepestCommonScope(unsigned A, unsigned B);
Chris Lattner1a1fdbd2009-04-19 04:46:21 +000090};
91} // end anonymous namespace
92
Alp Tokere265cf12014-05-09 08:40:10 +000093#define CHECK_PERMISSIVE(x) (assert(Permissive || !(x)), (Permissive && (x)))
Chris Lattner1a1fdbd2009-04-19 04:46:21 +000094
Alp Tokere265cf12014-05-09 08:40:10 +000095JumpScopeChecker::JumpScopeChecker(Stmt *Body, Sema &s)
96 : S(s), Permissive(s.hasAnyUnrecoverableErrorsInThisFunction()) {
Chris Lattner1a1fdbd2009-04-19 04:46:21 +000097 // Add a scope entry for function scope.
John McCallcf819ab2010-05-12 00:58:13 +000098 Scopes.push_back(GotoScope(~0U, ~0U, ~0U, SourceLocation()));
Mike Stump11289f42009-09-09 15:08:12 +000099
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000100 // Build information for the top level compound statement, so that we have a
101 // defined scope record for every "goto" and label.
Fariborz Jahanian256d39d2011-07-11 18:04:54 +0000102 unsigned BodyParentScope = 0;
103 BuildScopeInformation(Body, BodyParentScope);
Mike Stump11289f42009-09-09 15:08:12 +0000104
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000105 // Check that all jumps we saw are kosher.
106 VerifyJumps();
John McCallcf819ab2010-05-12 00:58:13 +0000107 VerifyIndirectJumps();
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000108}
Mike Stump11289f42009-09-09 15:08:12 +0000109
John McCall42f9f1f2010-05-12 02:37:54 +0000110/// GetDeepestCommonScope - Finds the innermost scope enclosing the
111/// two scopes.
112unsigned JumpScopeChecker::GetDeepestCommonScope(unsigned A, unsigned B) {
113 while (A != B) {
114 // Inner scopes are created after outer scopes and therefore have
115 // higher indices.
116 if (A < B) {
117 assert(Scopes[B].ParentScope < B);
118 B = Scopes[B].ParentScope;
119 } else {
120 assert(Scopes[A].ParentScope < A);
121 A = Scopes[A].ParentScope;
122 }
123 }
124 return A;
125}
126
John McCall31168b02011-06-15 23:02:42 +0000127typedef std::pair<unsigned,unsigned> ScopePair;
128
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000129/// GetDiagForGotoScopeDecl - If this decl induces a new goto scope, return a
130/// diagnostic that should be emitted if control goes over it. If not, return 0.
Richard Smithc934e4f2013-12-12 01:27:02 +0000131static ScopePair GetDiagForGotoScopeDecl(Sema &S, const Decl *D) {
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000132 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola44938a72012-10-28 02:44:03 +0000133 unsigned InDiag = 0;
Richard Smithc934e4f2013-12-12 01:27:02 +0000134 unsigned OutDiag = 0;
135
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000136 if (VD->getType()->isVariablyModifiedType())
John McCallcf819ab2010-05-12 00:58:13 +0000137 InDiag = diag::note_protected_by_vla;
138
John McCall31168b02011-06-15 23:02:42 +0000139 if (VD->hasAttr<BlocksAttr>())
140 return ScopePair(diag::note_protected_by___block,
141 diag::note_exits___block);
142
143 if (VD->hasAttr<CleanupAttr>())
144 return ScopePair(diag::note_protected_by_cleanup,
145 diag::note_exits_cleanup);
146
Richard Smithc934e4f2013-12-12 01:27:02 +0000147 if (VD->hasLocalStorage()) {
148 switch (VD->getType().isDestructedType()) {
149 case QualType::DK_objc_strong_lifetime:
John McCall039f2bb2015-10-21 18:06:38 +0000150 return ScopePair(diag::note_protected_by_objc_strong_init,
151 diag::note_exits_objc_strong);
152
Richard Smithc934e4f2013-12-12 01:27:02 +0000153 case QualType::DK_objc_weak_lifetime:
John McCall039f2bb2015-10-21 18:06:38 +0000154 return ScopePair(diag::note_protected_by_objc_weak_init,
155 diag::note_exits_objc_weak);
Richard Smithc934e4f2013-12-12 01:27:02 +0000156
Akira Hatanaka7275da02018-02-28 07:15:55 +0000157 case QualType::DK_nontrivial_c_struct:
158 return ScopePair(diag::note_protected_by_non_trivial_c_struct_init,
159 diag::note_exits_dtor);
160
Richard Smithc934e4f2013-12-12 01:27:02 +0000161 case QualType::DK_cxx_destructor:
162 OutDiag = diag::note_exits_dtor;
163 break;
164
165 case QualType::DK_none:
166 break;
John McCall31168b02011-06-15 23:02:42 +0000167 }
168 }
169
Richard Smithc934e4f2013-12-12 01:27:02 +0000170 const Expr *Init = VD->getInit();
171 if (S.Context.getLangOpts().CPlusPlus && VD->hasLocalStorage() && Init) {
Richard Smithfe2750d2011-10-20 21:42:12 +0000172 // C++11 [stmt.dcl]p3:
John McCall31168b02011-06-15 23:02:42 +0000173 // A program that jumps from a point where a variable with automatic
174 // storage duration is not in scope to a point where it is in scope
175 // is ill-formed unless the variable has scalar type, class type with
Hubert Tong64c2f5a2015-06-04 22:53:21 +0000176 // a trivial default constructor and a trivial destructor, a
John McCall31168b02011-06-15 23:02:42 +0000177 // cv-qualified version of one of these types, or an array of one of
178 // the preceding types and is declared without an initializer.
179
180 // C++03 [stmt.dcl.p3:
181 // A program that jumps from a point where a local variable
182 // with automatic storage duration is not in scope to a point
183 // where it is in scope is ill-formed unless the variable has
184 // POD type and is declared without an initializer.
185
Richard Smithc934e4f2013-12-12 01:27:02 +0000186 InDiag = diag::note_protected_by_variable_init;
John McCall31168b02011-06-15 23:02:42 +0000187
Richard Smithc934e4f2013-12-12 01:27:02 +0000188 // For a variable of (array of) class type declared without an
189 // initializer, we will have call-style initialization and the initializer
190 // will be the CXXConstructExpr with no intervening nodes.
191 if (const CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(Init)) {
192 const CXXConstructorDecl *Ctor = CCE->getConstructor();
193 if (Ctor->isTrivial() && Ctor->isDefaultConstructor() &&
194 VD->getInitStyle() == VarDecl::CallInit) {
Rafael Espindola44938a72012-10-28 02:44:03 +0000195 if (OutDiag)
196 InDiag = diag::note_protected_by_variable_nontriv_destructor;
Richard Smithc934e4f2013-12-12 01:27:02 +0000197 else if (!Ctor->getParent()->isPOD())
Rafael Espindola44938a72012-10-28 02:44:03 +0000198 InDiag = diag::note_protected_by_variable_non_pod;
Richard Smithc934e4f2013-12-12 01:27:02 +0000199 else
200 InDiag = 0;
Douglas Gregor27d0c442011-05-27 16:05:29 +0000201 }
Douglas Gregor5a5fcd82010-07-01 00:21:21 +0000202 }
John McCallcf819ab2010-05-12 00:58:13 +0000203 }
Rafael Espindola44938a72012-10-28 02:44:03 +0000204
Richard Smithc934e4f2013-12-12 01:27:02 +0000205 return ScopePair(InDiag, OutDiag);
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000206 }
Mike Stump11289f42009-09-09 15:08:12 +0000207
Richard Smithc934e4f2013-12-12 01:27:02 +0000208 if (const TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCallcf819ab2010-05-12 00:58:13 +0000209 if (TD->getUnderlyingType()->isVariablyModifiedType())
Richard Smithc934e4f2013-12-12 01:27:02 +0000210 return ScopePair(isa<TypedefDecl>(TD)
211 ? diag::note_protected_by_vla_typedef
212 : diag::note_protected_by_vla_type_alias,
213 0);
Richard Smithdda56e42011-04-15 14:24:37 +0000214 }
215
John McCall31168b02011-06-15 23:02:42 +0000216 return ScopePair(0U, 0U);
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000217}
218
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000219/// \brief Build scope information for a declaration that is part of a DeclStmt.
220void JumpScopeChecker::BuildScopeInformation(Decl *D, unsigned &ParentScope) {
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000221 // If this decl causes a new scope, push and switch to it.
Richard Smithc934e4f2013-12-12 01:27:02 +0000222 std::pair<unsigned,unsigned> Diags = GetDiagForGotoScopeDecl(S, D);
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000223 if (Diags.first || Diags.second) {
224 Scopes.push_back(GotoScope(ParentScope, Diags.first, Diags.second,
225 D->getLocation()));
226 ParentScope = Scopes.size()-1;
227 }
Hubert Tong64c2f5a2015-06-04 22:53:21 +0000228
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000229 // If the decl has an initializer, walk it with the potentially new
230 // scope we just installed.
231 if (VarDecl *VD = dyn_cast<VarDecl>(D))
232 if (Expr *Init = VD->getInit())
233 BuildScopeInformation(Init, ParentScope);
234}
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000235
Fariborz Jahanian256d39d2011-07-11 18:04:54 +0000236/// \brief Build scope information for a captured block literal variables.
Hubert Tong64c2f5a2015-06-04 22:53:21 +0000237void JumpScopeChecker::BuildScopeInformation(VarDecl *D,
238 const BlockDecl *BDecl,
Fariborz Jahanian256d39d2011-07-11 18:04:54 +0000239 unsigned &ParentScope) {
240 // exclude captured __block variables; there's no destructor
241 // associated with the block literal for them.
242 if (D->hasAttr<BlocksAttr>())
243 return;
244 QualType T = D->getType();
245 QualType::DestructionKind destructKind = T.isDestructedType();
246 if (destructKind != QualType::DK_none) {
247 std::pair<unsigned,unsigned> Diags;
248 switch (destructKind) {
249 case QualType::DK_cxx_destructor:
250 Diags = ScopePair(diag::note_enters_block_captures_cxx_obj,
251 diag::note_exits_block_captures_cxx_obj);
252 break;
253 case QualType::DK_objc_strong_lifetime:
254 Diags = ScopePair(diag::note_enters_block_captures_strong,
255 diag::note_exits_block_captures_strong);
256 break;
257 case QualType::DK_objc_weak_lifetime:
258 Diags = ScopePair(diag::note_enters_block_captures_weak,
259 diag::note_exits_block_captures_weak);
260 break;
Akira Hatanaka7275da02018-02-28 07:15:55 +0000261 case QualType::DK_nontrivial_c_struct:
262 Diags = ScopePair(diag::note_enters_block_captures_non_trivial_c_struct,
263 diag::note_exits_block_captures_non_trivial_c_struct);
264 break;
Fariborz Jahanian256d39d2011-07-11 18:04:54 +0000265 case QualType::DK_none:
Richard Smithfe2750d2011-10-20 21:42:12 +0000266 llvm_unreachable("non-lifetime captured variable");
Fariborz Jahanian256d39d2011-07-11 18:04:54 +0000267 }
268 SourceLocation Loc = D->getLocation();
269 if (Loc.isInvalid())
270 Loc = BDecl->getLocation();
Hubert Tong64c2f5a2015-06-04 22:53:21 +0000271 Scopes.push_back(GotoScope(ParentScope,
Fariborz Jahanian256d39d2011-07-11 18:04:54 +0000272 Diags.first, Diags.second, Loc));
273 ParentScope = Scopes.size()-1;
274 }
275}
276
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000277/// BuildScopeInformation - The statements from CI to CE are known to form a
278/// coherent VLA scope with a specified parent node. Walk through the
279/// statements, adding any labels or gotos to LabelAndGotoScopes and recursively
280/// walking the AST as needed.
Richard Smith8b65f102016-06-21 20:10:11 +0000281void JumpScopeChecker::BuildScopeInformation(Stmt *S,
282 unsigned &origParentScope) {
Fariborz Jahanian256d39d2011-07-11 18:04:54 +0000283 // If this is a statement, rather than an expression, scopes within it don't
284 // propagate out into the enclosing scope. Otherwise we have to worry
285 // about block literals, which have the lifetime of their enclosing statement.
286 unsigned independentParentScope = origParentScope;
Hubert Tong64c2f5a2015-06-04 22:53:21 +0000287 unsigned &ParentScope = ((isa<Expr>(S) && !isa<StmtExpr>(S))
Fariborz Jahanian256d39d2011-07-11 18:04:54 +0000288 ? origParentScope : independentParentScope);
289
Richard Smitha547eb22016-07-14 00:11:03 +0000290 unsigned StmtsToSkip = 0u;
Hubert Tong64c2f5a2015-06-04 22:53:21 +0000291
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000292 // If we found a label, remember that it is in ParentScope scope.
John McCallcf819ab2010-05-12 00:58:13 +0000293 switch (S->getStmtClass()) {
John McCallcf819ab2010-05-12 00:58:13 +0000294 case Stmt::AddrLabelExprClass:
295 IndirectJumpTargets.push_back(cast<AddrLabelExpr>(S)->getLabel());
296 break;
297
Akira Hatanaka39013772017-04-19 17:54:08 +0000298 case Stmt::ObjCForCollectionStmtClass: {
299 auto *CS = cast<ObjCForCollectionStmt>(S);
300 unsigned Diag = diag::note_protected_by_objc_fast_enumeration;
301 unsigned NewParentScope = Scopes.size();
302 Scopes.push_back(GotoScope(ParentScope, Diag, 0, S->getLocStart()));
303 BuildScopeInformation(CS->getBody(), NewParentScope);
304 return;
305 }
306
John McCallcf819ab2010-05-12 00:58:13 +0000307 case Stmt::IndirectGotoStmtClass:
John McCall9de91602010-10-28 08:53:48 +0000308 // "goto *&&lbl;" is a special case which we treat as equivalent
309 // to a normal goto. In addition, we don't calculate scope in the
310 // operand (to avoid recording the address-of-label use), which
311 // works only because of the restricted set of expressions which
312 // we detect as constant targets.
313 if (cast<IndirectGotoStmt>(S)->getConstantTarget()) {
314 LabelAndGotoScopes[S] = ParentScope;
315 Jumps.push_back(S);
316 return;
317 }
318
John McCallcf819ab2010-05-12 00:58:13 +0000319 LabelAndGotoScopes[S] = ParentScope;
320 IndirectJumps.push_back(cast<IndirectGotoStmt>(S));
321 break;
322
John McCallcf819ab2010-05-12 00:58:13 +0000323 case Stmt::SwitchStmtClass:
Richard Smitha547eb22016-07-14 00:11:03 +0000324 // Evaluate the C++17 init stmt and condition variable
325 // before entering the scope of the switch statement.
326 if (Stmt *Init = cast<SwitchStmt>(S)->getInit()) {
327 BuildScopeInformation(Init, ParentScope);
328 ++StmtsToSkip;
329 }
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000330 if (VarDecl *Var = cast<SwitchStmt>(S)->getConditionVariable()) {
331 BuildScopeInformation(Var, ParentScope);
Richard Smitha547eb22016-07-14 00:11:03 +0000332 ++StmtsToSkip;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000333 }
Adrian Prantlf3b3ccd2017-12-19 22:06:11 +0000334 LLVM_FALLTHROUGH;
Hubert Tong64c2f5a2015-06-04 22:53:21 +0000335
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000336 case Stmt::GotoStmtClass:
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000337 // Remember both what scope a goto is in as well as the fact that we have
338 // it. This makes the second scan not have to walk the AST again.
339 LabelAndGotoScopes[S] = ParentScope;
340 Jumps.push_back(S);
John McCallcf819ab2010-05-12 00:58:13 +0000341 break;
342
Richard Smithb130fe72016-06-23 19:16:49 +0000343 case Stmt::IfStmtClass: {
344 IfStmt *IS = cast<IfStmt>(S);
Erik Pilkington5cd57172016-08-16 17:44:11 +0000345 if (!(IS->isConstexpr() || IS->isObjCAvailabilityCheck()))
Richard Smithb130fe72016-06-23 19:16:49 +0000346 break;
347
Erik Pilkington5cd57172016-08-16 17:44:11 +0000348 unsigned Diag = IS->isConstexpr() ? diag::note_protected_by_constexpr_if
349 : diag::note_protected_by_if_available;
350
Richard Smithb130fe72016-06-23 19:16:49 +0000351 if (VarDecl *Var = IS->getConditionVariable())
352 BuildScopeInformation(Var, ParentScope);
353
354 // Cannot jump into the middle of the condition.
355 unsigned NewParentScope = Scopes.size();
Erik Pilkington5cd57172016-08-16 17:44:11 +0000356 Scopes.push_back(GotoScope(ParentScope, Diag, 0, IS->getLocStart()));
Richard Smithb130fe72016-06-23 19:16:49 +0000357 BuildScopeInformation(IS->getCond(), NewParentScope);
358
359 // Jumps into either arm of an 'if constexpr' are not allowed.
360 NewParentScope = Scopes.size();
Erik Pilkington5cd57172016-08-16 17:44:11 +0000361 Scopes.push_back(GotoScope(ParentScope, Diag, 0, IS->getLocStart()));
Richard Smithb130fe72016-06-23 19:16:49 +0000362 BuildScopeInformation(IS->getThen(), NewParentScope);
363 if (Stmt *Else = IS->getElse()) {
364 NewParentScope = Scopes.size();
Erik Pilkington5cd57172016-08-16 17:44:11 +0000365 Scopes.push_back(GotoScope(ParentScope, Diag, 0, IS->getLocStart()));
Richard Smithb130fe72016-06-23 19:16:49 +0000366 BuildScopeInformation(Else, NewParentScope);
367 }
368 return;
369 }
370
Eli Friedman1e95d4b2012-10-31 23:55:28 +0000371 case Stmt::CXXTryStmtClass: {
372 CXXTryStmt *TS = cast<CXXTryStmt>(S);
Richard Smith8b65f102016-06-21 20:10:11 +0000373 {
374 unsigned NewParentScope = Scopes.size();
375 Scopes.push_back(GotoScope(ParentScope,
376 diag::note_protected_by_cxx_try,
377 diag::note_exits_cxx_try,
378 TS->getSourceRange().getBegin()));
379 if (Stmt *TryBlock = TS->getTryBlock())
380 BuildScopeInformation(TryBlock, NewParentScope);
381 }
Eli Friedman1e95d4b2012-10-31 23:55:28 +0000382
383 // Jump from the catch into the try is not allowed either.
384 for (unsigned I = 0, E = TS->getNumHandlers(); I != E; ++I) {
385 CXXCatchStmt *CS = TS->getHandler(I);
Richard Smith8b65f102016-06-21 20:10:11 +0000386 unsigned NewParentScope = Scopes.size();
Eli Friedman1e95d4b2012-10-31 23:55:28 +0000387 Scopes.push_back(GotoScope(ParentScope,
388 diag::note_protected_by_cxx_catch,
389 diag::note_exits_cxx_catch,
390 CS->getSourceRange().getBegin()));
Richard Smith8b65f102016-06-21 20:10:11 +0000391 BuildScopeInformation(CS->getHandlerBlock(), NewParentScope);
Eli Friedman1e95d4b2012-10-31 23:55:28 +0000392 }
393 return;
394 }
395
Nico Weberb14f8722015-02-03 17:06:08 +0000396 case Stmt::SEHTryStmtClass: {
397 SEHTryStmt *TS = cast<SEHTryStmt>(S);
Richard Smith8b65f102016-06-21 20:10:11 +0000398 {
399 unsigned NewParentScope = Scopes.size();
400 Scopes.push_back(GotoScope(ParentScope,
401 diag::note_protected_by_seh_try,
402 diag::note_exits_seh_try,
403 TS->getSourceRange().getBegin()));
404 if (Stmt *TryBlock = TS->getTryBlock())
405 BuildScopeInformation(TryBlock, NewParentScope);
406 }
Nico Weberb14f8722015-02-03 17:06:08 +0000407
408 // Jump from __except or __finally into the __try are not allowed either.
409 if (SEHExceptStmt *Except = TS->getExceptHandler()) {
Richard Smith8b65f102016-06-21 20:10:11 +0000410 unsigned NewParentScope = Scopes.size();
Nico Weberb14f8722015-02-03 17:06:08 +0000411 Scopes.push_back(GotoScope(ParentScope,
412 diag::note_protected_by_seh_except,
413 diag::note_exits_seh_except,
414 Except->getSourceRange().getBegin()));
Richard Smith8b65f102016-06-21 20:10:11 +0000415 BuildScopeInformation(Except->getBlock(), NewParentScope);
Nico Weberb14f8722015-02-03 17:06:08 +0000416 } else if (SEHFinallyStmt *Finally = TS->getFinallyHandler()) {
Richard Smith8b65f102016-06-21 20:10:11 +0000417 unsigned NewParentScope = Scopes.size();
Nico Weberb14f8722015-02-03 17:06:08 +0000418 Scopes.push_back(GotoScope(ParentScope,
419 diag::note_protected_by_seh_finally,
420 diag::note_exits_seh_finally,
421 Finally->getSourceRange().getBegin()));
Richard Smith8b65f102016-06-21 20:10:11 +0000422 BuildScopeInformation(Finally->getBlock(), NewParentScope);
Nico Weberb14f8722015-02-03 17:06:08 +0000423 }
424
425 return;
426 }
Reid Kleckner1d59f992015-01-22 01:36:17 +0000427
Richard Smith8b65f102016-06-21 20:10:11 +0000428 case Stmt::DeclStmtClass: {
429 // If this is a declstmt with a VLA definition, it defines a scope from here
430 // to the end of the containing context.
431 DeclStmt *DS = cast<DeclStmt>(S);
432 // The decl statement creates a scope if any of the decls in it are VLAs
433 // or have the cleanup attribute.
434 for (auto *I : DS->decls())
435 BuildScopeInformation(I, origParentScope);
436 return;
437 }
438
439 case Stmt::ObjCAtTryStmtClass: {
440 // Disallow jumps into any part of an @try statement by pushing a scope and
441 // walking all sub-stmts in that scope.
442 ObjCAtTryStmt *AT = cast<ObjCAtTryStmt>(S);
443 // Recursively walk the AST for the @try part.
444 {
445 unsigned NewParentScope = Scopes.size();
446 Scopes.push_back(GotoScope(ParentScope,
447 diag::note_protected_by_objc_try,
448 diag::note_exits_objc_try,
449 AT->getAtTryLoc()));
450 if (Stmt *TryPart = AT->getTryBody())
451 BuildScopeInformation(TryPart, NewParentScope);
452 }
453
454 // Jump from the catch to the finally or try is not valid.
455 for (unsigned I = 0, N = AT->getNumCatchStmts(); I != N; ++I) {
456 ObjCAtCatchStmt *AC = AT->getCatchStmt(I);
457 unsigned NewParentScope = Scopes.size();
458 Scopes.push_back(GotoScope(ParentScope,
459 diag::note_protected_by_objc_catch,
460 diag::note_exits_objc_catch,
461 AC->getAtCatchLoc()));
462 // @catches are nested and it isn't
463 BuildScopeInformation(AC->getCatchBody(), NewParentScope);
464 }
465
466 // Jump from the finally to the try or catch is not valid.
467 if (ObjCAtFinallyStmt *AF = AT->getFinallyStmt()) {
468 unsigned NewParentScope = Scopes.size();
469 Scopes.push_back(GotoScope(ParentScope,
470 diag::note_protected_by_objc_finally,
471 diag::note_exits_objc_finally,
472 AF->getAtFinallyLoc()));
473 BuildScopeInformation(AF, NewParentScope);
474 }
475
476 return;
477 }
478
479 case Stmt::ObjCAtSynchronizedStmtClass: {
480 // Disallow jumps into the protected statement of an @synchronized, but
481 // allow jumps into the object expression it protects.
482 ObjCAtSynchronizedStmt *AS = cast<ObjCAtSynchronizedStmt>(S);
483 // Recursively walk the AST for the @synchronized object expr, it is
484 // evaluated in the normal scope.
485 BuildScopeInformation(AS->getSynchExpr(), ParentScope);
486
487 // Recursively walk the AST for the @synchronized part, protected by a new
488 // scope.
489 unsigned NewParentScope = Scopes.size();
490 Scopes.push_back(GotoScope(ParentScope,
491 diag::note_protected_by_objc_synchronized,
492 diag::note_exits_objc_synchronized,
493 AS->getAtSynchronizedLoc()));
494 BuildScopeInformation(AS->getSynchBody(), NewParentScope);
495 return;
496 }
497
498 case Stmt::ObjCAutoreleasePoolStmtClass: {
499 // Disallow jumps into the protected statement of an @autoreleasepool.
500 ObjCAutoreleasePoolStmt *AS = cast<ObjCAutoreleasePoolStmt>(S);
501 // Recursively walk the AST for the @autoreleasepool part, protected by a
502 // new scope.
503 unsigned NewParentScope = Scopes.size();
504 Scopes.push_back(GotoScope(ParentScope,
505 diag::note_protected_by_objc_autoreleasepool,
506 diag::note_exits_objc_autoreleasepool,
507 AS->getAtLoc()));
508 BuildScopeInformation(AS->getSubStmt(), NewParentScope);
509 return;
510 }
511
512 case Stmt::ExprWithCleanupsClass: {
513 // Disallow jumps past full-expressions that use blocks with
514 // non-trivial cleanups of their captures. This is theoretically
515 // implementable but a lot of work which we haven't felt up to doing.
516 ExprWithCleanups *EWC = cast<ExprWithCleanups>(S);
517 for (unsigned i = 0, e = EWC->getNumObjects(); i != e; ++i) {
518 const BlockDecl *BDecl = EWC->getObject(i);
519 for (const auto &CI : BDecl->captures()) {
520 VarDecl *variable = CI.getVariable();
521 BuildScopeInformation(variable, BDecl, origParentScope);
522 }
523 }
524 break;
525 }
526
527 case Stmt::MaterializeTemporaryExprClass: {
528 // Disallow jumps out of scopes containing temporaries lifetime-extended to
529 // automatic storage duration.
530 MaterializeTemporaryExpr *MTE = cast<MaterializeTemporaryExpr>(S);
531 if (MTE->getStorageDuration() == SD_Automatic) {
532 SmallVector<const Expr *, 4> CommaLHS;
533 SmallVector<SubobjectAdjustment, 4> Adjustments;
534 const Expr *ExtendedObject =
535 MTE->GetTemporaryExpr()->skipRValueSubobjectAdjustments(
536 CommaLHS, Adjustments);
537 if (ExtendedObject->getType().isDestructedType()) {
538 Scopes.push_back(GotoScope(ParentScope, 0,
539 diag::note_exits_temporary_dtor,
540 ExtendedObject->getExprLoc()));
541 origParentScope = Scopes.size()-1;
542 }
543 }
544 break;
545 }
546
547 case Stmt::CaseStmtClass:
548 case Stmt::DefaultStmtClass:
549 case Stmt::LabelStmtClass:
550 LabelAndGotoScopes[S] = ParentScope;
551 break;
552
John McCallcf819ab2010-05-12 00:58:13 +0000553 default:
554 break;
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000555 }
Mike Stump11289f42009-09-09 15:08:12 +0000556
Benjamin Kramer642f1732015-07-02 21:03:14 +0000557 for (Stmt *SubStmt : S->children()) {
Richard Smitha547eb22016-07-14 00:11:03 +0000558 if (!SubStmt)
559 continue;
560 if (StmtsToSkip) {
561 --StmtsToSkip;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000562 continue;
563 }
Hubert Tong64c2f5a2015-06-04 22:53:21 +0000564
John McCall4a33fa92010-08-02 23:33:14 +0000565 // Cases, labels, and defaults aren't "scope parents". It's also
566 // important to handle these iteratively instead of recursively in
567 // order to avoid blowing out the stack.
568 while (true) {
569 Stmt *Next;
Vitaly Buka28a1b8c2016-10-26 02:00:00 +0000570 if (SwitchCase *SC = dyn_cast<SwitchCase>(SubStmt))
571 Next = SC->getSubStmt();
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000572 else if (LabelStmt *LS = dyn_cast<LabelStmt>(SubStmt))
573 Next = LS->getSubStmt();
John McCall4a33fa92010-08-02 23:33:14 +0000574 else
575 break;
576
577 LabelAndGotoScopes[SubStmt] = ParentScope;
578 SubStmt = Next;
579 }
580
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000581 // Recursively walk the AST.
582 BuildScopeInformation(SubStmt, ParentScope);
583 }
584}
585
586/// VerifyJumps - Verify each element of the Jumps array to see if they are
587/// valid, emitting diagnostics if not.
588void JumpScopeChecker::VerifyJumps() {
589 while (!Jumps.empty()) {
590 Stmt *Jump = Jumps.pop_back_val();
Mike Stump11289f42009-09-09 15:08:12 +0000591
592 // With a goto,
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000593 if (GotoStmt *GS = dyn_cast<GotoStmt>(Jump)) {
Ehsan Akhgari31097582014-09-22 02:21:54 +0000594 // The label may not have a statement if it's coming from inline MS ASM.
595 if (GS->getLabel()->getStmt()) {
596 CheckJump(GS, GS->getLabel()->getStmt(), GS->getGotoLoc(),
597 diag::err_goto_into_protected_scope,
598 diag::ext_goto_into_protected_scope,
599 diag::warn_cxx98_compat_goto_into_protected_scope);
600 }
601 CheckGotoStmt(GS);
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000602 continue;
603 }
Mike Stump11289f42009-09-09 15:08:12 +0000604
John McCall9de91602010-10-28 08:53:48 +0000605 // We only get indirect gotos here when they have a constant target.
606 if (IndirectGotoStmt *IGS = dyn_cast<IndirectGotoStmt>(Jump)) {
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000607 LabelDecl *Target = IGS->getConstantTarget();
608 CheckJump(IGS, Target->getStmt(), IGS->getGotoLoc(),
Francois Pichet2f550192011-09-16 23:15:32 +0000609 diag::err_goto_into_protected_scope,
Richard Smith1b98ccc2014-07-19 01:39:17 +0000610 diag::ext_goto_into_protected_scope,
Richard Smithfe2750d2011-10-20 21:42:12 +0000611 diag::warn_cxx98_compat_goto_into_protected_scope);
John McCall9de91602010-10-28 08:53:48 +0000612 continue;
613 }
614
John McCallcf819ab2010-05-12 00:58:13 +0000615 SwitchStmt *SS = cast<SwitchStmt>(Jump);
616 for (SwitchCase *SC = SS->getSwitchCaseList(); SC;
617 SC = SC->getNextSwitchCase()) {
Alp Tokere265cf12014-05-09 08:40:10 +0000618 if (CHECK_PERMISSIVE(!LabelAndGotoScopes.count(SC)))
619 continue;
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000620 SourceLocation Loc;
621 if (CaseStmt *CS = dyn_cast<CaseStmt>(SC))
622 Loc = CS->getLocStart();
623 else if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC))
624 Loc = DS->getLocStart();
625 else
626 Loc = SC->getLocStart();
627 CheckJump(SS, SC, Loc, diag::err_switch_into_protected_scope, 0,
Richard Smithfe2750d2011-10-20 21:42:12 +0000628 diag::warn_cxx98_compat_switch_into_protected_scope);
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000629 }
630 }
631}
632
John McCall42f9f1f2010-05-12 02:37:54 +0000633/// VerifyIndirectJumps - Verify whether any possible indirect jump
634/// might cross a protection boundary. Unlike direct jumps, indirect
635/// jumps count cleanups as protection boundaries: since there's no
636/// way to know where the jump is going, we can't implicitly run the
637/// right cleanups the way we can with direct jumps.
John McCallcf819ab2010-05-12 00:58:13 +0000638///
John McCall42f9f1f2010-05-12 02:37:54 +0000639/// Thus, an indirect jump is "trivial" if it bypasses no
640/// initializations and no teardowns. More formally, an indirect jump
641/// from A to B is trivial if the path out from A to DCA(A,B) is
642/// trivial and the path in from DCA(A,B) to B is trivial, where
643/// DCA(A,B) is the deepest common ancestor of A and B.
644/// Jump-triviality is transitive but asymmetric.
645///
John McCallcf819ab2010-05-12 00:58:13 +0000646/// A path in is trivial if none of the entered scopes have an InDiag.
647/// A path out is trivial is none of the exited scopes have an OutDiag.
John McCall42f9f1f2010-05-12 02:37:54 +0000648///
649/// Under these definitions, this function checks that the indirect
650/// jump between A and B is trivial for every indirect goto statement A
651/// and every label B whose address was taken in the function.
John McCallcf819ab2010-05-12 00:58:13 +0000652void JumpScopeChecker::VerifyIndirectJumps() {
653 if (IndirectJumps.empty()) return;
654
655 // If there aren't any address-of-label expressions in this function,
656 // complain about the first indirect goto.
657 if (IndirectJumpTargets.empty()) {
658 S.Diag(IndirectJumps[0]->getGotoLoc(),
659 diag::err_indirect_goto_without_addrlabel);
660 return;
661 }
662
John McCall42f9f1f2010-05-12 02:37:54 +0000663 // Collect a single representative of every scope containing an
664 // indirect goto. For most code bases, this substantially cuts
665 // down on the number of jump sites we'll have to consider later.
John McCallcf819ab2010-05-12 00:58:13 +0000666 typedef std::pair<unsigned, IndirectGotoStmt*> JumpScope;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000667 SmallVector<JumpScope, 32> JumpScopes;
John McCallcf819ab2010-05-12 00:58:13 +0000668 {
669 llvm::DenseMap<unsigned, IndirectGotoStmt*> JumpScopesMap;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000670 for (SmallVectorImpl<IndirectGotoStmt*>::iterator
John McCallcf819ab2010-05-12 00:58:13 +0000671 I = IndirectJumps.begin(), E = IndirectJumps.end(); I != E; ++I) {
672 IndirectGotoStmt *IG = *I;
Alp Tokere265cf12014-05-09 08:40:10 +0000673 if (CHECK_PERMISSIVE(!LabelAndGotoScopes.count(IG)))
674 continue;
John McCallcf819ab2010-05-12 00:58:13 +0000675 unsigned IGScope = LabelAndGotoScopes[IG];
676 IndirectGotoStmt *&Entry = JumpScopesMap[IGScope];
677 if (!Entry) Entry = IG;
678 }
679 JumpScopes.reserve(JumpScopesMap.size());
680 for (llvm::DenseMap<unsigned, IndirectGotoStmt*>::iterator
681 I = JumpScopesMap.begin(), E = JumpScopesMap.end(); I != E; ++I)
682 JumpScopes.push_back(*I);
683 }
684
John McCall42f9f1f2010-05-12 02:37:54 +0000685 // Collect a single representative of every scope containing a
686 // label whose address was taken somewhere in the function.
687 // For most code bases, there will be only one such scope.
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000688 llvm::DenseMap<unsigned, LabelDecl*> TargetScopes;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000689 for (SmallVectorImpl<LabelDecl*>::iterator
John McCallcf819ab2010-05-12 00:58:13 +0000690 I = IndirectJumpTargets.begin(), E = IndirectJumpTargets.end();
691 I != E; ++I) {
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000692 LabelDecl *TheLabel = *I;
Alp Tokere265cf12014-05-09 08:40:10 +0000693 if (CHECK_PERMISSIVE(!LabelAndGotoScopes.count(TheLabel->getStmt())))
694 continue;
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000695 unsigned LabelScope = LabelAndGotoScopes[TheLabel->getStmt()];
696 LabelDecl *&Target = TargetScopes[LabelScope];
John McCallcf819ab2010-05-12 00:58:13 +0000697 if (!Target) Target = TheLabel;
698 }
699
John McCall42f9f1f2010-05-12 02:37:54 +0000700 // For each target scope, make sure it's trivially reachable from
701 // every scope containing a jump site.
702 //
703 // A path between scopes always consists of exitting zero or more
704 // scopes, then entering zero or more scopes. We build a set of
705 // of scopes S from which the target scope can be trivially
706 // entered, then verify that every jump scope can be trivially
707 // exitted to reach a scope in S.
John McCallcf819ab2010-05-12 00:58:13 +0000708 llvm::BitVector Reachable(Scopes.size(), false);
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000709 for (llvm::DenseMap<unsigned,LabelDecl*>::iterator
John McCallcf819ab2010-05-12 00:58:13 +0000710 TI = TargetScopes.begin(), TE = TargetScopes.end(); TI != TE; ++TI) {
711 unsigned TargetScope = TI->first;
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000712 LabelDecl *TargetLabel = TI->second;
John McCallcf819ab2010-05-12 00:58:13 +0000713
714 Reachable.reset();
715
716 // Mark all the enclosing scopes from which you can safely jump
John McCall42f9f1f2010-05-12 02:37:54 +0000717 // into the target scope. 'Min' will end up being the index of
718 // the shallowest such scope.
John McCallcf819ab2010-05-12 00:58:13 +0000719 unsigned Min = TargetScope;
720 while (true) {
721 Reachable.set(Min);
722
723 // Don't go beyond the outermost scope.
724 if (Min == 0) break;
725
John McCall42f9f1f2010-05-12 02:37:54 +0000726 // Stop if we can't trivially enter the current scope.
John McCallcf819ab2010-05-12 00:58:13 +0000727 if (Scopes[Min].InDiag) break;
728
729 Min = Scopes[Min].ParentScope;
730 }
731
732 // Walk through all the jump sites, checking that they can trivially
733 // reach this label scope.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000734 for (SmallVectorImpl<JumpScope>::iterator
John McCallcf819ab2010-05-12 00:58:13 +0000735 I = JumpScopes.begin(), E = JumpScopes.end(); I != E; ++I) {
736 unsigned Scope = I->first;
737
738 // Walk out the "scope chain" for this scope, looking for a scope
John McCall42f9f1f2010-05-12 02:37:54 +0000739 // we've marked reachable. For well-formed code this amortizes
740 // to O(JumpScopes.size() / Scopes.size()): we only iterate
741 // when we see something unmarked, and in well-formed code we
742 // mark everything we iterate past.
John McCallcf819ab2010-05-12 00:58:13 +0000743 bool IsReachable = false;
744 while (true) {
745 if (Reachable.test(Scope)) {
746 // If we find something reachable, mark all the scopes we just
747 // walked through as reachable.
748 for (unsigned S = I->first; S != Scope; S = Scopes[S].ParentScope)
749 Reachable.set(S);
750 IsReachable = true;
751 break;
752 }
753
754 // Don't walk out if we've reached the top-level scope or we've
755 // gotten shallower than the shallowest reachable scope.
756 if (Scope == 0 || Scope < Min) break;
757
758 // Don't walk out through an out-diagnostic.
759 if (Scopes[Scope].OutDiag) break;
760
761 Scope = Scopes[Scope].ParentScope;
762 }
763
764 // Only diagnose if we didn't find something.
765 if (IsReachable) continue;
766
767 DiagnoseIndirectJump(I->second, I->first, TargetLabel, TargetScope);
768 }
769 }
770}
771
Richard Smithfe2750d2011-10-20 21:42:12 +0000772/// Return true if a particular error+note combination must be downgraded to a
773/// warning in Microsoft mode.
774static bool IsMicrosoftJumpWarning(unsigned JumpDiag, unsigned InDiagNote) {
775 return (JumpDiag == diag::err_goto_into_protected_scope &&
776 (InDiagNote == diag::note_protected_by_variable_init ||
777 InDiagNote == diag::note_protected_by_variable_nontriv_destructor));
778}
779
780/// Return true if a particular note should be downgraded to a compatibility
781/// warning in C++11 mode.
782static bool IsCXX98CompatWarning(Sema &S, unsigned InDiagNote) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000783 return S.getLangOpts().CPlusPlus11 &&
Richard Smithfe2750d2011-10-20 21:42:12 +0000784 InDiagNote == diag::note_protected_by_variable_non_pod;
785}
786
787/// Produce primary diagnostic for an indirect jump statement.
788static void DiagnoseIndirectJumpStmt(Sema &S, IndirectGotoStmt *Jump,
789 LabelDecl *Target, bool &Diagnosed) {
790 if (Diagnosed)
791 return;
792 S.Diag(Jump->getGotoLoc(), diag::err_indirect_goto_in_protected_scope);
793 S.Diag(Target->getStmt()->getIdentLoc(), diag::note_indirect_goto_target);
794 Diagnosed = true;
795}
796
797/// Produce note diagnostics for a jump into a protected scope.
Bill Wendling8ac06af2012-02-22 09:38:11 +0000798void JumpScopeChecker::NoteJumpIntoScopes(ArrayRef<unsigned> ToScopes) {
Alp Tokere265cf12014-05-09 08:40:10 +0000799 if (CHECK_PERMISSIVE(ToScopes.empty()))
800 return;
Richard Smithfe2750d2011-10-20 21:42:12 +0000801 for (unsigned I = 0, E = ToScopes.size(); I != E; ++I)
802 if (Scopes[ToScopes[I]].InDiag)
803 S.Diag(Scopes[ToScopes[I]].Loc, Scopes[ToScopes[I]].InDiag);
804}
805
John McCall42f9f1f2010-05-12 02:37:54 +0000806/// Diagnose an indirect jump which is known to cross scopes.
John McCallcf819ab2010-05-12 00:58:13 +0000807void JumpScopeChecker::DiagnoseIndirectJump(IndirectGotoStmt *Jump,
808 unsigned JumpScope,
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000809 LabelDecl *Target,
John McCallcf819ab2010-05-12 00:58:13 +0000810 unsigned TargetScope) {
Alp Tokere265cf12014-05-09 08:40:10 +0000811 if (CHECK_PERMISSIVE(JumpScope == TargetScope))
812 return;
John McCallcf819ab2010-05-12 00:58:13 +0000813
John McCall42f9f1f2010-05-12 02:37:54 +0000814 unsigned Common = GetDeepestCommonScope(JumpScope, TargetScope);
Richard Smithfe2750d2011-10-20 21:42:12 +0000815 bool Diagnosed = false;
John McCallcf819ab2010-05-12 00:58:13 +0000816
John McCall42f9f1f2010-05-12 02:37:54 +0000817 // Walk out the scope chain until we reach the common ancestor.
818 for (unsigned I = JumpScope; I != Common; I = Scopes[I].ParentScope)
Richard Smithfe2750d2011-10-20 21:42:12 +0000819 if (Scopes[I].OutDiag) {
820 DiagnoseIndirectJumpStmt(S, Jump, Target, Diagnosed);
John McCall42f9f1f2010-05-12 02:37:54 +0000821 S.Diag(Scopes[I].Loc, Scopes[I].OutDiag);
Richard Smithfe2750d2011-10-20 21:42:12 +0000822 }
823
824 SmallVector<unsigned, 10> ToScopesCXX98Compat;
John McCallcf819ab2010-05-12 00:58:13 +0000825
826 // Now walk into the scopes containing the label whose address was taken.
John McCall42f9f1f2010-05-12 02:37:54 +0000827 for (unsigned I = TargetScope; I != Common; I = Scopes[I].ParentScope)
Richard Smithfe2750d2011-10-20 21:42:12 +0000828 if (IsCXX98CompatWarning(S, Scopes[I].InDiag))
829 ToScopesCXX98Compat.push_back(I);
830 else if (Scopes[I].InDiag) {
831 DiagnoseIndirectJumpStmt(S, Jump, Target, Diagnosed);
John McCall42f9f1f2010-05-12 02:37:54 +0000832 S.Diag(Scopes[I].Loc, Scopes[I].InDiag);
Richard Smithfe2750d2011-10-20 21:42:12 +0000833 }
John McCallcf819ab2010-05-12 00:58:13 +0000834
Richard Smithfe2750d2011-10-20 21:42:12 +0000835 // Diagnose this jump if it would be ill-formed in C++98.
836 if (!Diagnosed && !ToScopesCXX98Compat.empty()) {
837 S.Diag(Jump->getGotoLoc(),
838 diag::warn_cxx98_compat_indirect_goto_in_protected_scope);
839 S.Diag(Target->getStmt()->getIdentLoc(), diag::note_indirect_goto_target);
840 NoteJumpIntoScopes(ToScopesCXX98Compat);
841 }
Francois Pichet051f5e52011-09-13 10:26:51 +0000842}
843
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000844/// CheckJump - Validate that the specified jump statement is valid: that it is
845/// jumping within or out of its current scope, not into a deeper one.
Francois Pichet051f5e52011-09-13 10:26:51 +0000846void JumpScopeChecker::CheckJump(Stmt *From, Stmt *To, SourceLocation DiagLoc,
Richard Smithfe2750d2011-10-20 21:42:12 +0000847 unsigned JumpDiagError, unsigned JumpDiagWarning,
848 unsigned JumpDiagCXX98Compat) {
Alp Tokere265cf12014-05-09 08:40:10 +0000849 if (CHECK_PERMISSIVE(!LabelAndGotoScopes.count(From)))
850 return;
851 if (CHECK_PERMISSIVE(!LabelAndGotoScopes.count(To)))
852 return;
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000853
Alp Tokere265cf12014-05-09 08:40:10 +0000854 unsigned FromScope = LabelAndGotoScopes[From];
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000855 unsigned ToScope = LabelAndGotoScopes[To];
Mike Stump11289f42009-09-09 15:08:12 +0000856
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000857 // Common case: exactly the same scope, which is fine.
858 if (FromScope == ToScope) return;
Mike Stump11289f42009-09-09 15:08:12 +0000859
Nico Webereb0cfb52015-03-09 04:27:56 +0000860 // Warn on gotos out of __finally blocks.
861 if (isa<GotoStmt>(From) || isa<IndirectGotoStmt>(From)) {
862 // If FromScope > ToScope, FromScope is more nested and the jump goes to a
863 // less nested scope. Check if it crosses a __finally along the way.
864 for (unsigned I = FromScope; I > ToScope; I = Scopes[I].ParentScope) {
865 if (Scopes[I].InDiag == diag::note_protected_by_seh_finally) {
866 S.Diag(From->getLocStart(), diag::warn_jump_out_of_seh_finally);
867 break;
868 }
869 }
870 }
871
John McCall42f9f1f2010-05-12 02:37:54 +0000872 unsigned CommonScope = GetDeepestCommonScope(FromScope, ToScope);
Mike Stump11289f42009-09-09 15:08:12 +0000873
John McCall42f9f1f2010-05-12 02:37:54 +0000874 // It's okay to jump out from a nested scope.
875 if (CommonScope == ToScope) return;
Mike Stump11289f42009-09-09 15:08:12 +0000876
John McCall42f9f1f2010-05-12 02:37:54 +0000877 // Pull out (and reverse) any scopes we might need to diagnose skipping.
Richard Smithfe2750d2011-10-20 21:42:12 +0000878 SmallVector<unsigned, 10> ToScopesCXX98Compat;
Francois Pichet051f5e52011-09-13 10:26:51 +0000879 SmallVector<unsigned, 10> ToScopesError;
880 SmallVector<unsigned, 10> ToScopesWarning;
881 for (unsigned I = ToScope; I != CommonScope; I = Scopes[I].ParentScope) {
Alp Tokerbfa39342014-01-14 12:51:41 +0000882 if (S.getLangOpts().MSVCCompat && JumpDiagWarning != 0 &&
Francois Pichet051f5e52011-09-13 10:26:51 +0000883 IsMicrosoftJumpWarning(JumpDiagError, Scopes[I].InDiag))
884 ToScopesWarning.push_back(I);
Richard Smithfe2750d2011-10-20 21:42:12 +0000885 else if (IsCXX98CompatWarning(S, Scopes[I].InDiag))
886 ToScopesCXX98Compat.push_back(I);
Francois Pichet051f5e52011-09-13 10:26:51 +0000887 else if (Scopes[I].InDiag)
888 ToScopesError.push_back(I);
889 }
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000890
Francois Pichet051f5e52011-09-13 10:26:51 +0000891 // Handle warnings.
892 if (!ToScopesWarning.empty()) {
893 S.Diag(DiagLoc, JumpDiagWarning);
Richard Smithfe2750d2011-10-20 21:42:12 +0000894 NoteJumpIntoScopes(ToScopesWarning);
Francois Pichet051f5e52011-09-13 10:26:51 +0000895 }
John McCallcf819ab2010-05-12 00:58:13 +0000896
Francois Pichet051f5e52011-09-13 10:26:51 +0000897 // Handle errors.
898 if (!ToScopesError.empty()) {
899 S.Diag(DiagLoc, JumpDiagError);
Richard Smithfe2750d2011-10-20 21:42:12 +0000900 NoteJumpIntoScopes(ToScopesError);
901 }
902
903 // Handle -Wc++98-compat warnings if the jump is well-formed.
904 if (ToScopesError.empty() && !ToScopesCXX98Compat.empty()) {
905 S.Diag(DiagLoc, JumpDiagCXX98Compat);
906 NoteJumpIntoScopes(ToScopesCXX98Compat);
Francois Pichet051f5e52011-09-13 10:26:51 +0000907 }
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000908}
909
Ehsan Akhgari31097582014-09-22 02:21:54 +0000910void JumpScopeChecker::CheckGotoStmt(GotoStmt *GS) {
911 if (GS->getLabel()->isMSAsmLabel()) {
912 S.Diag(GS->getGotoLoc(), diag::err_goto_ms_asm_label)
913 << GS->getLabel()->getIdentifier();
914 S.Diag(GS->getLabel()->getLocation(), diag::note_goto_ms_asm_label)
915 << GS->getLabel()->getIdentifier();
916 }
917}
918
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000919void Sema::DiagnoseInvalidJumps(Stmt *Body) {
Douglas Gregor120f6a62009-11-17 06:14:37 +0000920 (void)JumpScopeChecker(Body, *this);
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000921}