Index: clangd/index/Index.h =================================================================== --- clangd/index/Index.h +++ clangd/index/Index.h @@ -10,12 +10,14 @@ #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_INDEX_H #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_INDEX_H +#include "ExpectedTypes.h" #include "clang/Index/IndexSymbol.h" #include "clang/Lex/Lexer.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/DenseSet.h" #include "llvm/ADT/Hashing.h" #include "llvm/ADT/Optional.h" +#include "llvm/ADT/STLExtras.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringExtras.h" #include "llvm/ADT/StringRef.h" @@ -270,6 +272,9 @@ ImplementationDetail = 1 << 2, }; + /// Type of the symbol, used for scoring purposes. + llvm::StringRef Type; + SymbolFlag Flags = SymbolFlag::None; /// FIXME: also add deprecation message and fixit? }; @@ -285,7 +290,8 @@ // Invokes Callback with each StringRef& contained in the Symbol. // Useful for deduplicating backing strings. -template void visitStrings(Symbol &S, const Callback &CB) { +inline void visitStrings(Symbol &S, + llvm::function_ref CB) { CB(S.Name); CB(S.Scope); CB(S.CanonicalDeclaration.FileURI); @@ -296,6 +302,7 @@ CB(S.ReturnType); for (auto &Include : S.IncludeHeaders) CB(Include.IncludeHeader); + CB(S.Type); } // Computes query-independent quality score for a Symbol. Index: clangd/index/Index.cpp =================================================================== --- clangd/index/Index.cpp +++ clangd/index/Index.cpp @@ -114,7 +114,8 @@ } // Copy the underlying data of the symbol into the owned arena. -static void own(Symbol &S, UniqueStringSaver &Strings) { +static void own(Symbol &S, UniqueStringSaver &Strings, + BumpPtrAllocator &Arena) { visitStrings(S, [&](StringRef &V) { V = Strings.save(V); }); } @@ -122,10 +123,10 @@ auto R = SymbolIndex.try_emplace(S.ID, Symbols.size()); if (R.second) { Symbols.push_back(S); - own(Symbols.back(), UniqueStrings); + own(Symbols.back(), UniqueStrings, Arena); } else { auto &Copy = Symbols[R.first->second] = S; - own(Copy, UniqueStrings); + own(Copy, UniqueStrings, Arena); } } @@ -138,7 +139,7 @@ BumpPtrAllocator NewArena; UniqueStringSaver Strings(NewArena); for (auto &S : Symbols) - own(S, Strings); + own(S, Strings, NewArena); return SymbolSlab(std::move(NewArena), std::move(Symbols)); } Index: clangd/index/SymbolCollector.h =================================================================== --- clangd/index/SymbolCollector.h +++ clangd/index/SymbolCollector.h @@ -76,6 +76,8 @@ /// If this is set, only collect symbols/references from a file if /// `FileFilter(SM, FID)` is true. If not set, all files are indexed. std::function FileFilter = nullptr; + /// Collect type information. Used to improve code completion ranking. + bool CollectTypes = true; }; SymbolCollector(Options Opts); Index: clangd/index/SymbolCollector.cpp =================================================================== --- clangd/index/SymbolCollector.cpp +++ clangd/index/SymbolCollector.cpp @@ -596,6 +596,11 @@ if (!Include.empty()) S.IncludeHeaders.emplace_back(Include, 1); + llvm::Optional Type; + if (Opts.CollectTypes && (S.Flags & Symbol::IndexedForCodeCompletion)) + Type = OpaqueType::fromCompletionResult(*ASTCtx, SymbolCompletion); + S.Type = Type ? Type->rawStr() : ""; + S.Origin = Opts.Origin; if (ND.getAvailability() == AR_Deprecated) S.Flags |= Symbol::Deprecated; Index: clangd/index/YAMLSerialization.cpp =================================================================== --- clangd/index/YAMLSerialization.cpp +++ clangd/index/YAMLSerialization.cpp @@ -179,6 +179,7 @@ IO.mapOptional("Documentation", Sym.Documentation); IO.mapOptional("ReturnType", Sym.ReturnType); IO.mapOptional("IncludeHeaders", Sym.IncludeHeaders); + IO.mapOptional("Type", Sym.Type); } }; Index: clangd/indexer/IndexerMain.cpp =================================================================== --- clangd/indexer/IndexerMain.cpp +++ clangd/indexer/IndexerMain.cpp @@ -37,12 +37,18 @@ "binary RIFF format")), cl::init(IndexFileFormat::RIFF)); +static llvm::cl::opt + CollectTypes("collect-types", + llvm::cl::desc("Collect type information during indexing"), + llvm::cl::init(false), llvm::cl::Hidden); + class IndexActionFactory : public tooling::FrontendActionFactory { public: IndexActionFactory(IndexFileIn &Result) : Result(Result) {} clang::FrontendAction *create() override { SymbolCollector::Options Opts; + Opts.CollectTypes = CollectTypes; return createStaticIndexingAction( Opts, [&](SymbolSlab S) {