From patchwork Wed Jun 28 04:42:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 1800855 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=IgaUOVot; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4QrTTV59KKz20ZV for ; Wed, 28 Jun 2023 14:42:37 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A9F3C3857701 for ; Wed, 28 Jun 2023 04:42:34 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A9F3C3857701 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1687927354; bh=F3wsj4IaQsBZ15azoCxnzk/dVUnr4ehL2gLAThzEEXk=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=IgaUOVotj6tDTZwV9nd+V9Zv5vkDQb4PqgGWABRfXLHNUeFCxGj1FknFzpKCvHqpJ qyER6LXSaubElJzOihKma/FJVOBVn5kHQNnrmoUr9n3XBGUW7AMevqgzviHoCG3Hy7 h5WdpgWsxik/7Qwc/9XAb1EO+Ni21i4wg+fEgizY= 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 3FB583858D32 for ; Wed, 28 Jun 2023 04:42:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3FB583858D32 Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-638-bEYXFO8CNdip1vHW90eYfA-1; Wed, 28 Jun 2023 00:42:05 -0400 X-MC-Unique: bEYXFO8CNdip1vHW90eYfA-1 Received: by mail-qk1-f200.google.com with SMTP id af79cd13be357-765986c0568so423275685a.1 for ; Tue, 27 Jun 2023 21:42:05 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687927325; x=1690519325; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=F3wsj4IaQsBZ15azoCxnzk/dVUnr4ehL2gLAThzEEXk=; b=PzjL6bUGoGLpc2Ldh5jlARTQwL8VNkAVnPocrnhwbgKtQswZUOJXJ5ABlJVl2cuv9b 3U1nW/GbXGrzqjOoeE3gJ+MNDl6IYcYq8mwhv9GsA0Res/qoo15JZVnXdbskJvk+VudU 35bTCe/fkMvCY2HuplrEOMNJqV3lg6nx5WURc9XSFtnniTGJPNN7/gNOo3OSE9hvkDMn JsGetd+Jb8klopC6ORX0q6koDSICn+KtZxxHIFEaJYfRahb+omvJ5Dzqn7E6FlCk2YVC 6pmZsY/POMKfjG+P1sgrlPK3lOUs08sADbniB7YjnhQH96cjphZ7TPyaoYQN+8KZMjxz epOA== X-Gm-Message-State: AC+VfDzozca8Rz82/dzxYZ1qXRMkFTt61uYLvaRoQnD7YFwpFvIXnFeQ dNKu9RvTc/MT4HKaDblF8wMKjnprj3YCurOr7WUwf8tADW8CZCFZfQ0Ggw9KQNv89jd/SrRDlXa 008t3USzTrMSg7l36zQ9G39DmK+2RUioVJ27xsjNPvm5ZmUL4/LbIqDVCvG8r02Ct6jxuQnm31g == X-Received: by 2002:a05:620a:4549:b0:763:b4d7:51c1 with SMTP id u9-20020a05620a454900b00763b4d751c1mr35824111qkp.50.1687927324922; Tue, 27 Jun 2023 21:42:04 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7XL1V/yyrSbLaE2LIOknGU03RGCyPIn/oXvPWqzbvW4mn7eCQJoR2nQ0Cn1ZIjg27zmi4eug== X-Received: by 2002:a05:620a:4549:b0:763:b4d7:51c1 with SMTP id u9-20020a05620a454900b00763b4d751c1mr35824088qkp.50.1687927324453; Tue, 27 Jun 2023 21:42:04 -0700 (PDT) Received: from jason.com (130-44-146-16.s12558.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.146.16]) by smtp.gmail.com with ESMTPSA id j13-20020a37c24d000000b00767209cfbcfsm209028qkm.78.2023.06.27.21.42.03 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Jun 2023 21:42:03 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [pushed] c++: inherited constructor attributes Date: Wed, 28 Jun 2023 00:42:00 -0400 Message-Id: <20230628044200.2636996-1-jason@redhat.com> X-Mailer: git-send-email 2.39.3 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, 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-Patchwork-Original-From: Jason Merrill via Gcc-patches From: Jason Merrill Reply-To: Jason Merrill Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Tested x86_64-pc-linux-gnu, applying to trunk. -- 8< -- Inherited constructors are like constructor clones; they don't exist from the language perspective, so they should copy the attributes in the same way. But it doesn't make sense to copy alias or ifunc attributes in either case. Unlike handle_copy_attribute, we do want to copy inlining attributes. The discussion of PR110334 pointed out that we weren't copying the always_inline attribute, leading to poor inlining choices. PR c++/110334 gcc/cp/ChangeLog: * cp-tree.h (clone_attrs): Declare. * method.cc (implicitly_declare_fn): Use it for inherited constructor. * optimize.cc (clone_attrs): New. (maybe_clone_body): Use it. gcc/testsuite/ChangeLog: * g++.dg/cpp1z/nodiscard-inh1.C: New test. --- gcc/cp/cp-tree.h | 1 + gcc/cp/method.cc | 2 ++ gcc/cp/optimize.cc | 26 ++++++++++++++++++++- gcc/testsuite/g++.dg/cpp1z/nodiscard-inh1.C | 15 ++++++++++++ 4 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/cpp1z/nodiscard-inh1.C base-commit: a1c6e9631ca33990a2b7411060ca4d18db081a7d diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index 83982233111..0d7a6c153dc 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -7282,6 +7282,7 @@ extern void module_preprocess_options (cpp_reader *); extern bool handle_module_option (unsigned opt, const char *arg, int value); /* In optimize.cc */ +extern tree clone_attrs (tree); extern bool maybe_clone_body (tree); /* In parser.cc */ diff --git a/gcc/cp/method.cc b/gcc/cp/method.cc index 91cf943f110..8ed967ddb21 100644 --- a/gcc/cp/method.cc +++ b/gcc/cp/method.cc @@ -3294,6 +3294,8 @@ implicitly_declare_fn (special_function_kind kind, tree type, /* Copy constexpr from the inherited constructor even if the inheriting constructor doesn't satisfy the requirements. */ constexpr_p = DECL_DECLARED_CONSTEXPR_P (inherited_ctor); + /* Also copy any attributes. */ + DECL_ATTRIBUTES (fn) = clone_attrs (DECL_ATTRIBUTES (inherited_ctor)); } /* Add the "this" parameter. */ diff --git a/gcc/cp/optimize.cc b/gcc/cp/optimize.cc index f73d86b6c6b..9e8926e4cc6 100644 --- a/gcc/cp/optimize.cc +++ b/gcc/cp/optimize.cc @@ -28,6 +28,7 @@ along with GCC; see the file COPYING3. If not see #include "debug.h" #include "tree-inline.h" #include "tree-iterator.h" +#include "attribs.h" /* Prototypes. */ @@ -446,6 +447,29 @@ maybe_thunk_body (tree fn, bool force) return 1; } +/* Copy most attributes from ATTRS, omitting attributes that can really only + apply to a single decl. */ + +tree +clone_attrs (tree attrs) +{ + tree new_attrs = NULL_TREE; + tree *p = &new_attrs; + + for (tree a = attrs; a; a = TREE_CHAIN (a)) + { + tree aname = get_attribute_name (a); + if (is_attribute_namespace_p ("", a) + && (is_attribute_p ("alias", aname) + || is_attribute_p ("ifunc", aname))) + continue; + *p = copy_node (a); + p = &TREE_CHAIN (*p); + } + *p = NULL_TREE; + return new_attrs; +} + /* FN is a function that has a complete body. Clone the body as necessary. Returns nonzero if there's no longer any need to process the main body. */ @@ -503,7 +527,7 @@ maybe_clone_body (tree fn) DECL_VISIBILITY (clone) = DECL_VISIBILITY (fn); DECL_VISIBILITY_SPECIFIED (clone) = DECL_VISIBILITY_SPECIFIED (fn); DECL_DLLIMPORT_P (clone) = DECL_DLLIMPORT_P (fn); - DECL_ATTRIBUTES (clone) = copy_list (DECL_ATTRIBUTES (fn)); + DECL_ATTRIBUTES (clone) = clone_attrs (DECL_ATTRIBUTES (fn)); DECL_DISREGARD_INLINE_LIMITS (clone) = DECL_DISREGARD_INLINE_LIMITS (fn); set_decl_section_name (clone, fn); diff --git a/gcc/testsuite/g++.dg/cpp1z/nodiscard-inh1.C b/gcc/testsuite/g++.dg/cpp1z/nodiscard-inh1.C new file mode 100644 index 00000000000..bc2555930f1 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1z/nodiscard-inh1.C @@ -0,0 +1,15 @@ +// [[nodiscard]] should apply to inherited constructors. +// { dg-do compile { target c++11 } } + +struct A { + [[nodiscard]] A(int); +}; + +struct B: A { + using A::A; +}; + +int main() +{ + B(42); // { dg-warning nodiscard } +}