Skip to content

Commit 5518bb2

Browse files
[clangd] Check getFunctionTypeLoc() for validity in InlayHintVisitor (#117296)
Fixes clangd/clangd#2223
1 parent a9882bd commit 5518bb2

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

clang-tools-extra/clangd/InlayHints.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -626,10 +626,15 @@ class InlayHintVisitor : public RecursiveASTVisitor<InlayHintVisitor> {
626626

627627
bool VisitLambdaExpr(LambdaExpr *E) {
628628
FunctionDecl *D = E->getCallOperator();
629-
if (!E->hasExplicitResultType())
630-
addReturnTypeHint(D, E->hasExplicitParameters()
631-
? D->getFunctionTypeLoc().getRParenLoc()
632-
: E->getIntroducerRange().getEnd());
629+
if (!E->hasExplicitResultType()) {
630+
SourceLocation TypeHintLoc;
631+
if (!E->hasExplicitParameters())
632+
TypeHintLoc = E->getIntroducerRange().getEnd();
633+
else if (auto FTL = D->getFunctionTypeLoc())
634+
TypeHintLoc = FTL.getRParenLoc();
635+
if (TypeHintLoc.isValid())
636+
addReturnTypeHint(D, TypeHintLoc);
637+
}
633638
return true;
634639
}
635640

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1576,6 +1576,22 @@ TEST(TypeHints, Aliased) {
15761576
EXPECT_THAT(hintsOfKind(AST, InlayHintKind::Type), IsEmpty());
15771577
}
15781578

1579+
TEST(TypeHints, CallingConvention) {
1580+
// Check that we don't crash for lambdas without a FunctionTypeLoc
1581+
// https://github.com/clangd/clangd/issues/2223
1582+
std::string Code = R"cpp(
1583+
void test() {
1584+
[]() __cdecl {};
1585+
}
1586+
)cpp";
1587+
TestTU TU = TestTU::withCode(Code);
1588+
TU.ExtraArgs.push_back("--target=x86_64-w64-mingw32");
1589+
TU.PredefineMacros = true; // for the __cdecl
1590+
auto AST = TU.build();
1591+
1592+
EXPECT_THAT(hintsOfKind(AST, InlayHintKind::Type), IsEmpty());
1593+
}
1594+
15791595
TEST(TypeHints, Decltype) {
15801596
assertTypeHints(R"cpp(
15811597
$a[[decltype(0)]] a;

0 commit comments

Comments
 (0)