From patchwork Wed Oct 26 20:40:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 11439 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp474389wru; Wed, 26 Oct 2022 13:40:57 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7LzQZ0R6kmwgLAZczTOvIqT/ASQPVV4ze3KA3l0jh+RF9F4LdfDPtt2XKRzwt8yGsRm1CN X-Received: by 2002:a17:906:5a48:b0:78e:18a1:32ba with SMTP id my8-20020a1709065a4800b0078e18a132bamr39233281ejc.346.1666816857127; Wed, 26 Oct 2022 13:40:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666816857; cv=none; d=google.com; s=arc-20160816; b=e4iozGXOxb8pVBlPE5FkUcNAyhJggFVUqu+oh+PblbbY8uqcTIVwSdSYs1l/yvGH44 8U2RQbp8lhWBSQ90//aNQvC3i1dfe5E9QbssAqyDt3zOdf9W5q+sMAqhRdxqEV1BiV1Q Tj2dMx+XEb8zuVCDyCTqIiRJ7xZsnmsj6OYxDBCywVvPduGpMXv1B/DZ9tU7s62sztdv V45RG9+K4QlfpANr9jN/V65XzjjZzwC0AGB9ckEV5fSyA0jNSG1c9Fs2fpo4B57rq3ec cpXxdZilqGhsaQe4iMWaplQ6RFVzHPKCBtUBJXN8qO4kJPBxmLmleuOP4m+AAf9Sx4bv ytGg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:dmarc-filter:delivered-to :dkim-signature:dkim-filter; bh=vs/aQJNUOSdIpED5X/41Q5pBkCd/K7CYFSBvfP5AFKg=; b=YHMhdGkLPiUMVnxkvpeux6cw6qeYDJRHhEyOWTxgDJ+eWhglCGqN1VErB8dtv7hxwq FdrcSuvtiP6mhSVS6karEzcK4Vhap7wc2kjZcH5rWgiahgE/2T+qUIw294ylPyyvTiPb y0Y8QUR70Tjrz7fvGuH4PYiFsi52mM6LejK6EGsS9LkQniXsPkW+tQ/LBLr4fLlkWi68 2YtxV4YqEbnElcIMw+93toLKG/UK1tFV5/Xx9bkpljtBQT6hy4RahrRgD8rxz3nEQtUf n4Gl/Iy3WPFvv/9tq1FKLjuxHo8F4PQEQ2czOgpj0qFUESZtTf6tge9RCHtasaxc+hEZ genQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=io7lufNk; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id t11-20020a1709063e4b00b0078def5c29e6si5793356eji.531.2022.10.26.13.40.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 Oct 2022 13:40:57 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=io7lufNk; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2795B382C14F for ; Wed, 26 Oct 2022 20:40:56 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2795B382C14F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1666816856; bh=vs/aQJNUOSdIpED5X/41Q5pBkCd/K7CYFSBvfP5AFKg=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=io7lufNk2eRnr0q35tgVdCRAp3iry+ESYKHkQShKqneISLZquAkNUUv6IdFSMD7Ub /Y6BH6A22FIiMZibChEjLLxhFqK0HJt5jdoakVddW1WqkG6wkmheext/FbC6idnNxl ISVfWOBI0ixr4VJojo9bW7SjUQntf5H/gU3WAi9k= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org 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 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) 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 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 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-Patchwork-Original-From: David Malcolm via Gcc-patches From: David Malcolm Reply-To: David Malcolm 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?1747784152796432396?= X-GMAIL-MSGID: =?utf-8?q?1747784152796432396?= 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 */