From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pf1-x433.google.com (mail-pf1-x433.google.com [IPv6:2607:f8b0:4864:20::433]) by sourceware.org (Postfix) with ESMTPS id D3CFE385828A for ; Sun, 19 Nov 2023 07:38:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D3CFE385828A Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org D3CFE385828A Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::433 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700379504; cv=none; b=kosSM+35IjQ/hn6cmyirsFTTz2Igw8h0BArjAV9XlxsB3ZMuVHdlH3FULtbi5AMpvX/j/PKB9aWQOd/6r1y8/B+YeJ1sR1vgMDmAditRIbMEKs/n/KwxMKiZHcFsXz7Wd5NaKlMY78b5EV2bpVI45t1rJHMVpwucEVFB9Art274= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700379504; c=relaxed/simple; bh=laJ5uxnPJ71NYpuCAeUvmTBN4HiUECMBBwPHssHHhWI=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=qb4yf5bVAjc53WtoPRweKnxCi0KGzDCNlW/yXF8yfuGE01beyxuCoOHDwQgVmteJaLUXtBtpMhfgJGVfHh+rpB1hkqotLFkD+4VT+FLU3nBS5WLVovjgLLXifxqQfHatm4B3srutMWuw0plMQhxDMKVs1MLkXcJnOwD3pxW83Vg= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pf1-x433.google.com with SMTP id d2e1a72fcca58-6ba54c3ed97so3539332b3a.2 for ; Sat, 18 Nov 2023 23:38:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1700379502; x=1700984302; darn=gcc.gnu.org; h=mime-version:user-agent:message-id:date:organization:subject:cc:to :from:from:to:cc:subject:date:message-id:reply-to; bh=q/7DjKNdq0vpMsNyD8W8PXv8Kdx0WeMeWZLbWXz+jX8=; b=NcLapV0AOmei/0PgI2rJvEIQeuqPDZdvC7IwfJbCyPNRaL8MmfRN3so9e4O6f7x4rl F8jbzBnROur8WnQuTpoJ27VzEMzpWdtz7cP7qWo0EYraixZeBUT6a5waJ0B1i0lgeuf4 n6yLgH6WYGZCvBrtlqPUZfd3nSC/PkOjwNoEztQXPanu97qMJtAcTJ7vP03BdHn4V9NG moXu/SwV9OC9lLuItt1c3G12oOW256UvjQ0CD2OPJk/Gz8PiWSf8gPOJ/nnB9dlNT81A O4XEyY28S+VkpWq872BSmg5QMEGQlAL8QjHMuVju3A/3WUEwOIb24s+YSx8LX8cq2iB9 GTfQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700379502; x=1700984302; h=mime-version:user-agent:message-id:date:organization:subject:cc:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=q/7DjKNdq0vpMsNyD8W8PXv8Kdx0WeMeWZLbWXz+jX8=; b=j096iVAgGJCOyYCNk2qk40NJHKnSeapiVipgNMwzPNsYe2DW91cSkPccpMjm68pZxV Km/mdvn58y2mD8+51rhYX6VYd9EchweMMiSjiOlB/L28mYIpBgRsH0mVT9bcS5Q4+FZ2 phobBY3SLvOK6vu+9QexiIR+jAKTjKkrg4oq+m/E1odMJq/L6iYCBPsA/eQch7NzyF/w BZaWW2EEhBgOLIcLP9vrgd70+qufgIU034ap3LL1ijBVfwj7bnRuuXxRVRuI09wgJOla hXeYLH6m2to6p9pitUNDiQzdnrxObFMkfsfrgeewxK7d6Hg1m8RjCHKUTx/Hzx8U/6MW VJ5Q== X-Gm-Message-State: AOJu0Yy8UpWtNngkTLI+QEx4Nyht5cUe/LQb3VowOhrokEbCy6Ezw9w9 1ItF1vf8fFnakFFaPRBJPscFnJiGhnPSnyW/wQAhyA== X-Google-Smtp-Source: AGHT+IGeGb2cQkjksFtEaE9FwTeYhr73JQjwiGqEsdHuGJY4QmBTauWTV0Gq89Z7VXI5mOA6zJ4xDA== X-Received: by 2002:a05:6a00:1946:b0:6c4:9672:9a17 with SMTP id s6-20020a056a00194600b006c496729a17mr4982554pfk.1.1700379501850; Sat, 18 Nov 2023 23:38:21 -0800 (PST) Received: from free.home ([2804:7f1:2080:51b3:d545:cbee:40c0:c7a7]) by smtp.gmail.com with ESMTPSA id x20-20020aa793b4000000b006c0fdc98c89sm3952773pff.85.2023.11.18.23.38.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 18 Nov 2023 23:38:21 -0800 (PST) Received: from livre (livre.home [172.31.160.2]) by free.home (8.15.2/8.15.2) with ESMTPS id 3AJ7cCu7121926 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Sun, 19 Nov 2023 04:38:12 -0300 From: Alexandre Oliva To: gcc-patches@gcc.gnu.org Cc: Jason Merrill , Nathan Sidwell Subject: [PATCH] call maybe_return_this in build_clone Organization: Free thinker, does not speak for AdaCore Date: Sun, 19 Nov 2023 04:38:12 -0300 Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain X-Scanned-By: MIMEDefang 2.84 X-Spam-Status: No, score=-11.5 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE,URIBL_BLACK,WEIRD_QUOTING 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: __dt_base doesn't get its body from a maybe_return_this caller, it's rather cloned with the full body within build_clone, and then it's left alone, without going through finish_function_body or build_delete_destructor_body, that call maybe_return_this. Now, this is correct as far as the generated code is concerned, since the cloned body of a cdtor that returns this is also a cdtor body that returns this. The problem is that the decl for THIS is also cloned, and it doesn't get the warning suppression introduced by maybe_return_this, so Wuse-after-free3.C fails with an excess warning at the closing brace of the dtor body. I've split out the warning suppression from maybe_return_this, and arranged to call that bit from the relevant build_clone case. Unfortunately, because the warning is silenced for all uses of the THIS decl, rather than only for the ABI-mandated return stmt, this also silences the very warning that the testcase checks for. I'm not revamping the warning suppression approach to overcome this, so I'm xfailing the expected warning on ARM EABI, hoping that's the only target with cdtor_return_this, and leaving it at that. Regstrapped on x86_64-linux-gnu, also tested on arm-eabi with default cpu on trunk, and with tms570 on gcc-13. Ok to install? for gcc/cp/ChangeLog * decl.cc (maybe_prepare_return_this): Split out of... (maybe_return_this): ... this. * cp-tree.h (maybe_prepare_return_this): Declare. * class.cc (build_clone): Call it. for gcc/testsuite/ChangeLog * g++.dg/warn/Wuse-after-free3.C: xfail on arm_eabi. --- gcc/cp/class.cc | 2 ++ gcc/cp/cp-tree.h | 1 + gcc/cp/decl.cc | 23 +++++++++++++++++++---- gcc/testsuite/g++.dg/warn/Wuse-after-free3.C | 4 +++- 4 files changed, 25 insertions(+), 5 deletions(-) diff --git a/gcc/cp/class.cc b/gcc/cp/class.cc index 9d4d95f85bf75..1abf5d90edcef 100644 --- a/gcc/cp/class.cc +++ b/gcc/cp/class.cc @@ -5053,6 +5053,8 @@ build_clone (tree fn, tree name, bool need_vtt_parm_p, clone = copy_fndecl_with_name (fn, name, ERROR_MARK, need_vtt_parm_p, omit_inherited_parms_p); DECL_CLONED_FUNCTION (clone) = fn; + + maybe_prepare_return_this (clone); } /* Remember where this function came from. */ diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index 1fa710d71543d..e1abb2a79dd81 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -6956,6 +6956,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 tree maybe_prepare_return_this (tree); extern void maybe_return_this (void); extern tree begin_function_body (void); extern void finish_function_body (tree); diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index d2ed46b145318..038c5ab71f201 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -17896,16 +17896,31 @@ store_parm_decls (tree current_function_parms) } +/* Mark CDTOR's implicit THIS argument for returning, if required by + the ABI.. Return the decl for THIS, if it is to be returned, and + NULL otherwise. */ + +tree +maybe_prepare_return_this (tree cdtor) +{ + if (targetm.cxx.cdtor_returns_this ()) + if (tree val = DECL_ARGUMENTS (cdtor)) + { + suppress_warning (val, OPT_Wuse_after_free); + return val; + } + + return NULL_TREE; +} + /* Set the return value of the [cd]tor if the ABI wants that. */ void -maybe_return_this (void) +maybe_return_this () { - if (targetm.cxx.cdtor_returns_this ()) + if (tree val = maybe_prepare_return_this (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); diff --git a/gcc/testsuite/g++.dg/warn/Wuse-after-free3.C b/gcc/testsuite/g++.dg/warn/Wuse-after-free3.C index e5b157865bf6e..8ef82021380a3 100644 --- a/gcc/testsuite/g++.dg/warn/Wuse-after-free3.C +++ b/gcc/testsuite/g++.dg/warn/Wuse-after-free3.C @@ -11,5 +11,7 @@ struct A A::~A () { operator delete (this); - f (); // { dg-warning "used after" } + f (); // { dg-warning "used after" "" { xfail arm_eabi } } + // arm_eabi's cdtors return this, which disables -Wuse-after-free + // warnings for cdtors' "this". } -- Alexandre Oliva, happy hacker https://FSFLA.org/blogs/lxo/ Free Software Activist GNU Toolchain Engineer More tolerance and less prejudice are key for inclusion and diversity Excluding neuro-others for not behaving ""normal"" is *not* inclusive