Skip to content

Commit

Permalink
[clangd] Avoid crash when summarizing pointer-to-member expr for bloc…
Browse files Browse the repository at this point in the history
…k-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
  • Loading branch information
HighCommander4 committed Dec 29, 2023
1 parent da5378e commit dbd1fb8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion clang-tools-extra/clangd/InlayHints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ std::string summarizeExpr(const Expr *E) {
// Step through implicit nodes that clang doesn't classify as such.
std::string VisitCXXMemberCallExpr(const CXXMemberCallExpr *E) {
// Call to operator bool() inside if (X): dispatch to X.
if (E->getNumArgs() == 0 &&
if (E->getNumArgs() == 0 && E->getMethodDecl() &&
E->getMethodDecl()->getDeclName().getNameKind() ==
DeclarationName::CXXConversionFunctionName &&
E->getSourceRange() ==
Expand Down
13 changes: 13 additions & 0 deletions clang-tools-extra/clangd/unittests/InlayHintTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2205,6 +2205,19 @@ TEST(BlockEndHints, Macro) {
ExpectedHint{" // struct S1", "S1"});
}

TEST(BlockEndHints, PointerToMemberFunction) {
// Do not crash trying to summarize `a->*p`.
assertBlockEndHints(R"cpp(
class A {};
using Predicate = bool(A::*)();
void foo(A* a, Predicate p) {
if ((a->*p)()) {
$ptrmem[[}]]
} // suppress
)cpp",
ExpectedHint{" // if", "ptrmem"});
}

// FIXME: Low-hanging fruit where we could omit a type hint:
// - auto x = TypeName(...);
// - auto x = (TypeName) (...);
Expand Down

0 comments on commit dbd1fb8

Please sign in to comment.