diff options
author | yronglin <yronglin777@gmail.com> | 2024-09-08 22:36:49 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-09-08 22:36:49 +0800 |
commit | 45c8766973bb3bb73dd8d996231e114dcf45df9f (patch) | |
tree | 6ed8a689d0df40dd9370ba77635b058e9583c705 /clang/lib/Sema/SemaInit.cpp | |
parent | dec0781c8b06b1a5b8ad8646d299f643745a36d0 (diff) | |
download | llvm-45c8766973bb3bb73dd8d996231e114dcf45df9f.zip llvm-45c8766973bb3bb73dd8d996231e114dcf45df9f.tar.gz llvm-45c8766973bb3bb73dd8d996231e114dcf45df9f.tar.bz2 |
Reapply "[Clang][CWG1815] Support lifetime extension of temporary created by aggregate initialization using a default member initializer" (#97308)
The PR reapply https://github.com/llvm/llvm-project/pull/92527.
Implemented CWG1815 and fixed the bugs mentioned in the comments of
https://github.com/llvm/llvm-project/pull/92527 and
https://github.com/llvm/llvm-project/pull/87933.
The reason why the original PR was reverted was that errors might occur
during the rebuild.
---------
Signed-off-by: yronglin <yronglin777@gmail.com>
Diffstat (limited to 'clang/lib/Sema/SemaInit.cpp')
-rw-r--r-- | clang/lib/Sema/SemaInit.cpp | 22 |
1 files changed, 16 insertions, 6 deletions
diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp index 7dc1718..de2686e 100644 --- a/clang/lib/Sema/SemaInit.cpp +++ b/clang/lib/Sema/SemaInit.cpp @@ -750,8 +750,20 @@ void InitListChecker::FillInEmptyInitForField(unsigned Init, FieldDecl *Field, if (Field->hasInClassInitializer()) { if (VerifyOnly) return; - - ExprResult DIE = SemaRef.BuildCXXDefaultInitExpr(Loc, Field); + ExprResult DIE; + { + // Enter a default initializer rebuild context, then we can support + // lifetime extension of temporary created by aggregate initialization + // using a default member initializer. + // CWG1815 (https://wg21.link/CWG1815). + EnterExpressionEvaluationContext RebuildDefaultInit( + SemaRef, Sema::ExpressionEvaluationContext::PotentiallyEvaluated); + // Just copy previous record, make sure we haven't forget anything. + SemaRef.currentEvaluationContext() = SemaRef.parentEvaluationContext(); + SemaRef.currentEvaluationContext().RebuildDefaultArgOrDefaultInit = + true; + DIE = SemaRef.BuildCXXDefaultInitExpr(Loc, Field); + } if (DIE.isInvalid()) { hadError = true; return; @@ -7521,10 +7533,8 @@ Sema::CreateMaterializeTemporaryExpr(QualType T, Expr *Temporary, // are done in both CreateMaterializeTemporaryExpr and MaybeBindToTemporary, // but there may be a chance to merge them. Cleanup.setExprNeedsCleanups(false); - if (isInLifetimeExtendingContext()) { - auto &Record = ExprEvalContexts.back(); - Record.ForRangeLifetimeExtendTemps.push_back(MTE); - } + if (isInLifetimeExtendingContext()) + currentEvaluationContext().ForRangeLifetimeExtendTemps.push_back(MTE); return MTE; } |