diff --git a/clang/include/clang/AST/ExprCXX.h b/clang/include/clang/AST/ExprCXX.h index 0f1354754b3c..c2efcd228f94 100644 --- a/clang/include/clang/AST/ExprCXX.h +++ b/clang/include/clang/AST/ExprCXX.h @@ -486,7 +486,6 @@ class CXXConstructExpr : public Expr { Stmt **Args; unsigned NumArgs; - protected: CXXConstructExpr(ASTContext &C, StmtClass SC, QualType T, diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp index 3818f56df941..ea534d3b2bd9 100644 --- a/clang/lib/CodeGen/CGExpr.cpp +++ b/clang/lib/CodeGen/CGExpr.cpp @@ -87,8 +87,24 @@ RValue CodeGenFunction::EmitReferenceBindingToExpr(const Expr* E, return RValue::get(LV.getAddress()); Val = EmitLoadOfLValue(LV, E->getType()); } else { + // FIXME: Initializers don't work with casts yet. For example + // const A& a = B(); + // if B inherits from A. Val = EmitAnyExprToTemp(E, /*IsAggLocVolatile=*/false, IsInitializer); + + // We might have to destroy the temporary variable. + if (const RecordType *RT = E->getType()->getAs()) { + if (CXXRecordDecl *ClassDecl = dyn_cast(RT->getDecl())) { + if (!ClassDecl->hasTrivialDestructor()) { + const CXXDestructorDecl *Dtor = + ClassDecl->getDestructor(getContext()); + + CleanupScope scope(*this); + EmitCXXDestructorCall(Dtor, Dtor_Complete, Val.getAggregateAddr()); + } + } + } } if (Val.isAggregate()) {