blob: d3de1732766e84d81205602fa9fb2859dba03ca7 [file] [log] [blame]
Francois Pichet67bc6072011-09-09 11:02:57 +00001//===--- JumpDiagnostics.cpp - Protected scope jump analysis ------*- C++ -*-=//
Chris Lattner5af280c2009-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 Pichet67bc6072011-09-09 11:02:57 +000011// jumps that enter a protected scope in an invalid way.
Chris Lattner5af280c2009-04-19 04:46:21 +000012//
13//===----------------------------------------------------------------------===//
14
John McCall2d887082010-08-25 22:03:47 +000015#include "clang/Sema/SemaInternal.h"
John McCall384aff82010-08-25 07:42:41 +000016#include "clang/AST/DeclCXX.h"
Chris Lattner5af280c2009-04-19 04:46:21 +000017#include "clang/AST/Expr.h"
Douglas Gregore4135162011-05-27 16:05:29 +000018#include "clang/AST/ExprCXX.h"
Sebastian Redl972041f2009-04-27 20:27:31 +000019#include "clang/AST/StmtCXX.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000020#include "clang/AST/StmtObjC.h"
Douglas Gregore737f502010-08-12 20:07:10 +000021#include "llvm/ADT/BitVector.h"
Chris Lattner5af280c2009-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 Stump1eb44332009-09-09 15:08:12 +000034
Chris Lattner5af280c2009-04-19 04:46:21 +000035 /// GotoScope - This is a record that we use to keep track of all of the
36 /// scopes that are introduced by VLAs and other things that scope jumps like
37 /// gotos. This scope tree has nothing to do with the source scope tree,
38 /// because you can have multiple VLA scopes per compound statement, and most
39 /// compound statements don't introduce any scopes.
40 struct GotoScope {
41 /// ParentScope - The index in ScopeMap of the parent scope. This is 0 for
42 /// the parent scope is the function body.
43 unsigned ParentScope;
Mike Stump1eb44332009-09-09 15:08:12 +000044
Richard Smith0e9e9812011-10-20 21:42:12 +000045 /// InDiag - The note to emit if there is a jump into this scope.
John McCallddb0b4d2010-05-12 00:58:13 +000046 unsigned InDiag;
47
Richard Smith0e9e9812011-10-20 21:42:12 +000048 /// OutDiag - The note to emit if there is an indirect jump out
John McCallddb0b4d2010-05-12 00:58:13 +000049 /// of this scope. Direct jumps always clean up their current scope
50 /// in an orderly way.
51 unsigned OutDiag;
Mike Stump1eb44332009-09-09 15:08:12 +000052
Chris Lattner5af280c2009-04-19 04:46:21 +000053 /// Loc - Location to emit the diagnostic.
54 SourceLocation Loc;
Mike Stump1eb44332009-09-09 15:08:12 +000055
John McCallddb0b4d2010-05-12 00:58:13 +000056 GotoScope(unsigned parentScope, unsigned InDiag, unsigned OutDiag,
57 SourceLocation L)
58 : ParentScope(parentScope), InDiag(InDiag), OutDiag(OutDiag), Loc(L) {}
Chris Lattner5af280c2009-04-19 04:46:21 +000059 };
Mike Stump1eb44332009-09-09 15:08:12 +000060
Chris Lattner5f9e2722011-07-23 10:55:15 +000061 SmallVector<GotoScope, 48> Scopes;
Chris Lattner5af280c2009-04-19 04:46:21 +000062 llvm::DenseMap<Stmt*, unsigned> LabelAndGotoScopes;
Chris Lattner5f9e2722011-07-23 10:55:15 +000063 SmallVector<Stmt*, 16> Jumps;
John McCallddb0b4d2010-05-12 00:58:13 +000064
Chris Lattner5f9e2722011-07-23 10:55:15 +000065 SmallVector<IndirectGotoStmt*, 4> IndirectJumps;
66 SmallVector<LabelDecl*, 4> IndirectJumpTargets;
Chris Lattner5af280c2009-04-19 04:46:21 +000067public:
68 JumpScopeChecker(Stmt *Body, Sema &S);
69private:
Douglas Gregor43dec6b2010-06-21 23:44:13 +000070 void BuildScopeInformation(Decl *D, unsigned &ParentScope);
Fariborz Jahanian4e7c7f22011-07-11 18:04:54 +000071 void BuildScopeInformation(VarDecl *D, const BlockDecl *BDecl,
72 unsigned &ParentScope);
73 void BuildScopeInformation(Stmt *S, unsigned &origParentScope);
74
Chris Lattner5af280c2009-04-19 04:46:21 +000075 void VerifyJumps();
John McCallddb0b4d2010-05-12 00:58:13 +000076 void VerifyIndirectJumps();
Bill Wendling4fe5be02012-02-22 09:38:11 +000077 void NoteJumpIntoScopes(ArrayRef<unsigned> ToScopes);
John McCallddb0b4d2010-05-12 00:58:13 +000078 void DiagnoseIndirectJump(IndirectGotoStmt *IG, unsigned IGScope,
Chris Lattnerad8dcf42011-02-17 07:39:24 +000079 LabelDecl *Target, unsigned TargetScope);
Francois Pichetc985b882011-09-13 10:26:51 +000080 void CheckJump(Stmt *From, Stmt *To, SourceLocation DiagLoc,
Richard Smith0e9e9812011-10-20 21:42:12 +000081 unsigned JumpDiag, unsigned JumpDiagWarning,
82 unsigned JumpDiagCXX98Compat);
John McCall5e2a7ac2010-05-12 02:37:54 +000083
84 unsigned GetDeepestCommonScope(unsigned A, unsigned B);
Chris Lattner5af280c2009-04-19 04:46:21 +000085};
86} // end anonymous namespace
87
88
89JumpScopeChecker::JumpScopeChecker(Stmt *Body, Sema &s) : S(s) {
90 // Add a scope entry for function scope.
John McCallddb0b4d2010-05-12 00:58:13 +000091 Scopes.push_back(GotoScope(~0U, ~0U, ~0U, SourceLocation()));
Mike Stump1eb44332009-09-09 15:08:12 +000092
Chris Lattner5af280c2009-04-19 04:46:21 +000093 // Build information for the top level compound statement, so that we have a
94 // defined scope record for every "goto" and label.
Fariborz Jahanian4e7c7f22011-07-11 18:04:54 +000095 unsigned BodyParentScope = 0;
96 BuildScopeInformation(Body, BodyParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +000097
Chris Lattner5af280c2009-04-19 04:46:21 +000098 // Check that all jumps we saw are kosher.
99 VerifyJumps();
John McCallddb0b4d2010-05-12 00:58:13 +0000100 VerifyIndirectJumps();
Chris Lattner5af280c2009-04-19 04:46:21 +0000101}
Mike Stump1eb44332009-09-09 15:08:12 +0000102
John McCall5e2a7ac2010-05-12 02:37:54 +0000103/// GetDeepestCommonScope - Finds the innermost scope enclosing the
104/// two scopes.
105unsigned JumpScopeChecker::GetDeepestCommonScope(unsigned A, unsigned B) {
106 while (A != B) {
107 // Inner scopes are created after outer scopes and therefore have
108 // higher indices.
109 if (A < B) {
110 assert(Scopes[B].ParentScope < B);
111 B = Scopes[B].ParentScope;
112 } else {
113 assert(Scopes[A].ParentScope < A);
114 A = Scopes[A].ParentScope;
115 }
116 }
117 return A;
118}
119
John McCallf85e1932011-06-15 23:02:42 +0000120typedef std::pair<unsigned,unsigned> ScopePair;
121
Chris Lattner5af280c2009-04-19 04:46:21 +0000122/// GetDiagForGotoScopeDecl - If this decl induces a new goto scope, return a
123/// diagnostic that should be emitted if control goes over it. If not, return 0.
John McCallf85e1932011-06-15 23:02:42 +0000124static ScopePair GetDiagForGotoScopeDecl(ASTContext &Context, const Decl *D) {
Chris Lattner5af280c2009-04-19 04:46:21 +0000125 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola644e90a2012-10-28 02:44:03 +0000126 unsigned InDiag = 0;
Chris Lattner5af280c2009-04-19 04:46:21 +0000127 if (VD->getType()->isVariablyModifiedType())
John McCallddb0b4d2010-05-12 00:58:13 +0000128 InDiag = diag::note_protected_by_vla;
129
John McCallf85e1932011-06-15 23:02:42 +0000130 if (VD->hasAttr<BlocksAttr>())
131 return ScopePair(diag::note_protected_by___block,
132 diag::note_exits___block);
133
134 if (VD->hasAttr<CleanupAttr>())
135 return ScopePair(diag::note_protected_by_cleanup,
136 diag::note_exits_cleanup);
137
David Blaikie4e4d0842012-03-11 07:00:24 +0000138 if (Context.getLangOpts().ObjCAutoRefCount && VD->hasLocalStorage()) {
John McCallf85e1932011-06-15 23:02:42 +0000139 switch (VD->getType().getObjCLifetime()) {
140 case Qualifiers::OCL_None:
141 case Qualifiers::OCL_ExplicitNone:
142 case Qualifiers::OCL_Autoreleasing:
143 break;
144
145 case Qualifiers::OCL_Strong:
146 case Qualifiers::OCL_Weak:
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +0000147 return ScopePair(diag::note_protected_by_objc_ownership,
148 diag::note_exits_objc_ownership);
John McCallf85e1932011-06-15 23:02:42 +0000149 }
150 }
151
David Blaikie4e4d0842012-03-11 07:00:24 +0000152 if (Context.getLangOpts().CPlusPlus && VD->hasLocalStorage()) {
Richard Smith0e9e9812011-10-20 21:42:12 +0000153 // C++11 [stmt.dcl]p3:
John McCallf85e1932011-06-15 23:02:42 +0000154 // A program that jumps from a point where a variable with automatic
155 // storage duration is not in scope to a point where it is in scope
156 // is ill-formed unless the variable has scalar type, class type with
157 // a trivial default constructor and a trivial destructor, a
158 // cv-qualified version of one of these types, or an array of one of
159 // the preceding types and is declared without an initializer.
160
161 // C++03 [stmt.dcl.p3:
162 // A program that jumps from a point where a local variable
163 // with automatic storage duration is not in scope to a point
164 // where it is in scope is ill-formed unless the variable has
165 // POD type and is declared without an initializer.
166
Rafael Espindola644e90a2012-10-28 02:44:03 +0000167 const Expr *Init = VD->getInit();
168 if (!Init)
169 return ScopePair(InDiag, 0);
John McCallf85e1932011-06-15 23:02:42 +0000170
Rafael Espindola644e90a2012-10-28 02:44:03 +0000171 const ExprWithCleanups *EWC = dyn_cast<ExprWithCleanups>(Init);
172 if (EWC)
173 Init = EWC->getSubExpr();
John McCallf85e1932011-06-15 23:02:42 +0000174
Rafael Espindola644e90a2012-10-28 02:44:03 +0000175 const MaterializeTemporaryExpr *M = NULL;
176 Init = Init->findMaterializedTemporary(M);
John McCallf85e1932011-06-15 23:02:42 +0000177
Richard Smith4e43dec2013-06-03 00:17:11 +0000178 SmallVector<const Expr *, 2> CommaLHSs;
Rafael Espindola644e90a2012-10-28 02:44:03 +0000179 SmallVector<SubobjectAdjustment, 2> Adjustments;
Richard Smith4e43dec2013-06-03 00:17:11 +0000180 Init = Init->skipRValueSubobjectAdjustments(CommaLHSs, Adjustments);
Rafael Espindola644e90a2012-10-28 02:44:03 +0000181
182 QualType QT = Init->getType();
Richard Smithb45a4d22013-06-03 01:05:37 +0000183 if (QT.isNull())
Rafael Espindola644e90a2012-10-28 02:44:03 +0000184 return ScopePair(diag::note_protected_by_variable_init, 0);
185
186 const Type *T = QT.getTypePtr();
187 if (T->isArrayType())
188 T = T->getBaseElementTypeUnsafe();
189
190 const CXXRecordDecl *Record = T->getAsCXXRecordDecl();
191 if (!Record)
192 return ScopePair(diag::note_protected_by_variable_init, 0);
193
194 // If we need to call a non trivial destructor for this variable,
195 // record an out diagnostic.
196 unsigned OutDiag = 0;
197 if (!Init->isGLValue() && !Record->hasTrivialDestructor())
198 OutDiag = diag::note_exits_dtor;
199
200 if (const CXXConstructExpr *cce = dyn_cast<CXXConstructExpr>(Init)) {
201 const CXXConstructorDecl *ctor = cce->getConstructor();
Richard Smithb45a4d22013-06-03 01:05:37 +0000202 // For a variable declared without an initializer, we will have
203 // call-style initialization and the initializer will be the
204 // CXXConstructExpr with no intervening nodes.
205 if (ctor->isTrivial() && ctor->isDefaultConstructor() &&
206 VD->getInit() == Init && VD->getInitStyle() == VarDecl::CallInit) {
Rafael Espindola644e90a2012-10-28 02:44:03 +0000207 if (OutDiag)
208 InDiag = diag::note_protected_by_variable_nontriv_destructor;
209 else if (!Record->isPOD())
210 InDiag = diag::note_protected_by_variable_non_pod;
211 return ScopePair(InDiag, OutDiag);
Douglas Gregore4135162011-05-27 16:05:29 +0000212 }
Douglas Gregor025291b2010-07-01 00:21:21 +0000213 }
Rafael Espindola644e90a2012-10-28 02:44:03 +0000214
215 return ScopePair(diag::note_protected_by_variable_init, OutDiag);
John McCallddb0b4d2010-05-12 00:58:13 +0000216 }
Rafael Espindola644e90a2012-10-28 02:44:03 +0000217
218 return ScopePair(InDiag, 0);
Chris Lattner5af280c2009-04-19 04:46:21 +0000219 }
Mike Stump1eb44332009-09-09 15:08:12 +0000220
John McCallddb0b4d2010-05-12 00:58:13 +0000221 if (const TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
222 if (TD->getUnderlyingType()->isVariablyModifiedType())
John McCallf85e1932011-06-15 23:02:42 +0000223 return ScopePair(diag::note_protected_by_vla_typedef, 0);
John McCallddb0b4d2010-05-12 00:58:13 +0000224 }
225
Richard Smith162e1c12011-04-15 14:24:37 +0000226 if (const TypeAliasDecl *TD = dyn_cast<TypeAliasDecl>(D)) {
227 if (TD->getUnderlyingType()->isVariablyModifiedType())
John McCallf85e1932011-06-15 23:02:42 +0000228 return ScopePair(diag::note_protected_by_vla_type_alias, 0);
Richard Smith162e1c12011-04-15 14:24:37 +0000229 }
230
John McCallf85e1932011-06-15 23:02:42 +0000231 return ScopePair(0U, 0U);
Chris Lattner5af280c2009-04-19 04:46:21 +0000232}
233
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000234/// \brief Build scope information for a declaration that is part of a DeclStmt.
235void JumpScopeChecker::BuildScopeInformation(Decl *D, unsigned &ParentScope) {
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000236 // If this decl causes a new scope, push and switch to it.
John McCallf85e1932011-06-15 23:02:42 +0000237 std::pair<unsigned,unsigned> Diags = GetDiagForGotoScopeDecl(S.Context, D);
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000238 if (Diags.first || Diags.second) {
239 Scopes.push_back(GotoScope(ParentScope, Diags.first, Diags.second,
240 D->getLocation()));
241 ParentScope = Scopes.size()-1;
242 }
243
244 // If the decl has an initializer, walk it with the potentially new
245 // scope we just installed.
246 if (VarDecl *VD = dyn_cast<VarDecl>(D))
247 if (Expr *Init = VD->getInit())
248 BuildScopeInformation(Init, ParentScope);
249}
Chris Lattner5af280c2009-04-19 04:46:21 +0000250
Fariborz Jahanian4e7c7f22011-07-11 18:04:54 +0000251/// \brief Build scope information for a captured block literal variables.
252void JumpScopeChecker::BuildScopeInformation(VarDecl *D,
253 const BlockDecl *BDecl,
254 unsigned &ParentScope) {
255 // exclude captured __block variables; there's no destructor
256 // associated with the block literal for them.
257 if (D->hasAttr<BlocksAttr>())
258 return;
259 QualType T = D->getType();
260 QualType::DestructionKind destructKind = T.isDestructedType();
261 if (destructKind != QualType::DK_none) {
262 std::pair<unsigned,unsigned> Diags;
263 switch (destructKind) {
264 case QualType::DK_cxx_destructor:
265 Diags = ScopePair(diag::note_enters_block_captures_cxx_obj,
266 diag::note_exits_block_captures_cxx_obj);
267 break;
268 case QualType::DK_objc_strong_lifetime:
269 Diags = ScopePair(diag::note_enters_block_captures_strong,
270 diag::note_exits_block_captures_strong);
271 break;
272 case QualType::DK_objc_weak_lifetime:
273 Diags = ScopePair(diag::note_enters_block_captures_weak,
274 diag::note_exits_block_captures_weak);
275 break;
276 case QualType::DK_none:
Richard Smith0e9e9812011-10-20 21:42:12 +0000277 llvm_unreachable("non-lifetime captured variable");
Fariborz Jahanian4e7c7f22011-07-11 18:04:54 +0000278 }
279 SourceLocation Loc = D->getLocation();
280 if (Loc.isInvalid())
281 Loc = BDecl->getLocation();
282 Scopes.push_back(GotoScope(ParentScope,
283 Diags.first, Diags.second, Loc));
284 ParentScope = Scopes.size()-1;
285 }
286}
287
Chris Lattner5af280c2009-04-19 04:46:21 +0000288/// BuildScopeInformation - The statements from CI to CE are known to form a
289/// coherent VLA scope with a specified parent node. Walk through the
290/// statements, adding any labels or gotos to LabelAndGotoScopes and recursively
291/// walking the AST as needed.
Fariborz Jahanian4e7c7f22011-07-11 18:04:54 +0000292void JumpScopeChecker::BuildScopeInformation(Stmt *S, unsigned &origParentScope) {
293 // If this is a statement, rather than an expression, scopes within it don't
294 // propagate out into the enclosing scope. Otherwise we have to worry
295 // about block literals, which have the lifetime of their enclosing statement.
296 unsigned independentParentScope = origParentScope;
297 unsigned &ParentScope = ((isa<Expr>(S) && !isa<StmtExpr>(S))
298 ? origParentScope : independentParentScope);
299
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000300 bool SkipFirstSubStmt = false;
301
Chris Lattner5af280c2009-04-19 04:46:21 +0000302 // If we found a label, remember that it is in ParentScope scope.
John McCallddb0b4d2010-05-12 00:58:13 +0000303 switch (S->getStmtClass()) {
John McCallddb0b4d2010-05-12 00:58:13 +0000304 case Stmt::AddrLabelExprClass:
305 IndirectJumpTargets.push_back(cast<AddrLabelExpr>(S)->getLabel());
306 break;
307
308 case Stmt::IndirectGotoStmtClass:
John McCall95c225d2010-10-28 08:53:48 +0000309 // "goto *&&lbl;" is a special case which we treat as equivalent
310 // to a normal goto. In addition, we don't calculate scope in the
311 // operand (to avoid recording the address-of-label use), which
312 // works only because of the restricted set of expressions which
313 // we detect as constant targets.
314 if (cast<IndirectGotoStmt>(S)->getConstantTarget()) {
315 LabelAndGotoScopes[S] = ParentScope;
316 Jumps.push_back(S);
317 return;
318 }
319
John McCallddb0b4d2010-05-12 00:58:13 +0000320 LabelAndGotoScopes[S] = ParentScope;
321 IndirectJumps.push_back(cast<IndirectGotoStmt>(S));
322 break;
323
John McCallddb0b4d2010-05-12 00:58:13 +0000324 case Stmt::SwitchStmtClass:
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000325 // Evaluate the condition variable before entering the scope of the switch
326 // statement.
327 if (VarDecl *Var = cast<SwitchStmt>(S)->getConditionVariable()) {
328 BuildScopeInformation(Var, ParentScope);
329 SkipFirstSubStmt = true;
330 }
331 // Fall through
332
333 case Stmt::GotoStmtClass:
Chris Lattner5af280c2009-04-19 04:46:21 +0000334 // Remember both what scope a goto is in as well as the fact that we have
335 // it. This makes the second scan not have to walk the AST again.
336 LabelAndGotoScopes[S] = ParentScope;
337 Jumps.push_back(S);
John McCallddb0b4d2010-05-12 00:58:13 +0000338 break;
339
Eli Friedman889b99e2012-10-31 23:55:28 +0000340 case Stmt::CXXTryStmtClass: {
341 CXXTryStmt *TS = cast<CXXTryStmt>(S);
342 unsigned newParentScope;
343 Scopes.push_back(GotoScope(ParentScope,
344 diag::note_protected_by_cxx_try,
345 diag::note_exits_cxx_try,
346 TS->getSourceRange().getBegin()));
347 if (Stmt *TryBlock = TS->getTryBlock())
348 BuildScopeInformation(TryBlock, (newParentScope = Scopes.size()-1));
349
350 // Jump from the catch into the try is not allowed either.
351 for (unsigned I = 0, E = TS->getNumHandlers(); I != E; ++I) {
352 CXXCatchStmt *CS = TS->getHandler(I);
353 Scopes.push_back(GotoScope(ParentScope,
354 diag::note_protected_by_cxx_catch,
355 diag::note_exits_cxx_catch,
356 CS->getSourceRange().getBegin()));
357 BuildScopeInformation(CS->getHandlerBlock(),
358 (newParentScope = Scopes.size()-1));
359 }
360 return;
361 }
362
John McCallddb0b4d2010-05-12 00:58:13 +0000363 default:
364 break;
Chris Lattner5af280c2009-04-19 04:46:21 +0000365 }
Mike Stump1eb44332009-09-09 15:08:12 +0000366
John McCall7502c1d2011-02-13 04:07:26 +0000367 for (Stmt::child_range CI = S->children(); CI; ++CI) {
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000368 if (SkipFirstSubStmt) {
369 SkipFirstSubStmt = false;
370 continue;
371 }
372
Chris Lattner5af280c2009-04-19 04:46:21 +0000373 Stmt *SubStmt = *CI;
374 if (SubStmt == 0) continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000375
John McCall97ba4812010-08-02 23:33:14 +0000376 // Cases, labels, and defaults aren't "scope parents". It's also
377 // important to handle these iteratively instead of recursively in
378 // order to avoid blowing out the stack.
379 while (true) {
380 Stmt *Next;
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000381 if (CaseStmt *CS = dyn_cast<CaseStmt>(SubStmt))
382 Next = CS->getSubStmt();
383 else if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SubStmt))
384 Next = DS->getSubStmt();
385 else if (LabelStmt *LS = dyn_cast<LabelStmt>(SubStmt))
386 Next = LS->getSubStmt();
John McCall97ba4812010-08-02 23:33:14 +0000387 else
388 break;
389
390 LabelAndGotoScopes[SubStmt] = ParentScope;
391 SubStmt = Next;
392 }
393
Chris Lattner5af280c2009-04-19 04:46:21 +0000394 // If this is a declstmt with a VLA definition, it defines a scope from here
395 // to the end of the containing context.
396 if (DeclStmt *DS = dyn_cast<DeclStmt>(SubStmt)) {
Chris Lattner6d97e5e2010-03-01 20:59:53 +0000397 // The decl statement creates a scope if any of the decls in it are VLAs
398 // or have the cleanup attribute.
Chris Lattner5af280c2009-04-19 04:46:21 +0000399 for (DeclStmt::decl_iterator I = DS->decl_begin(), E = DS->decl_end();
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000400 I != E; ++I)
401 BuildScopeInformation(*I, ParentScope);
Chris Lattner5af280c2009-04-19 04:46:21 +0000402 continue;
403 }
Chris Lattner5af280c2009-04-19 04:46:21 +0000404 // Disallow jumps into any part of an @try statement by pushing a scope and
405 // walking all sub-stmts in that scope.
406 if (ObjCAtTryStmt *AT = dyn_cast<ObjCAtTryStmt>(SubStmt)) {
Fariborz Jahanian4e7c7f22011-07-11 18:04:54 +0000407 unsigned newParentScope;
Chris Lattner5af280c2009-04-19 04:46:21 +0000408 // Recursively walk the AST for the @try part.
John McCallddb0b4d2010-05-12 00:58:13 +0000409 Scopes.push_back(GotoScope(ParentScope,
410 diag::note_protected_by_objc_try,
411 diag::note_exits_objc_try,
Chris Lattner5af280c2009-04-19 04:46:21 +0000412 AT->getAtTryLoc()));
413 if (Stmt *TryPart = AT->getTryBody())
Fariborz Jahanian4e7c7f22011-07-11 18:04:54 +0000414 BuildScopeInformation(TryPart, (newParentScope = Scopes.size()-1));
Chris Lattner5af280c2009-04-19 04:46:21 +0000415
416 // Jump from the catch to the finally or try is not valid.
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000417 for (unsigned I = 0, N = AT->getNumCatchStmts(); I != N; ++I) {
418 ObjCAtCatchStmt *AC = AT->getCatchStmt(I);
Chris Lattner5af280c2009-04-19 04:46:21 +0000419 Scopes.push_back(GotoScope(ParentScope,
420 diag::note_protected_by_objc_catch,
John McCallddb0b4d2010-05-12 00:58:13 +0000421 diag::note_exits_objc_catch,
Chris Lattner5af280c2009-04-19 04:46:21 +0000422 AC->getAtCatchLoc()));
Mike Stump1eb44332009-09-09 15:08:12 +0000423 // @catches are nested and it isn't
Fariborz Jahanian4e7c7f22011-07-11 18:04:54 +0000424 BuildScopeInformation(AC->getCatchBody(),
425 (newParentScope = Scopes.size()-1));
Chris Lattner5af280c2009-04-19 04:46:21 +0000426 }
Mike Stump1eb44332009-09-09 15:08:12 +0000427
Chris Lattner5af280c2009-04-19 04:46:21 +0000428 // Jump from the finally to the try or catch is not valid.
429 if (ObjCAtFinallyStmt *AF = AT->getFinallyStmt()) {
430 Scopes.push_back(GotoScope(ParentScope,
431 diag::note_protected_by_objc_finally,
John McCallddb0b4d2010-05-12 00:58:13 +0000432 diag::note_exits_objc_finally,
Chris Lattner5af280c2009-04-19 04:46:21 +0000433 AF->getAtFinallyLoc()));
Fariborz Jahanian4e7c7f22011-07-11 18:04:54 +0000434 BuildScopeInformation(AF, (newParentScope = Scopes.size()-1));
Chris Lattner5af280c2009-04-19 04:46:21 +0000435 }
Mike Stump1eb44332009-09-09 15:08:12 +0000436
Chris Lattner5af280c2009-04-19 04:46:21 +0000437 continue;
438 }
Fariborz Jahanian4e7c7f22011-07-11 18:04:54 +0000439
440 unsigned newParentScope;
Chris Lattner46c3c4b2009-04-21 06:01:00 +0000441 // Disallow jumps into the protected statement of an @synchronized, but
442 // allow jumps into the object expression it protects.
443 if (ObjCAtSynchronizedStmt *AS = dyn_cast<ObjCAtSynchronizedStmt>(SubStmt)){
444 // Recursively walk the AST for the @synchronized object expr, it is
445 // evaluated in the normal scope.
446 BuildScopeInformation(AS->getSynchExpr(), ParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000447
Chris Lattner46c3c4b2009-04-21 06:01:00 +0000448 // Recursively walk the AST for the @synchronized part, protected by a new
449 // scope.
450 Scopes.push_back(GotoScope(ParentScope,
451 diag::note_protected_by_objc_synchronized,
John McCallddb0b4d2010-05-12 00:58:13 +0000452 diag::note_exits_objc_synchronized,
Chris Lattner46c3c4b2009-04-21 06:01:00 +0000453 AS->getAtSynchronizedLoc()));
Fariborz Jahanian4e7c7f22011-07-11 18:04:54 +0000454 BuildScopeInformation(AS->getSynchBody(),
455 (newParentScope = Scopes.size()-1));
Chris Lattner46c3c4b2009-04-21 06:01:00 +0000456 continue;
457 }
Sebastian Redl972041f2009-04-27 20:27:31 +0000458
John McCallf85e1932011-06-15 23:02:42 +0000459 // Disallow jumps into the protected statement of an @autoreleasepool.
460 if (ObjCAutoreleasePoolStmt *AS = dyn_cast<ObjCAutoreleasePoolStmt>(SubStmt)){
461 // Recursively walk the AST for the @autoreleasepool part, protected by a new
462 // scope.
463 Scopes.push_back(GotoScope(ParentScope,
464 diag::note_protected_by_objc_autoreleasepool,
465 diag::note_exits_objc_autoreleasepool,
466 AS->getAtLoc()));
Fariborz Jahanian4e7c7f22011-07-11 18:04:54 +0000467 BuildScopeInformation(AS->getSubStmt(), (newParentScope = Scopes.size()-1));
John McCallf85e1932011-06-15 23:02:42 +0000468 continue;
469 }
John McCall9f357de2012-09-25 06:56:03 +0000470
471 // Disallow jumps past full-expressions that use blocks with
472 // non-trivial cleanups of their captures. This is theoretically
473 // implementable but a lot of work which we haven't felt up to doing.
474 if (ExprWithCleanups *EWC = dyn_cast<ExprWithCleanups>(SubStmt)) {
475 for (unsigned i = 0, e = EWC->getNumObjects(); i != e; ++i) {
476 const BlockDecl *BDecl = EWC->getObject(i);
Fariborz Jahanian4e7c7f22011-07-11 18:04:54 +0000477 for (BlockDecl::capture_const_iterator ci = BDecl->capture_begin(),
478 ce = BDecl->capture_end(); ci != ce; ++ci) {
479 VarDecl *variable = ci->getVariable();
480 BuildScopeInformation(variable, BDecl, ParentScope);
481 }
John McCall9f357de2012-09-25 06:56:03 +0000482 }
Fariborz Jahanian4e7c7f22011-07-11 18:04:54 +0000483 }
484
Chris Lattner5af280c2009-04-19 04:46:21 +0000485 // Recursively walk the AST.
486 BuildScopeInformation(SubStmt, ParentScope);
487 }
488}
489
490/// VerifyJumps - Verify each element of the Jumps array to see if they are
491/// valid, emitting diagnostics if not.
492void JumpScopeChecker::VerifyJumps() {
493 while (!Jumps.empty()) {
494 Stmt *Jump = Jumps.pop_back_val();
Mike Stump1eb44332009-09-09 15:08:12 +0000495
496 // With a goto,
Chris Lattner5af280c2009-04-19 04:46:21 +0000497 if (GotoStmt *GS = dyn_cast<GotoStmt>(Jump)) {
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000498 CheckJump(GS, GS->getLabel()->getStmt(), GS->getGotoLoc(),
Francois Pichetc985b882011-09-13 10:26:51 +0000499 diag::err_goto_into_protected_scope,
Richard Smith0e9e9812011-10-20 21:42:12 +0000500 diag::warn_goto_into_protected_scope,
501 diag::warn_cxx98_compat_goto_into_protected_scope);
Chris Lattner5af280c2009-04-19 04:46:21 +0000502 continue;
503 }
Mike Stump1eb44332009-09-09 15:08:12 +0000504
John McCall95c225d2010-10-28 08:53:48 +0000505 // We only get indirect gotos here when they have a constant target.
506 if (IndirectGotoStmt *IGS = dyn_cast<IndirectGotoStmt>(Jump)) {
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000507 LabelDecl *Target = IGS->getConstantTarget();
508 CheckJump(IGS, Target->getStmt(), IGS->getGotoLoc(),
Francois Pichet2e965112011-09-16 23:15:32 +0000509 diag::err_goto_into_protected_scope,
Richard Smith0e9e9812011-10-20 21:42:12 +0000510 diag::warn_goto_into_protected_scope,
511 diag::warn_cxx98_compat_goto_into_protected_scope);
John McCall95c225d2010-10-28 08:53:48 +0000512 continue;
513 }
514
John McCallddb0b4d2010-05-12 00:58:13 +0000515 SwitchStmt *SS = cast<SwitchStmt>(Jump);
516 for (SwitchCase *SC = SS->getSwitchCaseList(); SC;
517 SC = SC->getNextSwitchCase()) {
518 assert(LabelAndGotoScopes.count(SC) && "Case not visited?");
Erik Verbruggen65d78312012-12-25 14:51:39 +0000519 SourceLocation Loc;
520 if (CaseStmt *CS = dyn_cast<CaseStmt>(SC))
521 Loc = CS->getLocStart();
522 else if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC))
523 Loc = DS->getLocStart();
524 else
525 Loc = SC->getLocStart();
526 CheckJump(SS, SC, Loc, diag::err_switch_into_protected_scope, 0,
Richard Smith0e9e9812011-10-20 21:42:12 +0000527 diag::warn_cxx98_compat_switch_into_protected_scope);
Chris Lattner5af280c2009-04-19 04:46:21 +0000528 }
529 }
530}
531
John McCall5e2a7ac2010-05-12 02:37:54 +0000532/// VerifyIndirectJumps - Verify whether any possible indirect jump
533/// might cross a protection boundary. Unlike direct jumps, indirect
534/// jumps count cleanups as protection boundaries: since there's no
535/// way to know where the jump is going, we can't implicitly run the
536/// right cleanups the way we can with direct jumps.
John McCallddb0b4d2010-05-12 00:58:13 +0000537///
John McCall5e2a7ac2010-05-12 02:37:54 +0000538/// Thus, an indirect jump is "trivial" if it bypasses no
539/// initializations and no teardowns. More formally, an indirect jump
540/// from A to B is trivial if the path out from A to DCA(A,B) is
541/// trivial and the path in from DCA(A,B) to B is trivial, where
542/// DCA(A,B) is the deepest common ancestor of A and B.
543/// Jump-triviality is transitive but asymmetric.
544///
John McCallddb0b4d2010-05-12 00:58:13 +0000545/// A path in is trivial if none of the entered scopes have an InDiag.
546/// A path out is trivial is none of the exited scopes have an OutDiag.
John McCall5e2a7ac2010-05-12 02:37:54 +0000547///
548/// Under these definitions, this function checks that the indirect
549/// jump between A and B is trivial for every indirect goto statement A
550/// and every label B whose address was taken in the function.
John McCallddb0b4d2010-05-12 00:58:13 +0000551void JumpScopeChecker::VerifyIndirectJumps() {
552 if (IndirectJumps.empty()) return;
553
554 // If there aren't any address-of-label expressions in this function,
555 // complain about the first indirect goto.
556 if (IndirectJumpTargets.empty()) {
557 S.Diag(IndirectJumps[0]->getGotoLoc(),
558 diag::err_indirect_goto_without_addrlabel);
559 return;
560 }
561
John McCall5e2a7ac2010-05-12 02:37:54 +0000562 // Collect a single representative of every scope containing an
563 // indirect goto. For most code bases, this substantially cuts
564 // down on the number of jump sites we'll have to consider later.
John McCallddb0b4d2010-05-12 00:58:13 +0000565 typedef std::pair<unsigned, IndirectGotoStmt*> JumpScope;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000566 SmallVector<JumpScope, 32> JumpScopes;
John McCallddb0b4d2010-05-12 00:58:13 +0000567 {
568 llvm::DenseMap<unsigned, IndirectGotoStmt*> JumpScopesMap;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000569 for (SmallVectorImpl<IndirectGotoStmt*>::iterator
John McCallddb0b4d2010-05-12 00:58:13 +0000570 I = IndirectJumps.begin(), E = IndirectJumps.end(); I != E; ++I) {
571 IndirectGotoStmt *IG = *I;
572 assert(LabelAndGotoScopes.count(IG) &&
573 "indirect jump didn't get added to scopes?");
574 unsigned IGScope = LabelAndGotoScopes[IG];
575 IndirectGotoStmt *&Entry = JumpScopesMap[IGScope];
576 if (!Entry) Entry = IG;
577 }
578 JumpScopes.reserve(JumpScopesMap.size());
579 for (llvm::DenseMap<unsigned, IndirectGotoStmt*>::iterator
580 I = JumpScopesMap.begin(), E = JumpScopesMap.end(); I != E; ++I)
581 JumpScopes.push_back(*I);
582 }
583
John McCall5e2a7ac2010-05-12 02:37:54 +0000584 // Collect a single representative of every scope containing a
585 // label whose address was taken somewhere in the function.
586 // For most code bases, there will be only one such scope.
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000587 llvm::DenseMap<unsigned, LabelDecl*> TargetScopes;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000588 for (SmallVectorImpl<LabelDecl*>::iterator
John McCallddb0b4d2010-05-12 00:58:13 +0000589 I = IndirectJumpTargets.begin(), E = IndirectJumpTargets.end();
590 I != E; ++I) {
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000591 LabelDecl *TheLabel = *I;
592 assert(LabelAndGotoScopes.count(TheLabel->getStmt()) &&
John McCallddb0b4d2010-05-12 00:58:13 +0000593 "Referenced label didn't get added to scopes?");
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000594 unsigned LabelScope = LabelAndGotoScopes[TheLabel->getStmt()];
595 LabelDecl *&Target = TargetScopes[LabelScope];
John McCallddb0b4d2010-05-12 00:58:13 +0000596 if (!Target) Target = TheLabel;
597 }
598
John McCall5e2a7ac2010-05-12 02:37:54 +0000599 // For each target scope, make sure it's trivially reachable from
600 // every scope containing a jump site.
601 //
602 // A path between scopes always consists of exitting zero or more
603 // scopes, then entering zero or more scopes. We build a set of
604 // of scopes S from which the target scope can be trivially
605 // entered, then verify that every jump scope can be trivially
606 // exitted to reach a scope in S.
John McCallddb0b4d2010-05-12 00:58:13 +0000607 llvm::BitVector Reachable(Scopes.size(), false);
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000608 for (llvm::DenseMap<unsigned,LabelDecl*>::iterator
John McCallddb0b4d2010-05-12 00:58:13 +0000609 TI = TargetScopes.begin(), TE = TargetScopes.end(); TI != TE; ++TI) {
610 unsigned TargetScope = TI->first;
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000611 LabelDecl *TargetLabel = TI->second;
John McCallddb0b4d2010-05-12 00:58:13 +0000612
613 Reachable.reset();
614
615 // Mark all the enclosing scopes from which you can safely jump
John McCall5e2a7ac2010-05-12 02:37:54 +0000616 // into the target scope. 'Min' will end up being the index of
617 // the shallowest such scope.
John McCallddb0b4d2010-05-12 00:58:13 +0000618 unsigned Min = TargetScope;
619 while (true) {
620 Reachable.set(Min);
621
622 // Don't go beyond the outermost scope.
623 if (Min == 0) break;
624
John McCall5e2a7ac2010-05-12 02:37:54 +0000625 // Stop if we can't trivially enter the current scope.
John McCallddb0b4d2010-05-12 00:58:13 +0000626 if (Scopes[Min].InDiag) break;
627
628 Min = Scopes[Min].ParentScope;
629 }
630
631 // Walk through all the jump sites, checking that they can trivially
632 // reach this label scope.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000633 for (SmallVectorImpl<JumpScope>::iterator
John McCallddb0b4d2010-05-12 00:58:13 +0000634 I = JumpScopes.begin(), E = JumpScopes.end(); I != E; ++I) {
635 unsigned Scope = I->first;
636
637 // Walk out the "scope chain" for this scope, looking for a scope
John McCall5e2a7ac2010-05-12 02:37:54 +0000638 // we've marked reachable. For well-formed code this amortizes
639 // to O(JumpScopes.size() / Scopes.size()): we only iterate
640 // when we see something unmarked, and in well-formed code we
641 // mark everything we iterate past.
John McCallddb0b4d2010-05-12 00:58:13 +0000642 bool IsReachable = false;
643 while (true) {
644 if (Reachable.test(Scope)) {
645 // If we find something reachable, mark all the scopes we just
646 // walked through as reachable.
647 for (unsigned S = I->first; S != Scope; S = Scopes[S].ParentScope)
648 Reachable.set(S);
649 IsReachable = true;
650 break;
651 }
652
653 // Don't walk out if we've reached the top-level scope or we've
654 // gotten shallower than the shallowest reachable scope.
655 if (Scope == 0 || Scope < Min) break;
656
657 // Don't walk out through an out-diagnostic.
658 if (Scopes[Scope].OutDiag) break;
659
660 Scope = Scopes[Scope].ParentScope;
661 }
662
663 // Only diagnose if we didn't find something.
664 if (IsReachable) continue;
665
666 DiagnoseIndirectJump(I->second, I->first, TargetLabel, TargetScope);
667 }
668 }
669}
670
Richard Smith0e9e9812011-10-20 21:42:12 +0000671/// Return true if a particular error+note combination must be downgraded to a
672/// warning in Microsoft mode.
673static bool IsMicrosoftJumpWarning(unsigned JumpDiag, unsigned InDiagNote) {
674 return (JumpDiag == diag::err_goto_into_protected_scope &&
675 (InDiagNote == diag::note_protected_by_variable_init ||
676 InDiagNote == diag::note_protected_by_variable_nontriv_destructor));
677}
678
679/// Return true if a particular note should be downgraded to a compatibility
680/// warning in C++11 mode.
681static bool IsCXX98CompatWarning(Sema &S, unsigned InDiagNote) {
Richard Smith80ad52f2013-01-02 11:42:31 +0000682 return S.getLangOpts().CPlusPlus11 &&
Richard Smith0e9e9812011-10-20 21:42:12 +0000683 InDiagNote == diag::note_protected_by_variable_non_pod;
684}
685
686/// Produce primary diagnostic for an indirect jump statement.
687static void DiagnoseIndirectJumpStmt(Sema &S, IndirectGotoStmt *Jump,
688 LabelDecl *Target, bool &Diagnosed) {
689 if (Diagnosed)
690 return;
691 S.Diag(Jump->getGotoLoc(), diag::err_indirect_goto_in_protected_scope);
692 S.Diag(Target->getStmt()->getIdentLoc(), diag::note_indirect_goto_target);
693 Diagnosed = true;
694}
695
696/// Produce note diagnostics for a jump into a protected scope.
Bill Wendling4fe5be02012-02-22 09:38:11 +0000697void JumpScopeChecker::NoteJumpIntoScopes(ArrayRef<unsigned> ToScopes) {
Richard Smith0e9e9812011-10-20 21:42:12 +0000698 assert(!ToScopes.empty());
699 for (unsigned I = 0, E = ToScopes.size(); I != E; ++I)
700 if (Scopes[ToScopes[I]].InDiag)
701 S.Diag(Scopes[ToScopes[I]].Loc, Scopes[ToScopes[I]].InDiag);
702}
703
John McCall5e2a7ac2010-05-12 02:37:54 +0000704/// Diagnose an indirect jump which is known to cross scopes.
John McCallddb0b4d2010-05-12 00:58:13 +0000705void JumpScopeChecker::DiagnoseIndirectJump(IndirectGotoStmt *Jump,
706 unsigned JumpScope,
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000707 LabelDecl *Target,
John McCallddb0b4d2010-05-12 00:58:13 +0000708 unsigned TargetScope) {
709 assert(JumpScope != TargetScope);
710
John McCall5e2a7ac2010-05-12 02:37:54 +0000711 unsigned Common = GetDeepestCommonScope(JumpScope, TargetScope);
Richard Smith0e9e9812011-10-20 21:42:12 +0000712 bool Diagnosed = false;
John McCallddb0b4d2010-05-12 00:58:13 +0000713
John McCall5e2a7ac2010-05-12 02:37:54 +0000714 // Walk out the scope chain until we reach the common ancestor.
715 for (unsigned I = JumpScope; I != Common; I = Scopes[I].ParentScope)
Richard Smith0e9e9812011-10-20 21:42:12 +0000716 if (Scopes[I].OutDiag) {
717 DiagnoseIndirectJumpStmt(S, Jump, Target, Diagnosed);
John McCall5e2a7ac2010-05-12 02:37:54 +0000718 S.Diag(Scopes[I].Loc, Scopes[I].OutDiag);
Richard Smith0e9e9812011-10-20 21:42:12 +0000719 }
720
721 SmallVector<unsigned, 10> ToScopesCXX98Compat;
John McCallddb0b4d2010-05-12 00:58:13 +0000722
723 // Now walk into the scopes containing the label whose address was taken.
John McCall5e2a7ac2010-05-12 02:37:54 +0000724 for (unsigned I = TargetScope; I != Common; I = Scopes[I].ParentScope)
Richard Smith0e9e9812011-10-20 21:42:12 +0000725 if (IsCXX98CompatWarning(S, Scopes[I].InDiag))
726 ToScopesCXX98Compat.push_back(I);
727 else if (Scopes[I].InDiag) {
728 DiagnoseIndirectJumpStmt(S, Jump, Target, Diagnosed);
John McCall5e2a7ac2010-05-12 02:37:54 +0000729 S.Diag(Scopes[I].Loc, Scopes[I].InDiag);
Richard Smith0e9e9812011-10-20 21:42:12 +0000730 }
John McCallddb0b4d2010-05-12 00:58:13 +0000731
Richard Smith0e9e9812011-10-20 21:42:12 +0000732 // Diagnose this jump if it would be ill-formed in C++98.
733 if (!Diagnosed && !ToScopesCXX98Compat.empty()) {
734 S.Diag(Jump->getGotoLoc(),
735 diag::warn_cxx98_compat_indirect_goto_in_protected_scope);
736 S.Diag(Target->getStmt()->getIdentLoc(), diag::note_indirect_goto_target);
737 NoteJumpIntoScopes(ToScopesCXX98Compat);
738 }
Francois Pichetc985b882011-09-13 10:26:51 +0000739}
740
Chris Lattner5af280c2009-04-19 04:46:21 +0000741/// CheckJump - Validate that the specified jump statement is valid: that it is
742/// jumping within or out of its current scope, not into a deeper one.
Francois Pichetc985b882011-09-13 10:26:51 +0000743void JumpScopeChecker::CheckJump(Stmt *From, Stmt *To, SourceLocation DiagLoc,
Richard Smith0e9e9812011-10-20 21:42:12 +0000744 unsigned JumpDiagError, unsigned JumpDiagWarning,
745 unsigned JumpDiagCXX98Compat) {
Chris Lattner5af280c2009-04-19 04:46:21 +0000746 assert(LabelAndGotoScopes.count(From) && "Jump didn't get added to scopes?");
747 unsigned FromScope = LabelAndGotoScopes[From];
748
749 assert(LabelAndGotoScopes.count(To) && "Jump didn't get added to scopes?");
750 unsigned ToScope = LabelAndGotoScopes[To];
Mike Stump1eb44332009-09-09 15:08:12 +0000751
Chris Lattner5af280c2009-04-19 04:46:21 +0000752 // Common case: exactly the same scope, which is fine.
753 if (FromScope == ToScope) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000754
John McCall5e2a7ac2010-05-12 02:37:54 +0000755 unsigned CommonScope = GetDeepestCommonScope(FromScope, ToScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000756
John McCall5e2a7ac2010-05-12 02:37:54 +0000757 // It's okay to jump out from a nested scope.
758 if (CommonScope == ToScope) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000759
John McCall5e2a7ac2010-05-12 02:37:54 +0000760 // Pull out (and reverse) any scopes we might need to diagnose skipping.
Richard Smith0e9e9812011-10-20 21:42:12 +0000761 SmallVector<unsigned, 10> ToScopesCXX98Compat;
Francois Pichetc985b882011-09-13 10:26:51 +0000762 SmallVector<unsigned, 10> ToScopesError;
763 SmallVector<unsigned, 10> ToScopesWarning;
764 for (unsigned I = ToScope; I != CommonScope; I = Scopes[I].ParentScope) {
David Blaikie4e4d0842012-03-11 07:00:24 +0000765 if (S.getLangOpts().MicrosoftMode && JumpDiagWarning != 0 &&
Francois Pichetc985b882011-09-13 10:26:51 +0000766 IsMicrosoftJumpWarning(JumpDiagError, Scopes[I].InDiag))
767 ToScopesWarning.push_back(I);
Richard Smith0e9e9812011-10-20 21:42:12 +0000768 else if (IsCXX98CompatWarning(S, Scopes[I].InDiag))
769 ToScopesCXX98Compat.push_back(I);
Francois Pichetc985b882011-09-13 10:26:51 +0000770 else if (Scopes[I].InDiag)
771 ToScopesError.push_back(I);
772 }
Chris Lattner5af280c2009-04-19 04:46:21 +0000773
Francois Pichetc985b882011-09-13 10:26:51 +0000774 // Handle warnings.
775 if (!ToScopesWarning.empty()) {
776 S.Diag(DiagLoc, JumpDiagWarning);
Richard Smith0e9e9812011-10-20 21:42:12 +0000777 NoteJumpIntoScopes(ToScopesWarning);
Francois Pichetc985b882011-09-13 10:26:51 +0000778 }
John McCallddb0b4d2010-05-12 00:58:13 +0000779
Francois Pichetc985b882011-09-13 10:26:51 +0000780 // Handle errors.
781 if (!ToScopesError.empty()) {
782 S.Diag(DiagLoc, JumpDiagError);
Richard Smith0e9e9812011-10-20 21:42:12 +0000783 NoteJumpIntoScopes(ToScopesError);
784 }
785
786 // Handle -Wc++98-compat warnings if the jump is well-formed.
787 if (ToScopesError.empty() && !ToScopesCXX98Compat.empty()) {
788 S.Diag(DiagLoc, JumpDiagCXX98Compat);
789 NoteJumpIntoScopes(ToScopesCXX98Compat);
Francois Pichetc985b882011-09-13 10:26:51 +0000790 }
Chris Lattner5af280c2009-04-19 04:46:21 +0000791}
792
793void Sema::DiagnoseInvalidJumps(Stmt *Body) {
Douglas Gregor6490ae52009-11-17 06:14:37 +0000794 (void)JumpScopeChecker(Body, *this);
Chris Lattner5af280c2009-04-19 04:46:21 +0000795}