}
};
-class IndexAction : public WrapperFrontendAction {
- IndexingOptions IndexOpts;
+class IndexActionBase {
+protected:
std::shared_ptr<IndexDataConsumer> DataConsumer;
- std::unique_ptr<IndexingContext> IndexCtx;
+ IndexingContext IndexCtx;
+
+ IndexActionBase(std::shared_ptr<IndexDataConsumer> dataConsumer,
+ IndexingOptions Opts)
+ : DataConsumer(std::move(dataConsumer)),
+ IndexCtx(Opts, *DataConsumer) {}
+
+ std::unique_ptr<IndexASTConsumer> createIndexASTConsumer() {
+ return llvm::make_unique<IndexASTConsumer>(IndexCtx);
+ }
+ void finish() {
+ DataConsumer->finish();
+ }
+};
+
+class IndexAction : public ASTFrontendAction, IndexActionBase {
public:
- IndexAction(std::unique_ptr<FrontendAction> WrappedAction,
- std::shared_ptr<IndexDataConsumer> DataConsumer,
+ IndexAction(std::shared_ptr<IndexDataConsumer> DataConsumer,
IndexingOptions Opts)
+ : IndexActionBase(std::move(DataConsumer), Opts) {}
+
+protected:
+ std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
+ StringRef InFile) override {
+ return createIndexASTConsumer();
+ }
+
+ void EndSourceFileAction() override {
+ FrontendAction::EndSourceFileAction();
+ finish();
+ }
+};
+
+class WrappingIndexAction : public WrapperFrontendAction, IndexActionBase {
+ bool IndexActionFailed = false;
+
+public:
+ WrappingIndexAction(std::unique_ptr<FrontendAction> WrappedAction,
+ std::shared_ptr<IndexDataConsumer> DataConsumer,
+ IndexingOptions Opts)
: WrapperFrontendAction(std::move(WrappedAction)),
- IndexOpts(Opts),
- DataConsumer(std::move(DataConsumer)) {}
+ IndexActionBase(std::move(DataConsumer), Opts) {}
protected:
std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
} // anonymous namespace
-void IndexAction::EndSourceFileAction() {
+void WrappingIndexAction::EndSourceFileAction() {
// Invoke wrapped action's method.
WrapperFrontendAction::EndSourceFileAction();
-
- bool IndexActionFailed = !IndexCtx;
if (!IndexActionFailed)
- DataConsumer->finish();
+ finish();
}
std::unique_ptr<ASTConsumer>
-IndexAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
+WrappingIndexAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
auto OtherConsumer = WrapperFrontendAction::CreateASTConsumer(CI, InFile);
- if (!OtherConsumer)
+ if (!OtherConsumer) {
+ IndexActionFailed = true;
return nullptr;
-
- IndexCtx.reset(new IndexingContext(IndexOpts, *DataConsumer));
+ }
std::vector<std::unique_ptr<ASTConsumer>> Consumers;
Consumers.push_back(std::move(OtherConsumer));
- Consumers.push_back(llvm::make_unique<IndexASTConsumer>(*IndexCtx));
+ Consumers.push_back(createIndexASTConsumer());
return llvm::make_unique<MultiplexConsumer>(std::move(Consumers));
}
std::unique_ptr<FrontendAction>
-index::createIndexingAction(std::unique_ptr<FrontendAction> WrappedAction,
- std::shared_ptr<IndexDataConsumer> DataConsumer,
- IndexingOptions Opts) {
- return llvm::make_unique<IndexAction>(std::move(WrappedAction),
- std::move(DataConsumer),
- Opts);
+index::createIndexingAction(std::shared_ptr<IndexDataConsumer> DataConsumer,
+ IndexingOptions Opts,
+ std::unique_ptr<FrontendAction> WrappedAction) {
+ if (WrappedAction)
+ return llvm::make_unique<WrappingIndexAction>(std::move(WrappedAction),
+ std::move(DataConsumer),
+ Opts);
+ return llvm::make_unique<IndexAction>(std::move(DataConsumer), Opts);
}