From patchwork Tue May 10 05:05:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 1628931 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.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=U/mVE+uL; dkim-atps=neutral Authentication-Results: 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=) 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 RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4Ky5bs0sdLz9sG6 for ; Tue, 10 May 2022 15:06:15 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E7BFC38425B0 for ; Tue, 10 May 2022 05:06:11 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E7BFC38425B0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1652159171; bh=fk+gXSLo4aInJIQXd4srPjufYTtddyXzSNs3+bvQd3M=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=U/mVE+uLhOkgxyWp+olkWLYZ3KVQYoXfVo8yqcs+pGw0HKZPhIZN4xt/A3o5SCM1Y 31TntXQ5TWNOQS2V/fIBGU/TxCOd5/r/fbhMIDE1ucZs1atuPhTo1nFoiEyVzgtKIt N4Enq33x9CcNTsqthbNjGrlB3LlYjKiOCqyrUXQo= 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.129.124]) by sourceware.org (Postfix) with ESMTPS id 101B83838019 for ; Tue, 10 May 2022 05:05:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 101B83838019 Received: from mail-qk1-f197.google.com (mail-qk1-f197.google.com [209.85.222.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-251-TDfZpSLuN3OSUPNws0mhgw-1; Tue, 10 May 2022 01:05:37 -0400 X-MC-Unique: TDfZpSLuN3OSUPNws0mhgw-1 Received: by mail-qk1-f197.google.com with SMTP id a5-20020ae9e805000000b006a034b31384so7802101qkg.8 for ; Mon, 09 May 2022 22:05:37 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=fk+gXSLo4aInJIQXd4srPjufYTtddyXzSNs3+bvQd3M=; b=miAysxrfPl3Sq6XYeFWacvL2w2/JUCtsAViEb9n/oRCBHN2ibIfjHv+0jj1pZAEXFW FoHgfmVpAlEuzIeBiBFbtqkJ6Bz/yXp8vaMJjxeA9oqPp7nw1cyvsvc12+sMQiYwMp7B 3UCZeZZOf6Mw6yNhyAmcUCLlTPnIdvfcy1yk5Faxq4BuuDkzscAP4n9pZbSlvfH/h30Y hJxYv01qkvpahUWbhI/O5qK7vMpeyt9km9MAE0goJJHaPLiZjaFOWPKXuB0TvZCn+Qzv ss9wOSqsoxoBXsSiuezdIv6yftq6Z6+iTuJSt1e/yJau0jAZef9aQktHeGaj+Kg3DMC7 HR7Q== X-Gm-Message-State: AOAM532HM2OyfjbehMPh9vGhIsKLj3Iv7cy7QHwmibU5/PvJHjWCjdKu P21Iv7Zx6Knb3ygR18VD+kk+GDDyJvhaQHOjJDHMdstmXBoWCPckDIKmoshNTUQhAa8tDu+6zyx jYApvgF1Nd1q3Y0irP7DazWnSP/YAixtPx9Yt2bw6S6CVnXrYZzF1teqLxji1v1sDvQ== X-Received: by 2002:a05:6214:20e2:b0:45a:84aa:b10c with SMTP id 2-20020a05621420e200b0045a84aab10cmr16410411qvk.130.1652159136158; Mon, 09 May 2022 22:05:36 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyh8duVpc43dnH4E5hzUXkx4wGEha1ziRPdslZiPzbivzjvZ5vwSutrA3H/T8/bc1v/do/F2A== X-Received: by 2002:a05:6214:20e2:b0:45a:84aa:b10c with SMTP id 2-20020a05621420e200b0045a84aab10cmr16410388qvk.130.1652159135632; Mon, 09 May 2022 22:05:35 -0700 (PDT) Received: from barrymore.redhat.com (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 j5-20020ac84045000000b002f39b99f6a1sm9682409qtl.59.2022.05.09.22.05.33 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 May 2022 22:05:34 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [pushed] c++: fix arm-eabi crash building libstdc++ [PR105529] Date: Tue, 10 May 2022 01:05:32 -0400 Message-Id: <20220510050532.3301176-1-jason@redhat.com> X-Mailer: git-send-email 2.27.0 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-26.5 required=5.0 tests=BAYES_00, DKIM_INVALID, DKIM_SIGNED, GIT_PATCH_0, KAM_DMARC_NONE, KAM_DMARC_STATUS, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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" My recent change to cxx_eval_store_expression asserts that the target and value can only end up having different types in the case of an empty base; this was crashing arm-eabi compilers because in that ABI [cd]tors return *this, and weren't converting it to void* first. This also shares the 'return this' code between the three places it occurs. Thanks to Marek for the tests. Tested x86_64-pc-linux-gnu and arm-sim, applying to trunk. PR c++/105529 gcc/cp/ChangeLog: * decl.cc (maybe_return_this): Replace... (finish_constructor_body, finish_destructor_body): ...these. (finish_function_body): Call it. * optimize.cc (build_delete_destructor_body): Call it. * cp-tree.h (maybe_return_this): Declare. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/constexpr-dtor13.C: New test. * g++.dg/cpp2a/constexpr-dtor14.C: New test. --- gcc/cp/cp-tree.h | 1 + gcc/cp/decl.cc | 47 ++++--------------- gcc/cp/optimize.cc | 9 +--- gcc/testsuite/g++.dg/cpp2a/constexpr-dtor13.C | 11 +++++ gcc/testsuite/g++.dg/cpp2a/constexpr-dtor14.C | 13 +++++ 5 files changed, 36 insertions(+), 45 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp2a/constexpr-dtor13.C create mode 100644 gcc/testsuite/g++.dg/cpp2a/constexpr-dtor14.C base-commit: 0c7bce0ac184c057bacad9c8e615ce82923835fd diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index 7e50db0e35a..9fb07d8ea39 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -6841,6 +6841,7 @@ extern tree lookup_enumerator (tree, tree); extern bool start_preparsed_function (tree, tree, int); extern bool start_function (cp_decl_specifier_seq *, const cp_declarator *, tree); +extern void maybe_return_this (void); extern tree begin_function_body (void); extern void finish_function_body (tree); extern tree outer_curly_brace_block (tree); diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index 872b02d55bd..9c9cf9f7f6b 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -86,9 +86,7 @@ static tree check_initializer (tree, tree, int, vec **); static void make_rtl_for_nonlocal_decl (tree, tree, const char *); static void copy_type_enum (tree , tree); static void check_function_type (tree, tree); -static void finish_constructor_body (void); static void begin_destructor_body (void); -static void finish_destructor_body (void); static void record_key_method_defined (tree); static tree create_array_type_for_decl (tree, tree, tree, location_t); static tree get_atexit_node (void); @@ -17502,22 +17500,20 @@ store_parm_decls (tree current_function_parms) } -/* Set the return value of the constructor (if present). */ +/* Set the return value of the [cd]tor if the ABI wants that. */ -static void -finish_constructor_body (void) +void +maybe_return_this (void) { - tree val; - tree exprstmt; - if (targetm.cxx.cdtor_returns_this ()) { - val = DECL_ARGUMENTS (current_function_decl); + /* Return the address of the object. */ + tree val = DECL_ARGUMENTS (current_function_decl); suppress_warning (val, OPT_Wuse_after_free); + val = fold_convert (TREE_TYPE (DECL_RESULT (current_function_decl)), val); val = build2 (MODIFY_EXPR, TREE_TYPE (val), DECL_RESULT (current_function_decl), val); - /* Return the address of the object. */ - exprstmt = build_stmt (input_location, RETURN_EXPR, val); + tree exprstmt = build_stmt (input_location, RETURN_EXPR, val); add_stmt (exprstmt); } } @@ -17590,28 +17586,6 @@ begin_destructor_body (void) } } -/* At the end of every destructor we generate code to delete the object if - necessary. Do that now. */ - -static void -finish_destructor_body (void) -{ - tree exprstmt; - - if (targetm.cxx.cdtor_returns_this ()) - { - tree val; - - val = DECL_ARGUMENTS (current_function_decl); - suppress_warning (val, OPT_Wuse_after_free); - val = build2 (MODIFY_EXPR, TREE_TYPE (val), - DECL_RESULT (current_function_decl), val); - /* Return the address of the object. */ - exprstmt = build_stmt (input_location, RETURN_EXPR, val); - add_stmt (exprstmt); - } -} - /* Do the necessary processing for the beginning of a function body, which in this case includes member-initializers, but not the catch clauses of a function-try-block. Currently, this means opening a binding level @@ -17662,10 +17636,9 @@ finish_function_body (tree compstmt) if (processing_template_decl) /* Do nothing now. */; - else if (DECL_CONSTRUCTOR_P (current_function_decl)) - finish_constructor_body (); - else if (DECL_DESTRUCTOR_P (current_function_decl)) - finish_destructor_body (); + else if (DECL_CONSTRUCTOR_P (current_function_decl) + || DECL_DESTRUCTOR_P (current_function_decl)) + maybe_return_this (); } /* Given a function, returns the BLOCK corresponding to the outermost level diff --git a/gcc/cp/optimize.cc b/gcc/cp/optimize.cc index 13ab8b7361e..5c134fd2fed 100644 --- a/gcc/cp/optimize.cc +++ b/gcc/cp/optimize.cc @@ -163,14 +163,7 @@ build_delete_destructor_body (tree delete_dtor, tree complete_dtor) /* Return the address of the object. ??? How is it useful to return an invalid address? */ - if (targetm.cxx.cdtor_returns_this ()) - { - tree val = DECL_ARGUMENTS (delete_dtor); - suppress_warning (val, OPT_Wuse_after_free); - val = build2 (MODIFY_EXPR, TREE_TYPE (val), - DECL_RESULT (delete_dtor), val); - add_stmt (build_stmt (0, RETURN_EXPR, val)); - } + maybe_return_this (); } /* Return name of comdat group for complete and base ctor (or dtor) diff --git a/gcc/testsuite/g++.dg/cpp2a/constexpr-dtor13.C b/gcc/testsuite/g++.dg/cpp2a/constexpr-dtor13.C new file mode 100644 index 00000000000..7b289614fc1 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/constexpr-dtor13.C @@ -0,0 +1,11 @@ +// PR c++/105529 +// { dg-do compile { target c++20 } } +// { dg-options "-O" } + +struct allocator { + constexpr ~allocator() {} +}; +struct S { + S(int, int, allocator = allocator()); +}; +void to_string() { S(0, '\0'); } diff --git a/gcc/testsuite/g++.dg/cpp2a/constexpr-dtor14.C b/gcc/testsuite/g++.dg/cpp2a/constexpr-dtor14.C new file mode 100644 index 00000000000..9c55121eb8a --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/constexpr-dtor14.C @@ -0,0 +1,13 @@ +// PR c++/105529 +// { dg-do compile { target c++20 } } +// { dg-options "-O" } +// Like constexpr-dtor13.C, except that allocator is not an empty class. + +struct allocator { + constexpr ~allocator() {} + int a; +}; +struct S { + S(int, int, allocator = allocator()); +}; +void to_string() { S(0, '\0'); }