/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Sema/JumpDiagnostics.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===--- JumpDiagnostics.cpp - Protected scope jump analysis ------*- C++ -*-=// |
2 | | // |
3 | | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | | // See https://llvm.org/LICENSE.txt for license information. |
5 | | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | // |
9 | | // This file implements the JumpScopeChecker class, which is used to diagnose |
10 | | // jumps that enter a protected scope in an invalid way. |
11 | | // |
12 | | //===----------------------------------------------------------------------===// |
13 | | |
14 | | #include "clang/Sema/SemaInternal.h" |
15 | | #include "clang/AST/DeclCXX.h" |
16 | | #include "clang/AST/Expr.h" |
17 | | #include "clang/AST/ExprCXX.h" |
18 | | #include "clang/AST/StmtCXX.h" |
19 | | #include "clang/AST/StmtObjC.h" |
20 | | #include "clang/AST/StmtOpenMP.h" |
21 | | #include "llvm/ADT/BitVector.h" |
22 | | using namespace clang; |
23 | | |
24 | | namespace { |
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 | | /// |
32 | | class JumpScopeChecker { |
33 | | Sema &S; |
34 | | |
35 | | /// Permissive - True when recovering from errors, in which case precautions |
36 | | /// are taken to handle incomplete scope information. |
37 | | const bool Permissive; |
38 | | |
39 | | /// GotoScope - This is a record that we use to keep track of all of the |
40 | | /// scopes that are introduced by VLAs and other things that scope jumps like |
41 | | /// gotos. This scope tree has nothing to do with the source scope tree, |
42 | | /// because you can have multiple VLA scopes per compound statement, and most |
43 | | /// compound statements don't introduce any scopes. |
44 | | struct GotoScope { |
45 | | /// ParentScope - The index in ScopeMap of the parent scope. This is 0 for |
46 | | /// the parent scope is the function body. |
47 | | unsigned ParentScope; |
48 | | |
49 | | /// InDiag - The note to emit if there is a jump into this scope. |
50 | | unsigned InDiag; |
51 | | |
52 | | /// OutDiag - The note to emit if there is an indirect jump out |
53 | | /// of this scope. Direct jumps always clean up their current scope |
54 | | /// in an orderly way. |
55 | | unsigned OutDiag; |
56 | | |
57 | | /// Loc - Location to emit the diagnostic. |
58 | | SourceLocation Loc; |
59 | | |
60 | | GotoScope(unsigned parentScope, unsigned InDiag, unsigned OutDiag, |
61 | | SourceLocation L) |
62 | 40.2k | : ParentScope(parentScope), InDiag(InDiag), OutDiag(OutDiag), Loc(L) {} |
63 | | }; |
64 | | |
65 | | SmallVector<GotoScope, 48> Scopes; |
66 | | llvm::DenseMap<Stmt*, unsigned> LabelAndGotoScopes; |
67 | | SmallVector<Stmt*, 16> Jumps; |
68 | | |
69 | | SmallVector<Stmt*, 4> IndirectJumps; |
70 | | SmallVector<Stmt*, 4> AsmJumps; |
71 | | SmallVector<LabelDecl*, 4> IndirectJumpTargets; |
72 | | SmallVector<LabelDecl*, 4> AsmJumpTargets; |
73 | | public: |
74 | | JumpScopeChecker(Stmt *Body, Sema &S); |
75 | | private: |
76 | | void BuildScopeInformation(Decl *D, unsigned &ParentScope); |
77 | | void BuildScopeInformation(VarDecl *D, const BlockDecl *BDecl, |
78 | | unsigned &ParentScope); |
79 | | void BuildScopeInformation(CompoundLiteralExpr *CLE, unsigned &ParentScope); |
80 | | void BuildScopeInformation(Stmt *S, unsigned &origParentScope); |
81 | | |
82 | | void VerifyJumps(); |
83 | | void VerifyIndirectOrAsmJumps(bool IsAsmGoto); |
84 | | void NoteJumpIntoScopes(ArrayRef<unsigned> ToScopes); |
85 | | void DiagnoseIndirectOrAsmJump(Stmt *IG, unsigned IGScope, LabelDecl *Target, |
86 | | unsigned TargetScope); |
87 | | void CheckJump(Stmt *From, Stmt *To, SourceLocation DiagLoc, |
88 | | unsigned JumpDiag, unsigned JumpDiagWarning, |
89 | | unsigned JumpDiagCXX98Compat); |
90 | | void CheckGotoStmt(GotoStmt *GS); |
91 | | |
92 | | unsigned GetDeepestCommonScope(unsigned A, unsigned B); |
93 | | }; |
94 | | } // end anonymous namespace |
95 | | |
96 | 64.6k | #define CHECK_PERMISSIVE(x) (assert(Permissive || !(x)), (Permissive && (x)1.20k )) |
97 | | |
98 | | JumpScopeChecker::JumpScopeChecker(Stmt *Body, Sema &s) |
99 | 5.63k | : S(s), Permissive(s.hasAnyUnrecoverableErrorsInThisFunction()) { |
100 | | // Add a scope entry for function scope. |
101 | 5.63k | Scopes.push_back(GotoScope(~0U, ~0U, ~0U, SourceLocation())); |
102 | | |
103 | | // Build information for the top level compound statement, so that we have a |
104 | | // defined scope record for every "goto" and label. |
105 | 5.63k | unsigned BodyParentScope = 0; |
106 | 5.63k | BuildScopeInformation(Body, BodyParentScope); |
107 | | |
108 | | // Check that all jumps we saw are kosher. |
109 | 5.63k | VerifyJumps(); |
110 | 5.63k | VerifyIndirectOrAsmJumps(false); |
111 | 5.63k | VerifyIndirectOrAsmJumps(true); |
112 | 5.63k | } |
113 | | |
114 | | /// GetDeepestCommonScope - Finds the innermost scope enclosing the |
115 | | /// two scopes. |
116 | 1.53k | unsigned JumpScopeChecker::GetDeepestCommonScope(unsigned A, unsigned B) { |
117 | 6.79k | while (A != B) { |
118 | | // Inner scopes are created after outer scopes and therefore have |
119 | | // higher indices. |
120 | 5.26k | if (A < B) { |
121 | 252 | assert(Scopes[B].ParentScope < B); |
122 | 252 | B = Scopes[B].ParentScope; |
123 | 5.01k | } else { |
124 | 5.01k | assert(Scopes[A].ParentScope < A); |
125 | 5.01k | A = Scopes[A].ParentScope; |
126 | 5.01k | } |
127 | 5.26k | } |
128 | 1.53k | return A; |
129 | 1.53k | } |
130 | | |
131 | | typedef std::pair<unsigned,unsigned> ScopePair; |
132 | | |
133 | | /// GetDiagForGotoScopeDecl - If this decl induces a new goto scope, return a |
134 | | /// diagnostic that should be emitted if control goes over it. If not, return 0. |
135 | 40.7k | static ScopePair GetDiagForGotoScopeDecl(Sema &S, const Decl *D) { |
136 | 40.7k | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
137 | 36.1k | unsigned InDiag = 0; |
138 | 36.1k | unsigned OutDiag = 0; |
139 | | |
140 | 36.1k | if (VD->getType()->isVariablyModifiedType()) |
141 | 35 | InDiag = diag::note_protected_by_vla; |
142 | | |
143 | 36.1k | if (VD->hasAttr<BlocksAttr>()) |
144 | 2 | return ScopePair(diag::note_protected_by___block, |
145 | 2 | diag::note_exits___block); |
146 | | |
147 | 36.1k | if (VD->hasAttr<CleanupAttr>()) |
148 | 1 | return ScopePair(diag::note_protected_by_cleanup, |
149 | 1 | diag::note_exits_cleanup); |
150 | | |
151 | 36.1k | if (VD->hasLocalStorage()) { |
152 | 35.6k | switch (VD->getType().isDestructedType()) { |
153 | 22 | case QualType::DK_objc_strong_lifetime: |
154 | 22 | return ScopePair(diag::note_protected_by_objc_strong_init, |
155 | 22 | diag::note_exits_objc_strong); |
156 | | |
157 | 5 | case QualType::DK_objc_weak_lifetime: |
158 | 5 | return ScopePair(diag::note_protected_by_objc_weak_init, |
159 | 5 | diag::note_exits_objc_weak); |
160 | | |
161 | 4 | case QualType::DK_nontrivial_c_struct: |
162 | 4 | return ScopePair(diag::note_protected_by_non_trivial_c_struct_init, |
163 | 4 | diag::note_exits_dtor); |
164 | | |
165 | 556 | case QualType::DK_cxx_destructor: |
166 | 556 | OutDiag = diag::note_exits_dtor; |
167 | 556 | break; |
168 | | |
169 | 35.1k | case QualType::DK_none: |
170 | 35.1k | break; |
171 | 36.1k | } |
172 | 36.1k | } |
173 | | |
174 | 36.1k | const Expr *Init = VD->getInit(); |
175 | 36.1k | if (S.Context.getLangOpts().CPlusPlus && VD->hasLocalStorage()35.8k && Init35.3k ) { |
176 | | // C++11 [stmt.dcl]p3: |
177 | | // A program that jumps from a point where a variable with automatic |
178 | | // storage duration is not in scope to a point where it is in scope |
179 | | // is ill-formed unless the variable has scalar type, class type with |
180 | | // a trivial default constructor and a trivial destructor, a |
181 | | // cv-qualified version of one of these types, or an array of one of |
182 | | // the preceding types and is declared without an initializer. |
183 | | |
184 | | // C++03 [stmt.dcl.p3: |
185 | | // A program that jumps from a point where a local variable |
186 | | // with automatic storage duration is not in scope to a point |
187 | | // where it is in scope is ill-formed unless the variable has |
188 | | // POD type and is declared without an initializer. |
189 | | |
190 | 32.4k | InDiag = diag::note_protected_by_variable_init; |
191 | | |
192 | | // For a variable of (array of) class type declared without an |
193 | | // initializer, we will have call-style initialization and the initializer |
194 | | // will be the CXXConstructExpr with no intervening nodes. |
195 | 32.4k | if (const CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(Init)) { |
196 | 2.39k | const CXXConstructorDecl *Ctor = CCE->getConstructor(); |
197 | 2.39k | if (Ctor->isTrivial() && Ctor->isDefaultConstructor()363 && |
198 | 246 | VD->getInitStyle() == VarDecl::CallInit) { |
199 | 235 | if (OutDiag) |
200 | 79 | InDiag = diag::note_protected_by_variable_nontriv_destructor; |
201 | 156 | else if (!Ctor->getParent()->isPOD()) |
202 | 21 | InDiag = diag::note_protected_by_variable_non_pod; |
203 | 135 | else |
204 | 135 | InDiag = 0; |
205 | 235 | } |
206 | 2.39k | } |
207 | 32.4k | } |
208 | | |
209 | 36.1k | return ScopePair(InDiag, OutDiag); |
210 | 36.1k | } |
211 | | |
212 | 4.66k | if (const TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
213 | 4.24k | if (TD->getUnderlyingType()->isVariablyModifiedType()) |
214 | 4 | return ScopePair(isa<TypedefDecl>(TD) |
215 | 3 | ? diag::note_protected_by_vla_typedef |
216 | 1 | : diag::note_protected_by_vla_type_alias, |
217 | 4 | 0); |
218 | 4.66k | } |
219 | | |
220 | 4.66k | return ScopePair(0U, 0U); |
221 | 4.66k | } |
222 | | |
223 | | /// Build scope information for a declaration that is part of a DeclStmt. |
224 | 40.7k | void JumpScopeChecker::BuildScopeInformation(Decl *D, unsigned &ParentScope) { |
225 | | // If this decl causes a new scope, push and switch to it. |
226 | 40.7k | std::pair<unsigned,unsigned> Diags = GetDiagForGotoScopeDecl(S, D); |
227 | 40.7k | if (Diags.first || Diags.second8.43k ) { |
228 | 32.3k | Scopes.push_back(GotoScope(ParentScope, Diags.first, Diags.second, |
229 | 32.3k | D->getLocation())); |
230 | 32.3k | ParentScope = Scopes.size()-1; |
231 | 32.3k | } |
232 | | |
233 | | // If the decl has an initializer, walk it with the potentially new |
234 | | // scope we just installed. |
235 | 40.7k | if (VarDecl *VD = dyn_cast<VarDecl>(D)) |
236 | 36.1k | if (Expr *Init = VD->getInit()) |
237 | 33.0k | BuildScopeInformation(Init, ParentScope); |
238 | 40.7k | } |
239 | | |
240 | | /// Build scope information for a captured block literal variables. |
241 | | void JumpScopeChecker::BuildScopeInformation(VarDecl *D, |
242 | | const BlockDecl *BDecl, |
243 | 109 | unsigned &ParentScope) { |
244 | | // exclude captured __block variables; there's no destructor |
245 | | // associated with the block literal for them. |
246 | 109 | if (D->hasAttr<BlocksAttr>()) |
247 | 2 | return; |
248 | 107 | QualType T = D->getType(); |
249 | 107 | QualType::DestructionKind destructKind = T.isDestructedType(); |
250 | 107 | if (destructKind != QualType::DK_none) { |
251 | 17 | std::pair<unsigned,unsigned> Diags; |
252 | 17 | switch (destructKind) { |
253 | 1 | case QualType::DK_cxx_destructor: |
254 | 1 | Diags = ScopePair(diag::note_enters_block_captures_cxx_obj, |
255 | 1 | diag::note_exits_block_captures_cxx_obj); |
256 | 1 | break; |
257 | 14 | case QualType::DK_objc_strong_lifetime: |
258 | 14 | Diags = ScopePair(diag::note_enters_block_captures_strong, |
259 | 14 | diag::note_exits_block_captures_strong); |
260 | 14 | break; |
261 | 0 | case QualType::DK_objc_weak_lifetime: |
262 | 0 | Diags = ScopePair(diag::note_enters_block_captures_weak, |
263 | 0 | diag::note_exits_block_captures_weak); |
264 | 0 | break; |
265 | 2 | case QualType::DK_nontrivial_c_struct: |
266 | 2 | Diags = ScopePair(diag::note_enters_block_captures_non_trivial_c_struct, |
267 | 2 | diag::note_exits_block_captures_non_trivial_c_struct); |
268 | 2 | break; |
269 | 0 | case QualType::DK_none: |
270 | 0 | llvm_unreachable("non-lifetime captured variable"); |
271 | 17 | } |
272 | 17 | SourceLocation Loc = D->getLocation(); |
273 | 17 | if (Loc.isInvalid()) |
274 | 9 | Loc = BDecl->getLocation(); |
275 | 17 | Scopes.push_back(GotoScope(ParentScope, |
276 | 17 | Diags.first, Diags.second, Loc)); |
277 | 17 | ParentScope = Scopes.size()-1; |
278 | 17 | } |
279 | 107 | } |
280 | | |
281 | | /// Build scope information for compound literals of C struct types that are |
282 | | /// non-trivial to destruct. |
283 | | void JumpScopeChecker::BuildScopeInformation(CompoundLiteralExpr *CLE, |
284 | 2 | unsigned &ParentScope) { |
285 | 2 | unsigned InDiag = diag::note_enters_compound_literal_scope; |
286 | 2 | unsigned OutDiag = diag::note_exits_compound_literal_scope; |
287 | 2 | Scopes.push_back(GotoScope(ParentScope, InDiag, OutDiag, CLE->getExprLoc())); |
288 | 2 | ParentScope = Scopes.size() - 1; |
289 | 2 | } |
290 | | |
291 | | /// BuildScopeInformation - The statements from CI to CE are known to form a |
292 | | /// coherent VLA scope with a specified parent node. Walk through the |
293 | | /// statements, adding any labels or gotos to LabelAndGotoScopes and recursively |
294 | | /// walking the AST as needed. |
295 | | void JumpScopeChecker::BuildScopeInformation(Stmt *S, |
296 | 1.12M | unsigned &origParentScope) { |
297 | | // If this is a statement, rather than an expression, scopes within it don't |
298 | | // propagate out into the enclosing scope. Otherwise we have to worry |
299 | | // about block literals, which have the lifetime of their enclosing statement. |
300 | 1.12M | unsigned independentParentScope = origParentScope; |
301 | 1.12M | unsigned &ParentScope = ((isa<Expr>(S) && !isa<StmtExpr>(S)939k ) |
302 | 938k | ? origParentScope : independentParentScope191k ); |
303 | | |
304 | 1.12M | unsigned StmtsToSkip = 0u; |
305 | | |
306 | | // If we found a label, remember that it is in ParentScope scope. |
307 | 1.12M | switch (S->getStmtClass()) { |
308 | 187 | case Stmt::AddrLabelExprClass: |
309 | 187 | IndirectJumpTargets.push_back(cast<AddrLabelExpr>(S)->getLabel()); |
310 | 187 | break; |
311 | | |
312 | 4 | case Stmt::ObjCForCollectionStmtClass: { |
313 | 4 | auto *CS = cast<ObjCForCollectionStmt>(S); |
314 | 4 | unsigned Diag = diag::note_protected_by_objc_fast_enumeration; |
315 | 4 | unsigned NewParentScope = Scopes.size(); |
316 | 4 | Scopes.push_back(GotoScope(ParentScope, Diag, 0, S->getBeginLoc())); |
317 | 4 | BuildScopeInformation(CS->getBody(), NewParentScope); |
318 | 4 | return; |
319 | 0 | } |
320 | | |
321 | 127 | case Stmt::IndirectGotoStmtClass: |
322 | | // "goto *&&lbl;" is a special case which we treat as equivalent |
323 | | // to a normal goto. In addition, we don't calculate scope in the |
324 | | // operand (to avoid recording the address-of-label use), which |
325 | | // works only because of the restricted set of expressions which |
326 | | // we detect as constant targets. |
327 | 127 | if (cast<IndirectGotoStmt>(S)->getConstantTarget()) { |
328 | 7 | LabelAndGotoScopes[S] = ParentScope; |
329 | 7 | Jumps.push_back(S); |
330 | 7 | return; |
331 | 7 | } |
332 | | |
333 | 120 | LabelAndGotoScopes[S] = ParentScope; |
334 | 120 | IndirectJumps.push_back(S); |
335 | 120 | break; |
336 | | |
337 | 4.45k | case Stmt::SwitchStmtClass: |
338 | | // Evaluate the C++17 init stmt and condition variable |
339 | | // before entering the scope of the switch statement. |
340 | 4.45k | if (Stmt *Init = cast<SwitchStmt>(S)->getInit()) { |
341 | 65 | BuildScopeInformation(Init, ParentScope); |
342 | 65 | ++StmtsToSkip; |
343 | 65 | } |
344 | 4.45k | if (VarDecl *Var = cast<SwitchStmt>(S)->getConditionVariable()) { |
345 | 53 | BuildScopeInformation(Var, ParentScope); |
346 | 53 | ++StmtsToSkip; |
347 | 53 | } |
348 | 4.45k | LLVM_FALLTHROUGH; |
349 | | |
350 | 8.34k | case Stmt::GotoStmtClass: |
351 | | // Remember both what scope a goto is in as well as the fact that we have |
352 | | // it. This makes the second scan not have to walk the AST again. |
353 | 8.34k | LabelAndGotoScopes[S] = ParentScope; |
354 | 8.34k | Jumps.push_back(S); |
355 | 8.34k | break; |
356 | | |
357 | 28 | case Stmt::GCCAsmStmtClass: |
358 | 28 | if (auto *GS = dyn_cast<GCCAsmStmt>(S)) |
359 | 28 | if (GS->isAsmGoto()) { |
360 | | // Remember both what scope a goto is in as well as the fact that we |
361 | | // have it. This makes the second scan not have to walk the AST again. |
362 | 17 | LabelAndGotoScopes[S] = ParentScope; |
363 | 17 | AsmJumps.push_back(GS); |
364 | 17 | for (auto *E : GS->labels()) |
365 | 19 | AsmJumpTargets.push_back(E->getLabel()); |
366 | 17 | } |
367 | 28 | break; |
368 | | |
369 | 36.5k | case Stmt::IfStmtClass: { |
370 | 36.5k | IfStmt *IS = cast<IfStmt>(S); |
371 | 36.5k | if (!(IS->isConstexpr() || IS->isObjCAvailabilityCheck()36.5k )) |
372 | 36.5k | break; |
373 | | |
374 | 16 | unsigned Diag = IS->isConstexpr() ? diag::note_protected_by_constexpr_if14 |
375 | 2 | : diag::note_protected_by_if_available; |
376 | | |
377 | 16 | if (VarDecl *Var = IS->getConditionVariable()) |
378 | 0 | BuildScopeInformation(Var, ParentScope); |
379 | | |
380 | | // Cannot jump into the middle of the condition. |
381 | 16 | unsigned NewParentScope = Scopes.size(); |
382 | 16 | Scopes.push_back(GotoScope(ParentScope, Diag, 0, IS->getBeginLoc())); |
383 | 16 | BuildScopeInformation(IS->getCond(), NewParentScope); |
384 | | |
385 | | // Jumps into either arm of an 'if constexpr' are not allowed. |
386 | 16 | NewParentScope = Scopes.size(); |
387 | 16 | Scopes.push_back(GotoScope(ParentScope, Diag, 0, IS->getBeginLoc())); |
388 | 16 | BuildScopeInformation(IS->getThen(), NewParentScope); |
389 | 16 | if (Stmt *Else = IS->getElse()) { |
390 | 9 | NewParentScope = Scopes.size(); |
391 | 9 | Scopes.push_back(GotoScope(ParentScope, Diag, 0, IS->getBeginLoc())); |
392 | 9 | BuildScopeInformation(Else, NewParentScope); |
393 | 9 | } |
394 | 16 | return; |
395 | 16 | } |
396 | | |
397 | 125 | case Stmt::CXXTryStmtClass: { |
398 | 125 | CXXTryStmt *TS = cast<CXXTryStmt>(S); |
399 | 125 | { |
400 | 125 | unsigned NewParentScope = Scopes.size(); |
401 | 125 | Scopes.push_back(GotoScope(ParentScope, |
402 | 125 | diag::note_protected_by_cxx_try, |
403 | 125 | diag::note_exits_cxx_try, |
404 | 125 | TS->getSourceRange().getBegin())); |
405 | 125 | if (Stmt *TryBlock = TS->getTryBlock()) |
406 | 125 | BuildScopeInformation(TryBlock, NewParentScope); |
407 | 125 | } |
408 | | |
409 | | // Jump from the catch into the try is not allowed either. |
410 | 254 | for (unsigned I = 0, E = TS->getNumHandlers(); I != E; ++I129 ) { |
411 | 129 | CXXCatchStmt *CS = TS->getHandler(I); |
412 | 129 | unsigned NewParentScope = Scopes.size(); |
413 | 129 | Scopes.push_back(GotoScope(ParentScope, |
414 | 129 | diag::note_protected_by_cxx_catch, |
415 | 129 | diag::note_exits_cxx_catch, |
416 | 129 | CS->getSourceRange().getBegin())); |
417 | 129 | BuildScopeInformation(CS->getHandlerBlock(), NewParentScope); |
418 | 129 | } |
419 | 125 | return; |
420 | 16 | } |
421 | | |
422 | 78 | case Stmt::SEHTryStmtClass: { |
423 | 78 | SEHTryStmt *TS = cast<SEHTryStmt>(S); |
424 | 78 | { |
425 | 78 | unsigned NewParentScope = Scopes.size(); |
426 | 78 | Scopes.push_back(GotoScope(ParentScope, |
427 | 78 | diag::note_protected_by_seh_try, |
428 | 78 | diag::note_exits_seh_try, |
429 | 78 | TS->getSourceRange().getBegin())); |
430 | 78 | if (Stmt *TryBlock = TS->getTryBlock()) |
431 | 78 | BuildScopeInformation(TryBlock, NewParentScope); |
432 | 78 | } |
433 | | |
434 | | // Jump from __except or __finally into the __try are not allowed either. |
435 | 78 | if (SEHExceptStmt *Except = TS->getExceptHandler()) { |
436 | 24 | unsigned NewParentScope = Scopes.size(); |
437 | 24 | Scopes.push_back(GotoScope(ParentScope, |
438 | 24 | diag::note_protected_by_seh_except, |
439 | 24 | diag::note_exits_seh_except, |
440 | 24 | Except->getSourceRange().getBegin())); |
441 | 24 | BuildScopeInformation(Except->getBlock(), NewParentScope); |
442 | 54 | } else if (SEHFinallyStmt *Finally = TS->getFinallyHandler()) { |
443 | 54 | unsigned NewParentScope = Scopes.size(); |
444 | 54 | Scopes.push_back(GotoScope(ParentScope, |
445 | 54 | diag::note_protected_by_seh_finally, |
446 | 54 | diag::note_exits_seh_finally, |
447 | 54 | Finally->getSourceRange().getBegin())); |
448 | 54 | BuildScopeInformation(Finally->getBlock(), NewParentScope); |
449 | 54 | } |
450 | | |
451 | 78 | return; |
452 | 16 | } |
453 | | |
454 | 40.5k | case Stmt::DeclStmtClass: { |
455 | | // If this is a declstmt with a VLA definition, it defines a scope from here |
456 | | // to the end of the containing context. |
457 | 40.5k | DeclStmt *DS = cast<DeclStmt>(S); |
458 | | // The decl statement creates a scope if any of the decls in it are VLAs |
459 | | // or have the cleanup attribute. |
460 | 40.5k | for (auto *I : DS->decls()) |
461 | 40.7k | BuildScopeInformation(I, origParentScope); |
462 | 40.5k | return; |
463 | 16 | } |
464 | | |
465 | 7 | case Stmt::ObjCAtTryStmtClass: { |
466 | | // Disallow jumps into any part of an @try statement by pushing a scope and |
467 | | // walking all sub-stmts in that scope. |
468 | 7 | ObjCAtTryStmt *AT = cast<ObjCAtTryStmt>(S); |
469 | | // Recursively walk the AST for the @try part. |
470 | 7 | { |
471 | 7 | unsigned NewParentScope = Scopes.size(); |
472 | 7 | Scopes.push_back(GotoScope(ParentScope, |
473 | 7 | diag::note_protected_by_objc_try, |
474 | 7 | diag::note_exits_objc_try, |
475 | 7 | AT->getAtTryLoc())); |
476 | 7 | if (Stmt *TryPart = AT->getTryBody()) |
477 | 7 | BuildScopeInformation(TryPart, NewParentScope); |
478 | 7 | } |
479 | | |
480 | | // Jump from the catch to the finally or try is not valid. |
481 | 18 | for (unsigned I = 0, N = AT->getNumCatchStmts(); I != N; ++I11 ) { |
482 | 11 | ObjCAtCatchStmt *AC = AT->getCatchStmt(I); |
483 | 11 | unsigned NewParentScope = Scopes.size(); |
484 | 11 | Scopes.push_back(GotoScope(ParentScope, |
485 | 11 | diag::note_protected_by_objc_catch, |
486 | 11 | diag::note_exits_objc_catch, |
487 | 11 | AC->getAtCatchLoc())); |
488 | | // @catches are nested and it isn't |
489 | 11 | BuildScopeInformation(AC->getCatchBody(), NewParentScope); |
490 | 11 | } |
491 | | |
492 | | // Jump from the finally to the try or catch is not valid. |
493 | 7 | if (ObjCAtFinallyStmt *AF = AT->getFinallyStmt()) { |
494 | 4 | unsigned NewParentScope = Scopes.size(); |
495 | 4 | Scopes.push_back(GotoScope(ParentScope, |
496 | 4 | diag::note_protected_by_objc_finally, |
497 | 4 | diag::note_exits_objc_finally, |
498 | 4 | AF->getAtFinallyLoc())); |
499 | 4 | BuildScopeInformation(AF, NewParentScope); |
500 | 4 | } |
501 | | |
502 | 7 | return; |
503 | 16 | } |
504 | | |
505 | 1 | case Stmt::ObjCAtSynchronizedStmtClass: { |
506 | | // Disallow jumps into the protected statement of an @synchronized, but |
507 | | // allow jumps into the object expression it protects. |
508 | 1 | ObjCAtSynchronizedStmt *AS = cast<ObjCAtSynchronizedStmt>(S); |
509 | | // Recursively walk the AST for the @synchronized object expr, it is |
510 | | // evaluated in the normal scope. |
511 | 1 | BuildScopeInformation(AS->getSynchExpr(), ParentScope); |
512 | | |
513 | | // Recursively walk the AST for the @synchronized part, protected by a new |
514 | | // scope. |
515 | 1 | unsigned NewParentScope = Scopes.size(); |
516 | 1 | Scopes.push_back(GotoScope(ParentScope, |
517 | 1 | diag::note_protected_by_objc_synchronized, |
518 | 1 | diag::note_exits_objc_synchronized, |
519 | 1 | AS->getAtSynchronizedLoc())); |
520 | 1 | BuildScopeInformation(AS->getSynchBody(), NewParentScope); |
521 | 1 | return; |
522 | 16 | } |
523 | | |
524 | 2 | case Stmt::ObjCAutoreleasePoolStmtClass: { |
525 | | // Disallow jumps into the protected statement of an @autoreleasepool. |
526 | 2 | ObjCAutoreleasePoolStmt *AS = cast<ObjCAutoreleasePoolStmt>(S); |
527 | | // Recursively walk the AST for the @autoreleasepool part, protected by a |
528 | | // new scope. |
529 | 2 | unsigned NewParentScope = Scopes.size(); |
530 | 2 | Scopes.push_back(GotoScope(ParentScope, |
531 | 2 | diag::note_protected_by_objc_autoreleasepool, |
532 | 2 | diag::note_exits_objc_autoreleasepool, |
533 | 2 | AS->getAtLoc())); |
534 | 2 | BuildScopeInformation(AS->getSubStmt(), NewParentScope); |
535 | 2 | return; |
536 | 16 | } |
537 | | |
538 | 1.83k | case Stmt::ExprWithCleanupsClass: { |
539 | | // Disallow jumps past full-expressions that use blocks with |
540 | | // non-trivial cleanups of their captures. This is theoretically |
541 | | // implementable but a lot of work which we haven't felt up to doing. |
542 | 1.83k | ExprWithCleanups *EWC = cast<ExprWithCleanups>(S); |
543 | 1.94k | for (unsigned i = 0, e = EWC->getNumObjects(); i != e; ++i111 ) { |
544 | 111 | if (auto *BDecl = EWC->getObject(i).dyn_cast<BlockDecl *>()) |
545 | 109 | for (const auto &CI : BDecl->captures()) { |
546 | 109 | VarDecl *variable = CI.getVariable(); |
547 | 109 | BuildScopeInformation(variable, BDecl, origParentScope); |
548 | 109 | } |
549 | 2 | else if (auto *CLE = EWC->getObject(i).dyn_cast<CompoundLiteralExpr *>()) |
550 | 2 | BuildScopeInformation(CLE, origParentScope); |
551 | 2 | else |
552 | 0 | llvm_unreachable("unexpected cleanup object type"); |
553 | 111 | } |
554 | 1.83k | break; |
555 | 1.83k | } |
556 | | |
557 | 1.09k | case Stmt::MaterializeTemporaryExprClass: { |
558 | | // Disallow jumps out of scopes containing temporaries lifetime-extended to |
559 | | // automatic storage duration. |
560 | 1.09k | MaterializeTemporaryExpr *MTE = cast<MaterializeTemporaryExpr>(S); |
561 | 1.09k | if (MTE->getStorageDuration() == SD_Automatic) { |
562 | 96 | SmallVector<const Expr *, 4> CommaLHS; |
563 | 96 | SmallVector<SubobjectAdjustment, 4> Adjustments; |
564 | 96 | const Expr *ExtendedObject = |
565 | 96 | MTE->getSubExpr()->skipRValueSubobjectAdjustments(CommaLHS, |
566 | 96 | Adjustments); |
567 | 96 | if (ExtendedObject->getType().isDestructedType()) { |
568 | 11 | Scopes.push_back(GotoScope(ParentScope, 0, |
569 | 11 | diag::note_exits_temporary_dtor, |
570 | 11 | ExtendedObject->getExprLoc())); |
571 | 11 | origParentScope = Scopes.size()-1; |
572 | 11 | } |
573 | 96 | } |
574 | 1.09k | break; |
575 | 1.83k | } |
576 | | |
577 | 2 | case Stmt::CaseStmtClass: |
578 | 4 | case Stmt::DefaultStmtClass: |
579 | 76 | case Stmt::LabelStmtClass: |
580 | 76 | LabelAndGotoScopes[S] = ParentScope; |
581 | 76 | break; |
582 | | |
583 | 1.04M | default: |
584 | 1.04M | if (auto *ED = dyn_cast<OMPExecutableDirective>(S)) { |
585 | 2.29k | if (!ED->isStandaloneDirective()) { |
586 | 1.76k | unsigned NewParentScope = Scopes.size(); |
587 | 1.76k | Scopes.emplace_back(ParentScope, |
588 | 1.76k | diag::note_omp_protected_structured_block, |
589 | 1.76k | diag::note_omp_exits_structured_block, |
590 | 1.76k | ED->getStructuredBlock()->getBeginLoc()); |
591 | 1.76k | BuildScopeInformation(ED->getStructuredBlock(), NewParentScope); |
592 | 1.76k | return; |
593 | 1.76k | } |
594 | 1.03M | } |
595 | 1.03M | break; |
596 | 1.08M | } |
597 | | |
598 | 1.09M | for (Stmt *SubStmt : S->children())1.08M { |
599 | 1.09M | if (!SubStmt) |
600 | 3.76k | continue; |
601 | 1.08M | if (StmtsToSkip) { |
602 | 118 | --StmtsToSkip; |
603 | 118 | continue; |
604 | 118 | } |
605 | | |
606 | | // Cases, labels, and defaults aren't "scope parents". It's also |
607 | | // important to handle these iteratively instead of recursively in |
608 | | // order to avoid blowing out the stack. |
609 | 1.11M | while (1.08M true) { |
610 | 1.11M | Stmt *Next; |
611 | 1.11M | if (SwitchCase *SC = dyn_cast<SwitchCase>(SubStmt)) |
612 | 18.9k | Next = SC->getSubStmt(); |
613 | 1.09M | else if (LabelStmt *LS = dyn_cast<LabelStmt>(SubStmt)) |
614 | 3.37k | Next = LS->getSubStmt(); |
615 | 1.08M | else |
616 | 1.08M | break; |
617 | | |
618 | 22.3k | LabelAndGotoScopes[SubStmt] = ParentScope; |
619 | 22.3k | SubStmt = Next; |
620 | 22.3k | } |
621 | | |
622 | | // Recursively walk the AST. |
623 | 1.08M | BuildScopeInformation(SubStmt, ParentScope); |
624 | 1.08M | } |
625 | 1.08M | } |
626 | | |
627 | | /// VerifyJumps - Verify each element of the Jumps array to see if they are |
628 | | /// valid, emitting diagnostics if not. |
629 | 5.63k | void JumpScopeChecker::VerifyJumps() { |
630 | 13.9k | while (!Jumps.empty()) { |
631 | 8.34k | Stmt *Jump = Jumps.pop_back_val(); |
632 | | |
633 | | // With a goto, |
634 | 8.34k | if (GotoStmt *GS = dyn_cast<GotoStmt>(Jump)) { |
635 | | // The label may not have a statement if it's coming from inline MS ASM. |
636 | 3.88k | if (GS->getLabel()->getStmt()) { |
637 | 3.57k | CheckJump(GS, GS->getLabel()->getStmt(), GS->getGotoLoc(), |
638 | 3.57k | diag::err_goto_into_protected_scope, |
639 | 3.57k | diag::ext_goto_into_protected_scope, |
640 | 3.57k | diag::warn_cxx98_compat_goto_into_protected_scope); |
641 | 3.57k | } |
642 | 3.88k | CheckGotoStmt(GS); |
643 | 3.88k | continue; |
644 | 3.88k | } |
645 | | |
646 | | // We only get indirect gotos here when they have a constant target. |
647 | 4.46k | if (IndirectGotoStmt *IGS = dyn_cast<IndirectGotoStmt>(Jump)) { |
648 | 7 | LabelDecl *Target = IGS->getConstantTarget(); |
649 | 7 | CheckJump(IGS, Target->getStmt(), IGS->getGotoLoc(), |
650 | 7 | diag::err_goto_into_protected_scope, |
651 | 7 | diag::ext_goto_into_protected_scope, |
652 | 7 | diag::warn_cxx98_compat_goto_into_protected_scope); |
653 | 7 | continue; |
654 | 7 | } |
655 | | |
656 | 4.45k | SwitchStmt *SS = cast<SwitchStmt>(Jump); |
657 | 23.4k | for (SwitchCase *SC = SS->getSwitchCaseList(); SC; |
658 | 18.9k | SC = SC->getNextSwitchCase()) { |
659 | 18.9k | if (CHECK_PERMISSIVE(!LabelAndGotoScopes.count(SC))) |
660 | 0 | continue; |
661 | 18.9k | SourceLocation Loc; |
662 | 18.9k | if (CaseStmt *CS = dyn_cast<CaseStmt>(SC)) |
663 | 17.8k | Loc = CS->getBeginLoc(); |
664 | 1.14k | else if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) |
665 | 1.14k | Loc = DS->getBeginLoc(); |
666 | 0 | else |
667 | 0 | Loc = SC->getBeginLoc(); |
668 | 18.9k | CheckJump(SS, SC, Loc, diag::err_switch_into_protected_scope, 0, |
669 | 18.9k | diag::warn_cxx98_compat_switch_into_protected_scope); |
670 | 18.9k | } |
671 | 4.45k | } |
672 | 5.63k | } |
673 | | |
674 | | /// VerifyIndirectOrAsmJumps - Verify whether any possible indirect goto or |
675 | | /// asm goto jump might cross a protection boundary. Unlike direct jumps, |
676 | | /// indirect or asm goto jumps count cleanups as protection boundaries: |
677 | | /// since there's no way to know where the jump is going, we can't implicitly |
678 | | /// run the right cleanups the way we can with direct jumps. |
679 | | /// Thus, an indirect/asm jump is "trivial" if it bypasses no |
680 | | /// initializations and no teardowns. More formally, an indirect/asm jump |
681 | | /// from A to B is trivial if the path out from A to DCA(A,B) is |
682 | | /// trivial and the path in from DCA(A,B) to B is trivial, where |
683 | | /// DCA(A,B) is the deepest common ancestor of A and B. |
684 | | /// Jump-triviality is transitive but asymmetric. |
685 | | /// |
686 | | /// A path in is trivial if none of the entered scopes have an InDiag. |
687 | | /// A path out is trivial is none of the exited scopes have an OutDiag. |
688 | | /// |
689 | | /// Under these definitions, this function checks that the indirect |
690 | | /// jump between A and B is trivial for every indirect goto statement A |
691 | | /// and every label B whose address was taken in the function. |
692 | 11.2k | void JumpScopeChecker::VerifyIndirectOrAsmJumps(bool IsAsmGoto) { |
693 | 5.63k | SmallVector<Stmt*, 4> GotoJumps = IsAsmGoto ? AsmJumps : IndirectJumps; |
694 | 11.2k | if (GotoJumps.empty()) |
695 | 11.1k | return; |
696 | 115 | SmallVector<LabelDecl *, 4> JumpTargets = |
697 | 98 | IsAsmGoto ? AsmJumpTargets17 : IndirectJumpTargets; |
698 | | // If there aren't any address-of-label expressions in this function, |
699 | | // complain about the first indirect goto. |
700 | 115 | if (JumpTargets.empty()) { |
701 | 0 | assert(!IsAsmGoto &&"only indirect goto can get here"); |
702 | 0 | S.Diag(GotoJumps[0]->getBeginLoc(), |
703 | 0 | diag::err_indirect_goto_without_addrlabel); |
704 | 0 | return; |
705 | 0 | } |
706 | | // Collect a single representative of every scope containing an |
707 | | // indirect or asm goto. For most code bases, this substantially cuts |
708 | | // down on the number of jump sites we'll have to consider later. |
709 | 115 | typedef std::pair<unsigned, Stmt*> JumpScope; |
710 | 115 | SmallVector<JumpScope, 32> JumpScopes; |
711 | 115 | { |
712 | 115 | llvm::DenseMap<unsigned, Stmt*> JumpScopesMap; |
713 | 115 | for (SmallVectorImpl<Stmt *>::iterator I = GotoJumps.begin(), |
714 | 115 | E = GotoJumps.end(); |
715 | 252 | I != E; ++I137 ) { |
716 | 137 | Stmt *IG = *I; |
717 | 137 | if (CHECK_PERMISSIVE(!LabelAndGotoScopes.count(IG))) |
718 | 0 | continue; |
719 | 137 | unsigned IGScope = LabelAndGotoScopes[IG]; |
720 | 137 | Stmt *&Entry = JumpScopesMap[IGScope]; |
721 | 137 | if (!Entry) Entry = IG126 ; |
722 | 137 | } |
723 | 115 | JumpScopes.reserve(JumpScopesMap.size()); |
724 | 115 | for (llvm::DenseMap<unsigned, Stmt *>::iterator I = JumpScopesMap.begin(), |
725 | 115 | E = JumpScopesMap.end(); |
726 | 241 | I != E; ++I126 ) |
727 | 126 | JumpScopes.push_back(*I); |
728 | 115 | } |
729 | | |
730 | | // Collect a single representative of every scope containing a |
731 | | // label whose address was taken somewhere in the function. |
732 | | // For most code bases, there will be only one such scope. |
733 | 115 | llvm::DenseMap<unsigned, LabelDecl*> TargetScopes; |
734 | 115 | for (SmallVectorImpl<LabelDecl *>::iterator I = JumpTargets.begin(), |
735 | 115 | E = JumpTargets.end(); |
736 | 311 | I != E; ++I196 ) { |
737 | 196 | LabelDecl *TheLabel = *I; |
738 | 196 | if (CHECK_PERMISSIVE(!LabelAndGotoScopes.count(TheLabel->getStmt()))) |
739 | 0 | continue; |
740 | 196 | unsigned LabelScope = LabelAndGotoScopes[TheLabel->getStmt()]; |
741 | 196 | LabelDecl *&Target = TargetScopes[LabelScope]; |
742 | 196 | if (!Target) Target = TheLabel122 ; |
743 | 196 | } |
744 | | |
745 | | // For each target scope, make sure it's trivially reachable from |
746 | | // every scope containing a jump site. |
747 | | // |
748 | | // A path between scopes always consists of exitting zero or more |
749 | | // scopes, then entering zero or more scopes. We build a set of |
750 | | // of scopes S from which the target scope can be trivially |
751 | | // entered, then verify that every jump scope can be trivially |
752 | | // exitted to reach a scope in S. |
753 | 115 | llvm::BitVector Reachable(Scopes.size(), false); |
754 | 115 | for (llvm::DenseMap<unsigned,LabelDecl*>::iterator |
755 | 237 | TI = TargetScopes.begin(), TE = TargetScopes.end(); TI != TE; ++TI122 ) { |
756 | 122 | unsigned TargetScope = TI->first; |
757 | 122 | LabelDecl *TargetLabel = TI->second; |
758 | | |
759 | 122 | Reachable.reset(); |
760 | | |
761 | | // Mark all the enclosing scopes from which you can safely jump |
762 | | // into the target scope. 'Min' will end up being the index of |
763 | | // the shallowest such scope. |
764 | 122 | unsigned Min = TargetScope; |
765 | 122 | while (true) { |
766 | 122 | Reachable.set(Min); |
767 | | |
768 | | // Don't go beyond the outermost scope. |
769 | 122 | if (Min == 0) break86 ; |
770 | | |
771 | | // Stop if we can't trivially enter the current scope. |
772 | 36 | if (Scopes[Min].InDiag) break; |
773 | | |
774 | 0 | Min = Scopes[Min].ParentScope; |
775 | 0 | } |
776 | | |
777 | | // Walk through all the jump sites, checking that they can trivially |
778 | | // reach this label scope. |
779 | 122 | for (SmallVectorImpl<JumpScope>::iterator |
780 | 256 | I = JumpScopes.begin(), E = JumpScopes.end(); I != E; ++I134 ) { |
781 | 134 | unsigned Scope = I->first; |
782 | | |
783 | | // Walk out the "scope chain" for this scope, looking for a scope |
784 | | // we've marked reachable. For well-formed code this amortizes |
785 | | // to O(JumpScopes.size() / Scopes.size()): we only iterate |
786 | | // when we see something unmarked, and in well-formed code we |
787 | | // mark everything we iterate past. |
788 | 134 | bool IsReachable = false; |
789 | 163 | while (true) { |
790 | 163 | if (Reachable.test(Scope)) { |
791 | | // If we find something reachable, mark all the scopes we just |
792 | | // walked through as reachable. |
793 | 115 | for (unsigned S = I->first; S != Scope; S = Scopes[S].ParentScope23 ) |
794 | 23 | Reachable.set(S); |
795 | 92 | IsReachable = true; |
796 | 92 | break; |
797 | 92 | } |
798 | | |
799 | | // Don't walk out if we've reached the top-level scope or we've |
800 | | // gotten shallower than the shallowest reachable scope. |
801 | 71 | if (Scope == 0 || Scope < Min65 ) break13 ; |
802 | | |
803 | | // Don't walk out through an out-diagnostic. |
804 | 58 | if (Scopes[Scope].OutDiag) break29 ; |
805 | | |
806 | 29 | Scope = Scopes[Scope].ParentScope; |
807 | 29 | } |
808 | | |
809 | | // Only diagnose if we didn't find something. |
810 | 134 | if (IsReachable) continue92 ; |
811 | | |
812 | 42 | DiagnoseIndirectOrAsmJump(I->second, I->first, TargetLabel, TargetScope); |
813 | 42 | } |
814 | 122 | } |
815 | 115 | } |
816 | | |
817 | | /// Return true if a particular error+note combination must be downgraded to a |
818 | | /// warning in Microsoft mode. |
819 | 8 | static bool IsMicrosoftJumpWarning(unsigned JumpDiag, unsigned InDiagNote) { |
820 | 8 | return (JumpDiag == diag::err_goto_into_protected_scope && |
821 | 8 | (InDiagNote == diag::note_protected_by_variable_init || |
822 | 4 | InDiagNote == diag::note_protected_by_variable_nontriv_destructor)); |
823 | 8 | } |
824 | | |
825 | | /// Return true if a particular note should be downgraded to a compatibility |
826 | | /// warning in C++11 mode. |
827 | 246 | static bool IsCXX98CompatWarning(Sema &S, unsigned InDiagNote) { |
828 | 246 | return S.getLangOpts().CPlusPlus11 && |
829 | 137 | InDiagNote == diag::note_protected_by_variable_non_pod; |
830 | 246 | } |
831 | | |
832 | | /// Produce primary diagnostic for an indirect jump statement. |
833 | | static void DiagnoseIndirectOrAsmJumpStmt(Sema &S, Stmt *Jump, |
834 | 40 | LabelDecl *Target, bool &Diagnosed) { |
835 | 40 | if (Diagnosed) |
836 | 1 | return; |
837 | 39 | bool IsAsmGoto = isa<GCCAsmStmt>(Jump); |
838 | 39 | S.Diag(Jump->getBeginLoc(), diag::err_indirect_goto_in_protected_scope) |
839 | 39 | << IsAsmGoto; |
840 | 39 | S.Diag(Target->getStmt()->getIdentLoc(), diag::note_indirect_goto_target) |
841 | 39 | << IsAsmGoto; |
842 | 39 | Diagnosed = true; |
843 | 39 | } |
844 | | |
845 | | /// Produce note diagnostics for a jump into a protected scope. |
846 | 221 | void JumpScopeChecker::NoteJumpIntoScopes(ArrayRef<unsigned> ToScopes) { |
847 | 221 | if (CHECK_PERMISSIVE(ToScopes.empty())) |
848 | 0 | return; |
849 | 463 | for (unsigned I = 0, E = ToScopes.size(); 221 I != E; ++I242 ) |
850 | 242 | if (Scopes[ToScopes[I]].InDiag) |
851 | 242 | S.Diag(Scopes[ToScopes[I]].Loc, Scopes[ToScopes[I]].InDiag); |
852 | 221 | } |
853 | | |
854 | | /// Diagnose an indirect jump which is known to cross scopes. |
855 | | void JumpScopeChecker::DiagnoseIndirectOrAsmJump(Stmt *Jump, unsigned JumpScope, |
856 | | LabelDecl *Target, |
857 | 42 | unsigned TargetScope) { |
858 | 42 | if (CHECK_PERMISSIVE(JumpScope == TargetScope)) |
859 | 0 | return; |
860 | | |
861 | 42 | unsigned Common = GetDeepestCommonScope(JumpScope, TargetScope); |
862 | 42 | bool Diagnosed = false; |
863 | | |
864 | | // Walk out the scope chain until we reach the common ancestor. |
865 | 88 | for (unsigned I = JumpScope; I != Common; I = Scopes[I].ParentScope46 ) |
866 | 46 | if (Scopes[I].OutDiag) { |
867 | 30 | DiagnoseIndirectOrAsmJumpStmt(S, Jump, Target, Diagnosed); |
868 | 30 | S.Diag(Scopes[I].Loc, Scopes[I].OutDiag); |
869 | 30 | } |
870 | | |
871 | 42 | SmallVector<unsigned, 10> ToScopesCXX98Compat; |
872 | | |
873 | | // Now walk into the scopes containing the label whose address was taken. |
874 | 55 | for (unsigned I = TargetScope; I != Common; I = Scopes[I].ParentScope13 ) |
875 | 13 | if (IsCXX98CompatWarning(S, Scopes[I].InDiag)) |
876 | 3 | ToScopesCXX98Compat.push_back(I); |
877 | 10 | else if (Scopes[I].InDiag) { |
878 | 10 | DiagnoseIndirectOrAsmJumpStmt(S, Jump, Target, Diagnosed); |
879 | 10 | S.Diag(Scopes[I].Loc, Scopes[I].InDiag); |
880 | 10 | } |
881 | | |
882 | | // Diagnose this jump if it would be ill-formed in C++98. |
883 | 42 | if (!Diagnosed && !ToScopesCXX98Compat.empty()3 ) { |
884 | 3 | bool IsAsmGoto = isa<GCCAsmStmt>(Jump); |
885 | 3 | S.Diag(Jump->getBeginLoc(), |
886 | 3 | diag::warn_cxx98_compat_indirect_goto_in_protected_scope) |
887 | 3 | << IsAsmGoto; |
888 | 3 | S.Diag(Target->getStmt()->getIdentLoc(), diag::note_indirect_goto_target) |
889 | 3 | << IsAsmGoto; |
890 | 3 | NoteJumpIntoScopes(ToScopesCXX98Compat); |
891 | 3 | } |
892 | 42 | } |
893 | | |
894 | | /// CheckJump - Validate that the specified jump statement is valid: that it is |
895 | | /// jumping within or out of its current scope, not into a deeper one. |
896 | | void JumpScopeChecker::CheckJump(Stmt *From, Stmt *To, SourceLocation DiagLoc, |
897 | | unsigned JumpDiagError, unsigned JumpDiagWarning, |
898 | 22.5k | unsigned JumpDiagCXX98Compat) { |
899 | 22.5k | if (CHECK_PERMISSIVE(!LabelAndGotoScopes.count(From))) |
900 | 0 | return; |
901 | 22.5k | if (CHECK_PERMISSIVE(!LabelAndGotoScopes.count(To))) |
902 | 0 | return; |
903 | | |
904 | 22.5k | unsigned FromScope = LabelAndGotoScopes[From]; |
905 | 22.5k | unsigned ToScope = LabelAndGotoScopes[To]; |
906 | | |
907 | | // Common case: exactly the same scope, which is fine. |
908 | 22.5k | if (FromScope == ToScope) return21.0k ; |
909 | | |
910 | | // Warn on gotos out of __finally blocks. |
911 | 1.49k | if (isa<GotoStmt>(From) || isa<IndirectGotoStmt>(From)52 ) { |
912 | | // If FromScope > ToScope, FromScope is more nested and the jump goes to a |
913 | | // less nested scope. Check if it crosses a __finally along the way. |
914 | 6.36k | for (unsigned I = FromScope; I > ToScope; I = Scopes[I].ParentScope4.92k ) { |
915 | 4.93k | if (Scopes[I].InDiag == diag::note_protected_by_seh_finally) { |
916 | 8 | S.Diag(From->getBeginLoc(), diag::warn_jump_out_of_seh_finally); |
917 | 8 | break; |
918 | 8 | } |
919 | 4.92k | if (Scopes[I].InDiag == diag::note_omp_protected_structured_block) { |
920 | 4 | S.Diag(From->getBeginLoc(), diag::err_goto_into_protected_scope); |
921 | 4 | S.Diag(To->getBeginLoc(), diag::note_omp_exits_structured_block); |
922 | 4 | break; |
923 | 4 | } |
924 | 4.92k | } |
925 | 1.44k | } |
926 | | |
927 | 1.49k | unsigned CommonScope = GetDeepestCommonScope(FromScope, ToScope); |
928 | | |
929 | | // It's okay to jump out from a nested scope. |
930 | 1.49k | if (CommonScope == ToScope) return1.27k ; |
931 | | |
932 | | // Pull out (and reverse) any scopes we might need to diagnose skipping. |
933 | 218 | SmallVector<unsigned, 10> ToScopesCXX98Compat; |
934 | 218 | SmallVector<unsigned, 10> ToScopesError; |
935 | 218 | SmallVector<unsigned, 10> ToScopesWarning; |
936 | 457 | for (unsigned I = ToScope; I != CommonScope; I = Scopes[I].ParentScope239 ) { |
937 | 239 | if (S.getLangOpts().MSVCCompat && JumpDiagWarning != 010 && |
938 | 8 | IsMicrosoftJumpWarning(JumpDiagError, Scopes[I].InDiag)) |
939 | 6 | ToScopesWarning.push_back(I); |
940 | 233 | else if (IsCXX98CompatWarning(S, Scopes[I].InDiag)) |
941 | 12 | ToScopesCXX98Compat.push_back(I); |
942 | 221 | else if (Scopes[I].InDiag) |
943 | 221 | ToScopesError.push_back(I); |
944 | 239 | } |
945 | | |
946 | | // Handle warnings. |
947 | 218 | if (!ToScopesWarning.empty()) { |
948 | 6 | S.Diag(DiagLoc, JumpDiagWarning); |
949 | 6 | NoteJumpIntoScopes(ToScopesWarning); |
950 | 6 | } |
951 | | |
952 | | // Handle errors. |
953 | 218 | if (!ToScopesError.empty()) { |
954 | 200 | S.Diag(DiagLoc, JumpDiagError); |
955 | 200 | NoteJumpIntoScopes(ToScopesError); |
956 | 200 | } |
957 | | |
958 | | // Handle -Wc++98-compat warnings if the jump is well-formed. |
959 | 218 | if (ToScopesError.empty() && !ToScopesCXX98Compat.empty()18 ) { |
960 | 12 | S.Diag(DiagLoc, JumpDiagCXX98Compat); |
961 | 12 | NoteJumpIntoScopes(ToScopesCXX98Compat); |
962 | 12 | } |
963 | 218 | } |
964 | | |
965 | 3.88k | void JumpScopeChecker::CheckGotoStmt(GotoStmt *GS) { |
966 | 3.88k | if (GS->getLabel()->isMSAsmLabel()) { |
967 | 4 | S.Diag(GS->getGotoLoc(), diag::err_goto_ms_asm_label) |
968 | 4 | << GS->getLabel()->getIdentifier(); |
969 | 4 | S.Diag(GS->getLabel()->getLocation(), diag::note_goto_ms_asm_label) |
970 | 4 | << GS->getLabel()->getIdentifier(); |
971 | 4 | } |
972 | 3.88k | } |
973 | | |
974 | 5.63k | void Sema::DiagnoseInvalidJumps(Stmt *Body) { |
975 | 5.63k | (void)JumpScopeChecker(Body, *this); |
976 | 5.63k | } |