diff --git a/clang-tools-extra/clangd/Config.h b/clang-tools-extra/clangd/Config.h --- a/clang-tools-extra/clangd/Config.h +++ b/clang-tools-extra/clangd/Config.h @@ -86,6 +86,7 @@ BackgroundPolicy Background = BackgroundPolicy::Build; ExternalIndexSpec External; bool StandardLibrary = true; + bool ReservedIdentifiers = false; } Index; enum class IncludesPolicy { diff --git a/clang-tools-extra/clangd/ConfigCompile.cpp b/clang-tools-extra/clangd/ConfigCompile.cpp --- a/clang-tools-extra/clangd/ConfigCompile.cpp +++ b/clang-tools-extra/clangd/ConfigCompile.cpp @@ -337,6 +337,11 @@ [Val(**F.StandardLibrary)](const Params &, Config &C) { C.Index.StandardLibrary = Val; }); + if (F.ReservedIdentifiers) + Out.Apply.push_back( + [Val(**F.ReservedIdentifiers)](const Params &, Config &C) { + C.Index.ReservedIdentifiers = Val; + }); } void compile(Fragment::IndexBlock::ExternalBlock &&External, diff --git a/clang-tools-extra/clangd/ConfigFragment.h b/clang-tools-extra/clangd/ConfigFragment.h --- a/clang-tools-extra/clangd/ConfigFragment.h +++ b/clang-tools-extra/clangd/ConfigFragment.h @@ -199,9 +199,12 @@ std::optional> MountPoint; }; std::optional> External; - // Whether the standard library visible from this file should be indexed. - // This makes all standard library symbols available, included or not. + /// Whether the standard library visible from this file should be indexed. + /// This makes all standard library symbols available, included or not. std::optional> StandardLibrary; + /// Whether identifiers reserved for use by the implementation (e.g. + /// beginning with two underscores) should be indexed. + std::optional> ReservedIdentifiers; }; IndexBlock Index; diff --git a/clang-tools-extra/clangd/ConfigYAML.cpp b/clang-tools-extra/clangd/ConfigYAML.cpp --- a/clang-tools-extra/clangd/ConfigYAML.cpp +++ b/clang-tools-extra/clangd/ConfigYAML.cpp @@ -194,6 +194,10 @@ if (auto StandardLibrary = boolValue(N, "StandardLibrary")) F.StandardLibrary = *StandardLibrary; }); + Dict.handle("ReservedIdentifiers", [&](Node &N) { + if (auto ReservedIdentifiers = boolValue(N, "ReservedIdentifiers")) + F.ReservedIdentifiers = *ReservedIdentifiers; + }); Dict.parse(N); } diff --git a/clang-tools-extra/clangd/Hover.cpp b/clang-tools-extra/clangd/Hover.cpp --- a/clang-tools-extra/clangd/Hover.cpp +++ b/clang-tools-extra/clangd/Hover.cpp @@ -335,8 +335,9 @@ // Skip querying for non-indexable symbols, there's no point. // We're searching for symbols that might be indexed outside this main file. - if (!SymbolCollector::shouldCollectSymbol(ND, ND.getASTContext(), - SymbolCollector::Options(), + SymbolCollector::Options Opts; + Opts.CollectReserved = Config::current().Index.ReservedIdentifiers; + if (!SymbolCollector::shouldCollectSymbol(ND, ND.getASTContext(), Opts, /*IsMainFileOnly=*/false)) return; auto ID = getSymbolID(&ND); diff --git a/clang-tools-extra/clangd/XRefs.cpp b/clang-tools-extra/clangd/XRefs.cpp --- a/clang-tools-extra/clangd/XRefs.cpp +++ b/clang-tools-extra/clangd/XRefs.cpp @@ -7,6 +7,7 @@ //===----------------------------------------------------------------------===// #include "XRefs.h" #include "AST.h" +#include "Config.h" #include "FindSymbols.h" #include "FindTarget.h" #include "Headers.h" @@ -929,6 +930,7 @@ SymbolCollector::Options CollectorOpts; CollectorOpts.CollectMainFileSymbols = true; + CollectorOpts.CollectReserved = Config::current().Index.ReservedIdentifiers; for (SourceLocation L : Locs) { L = SM.getFileLoc(L); if (const auto *Tok = TB.spelledTokenAt(L)) diff --git a/clang-tools-extra/clangd/index/Background.cpp b/clang-tools-extra/clangd/index/Background.cpp --- a/clang-tools-extra/clangd/index/Background.cpp +++ b/clang-tools-extra/clangd/index/Background.cpp @@ -304,6 +304,7 @@ return true; }; IndexOpts.CollectMainFileRefs = true; + IndexOpts.CollectReserved = Config::current().Index.ReservedIdentifiers; IndexFileIn Index; auto Action = createStaticIndexingAction( diff --git a/clang-tools-extra/clangd/index/FileIndex.cpp b/clang-tools-extra/clangd/index/FileIndex.cpp --- a/clang-tools-extra/clangd/index/FileIndex.cpp +++ b/clang-tools-extra/clangd/index/FileIndex.cpp @@ -8,6 +8,7 @@ #include "FileIndex.h" #include "CollectMacros.h" +#include "Config.h" #include "ParsedAST.h" #include "index/CanonicalIncludes.h" #include "index/Index.h" @@ -57,7 +58,8 @@ CollectorOpts.CollectMainFileRefs = CollectMainFileRefs; // We want stdlib implementation details in the index only if we've opened the // file in question. This does means xrefs won't work, though. - CollectorOpts.CollectReserved = IsIndexMainAST; + CollectorOpts.CollectReserved = + IsIndexMainAST || Config::current().Index.ReservedIdentifiers; index::IndexingOptions IndexOpts; // We only need declarations, because we don't count references. diff --git a/clang-tools-extra/clangd/index/StdLib.cpp b/clang-tools-extra/clangd/index/StdLib.cpp --- a/clang-tools-extra/clangd/index/StdLib.cpp +++ b/clang-tools-extra/clangd/index/StdLib.cpp @@ -230,6 +230,7 @@ IndexOpts.CollectMainFileRefs = false; IndexOpts.CollectMacro = true; IndexOpts.StoreAllDocumentation = true; + // FIXME: Should we respect the Index.ReservedIdentifiers config here? // Sadly we can't use IndexOpts.FileFilter to restrict indexing scope. // Files from outside the StdLibLocation may define true std symbols anyway. // We end up "blessing" such headers, and can only do that by indexing diff --git a/clang-tools-extra/clangd/indexer/IndexerMain.cpp b/clang-tools-extra/clangd/indexer/IndexerMain.cpp --- a/clang-tools-extra/clangd/indexer/IndexerMain.cpp +++ b/clang-tools-extra/clangd/indexer/IndexerMain.cpp @@ -45,6 +45,8 @@ std::unique_ptr create() override { SymbolCollector::Options Opts; Opts.CountReferences = true; + // FIXME: Do we have access to Config here? If so, should we respect + // Index.ReservedIdentifiers? Opts.FileFilter = [&](const SourceManager &SM, FileID FID) { const auto F = SM.getFileEntryRefForID(FID); if (!F) diff --git a/clang-tools-extra/clangd/refactor/Rename.cpp b/clang-tools-extra/clangd/refactor/Rename.cpp --- a/clang-tools-extra/clangd/refactor/Rename.cpp +++ b/clang-tools-extra/clangd/refactor/Rename.cpp @@ -8,6 +8,7 @@ #include "refactor/Rename.h" #include "AST.h" +#include "Config.h" #include "FindTarget.h" #include "ParsedAST.h" #include "Selection.h" @@ -234,9 +235,10 @@ else if (!DeclaredInMainFile) IsMainFileOnly = false; // If the symbol is not indexable, we disallow rename. + SymbolCollector::Options Options; + Options.CollectReserved = Config::current().Index.ReservedIdentifiers; if (!SymbolCollector::shouldCollectSymbol( - RenameDecl, RenameDecl.getASTContext(), SymbolCollector::Options(), - IsMainFileOnly)) + RenameDecl, RenameDecl.getASTContext(), Options, IsMainFileOnly)) return ReasonToReject::NonIndexable; return std::nullopt;