[PATCH] D45482: [clangd] Match AST and Index label for template Symbols
This revision was automatically updated to reflect the committed changes. Closed by commit rL330004: [clangd] Match AST and Index label for template Symbols (authored by ibiryukov, committed by ). Herald added a subscriber: llvm-commits. Repository: rL LLVM https://reviews.llvm.org/D45482 Files: clang-tools-extra/trunk/clangd/index/SymbolCollector.cpp clang-tools-extra/trunk/unittests/clangd/FileIndexTests.cpp Index: clang-tools-extra/trunk/unittests/clangd/FileIndexTests.cpp === --- clang-tools-extra/trunk/unittests/clangd/FileIndexTests.cpp +++ clang-tools-extra/trunk/unittests/clangd/FileIndexTests.cpp @@ -195,6 +195,44 @@ EXPECT_TRUE(SeenSymbol); } +TEST(FileIndexTest, TemplateParamsInLabel) { + auto Source = R"cpp( +template +class vector { +}; + +template +vector make_vector(Ty* begin, Ty* end) {} +)cpp"; + + FileIndex M; + M.update("f", build("f", Source).getPointer()); + + FuzzyFindRequest Req; + Req.Query = ""; + bool SeenVector = false; + bool SeenMakeVector = false; + M.fuzzyFind(Req, [&](const Symbol ) { +if (Sym.Name == "vector") { + EXPECT_EQ(Sym.CompletionLabel, "vector"); + EXPECT_EQ(Sym.CompletionSnippetInsertText, "vector<${1:class Ty}>"); + EXPECT_EQ(Sym.CompletionPlainInsertText, "vector"); + SeenVector = true; + return; +} + +if (Sym.Name == "make_vector") { + EXPECT_EQ(Sym.CompletionLabel, "make_vector(Ty *begin, Ty *end)"); + EXPECT_EQ(Sym.CompletionSnippetInsertText, +"make_vector(${1:Ty *begin}, ${2:Ty *end})"); + EXPECT_EQ(Sym.CompletionPlainInsertText, "make_vector"); + SeenMakeVector = true; +} + }); + EXPECT_TRUE(SeenVector); + EXPECT_TRUE(SeenMakeVector); +} + } // namespace } // namespace clangd } // namespace clang Index: clang-tools-extra/trunk/clangd/index/SymbolCollector.cpp === --- clang-tools-extra/trunk/clangd/index/SymbolCollector.cpp +++ clang-tools-extra/trunk/clangd/index/SymbolCollector.cpp @@ -14,6 +14,7 @@ #include "../URI.h" #include "CanonicalIncludes.h" #include "clang/AST/DeclCXX.h" +#include "clang/AST/DeclTemplate.h" #include "clang/ASTMatchers/ASTMatchFinder.h" #include "clang/Basic/SourceManager.h" #include "clang/Index/IndexSymbol.h" @@ -26,6 +27,19 @@ namespace clangd { namespace { +/// If \p ND is a template specialization, returns the primary template. +/// Otherwise, returns \p ND. +const NamedDecl (const NamedDecl ) { + if (auto Cls = dyn_cast()) { +if (auto T = Cls->getDescribedTemplate()) + return *T; + } else if (auto Func = dyn_cast()) { +if (auto T = Func->getPrimaryTemplate()) + return *T; + } + return ND; +} + // Returns a URI of \p Path. Firstly, this makes the \p Path absolute using the // current working directory of the given SourceManager if the Path is not an // absolute path. If failed, this resolves relative paths against \p FallbackDir @@ -325,7 +339,8 @@ // Add completion info. // FIXME: we may want to choose a different redecl, or combine from several. assert(ASTCtx && PP.get() && "ASTContext and Preprocessor must be set."); - CodeCompletionResult SymbolCompletion(, 0); + // We use the primary template, as clang does during code completion. + CodeCompletionResult SymbolCompletion((ND), 0); const auto *CCS = SymbolCompletion.CreateCodeCompletionString( *ASTCtx, *PP, CodeCompletionContext::CCC_Name, *CompletionAllocator, *CompletionTUInfo, Index: clang-tools-extra/trunk/unittests/clangd/FileIndexTests.cpp === --- clang-tools-extra/trunk/unittests/clangd/FileIndexTests.cpp +++ clang-tools-extra/trunk/unittests/clangd/FileIndexTests.cpp @@ -195,6 +195,44 @@ EXPECT_TRUE(SeenSymbol); } +TEST(FileIndexTest, TemplateParamsInLabel) { + auto Source = R"cpp( +template +class vector { +}; + +template +vector make_vector(Ty* begin, Ty* end) {} +)cpp"; + + FileIndex M; + M.update("f", build("f", Source).getPointer()); + + FuzzyFindRequest Req; + Req.Query = ""; + bool SeenVector = false; + bool SeenMakeVector = false; + M.fuzzyFind(Req, [&](const Symbol ) { +if (Sym.Name == "vector") { + EXPECT_EQ(Sym.CompletionLabel, "vector"); + EXPECT_EQ(Sym.CompletionSnippetInsertText, "vector<${1:class Ty}>"); + EXPECT_EQ(Sym.CompletionPlainInsertText, "vector"); + SeenVector = true; + return; +} + +if (Sym.Name == "make_vector") { + EXPECT_EQ(Sym.CompletionLabel, "make_vector(Ty *begin, Ty *end)"); + EXPECT_EQ(Sym.CompletionSnippetInsertText, +"make_vector(${1:Ty *begin}, ${2:Ty *end})"); + EXPECT_EQ(Sym.CompletionPlainInsertText, "make_vector"); + SeenMakeVector = true; +} + }); + EXPECT_TRUE(SeenVector); + EXPECT_TRUE(SeenMakeVector); +} + } // namespace } // namespace clangd } // namespace
[PATCH] D45482: [clangd] Match AST and Index label for template Symbols
ilya-biryukov added inline comments. Comment at: unittests/clangd/FileIndexTests.cpp:218 + EXPECT_EQ(Sym.CompletionLabel, "vector"); + EXPECT_EQ(Sym.CompletionSnippetInsertText, "vector<${1:class Ty}>"); + SeenVector = true; sammccall wrote: > If snippets are off, we'll get "vector", not "vector<>", right? > > (Probably no need to test this explicitly, but I just want to be sure) Yes, that's exactly the case. Testing doesn't hurt too, added a test. Repository: rCTE Clang Tools Extra https://reviews.llvm.org/D45482 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D45482: [clangd] Match AST and Index label for template Symbols
This revision was automatically updated to reflect the committed changes. Closed by commit rCTE330004: [clangd] Match AST and Index label for template Symbols (authored by ibiryukov, committed by ). Changed prior to commit: https://reviews.llvm.org/D45482?vs=142372=142373#toc Repository: rL LLVM https://reviews.llvm.org/D45482 Files: clangd/index/SymbolCollector.cpp unittests/clangd/FileIndexTests.cpp Index: clangd/index/SymbolCollector.cpp === --- clangd/index/SymbolCollector.cpp +++ clangd/index/SymbolCollector.cpp @@ -14,6 +14,7 @@ #include "../URI.h" #include "CanonicalIncludes.h" #include "clang/AST/DeclCXX.h" +#include "clang/AST/DeclTemplate.h" #include "clang/ASTMatchers/ASTMatchFinder.h" #include "clang/Basic/SourceManager.h" #include "clang/Index/IndexSymbol.h" @@ -26,6 +27,19 @@ namespace clangd { namespace { +/// If \p ND is a template specialization, returns the primary template. +/// Otherwise, returns \p ND. +const NamedDecl (const NamedDecl ) { + if (auto Cls = dyn_cast()) { +if (auto T = Cls->getDescribedTemplate()) + return *T; + } else if (auto Func = dyn_cast()) { +if (auto T = Func->getPrimaryTemplate()) + return *T; + } + return ND; +} + // Returns a URI of \p Path. Firstly, this makes the \p Path absolute using the // current working directory of the given SourceManager if the Path is not an // absolute path. If failed, this resolves relative paths against \p FallbackDir @@ -325,7 +339,8 @@ // Add completion info. // FIXME: we may want to choose a different redecl, or combine from several. assert(ASTCtx && PP.get() && "ASTContext and Preprocessor must be set."); - CodeCompletionResult SymbolCompletion(, 0); + // We use the primary template, as clang does during code completion. + CodeCompletionResult SymbolCompletion((ND), 0); const auto *CCS = SymbolCompletion.CreateCodeCompletionString( *ASTCtx, *PP, CodeCompletionContext::CCC_Name, *CompletionAllocator, *CompletionTUInfo, Index: unittests/clangd/FileIndexTests.cpp === --- unittests/clangd/FileIndexTests.cpp +++ unittests/clangd/FileIndexTests.cpp @@ -195,6 +195,44 @@ EXPECT_TRUE(SeenSymbol); } +TEST(FileIndexTest, TemplateParamsInLabel) { + auto Source = R"cpp( +template +class vector { +}; + +template +vector make_vector(Ty* begin, Ty* end) {} +)cpp"; + + FileIndex M; + M.update("f", build("f", Source).getPointer()); + + FuzzyFindRequest Req; + Req.Query = ""; + bool SeenVector = false; + bool SeenMakeVector = false; + M.fuzzyFind(Req, [&](const Symbol ) { +if (Sym.Name == "vector") { + EXPECT_EQ(Sym.CompletionLabel, "vector"); + EXPECT_EQ(Sym.CompletionSnippetInsertText, "vector<${1:class Ty}>"); + EXPECT_EQ(Sym.CompletionPlainInsertText, "vector"); + SeenVector = true; + return; +} + +if (Sym.Name == "make_vector") { + EXPECT_EQ(Sym.CompletionLabel, "make_vector(Ty *begin, Ty *end)"); + EXPECT_EQ(Sym.CompletionSnippetInsertText, +"make_vector(${1:Ty *begin}, ${2:Ty *end})"); + EXPECT_EQ(Sym.CompletionPlainInsertText, "make_vector"); + SeenMakeVector = true; +} + }); + EXPECT_TRUE(SeenVector); + EXPECT_TRUE(SeenMakeVector); +} + } // namespace } // namespace clangd } // namespace clang Index: clangd/index/SymbolCollector.cpp === --- clangd/index/SymbolCollector.cpp +++ clangd/index/SymbolCollector.cpp @@ -14,6 +14,7 @@ #include "../URI.h" #include "CanonicalIncludes.h" #include "clang/AST/DeclCXX.h" +#include "clang/AST/DeclTemplate.h" #include "clang/ASTMatchers/ASTMatchFinder.h" #include "clang/Basic/SourceManager.h" #include "clang/Index/IndexSymbol.h" @@ -26,6 +27,19 @@ namespace clangd { namespace { +/// If \p ND is a template specialization, returns the primary template. +/// Otherwise, returns \p ND. +const NamedDecl (const NamedDecl ) { + if (auto Cls = dyn_cast()) { +if (auto T = Cls->getDescribedTemplate()) + return *T; + } else if (auto Func = dyn_cast()) { +if (auto T = Func->getPrimaryTemplate()) + return *T; + } + return ND; +} + // Returns a URI of \p Path. Firstly, this makes the \p Path absolute using the // current working directory of the given SourceManager if the Path is not an // absolute path. If failed, this resolves relative paths against \p FallbackDir @@ -325,7 +339,8 @@ // Add completion info. // FIXME: we may want to choose a different redecl, or combine from several. assert(ASTCtx && PP.get() && "ASTContext and Preprocessor must be set."); - CodeCompletionResult SymbolCompletion(, 0); + // We use the primary template, as clang does during code completion. + CodeCompletionResult SymbolCompletion((ND), 0); const auto *CCS = SymbolCompletion.CreateCodeCompletionString( *ASTCtx,
[PATCH] D45482: [clangd] Match AST and Index label for template Symbols
ilya-biryukov updated this revision to Diff 142372. ilya-biryukov marked 2 inline comments as done. ilya-biryukov added a comment. - Also test plain text completion text - Clarify the comment - Simplify conditions in getTemplateOrThis Repository: rCTE Clang Tools Extra https://reviews.llvm.org/D45482 Files: clangd/index/SymbolCollector.cpp unittests/clangd/FileIndexTests.cpp Index: unittests/clangd/FileIndexTests.cpp === --- unittests/clangd/FileIndexTests.cpp +++ unittests/clangd/FileIndexTests.cpp @@ -195,6 +195,44 @@ EXPECT_TRUE(SeenSymbol); } +TEST(FileIndexTest, TemplateParamsInLabel) { + auto Source = R"cpp( +template +class vector { +}; + +template +vector make_vector(Ty* begin, Ty* end) {} +)cpp"; + + FileIndex M; + M.update("f", build("f", Source).getPointer()); + + FuzzyFindRequest Req; + Req.Query = ""; + bool SeenVector = false; + bool SeenMakeVector = false; + M.fuzzyFind(Req, [&](const Symbol ) { +if (Sym.Name == "vector") { + EXPECT_EQ(Sym.CompletionLabel, "vector"); + EXPECT_EQ(Sym.CompletionSnippetInsertText, "vector<${1:class Ty}>"); + EXPECT_EQ(Sym.CompletionPlainInsertText, "vector"); + SeenVector = true; + return; +} + +if (Sym.Name == "make_vector") { + EXPECT_EQ(Sym.CompletionLabel, "make_vector(Ty *begin, Ty *end)"); + EXPECT_EQ(Sym.CompletionSnippetInsertText, +"make_vector(${1:Ty *begin}, ${2:Ty *end})"); + EXPECT_EQ(Sym.CompletionPlainInsertText, "make_vector"); + SeenMakeVector = true; +} + }); + EXPECT_TRUE(SeenVector); + EXPECT_TRUE(SeenMakeVector); +} + } // namespace } // namespace clangd } // namespace clang Index: clangd/index/SymbolCollector.cpp === --- clangd/index/SymbolCollector.cpp +++ clangd/index/SymbolCollector.cpp @@ -14,6 +14,7 @@ #include "../URI.h" #include "CanonicalIncludes.h" #include "clang/AST/DeclCXX.h" +#include "clang/AST/DeclTemplate.h" #include "clang/ASTMatchers/ASTMatchFinder.h" #include "clang/Basic/SourceManager.h" #include "clang/Index/IndexSymbol.h" @@ -26,6 +27,19 @@ namespace clangd { namespace { +/// If \p ND is a template specialization, returns the primary template. +/// Otherwise, returns \p ND. +const NamedDecl (const NamedDecl ) { + if (auto Cls = dyn_cast()) { +if (auto T = Cls->getDescribedTemplate()) + return *T; + } else if (auto Func = dyn_cast()) { +if (auto T = Func->getPrimaryTemplate()) + return *T; + } + return ND; +} + // Returns a URI of \p Path. Firstly, this makes the \p Path absolute using the // current working directory of the given SourceManager if the Path is not an // absolute path. If failed, this resolves relative paths against \p FallbackDir @@ -325,7 +339,8 @@ // Add completion info. // FIXME: we may want to choose a different redecl, or combine from several. assert(ASTCtx && PP.get() && "ASTContext and Preprocessor must be set."); - CodeCompletionResult SymbolCompletion(, 0); + // We use the primary template, as clang does during code completion. + CodeCompletionResult SymbolCompletion((ND), 0); const auto *CCS = SymbolCompletion.CreateCodeCompletionString( *ASTCtx, *PP, CodeCompletionContext::CCC_Name, *CompletionAllocator, *CompletionTUInfo, Index: unittests/clangd/FileIndexTests.cpp === --- unittests/clangd/FileIndexTests.cpp +++ unittests/clangd/FileIndexTests.cpp @@ -195,6 +195,44 @@ EXPECT_TRUE(SeenSymbol); } +TEST(FileIndexTest, TemplateParamsInLabel) { + auto Source = R"cpp( +template +class vector { +}; + +template +vector make_vector(Ty* begin, Ty* end) {} +)cpp"; + + FileIndex M; + M.update("f", build("f", Source).getPointer()); + + FuzzyFindRequest Req; + Req.Query = ""; + bool SeenVector = false; + bool SeenMakeVector = false; + M.fuzzyFind(Req, [&](const Symbol ) { +if (Sym.Name == "vector") { + EXPECT_EQ(Sym.CompletionLabel, "vector"); + EXPECT_EQ(Sym.CompletionSnippetInsertText, "vector<${1:class Ty}>"); + EXPECT_EQ(Sym.CompletionPlainInsertText, "vector"); + SeenVector = true; + return; +} + +if (Sym.Name == "make_vector") { + EXPECT_EQ(Sym.CompletionLabel, "make_vector(Ty *begin, Ty *end)"); + EXPECT_EQ(Sym.CompletionSnippetInsertText, +"make_vector(${1:Ty *begin}, ${2:Ty *end})"); + EXPECT_EQ(Sym.CompletionPlainInsertText, "make_vector"); + SeenMakeVector = true; +} + }); + EXPECT_TRUE(SeenVector); + EXPECT_TRUE(SeenMakeVector); +} + } // namespace } // namespace clangd } // namespace clang Index: clangd/index/SymbolCollector.cpp === --- clangd/index/SymbolCollector.cpp +++ clangd/index/SymbolCollector.cpp @@ -14,6 +14,7 @@ #include "../URI.h" #include
[PATCH] D45482: [clangd] Match AST and Index label for template Symbols
sammccall accepted this revision. sammccall added a comment. This revision is now accepted and ready to land. Nice, I'd been wondering about that... Comment at: clangd/index/SymbolCollector.cpp:36 + return *T; +return ND; + } uber-nit: these three return statements are a bit confusing to me. Maybe omit them and if/elseif, so the default case falls through to the bottom. Comment at: clangd/index/SymbolCollector.cpp:331 + // We call getTemplateOrThis, since this is what clang's code completion gets + // from the lookup in an actual run. + CodeCompletionResult SymbolCompletion((ND), 0); "an actual run" confused me here. Maybe "We use the primary template, as clang does during code completion"? Comment at: unittests/clangd/FileIndexTests.cpp:218 + EXPECT_EQ(Sym.CompletionLabel, "vector"); + EXPECT_EQ(Sym.CompletionSnippetInsertText, "vector<${1:class Ty}>"); + SeenVector = true; If snippets are off, we'll get "vector", not "vector<>", right? (Probably no need to test this explicitly, but I just want to be sure) Repository: rCTE Clang Tools Extra https://reviews.llvm.org/D45482 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D45482: [clangd] Match AST and Index label for template Symbols
ilya-biryukov created this revision. ilya-biryukov added reviewers: sammccall, hokein. Herald added subscribers: MaskRay, ioeric, jkorous-apple, klimek. Previsouly, class completions items from the index were missing template parameters in both the snippet and the label. Repository: rCTE Clang Tools Extra https://reviews.llvm.org/D45482 Files: clangd/index/SymbolCollector.cpp unittests/clangd/FileIndexTests.cpp Index: unittests/clangd/FileIndexTests.cpp === --- unittests/clangd/FileIndexTests.cpp +++ unittests/clangd/FileIndexTests.cpp @@ -195,6 +195,42 @@ EXPECT_TRUE(SeenSymbol); } +TEST(FileIndexTest, TemplateParamsInLabel) { + auto Source = R"cpp( +template +class vector { +}; + +template +vector make_vector(Ty* begin, Ty* end) {} +)cpp"; + + FileIndex M; + M.update("f", build("f", Source).getPointer()); + + FuzzyFindRequest Req; + Req.Query = ""; + bool SeenVector = false; + bool SeenMakeVector = false; + M.fuzzyFind(Req, [&](const Symbol ) { +if (Sym.Name == "vector") { + EXPECT_EQ(Sym.CompletionLabel, "vector"); + EXPECT_EQ(Sym.CompletionSnippetInsertText, "vector<${1:class Ty}>"); + SeenVector = true; + return; +} + +if (Sym.Name == "make_vector") { + EXPECT_EQ(Sym.CompletionLabel, "make_vector(Ty *begin, Ty *end)"); + EXPECT_EQ(Sym.CompletionSnippetInsertText, +"make_vector(${1:Ty *begin}, ${2:Ty *end})"); + SeenMakeVector = true; +} + }); + EXPECT_TRUE(SeenVector); + EXPECT_TRUE(SeenMakeVector); +} + } // namespace } // namespace clangd } // namespace clang Index: clangd/index/SymbolCollector.cpp === --- clangd/index/SymbolCollector.cpp +++ clangd/index/SymbolCollector.cpp @@ -14,6 +14,7 @@ #include "../URI.h" #include "CanonicalIncludes.h" #include "clang/AST/DeclCXX.h" +#include "clang/AST/DeclTemplate.h" #include "clang/ASTMatchers/ASTMatchFinder.h" #include "clang/Basic/SourceManager.h" #include "clang/Index/IndexSymbol.h" @@ -26,6 +27,22 @@ namespace clangd { namespace { +/// If \p ND is a template specialization, returns the primary template. +/// Otherwise, returns \p ND. +const NamedDecl (const NamedDecl ) { + if (auto Cls = dyn_cast()) { +if (auto T = Cls->getDescribedTemplate()) + return *T; +return ND; + } + if (auto Func = dyn_cast()) { +if (auto T = Func->getPrimaryTemplate()) + return *T; +return ND; + } + return ND; +} + // Returns a URI of \p Path. Firstly, this makes the \p Path absolute using the // current working directory of the given SourceManager if the Path is not an // absolute path. If failed, this resolves relative paths against \p FallbackDir @@ -310,7 +327,9 @@ // Add completion info. // FIXME: we may want to choose a different redecl, or combine from several. assert(ASTCtx && PP.get() && "ASTContext and Preprocessor must be set."); - CodeCompletionResult SymbolCompletion(, 0); + // We call getTemplateOrThis, since this is what clang's code completion gets + // from the lookup in an actual run. + CodeCompletionResult SymbolCompletion((ND), 0); const auto *CCS = SymbolCompletion.CreateCodeCompletionString( *ASTCtx, *PP, CodeCompletionContext::CCC_Name, *CompletionAllocator, *CompletionTUInfo, Index: unittests/clangd/FileIndexTests.cpp === --- unittests/clangd/FileIndexTests.cpp +++ unittests/clangd/FileIndexTests.cpp @@ -195,6 +195,42 @@ EXPECT_TRUE(SeenSymbol); } +TEST(FileIndexTest, TemplateParamsInLabel) { + auto Source = R"cpp( +template +class vector { +}; + +template +vector make_vector(Ty* begin, Ty* end) {} +)cpp"; + + FileIndex M; + M.update("f", build("f", Source).getPointer()); + + FuzzyFindRequest Req; + Req.Query = ""; + bool SeenVector = false; + bool SeenMakeVector = false; + M.fuzzyFind(Req, [&](const Symbol ) { +if (Sym.Name == "vector") { + EXPECT_EQ(Sym.CompletionLabel, "vector"); + EXPECT_EQ(Sym.CompletionSnippetInsertText, "vector<${1:class Ty}>"); + SeenVector = true; + return; +} + +if (Sym.Name == "make_vector") { + EXPECT_EQ(Sym.CompletionLabel, "make_vector(Ty *begin, Ty *end)"); + EXPECT_EQ(Sym.CompletionSnippetInsertText, +"make_vector(${1:Ty *begin}, ${2:Ty *end})"); + SeenMakeVector = true; +} + }); + EXPECT_TRUE(SeenVector); + EXPECT_TRUE(SeenMakeVector); +} + } // namespace } // namespace clangd } // namespace clang Index: clangd/index/SymbolCollector.cpp === --- clangd/index/SymbolCollector.cpp +++ clangd/index/SymbolCollector.cpp @@ -14,6 +14,7 @@ #include "../URI.h" #include "CanonicalIncludes.h" #include "clang/AST/DeclCXX.h" +#include "clang/AST/DeclTemplate.h" #include "clang/ASTMatchers/ASTMatchFinder.h"