blob: 1f5d682bba1bcfbc086c1254b8d2c7020d683a3b [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.
Aaron Ballman535bbcc2014-03-14 17:01:24 +0000370 for (auto *I : DS->decls())
371 BuildScopeInformation(I, ParentScope);
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000372 continue;
373 }
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000374 // Disallow jumps into any part of an @try statement by pushing a scope and
375 // walking all sub-stmts in that scope.
376 if (ObjCAtTryStmt *AT = dyn_cast<ObjCAtTryStmt>(SubStmt)) {
Fariborz Jahanian256d39d2011-07-11 18:04:54 +0000377 unsigned newParentScope;
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000378 // Recursively walk the AST for the @try part.
John McCallcf819ab2010-05-12 00:58:13 +0000379 Scopes.push_back(GotoScope(ParentScope,
380 diag::note_protected_by_objc_try,
381 diag::note_exits_objc_try,
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000382 AT->getAtTryLoc()));
383 if (Stmt *TryPart = AT->getTryBody())
Fariborz Jahanian256d39d2011-07-11 18:04:54 +0000384 BuildScopeInformation(TryPart, (newParentScope = Scopes.size()-1));
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000385
386 // Jump from the catch to the finally or try is not valid.
Douglas Gregor96c79492010-04-23 22:50:49 +0000387 for (unsigned I = 0, N = AT->getNumCatchStmts(); I != N; ++I) {
388 ObjCAtCatchStmt *AC = AT->getCatchStmt(I);
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000389 Scopes.push_back(GotoScope(ParentScope,
390 diag::note_protected_by_objc_catch,
John McCallcf819ab2010-05-12 00:58:13 +0000391 diag::note_exits_objc_catch,
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000392 AC->getAtCatchLoc()));
Mike Stump11289f42009-09-09 15:08:12 +0000393 // @catches are nested and it isn't
Fariborz Jahanian256d39d2011-07-11 18:04:54 +0000394 BuildScopeInformation(AC->getCatchBody(),
395 (newParentScope = Scopes.size()-1));
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000396 }
Mike Stump11289f42009-09-09 15:08:12 +0000397
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000398 // Jump from the finally to the try or catch is not valid.
399 if (ObjCAtFinallyStmt *AF = AT->getFinallyStmt()) {
400 Scopes.push_back(GotoScope(ParentScope,
401 diag::note_protected_by_objc_finally,
John McCallcf819ab2010-05-12 00:58:13 +0000402 diag::note_exits_objc_finally,
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000403 AF->getAtFinallyLoc()));
Fariborz Jahanian256d39d2011-07-11 18:04:54 +0000404 BuildScopeInformation(AF, (newParentScope = Scopes.size()-1));
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000405 }
Mike Stump11289f42009-09-09 15:08:12 +0000406
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000407 continue;
408 }
Fariborz Jahanian256d39d2011-07-11 18:04:54 +0000409
410 unsigned newParentScope;
Chris Lattnerc70dd562009-04-21 06:01:00 +0000411 // Disallow jumps into the protected statement of an @synchronized, but
412 // allow jumps into the object expression it protects.
413 if (ObjCAtSynchronizedStmt *AS = dyn_cast<ObjCAtSynchronizedStmt>(SubStmt)){
414 // Recursively walk the AST for the @synchronized object expr, it is
415 // evaluated in the normal scope.
416 BuildScopeInformation(AS->getSynchExpr(), ParentScope);
Mike Stump11289f42009-09-09 15:08:12 +0000417
Chris Lattnerc70dd562009-04-21 06:01:00 +0000418 // Recursively walk the AST for the @synchronized part, protected by a new
419 // scope.
420 Scopes.push_back(GotoScope(ParentScope,
421 diag::note_protected_by_objc_synchronized,
John McCallcf819ab2010-05-12 00:58:13 +0000422 diag::note_exits_objc_synchronized,
Chris Lattnerc70dd562009-04-21 06:01:00 +0000423 AS->getAtSynchronizedLoc()));
Fariborz Jahanian256d39d2011-07-11 18:04:54 +0000424 BuildScopeInformation(AS->getSynchBody(),
425 (newParentScope = Scopes.size()-1));
Chris Lattnerc70dd562009-04-21 06:01:00 +0000426 continue;
427 }
Sebastian Redl4de47b42009-04-27 20:27:31 +0000428
John McCall31168b02011-06-15 23:02:42 +0000429 // Disallow jumps into the protected statement of an @autoreleasepool.
430 if (ObjCAutoreleasePoolStmt *AS = dyn_cast<ObjCAutoreleasePoolStmt>(SubStmt)){
431 // Recursively walk the AST for the @autoreleasepool part, protected by a new
432 // scope.
433 Scopes.push_back(GotoScope(ParentScope,
434 diag::note_protected_by_objc_autoreleasepool,
435 diag::note_exits_objc_autoreleasepool,
436 AS->getAtLoc()));
Fariborz Jahanian256d39d2011-07-11 18:04:54 +0000437 BuildScopeInformation(AS->getSubStmt(), (newParentScope = Scopes.size()-1));
John McCall31168b02011-06-15 23:02:42 +0000438 continue;
439 }
John McCallb0433ee2012-09-25 06:56:03 +0000440
441 // Disallow jumps past full-expressions that use blocks with
442 // non-trivial cleanups of their captures. This is theoretically
443 // implementable but a lot of work which we haven't felt up to doing.
444 if (ExprWithCleanups *EWC = dyn_cast<ExprWithCleanups>(SubStmt)) {
445 for (unsigned i = 0, e = EWC->getNumObjects(); i != e; ++i) {
446 const BlockDecl *BDecl = EWC->getObject(i);
Aaron Ballman9371dd22014-03-14 18:34:04 +0000447 for (const auto &CI : BDecl->captures()) {
448 VarDecl *variable = CI.getVariable();
Fariborz Jahanian256d39d2011-07-11 18:04:54 +0000449 BuildScopeInformation(variable, BDecl, ParentScope);
450 }
John McCallb0433ee2012-09-25 06:56:03 +0000451 }
Fariborz Jahanian256d39d2011-07-11 18:04:54 +0000452 }
Richard Smithc934e4f2013-12-12 01:27:02 +0000453
454 // Disallow jumps out of scopes containing temporaries lifetime-extended to
455 // automatic storage duration.
456 if (MaterializeTemporaryExpr *MTE =
457 dyn_cast<MaterializeTemporaryExpr>(SubStmt)) {
458 if (MTE->getStorageDuration() == SD_Automatic) {
459 SmallVector<const Expr *, 4> CommaLHS;
460 SmallVector<SubobjectAdjustment, 4> Adjustments;
461 const Expr *ExtendedObject =
462 MTE->GetTemporaryExpr()->skipRValueSubobjectAdjustments(
463 CommaLHS, Adjustments);
464 if (ExtendedObject->getType().isDestructedType()) {
465 Scopes.push_back(GotoScope(ParentScope, 0,
466 diag::note_exits_temporary_dtor,
467 ExtendedObject->getExprLoc()));
468 ParentScope = Scopes.size()-1;
469 }
470 }
471 }
472
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000473 // Recursively walk the AST.
474 BuildScopeInformation(SubStmt, ParentScope);
475 }
476}
477
478/// VerifyJumps - Verify each element of the Jumps array to see if they are
479/// valid, emitting diagnostics if not.
480void JumpScopeChecker::VerifyJumps() {
481 while (!Jumps.empty()) {
482 Stmt *Jump = Jumps.pop_back_val();
Mike Stump11289f42009-09-09 15:08:12 +0000483
484 // With a goto,
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000485 if (GotoStmt *GS = dyn_cast<GotoStmt>(Jump)) {
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000486 CheckJump(GS, GS->getLabel()->getStmt(), GS->getGotoLoc(),
Francois Pichet051f5e52011-09-13 10:26:51 +0000487 diag::err_goto_into_protected_scope,
Richard Smithfe2750d2011-10-20 21:42:12 +0000488 diag::warn_goto_into_protected_scope,
489 diag::warn_cxx98_compat_goto_into_protected_scope);
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000490 continue;
491 }
Mike Stump11289f42009-09-09 15:08:12 +0000492
John McCall9de91602010-10-28 08:53:48 +0000493 // We only get indirect gotos here when they have a constant target.
494 if (IndirectGotoStmt *IGS = dyn_cast<IndirectGotoStmt>(Jump)) {
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000495 LabelDecl *Target = IGS->getConstantTarget();
496 CheckJump(IGS, Target->getStmt(), IGS->getGotoLoc(),
Francois Pichet2f550192011-09-16 23:15:32 +0000497 diag::err_goto_into_protected_scope,
Richard Smithfe2750d2011-10-20 21:42:12 +0000498 diag::warn_goto_into_protected_scope,
499 diag::warn_cxx98_compat_goto_into_protected_scope);
John McCall9de91602010-10-28 08:53:48 +0000500 continue;
501 }
502
John McCallcf819ab2010-05-12 00:58:13 +0000503 SwitchStmt *SS = cast<SwitchStmt>(Jump);
504 for (SwitchCase *SC = SS->getSwitchCaseList(); SC;
505 SC = SC->getNextSwitchCase()) {
506 assert(LabelAndGotoScopes.count(SC) && "Case not visited?");
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000507 SourceLocation Loc;
508 if (CaseStmt *CS = dyn_cast<CaseStmt>(SC))
509 Loc = CS->getLocStart();
510 else if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC))
511 Loc = DS->getLocStart();
512 else
513 Loc = SC->getLocStart();
514 CheckJump(SS, SC, Loc, diag::err_switch_into_protected_scope, 0,
Richard Smithfe2750d2011-10-20 21:42:12 +0000515 diag::warn_cxx98_compat_switch_into_protected_scope);
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000516 }
517 }
518}
519
John McCall42f9f1f2010-05-12 02:37:54 +0000520/// VerifyIndirectJumps - Verify whether any possible indirect jump
521/// might cross a protection boundary. Unlike direct jumps, indirect
522/// jumps count cleanups as protection boundaries: since there's no
523/// way to know where the jump is going, we can't implicitly run the
524/// right cleanups the way we can with direct jumps.
John McCallcf819ab2010-05-12 00:58:13 +0000525///
John McCall42f9f1f2010-05-12 02:37:54 +0000526/// Thus, an indirect jump is "trivial" if it bypasses no
527/// initializations and no teardowns. More formally, an indirect jump
528/// from A to B is trivial if the path out from A to DCA(A,B) is
529/// trivial and the path in from DCA(A,B) to B is trivial, where
530/// DCA(A,B) is the deepest common ancestor of A and B.
531/// Jump-triviality is transitive but asymmetric.
532///
John McCallcf819ab2010-05-12 00:58:13 +0000533/// A path in is trivial if none of the entered scopes have an InDiag.
534/// A path out is trivial is none of the exited scopes have an OutDiag.
John McCall42f9f1f2010-05-12 02:37:54 +0000535///
536/// Under these definitions, this function checks that the indirect
537/// jump between A and B is trivial for every indirect goto statement A
538/// and every label B whose address was taken in the function.
John McCallcf819ab2010-05-12 00:58:13 +0000539void JumpScopeChecker::VerifyIndirectJumps() {
540 if (IndirectJumps.empty()) return;
541
542 // If there aren't any address-of-label expressions in this function,
543 // complain about the first indirect goto.
544 if (IndirectJumpTargets.empty()) {
545 S.Diag(IndirectJumps[0]->getGotoLoc(),
546 diag::err_indirect_goto_without_addrlabel);
547 return;
548 }
549
John McCall42f9f1f2010-05-12 02:37:54 +0000550 // Collect a single representative of every scope containing an
551 // indirect goto. For most code bases, this substantially cuts
552 // down on the number of jump sites we'll have to consider later.
John McCallcf819ab2010-05-12 00:58:13 +0000553 typedef std::pair<unsigned, IndirectGotoStmt*> JumpScope;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000554 SmallVector<JumpScope, 32> JumpScopes;
John McCallcf819ab2010-05-12 00:58:13 +0000555 {
556 llvm::DenseMap<unsigned, IndirectGotoStmt*> JumpScopesMap;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000557 for (SmallVectorImpl<IndirectGotoStmt*>::iterator
John McCallcf819ab2010-05-12 00:58:13 +0000558 I = IndirectJumps.begin(), E = IndirectJumps.end(); I != E; ++I) {
559 IndirectGotoStmt *IG = *I;
560 assert(LabelAndGotoScopes.count(IG) &&
561 "indirect jump didn't get added to scopes?");
562 unsigned IGScope = LabelAndGotoScopes[IG];
563 IndirectGotoStmt *&Entry = JumpScopesMap[IGScope];
564 if (!Entry) Entry = IG;
565 }
566 JumpScopes.reserve(JumpScopesMap.size());
567 for (llvm::DenseMap<unsigned, IndirectGotoStmt*>::iterator
568 I = JumpScopesMap.begin(), E = JumpScopesMap.end(); I != E; ++I)
569 JumpScopes.push_back(*I);
570 }
571
John McCall42f9f1f2010-05-12 02:37:54 +0000572 // Collect a single representative of every scope containing a
573 // label whose address was taken somewhere in the function.
574 // For most code bases, there will be only one such scope.
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000575 llvm::DenseMap<unsigned, LabelDecl*> TargetScopes;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000576 for (SmallVectorImpl<LabelDecl*>::iterator
John McCallcf819ab2010-05-12 00:58:13 +0000577 I = IndirectJumpTargets.begin(), E = IndirectJumpTargets.end();
578 I != E; ++I) {
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000579 LabelDecl *TheLabel = *I;
580 assert(LabelAndGotoScopes.count(TheLabel->getStmt()) &&
John McCallcf819ab2010-05-12 00:58:13 +0000581 "Referenced label didn't get added to scopes?");
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000582 unsigned LabelScope = LabelAndGotoScopes[TheLabel->getStmt()];
583 LabelDecl *&Target = TargetScopes[LabelScope];
John McCallcf819ab2010-05-12 00:58:13 +0000584 if (!Target) Target = TheLabel;
585 }
586
John McCall42f9f1f2010-05-12 02:37:54 +0000587 // For each target scope, make sure it's trivially reachable from
588 // every scope containing a jump site.
589 //
590 // A path between scopes always consists of exitting zero or more
591 // scopes, then entering zero or more scopes. We build a set of
592 // of scopes S from which the target scope can be trivially
593 // entered, then verify that every jump scope can be trivially
594 // exitted to reach a scope in S.
John McCallcf819ab2010-05-12 00:58:13 +0000595 llvm::BitVector Reachable(Scopes.size(), false);
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000596 for (llvm::DenseMap<unsigned,LabelDecl*>::iterator
John McCallcf819ab2010-05-12 00:58:13 +0000597 TI = TargetScopes.begin(), TE = TargetScopes.end(); TI != TE; ++TI) {
598 unsigned TargetScope = TI->first;
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000599 LabelDecl *TargetLabel = TI->second;
John McCallcf819ab2010-05-12 00:58:13 +0000600
601 Reachable.reset();
602
603 // Mark all the enclosing scopes from which you can safely jump
John McCall42f9f1f2010-05-12 02:37:54 +0000604 // into the target scope. 'Min' will end up being the index of
605 // the shallowest such scope.
John McCallcf819ab2010-05-12 00:58:13 +0000606 unsigned Min = TargetScope;
607 while (true) {
608 Reachable.set(Min);
609
610 // Don't go beyond the outermost scope.
611 if (Min == 0) break;
612
John McCall42f9f1f2010-05-12 02:37:54 +0000613 // Stop if we can't trivially enter the current scope.
John McCallcf819ab2010-05-12 00:58:13 +0000614 if (Scopes[Min].InDiag) break;
615
616 Min = Scopes[Min].ParentScope;
617 }
618
619 // Walk through all the jump sites, checking that they can trivially
620 // reach this label scope.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000621 for (SmallVectorImpl<JumpScope>::iterator
John McCallcf819ab2010-05-12 00:58:13 +0000622 I = JumpScopes.begin(), E = JumpScopes.end(); I != E; ++I) {
623 unsigned Scope = I->first;
624
625 // Walk out the "scope chain" for this scope, looking for a scope
John McCall42f9f1f2010-05-12 02:37:54 +0000626 // we've marked reachable. For well-formed code this amortizes
627 // to O(JumpScopes.size() / Scopes.size()): we only iterate
628 // when we see something unmarked, and in well-formed code we
629 // mark everything we iterate past.
John McCallcf819ab2010-05-12 00:58:13 +0000630 bool IsReachable = false;
631 while (true) {
632 if (Reachable.test(Scope)) {
633 // If we find something reachable, mark all the scopes we just
634 // walked through as reachable.
635 for (unsigned S = I->first; S != Scope; S = Scopes[S].ParentScope)
636 Reachable.set(S);
637 IsReachable = true;
638 break;
639 }
640
641 // Don't walk out if we've reached the top-level scope or we've
642 // gotten shallower than the shallowest reachable scope.
643 if (Scope == 0 || Scope < Min) break;
644
645 // Don't walk out through an out-diagnostic.
646 if (Scopes[Scope].OutDiag) break;
647
648 Scope = Scopes[Scope].ParentScope;
649 }
650
651 // Only diagnose if we didn't find something.
652 if (IsReachable) continue;
653
654 DiagnoseIndirectJump(I->second, I->first, TargetLabel, TargetScope);
655 }
656 }
657}
658
Richard Smithfe2750d2011-10-20 21:42:12 +0000659/// Return true if a particular error+note combination must be downgraded to a
660/// warning in Microsoft mode.
661static bool IsMicrosoftJumpWarning(unsigned JumpDiag, unsigned InDiagNote) {
662 return (JumpDiag == diag::err_goto_into_protected_scope &&
663 (InDiagNote == diag::note_protected_by_variable_init ||
664 InDiagNote == diag::note_protected_by_variable_nontriv_destructor));
665}
666
667/// Return true if a particular note should be downgraded to a compatibility
668/// warning in C++11 mode.
669static bool IsCXX98CompatWarning(Sema &S, unsigned InDiagNote) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000670 return S.getLangOpts().CPlusPlus11 &&
Richard Smithfe2750d2011-10-20 21:42:12 +0000671 InDiagNote == diag::note_protected_by_variable_non_pod;
672}
673
674/// Produce primary diagnostic for an indirect jump statement.
675static void DiagnoseIndirectJumpStmt(Sema &S, IndirectGotoStmt *Jump,
676 LabelDecl *Target, bool &Diagnosed) {
677 if (Diagnosed)
678 return;
679 S.Diag(Jump->getGotoLoc(), diag::err_indirect_goto_in_protected_scope);
680 S.Diag(Target->getStmt()->getIdentLoc(), diag::note_indirect_goto_target);
681 Diagnosed = true;
682}
683
684/// Produce note diagnostics for a jump into a protected scope.
Bill Wendling8ac06af2012-02-22 09:38:11 +0000685void JumpScopeChecker::NoteJumpIntoScopes(ArrayRef<unsigned> ToScopes) {
Richard Smithfe2750d2011-10-20 21:42:12 +0000686 assert(!ToScopes.empty());
687 for (unsigned I = 0, E = ToScopes.size(); I != E; ++I)
688 if (Scopes[ToScopes[I]].InDiag)
689 S.Diag(Scopes[ToScopes[I]].Loc, Scopes[ToScopes[I]].InDiag);
690}
691
John McCall42f9f1f2010-05-12 02:37:54 +0000692/// Diagnose an indirect jump which is known to cross scopes.
John McCallcf819ab2010-05-12 00:58:13 +0000693void JumpScopeChecker::DiagnoseIndirectJump(IndirectGotoStmt *Jump,
694 unsigned JumpScope,
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000695 LabelDecl *Target,
John McCallcf819ab2010-05-12 00:58:13 +0000696 unsigned TargetScope) {
697 assert(JumpScope != TargetScope);
698
John McCall42f9f1f2010-05-12 02:37:54 +0000699 unsigned Common = GetDeepestCommonScope(JumpScope, TargetScope);
Richard Smithfe2750d2011-10-20 21:42:12 +0000700 bool Diagnosed = false;
John McCallcf819ab2010-05-12 00:58:13 +0000701
John McCall42f9f1f2010-05-12 02:37:54 +0000702 // Walk out the scope chain until we reach the common ancestor.
703 for (unsigned I = JumpScope; I != Common; I = Scopes[I].ParentScope)
Richard Smithfe2750d2011-10-20 21:42:12 +0000704 if (Scopes[I].OutDiag) {
705 DiagnoseIndirectJumpStmt(S, Jump, Target, Diagnosed);
John McCall42f9f1f2010-05-12 02:37:54 +0000706 S.Diag(Scopes[I].Loc, Scopes[I].OutDiag);
Richard Smithfe2750d2011-10-20 21:42:12 +0000707 }
708
709 SmallVector<unsigned, 10> ToScopesCXX98Compat;
John McCallcf819ab2010-05-12 00:58:13 +0000710
711 // Now walk into the scopes containing the label whose address was taken.
John McCall42f9f1f2010-05-12 02:37:54 +0000712 for (unsigned I = TargetScope; I != Common; I = Scopes[I].ParentScope)
Richard Smithfe2750d2011-10-20 21:42:12 +0000713 if (IsCXX98CompatWarning(S, Scopes[I].InDiag))
714 ToScopesCXX98Compat.push_back(I);
715 else if (Scopes[I].InDiag) {
716 DiagnoseIndirectJumpStmt(S, Jump, Target, Diagnosed);
John McCall42f9f1f2010-05-12 02:37:54 +0000717 S.Diag(Scopes[I].Loc, Scopes[I].InDiag);
Richard Smithfe2750d2011-10-20 21:42:12 +0000718 }
John McCallcf819ab2010-05-12 00:58:13 +0000719
Richard Smithfe2750d2011-10-20 21:42:12 +0000720 // Diagnose this jump if it would be ill-formed in C++98.
721 if (!Diagnosed && !ToScopesCXX98Compat.empty()) {
722 S.Diag(Jump->getGotoLoc(),
723 diag::warn_cxx98_compat_indirect_goto_in_protected_scope);
724 S.Diag(Target->getStmt()->getIdentLoc(), diag::note_indirect_goto_target);
725 NoteJumpIntoScopes(ToScopesCXX98Compat);
726 }
Francois Pichet051f5e52011-09-13 10:26:51 +0000727}
728
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000729/// CheckJump - Validate that the specified jump statement is valid: that it is
730/// jumping within or out of its current scope, not into a deeper one.
Francois Pichet051f5e52011-09-13 10:26:51 +0000731void JumpScopeChecker::CheckJump(Stmt *From, Stmt *To, SourceLocation DiagLoc,
Richard Smithfe2750d2011-10-20 21:42:12 +0000732 unsigned JumpDiagError, unsigned JumpDiagWarning,
733 unsigned JumpDiagCXX98Compat) {
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000734 assert(LabelAndGotoScopes.count(From) && "Jump didn't get added to scopes?");
735 unsigned FromScope = LabelAndGotoScopes[From];
736
737 assert(LabelAndGotoScopes.count(To) && "Jump didn't get added to scopes?");
738 unsigned ToScope = LabelAndGotoScopes[To];
Mike Stump11289f42009-09-09 15:08:12 +0000739
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000740 // Common case: exactly the same scope, which is fine.
741 if (FromScope == ToScope) return;
Mike Stump11289f42009-09-09 15:08:12 +0000742
John McCall42f9f1f2010-05-12 02:37:54 +0000743 unsigned CommonScope = GetDeepestCommonScope(FromScope, ToScope);
Mike Stump11289f42009-09-09 15:08:12 +0000744
John McCall42f9f1f2010-05-12 02:37:54 +0000745 // It's okay to jump out from a nested scope.
746 if (CommonScope == ToScope) return;
Mike Stump11289f42009-09-09 15:08:12 +0000747
John McCall42f9f1f2010-05-12 02:37:54 +0000748 // Pull out (and reverse) any scopes we might need to diagnose skipping.
Richard Smithfe2750d2011-10-20 21:42:12 +0000749 SmallVector<unsigned, 10> ToScopesCXX98Compat;
Francois Pichet051f5e52011-09-13 10:26:51 +0000750 SmallVector<unsigned, 10> ToScopesError;
751 SmallVector<unsigned, 10> ToScopesWarning;
752 for (unsigned I = ToScope; I != CommonScope; I = Scopes[I].ParentScope) {
Alp Tokerbfa39342014-01-14 12:51:41 +0000753 if (S.getLangOpts().MSVCCompat && JumpDiagWarning != 0 &&
Francois Pichet051f5e52011-09-13 10:26:51 +0000754 IsMicrosoftJumpWarning(JumpDiagError, Scopes[I].InDiag))
755 ToScopesWarning.push_back(I);
Richard Smithfe2750d2011-10-20 21:42:12 +0000756 else if (IsCXX98CompatWarning(S, Scopes[I].InDiag))
757 ToScopesCXX98Compat.push_back(I);
Francois Pichet051f5e52011-09-13 10:26:51 +0000758 else if (Scopes[I].InDiag)
759 ToScopesError.push_back(I);
760 }
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000761
Francois Pichet051f5e52011-09-13 10:26:51 +0000762 // Handle warnings.
763 if (!ToScopesWarning.empty()) {
764 S.Diag(DiagLoc, JumpDiagWarning);
Richard Smithfe2750d2011-10-20 21:42:12 +0000765 NoteJumpIntoScopes(ToScopesWarning);
Francois Pichet051f5e52011-09-13 10:26:51 +0000766 }
John McCallcf819ab2010-05-12 00:58:13 +0000767
Francois Pichet051f5e52011-09-13 10:26:51 +0000768 // Handle errors.
769 if (!ToScopesError.empty()) {
770 S.Diag(DiagLoc, JumpDiagError);
Richard Smithfe2750d2011-10-20 21:42:12 +0000771 NoteJumpIntoScopes(ToScopesError);
772 }
773
774 // Handle -Wc++98-compat warnings if the jump is well-formed.
775 if (ToScopesError.empty() && !ToScopesCXX98Compat.empty()) {
776 S.Diag(DiagLoc, JumpDiagCXX98Compat);
777 NoteJumpIntoScopes(ToScopesCXX98Compat);
Francois Pichet051f5e52011-09-13 10:26:51 +0000778 }
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000779}
780
781void Sema::DiagnoseInvalidJumps(Stmt *Body) {
Douglas Gregor120f6a62009-11-17 06:14:37 +0000782 (void)JumpScopeChecker(Body, *this);
Chris Lattner1a1fdbd2009-04-19 04:46:21 +0000783}