From 6ca0b32227c21ba4bc66e8f1bdbf767b563567a8 Mon Sep 17 00:00:00 2001 From: Fariborz Jahanian Date: Mon, 3 Aug 2009 20:20:07 +0000 Subject: [PATCH] ir-gen for destructor calls. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@77991 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/CGDecl.cpp | 12 ++++++++++ test/CodeGenCXX/destructor-calls.cpp | 36 ++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 test/CodeGenCXX/destructor-calls.cpp diff --git a/lib/CodeGen/CGDecl.cpp b/lib/CodeGen/CGDecl.cpp index 9fd571b816..347a0af696 100644 --- a/lib/CodeGen/CGDecl.cpp +++ b/lib/CodeGen/CGDecl.cpp @@ -432,6 +432,18 @@ void CodeGenFunction::EmitLocalBlockVarDecl(const VarDecl &D) { } } + // Handle CXX destruction of variables. + // FIXME - destruction of arrays NYI. + if (const RecordType *RT = Ty->getAs()) + if (CXXRecordDecl *ClassDecl = dyn_cast(RT->getDecl())) { + if (!ClassDecl->hasTrivialDestructor()) { + const CXXDestructorDecl *D = ClassDecl->getDestructor(getContext()); + assert(D && "EmitLocalBlockVarDecl - destructor is nul"); + CleanupScope scope(*this); + EmitCXXDestructorCall(D, Dtor_Complete, DeclPtr); + } + } + // Handle the cleanup attribute if (const CleanupAttr *CA = D.getAttr()) { const FunctionDecl *FD = CA->getFunctionDecl(); diff --git a/test/CodeGenCXX/destructor-calls.cpp b/test/CodeGenCXX/destructor-calls.cpp new file mode 100644 index 0000000000..7b9225590a --- /dev/null +++ b/test/CodeGenCXX/destructor-calls.cpp @@ -0,0 +1,36 @@ +// RUN: clang-cc %s -emit-llvm -o %t + +extern "C" int printf(...); + +static int val; + +struct B { + B() : iB(++val) { printf("B()\n"); } + int iB; + ~B() { printf("~B(%d)\n", iB); --val; } +}; + +struct M : B { + M() : iM(++val) { printf("M()\n"); } + int iM; + ~M() { printf("~M(%d)\n", iM); --val; } +}; + +struct P { + P() : iP(++val) { printf("P()\n"); } + int iP; + ~P() { printf("~P(%d)\n", iP); --val; } +}; + +struct N : M, P { + N() { printf("N()\n"); iN = ++val; } + ~N() { printf("~N(%d) val = %d\n", iN, --val); } + int iN; + M m; + P p; +}; + +int main() { + N n1; + N n2; +} -- 2.40.0