diff --git a/clang-tools-extra/clangd/ClangdServer.h b/clang-tools-extra/clangd/ClangdServer.h --- a/clang-tools-extra/clangd/ClangdServer.h +++ b/clang-tools-extra/clangd/ClangdServer.h @@ -24,6 +24,7 @@ #include "index/Background.h" #include "index/FileIndex.h" #include "index/Index.h" +#include "index/StdLib.h" #include "refactor/Rename.h" #include "refactor/Tweak.h" #include "support/Cancellation.h" @@ -167,7 +168,11 @@ FeatureModuleSet *FeatureModules = nullptr; explicit operator TUScheduler::Options() const; + + // Automatically index the standard library - experimental feature + bool IndexStandardLibrary = false; }; + // Sensible default options for use in tests. // Features like indexing must be enabled if desired. static Options optsForTest(); @@ -403,6 +408,8 @@ std::unique_ptr BackgroundIdx; // Storage for merged views of the various indexes. std::vector> MergedIdx; + // If present, the index of the standard library. + std::unique_ptr StandardLibraryIdx; // When set, provides clang-tidy options for a specific file. TidyProviderRef ClangTidyProvider; diff --git a/clang-tools-extra/clangd/ClangdServer.cpp b/clang-tools-extra/clangd/ClangdServer.cpp --- a/clang-tools-extra/clangd/ClangdServer.cpp +++ b/clang-tools-extra/clangd/ClangdServer.cpp @@ -27,6 +27,7 @@ #include "index/CanonicalIncludes.h" #include "index/FileIndex.h" #include "index/Merge.h" +#include "index/StdLib.h" #include "refactor/Rename.h" #include "refactor/Tweak.h" #include "support/Logger.h" @@ -175,6 +176,16 @@ this->Index = Idx; } }; + if (Opts.IndexStandardLibrary) { + auto SLIndex = indexStandardLibrary(TFS); + if (!SLIndex) { + elog("Unable to build standard library index. Not using it."); + elog(toString(SLIndex.takeError()).c_str()); + } else { + StandardLibraryIdx = std::move(SLIndex.get()); + AddIndex(StandardLibraryIdx.get()); + } + } if (Opts.StaticIndex) AddIndex(Opts.StaticIndex); if (Opts.BackgroundIndex) { diff --git a/clang-tools-extra/clangd/tool/ClangdMain.cpp b/clang-tools-extra/clangd/tool/ClangdMain.cpp --- a/clang-tools-extra/clangd/tool/ClangdMain.cpp +++ b/clang-tools-extra/clangd/tool/ClangdMain.cpp @@ -484,6 +484,18 @@ init(true), }; +opt IndexStandardLibrary{ + "index-standard-library", + cat(Features), + desc("Background index should always include the STL headers even if \n" + "not used at the moment. This will enable code completion and \n" + "include fixer." + "WARNING: This option is experimental only, and will be removed " + "eventually. Don't rely on it"), + init(false), + Hidden, +}; + std::function getMemoryCleanupFunction() { if (!EnableMallocTrim) return nullptr; @@ -912,6 +924,7 @@ if (ForceOffsetEncoding != OffsetEncoding::UnsupportedEncoding) Opts.Encoding = ForceOffsetEncoding; + Opts.IndexStandardLibrary = IndexStandardLibrary; if (CheckFile.getNumOccurrences()) { llvm::SmallString<256> Path; if (auto Error =