From 41104a9406dd284d984f7bee30c7756fcfe2b59e Mon Sep 17 00:00:00 2001 From: Haojian Wu Date: Mon, 11 Nov 2019 10:34:29 +0100 Subject: [PATCH] [clangd] Fix a regression of not showing documentation from forward declarations. Summary: There is a regression from https://reviews.llvm.org/D68467. Unlike class forward declarations, function ducomentation is written in the declaration in headers, the function definition doesn't contain any documentation, cases like: ``` foo.h // this is foo. void foo(); foo.cc void foo() {} ``` we should still show documentation from the foo declaration. Reviewers: ilya-biryukov Reviewed By: ilya-biryukov Subscribers: MaskRay, jkorous, arphaman, kadircet, usaxena95, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D69961 --- clang-tools-extra/clangd/index/Merge.cpp | 16 +++++++++++----- .../clangd/unittests/IndexTests.cpp | 11 +++++++++-- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/clang-tools-extra/clangd/index/Merge.cpp b/clang-tools-extra/clangd/index/Merge.cpp index 76c5955893e4..0c9d0c8d3b12 100644 --- a/clang-tools-extra/clangd/index/Merge.cpp +++ b/clang-tools-extra/clangd/index/Merge.cpp @@ -186,11 +186,17 @@ Symbol mergeSymbol(const Symbol &L, const Symbol &R) { S.Signature = O.Signature; if (S.CompletionSnippetSuffix == "") S.CompletionSnippetSuffix = O.CompletionSnippetSuffix; - // Don't accept documentation from bare forward declarations, if there is a - // definition and it didn't provide one. S is often an undocumented class, - // and O is a non-canonical forward decl preceded by an irrelevant comment. - if (S.Documentation == "" && !S.Definition) - S.Documentation = O.Documentation; + if (S.Documentation == "") { + // Don't accept documentation from bare forward class declarations, if there + // is a definition and it didn't provide one. S is often an undocumented + // class, and O is a non-canonical forward decl preceded by an irrelevant + // comment. + bool IsClass = S.SymInfo.Kind == index::SymbolKind::Class || + S.SymInfo.Kind == index::SymbolKind::Struct || + S.SymInfo.Kind == index::SymbolKind::Union; + if (!IsClass || !S.Definition) + S.Documentation = O.Documentation; + } if (S.ReturnType == "") S.ReturnType = O.ReturnType; if (S.Type == "") diff --git a/clang-tools-extra/clangd/unittests/IndexTests.cpp b/clang-tools-extra/clangd/unittests/IndexTests.cpp index 9f0265d4190a..e868828d7868 100644 --- a/clang-tools-extra/clangd/unittests/IndexTests.cpp +++ b/clang-tools-extra/clangd/unittests/IndexTests.cpp @@ -408,13 +408,20 @@ TEST(MergeIndexTest, Refs) { } TEST(MergeIndexTest, NonDocumentation) { + using index::SymbolKind; Symbol L, R; L.ID = R.ID = SymbolID("x"); L.Definition.FileURI = "file:/x.h"; R.Documentation = "Forward declarations because x.h is too big to include"; + for (auto ClassLikeKind : + {SymbolKind::Class, SymbolKind::Struct, SymbolKind::Union}) { + L.SymInfo.Kind = ClassLikeKind; + EXPECT_EQ(mergeSymbol(L, R).Documentation, ""); + } - Symbol M = mergeSymbol(L, R); - EXPECT_EQ(M.Documentation, ""); + L.SymInfo.Kind = SymbolKind::Function; + R.Documentation = "Documentation from non-class symbols should be included"; + EXPECT_EQ(mergeSymbol(L, R).Documentation, R.Documentation); } MATCHER_P2(IncludeHeaderWithRef, IncludeHeader, References, "") {