From patchwork Wed Jun 7 15:01:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Thomas Schwinge X-Patchwork-Id: 104560 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp263250vqr; Wed, 7 Jun 2023 08:02:43 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5SK8VraplFzKGNZolPGEiDOE0hHCf4VU1uM1zpXj5odudju98gvEVhdtzwvVYe29+re0sC X-Received: by 2002:a17:907:1606:b0:973:ff4d:d01e with SMTP id hb6-20020a170907160600b00973ff4dd01emr7206969ejc.31.1686150163437; Wed, 07 Jun 2023 08:02:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686150163; cv=none; d=google.com; s=arc-20160816; b=Uqv/Eccq+sOoOdEInNcvgyeAi6TvbVl9x+4IhEq86dAh8I4ZeX/i+cfqZOT2fnkYu7 q/0Cb5wNZpDDN9tLiy45GCI5oJpp/NQmkTlZTeko8FH/xV8o7Y+YdDCf9ZLFM8r1s6ae AHL10GfLt3MbXS0OKrayqIhrvnuYeZcK8CE4cO/j6qIZTmvIu0U+3xOznteNIfajsDXX Fc3l4fotLJggZ/hGsB+nEgXJWG+ahzrnDXpF/BlJOz1yzWiM4ptBB5Iou9Bp0KeONIOh 1sfQ83WqaQL2Lzr3mzK+ZaNxxYIm/s48reOrw8UqsKPaHx1uiZce3ffNYVr0HBly6Kwr Azbw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:mime-version:message-id:date :user-agent:references:in-reply-to:subject:cc:to:from:ironport-sdr :dmarc-filter:delivered-to; bh=O6i64HmZvvXPIyzPh6qbq/EBzuxXh0bBm8CSkyBGM78=; b=HYiQh7TkJR+TXn54nyYiG2LwWa5EsMxx8g7pbmzNIbUmx6P44bmaAMZcJAPcdS+T23 tsXPEpLdjb4q1kDjintVOTscpLLvaQUvEwdWw+wW3QHPFGc0/cZa9QIx34FpwV2xBMZe PEUY60zrYFV3Eoan3RsxVIcfoGWDATU0aNv1Fttg8Utb8YUFbug5xZXLI/Cb8pRK05B4 jz9CYwWNHPq7SOz3rHsEMxRmKwmVyZTRomhjPVGNa37IN3hq4HGT8gL4yojBpdIXOEIe cUgk4r5BdAIn8DPfRbsPV8t1arR3ySJPnN2IVyzCfUkO8Pa9jOYGWPwzlQD7//1au5/M BG5Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id sa5-20020a1709076d0500b00969ee50a2bbsi9280957ejc.158.2023.06.07.08.02.43 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Jun 2023 08:02:43 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6F32E3857701 for ; Wed, 7 Jun 2023 15:02:36 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from esa1.mentor.iphmx.com (esa1.mentor.iphmx.com [68.232.129.153]) by sourceware.org (Postfix) with ESMTPS id 5B8BE3858C54 for ; Wed, 7 Jun 2023 15:02:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5B8BE3858C54 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=codesourcery.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=mentor.com X-IronPort-AV: E=Sophos;i="6.00,224,1681200000"; d="scan'208,223";a="9120331" Received: from orw-gwy-01-in.mentorg.com ([192.94.38.165]) by esa1.mentor.iphmx.com with ESMTP; 07 Jun 2023 07:02:00 -0800 IronPort-SDR: 3Gv5DXRRZFls092EtTmR1NXK/mb3XEW+OkRzdgoSF0d+FkUP9DUBSMYOE3guUGsninPIndgpNz p5N26hrqu3kk3z2sZ7jFMupGG7oW0aUdL2Anhv623TQoJEAgCP7Luq+uMxw1CLDqYJPWB8CXlB papZ5VlHfy2Ky2OeTVxs8PVd1OYNCYYHrdqqy/8Zw2SWatSMs/VyjnW2z4XuwWdRtFVEPVNbwg mAqjjeRZFg/ltbWEsKfR3xdBANHdfjuijrBlseKkjTlRlI+zgY2Ts+Tqq5lkC48sNvaicZhMBu I44= From: Thomas Schwinge To: , Rainer Orth , "Mike Stump" CC: Martin Sebor Subject: Tighten 'dg-warning' alternatives in 'c-c++-common/Wfree-nonheap-object{,-2,-3}.c' (was: [PATCH] correct -Wmismatched-new-delete (PR 98160, 98166)) In-Reply-To: <359da2e7-2207-0879-93b6-10bc019dc8d0@gmail.com> References: <359da2e7-2207-0879-93b6-10bc019dc8d0@gmail.com> User-Agent: Notmuch/0.29.3+94~g74c3f1b (https://notmuchmail.org) Emacs/28.2 (x86_64-pc-linux-gnu) Date: Wed, 7 Jun 2023 17:01:49 +0200 Message-ID: <87wn0f71uq.fsf@euler.schwinge.homeip.net> MIME-Version: 1.0 X-Originating-IP: [137.202.0.90] X-ClientProxiedBy: svr-ies-mbx-10.mgc.mentorg.com (139.181.222.10) To svr-ies-mbx-10.mgc.mentorg.com (139.181.222.10) X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, KAM_DMARC_STATUS, SPF_HELO_PASS, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1768056593401657868?= X-GMAIL-MSGID: =?utf-8?q?1768056593401657868?= Hi! On 2020-12-08T13:46:32-0700, Martin Sebor via Gcc-patches wrote: > The attached changes [...] ... eventually became commit fe7f75cf16783589eedbab597e6d0b8d35d7e470 "Correct/improve maybe_emit_free_warning (PR middle-end/98166, PR c++/57111, PR middle-end/98160)". > * c-c++-common/Wfree-nonheap-object-2.c: New test. > * c-c++-common/Wfree-nonheap-object-3.c: New test. > * c-c++-common/Wfree-nonheap-object.c: New test. OK to push the attached "Tighten 'dg-warning' alternatives in 'c-c++-common/Wfree-nonheap-object{,-2,-3}.c'"? Grüße Thomas > diff --git a/gcc/testsuite/c-c++-common/Wfree-nonheap-object-2.c b/gcc/testsuite/c-c++-common/Wfree-nonheap-object-2.c > new file mode 100644 > index 00000000000..0aedf1babbc > --- /dev/null > +++ b/gcc/testsuite/c-c++-common/Wfree-nonheap-object-2.c > @@ -0,0 +1,52 @@ > +/* PR middle-end/98166: bogus -Wmismatched-dealloc on user-defined allocator > + and inlining > + Verify that the allocator can be declared inline without a warning when > + it's associated with a standard deallocator. Associating an inline > + deallocator with an allocator would cause false positives when the former > + calls a deallocation function the allocator isn't associated with, so > + that triggers a warning on declaration. > + { dg-do compile } > + { dg-options "-O2 -Wall" } */ > + > +__attribute__ ((malloc (__builtin_free))) > +inline int* > +alloc_int (int n) > +{ > + return (int*)__builtin_malloc (n + sizeof (int)); > +} > + > +void test_nowarn_int (int n) > +{ > + { > + int *p = alloc_int (n); > + __builtin_free (p); > + } > + > + { > + int *p = alloc_int (n); > + __builtin_free (p + 1); // { dg-warning "\\\[-Wfree-nonheap-object" } > + } > +} > + > + > +inline void > +dealloc_long (long *p) > +{ > + __builtin_free (p); // { dg-warning "'__builtin_free|void __builtin_free\\(void\\*\\)' called on pointer 'p|' with nonzero offset" } > +} > + > +__attribute__ ((malloc (dealloc_long))) > +long* alloc_long (int); // { dg-warning "'malloc \\\(dealloc_long\\\)' attribute ignored with deallocation functions declared 'inline'" } > + > +void test_nowarn_long (int n) > +{ > + { > + long *p = alloc_long (n); > + dealloc_long (p); > + } > + > + { > + long *p = alloc_long (n); > + dealloc_long (p + 1); > + } > +} > diff --git a/gcc/testsuite/c-c++-common/Wfree-nonheap-object-3.c b/gcc/testsuite/c-c++-common/Wfree-nonheap-object-3.c > new file mode 100644 > index 00000000000..41a5b50362e > --- /dev/null > +++ b/gcc/testsuite/c-c++-common/Wfree-nonheap-object-3.c > @@ -0,0 +1,70 @@ > +/* PR middle-end/98166: bogus -Wmismatched-dealloc on user-defined allocator > + and inlining > + Verify that without inlining, both the allocator and the deallocator > + can be declared inline without a warning and that mismatched calls are > + detected, but that declaring them always_inline does trigger a warning. > + { dg-do compile } > + { dg-options "-Wall" } */ > + > +__attribute__ ((malloc (__builtin_free))) > +inline int* > +alloc_int (int n) > +{ > + return (int*)__builtin_malloc (n + sizeof (int)); > +} > + > +void test_nowarn_int (int n) > +{ > + { > + int *p = alloc_int (n); > + __builtin_free (p); > + } > + > + { > + int *p = alloc_int (n); > + __builtin_free (p + 1); // { dg-warning "'__builtin_free|void __builtin_free\\(void\\*\\)' called on pointer 'p|' with nonzero offset" } > + } > +} > + > + > +inline void > +dealloc_long (long *p) { __builtin_free (p); } > + > +__attribute__ ((malloc (dealloc_long))) > +long* alloc_long (int); > + > +void test_nowarn_long (int n) > +{ > + { > + long *p = alloc_long (n); > + dealloc_long (p); > + } > + > + { > + long *p = alloc_long (n); > + dealloc_long (p + 1); // { dg-warning "'dealloc_long' called on pointer 'p|' with nonzero offset" } > + } > +} > + > + > +inline __attribute__ ((always_inline)) void > +dealloc_float (float *p) // { dg-message "deallocation function declared here" } > +{ > + __builtin_free (p); // { dg-warning "'__builtin_free|void __builtin_free\\(void\\*\\)' called on pointer 'p|' with nonzero offset" } > +} > + > +__attribute__ ((malloc (dealloc_float))) > +float* alloc_float (int); // { dg-warning "'malloc \\(dealloc_float\\)' attribute ignored with deallocation functions declared 'inline'" } > + > +void test_nowarn_float (int n) > +{ > + { > + float *p = alloc_float (n); > + dealloc_float (p); > + } > + > + { > + float *p = alloc_float (n); > + dealloc_float (p + 2); > + } > +} > diff --git a/gcc/testsuite/c-c++-common/Wfree-nonheap-object.c b/gcc/testsuite/c-c++-common/Wfree-nonheap-object.c > new file mode 100644 > index 00000000000..dfbb296e9a7 > --- /dev/null > +++ b/gcc/testsuite/c-c++-common/Wfree-nonheap-object.c > @@ -0,0 +1,50 @@ > +/* Verify that built-in forms of functions can be used interchangeably > + with their ordinary (library) forms in attribute malloc. > + { dg-do compile } > + { dg-options "-Wall" } */ > + > +char* f (void) __attribute__ ((malloc (__builtin_free))); > + > +#if __cplusplus > +extern "C" { > +#endif > + > +void free (void*); > + > +#if __cplusplus > +} > +#endif > + > +char* g (void) __attribute__ ((malloc (free))); > + > + > +void test_nowarm (void) > +{ > + char *p = f (); > + free (p); > + > + p = g (); > + free (p); > + > + p = f (); > + __builtin_free (p); > + > + p = g (); > + __builtin_free (p); > +} > + > + > +void test_warn (void) > +{ > + char *p = f (); > + free (p + 1); // { dg-warning "'free|void free\\(void\\*\\)' called on pointer 'p|' with nonzero offset" } > + > + p = g (); > + free (p + 2); // { dg-warning "'free|void free\\(void\\*\\)' called on pointer 'p|' with nonzero offset" } > + > + p = f (); > + __builtin_free (p + 3); // { dg-warning "'__builtin_free|void __builtin_free\\(void\\*\\)' called on pointer 'p|' with nonzero offset" } > + > + p = g (); > + __builtin_free (p + 4); // { dg-warning "'__builtin_free|void __builtin_free\\(void\\*\\)' called on pointer 'p|' with nonzero offset" } > +} ----------------- Siemens Electronic Design Automation GmbH; Anschrift: Arnulfstraße 201, 80634 München; Gesellschaft mit beschränkter Haftung; Geschäftsführer: Thomas Heurung, Frank Thürauf; Sitz der Gesellschaft: München; Registergericht München, HRB 106955 From d9039d3d5b410075e2afa3a5a635f7399f072edd Mon Sep 17 00:00:00 2001 From: Thomas Schwinge Date: Wed, 7 Jun 2023 16:24:26 +0200 Subject: [PATCH] Tighten 'dg-warning' alternatives in 'c-c++-common/Wfree-nonheap-object{,-2,-3}.c' ..., added in commit fe7f75cf16783589eedbab597e6d0b8d35d7e470 "Correct/improve maybe_emit_free_warning (PR middle-end/98166, PR c++/57111, PR middle-end/98160)". These use alternatives like, for example, "AB|CDE|FG", but what really must've been meant is "A(B|C)D(E|F)G". The former variant also does "work": it matches any of "AB", or "CDE", or "FG", which are components of the latter variant. (That means, the former variant matches too loosely.) gcc/testsuite/ * c-c++-common/Wfree-nonheap-object-2.c: Tighten 'dg-warning' alternatives. * c-c++-common/Wfree-nonheap-object-3.c: Likewise. * c-c++-common/Wfree-nonheap-object.c: Likewise. --- gcc/testsuite/c-c++-common/Wfree-nonheap-object-2.c | 2 +- gcc/testsuite/c-c++-common/Wfree-nonheap-object-3.c | 6 +++--- gcc/testsuite/c-c++-common/Wfree-nonheap-object.c | 8 ++++---- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/gcc/testsuite/c-c++-common/Wfree-nonheap-object-2.c b/gcc/testsuite/c-c++-common/Wfree-nonheap-object-2.c index 0aedf1babbc..a2dbd181e33 100644 --- a/gcc/testsuite/c-c++-common/Wfree-nonheap-object-2.c +++ b/gcc/testsuite/c-c++-common/Wfree-nonheap-object-2.c @@ -32,7 +32,7 @@ void test_nowarn_int (int n) inline void dealloc_long (long *p) { - __builtin_free (p); // { dg-warning "'__builtin_free|void __builtin_free\\(void\\*\\)' called on pointer 'p|' with nonzero offset" } + __builtin_free (p); // { dg-warning "'(__builtin_free|void __builtin_free\\(void\\*\\))' called on pointer '(p|)' with nonzero offset" } } __attribute__ ((malloc (dealloc_long))) diff --git a/gcc/testsuite/c-c++-common/Wfree-nonheap-object-3.c b/gcc/testsuite/c-c++-common/Wfree-nonheap-object-3.c index 41a5b50362e..8f20de8455f 100644 --- a/gcc/testsuite/c-c++-common/Wfree-nonheap-object-3.c +++ b/gcc/testsuite/c-c++-common/Wfree-nonheap-object-3.c @@ -22,7 +22,7 @@ void test_nowarn_int (int n) { int *p = alloc_int (n); - __builtin_free (p + 1); // { dg-warning "'__builtin_free|void __builtin_free\\(void\\*\\)' called on pointer 'p|' with nonzero offset" } + __builtin_free (p + 1); // { dg-warning "'(__builtin_free|void __builtin_free\\(void\\*\\))' called on pointer '(p|)' with nonzero offset" } } } @@ -42,7 +42,7 @@ void test_nowarn_long (int n) { long *p = alloc_long (n); - dealloc_long (p + 1); // { dg-warning "'dealloc_long' called on pointer 'p|' with nonzero offset" } + dealloc_long (p + 1); // { dg-warning "'dealloc_long' called on pointer '(p|)' with nonzero offset" } } } @@ -50,7 +50,7 @@ void test_nowarn_long (int n) inline __attribute__ ((always_inline)) void dealloc_float (float *p) // { dg-message "deallocation function declared here" } { - __builtin_free (p); // { dg-warning "'__builtin_free|void __builtin_free\\(void\\*\\)' called on pointer 'p|' with nonzero offset" } + __builtin_free (p); // { dg-warning "'(__builtin_free|void __builtin_free\\(void\\*\\))' called on pointer '(p|)' with nonzero offset" } } __attribute__ ((malloc (dealloc_float))) diff --git a/gcc/testsuite/c-c++-common/Wfree-nonheap-object.c b/gcc/testsuite/c-c++-common/Wfree-nonheap-object.c index dfbb296e9a7..5f1e3fb28f3 100644 --- a/gcc/testsuite/c-c++-common/Wfree-nonheap-object.c +++ b/gcc/testsuite/c-c++-common/Wfree-nonheap-object.c @@ -37,14 +37,14 @@ void test_nowarm (void) void test_warn (void) { char *p = f (); - free (p + 1); // { dg-warning "'free|void free\\(void\\*\\)' called on pointer 'p|' with nonzero offset" } + free (p + 1); // { dg-warning "'(free|void free\\(void\\*\\))' called on pointer '(p|)' with nonzero offset" } p = g (); - free (p + 2); // { dg-warning "'free|void free\\(void\\*\\)' called on pointer 'p|' with nonzero offset" } + free (p + 2); // { dg-warning "'(free|void free\\(void\\*\\))' called on pointer '(p|)' with nonzero offset" } p = f (); - __builtin_free (p + 3); // { dg-warning "'__builtin_free|void __builtin_free\\(void\\*\\)' called on pointer 'p|' with nonzero offset" } + __builtin_free (p + 3); // { dg-warning "'(__builtin_free|void __builtin_free\\(void\\*\\))' called on pointer '(p|)' with nonzero offset" } p = g (); - __builtin_free (p + 4); // { dg-warning "'__builtin_free|void __builtin_free\\(void\\*\\)' called on pointer 'p|' with nonzero offset" } + __builtin_free (p + 4); // { dg-warning "'(__builtin_free|void __builtin_free\\(void\\*\\))' called on pointer '(p|)' with nonzero offset" } } -- 2.34.1