blob: 54013a8e9c7d1f085a17173de1e6a7460f436184 [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"
Chris Lattner16f00492009-04-26 01:32:48 +000019#include "clang/AST/StmtObjC.h"
Sebastian Redl972041f2009-04-27 20:27:31 +000020#include "clang/AST/StmtCXX.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();
Richard Smith0e9e9812011-10-20 21:42:12 +000077 void NoteJumpIntoScopes(const SmallVectorImpl<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)) {
John McCallddb0b4d2010-05-12 00:58:13 +0000126 unsigned InDiag = 0, OutDiag = 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
138 if (Context.getLangOptions().ObjCAutoRefCount && VD->hasLocalStorage()) {
139 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
152 if (Context.getLangOptions().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
167 if (const Expr *init = VD->getInit()) {
168 // We actually give variables of record type (or array thereof)
169 // an initializer even if that initializer only calls a trivial
170 // ctor. Detect that case.
171 // FIXME: With generalized initializer lists, this may
172 // classify "X x{};" as having no initializer.
173 unsigned inDiagToUse = diag::note_protected_by_variable_init;
174
175 const CXXRecordDecl *record = 0;
176
177 if (const CXXConstructExpr *cce = dyn_cast<CXXConstructExpr>(init)) {
178 const CXXConstructorDecl *ctor = cce->getConstructor();
179 record = ctor->getParent();
180
181 if (ctor->isTrivial() && ctor->isDefaultConstructor()) {
Richard Smith0e9e9812011-10-20 21:42:12 +0000182 if (!record->hasTrivialDestructor())
183 inDiagToUse = diag::note_protected_by_variable_nontriv_destructor;
184 else if (!record->isPOD())
185 inDiagToUse = diag::note_protected_by_variable_non_pod;
186 else
187 inDiagToUse = 0;
Douglas Gregore4135162011-05-27 16:05:29 +0000188 }
John McCallf85e1932011-06-15 23:02:42 +0000189 } else if (VD->getType()->isArrayType()) {
190 record = VD->getType()->getBaseElementTypeUnsafe()
191 ->getAsCXXRecordDecl();
Douglas Gregore4135162011-05-27 16:05:29 +0000192 }
John McCallf85e1932011-06-15 23:02:42 +0000193
194 if (inDiagToUse)
195 InDiag = inDiagToUse;
196
197 // Also object to indirect jumps which leave scopes with dtors.
198 if (record && !record->hasTrivialDestructor())
Douglas Gregorf61103e2011-05-27 21:28:00 +0000199 OutDiag = diag::note_exits_dtor;
Douglas Gregor025291b2010-07-01 00:21:21 +0000200 }
John McCallddb0b4d2010-05-12 00:58:13 +0000201 }
Chris Lattner6d97e5e2010-03-01 20:59:53 +0000202
John McCallf85e1932011-06-15 23:02:42 +0000203 return ScopePair(InDiag, OutDiag);
Chris Lattner5af280c2009-04-19 04:46:21 +0000204 }
Mike Stump1eb44332009-09-09 15:08:12 +0000205
John McCallddb0b4d2010-05-12 00:58:13 +0000206 if (const TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
207 if (TD->getUnderlyingType()->isVariablyModifiedType())
John McCallf85e1932011-06-15 23:02:42 +0000208 return ScopePair(diag::note_protected_by_vla_typedef, 0);
John McCallddb0b4d2010-05-12 00:58:13 +0000209 }
210
Richard Smith162e1c12011-04-15 14:24:37 +0000211 if (const TypeAliasDecl *TD = dyn_cast<TypeAliasDecl>(D)) {
212 if (TD->getUnderlyingType()->isVariablyModifiedType())
John McCallf85e1932011-06-15 23:02:42 +0000213 return ScopePair(diag::note_protected_by_vla_type_alias, 0);
Richard Smith162e1c12011-04-15 14:24:37 +0000214 }
215
John McCallf85e1932011-06-15 23:02:42 +0000216 return ScopePair(0U, 0U);
Chris Lattner5af280c2009-04-19 04:46:21 +0000217}
218
Douglas Gregor43dec6b2010-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 Gregor43dec6b2010-06-21 23:44:13 +0000221 // If this decl causes a new scope, push and switch to it.
John McCallf85e1932011-06-15 23:02:42 +0000222 std::pair<unsigned,unsigned> Diags = GetDiagForGotoScopeDecl(S.Context, D);
Douglas Gregor43dec6b2010-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 }
228
229 // 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 Lattner5af280c2009-04-19 04:46:21 +0000235
Fariborz Jahanian4e7c7f22011-07-11 18:04:54 +0000236/// \brief Build scope information for a captured block literal variables.
237void JumpScopeChecker::BuildScopeInformation(VarDecl *D,
238 const BlockDecl *BDecl,
239 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;
261 case QualType::DK_none:
Richard Smith0e9e9812011-10-20 21:42:12 +0000262 llvm_unreachable("non-lifetime captured variable");
Fariborz Jahanian4e7c7f22011-07-11 18:04:54 +0000263 }
264 SourceLocation Loc = D->getLocation();
265 if (Loc.isInvalid())
266 Loc = BDecl->getLocation();
267 Scopes.push_back(GotoScope(ParentScope,
268 Diags.first, Diags.second, Loc));
269 ParentScope = Scopes.size()-1;
270 }
271}
272
Chris Lattner5af280c2009-04-19 04:46:21 +0000273/// BuildScopeInformation - The statements from CI to CE are known to form a
274/// coherent VLA scope with a specified parent node. Walk through the
275/// statements, adding any labels or gotos to LabelAndGotoScopes and recursively
276/// walking the AST as needed.
Fariborz Jahanian4e7c7f22011-07-11 18:04:54 +0000277void JumpScopeChecker::BuildScopeInformation(Stmt *S, unsigned &origParentScope) {
278 // If this is a statement, rather than an expression, scopes within it don't
279 // propagate out into the enclosing scope. Otherwise we have to worry
280 // about block literals, which have the lifetime of their enclosing statement.
281 unsigned independentParentScope = origParentScope;
282 unsigned &ParentScope = ((isa<Expr>(S) && !isa<StmtExpr>(S))
283 ? origParentScope : independentParentScope);
284
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000285 bool SkipFirstSubStmt = false;
286
Chris Lattner5af280c2009-04-19 04:46:21 +0000287 // If we found a label, remember that it is in ParentScope scope.
John McCallddb0b4d2010-05-12 00:58:13 +0000288 switch (S->getStmtClass()) {
John McCallddb0b4d2010-05-12 00:58:13 +0000289 case Stmt::AddrLabelExprClass:
290 IndirectJumpTargets.push_back(cast<AddrLabelExpr>(S)->getLabel());
291 break;
292
293 case Stmt::IndirectGotoStmtClass:
John McCall95c225d2010-10-28 08:53:48 +0000294 // "goto *&&lbl;" is a special case which we treat as equivalent
295 // to a normal goto. In addition, we don't calculate scope in the
296 // operand (to avoid recording the address-of-label use), which
297 // works only because of the restricted set of expressions which
298 // we detect as constant targets.
299 if (cast<IndirectGotoStmt>(S)->getConstantTarget()) {
300 LabelAndGotoScopes[S] = ParentScope;
301 Jumps.push_back(S);
302 return;
303 }
304
John McCallddb0b4d2010-05-12 00:58:13 +0000305 LabelAndGotoScopes[S] = ParentScope;
306 IndirectJumps.push_back(cast<IndirectGotoStmt>(S));
307 break;
308
John McCallddb0b4d2010-05-12 00:58:13 +0000309 case Stmt::SwitchStmtClass:
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000310 // Evaluate the condition variable before entering the scope of the switch
311 // statement.
312 if (VarDecl *Var = cast<SwitchStmt>(S)->getConditionVariable()) {
313 BuildScopeInformation(Var, ParentScope);
314 SkipFirstSubStmt = true;
315 }
316 // Fall through
317
318 case Stmt::GotoStmtClass:
Chris Lattner5af280c2009-04-19 04:46:21 +0000319 // Remember both what scope a goto is in as well as the fact that we have
320 // it. This makes the second scan not have to walk the AST again.
321 LabelAndGotoScopes[S] = ParentScope;
322 Jumps.push_back(S);
John McCallddb0b4d2010-05-12 00:58:13 +0000323 break;
324
325 default:
326 break;
Chris Lattner5af280c2009-04-19 04:46:21 +0000327 }
Mike Stump1eb44332009-09-09 15:08:12 +0000328
John McCall7502c1d2011-02-13 04:07:26 +0000329 for (Stmt::child_range CI = S->children(); CI; ++CI) {
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000330 if (SkipFirstSubStmt) {
331 SkipFirstSubStmt = false;
332 continue;
333 }
334
Chris Lattner5af280c2009-04-19 04:46:21 +0000335 Stmt *SubStmt = *CI;
336 if (SubStmt == 0) continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000337
John McCall97ba4812010-08-02 23:33:14 +0000338 // Cases, labels, and defaults aren't "scope parents". It's also
339 // important to handle these iteratively instead of recursively in
340 // order to avoid blowing out the stack.
341 while (true) {
342 Stmt *Next;
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000343 if (CaseStmt *CS = dyn_cast<CaseStmt>(SubStmt))
344 Next = CS->getSubStmt();
345 else if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SubStmt))
346 Next = DS->getSubStmt();
347 else if (LabelStmt *LS = dyn_cast<LabelStmt>(SubStmt))
348 Next = LS->getSubStmt();
John McCall97ba4812010-08-02 23:33:14 +0000349 else
350 break;
351
352 LabelAndGotoScopes[SubStmt] = ParentScope;
353 SubStmt = Next;
354 }
355
Chris Lattner5af280c2009-04-19 04:46:21 +0000356 // If this is a declstmt with a VLA definition, it defines a scope from here
357 // to the end of the containing context.
358 if (DeclStmt *DS = dyn_cast<DeclStmt>(SubStmt)) {
Chris Lattner6d97e5e2010-03-01 20:59:53 +0000359 // The decl statement creates a scope if any of the decls in it are VLAs
360 // or have the cleanup attribute.
Chris Lattner5af280c2009-04-19 04:46:21 +0000361 for (DeclStmt::decl_iterator I = DS->decl_begin(), E = DS->decl_end();
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000362 I != E; ++I)
363 BuildScopeInformation(*I, ParentScope);
Chris Lattner5af280c2009-04-19 04:46:21 +0000364 continue;
365 }
Chris Lattner5af280c2009-04-19 04:46:21 +0000366 // Disallow jumps into any part of an @try statement by pushing a scope and
367 // walking all sub-stmts in that scope.
368 if (ObjCAtTryStmt *AT = dyn_cast<ObjCAtTryStmt>(SubStmt)) {
Fariborz Jahanian4e7c7f22011-07-11 18:04:54 +0000369 unsigned newParentScope;
Chris Lattner5af280c2009-04-19 04:46:21 +0000370 // Recursively walk the AST for the @try part.
John McCallddb0b4d2010-05-12 00:58:13 +0000371 Scopes.push_back(GotoScope(ParentScope,
372 diag::note_protected_by_objc_try,
373 diag::note_exits_objc_try,
Chris Lattner5af280c2009-04-19 04:46:21 +0000374 AT->getAtTryLoc()));
375 if (Stmt *TryPart = AT->getTryBody())
Fariborz Jahanian4e7c7f22011-07-11 18:04:54 +0000376 BuildScopeInformation(TryPart, (newParentScope = Scopes.size()-1));
Chris Lattner5af280c2009-04-19 04:46:21 +0000377
378 // Jump from the catch to the finally or try is not valid.
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000379 for (unsigned I = 0, N = AT->getNumCatchStmts(); I != N; ++I) {
380 ObjCAtCatchStmt *AC = AT->getCatchStmt(I);
Chris Lattner5af280c2009-04-19 04:46:21 +0000381 Scopes.push_back(GotoScope(ParentScope,
382 diag::note_protected_by_objc_catch,
John McCallddb0b4d2010-05-12 00:58:13 +0000383 diag::note_exits_objc_catch,
Chris Lattner5af280c2009-04-19 04:46:21 +0000384 AC->getAtCatchLoc()));
Mike Stump1eb44332009-09-09 15:08:12 +0000385 // @catches are nested and it isn't
Fariborz Jahanian4e7c7f22011-07-11 18:04:54 +0000386 BuildScopeInformation(AC->getCatchBody(),
387 (newParentScope = Scopes.size()-1));
Chris Lattner5af280c2009-04-19 04:46:21 +0000388 }
Mike Stump1eb44332009-09-09 15:08:12 +0000389
Chris Lattner5af280c2009-04-19 04:46:21 +0000390 // Jump from the finally to the try or catch is not valid.
391 if (ObjCAtFinallyStmt *AF = AT->getFinallyStmt()) {
392 Scopes.push_back(GotoScope(ParentScope,
393 diag::note_protected_by_objc_finally,
John McCallddb0b4d2010-05-12 00:58:13 +0000394 diag::note_exits_objc_finally,
Chris Lattner5af280c2009-04-19 04:46:21 +0000395 AF->getAtFinallyLoc()));
Fariborz Jahanian4e7c7f22011-07-11 18:04:54 +0000396 BuildScopeInformation(AF, (newParentScope = Scopes.size()-1));
Chris Lattner5af280c2009-04-19 04:46:21 +0000397 }
Mike Stump1eb44332009-09-09 15:08:12 +0000398
Chris Lattner5af280c2009-04-19 04:46:21 +0000399 continue;
400 }
Fariborz Jahanian4e7c7f22011-07-11 18:04:54 +0000401
402 unsigned newParentScope;
Chris Lattner46c3c4b2009-04-21 06:01:00 +0000403 // Disallow jumps into the protected statement of an @synchronized, but
404 // allow jumps into the object expression it protects.
405 if (ObjCAtSynchronizedStmt *AS = dyn_cast<ObjCAtSynchronizedStmt>(SubStmt)){
406 // Recursively walk the AST for the @synchronized object expr, it is
407 // evaluated in the normal scope.
408 BuildScopeInformation(AS->getSynchExpr(), ParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000409
Chris Lattner46c3c4b2009-04-21 06:01:00 +0000410 // Recursively walk the AST for the @synchronized part, protected by a new
411 // scope.
412 Scopes.push_back(GotoScope(ParentScope,
413 diag::note_protected_by_objc_synchronized,
John McCallddb0b4d2010-05-12 00:58:13 +0000414 diag::note_exits_objc_synchronized,
Chris Lattner46c3c4b2009-04-21 06:01:00 +0000415 AS->getAtSynchronizedLoc()));
Fariborz Jahanian4e7c7f22011-07-11 18:04:54 +0000416 BuildScopeInformation(AS->getSynchBody(),
417 (newParentScope = Scopes.size()-1));
Chris Lattner46c3c4b2009-04-21 06:01:00 +0000418 continue;
419 }
Sebastian Redl972041f2009-04-27 20:27:31 +0000420
421 // Disallow jumps into any part of a C++ try statement. This is pretty
422 // much the same as for Obj-C.
423 if (CXXTryStmt *TS = dyn_cast<CXXTryStmt>(SubStmt)) {
John McCallddb0b4d2010-05-12 00:58:13 +0000424 Scopes.push_back(GotoScope(ParentScope,
425 diag::note_protected_by_cxx_try,
426 diag::note_exits_cxx_try,
Sebastian Redl972041f2009-04-27 20:27:31 +0000427 TS->getSourceRange().getBegin()));
428 if (Stmt *TryBlock = TS->getTryBlock())
Fariborz Jahanian4e7c7f22011-07-11 18:04:54 +0000429 BuildScopeInformation(TryBlock, (newParentScope = Scopes.size()-1));
Sebastian Redl972041f2009-04-27 20:27:31 +0000430
431 // Jump from the catch into the try is not allowed either.
Mike Stump1eb44332009-09-09 15:08:12 +0000432 for (unsigned I = 0, E = TS->getNumHandlers(); I != E; ++I) {
Sebastian Redl972041f2009-04-27 20:27:31 +0000433 CXXCatchStmt *CS = TS->getHandler(I);
434 Scopes.push_back(GotoScope(ParentScope,
435 diag::note_protected_by_cxx_catch,
John McCallddb0b4d2010-05-12 00:58:13 +0000436 diag::note_exits_cxx_catch,
Sebastian Redl972041f2009-04-27 20:27:31 +0000437 CS->getSourceRange().getBegin()));
Fariborz Jahanian4e7c7f22011-07-11 18:04:54 +0000438 BuildScopeInformation(CS->getHandlerBlock(),
439 (newParentScope = Scopes.size()-1));
Sebastian Redl972041f2009-04-27 20:27:31 +0000440 }
441
442 continue;
443 }
444
John McCallf85e1932011-06-15 23:02:42 +0000445 // Disallow jumps into the protected statement of an @autoreleasepool.
446 if (ObjCAutoreleasePoolStmt *AS = dyn_cast<ObjCAutoreleasePoolStmt>(SubStmt)){
447 // Recursively walk the AST for the @autoreleasepool part, protected by a new
448 // scope.
449 Scopes.push_back(GotoScope(ParentScope,
450 diag::note_protected_by_objc_autoreleasepool,
451 diag::note_exits_objc_autoreleasepool,
452 AS->getAtLoc()));
Fariborz Jahanian4e7c7f22011-07-11 18:04:54 +0000453 BuildScopeInformation(AS->getSubStmt(), (newParentScope = Scopes.size()-1));
John McCallf85e1932011-06-15 23:02:42 +0000454 continue;
455 }
Fariborz Jahanian4e7c7f22011-07-11 18:04:54 +0000456
457 if (const BlockExpr *BE = dyn_cast<BlockExpr>(SubStmt)) {
458 const BlockDecl *BDecl = BE->getBlockDecl();
459 for (BlockDecl::capture_const_iterator ci = BDecl->capture_begin(),
460 ce = BDecl->capture_end(); ci != ce; ++ci) {
461 VarDecl *variable = ci->getVariable();
462 BuildScopeInformation(variable, BDecl, ParentScope);
463 }
464 }
465
Chris Lattner5af280c2009-04-19 04:46:21 +0000466 // Recursively walk the AST.
467 BuildScopeInformation(SubStmt, ParentScope);
468 }
469}
470
471/// VerifyJumps - Verify each element of the Jumps array to see if they are
472/// valid, emitting diagnostics if not.
473void JumpScopeChecker::VerifyJumps() {
474 while (!Jumps.empty()) {
475 Stmt *Jump = Jumps.pop_back_val();
Mike Stump1eb44332009-09-09 15:08:12 +0000476
477 // With a goto,
Chris Lattner5af280c2009-04-19 04:46:21 +0000478 if (GotoStmt *GS = dyn_cast<GotoStmt>(Jump)) {
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000479 CheckJump(GS, GS->getLabel()->getStmt(), GS->getGotoLoc(),
Francois Pichetc985b882011-09-13 10:26:51 +0000480 diag::err_goto_into_protected_scope,
Richard Smith0e9e9812011-10-20 21:42:12 +0000481 diag::warn_goto_into_protected_scope,
482 diag::warn_cxx98_compat_goto_into_protected_scope);
Chris Lattner5af280c2009-04-19 04:46:21 +0000483 continue;
484 }
Mike Stump1eb44332009-09-09 15:08:12 +0000485
John McCall95c225d2010-10-28 08:53:48 +0000486 // We only get indirect gotos here when they have a constant target.
487 if (IndirectGotoStmt *IGS = dyn_cast<IndirectGotoStmt>(Jump)) {
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000488 LabelDecl *Target = IGS->getConstantTarget();
489 CheckJump(IGS, Target->getStmt(), IGS->getGotoLoc(),
Francois Pichet2e965112011-09-16 23:15:32 +0000490 diag::err_goto_into_protected_scope,
Richard Smith0e9e9812011-10-20 21:42:12 +0000491 diag::warn_goto_into_protected_scope,
492 diag::warn_cxx98_compat_goto_into_protected_scope);
John McCall95c225d2010-10-28 08:53:48 +0000493 continue;
494 }
495
John McCallddb0b4d2010-05-12 00:58:13 +0000496 SwitchStmt *SS = cast<SwitchStmt>(Jump);
497 for (SwitchCase *SC = SS->getSwitchCaseList(); SC;
498 SC = SC->getNextSwitchCase()) {
499 assert(LabelAndGotoScopes.count(SC) && "Case not visited?");
500 CheckJump(SS, SC, SC->getLocStart(),
Richard Smith0e9e9812011-10-20 21:42:12 +0000501 diag::err_switch_into_protected_scope, 0,
502 diag::warn_cxx98_compat_switch_into_protected_scope);
Chris Lattner5af280c2009-04-19 04:46:21 +0000503 }
504 }
505}
506
John McCall5e2a7ac2010-05-12 02:37:54 +0000507/// VerifyIndirectJumps - Verify whether any possible indirect jump
508/// might cross a protection boundary. Unlike direct jumps, indirect
509/// jumps count cleanups as protection boundaries: since there's no
510/// way to know where the jump is going, we can't implicitly run the
511/// right cleanups the way we can with direct jumps.
John McCallddb0b4d2010-05-12 00:58:13 +0000512///
John McCall5e2a7ac2010-05-12 02:37:54 +0000513/// Thus, an indirect jump is "trivial" if it bypasses no
514/// initializations and no teardowns. More formally, an indirect jump
515/// from A to B is trivial if the path out from A to DCA(A,B) is
516/// trivial and the path in from DCA(A,B) to B is trivial, where
517/// DCA(A,B) is the deepest common ancestor of A and B.
518/// Jump-triviality is transitive but asymmetric.
519///
John McCallddb0b4d2010-05-12 00:58:13 +0000520/// A path in is trivial if none of the entered scopes have an InDiag.
521/// A path out is trivial is none of the exited scopes have an OutDiag.
John McCall5e2a7ac2010-05-12 02:37:54 +0000522///
523/// Under these definitions, this function checks that the indirect
524/// jump between A and B is trivial for every indirect goto statement A
525/// and every label B whose address was taken in the function.
John McCallddb0b4d2010-05-12 00:58:13 +0000526void JumpScopeChecker::VerifyIndirectJumps() {
527 if (IndirectJumps.empty()) return;
528
529 // If there aren't any address-of-label expressions in this function,
530 // complain about the first indirect goto.
531 if (IndirectJumpTargets.empty()) {
532 S.Diag(IndirectJumps[0]->getGotoLoc(),
533 diag::err_indirect_goto_without_addrlabel);
534 return;
535 }
536
John McCall5e2a7ac2010-05-12 02:37:54 +0000537 // Collect a single representative of every scope containing an
538 // indirect goto. For most code bases, this substantially cuts
539 // down on the number of jump sites we'll have to consider later.
John McCallddb0b4d2010-05-12 00:58:13 +0000540 typedef std::pair<unsigned, IndirectGotoStmt*> JumpScope;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000541 SmallVector<JumpScope, 32> JumpScopes;
John McCallddb0b4d2010-05-12 00:58:13 +0000542 {
543 llvm::DenseMap<unsigned, IndirectGotoStmt*> JumpScopesMap;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000544 for (SmallVectorImpl<IndirectGotoStmt*>::iterator
John McCallddb0b4d2010-05-12 00:58:13 +0000545 I = IndirectJumps.begin(), E = IndirectJumps.end(); I != E; ++I) {
546 IndirectGotoStmt *IG = *I;
547 assert(LabelAndGotoScopes.count(IG) &&
548 "indirect jump didn't get added to scopes?");
549 unsigned IGScope = LabelAndGotoScopes[IG];
550 IndirectGotoStmt *&Entry = JumpScopesMap[IGScope];
551 if (!Entry) Entry = IG;
552 }
553 JumpScopes.reserve(JumpScopesMap.size());
554 for (llvm::DenseMap<unsigned, IndirectGotoStmt*>::iterator
555 I = JumpScopesMap.begin(), E = JumpScopesMap.end(); I != E; ++I)
556 JumpScopes.push_back(*I);
557 }
558
John McCall5e2a7ac2010-05-12 02:37:54 +0000559 // Collect a single representative of every scope containing a
560 // label whose address was taken somewhere in the function.
561 // For most code bases, there will be only one such scope.
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000562 llvm::DenseMap<unsigned, LabelDecl*> TargetScopes;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000563 for (SmallVectorImpl<LabelDecl*>::iterator
John McCallddb0b4d2010-05-12 00:58:13 +0000564 I = IndirectJumpTargets.begin(), E = IndirectJumpTargets.end();
565 I != E; ++I) {
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000566 LabelDecl *TheLabel = *I;
567 assert(LabelAndGotoScopes.count(TheLabel->getStmt()) &&
John McCallddb0b4d2010-05-12 00:58:13 +0000568 "Referenced label didn't get added to scopes?");
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000569 unsigned LabelScope = LabelAndGotoScopes[TheLabel->getStmt()];
570 LabelDecl *&Target = TargetScopes[LabelScope];
John McCallddb0b4d2010-05-12 00:58:13 +0000571 if (!Target) Target = TheLabel;
572 }
573
John McCall5e2a7ac2010-05-12 02:37:54 +0000574 // For each target scope, make sure it's trivially reachable from
575 // every scope containing a jump site.
576 //
577 // A path between scopes always consists of exitting zero or more
578 // scopes, then entering zero or more scopes. We build a set of
579 // of scopes S from which the target scope can be trivially
580 // entered, then verify that every jump scope can be trivially
581 // exitted to reach a scope in S.
John McCallddb0b4d2010-05-12 00:58:13 +0000582 llvm::BitVector Reachable(Scopes.size(), false);
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000583 for (llvm::DenseMap<unsigned,LabelDecl*>::iterator
John McCallddb0b4d2010-05-12 00:58:13 +0000584 TI = TargetScopes.begin(), TE = TargetScopes.end(); TI != TE; ++TI) {
585 unsigned TargetScope = TI->first;
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000586 LabelDecl *TargetLabel = TI->second;
John McCallddb0b4d2010-05-12 00:58:13 +0000587
588 Reachable.reset();
589
590 // Mark all the enclosing scopes from which you can safely jump
John McCall5e2a7ac2010-05-12 02:37:54 +0000591 // into the target scope. 'Min' will end up being the index of
592 // the shallowest such scope.
John McCallddb0b4d2010-05-12 00:58:13 +0000593 unsigned Min = TargetScope;
594 while (true) {
595 Reachable.set(Min);
596
597 // Don't go beyond the outermost scope.
598 if (Min == 0) break;
599
John McCall5e2a7ac2010-05-12 02:37:54 +0000600 // Stop if we can't trivially enter the current scope.
John McCallddb0b4d2010-05-12 00:58:13 +0000601 if (Scopes[Min].InDiag) break;
602
603 Min = Scopes[Min].ParentScope;
604 }
605
606 // Walk through all the jump sites, checking that they can trivially
607 // reach this label scope.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000608 for (SmallVectorImpl<JumpScope>::iterator
John McCallddb0b4d2010-05-12 00:58:13 +0000609 I = JumpScopes.begin(), E = JumpScopes.end(); I != E; ++I) {
610 unsigned Scope = I->first;
611
612 // Walk out the "scope chain" for this scope, looking for a scope
John McCall5e2a7ac2010-05-12 02:37:54 +0000613 // we've marked reachable. For well-formed code this amortizes
614 // to O(JumpScopes.size() / Scopes.size()): we only iterate
615 // when we see something unmarked, and in well-formed code we
616 // mark everything we iterate past.
John McCallddb0b4d2010-05-12 00:58:13 +0000617 bool IsReachable = false;
618 while (true) {
619 if (Reachable.test(Scope)) {
620 // If we find something reachable, mark all the scopes we just
621 // walked through as reachable.
622 for (unsigned S = I->first; S != Scope; S = Scopes[S].ParentScope)
623 Reachable.set(S);
624 IsReachable = true;
625 break;
626 }
627
628 // Don't walk out if we've reached the top-level scope or we've
629 // gotten shallower than the shallowest reachable scope.
630 if (Scope == 0 || Scope < Min) break;
631
632 // Don't walk out through an out-diagnostic.
633 if (Scopes[Scope].OutDiag) break;
634
635 Scope = Scopes[Scope].ParentScope;
636 }
637
638 // Only diagnose if we didn't find something.
639 if (IsReachable) continue;
640
641 DiagnoseIndirectJump(I->second, I->first, TargetLabel, TargetScope);
642 }
643 }
644}
645
Richard Smith0e9e9812011-10-20 21:42:12 +0000646/// Return true if a particular error+note combination must be downgraded to a
647/// warning in Microsoft mode.
648static bool IsMicrosoftJumpWarning(unsigned JumpDiag, unsigned InDiagNote) {
649 return (JumpDiag == diag::err_goto_into_protected_scope &&
650 (InDiagNote == diag::note_protected_by_variable_init ||
651 InDiagNote == diag::note_protected_by_variable_nontriv_destructor));
652}
653
654/// Return true if a particular note should be downgraded to a compatibility
655/// warning in C++11 mode.
656static bool IsCXX98CompatWarning(Sema &S, unsigned InDiagNote) {
657 return S.getLangOptions().CPlusPlus0x &&
658 InDiagNote == diag::note_protected_by_variable_non_pod;
659}
660
661/// Produce primary diagnostic for an indirect jump statement.
662static void DiagnoseIndirectJumpStmt(Sema &S, IndirectGotoStmt *Jump,
663 LabelDecl *Target, bool &Diagnosed) {
664 if (Diagnosed)
665 return;
666 S.Diag(Jump->getGotoLoc(), diag::err_indirect_goto_in_protected_scope);
667 S.Diag(Target->getStmt()->getIdentLoc(), diag::note_indirect_goto_target);
668 Diagnosed = true;
669}
670
671/// Produce note diagnostics for a jump into a protected scope.
672void JumpScopeChecker::NoteJumpIntoScopes(
673 const SmallVectorImpl<unsigned> &ToScopes) {
674 assert(!ToScopes.empty());
675 for (unsigned I = 0, E = ToScopes.size(); I != E; ++I)
676 if (Scopes[ToScopes[I]].InDiag)
677 S.Diag(Scopes[ToScopes[I]].Loc, Scopes[ToScopes[I]].InDiag);
678}
679
John McCall5e2a7ac2010-05-12 02:37:54 +0000680/// Diagnose an indirect jump which is known to cross scopes.
John McCallddb0b4d2010-05-12 00:58:13 +0000681void JumpScopeChecker::DiagnoseIndirectJump(IndirectGotoStmt *Jump,
682 unsigned JumpScope,
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000683 LabelDecl *Target,
John McCallddb0b4d2010-05-12 00:58:13 +0000684 unsigned TargetScope) {
685 assert(JumpScope != TargetScope);
686
John McCall5e2a7ac2010-05-12 02:37:54 +0000687 unsigned Common = GetDeepestCommonScope(JumpScope, TargetScope);
Richard Smith0e9e9812011-10-20 21:42:12 +0000688 bool Diagnosed = false;
John McCallddb0b4d2010-05-12 00:58:13 +0000689
John McCall5e2a7ac2010-05-12 02:37:54 +0000690 // Walk out the scope chain until we reach the common ancestor.
691 for (unsigned I = JumpScope; I != Common; I = Scopes[I].ParentScope)
Richard Smith0e9e9812011-10-20 21:42:12 +0000692 if (Scopes[I].OutDiag) {
693 DiagnoseIndirectJumpStmt(S, Jump, Target, Diagnosed);
John McCall5e2a7ac2010-05-12 02:37:54 +0000694 S.Diag(Scopes[I].Loc, Scopes[I].OutDiag);
Richard Smith0e9e9812011-10-20 21:42:12 +0000695 }
696
697 SmallVector<unsigned, 10> ToScopesCXX98Compat;
John McCallddb0b4d2010-05-12 00:58:13 +0000698
699 // Now walk into the scopes containing the label whose address was taken.
John McCall5e2a7ac2010-05-12 02:37:54 +0000700 for (unsigned I = TargetScope; I != Common; I = Scopes[I].ParentScope)
Richard Smith0e9e9812011-10-20 21:42:12 +0000701 if (IsCXX98CompatWarning(S, Scopes[I].InDiag))
702 ToScopesCXX98Compat.push_back(I);
703 else if (Scopes[I].InDiag) {
704 DiagnoseIndirectJumpStmt(S, Jump, Target, Diagnosed);
John McCall5e2a7ac2010-05-12 02:37:54 +0000705 S.Diag(Scopes[I].Loc, Scopes[I].InDiag);
Richard Smith0e9e9812011-10-20 21:42:12 +0000706 }
John McCallddb0b4d2010-05-12 00:58:13 +0000707
Richard Smith0e9e9812011-10-20 21:42:12 +0000708 // Diagnose this jump if it would be ill-formed in C++98.
709 if (!Diagnosed && !ToScopesCXX98Compat.empty()) {
710 S.Diag(Jump->getGotoLoc(),
711 diag::warn_cxx98_compat_indirect_goto_in_protected_scope);
712 S.Diag(Target->getStmt()->getIdentLoc(), diag::note_indirect_goto_target);
713 NoteJumpIntoScopes(ToScopesCXX98Compat);
714 }
Francois Pichetc985b882011-09-13 10:26:51 +0000715}
716
Chris Lattner5af280c2009-04-19 04:46:21 +0000717/// CheckJump - Validate that the specified jump statement is valid: that it is
718/// jumping within or out of its current scope, not into a deeper one.
Francois Pichetc985b882011-09-13 10:26:51 +0000719void JumpScopeChecker::CheckJump(Stmt *From, Stmt *To, SourceLocation DiagLoc,
Richard Smith0e9e9812011-10-20 21:42:12 +0000720 unsigned JumpDiagError, unsigned JumpDiagWarning,
721 unsigned JumpDiagCXX98Compat) {
Chris Lattner5af280c2009-04-19 04:46:21 +0000722 assert(LabelAndGotoScopes.count(From) && "Jump didn't get added to scopes?");
723 unsigned FromScope = LabelAndGotoScopes[From];
724
725 assert(LabelAndGotoScopes.count(To) && "Jump didn't get added to scopes?");
726 unsigned ToScope = LabelAndGotoScopes[To];
Mike Stump1eb44332009-09-09 15:08:12 +0000727
Chris Lattner5af280c2009-04-19 04:46:21 +0000728 // Common case: exactly the same scope, which is fine.
729 if (FromScope == ToScope) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000730
John McCall5e2a7ac2010-05-12 02:37:54 +0000731 unsigned CommonScope = GetDeepestCommonScope(FromScope, ToScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000732
John McCall5e2a7ac2010-05-12 02:37:54 +0000733 // It's okay to jump out from a nested scope.
734 if (CommonScope == ToScope) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000735
John McCall5e2a7ac2010-05-12 02:37:54 +0000736 // Pull out (and reverse) any scopes we might need to diagnose skipping.
Richard Smith0e9e9812011-10-20 21:42:12 +0000737 SmallVector<unsigned, 10> ToScopesCXX98Compat;
Francois Pichetc985b882011-09-13 10:26:51 +0000738 SmallVector<unsigned, 10> ToScopesError;
739 SmallVector<unsigned, 10> ToScopesWarning;
740 for (unsigned I = ToScope; I != CommonScope; I = Scopes[I].ParentScope) {
Francois Pichet8b3c99e2011-09-18 21:48:27 +0000741 if (S.getLangOptions().MicrosoftMode && JumpDiagWarning != 0 &&
Francois Pichetc985b882011-09-13 10:26:51 +0000742 IsMicrosoftJumpWarning(JumpDiagError, Scopes[I].InDiag))
743 ToScopesWarning.push_back(I);
Richard Smith0e9e9812011-10-20 21:42:12 +0000744 else if (IsCXX98CompatWarning(S, Scopes[I].InDiag))
745 ToScopesCXX98Compat.push_back(I);
Francois Pichetc985b882011-09-13 10:26:51 +0000746 else if (Scopes[I].InDiag)
747 ToScopesError.push_back(I);
748 }
Chris Lattner5af280c2009-04-19 04:46:21 +0000749
Francois Pichetc985b882011-09-13 10:26:51 +0000750 // Handle warnings.
751 if (!ToScopesWarning.empty()) {
752 S.Diag(DiagLoc, JumpDiagWarning);
Richard Smith0e9e9812011-10-20 21:42:12 +0000753 NoteJumpIntoScopes(ToScopesWarning);
Francois Pichetc985b882011-09-13 10:26:51 +0000754 }
John McCallddb0b4d2010-05-12 00:58:13 +0000755
Francois Pichetc985b882011-09-13 10:26:51 +0000756 // Handle errors.
757 if (!ToScopesError.empty()) {
758 S.Diag(DiagLoc, JumpDiagError);
Richard Smith0e9e9812011-10-20 21:42:12 +0000759 NoteJumpIntoScopes(ToScopesError);
760 }
761
762 // Handle -Wc++98-compat warnings if the jump is well-formed.
763 if (ToScopesError.empty() && !ToScopesCXX98Compat.empty()) {
764 S.Diag(DiagLoc, JumpDiagCXX98Compat);
765 NoteJumpIntoScopes(ToScopesCXX98Compat);
Francois Pichetc985b882011-09-13 10:26:51 +0000766 }
Chris Lattner5af280c2009-04-19 04:46:21 +0000767}
768
769void Sema::DiagnoseInvalidJumps(Stmt *Body) {
Douglas Gregor6490ae52009-11-17 06:14:37 +0000770 (void)JumpScopeChecker(Body, *this);
Chris Lattner5af280c2009-04-19 04:46:21 +0000771}