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.129.124]) by sourceware.org (Postfix) with ESMTPS id 1AA753AA88EB for ; Tue, 12 Jul 2022 00:25:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 1AA753AA88EB 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-183-oy2tZDL6M2-03-f-aWgzJw-1; Mon, 11 Jul 2022 20:25:36 -0400 X-MC-Unique: oy2tZDL6M2-03-f-aWgzJw-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 4D9DB101A54E; Tue, 12 Jul 2022 00:25:36 +0000 (UTC) Received: from t14s.localdomain.com (unknown [10.2.16.236]) by smtp.corp.redhat.com (Postfix) with ESMTP id 10D8F400EA82; Tue, 12 Jul 2022 00:25:35 +0000 (UTC) From: David Malcolm To: Jonathan Wakely , gcc-patches@gcc.gnu.org Subject: [PATCH 1/2] Add gcc/make-unique.h Date: Mon, 11 Jul 2022 20:25:26 -0400 Message-Id: <20220712002527.417444-1-dmalcolm@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.11.54.1 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=-12.1 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_LOW, SPF_HELO_NONE, SPF_NONE, 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: , X-List-Received-Date: Tue, 12 Jul 2022 00:25:40 -0000 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). 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 This patch adds gcc/make-unique.h, containing a minimal C++11 implementation of make_unique (std::make_unique is C++14). gcc/ChangeLog: * make-unique.h: New file. Signed-off-by: David Malcolm --- gcc/make-unique.h | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 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..c99c5328545 --- /dev/null +++ b/gcc/make-unique.h @@ -0,0 +1,41 @@ +/* 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 it must be included from + system.h by defining INCLUDE_MEMORY in any source file using it. */ + +#ifndef INCLUDE_MEMORY +# error "You must define INCLUDE_MEMORY before including system.h to use make-unique.h" +#endif + +/* 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