diff --git a/clang-tools-extra/clangd/Hover.cpp b/clang-tools-extra/clangd/Hover.cpp --- a/clang-tools-extra/clangd/Hover.cpp +++ b/clang-tools-extra/clangd/Hover.cpp @@ -262,24 +262,30 @@ // Returns the decl that should be used for querying comments, either from index // or AST. const NamedDecl *getDeclForComment(const NamedDecl *D) { + const NamedDecl *DeclForComment = D; if (const auto *TSD = llvm::dyn_cast(D)) { // Template may not be instantiated e.g. if the type didn't need to be // complete; fallback to primary template. if (TSD->getTemplateSpecializationKind() == TSK_Undeclared) - return TSD->getSpecializedTemplate(); - if (const auto *TIP = TSD->getTemplateInstantiationPattern()) - return TIP; - } - if (const auto *TSD = llvm::dyn_cast(D)) { + DeclForComment = TSD->getSpecializedTemplate(); + else if (const auto *TIP = TSD->getTemplateInstantiationPattern()) + DeclForComment = TIP; + } else if (const auto *TSD = + llvm::dyn_cast(D)) { if (TSD->getTemplateSpecializationKind() == TSK_Undeclared) - return TSD->getSpecializedTemplate(); - if (const auto *TIP = TSD->getTemplateInstantiationPattern()) - return TIP; - } - if (const auto *FD = D->getAsFunction()) + DeclForComment = TSD->getSpecializedTemplate(); + else if (const auto *TIP = TSD->getTemplateInstantiationPattern()) + DeclForComment = TIP; + } else if (const auto *FD = D->getAsFunction()) if (const auto *TIP = FD->getTemplateInstantiationPattern()) - return TIP; - return D; + DeclForComment = TIP; + // Ensure that getDeclForComment(getDeclForComment(X)) = getDeclForComment(X). + // This is usually not needed, but in strange cases of comparision operators + // being instantiated from spasceship operater, which itself is a template + // instantiation the recursrive call is necessary. + if (D != DeclForComment) + DeclForComment = getDeclForComment(DeclForComment); + return DeclForComment; } // Look up information about D from the index, and add it to Hover. diff --git a/clang-tools-extra/clangd/unittests/HoverTests.cpp b/clang-tools-extra/clangd/unittests/HoverTests.cpp --- a/clang-tools-extra/clangd/unittests/HoverTests.cpp +++ b/clang-tools-extra/clangd/unittests/HoverTests.cpp @@ -2934,6 +2934,35 @@ def)pt"; EXPECT_EQ(HI.present().asPlainText(), ExpectedPlaintext); } + +TEST(Hover, SpaceshipTemplateNoCrash) { + Annotations T(R"cpp( + namespace std { + struct strong_ordering { + int n; + constexpr operator int() const { return n; } + static const strong_ordering equal, greater, less; + }; + constexpr strong_ordering strong_ordering::equal = {0}; + constexpr strong_ordering strong_ordering::greater = {1}; + constexpr strong_ordering strong_ordering::less = {-1}; + } + + template + struct S { + // Foo bar baz + friend auto operator<=>(S, S) = default; + }; + static_assert(S() =^= S()); + )cpp"); + + TestTU TU = TestTU::withCode(T.code()); + TU.ExtraArgs.push_back("-std=c++20"); + auto AST = TU.build(); + auto HI = getHover(AST, T.point(), format::getLLVMStyle(), nullptr); + EXPECT_EQ(HI->Documentation, "Foo bar baz"); +} + } // namespace } // namespace clangd } // namespace clang