Skip to content

Commit ec31d40

Browse files
[clangd] Avoid crash when summarizing pointer-to-member expr for block-end hint
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 190b917 commit ec31d40

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
@@ -2172,6 +2172,19 @@ TEST(BlockEndHints, Macro) {
21722172
ExpectedHint{" // struct S1", "S1"});
21732173
}
21742174

2175+
TEST(BlockEndHints, PointerToMemberFunction) {
2176+
// Do not crash trying to summarize `a->*p`.
2177+
assertBlockEndHints(R"cpp(
2178+
class A {};
2179+
using Predicate = bool(A::*)();
2180+
void foo(A* a, Predicate p) {
2181+
if ((a->*p)()) {
2182+
$ptrmem[[}]]
2183+
} // suppress
2184+
)cpp",
2185+
ExpectedHint{" // if", "ptrmem"});
2186+
}
2187+
21752188
// FIXME: Low-hanging fruit where we could omit a type hint:
21762189
// - auto x = TypeName(...);
21772190
// - auto x = (TypeName) (...);

0 commit comments

Comments
 (0)