summaryrefslogtreecommitdiff
path: root/clang-tools-extra
diff options
context:
space:
mode:
authorSam McCall <sam.mccall@gmail.com>2018-09-25 20:02:36 +0000
committerSam McCall <sam.mccall@gmail.com>2018-09-25 20:02:36 +0000
commit2ab34f8d7c058b2420eb20cd6b3788717f3d1471 (patch)
treea13cb1aebff9c82d21b5f5bd8764b3e1698b9952 /clang-tools-extra
parenteba9b9a03aea419f027ae81db777e98e18fb524e (diff)
[clangd] Extract mapper logic from clangd-indexer into a library.
Summary: Soon we can drop support for MR-via-YAML. I need to modify some out-of-tree versions to use the library, first. Reviewers: kadircet Subscribers: mgorny, ilya-biryukov, ioeric, MaskRay, jkorous, arphaman, cfe-commits Differential Revision: https://reviews.llvm.org/D52465
Diffstat (limited to 'clang-tools-extra')
-rw-r--r--clang-tools-extra/clangd/CMakeLists.txt1
-rw-r--r--clang-tools-extra/clangd/index/IndexAction.cpp73
-rw-r--r--clang-tools-extra/clangd/index/IndexAction.h32
-rw-r--r--clang-tools-extra/clangd/index/Serialization.h21
-rw-r--r--clang-tools-extra/clangd/index/SymbolCollector.h3
-rw-r--r--clang-tools-extra/clangd/indexer/IndexerMain.cpp65
6 files changed, 126 insertions, 69 deletions
diff --git a/clang-tools-extra/clangd/CMakeLists.txt b/clang-tools-extra/clangd/CMakeLists.txt
index a8cd64a1039..0894a927ead 100644
--- a/clang-tools-extra/clangd/CMakeLists.txt
+++ b/clang-tools-extra/clangd/CMakeLists.txt
@@ -40,6 +40,7 @@ add_clang_library(clangDaemon
index/CanonicalIncludes.cpp
index/FileIndex.cpp
index/Index.cpp
+ index/IndexAction.cpp
index/MemIndex.cpp
index/Merge.cpp
index/Serialization.cpp
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
diff --git a/clang-tools-extra/clangd/indexer/IndexerMain.cpp b/clang-tools-extra/clangd/indexer/IndexerMain.cpp
index dbd43cd1952..9136693822d 100644
--- a/clang-tools-extra/clangd/indexer/IndexerMain.cpp
+++ b/clang-tools-extra/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 @@ public:
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<SymbolCollector> C,
- std::unique_ptr<CanonicalIncludes> 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<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()
- << "Found uncompilable errors in the translation unit. Igoring "
- "collected symbols...\n";
- return;
- }
-
- Consumer.consumeSymbols(Collector->takeSymbols());
- }
-
- private:
- SymbolsConsumer &Consumer;
- std::shared_ptr<SymbolCollector> Collector;
- std::unique_ptr<CanonicalIncludes> Includes;
- std::unique_ptr<CommentHandler> 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<CanonicalIncludes>();
- addSystemHeadersMapping(Includes.get());
- CollectorOpts.Includes = Includes.get();
- return new WrappedIndexAction(
- std::make_shared<SymbolCollector>(std::move(CollectorOpts)),
- std::move(Includes), IndexOpts, Consumer);
+ return createStaticIndexingAction(
+ CollectorOpts,
+ [&](SymbolSlab S) { Consumer.consumeSymbols(std::move(S)); })
+ .release();
}
SymbolsConsumer &Consumer;