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.129.124]) by sourceware.org (Postfix) with ESMTPS id C819138B67AA for ; Thu, 27 Oct 2022 15:39:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org C819138B67AA 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=1666885154; 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; bh=MdQn4n6ilM9X15AA/9BFUsSvh+6rL7Swvd7jHcvpZH0=; b=OQkirhYM/h2omRRb+ABsLjtsJV2vSstpkSqJFCKsGjur/LMKOJ6m6SiOrtb2ikOe9EIKt1 2M0Yax08g9MdTLRKnu2IWxBDpIwNKU/MSeDvm3/y5ErK6bGwwkK/opveyWaG2KAncKmRzs eZjPIr7NlK/4OzfhLNBTHghQrxqkY/Q= 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-329-q-zunJQANQ6NeVkw8Mj77A-1; Thu, 27 Oct 2022 11:39:13 -0400 X-MC-Unique: q-zunJQANQ6NeVkw8Mj77A-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id DDB23884346 for ; Thu, 27 Oct 2022 15:39:12 +0000 (UTC) Received: from pdp-11.redhat.com (unknown [10.22.17.213]) by smtp.corp.redhat.com (Postfix) with ESMTP id B4A2E18EB4; Thu, 27 Oct 2022 15:39:12 +0000 (UTC) From: Marek Polacek To: GCC Patches , Jason Merrill Cc: Jonathan Wakely Subject: [PATCH] c++: -Wdangling-reference and system headers Date: Thu, 27 Oct 2022 11:39:06 -0400 Message-Id: <20221027153906.24773-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-12.3 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_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: 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)); 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 -- 2.37.3