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