From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1734) id F249C3858C2C; Fri, 28 Oct 2022 17:42:12 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org F249C3858C2C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1666978940; bh=1kJzwHymLhbPMaJgkZjyVoTqQnxNspKeKMG62OxgOsE=; h=From:To:Subject:Date:From; b=oCpsiVDjCEpkFDDbpDakmTu1aRLOXTWiDmKoJDIwbMiVXXvKKM6v+tK9ObzfoPfeg Cc9abelphea98X94aWNp5dKAR5XpRUHKfx8Q+aqTkmZ15Iyay3ecwIddPPwuX5dQmU vPj/A9T8mDweHg/ptcjhvL30jMjF4mo4QFQHQ+Sw= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Marek Polacek To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-3551] c++: -Wdangling-reference and system headers X-Act-Checkin: gcc X-Git-Author: Marek Polacek X-Git-Refname: refs/heads/trunk X-Git-Oldrev: 38a628f52cf0ff5db6708578248484d50a50b366 X-Git-Newrev: e583c86f49b9ef6991b25308a0ad60de9697f24a Message-Id: <20221028174220.F249C3858C2C@sourceware.org> Date: Fri, 28 Oct 2022 17:42:12 +0000 (GMT) List-Id: https://gcc.gnu.org/g:e583c86f49b9ef6991b25308a0ad60de9697f24a commit r13-3551-ge583c86f49b9ef6991b25308a0ad60de9697f24a Author: Marek Polacek Date: Wed Oct 26 21:15:53 2022 -0400 c++: -Wdangling-reference and system headers 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. 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. Diff: --- gcc/cp/call.cc | 7 +++++++ gcc/testsuite/g++.dg/warn/Wdangling-reference4.C | 14 ++++++++++++++ 2 files changed, 21 insertions(+) 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" } +}