From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 31264 invoked by alias); 27 May 2016 16:31:57 -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 31199 invoked by uid 89); 27 May 2016 16:31:57 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.4 required=5.0 tests=AWL,BAYES_00,FREEMAIL_FROM,RCVD_IN_DNSWL_LOW,SPF_PASS autolearn=ham version=3.3.2 spammy=ps2, 24048, sk:msebor@, sk:msebor X-HELO: mail-qk0-f176.google.com Received: from mail-qk0-f176.google.com (HELO mail-qk0-f176.google.com) (209.85.220.176) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-GCM-SHA256 encrypted) ESMTPS; Fri, 27 May 2016 16:31:46 +0000 Received: by mail-qk0-f176.google.com with SMTP id x7so83039017qkd.3 for ; Fri, 27 May 2016 09:31:46 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:to:from:subject:message-id:date:user-agent :mime-version; bh=fJKZgy2DMBFtgj3FRZiH1CbxZHZ5mW8qdsrlqopIdKQ=; b=Y9vgXEJxnC/xpAzT5tVCiY+Jz44e2WLRloIGHID1gAF4WrUS5kfzeqwzVldslyIAqW 7HfRgaB0cs0jW2RDN/ughu/HgFOC7x+06VsAJaVG2D1t8hCkcryGVvYosImU6qQPAW72 3s6kGRMEyd4aCtMYjkGRYNuFiQOIYEA4rq4H1s5VkW1mCc68C3WU1OlF5QZSkP3PBH0h CxbwAOE6e+9YxvFgDmuk7ZFMvZdUECrStcx/Gjd+hY57qYFY8xIPQDiVVZcOT/ao0MCE qKkGOtdfkXlN4FD3oIjAI7Svv6jfOQ9lwfTVAeiPGBRGZWXzWoFgiFeaNeEO1cLJwnQG phAw== X-Gm-Message-State: ALyK8tK9pKLVlElVAoOL7CU4G6IZR+NzfStnqoegkgO5ZO0IIIavXS6YhFiUs74qXa7crQ== X-Received: by 10.55.41.79 with SMTP id p76mr2103983qkh.120.1464366704628; Fri, 27 May 2016 09:31:44 -0700 (PDT) Received: from [192.168.0.26] (75-166-197-76.hlrn.qwest.net. [75.166.197.76]) by smtp.gmail.com with ESMTPSA id s84sm3612909qhc.13.2016.05.27.09.31.43 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 27 May 2016 09:31:44 -0700 (PDT) To: Gcc Patch List From: Martin Sebor Subject: [PATCH] c++/71306 - bogus -Wplacement-new with an array element Message-ID: <5748766E.3010803@gmail.com> Date: Fri, 27 May 2016 19:36:00 -0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.1.0 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="------------010909010604040007040605" X-IsSubscribed: yes X-SW-Source: 2016-05/txt/msg02220.txt.bz2 This is a multi-part message in MIME format. --------------010909010604040007040605 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Content-length: 448 It was pointed out on gcc-help last night that the -Wplacement-new warning issues a false positive when a placement new expression is invoked with an operand that is an element of an array of pointers (to buffers of unknown size). The attached patch adjusts the warning so as to avoid this false positive. The patch also removes the pointless loop that Jakub questioned below: https://gcc.gnu.org/ml/gcc-patches/2016-04/msg00050.html Martin --------------010909010604040007040605 Content-Type: text/x-patch; name="gcc-71306.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="gcc-71306.patch" Content-length: 2668 PR c++/71306 - bogus -Wplacement-new with an array element gcc/cp/ChangeLog: 2016-05-27 Martin Sebor PR c++/71306 * init.c (warn_placement_new_too_small): Handle placement new arguments that are elements of arrays more carefully. Remove a pointless loop. gcc/testsuite/ChangeLog: 2016-05-27 Martin Sebor PR c++/71306 * g++.dg/warn/Wplacement-new-size-3.C: New test. diff --git a/gcc/cp/init.c b/gcc/cp/init.c index 681ca12..9cbd43f 100644 --- a/gcc/cp/init.c +++ b/gcc/cp/init.c @@ -2375,7 +2375,8 @@ warn_placement_new_too_small (tree type, tree nelts, tree size, tree oper) STRIP_NOPS (oper); - if (TREE_CODE (oper) == ARRAY_REF) + if (TREE_CODE (oper) == ARRAY_REF + && (addr_expr || TREE_CODE (TREE_TYPE (oper)) == ARRAY_TYPE)) { /* Similar to the offset computed above, see if the array index is a compile-time constant. If so, and unless the offset was @@ -2404,8 +2405,8 @@ warn_placement_new_too_small (tree type, tree nelts, tree size, tree oper) bool compref = TREE_CODE (oper) == COMPONENT_REF; /* Descend into a struct or union to find the member whose address - is being used as the agument. */ - while (TREE_CODE (oper) == COMPONENT_REF) + is being used as the argument. */ + if (TREE_CODE (oper) == COMPONENT_REF) { tree op0 = oper; while (TREE_CODE (op0 = TREE_OPERAND (op0, 0)) == COMPONENT_REF); diff --git a/gcc/testsuite/g++.dg/warn/Wplacement-new-size-3.C b/gcc/testsuite/g++.dg/warn/Wplacement-new-size-3.C new file mode 100644 index 0000000..f5dd642 --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wplacement-new-size-3.C @@ -0,0 +1,40 @@ +// PR c++/71306 - bogus -Wplacement-new with an array element +// { dg-do compile } +// { dg-options "-Wplacement-new" } + +void* operator new (__SIZE_TYPE__, void *p) { return p; } + +struct S64 { char c [64]; }; + +S64 s2 [2]; +S64* ps2 [2]; +S64* ps2_2 [2][2]; + +void* pv2 [2]; + +void f () +{ + char a [2][sizeof (S64)]; + + new (a) S64; + new (a [0]) S64; + new (a [1]) S64; + + // Verify there is no warning with buffers of sufficient size. + new (&s2 [0]) S64; + new (&s2 [1]) S64; + + // ..and no warning with pointers to buffers of unknown size. + new (ps2 [0]) S64; + new (ps2 [1]) S64; + + // But a warning when using the ps2_2 array itself as opposed + // to the pointers it's elements might point to. + new (ps2_2 [0]) S64; // { dg-warning "placement new" } + new (ps2_2 [1]) S64; // { dg-warning "placement new" } + + // ..and no warning again with pointers to buffers of unknown + // size. + new (pv2 [0]) S64; + new (pv2 [1]) S64; +} --------------010909010604040007040605--