From patchwork Mon Sep 9 10:30:16 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Carlini X-Patchwork-Id: 273548 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "www.sourceware.org", Issuer "StartCom Class 1 Primary Intermediate Server CA" (not verified)) by ozlabs.org (Postfix) with ESMTPS id 773E92C00AB for ; Mon, 9 Sep 2013 20:30:31 +1000 (EST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :message-id:date:from:mime-version:to:cc:subject:references :in-reply-to:content-type; q=dns; s=default; b=WNE0yQDnnEl4j0pIw 10AwHNZvJqFSgGSS9Ryo3tYFetiZLg0U1++YxqDYw/vnChccqFRV27x1KtBiJk6V axNqAuqU/x//SAbtj+ATM3ZwOZ1A357bTZPZ3eKtfks0f7mj7q6jQ4Xz9hMvQl09 /Jxc843nW49HemzrAfrCzXVjBk= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :message-id:date:from:mime-version:to:cc:subject:references :in-reply-to:content-type; s=default; bh=s2XxOYpbykr5jUXvIXwoi9J ejZ0=; b=rqLtKV44VHvLifFxx/Hru0MHFynkEOQLmiqZNQMpAdhlgO2Vxuggfpd eVB3GAZj/uK97wFjAC1tiyKeH899vuqjeLyB3dTHlWiCa8cKci/y5VFHCEydhDNe txwmFohCFrCZIo+6y3AGejNHAXQ57iMUtydtdxcIBYSp4Y+AWWfs= Received: (qmail 26447 invoked by alias); 9 Sep 2013 10:30:25 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 26434 invoked by uid 89); 9 Sep 2013 10:30:24 -0000 Received: from aserp1040.oracle.com (HELO aserp1040.oracle.com) (141.146.126.69) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-SHA encrypted) ESMTPS; Mon, 09 Sep 2013 10:30:24 +0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-7.2 required=5.0 tests=AWL, BAYES_00, KHOP_THREADED, RCVD_IN_HOSTKARMA_W, RCVD_IN_HOSTKARMA_WL, RP_MATCHES_RCVD, SPF_PASS, UNPARSEABLE_RELAY autolearn=ham version=3.3.2 X-HELO: aserp1040.oracle.com Received: from ucsinet21.oracle.com (ucsinet21.oracle.com [156.151.31.93]) by aserp1040.oracle.com (Sentrion-MTA-4.3.1/Sentrion-MTA-4.3.1) with ESMTP id r89AUKvr025744 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Mon, 9 Sep 2013 10:30:21 GMT Received: from userz7021.oracle.com (userz7021.oracle.com [156.151.31.85]) by ucsinet21.oracle.com (8.14.4+Sun/8.14.4) with ESMTP id r89AUJbC008200 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Mon, 9 Sep 2013 10:30:19 GMT Received: from abhmt104.oracle.com (abhmt104.oracle.com [141.146.116.56]) by userz7021.oracle.com (8.14.4+Sun/8.14.4) with ESMTP id r89AUIlc008187; Mon, 9 Sep 2013 10:30:19 GMT Received: from poldo4.casa (/79.36.30.69) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Mon, 09 Sep 2013 03:30:18 -0700 Message-ID: <522DA338.1030305@oracle.com> Date: Mon, 09 Sep 2013 12:30:16 +0200 From: Paolo Carlini User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130801 Thunderbird/17.0.8 MIME-Version: 1.0 To: Jason Merrill CC: "gcc-patches@gcc.gnu.org" Subject: Re: [C++ Patch] PR 43452 References: <5229093C.6050904@oracle.com> <522D5017.1050101@redhat.com> In-Reply-To: <522D5017.1050101@redhat.com> X-IsSubscribed: yes Hi, On 09/09/2013 06:35 AM, Jason Merrill wrote: > On 09/05/2013 06:44 PM, Paolo Carlini wrote: >> + && warning (0, "possible problem detected in invocation of " >> + "delete [] operator:")) > > The warning should probably be suppressible by some flag. In fact clang has such a flag - I wasn't sure we wanted it because we come from an hard error - thus instead of inventing a name I choose the same name. Enabled by default anyway. Thanks! Paolo. ///////////////////// 2013-09-09 Paolo Carlini PR c++/43452 * doc/invoke.texi (-Wdelete-incomplete): Document it. /c-family 2013-09-09 Paolo Carlini PR c++/43452 * c.opt (Wdelete-incomplete): Add. /cp 2013-09-09 Paolo Carlini PR c++/43452 * init.c (build_vec_delete_1): When the type is incomplete emit a warning, enabled by default (not an error). (build_delete): Adjust to use OPT_Wdelete_incomplete. /testsuite 2013-09-09 Paolo Carlini PR c++/43452 * g++.dg/warn/Wdelete-incomplete-1.C: New. * g++.dg/warn/Wdelete-incomplete-2.C: Likewise. * g++.dg/init/delete1.C: Adjust. Index: c-family/c.opt =================================================================== --- c-family/c.opt (revision 202384) +++ c-family/c.opt (working copy) @@ -339,6 +339,10 @@ Wdeclaration-after-statement C ObjC Var(warn_declaration_after_statement) Warning Warn when a declaration is found after a statement +Wdelete-incomplete +C++ ObjC++ Var(warn_delete_incomplete) Init(1) Warning +Warn when deleting a pointer to incomplete type + Wdelete-non-virtual-dtor C++ ObjC++ Var(warn_delnonvdtor) Warning LangEnabledBy(C++ ObjC++,Wall) Warn about deleting polymorphic objects with non-virtual destructors Index: cp/init.c =================================================================== --- cp/init.c (revision 202384) +++ cp/init.c (working copy) @@ -3078,7 +3078,7 @@ build_vec_delete_1 (tree base, tree maxindex, tree { tree virtual_size; tree ptype = build_pointer_type (type = complete_type (type)); - tree size_exp = size_in_bytes (type); + tree size_exp; /* Temporary variables used by the loop. */ tree tbase, tbase_init; @@ -3106,6 +3106,23 @@ build_vec_delete_1 (tree base, tree maxindex, tree if (base == error_mark_node || maxindex == error_mark_node) return error_mark_node; + if (!COMPLETE_TYPE_P (type)) + { + if ((complain & tf_warning) + && warning (OPT_Wdelete_incomplete, + "possible problem detected in invocation of " + "delete [] operator:")) + { + cxx_incomplete_type_diagnostic (base, type, DK_WARNING); + inform (input_location, "neither the destructor nor the " + "class-specific operator delete [] will be called, " + "even if they are declared when the class is defined"); + } + return build_builtin_delete_call (base); + } + + size_exp = size_in_bytes (type); + if (! MAYBE_CLASS_TYPE_P (type) || TYPE_HAS_TRIVIAL_DESTRUCTOR (type)) goto no_destructor; @@ -3820,11 +3837,13 @@ build_delete (tree type, tree addr, special_functi if (!COMPLETE_TYPE_P (type)) { if ((complain & tf_warning) - && warning (0, "possible problem detected in invocation of " + && warning (OPT_Wdelete_incomplete, + "possible problem detected in invocation of " "delete operator:")) { cxx_incomplete_type_diagnostic (addr, type, DK_WARNING); - inform (input_location, "neither the destructor nor the class-specific " + inform (input_location, + "neither the destructor nor the class-specific " "operator delete will be called, even if they are " "declared when the class is defined"); } Index: doc/invoke.texi =================================================================== --- doc/invoke.texi (revision 202384) +++ doc/invoke.texi (working copy) @@ -240,8 +240,8 @@ Objective-C and Objective-C++ Dialects}. -Wno-attributes -Wno-builtin-macro-redefined @gol -Wc++-compat -Wc++11-compat -Wcast-align -Wcast-qual @gol -Wchar-subscripts -Wclobbered -Wcomment -Wconditionally-supported @gol --Wconversion -Wcoverage-mismatch -Wno-cpp -Wno-deprecated @gol --Wno-deprecated-declarations -Wdisabled-optimization @gol +-Wconversion -Wcoverage-mismatch -Wdelete-incomplete -Wno-cpp @gol +-Wno-deprecated -Wno-deprecated-declarations -Wdisabled-optimization @gol -Wno-div-by-zero -Wdouble-promotion -Wempty-body -Wenum-compare @gol -Wno-endif-labels -Werror -Werror=* @gol -Wfatal-errors -Wfloat-equal -Wformat -Wformat=2 @gol @@ -4490,6 +4490,12 @@ types. @option{-Wconversion-null} is enabled by de Warn when a literal '0' is used as null pointer constant. This can be useful to facilitate the conversion to @code{nullptr} in C++11. +@item -Wdelete-incomplete @r{(C++ and Objective-C++ only)} +@opindex Wdelete-incomplete +@opindex Wno-delete-incomplete +Warn when deleting a pointer to incomplete type, which may cause +undefined behavior at runtime. This warning is enabled by default. + @item -Wuseless-cast @r{(C++ and Objective-C++ only)} @opindex Wuseless-cast @opindex Wno-useless-cast Index: testsuite/g++.dg/init/delete1.C =================================================================== --- testsuite/g++.dg/init/delete1.C (revision 202384) +++ testsuite/g++.dg/init/delete1.C (working copy) @@ -1,7 +1,7 @@ // PR c++/19811 -class C; // { dg-error "forward" } +class C; // { dg-warning "forward" } void foo(void *p) { - delete [] ((C*)p) ; // { dg-error "" } + delete [] ((C*)p) ; // { dg-warning "problem|incomplete" } } Index: testsuite/g++.dg/warn/Wdelete-incomplete-1.C =================================================================== --- testsuite/g++.dg/warn/Wdelete-incomplete-1.C (revision 0) +++ testsuite/g++.dg/warn/Wdelete-incomplete-1.C (working copy) @@ -0,0 +1,7 @@ +// PR c++/43452 + +class Foo; // { dg-warning "forward" } +int main() { + Foo* p; // { dg-warning "incomplete" } + delete [] p; // { dg-warning "problem" } +} Index: testsuite/g++.dg/warn/Wdelete-incomplete-2.C =================================================================== --- testsuite/g++.dg/warn/Wdelete-incomplete-2.C (revision 0) +++ testsuite/g++.dg/warn/Wdelete-incomplete-2.C (working copy) @@ -0,0 +1,8 @@ +// PR c++/43452 +// { dg-options -Wno-delete-incomplete } + +class Foo; +int main() { + Foo* p; + delete [] p; +}