Debian Codegraph @

clang/lib/Sema/SemaStmt.cpp

raw
1 //===--- SemaStmt.cpp - Semantic Analysis for Statements ------------------===//
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 semantic analysis for statements.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "CheckExprLifetime.h"
14 #include "clang/AST/ASTContext.h"
15 #include "clang/AST/ASTLambda.h"
16 #include "clang/AST/CXXInheritance.h"
17 #include "clang/AST/CharUnits.h"
18 #include "clang/AST/DeclObjC.h"
19 #include "clang/AST/DynamicRecursiveASTVisitor.h"
20 #include "clang/AST/EvaluatedExprVisitor.h"
21 #include "clang/AST/ExprCXX.h"
22 #include "clang/AST/ExprObjC.h"
23 #include "clang/AST/IgnoreExpr.h"
24 #include "clang/AST/StmtCXX.h"
25 #include "clang/AST/StmtObjC.h"
26 #include "clang/AST/TypeLoc.h"
27 #include "clang/AST/TypeOrdering.h"
28 #include "clang/Basic/TargetInfo.h"
29 #include "clang/Lex/Preprocessor.h"
30 #include "clang/Sema/EnterExpressionEvaluationContext.h"
31 #include "clang/Sema/Initialization.h"
32 #include "clang/Sema/Lookup.h"
33 #include "clang/Sema/Ownership.h"
34 #include "clang/Sema/Scope.h"
35 #include "clang/Sema/ScopeInfo.h"
36 #include "clang/Sema/SemaCUDA.h"
37 #include "clang/Sema/SemaObjC.h"
38 #include "clang/Sema/SemaOpenMP.h"
39 #include "llvm/ADT/ArrayRef.h"
40 #include "llvm/ADT/DenseMap.h"
41 #include "llvm/ADT/STLExtras.h"
42 #include "llvm/ADT/SmallVector.h"
43 #include "llvm/ADT/StringExtras.h"
44
45 using namespace clang;
46 using namespace sema;
47
48 StmtResult Sema::ActOnExprStmt(ExprResult FE, bool DiscardedValue) {
49 if (FE.isInvalid())
50 return StmtError();
51
52 FE = ActOnFinishFullExpr(FE.get(), FE.get()->getExprLoc(), DiscardedValue);
53 if (FE.isInvalid())
54 return StmtError();
55
56 // C99 6.8.3p2: The expression in an expression statement is evaluated as a
57 // void expression for its side effects. Conversion to void allows any
58 // operand, even incomplete types.
59
60 // Same thing in for stmt first clause (when expr) and third clause.
61 return StmtResult(FE.getAs<Stmt>());
62 }
63
64
65 StmtResult Sema::ActOnExprStmtError() {
66 DiscardCleanupsInEvaluationContext();
67 return StmtError();
68 }
69
70 StmtResult Sema::ActOnNullStmt(SourceLocation SemiLoc,
71 bool HasLeadingEmptyMacro) {
72 return new (Context) NullStmt(SemiLoc, HasLeadingEmptyMacro);
73 }
74
75 StmtResult Sema::ActOnDeclStmt(DeclGroupPtrTy dg, SourceLocation StartLoc,
76 SourceLocation EndLoc) {
77 DeclGroupRef DG = dg.get();
78
79 // If we have an invalid decl, just return an error.
80 if (DG.isNull()) return StmtError();
81
82 return new (Context) DeclStmt(DG, StartLoc, EndLoc);
83 }
84
85 void Sema::ActOnForEachDeclStmt(DeclGroupPtrTy dg) {
86 DeclGroupRef DG = dg.get();
87
88 // If we don't have a declaration, or we have an invalid declaration,
89 // just return.
90 if (DG.isNull() || !DG.isSingleDecl())
91 return;
92
93 Decl *decl = DG.getSingleDecl();
94 if (!decl || decl->isInvalidDecl())
95 return;
96
97 // Only variable declarations are permitted.
98 VarDecl *var = dyn_cast<VarDecl>(decl);
99 if (!var) {
100 Diag(decl->getLocation(), diag::err_non_variable_decl_in_for);
101 decl->setInvalidDecl();
102 return;
103 }
104
105 // foreach variables are never actually initialized in the way that
106 // the parser came up with.
107 var->setInit(nullptr);
108
109 // In ARC, we don't need to retain the iteration variable of a fast
110 // enumeration loop. Rather than actually trying to catch that
111 // during declaration processing, we remove the consequences here.
112 if (getLangOpts().ObjCAutoRefCount) {
113 QualType type = var->getType();
114
115 // Only do this if we inferred the lifetime. Inferred lifetime
116 // will show up as a local qualifier because explicit lifetime
117 // should have shown up as an AttributedType instead.
118 if (type.getLocalQualifiers().getObjCLifetime() == Qualifiers::OCL_Strong) {
119 // Add 'const' and mark the variable as pseudo-strong.
120 var->setType(type.withConst());
121 var->setARCPseudoStrong(true);
122 }
123 }
124 }
125
126 /// Diagnose unused comparisons, both builtin and overloaded operators.
127 /// For '==' and '!=', suggest fixits for '=' or '|='.
128 ///
129 /// Adding a cast to void (or other expression wrappers) will prevent the
130 /// warning from firing.
131 static bool DiagnoseUnusedComparison(Sema &S, const Expr *E) {
132 SourceLocation Loc;
133 bool CanAssign;
134 enum { Equality, Inequality, Relational, ThreeWay } Kind;
135
136 if (const BinaryOperator *Op = dyn_cast<BinaryOperator>(E)) {
137 if (!Op->isComparisonOp())
138 return false;
139
140 if (Op->getOpcode() == BO_EQ)
141 Kind = Equality;
142 else if (Op->getOpcode() == BO_NE)
143 Kind = Inequality;
144 else if (Op->getOpcode() == BO_Cmp)
145 Kind = ThreeWay;
146 else {
147 assert(Op->isRelationalOp());
148 Kind = Relational;
149 }
150 Loc = Op->getOperatorLoc();
151 CanAssign = Op->getLHS()->IgnoreParenImpCasts()->isLValue();
152 } else if (const CXXOperatorCallExpr *Op = dyn_cast<CXXOperatorCallExpr>(E)) {
153 switch (Op->getOperator()) {
154 case OO_EqualEqual:
155 Kind = Equality;
156 break;
157 case OO_ExclaimEqual:
158 Kind = Inequality;
159 break;
160 case OO_Less:
161 case OO_Greater:
162 case OO_GreaterEqual:
163 case OO_LessEqual:
164 Kind = Relational;
165 break;
166 case OO_Spaceship:
167 Kind = ThreeWay;
168 break;
169 default:
170 return false;
171 }
172
173 Loc = Op->getOperatorLoc();
174 CanAssign = Op->getArg(0)->IgnoreParenImpCasts()->isLValue();
175 } else {
176 // Not a typo-prone comparison.
177 return false;
178 }
179
180 // Suppress warnings when the operator, suspicious as it may be, comes from
181 // a macro expansion.
182 if (S.SourceMgr.isMacroBodyExpansion(Loc))
183 return false;
184
185 S.Diag(Loc, diag::warn_unused_comparison)
186 << (unsigned)Kind << E->getSourceRange();
187
188 // If the LHS is a plausible entity to assign to, provide a fixit hint to
189 // correct common typos.
190 if (CanAssign) {
191 if (Kind == Inequality)
192 S.Diag(Loc, diag::note_inequality_comparison_to_or_assign)
193 << FixItHint::CreateReplacement(Loc, "|=");
194 else if (Kind == Equality)
195 S.Diag(Loc, diag::note_equality_comparison_to_assign)
196 << FixItHint::CreateReplacement(Loc, "=");
197 }
198
199 return true;
200 }
201
202 static bool DiagnoseNoDiscard(Sema &S, const NamedDecl *OffendingDecl,
203 const WarnUnusedResultAttr *A, SourceLocation Loc,
204 SourceRange R1, SourceRange R2, bool IsCtor) {
205 if (!A)
206 return false;
207 StringRef Msg = A->getMessage();
208
209 if (Msg.empty()) {
210 if (OffendingDecl)
211 return S.Diag(Loc, diag::warn_unused_return_type)
212 << IsCtor << A << OffendingDecl << false << R1 << R2;
213 if (IsCtor)
214 return S.Diag(Loc, diag::warn_unused_constructor)
215 << A << false << R1 << R2;
216 return S.Diag(Loc, diag::warn_unused_result) << A << false << R1 << R2;
217 }
218
219 if (OffendingDecl)
220 return S.Diag(Loc, diag::warn_unused_return_type)
221 << IsCtor << A << OffendingDecl << true << Msg << R1 << R2;
222 if (IsCtor)
223 return S.Diag(Loc, diag::warn_unused_constructor)
224 << A << true << Msg << R1 << R2;
225 return S.Diag(Loc, diag::warn_unused_result) << A << true << Msg << R1 << R2;
226 }
227
228 namespace {
229
230 // Diagnoses unused expressions that call functions marked [[nodiscard]],
231 // [[gnu::warn_unused_result]] and similar.
232 // Additionally, a DiagID can be provided to emit a warning in additional
233 // contexts (such as for an unused LHS of a comma expression)
234 void DiagnoseUnused(Sema &S, const Expr *E, std::optional<unsigned> DiagID) {
235 bool NoDiscardOnly = !DiagID.has_value();
236
237 // If we are in an unevaluated expression context, then there can be no unused
238 // results because the results aren't expected to be used in the first place.
239 if (S.isUnevaluatedContext())
240 return;
241
242 SourceLocation ExprLoc = E->IgnoreParenImpCasts()->getExprLoc();
243 // In most cases, we don't want to warn if the expression is written in a
244 // macro body, or if the macro comes from a system header. If the offending
245 // expression is a call to a function with the warn_unused_result attribute,
246 // we warn no matter the location. Because of the order in which the various
247 // checks need to happen, we factor out the macro-related test here.
248 bool ShouldSuppress = S.SourceMgr.isMacroBodyExpansion(ExprLoc) ||
249 S.SourceMgr.isInSystemMacro(ExprLoc);
250
251 const Expr *WarnExpr;
252 SourceLocation Loc;
253 SourceRange R1, R2;
254 if (!E->isUnusedResultAWarning(WarnExpr, Loc, R1, R2, S.Context))
255 return;
256
257 if (!NoDiscardOnly) {
258 // If this is a GNU statement expression expanded from a macro, it is
259 // probably unused because it is a function-like macro that can be used as
260 // either an expression or statement. Don't warn, because it is almost
261 // certainly a false positive.
262 if (isa<StmtExpr>(E) && Loc.isMacroID())
263 return;
264
265 // Check if this is the UNREFERENCED_PARAMETER from the Microsoft headers.
266 // That macro is frequently used to suppress "unused parameter" warnings,
267 // but its implementation makes clang's -Wunused-value fire. Prevent this.
268 if (isa<ParenExpr>(E->IgnoreImpCasts()) && Loc.isMacroID()) {
269 SourceLocation SpellLoc = Loc;
270 if (S.findMacroSpelling(SpellLoc, "UNREFERENCED_PARAMETER"))
271 return;
272 }
273 }
274
275 // Okay, we have an unused result. Depending on what the base expression is,
276 // we might want to make a more specific diagnostic. Check for one of these
277 // cases now.
278 if (const FullExpr *Temps = dyn_cast<FullExpr>(E))
279 E = Temps->getSubExpr();
280 if (const CXXBindTemporaryExpr *TempExpr = dyn_cast<CXXBindTemporaryExpr>(E))
281 E = TempExpr->getSubExpr();
282
283 if (DiagnoseUnusedComparison(S, E))
284 return;
285
286 E = WarnExpr;
287 if (const auto *Cast = dyn_cast<CastExpr>(E))
288 if (Cast->getCastKind() == CK_NoOp ||
289 Cast->getCastKind() == CK_ConstructorConversion ||
290 Cast->getCastKind() == CK_IntegralCast)
291 E = Cast->getSubExpr()->IgnoreImpCasts();
292
293 if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
294 if (E->getType()->isVoidType())
295 return;
296
297 auto [OffendingDecl, A] = CE->getUnusedResultAttr(S.Context);
298 if (DiagnoseNoDiscard(S, OffendingDecl, A, Loc, R1, R2,
299 /*isCtor=*/false))
300 return;
301
302 // If the callee has attribute pure, const, or warn_unused_result, warn with
303 // a more specific message to make it clear what is happening. If the call
304 // is written in a macro body, only warn if it has the warn_unused_result
305 // attribute.
306 if (const Decl *FD = CE->getCalleeDecl()) {
307 if (ShouldSuppress)
308 return;
309 if (FD->hasAttr<PureAttr>()) {
310 S.Diag(Loc, diag::warn_unused_call) << R1 << R2 << "pure";
311 return;
312 }
313 if (FD->hasAttr<ConstAttr>()) {
314 S.Diag(Loc, diag::warn_unused_call) << R1 << R2 << "const";
315 return;
316 }
317 }
318 } else if (const auto *CE = dyn_cast<CXXConstructExpr>(E)) {
319 auto [OffendingDecl, A] = CE->getUnusedResultAttr(S.Context);
320 if (DiagnoseNoDiscard(S, OffendingDecl, A, Loc, R1, R2,
321 /*isCtor=*/true))
322 return;
323 } else if (const auto *ILE = dyn_cast<InitListExpr>(E)) {
324 if (const TagDecl *TD = ILE->getType()->getAsTagDecl()) {
325
326 if (DiagnoseNoDiscard(S, TD, TD->getAttr<WarnUnusedResultAttr>(), Loc, R1,
327 R2, /*isCtor=*/false))
328 return;
329 }
330 } else if (ShouldSuppress)
331 return;
332
333 E = WarnExpr;
334 if (const ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(E)) {
335 if (S.getLangOpts().ObjCAutoRefCount && ME->isDelegateInitCall()) {
336 S.Diag(Loc, diag::err_arc_unused_init_message) << R1;
337 return;
338 }
339
340 auto [OffendingDecl, A] = ME->getUnusedResultAttr(S.Context);
341 if (DiagnoseNoDiscard(S, OffendingDecl, A, Loc, R1, R2,
342 /*isCtor=*/false))
343 return;
344 } else if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) {
345 const Expr *Source = POE->getSyntacticForm();
346 // Handle the actually selected call of an OpenMP specialized call.
347 if (S.LangOpts.OpenMP && isa<CallExpr>(Source) &&
348 POE->getNumSemanticExprs() == 1 &&
349 isa<CallExpr>(POE->getSemanticExpr(0)))
350 return DiagnoseUnused(S, POE->getSemanticExpr(0), DiagID);
351 if (isa<ObjCSubscriptRefExpr>(Source))
352 DiagID = diag::warn_unused_container_subscript_expr;
353 else if (isa<ObjCPropertyRefExpr>(Source))
354 DiagID = diag::warn_unused_property_expr;
355 } else if (const CXXFunctionalCastExpr *FC
356 = dyn_cast<CXXFunctionalCastExpr>(E)) {
357 const Expr *E = FC->getSubExpr();
358 if (const CXXBindTemporaryExpr *TE = dyn_cast<CXXBindTemporaryExpr>(E))
359 E = TE->getSubExpr();
360 if (isa<CXXTemporaryObjectExpr>(E))
361 return;
362 if (const CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(E))
363 if (const CXXRecordDecl *RD = CE->getType()->getAsCXXRecordDecl())
364 if (!RD->getAttr<WarnUnusedAttr>())
365 return;
366 }
367
368 if (NoDiscardOnly)
369 return;
370
371 // Diagnose "(void*) blah" as a typo for "(void) blah".
372 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(E)) {
373 TypeSourceInfo *TI = CE->getTypeInfoAsWritten();
374 QualType T = TI->getType();
375
376 // We really do want to use the non-canonical type here.
377 if (T == S.Context.VoidPtrTy) {
378 PointerTypeLoc TL = TI->getTypeLoc().castAs<PointerTypeLoc>();
379
380 S.Diag(Loc, diag::warn_unused_voidptr)
381 << FixItHint::CreateRemoval(TL.getStarLoc());
382 return;
383 }
384 }
385
386 // Tell the user to assign it into a variable to force a volatile load if this
387 // isn't an array.
388 if (E->isGLValue() && E->getType().isVolatileQualified() &&
389 !E->getType()->isArrayType()) {
390 S.Diag(Loc, diag::warn_unused_volatile) << R1 << R2;
391 return;
392 }
393
394 // Do not diagnose use of a comma operator in a SFINAE context because the
395 // type of the left operand could be used for SFINAE, so technically it is
396 // *used*.
397 if (DiagID == diag::warn_unused_comma_left_operand && S.isSFINAEContext())
398 return;
399
400 S.DiagIfReachable(Loc, llvm::ArrayRef<const Stmt *>(E),
401 S.PDiag(*DiagID) << R1 << R2);
402 }
403 } // namespace
404
405 void Sema::DiagnoseUnusedExprResult(const Stmt *S, unsigned DiagID) {
406 if (const LabelStmt *Label = dyn_cast_if_present<LabelStmt>(S))
407 S = Label->getSubStmt();
408
409 const Expr *E = dyn_cast_if_present<Expr>(S);
410 if (!E)
411 return;
412
413 DiagnoseUnused(*this, E, DiagID);
414 }
415
416 void Sema::ActOnStartOfCompoundStmt(bool IsStmtExpr) {
417 PushCompoundScope(IsStmtExpr);
418 }
419
420 void Sema::ActOnAfterCompoundStatementLeadingPragmas() {
421 if (getCurFPFeatures().isFPConstrained()) {
422 FunctionScopeInfo *FSI = getCurFunction();
423 assert(FSI);
424 FSI->setUsesFPIntrin();
425 }
426 }
427
428 void Sema::ActOnFinishOfCompoundStmt() {
429 PopCompoundScope();
430 }
431
432 sema::CompoundScopeInfo &Sema::getCurCompoundScope() const {
433 return getCurFunction()->CompoundScopes.back();
434 }
435
436 StmtResult Sema::ActOnCompoundStmt(SourceLocation L, SourceLocation R,
437 ArrayRef<Stmt *> Elts, bool isStmtExpr) {
438 const unsigned NumElts = Elts.size();
439
440 // If we're in C mode, check that we don't have any decls after stmts. If
441 // so, emit an extension diagnostic in C89 and potentially a warning in later
442 // versions.
443 const unsigned MixedDeclsCodeID = getLangOpts().C99
444 ? diag::warn_mixed_decls_code
445 : diag::ext_mixed_decls_code;
446 if (!getLangOpts().CPlusPlus && !Diags.isIgnored(MixedDeclsCodeID, L)) {
447 // Note that __extension__ can be around a decl.
448 unsigned i = 0;
449 // Skip over all declarations.
450 for (; i != NumElts && isa<DeclStmt>(Elts[i]); ++i)
451 /*empty*/;
452
453 // We found the end of the list or a statement. Scan for another declstmt.
454 for (; i != NumElts && !isa<DeclStmt>(Elts[i]); ++i)
455 /*empty*/;
456
457 if (i != NumElts) {
458 Decl *D = *cast<DeclStmt>(Elts[i])->decl_begin();
459 Diag(D->getLocation(), MixedDeclsCodeID);
460 }
461 }
462
463 // Check for suspicious empty body (null statement) in `for' and `while'
464 // statements. Don't do anything for template instantiations, this just adds
465 // noise.
466 if (NumElts != 0 && !CurrentInstantiationScope &&
467 getCurCompoundScope().HasEmptyLoopBodies) {
468 for (unsigned i = 0; i != NumElts - 1; ++i)
469 DiagnoseEmptyLoopBody(Elts[i], Elts[i + 1]);
470 }
471
472 // Calculate difference between FP options in this compound statement and in
473 // the enclosing one. If this is a function body, take the difference against
474 // default options. In this case the difference will indicate options that are
475 // changed upon entry to the statement.
476 FPOptions FPO = (getCurFunction()->CompoundScopes.size() == 1)
477 ? FPOptions(getLangOpts())
478 : getCurCompoundScope().InitialFPFeatures;
479 FPOptionsOverride FPDiff = getCurFPFeatures().getChangesFrom(FPO);
480
481 return CompoundStmt::Create(Context, Elts, FPDiff, L, R);
482 }
483
484 ExprResult
485 Sema::ActOnCaseExpr(SourceLocation CaseLoc, ExprResult Val) {
486 if (!Val.get())
487 return Val;
488
489 if (DiagnoseUnexpandedParameterPack(Val.get()))
490 return ExprError();
491
492 // If we're not inside a switch, let the 'case' statement handling diagnose
493 // this. Just clean up after the expression as best we can.
494 if (getCurFunction()->SwitchStack.empty())
495 return ActOnFinishFullExpr(Val.get(), Val.get()->getExprLoc(), false,
496 getLangOpts().CPlusPlus11);
497
498 Expr *CondExpr =
499 getCurFunction()->SwitchStack.back().getPointer()->getCond();
500 if (!CondExpr)
501 return ExprError();
502 QualType CondType = CondExpr->getType();
503
504 auto CheckAndFinish = [&](Expr *E) {
505 if (CondType->isDependentType() || E->isTypeDependent())
506 return ExprResult(E);
507
508 if (getLangOpts().CPlusPlus11) {
509 // C++11 [stmt.switch]p2: the constant-expression shall be a converted
510 // constant expression of the promoted type of the switch condition.
511 llvm::APSInt TempVal;
512 return CheckConvertedConstantExpression(E, CondType, TempVal,
513 CCEKind::CaseValue);
514 }
515
516 ExprResult ER = E;
517 if (!E->isValueDependent())
518 ER = VerifyIntegerConstantExpression(E, AllowFoldKind::Allow);
519 if (!ER.isInvalid())
520 ER = DefaultLvalueConversion(ER.get());
521 if (!ER.isInvalid())
522 ER = ImpCastExprToType(ER.get(), CondType, CK_IntegralCast);
523 if (!ER.isInvalid())
524 ER = ActOnFinishFullExpr(ER.get(), ER.get()->getExprLoc(), false);
525 return ER;
526 };
527
528 return CheckAndFinish(Val.get());
529 }
530
531 StmtResult
532 Sema::ActOnCaseStmt(SourceLocation CaseLoc, ExprResult LHSVal,
533 SourceLocation DotDotDotLoc, ExprResult RHSVal,
534 SourceLocation ColonLoc) {
535 assert((LHSVal.isInvalid() || LHSVal.get()) && "missing LHS value");
536 assert((DotDotDotLoc.isInvalid() ? RHSVal.isUnset()
537 : RHSVal.isInvalid() || RHSVal.get()) &&
538 "missing RHS value");
539
540 if (getCurFunction()->SwitchStack.empty()) {
541 Diag(CaseLoc, diag::err_case_not_in_switch);
542 return StmtError();
543 }
544
545 if (LHSVal.isInvalid() || RHSVal.isInvalid()) {
546 getCurFunction()->SwitchStack.back().setInt(true);
547 return StmtError();
548 }
549
550 if (LangOpts.OpenACC &&
551 getCurScope()->isInOpenACCComputeConstructScope(Scope::SwitchScope)) {
552 Diag(CaseLoc, diag::err_acc_branch_in_out_compute_construct)
553 << /*branch*/ 0 << /*into*/ 1;
554 return StmtError();
555 }
556
557 auto *CS = CaseStmt::Create(Context, LHSVal.get(), RHSVal.get(),
558 CaseLoc, DotDotDotLoc, ColonLoc);
559 getCurFunction()->SwitchStack.back().getPointer()->addSwitchCase(CS);
560 return CS;
561 }
562
563 void Sema::ActOnCaseStmtBody(Stmt *S, Stmt *SubStmt) {
564 cast<CaseStmt>(S)->setSubStmt(SubStmt);
565 }
566
567 StmtResult
568 Sema::ActOnDefaultStmt(SourceLocation DefaultLoc, SourceLocation ColonLoc,
569 Stmt *SubStmt, Scope *CurScope) {
570 if (getCurFunction()->SwitchStack.empty()) {
571 Diag(DefaultLoc, diag::err_default_not_in_switch);
572 return SubStmt;
573 }
574
575 if (LangOpts.OpenACC &&
576 getCurScope()->isInOpenACCComputeConstructScope(Scope::SwitchScope)) {
577 Diag(DefaultLoc, diag::err_acc_branch_in_out_compute_construct)
578 << /*branch*/ 0 << /*into*/ 1;
579 return StmtError();
580 }
581
582 DefaultStmt *DS = new (Context) DefaultStmt(DefaultLoc, ColonLoc, SubStmt);
583 getCurFunction()->SwitchStack.back().getPointer()->addSwitchCase(DS);
584 return DS;
585 }
586
587 StmtResult
588 Sema::ActOnLabelStmt(SourceLocation IdentLoc, LabelDecl *TheDecl,
589 SourceLocation ColonLoc, Stmt *SubStmt) {
590 // If the label was multiply defined, reject it now.
591 if (TheDecl->getStmt()) {
592 Diag(IdentLoc, diag::err_redefinition_of_label) << TheDecl->getDeclName();
593 Diag(TheDecl->getLocation(), diag::note_previous_definition);
594 return SubStmt;
595 }
596
597 ReservedIdentifierStatus Status = TheDecl->isReserved(getLangOpts());
598 if (isReservedInAllContexts(Status) &&
599 !Context.getSourceManager().isInSystemHeader(IdentLoc))
600 Diag(IdentLoc, diag::warn_reserved_extern_symbol)
601 << TheDecl << static_cast<int>(Status);
602
603 // If this label is in a compute construct scope, we need to make sure we
604 // check gotos in/out.
605 if (getCurScope()->isInOpenACCComputeConstructScope())
606 setFunctionHasBranchProtectedScope();
607
608 // OpenACC3.3 2.14.4:
609 // The update directive is executable. It must not appear in place of the
610 // statement following an 'if', 'while', 'do', 'switch', or 'label' in C or
611 // C++.
612 if (isa<OpenACCUpdateConstruct>(SubStmt)) {
613 Diag(SubStmt->getBeginLoc(), diag::err_acc_update_as_body) << /*Label*/ 4;
614 SubStmt = new (Context) NullStmt(SubStmt->getBeginLoc());
615 }
616
617 // Otherwise, things are good. Fill in the declaration and return it.
618 LabelStmt *LS = new (Context) LabelStmt(IdentLoc, TheDecl, SubStmt);
619 TheDecl->setStmt(LS);
620 if (!TheDecl->isGnuLocal()) {
621 TheDecl->setLocStart(IdentLoc);
622 if (!TheDecl->isMSAsmLabel()) {
623 // Don't update the location of MS ASM labels. These will result in
624 // a diagnostic, and changing the location here will mess that up.
625 TheDecl->setLocation(IdentLoc);
626 }
627 }
628 return LS;
629 }
630
631 StmtResult Sema::BuildAttributedStmt(SourceLocation AttrsLoc,
632 ArrayRef<const Attr *> Attrs,
633 Stmt *SubStmt) {
634 // FIXME: this code should move when a planned refactoring around statement
635 // attributes lands.
636 for (const auto *A : Attrs) {
637 if (A->getKind() == attr::MustTail) {
638 if (!checkAndRewriteMustTailAttr(SubStmt, *A)) {
639 return SubStmt;
640 }
641 setFunctionHasMustTail();
642 }
643 }
644
645 return AttributedStmt::Create(Context, AttrsLoc, Attrs, SubStmt);
646 }
647
648 StmtResult Sema::ActOnAttributedStmt(const ParsedAttributes &Attrs,
649 Stmt *SubStmt) {
650 SmallVector<const Attr *, 1> SemanticAttrs;
651 ProcessStmtAttributes(SubStmt, Attrs, SemanticAttrs);
652 if (!SemanticAttrs.empty())
653 return BuildAttributedStmt(Attrs.Range.getBegin(), SemanticAttrs, SubStmt);
654 // If none of the attributes applied, that's fine, we can recover by
655 // returning the substatement directly instead of making an AttributedStmt
656 // with no attributes on it.
657 return SubStmt;
658 }
659
660 bool Sema::checkAndRewriteMustTailAttr(Stmt *St, const Attr &MTA) {
661 ReturnStmt *R = cast<ReturnStmt>(St);
662 Expr *E = R->getRetValue();
663
664 if (CurContext->isDependentContext() || (E && E->isInstantiationDependent()))
665 // We have to suspend our check until template instantiation time.
666 return true;
667
668 if (!checkMustTailAttr(St, MTA))
669 return false;
670
671 // FIXME: Replace Expr::IgnoreImplicitAsWritten() with this function.
672 // Currently it does not skip implicit constructors in an initialization
673 // context.
674 auto IgnoreImplicitAsWritten = [](Expr *E) -> Expr * {
675 return IgnoreExprNodes(E, IgnoreImplicitAsWrittenSingleStep,
676 IgnoreElidableImplicitConstructorSingleStep);
677 };
678
679 // Now that we have verified that 'musttail' is valid here, rewrite the
680 // return value to remove all implicit nodes, but retain parentheses.
681 R->setRetValue(IgnoreImplicitAsWritten(E));
682 return true;
683 }
684
685 bool Sema::checkMustTailAttr(const Stmt *St, const Attr &MTA) {
686 assert(!CurContext->isDependentContext() &&
687 "musttail cannot be checked from a dependent context");
688
689 // FIXME: Add Expr::IgnoreParenImplicitAsWritten() with this definition.
690 auto IgnoreParenImplicitAsWritten = [](const Expr *E) -> const Expr * {
691 return IgnoreExprNodes(const_cast<Expr *>(E), IgnoreParensSingleStep,
692 IgnoreImplicitAsWrittenSingleStep,
693 IgnoreElidableImplicitConstructorSingleStep);
694 };
695
696 const Expr *E = cast<ReturnStmt>(St)->getRetValue();
697 const auto *CE = dyn_cast_or_null<CallExpr>(IgnoreParenImplicitAsWritten(E));
698
699 if (!CE) {
700 Diag(St->getBeginLoc(), diag::err_musttail_needs_call) << &MTA;
701 return false;
702 }
703
704 if (const FunctionDecl *CalleeDecl = CE->getDirectCallee();
705 CalleeDecl && CalleeDecl->hasAttr<NotTailCalledAttr>()) {
706 Diag(St->getBeginLoc(), diag::err_musttail_mismatch) << /*show-function-callee=*/true << CalleeDecl;
707 Diag(CalleeDecl->getLocation(), diag::note_musttail_disabled_by_not_tail_called);
708 return false;
709 }
710
711 if (const auto *EWC = dyn_cast<ExprWithCleanups>(E)) {
712 if (EWC->cleanupsHaveSideEffects()) {
713 Diag(St->getBeginLoc(), diag::err_musttail_needs_trivial_args) << &MTA;
714 return false;
715 }
716 }
717
718 // We need to determine the full function type (including "this" type, if any)
719 // for both caller and callee.
720 struct FuncType {
721 enum {
722 ft_non_member,
723 ft_static_member,
724 ft_non_static_member,
725 ft_pointer_to_member,
726 } MemberType = ft_non_member;
727
728 QualType This;
729 const FunctionProtoType *Func;
730 const CXXMethodDecl *Method = nullptr;
731 } CallerType, CalleeType;
732
733 auto GetMethodType = [this, St, MTA](const CXXMethodDecl *CMD, FuncType &Type,
734 bool IsCallee) -> bool {
735 if (isa<CXXConstructorDecl, CXXDestructorDecl>(CMD)) {
736 Diag(St->getBeginLoc(), diag::err_musttail_structors_forbidden)
737 << IsCallee << isa<CXXDestructorDecl>(CMD);
738 if (IsCallee)
739 Diag(CMD->getBeginLoc(), diag::note_musttail_structors_forbidden)
740 << isa<CXXDestructorDecl>(CMD);
741 Diag(MTA.getLocation(), diag::note_tail_call_required) << &MTA;
742 return false;
743 }
744 if (CMD->isStatic())
745 Type.MemberType = FuncType::ft_static_member;
746 else {
747 Type.This = CMD->getFunctionObjectParameterType();
748 Type.MemberType = FuncType::ft_non_static_member;
749 }
750 Type.Func = CMD->getType()->castAs<FunctionProtoType>();
751 return true;
752 };
753
754 const auto *CallerDecl = dyn_cast<FunctionDecl>(CurContext);
755
756 // Find caller function signature.
757 if (!CallerDecl) {
758 int ContextType;
759 if (isa<BlockDecl>(CurContext))
760 ContextType = 0;
761 else if (isa<ObjCMethodDecl>(CurContext))
762 ContextType = 1;
763 else
764 ContextType = 2;
765 Diag(St->getBeginLoc(), diag::err_musttail_forbidden_from_this_context)
766 << &MTA << ContextType;
767 return false;
768 } else if (const auto *CMD = dyn_cast<CXXMethodDecl>(CurContext)) {
769 // Caller is a class/struct method.
770 if (!GetMethodType(CMD, CallerType, false))
771 return false;
772 } else {
773 // Caller is a non-method function.
774 CallerType.Func = CallerDecl->getType()->getAs<FunctionProtoType>();
775 }
776
777 const Expr *CalleeExpr = CE->getCallee()->IgnoreParens();
778 const auto *CalleeBinOp = dyn_cast<BinaryOperator>(CalleeExpr);
779 SourceLocation CalleeLoc = CE->getCalleeDecl()
780 ? CE->getCalleeDecl()->getBeginLoc()
781 : St->getBeginLoc();
782
783 // Find callee function signature.
784 if (const CXXMethodDecl *CMD =
785 dyn_cast_or_null<CXXMethodDecl>(CE->getCalleeDecl())) {
786 // Call is: obj.method(), obj->method(), functor(), etc.
787 if (!GetMethodType(CMD, CalleeType, true))
788 return false;
789 } else if (CalleeBinOp && CalleeBinOp->isPtrMemOp()) {
790 // Call is: obj->*method_ptr or obj.*method_ptr
791 const auto *MPT =
792 CalleeBinOp->getRHS()->getType()->castAs<MemberPointerType>();
793 CalleeType.This =
794 Context.getCanonicalTagType(MPT->getMostRecentCXXRecordDecl());
795 CalleeType.Func = MPT->getPointeeType()->castAs<FunctionProtoType>();
796 CalleeType.MemberType = FuncType::ft_pointer_to_member;
797 } else if (isa<CXXPseudoDestructorExpr>(CalleeExpr)) {
798 Diag(St->getBeginLoc(), diag::err_musttail_structors_forbidden)
799 << /* IsCallee = */ 1 << /* IsDestructor = */ 1;
800 Diag(MTA.getLocation(), diag::note_tail_call_required) << &MTA;
801 return false;
802 } else {
803 // Non-method function.
804 CalleeType.Func =
805 CalleeExpr->getType()->getPointeeType()->getAs<FunctionProtoType>();
806 }
807
808 // Both caller and callee must have a prototype (no K&R declarations).
809 if (!CalleeType.Func || !CallerType.Func) {
810 Diag(St->getBeginLoc(), diag::err_musttail_needs_prototype) << &MTA;
811 if (!CalleeType.Func && CE->getDirectCallee()) {
812 Diag(CE->getDirectCallee()->getBeginLoc(),
813 diag::note_musttail_fix_non_prototype);
814 }
815 if (!CallerType.Func)
816 Diag(CallerDecl->getBeginLoc(), diag::note_musttail_fix_non_prototype);
817 return false;
818 }
819
820 // Caller and callee must have matching calling conventions.
821 //
822 // Some calling conventions are physically capable of supporting tail calls
823 // even if the function types don't perfectly match. LLVM is currently too
824 // strict to allow this, but if LLVM added support for this in the future, we
825 // could exit early here and skip the remaining checks if the functions are
826 // using such a calling convention.
827 if (CallerType.Func->getCallConv() != CalleeType.Func->getCallConv()) {
828 if (const auto *ND = dyn_cast_or_null<NamedDecl>(CE->getCalleeDecl()))
829 Diag(St->getBeginLoc(), diag::err_musttail_callconv_mismatch)
830 << true << ND->getDeclName();
831 else
832 Diag(St->getBeginLoc(), diag::err_musttail_callconv_mismatch) << false;
833 Diag(CalleeLoc, diag::note_musttail_callconv_mismatch)
834 << FunctionType::getNameForCallConv(CallerType.Func->getCallConv())
835 << FunctionType::getNameForCallConv(CalleeType.Func->getCallConv());
836 Diag(MTA.getLocation(), diag::note_tail_call_required) << &MTA;
837 return false;
838 }
839
840 if (CalleeType.Func->isVariadic() || CallerType.Func->isVariadic()) {
841 Diag(St->getBeginLoc(), diag::err_musttail_no_variadic) << &MTA;
842 return false;
843 }
844
845 const auto *CalleeDecl = CE->getCalleeDecl();
846 if (CalleeDecl && CalleeDecl->hasAttr<CXX11NoReturnAttr>()) {
847 Diag(St->getBeginLoc(), diag::err_musttail_no_return) << &MTA;
848 return false;
849 }
850
851 // Caller and callee must match in whether they have a "this" parameter.
852 if (CallerType.This.isNull() != CalleeType.This.isNull()) {
853 if (const auto *ND = dyn_cast_or_null<NamedDecl>(CE->getCalleeDecl())) {
854 Diag(St->getBeginLoc(), diag::err_musttail_member_mismatch)
855 << CallerType.MemberType << CalleeType.MemberType << true
856 << ND->getDeclName();
857 Diag(CalleeLoc, diag::note_musttail_callee_defined_here)
858 << ND->getDeclName();
859 } else
860 Diag(St->getBeginLoc(), diag::err_musttail_member_mismatch)
861 << CallerType.MemberType << CalleeType.MemberType << false;
862 Diag(MTA.getLocation(), diag::note_tail_call_required) << &MTA;
863 return false;
864 }
865
866 auto CheckTypesMatch = [this](FuncType CallerType, FuncType CalleeType,
867 PartialDiagnostic &PD) -> bool {
868 enum {
869 ft_different_class,
870 ft_parameter_arity,
871 ft_parameter_mismatch,
872 ft_return_type,
873 };
874
875 auto DoTypesMatch = [this, &PD](QualType A, QualType B,
876 unsigned Select) -> bool {
877 if (!Context.hasSimilarType(A, B)) {
878 PD << Select << A.getUnqualifiedType() << B.getUnqualifiedType();
879 return false;
880 }
881 return true;
882 };
883
884 if (!CallerType.This.isNull() &&
885 !DoTypesMatch(CallerType.This, CalleeType.This, ft_different_class))
886 return false;
887
888 if (!DoTypesMatch(CallerType.Func->getReturnType(),
889 CalleeType.Func->getReturnType(), ft_return_type))
890 return false;
891
892 if (CallerType.Func->getNumParams() != CalleeType.Func->getNumParams()) {
893 PD << ft_parameter_arity << CallerType.Func->getNumParams()
894 << CalleeType.Func->getNumParams();
895 return false;
896 }
897
898 ArrayRef<QualType> CalleeParams = CalleeType.Func->getParamTypes();
899 ArrayRef<QualType> CallerParams = CallerType.Func->getParamTypes();
900 size_t N = CallerType.Func->getNumParams();
901 for (size_t I = 0; I < N; I++) {
902 if (!DoTypesMatch(CalleeParams[I], CallerParams[I],
903 ft_parameter_mismatch)) {
904 PD << static_cast<int>(I) + 1;
905 return false;
906 }
907 }
908
909 return true;
910 };
911
912 PartialDiagnostic PD = PDiag(diag::note_musttail_mismatch);
913 if (!CheckTypesMatch(CallerType, CalleeType, PD)) {
914 if (const auto *ND = dyn_cast_or_null<NamedDecl>(CE->getCalleeDecl()))
915 Diag(St->getBeginLoc(), diag::err_musttail_mismatch)
916 << true << ND->getDeclName();
917 else
918 Diag(St->getBeginLoc(), diag::err_musttail_mismatch) << false;
919 Diag(CalleeLoc, PD);
920 Diag(MTA.getLocation(), diag::note_tail_call_required) << &MTA;
921 return false;
922 }
923
924 // The lifetimes of locals and incoming function parameters must end before
925 // the call, because we can't have a stack frame to store them, so diagnose
926 // any pointers or references to them passed into the musttail call.
927 for (auto ArgExpr : CE->arguments()) {
928 InitializedEntity Entity = InitializedEntity::InitializeParameter(
929 Context, ArgExpr->getType(), false);
930 checkExprLifetimeMustTailArg(*this, Entity, const_cast<Expr *>(ArgExpr));
931 }
932
933 return true;
934 }
935
936 namespace {
937 class CommaVisitor : public EvaluatedExprVisitor<CommaVisitor> {
938 typedef EvaluatedExprVisitor<CommaVisitor> Inherited;
939 Sema &SemaRef;
940 public:
941 CommaVisitor(Sema &SemaRef) : Inherited(SemaRef.Context), SemaRef(SemaRef) {}
942 void VisitBinaryOperator(BinaryOperator *E) {
943 if (E->getOpcode() == BO_Comma)
944 SemaRef.DiagnoseCommaOperator(E->getLHS(), E->getExprLoc());
945 EvaluatedExprVisitor<CommaVisitor>::VisitBinaryOperator(E);
946 }
947 };
948 }
949
950 StmtResult Sema::ActOnIfStmt(SourceLocation IfLoc,
951 IfStatementKind StatementKind,
952 SourceLocation LParenLoc, Stmt *InitStmt,
953 ConditionResult Cond, SourceLocation RParenLoc,
954 Stmt *thenStmt, SourceLocation ElseLoc,
955 Stmt *elseStmt) {
956 if (Cond.isInvalid())
957 return StmtError();
958
959 bool ConstevalOrNegatedConsteval =
960 StatementKind == IfStatementKind::ConstevalNonNegated ||
961 StatementKind == IfStatementKind::ConstevalNegated;
962
963 Expr *CondExpr = Cond.get().second;
964 assert((CondExpr || ConstevalOrNegatedConsteval) &&
965 "If statement: missing condition");
966 // Only call the CommaVisitor when not C89 due to differences in scope flags.
967 if (CondExpr && (getLangOpts().C99 || getLangOpts().CPlusPlus) &&
968 !Diags.isIgnored(diag::warn_comma_operator, CondExpr->getExprLoc()))
969 CommaVisitor(*this).Visit(CondExpr);
970
971 if (!ConstevalOrNegatedConsteval && !elseStmt)
972 DiagnoseEmptyStmtBody(RParenLoc, thenStmt, diag::warn_empty_if_body);
973
974 if (ConstevalOrNegatedConsteval ||
975 StatementKind == IfStatementKind::Constexpr) {
976 auto DiagnoseLikelihood = [&](const Stmt *S) {
977 if (const Attr *A = Stmt::getLikelihoodAttr(S)) {
978 Diags.Report(A->getLocation(),
979 diag::warn_attribute_has_no_effect_on_compile_time_if)
980 << A << ConstevalOrNegatedConsteval << A->getRange();
981 Diags.Report(IfLoc,
982 diag::note_attribute_has_no_effect_on_compile_time_if_here)
983 << ConstevalOrNegatedConsteval
984 << SourceRange(IfLoc, (ConstevalOrNegatedConsteval
985 ? thenStmt->getBeginLoc()
986 : LParenLoc)
987 .getLocWithOffset(-1));
988 }
989 };
990 DiagnoseLikelihood(thenStmt);
991 DiagnoseLikelihood(elseStmt);
992 } else {
993 std::tuple<bool, const Attr *, const Attr *> LHC =
994 Stmt::determineLikelihoodConflict(thenStmt, elseStmt);
995 if (std::get<0>(LHC)) {
996 const Attr *ThenAttr = std::get<1>(LHC);
997 const Attr *ElseAttr = std::get<2>(LHC);
998 Diags.Report(ThenAttr->getLocation(),
999 diag::warn_attributes_likelihood_ifstmt_conflict)
1000 << ThenAttr << ThenAttr->getRange();
1001 Diags.Report(ElseAttr->getLocation(), diag::note_conflicting_attribute)
1002 << ElseAttr << ElseAttr->getRange();
1003 }
1004 }
1005
1006 if (ConstevalOrNegatedConsteval) {
1007 bool Immediate = ExprEvalContexts.back().Context ==
1008 ExpressionEvaluationContext::ImmediateFunctionContext;
1009 if (CurContext->isFunctionOrMethod()) {
1010 const auto *FD =
1011 dyn_cast<FunctionDecl>(Decl::castFromDeclContext(CurContext));
1012 if (FD && FD->isImmediateFunction())
1013 Immediate = true;
1014 }
1015 if (isUnevaluatedContext() || Immediate)
1016 Diags.Report(IfLoc, diag::warn_consteval_if_always_true) << Immediate;
1017 }
1018
1019 // OpenACC3.3 2.14.4:
1020 // The update directive is executable. It must not appear in place of the
1021 // statement following an 'if', 'while', 'do', 'switch', or 'label' in C or
1022 // C++.
1023 if (isa<OpenACCUpdateConstruct>(thenStmt)) {
1024 Diag(thenStmt->getBeginLoc(), diag::err_acc_update_as_body) << /*if*/ 0;
1025 thenStmt = new (Context) NullStmt(thenStmt->getBeginLoc());
1026 }
1027
1028 return BuildIfStmt(IfLoc, StatementKind, LParenLoc, InitStmt, Cond, RParenLoc,
1029 thenStmt, ElseLoc, elseStmt);
1030 }
1031
1032 StmtResult Sema::BuildIfStmt(SourceLocation IfLoc,
1033 IfStatementKind StatementKind,
1034 SourceLocation LParenLoc, Stmt *InitStmt,
1035 ConditionResult Cond, SourceLocation RParenLoc,
1036 Stmt *thenStmt, SourceLocation ElseLoc,
1037 Stmt *elseStmt) {
1038 if (Cond.isInvalid())
1039 return StmtError();
1040
1041 if (StatementKind != IfStatementKind::Ordinary ||
1042 isa<ObjCAvailabilityCheckExpr>(Cond.get().second))
1043 setFunctionHasBranchProtectedScope();
1044
1045 return IfStmt::Create(Context, IfLoc, StatementKind, InitStmt,
1046 Cond.get().first, Cond.get().second, LParenLoc,
1047 RParenLoc, thenStmt, ElseLoc, elseStmt);
1048 }
1049
1050 namespace {
1051 struct CaseCompareFunctor {
1052 bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS,
1053 const llvm::APSInt &RHS) {
1054 return LHS.first < RHS;
1055 }
1056 bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS,
1057 const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
1058 return LHS.first < RHS.first;
1059 }
1060 bool operator()(const llvm::APSInt &LHS,
1061 const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
1062 return LHS < RHS.first;
1063 }
1064 };
1065 }
1066
1067 /// CmpCaseVals - Comparison predicate for sorting case values.
1068 ///
1069 static bool CmpCaseVals(const std::pair<llvm::APSInt, CaseStmt*>& lhs,
1070 const std::pair<llvm::APSInt, CaseStmt*>& rhs) {
1071 if (lhs.first < rhs.first)
1072 return true;
1073
1074 if (lhs.first == rhs.first &&
1075 lhs.second->getCaseLoc() < rhs.second->getCaseLoc())
1076 return true;
1077 return false;
1078 }
1079
1080 /// CmpEnumVals - Comparison predicate for sorting enumeration values.
1081 ///
1082 static bool CmpEnumVals(const std::pair<llvm::APSInt, EnumConstantDecl*>& lhs,
1083 const std::pair<llvm::APSInt, EnumConstantDecl*>& rhs)
1084 {
1085 return lhs.first < rhs.first;
1086 }
1087
1088 /// EqEnumVals - Comparison preficate for uniqing enumeration values.
1089 ///
1090 static bool EqEnumVals(const std::pair<llvm::APSInt, EnumConstantDecl*>& lhs,
1091 const std::pair<llvm::APSInt, EnumConstantDecl*>& rhs)
1092 {
1093 return lhs.first == rhs.first;
1094 }
1095
1096 /// GetTypeBeforeIntegralPromotion - Returns the pre-promotion type of
1097 /// potentially integral-promoted expression @p expr.
1098 static QualType GetTypeBeforeIntegralPromotion(const Expr *&E) {
1099 if (const auto *FE = dyn_cast<FullExpr>(E))
1100 E = FE->getSubExpr();
1101 while (const auto *ImpCast = dyn_cast<ImplicitCastExpr>(E)) {
1102 if (ImpCast->getCastKind() != CK_IntegralCast) break;
1103 E = ImpCast->getSubExpr();
1104 }
1105 return E->getType();
1106 }
1107
1108 ExprResult Sema::CheckSwitchCondition(SourceLocation SwitchLoc, Expr *Cond) {
1109 class SwitchConvertDiagnoser : public ICEConvertDiagnoser {
1110 Expr *Cond;
1111
1112 public:
1113 SwitchConvertDiagnoser(Expr *Cond)
1114 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/true, false, true),
1115 Cond(Cond) {}
1116
1117 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
1118 QualType T) override {
1119 return S.Diag(Loc, diag::err_typecheck_statement_requires_integer) << T;
1120 }
1121
1122 SemaDiagnosticBuilder diagnoseIncomplete(
1123 Sema &S, SourceLocation Loc, QualType T) override {
1124 return S.Diag(Loc, diag::err_switch_incomplete_class_type)
1125 << T << Cond->getSourceRange();
1126 }
1127
1128 SemaDiagnosticBuilder diagnoseExplicitConv(
1129 Sema &S, SourceLocation Loc, QualType T, QualType ConvTy) override {
1130 return S.Diag(Loc, diag::err_switch_explicit_conversion) << T << ConvTy;
1131 }
1132
1133 SemaDiagnosticBuilder noteExplicitConv(
1134 Sema &S, CXXConversionDecl *Conv, QualType ConvTy) override {
1135 return S.Diag(Conv->getLocation(), diag::note_switch_conversion)
1136 << ConvTy->isEnumeralType() << ConvTy;
1137 }
1138
1139 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
1140 QualType T) override {
1141 return S.Diag(Loc, diag::err_switch_multiple_conversions) << T;
1142 }
1143
1144 SemaDiagnosticBuilder noteAmbiguous(
1145 Sema &S, CXXConversionDecl *Conv, QualType ConvTy) override {
1146 return S.Diag(Conv->getLocation(), diag::note_switch_conversion)
1147 << ConvTy->isEnumeralType() << ConvTy;
1148 }
1149
1150 SemaDiagnosticBuilder diagnoseConversion(
1151 Sema &S, SourceLocation Loc, QualType T, QualType ConvTy) override {
1152 llvm_unreachable("conversion functions are permitted");
1153 }
1154 } SwitchDiagnoser(Cond);
1155
1156 ExprResult CondResult =
1157 PerformContextualImplicitConversion(SwitchLoc, Cond, SwitchDiagnoser);
1158 if (CondResult.isInvalid())
1159 return ExprError();
1160
1161 // FIXME: PerformContextualImplicitConversion doesn't always tell us if it
1162 // failed and produced a diagnostic.
1163 Cond = CondResult.get();
1164 if (!Cond->isTypeDependent() &&
1165 !Cond->getType()->isIntegralOrEnumerationType())
1166 return ExprError();
1167
1168 // C99 6.8.4.2p5 - Integer promotions are performed on the controlling expr.
1169 return UsualUnaryConversions(Cond);
1170 }
1171
1172 StmtResult Sema::ActOnStartOfSwitchStmt(SourceLocation SwitchLoc,
1173 SourceLocation LParenLoc,
1174 Stmt *InitStmt, ConditionResult Cond,
1175 SourceLocation RParenLoc) {
1176 Expr *CondExpr = Cond.get().second;
1177 assert((Cond.isInvalid() || CondExpr) && "switch with no condition");
1178
1179 if (CondExpr && !CondExpr->isTypeDependent()) {
1180 // We have already converted the expression to an integral or enumeration
1181 // type, when we parsed the switch condition. There are cases where we don't
1182 // have an appropriate type, e.g. a typo-expr Cond was corrected to an
1183 // inappropriate-type expr, we just return an error.
1184 if (!CondExpr->getType()->isIntegralOrEnumerationType())
1185 return StmtError();
1186 if (CondExpr->isKnownToHaveBooleanValue()) {
1187 // switch(bool_expr) {...} is often a programmer error, e.g.
1188 // switch(n && mask) { ... } // Doh - should be "n & mask".
1189 // One can always use an if statement instead of switch(bool_expr).
1190 Diag(SwitchLoc, diag::warn_bool_switch_condition)
1191 << CondExpr->getSourceRange();
1192 }
1193 }
1194
1195 setFunctionHasBranchIntoScope();
1196
1197 auto *SS = SwitchStmt::Create(Context, InitStmt, Cond.get().first, CondExpr,
1198 LParenLoc, RParenLoc);
1199 getCurFunction()->SwitchStack.push_back(
1200 FunctionScopeInfo::SwitchInfo(SS, false));
1201 return SS;
1202 }
1203
1204 static void AdjustAPSInt(llvm::APSInt &Val, unsigned BitWidth, bool IsSigned) {
1205 Val = Val.extOrTrunc(BitWidth);
1206 Val.setIsSigned(IsSigned);
1207 }
1208
1209 /// Check the specified case value is in range for the given unpromoted switch
1210 /// type.
1211 static void checkCaseValue(Sema &S, SourceLocation Loc, const llvm::APSInt &Val,
1212 unsigned UnpromotedWidth, bool UnpromotedSign) {
1213 // In C++11 onwards, this is checked by the language rules.
1214 if (S.getLangOpts().CPlusPlus11)
1215 return;
1216
1217 // If the case value was signed and negative and the switch expression is
1218 // unsigned, don't bother to warn: this is implementation-defined behavior.
1219 // FIXME: Introduce a second, default-ignored warning for this case?
1220 if (UnpromotedWidth < Val.getBitWidth()) {
1221 llvm::APSInt ConvVal(Val);
1222 AdjustAPSInt(ConvVal, UnpromotedWidth, UnpromotedSign);
1223 AdjustAPSInt(ConvVal, Val.getBitWidth(), Val.isSigned());
1224 // FIXME: Use different diagnostics for overflow in conversion to promoted
1225 // type versus "switch expression cannot have this value". Use proper
1226 // IntRange checking rather than just looking at the unpromoted type here.
1227 if (ConvVal != Val)
1228 S.Diag(Loc, diag::warn_case_value_overflow) << toString(Val, 10)
1229 << toString(ConvVal, 10);
1230 }
1231 }
1232
1233 typedef SmallVector<std::pair<llvm::APSInt, EnumConstantDecl*>, 64> EnumValsTy;
1234
1235 /// Returns true if we should emit a diagnostic about this case expression not
1236 /// being a part of the enum used in the switch controlling expression.
1237 static bool ShouldDiagnoseSwitchCaseNotInEnum(const Sema &S,
1238 const EnumDecl *ED,
1239 const Expr *CaseExpr,
1240 EnumValsTy::iterator &EI,
1241 EnumValsTy::iterator &EIEnd,
1242 const llvm::APSInt &Val) {
1243 if (!ED->isClosed())
1244 return false;
1245
1246 if (const DeclRefExpr *DRE =
1247 dyn_cast<DeclRefExpr>(CaseExpr->IgnoreParenImpCasts())) {
1248 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
1249 QualType VarType = VD->getType();
1250 CanQualType EnumType = S.Context.getCanonicalTagType(ED);
1251 if (VD->hasGlobalStorage() && VarType.isConstQualified() &&
1252 S.Context.hasSameUnqualifiedType(EnumType, VarType))
1253 return false;
1254 }
1255 }
1256
1257 if (ED->hasAttr<FlagEnumAttr>())
1258 return !S.IsValueInFlagEnum(ED, Val, false);
1259
1260 while (EI != EIEnd && EI->first < Val)
1261 EI++;
1262
1263 if (EI != EIEnd && EI->first == Val)
1264 return false;
1265
1266 return true;
1267 }
1268
1269 static void checkEnumTypesInSwitchStmt(Sema &S, const Expr *Cond,
1270 const Expr *Case) {
1271 QualType CondType = Cond->getType();
1272 QualType CaseType = Case->getType();
1273
1274 const EnumType *CondEnumType = CondType->getAsCanonical<EnumType>();
1275 const EnumType *CaseEnumType = CaseType->getAsCanonical<EnumType>();
1276 if (!CondEnumType || !CaseEnumType)
1277 return;
1278
1279 // Ignore anonymous enums.
1280 if (!CondEnumType->getDecl()->getIdentifier() &&
1281 !CondEnumType->getDecl()->getTypedefNameForAnonDecl())
1282 return;
1283 if (!CaseEnumType->getDecl()->getIdentifier() &&
1284 !CaseEnumType->getDecl()->getTypedefNameForAnonDecl())
1285 return;
1286
1287 if (S.Context.hasSameUnqualifiedType(CondType, CaseType))
1288 return;
1289
1290 S.Diag(Case->getExprLoc(), diag::warn_comparison_of_mixed_enum_types_switch)
1291 << CondType << CaseType << Cond->getSourceRange()
1292 << Case->getSourceRange();
1293 }
1294
1295 StmtResult
1296 Sema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, Stmt *Switch,
1297 Stmt *BodyStmt) {
1298 SwitchStmt *SS = cast<SwitchStmt>(Switch);
1299 bool CaseListIsIncomplete = getCurFunction()->SwitchStack.back().getInt();
1300 assert(SS == getCurFunction()->SwitchStack.back().getPointer() &&
1301 "switch stack missing push/pop!");
1302
1303 getCurFunction()->SwitchStack.pop_back();
1304
1305 if (!BodyStmt) return StmtError();
1306
1307 // OpenACC3.3 2.14.4:
1308 // The update directive is executable. It must not appear in place of the
1309 // statement following an 'if', 'while', 'do', 'switch', or 'label' in C or
1310 // C++.
1311 if (isa<OpenACCUpdateConstruct>(BodyStmt)) {
1312 Diag(BodyStmt->getBeginLoc(), diag::err_acc_update_as_body) << /*switch*/ 3;
1313 BodyStmt = new (Context) NullStmt(BodyStmt->getBeginLoc());
1314 }
1315
1316 SS->setBody(BodyStmt, SwitchLoc);
1317
1318 Expr *CondExpr = SS->getCond();
1319 if (!CondExpr) return StmtError();
1320
1321 QualType CondType = CondExpr->getType();
1322
1323 // C++ 6.4.2.p2:
1324 // Integral promotions are performed (on the switch condition).
1325 //
1326 // A case value unrepresentable by the original switch condition
1327 // type (before the promotion) doesn't make sense, even when it can
1328 // be represented by the promoted type. Therefore we need to find
1329 // the pre-promotion type of the switch condition.
1330 const Expr *CondExprBeforePromotion = CondExpr;
1331 QualType CondTypeBeforePromotion =
1332 GetTypeBeforeIntegralPromotion(CondExprBeforePromotion);
1333
1334 // Get the bitwidth of the switched-on value after promotions. We must
1335 // convert the integer case values to this width before comparison.
1336 bool HasDependentValue
1337 = CondExpr->isTypeDependent() || CondExpr->isValueDependent();
1338 unsigned CondWidth = HasDependentValue ? 0 : Context.getIntWidth(CondType);
1339 bool CondIsSigned = CondType->isSignedIntegerOrEnumerationType();
1340
1341 // Get the width and signedness that the condition might actually have, for
1342 // warning purposes.
1343 // FIXME: Grab an IntRange for the condition rather than using the unpromoted
1344 // type.
1345 unsigned CondWidthBeforePromotion
1346 = HasDependentValue ? 0 : Context.getIntWidth(CondTypeBeforePromotion);
1347 bool CondIsSignedBeforePromotion
1348 = CondTypeBeforePromotion->isSignedIntegerOrEnumerationType();
1349
1350 // Accumulate all of the case values in a vector so that we can sort them
1351 // and detect duplicates. This vector contains the APInt for the case after
1352 // it has been converted to the condition type.
1353 typedef SmallVector<std::pair<llvm::APSInt, CaseStmt*>, 64> CaseValsTy;
1354 CaseValsTy CaseVals;
1355
1356 // Keep track of any GNU case ranges we see. The APSInt is the low value.
1357 typedef std::vector<std::pair<llvm::APSInt, CaseStmt*> > CaseRangesTy;
1358 CaseRangesTy CaseRanges;
1359
1360 DefaultStmt *TheDefaultStmt = nullptr;
1361
1362 bool CaseListIsErroneous = false;
1363
1364 // FIXME: We'd better diagnose missing or duplicate default labels even
1365 // in the dependent case. Because default labels themselves are never
1366 // dependent.
1367 for (SwitchCase *SC = SS->getSwitchCaseList(); SC && !HasDependentValue;
1368 SC = SC->getNextSwitchCase()) {
1369
1370 if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) {
1371 if (TheDefaultStmt) {
1372 Diag(DS->getDefaultLoc(), diag::err_multiple_default_labels_defined);
1373 Diag(TheDefaultStmt->getDefaultLoc(), diag::note_duplicate_case_prev);
1374
1375 // FIXME: Remove the default statement from the switch block so that
1376 // we'll return a valid AST. This requires recursing down the AST and
1377 // finding it, not something we are set up to do right now. For now,
1378 // just lop the entire switch stmt out of the AST.
1379 CaseListIsErroneous = true;
1380 }
1381 TheDefaultStmt = DS;
1382
1383 } else {
1384 CaseStmt *CS = cast<CaseStmt>(SC);
1385
1386 Expr *Lo = CS->getLHS();
1387
1388 if (Lo->isValueDependent()) {
1389 HasDependentValue = true;
1390 break;
1391 }
1392
1393 // We already verified that the expression has a constant value;
1394 // get that value (prior to conversions).
1395 const Expr *LoBeforePromotion = Lo;
1396 GetTypeBeforeIntegralPromotion(LoBeforePromotion);
1397 llvm::APSInt LoVal = LoBeforePromotion->EvaluateKnownConstInt(Context);
1398
1399 // Check the unconverted value is within the range of possible values of
1400 // the switch expression.
1401 checkCaseValue(*this, Lo->getBeginLoc(), LoVal, CondWidthBeforePromotion,
1402 CondIsSignedBeforePromotion);
1403
1404 // FIXME: This duplicates the check performed for warn_not_in_enum below.
1405 checkEnumTypesInSwitchStmt(*this, CondExprBeforePromotion,
1406 LoBeforePromotion);
1407
1408 // Convert the value to the same width/sign as the condition.
1409 AdjustAPSInt(LoVal, CondWidth, CondIsSigned);
1410
1411 // If this is a case range, remember it in CaseRanges, otherwise CaseVals.
1412 if (CS->getRHS()) {
1413 if (CS->getRHS()->isValueDependent()) {
1414 HasDependentValue = true;
1415 break;
1416 }
1417 CaseRanges.push_back(std::make_pair(LoVal, CS));
1418 } else
1419 CaseVals.push_back(std::make_pair(LoVal, CS));
1420 }
1421 }
1422
1423 if (!HasDependentValue) {
1424 // If we don't have a default statement, check whether the
1425 // condition is constant.
1426 llvm::APSInt ConstantCondValue;
1427 bool HasConstantCond = false;
1428 if (!TheDefaultStmt) {
1429 Expr::EvalResult Result;
1430 HasConstantCond = CondExpr->EvaluateAsInt(Result, Context,
1431 Expr::SE_AllowSideEffects);
1432 if (Result.Val.isInt())
1433 ConstantCondValue = Result.Val.getInt();
1434 assert(!HasConstantCond ||
1435 (ConstantCondValue.getBitWidth() == CondWidth &&
1436 ConstantCondValue.isSigned() == CondIsSigned));
1437 Diag(SwitchLoc, diag::warn_switch_default);
1438 }
1439 bool ShouldCheckConstantCond = HasConstantCond;
1440
1441 // Sort all the scalar case values so we can easily detect duplicates.
1442 llvm::stable_sort(CaseVals, CmpCaseVals);
1443
1444 if (!CaseVals.empty()) {
1445 for (unsigned i = 0, e = CaseVals.size(); i != e; ++i) {
1446 if (ShouldCheckConstantCond &&
1447 CaseVals[i].first == ConstantCondValue)
1448 ShouldCheckConstantCond = false;
1449
1450 if (i != 0 && CaseVals[i].first == CaseVals[i-1].first) {
1451 // If we have a duplicate, report it.
1452 // First, determine if either case value has a name
1453 StringRef PrevString, CurrString;
1454 Expr *PrevCase = CaseVals[i-1].second->getLHS()->IgnoreParenCasts();
1455 Expr *CurrCase = CaseVals[i].second->getLHS()->IgnoreParenCasts();
1456 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(PrevCase)) {
1457 PrevString = DeclRef->getDecl()->getName();
1458 }
1459 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(CurrCase)) {
1460 CurrString = DeclRef->getDecl()->getName();
1461 }
1462 SmallString<16> CaseValStr;
1463 CaseVals[i-1].first.toString(CaseValStr);
1464
1465 if (PrevString == CurrString)
1466 Diag(CaseVals[i].second->getLHS()->getBeginLoc(),
1467 diag::err_duplicate_case)
1468 << (PrevString.empty() ? CaseValStr.str() : PrevString);
1469 else
1470 Diag(CaseVals[i].second->getLHS()->getBeginLoc(),
1471 diag::err_duplicate_case_differing_expr)
1472 << (PrevString.empty() ? CaseValStr.str() : PrevString)
1473 << (CurrString.empty() ? CaseValStr.str() : CurrString)
1474 << CaseValStr;
1475
1476 Diag(CaseVals[i - 1].second->getLHS()->getBeginLoc(),
1477 diag::note_duplicate_case_prev);
1478 // FIXME: We really want to remove the bogus case stmt from the
1479 // substmt, but we have no way to do this right now.
1480 CaseListIsErroneous = true;
1481 }
1482 }
1483 }
1484
1485 // Detect duplicate case ranges, which usually don't exist at all in
1486 // the first place.
1487 if (!CaseRanges.empty()) {
1488 // Sort all the case ranges by their low value so we can easily detect
1489 // overlaps between ranges.
1490 llvm::stable_sort(CaseRanges);
1491
1492 // Scan the ranges, computing the high values and removing empty ranges.
1493 std::vector<llvm::APSInt> HiVals;
1494 for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
1495 llvm::APSInt &LoVal = CaseRanges[i].first;
1496 CaseStmt *CR = CaseRanges[i].second;
1497 Expr *Hi = CR->getRHS();
1498
1499 const Expr *HiBeforePromotion = Hi;
1500 GetTypeBeforeIntegralPromotion(HiBeforePromotion);
1501 llvm::APSInt HiVal = HiBeforePromotion->EvaluateKnownConstInt(Context);
1502
1503 // Check the unconverted value is within the range of possible values of
1504 // the switch expression.
1505 checkCaseValue(*this, Hi->getBeginLoc(), HiVal,
1506 CondWidthBeforePromotion, CondIsSignedBeforePromotion);
1507
1508 // Convert the value to the same width/sign as the condition.
1509 AdjustAPSInt(HiVal, CondWidth, CondIsSigned);
1510
1511 // If the low value is bigger than the high value, the case is empty.
1512 if (LoVal > HiVal) {
1513 Diag(CR->getLHS()->getBeginLoc(), diag::warn_case_empty_range)
1514 << SourceRange(CR->getLHS()->getBeginLoc(), Hi->getEndLoc());
1515 CaseRanges.erase(CaseRanges.begin()+i);
1516 --i;
1517 --e;
1518 continue;
1519 }
1520
1521 if (ShouldCheckConstantCond &&
1522 LoVal <= ConstantCondValue &&
1523 ConstantCondValue <= HiVal)
1524 ShouldCheckConstantCond = false;
1525
1526 HiVals.push_back(HiVal);
1527 }
1528
1529 // Rescan the ranges, looking for overlap with singleton values and other
1530 // ranges. Since the range list is sorted, we only need to compare case
1531 // ranges with their neighbors.
1532 for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
1533 llvm::APSInt &CRLo = CaseRanges[i].first;
1534 llvm::APSInt &CRHi = HiVals[i];
1535 CaseStmt *CR = CaseRanges[i].second;
1536
1537 // Check to see whether the case range overlaps with any
1538 // singleton cases.
1539 CaseStmt *OverlapStmt = nullptr;
1540 llvm::APSInt OverlapVal(32);
1541
1542 // Find the smallest value >= the lower bound. If I is in the
1543 // case range, then we have overlap.
1544 CaseValsTy::iterator I =
1545 llvm::lower_bound(CaseVals, CRLo, CaseCompareFunctor());
1546 if (I != CaseVals.end() && I->first < CRHi) {
1547 OverlapVal = I->first; // Found overlap with scalar.
1548 OverlapStmt = I->second;
1549 }
1550
1551 // Find the smallest value bigger than the upper bound.
1552 I = std::upper_bound(I, CaseVals.end(), CRHi, CaseCompareFunctor());
1553 if (I != CaseVals.begin() && (I-1)->first >= CRLo) {
1554 OverlapVal = (I-1)->first; // Found overlap with scalar.
1555 OverlapStmt = (I-1)->second;
1556 }
1557
1558 // Check to see if this case stmt overlaps with the subsequent
1559 // case range.
1560 if (i && CRLo <= HiVals[i-1]) {
1561 OverlapVal = HiVals[i-1]; // Found overlap with range.
1562 OverlapStmt = CaseRanges[i-1].second;
1563 }
1564
1565 if (OverlapStmt) {
1566 // If we have a duplicate, report it.
1567 Diag(CR->getLHS()->getBeginLoc(), diag::err_duplicate_case)
1568 << toString(OverlapVal, 10);
1569 Diag(OverlapStmt->getLHS()->getBeginLoc(),
1570 diag::note_duplicate_case_prev);
1571 // FIXME: We really want to remove the bogus case stmt from the
1572 // substmt, but we have no way to do this right now.
1573 CaseListIsErroneous = true;
1574 }
1575 }
1576 }
1577
1578 // Complain if we have a constant condition and we didn't find a match.
1579 if (!CaseListIsErroneous && !CaseListIsIncomplete &&
1580 ShouldCheckConstantCond) {
1581 // TODO: it would be nice if we printed enums as enums, chars as
1582 // chars, etc.
1583 Diag(CondExpr->getExprLoc(), diag::warn_missing_case_for_condition)
1584 << toString(ConstantCondValue, 10)
1585 << CondExpr->getSourceRange();
1586 }
1587
1588 // Check to see if switch is over an Enum and handles all of its
1589 // values. We only issue a warning if there is not 'default:', but
1590 // we still do the analysis to preserve this information in the AST
1591 // (which can be used by flow-based analyes).
1592 //
1593 // If switch has default case, then ignore it.
1594 if (!CaseListIsErroneous && !CaseListIsIncomplete && !HasConstantCond &&
1595 CondTypeBeforePromotion->isEnumeralType()) {
1596 const auto *ED = CondTypeBeforePromotion->castAsEnumDecl();
1597 if (!ED->isCompleteDefinition() || ED->enumerators().empty())
1598 goto enum_out;
1599
1600 EnumValsTy EnumVals;
1601
1602 // Gather all enum values, set their type and sort them,
1603 // allowing easier comparison with CaseVals.
1604 for (auto *EDI : ED->enumerators()) {
1605 llvm::APSInt Val = EDI->getInitVal();
1606 AdjustAPSInt(Val, CondWidth, CondIsSigned);
1607 EnumVals.push_back(std::make_pair(Val, EDI));
1608 }
1609 llvm::stable_sort(EnumVals, CmpEnumVals);
1610 auto EI = EnumVals.begin(), EIEnd = llvm::unique(EnumVals, EqEnumVals);
1611
1612 // See which case values aren't in enum.
1613 for (CaseValsTy::const_iterator CI = CaseVals.begin();
1614 CI != CaseVals.end(); CI++) {
1615 Expr *CaseExpr = CI->second->getLHS();
1616 if (ShouldDiagnoseSwitchCaseNotInEnum(*this, ED, CaseExpr, EI, EIEnd,
1617 CI->first))
1618 Diag(CaseExpr->getExprLoc(), diag::warn_not_in_enum)
1619 << CondTypeBeforePromotion;
1620 }
1621
1622 // See which of case ranges aren't in enum
1623 EI = EnumVals.begin();
1624 for (CaseRangesTy::const_iterator RI = CaseRanges.begin();
1625 RI != CaseRanges.end(); RI++) {
1626 Expr *CaseExpr = RI->second->getLHS();
1627 if (ShouldDiagnoseSwitchCaseNotInEnum(*this, ED, CaseExpr, EI, EIEnd,
1628 RI->first))
1629 Diag(CaseExpr->getExprLoc(), diag::warn_not_in_enum)
1630 << CondTypeBeforePromotion;
1631
1632 llvm::APSInt Hi =
1633 RI->second->getRHS()->EvaluateKnownConstInt(Context);
1634 AdjustAPSInt(Hi, CondWidth, CondIsSigned);
1635
1636 CaseExpr = RI->second->getRHS();
1637 if (ShouldDiagnoseSwitchCaseNotInEnum(*this, ED, CaseExpr, EI, EIEnd,
1638 Hi))
1639 Diag(CaseExpr->getExprLoc(), diag::warn_not_in_enum)
1640 << CondTypeBeforePromotion;
1641 }
1642
1643 // Check which enum vals aren't in switch
1644 auto CI = CaseVals.begin();
1645 auto RI = CaseRanges.begin();
1646 bool hasCasesNotInSwitch = false;
1647
1648 SmallVector<DeclarationName,8> UnhandledNames;
1649
1650 for (EI = EnumVals.begin(); EI != EIEnd; EI++) {
1651 // Don't warn about omitted unavailable EnumConstantDecls.
1652 switch (EI->second->getAvailability()) {
1653 case AR_Deprecated:
1654 // Deprecated enumerators need to be handled: they may be deprecated,
1655 // but can still occur.
1656 break;
1657
1658 case AR_Unavailable:
1659 // Omitting an unavailable enumerator is ok; it should never occur.
1660 continue;
1661
1662 case AR_NotYetIntroduced:
1663 // Partially available enum constants should be present. Note that we
1664 // suppress -Wunguarded-availability diagnostics for such uses.
1665 case AR_Available:
1666 break;
1667 }
1668
1669 if (EI->second->hasAttr<UnusedAttr>())
1670 continue;
1671
1672 // Drop unneeded case values
1673 while (CI != CaseVals.end() && CI->first < EI->first)
1674 CI++;
1675
1676 if (CI != CaseVals.end() && CI->first == EI->first)
1677 continue;
1678
1679 // Drop unneeded case ranges
1680 for (; RI != CaseRanges.end(); RI++) {
1681 llvm::APSInt Hi =
1682 RI->second->getRHS()->EvaluateKnownConstInt(Context);
1683 AdjustAPSInt(Hi, CondWidth, CondIsSigned);
1684 if (EI->first <= Hi)
1685 break;
1686 }
1687
1688 if (RI == CaseRanges.end() || EI->first < RI->first) {
1689 hasCasesNotInSwitch = true;
1690 UnhandledNames.push_back(EI->second->getDeclName());
1691 }
1692 }
1693
1694 if (TheDefaultStmt && UnhandledNames.empty() && ED->isClosedNonFlag())
1695 Diag(TheDefaultStmt->getDefaultLoc(), diag::warn_unreachable_default);
1696
1697 // Produce a nice diagnostic if multiple values aren't handled.
1698 if (!UnhandledNames.empty()) {
1699 auto DB = Diag(CondExpr->getExprLoc(), TheDefaultStmt
1700 ? diag::warn_def_missing_case
1701 : diag::warn_missing_case)
1702 << CondExpr->getSourceRange() << (int)UnhandledNames.size();
1703
1704 for (size_t I = 0, E = std::min(UnhandledNames.size(), (size_t)3);
1705 I != E; ++I)
1706 DB << UnhandledNames[I];
1707 }
1708
1709 if (!hasCasesNotInSwitch)
1710 SS->setAllEnumCasesCovered();
1711 }
1712 enum_out:;
1713 }
1714
1715 if (BodyStmt)
1716 DiagnoseEmptyStmtBody(CondExpr->getEndLoc(), BodyStmt,
1717 diag::warn_empty_switch_body);
1718
1719 // FIXME: If the case list was broken is some way, we don't have a good system
1720 // to patch it up. Instead, just return the whole substmt as broken.
1721 if (CaseListIsErroneous)
1722 return StmtError();
1723
1724 return SS;
1725 }
1726
1727 void
1728 Sema::DiagnoseAssignmentEnum(QualType DstType, QualType SrcType,
1729 Expr *SrcExpr) {
1730
1731 if (!DstType->isEnumeralType())
1732 return;
1733
1734 if (!SrcType->isIntegerType() ||
1735 Context.hasSameUnqualifiedType(SrcType, DstType))
1736 return;
1737
1738 if (SrcExpr->isTypeDependent() || SrcExpr->isValueDependent())
1739 return;
1740
1741 const auto *ED = DstType->castAsEnumDecl();
1742 if (!ED->isClosed())
1743 return;
1744
1745 if (Diags.isIgnored(diag::warn_not_in_enum_assignment, SrcExpr->getExprLoc()))
1746 return;
1747
1748 std::optional<llvm::APSInt> RHSVal = SrcExpr->getIntegerConstantExpr(Context);
1749 if (!RHSVal)
1750 return;
1751
1752 // Get the bitwidth of the enum value before promotions.
1753 unsigned DstWidth = Context.getIntWidth(DstType);
1754 bool DstIsSigned = DstType->isSignedIntegerOrEnumerationType();
1755 AdjustAPSInt(*RHSVal, DstWidth, DstIsSigned);
1756
1757 if (ED->hasAttr<FlagEnumAttr>()) {
1758 if (!IsValueInFlagEnum(ED, *RHSVal, /*AllowMask=*/true))
1759 Diag(SrcExpr->getExprLoc(), diag::warn_not_in_enum_assignment)
1760 << DstType.getUnqualifiedType();
1761 return;
1762 }
1763
1764 typedef SmallVector<std::pair<llvm::APSInt, EnumConstantDecl *>, 64>
1765 EnumValsTy;
1766 EnumValsTy EnumVals;
1767
1768 // Gather all enum values, set their type and sort them,
1769 // allowing easier comparison with rhs constant.
1770 for (auto *EDI : ED->enumerators()) {
1771 llvm::APSInt Val = EDI->getInitVal();
1772 AdjustAPSInt(Val, DstWidth, DstIsSigned);
1773 EnumVals.emplace_back(Val, EDI);
1774 }
1775 if (EnumVals.empty())
1776 return;
1777 llvm::stable_sort(EnumVals, CmpEnumVals);
1778 EnumValsTy::iterator EIend = llvm::unique(EnumVals, EqEnumVals);
1779
1780 // See which values aren't in the enum.
1781 EnumValsTy::const_iterator EI = EnumVals.begin();
1782 while (EI != EIend && EI->first < *RHSVal)
1783 EI++;
1784 if (EI == EIend || EI->first != *RHSVal) {
1785 Diag(SrcExpr->getExprLoc(), diag::warn_not_in_enum_assignment)
1786 << DstType.getUnqualifiedType();
1787 }
1788 }
1789
1790 StmtResult Sema::ActOnWhileStmt(SourceLocation WhileLoc,
1791 SourceLocation LParenLoc, ConditionResult Cond,
1792 SourceLocation RParenLoc, Stmt *Body) {
1793 if (Cond.isInvalid())
1794 return StmtError();
1795
1796 auto CondVal = Cond.get();
1797 CheckBreakContinueBinding(CondVal.second);
1798
1799 if (CondVal.second &&
1800 !Diags.isIgnored(diag::warn_comma_operator, CondVal.second->getExprLoc()))
1801 CommaVisitor(*this).Visit(CondVal.second);
1802
1803 // OpenACC3.3 2.14.4:
1804 // The update directive is executable. It must not appear in place of the
1805 // statement following an 'if', 'while', 'do', 'switch', or 'label' in C or
1806 // C++.
1807 if (isa<OpenACCUpdateConstruct>(Body)) {
1808 Diag(Body->getBeginLoc(), diag::err_acc_update_as_body) << /*while*/ 1;
1809 Body = new (Context) NullStmt(Body->getBeginLoc());
1810 }
1811
1812 if (isa<NullStmt>(Body))
1813 getCurCompoundScope().setHasEmptyLoopBodies();
1814
1815 return WhileStmt::Create(Context, CondVal.first, CondVal.second, Body,
1816 WhileLoc, LParenLoc, RParenLoc);
1817 }
1818
1819 StmtResult
1820 Sema::ActOnDoStmt(SourceLocation DoLoc, Stmt *Body,
1821 SourceLocation WhileLoc, SourceLocation CondLParen,
1822 Expr *Cond, SourceLocation CondRParen) {
1823 assert(Cond && "ActOnDoStmt(): missing expression");
1824
1825 CheckBreakContinueBinding(Cond);
1826 ExprResult CondResult = CheckBooleanCondition(DoLoc, Cond);
1827 if (CondResult.isInvalid())
1828 return StmtError();
1829 Cond = CondResult.get();
1830
1831 CondResult = ActOnFinishFullExpr(Cond, DoLoc, /*DiscardedValue*/ false);
1832 if (CondResult.isInvalid())
1833 return StmtError();
1834 Cond = CondResult.get();
1835
1836 // Only call the CommaVisitor for C89 due to differences in scope flags.
1837 if (Cond && !getLangOpts().C99 && !getLangOpts().CPlusPlus &&
1838 !Diags.isIgnored(diag::warn_comma_operator, Cond->getExprLoc()))
1839 CommaVisitor(*this).Visit(Cond);
1840
1841 // OpenACC3.3 2.14.4:
1842 // The update directive is executable. It must not appear in place of the
1843 // statement following an 'if', 'while', 'do', 'switch', or 'label' in C or
1844 // C++.
1845 if (isa<OpenACCUpdateConstruct>(Body)) {
1846 Diag(Body->getBeginLoc(), diag::err_acc_update_as_body) << /*do*/ 2;
1847 Body = new (Context) NullStmt(Body->getBeginLoc());
1848 }
1849
1850 return new (Context) DoStmt(Body, Cond, DoLoc, WhileLoc, CondRParen);
1851 }
1852
1853 namespace {
1854 // Use SetVector since the diagnostic cares about the ordering of the Decl's.
1855 using DeclSetVector = llvm::SmallSetVector<VarDecl *, 8>;
1856
1857 // This visitor will traverse a conditional statement and store all
1858 // the evaluated decls into a vector. Simple is set to true if none
1859 // of the excluded constructs are used.
1860 class DeclExtractor : public EvaluatedExprVisitor<DeclExtractor> {
1861 DeclSetVector &Decls;
1862 SmallVectorImpl<SourceRange> &Ranges;
1863 bool Simple;
1864 public:
1865 typedef EvaluatedExprVisitor<DeclExtractor> Inherited;
1866
1867 DeclExtractor(Sema &S, DeclSetVector &Decls,
1868 SmallVectorImpl<SourceRange> &Ranges) :
1869 Inherited(S.Context),
1870 Decls(Decls),
1871 Ranges(Ranges),
1872 Simple(true) {}
1873
1874 bool isSimple() { return Simple; }
1875
1876 // Replaces the method in EvaluatedExprVisitor.
1877 void VisitMemberExpr(MemberExpr* E) {
1878 Simple = false;
1879 }
1880
1881 // Any Stmt not explicitly listed will cause the condition to be marked
1882 // complex.
1883 void VisitStmt(Stmt *S) { Simple = false; }
1884
1885 void VisitBinaryOperator(BinaryOperator *E) {
1886 Visit(E->getLHS());
1887 Visit(E->getRHS());
1888 }
1889
1890 void VisitCastExpr(CastExpr *E) {
1891 Visit(E->getSubExpr());
1892 }
1893
1894 void VisitUnaryOperator(UnaryOperator *E) {
1895 // Skip checking conditionals with derefernces.
1896 if (E->getOpcode() == UO_Deref)
1897 Simple = false;
1898 else
1899 Visit(E->getSubExpr());
1900 }
1901
1902 void VisitConditionalOperator(ConditionalOperator *E) {
1903 Visit(E->getCond());
1904 Visit(E->getTrueExpr());
1905 Visit(E->getFalseExpr());
1906 }
1907
1908 void VisitParenExpr(ParenExpr *E) {
1909 Visit(E->getSubExpr());
1910 }
1911
1912 void VisitBinaryConditionalOperator(BinaryConditionalOperator *E) {
1913 Visit(E->getOpaqueValue()->getSourceExpr());
1914 Visit(E->getFalseExpr());
1915 }
1916
1917 void VisitIntegerLiteral(IntegerLiteral *E) { }
1918 void VisitFloatingLiteral(FloatingLiteral *E) { }
1919 void VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) { }
1920 void VisitCharacterLiteral(CharacterLiteral *E) { }
1921 void VisitGNUNullExpr(GNUNullExpr *E) { }
1922 void VisitImaginaryLiteral(ImaginaryLiteral *E) { }
1923
1924 void VisitDeclRefExpr(DeclRefExpr *E) {
1925 VarDecl *VD = dyn_cast<VarDecl>(E->getDecl());
1926 if (!VD) {
1927 // Don't allow unhandled Decl types.
1928 Simple = false;
1929 return;
1930 }
1931
1932 Ranges.push_back(E->getSourceRange());
1933
1934 Decls.insert(VD);
1935 }
1936
1937 }; // end class DeclExtractor
1938
1939 // DeclMatcher checks to see if the decls are used in a non-evaluated
1940 // context.
1941 class DeclMatcher : public EvaluatedExprVisitor<DeclMatcher> {
1942 DeclSetVector &Decls;
1943 bool FoundDecl;
1944
1945 public:
1946 typedef EvaluatedExprVisitor<DeclMatcher> Inherited;
1947
1948 DeclMatcher(Sema &S, DeclSetVector &Decls, Stmt *Statement) :
1949 Inherited(S.Context), Decls(Decls), FoundDecl(false) {
1950 if (!Statement) return;
1951
1952 Visit(Statement);
1953 }
1954
1955 void VisitReturnStmt(ReturnStmt *S) {
1956 FoundDecl = true;
1957 }
1958
1959 void VisitBreakStmt(BreakStmt *S) {
1960 FoundDecl = true;
1961 }
1962
1963 void VisitGotoStmt(GotoStmt *S) {
1964 FoundDecl = true;
1965 }
1966
1967 void VisitCastExpr(CastExpr *E) {
1968 if (E->getCastKind() == CK_LValueToRValue)
1969 CheckLValueToRValueCast(E->getSubExpr());
1970 else
1971 Visit(E->getSubExpr());
1972 }
1973
1974 void CheckLValueToRValueCast(Expr *E) {
1975 E = E->IgnoreParenImpCasts();
1976
1977 if (isa<DeclRefExpr>(E)) {
1978 return;
1979 }
1980
1981 if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) {
1982 Visit(CO->getCond());
1983 CheckLValueToRValueCast(CO->getTrueExpr());
1984 CheckLValueToRValueCast(CO->getFalseExpr());
1985 return;
1986 }
1987
1988 if (BinaryConditionalOperator *BCO =
1989 dyn_cast<BinaryConditionalOperator>(E)) {
1990 CheckLValueToRValueCast(BCO->getOpaqueValue()->getSourceExpr());
1991 CheckLValueToRValueCast(BCO->getFalseExpr());
1992 return;
1993 }
1994
1995 Visit(E);
1996 }
1997
1998 void VisitDeclRefExpr(DeclRefExpr *E) {
1999 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl()))
2000 if (Decls.count(VD))
2001 FoundDecl = true;
2002 }
2003
2004 void VisitPseudoObjectExpr(PseudoObjectExpr *POE) {
2005 // Only need to visit the semantics for POE.
2006 // SyntaticForm doesn't really use the Decal.
2007 for (auto *S : POE->semantics()) {
2008 if (auto *OVE = dyn_cast<OpaqueValueExpr>(S))
2009 // Look past the OVE into the expression it binds.
2010 Visit(OVE->getSourceExpr());
2011 else
2012 Visit(S);
2013 }
2014 }
2015
2016 bool FoundDeclInUse() { return FoundDecl; }
2017
2018 }; // end class DeclMatcher
2019
2020 void CheckForLoopConditionalStatement(Sema &S, Expr *Second,
2021 Expr *Third, Stmt *Body) {
2022 // Condition is empty
2023 if (!Second) return;
2024
2025 if (S.Diags.isIgnored(diag::warn_variables_not_in_loop_body,
2026 Second->getBeginLoc()))
2027 return;
2028
2029 PartialDiagnostic PDiag = S.PDiag(diag::warn_variables_not_in_loop_body);
2030 DeclSetVector Decls;
2031 SmallVector<SourceRange, 10> Ranges;
2032 DeclExtractor DE(S, Decls, Ranges);
2033 DE.Visit(Second);
2034
2035 // Don't analyze complex conditionals.
2036 if (!DE.isSimple()) return;
2037
2038 // No decls found.
2039 if (Decls.size() == 0) return;
2040
2041 // Don't warn on volatile, static, or global variables.
2042 for (auto *VD : Decls)
2043 if (VD->getType().isVolatileQualified() || VD->hasGlobalStorage())
2044 return;
2045
2046 if (DeclMatcher(S, Decls, Second).FoundDeclInUse() ||
2047 DeclMatcher(S, Decls, Third).FoundDeclInUse() ||
2048 DeclMatcher(S, Decls, Body).FoundDeclInUse())
2049 return;
2050
2051 // Load decl names into diagnostic.
2052 if (Decls.size() > 4) {
2053 PDiag << 0;
2054 } else {
2055 PDiag << (unsigned)Decls.size();
2056 for (auto *VD : Decls)
2057 PDiag << VD->getDeclName();
2058 }
2059
2060 for (auto Range : Ranges)
2061 PDiag << Range;
2062
2063 S.Diag(Ranges.begin()->getBegin(), PDiag);
2064 }
2065
2066 // If Statement is an incemement or decrement, return true and sets the
2067 // variables Increment and DRE.
2068 bool ProcessIterationStmt(Sema &S, Stmt* Statement, bool &Increment,
2069 DeclRefExpr *&DRE) {
2070 if (auto Cleanups = dyn_cast<ExprWithCleanups>(Statement))
2071 if (!Cleanups->cleanupsHaveSideEffects())
2072 Statement = Cleanups->getSubExpr();
2073
2074 if (UnaryOperator *UO = dyn_cast<UnaryOperator>(Statement)) {
2075 switch (UO->getOpcode()) {
2076 default: return false;
2077 case UO_PostInc:
2078 case UO_PreInc:
2079 Increment = true;
2080 break;
2081 case UO_PostDec:
2082 case UO_PreDec:
2083 Increment = false;
2084 break;
2085 }
2086 DRE = dyn_cast<DeclRefExpr>(UO->getSubExpr());
2087 return DRE;
2088 }
2089
2090 if (CXXOperatorCallExpr *Call = dyn_cast<CXXOperatorCallExpr>(Statement)) {
2091 FunctionDecl *FD = Call->getDirectCallee();
2092 if (!FD || !FD->isOverloadedOperator()) return false;
2093 switch (FD->getOverloadedOperator()) {
2094 default: return false;
2095 case OO_PlusPlus:
2096 Increment = true;
2097 break;
2098 case OO_MinusMinus:
2099 Increment = false;
2100 break;
2101 }
2102 DRE = dyn_cast<DeclRefExpr>(Call->getArg(0));
2103 return DRE;
2104 }
2105
2106 return false;
2107 }
2108
2109 // A visitor to determine if a continue or break statement is a
2110 // subexpression.
2111 class BreakContinueFinder : public ConstEvaluatedExprVisitor<BreakContinueFinder> {
2112 SourceLocation BreakLoc;
2113 SourceLocation ContinueLoc;
2114 bool InSwitch = false;
2115
2116 public:
2117 BreakContinueFinder(Sema &S, const Stmt* Body) :
2118 Inherited(S.Context) {
2119 Visit(Body);
2120 }
2121
2122 typedef ConstEvaluatedExprVisitor<BreakContinueFinder> Inherited;
2123
2124 void VisitContinueStmt(const ContinueStmt* E) {
2125 ContinueLoc = E->getKwLoc();
2126 }
2127
2128 void VisitBreakStmt(const BreakStmt* E) {
2129 if (!InSwitch)
2130 BreakLoc = E->getKwLoc();
2131 }
2132
2133 void VisitSwitchStmt(const SwitchStmt* S) {
2134 if (const Stmt *Init = S->getInit())
2135 Visit(Init);
2136 if (const Stmt *CondVar = S->getConditionVariableDeclStmt())
2137 Visit(CondVar);
2138 if (const Stmt *Cond = S->getCond())
2139 Visit(Cond);
2140
2141 // Don't return break statements from the body of a switch.
2142 InSwitch = true;
2143 if (const Stmt *Body = S->getBody())
2144 Visit(Body);
2145 InSwitch = false;
2146 }
2147
2148 void VisitForStmt(const ForStmt *S) {
2149 // Only visit the init statement of a for loop; the body
2150 // has a different break/continue scope.
2151 if (const Stmt *Init = S->getInit())
2152 Visit(Init);
2153 }
2154
2155 void VisitWhileStmt(const WhileStmt *) {
2156 // Do nothing; the children of a while loop have a different
2157 // break/continue scope.
2158 }
2159
2160 void VisitDoStmt(const DoStmt *) {
2161 // Do nothing; the children of a while loop have a different
2162 // break/continue scope.
2163 }
2164
2165 void VisitCXXForRangeStmt(const CXXForRangeStmt *S) {
2166 // Only visit the initialization of a for loop; the body
2167 // has a different break/continue scope.
2168 if (const Stmt *Init = S->getInit())
2169 Visit(Init);
2170 if (const Stmt *Range = S->getRangeStmt())
2171 Visit(Range);
2172 if (const Stmt *Begin = S->getBeginStmt())
2173 Visit(Begin);
2174 if (const Stmt *End = S->getEndStmt())
2175 Visit(End);
2176 }
2177
2178 void VisitObjCForCollectionStmt(const ObjCForCollectionStmt *S) {
2179 // Only visit the initialization of a for loop; the body
2180 // has a different break/continue scope.
2181 if (const Stmt *Element = S->getElement())
2182 Visit(Element);
2183 if (const Stmt *Collection = S->getCollection())
2184 Visit(Collection);
2185 }
2186
2187 bool ContinueFound() { return ContinueLoc.isValid(); }
2188 bool BreakFound() { return BreakLoc.isValid(); }
2189 SourceLocation GetContinueLoc() { return ContinueLoc; }
2190 SourceLocation GetBreakLoc() { return BreakLoc; }
2191
2192 }; // end class BreakContinueFinder
2193
2194 // Emit a warning when a loop increment/decrement appears twice per loop
2195 // iteration. The conditions which trigger this warning are:
2196 // 1) The last statement in the loop body and the third expression in the
2197 // for loop are both increment or both decrement of the same variable
2198 // 2) No continue statements in the loop body.
2199 void CheckForRedundantIteration(Sema &S, Expr *Third, Stmt *Body) {
2200 // Return when there is nothing to check.
2201 if (!Body || !Third) return;
2202
2203 // Get the last statement from the loop body.
2204 CompoundStmt *CS = dyn_cast<CompoundStmt>(Body);
2205 if (!CS || CS->body_empty()) return;
2206 Stmt *LastStmt = CS->body_back();
2207 if (!LastStmt) return;
2208
2209 if (S.Diags.isIgnored(diag::warn_redundant_loop_iteration,
2210 Third->getBeginLoc()))
2211 return;
2212
2213 bool LoopIncrement, LastIncrement;
2214 DeclRefExpr *LoopDRE, *LastDRE;
2215
2216 if (!ProcessIterationStmt(S, Third, LoopIncrement, LoopDRE)) return;
2217 if (!ProcessIterationStmt(S, LastStmt, LastIncrement, LastDRE)) return;
2218
2219 // Check that the two statements are both increments or both decrements
2220 // on the same variable.
2221 if (LoopIncrement != LastIncrement ||
2222 LoopDRE->getDecl() != LastDRE->getDecl()) return;
2223
2224 if (BreakContinueFinder(S, Body).ContinueFound()) return;
2225
2226 S.Diag(LastDRE->getLocation(), diag::warn_redundant_loop_iteration)
2227 << LastDRE->getDecl() << LastIncrement;
2228 S.Diag(LoopDRE->getLocation(), diag::note_loop_iteration_here)
2229 << LoopIncrement;
2230 }
2231
2232 } // end namespace
2233
2234
2235 void Sema::CheckBreakContinueBinding(Expr *E) {
2236 if (!E || getLangOpts().CPlusPlus)
2237 return;
2238 BreakContinueFinder BCFinder(*this, E);
2239 Scope *BreakParent = CurScope->getBreakParent();
2240 if (BCFinder.BreakFound() && BreakParent) {
2241 if (BreakParent->getFlags() & Scope::SwitchScope) {
2242 Diag(BCFinder.GetBreakLoc(), diag::warn_break_binds_to_switch);
2243 } else {
2244 Diag(BCFinder.GetBreakLoc(), diag::warn_loop_ctrl_binds_to_inner)
2245 << "break";
2246 }
2247 } else if (BCFinder.ContinueFound() && CurScope->getContinueParent()) {
2248 Diag(BCFinder.GetContinueLoc(), diag::warn_loop_ctrl_binds_to_inner)
2249 << "continue";
2250 }
2251 }
2252
2253 StmtResult Sema::ActOnForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
2254 Stmt *First, ConditionResult Second,
2255 FullExprArg third, SourceLocation RParenLoc,
2256 Stmt *Body) {
2257 if (Second.isInvalid())
2258 return StmtError();
2259
2260 if (!getLangOpts().CPlusPlus) {
2261 if (DeclStmt *DS = dyn_cast_or_null<DeclStmt>(First)) {
2262 // C99 6.8.5p3: The declaration part of a 'for' statement shall only
2263 // declare identifiers for objects having storage class 'auto' or
2264 // 'register'.
2265 const Decl *NonVarSeen = nullptr;
2266 bool VarDeclSeen = false;
2267 for (auto *DI : DS->decls()) {
2268 if (VarDecl *VD = dyn_cast<VarDecl>(DI)) {
2269 VarDeclSeen = true;
2270 if (VD->isLocalVarDecl() && !VD->hasLocalStorage())
2271 Diag(DI->getLocation(),
2272 getLangOpts().C23
2273 ? diag::warn_c17_non_local_variable_decl_in_for
2274 : diag::ext_c23_non_local_variable_decl_in_for);
2275 } else if (!NonVarSeen) {
2276 // Keep track of the first non-variable declaration we saw so that
2277 // we can diagnose if we don't see any variable declarations. This
2278 // covers a case like declaring a typedef, function, or structure
2279 // type rather than a variable.
2280 //
2281 // Note, _Static_assert is acceptable because it does not declare an
2282 // identifier at all, so "for object having" does not apply.
2283 if (!isa<StaticAssertDecl>(DI))
2284 NonVarSeen = DI;
2285 }
2286 }
2287 // Diagnose if we saw a non-variable declaration but no variable
2288 // declarations.
2289 if (NonVarSeen && !VarDeclSeen)
2290 Diag(NonVarSeen->getLocation(),
2291 getLangOpts().C23 ? diag::warn_c17_non_variable_decl_in_for
2292 : diag::ext_c23_non_variable_decl_in_for);
2293 }
2294 }
2295
2296 CheckBreakContinueBinding(Second.get().second);
2297 CheckBreakContinueBinding(third.get());
2298
2299 if (!Second.get().first)
2300 CheckForLoopConditionalStatement(*this, Second.get().second, third.get(),
2301 Body);
2302 CheckForRedundantIteration(*this, third.get(), Body);
2303
2304 if (Second.get().second &&
2305 !Diags.isIgnored(diag::warn_comma_operator,
2306 Second.get().second->getExprLoc()))
2307 CommaVisitor(*this).Visit(Second.get().second);
2308
2309 Expr *Third = third.release().getAs<Expr>();
2310 if (isa<NullStmt>(Body))
2311 getCurCompoundScope().setHasEmptyLoopBodies();
2312
2313 return new (Context)
2314 ForStmt(Context, First, Second.get().second, Second.get().first, Third,
2315 Body, ForLoc, LParenLoc, RParenLoc);
2316 }
2317
2318 StmtResult Sema::ActOnForEachLValueExpr(Expr *E) {
2319 // Reduce placeholder expressions here. Note that this rejects the
2320 // use of pseudo-object l-values in this position.
2321 ExprResult result = CheckPlaceholderExpr(E);
2322 if (result.isInvalid()) return StmtError();
2323 E = result.get();
2324
2325 ExprResult FullExpr = ActOnFinishFullExpr(E, /*DiscardedValue*/ false);
2326 if (FullExpr.isInvalid())
2327 return StmtError();
2328 return StmtResult(static_cast<Stmt*>(FullExpr.get()));
2329 }
2330
2331 /// Finish building a variable declaration for a for-range statement.
2332 /// \return true if an error occurs.
2333 static bool FinishForRangeVarDecl(Sema &SemaRef, VarDecl *Decl, Expr *Init,
2334 SourceLocation Loc, int DiagID) {
2335 if (Decl->getType()->isUndeducedType()) {
2336 ExprResult Res = Init;
2337 if (!Res.isUsable()) {
2338 Decl->setInvalidDecl();
2339 return true;
2340 }
2341 Init = Res.get();
2342 }
2343
2344 // Deduce the type for the iterator variable now rather than leaving it to
2345 // AddInitializerToDecl, so we can produce a more suitable diagnostic.
2346 QualType InitType;
2347 if (!isa<InitListExpr>(Init) && Init->getType()->isVoidType()) {
2348 SemaRef.Diag(Loc, DiagID) << Init->getType();
2349 } else {
2350 TemplateDeductionInfo Info(Init->getExprLoc());
2351 TemplateDeductionResult Result = SemaRef.DeduceAutoType(
2352 Decl->getTypeSourceInfo()->getTypeLoc(), Init, InitType, Info);
2353 if (Result != TemplateDeductionResult::Success &&
2354 Result != TemplateDeductionResult::AlreadyDiagnosed)
2355 SemaRef.Diag(Loc, DiagID) << Init->getType();
2356 }
2357
2358 if (InitType.isNull()) {
2359 Decl->setInvalidDecl();
2360 return true;
2361 }
2362 Decl->setType(InitType);
2363
2364 // In ARC, infer lifetime.
2365 // FIXME: ARC may want to turn this into 'const __unsafe_unretained' if
2366 // we're doing the equivalent of fast iteration.
2367 if (SemaRef.getLangOpts().ObjCAutoRefCount &&
2368 SemaRef.ObjC().inferObjCARCLifetime(Decl))
2369 Decl->setInvalidDecl();
2370
2371 SemaRef.AddInitializerToDecl(Decl, Init, /*DirectInit=*/false);
2372 SemaRef.FinalizeDeclaration(Decl);
2373 SemaRef.CurContext->addHiddenDecl(Decl);
2374 return false;
2375 }
2376
2377 namespace {
2378 // An enum to represent whether something is dealing with a call to begin()
2379 // or a call to end() in a range-based for loop.
2380 enum BeginEndFunction {
2381 BEF_begin,
2382 BEF_end
2383 };
2384
2385 /// Produce a note indicating which begin/end function was implicitly called
2386 /// by a C++11 for-range statement. This is often not obvious from the code,
2387 /// nor from the diagnostics produced when analysing the implicit expressions
2388 /// required in a for-range statement.
2389 void NoteForRangeBeginEndFunction(Sema &SemaRef, Expr *E,
2390 BeginEndFunction BEF) {
2391 CallExpr *CE = dyn_cast<CallExpr>(E);
2392 if (!CE)
2393 return;
2394 FunctionDecl *D = dyn_cast<FunctionDecl>(CE->getCalleeDecl());
2395 if (!D)
2396 return;
2397 SourceLocation Loc = D->getLocation();
2398
2399 std::string Description;
2400 bool IsTemplate = false;
2401 if (FunctionTemplateDecl *FunTmpl = D->getPrimaryTemplate()) {
2402 Description = SemaRef.getTemplateArgumentBindingsText(
2403 FunTmpl->getTemplateParameters(), *D->getTemplateSpecializationArgs());
2404 IsTemplate = true;
2405 }
2406
2407 SemaRef.Diag(Loc, diag::note_for_range_begin_end)
2408 << BEF << IsTemplate << Description << E->getType();
2409 }
2410
2411 /// Build a variable declaration for a for-range statement.
2412 VarDecl *BuildForRangeVarDecl(Sema &SemaRef, SourceLocation Loc,
2413 QualType Type, StringRef Name) {
2414 DeclContext *DC = SemaRef.CurContext;
2415 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
2416 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
2417 VarDecl *Decl = VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type,
2418 TInfo, SC_None);
2419 Decl->setImplicit();
2420 Decl->setCXXForRangeImplicitVar(true);
2421 return Decl;
2422 }
2423
2424 }
2425
2426 static bool ObjCEnumerationCollection(Expr *Collection) {
2427 return !Collection->isTypeDependent()
2428 && Collection->getType()->getAs<ObjCObjectPointerType>() != nullptr;
2429 }
2430
2431 StmtResult Sema::ActOnCXXForRangeStmt(
2432 Scope *S, SourceLocation ForLoc, SourceLocation CoawaitLoc, Stmt *InitStmt,
2433 Stmt *First, SourceLocation ColonLoc, Expr *Range, SourceLocation RParenLoc,
2434 BuildForRangeKind Kind,
2435 ArrayRef<MaterializeTemporaryExpr *> LifetimeExtendTemps) {
2436 // FIXME: recover in order to allow the body to be parsed.
2437 if (!First)
2438 return StmtError();
2439
2440 if (Range && ObjCEnumerationCollection(Range)) {
2441 // FIXME: Support init-statements in Objective-C++20 ranged for statement.
2442 if (InitStmt)
2443 return Diag(InitStmt->getBeginLoc(), diag::err_objc_for_range_init_stmt)
2444 << InitStmt->getSourceRange();
2445 return ObjC().ActOnObjCForCollectionStmt(ForLoc, First, Range, RParenLoc);
2446 }
2447
2448 DeclStmt *DS = dyn_cast<DeclStmt>(First);
2449 assert(DS && "first part of for range not a decl stmt");
2450
2451 if (!DS->isSingleDecl()) {
2452 Diag(DS->getBeginLoc(), diag::err_type_defined_in_for_range);
2453 return StmtError();
2454 }
2455
2456 // This function is responsible for attaching an initializer to LoopVar. We
2457 // must call ActOnInitializerError if we fail to do so.
2458 Decl *LoopVar = DS->getSingleDecl();
2459 if (LoopVar->isInvalidDecl() || !Range ||
2460 DiagnoseUnexpandedParameterPack(Range, UPPC_Expression)) {
2461 ActOnInitializerError(LoopVar);
2462 return StmtError();
2463 }
2464
2465 // Build the coroutine state immediately and not later during template
2466 // instantiation
2467 if (!CoawaitLoc.isInvalid()) {
2468 if (!ActOnCoroutineBodyStart(S, CoawaitLoc, "co_await")) {
2469 ActOnInitializerError(LoopVar);
2470 return StmtError();
2471 }
2472 }
2473
2474 // Build auto && __range = range-init
2475 // Divide by 2, since the variables are in the inner scope (loop body).
2476 const auto DepthStr = std::to_string(S->getDepth() / 2);
2477 SourceLocation RangeLoc = Range->getBeginLoc();
2478 VarDecl *RangeVar = BuildForRangeVarDecl(*this, RangeLoc,
2479 Context.getAutoRRefDeductType(),
2480 std::string("__range") + DepthStr);
2481 if (FinishForRangeVarDecl(*this, RangeVar, Range, RangeLoc,
2482 diag::err_for_range_deduction_failure)) {
2483 ActOnInitializerError(LoopVar);
2484 return StmtError();
2485 }
2486
2487 // Claim the type doesn't contain auto: we've already done the checking.
2488 DeclGroupPtrTy RangeGroup =
2489 BuildDeclaratorGroup(MutableArrayRef<Decl *>((Decl **)&RangeVar, 1));
2490 StmtResult RangeDecl = ActOnDeclStmt(RangeGroup, RangeLoc, RangeLoc);
2491 if (RangeDecl.isInvalid()) {
2492 ActOnInitializerError(LoopVar);
2493 return StmtError();
2494 }
2495
2496 StmtResult R = BuildCXXForRangeStmt(
2497 ForLoc, CoawaitLoc, InitStmt, ColonLoc, RangeDecl.get(),
2498 /*BeginStmt=*/nullptr, /*EndStmt=*/nullptr,
2499 /*Cond=*/nullptr, /*Inc=*/nullptr, DS, RParenLoc, Kind,
2500 LifetimeExtendTemps);
2501 if (R.isInvalid()) {
2502 ActOnInitializerError(LoopVar);
2503 return StmtError();
2504 }
2505
2506 return R;
2507 }
2508
2509 /// Create the initialization, compare, and increment steps for
2510 /// the range-based for loop expression.
2511 /// This function does not handle array-based for loops,
2512 /// which are created in Sema::BuildCXXForRangeStmt.
2513 ///
2514 /// \returns a ForRangeStatus indicating success or what kind of error occurred.
2515 /// BeginExpr and EndExpr are set and FRS_Success is returned on success;
2516 /// CandidateSet and BEF are set and some non-success value is returned on
2517 /// failure.
2518 static Sema::ForRangeStatus
2519 BuildNonArrayForRange(Sema &SemaRef, Expr *BeginRange, Expr *EndRange,
2520 QualType RangeType, VarDecl *BeginVar, VarDecl *EndVar,
2521 SourceLocation ColonLoc, SourceLocation CoawaitLoc,
2522 OverloadCandidateSet *CandidateSet, ExprResult *BeginExpr,
2523 ExprResult *EndExpr, BeginEndFunction *BEF) {
2524 DeclarationNameInfo BeginNameInfo(
2525 &SemaRef.PP.getIdentifierTable().get("begin"), ColonLoc);
2526 DeclarationNameInfo EndNameInfo(&SemaRef.PP.getIdentifierTable().get("end"),
2527 ColonLoc);
2528
2529 LookupResult BeginMemberLookup(SemaRef, BeginNameInfo,
2530 Sema::LookupMemberName);
2531 LookupResult EndMemberLookup(SemaRef, EndNameInfo, Sema::LookupMemberName);
2532
2533 auto BuildBegin = [&] {
2534 *BEF = BEF_begin;
2535 Sema::ForRangeStatus RangeStatus =
2536 SemaRef.BuildForRangeBeginEndCall(ColonLoc, ColonLoc, BeginNameInfo,
2537 BeginMemberLookup, CandidateSet,
2538 BeginRange, BeginExpr);
2539
2540 if (RangeStatus != Sema::FRS_Success) {
2541 if (RangeStatus == Sema::FRS_DiagnosticIssued)
2542 SemaRef.Diag(BeginRange->getBeginLoc(), diag::note_in_for_range)
2543 << ColonLoc << BEF_begin << BeginRange->getType();
2544 return RangeStatus;
2545 }
2546 if (!CoawaitLoc.isInvalid()) {
2547 // FIXME: getCurScope() should not be used during template instantiation.
2548 // We should pick up the set of unqualified lookup results for operator
2549 // co_await during the initial parse.
2550 *BeginExpr = SemaRef.ActOnCoawaitExpr(SemaRef.getCurScope(), ColonLoc,
2551 BeginExpr->get());
2552 if (BeginExpr->isInvalid())
2553 return Sema::FRS_DiagnosticIssued;
2554 }
2555 if (FinishForRangeVarDecl(SemaRef, BeginVar, BeginExpr->get(), ColonLoc,
2556 diag::err_for_range_iter_deduction_failure)) {
2557 NoteForRangeBeginEndFunction(SemaRef, BeginExpr->get(), *BEF);
2558 return Sema::FRS_DiagnosticIssued;
2559 }
2560 return Sema::FRS_Success;
2561 };
2562
2563 auto BuildEnd = [&] {
2564 *BEF = BEF_end;
2565 Sema::ForRangeStatus RangeStatus =
2566 SemaRef.BuildForRangeBeginEndCall(ColonLoc, ColonLoc, EndNameInfo,
2567 EndMemberLookup, CandidateSet,
2568 EndRange, EndExpr);
2569 if (RangeStatus != Sema::FRS_Success) {
2570 if (RangeStatus == Sema::FRS_DiagnosticIssued)
2571 SemaRef.Diag(EndRange->getBeginLoc(), diag::note_in_for_range)
2572 << ColonLoc << BEF_end << EndRange->getType();
2573 return RangeStatus;
2574 }
2575 if (FinishForRangeVarDecl(SemaRef, EndVar, EndExpr->get(), ColonLoc,
2576 diag::err_for_range_iter_deduction_failure)) {
2577 NoteForRangeBeginEndFunction(SemaRef, EndExpr->get(), *BEF);
2578 return Sema::FRS_DiagnosticIssued;
2579 }
2580 return Sema::FRS_Success;
2581 };
2582
2583 if (CXXRecordDecl *D = RangeType->getAsCXXRecordDecl()) {
2584 // - if _RangeT is a class type, the unqualified-ids begin and end are
2585 // looked up in the scope of class _RangeT as if by class member access
2586 // lookup (3.4.5), and if either (or both) finds at least one
2587 // declaration, begin-expr and end-expr are __range.begin() and
2588 // __range.end(), respectively;
2589 SemaRef.LookupQualifiedName(BeginMemberLookup, D);
2590 if (BeginMemberLookup.isAmbiguous())
2591 return Sema::FRS_DiagnosticIssued;
2592
2593 SemaRef.LookupQualifiedName(EndMemberLookup, D);
2594 if (EndMemberLookup.isAmbiguous())
2595 return Sema::FRS_DiagnosticIssued;
2596
2597 if (BeginMemberLookup.empty() != EndMemberLookup.empty()) {
2598 // Look up the non-member form of the member we didn't find, first.
2599 // This way we prefer a "no viable 'end'" diagnostic over a "i found
2600 // a 'begin' but ignored it because there was no member 'end'"
2601 // diagnostic.
2602 auto BuildNonmember = [&](
2603 BeginEndFunction BEFFound, LookupResult &Found,
2604 llvm::function_ref<Sema::ForRangeStatus()> BuildFound,
2605 llvm::function_ref<Sema::ForRangeStatus()> BuildNotFound) {
2606 LookupResult OldFound = std::move(Found);
2607 Found.clear();
2608
2609 if (Sema::ForRangeStatus Result = BuildNotFound())
2610 return Result;
2611
2612 switch (BuildFound()) {
2613 case Sema::FRS_Success:
2614 return Sema::FRS_Success;
2615
2616 case Sema::FRS_NoViableFunction:
2617 CandidateSet->NoteCandidates(
2618 PartialDiagnosticAt(BeginRange->getBeginLoc(),
2619 SemaRef.PDiag(diag::err_for_range_invalid)
2620 << BeginRange->getType() << BEFFound),
2621 SemaRef, OCD_AllCandidates, BeginRange);
2622 [[fallthrough]];
2623
2624 case Sema::FRS_DiagnosticIssued:
2625 for (NamedDecl *D : OldFound) {
2626 SemaRef.Diag(D->getLocation(),
2627 diag::note_for_range_member_begin_end_ignored)
2628 << BeginRange->getType() << BEFFound;
2629 }
2630 return Sema::FRS_DiagnosticIssued;
2631 }
2632 llvm_unreachable("unexpected ForRangeStatus");
2633 };
2634 if (BeginMemberLookup.empty())
2635 return BuildNonmember(BEF_end, EndMemberLookup, BuildEnd, BuildBegin);
2636 return BuildNonmember(BEF_begin, BeginMemberLookup, BuildBegin, BuildEnd);
2637 }
2638 } else {
2639 // - otherwise, begin-expr and end-expr are begin(__range) and
2640 // end(__range), respectively, where begin and end are looked up with
2641 // argument-dependent lookup (3.4.2). For the purposes of this name
2642 // lookup, namespace std is an associated namespace.
2643 }
2644
2645 if (Sema::ForRangeStatus Result = BuildBegin())
2646 return Result;
2647 return BuildEnd();
2648 }
2649
2650 /// Speculatively attempt to dereference an invalid range expression.
2651 /// If the attempt fails, this function will return a valid, null StmtResult
2652 /// and emit no diagnostics.
2653 static StmtResult RebuildForRangeWithDereference(Sema &SemaRef, Scope *S,
2654 SourceLocation ForLoc,
2655 SourceLocation CoawaitLoc,
2656 Stmt *InitStmt,
2657 Stmt *LoopVarDecl,
2658 SourceLocation ColonLoc,
2659 Expr *Range,
2660 SourceLocation RangeLoc,
2661 SourceLocation RParenLoc) {
2662 // Determine whether we can rebuild the for-range statement with a
2663 // dereferenced range expression.
2664 ExprResult AdjustedRange;
2665 {
2666 Sema::SFINAETrap Trap(SemaRef);
2667
2668 AdjustedRange = SemaRef.BuildUnaryOp(S, RangeLoc, UO_Deref, Range);
2669 if (AdjustedRange.isInvalid())
2670 return StmtResult();
2671
2672 StmtResult SR = SemaRef.ActOnCXXForRangeStmt(
2673 S, ForLoc, CoawaitLoc, InitStmt, LoopVarDecl, ColonLoc,
2674 AdjustedRange.get(), RParenLoc, Sema::BFRK_Check);
2675 if (SR.isInvalid())
2676 return StmtResult();
2677 }
2678
2679 // The attempt to dereference worked well enough that it could produce a valid
2680 // loop. Produce a fixit, and rebuild the loop with diagnostics enabled, in
2681 // case there are any other (non-fatal) problems with it.
2682 SemaRef.Diag(RangeLoc, diag::err_for_range_dereference)
2683 << Range->getType() << FixItHint::CreateInsertion(RangeLoc, "*");
2684 return SemaRef.ActOnCXXForRangeStmt(
2685 S, ForLoc, CoawaitLoc, InitStmt, LoopVarDecl, ColonLoc,
2686 AdjustedRange.get(), RParenLoc, Sema::BFRK_Rebuild);
2687 }
2688
2689 StmtResult Sema::BuildCXXForRangeStmt(
2690 SourceLocation ForLoc, SourceLocation CoawaitLoc, Stmt *InitStmt,
2691 SourceLocation ColonLoc, Stmt *RangeDecl, Stmt *Begin, Stmt *End,
2692 Expr *Cond, Expr *Inc, Stmt *LoopVarDecl, SourceLocation RParenLoc,
2693 BuildForRangeKind Kind,
2694 ArrayRef<MaterializeTemporaryExpr *> LifetimeExtendTemps) {
2695 // FIXME: This should not be used during template instantiation. We should
2696 // pick up the set of unqualified lookup results for the != and + operators
2697 // in the initial parse.
2698 //
2699 // Testcase (accepts-invalid):
2700 // template<typename T> void f() { for (auto x : T()) {} }
2701 // namespace N { struct X { X begin(); X end(); int operator*(); }; }
2702 // bool operator!=(N::X, N::X); void operator++(N::X);
2703 // void g() { f<N::X>(); }
2704 Scope *S = getCurScope();
2705
2706 DeclStmt *RangeDS = cast<DeclStmt>(RangeDecl);
2707 VarDecl *RangeVar = cast<VarDecl>(RangeDS->getSingleDecl());
2708 QualType RangeVarType = RangeVar->getType();
2709
2710 DeclStmt *LoopVarDS = cast<DeclStmt>(LoopVarDecl);
2711 VarDecl *LoopVar = cast<VarDecl>(LoopVarDS->getSingleDecl());
2712
2713 StmtResult BeginDeclStmt = Begin;
2714 StmtResult EndDeclStmt = End;
2715 ExprResult NotEqExpr = Cond, IncrExpr = Inc;
2716
2717 if (RangeVarType->isDependentType()) {
2718 // The range is implicitly used as a placeholder when it is dependent.
2719 RangeVar->markUsed(Context);
2720
2721 // Deduce any 'auto's in the loop variable as 'DependentTy'. We'll fill
2722 // them in properly when we instantiate the loop.
2723 if (!LoopVar->isInvalidDecl() && Kind != BFRK_Check) {
2724 if (auto *DD = dyn_cast<DecompositionDecl>(LoopVar))
2725 for (auto *Binding : DD->bindings()) {
2726 if (!Binding->isParameterPack())
2727 Binding->setType(Context.DependentTy);
2728 }
2729 LoopVar->setType(SubstAutoTypeDependent(LoopVar->getType()));
2730 }
2731 } else if (!BeginDeclStmt.get()) {
2732 SourceLocation RangeLoc = RangeVar->getLocation();
2733
2734 const QualType RangeVarNonRefType = RangeVarType.getNonReferenceType();
2735
2736 ExprResult BeginRangeRef = BuildDeclRefExpr(RangeVar, RangeVarNonRefType,
2737 VK_LValue, ColonLoc);
2738 if (BeginRangeRef.isInvalid())
2739 return StmtError();
2740
2741 ExprResult EndRangeRef = BuildDeclRefExpr(RangeVar, RangeVarNonRefType,
2742 VK_LValue, ColonLoc);
2743 if (EndRangeRef.isInvalid())
2744 return StmtError();
2745
2746 QualType AutoType = Context.getAutoDeductType();
2747 Expr *Range = RangeVar->getInit();
2748 if (!Range)
2749 return StmtError();
2750 QualType RangeType = Range->getType();
2751
2752 if (RequireCompleteType(RangeLoc, RangeType,
2753 diag::err_for_range_incomplete_type))
2754 return StmtError();
2755
2756 // P2718R0 - Lifetime extension in range-based for loops.
2757 if (getLangOpts().CPlusPlus23 && !LifetimeExtendTemps.empty()) {
2758 InitializedEntity Entity =
2759 InitializedEntity::InitializeVariable(RangeVar);
2760 for (auto *MTE : LifetimeExtendTemps)
2761 MTE->setExtendingDecl(RangeVar, Entity.allocateManglingNumber());
2762 }
2763
2764 // Build auto __begin = begin-expr, __end = end-expr.
2765 // Divide by 2, since the variables are in the inner scope (loop body).
2766 const auto DepthStr = std::to_string(S->getDepth() / 2);
2767 VarDecl *BeginVar = BuildForRangeVarDecl(*this, ColonLoc, AutoType,
2768 std::string("__begin") + DepthStr);
2769 VarDecl *EndVar = BuildForRangeVarDecl(*this, ColonLoc, AutoType,
2770 std::string("__end") + DepthStr);
2771
2772 // Build begin-expr and end-expr and attach to __begin and __end variables.
2773 ExprResult BeginExpr, EndExpr;
2774 if (const ArrayType *UnqAT = RangeType->getAsArrayTypeUnsafe()) {
2775 // - if _RangeT is an array type, begin-expr and end-expr are __range and
2776 // __range + __bound, respectively, where __bound is the array bound. If
2777 // _RangeT is an array of unknown size or an array of incomplete type,
2778 // the program is ill-formed;
2779
2780 // begin-expr is __range.
2781 BeginExpr = BeginRangeRef;
2782 if (!CoawaitLoc.isInvalid()) {
2783 BeginExpr = ActOnCoawaitExpr(S, ColonLoc, BeginExpr.get());
2784 if (BeginExpr.isInvalid())
2785 return StmtError();
2786 }
2787 if (FinishForRangeVarDecl(*this, BeginVar, BeginRangeRef.get(), ColonLoc,
2788 diag::err_for_range_iter_deduction_failure)) {
2789 NoteForRangeBeginEndFunction(*this, BeginExpr.get(), BEF_begin);
2790 return StmtError();
2791 }
2792
2793 // Find the array bound.
2794 ExprResult BoundExpr;
2795 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(UnqAT))
2796 BoundExpr = IntegerLiteral::Create(
2797 Context, CAT->getSize(), Context.getPointerDiffType(), RangeLoc);
2798 else if (const VariableArrayType *VAT =
2799 dyn_cast<VariableArrayType>(UnqAT)) {
2800 // For a variably modified type we can't just use the expression within
2801 // the array bounds, since we don't want that to be re-evaluated here.
2802 // Rather, we need to determine what it was when the array was first
2803 // created - so we resort to using sizeof(vla)/sizeof(element).
2804 // For e.g.
2805 // void f(int b) {
2806 // int vla[b];
2807 // b = -1; <-- This should not affect the num of iterations below
2808 // for (int &c : vla) { .. }
2809 // }
2810
2811 // FIXME: This results in codegen generating IR that recalculates the
2812 // run-time number of elements (as opposed to just using the IR Value
2813 // that corresponds to the run-time value of each bound that was
2814 // generated when the array was created.) If this proves too embarrassing
2815 // even for unoptimized IR, consider passing a magic-value/cookie to
2816 // codegen that then knows to simply use that initial llvm::Value (that
2817 // corresponds to the bound at time of array creation) within
2818 // getelementptr. But be prepared to pay the price of increasing a
2819 // customized form of coupling between the two components - which could
2820 // be hard to maintain as the codebase evolves.
2821
2822 ExprResult SizeOfVLAExprR = ActOnUnaryExprOrTypeTraitExpr(
2823 EndVar->getLocation(), UETT_SizeOf,
2824 /*IsType=*/true,
2825 CreateParsedType(VAT->desugar(), Context.getTrivialTypeSourceInfo(
2826 VAT->desugar(), RangeLoc))
2827 .getAsOpaquePtr(),
2828 EndVar->getSourceRange());
2829 if (SizeOfVLAExprR.isInvalid())
2830 return StmtError();
2831
2832 ExprResult SizeOfEachElementExprR = ActOnUnaryExprOrTypeTraitExpr(
2833 EndVar->getLocation(), UETT_SizeOf,
2834 /*IsType=*/true,
2835 CreateParsedType(VAT->desugar(),
2836 Context.getTrivialTypeSourceInfo(
2837 VAT->getElementType(), RangeLoc))
2838 .getAsOpaquePtr(),
2839 EndVar->getSourceRange());
2840 if (SizeOfEachElementExprR.isInvalid())
2841 return StmtError();
2842
2843 BoundExpr =
2844 ActOnBinOp(S, EndVar->getLocation(), tok::slash,
2845 SizeOfVLAExprR.get(), SizeOfEachElementExprR.get());
2846 if (BoundExpr.isInvalid())
2847 return StmtError();
2848
2849 } else {
2850 // Can't be a DependentSizedArrayType or an IncompleteArrayType since
2851 // UnqAT is not incomplete and Range is not type-dependent.
2852 llvm_unreachable("Unexpected array type in for-range");
2853 }
2854
2855 // end-expr is __range + __bound.
2856 EndExpr = ActOnBinOp(S, ColonLoc, tok::plus, EndRangeRef.get(),
2857 BoundExpr.get());
2858 if (EndExpr.isInvalid())
2859 return StmtError();
2860 if (FinishForRangeVarDecl(*this, EndVar, EndExpr.get(), ColonLoc,
2861 diag::err_for_range_iter_deduction_failure)) {
2862 NoteForRangeBeginEndFunction(*this, EndExpr.get(), BEF_end);
2863 return StmtError();
2864 }
2865 } else {
2866 OverloadCandidateSet CandidateSet(RangeLoc,
2867 OverloadCandidateSet::CSK_Normal);
2868 BeginEndFunction BEFFailure;
2869 ForRangeStatus RangeStatus = BuildNonArrayForRange(
2870 *this, BeginRangeRef.get(), EndRangeRef.get(), RangeType, BeginVar,
2871 EndVar, ColonLoc, CoawaitLoc, &CandidateSet, &BeginExpr, &EndExpr,
2872 &BEFFailure);
2873
2874 if (Kind == BFRK_Build && RangeStatus == FRS_NoViableFunction &&
2875 BEFFailure == BEF_begin) {
2876 // If the range is being built from an array parameter, emit a
2877 // a diagnostic that it is being treated as a pointer.
2878 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Range)) {
2879 if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
2880 QualType ArrayTy = PVD->getOriginalType();
2881 QualType PointerTy = PVD->getType();
2882 if (PointerTy->isPointerType() && ArrayTy->isArrayType()) {
2883 Diag(Range->getBeginLoc(), diag::err_range_on_array_parameter)
2884 << RangeLoc << PVD << ArrayTy << PointerTy;
2885 Diag(PVD->getLocation(), diag::note_declared_at);
2886 return StmtError();
2887 }
2888 }
2889 }
2890
2891 // If building the range failed, try dereferencing the range expression
2892 // unless a diagnostic was issued or the end function is problematic.
2893 StmtResult SR = RebuildForRangeWithDereference(*this, S, ForLoc,
2894 CoawaitLoc, InitStmt,
2895 LoopVarDecl, ColonLoc,
2896 Range, RangeLoc,
2897 RParenLoc);
2898 if (SR.isInvalid() || SR.isUsable())
2899 return SR;
2900 }
2901
2902 // Otherwise, emit diagnostics if we haven't already.
2903 if (RangeStatus == FRS_NoViableFunction) {
2904 Expr *Range = BEFFailure ? EndRangeRef.get() : BeginRangeRef.get();
2905 CandidateSet.NoteCandidates(
2906 PartialDiagnosticAt(Range->getBeginLoc(),
2907 PDiag(diag::err_for_range_invalid)
2908 << RangeLoc << Range->getType()
2909 << BEFFailure),
2910 *this, OCD_AllCandidates, Range);
2911 }
2912 // Return an error if no fix was discovered.
2913 if (RangeStatus != FRS_Success)
2914 return StmtError();
2915 }
2916
2917 assert(!BeginExpr.isInvalid() && !EndExpr.isInvalid() &&
2918 "invalid range expression in for loop");
2919
2920 // C++11 [dcl.spec.auto]p7: BeginType and EndType must be the same.
2921 // C++1z removes this restriction.
2922 QualType BeginType = BeginVar->getType(), EndType = EndVar->getType();
2923 if (!Context.hasSameType(BeginType, EndType)) {
2924 Diag(RangeLoc, getLangOpts().CPlusPlus17
2925 ? diag::warn_for_range_begin_end_types_differ
2926 : diag::ext_for_range_begin_end_types_differ)
2927 << BeginType << EndType;
2928 NoteForRangeBeginEndFunction(*this, BeginExpr.get(), BEF_begin);
2929 NoteForRangeBeginEndFunction(*this, EndExpr.get(), BEF_end);
2930 }
2931
2932 BeginDeclStmt =
2933 ActOnDeclStmt(ConvertDeclToDeclGroup(BeginVar), ColonLoc, ColonLoc);
2934 EndDeclStmt =
2935 ActOnDeclStmt(ConvertDeclToDeclGroup(EndVar), ColonLoc, ColonLoc);
2936
2937 const QualType BeginRefNonRefType = BeginType.getNonReferenceType();
2938 ExprResult BeginRef = BuildDeclRefExpr(BeginVar, BeginRefNonRefType,
2939 VK_LValue, ColonLoc);
2940 if (BeginRef.isInvalid())
2941 return StmtError();
2942
2943 ExprResult EndRef = BuildDeclRefExpr(EndVar, EndType.getNonReferenceType(),
2944 VK_LValue, ColonLoc);
2945 if (EndRef.isInvalid())
2946 return StmtError();
2947
2948 // Build and check __begin != __end expression.
2949 NotEqExpr = ActOnBinOp(S, ColonLoc, tok::exclaimequal,
2950 BeginRef.get(), EndRef.get());
2951 if (!NotEqExpr.isInvalid())
2952 NotEqExpr = CheckBooleanCondition(ColonLoc, NotEqExpr.get());
2953 if (!NotEqExpr.isInvalid())
2954 NotEqExpr =
2955 ActOnFinishFullExpr(NotEqExpr.get(), /*DiscardedValue*/ false);
2956 if (NotEqExpr.isInvalid()) {
2957 Diag(RangeLoc, diag::note_for_range_invalid_iterator)
2958 << RangeLoc << 0 << BeginRangeRef.get()->getType();
2959 NoteForRangeBeginEndFunction(*this, BeginExpr.get(), BEF_begin);
2960 if (!Context.hasSameType(BeginType, EndType))
2961 NoteForRangeBeginEndFunction(*this, EndExpr.get(), BEF_end);
2962 return StmtError();
2963 }
2964
2965 // Build and check ++__begin expression.
2966 BeginRef = BuildDeclRefExpr(BeginVar, BeginRefNonRefType,
2967 VK_LValue, ColonLoc);
2968 if (BeginRef.isInvalid())
2969 return StmtError();
2970
2971 IncrExpr = ActOnUnaryOp(S, ColonLoc, tok::plusplus, BeginRef.get());
2972 if (!IncrExpr.isInvalid() && CoawaitLoc.isValid())
2973 // FIXME: getCurScope() should not be used during template instantiation.
2974 // We should pick up the set of unqualified lookup results for operator
2975 // co_await during the initial parse.
2976 IncrExpr = ActOnCoawaitExpr(S, CoawaitLoc, IncrExpr.get());
2977 if (!IncrExpr.isInvalid())
2978 IncrExpr = ActOnFinishFullExpr(IncrExpr.get(), /*DiscardedValue*/ false);
2979 if (IncrExpr.isInvalid()) {
2980 Diag(RangeLoc, diag::note_for_range_invalid_iterator)
2981 << RangeLoc << 2 << BeginRangeRef.get()->getType() ;
2982 NoteForRangeBeginEndFunction(*this, BeginExpr.get(), BEF_begin);
2983 return StmtError();
2984 }
2985
2986 // Build and check *__begin expression.
2987 BeginRef = BuildDeclRefExpr(BeginVar, BeginRefNonRefType,
2988 VK_LValue, ColonLoc);
2989 if (BeginRef.isInvalid())
2990 return StmtError();
2991
2992 ExprResult DerefExpr = ActOnUnaryOp(S, ColonLoc, tok::star, BeginRef.get());
2993 if (DerefExpr.isInvalid()) {
2994 Diag(RangeLoc, diag::note_for_range_invalid_iterator)
2995 << RangeLoc << 1 << BeginRangeRef.get()->getType();
2996 NoteForRangeBeginEndFunction(*this, BeginExpr.get(), BEF_begin);
2997 return StmtError();
2998 }
2999
3000 // Attach *__begin as initializer for VD. Don't touch it if we're just
3001 // trying to determine whether this would be a valid range.
3002 if (!LoopVar->isInvalidDecl() && Kind != BFRK_Check) {
3003 AddInitializerToDecl(LoopVar, DerefExpr.get(), /*DirectInit=*/false);
3004 if (LoopVar->isInvalidDecl() ||
3005 (LoopVar->getInit() && LoopVar->getInit()->containsErrors()))
3006 NoteForRangeBeginEndFunction(*this, BeginExpr.get(), BEF_begin);
3007 }
3008 }
3009
3010 // Don't bother to actually allocate the result if we're just trying to
3011 // determine whether it would be valid.
3012 if (Kind == BFRK_Check)
3013 return StmtResult();
3014
3015 // In OpenMP loop region loop control variable must be private. Perform
3016 // analysis of first part (if any).
3017 if (getLangOpts().OpenMP >= 50 && BeginDeclStmt.isUsable())
3018 OpenMP().ActOnOpenMPLoopInitialization(ForLoc, BeginDeclStmt.get());
3019
3020 return new (Context) CXXForRangeStmt(
3021 InitStmt, RangeDS, cast_or_null<DeclStmt>(BeginDeclStmt.get()),
3022 cast_or_null<DeclStmt>(EndDeclStmt.get()), NotEqExpr.get(),
3023 IncrExpr.get(), LoopVarDS, /*Body=*/nullptr, ForLoc, CoawaitLoc,
3024 ColonLoc, RParenLoc);
3025 }
3026
3027 // Warn when the loop variable is a const reference that creates a copy.
3028 // Suggest using the non-reference type for copies. If a copy can be prevented
3029 // suggest the const reference type that would do so.
3030 // For instance, given "for (const &Foo : Range)", suggest
3031 // "for (const Foo : Range)" to denote a copy is made for the loop. If
3032 // possible, also suggest "for (const &Bar : Range)" if this type prevents
3033 // the copy altogether.
3034 static void DiagnoseForRangeReferenceVariableCopies(Sema &SemaRef,
3035 const VarDecl *VD,
3036 QualType RangeInitType) {
3037 const Expr *InitExpr = VD->getInit();
3038 if (!InitExpr)
3039 return;
3040
3041 QualType VariableType = VD->getType();
3042
3043 if (auto Cleanups = dyn_cast<ExprWithCleanups>(InitExpr))
3044 if (!Cleanups->cleanupsHaveSideEffects())
3045 InitExpr = Cleanups->getSubExpr();
3046
3047 const MaterializeTemporaryExpr *MTE =
3048 dyn_cast<MaterializeTemporaryExpr>(InitExpr);
3049
3050 // No copy made.
3051 if (!MTE)
3052 return;
3053
3054 const Expr *E = MTE->getSubExpr()->IgnoreImpCasts();
3055
3056 // Searching for either UnaryOperator for dereference of a pointer or
3057 // CXXOperatorCallExpr for handling iterators.
3058 while (!isa<CXXOperatorCallExpr>(E) && !isa<UnaryOperator>(E)) {
3059 if (const CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(E)) {
3060 E = CCE->getArg(0);
3061 } else if (const CXXMemberCallExpr *Call = dyn_cast<CXXMemberCallExpr>(E)) {
3062 const MemberExpr *ME = cast<MemberExpr>(Call->getCallee());
3063 E = ME->getBase();
3064 } else {
3065 const MaterializeTemporaryExpr *MTE = cast<MaterializeTemporaryExpr>(E);
3066 E = MTE->getSubExpr();
3067 }
3068 E = E->IgnoreImpCasts();
3069 }
3070
3071 QualType ReferenceReturnType;
3072 if (isa<UnaryOperator>(E)) {
3073 ReferenceReturnType = SemaRef.Context.getLValueReferenceType(E->getType());
3074 } else {
3075 const CXXOperatorCallExpr *Call = cast<CXXOperatorCallExpr>(E);
3076 const FunctionDecl *FD = Call->getDirectCallee();
3077 QualType ReturnType = FD->getReturnType();
3078 if (ReturnType->isReferenceType())
3079 ReferenceReturnType = ReturnType;
3080 }
3081
3082 if (!ReferenceReturnType.isNull()) {
3083 // Loop variable creates a temporary. Suggest either to go with
3084 // non-reference loop variable to indicate a copy is made, or
3085 // the correct type to bind a const reference.
3086 SemaRef.Diag(VD->getLocation(),
3087 diag::warn_for_range_const_ref_binds_temp_built_from_ref)
3088 << VD << VariableType << ReferenceReturnType;
3089 QualType NonReferenceType = VariableType.getNonReferenceType();
3090 NonReferenceType.removeLocalConst();
3091 QualType NewReferenceType =
3092 SemaRef.Context.getLValueReferenceType(E->getType().withConst());
3093 SemaRef.Diag(VD->getBeginLoc(), diag::note_use_type_or_non_reference)
3094 << NonReferenceType << NewReferenceType << VD->getSourceRange()
3095 << FixItHint::CreateRemoval(VD->getTypeSpecEndLoc());
3096 } else if (!VariableType->isRValueReferenceType()) {
3097 // The range always returns a copy, so a temporary is always created.
3098 // Suggest removing the reference from the loop variable.
3099 // If the type is a rvalue reference do not warn since that changes the
3100 // semantic of the code.
3101 SemaRef.Diag(VD->getLocation(), diag::warn_for_range_ref_binds_ret_temp)
3102 << VD << RangeInitType;
3103 QualType NonReferenceType = VariableType.getNonReferenceType();
3104 NonReferenceType.removeLocalConst();
3105 SemaRef.Diag(VD->getBeginLoc(), diag::note_use_non_reference_type)
3106 << NonReferenceType << VD->getSourceRange()
3107 << FixItHint::CreateRemoval(VD->getTypeSpecEndLoc());
3108 }
3109 }
3110
3111 /// Determines whether the @p VariableType's declaration is a record with the
3112 /// clang::trivial_abi attribute.
3113 static bool hasTrivialABIAttr(QualType VariableType) {
3114 if (CXXRecordDecl *RD = VariableType->getAsCXXRecordDecl())
3115 return RD->hasAttr<TrivialABIAttr>();
3116
3117 return false;
3118 }
3119
3120 // Warns when the loop variable can be changed to a reference type to
3121 // prevent a copy. For instance, if given "for (const Foo x : Range)" suggest
3122 // "for (const Foo &x : Range)" if this form does not make a copy.
3123 static void DiagnoseForRangeConstVariableCopies(Sema &SemaRef,
3124 const VarDecl *VD) {
3125 const Expr *InitExpr = VD->getInit();
3126 if (!InitExpr)
3127 return;
3128
3129 QualType VariableType = VD->getType();
3130
3131 if (const CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(InitExpr)) {
3132 if (!CE->getConstructor()->isCopyConstructor())
3133 return;
3134 } else if (const CastExpr *CE = dyn_cast<CastExpr>(InitExpr)) {
3135 if (CE->getCastKind() != CK_LValueToRValue)
3136 return;
3137 } else {
3138 return;
3139 }
3140
3141 // Small trivially copyable types are cheap to copy. Do not emit the
3142 // diagnostic for these instances. 64 bytes is a common size of a cache line.
3143 // (The function `getTypeSize` returns the size in bits.)
3144 ASTContext &Ctx = SemaRef.Context;
3145 if (Ctx.getTypeSize(VariableType) <= 64 * 8 &&
3146 (VariableType.isTriviallyCopyConstructibleType(Ctx) ||
3147 hasTrivialABIAttr(VariableType)))
3148 return;
3149
3150 // Suggest changing from a const variable to a const reference variable
3151 // if doing so will prevent a copy.
3152 SemaRef.Diag(VD->getLocation(), diag::warn_for_range_copy)
3153 << VD << VariableType;
3154 SemaRef.Diag(VD->getBeginLoc(), diag::note_use_reference_type)
3155 << SemaRef.Context.getLValueReferenceType(VariableType)
3156 << VD->getSourceRange()
3157 << FixItHint::CreateInsertion(VD->getLocation(), "&");
3158 }
3159
3160 /// DiagnoseForRangeVariableCopies - Diagnose three cases and fixes for them.
3161 /// 1) for (const foo &x : foos) where foos only returns a copy. Suggest
3162 /// using "const foo x" to show that a copy is made
3163 /// 2) for (const bar &x : foos) where bar is a temporary initialized by bar.
3164 /// Suggest either "const bar x" to keep the copying or "const foo& x" to
3165 /// prevent the copy.
3166 /// 3) for (const foo x : foos) where x is constructed from a reference foo.
3167 /// Suggest "const foo &x" to prevent the copy.
3168 static void DiagnoseForRangeVariableCopies(Sema &SemaRef,
3169 const CXXForRangeStmt *ForStmt) {
3170 if (SemaRef.inTemplateInstantiation())
3171 return;
3172
3173 SourceLocation Loc = ForStmt->getBeginLoc();
3174 if (SemaRef.Diags.isIgnored(
3175 diag::warn_for_range_const_ref_binds_temp_built_from_ref, Loc) &&
3176 SemaRef.Diags.isIgnored(diag::warn_for_range_ref_binds_ret_temp, Loc) &&
3177 SemaRef.Diags.isIgnored(diag::warn_for_range_copy, Loc)) {
3178 return;
3179 }
3180
3181 const VarDecl *VD = ForStmt->getLoopVariable();
3182 if (!VD)
3183 return;
3184
3185 QualType VariableType = VD->getType();
3186
3187 if (VariableType->isIncompleteType())
3188 return;
3189
3190 const Expr *InitExpr = VD->getInit();
3191 if (!InitExpr)
3192 return;
3193
3194 if (InitExpr->getExprLoc().isMacroID())
3195 return;
3196
3197 if (VariableType->isReferenceType()) {
3198 DiagnoseForRangeReferenceVariableCopies(SemaRef, VD,
3199 ForStmt->getRangeInit()->getType());
3200 } else if (VariableType.isConstQualified()) {
3201 DiagnoseForRangeConstVariableCopies(SemaRef, VD);
3202 }
3203 }
3204
3205 StmtResult Sema::FinishCXXForRangeStmt(Stmt *S, Stmt *B) {
3206 if (!S || !B)
3207 return StmtError();
3208
3209 if (isa<ObjCForCollectionStmt>(S))
3210 return ObjC().FinishObjCForCollectionStmt(S, B);
3211
3212 CXXForRangeStmt *ForStmt = cast<CXXForRangeStmt>(S);
3213 ForStmt->setBody(B);
3214
3215 DiagnoseEmptyStmtBody(ForStmt->getRParenLoc(), B,
3216 diag::warn_empty_range_based_for_body);
3217
3218 DiagnoseForRangeVariableCopies(*this, ForStmt);
3219
3220 return S;
3221 }
3222
3223 StmtResult Sema::ActOnGotoStmt(SourceLocation GotoLoc,
3224 SourceLocation LabelLoc,
3225 LabelDecl *TheDecl) {
3226 setFunctionHasBranchIntoScope();
3227
3228 // If this goto is in a compute construct scope, we need to make sure we check
3229 // gotos in/out.
3230 if (getCurScope()->isInOpenACCComputeConstructScope())
3231 setFunctionHasBranchProtectedScope();
3232
3233 TheDecl->markUsed(Context);
3234 return new (Context) GotoStmt(TheDecl, GotoLoc, LabelLoc);
3235 }
3236
3237 StmtResult
3238 Sema::ActOnIndirectGotoStmt(SourceLocation GotoLoc, SourceLocation StarLoc,
3239 Expr *E) {
3240 // Convert operand to void*
3241 if (!E->isTypeDependent()) {
3242 QualType ETy = E->getType();
3243 QualType DestTy = Context.getPointerType(Context.VoidTy.withConst());
3244 ExprResult ExprRes = E;
3245 AssignConvertType ConvTy =
3246 CheckSingleAssignmentConstraints(DestTy, ExprRes);
3247 if (ExprRes.isInvalid())
3248 return StmtError();
3249 E = ExprRes.get();
3250 if (DiagnoseAssignmentResult(ConvTy, StarLoc, DestTy, ETy, E,
3251 AssignmentAction::Passing))
3252 return StmtError();
3253 }
3254
3255 ExprResult ExprRes = ActOnFinishFullExpr(E, /*DiscardedValue*/ false);
3256 if (ExprRes.isInvalid())
3257 return StmtError();
3258 E = ExprRes.get();
3259
3260 setFunctionHasIndirectGoto();
3261
3262 // If this goto is in a compute construct scope, we need to make sure we
3263 // check gotos in/out.
3264 if (getCurScope()->isInOpenACCComputeConstructScope())
3265 setFunctionHasBranchProtectedScope();
3266
3267 return new (Context) IndirectGotoStmt(GotoLoc, StarLoc, E);
3268 }
3269
3270 static void CheckJumpOutOfSEHFinallyOrDefer(Sema &S, SourceLocation Loc,
3271 const Scope &DestScope,
3272 unsigned DeferJumpKind) {
3273 if (!S.CurrentSEHFinally.empty() &&
3274 DestScope.Contains(*S.CurrentSEHFinally.back())) {
3275 S.Diag(Loc, diag::warn_jump_out_of_seh_finally);
3276 }
3277
3278 if (!S.CurrentDefer.empty()) {
3279 Scope *Parent = S.CurrentDefer.back().first;
3280 assert(Parent);
3281
3282 // Note: We don't create a new scope for defer statements, so 'Parent'
3283 // is actually the scope that contains the '_Defer'.
3284 if (DestScope.Contains(*Parent) || &DestScope == Parent)
3285 S.Diag(Loc, diag::err_jump_out_of_defer_stmt) << DeferJumpKind;
3286 }
3287 }
3288
3289 static Scope *FindLabeledBreakContinueScope(Sema &S, Scope *CurScope,
3290 SourceLocation KWLoc,
3291 LabelDecl *Target,
3292 SourceLocation LabelLoc,
3293 bool IsContinue) {
3294 assert(Target && "not a named break/continue?");
3295
3296 Target->markUsed(S.Context);
3297
3298 Scope *Found = nullptr;
3299 for (Scope *Scope = CurScope; Scope; Scope = Scope->getParent()) {
3300 if (Scope->isFunctionScope())
3301 break;
3302
3303 if (Scope->isOpenACCComputeConstructScope()) {
3304 S.Diag(KWLoc, diag::err_acc_branch_in_out_compute_construct)
3305 << /*branch*/ 0 << /*out of*/ 0;
3306 return nullptr;
3307 }
3308
3309 if (Scope->isBreakOrContinueScope() &&
3310 Scope->getPrecedingLabel() == Target) {
3311 Found = Scope;
3312 break;
3313 }
3314 }
3315
3316 if (Found) {
3317 if (IsContinue && !Found->isContinueScope()) {
3318 S.Diag(LabelLoc, diag::err_continue_switch);
3319 return nullptr;
3320 }
3321 return Found;
3322 }
3323
3324 S.Diag(LabelLoc, diag::err_break_continue_label_not_found) << IsContinue;
3325 return nullptr;
3326 }
3327
3328 StmtResult Sema::ActOnContinueStmt(SourceLocation ContinueLoc, Scope *CurScope,
3329 LabelDecl *Target, SourceLocation LabelLoc) {
3330 Scope *S;
3331 if (Target) {
3332 S = FindLabeledBreakContinueScope(*this, CurScope, ContinueLoc, Target,
3333 LabelLoc,
3334 /*IsContinue=*/true);
3335 if (!S)
3336 return StmtError();
3337 } else {
3338 S = CurScope->getContinueParent();
3339 }
3340
3341 if (!S) {
3342 // C99 6.8.6.2p1: A break shall appear only in or as a loop body.
3343 return StmtError(Diag(ContinueLoc, diag::err_continue_not_in_loop));
3344 }
3345 if (S->isConditionVarScope()) {
3346 // We cannot 'continue;' from within a statement expression in the
3347 // initializer of a condition variable because we would jump past the
3348 // initialization of that variable.
3349 return StmtError(Diag(ContinueLoc, diag::err_continue_from_cond_var_init));
3350 }
3351
3352 // A 'continue' that would normally have execution continue on a block outside
3353 // of a compute construct counts as 'branching out of' the compute construct,
3354 // so diagnose here.
3355 if (S->isOpenACCComputeConstructScope())
3356 return StmtError(
3357 Diag(ContinueLoc, diag::err_acc_branch_in_out_compute_construct)
3358 << /*branch*/ 0 << /*out of */ 0);
3359
3360 CheckJumpOutOfSEHFinallyOrDefer(*this, ContinueLoc, *S,
3361 diag::DeferJumpKind::Continue);
3362
3363 return new (Context) ContinueStmt(ContinueLoc, LabelLoc, Target);
3364 }
3365
3366 StmtResult Sema::ActOnBreakStmt(SourceLocation BreakLoc, Scope *CurScope,
3367 LabelDecl *Target, SourceLocation LabelLoc) {
3368 Scope *S;
3369 if (Target) {
3370 S = FindLabeledBreakContinueScope(*this, CurScope, BreakLoc, Target,
3371 LabelLoc,
3372 /*IsContinue=*/false);
3373 if (!S)
3374 return StmtError();
3375 } else {
3376 S = CurScope->getBreakParent();
3377 }
3378
3379 if (!S) {
3380 // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body.
3381 return StmtError(Diag(BreakLoc, diag::err_break_not_in_loop_or_switch));
3382 }
3383
3384 if (S->isOpenMPLoopScope())
3385 return StmtError(Diag(BreakLoc, diag::err_omp_loop_cannot_use_stmt)
3386 << "break");
3387
3388 // OpenACC doesn't allow 'break'ing from a compute construct, so diagnose if
3389 // we are trying to do so. This can come in 2 flavors: 1-the break'able thing
3390 // (besides the compute construct) 'contains' the compute construct, at which
3391 // point the 'break' scope will be the compute construct. Else it could be a
3392 // loop of some sort that has a direct parent of the compute construct.
3393 // However, a 'break' in a 'switch' marked as a compute construct doesn't
3394 // count as 'branch out of' the compute construct.
3395 if (S->isOpenACCComputeConstructScope() ||
3396 (S->isLoopScope() && S->getParent() &&
3397 S->getParent()->isOpenACCComputeConstructScope()))
3398 return StmtError(
3399 Diag(BreakLoc, diag::err_acc_branch_in_out_compute_construct)
3400 << /*branch*/ 0 << /*out of */ 0);
3401
3402 CheckJumpOutOfSEHFinallyOrDefer(*this, BreakLoc, *S,
3403 diag::DeferJumpKind::Break);
3404
3405 return new (Context) BreakStmt(BreakLoc, LabelLoc, Target);
3406 }
3407
3408 Sema::NamedReturnInfo Sema::getNamedReturnInfo(Expr *&E,
3409 SimplerImplicitMoveMode Mode) {
3410 if (!E)
3411 return NamedReturnInfo();
3412 // - in a return statement in a function [where] ...
3413 // ... the expression is the name of a non-volatile automatic object ...
3414 const auto *DR = dyn_cast<DeclRefExpr>(E->IgnoreParens());
3415 if (!DR || DR->refersToEnclosingVariableOrCapture())
3416 return NamedReturnInfo();
3417 const auto *VD = dyn_cast<VarDecl>(DR->getDecl());
3418 if (!VD)
3419 return NamedReturnInfo();
3420 if (VD->getInit() && VD->getInit()->containsErrors())
3421 return NamedReturnInfo();
3422 NamedReturnInfo Res = getNamedReturnInfo(VD);
3423 if (Res.Candidate && !E->isXValue() &&
3424 (Mode == SimplerImplicitMoveMode::ForceOn ||
3425 (Mode != SimplerImplicitMoveMode::ForceOff &&
3426 getLangOpts().CPlusPlus23))) {
3427 E = ImplicitCastExpr::Create(Context, VD->getType().getNonReferenceType(),
3428 CK_NoOp, E, nullptr, VK_XValue,
3429 FPOptionsOverride());
3430 }
3431 return Res;
3432 }
3433
3434 Sema::NamedReturnInfo Sema::getNamedReturnInfo(const VarDecl *VD) {
3435 NamedReturnInfo Info{VD, NamedReturnInfo::MoveEligibleAndCopyElidable};
3436
3437 // C++20 [class.copy.elision]p3:
3438 // - in a return statement in a function with ...
3439 // (other than a function ... parameter)
3440 if (VD->getKind() == Decl::ParmVar)
3441 Info.S = NamedReturnInfo::MoveEligible;
3442 else if (VD->getKind() != Decl::Var)
3443 return NamedReturnInfo();
3444
3445 // (other than ... a catch-clause parameter)
3446 if (VD->isExceptionVariable())
3447 Info.S = NamedReturnInfo::MoveEligible;
3448
3449 // ...automatic...
3450 if (!VD->hasLocalStorage())
3451 return NamedReturnInfo();
3452
3453 // We don't want to implicitly move out of a __block variable during a return
3454 // because we cannot assume the variable will no longer be used.
3455 if (VD->hasAttr<BlocksAttr>())
3456 return NamedReturnInfo();
3457
3458 QualType VDType = VD->getType();
3459 if (VDType->isObjectType()) {
3460 // C++17 [class.copy.elision]p3:
3461 // ...non-volatile automatic object...
3462 if (VDType.isVolatileQualified())
3463 return NamedReturnInfo();
3464 } else if (VDType->isRValueReferenceType()) {
3465 // C++20 [class.copy.elision]p3:
3466 // ...either a non-volatile object or an rvalue reference to a non-volatile
3467 // object type...
3468 QualType VDReferencedType = VDType.getNonReferenceType();
3469 if (VDReferencedType.isVolatileQualified() ||
3470 !VDReferencedType->isObjectType())
3471 return NamedReturnInfo();
3472 Info.S = NamedReturnInfo::MoveEligible;
3473 } else {
3474 return NamedReturnInfo();
3475 }
3476
3477 // Variables with higher required alignment than their type's ABI
3478 // alignment cannot use NRVO.
3479 if (!VD->hasDependentAlignment() && !VDType->isIncompleteType() &&
3480 Context.getDeclAlign(VD) > Context.getTypeAlignInChars(VDType))
3481 Info.S = NamedReturnInfo::MoveEligible;
3482
3483 return Info;
3484 }
3485
3486 const VarDecl *Sema::getCopyElisionCandidate(NamedReturnInfo &Info,
3487 QualType ReturnType) {
3488 if (!Info.Candidate)
3489 return nullptr;
3490
3491 auto invalidNRVO = [&] {
3492 Info = NamedReturnInfo();
3493 return nullptr;
3494 };
3495
3496 // If we got a non-deduced auto ReturnType, we are in a dependent context and
3497 // there is no point in allowing copy elision since we won't have it deduced
3498 // by the point the VardDecl is instantiated, which is the last chance we have
3499 // of deciding if the candidate is really copy elidable.
3500 if ((ReturnType->getTypeClass() == Type::TypeClass::Auto &&
3501 ReturnType->isCanonicalUnqualified()) ||
3502 ReturnType->isSpecificBuiltinType(BuiltinType::Dependent))
3503 return invalidNRVO();
3504
3505 if (!ReturnType->isDependentType()) {
3506 // - in a return statement in a function with ...
3507 // ... a class return type ...
3508 if (!ReturnType->isRecordType())
3509 return invalidNRVO();
3510
3511 QualType VDType = Info.Candidate->getType();
3512 // ... the same cv-unqualified type as the function return type ...
3513 // When considering moving this expression out, allow dissimilar types.
3514 if (!VDType->isDependentType() &&
3515 !Context.hasSameUnqualifiedType(ReturnType, VDType))
3516 Info.S = NamedReturnInfo::MoveEligible;
3517 }
3518 return Info.isCopyElidable() ? Info.Candidate : nullptr;
3519 }
3520
3521 /// Verify that the initialization sequence that was picked for the
3522 /// first overload resolution is permissible under C++98.
3523 ///
3524 /// Reject (possibly converting) constructors not taking an rvalue reference,
3525 /// or user conversion operators which are not ref-qualified.
3526 static bool
3527 VerifyInitializationSequenceCXX98(const Sema &S,
3528 const InitializationSequence &Seq) {
3529 const auto *Step = llvm::find_if(Seq.steps(), [](const auto &Step) {
3530 return Step.Kind == InitializationSequence::SK_ConstructorInitialization ||
3531 Step.Kind == InitializationSequence::SK_UserConversion;
3532 });
3533 if (Step != Seq.step_end()) {
3534 const auto *FD = Step->Function.Function;
3535 if (isa<CXXConstructorDecl>(FD)
3536 ? !FD->getParamDecl(0)->getType()->isRValueReferenceType()
3537 : cast<CXXMethodDecl>(FD)->getRefQualifier() == RQ_None)
3538 return false;
3539 }
3540 return true;
3541 }
3542
3543 ExprResult Sema::PerformMoveOrCopyInitialization(
3544 const InitializedEntity &Entity, const NamedReturnInfo &NRInfo, Expr *Value,
3545 bool SupressSimplerImplicitMoves) {
3546 if (getLangOpts().CPlusPlus &&
3547 (!getLangOpts().CPlusPlus23 || SupressSimplerImplicitMoves) &&
3548 NRInfo.isMoveEligible()) {
3549 ImplicitCastExpr AsRvalue(ImplicitCastExpr::OnStack, Value->getType(),
3550 CK_NoOp, Value, VK_XValue, FPOptionsOverride());
3551 Expr *InitExpr = &AsRvalue;
3552 auto Kind = InitializationKind::CreateCopy(Value->getBeginLoc(),
3553 Value->getBeginLoc());
3554 InitializationSequence Seq(*this, Entity, Kind, InitExpr);
3555 auto Res = Seq.getFailedOverloadResult();
3556 if ((Res == OR_Success || Res == OR_Deleted) &&
3557 (getLangOpts().CPlusPlus11 ||
3558 VerifyInitializationSequenceCXX98(*this, Seq))) {
3559 // Promote "AsRvalue" to the heap, since we now need this
3560 // expression node to persist.
3561 Value =
3562 ImplicitCastExpr::Create(Context, Value->getType(), CK_NoOp, Value,
3563 nullptr, VK_XValue, FPOptionsOverride());
3564 // Complete type-checking the initialization of the return type
3565 // using the constructor we found.
3566 return Seq.Perform(*this, Entity, Kind, Value);
3567 }
3568 }
3569 // Either we didn't meet the criteria for treating an lvalue as an rvalue,
3570 // above, or overload resolution failed. Either way, we need to try
3571 // (again) now with the return value expression as written.
3572 return PerformCopyInitialization(Entity, SourceLocation(), Value);
3573 }
3574
3575 /// Determine whether the declared return type of the specified function
3576 /// contains 'auto'.
3577 static bool hasDeducedReturnType(FunctionDecl *FD) {
3578 const FunctionProtoType *FPT =
3579 FD->getTypeSourceInfo()->getType()->castAs<FunctionProtoType>();
3580 return FPT->getReturnType()->isUndeducedType();
3581 }
3582
3583 StmtResult Sema::ActOnCapScopeReturnStmt(SourceLocation ReturnLoc,
3584 Expr *RetValExp,
3585 NamedReturnInfo &NRInfo,
3586 bool SupressSimplerImplicitMoves) {
3587 // If this is the first return we've seen, infer the return type.
3588 // [expr.prim.lambda]p4 in C++11; block literals follow the same rules.
3589 CapturingScopeInfo *CurCap = cast<CapturingScopeInfo>(getCurFunction());
3590 QualType FnRetType = CurCap->ReturnType;
3591 LambdaScopeInfo *CurLambda = dyn_cast<LambdaScopeInfo>(CurCap);
3592 if (CurLambda && CurLambda->CallOperator->getType().isNull())
3593 return StmtError();
3594 bool HasDeducedReturnType =
3595 CurLambda && hasDeducedReturnType(CurLambda->CallOperator);
3596
3597 if (ExprEvalContexts.back().isDiscardedStatementContext() &&
3598 (HasDeducedReturnType || CurCap->HasImplicitReturnType)) {
3599 if (RetValExp) {
3600 ExprResult ER =
3601 ActOnFinishFullExpr(RetValExp, ReturnLoc, /*DiscardedValue*/ false);
3602 if (ER.isInvalid())
3603 return StmtError();
3604 RetValExp = ER.get();
3605 }
3606 return ReturnStmt::Create(Context, ReturnLoc, RetValExp,
3607 /* NRVOCandidate=*/nullptr);
3608 }
3609
3610 if (HasDeducedReturnType) {
3611 FunctionDecl *FD = CurLambda->CallOperator;
3612 // If we've already decided this lambda is invalid, e.g. because
3613 // we saw a `return` whose expression had an error, don't keep
3614 // trying to deduce its return type.
3615 if (FD->isInvalidDecl())
3616 return StmtError();
3617 // In C++1y, the return type may involve 'auto'.
3618 // FIXME: Blocks might have a return type of 'auto' explicitly specified.
3619 if (CurCap->ReturnType.isNull())
3620 CurCap->ReturnType = FD->getReturnType();
3621
3622 AutoType *AT = CurCap->ReturnType->getContainedAutoType();
3623 assert(AT && "lost auto type from lambda return type");
3624 if (DeduceFunctionTypeFromReturnExpr(FD, ReturnLoc, RetValExp, AT)) {
3625 FD->setInvalidDecl();
3626 // FIXME: preserve the ill-formed return expression.
3627 return StmtError();
3628 }
3629 CurCap->ReturnType = FnRetType = FD->getReturnType();
3630 } else if (CurCap->HasImplicitReturnType) {
3631 // For blocks/lambdas with implicit return types, we check each return
3632 // statement individually, and deduce the common return type when the block
3633 // or lambda is completed.
3634 // FIXME: Fold this into the 'auto' codepath above.
3635 if (RetValExp && !isa<InitListExpr>(RetValExp)) {
3636 ExprResult Result = DefaultFunctionArrayLvalueConversion(RetValExp);
3637 if (Result.isInvalid())
3638 return StmtError();
3639 RetValExp = Result.get();
3640
3641 // DR1048: even prior to C++14, we should use the 'auto' deduction rules
3642 // when deducing a return type for a lambda-expression (or by extension
3643 // for a block). These rules differ from the stated C++11 rules only in
3644 // that they remove top-level cv-qualifiers.
3645 if (!CurContext->isDependentContext())
3646 FnRetType = RetValExp->getType().getUnqualifiedType();
3647 else
3648 FnRetType = CurCap->ReturnType = Context.DependentTy;
3649 } else {
3650 if (RetValExp) {
3651 // C++11 [expr.lambda.prim]p4 bans inferring the result from an
3652 // initializer list, because it is not an expression (even
3653 // though we represent it as one). We still deduce 'void'.
3654 Diag(ReturnLoc, diag::err_lambda_return_init_list)
3655 << RetValExp->getSourceRange();
3656 }
3657
3658 FnRetType = Context.VoidTy;
3659 }
3660
3661 // Although we'll properly infer the type of the block once it's completed,
3662 // make sure we provide a return type now for better error recovery.
3663 if (CurCap->ReturnType.isNull())
3664 CurCap->ReturnType = FnRetType;
3665 }
3666 const VarDecl *NRVOCandidate = getCopyElisionCandidate(NRInfo, FnRetType);
3667
3668 if (auto *CurBlock = dyn_cast<BlockScopeInfo>(CurCap)) {
3669 if (CurBlock->FunctionType->castAs<FunctionType>()->getNoReturnAttr()) {
3670 Diag(ReturnLoc, diag::err_noreturn_has_return_expr)
3671 << diag::FalloffFunctionKind::Block;
3672 return StmtError();
3673 }
3674 } else if (auto *CurRegion = dyn_cast<CapturedRegionScopeInfo>(CurCap)) {
3675 Diag(ReturnLoc, diag::err_return_in_captured_stmt) << CurRegion->getRegionName();
3676 return StmtError();
3677 } else {
3678 assert(CurLambda && "unknown kind of captured scope");
3679 if (CurLambda->CallOperator->getType()
3680 ->castAs<FunctionType>()
3681 ->getNoReturnAttr()) {
3682 Diag(ReturnLoc, diag::err_noreturn_has_return_expr)
3683 << diag::FalloffFunctionKind::Lambda;
3684 return StmtError();
3685 }
3686 }
3687
3688 // Otherwise, verify that this result type matches the previous one. We are
3689 // pickier with blocks than for normal functions because we don't have GCC
3690 // compatibility to worry about here.
3691 if (FnRetType->isDependentType()) {
3692 // Delay processing for now. TODO: there are lots of dependent
3693 // types we can conclusively prove aren't void.
3694 } else if (FnRetType->isVoidType()) {
3695 if (RetValExp && !isa<InitListExpr>(RetValExp) &&
3696 !(getLangOpts().CPlusPlus &&
3697 (RetValExp->isTypeDependent() ||
3698 RetValExp->getType()->isVoidType()))) {
3699 if (!getLangOpts().CPlusPlus &&
3700 RetValExp->getType()->isVoidType())
3701 Diag(ReturnLoc, diag::ext_return_has_void_expr) << "literal" << 2;
3702 else {
3703 Diag(ReturnLoc, diag::err_return_block_has_expr);
3704 RetValExp = nullptr;
3705 }
3706 }
3707 } else if (!RetValExp) {
3708 return StmtError(Diag(ReturnLoc, diag::err_block_return_missing_expr));
3709 } else if (!RetValExp->isTypeDependent()) {
3710 // we have a non-void block with an expression, continue checking
3711
3712 // C99 6.8.6.4p3(136): The return statement is not an assignment. The
3713 // overlap restriction of subclause 6.5.16.1 does not apply to the case of
3714 // function return.
3715
3716 // In C++ the return statement is handled via a copy initialization.
3717 // the C version of which boils down to CheckSingleAssignmentConstraints.
3718 InitializedEntity Entity =
3719 InitializedEntity::InitializeResult(ReturnLoc, FnRetType);
3720 ExprResult Res = PerformMoveOrCopyInitialization(
3721 Entity, NRInfo, RetValExp, SupressSimplerImplicitMoves);
3722 if (Res.isInvalid()) {
3723 // FIXME: Cleanup temporaries here, anyway?
3724 return StmtError();
3725 }
3726 RetValExp = Res.get();
3727 CheckReturnValExpr(RetValExp, FnRetType, ReturnLoc);
3728 }
3729
3730 if (RetValExp) {
3731 ExprResult ER =
3732 ActOnFinishFullExpr(RetValExp, ReturnLoc, /*DiscardedValue*/ false);
3733 if (ER.isInvalid())
3734 return StmtError();
3735 RetValExp = ER.get();
3736 }
3737 auto *Result =
3738 ReturnStmt::Create(Context, ReturnLoc, RetValExp, NRVOCandidate);
3739
3740 // If we need to check for the named return value optimization,
3741 // or if we need to infer the return type,
3742 // save the return statement in our scope for later processing.
3743 if (CurCap->HasImplicitReturnType || NRVOCandidate)
3744 FunctionScopes.back()->Returns.push_back(Result);
3745
3746 if (FunctionScopes.back()->FirstReturnLoc.isInvalid())
3747 FunctionScopes.back()->FirstReturnLoc = ReturnLoc;
3748
3749 if (auto *CurBlock = dyn_cast<BlockScopeInfo>(CurCap);
3750 CurBlock && CurCap->HasImplicitReturnType && RetValExp &&
3751 RetValExp->containsErrors())
3752 CurBlock->TheDecl->setInvalidDecl();
3753
3754 return Result;
3755 }
3756
3757 namespace {
3758 /// Marks all typedefs in all local classes in a type referenced.
3759 ///
3760 /// In a function like
3761 /// auto f() {
3762 /// struct S { typedef int a; };
3763 /// return S();
3764 /// }
3765 ///
3766 /// the local type escapes and could be referenced in some TUs but not in
3767 /// others. Pretend that all local typedefs are always referenced, to not warn
3768 /// on this. This isn't necessary if f has internal linkage, or the typedef
3769 /// is private.
3770 class LocalTypedefNameReferencer : public DynamicRecursiveASTVisitor {
3771 public:
3772 LocalTypedefNameReferencer(Sema &S) : S(S) {}
3773 bool VisitRecordType(RecordType *RT) override;
3774
3775 private:
3776 Sema &S;
3777 };
3778 bool LocalTypedefNameReferencer::VisitRecordType(RecordType *RT) {
3779 auto *R = dyn_cast<CXXRecordDecl>(RT->getDecl());
3780 if (!R || !R->isLocalClass() || !R->isLocalClass()->isExternallyVisible() ||
3781 R->isDependentType())
3782 return true;
3783 for (auto *TmpD : R->decls())
3784 if (auto *T = dyn_cast<TypedefNameDecl>(TmpD))
3785 if (T->getAccess() != AS_private || R->hasFriends())
3786 S.MarkAnyDeclReferenced(T->getLocation(), T, /*OdrUse=*/false);
3787 return true;
3788 }
3789 }
3790
3791 TypeLoc Sema::getReturnTypeLoc(FunctionDecl *FD) const {
3792 return FD->getTypeSourceInfo()
3793 ->getTypeLoc()
3794 .getAsAdjusted<FunctionProtoTypeLoc>()
3795 .getReturnLoc();
3796 }
3797
3798 bool Sema::DeduceFunctionTypeFromReturnExpr(FunctionDecl *FD,
3799 SourceLocation ReturnLoc,
3800 Expr *RetExpr, const AutoType *AT) {
3801 // If this is the conversion function for a lambda, we choose to deduce its
3802 // type from the corresponding call operator, not from the synthesized return
3803 // statement within it. See Sema::DeduceReturnType.
3804 if (isLambdaConversionOperator(FD))
3805 return false;
3806
3807 if (isa_and_nonnull<InitListExpr>(RetExpr)) {
3808 // If the deduction is for a return statement and the initializer is
3809 // a braced-init-list, the program is ill-formed.
3810 Diag(RetExpr->getExprLoc(),
3811 getCurLambda() ? diag::err_lambda_return_init_list
3812 : diag::err_auto_fn_return_init_list)
3813 << RetExpr->getSourceRange();
3814 return true;
3815 }
3816
3817 if (FD->isDependentContext()) {
3818 // C++1y [dcl.spec.auto]p12:
3819 // Return type deduction [...] occurs when the definition is
3820 // instantiated even if the function body contains a return
3821 // statement with a non-type-dependent operand.
3822 assert(AT->isDeduced() && "should have deduced to dependent type");
3823 return false;
3824 }
3825
3826 TypeLoc OrigResultType = getReturnTypeLoc(FD);
3827 // In the case of a return with no operand, the initializer is considered
3828 // to be void().
3829 CXXScalarValueInitExpr VoidVal(Context.VoidTy, nullptr, SourceLocation());
3830 if (!RetExpr) {
3831 // For a function with a deduced result type to return with omitted
3832 // expression, the result type as written must be 'auto' or
3833 // 'decltype(auto)', possibly cv-qualified or constrained, but not
3834 // ref-qualified.
3835 if (!OrigResultType.getType()->getAs<AutoType>()) {
3836 Diag(ReturnLoc, diag::err_auto_fn_return_void_but_not_auto)
3837 << OrigResultType.getType();
3838 return true;
3839 }
3840 RetExpr = &VoidVal;
3841 }
3842
3843 QualType Deduced = AT->getDeducedType();
3844 {
3845 // Otherwise, [...] deduce a value for U using the rules of template
3846 // argument deduction.
3847 auto RetExprLoc = RetExpr->getExprLoc();
3848 TemplateDeductionInfo Info(RetExprLoc);
3849 SourceLocation TemplateSpecLoc;
3850 if (RetExpr->getType() == Context.OverloadTy) {
3851 auto FindResult = OverloadExpr::find(RetExpr);
3852 if (FindResult.Expression)
3853 TemplateSpecLoc = FindResult.Expression->getNameLoc();
3854 }
3855 TemplateSpecCandidateSet FailedTSC(TemplateSpecLoc);
3856 TemplateDeductionResult Res = DeduceAutoType(
3857 OrigResultType, RetExpr, Deduced, Info, /*DependentDeduction=*/false,
3858 /*IgnoreConstraints=*/false, &FailedTSC);
3859 if (Res != TemplateDeductionResult::Success && FD->isInvalidDecl())
3860 return true;
3861 switch (Res) {
3862 case TemplateDeductionResult::Success:
3863 break;
3864 case TemplateDeductionResult::AlreadyDiagnosed:
3865 return true;
3866 case TemplateDeductionResult::Inconsistent: {
3867 // If a function with a declared return type that contains a placeholder
3868 // type has multiple return statements, the return type is deduced for
3869 // each return statement. [...] if the type deduced is not the same in
3870 // each deduction, the program is ill-formed.
3871 const LambdaScopeInfo *LambdaSI = getCurLambda();
3872 if (LambdaSI && LambdaSI->HasImplicitReturnType)
3873 Diag(ReturnLoc, diag::err_typecheck_missing_return_type_incompatible)
3874 << Info.SecondArg << Info.FirstArg << true /*IsLambda*/;
3875 else
3876 Diag(ReturnLoc, diag::err_auto_fn_different_deductions)
3877 << (AT->isDecltypeAuto() ? 1 : 0) << Info.SecondArg
3878 << Info.FirstArg;
3879 return true;
3880 }
3881 default:
3882 Diag(RetExpr->getExprLoc(), diag::err_auto_fn_deduction_failure)
3883 << OrigResultType.getType() << RetExpr->getType();
3884 FailedTSC.NoteCandidates(*this, RetExprLoc);
3885 return true;
3886 }
3887 }
3888
3889 // If a local type is part of the returned type, mark its fields as
3890 // referenced.
3891 LocalTypedefNameReferencer(*this).TraverseType(RetExpr->getType());
3892
3893 // CUDA: Kernel function must have 'void' return type.
3894 if (getLangOpts().CUDA && FD->hasAttr<CUDAGlobalAttr>() &&
3895 !Deduced->isVoidType()) {
3896 Diag(FD->getLocation(), diag::err_kern_type_not_void_return)
3897 << FD->getType() << FD->getSourceRange();
3898 return true;
3899 }
3900
3901 if (!FD->isInvalidDecl() && AT->getDeducedType() != Deduced)
3902 // Update all declarations of the function to have the deduced return type.
3903 Context.adjustDeducedFunctionResultType(FD, Deduced);
3904
3905 if (!Deduced->isDependentType() && !Deduced->isRecordType() &&
3906 !FD->isFunctionTemplateSpecialization())
3907 diagnoseIgnoredQualifiers(
3908 diag::warn_qual_return_type,
3909 FD->getDeclaredReturnType().getLocalCVRQualifiers(), FD->getLocation());
3910 return false;
3911 }
3912
3913 StmtResult
3914 Sema::ActOnReturnStmt(SourceLocation ReturnLoc, Expr *RetValExp,
3915 Scope *CurScope) {
3916 ExprResult RetVal = RetValExp;
3917 if (RetVal.isInvalid())
3918 return StmtError();
3919
3920 if (getCurScope()->isInOpenACCComputeConstructScope())
3921 return StmtError(
3922 Diag(ReturnLoc, diag::err_acc_branch_in_out_compute_construct)
3923 << /*return*/ 1 << /*out of */ 0);
3924
3925 // using plain return in a coroutine is not allowed.
3926 FunctionScopeInfo *FSI = getCurFunction();
3927 if (FSI->FirstReturnLoc.isInvalid() && FSI->isCoroutine()) {
3928 assert(FSI->FirstCoroutineStmtLoc.isValid() &&
3929 "first coroutine location not set");
3930 Diag(ReturnLoc, diag::err_return_in_coroutine);
3931 Diag(FSI->FirstCoroutineStmtLoc, diag::note_declared_coroutine_here)
3932 << FSI->getFirstCoroutineStmtKeyword();
3933 }
3934
3935 CheckInvalidBuiltinCountedByRef(RetVal.get(),
3936 BuiltinCountedByRefKind::ReturnArg);
3937
3938 StmtResult R =
3939 BuildReturnStmt(ReturnLoc, RetVal.get(), /*AllowRecovery=*/true);
3940 if (R.isInvalid() || ExprEvalContexts.back().isDiscardedStatementContext())
3941 return R;
3942
3943 VarDecl *VD =
3944 const_cast<VarDecl *>(cast<ReturnStmt>(R.get())->getNRVOCandidate());
3945
3946 CurScope->updateNRVOCandidate(VD);
3947
3948 CheckJumpOutOfSEHFinallyOrDefer(*this, ReturnLoc, *CurScope->getFnParent(),
3949 diag::DeferJumpKind::Return);
3950
3951 return R;
3952 }
3953
3954 void Sema::ActOnStartOfDeferStmt(SourceLocation DeferLoc, Scope *CurScope) {
3955 CurrentDefer.emplace_back(CurScope, DeferLoc);
3956 }
3957
3958 void Sema::ActOnDeferStmtError([[maybe_unused]] Scope *CurScope) {
3959 assert(!CurrentDefer.empty() && CurrentDefer.back().first == CurScope);
3960 CurrentDefer.pop_back();
3961 }
3962
3963 StmtResult Sema::ActOnEndOfDeferStmt(Stmt *Body,
3964 [[maybe_unused]] Scope *CurScope) {
3965 assert(!CurrentDefer.empty() && CurrentDefer.back().first == CurScope);
3966 SourceLocation DeferLoc = CurrentDefer.pop_back_val().second;
3967 DiagnoseEmptyStmtBody(DeferLoc, Body, diag::warn_empty_defer_body);
3968 setFunctionHasBranchProtectedScope();
3969 return DeferStmt::Create(Context, DeferLoc, Body);
3970 }
3971
3972 static bool CheckSimplerImplicitMovesMSVCWorkaround(const Sema &S,
3973 const Expr *E) {
3974 if (!E || !S.getLangOpts().CPlusPlus23 || !S.getLangOpts().MSVCCompat)
3975 return false;
3976 const Decl *D = E->getReferencedDeclOfCallee();
3977 if (!D || !S.SourceMgr.isInSystemHeader(D->getLocation()))
3978 return false;
3979 for (const DeclContext *DC = D->getDeclContext(); DC; DC = DC->getParent()) {
3980 if (DC->isStdNamespace())
3981 return true;
3982 }
3983 return false;
3984 }
3985
3986 StmtResult Sema::BuildReturnStmt(SourceLocation ReturnLoc, Expr *RetValExp,
3987 bool AllowRecovery) {
3988 // Check for unexpanded parameter packs.
3989 if (RetValExp && DiagnoseUnexpandedParameterPack(RetValExp))
3990 return StmtError();
3991
3992 // HACK: We suppress simpler implicit move here in msvc compatibility mode
3993 // just as a temporary work around, as the MSVC STL has issues with
3994 // this change.
3995 bool SupressSimplerImplicitMoves =
3996 CheckSimplerImplicitMovesMSVCWorkaround(*this, RetValExp);
3997 NamedReturnInfo NRInfo = getNamedReturnInfo(
3998 RetValExp, SupressSimplerImplicitMoves ? SimplerImplicitMoveMode::ForceOff
3999 : SimplerImplicitMoveMode::Normal);
4000
4001 if (isa<CapturingScopeInfo>(getCurFunction()))
4002 return ActOnCapScopeReturnStmt(ReturnLoc, RetValExp, NRInfo,
4003 SupressSimplerImplicitMoves);
4004
4005 QualType FnRetType;
4006 QualType RelatedRetType;
4007 const AttrVec *Attrs = nullptr;
4008 bool isObjCMethod = false;
4009
4010 if (const FunctionDecl *FD = getCurFunctionDecl()) {
4011 FnRetType = FD->getReturnType();
4012 if (FD->hasAttrs())
4013 Attrs = &FD->getAttrs();
4014 if (FD->isNoReturn() && !getCurFunction()->isCoroutine())
4015 Diag(ReturnLoc, diag::warn_noreturn_function_has_return_expr) << FD;
4016 if (FD->isMain() && RetValExp)
4017 if (isa<CXXBoolLiteralExpr>(RetValExp))
4018 Diag(ReturnLoc, diag::warn_main_returns_bool_literal)
4019 << RetValExp->getSourceRange();
4020 if (FD->hasAttr<CmseNSEntryAttr>() && RetValExp) {
4021 if (const auto *RT = dyn_cast<RecordType>(FnRetType.getCanonicalType())) {
4022 if (RT->getDecl()->isOrContainsUnion())
4023 Diag(RetValExp->getBeginLoc(), diag::warn_cmse_nonsecure_union) << 1;
4024 }
4025 }
4026 } else if (ObjCMethodDecl *MD = getCurMethodDecl()) {
4027 FnRetType = MD->getReturnType();
4028 isObjCMethod = true;
4029 if (MD->hasAttrs())
4030 Attrs = &MD->getAttrs();
4031 if (MD->hasRelatedResultType() && MD->getClassInterface()) {
4032 // In the implementation of a method with a related return type, the
4033 // type used to type-check the validity of return statements within the
4034 // method body is a pointer to the type of the class being implemented.
4035 RelatedRetType = Context.getObjCInterfaceType(MD->getClassInterface());
4036 RelatedRetType = Context.getObjCObjectPointerType(RelatedRetType);
4037 }
4038 } else // If we don't have a function/method context, bail.
4039 return StmtError();
4040
4041 if (RetValExp) {
4042 const auto *ATy = dyn_cast<ArrayType>(RetValExp->getType());
4043 if (ATy && ATy->getElementType().isWebAssemblyReferenceType()) {
4044 Diag(ReturnLoc, diag::err_wasm_table_art) << 1;
4045 return StmtError();
4046 }
4047 }
4048
4049 // C++1z: discarded return statements are not considered when deducing a
4050 // return type.
4051 if (ExprEvalContexts.back().isDiscardedStatementContext() &&
4052 FnRetType->getContainedAutoType()) {
4053 if (RetValExp) {
4054 ExprResult ER =
4055 ActOnFinishFullExpr(RetValExp, ReturnLoc, /*DiscardedValue*/ false);
4056 if (ER.isInvalid())
4057 return StmtError();
4058 RetValExp = ER.get();
4059 }
4060 return ReturnStmt::Create(Context, ReturnLoc, RetValExp,
4061 /* NRVOCandidate=*/nullptr);
4062 }
4063
4064 // FIXME: Add a flag to the ScopeInfo to indicate whether we're performing
4065 // deduction.
4066 if (getLangOpts().CPlusPlus14) {
4067 if (AutoType *AT = FnRetType->getContainedAutoType()) {
4068 FunctionDecl *FD = cast<FunctionDecl>(CurContext);
4069 // If we've already decided this function is invalid, e.g. because
4070 // we saw a `return` whose expression had an error, don't keep
4071 // trying to deduce its return type.
4072 // (Some return values may be needlessly wrapped in RecoveryExpr).
4073 if (FD->isInvalidDecl() ||
4074 DeduceFunctionTypeFromReturnExpr(FD, ReturnLoc, RetValExp, AT)) {
4075 FD->setInvalidDecl();
4076 if (!AllowRecovery)
4077 return StmtError();
4078 // The deduction failure is diagnosed and marked, try to recover.
4079 if (RetValExp) {
4080 // Wrap return value with a recovery expression of the previous type.
4081 // If no deduction yet, use DependentTy.
4082 auto Recovery = CreateRecoveryExpr(
4083 RetValExp->getBeginLoc(), RetValExp->getEndLoc(), RetValExp,
4084 AT->isDeduced() ? FnRetType : QualType());
4085 if (Recovery.isInvalid())
4086 return StmtError();
4087 RetValExp = Recovery.get();
4088 } else {
4089 // Nothing to do: a ReturnStmt with no value is fine recovery.
4090 }
4091 } else {
4092 FnRetType = FD->getReturnType();
4093 }
4094 }
4095 }
4096 const VarDecl *NRVOCandidate = getCopyElisionCandidate(NRInfo, FnRetType);
4097
4098 bool HasDependentReturnType = FnRetType->isDependentType();
4099
4100 ReturnStmt *Result = nullptr;
4101 if (FnRetType->isVoidType()) {
4102 if (RetValExp) {
4103 if (auto *ILE = dyn_cast<InitListExpr>(RetValExp)) {
4104 // We simply never allow init lists as the return value of void
4105 // functions. This is compatible because this was never allowed before,
4106 // so there's no legacy code to deal with.
4107 NamedDecl *CurDecl = getCurFunctionOrMethodDecl();
4108 int FunctionKind = 0;
4109 if (isa<ObjCMethodDecl>(CurDecl))
4110 FunctionKind = 1;
4111 else if (isa<CXXConstructorDecl>(CurDecl))
4112 FunctionKind = 2;
4113 else if (isa<CXXDestructorDecl>(CurDecl))
4114 FunctionKind = 3;
4115
4116 Diag(ReturnLoc, diag::err_return_init_list)
4117 << CurDecl << FunctionKind << RetValExp->getSourceRange();
4118
4119 // Preserve the initializers in the AST.
4120 RetValExp = AllowRecovery
4121 ? CreateRecoveryExpr(ILE->getLBraceLoc(),
4122 ILE->getRBraceLoc(), ILE->inits())
4123 .get()
4124 : nullptr;
4125 } else if (!RetValExp->isTypeDependent()) {
4126 // C99 6.8.6.4p1 (ext_ since GCC warns)
4127 unsigned D = diag::ext_return_has_expr;
4128 if (RetValExp->getType()->isVoidType()) {
4129 NamedDecl *CurDecl = getCurFunctionOrMethodDecl();
4130 if (isa<CXXConstructorDecl>(CurDecl) ||
4131 isa<CXXDestructorDecl>(CurDecl))
4132 D = diag::err_ctor_dtor_returns_void;
4133 else
4134 D = diag::ext_return_has_void_expr;
4135 }
4136 else {
4137 ExprResult Result = RetValExp;
4138 Result = IgnoredValueConversions(Result.get());
4139 if (Result.isInvalid())
4140 return StmtError();
4141 RetValExp = Result.get();
4142 RetValExp = ImpCastExprToType(RetValExp,
4143 Context.VoidTy, CK_ToVoid).get();
4144 }
4145 // return of void in constructor/destructor is illegal in C++.
4146 if (D == diag::err_ctor_dtor_returns_void) {
4147 NamedDecl *CurDecl = getCurFunctionOrMethodDecl();
4148 Diag(ReturnLoc, D) << CurDecl << isa<CXXDestructorDecl>(CurDecl)
4149 << RetValExp->getSourceRange();
4150 }
4151 // return (some void expression); is legal in C++ and C2y.
4152 else if (D != diag::ext_return_has_void_expr ||
4153 (!getLangOpts().CPlusPlus && !getLangOpts().C2y)) {
4154 NamedDecl *CurDecl = getCurFunctionOrMethodDecl();
4155
4156 int FunctionKind = 0;
4157 if (isa<ObjCMethodDecl>(CurDecl))
4158 FunctionKind = 1;
4159 else if (isa<CXXConstructorDecl>(CurDecl))
4160 FunctionKind = 2;
4161 else if (isa<CXXDestructorDecl>(CurDecl))
4162 FunctionKind = 3;
4163
4164 Diag(ReturnLoc, D)
4165 << CurDecl << FunctionKind << RetValExp->getSourceRange();
4166 }
4167 }
4168
4169 if (RetValExp) {
4170 ExprResult ER =
4171 ActOnFinishFullExpr(RetValExp, ReturnLoc, /*DiscardedValue*/ false);
4172 if (ER.isInvalid())
4173 return StmtError();
4174 RetValExp = ER.get();
4175 }
4176 }
4177
4178 Result = ReturnStmt::Create(Context, ReturnLoc, RetValExp,
4179 /* NRVOCandidate=*/nullptr);
4180 } else if (!RetValExp && !HasDependentReturnType) {
4181 FunctionDecl *FD = getCurFunctionDecl();
4182
4183 if ((FD && FD->isInvalidDecl()) || FnRetType->containsErrors()) {
4184 // The intended return type might have been "void", so don't warn.
4185 } else if (getLangOpts().CPlusPlus11 && FD && FD->isConstexpr()) {
4186 // C++11 [stmt.return]p2
4187 Diag(ReturnLoc, diag::err_constexpr_return_missing_expr)
4188 << FD << FD->isConsteval();
4189 FD->setInvalidDecl();
4190 } else {
4191 // C99 6.8.6.4p1 (ext_ since GCC warns)
4192 // C90 6.6.6.4p4
4193 unsigned DiagID = getLangOpts().C99 ? diag::ext_return_missing_expr
4194 : diag::warn_return_missing_expr;
4195 // Note that at this point one of getCurFunctionDecl() or
4196 // getCurMethodDecl() must be non-null (see above).
4197 assert((getCurFunctionDecl() || getCurMethodDecl()) &&
4198 "Not in a FunctionDecl or ObjCMethodDecl?");
4199 bool IsMethod = FD == nullptr;
4200 const NamedDecl *ND =
4201 IsMethod ? cast<NamedDecl>(getCurMethodDecl()) : cast<NamedDecl>(FD);
4202 Diag(ReturnLoc, DiagID) << ND << IsMethod;
4203 }
4204
4205 Result = ReturnStmt::Create(Context, ReturnLoc, /* RetExpr=*/nullptr,
4206 /* NRVOCandidate=*/nullptr);
4207 } else {
4208 assert(RetValExp || HasDependentReturnType);
4209 QualType RetType = RelatedRetType.isNull() ? FnRetType : RelatedRetType;
4210
4211 // C99 6.8.6.4p3(136): The return statement is not an assignment. The
4212 // overlap restriction of subclause 6.5.16.1 does not apply to the case of
4213 // function return.
4214
4215 // In C++ the return statement is handled via a copy initialization,
4216 // the C version of which boils down to CheckSingleAssignmentConstraints.
4217 if (!HasDependentReturnType && !RetValExp->isTypeDependent()) {
4218 // we have a non-void function with an expression, continue checking
4219 InitializedEntity Entity =
4220 InitializedEntity::InitializeResult(ReturnLoc, RetType);
4221 ExprResult Res = PerformMoveOrCopyInitialization(
4222 Entity, NRInfo, RetValExp, SupressSimplerImplicitMoves);
4223 if (Res.isInvalid() && AllowRecovery)
4224 Res = CreateRecoveryExpr(RetValExp->getBeginLoc(),
4225 RetValExp->getEndLoc(), RetValExp, RetType);
4226 if (Res.isInvalid()) {
4227 // FIXME: Clean up temporaries here anyway?
4228 return StmtError();
4229 }
4230 RetValExp = Res.getAs<Expr>();
4231
4232 // If we have a related result type, we need to implicitly
4233 // convert back to the formal result type. We can't pretend to
4234 // initialize the result again --- we might end double-retaining
4235 // --- so instead we initialize a notional temporary.
4236 if (!RelatedRetType.isNull()) {
4237 Entity = InitializedEntity::InitializeRelatedResult(getCurMethodDecl(),
4238 FnRetType);
4239 Res = PerformCopyInitialization(Entity, ReturnLoc, RetValExp);
4240 if (Res.isInvalid()) {
4241 // FIXME: Clean up temporaries here anyway?
4242 return StmtError();
4243 }
4244 RetValExp = Res.getAs<Expr>();
4245 }
4246
4247 CheckReturnValExpr(RetValExp, FnRetType, ReturnLoc, isObjCMethod, Attrs,
4248 getCurFunctionDecl());
4249 }
4250
4251 if (RetValExp) {
4252 ExprResult ER =
4253 ActOnFinishFullExpr(RetValExp, ReturnLoc, /*DiscardedValue*/ false);
4254 if (ER.isInvalid())
4255 return StmtError();
4256 RetValExp = ER.get();
4257 }
4258 Result = ReturnStmt::Create(Context, ReturnLoc, RetValExp, NRVOCandidate);
4259 }
4260
4261 // If we need to check for the named return value optimization, save the
4262 // return statement in our scope for later processing.
4263 if (Result->getNRVOCandidate())
4264 FunctionScopes.back()->Returns.push_back(Result);
4265
4266 if (FunctionScopes.back()->FirstReturnLoc.isInvalid())
4267 FunctionScopes.back()->FirstReturnLoc = ReturnLoc;
4268
4269 return Result;
4270 }
4271
4272 StmtResult
4273 Sema::ActOnCXXCatchBlock(SourceLocation CatchLoc, Decl *ExDecl,
4274 Stmt *HandlerBlock) {
4275 // There's nothing to test that ActOnExceptionDecl didn't already test.
4276 return new (Context)
4277 CXXCatchStmt(CatchLoc, cast_or_null<VarDecl>(ExDecl), HandlerBlock);
4278 }
4279
4280 namespace {
4281 class CatchHandlerType {
4282 QualType QT;
4283 LLVM_PREFERRED_TYPE(bool)
4284 unsigned IsPointer : 1;
4285
4286 // This is a special constructor to be used only with DenseMapInfo's
4287 // getEmptyKey() and getTombstoneKey() functions.
4288 friend struct llvm::DenseMapInfo<CatchHandlerType>;
4289 enum Unique { ForDenseMap };
4290 CatchHandlerType(QualType QT, Unique) : QT(QT), IsPointer(false) {}
4291
4292 public:
4293 /// Used when creating a CatchHandlerType from a handler type; will determine
4294 /// whether the type is a pointer or reference and will strip off the top
4295 /// level pointer and cv-qualifiers.
4296 CatchHandlerType(QualType Q) : QT(Q), IsPointer(false) {
4297 if (QT->isPointerType())
4298 IsPointer = true;
4299
4300 QT = QT.getUnqualifiedType();
4301 if (IsPointer || QT->isReferenceType())
4302 QT = QT->getPointeeType();
4303 }
4304
4305 /// Used when creating a CatchHandlerType from a base class type; pretends the
4306 /// type passed in had the pointer qualifier, does not need to get an
4307 /// unqualified type.
4308 CatchHandlerType(QualType QT, bool IsPointer)
4309 : QT(QT), IsPointer(IsPointer) {}
4310
4311 QualType underlying() const { return QT; }
4312 bool isPointer() const { return IsPointer; }
4313
4314 friend bool operator==(const CatchHandlerType &LHS,
4315 const CatchHandlerType &RHS) {
4316 // If the pointer qualification does not match, we can return early.
4317 if (LHS.IsPointer != RHS.IsPointer)
4318 return false;
4319 // Otherwise, check the underlying type without cv-qualifiers.
4320 return LHS.QT == RHS.QT;
4321 }
4322 };
4323 } // namespace
4324
4325 namespace llvm {
4326 template <> struct DenseMapInfo<CatchHandlerType> {
4327 static CatchHandlerType getEmptyKey() {
4328 return CatchHandlerType(DenseMapInfo<QualType>::getEmptyKey(),
4329 CatchHandlerType::ForDenseMap);
4330 }
4331
4332 static CatchHandlerType getTombstoneKey() {
4333 return CatchHandlerType(DenseMapInfo<QualType>::getTombstoneKey(),
4334 CatchHandlerType::ForDenseMap);
4335 }
4336
4337 static unsigned getHashValue(const CatchHandlerType &Base) {
4338 return DenseMapInfo<QualType>::getHashValue(Base.underlying());
4339 }
4340
4341 static bool isEqual(const CatchHandlerType &LHS,
4342 const CatchHandlerType &RHS) {
4343 return LHS == RHS;
4344 }
4345 };
4346 }
4347
4348 namespace {
4349 class CatchTypePublicBases {
4350 const llvm::DenseMap<QualType, CXXCatchStmt *> &TypesToCheck;
4351
4352 CXXCatchStmt *FoundHandler;
4353 QualType FoundHandlerType;
4354 QualType TestAgainstType;
4355
4356 public:
4357 CatchTypePublicBases(const llvm::DenseMap<QualType, CXXCatchStmt *> &T,
4358 QualType QT)
4359 : TypesToCheck(T), FoundHandler(nullptr), TestAgainstType(QT) {}
4360
4361 CXXCatchStmt *getFoundHandler() const { return FoundHandler; }
4362 QualType getFoundHandlerType() const { return FoundHandlerType; }
4363
4364 bool operator()(const CXXBaseSpecifier *S, CXXBasePath &) {
4365 if (S->getAccessSpecifier() == AccessSpecifier::AS_public) {
4366 QualType Check = S->getType().getCanonicalType();
4367 const auto &M = TypesToCheck;
4368 auto I = M.find(Check);
4369 if (I != M.end()) {
4370 // We're pretty sure we found what we need to find. However, we still
4371 // need to make sure that we properly compare for pointers and
4372 // references, to handle cases like:
4373 //
4374 // } catch (Base *b) {
4375 // } catch (Derived &d) {
4376 // }
4377 //
4378 // where there is a qualification mismatch that disqualifies this
4379 // handler as a potential problem.
4380 if (I->second->getCaughtType()->isPointerType() ==
4381 TestAgainstType->isPointerType()) {
4382 FoundHandler = I->second;
4383 FoundHandlerType = Check;
4384 return true;
4385 }
4386 }
4387 }
4388 return false;
4389 }
4390 };
4391 }
4392
4393 StmtResult Sema::ActOnCXXTryBlock(SourceLocation TryLoc, Stmt *TryBlock,
4394 ArrayRef<Stmt *> Handlers) {
4395 const llvm::Triple &T = Context.getTargetInfo().getTriple();
4396 const bool IsOpenMPGPUTarget =
4397 getLangOpts().OpenMPIsTargetDevice && T.isGPU();
4398
4399 DiagnoseExceptionUse(TryLoc, /* IsTry= */ true);
4400
4401 // In OpenMP target regions, we assume that catch is never reached on GPU
4402 // targets.
4403 if (IsOpenMPGPUTarget)
4404 targetDiag(TryLoc, diag::warn_try_not_valid_on_target) << T.str();
4405
4406 // Exceptions aren't allowed in CUDA device code.
4407 if (getLangOpts().CUDA)
4408 CUDA().DiagIfDeviceCode(TryLoc, diag::err_cuda_device_exceptions)
4409 << "try" << CUDA().CurrentTarget();
4410
4411 if (getCurScope() && getCurScope()->isOpenMPSimdDirectiveScope())
4412 Diag(TryLoc, diag::err_omp_simd_region_cannot_use_stmt) << "try";
4413
4414 sema::FunctionScopeInfo *FSI = getCurFunction();
4415
4416 // C++ try is incompatible with SEH __try.
4417 if (!getLangOpts().Borland && FSI->FirstSEHTryLoc.isValid()) {
4418 Diag(TryLoc, diag::err_mixing_cxx_try_seh_try) << 0;
4419 Diag(FSI->FirstSEHTryLoc, diag::note_conflicting_try_here) << "'__try'";
4420 }
4421
4422 const unsigned NumHandlers = Handlers.size();
4423 assert(!Handlers.empty() &&
4424 "The parser shouldn't call this if there are no handlers.");
4425
4426 llvm::DenseMap<QualType, CXXCatchStmt *> HandledBaseTypes;
4427 llvm::DenseMap<CatchHandlerType, CXXCatchStmt *> HandledTypes;
4428 for (unsigned i = 0; i < NumHandlers; ++i) {
4429 CXXCatchStmt *H = cast<CXXCatchStmt>(Handlers[i]);
4430
4431 // Diagnose when the handler is a catch-all handler, but it isn't the last
4432 // handler for the try block. [except.handle]p5. Also, skip exception
4433 // declarations that are invalid, since we can't usefully report on them.
4434 if (!H->getExceptionDecl()) {
4435 if (i < NumHandlers - 1)
4436 return StmtError(Diag(H->getBeginLoc(), diag::err_early_catch_all));
4437 continue;
4438 } else if (H->getExceptionDecl()->isInvalidDecl())
4439 continue;
4440
4441 // Walk the type hierarchy to diagnose when this type has already been
4442 // handled (duplication), or cannot be handled (derivation inversion). We
4443 // ignore top-level cv-qualifiers, per [except.handle]p3
4444 CatchHandlerType HandlerCHT = H->getCaughtType().getCanonicalType();
4445
4446 // We can ignore whether the type is a reference or a pointer; we need the
4447 // underlying declaration type in order to get at the underlying record
4448 // decl, if there is one.
4449 QualType Underlying = HandlerCHT.underlying();
4450 if (auto *RD = Underlying->getAsCXXRecordDecl()) {
4451 if (!RD->hasDefinition())
4452 continue;
4453 // Check that none of the public, unambiguous base classes are in the
4454 // map ([except.handle]p1). Give the base classes the same pointer
4455 // qualification as the original type we are basing off of. This allows
4456 // comparison against the handler type using the same top-level pointer
4457 // as the original type.
4458 CXXBasePaths Paths;
4459 Paths.setOrigin(RD);
4460 CatchTypePublicBases CTPB(HandledBaseTypes,
4461 H->getCaughtType().getCanonicalType());
4462 if (RD->lookupInBases(CTPB, Paths)) {
4463 const CXXCatchStmt *Problem = CTPB.getFoundHandler();
4464 if (!Paths.isAmbiguous(
4465 CanQualType::CreateUnsafe(CTPB.getFoundHandlerType()))) {
4466 Diag(H->getExceptionDecl()->getTypeSpecStartLoc(),
4467 diag::warn_exception_caught_by_earlier_handler)
4468 << H->getCaughtType();
4469 Diag(Problem->getExceptionDecl()->getTypeSpecStartLoc(),
4470 diag::note_previous_exception_handler)
4471 << Problem->getCaughtType();
4472 }
4473 }
4474 // Strip the qualifiers here because we're going to be comparing this
4475 // type to the base type specifiers of a class, which are ignored in a
4476 // base specifier per [class.derived.general]p2.
4477 HandledBaseTypes[Underlying.getUnqualifiedType()] = H;
4478 }
4479
4480 // Add the type the list of ones we have handled; diagnose if we've already
4481 // handled it.
4482 auto R = HandledTypes.insert(
4483 std::make_pair(H->getCaughtType().getCanonicalType(), H));
4484 if (!R.second) {
4485 const CXXCatchStmt *Problem = R.first->second;
4486 Diag(H->getExceptionDecl()->getTypeSpecStartLoc(),
4487 diag::warn_exception_caught_by_earlier_handler)
4488 << H->getCaughtType();
4489 Diag(Problem->getExceptionDecl()->getTypeSpecStartLoc(),
4490 diag::note_previous_exception_handler)
4491 << Problem->getCaughtType();
4492 }
4493 }
4494
4495 FSI->setHasCXXTry(TryLoc);
4496
4497 return CXXTryStmt::Create(Context, TryLoc, cast<CompoundStmt>(TryBlock),
4498 Handlers);
4499 }
4500
4501 void Sema::DiagnoseExceptionUse(SourceLocation Loc, bool IsTry) {
4502 const llvm::Triple &T = Context.getTargetInfo().getTriple();
4503 const bool IsOpenMPGPUTarget =
4504 getLangOpts().OpenMPIsTargetDevice && T.isGPU();
4505
4506 // Don't report an error if 'try' is used in system headers or in an OpenMP
4507 // target region compiled for a GPU architecture.
4508 if (IsOpenMPGPUTarget || getLangOpts().CUDA)
4509 // Delay error emission for the OpenMP device code.
4510 return;
4511
4512 if (!getLangOpts().CXXExceptions &&
4513 !getSourceManager().isInSystemHeader(Loc) &&
4514 !CurContext->isDependentContext())
4515 targetDiag(Loc, diag::err_exceptions_disabled) << (IsTry ? "try" : "throw");
4516 }
4517
4518 StmtResult Sema::ActOnSEHTryBlock(bool IsCXXTry, SourceLocation TryLoc,
4519 Stmt *TryBlock, Stmt *Handler) {
4520 assert(TryBlock && Handler);
4521
4522 sema::FunctionScopeInfo *FSI = getCurFunction();
4523
4524 // SEH __try is incompatible with C++ try. Borland appears to support this,
4525 // however.
4526 if (!getLangOpts().Borland) {
4527 if (FSI->FirstCXXOrObjCTryLoc.isValid()) {
4528 Diag(TryLoc, diag::err_mixing_cxx_try_seh_try) << FSI->FirstTryType;
4529 Diag(FSI->FirstCXXOrObjCTryLoc, diag::note_conflicting_try_here)
4530 << (FSI->FirstTryType == sema::FunctionScopeInfo::TryLocIsCXX
4531 ? "'try'"
4532 : "'@try'");
4533 }
4534 }
4535
4536 FSI->setHasSEHTry(TryLoc);
4537
4538 // Reject __try in Obj-C methods, blocks, and captured decls, since we don't
4539 // track if they use SEH.
4540 DeclContext *DC = CurContext;
4541 while (DC && !DC->isFunctionOrMethod())
4542 DC = DC->getParent();
4543 FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(DC);
4544 if (FD)
4545 FD->setUsesSEHTry(true);
4546 else
4547 Diag(TryLoc, diag::err_seh_try_outside_functions);
4548
4549 // Reject __try on unsupported targets.
4550 if (!Context.getTargetInfo().isSEHTrySupported())
4551 Diag(TryLoc, diag::err_seh_try_unsupported);
4552
4553 return SEHTryStmt::Create(Context, IsCXXTry, TryLoc, TryBlock, Handler);
4554 }
4555
4556 StmtResult Sema::ActOnSEHExceptBlock(SourceLocation Loc, Expr *FilterExpr,
4557 Stmt *Block) {
4558 assert(FilterExpr && Block);
4559 QualType FTy = FilterExpr->getType();
4560 if (!FTy->isIntegerType() && !FTy->isDependentType()) {
4561 return StmtError(
4562 Diag(FilterExpr->getExprLoc(), diag::err_filter_expression_integral)
4563 << FTy);
4564 }
4565 return SEHExceptStmt::Create(Context, Loc, FilterExpr, Block);
4566 }
4567
4568 void Sema::ActOnStartSEHFinallyBlock() {
4569 CurrentSEHFinally.push_back(CurScope);
4570 }
4571
4572 void Sema::ActOnAbortSEHFinallyBlock() {
4573 CurrentSEHFinally.pop_back();
4574 }
4575
4576 StmtResult Sema::ActOnFinishSEHFinallyBlock(SourceLocation Loc, Stmt *Block) {
4577 assert(Block);
4578 CurrentSEHFinally.pop_back();
4579 return SEHFinallyStmt::Create(Context, Loc, Block);
4580 }
4581
4582 StmtResult
4583 Sema::ActOnSEHLeaveStmt(SourceLocation Loc, Scope *CurScope) {
4584 Scope *SEHTryParent = CurScope;
4585 while (SEHTryParent && !SEHTryParent->isSEHTryScope())
4586 SEHTryParent = SEHTryParent->getParent();
4587 if (!SEHTryParent)
4588 return StmtError(Diag(Loc, diag::err_ms___leave_not_in___try));
4589 CheckJumpOutOfSEHFinallyOrDefer(*this, Loc, *SEHTryParent,
4590 diag::DeferJumpKind::SEHLeave);
4591
4592 return new (Context) SEHLeaveStmt(Loc);
4593 }
4594
4595 StmtResult Sema::BuildMSDependentExistsStmt(SourceLocation KeywordLoc,
4596 bool IsIfExists,
4597 NestedNameSpecifierLoc QualifierLoc,
4598 DeclarationNameInfo NameInfo,
4599 Stmt *Nested)
4600 {
4601 return new (Context) MSDependentExistsStmt(KeywordLoc, IsIfExists,
4602 QualifierLoc, NameInfo,
4603 cast<CompoundStmt>(Nested));
4604 }
4605
4606
4607 StmtResult Sema::ActOnMSDependentExistsStmt(SourceLocation KeywordLoc,
4608 bool IsIfExists,
4609 CXXScopeSpec &SS,
4610 UnqualifiedId &Name,
4611 Stmt *Nested) {
4612 return BuildMSDependentExistsStmt(KeywordLoc, IsIfExists,
4613 SS.getWithLocInContext(Context),
4614 GetNameFromUnqualifiedId(Name),
4615 Nested);
4616 }
4617
4618 RecordDecl*
4619 Sema::CreateCapturedStmtRecordDecl(CapturedDecl *&CD, SourceLocation Loc,
4620 unsigned NumParams) {
4621 DeclContext *DC = CurContext;
4622 while (!(DC->isFunctionOrMethod() || DC->isRecord() || DC->isFileContext()))
4623 DC = DC->getParent();
4624
4625 RecordDecl *RD = nullptr;
4626 if (getLangOpts().CPlusPlus)
4627 RD = CXXRecordDecl::Create(Context, TagTypeKind::Struct, DC, Loc, Loc,
4628 /*Id=*/nullptr);
4629 else
4630 RD = RecordDecl::Create(Context, TagTypeKind::Struct, DC, Loc, Loc,
4631 /*Id=*/nullptr);
4632
4633 RD->setCapturedRecord();
4634 DC->addDecl(RD);
4635 RD->setImplicit();
4636 RD->startDefinition();
4637
4638 assert(NumParams > 0 && "CapturedStmt requires context parameter");
4639 CD = CapturedDecl::Create(Context, CurContext, NumParams);
4640 DC->addDecl(CD);
4641 return RD;
4642 }
4643
4644 static bool
4645 buildCapturedStmtCaptureList(Sema &S, CapturedRegionScopeInfo *RSI,
4646 SmallVectorImpl<CapturedStmt::Capture> &Captures,
4647 SmallVectorImpl<Expr *> &CaptureInits) {
4648 for (const sema::Capture &Cap : RSI->Captures) {
4649 if (Cap.isInvalid())
4650 continue;
4651
4652 // Form the initializer for the capture.
4653 ExprResult Init = S.BuildCaptureInit(Cap, Cap.getLocation(),
4654 RSI->CapRegionKind == CR_OpenMP);
4655
4656 // FIXME: Bail out now if the capture is not used and the initializer has
4657 // no side-effects.
4658
4659 // Create a field for this capture.
4660 FieldDecl *Field = S.BuildCaptureField(RSI->TheRecordDecl, Cap);
4661
4662 // Add the capture to our list of captures.
4663 if (Cap.isThisCapture()) {
4664 Captures.push_back(CapturedStmt::Capture(Cap.getLocation(),
4665 CapturedStmt::VCK_This));
4666 } else if (Cap.isVLATypeCapture()) {
4667 Captures.push_back(
4668 CapturedStmt::Capture(Cap.getLocation(), CapturedStmt::VCK_VLAType));
4669 } else {
4670 assert(Cap.isVariableCapture() && "unknown kind of capture");
4671
4672 if (S.getLangOpts().OpenMP && RSI->CapRegionKind == CR_OpenMP)
4673 S.OpenMP().setOpenMPCaptureKind(Field, Cap.getVariable(),
4674 RSI->OpenMPLevel);
4675
4676 Captures.push_back(CapturedStmt::Capture(
4677 Cap.getLocation(),
4678 Cap.isReferenceCapture() ? CapturedStmt::VCK_ByRef
4679 : CapturedStmt::VCK_ByCopy,
4680 cast<VarDecl>(Cap.getVariable())));
4681 }
4682 CaptureInits.push_back(Init.get());
4683 }
4684 return false;
4685 }
4686
4687 static std::optional<int>
4688 isOpenMPCapturedRegionInArmSMEFunction(Sema const &S, CapturedRegionKind Kind) {
4689 if (!S.getLangOpts().OpenMP || Kind != CR_OpenMP)
4690 return {};
4691 if (const FunctionDecl *FD = S.getCurFunctionDecl(/*AllowLambda=*/true)) {
4692 if (IsArmStreamingFunction(FD, /*IncludeLocallyStreaming=*/true))
4693 return /* in streaming functions */ 0;
4694 if (hasArmZAState(FD))
4695 return /* in functions with ZA state */ 1;
4696 if (hasArmZT0State(FD))
4697 return /* in fuctions with ZT0 state */ 2;
4698 }
4699 return {};
4700 }
4701
4702 void Sema::ActOnCapturedRegionStart(SourceLocation Loc, Scope *CurScope,
4703 CapturedRegionKind Kind,
4704 unsigned NumParams) {
4705 if (auto ErrorIndex = isOpenMPCapturedRegionInArmSMEFunction(*this, Kind))
4706 Diag(Loc, diag::err_sme_openmp_captured_region) << *ErrorIndex;
4707
4708 CapturedDecl *CD = nullptr;
4709 RecordDecl *RD = CreateCapturedStmtRecordDecl(CD, Loc, NumParams);
4710
4711 // Build the context parameter
4712 DeclContext *DC = CapturedDecl::castToDeclContext(CD);
4713 IdentifierInfo *ParamName = &Context.Idents.get("__context");
4714 CanQualType ParamType =
4715 Context.getPointerType(Context.getCanonicalTagType(RD));
4716 auto *Param =
4717 ImplicitParamDecl::Create(Context, DC, Loc, ParamName, ParamType,
4718 ImplicitParamKind::CapturedContext);
4719 DC->addDecl(Param);
4720
4721 CD->setContextParam(0, Param);
4722
4723 // Enter the capturing scope for this captured region.
4724 PushCapturedRegionScope(CurScope, CD, RD, Kind);
4725
4726 if (CurScope)
4727 PushDeclContext(CurScope, CD);
4728 else
4729 CurContext = CD;
4730
4731 PushExpressionEvaluationContext(
4732 ExpressionEvaluationContext::PotentiallyEvaluated);
4733 ExprEvalContexts.back().InImmediateEscalatingFunctionContext = false;
4734 }
4735
4736 void Sema::ActOnCapturedRegionStart(SourceLocation Loc, Scope *CurScope,
4737 CapturedRegionKind Kind,
4738 ArrayRef<CapturedParamNameType> Params,
4739 unsigned OpenMPCaptureLevel) {
4740 if (auto ErrorIndex = isOpenMPCapturedRegionInArmSMEFunction(*this, Kind))
4741 Diag(Loc, diag::err_sme_openmp_captured_region) << *ErrorIndex;
4742
4743 CapturedDecl *CD = nullptr;
4744 RecordDecl *RD = CreateCapturedStmtRecordDecl(CD, Loc, Params.size());
4745
4746 // Build the context parameter
4747 DeclContext *DC = CapturedDecl::castToDeclContext(CD);
4748 bool ContextIsFound = false;
4749 unsigned ParamNum = 0;
4750 for (ArrayRef<CapturedParamNameType>::iterator I = Params.begin(),
4751 E = Params.end();
4752 I != E; ++I, ++ParamNum) {
4753 if (I->second.isNull()) {
4754 assert(!ContextIsFound &&
4755 "null type has been found already for '__context' parameter");
4756 IdentifierInfo *ParamName = &Context.Idents.get("__context");
4757 QualType ParamType =
4758 Context.getPointerType(Context.getCanonicalTagType(RD))
4759 .withConst()
4760 .withRestrict();
4761 auto *Param =
4762 ImplicitParamDecl::Create(Context, DC, Loc, ParamName, ParamType,
4763 ImplicitParamKind::CapturedContext);
4764 DC->addDecl(Param);
4765 CD->setContextParam(ParamNum, Param);
4766 ContextIsFound = true;
4767 } else {
4768 IdentifierInfo *ParamName = &Context.Idents.get(I->first);
4769 auto *Param =
4770 ImplicitParamDecl::Create(Context, DC, Loc, ParamName, I->second,
4771 ImplicitParamKind::CapturedContext);
4772 DC->addDecl(Param);
4773 CD->setParam(ParamNum, Param);
4774 }
4775 }
4776 assert(ContextIsFound && "no null type for '__context' parameter");
4777 if (!ContextIsFound) {
4778 // Add __context implicitly if it is not specified.
4779 IdentifierInfo *ParamName = &Context.Idents.get("__context");
4780 CanQualType ParamType =
4781 Context.getPointerType(Context.getCanonicalTagType(RD));
4782 auto *Param =
4783 ImplicitParamDecl::Create(Context, DC, Loc, ParamName, ParamType,
4784 ImplicitParamKind::CapturedContext);
4785 DC->addDecl(Param);
4786 CD->setContextParam(ParamNum, Param);
4787 }
4788 // Enter the capturing scope for this captured region.
4789 PushCapturedRegionScope(CurScope, CD, RD, Kind, OpenMPCaptureLevel);
4790
4791 if (CurScope)
4792 PushDeclContext(CurScope, CD);
4793 else
4794 CurContext = CD;
4795
4796 PushExpressionEvaluationContext(
4797 ExpressionEvaluationContext::PotentiallyEvaluated);
4798 }
4799
4800 void Sema::ActOnCapturedRegionError() {
4801 DiscardCleanupsInEvaluationContext();
4802 PopExpressionEvaluationContext();
4803 PopDeclContext();
4804 PoppedFunctionScopePtr ScopeRAII = PopFunctionScopeInfo();
4805 CapturedRegionScopeInfo *RSI = cast<CapturedRegionScopeInfo>(ScopeRAII.get());
4806
4807 RecordDecl *Record = RSI->TheRecordDecl;
4808 Record->setInvalidDecl();
4809
4810 SmallVector<Decl*, 4> Fields(Record->fields());
4811 ActOnFields(/*Scope=*/nullptr, Record->getLocation(), Record, Fields,
4812 SourceLocation(), SourceLocation(), ParsedAttributesView());
4813 }
4814
4815 StmtResult Sema::ActOnCapturedRegionEnd(Stmt *S) {
4816 // Leave the captured scope before we start creating captures in the
4817 // enclosing scope.
4818 DiscardCleanupsInEvaluationContext();
4819 PopExpressionEvaluationContext();
4820 PopDeclContext();
4821 PoppedFunctionScopePtr ScopeRAII = PopFunctionScopeInfo();
4822 CapturedRegionScopeInfo *RSI = cast<CapturedRegionScopeInfo>(ScopeRAII.get());
4823
4824 SmallVector<CapturedStmt::Capture, 4> Captures;
4825 SmallVector<Expr *, 4> CaptureInits;
4826 if (buildCapturedStmtCaptureList(*this, RSI, Captures, CaptureInits))
4827 return StmtError();
4828
4829 CapturedDecl *CD = RSI->TheCapturedDecl;
4830 RecordDecl *RD = RSI->TheRecordDecl;
4831
4832 CapturedStmt *Res = CapturedStmt::Create(
4833 getASTContext(), S, static_cast<CapturedRegionKind>(RSI->CapRegionKind),
4834 Captures, CaptureInits, CD, RD);
4835
4836 CD->setBody(Res->getCapturedStmt());
4837 RD->completeDefinition();
4838
4839 return Res;
4840 }