blob: a07f44221a03eee43655820ac8d47598b0aafdde [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
Chris Lattner1a1fdbd2009-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 Stump11289f42009-09-09 15:08:12 +000044
Richard Smithfe2750d2011-10-20 21:42:12 +000045 /// InDiag - The note to emit if there is a jump into this scope.
John McCallcf819ab2010-05-12 00:58:13 +000046 unsigned InDiag;
47
Richard Smithfe2750d2011-10-20 21:42:12 +000048 /// OutDiag - The note to emit if there is an indirect jump out
John McCallcf819ab2010-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 Stump11289f42009-09-09 15:08:12 +000052
Chris Lattner1a1fdbd2009-04-19 04:46:21 +000053 /// Loc - Location to emit the diagnostic.
54 SourceLocation Loc;
Mike Stump11289f42009-09-09 15:08:12 +000055
John McCallcf819ab2010-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 Lattner1a1fdbd2009-04-19 04:46:21 +000059 };
Mike Stump11289f42009-09-09 15:08:12 +000060
Chris Lattner0e62c1c2011-07-23 10:55:15 +000061 SmallVector<GotoScope, 48> Scopes;
Chris Lattner1a1fdbd2009-04-19 04:46:21 +000062 llvm::DenseMap<Stmt*, unsigned> LabelAndGotoScopes;
Chris Lattner0e62c1c2011-07-23 10:55:15 +000063 SmallVector<Stmt*, 16> Jumps;
John McCallcf819ab2010-05-12 00:58:13 +000064
Chris Lattner0e62c1c2011-07-23 10:55:15 +000065 SmallVector<IndirectGotoStmt*, 4> IndirectJumps;
66 SmallVector<LabelDecl*, 4> IndirectJumpTargets;
Chris Lattner1a1fdbd2009-04-19 04:46:21 +000067public:
68 JumpScopeChecker(Stmt *Body, Sema &S);
69private:
Douglas Gregor27b98ea2010-06-21 23:44:13 +000070 void BuildScopeInformation(Decl *D, unsigned &ParentScope);
Fariborz Jahanian256d39d2011-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 Lattner1a1fdbd2009-04-19 04:46:21 +000075 void VerifyJumps();
John McCallcf819ab2010-05-12 00:58:13 +000076 void VerifyIndirectJumps();
Bill Wendling8ac06af2012-02-22 09:38:11 +000077 void NoteJumpIntoScopes(ArrayRef<unsigned> ToScopes);
John McCallcf819ab2010-05-12 00:58:13 +000078 void DiagnoseIndirectJump(IndirectGotoStmt *IG, unsigned IGScope,
Chris Lattnerc8e630e2011-02-17 07:39:24 +000079 LabelDecl *Target, unsigned TargetScope);
Francois Pichet051f5e52011-09-13 10:26:51 +000080 void CheckJump(Stmt *From, Stmt *To, SourceLocation DiagLoc,
Richard Smithfe2750d2011-10-20 21:42:12 +000081 unsigned JumpDiag, unsigned JumpDiagWarning,
82 unsigned JumpDiagCXX98Compat);
John McCall42f9f1f2010-05-12 02:37:54 +000083
84 unsigned GetDeepestCommonScope(unsigned A, unsigned B);
Chris Lattner1a1fdbd2009-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 McCallcf819ab2010-05-12 00:58:13 +000091 Scopes.push_back(GotoScope(~0U, ~0U, ~0U, SourceLocation()));
Mike Stump11289f42009-09-09 15:08:12 +000092
Chris Lattner1a1fdbd2009-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 Jahanian256d39d2011-07-11 18:04:54 +000095 unsigned BodyParentScope = 0;
96 BuildScopeInformation(Body, BodyParentScope);
Mike Stump11289f42009-09-09 15:08:12 +000097
Chris Lattner1a1fdbd2009-04-19 04:46:21 +000098 // Check that all jumps we saw are kosher.
99 VerifyJumps();
John McCallcf819ab2010-05-12 00:58:13 +0000100 VerifyIndirectJumps();
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000101}
Mike Stump11289f42009-09-09 15:08:12 +0000102
John McCall42f9f1f2010-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 McCall31168b02011-06-15 23:02:42 +0000120typedef std::pair<unsigned,unsigned> ScopePair;
121
Chris Lattner1a1fdbd2009-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.
Richard Smithc934e4f2013-12-12 01:27:02 +0000124static ScopePair GetDiagForGotoScopeDecl(Sema &S, const Decl *D) {
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000125 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola44938a72012-10-28 02:44:03 +0000126 unsigned InDiag = 0;
Richard Smithc934e4f2013-12-12 01:27:02 +0000127 unsigned OutDiag = 0;
128
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000129 if (VD->getType()->isVariablyModifiedType())
John McCallcf819ab2010-05-12 00:58:13 +0000130 InDiag = diag::note_protected_by_vla;
131
John McCall31168b02011-06-15 23:02:42 +0000132 if (VD->hasAttr<BlocksAttr>())
133 return ScopePair(diag::note_protected_by___block,
134 diag::note_exits___block);
135
136 if (VD->hasAttr<CleanupAttr>())
137 return ScopePair(diag::note_protected_by_cleanup,
138 diag::note_exits_cleanup);
139
Richard Smithc934e4f2013-12-12 01:27:02 +0000140 if (VD->hasLocalStorage()) {
141 switch (VD->getType().isDestructedType()) {
142 case QualType::DK_objc_strong_lifetime:
143 case QualType::DK_objc_weak_lifetime:
Argyrios Kyrtzidiscff00d92011-06-24 00:08:59 +0000144 return ScopePair(diag::note_protected_by_objc_ownership,
145 diag::note_exits_objc_ownership);
Richard Smithc934e4f2013-12-12 01:27:02 +0000146
147 case QualType::DK_cxx_destructor:
148 OutDiag = diag::note_exits_dtor;
149 break;
150
151 case QualType::DK_none:
152 break;
John McCall31168b02011-06-15 23:02:42 +0000153 }
154 }
155
Richard Smithc934e4f2013-12-12 01:27:02 +0000156 const Expr *Init = VD->getInit();
157 if (S.Context.getLangOpts().CPlusPlus && VD->hasLocalStorage() && Init) {
Richard Smithfe2750d2011-10-20 21:42:12 +0000158 // C++11 [stmt.dcl]p3:
John McCall31168b02011-06-15 23:02:42 +0000159 // A program that jumps from a point where a variable with automatic
160 // storage duration is not in scope to a point where it is in scope
161 // is ill-formed unless the variable has scalar type, class type with
162 // a trivial default constructor and a trivial destructor, a
163 // cv-qualified version of one of these types, or an array of one of
164 // the preceding types and is declared without an initializer.
165
166 // C++03 [stmt.dcl.p3:
167 // A program that jumps from a point where a local variable
168 // with automatic storage duration is not in scope to a point
169 // where it is in scope is ill-formed unless the variable has
170 // POD type and is declared without an initializer.
171
Richard Smithc934e4f2013-12-12 01:27:02 +0000172 InDiag = diag::note_protected_by_variable_init;
John McCall31168b02011-06-15 23:02:42 +0000173
Richard Smithc934e4f2013-12-12 01:27:02 +0000174 // For a variable of (array of) class type declared without an
175 // initializer, we will have call-style initialization and the initializer
176 // will be the CXXConstructExpr with no intervening nodes.
177 if (const CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(Init)) {
178 const CXXConstructorDecl *Ctor = CCE->getConstructor();
179 if (Ctor->isTrivial() && Ctor->isDefaultConstructor() &&
180 VD->getInitStyle() == VarDecl::CallInit) {
Rafael Espindola44938a72012-10-28 02:44:03 +0000181 if (OutDiag)
182 InDiag = diag::note_protected_by_variable_nontriv_destructor;
Richard Smithc934e4f2013-12-12 01:27:02 +0000183 else if (!Ctor->getParent()->isPOD())
Rafael Espindola44938a72012-10-28 02:44:03 +0000184 InDiag = diag::note_protected_by_variable_non_pod;
Richard Smithc934e4f2013-12-12 01:27:02 +0000185 else
186 InDiag = 0;
Douglas Gregor27d0c442011-05-27 16:05:29 +0000187 }
Douglas Gregor5a5fcd82010-07-01 00:21:21 +0000188 }
John McCallcf819ab2010-05-12 00:58:13 +0000189 }
Rafael Espindola44938a72012-10-28 02:44:03 +0000190
Richard Smithc934e4f2013-12-12 01:27:02 +0000191 return ScopePair(InDiag, OutDiag);
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000192 }
Mike Stump11289f42009-09-09 15:08:12 +0000193
Richard Smithc934e4f2013-12-12 01:27:02 +0000194 if (const TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCallcf819ab2010-05-12 00:58:13 +0000195 if (TD->getUnderlyingType()->isVariablyModifiedType())
Richard Smithc934e4f2013-12-12 01:27:02 +0000196 return ScopePair(isa<TypedefDecl>(TD)
197 ? diag::note_protected_by_vla_typedef
198 : diag::note_protected_by_vla_type_alias,
199 0);
Richard Smithdda56e42011-04-15 14:24:37 +0000200 }
201
John McCall31168b02011-06-15 23:02:42 +0000202 return ScopePair(0U, 0U);
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000203}
204
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000205/// \brief Build scope information for a declaration that is part of a DeclStmt.
206void JumpScopeChecker::BuildScopeInformation(Decl *D, unsigned &ParentScope) {
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000207 // If this decl causes a new scope, push and switch to it.
Richard Smithc934e4f2013-12-12 01:27:02 +0000208 std::pair<unsigned,unsigned> Diags = GetDiagForGotoScopeDecl(S, D);
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000209 if (Diags.first || Diags.second) {
210 Scopes.push_back(GotoScope(ParentScope, Diags.first, Diags.second,
211 D->getLocation()));
212 ParentScope = Scopes.size()-1;
213 }
214
215 // If the decl has an initializer, walk it with the potentially new
216 // scope we just installed.
217 if (VarDecl *VD = dyn_cast<VarDecl>(D))
218 if (Expr *Init = VD->getInit())
219 BuildScopeInformation(Init, ParentScope);
220}
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000221
Fariborz Jahanian256d39d2011-07-11 18:04:54 +0000222/// \brief Build scope information for a captured block literal variables.
223void JumpScopeChecker::BuildScopeInformation(VarDecl *D,
224 const BlockDecl *BDecl,
225 unsigned &ParentScope) {
226 // exclude captured __block variables; there's no destructor
227 // associated with the block literal for them.
228 if (D->hasAttr<BlocksAttr>())
229 return;
230 QualType T = D->getType();
231 QualType::DestructionKind destructKind = T.isDestructedType();
232 if (destructKind != QualType::DK_none) {
233 std::pair<unsigned,unsigned> Diags;
234 switch (destructKind) {
235 case QualType::DK_cxx_destructor:
236 Diags = ScopePair(diag::note_enters_block_captures_cxx_obj,
237 diag::note_exits_block_captures_cxx_obj);
238 break;
239 case QualType::DK_objc_strong_lifetime:
240 Diags = ScopePair(diag::note_enters_block_captures_strong,
241 diag::note_exits_block_captures_strong);
242 break;
243 case QualType::DK_objc_weak_lifetime:
244 Diags = ScopePair(diag::note_enters_block_captures_weak,
245 diag::note_exits_block_captures_weak);
246 break;
247 case QualType::DK_none:
Richard Smithfe2750d2011-10-20 21:42:12 +0000248 llvm_unreachable("non-lifetime captured variable");
Fariborz Jahanian256d39d2011-07-11 18:04:54 +0000249 }
250 SourceLocation Loc = D->getLocation();
251 if (Loc.isInvalid())
252 Loc = BDecl->getLocation();
253 Scopes.push_back(GotoScope(ParentScope,
254 Diags.first, Diags.second, Loc));
255 ParentScope = Scopes.size()-1;
256 }
257}
258
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000259/// BuildScopeInformation - The statements from CI to CE are known to form a
260/// coherent VLA scope with a specified parent node. Walk through the
261/// statements, adding any labels or gotos to LabelAndGotoScopes and recursively
262/// walking the AST as needed.
Fariborz Jahanian256d39d2011-07-11 18:04:54 +0000263void JumpScopeChecker::BuildScopeInformation(Stmt *S, unsigned &origParentScope) {
264 // If this is a statement, rather than an expression, scopes within it don't
265 // propagate out into the enclosing scope. Otherwise we have to worry
266 // about block literals, which have the lifetime of their enclosing statement.
267 unsigned independentParentScope = origParentScope;
268 unsigned &ParentScope = ((isa<Expr>(S) && !isa<StmtExpr>(S))
269 ? origParentScope : independentParentScope);
270
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000271 bool SkipFirstSubStmt = false;
272
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000273 // If we found a label, remember that it is in ParentScope scope.
John McCallcf819ab2010-05-12 00:58:13 +0000274 switch (S->getStmtClass()) {
John McCallcf819ab2010-05-12 00:58:13 +0000275 case Stmt::AddrLabelExprClass:
276 IndirectJumpTargets.push_back(cast<AddrLabelExpr>(S)->getLabel());
277 break;
278
279 case Stmt::IndirectGotoStmtClass:
John McCall9de91602010-10-28 08:53:48 +0000280 // "goto *&&lbl;" is a special case which we treat as equivalent
281 // to a normal goto. In addition, we don't calculate scope in the
282 // operand (to avoid recording the address-of-label use), which
283 // works only because of the restricted set of expressions which
284 // we detect as constant targets.
285 if (cast<IndirectGotoStmt>(S)->getConstantTarget()) {
286 LabelAndGotoScopes[S] = ParentScope;
287 Jumps.push_back(S);
288 return;
289 }
290
John McCallcf819ab2010-05-12 00:58:13 +0000291 LabelAndGotoScopes[S] = ParentScope;
292 IndirectJumps.push_back(cast<IndirectGotoStmt>(S));
293 break;
294
John McCallcf819ab2010-05-12 00:58:13 +0000295 case Stmt::SwitchStmtClass:
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000296 // Evaluate the condition variable before entering the scope of the switch
297 // statement.
298 if (VarDecl *Var = cast<SwitchStmt>(S)->getConditionVariable()) {
299 BuildScopeInformation(Var, ParentScope);
300 SkipFirstSubStmt = true;
301 }
302 // Fall through
303
304 case Stmt::GotoStmtClass:
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000305 // Remember both what scope a goto is in as well as the fact that we have
306 // it. This makes the second scan not have to walk the AST again.
307 LabelAndGotoScopes[S] = ParentScope;
308 Jumps.push_back(S);
John McCallcf819ab2010-05-12 00:58:13 +0000309 break;
310
Eli Friedman1e95d4b2012-10-31 23:55:28 +0000311 case Stmt::CXXTryStmtClass: {
312 CXXTryStmt *TS = cast<CXXTryStmt>(S);
313 unsigned newParentScope;
314 Scopes.push_back(GotoScope(ParentScope,
315 diag::note_protected_by_cxx_try,
316 diag::note_exits_cxx_try,
317 TS->getSourceRange().getBegin()));
318 if (Stmt *TryBlock = TS->getTryBlock())
319 BuildScopeInformation(TryBlock, (newParentScope = Scopes.size()-1));
320
321 // Jump from the catch into the try is not allowed either.
322 for (unsigned I = 0, E = TS->getNumHandlers(); I != E; ++I) {
323 CXXCatchStmt *CS = TS->getHandler(I);
324 Scopes.push_back(GotoScope(ParentScope,
325 diag::note_protected_by_cxx_catch,
326 diag::note_exits_cxx_catch,
327 CS->getSourceRange().getBegin()));
328 BuildScopeInformation(CS->getHandlerBlock(),
329 (newParentScope = Scopes.size()-1));
330 }
331 return;
332 }
333
John McCallcf819ab2010-05-12 00:58:13 +0000334 default:
335 break;
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000336 }
Mike Stump11289f42009-09-09 15:08:12 +0000337
John McCall8322c3a2011-02-13 04:07:26 +0000338 for (Stmt::child_range CI = S->children(); CI; ++CI) {
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000339 if (SkipFirstSubStmt) {
340 SkipFirstSubStmt = false;
341 continue;
342 }
343
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000344 Stmt *SubStmt = *CI;
345 if (SubStmt == 0) continue;
Mike Stump11289f42009-09-09 15:08:12 +0000346
John McCall4a33fa92010-08-02 23:33:14 +0000347 // Cases, labels, and defaults aren't "scope parents". It's also
348 // important to handle these iteratively instead of recursively in
349 // order to avoid blowing out the stack.
350 while (true) {
351 Stmt *Next;
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000352 if (CaseStmt *CS = dyn_cast<CaseStmt>(SubStmt))
353 Next = CS->getSubStmt();
354 else if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SubStmt))
355 Next = DS->getSubStmt();
356 else if (LabelStmt *LS = dyn_cast<LabelStmt>(SubStmt))
357 Next = LS->getSubStmt();
John McCall4a33fa92010-08-02 23:33:14 +0000358 else
359 break;
360
361 LabelAndGotoScopes[SubStmt] = ParentScope;
362 SubStmt = Next;
363 }
364
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000365 // If this is a declstmt with a VLA definition, it defines a scope from here
366 // to the end of the containing context.
367 if (DeclStmt *DS = dyn_cast<DeclStmt>(SubStmt)) {
Chris Lattner30d0cfd2010-03-01 20:59:53 +0000368 // The decl statement creates a scope if any of the decls in it are VLAs
369 // or have the cleanup attribute.
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000370 for (DeclStmt::decl_iterator I = DS->decl_begin(), E = DS->decl_end();
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000371 I != E; ++I)
372 BuildScopeInformation(*I, ParentScope);
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000373 continue;
374 }
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000375 // Disallow jumps into any part of an @try statement by pushing a scope and
376 // walking all sub-stmts in that scope.
377 if (ObjCAtTryStmt *AT = dyn_cast<ObjCAtTryStmt>(SubStmt)) {
Fariborz Jahanian256d39d2011-07-11 18:04:54 +0000378 unsigned newParentScope;
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000379 // Recursively walk the AST for the @try part.
John McCallcf819ab2010-05-12 00:58:13 +0000380 Scopes.push_back(GotoScope(ParentScope,
381 diag::note_protected_by_objc_try,
382 diag::note_exits_objc_try,
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000383 AT->getAtTryLoc()));
384 if (Stmt *TryPart = AT->getTryBody())
Fariborz Jahanian256d39d2011-07-11 18:04:54 +0000385 BuildScopeInformation(TryPart, (newParentScope = Scopes.size()-1));
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000386
387 // Jump from the catch to the finally or try is not valid.
Douglas Gregor96c79492010-04-23 22:50:49 +0000388 for (unsigned I = 0, N = AT->getNumCatchStmts(); I != N; ++I) {
389 ObjCAtCatchStmt *AC = AT->getCatchStmt(I);
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000390 Scopes.push_back(GotoScope(ParentScope,
391 diag::note_protected_by_objc_catch,
John McCallcf819ab2010-05-12 00:58:13 +0000392 diag::note_exits_objc_catch,
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000393 AC->getAtCatchLoc()));
Mike Stump11289f42009-09-09 15:08:12 +0000394 // @catches are nested and it isn't
Fariborz Jahanian256d39d2011-07-11 18:04:54 +0000395 BuildScopeInformation(AC->getCatchBody(),
396 (newParentScope = Scopes.size()-1));
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000397 }
Mike Stump11289f42009-09-09 15:08:12 +0000398
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000399 // Jump from the finally to the try or catch is not valid.
400 if (ObjCAtFinallyStmt *AF = AT->getFinallyStmt()) {
401 Scopes.push_back(GotoScope(ParentScope,
402 diag::note_protected_by_objc_finally,
John McCallcf819ab2010-05-12 00:58:13 +0000403 diag::note_exits_objc_finally,
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000404 AF->getAtFinallyLoc()));
Fariborz Jahanian256d39d2011-07-11 18:04:54 +0000405 BuildScopeInformation(AF, (newParentScope = Scopes.size()-1));
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000406 }
Mike Stump11289f42009-09-09 15:08:12 +0000407
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000408 continue;
409 }
Fariborz Jahanian256d39d2011-07-11 18:04:54 +0000410
411 unsigned newParentScope;
Chris Lattnerc70dd562009-04-21 06:01:00 +0000412 // Disallow jumps into the protected statement of an @synchronized, but
413 // allow jumps into the object expression it protects.
414 if (ObjCAtSynchronizedStmt *AS = dyn_cast<ObjCAtSynchronizedStmt>(SubStmt)){
415 // Recursively walk the AST for the @synchronized object expr, it is
416 // evaluated in the normal scope.
417 BuildScopeInformation(AS->getSynchExpr(), ParentScope);
Mike Stump11289f42009-09-09 15:08:12 +0000418
Chris Lattnerc70dd562009-04-21 06:01:00 +0000419 // Recursively walk the AST for the @synchronized part, protected by a new
420 // scope.
421 Scopes.push_back(GotoScope(ParentScope,
422 diag::note_protected_by_objc_synchronized,
John McCallcf819ab2010-05-12 00:58:13 +0000423 diag::note_exits_objc_synchronized,
Chris Lattnerc70dd562009-04-21 06:01:00 +0000424 AS->getAtSynchronizedLoc()));
Fariborz Jahanian256d39d2011-07-11 18:04:54 +0000425 BuildScopeInformation(AS->getSynchBody(),
426 (newParentScope = Scopes.size()-1));
Chris Lattnerc70dd562009-04-21 06:01:00 +0000427 continue;
428 }
Sebastian Redl4de47b42009-04-27 20:27:31 +0000429
John McCall31168b02011-06-15 23:02:42 +0000430 // Disallow jumps into the protected statement of an @autoreleasepool.
431 if (ObjCAutoreleasePoolStmt *AS = dyn_cast<ObjCAutoreleasePoolStmt>(SubStmt)){
432 // Recursively walk the AST for the @autoreleasepool part, protected by a new
433 // scope.
434 Scopes.push_back(GotoScope(ParentScope,
435 diag::note_protected_by_objc_autoreleasepool,
436 diag::note_exits_objc_autoreleasepool,
437 AS->getAtLoc()));
Fariborz Jahanian256d39d2011-07-11 18:04:54 +0000438 BuildScopeInformation(AS->getSubStmt(), (newParentScope = Scopes.size()-1));
John McCall31168b02011-06-15 23:02:42 +0000439 continue;
440 }
John McCallb0433ee2012-09-25 06:56:03 +0000441
442 // Disallow jumps past full-expressions that use blocks with
443 // non-trivial cleanups of their captures. This is theoretically
444 // implementable but a lot of work which we haven't felt up to doing.
445 if (ExprWithCleanups *EWC = dyn_cast<ExprWithCleanups>(SubStmt)) {
446 for (unsigned i = 0, e = EWC->getNumObjects(); i != e; ++i) {
447 const BlockDecl *BDecl = EWC->getObject(i);
Fariborz Jahanian256d39d2011-07-11 18:04:54 +0000448 for (BlockDecl::capture_const_iterator ci = BDecl->capture_begin(),
449 ce = BDecl->capture_end(); ci != ce; ++ci) {
450 VarDecl *variable = ci->getVariable();
451 BuildScopeInformation(variable, BDecl, ParentScope);
452 }
John McCallb0433ee2012-09-25 06:56:03 +0000453 }
Fariborz Jahanian256d39d2011-07-11 18:04:54 +0000454 }
Richard Smithc934e4f2013-12-12 01:27:02 +0000455
456 // Disallow jumps out of scopes containing temporaries lifetime-extended to
457 // automatic storage duration.
458 if (MaterializeTemporaryExpr *MTE =
459 dyn_cast<MaterializeTemporaryExpr>(SubStmt)) {
460 if (MTE->getStorageDuration() == SD_Automatic) {
461 SmallVector<const Expr *, 4> CommaLHS;
462 SmallVector<SubobjectAdjustment, 4> Adjustments;
463 const Expr *ExtendedObject =
464 MTE->GetTemporaryExpr()->skipRValueSubobjectAdjustments(
465 CommaLHS, Adjustments);
466 if (ExtendedObject->getType().isDestructedType()) {
467 Scopes.push_back(GotoScope(ParentScope, 0,
468 diag::note_exits_temporary_dtor,
469 ExtendedObject->getExprLoc()));
470 ParentScope = Scopes.size()-1;
471 }
472 }
473 }
474
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000475 // Recursively walk the AST.
476 BuildScopeInformation(SubStmt, ParentScope);
477 }
478}
479
480/// VerifyJumps - Verify each element of the Jumps array to see if they are
481/// valid, emitting diagnostics if not.
482void JumpScopeChecker::VerifyJumps() {
483 while (!Jumps.empty()) {
484 Stmt *Jump = Jumps.pop_back_val();
Mike Stump11289f42009-09-09 15:08:12 +0000485
486 // With a goto,
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000487 if (GotoStmt *GS = dyn_cast<GotoStmt>(Jump)) {
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000488 CheckJump(GS, GS->getLabel()->getStmt(), GS->getGotoLoc(),
Francois Pichet051f5e52011-09-13 10:26:51 +0000489 diag::err_goto_into_protected_scope,
Richard Smithfe2750d2011-10-20 21:42:12 +0000490 diag::warn_goto_into_protected_scope,
491 diag::warn_cxx98_compat_goto_into_protected_scope);
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000492 continue;
493 }
Mike Stump11289f42009-09-09 15:08:12 +0000494
John McCall9de91602010-10-28 08:53:48 +0000495 // We only get indirect gotos here when they have a constant target.
496 if (IndirectGotoStmt *IGS = dyn_cast<IndirectGotoStmt>(Jump)) {
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000497 LabelDecl *Target = IGS->getConstantTarget();
498 CheckJump(IGS, Target->getStmt(), IGS->getGotoLoc(),
Francois Pichet2f550192011-09-16 23:15:32 +0000499 diag::err_goto_into_protected_scope,
Richard Smithfe2750d2011-10-20 21:42:12 +0000500 diag::warn_goto_into_protected_scope,
501 diag::warn_cxx98_compat_goto_into_protected_scope);
John McCall9de91602010-10-28 08:53:48 +0000502 continue;
503 }
504
John McCallcf819ab2010-05-12 00:58:13 +0000505 SwitchStmt *SS = cast<SwitchStmt>(Jump);
506 for (SwitchCase *SC = SS->getSwitchCaseList(); SC;
507 SC = SC->getNextSwitchCase()) {
508 assert(LabelAndGotoScopes.count(SC) && "Case not visited?");
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000509 SourceLocation Loc;
510 if (CaseStmt *CS = dyn_cast<CaseStmt>(SC))
511 Loc = CS->getLocStart();
512 else if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC))
513 Loc = DS->getLocStart();
514 else
515 Loc = SC->getLocStart();
516 CheckJump(SS, SC, Loc, diag::err_switch_into_protected_scope, 0,
Richard Smithfe2750d2011-10-20 21:42:12 +0000517 diag::warn_cxx98_compat_switch_into_protected_scope);
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000518 }
519 }
520}
521
John McCall42f9f1f2010-05-12 02:37:54 +0000522/// VerifyIndirectJumps - Verify whether any possible indirect jump
523/// might cross a protection boundary. Unlike direct jumps, indirect
524/// jumps count cleanups as protection boundaries: since there's no
525/// way to know where the jump is going, we can't implicitly run the
526/// right cleanups the way we can with direct jumps.
John McCallcf819ab2010-05-12 00:58:13 +0000527///
John McCall42f9f1f2010-05-12 02:37:54 +0000528/// Thus, an indirect jump is "trivial" if it bypasses no
529/// initializations and no teardowns. More formally, an indirect jump
530/// from A to B is trivial if the path out from A to DCA(A,B) is
531/// trivial and the path in from DCA(A,B) to B is trivial, where
532/// DCA(A,B) is the deepest common ancestor of A and B.
533/// Jump-triviality is transitive but asymmetric.
534///
John McCallcf819ab2010-05-12 00:58:13 +0000535/// A path in is trivial if none of the entered scopes have an InDiag.
536/// A path out is trivial is none of the exited scopes have an OutDiag.
John McCall42f9f1f2010-05-12 02:37:54 +0000537///
538/// Under these definitions, this function checks that the indirect
539/// jump between A and B is trivial for every indirect goto statement A
540/// and every label B whose address was taken in the function.
John McCallcf819ab2010-05-12 00:58:13 +0000541void JumpScopeChecker::VerifyIndirectJumps() {
542 if (IndirectJumps.empty()) return;
543
544 // If there aren't any address-of-label expressions in this function,
545 // complain about the first indirect goto.
546 if (IndirectJumpTargets.empty()) {
547 S.Diag(IndirectJumps[0]->getGotoLoc(),
548 diag::err_indirect_goto_without_addrlabel);
549 return;
550 }
551
John McCall42f9f1f2010-05-12 02:37:54 +0000552 // Collect a single representative of every scope containing an
553 // indirect goto. For most code bases, this substantially cuts
554 // down on the number of jump sites we'll have to consider later.
John McCallcf819ab2010-05-12 00:58:13 +0000555 typedef std::pair<unsigned, IndirectGotoStmt*> JumpScope;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000556 SmallVector<JumpScope, 32> JumpScopes;
John McCallcf819ab2010-05-12 00:58:13 +0000557 {
558 llvm::DenseMap<unsigned, IndirectGotoStmt*> JumpScopesMap;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000559 for (SmallVectorImpl<IndirectGotoStmt*>::iterator
John McCallcf819ab2010-05-12 00:58:13 +0000560 I = IndirectJumps.begin(), E = IndirectJumps.end(); I != E; ++I) {
561 IndirectGotoStmt *IG = *I;
562 assert(LabelAndGotoScopes.count(IG) &&
563 "indirect jump didn't get added to scopes?");
564 unsigned IGScope = LabelAndGotoScopes[IG];
565 IndirectGotoStmt *&Entry = JumpScopesMap[IGScope];
566 if (!Entry) Entry = IG;
567 }
568 JumpScopes.reserve(JumpScopesMap.size());
569 for (llvm::DenseMap<unsigned, IndirectGotoStmt*>::iterator
570 I = JumpScopesMap.begin(), E = JumpScopesMap.end(); I != E; ++I)
571 JumpScopes.push_back(*I);
572 }
573
John McCall42f9f1f2010-05-12 02:37:54 +0000574 // Collect a single representative of every scope containing a
575 // label whose address was taken somewhere in the function.
576 // For most code bases, there will be only one such scope.
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000577 llvm::DenseMap<unsigned, LabelDecl*> TargetScopes;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000578 for (SmallVectorImpl<LabelDecl*>::iterator
John McCallcf819ab2010-05-12 00:58:13 +0000579 I = IndirectJumpTargets.begin(), E = IndirectJumpTargets.end();
580 I != E; ++I) {
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000581 LabelDecl *TheLabel = *I;
582 assert(LabelAndGotoScopes.count(TheLabel->getStmt()) &&
John McCallcf819ab2010-05-12 00:58:13 +0000583 "Referenced label didn't get added to scopes?");
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000584 unsigned LabelScope = LabelAndGotoScopes[TheLabel->getStmt()];
585 LabelDecl *&Target = TargetScopes[LabelScope];
John McCallcf819ab2010-05-12 00:58:13 +0000586 if (!Target) Target = TheLabel;
587 }
588
John McCall42f9f1f2010-05-12 02:37:54 +0000589 // For each target scope, make sure it's trivially reachable from
590 // every scope containing a jump site.
591 //
592 // A path between scopes always consists of exitting zero or more
593 // scopes, then entering zero or more scopes. We build a set of
594 // of scopes S from which the target scope can be trivially
595 // entered, then verify that every jump scope can be trivially
596 // exitted to reach a scope in S.
John McCallcf819ab2010-05-12 00:58:13 +0000597 llvm::BitVector Reachable(Scopes.size(), false);
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000598 for (llvm::DenseMap<unsigned,LabelDecl*>::iterator
John McCallcf819ab2010-05-12 00:58:13 +0000599 TI = TargetScopes.begin(), TE = TargetScopes.end(); TI != TE; ++TI) {
600 unsigned TargetScope = TI->first;
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000601 LabelDecl *TargetLabel = TI->second;
John McCallcf819ab2010-05-12 00:58:13 +0000602
603 Reachable.reset();
604
605 // Mark all the enclosing scopes from which you can safely jump
John McCall42f9f1f2010-05-12 02:37:54 +0000606 // into the target scope. 'Min' will end up being the index of
607 // the shallowest such scope.
John McCallcf819ab2010-05-12 00:58:13 +0000608 unsigned Min = TargetScope;
609 while (true) {
610 Reachable.set(Min);
611
612 // Don't go beyond the outermost scope.
613 if (Min == 0) break;
614
John McCall42f9f1f2010-05-12 02:37:54 +0000615 // Stop if we can't trivially enter the current scope.
John McCallcf819ab2010-05-12 00:58:13 +0000616 if (Scopes[Min].InDiag) break;
617
618 Min = Scopes[Min].ParentScope;
619 }
620
621 // Walk through all the jump sites, checking that they can trivially
622 // reach this label scope.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000623 for (SmallVectorImpl<JumpScope>::iterator
John McCallcf819ab2010-05-12 00:58:13 +0000624 I = JumpScopes.begin(), E = JumpScopes.end(); I != E; ++I) {
625 unsigned Scope = I->first;
626
627 // Walk out the "scope chain" for this scope, looking for a scope
John McCall42f9f1f2010-05-12 02:37:54 +0000628 // we've marked reachable. For well-formed code this amortizes
629 // to O(JumpScopes.size() / Scopes.size()): we only iterate
630 // when we see something unmarked, and in well-formed code we
631 // mark everything we iterate past.
John McCallcf819ab2010-05-12 00:58:13 +0000632 bool IsReachable = false;
633 while (true) {
634 if (Reachable.test(Scope)) {
635 // If we find something reachable, mark all the scopes we just
636 // walked through as reachable.
637 for (unsigned S = I->first; S != Scope; S = Scopes[S].ParentScope)
638 Reachable.set(S);
639 IsReachable = true;
640 break;
641 }
642
643 // Don't walk out if we've reached the top-level scope or we've
644 // gotten shallower than the shallowest reachable scope.
645 if (Scope == 0 || Scope < Min) break;
646
647 // Don't walk out through an out-diagnostic.
648 if (Scopes[Scope].OutDiag) break;
649
650 Scope = Scopes[Scope].ParentScope;
651 }
652
653 // Only diagnose if we didn't find something.
654 if (IsReachable) continue;
655
656 DiagnoseIndirectJump(I->second, I->first, TargetLabel, TargetScope);
657 }
658 }
659}
660
Richard Smithfe2750d2011-10-20 21:42:12 +0000661/// Return true if a particular error+note combination must be downgraded to a
662/// warning in Microsoft mode.
663static bool IsMicrosoftJumpWarning(unsigned JumpDiag, unsigned InDiagNote) {
664 return (JumpDiag == diag::err_goto_into_protected_scope &&
665 (InDiagNote == diag::note_protected_by_variable_init ||
666 InDiagNote == diag::note_protected_by_variable_nontriv_destructor));
667}
668
669/// Return true if a particular note should be downgraded to a compatibility
670/// warning in C++11 mode.
671static bool IsCXX98CompatWarning(Sema &S, unsigned InDiagNote) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000672 return S.getLangOpts().CPlusPlus11 &&
Richard Smithfe2750d2011-10-20 21:42:12 +0000673 InDiagNote == diag::note_protected_by_variable_non_pod;
674}
675
676/// Produce primary diagnostic for an indirect jump statement.
677static void DiagnoseIndirectJumpStmt(Sema &S, IndirectGotoStmt *Jump,
678 LabelDecl *Target, bool &Diagnosed) {
679 if (Diagnosed)
680 return;
681 S.Diag(Jump->getGotoLoc(), diag::err_indirect_goto_in_protected_scope);
682 S.Diag(Target->getStmt()->getIdentLoc(), diag::note_indirect_goto_target);
683 Diagnosed = true;
684}
685
686/// Produce note diagnostics for a jump into a protected scope.
Bill Wendling8ac06af2012-02-22 09:38:11 +0000687void JumpScopeChecker::NoteJumpIntoScopes(ArrayRef<unsigned> ToScopes) {
Richard Smithfe2750d2011-10-20 21:42:12 +0000688 assert(!ToScopes.empty());
689 for (unsigned I = 0, E = ToScopes.size(); I != E; ++I)
690 if (Scopes[ToScopes[I]].InDiag)
691 S.Diag(Scopes[ToScopes[I]].Loc, Scopes[ToScopes[I]].InDiag);
692}
693
John McCall42f9f1f2010-05-12 02:37:54 +0000694/// Diagnose an indirect jump which is known to cross scopes.
John McCallcf819ab2010-05-12 00:58:13 +0000695void JumpScopeChecker::DiagnoseIndirectJump(IndirectGotoStmt *Jump,
696 unsigned JumpScope,
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000697 LabelDecl *Target,
John McCallcf819ab2010-05-12 00:58:13 +0000698 unsigned TargetScope) {
699 assert(JumpScope != TargetScope);
700
John McCall42f9f1f2010-05-12 02:37:54 +0000701 unsigned Common = GetDeepestCommonScope(JumpScope, TargetScope);
Richard Smithfe2750d2011-10-20 21:42:12 +0000702 bool Diagnosed = false;
John McCallcf819ab2010-05-12 00:58:13 +0000703
John McCall42f9f1f2010-05-12 02:37:54 +0000704 // Walk out the scope chain until we reach the common ancestor.
705 for (unsigned I = JumpScope; I != Common; I = Scopes[I].ParentScope)
Richard Smithfe2750d2011-10-20 21:42:12 +0000706 if (Scopes[I].OutDiag) {
707 DiagnoseIndirectJumpStmt(S, Jump, Target, Diagnosed);
John McCall42f9f1f2010-05-12 02:37:54 +0000708 S.Diag(Scopes[I].Loc, Scopes[I].OutDiag);
Richard Smithfe2750d2011-10-20 21:42:12 +0000709 }
710
711 SmallVector<unsigned, 10> ToScopesCXX98Compat;
John McCallcf819ab2010-05-12 00:58:13 +0000712
713 // Now walk into the scopes containing the label whose address was taken.
John McCall42f9f1f2010-05-12 02:37:54 +0000714 for (unsigned I = TargetScope; I != Common; I = Scopes[I].ParentScope)
Richard Smithfe2750d2011-10-20 21:42:12 +0000715 if (IsCXX98CompatWarning(S, Scopes[I].InDiag))
716 ToScopesCXX98Compat.push_back(I);
717 else if (Scopes[I].InDiag) {
718 DiagnoseIndirectJumpStmt(S, Jump, Target, Diagnosed);
John McCall42f9f1f2010-05-12 02:37:54 +0000719 S.Diag(Scopes[I].Loc, Scopes[I].InDiag);
Richard Smithfe2750d2011-10-20 21:42:12 +0000720 }
John McCallcf819ab2010-05-12 00:58:13 +0000721
Richard Smithfe2750d2011-10-20 21:42:12 +0000722 // Diagnose this jump if it would be ill-formed in C++98.
723 if (!Diagnosed && !ToScopesCXX98Compat.empty()) {
724 S.Diag(Jump->getGotoLoc(),
725 diag::warn_cxx98_compat_indirect_goto_in_protected_scope);
726 S.Diag(Target->getStmt()->getIdentLoc(), diag::note_indirect_goto_target);
727 NoteJumpIntoScopes(ToScopesCXX98Compat);
728 }
Francois Pichet051f5e52011-09-13 10:26:51 +0000729}
730
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000731/// CheckJump - Validate that the specified jump statement is valid: that it is
732/// jumping within or out of its current scope, not into a deeper one.
Francois Pichet051f5e52011-09-13 10:26:51 +0000733void JumpScopeChecker::CheckJump(Stmt *From, Stmt *To, SourceLocation DiagLoc,
Richard Smithfe2750d2011-10-20 21:42:12 +0000734 unsigned JumpDiagError, unsigned JumpDiagWarning,
735 unsigned JumpDiagCXX98Compat) {
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000736 assert(LabelAndGotoScopes.count(From) && "Jump didn't get added to scopes?");
737 unsigned FromScope = LabelAndGotoScopes[From];
738
739 assert(LabelAndGotoScopes.count(To) && "Jump didn't get added to scopes?");
740 unsigned ToScope = LabelAndGotoScopes[To];
Mike Stump11289f42009-09-09 15:08:12 +0000741
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000742 // Common case: exactly the same scope, which is fine.
743 if (FromScope == ToScope) return;
Mike Stump11289f42009-09-09 15:08:12 +0000744
John McCall42f9f1f2010-05-12 02:37:54 +0000745 unsigned CommonScope = GetDeepestCommonScope(FromScope, ToScope);
Mike Stump11289f42009-09-09 15:08:12 +0000746
John McCall42f9f1f2010-05-12 02:37:54 +0000747 // It's okay to jump out from a nested scope.
748 if (CommonScope == ToScope) return;
Mike Stump11289f42009-09-09 15:08:12 +0000749
John McCall42f9f1f2010-05-12 02:37:54 +0000750 // Pull out (and reverse) any scopes we might need to diagnose skipping.
Richard Smithfe2750d2011-10-20 21:42:12 +0000751 SmallVector<unsigned, 10> ToScopesCXX98Compat;
Francois Pichet051f5e52011-09-13 10:26:51 +0000752 SmallVector<unsigned, 10> ToScopesError;
753 SmallVector<unsigned, 10> ToScopesWarning;
754 for (unsigned I = ToScope; I != CommonScope; I = Scopes[I].ParentScope) {
David Blaikiebbafb8a2012-03-11 07:00:24 +0000755 if (S.getLangOpts().MicrosoftMode && JumpDiagWarning != 0 &&
Francois Pichet051f5e52011-09-13 10:26:51 +0000756 IsMicrosoftJumpWarning(JumpDiagError, Scopes[I].InDiag))
757 ToScopesWarning.push_back(I);
Richard Smithfe2750d2011-10-20 21:42:12 +0000758 else if (IsCXX98CompatWarning(S, Scopes[I].InDiag))
759 ToScopesCXX98Compat.push_back(I);
Francois Pichet051f5e52011-09-13 10:26:51 +0000760 else if (Scopes[I].InDiag)
761 ToScopesError.push_back(I);
762 }
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000763
Francois Pichet051f5e52011-09-13 10:26:51 +0000764 // Handle warnings.
765 if (!ToScopesWarning.empty()) {
766 S.Diag(DiagLoc, JumpDiagWarning);
Richard Smithfe2750d2011-10-20 21:42:12 +0000767 NoteJumpIntoScopes(ToScopesWarning);
Francois Pichet051f5e52011-09-13 10:26:51 +0000768 }
John McCallcf819ab2010-05-12 00:58:13 +0000769
Francois Pichet051f5e52011-09-13 10:26:51 +0000770 // Handle errors.
771 if (!ToScopesError.empty()) {
772 S.Diag(DiagLoc, JumpDiagError);
Richard Smithfe2750d2011-10-20 21:42:12 +0000773 NoteJumpIntoScopes(ToScopesError);
774 }
775
776 // Handle -Wc++98-compat warnings if the jump is well-formed.
777 if (ToScopesError.empty() && !ToScopesCXX98Compat.empty()) {
778 S.Diag(DiagLoc, JumpDiagCXX98Compat);
779 NoteJumpIntoScopes(ToScopesCXX98Compat);
Francois Pichet051f5e52011-09-13 10:26:51 +0000780 }
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000781}
782
783void Sema::DiagnoseInvalidJumps(Stmt *Body) {
Douglas Gregor120f6a62009-11-17 06:14:37 +0000784 (void)JumpScopeChecker(Body, *this);
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000785}