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.133.124]) by sourceware.org (Postfix) with ESMTPS id 7027E3858CDA for ; Fri, 28 Oct 2022 17:32:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 7027E3858CDA Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1666978332; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=R1oEpAac781EOV62UbRm4gS8o9BUPAknrMRTu8adq4M=; b=iz5wPYfb/dDxozKRcEV3hsI+cSPqcrb+azKZc/3d1OWSgcPuPv7dakTbkemJYK1cSH75Fp x/rASUiaKyjdj5rXQMk6CdYdVVemR0vE5Jd6VGhOBrU8GHOPGVA9TQqwzqEgHUf2CBKkqE MeL7NRZJuoqh/J5Og+5jjJJAZfapmy4= Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-660-TNekW-hKM6SkoKzFluYJWQ-1; Fri, 28 Oct 2022 13:32:11 -0400 X-MC-Unique: TNekW-hKM6SkoKzFluYJWQ-1 Received: by mail-qv1-f70.google.com with SMTP id d8-20020a0cfe88000000b004bb65193fdcso3280606qvs.12 for ; Fri, 28 Oct 2022 10:32:10 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=R1oEpAac781EOV62UbRm4gS8o9BUPAknrMRTu8adq4M=; b=5OGp8n301wDaHtSOF9TU3DFtDDpjRcrf7viGwCeYvaCyl1Cbyo0vPCD2+Y0ZQKogJq K4cfzewT4UIck5oHWVPRl4bjp4zFpXiSbonYQkI8fabF80N3pHE9aogPdpXLQ4FEbjBZ d0T0PJ/a9vSHqS+qVSsLOb+DR4SFz2Zdd9jX5qHx88z2JqUXFmsN947X1qOPhGE7FoZa e9zVfmt7PasgACPJYMmqzV60kOnlMbBND4U2eZbwoaIdtSsdgYrEhlUqf2Hur1VqpcpA FaKAyrPqfP5WbxySCgI4xFYtmWG/RIotfsmrxiW+nEoLhJsSPg7FqY7OdJxOMxg4Nw78 0Wcg== X-Gm-Message-State: ACrzQf3Mo1kohZ4HT2cR9JQ6nap3aR2+o3tRJPWPhtHNXZxkzyTRwFgT BdYIlZqE9T+JZ4Qoa0ps3VWfJo/6v1fqn134B2rkpHtvYJTt9m+pGzHUxB2w3L3FpdwNxl3jeEm 09+WKmgPBua/H/2gdbQ== X-Received: by 2002:a05:6214:f25:b0:4bb:b5dc:63eb with SMTP id iw5-20020a0562140f2500b004bbb5dc63ebmr421703qvb.55.1666978330454; Fri, 28 Oct 2022 10:32:10 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5RpRdA6RGLpc1/k3+oDgagBGRSH7f8A+0TWEE0dzxKxjZRYgbQfC6aOY0+CocGLSLpo/1YYQ== X-Received: by 2002:a05:6214:f25:b0:4bb:b5dc:63eb with SMTP id iw5-20020a0562140f2500b004bbb5dc63ebmr421681qvb.55.1666978330140; Fri, 28 Oct 2022 10:32:10 -0700 (PDT) Received: from [192.168.1.101] (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 h10-20020ac8138a000000b0039953dcc480sm2621065qtj.88.2022.10.28.10.32.09 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 28 Oct 2022 10:32:09 -0700 (PDT) Message-ID: <95066d1f-128a-2809-a0eb-03ce9b803112@redhat.com> Date: Fri, 28 Oct 2022 13:32:08 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.4.0 Subject: Re: [PATCH] c++: -Wdangling-reference and system headers To: Marek Polacek , GCC Patches Cc: Jonathan Wakely References: <20221027153906.24773-1-polacek@redhat.com> From: Jason Merrill In-Reply-To: <20221027153906.24773-1-polacek@redhat.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit 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,NICE_REPLY_A,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 List-Id: On 10/27/22 11:39, Marek Polacek wrote: > I got this testcase: > > auto f() -> std::optional; > for (char c : f().value()) { } > > which has a dangling reference: std::optional::value returns > a reference to the contained value, but here it's the f() temporary. > We warn, which is great, but only with -Wsystem-headers, because > the function comes from a system header and warning_enabled_at used > in do_warn_dangling_reference checks diagnostic_report_warnings_p, > which in this case returned false so we didn't warn. > > Fixed as below. I could also override dc_warn_system_headers so that > the warning is enabled in system headers always. With that, I found one > issue in libstdc++: > > libstdc++-v3/include/bits/fs_path.h:1265:15: warning: possibly dangling reference to a temporary [-Wdangling-reference] > 1265 | auto& __last = *--end(); > | ^~~~~~ > > which looks like a true positive as well. > > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? > > gcc/cp/ChangeLog: > > * call.cc (maybe_warn_dangling_reference): Enable the warning in > system headers if the decl isn't in a system header. > > gcc/testsuite/ChangeLog: > > * g++.dg/warn/Wdangling-reference4.C: New test. > --- > gcc/cp/call.cc | 7 +++++++ > gcc/testsuite/g++.dg/warn/Wdangling-reference4.C | 14 ++++++++++++++ > 2 files changed, 21 insertions(+) > create mode 100644 gcc/testsuite/g++.dg/warn/Wdangling-reference4.C > > diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc > index 951b9fd2a88..c7c7a122045 100644 > --- a/gcc/cp/call.cc > +++ b/gcc/cp/call.cc > @@ -13539,6 +13539,13 @@ maybe_warn_dangling_reference (const_tree decl, tree init) > return; > if (!TYPE_REF_P (TREE_TYPE (decl))) > return; > + /* Don't suppress the diagnostic just because the call comes from > + a system header. If the DECL is not in a system header, or if > + -Wsystem-headers was provided, warn. */ > + auto wsh > + = make_temp_override (global_dc->dc_warn_system_headers, > + (!in_system_header_at (DECL_SOURCE_LOCATION (decl)) > + || global_dc->dc_warn_system_headers)); Hmm, this is OK, but maybe we want a warning_enabled_at_ignore_system_header? > if (tree call = do_warn_dangling_reference (init)) > { > auto_diagnostic_group d; > diff --git a/gcc/testsuite/g++.dg/warn/Wdangling-reference4.C b/gcc/testsuite/g++.dg/warn/Wdangling-reference4.C > new file mode 100644 > index 00000000000..aee7a29019b > --- /dev/null > +++ b/gcc/testsuite/g++.dg/warn/Wdangling-reference4.C > @@ -0,0 +1,14 @@ > +// { dg-do compile { target c++17 } } > +// { dg-options "-Wdangling-reference" } > +// Check that we warn here even without -Wsystem-headers. > + > +#include > +#include > + > +auto f() -> std::optional; > + > +void > +g () > +{ > + for (char c : f().value()) { (void) c; } // { dg-warning "dangling reference" } > +} > > base-commit: f95d3d5de72a1c43e8d529bad3ef59afc3214705