Skip to content

Commit af3cfc9

Browse files
HighCommander4sivan-shani
authored andcommitted
[clang][AST] Handle dependent representation of call to function with explicit object parameter in CallExpr::getBeginLoc() (llvm#126868)
This fixes a crash where CallExpr::getBeginLoc() tries to access the first argument of a CallExpr representing a call to a function with an explicit object parameter, assuming that a first argument exists because it's the object argument. This is the case for non-dependent calls, but for dependent calls the object argument is part of the callee (the semantic analysis that separates it out has not been performed yet) and so there may not be a first argument. Fixes llvm#126720
1 parent 2ce2654 commit af3cfc9

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

clang/lib/AST/Expr.cpp

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1645,11 +1645,22 @@ SourceLocation CallExpr::getBeginLoc() const {
16451645
if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(this))
16461646
return OCE->getBeginLoc();
16471647

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

16551666
SourceLocation begin = getCallee()->getBeginLoc();

clang/test/AST/ast-dump-cxx2b-deducing-this.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,16 @@ void main() {
1313
// CHECK-NEXT: | `-DeclRefExpr 0x{{[^ ]*}} <col:13> 'int (S &)' lvalue CXXMethod 0x{{[^ ]*}} 'f' 'int (S &)'
1414
}
1515
}
16+
17+
namespace GH1269720 {
18+
template <typename T>
19+
struct S {
20+
void f(this S&);
21+
void g(S s) {
22+
s.f();
23+
}
24+
// CHECK: CallExpr 0x{{[^ ]*}} <line:22:5, col:9> '<dependent type>'
25+
// CHECK-NEXT: `-MemberExpr 0x{{[^ ]*}} <col:5, col:7> '<bound member function type>' .f
26+
// CHECK-NEXT: `-DeclRefExpr 0x{{[^ ]*}} <col:5> 'S<T>' lvalue ParmVar 0x{{[^ ]*}} 's' 'S<T>'
27+
};
28+
}

0 commit comments

Comments
 (0)