[clang-tools-extra] 6385c03 - [clangd] Fix inlayhints crash, don't assume functions have FunctionTypeLocs

Sam McCall via cfe-commits cfe-commits at lists.llvm.org
Thu May 5 09:51:43 PDT 2022


Author: Sam McCall
Date: 2022-05-05T18:51:36+02:00
New Revision: 6385c039b821c76c215c3ae6202f9e702a01fd92

URL: https://github.com/llvm/llvm-project/commit/6385c039b821c76c215c3ae6202f9e702a01fd92
DIFF: https://github.com/llvm/llvm-project/commit/6385c039b821c76c215c3ae6202f9e702a01fd92.diff

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

Fixes https://github.com/clangd/clangd/issues/1140

Added: 
    

Modified: 
    clang-tools-extra/clangd/InlayHints.cpp
    clang-tools-extra/clangd/unittests/InlayHintTests.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clangd/InlayHints.cpp b/clang-tools-extra/clangd/InlayHints.cpp
index c5876ac5554b6..315dd5109d7c9 100644
--- a/clang-tools-extra/clangd/InlayHints.cpp
+++ b/clang-tools-extra/clangd/InlayHints.cpp
@@ -258,8 +258,10 @@ class InlayHintVisitor : public RecursiveASTVisitor<InlayHintVisitor> {
   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;
   }

diff  --git a/clang-tools-extra/clangd/unittests/InlayHintTests.cpp b/clang-tools-extra/clangd/unittests/InlayHintTests.cpp
index 1b39ebb8de5ae..31ce7d0df0dc5 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; };


        


More information about the cfe-commits mailing list