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 C721B3885532 for ; Wed, 26 Oct 2022 20:40:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org C721B3885532 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=1666816808; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=vs/aQJNUOSdIpED5X/41Q5pBkCd/K7CYFSBvfP5AFKg=; b=TTskLmChan1S1ywdpqX06el2LxMdrnDh/P8QHUwuptXRibz1lEPygvnNfPi3IWlBlApBjU J27DEiGE6EkJLvlYN1gxA42e4WvBjs82MtOlazcTriY64+75oGa+kea73zhIQg2pJgpVI2 OMgX92klfYxqN/rWPJr963fU9deh80E= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-190-hyJskFvePX-rWWMOUaIIyw-1; Wed, 26 Oct 2022 16:40:07 -0400 X-MC-Unique: hyJskFvePX-rWWMOUaIIyw-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 067F4811E84; Wed, 26 Oct 2022 20:40:07 +0000 (UTC) Received: from t14s.localdomain.com (unknown [10.2.17.189]) by smtp.corp.redhat.com (Postfix) with ESMTP id C9567C15BAB; Wed, 26 Oct 2022 20:40:06 +0000 (UTC) From: David Malcolm To: gcc-patches@gcc.gnu.org, Jonathan Wakely Cc: David Malcolm Subject: [PATCH v3] Add gcc/make-unique.h Date: Wed, 26 Oct 2022 16:40:05 -0400 Message-Id: <20221026204005.1864136-1-dmalcolm@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-11.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,KAM_SHORT,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: 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? 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 */ -- 2.26.3