From 6385c039b821c76c215c3ae6202f9e702a01fd92 Mon Sep 17 00:00:00 2001 From: Sam McCall Date: Thu, 5 May 2022 18:50:44 +0200 Subject: [PATCH] [clangd] Fix inlayhints crash, don't assume functions have FunctionTypeLocs Fixes https://github.com/clangd/clangd/issues/1140 --- clang-tools-extra/clangd/InlayHints.cpp | 6 ++++-- clang-tools-extra/clangd/unittests/InlayHintTests.cpp | 10 ++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/clang-tools-extra/clangd/InlayHints.cpp b/clang-tools-extra/clangd/InlayHints.cpp index c5876ac5554b..315dd5109d7c 100644 --- a/clang-tools-extra/clangd/InlayHints.cpp +++ b/clang-tools-extra/clangd/InlayHints.cpp @@ -258,8 +258,10 @@ public: bool VisitFunctionDecl(FunctionDecl *D) { if (auto *FPT = llvm::dyn_cast(D->getType().getTypePtr())) { - if (!FPT->hasTrailingReturn()) - addReturnTypeHint(D, D->getFunctionTypeLoc().getRParenLoc()); + if (!FPT->hasTrailingReturn()) { + if (auto FTL = D->getFunctionTypeLoc()) + addReturnTypeHint(D, FTL.getRParenLoc()); + } } return true; } diff --git a/clang-tools-extra/clangd/unittests/InlayHintTests.cpp b/clang-tools-extra/clangd/unittests/InlayHintTests.cpp index 1b39ebb8de5a..31ce7d0df0dc 100644 --- a/clang-tools-extra/clangd/unittests/InlayHintTests.cpp +++ b/clang-tools-extra/clangd/unittests/InlayHintTests.cpp @@ -811,6 +811,16 @@ TEST(TypeHints, SinglyInstantiatedTemplate) { ExpectedHint{": void *", "a"}); } +TEST(TypeHints, Aliased) { + // Check that we don't crash for functions without a FunctionTypeLoc. + // https://github.com/clangd/clangd/issues/1140 + TestTU TU = TestTU::withCode("void foo(void){} extern typeof(foo) foo;"); + TU.ExtraArgs.push_back("-xc"); + auto AST = TU.build(); + + EXPECT_THAT(hintsOfKind(AST, InlayHintKind::TypeHint), IsEmpty()); +} + TEST(DesignatorHints, Basic) { assertDesignatorHints(R"cpp( struct S { int x, y, z; };