Skip to content

Commit d7b669b

Browse files
mizvekovtstellar
authored andcommitted
[clang] don't mark as Elidable CXXConstruct expressions used in NRVO
See PR51862. The consumers of the Elidable flag in CXXConstructExpr assume that an elidable construction just goes through a single copy/move construction, so that the source object is immediately passed as an argument and is the same type as the parameter itself. With the implementation of P2266 and after some adjustments to the implementation of P1825, we started (correctly, as per standard) allowing more cases where the copy initialization goes through user defined conversions. With this patch we stop using this flag in NRVO contexts, to preserve code that relies on that assumption. This causes no known functional changes, we just stop firing some asserts in a cople of included test cases. Reviewed By: rsmith Differential Revision: https://reviews.llvm.org/D109800 (cherry picked from commit d9308aa)
1 parent ee6913c commit d7b669b

File tree

13 files changed

+122
-33
lines changed

13 files changed

+122
-33
lines changed

clang/include/clang/Sema/Initialization.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -298,8 +298,8 @@ class alignas(8) InitializedEntity {
298298

299299
/// Create the initialization entity for the result of a function.
300300
static InitializedEntity InitializeResult(SourceLocation ReturnLoc,
301-
QualType Type, bool NRVO) {
302-
return InitializedEntity(EK_Result, ReturnLoc, Type, NRVO);
301+
QualType Type) {
302+
return InitializedEntity(EK_Result, ReturnLoc, Type);
303303
}
304304

305305
static InitializedEntity InitializeStmtExprResult(SourceLocation ReturnLoc,
@@ -308,20 +308,20 @@ class alignas(8) InitializedEntity {
308308
}
309309

310310
static InitializedEntity InitializeBlock(SourceLocation BlockVarLoc,
311-
QualType Type, bool NRVO) {
312-
return InitializedEntity(EK_BlockElement, BlockVarLoc, Type, NRVO);
311+
QualType Type) {
312+
return InitializedEntity(EK_BlockElement, BlockVarLoc, Type);
313313
}
314314

315315
static InitializedEntity InitializeLambdaToBlock(SourceLocation BlockVarLoc,
316-
QualType Type, bool NRVO) {
316+
QualType Type) {
317317
return InitializedEntity(EK_LambdaToBlockConversionBlockElement,
318-
BlockVarLoc, Type, NRVO);
318+
BlockVarLoc, Type);
319319
}
320320

321321
/// Create the initialization entity for an exception object.
322322
static InitializedEntity InitializeException(SourceLocation ThrowLoc,
323-
QualType Type, bool NRVO) {
324-
return InitializedEntity(EK_Exception, ThrowLoc, Type, NRVO);
323+
QualType Type) {
324+
return InitializedEntity(EK_Exception, ThrowLoc, Type);
325325
}
326326

327327
/// Create the initialization entity for an object allocated via new.

clang/lib/AST/ExprConstant.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9931,10 +9931,19 @@ bool RecordExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E,
99319931
return false;
99329932

99339933
// Avoid materializing a temporary for an elidable copy/move constructor.
9934-
if (E->isElidable() && !ZeroInit)
9935-
if (const MaterializeTemporaryExpr *ME
9936-
= dyn_cast<MaterializeTemporaryExpr>(E->getArg(0)))
9934+
if (E->isElidable() && !ZeroInit) {
9935+
// FIXME: This only handles the simplest case, where the source object
9936+
// is passed directly as the first argument to the constructor.
9937+
// This should also handle stepping though implicit casts and
9938+
// and conversion sequences which involve two steps, with a
9939+
// conversion operator followed by a converting constructor.
9940+
const Expr *SrcObj = E->getArg(0);
9941+
assert(SrcObj->isTemporaryObject(Info.Ctx, FD->getParent()));
9942+
assert(Info.Ctx.hasSameUnqualifiedType(E->getType(), SrcObj->getType()));
9943+
if (const MaterializeTemporaryExpr *ME =
9944+
dyn_cast<MaterializeTemporaryExpr>(SrcObj))
99379945
return Visit(ME->getSubExpr());
9946+
}
99389947

99399948
if (ZeroInit && !ZeroInitialization(E, T))
99409949
return false;

clang/lib/CodeGen/CGExprCXX.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -609,15 +609,18 @@ CodeGenFunction::EmitCXXConstructExpr(const CXXConstructExpr *E,
609609
return;
610610

611611
// Elide the constructor if we're constructing from a temporary.
612-
// The temporary check is required because Sema sets this on NRVO
613-
// returns.
614612
if (getLangOpts().ElideConstructors && E->isElidable()) {
615-
assert(getContext().hasSameUnqualifiedType(E->getType(),
616-
E->getArg(0)->getType()));
617-
if (E->getArg(0)->isTemporaryObject(getContext(), CD->getParent())) {
618-
EmitAggExpr(E->getArg(0), Dest);
619-
return;
620-
}
613+
// FIXME: This only handles the simplest case, where the source object
614+
// is passed directly as the first argument to the constructor.
615+
// This should also handle stepping though implicit casts and
616+
// conversion sequences which involve two steps, with a
617+
// conversion operator followed by a converting constructor.
618+
const Expr *SrcObj = E->getArg(0);
619+
assert(SrcObj->isTemporaryObject(getContext(), CD->getParent()));
620+
assert(
621+
getContext().hasSameUnqualifiedType(E->getType(), SrcObj->getType()));
622+
EmitAggExpr(SrcObj, Dest);
623+
return;
621624
}
622625

623626
if (const ArrayType *arrayType

clang/lib/Sema/Sema.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2010,7 +2010,7 @@ static void checkEscapingByref(VarDecl *VD, Sema &S) {
20102010
Expr *VarRef =
20112011
new (S.Context) DeclRefExpr(S.Context, VD, false, T, VK_LValue, Loc);
20122012
ExprResult Result;
2013-
auto IE = InitializedEntity::InitializeBlock(Loc, T, false);
2013+
auto IE = InitializedEntity::InitializeBlock(Loc, T);
20142014
if (S.getLangOpts().CPlusPlus2b) {
20152015
auto *E = ImplicitCastExpr::Create(S.Context, T, CK_NoOp, VarRef, nullptr,
20162016
VK_XValue, FPOptionsOverride());

clang/lib/Sema/SemaCoroutine.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1533,7 +1533,7 @@ bool CoroutineStmtBuilder::makeGroDeclAndReturnStmt() {
15331533
if (GroType->isVoidType()) {
15341534
// Trigger a nice error message.
15351535
InitializedEntity Entity =
1536-
InitializedEntity::InitializeResult(Loc, FnRetType, false);
1536+
InitializedEntity::InitializeResult(Loc, FnRetType);
15371537
S.PerformCopyInitialization(Entity, SourceLocation(), ReturnValue);
15381538
noteMemberDeclaredHere(S, ReturnValue, Fn);
15391539
return false;

clang/lib/Sema/SemaDeclCXX.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15262,8 +15262,17 @@ Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType,
1526215262
// can be omitted by constructing the temporary object
1526315263
// directly into the target of the omitted copy/move
1526415264
if (ConstructKind == CXXConstructExpr::CK_Complete && Constructor &&
15265+
// FIXME: Converting constructors should also be accepted.
15266+
// But to fix this, the logic that digs down into a CXXConstructExpr
15267+
// to find the source object needs to handle it.
15268+
// Right now it assumes the source object is passed directly as the
15269+
// first argument.
1526515270
Constructor->isCopyOrMoveConstructor() && hasOneRealArgument(ExprArgs)) {
1526615271
Expr *SubExpr = ExprArgs[0];
15272+
// FIXME: Per above, this is also incorrect if we want to accept
15273+
// converting constructors, as isTemporaryObject will
15274+
// reject temporaries with different type from the
15275+
// CXXRecord itself.
1526715276
Elidable = SubExpr->isTemporaryObject(
1526815277
Context, cast<CXXRecordDecl>(FoundDecl->getDeclContext()));
1526915278
}

clang/lib/Sema/SemaExpr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15683,7 +15683,7 @@ ExprResult Sema::ActOnBlockStmtExpr(SourceLocation CaretLoc,
1568315683
if (!Result.isInvalid()) {
1568415684
Result = PerformCopyInitialization(
1568515685
InitializedEntity::InitializeBlock(Var->getLocation(),
15686-
Cap.getCaptureType(), false),
15686+
Cap.getCaptureType()),
1568715687
Loc, Result.get());
1568815688
}
1568915689

clang/lib/Sema/SemaExprCXX.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -893,9 +893,8 @@ ExprResult Sema::BuildCXXThrow(SourceLocation OpLoc, Expr *Ex,
893893
if (CheckCXXThrowOperand(OpLoc, ExceptionObjectTy, Ex))
894894
return ExprError();
895895

896-
InitializedEntity Entity = InitializedEntity::InitializeException(
897-
OpLoc, ExceptionObjectTy,
898-
/*NRVO=*/NRInfo.isCopyElidable());
896+
InitializedEntity Entity =
897+
InitializedEntity::InitializeException(OpLoc, ExceptionObjectTy);
899898
ExprResult Res = PerformMoveOrCopyInitialization(Entity, NRInfo, Ex);
900899
if (Res.isInvalid())
901900
return ExprError();

clang/lib/Sema/SemaLambda.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1975,8 +1975,7 @@ ExprResult Sema::BuildBlockForLambdaConversion(SourceLocation CurrentLocation,
19751975
CallOperator->markUsed(Context);
19761976

19771977
ExprResult Init = PerformCopyInitialization(
1978-
InitializedEntity::InitializeLambdaToBlock(ConvLocation, Src->getType(),
1979-
/*NRVO=*/false),
1978+
InitializedEntity::InitializeLambdaToBlock(ConvLocation, Src->getType()),
19801979
CurrentLocation, Src);
19811980
if (!Init.isInvalid())
19821981
Init = ActOnFinishFullExpr(Init.get(), /*DiscardedValue*/ false);

clang/lib/Sema/SemaObjCProperty.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,8 +1467,7 @@ Decl *Sema::ActOnPropertyImplDecl(Scope *S,
14671467
LoadSelfExpr, true, true);
14681468
ExprResult Res = PerformCopyInitialization(
14691469
InitializedEntity::InitializeResult(PropertyDiagLoc,
1470-
getterMethod->getReturnType(),
1471-
/*NRVO=*/false),
1470+
getterMethod->getReturnType()),
14721471
PropertyDiagLoc, IvarRefExpr);
14731472
if (!Res.isInvalid()) {
14741473
Expr *ResExpr = Res.getAs<Expr>();

0 commit comments

Comments
 (0)