]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: -Wdangling-reference and system headers
authorMarek Polacek <polacek@redhat.com>
Thu, 27 Oct 2022 01:15:53 +0000 (21:15 -0400)
committerMarek Polacek <polacek@redhat.com>
Fri, 28 Oct 2022 17:40:41 +0000 (13:40 -0400)
I got this testcase:

  auto f() -> std::optional<std::string>;
  for (char c : f().value()) { }

which has a dangling reference: std::optional<T>::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.

gcc/cp/call.cc
gcc/testsuite/g++.dg/warn/Wdangling-reference4.C [new file with mode: 0644]

index 951b9fd2a88dea0aa66d73762d979c63cf65aeff..c7c7a122045cf868c526320ba3a3534e37839529 100644 (file)
@@ -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 (file)
index 0000000..aee7a29
--- /dev/null
@@ -0,0 +1,14 @@
+// { dg-do compile { target c++17 } }
+// { dg-options "-Wdangling-reference" }
+// Check that we warn here even without -Wsystem-headers.
+
+#include <optional>
+#include <string>
+
+auto f() -> std::optional<std::string>;
+
+void
+g ()
+{
+  for (char c : f().value()) { (void) c; } // { dg-warning "dangling reference" }
+}