diff options
Diffstat (limited to 'clang-tools-extra/clangd/index')
| -rw-r--r-- | clang-tools-extra/clangd/index/IndexAction.cpp | 73 | ||||
| -rw-r--r-- | clang-tools-extra/clangd/index/IndexAction.h | 32 | ||||
| -rw-r--r-- | clang-tools-extra/clangd/index/Serialization.h | 21 | ||||
| -rw-r--r-- | clang-tools-extra/clangd/index/SymbolCollector.h | 3 |
4 files changed, 120 insertions, 9 deletions
diff --git a/clang-tools-extra/clangd/index/IndexAction.cpp b/clang-tools-extra/clangd/index/IndexAction.cpp new file mode 100644 index 00000000000..b9dcb6e19c0 --- /dev/null +++ b/clang-tools-extra/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<SymbolCollector> C, + std::unique_ptr<CanonicalIncludes> Includes, + const index::IndexingOptions &Opts, + std::function<void(SymbolSlab)> &SymbolsCallback) + : WrapperFrontendAction(index::createIndexingAction(C, Opts, nullptr)), + SymbolsCallback(SymbolsCallback), Collector(C), + Includes(std::move(Includes)), + PragmaHandler(collectIWYUHeaderMaps(this->Includes.get())) {} + + std::unique_ptr<ASTConsumer> 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<void(SymbolSlab)> SymbolsCallback; + std::shared_ptr<SymbolCollector> Collector; + std::unique_ptr<CanonicalIncludes> Includes; + std::unique_ptr<CommentHandler> PragmaHandler; +}; + +} // namespace + +std::unique_ptr<FrontendAction> +createStaticIndexingAction(SymbolCollector::Options Opts, + std::function<void(SymbolSlab)> 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<CanonicalIncludes>(); + addSystemHeadersMapping(Includes.get()); + Opts.Includes = Includes.get(); + return llvm::make_unique<IndexAction>( + std::make_shared<SymbolCollector>(std::move(Opts)), std::move(Includes), + IndexOpts, SymbolsCallback); +}; + +} // namespace clangd +} // namespace clang diff --git a/clang-tools-extra/clangd/index/IndexAction.h b/clang-tools-extra/clangd/index/IndexAction.h new file mode 100644 index 00000000000..b51bfd2520f --- /dev/null +++ b/clang-tools-extra/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<FrontendAction> +createStaticIndexingAction(SymbolCollector::Options Opts, + std::function<void(SymbolSlab)> SymbolsCallback); + +} // namespace clangd +} // namespace clang + +#endif diff --git a/clang-tools-extra/clangd/index/Serialization.h b/clang-tools-extra/clangd/index/Serialization.h index c73ce23b5e5..4240bdb722b 100644 --- a/clang-tools-extra/clangd/index/Serialization.h +++ b/clang-tools-extra/clangd/index/Serialization.h @@ -40,23 +40,26 @@ enum class IndexFileFormat { YAML, // Human-readable format, suitable for experiments and debugging. }; +// Holds the contents of an index file that was read. +struct IndexFileIn { + llvm::Optional<SymbolSlab> Symbols; +}; +// Parse an index file. The input must be a RIFF container chunk. +llvm::Expected<IndexFileIn> 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<SymbolSlab> 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<IndexFileIn> 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. diff --git a/clang-tools-extra/clangd/index/SymbolCollector.h b/clang-tools-extra/clangd/index/SymbolCollector.h index db0e3074ecf..1994183c21e 100644 --- a/clang-tools-extra/clangd/index/SymbolCollector.h +++ b/clang-tools-extra/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 @@ private: } // namespace clangd } // namespace clang +#endif |

