aboutsummaryrefslogtreecommitdiff
path: root/gcc/cp
diff options
context:
space:
mode:
authorJakub Jelinek <jakub@redhat.com>2021-01-29 00:39:00 +0100
committerJakub Jelinek <jakub@redhat.com>2021-01-29 00:39:00 +0100
commit850a8ec54c4310d779004299bf9a0dec52324e9e (patch)
treecbd00e7d5812ae2a06755d730f8e156c0702a2d2 /gcc/cp
parent513ee7d2cd9a60339a50dc9c4cba39a8f1c707f0 (diff)
downloadgcc-850a8ec54c4310d779004299bf9a0dec52324e9e.zip
gcc-850a8ec54c4310d779004299bf9a0dec52324e9e.tar.gz
gcc-850a8ec54c4310d779004299bf9a0dec52324e9e.tar.bz2
c++: Fix -Weffc++ in templates [PR98841]
We emit a bogus warning on the following testcase, suggesting that the operator should return *this even when it does that already. The problem is that normally cp_build_indirect_ref_1 ensures that *this is folded as current_class_ref, but in templates (if return type is non-dependent, otherwise check_return_expr doesn't check it) it didn't go through cp_build_indirect_ref_1, but just built another INDIRECT_REF. Which means it then doesn't compare pointer-equal to current_class_ref. The following patch fixes it by doing in build_x_indirect_ref for *this what cp_build_indirect_ref_1 would do. 2021-01-28 Jakub Jelinek <jakub@redhat.com> PR c++/98841 * typeck.c (build_x_indirect_ref): For *this, return current_class_ref. * g++.dg/warn/effc5.C: New test.
Diffstat (limited to 'gcc/cp')
-rw-r--r--gcc/cp/typeck.c10
1 files changed, 9 insertions, 1 deletions
diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c
index 315f706..9322e08 100644
--- a/gcc/cp/typeck.c
+++ b/gcc/cp/typeck.c
@@ -3326,7 +3326,15 @@ build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring,
{
/* Retain the type if we know the operand is a pointer. */
if (TREE_TYPE (expr) && INDIRECT_TYPE_P (TREE_TYPE (expr)))
- return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
+ {
+ if (expr == current_class_ptr
+ || (TREE_CODE (expr) == NOP_EXPR
+ && TREE_OPERAND (expr, 0) == current_class_ptr
+ && (same_type_ignoring_top_level_qualifiers_p
+ (TREE_TYPE (expr), TREE_TYPE (current_class_ptr)))))
+ return current_class_ref;
+ return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
+ }
if (type_dependent_expression_p (expr))
return build_min_nt_loc (loc, INDIRECT_REF, expr);
expr = build_non_dependent_expr (expr);