From 3e5e55fa72c04996dfde1304662e9def6a1ef726 Mon Sep 17 00:00:00 2001 From: Nathan Ridge Date: Fri, 29 Nov 2024 00:08:40 -0500 Subject: [PATCH] [clang] Compute accurate begin location for CallExpr with explicit object parameter The explicit object parameter is written before the callee expression, so the begin location should come from the explicit object parameter. --- clang-tools-extra/clangd/unittests/XRefsTests.cpp | 10 ++++++++++ clang/lib/AST/Expr.cpp | 8 ++++++++ clang/lib/Sema/SemaOverload.cpp | 2 +- clang/test/AST/ast-dump-cxx2b-deducing-this.cpp | 4 ++-- 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/clang-tools-extra/clangd/unittests/XRefsTests.cpp b/clang-tools-extra/clangd/unittests/XRefsTests.cpp index d393c72974d44..7d824d659ad2c 100644 --- a/clang-tools-extra/clangd/unittests/XRefsTests.cpp +++ b/clang-tools-extra/clangd/unittests/XRefsTests.cpp @@ -1019,6 +1019,15 @@ TEST(LocateSymbol, All) { void *Value; void *getPointer() const { return Info::get^Pointer(Value); } }; + )cpp", + R"cpp(// Deducing this + struct S { + int bar(this S&); + }; + void foo() { + S [[waldo]]; + int x = wa^ldo.bar(); + } )cpp"}; for (const char *Test : Tests) { Annotations T(Test); @@ -1035,6 +1044,7 @@ TEST(LocateSymbol, All) { TU.Code = std::string(T.code()); TU.ExtraArgs.push_back("-xobjective-c++"); + TU.ExtraArgs.push_back("-std=c++23"); auto AST = TU.build(); auto Results = locateSymbolAt(AST, T.point()); diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp index a4fb4d5a1f2ec..5a6738196d289 100644 --- a/clang/lib/AST/Expr.cpp +++ b/clang/lib/AST/Expr.cpp @@ -1639,11 +1639,19 @@ SourceLocation CallExpr::getBeginLoc() const { if (const auto *OCE = dyn_cast(this)) return OCE->getBeginLoc(); + if (const auto *Method = + dyn_cast_if_present(getCalleeDecl()); + Method && Method->isExplicitObjectMemberFunction()) { + assert(getNumArgs() > 0 && getArg(0)); + return getArg(0)->getBeginLoc(); + } + SourceLocation begin = getCallee()->getBeginLoc(); if (begin.isInvalid() && getNumArgs() > 0 && getArg(0)) begin = getArg(0)->getBeginLoc(); return begin; } + SourceLocation CallExpr::getEndLoc() const { if (const auto *OCE = dyn_cast(this)) return OCE->getEndLoc(); diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp index 4c9e37bd286de..e4bf9aa521224 100644 --- a/clang/lib/Sema/SemaOverload.cpp +++ b/clang/lib/Sema/SemaOverload.cpp @@ -15565,7 +15565,7 @@ ExprResult Sema::BuildCallToMemberFunction(Scope *S, Expr *MemExprE, // Build the actual expression node. ExprResult FnExpr = CreateFunctionRefExpr(*this, Method, FoundDecl, MemExpr, - HadMultipleCandidates, MemExpr->getBeginLoc()); + HadMultipleCandidates, MemExpr->getExprLoc()); if (FnExpr.isInvalid()) return ExprError(); diff --git a/clang/test/AST/ast-dump-cxx2b-deducing-this.cpp b/clang/test/AST/ast-dump-cxx2b-deducing-this.cpp index 04cff07376885..1b385e0fc3331 100644 --- a/clang/test/AST/ast-dump-cxx2b-deducing-this.cpp +++ b/clang/test/AST/ast-dump-cxx2b-deducing-this.cpp @@ -9,7 +9,7 @@ int main() { S s; int x = s.f(); // CHECK: CallExpr 0x{{[^ ]*}} 'int - // CHECK-NEXT: |-ImplicitCastExpr 0x{{[^ ]*}} 'int (*)(S &)' - // CHECK-NEXT: | `-DeclRefExpr 0x{{[^ ]*}} 'int (S &)' lvalue CXXMethod 0x{{[^ ]*}} 'f' 'int (S &)' + // CHECK-NEXT: |-ImplicitCastExpr 0x{{[^ ]*}} 'int (*)(S &)' + // CHECK-NEXT: | `-DeclRefExpr 0x{{[^ ]*}} 'int (S &)' lvalue CXXMethod 0x{{[^ ]*}} 'f' 'int (S &)' } }