From 2835745a451002798fed9800aeb19277f6a8fcb3 Mon Sep 17 00:00:00 2001 From: Sebastian Redl Date: Mon, 5 Mar 2012 19:35:43 +0000 Subject: [PATCH] If the element type of an initializer list has a destructor, make sure we check it. Fixes PR12178. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@152048 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Sema/SemaExprCXX.cpp | 3 +-- lib/Sema/SemaInit.cpp | 13 +++++++++++++ .../cxx0x-initializer-stdinitializerlist.cpp | 18 ++++++++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/lib/Sema/SemaExprCXX.cpp b/lib/Sema/SemaExprCXX.cpp index 1893d6dfa1..f6662ba37f 100644 --- a/lib/Sema/SemaExprCXX.cpp +++ b/lib/Sema/SemaExprCXX.cpp @@ -641,8 +641,7 @@ ExprResult Sema::CheckCXXThrowOperand(SourceLocation ThrowLoc, Expr *E, if (RD->hasIrrelevantDestructor()) return Owned(E); - CXXDestructorDecl *Destructor - = const_cast(LookupDestructor(RD)); + CXXDestructorDecl *Destructor = LookupDestructor(RD); if (!Destructor) return Owned(E); diff --git a/lib/Sema/SemaInit.cpp b/lib/Sema/SemaInit.cpp index ba4453c08c..5d2536ae1b 100644 --- a/lib/Sema/SemaInit.cpp +++ b/lib/Sema/SemaInit.cpp @@ -5309,6 +5309,19 @@ InitializationSequence::Perform(Sema &S, bool Success = S.isStdInitializerList(Dest, &E); (void)Success; assert(Success && "Destination type changed?"); + + // If the element type has a destructor, check it. + if (CXXRecordDecl *RD = E->getAsCXXRecordDecl()) { + if (!RD->hasIrrelevantDestructor()) { + if (CXXDestructorDecl *Destructor = S.LookupDestructor(RD)) { + S.MarkFunctionReferenced(Kind.getLocation(), Destructor); + S.CheckDestructorAccess(Kind.getLocation(), Destructor, + S.PDiag(diag::err_access_dtor_temp) << E); + S.DiagnoseUseOfDecl(Destructor, Kind.getLocation()); + } + } + } + InitListExpr *ILE = cast(CurInit.take()); unsigned NumInits = ILE->getNumInits(); SmallVector Converted(NumInits); diff --git a/test/CodeGenCXX/cxx0x-initializer-stdinitializerlist.cpp b/test/CodeGenCXX/cxx0x-initializer-stdinitializerlist.cpp index 775060bfac..81ce559844 100644 --- a/test/CodeGenCXX/cxx0x-initializer-stdinitializerlist.cpp +++ b/test/CodeGenCXX/cxx0x-initializer-stdinitializerlist.cpp @@ -232,3 +232,21 @@ void fn11() { destroyme2 dm2; // CHECK: call void @_ZN10destroyme2D1Ev } + +namespace PR12178 { + struct string { + string(int); + ~string(); + }; + + struct pair { + string a; + int b; + }; + + struct map { + map(std::initializer_list); + }; + + map m{ {1, 2}, {3, 4} }; +} -- 2.40.0