From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 98969 invoked by alias); 11 Jan 2019 18:36:50 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Received: (qmail 98959 invoked by uid 89); 11 Jan 2019 18:36:50 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-25.9 required=5.0 tests=BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,KAM_LAZY_DOMAIN_SECURITY,KAM_SHORT autolearn=ham version=3.3.2 spammy=H*r:4.89, function_depth, U*burnus, sk:type_ha X-HELO: outpost19.zedat.fu-berlin.de Received: from outpost19.zedat.fu-berlin.de (HELO outpost19.zedat.fu-berlin.de) (130.133.4.112) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 11 Jan 2019 18:36:47 +0000 Received: from relay1.zedat.fu-berlin.de ([130.133.4.67]) by outpost.zedat.fu-berlin.de (Exim 4.85) with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (envelope-from ) id <1gi1fh-00292J-17>; Fri, 11 Jan 2019 19:36:45 +0100 Received: from mx.physik.fu-berlin.de ([160.45.64.218]) by relay1.zedat.fu-berlin.de (Exim 4.85) with esmtps (TLSv1.2:DHE-RSA-AES128-SHA:128) (envelope-from ) id <1gi1fg-001B4E-UF>; Fri, 11 Jan 2019 19:36:44 +0100 Received: from login1.physik.fu-berlin.de ([160.45.66.207]) by mx.physik.fu-berlin.de with esmtps (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1gi1fd-0007iL-8u; Fri, 11 Jan 2019 19:36:41 +0100 Received: from tburnus by login1.physik.fu-berlin.de with local (Exim 4.89 #2 (Debian)) id 1gi1fd-0008GN-7S; Fri, 11 Jan 2019 19:36:41 +0100 Date: Fri, 11 Jan 2019 18:36:00 -0000 From: Tobias Burnus To: gcc-patches@gcc.gnu.org, Jason Merrill Subject: Re: PR C++/88114 - patch for destructor not generated for "virtual ~destructor() = default" Message-ID: <20190111183641.4n3tb4kb4iqks4ib@physik.fu-berlin.de> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="iwwxxvl2kettxlov" Content-Disposition: inline In-Reply-To: <4c030212-9420-00cd-c9b4-c35ef7fd91a0@redhat.com> User-Agent: NeoMutt/20170113 (1.7.2) Sender: Tobias Burnus X-SW-Source: 2019-01/txt/msg00646.txt.bz2 --iwwxxvl2kettxlov Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-length: 1223 Dear Jason, dear all, Jason Merrill wrote on 5 Dec 2018: > You can get at the destructor with CLASSTYPE_DESTRUCTOR rather than walking through TYPE_FIELDS. I'd also check DECL_DEFAULTED_IN_CLASS_P. > I'd also do this in maybe_emit_vtables rather than here, so that it only happens once per class. Updated patch. I also added a test case which checks that the destructor is not generated. [ Original patch: https://gcc.gnu.org/ml/gcc-patches/2018-11/msg01824.html ] Background again: If a class contains any 'virtual ... = 0', it's an abstract class and for an abstract class, the destructor not added to the vtable. For a normal virtual ~class() { } that's not a problem as the class::~class() destructor will be generated during the parsing of the function. But for virtual ~class() = default; the destructor will be generated via mark_used via the vtable. If one now declares a derived class and uses it, the class::~class() is generated in that translation unit. Unless, #pragma interface/implementation is used. In that case, the 'default' destructor will never be generated. Build and regtested on x86_64-gnu-linux. OK? Or do you have more suggested changes? Tobias --iwwxxvl2kettxlov Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="pr88114-destructor-default-v2.diff" Content-length: 3190 2019-01-11 Tobias Burnus PR C++/88114 * decl2.c (maybe_emit_vtables): If needed, generate code for the destructor of an abstract class. (mark_used): Update comment for older function-name change. PR C++/88114 * g++.dg/cpp0x/defaulted61.C: New. * g++.dg/cpp0x/defaulted62.C: New. diff --git a/gcc/cp/decl2.c b/gcc/cp/decl2.c index dbab95fbc96..2e7ecdaa01c 100644 --- a/gcc/cp/decl2.c +++ b/gcc/cp/decl2.c @@ -2220,6 +2220,17 @@ maybe_emit_vtables (tree ctype) } } + /* For abstract classes, the destructor has been removed from the + vtable (in class.c's build_vtbl_initializer). For a compiler- + generated destructor, it hence might not have been generated in + this translation unit - and with '#pragma interface' it might + never get generated. */ + if (CLASSTYPE_PURE_VIRTUALS (ctype) + && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) + && DECL_DEFAULTED_IN_CLASS_P(CLASSTYPE_DESTRUCTOR(ctype)) + && DECL_DEFAULTED_FN (CLASSTYPE_DESTRUCTOR(ctype))) + note_vague_linkage_fn (CLASSTYPE_DESTRUCTOR(ctype)); + /* Since we're writing out the vtable here, also write the debug info. */ note_debug_info_needed (ctype); @@ -5497,7 +5508,7 @@ mark_used (tree decl, tsubst_flags_t complain) within the body of a function so as to avoid collecting live data on the stack (such as overload resolution candidates). - We could just let cp_write_global_declarations handle synthesizing + We could just let c_parse_final_cleanups handle synthesizing this function by adding it to deferred_fns, but doing it at the use site produces better error messages. */ ++function_depth; diff --git a/gcc/testsuite/g++.dg/cpp0x/defaulted61.C b/gcc/testsuite/g++.dg/cpp0x/defaulted61.C new file mode 100644 index 00000000000..e7e0a486292 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/defaulted61.C @@ -0,0 +1,22 @@ +// { dg-do compile { target c++11 } } +// { dg-final { scan-assembler "_ZN3OneD0Ev" } } + +// PR C++/88114 +// Destructor of an abstract class was never generated +// when compiling the class - nor later due to the +// '#pragma interface' + +#pragma implementation +#pragma interface + +class One +{ + public: + virtual ~One() = default; + void some_fn(); + virtual void later() = 0; + private: + int m_int; +}; + +void One::some_fn() { } diff --git a/gcc/testsuite/g++.dg/cpp0x/defaulted62.C b/gcc/testsuite/g++.dg/cpp0x/defaulted62.C new file mode 100644 index 00000000000..d8dab608816 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/defaulted62.C @@ -0,0 +1,25 @@ +// { dg-do compile { target c++11 } } +// { dg-final { scan-assembler-not "_ZN3OneD0Ev" } } + +// PR C++/88114 +// Destructor of an abstract class was never generated +// when compiling the class - nor later due to the +// '#pragma interface' +// -> g++.dg/cpp0x/defaulted61.C + +// HERE, in g++.dg/cpp0x/defaulted62.C: +// As we have commented the pragmas, it should NOT be created +// #pragma implementation +// #pragma interface + +class One +{ + public: + virtual ~One() = default; + void some_fn(); + virtual void later() = 0; + private: + int m_int; +}; + +void One::some_fn() { } --iwwxxvl2kettxlov--