[PATCH] D45482: [clangd] Match AST and Index label for template Symbols
Ilya Biryukov via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Tue Apr 10 03:51:23 PDT 2018
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 Ty>
+class vector {
+};
+
+template <class Ty>
+vector<Ty> 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 &Sym) {
+ if (Sym.Name == "vector") {
+ EXPECT_EQ(Sym.CompletionLabel, "vector<class Ty>");
+ 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 &getTemplateOrThis(const NamedDecl &ND) {
+ if (auto Cls = dyn_cast<CXXRecordDecl>(&ND)) {
+ if (auto T = Cls->getDescribedTemplate())
+ return *T;
+ return ND;
+ }
+ if (auto Func = dyn_cast<FunctionDecl>(&ND)) {
+ 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(&ND, 0);
+ // We call getTemplateOrThis, since this is what clang's code completion gets
+ // from the lookup in an actual run.
+ CodeCompletionResult SymbolCompletion(&getTemplateOrThis(ND), 0);
const auto *CCS = SymbolCompletion.CreateCodeCompletionString(
*ASTCtx, *PP, CodeCompletionContext::CCC_Name, *CompletionAllocator,
*CompletionTUInfo,
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D45482.141821.patch
Type: text/x-patch
Size: 3076 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180410/254c2a75/attachment.bin>
More information about the cfe-commits
mailing list