Skip to content

Commit dbd1fb8

Browse files
[clangd] Avoid crash when summarizing pointer-to-member expr for block-end hint (#76492)
For calls through a pointer to member, CXXMemberCallExpr::getCallee() is a BinaryOperator with operator ->* (after unwrapping parens). getMethodDecl() only returns non-null if the callee is a MemberExpr. Fixes clangd/clangd#1873
1 parent da5378e commit dbd1fb8

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

clang-tools-extra/clangd/InlayHints.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ std::string summarizeExpr(const Expr *E) {
286286
// Step through implicit nodes that clang doesn't classify as such.
287287
std::string VisitCXXMemberCallExpr(const CXXMemberCallExpr *E) {
288288
// Call to operator bool() inside if (X): dispatch to X.
289-
if (E->getNumArgs() == 0 &&
289+
if (E->getNumArgs() == 0 && E->getMethodDecl() &&
290290
E->getMethodDecl()->getDeclName().getNameKind() ==
291291
DeclarationName::CXXConversionFunctionName &&
292292
E->getSourceRange() ==

clang-tools-extra/clangd/unittests/InlayHintTests.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2205,6 +2205,19 @@ TEST(BlockEndHints, Macro) {
22052205
ExpectedHint{" // struct S1", "S1"});
22062206
}
22072207

2208+
TEST(BlockEndHints, PointerToMemberFunction) {
2209+
// Do not crash trying to summarize `a->*p`.
2210+
assertBlockEndHints(R"cpp(
2211+
class A {};
2212+
using Predicate = bool(A::*)();
2213+
void foo(A* a, Predicate p) {
2214+
if ((a->*p)()) {
2215+
$ptrmem[[}]]
2216+
} // suppress
2217+
)cpp",
2218+
ExpectedHint{" // if", "ptrmem"});
2219+
}
2220+
22082221
// FIXME: Low-hanging fruit where we could omit a type hint:
22092222
// - auto x = TypeName(...);
22102223
// - auto x = (TypeName) (...);

0 commit comments

Comments
 (0)