diff --git a/clang/lib/Index/IndexingAction.cpp b/clang/lib/Index/IndexingAction.cpp index 47e0ab3a0188..8605a2cfa149 100644 --- a/clang/lib/Index/IndexingAction.cpp +++ b/clang/lib/Index/IndexingAction.cpp @@ -54,13 +54,20 @@ public: }; class IndexASTConsumer final : public ASTConsumer { - std::shared_ptr PP; + std::shared_ptr DataConsumer; std::shared_ptr IndexCtx; + std::shared_ptr PP; public: - IndexASTConsumer(std::shared_ptr PP, - std::shared_ptr IndexCtx) - : PP(std::move(PP)), IndexCtx(std::move(IndexCtx)) {} + IndexASTConsumer(std::shared_ptr DataConsumer, + const IndexingOptions &Opts, + std::shared_ptr PP) + : DataConsumer(std::move(DataConsumer)), + IndexCtx(new IndexingContext(Opts, *this->DataConsumer)), + PP(std::move(PP)) { + assert(this->DataConsumer != nullptr); + assert(this->PP != nullptr); + } protected: void Initialize(ASTContext &Context) override { @@ -83,27 +90,25 @@ protected: } void HandleTranslationUnit(ASTContext &Ctx) override { + DataConsumer->finish(); } }; class IndexActionBase { protected: std::shared_ptr DataConsumer; - std::shared_ptr IndexCtx; + IndexingOptions Opts; - IndexActionBase(std::shared_ptr dataConsumer, + IndexActionBase(std::shared_ptr DataConsumer, IndexingOptions Opts) - : DataConsumer(std::move(dataConsumer)), - IndexCtx(new IndexingContext(Opts, *DataConsumer)) {} + : DataConsumer(std::move(DataConsumer)), Opts(Opts) { + assert(this->DataConsumer != nullptr); + } std::unique_ptr createIndexASTConsumer(CompilerInstance &CI) { - return std::make_unique(CI.getPreprocessorPtr(), - IndexCtx); - } - - void finish() { - DataConsumer->finish(); + return std::make_unique(DataConsumer, Opts, + CI.getPreprocessorPtr()); } }; @@ -118,16 +123,10 @@ protected: StringRef InFile) override { return createIndexASTConsumer(CI); } - - void EndSourceFileAction() override { - FrontendAction::EndSourceFileAction(); - finish(); - } }; -class WrappingIndexAction final : public WrapperFrontendAction, IndexActionBase { - bool IndexActionFailed = false; - +class WrappingIndexAction final : public WrapperFrontendAction, + IndexActionBase { public: WrappingIndexAction(std::unique_ptr WrappedAction, std::shared_ptr DataConsumer, @@ -140,7 +139,6 @@ protected: StringRef InFile) override { auto OtherConsumer = WrapperFrontendAction::CreateASTConsumer(CI, InFile); if (!OtherConsumer) { - IndexActionFailed = true; return nullptr; } @@ -149,13 +147,6 @@ protected: Consumers.push_back(createIndexASTConsumer(CI)); return std::make_unique(std::move(Consumers)); } - - void EndSourceFileAction() override { - // Invoke wrapped action's method. - WrapperFrontendAction::EndSourceFileAction(); - if (!IndexActionFailed) - finish(); - } }; } // anonymous namespace @@ -164,10 +155,11 @@ std::unique_ptr index::createIndexingAction(std::shared_ptr DataConsumer, IndexingOptions Opts, std::unique_ptr WrappedAction) { + assert(DataConsumer != nullptr); + if (WrappedAction) return std::make_unique(std::move(WrappedAction), - std::move(DataConsumer), - Opts); + std::move(DataConsumer), Opts); return std::make_unique(std::move(DataConsumer), Opts); }