diff options
author | Jason Merrill <jason@redhat.com> | 2024-09-15 13:50:04 +0200 |
---|---|---|
committer | Jason Merrill <jason@redhat.com> | 2024-09-18 08:58:52 -0400 |
commit | 8733d5d3873977d6ca82d71b28728650f988e9c8 (patch) | |
tree | 1430a816602f810b813da7b2599a91f7a5a41f78 /gcc/cp | |
parent | 6f3b6a451771cd54c98768e7db3c5d58aab2b6aa (diff) | |
download | gcc-8733d5d3873977d6ca82d71b28728650f988e9c8.zip gcc-8733d5d3873977d6ca82d71b28728650f988e9c8.tar.gz gcc-8733d5d3873977d6ca82d71b28728650f988e9c8.tar.bz2 |
c++: -Wdangling-reference and empty class [PR115361]
We can't have a dangling reference to an empty class unless it's
specifically to that class or one of its bases. This was giving a
false positive on the _ExtractKey pattern in libstdc++ hashtable.h.
This also adjusts the order of arguments to reference_related_p, which
is relevant for empty classes (unlike scalars).
Several of the classes in the testsuite needed to gain data members to
continue to warn.
PR c++/115361
gcc/cp/ChangeLog:
* call.cc (do_warn_dangling_reference): Check is_empty_class.
gcc/testsuite/ChangeLog:
* g++.dg/ext/attr-no-dangling6.C
* g++.dg/ext/attr-no-dangling7.C
* g++.dg/ext/attr-no-dangling8.C
* g++.dg/ext/attr-no-dangling9.C
* g++.dg/warn/Wdangling-reference1.C
* g++.dg/warn/Wdangling-reference2.C
* g++.dg/warn/Wdangling-reference3.C: Make classes non-empty.
* g++.dg/warn/Wdangling-reference23.C: New test.
Diffstat (limited to 'gcc/cp')
-rw-r--r-- | gcc/cp/call.cc | 12 |
1 files changed, 7 insertions, 5 deletions
diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc index 664088e..1ecf3aa 100644 --- a/gcc/cp/call.cc +++ b/gcc/cp/call.cc @@ -14356,12 +14356,14 @@ do_warn_dangling_reference (tree expr, bool arg_p) if ((arg = do_warn_dangling_reference (arg, /*arg_p=*/true))) { /* If we know the temporary could not bind to the return type, - don't warn. This is for scalars only because for classes - we can't be sure we are not returning its sub-object. */ - if (SCALAR_TYPE_P (TREE_TYPE (arg)) + don't warn. This is for scalars and empty classes only + because for other classes we can't be sure we are not + returning its sub-object. */ + if ((SCALAR_TYPE_P (TREE_TYPE (arg)) + || is_empty_class (TREE_TYPE (arg))) && TYPE_REF_P (rettype) - && !reference_related_p (TREE_TYPE (arg), - TREE_TYPE (rettype))) + && !reference_related_p (TREE_TYPE (rettype), + TREE_TYPE (arg))) continue; return expr; } |