Index: clangd/CMakeLists.txt =================================================================== --- clangd/CMakeLists.txt +++ clangd/CMakeLists.txt @@ -40,6 +40,7 @@ index/CanonicalIncludes.cpp index/FileIndex.cpp index/Index.cpp + index/IndexAction.cpp index/MemIndex.cpp index/Merge.cpp index/Serialization.cpp Index: clangd/index/IndexAction.h =================================================================== --- /dev/null +++ clangd/index/IndexAction.h @@ -0,0 +1,32 @@ +//===--- IndexAction.h - Run the indexer as a frontend action ----*- C++-*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_INDEX_ACTION_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_INDEX_ACTION_H +#include "SymbolCollector.h" +#include "clang/Frontend/FrontendActions.h" + +namespace clang { +namespace clangd { + +// Creates an action that indexes translation units and delivers the results +// for SymbolsCallback (each slab corresponds to one TU). +// +// Only a subset of SymbolCollector::Options are respected: +// - include paths are always collected, and canonicalized appropriately +// - references are always counted +// - the symbol origin is always Static +std::unique_ptr +createStaticIndexingAction(SymbolCollector::Options Opts, + std::function SymbolsCallback); + +} // namespace clangd +} // namespace clang + +#endif Index: clangd/index/IndexAction.cpp =================================================================== --- /dev/null +++ clangd/index/IndexAction.cpp @@ -0,0 +1,73 @@ +#include "IndexAction.h" +#include "clang/Frontend/CompilerInstance.h" +#include "clang/Index/IndexDataConsumer.h" +#include "clang/Index/IndexingAction.h" +#include "clang/Tooling/Tooling.h" +namespace clang { +namespace clangd { +namespace { + +// Wraps the index action and reports index data after each translation unit. +class IndexAction : public WrapperFrontendAction { +public: + IndexAction(std::shared_ptr C, + std::unique_ptr Includes, + const index::IndexingOptions &Opts, + std::function &SymbolsCallback) + : WrapperFrontendAction(index::createIndexingAction(C, Opts, nullptr)), + SymbolsCallback(SymbolsCallback), Collector(C), + Includes(std::move(Includes)), + PragmaHandler(collectIWYUHeaderMaps(this->Includes.get())) {} + + std::unique_ptr CreateASTConsumer(CompilerInstance &CI, + StringRef InFile) override { + CI.getPreprocessor().addCommentHandler(PragmaHandler.get()); + return WrapperFrontendAction::CreateASTConsumer(CI, InFile); + } + + bool BeginInvocation(CompilerInstance &CI) override { + // We want all comments, not just the doxygen ones. + CI.getLangOpts().CommentOpts.ParseAllComments = true; + return WrapperFrontendAction::BeginInvocation(CI); + } + + void EndSourceFileAction() override { + WrapperFrontendAction::EndSourceFileAction(); + + const auto &CI = getCompilerInstance(); + if (CI.hasDiagnostics() && + CI.getDiagnostics().hasUncompilableErrorOccurred()) { + llvm::errs() << "Skipping TU due to uncompilable errors\n"; + return; + } + SymbolsCallback(Collector->takeSymbols()); + } + +private: + std::function SymbolsCallback; + std::shared_ptr Collector; + std::unique_ptr Includes; + std::unique_ptr PragmaHandler; +}; + +} // namespace + +std::unique_ptr +createStaticIndexingAction(SymbolCollector::Options Opts, + std::function SymbolsCallback) { + index::IndexingOptions IndexOpts; + IndexOpts.SystemSymbolFilter = + index::IndexingOptions::SystemSymbolFilterKind::All; + Opts.CollectIncludePath = true; + Opts.CountReferences = true; + Opts.Origin = SymbolOrigin::Static; + auto Includes = llvm::make_unique(); + addSystemHeadersMapping(Includes.get()); + Opts.Includes = Includes.get(); + return llvm::make_unique( + std::make_shared(std::move(Opts)), std::move(Includes), + IndexOpts, SymbolsCallback); +}; + +} // namespace clangd +} // namespace clang Index: clangd/index/Serialization.h =================================================================== --- clangd/index/Serialization.h +++ clangd/index/Serialization.h @@ -40,23 +40,26 @@ YAML, // Human-readable format, suitable for experiments and debugging. }; +// Holds the contents of an index file that was read. +struct IndexFileIn { + llvm::Optional Symbols; +}; +// Parse an index file. The input must be a RIFF container chunk. +llvm::Expected readIndexFile(llvm::StringRef); + // Specifies the contents of an index file to be written. struct IndexFileOut { const SymbolSlab *Symbols; // TODO: Support serializing symbol occurrences. // TODO: Support serializing Dex posting lists. IndexFileFormat Format = IndexFileFormat::RIFF; -}; -// Serializes an index file. (This is a RIFF container chunk). -llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const IndexFileOut &O); -// Holds the contents of an index file that was read. -struct IndexFileIn { - llvm::Optional Symbols; - IndexFileFormat Format; + IndexFileOut() = default; + IndexFileOut(const IndexFileIn &I) + : Symbols(I.Symbols ? I.Symbols.getPointer() : nullptr) {} }; -// Parse an index file. The input must be a RIFF container chunk. -llvm::Expected readIndexFile(llvm::StringRef); +// Serializes an index file. +llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const IndexFileOut &O); std::string toYAML(const Symbol &); // Returned symbol is backed by the YAML input. Index: clangd/index/SymbolCollector.h =================================================================== --- clangd/index/SymbolCollector.h +++ clangd/index/SymbolCollector.h @@ -6,6 +6,8 @@ // License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_SYMBOL_COLLECTOR_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_SYMBOL_COLLECTOR_H #include "CanonicalIncludes.h" #include "Index.h" @@ -122,3 +124,4 @@ } // namespace clangd } // namespace clang +#endif Index: clangd/indexer/IndexerMain.cpp =================================================================== --- clangd/indexer/IndexerMain.cpp +++ clangd/indexer/IndexerMain.cpp @@ -15,6 +15,7 @@ #include "RIFF.h" #include "index/CanonicalIncludes.h" #include "index/Index.h" +#include "index/IndexAction.h" #include "index/Merge.h" #include "index/Serialization.h" #include "index/SymbolCollector.h" @@ -86,68 +87,12 @@ SymbolIndexActionFactory(SymbolsConsumer &Consumer) : Consumer(Consumer) {} clang::FrontendAction *create() override { - // Wraps the index action and reports collected symbols to the execution - // context at the end of each translation unit. - class WrappedIndexAction : public WrapperFrontendAction { - public: - WrappedIndexAction(std::shared_ptr C, - std::unique_ptr Includes, - const index::IndexingOptions &Opts, - SymbolsConsumer &Consumer) - : WrapperFrontendAction( - index::createIndexingAction(C, Opts, nullptr)), - Consumer(Consumer), Collector(C), Includes(std::move(Includes)), - PragmaHandler(collectIWYUHeaderMaps(this->Includes.get())) {} - - std::unique_ptr - CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override { - CI.getPreprocessor().addCommentHandler(PragmaHandler.get()); - return WrapperFrontendAction::CreateASTConsumer(CI, InFile); - } - - bool BeginInvocation(CompilerInstance &CI) override { - // We want all comments, not just the doxygen ones. - CI.getLangOpts().CommentOpts.ParseAllComments = true; - return WrapperFrontendAction::BeginInvocation(CI); - } - - void EndSourceFileAction() override { - WrapperFrontendAction::EndSourceFileAction(); - - const auto &CI = getCompilerInstance(); - if (CI.hasDiagnostics() && - CI.getDiagnostics().hasUncompilableErrorOccurred()) { - llvm::errs() - << "Found uncompilable errors in the translation unit. Igoring " - "collected symbols...\n"; - return; - } - - Consumer.consumeSymbols(Collector->takeSymbols()); - } - - private: - SymbolsConsumer &Consumer; - std::shared_ptr Collector; - std::unique_ptr Includes; - std::unique_ptr PragmaHandler; - }; - - index::IndexingOptions IndexOpts; - IndexOpts.SystemSymbolFilter = - index::IndexingOptions::SystemSymbolFilterKind::All; - IndexOpts.IndexFunctionLocals = false; auto CollectorOpts = SymbolCollector::Options(); CollectorOpts.FallbackDir = AssumedHeaderDir; - CollectorOpts.CollectIncludePath = true; - CollectorOpts.CountReferences = true; - CollectorOpts.Origin = SymbolOrigin::Static; - auto Includes = llvm::make_unique(); - addSystemHeadersMapping(Includes.get()); - CollectorOpts.Includes = Includes.get(); - return new WrappedIndexAction( - std::make_shared(std::move(CollectorOpts)), - std::move(Includes), IndexOpts, Consumer); + return createStaticIndexingAction( + CollectorOpts, + [&](SymbolSlab S) { Consumer.consumeSymbols(std::move(S)); }) + .release(); } SymbolsConsumer &Consumer;