[clang] [Clang] fix static operator()/[] call not evaluating object (PR #78356)

Mital Ashok via cfe-commits cfe-commits at lists.llvm.org
Tue Jan 16 13:59:21 PST 2024


https://github.com/MitalAshok updated https://github.com/llvm/llvm-project/pull/78356

>From c6d4aca37bf7ede785313135abdad986d9cd783a Mon Sep 17 00:00:00 2001
From: Mital Ashok <mital at mitalashok.co.uk>
Date: Tue, 16 Jan 2024 21:42:01 +0000
Subject: [PATCH] [Clang] fix static operator()/[] call not evaluating object

---
 clang/lib/Sema/SemaOverload.cpp            | 36 +++++++++++++++++-----
 clang/test/SemaCXX/overloaded-operator.cpp | 28 ++++++++++++++++-
 2 files changed, 56 insertions(+), 8 deletions(-)

diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp
index 37c62b306b3cd3f..d34fa54bc9889a2 100644
--- a/clang/lib/Sema/SemaOverload.cpp
+++ b/clang/lib/Sema/SemaOverload.cpp
@@ -15089,10 +15089,20 @@ ExprResult Sema::CreateOverloadedArraySubscriptExpr(SourceLocation LLoc,
           TheCall = CXXOperatorCallExpr::Create(
               Context, OO_Subscript, FnExpr.get(), MethodArgs, ResultTy, VK,
               RLoc, CurFPFeatureOverrides());
-        else
-          TheCall =
-              CallExpr::Create(Context, FnExpr.get(), MethodArgs, ResultTy, VK,
-                               RLoc, CurFPFeatureOverrides());
+        else {
+          if (Base->isPRValue())
+            Base = CreateMaterializeTemporaryExpr(
+                Base->getType().getNonReferenceType(), Base,
+                /*BoundToLvalueReference=*/false);
+          ExprResult OperatorAccess = MemberExpr::Create(
+              Context, Base, /*IsArrow=*/false, LLoc, NestedNameSpecifierLoc(),
+              SourceLocation(), FnDecl, Best->FoundDecl, OpLocInfo, nullptr,
+              FnDecl->getType(), ExprValueKind::VK_LValue,
+              ExprObjectKind::OK_Ordinary, NonOdrUseReason::NOUR_None);
+          TheCall = CallExpr::Create(
+              Context, CallExprUnaryConversions(OperatorAccess.get()).get(),
+              MethodArgs, ResultTy, VK, RLoc, CurFPFeatureOverrides());
+        }
 
         if (CheckCallReturnType(FnDecl->getReturnType(), LLoc, TheCall, FnDecl))
           return ExprError();
@@ -15767,9 +15777,21 @@ Sema::BuildCallToObjectOfClassType(Scope *S, Expr *Obj,
     TheCall = CXXOperatorCallExpr::Create(Context, OO_Call, NewFn.get(),
                                           MethodArgs, ResultTy, VK, RParenLoc,
                                           CurFPFeatureOverrides());
-  else
-    TheCall = CallExpr::Create(Context, NewFn.get(), MethodArgs, ResultTy, VK,
-                               RParenLoc, CurFPFeatureOverrides());
+  else {
+    Expr *Base = Object.get();
+    if (Base->isPRValue())
+      Base = CreateMaterializeTemporaryExpr(
+          Base->getType().getNonReferenceType(), Base,
+          /*BoundToLvalueReference=*/false);
+    ExprResult OperatorAccess = MemberExpr::Create(
+        Context, Base, /*IsArrow=*/false, LParenLoc, NestedNameSpecifierLoc(),
+        SourceLocation(), Method, Best->FoundDecl, OpLocInfo, nullptr,
+        Method->getType(), ExprValueKind::VK_LValue,
+        ExprObjectKind::OK_Ordinary, NonOdrUseReason::NOUR_None);
+    TheCall = CallExpr::Create(
+        Context, CallExprUnaryConversions(OperatorAccess.get()).get(),
+        MethodArgs, ResultTy, VK, RParenLoc, CurFPFeatureOverrides());
+  }
 
   if (CheckCallReturnType(Method->getReturnType(), LParenLoc, TheCall, Method))
     return true;
diff --git a/clang/test/SemaCXX/overloaded-operator.cpp b/clang/test/SemaCXX/overloaded-operator.cpp
index 83a7e65b43dd012..361adda58e48b63 100644
--- a/clang/test/SemaCXX/overloaded-operator.cpp
+++ b/clang/test/SemaCXX/overloaded-operator.cpp
@@ -1,4 +1,6 @@
-// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s 
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++17 %s
+
 class X { };
 
 X operator+(X, X);
@@ -598,3 +600,27 @@ namespace B {
 }
 void g(B::X x) { A::f(x); }
 }
+
+namespace static_operator {
+#if __cplusplus >= 201703L
+struct X {
+  static constexpr int operator()() { return 0; }
+  // expected-warning at -1 {{declaring overloaded 'operator()' as 'static' is a C++23 extension}}
+  static constexpr int operator[](int) { return 0; }
+  // expected-warning at -1 {{declaring overloaded 'operator[]' as 'static' is a C++23 extension}}
+};
+
+constexpr int f(int x) {
+  return (++x, X())(), (++x, X())[1], x;
+}
+
+static_assert(f(0) == 2, "");
+
+constexpr int g(int x) {
+  return (++x, []() static { return 0; })(), x;
+  // expected-warning at -1 {{static lambdas are a C++23 extension}}
+}
+
+static_assert(g(0) == 1, "");
+#endif
+}



More information about the cfe-commits mailing list