From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id DADC53858D32 for ; Wed, 2 Nov 2022 21:46:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org DADC53858D32 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1667425563; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=R8vUwxUzBH5LiU00/vMn8vi0eT1NN7M1nnwPfcwP+NQ=; b=jBPSeGB8ttrMh+l/Snm42YoadPufaYTMnDmckeME1z7zr01hQ7mFFeQ2XtqyobYSDkDzMB qO0SCYRcFOapuqtmnpdU+VsyztCZ8FPP7mtHbF3aJy7GITCr+/GoIjljAn98TaAThqWLsf avCIp5JFYFZX4VMMekem9iKacKDjU3Y= Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-26--8b5Kj8IMZug0c5QzE_Zkw-1; Wed, 02 Nov 2022 17:46:02 -0400 X-MC-Unique: -8b5Kj8IMZug0c5QzE_Zkw-1 Received: by mail-qv1-f70.google.com with SMTP id e13-20020ad450cd000000b004bb49d98da4so10845498qvq.9 for ; Wed, 02 Nov 2022 14:46:02 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=R8vUwxUzBH5LiU00/vMn8vi0eT1NN7M1nnwPfcwP+NQ=; b=lL2qjARxcpifoP2sfNVSrvMnsy/SWFQZLRGAkt1q+qbySQaG7qn8/GCR9hJp10RP6O 5lRfdpA3HQLvBZfiI+qd+/8U0yV+kX1KnM7hY648WRuud3oH9LDjbPWoUjQmzWi/P9Wo 7YMxdP6wZVHwjpj1hKm9Hhy9NFSQ2/RBrFv/SI3NYFBUAt6wtwvou7WDeN8tO+X0VfpN BeNDShnOv0o1vIZkbEWCO9gyJSavHEuABVPgpak4b61IG7zP2BUBeksmxuWlDNxN6R2O ua/wiTmfRQe07bk+LOEmwtmVFOhjm0q7uG55k7/YNGOeNkxszwZ/OzcwNAychB/EJCQK xQLg== X-Gm-Message-State: ACrzQf2LAUhOGcaaVCWjLM5Jp4UbV4vLYyXcp0JlMO50olz+ErZeZuVC 5UKbZFUBRvoOUtwGH1IEujeNSpym3b855F9Tlut4B/ZH0r3JjA9C+QajG8xBJ+b2yalj61oZ+xL BavOfOWJJwxj8OQsy1A== X-Received: by 2002:a05:620a:4588:b0:6fa:36d2:1c6b with SMTP id bp8-20020a05620a458800b006fa36d21c6bmr11509744qkb.21.1667425561854; Wed, 02 Nov 2022 14:46:01 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4tjTym4n/aajLqlCy1p7RfwIFU72byh2HulpKB63YGVDYn9wC7kfuxCn9KuWB5buMuSAWrLQ== X-Received: by 2002:a05:620a:4588:b0:6fa:36d2:1c6b with SMTP id bp8-20020a05620a458800b006fa36d21c6bmr11509713qkb.21.1667425561460; Wed, 02 Nov 2022 14:46:01 -0700 (PDT) Received: from [192.168.1.101] (130-44-159-43.s15913.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.159.43]) by smtp.gmail.com with ESMTPSA id f13-20020ac8134d000000b003998bb7b83asm7196994qtj.90.2022.11.02.14.46.00 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 02 Nov 2022 14:46:00 -0700 (PDT) Message-ID: <0f35ef7a-5db5-ac87-a862-4b80903a4b91@redhat.com> Date: Wed, 2 Nov 2022 17:45:59 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.4.1 Subject: Re: [PATCH v3] Add gcc/make-unique.h To: David Malcolm , gcc-patches@gcc.gnu.org, Jonathan Wakely References: <20221026204005.1864136-1-dmalcolm@redhat.com> From: Jason Merrill In-Reply-To: <20221026204005.1864136-1-dmalcolm@redhat.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-13.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,KAM_SHORT,NICE_REPLY_A,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On 10/26/22 16:40, David Malcolm via Gcc-patches wrote: > Changed in v3: added include of > v2: https://gcc.gnu.org/pipermail/gcc-patches/2022-October/604137.html > v1: https://gcc.gnu.org/pipermail/gcc-patches/2022-July/598189.html > > On Tue, 2022-07-12 at 07:48 +0100, Jonathan Wakely wrote: >> On Tue, 12 Jul 2022, 01:25 David Malcolm, >> wrote: >> >>> On Fri, 2022-07-08 at 22:16 +0100, Jonathan Wakely wrote: >>>> On Fri, 8 Jul 2022 at 21:47, David Malcolm via Gcc >>>> >>>> wrote: >>>>> >>>>> std::unique_ptr is C++11, and I'd like to use it in the >>>>> gcc/analyzer >>>>> subdirectory, at least. The following patch eliminates a bunch >>>>> of >>>>> "takes ownership" comments and manual "delete" invocations in >>>>> favor >>>>> of simply using std::unique_ptr. >>>>> >>>>> The problem is that the patch makes use of std::make_unique, >>>>> but >>>>> that >>>>> was added in C++14. >>>>> >>>>> I've heard that it's reasonably easy to reimplement >>>>> std::make_unique, >>>>> but I'm not sure that my C++11 skills are up to it. >>>> >>>> You know we have an implementation of std::make_unique in GCC, >>>> with a >>>> GCC-compatible licence that you can look at, right? :-) >>>> >>>> But it's not really necessary. There are only two reasons to >>>> prefer >>>> make_unique over just allocating an object with new and >>>> constructing >>>> a >>>> unique_ptr from it: >>>> >>>> 1) avoid a "naked" new in your code (some coding styles like >>>> this, >>>> but >>>> it's not really important as long as the 'delete' is managed >>>> automatically by unique_ptr). >>>> >>>> 2) exception-safety when allocating multiple objects as args to a >>>> function, see https://herbsutter.com/gotw/_102/ for details. >>>> Irrelevant for GCC, because we build without exceptions. >>> >>> [moving from gcc to gcc-patches mailing list] >>> >>> Also, I *think* it's a lot less typing, since I can write just: >>> >>> std::make_unique (args) >>> >>> rather than >>> >>> std::unique_ptr (new >>> name_of_type_which_could_be_long (args)); >>> >>>> >>>> >>>> >>>>> Is there: >>>>> (a) an easy way to implement a std::make_unique replacement >>>>> (e.g. in system.h? what to call it?), or >>>> >>>> If you don't care about using it to create unique_ptr >>>> arrays, >>>> it's trivial: >>>> >>>> template >>>> inline typename std::enable_if::value, >>>> std::unique_ptr>::type >>>> make_unique(Args&&... args) >>>> { return std::unique_ptr(new >>>> T(std::forward(args)...)); >>>> } >>>> >>>> To add the overload that works for arrays is a little trickier. >>> >>> Thanks! >>> >>> I tried adding it to gcc/system.h, but anything that uses it needs >>> to >>> have std::unique_ptr declared, which meant forcibly including >>> >>> from gcc/system.h >>> >>> So instead, here's a patch that adds a new gcc/make-unique.h >>> header, >>> containing just the template decl above (in the root namespace, >>> rather >>> than std::, which saves a bit more typing). >>> >> >> Adding things to std isn't allowed anyway, so that's correct. >> >> >>> I've successfully bootstrapped®ression-tested a version of my >>> earlier >>> analyzer patch that uses this patch (see patch 2 of the kit, which >>> has >>> lots of usage examples). >>> >>> OK for trunk? >>> >>> Dave > > [...snip...] > >>> +#ifndef INCLUDE_MEMORY >>> +# error "You must define INCLUDE_MEMORY before including system.h >>> to use >>> make-unique.h" >>> +#endif >>> >> >> You also need for the enable_if and is_array traits. >> With >> libstdc++ that gets included by but that's guaranteed for >> other >> library implementations. >> >> I don't know if that had the same kind of issues as other system >> headers or >> if it can just be included here. > > I've added an include of in this version of the patch. > >> >> >> + >>> +/* Minimal implementation of make_unique for C++11 compatibility >>> + (std::make_unique is C++14). */ >>> + >>> +template >>> +inline typename std::enable_if::value, >>> std::unique_ptr>::type >>> +make_unique(Args&&... args) >>> +{ >>> + return std::unique_ptr (new T (std::forward >>> (args)...)); >>> +} >>> + >>> +#endif /* ! GCC_MAKE_UNIQUE */ >>> -- >>> 2.26.3 >>> >>> > > This patch adds gcc/make-unique.h, containing a minimal C++11 > implementation of make_unique (std::make_unique is C++14). > > Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu > in conjunction with a followup series of patches which use this > in dozens of places in the analyzer. > > OK for trunk? OK. > gcc/ChangeLog: > * make-unique.h: New file. > --- > gcc/make-unique.h | 44 ++++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 44 insertions(+) > create mode 100644 gcc/make-unique.h > > diff --git a/gcc/make-unique.h b/gcc/make-unique.h > new file mode 100644 > index 00000000000..c9a7d6ef6ce > --- /dev/null > +++ b/gcc/make-unique.h > @@ -0,0 +1,44 @@ > +/* Minimal implementation of make_unique for C++11 compatibility. > + Copyright (C) 2022 Free Software Foundation, Inc. > + > +This file is part of GCC. > + > +GCC is free software; you can redistribute it and/or modify it under > +the terms of the GNU General Public License as published by the Free > +Software Foundation; either version 3, or (at your option) any later > +version. > + > +GCC is distributed in the hope that it will be useful, but WITHOUT ANY > +WARRANTY; without even the implied warranty of MERCHANTABILITY or > +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License > +for more details. > + > +You should have received a copy of the GNU General Public License > +along with GCC; see the file COPYING3. If not see > +. */ > + > +#ifndef GCC_MAKE_UNIQUE > +#define GCC_MAKE_UNIQUE > + > +/* This header uses std::unique_ptr, but can't be directly > + included due to issues with macros. Hence must be included > + from system.h by defining INCLUDE_MEMORY in any source file using > + make-unique.h. */ > + > +#ifndef INCLUDE_MEMORY > +# error "You must define INCLUDE_MEMORY before including system.h to use make-unique.h" > +#endif > + > +#include > + > +/* Minimal implementation of make_unique for C++11 compatibility > + (std::make_unique is C++14). */ > + > +template > +inline typename std::enable_if::value, std::unique_ptr>::type > +make_unique(Args&&... args) > +{ > + return std::unique_ptr (new T (std::forward (args)...)); > +} > + > +#endif /* ! GCC_MAKE_UNIQUE */