Skip to content

[clang][AST] Handle dependent representation of call to function with explicit object parameter in CallExpr::getBeginLoc() #126868

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions clang/lib/AST/Expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1645,11 +1645,22 @@ SourceLocation CallExpr::getBeginLoc() const {
if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(this))
return OCE->getBeginLoc();

if (const auto *Method =
dyn_cast_if_present<const CXXMethodDecl>(getCalleeDecl());
Method && Method->isExplicitObjectMemberFunction()) {
assert(getNumArgs() > 0 && getArg(0));
return getArg(0)->getBeginLoc();
// A non-dependent call to a member function with an explicit object parameter
// is modelled with the object expression being the first argument, e.g. in
// `o.f(x)`, the callee will be just `f`, and `o` will be the first argument.
// Since the first argument is written before the callee, the expression's
// begin location should come from the first argument.
// This does not apply to dependent calls, which are modelled with `o.f`
// being the callee.
if (!isTypeDependent()) {
if (const auto *Method =
dyn_cast_if_present<const CXXMethodDecl>(getCalleeDecl());
Method && Method->isExplicitObjectMemberFunction()) {
bool HasFirstArg = getNumArgs() > 0 && getArg(0);
assert(HasFirstArg);
if (HasFirstArg)
return getArg(0)->getBeginLoc();
}
}

SourceLocation begin = getCallee()->getBeginLoc();
Expand Down
13 changes: 13 additions & 0 deletions clang/test/AST/ast-dump-cxx2b-deducing-this.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,16 @@ void main() {
// CHECK-NEXT: | `-DeclRefExpr 0x{{[^ ]*}} <col:13> 'int (S &)' lvalue CXXMethod 0x{{[^ ]*}} 'f' 'int (S &)'
}
}

namespace GH1269720 {
template <typename T>
struct S {
void f(this S&);
void g(S s) {
s.f();
}
// CHECK: CallExpr 0x{{[^ ]*}} <line:22:5, col:9> '<dependent type>'
// CHECK-NEXT: `-MemberExpr 0x{{[^ ]*}} <col:5, col:7> '<bound member function type>' .f
// CHECK-NEXT: `-DeclRefExpr 0x{{[^ ]*}} <col:5> 'S<T>' lvalue ParmVar 0x{{[^ ]*}} 's' 'S<T>'
};
}