[clang] [Clang][Sema] Correctly transform dependent operands of overloaded binary operator& (PR #97596)
Krystian Stasiowski via cfe-commits
cfe-commits at lists.llvm.org
Wed Jul 3 08:51:58 PDT 2024
https://github.com/sdkrystian created https://github.com/llvm/llvm-project/pull/97596
Currently, `TreeTransform::TransformCXXOperatorCallExpr` calls `TreeTransform::TransformAddressOfOperand` to transform the first operand of a `CXXOperatorCallExpr` when its `OverloadOperatorKind` is `OO_Amp` -- regardless of arity. This results in the first operand of binary `operator&` being incorrectly transformed as if it was the operand of the address of operator in cases such as the following:
```cpp
struct A {
int x;
};
void operator&(A, A);
template<typename T>
struct B {
int f() {
return T::x & 1; // invalid reference to 'A::x' is not diagnosed because 'T::x' is incorrectly transformed as if it was the operand of unary operator&
}
};
template struct B<A>;
```
Prior to #92318 we would build a `CXXDependentScopeMemberExpr` for `T::x` (as with most dependent qualified names that were not member qualified names). Since `TreeTransform::TransformAddressOfOperand` only differs from `TransformExpr` for `DependentScopeDeclRefExpr` and `UnresolvedLookupExpr` operands, `T::x` was transformed "correctly". Now that we build a `DependentScopeDeclRefExpr` for `T::x`, it is incorrectly transformed as if it was the operand of the address of operator and we fail to diagnose the invalid reference to a non-static data member. This patch fixes the issue by only calling `TreeTransform::TransformAddressOfOperand` for `CXXOperatorCallExpr`s with a single operand. This fixes #97483.
>From d041273f56d59c9f466a9bb9a60b7501daa4844f Mon Sep 17 00:00:00 2001
From: Krystian Stasiowski <sdkrystian at gmail.com>
Date: Wed, 3 Jul 2024 10:47:23 -0400
Subject: [PATCH] [Clang][Sema] Correctly transform dependent operands of
overloaded binary operator&
---
clang/lib/Sema/TreeTransform.h | 2 +-
.../expr.prim.id/expr.prim.id.general/p4.cpp | 16 ++++++++++++++++
2 files changed, 17 insertions(+), 1 deletion(-)
create mode 100644 clang/test/CXX/expr/expr.prim/expr.prim.id/expr.prim.id.general/p4.cpp
diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h
index 51ba22f99e3a3..4450ebaf615cd 100644
--- a/clang/lib/Sema/TreeTransform.h
+++ b/clang/lib/Sema/TreeTransform.h
@@ -12919,7 +12919,7 @@ TreeTransform<Derived>::TransformCXXOperatorCallExpr(CXXOperatorCallExpr *E) {
}
ExprResult First;
- if (E->getOperator() == OO_Amp)
+ if (E->getNumArgs() == 1 && E->getOperator() == OO_Amp)
First = getDerived().TransformAddressOfOperand(E->getArg(0));
else
First = getDerived().TransformExpr(E->getArg(0));
diff --git a/clang/test/CXX/expr/expr.prim/expr.prim.id/expr.prim.id.general/p4.cpp b/clang/test/CXX/expr/expr.prim/expr.prim.id/expr.prim.id.general/p4.cpp
new file mode 100644
index 0000000000000..e6d9c171e3893
--- /dev/null
+++ b/clang/test/CXX/expr/expr.prim/expr.prim.id/expr.prim.id.general/p4.cpp
@@ -0,0 +1,16 @@
+// RUN: %clang_cc1 -verify %s
+
+struct A {
+ int x;
+};
+
+void operator&(A, A);
+
+template<typename T>
+struct B {
+ int f() {
+ return T::x & 1; // expected-error {{invalid use of non-static data member 'x'}}
+ }
+};
+
+template struct B<A>; // expected-note {{in instantiation of}}
More information about the cfe-commits
mailing list