[clangd] Fix inlayhints crash, don't assume functions have FunctionTypeLocs

Fixes https://github.com/clangd/clangd/issues/1140
This commit is contained in:
Sam McCall 2022-05-05 18:50:44 +02:00
parent 21c028ac94
commit 6385c039b8
2 changed files with 14 additions and 2 deletions

View File

@ -258,8 +258,10 @@ public:
bool VisitFunctionDecl(FunctionDecl *D) {
if (auto *FPT =
llvm::dyn_cast<FunctionProtoType>(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;
}

View File

@ -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; };