blob: 775fe85740d4b9be2db41a465f33e590351df01d [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:
150 case QualType::DK_objc_weak_lifetime:
Argyrios Kyrtzidiscff00d92011-06-24 00:08:59 +0000151 return ScopePair(diag::note_protected_by_objc_ownership,
152 diag::note_exits_objc_ownership);
Richard Smithc934e4f2013-12-12 01:27:02 +0000153
154 case QualType::DK_cxx_destructor:
155 OutDiag = diag::note_exits_dtor;
156 break;
157
158 case QualType::DK_none:
159 break;
John McCall31168b02011-06-15 23:02:42 +0000160 }
161 }
162
Richard Smithc934e4f2013-12-12 01:27:02 +0000163 const Expr *Init = VD->getInit();
164 if (S.Context.getLangOpts().CPlusPlus && VD->hasLocalStorage() && Init) {
Richard Smithfe2750d2011-10-20 21:42:12 +0000165 // C++11 [stmt.dcl]p3:
John McCall31168b02011-06-15 23:02:42 +0000166 // A program that jumps from a point where a variable with automatic
167 // storage duration is not in scope to a point where it is in scope
168 // is ill-formed unless the variable has scalar type, class type with
Hubert Tong64c2f5a2015-06-04 22:53:21 +0000169 // a trivial default constructor and a trivial destructor, a
John McCall31168b02011-06-15 23:02:42 +0000170 // cv-qualified version of one of these types, or an array of one of
171 // the preceding types and is declared without an initializer.
172
173 // C++03 [stmt.dcl.p3:
174 // A program that jumps from a point where a local variable
175 // with automatic storage duration is not in scope to a point
176 // where it is in scope is ill-formed unless the variable has
177 // POD type and is declared without an initializer.
178
Richard Smithc934e4f2013-12-12 01:27:02 +0000179 InDiag = diag::note_protected_by_variable_init;
John McCall31168b02011-06-15 23:02:42 +0000180
Richard Smithc934e4f2013-12-12 01:27:02 +0000181 // For a variable of (array of) class type declared without an
182 // initializer, we will have call-style initialization and the initializer
183 // will be the CXXConstructExpr with no intervening nodes.
184 if (const CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(Init)) {
185 const CXXConstructorDecl *Ctor = CCE->getConstructor();
186 if (Ctor->isTrivial() && Ctor->isDefaultConstructor() &&
187 VD->getInitStyle() == VarDecl::CallInit) {
Rafael Espindola44938a72012-10-28 02:44:03 +0000188 if (OutDiag)
189 InDiag = diag::note_protected_by_variable_nontriv_destructor;
Richard Smithc934e4f2013-12-12 01:27:02 +0000190 else if (!Ctor->getParent()->isPOD())
Rafael Espindola44938a72012-10-28 02:44:03 +0000191 InDiag = diag::note_protected_by_variable_non_pod;
Richard Smithc934e4f2013-12-12 01:27:02 +0000192 else
193 InDiag = 0;
Douglas Gregor27d0c442011-05-27 16:05:29 +0000194 }
Douglas Gregor5a5fcd82010-07-01 00:21:21 +0000195 }
John McCallcf819ab2010-05-12 00:58:13 +0000196 }
Rafael Espindola44938a72012-10-28 02:44:03 +0000197
Richard Smithc934e4f2013-12-12 01:27:02 +0000198 return ScopePair(InDiag, OutDiag);
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000199 }
Mike Stump11289f42009-09-09 15:08:12 +0000200
Richard Smithc934e4f2013-12-12 01:27:02 +0000201 if (const TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCallcf819ab2010-05-12 00:58:13 +0000202 if (TD->getUnderlyingType()->isVariablyModifiedType())
Richard Smithc934e4f2013-12-12 01:27:02 +0000203 return ScopePair(isa<TypedefDecl>(TD)
204 ? diag::note_protected_by_vla_typedef
205 : diag::note_protected_by_vla_type_alias,
206 0);
Richard Smithdda56e42011-04-15 14:24:37 +0000207 }
208
John McCall31168b02011-06-15 23:02:42 +0000209 return ScopePair(0U, 0U);
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000210}
211
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000212/// \brief Build scope information for a declaration that is part of a DeclStmt.
213void JumpScopeChecker::BuildScopeInformation(Decl *D, unsigned &ParentScope) {
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000214 // If this decl causes a new scope, push and switch to it.
Richard Smithc934e4f2013-12-12 01:27:02 +0000215 std::pair<unsigned,unsigned> Diags = GetDiagForGotoScopeDecl(S, D);
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000216 if (Diags.first || Diags.second) {
217 Scopes.push_back(GotoScope(ParentScope, Diags.first, Diags.second,
218 D->getLocation()));
219 ParentScope = Scopes.size()-1;
220 }
Hubert Tong64c2f5a2015-06-04 22:53:21 +0000221
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000222 // If the decl has an initializer, walk it with the potentially new
223 // scope we just installed.
224 if (VarDecl *VD = dyn_cast<VarDecl>(D))
225 if (Expr *Init = VD->getInit())
226 BuildScopeInformation(Init, ParentScope);
227}
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000228
Fariborz Jahanian256d39d2011-07-11 18:04:54 +0000229/// \brief Build scope information for a captured block literal variables.
Hubert Tong64c2f5a2015-06-04 22:53:21 +0000230void JumpScopeChecker::BuildScopeInformation(VarDecl *D,
231 const BlockDecl *BDecl,
Fariborz Jahanian256d39d2011-07-11 18:04:54 +0000232 unsigned &ParentScope) {
233 // exclude captured __block variables; there's no destructor
234 // associated with the block literal for them.
235 if (D->hasAttr<BlocksAttr>())
236 return;
237 QualType T = D->getType();
238 QualType::DestructionKind destructKind = T.isDestructedType();
239 if (destructKind != QualType::DK_none) {
240 std::pair<unsigned,unsigned> Diags;
241 switch (destructKind) {
242 case QualType::DK_cxx_destructor:
243 Diags = ScopePair(diag::note_enters_block_captures_cxx_obj,
244 diag::note_exits_block_captures_cxx_obj);
245 break;
246 case QualType::DK_objc_strong_lifetime:
247 Diags = ScopePair(diag::note_enters_block_captures_strong,
248 diag::note_exits_block_captures_strong);
249 break;
250 case QualType::DK_objc_weak_lifetime:
251 Diags = ScopePair(diag::note_enters_block_captures_weak,
252 diag::note_exits_block_captures_weak);
253 break;
254 case QualType::DK_none:
Richard Smithfe2750d2011-10-20 21:42:12 +0000255 llvm_unreachable("non-lifetime captured variable");
Fariborz Jahanian256d39d2011-07-11 18:04:54 +0000256 }
257 SourceLocation Loc = D->getLocation();
258 if (Loc.isInvalid())
259 Loc = BDecl->getLocation();
Hubert Tong64c2f5a2015-06-04 22:53:21 +0000260 Scopes.push_back(GotoScope(ParentScope,
Fariborz Jahanian256d39d2011-07-11 18:04:54 +0000261 Diags.first, Diags.second, Loc));
262 ParentScope = Scopes.size()-1;
263 }
264}
265
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000266/// BuildScopeInformation - The statements from CI to CE are known to form a
267/// coherent VLA scope with a specified parent node. Walk through the
268/// statements, adding any labels or gotos to LabelAndGotoScopes and recursively
269/// walking the AST as needed.
Fariborz Jahanian256d39d2011-07-11 18:04:54 +0000270void JumpScopeChecker::BuildScopeInformation(Stmt *S, unsigned &origParentScope) {
271 // If this is a statement, rather than an expression, scopes within it don't
272 // propagate out into the enclosing scope. Otherwise we have to worry
273 // about block literals, which have the lifetime of their enclosing statement.
274 unsigned independentParentScope = origParentScope;
Hubert Tong64c2f5a2015-06-04 22:53:21 +0000275 unsigned &ParentScope = ((isa<Expr>(S) && !isa<StmtExpr>(S))
Fariborz Jahanian256d39d2011-07-11 18:04:54 +0000276 ? origParentScope : independentParentScope);
277
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000278 bool SkipFirstSubStmt = false;
Hubert Tong64c2f5a2015-06-04 22:53:21 +0000279
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000280 // If we found a label, remember that it is in ParentScope scope.
John McCallcf819ab2010-05-12 00:58:13 +0000281 switch (S->getStmtClass()) {
John McCallcf819ab2010-05-12 00:58:13 +0000282 case Stmt::AddrLabelExprClass:
283 IndirectJumpTargets.push_back(cast<AddrLabelExpr>(S)->getLabel());
284 break;
285
286 case Stmt::IndirectGotoStmtClass:
John McCall9de91602010-10-28 08:53:48 +0000287 // "goto *&&lbl;" is a special case which we treat as equivalent
288 // to a normal goto. In addition, we don't calculate scope in the
289 // operand (to avoid recording the address-of-label use), which
290 // works only because of the restricted set of expressions which
291 // we detect as constant targets.
292 if (cast<IndirectGotoStmt>(S)->getConstantTarget()) {
293 LabelAndGotoScopes[S] = ParentScope;
294 Jumps.push_back(S);
295 return;
296 }
297
John McCallcf819ab2010-05-12 00:58:13 +0000298 LabelAndGotoScopes[S] = ParentScope;
299 IndirectJumps.push_back(cast<IndirectGotoStmt>(S));
300 break;
301
John McCallcf819ab2010-05-12 00:58:13 +0000302 case Stmt::SwitchStmtClass:
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000303 // Evaluate the condition variable before entering the scope of the switch
304 // statement.
305 if (VarDecl *Var = cast<SwitchStmt>(S)->getConditionVariable()) {
306 BuildScopeInformation(Var, ParentScope);
307 SkipFirstSubStmt = true;
308 }
309 // Fall through
Hubert Tong64c2f5a2015-06-04 22:53:21 +0000310
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000311 case Stmt::GotoStmtClass:
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000312 // Remember both what scope a goto is in as well as the fact that we have
313 // it. This makes the second scan not have to walk the AST again.
314 LabelAndGotoScopes[S] = ParentScope;
315 Jumps.push_back(S);
John McCallcf819ab2010-05-12 00:58:13 +0000316 break;
317
Eli Friedman1e95d4b2012-10-31 23:55:28 +0000318 case Stmt::CXXTryStmtClass: {
319 CXXTryStmt *TS = cast<CXXTryStmt>(S);
320 unsigned newParentScope;
321 Scopes.push_back(GotoScope(ParentScope,
322 diag::note_protected_by_cxx_try,
323 diag::note_exits_cxx_try,
324 TS->getSourceRange().getBegin()));
325 if (Stmt *TryBlock = TS->getTryBlock())
326 BuildScopeInformation(TryBlock, (newParentScope = Scopes.size()-1));
327
328 // Jump from the catch into the try is not allowed either.
329 for (unsigned I = 0, E = TS->getNumHandlers(); I != E; ++I) {
330 CXXCatchStmt *CS = TS->getHandler(I);
331 Scopes.push_back(GotoScope(ParentScope,
332 diag::note_protected_by_cxx_catch,
333 diag::note_exits_cxx_catch,
334 CS->getSourceRange().getBegin()));
Hubert Tong64c2f5a2015-06-04 22:53:21 +0000335 BuildScopeInformation(CS->getHandlerBlock(),
Eli Friedman1e95d4b2012-10-31 23:55:28 +0000336 (newParentScope = Scopes.size()-1));
337 }
338 return;
339 }
340
Nico Weberb14f8722015-02-03 17:06:08 +0000341 case Stmt::SEHTryStmtClass: {
342 SEHTryStmt *TS = cast<SEHTryStmt>(S);
343 unsigned newParentScope;
344 Scopes.push_back(GotoScope(ParentScope,
345 diag::note_protected_by_seh_try,
346 diag::note_exits_seh_try,
347 TS->getSourceRange().getBegin()));
348 if (Stmt *TryBlock = TS->getTryBlock())
349 BuildScopeInformation(TryBlock, (newParentScope = Scopes.size()-1));
350
351 // Jump from __except or __finally into the __try are not allowed either.
352 if (SEHExceptStmt *Except = TS->getExceptHandler()) {
353 Scopes.push_back(GotoScope(ParentScope,
354 diag::note_protected_by_seh_except,
355 diag::note_exits_seh_except,
356 Except->getSourceRange().getBegin()));
Hubert Tong64c2f5a2015-06-04 22:53:21 +0000357 BuildScopeInformation(Except->getBlock(),
Nico Weberb14f8722015-02-03 17:06:08 +0000358 (newParentScope = Scopes.size()-1));
359 } else if (SEHFinallyStmt *Finally = TS->getFinallyHandler()) {
360 Scopes.push_back(GotoScope(ParentScope,
361 diag::note_protected_by_seh_finally,
362 diag::note_exits_seh_finally,
363 Finally->getSourceRange().getBegin()));
Hubert Tong64c2f5a2015-06-04 22:53:21 +0000364 BuildScopeInformation(Finally->getBlock(),
Nico Weberb14f8722015-02-03 17:06:08 +0000365 (newParentScope = Scopes.size()-1));
366 }
367
368 return;
369 }
Reid Kleckner1d59f992015-01-22 01:36:17 +0000370
John McCallcf819ab2010-05-12 00:58:13 +0000371 default:
372 break;
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000373 }
Mike Stump11289f42009-09-09 15:08:12 +0000374
Benjamin Kramer642f1732015-07-02 21:03:14 +0000375 for (Stmt *SubStmt : S->children()) {
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000376 if (SkipFirstSubStmt) {
377 SkipFirstSubStmt = false;
378 continue;
379 }
Hubert Tong64c2f5a2015-06-04 22:53:21 +0000380
Craig Topperc3ec1492014-05-26 06:22:03 +0000381 if (!SubStmt) continue;
Mike Stump11289f42009-09-09 15:08:12 +0000382
John McCall4a33fa92010-08-02 23:33:14 +0000383 // Cases, labels, and defaults aren't "scope parents". It's also
384 // important to handle these iteratively instead of recursively in
385 // order to avoid blowing out the stack.
386 while (true) {
387 Stmt *Next;
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000388 if (CaseStmt *CS = dyn_cast<CaseStmt>(SubStmt))
389 Next = CS->getSubStmt();
390 else if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SubStmt))
391 Next = DS->getSubStmt();
392 else if (LabelStmt *LS = dyn_cast<LabelStmt>(SubStmt))
393 Next = LS->getSubStmt();
John McCall4a33fa92010-08-02 23:33:14 +0000394 else
395 break;
396
397 LabelAndGotoScopes[SubStmt] = ParentScope;
398 SubStmt = Next;
399 }
400
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000401 // If this is a declstmt with a VLA definition, it defines a scope from here
402 // to the end of the containing context.
403 if (DeclStmt *DS = dyn_cast<DeclStmt>(SubStmt)) {
Chris Lattner30d0cfd2010-03-01 20:59:53 +0000404 // The decl statement creates a scope if any of the decls in it are VLAs
405 // or have the cleanup attribute.
Aaron Ballman535bbcc2014-03-14 17:01:24 +0000406 for (auto *I : DS->decls())
407 BuildScopeInformation(I, ParentScope);
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000408 continue;
409 }
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000410 // Disallow jumps into any part of an @try statement by pushing a scope and
411 // walking all sub-stmts in that scope.
412 if (ObjCAtTryStmt *AT = dyn_cast<ObjCAtTryStmt>(SubStmt)) {
Fariborz Jahanian256d39d2011-07-11 18:04:54 +0000413 unsigned newParentScope;
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000414 // Recursively walk the AST for the @try part.
John McCallcf819ab2010-05-12 00:58:13 +0000415 Scopes.push_back(GotoScope(ParentScope,
416 diag::note_protected_by_objc_try,
417 diag::note_exits_objc_try,
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000418 AT->getAtTryLoc()));
419 if (Stmt *TryPart = AT->getTryBody())
Fariborz Jahanian256d39d2011-07-11 18:04:54 +0000420 BuildScopeInformation(TryPart, (newParentScope = Scopes.size()-1));
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000421
422 // Jump from the catch to the finally or try is not valid.
Douglas Gregor96c79492010-04-23 22:50:49 +0000423 for (unsigned I = 0, N = AT->getNumCatchStmts(); I != N; ++I) {
424 ObjCAtCatchStmt *AC = AT->getCatchStmt(I);
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000425 Scopes.push_back(GotoScope(ParentScope,
426 diag::note_protected_by_objc_catch,
John McCallcf819ab2010-05-12 00:58:13 +0000427 diag::note_exits_objc_catch,
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000428 AC->getAtCatchLoc()));
Mike Stump11289f42009-09-09 15:08:12 +0000429 // @catches are nested and it isn't
Hubert Tong64c2f5a2015-06-04 22:53:21 +0000430 BuildScopeInformation(AC->getCatchBody(),
Fariborz Jahanian256d39d2011-07-11 18:04:54 +0000431 (newParentScope = Scopes.size()-1));
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000432 }
Mike Stump11289f42009-09-09 15:08:12 +0000433
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000434 // Jump from the finally to the try or catch is not valid.
435 if (ObjCAtFinallyStmt *AF = AT->getFinallyStmt()) {
436 Scopes.push_back(GotoScope(ParentScope,
437 diag::note_protected_by_objc_finally,
John McCallcf819ab2010-05-12 00:58:13 +0000438 diag::note_exits_objc_finally,
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000439 AF->getAtFinallyLoc()));
Fariborz Jahanian256d39d2011-07-11 18:04:54 +0000440 BuildScopeInformation(AF, (newParentScope = Scopes.size()-1));
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000441 }
Mike Stump11289f42009-09-09 15:08:12 +0000442
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000443 continue;
444 }
Hubert Tong64c2f5a2015-06-04 22:53:21 +0000445
Fariborz Jahanian256d39d2011-07-11 18:04:54 +0000446 unsigned newParentScope;
Chris Lattnerc70dd562009-04-21 06:01:00 +0000447 // Disallow jumps into the protected statement of an @synchronized, but
448 // allow jumps into the object expression it protects.
Nico Weberf3e46ad2015-02-03 23:10:18 +0000449 if (ObjCAtSynchronizedStmt *AS =
450 dyn_cast<ObjCAtSynchronizedStmt>(SubStmt)) {
Chris Lattnerc70dd562009-04-21 06:01:00 +0000451 // Recursively walk the AST for the @synchronized object expr, it is
452 // evaluated in the normal scope.
453 BuildScopeInformation(AS->getSynchExpr(), ParentScope);
Mike Stump11289f42009-09-09 15:08:12 +0000454
Chris Lattnerc70dd562009-04-21 06:01:00 +0000455 // Recursively walk the AST for the @synchronized part, protected by a new
456 // scope.
457 Scopes.push_back(GotoScope(ParentScope,
458 diag::note_protected_by_objc_synchronized,
John McCallcf819ab2010-05-12 00:58:13 +0000459 diag::note_exits_objc_synchronized,
Chris Lattnerc70dd562009-04-21 06:01:00 +0000460 AS->getAtSynchronizedLoc()));
Hubert Tong64c2f5a2015-06-04 22:53:21 +0000461 BuildScopeInformation(AS->getSynchBody(),
Fariborz Jahanian256d39d2011-07-11 18:04:54 +0000462 (newParentScope = Scopes.size()-1));
Chris Lattnerc70dd562009-04-21 06:01:00 +0000463 continue;
464 }
Sebastian Redl4de47b42009-04-27 20:27:31 +0000465
John McCall31168b02011-06-15 23:02:42 +0000466 // Disallow jumps into the protected statement of an @autoreleasepool.
Nico Weberf3e46ad2015-02-03 23:10:18 +0000467 if (ObjCAutoreleasePoolStmt *AS =
468 dyn_cast<ObjCAutoreleasePoolStmt>(SubStmt)) {
469 // Recursively walk the AST for the @autoreleasepool part, protected by a
470 // new scope.
John McCall31168b02011-06-15 23:02:42 +0000471 Scopes.push_back(GotoScope(ParentScope,
472 diag::note_protected_by_objc_autoreleasepool,
473 diag::note_exits_objc_autoreleasepool,
474 AS->getAtLoc()));
Nico Weberf3e46ad2015-02-03 23:10:18 +0000475 BuildScopeInformation(AS->getSubStmt(),
476 (newParentScope = Scopes.size() - 1));
John McCall31168b02011-06-15 23:02:42 +0000477 continue;
478 }
John McCallb0433ee2012-09-25 06:56:03 +0000479
480 // Disallow jumps past full-expressions that use blocks with
481 // non-trivial cleanups of their captures. This is theoretically
482 // implementable but a lot of work which we haven't felt up to doing.
483 if (ExprWithCleanups *EWC = dyn_cast<ExprWithCleanups>(SubStmt)) {
484 for (unsigned i = 0, e = EWC->getNumObjects(); i != e; ++i) {
485 const BlockDecl *BDecl = EWC->getObject(i);
Aaron Ballman9371dd22014-03-14 18:34:04 +0000486 for (const auto &CI : BDecl->captures()) {
487 VarDecl *variable = CI.getVariable();
Fariborz Jahanian256d39d2011-07-11 18:04:54 +0000488 BuildScopeInformation(variable, BDecl, ParentScope);
489 }
John McCallb0433ee2012-09-25 06:56:03 +0000490 }
Fariborz Jahanian256d39d2011-07-11 18:04:54 +0000491 }
Richard Smithc934e4f2013-12-12 01:27:02 +0000492
493 // Disallow jumps out of scopes containing temporaries lifetime-extended to
494 // automatic storage duration.
495 if (MaterializeTemporaryExpr *MTE =
496 dyn_cast<MaterializeTemporaryExpr>(SubStmt)) {
497 if (MTE->getStorageDuration() == SD_Automatic) {
498 SmallVector<const Expr *, 4> CommaLHS;
499 SmallVector<SubobjectAdjustment, 4> Adjustments;
500 const Expr *ExtendedObject =
501 MTE->GetTemporaryExpr()->skipRValueSubobjectAdjustments(
502 CommaLHS, Adjustments);
503 if (ExtendedObject->getType().isDestructedType()) {
504 Scopes.push_back(GotoScope(ParentScope, 0,
505 diag::note_exits_temporary_dtor,
506 ExtendedObject->getExprLoc()));
507 ParentScope = Scopes.size()-1;
508 }
509 }
510 }
511
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000512 // Recursively walk the AST.
513 BuildScopeInformation(SubStmt, ParentScope);
514 }
515}
516
517/// VerifyJumps - Verify each element of the Jumps array to see if they are
518/// valid, emitting diagnostics if not.
519void JumpScopeChecker::VerifyJumps() {
520 while (!Jumps.empty()) {
521 Stmt *Jump = Jumps.pop_back_val();
Mike Stump11289f42009-09-09 15:08:12 +0000522
523 // With a goto,
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000524 if (GotoStmt *GS = dyn_cast<GotoStmt>(Jump)) {
Ehsan Akhgari31097582014-09-22 02:21:54 +0000525 // The label may not have a statement if it's coming from inline MS ASM.
526 if (GS->getLabel()->getStmt()) {
527 CheckJump(GS, GS->getLabel()->getStmt(), GS->getGotoLoc(),
528 diag::err_goto_into_protected_scope,
529 diag::ext_goto_into_protected_scope,
530 diag::warn_cxx98_compat_goto_into_protected_scope);
531 }
532 CheckGotoStmt(GS);
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000533 continue;
534 }
Mike Stump11289f42009-09-09 15:08:12 +0000535
John McCall9de91602010-10-28 08:53:48 +0000536 // We only get indirect gotos here when they have a constant target.
537 if (IndirectGotoStmt *IGS = dyn_cast<IndirectGotoStmt>(Jump)) {
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000538 LabelDecl *Target = IGS->getConstantTarget();
539 CheckJump(IGS, Target->getStmt(), IGS->getGotoLoc(),
Francois Pichet2f550192011-09-16 23:15:32 +0000540 diag::err_goto_into_protected_scope,
Richard Smith1b98ccc2014-07-19 01:39:17 +0000541 diag::ext_goto_into_protected_scope,
Richard Smithfe2750d2011-10-20 21:42:12 +0000542 diag::warn_cxx98_compat_goto_into_protected_scope);
John McCall9de91602010-10-28 08:53:48 +0000543 continue;
544 }
545
John McCallcf819ab2010-05-12 00:58:13 +0000546 SwitchStmt *SS = cast<SwitchStmt>(Jump);
547 for (SwitchCase *SC = SS->getSwitchCaseList(); SC;
548 SC = SC->getNextSwitchCase()) {
Alp Tokere265cf12014-05-09 08:40:10 +0000549 if (CHECK_PERMISSIVE(!LabelAndGotoScopes.count(SC)))
550 continue;
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000551 SourceLocation Loc;
552 if (CaseStmt *CS = dyn_cast<CaseStmt>(SC))
553 Loc = CS->getLocStart();
554 else if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC))
555 Loc = DS->getLocStart();
556 else
557 Loc = SC->getLocStart();
558 CheckJump(SS, SC, Loc, diag::err_switch_into_protected_scope, 0,
Richard Smithfe2750d2011-10-20 21:42:12 +0000559 diag::warn_cxx98_compat_switch_into_protected_scope);
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000560 }
561 }
562}
563
John McCall42f9f1f2010-05-12 02:37:54 +0000564/// VerifyIndirectJumps - Verify whether any possible indirect jump
565/// might cross a protection boundary. Unlike direct jumps, indirect
566/// jumps count cleanups as protection boundaries: since there's no
567/// way to know where the jump is going, we can't implicitly run the
568/// right cleanups the way we can with direct jumps.
John McCallcf819ab2010-05-12 00:58:13 +0000569///
John McCall42f9f1f2010-05-12 02:37:54 +0000570/// Thus, an indirect jump is "trivial" if it bypasses no
571/// initializations and no teardowns. More formally, an indirect jump
572/// from A to B is trivial if the path out from A to DCA(A,B) is
573/// trivial and the path in from DCA(A,B) to B is trivial, where
574/// DCA(A,B) is the deepest common ancestor of A and B.
575/// Jump-triviality is transitive but asymmetric.
576///
John McCallcf819ab2010-05-12 00:58:13 +0000577/// A path in is trivial if none of the entered scopes have an InDiag.
578/// A path out is trivial is none of the exited scopes have an OutDiag.
John McCall42f9f1f2010-05-12 02:37:54 +0000579///
580/// Under these definitions, this function checks that the indirect
581/// jump between A and B is trivial for every indirect goto statement A
582/// and every label B whose address was taken in the function.
John McCallcf819ab2010-05-12 00:58:13 +0000583void JumpScopeChecker::VerifyIndirectJumps() {
584 if (IndirectJumps.empty()) return;
585
586 // If there aren't any address-of-label expressions in this function,
587 // complain about the first indirect goto.
588 if (IndirectJumpTargets.empty()) {
589 S.Diag(IndirectJumps[0]->getGotoLoc(),
590 diag::err_indirect_goto_without_addrlabel);
591 return;
592 }
593
John McCall42f9f1f2010-05-12 02:37:54 +0000594 // Collect a single representative of every scope containing an
595 // indirect goto. For most code bases, this substantially cuts
596 // down on the number of jump sites we'll have to consider later.
John McCallcf819ab2010-05-12 00:58:13 +0000597 typedef std::pair<unsigned, IndirectGotoStmt*> JumpScope;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000598 SmallVector<JumpScope, 32> JumpScopes;
John McCallcf819ab2010-05-12 00:58:13 +0000599 {
600 llvm::DenseMap<unsigned, IndirectGotoStmt*> JumpScopesMap;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000601 for (SmallVectorImpl<IndirectGotoStmt*>::iterator
John McCallcf819ab2010-05-12 00:58:13 +0000602 I = IndirectJumps.begin(), E = IndirectJumps.end(); I != E; ++I) {
603 IndirectGotoStmt *IG = *I;
Alp Tokere265cf12014-05-09 08:40:10 +0000604 if (CHECK_PERMISSIVE(!LabelAndGotoScopes.count(IG)))
605 continue;
John McCallcf819ab2010-05-12 00:58:13 +0000606 unsigned IGScope = LabelAndGotoScopes[IG];
607 IndirectGotoStmt *&Entry = JumpScopesMap[IGScope];
608 if (!Entry) Entry = IG;
609 }
610 JumpScopes.reserve(JumpScopesMap.size());
611 for (llvm::DenseMap<unsigned, IndirectGotoStmt*>::iterator
612 I = JumpScopesMap.begin(), E = JumpScopesMap.end(); I != E; ++I)
613 JumpScopes.push_back(*I);
614 }
615
John McCall42f9f1f2010-05-12 02:37:54 +0000616 // Collect a single representative of every scope containing a
617 // label whose address was taken somewhere in the function.
618 // For most code bases, there will be only one such scope.
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000619 llvm::DenseMap<unsigned, LabelDecl*> TargetScopes;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000620 for (SmallVectorImpl<LabelDecl*>::iterator
John McCallcf819ab2010-05-12 00:58:13 +0000621 I = IndirectJumpTargets.begin(), E = IndirectJumpTargets.end();
622 I != E; ++I) {
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000623 LabelDecl *TheLabel = *I;
Alp Tokere265cf12014-05-09 08:40:10 +0000624 if (CHECK_PERMISSIVE(!LabelAndGotoScopes.count(TheLabel->getStmt())))
625 continue;
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000626 unsigned LabelScope = LabelAndGotoScopes[TheLabel->getStmt()];
627 LabelDecl *&Target = TargetScopes[LabelScope];
John McCallcf819ab2010-05-12 00:58:13 +0000628 if (!Target) Target = TheLabel;
629 }
630
John McCall42f9f1f2010-05-12 02:37:54 +0000631 // For each target scope, make sure it's trivially reachable from
632 // every scope containing a jump site.
633 //
634 // A path between scopes always consists of exitting zero or more
635 // scopes, then entering zero or more scopes. We build a set of
636 // of scopes S from which the target scope can be trivially
637 // entered, then verify that every jump scope can be trivially
638 // exitted to reach a scope in S.
John McCallcf819ab2010-05-12 00:58:13 +0000639 llvm::BitVector Reachable(Scopes.size(), false);
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000640 for (llvm::DenseMap<unsigned,LabelDecl*>::iterator
John McCallcf819ab2010-05-12 00:58:13 +0000641 TI = TargetScopes.begin(), TE = TargetScopes.end(); TI != TE; ++TI) {
642 unsigned TargetScope = TI->first;
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000643 LabelDecl *TargetLabel = TI->second;
John McCallcf819ab2010-05-12 00:58:13 +0000644
645 Reachable.reset();
646
647 // Mark all the enclosing scopes from which you can safely jump
John McCall42f9f1f2010-05-12 02:37:54 +0000648 // into the target scope. 'Min' will end up being the index of
649 // the shallowest such scope.
John McCallcf819ab2010-05-12 00:58:13 +0000650 unsigned Min = TargetScope;
651 while (true) {
652 Reachable.set(Min);
653
654 // Don't go beyond the outermost scope.
655 if (Min == 0) break;
656
John McCall42f9f1f2010-05-12 02:37:54 +0000657 // Stop if we can't trivially enter the current scope.
John McCallcf819ab2010-05-12 00:58:13 +0000658 if (Scopes[Min].InDiag) break;
659
660 Min = Scopes[Min].ParentScope;
661 }
662
663 // Walk through all the jump sites, checking that they can trivially
664 // reach this label scope.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000665 for (SmallVectorImpl<JumpScope>::iterator
John McCallcf819ab2010-05-12 00:58:13 +0000666 I = JumpScopes.begin(), E = JumpScopes.end(); I != E; ++I) {
667 unsigned Scope = I->first;
668
669 // Walk out the "scope chain" for this scope, looking for a scope
John McCall42f9f1f2010-05-12 02:37:54 +0000670 // we've marked reachable. For well-formed code this amortizes
671 // to O(JumpScopes.size() / Scopes.size()): we only iterate
672 // when we see something unmarked, and in well-formed code we
673 // mark everything we iterate past.
John McCallcf819ab2010-05-12 00:58:13 +0000674 bool IsReachable = false;
675 while (true) {
676 if (Reachable.test(Scope)) {
677 // If we find something reachable, mark all the scopes we just
678 // walked through as reachable.
679 for (unsigned S = I->first; S != Scope; S = Scopes[S].ParentScope)
680 Reachable.set(S);
681 IsReachable = true;
682 break;
683 }
684
685 // Don't walk out if we've reached the top-level scope or we've
686 // gotten shallower than the shallowest reachable scope.
687 if (Scope == 0 || Scope < Min) break;
688
689 // Don't walk out through an out-diagnostic.
690 if (Scopes[Scope].OutDiag) break;
691
692 Scope = Scopes[Scope].ParentScope;
693 }
694
695 // Only diagnose if we didn't find something.
696 if (IsReachable) continue;
697
698 DiagnoseIndirectJump(I->second, I->first, TargetLabel, TargetScope);
699 }
700 }
701}
702
Richard Smithfe2750d2011-10-20 21:42:12 +0000703/// Return true if a particular error+note combination must be downgraded to a
704/// warning in Microsoft mode.
705static bool IsMicrosoftJumpWarning(unsigned JumpDiag, unsigned InDiagNote) {
706 return (JumpDiag == diag::err_goto_into_protected_scope &&
707 (InDiagNote == diag::note_protected_by_variable_init ||
708 InDiagNote == diag::note_protected_by_variable_nontriv_destructor));
709}
710
711/// Return true if a particular note should be downgraded to a compatibility
712/// warning in C++11 mode.
713static bool IsCXX98CompatWarning(Sema &S, unsigned InDiagNote) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000714 return S.getLangOpts().CPlusPlus11 &&
Richard Smithfe2750d2011-10-20 21:42:12 +0000715 InDiagNote == diag::note_protected_by_variable_non_pod;
716}
717
718/// Produce primary diagnostic for an indirect jump statement.
719static void DiagnoseIndirectJumpStmt(Sema &S, IndirectGotoStmt *Jump,
720 LabelDecl *Target, bool &Diagnosed) {
721 if (Diagnosed)
722 return;
723 S.Diag(Jump->getGotoLoc(), diag::err_indirect_goto_in_protected_scope);
724 S.Diag(Target->getStmt()->getIdentLoc(), diag::note_indirect_goto_target);
725 Diagnosed = true;
726}
727
728/// Produce note diagnostics for a jump into a protected scope.
Bill Wendling8ac06af2012-02-22 09:38:11 +0000729void JumpScopeChecker::NoteJumpIntoScopes(ArrayRef<unsigned> ToScopes) {
Alp Tokere265cf12014-05-09 08:40:10 +0000730 if (CHECK_PERMISSIVE(ToScopes.empty()))
731 return;
Richard Smithfe2750d2011-10-20 21:42:12 +0000732 for (unsigned I = 0, E = ToScopes.size(); I != E; ++I)
733 if (Scopes[ToScopes[I]].InDiag)
734 S.Diag(Scopes[ToScopes[I]].Loc, Scopes[ToScopes[I]].InDiag);
735}
736
John McCall42f9f1f2010-05-12 02:37:54 +0000737/// Diagnose an indirect jump which is known to cross scopes.
John McCallcf819ab2010-05-12 00:58:13 +0000738void JumpScopeChecker::DiagnoseIndirectJump(IndirectGotoStmt *Jump,
739 unsigned JumpScope,
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000740 LabelDecl *Target,
John McCallcf819ab2010-05-12 00:58:13 +0000741 unsigned TargetScope) {
Alp Tokere265cf12014-05-09 08:40:10 +0000742 if (CHECK_PERMISSIVE(JumpScope == TargetScope))
743 return;
John McCallcf819ab2010-05-12 00:58:13 +0000744
John McCall42f9f1f2010-05-12 02:37:54 +0000745 unsigned Common = GetDeepestCommonScope(JumpScope, TargetScope);
Richard Smithfe2750d2011-10-20 21:42:12 +0000746 bool Diagnosed = false;
John McCallcf819ab2010-05-12 00:58:13 +0000747
John McCall42f9f1f2010-05-12 02:37:54 +0000748 // Walk out the scope chain until we reach the common ancestor.
749 for (unsigned I = JumpScope; I != Common; I = Scopes[I].ParentScope)
Richard Smithfe2750d2011-10-20 21:42:12 +0000750 if (Scopes[I].OutDiag) {
751 DiagnoseIndirectJumpStmt(S, Jump, Target, Diagnosed);
John McCall42f9f1f2010-05-12 02:37:54 +0000752 S.Diag(Scopes[I].Loc, Scopes[I].OutDiag);
Richard Smithfe2750d2011-10-20 21:42:12 +0000753 }
754
755 SmallVector<unsigned, 10> ToScopesCXX98Compat;
John McCallcf819ab2010-05-12 00:58:13 +0000756
757 // Now walk into the scopes containing the label whose address was taken.
John McCall42f9f1f2010-05-12 02:37:54 +0000758 for (unsigned I = TargetScope; I != Common; I = Scopes[I].ParentScope)
Richard Smithfe2750d2011-10-20 21:42:12 +0000759 if (IsCXX98CompatWarning(S, Scopes[I].InDiag))
760 ToScopesCXX98Compat.push_back(I);
761 else if (Scopes[I].InDiag) {
762 DiagnoseIndirectJumpStmt(S, Jump, Target, Diagnosed);
John McCall42f9f1f2010-05-12 02:37:54 +0000763 S.Diag(Scopes[I].Loc, Scopes[I].InDiag);
Richard Smithfe2750d2011-10-20 21:42:12 +0000764 }
John McCallcf819ab2010-05-12 00:58:13 +0000765
Richard Smithfe2750d2011-10-20 21:42:12 +0000766 // Diagnose this jump if it would be ill-formed in C++98.
767 if (!Diagnosed && !ToScopesCXX98Compat.empty()) {
768 S.Diag(Jump->getGotoLoc(),
769 diag::warn_cxx98_compat_indirect_goto_in_protected_scope);
770 S.Diag(Target->getStmt()->getIdentLoc(), diag::note_indirect_goto_target);
771 NoteJumpIntoScopes(ToScopesCXX98Compat);
772 }
Francois Pichet051f5e52011-09-13 10:26:51 +0000773}
774
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000775/// CheckJump - Validate that the specified jump statement is valid: that it is
776/// jumping within or out of its current scope, not into a deeper one.
Francois Pichet051f5e52011-09-13 10:26:51 +0000777void JumpScopeChecker::CheckJump(Stmt *From, Stmt *To, SourceLocation DiagLoc,
Richard Smithfe2750d2011-10-20 21:42:12 +0000778 unsigned JumpDiagError, unsigned JumpDiagWarning,
779 unsigned JumpDiagCXX98Compat) {
Alp Tokere265cf12014-05-09 08:40:10 +0000780 if (CHECK_PERMISSIVE(!LabelAndGotoScopes.count(From)))
781 return;
782 if (CHECK_PERMISSIVE(!LabelAndGotoScopes.count(To)))
783 return;
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000784
Alp Tokere265cf12014-05-09 08:40:10 +0000785 unsigned FromScope = LabelAndGotoScopes[From];
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000786 unsigned ToScope = LabelAndGotoScopes[To];
Mike Stump11289f42009-09-09 15:08:12 +0000787
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000788 // Common case: exactly the same scope, which is fine.
789 if (FromScope == ToScope) return;
Mike Stump11289f42009-09-09 15:08:12 +0000790
Nico Webereb0cfb52015-03-09 04:27:56 +0000791 // Warn on gotos out of __finally blocks.
792 if (isa<GotoStmt>(From) || isa<IndirectGotoStmt>(From)) {
793 // If FromScope > ToScope, FromScope is more nested and the jump goes to a
794 // less nested scope. Check if it crosses a __finally along the way.
795 for (unsigned I = FromScope; I > ToScope; I = Scopes[I].ParentScope) {
796 if (Scopes[I].InDiag == diag::note_protected_by_seh_finally) {
797 S.Diag(From->getLocStart(), diag::warn_jump_out_of_seh_finally);
798 break;
799 }
800 }
801 }
802
John McCall42f9f1f2010-05-12 02:37:54 +0000803 unsigned CommonScope = GetDeepestCommonScope(FromScope, ToScope);
Mike Stump11289f42009-09-09 15:08:12 +0000804
John McCall42f9f1f2010-05-12 02:37:54 +0000805 // It's okay to jump out from a nested scope.
806 if (CommonScope == ToScope) return;
Mike Stump11289f42009-09-09 15:08:12 +0000807
John McCall42f9f1f2010-05-12 02:37:54 +0000808 // Pull out (and reverse) any scopes we might need to diagnose skipping.
Richard Smithfe2750d2011-10-20 21:42:12 +0000809 SmallVector<unsigned, 10> ToScopesCXX98Compat;
Francois Pichet051f5e52011-09-13 10:26:51 +0000810 SmallVector<unsigned, 10> ToScopesError;
811 SmallVector<unsigned, 10> ToScopesWarning;
812 for (unsigned I = ToScope; I != CommonScope; I = Scopes[I].ParentScope) {
Alp Tokerbfa39342014-01-14 12:51:41 +0000813 if (S.getLangOpts().MSVCCompat && JumpDiagWarning != 0 &&
Francois Pichet051f5e52011-09-13 10:26:51 +0000814 IsMicrosoftJumpWarning(JumpDiagError, Scopes[I].InDiag))
815 ToScopesWarning.push_back(I);
Richard Smithfe2750d2011-10-20 21:42:12 +0000816 else if (IsCXX98CompatWarning(S, Scopes[I].InDiag))
817 ToScopesCXX98Compat.push_back(I);
Francois Pichet051f5e52011-09-13 10:26:51 +0000818 else if (Scopes[I].InDiag)
819 ToScopesError.push_back(I);
820 }
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000821
Francois Pichet051f5e52011-09-13 10:26:51 +0000822 // Handle warnings.
823 if (!ToScopesWarning.empty()) {
824 S.Diag(DiagLoc, JumpDiagWarning);
Richard Smithfe2750d2011-10-20 21:42:12 +0000825 NoteJumpIntoScopes(ToScopesWarning);
Francois Pichet051f5e52011-09-13 10:26:51 +0000826 }
John McCallcf819ab2010-05-12 00:58:13 +0000827
Francois Pichet051f5e52011-09-13 10:26:51 +0000828 // Handle errors.
829 if (!ToScopesError.empty()) {
830 S.Diag(DiagLoc, JumpDiagError);
Richard Smithfe2750d2011-10-20 21:42:12 +0000831 NoteJumpIntoScopes(ToScopesError);
832 }
833
834 // Handle -Wc++98-compat warnings if the jump is well-formed.
835 if (ToScopesError.empty() && !ToScopesCXX98Compat.empty()) {
836 S.Diag(DiagLoc, JumpDiagCXX98Compat);
837 NoteJumpIntoScopes(ToScopesCXX98Compat);
Francois Pichet051f5e52011-09-13 10:26:51 +0000838 }
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000839}
840
Ehsan Akhgari31097582014-09-22 02:21:54 +0000841void JumpScopeChecker::CheckGotoStmt(GotoStmt *GS) {
842 if (GS->getLabel()->isMSAsmLabel()) {
843 S.Diag(GS->getGotoLoc(), diag::err_goto_ms_asm_label)
844 << GS->getLabel()->getIdentifier();
845 S.Diag(GS->getLabel()->getLocation(), diag::note_goto_ms_asm_label)
846 << GS->getLabel()->getIdentifier();
847 }
848}
849
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000850void Sema::DiagnoseInvalidJumps(Stmt *Body) {
Douglas Gregor120f6a62009-11-17 06:14:37 +0000851 (void)JumpScopeChecker(Body, *this);
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000852}