diff --git a/clang/lib/Index/IndexingAction.cpp b/clang/lib/Index/IndexingAction.cpp --- a/clang/lib/Index/IndexingAction.cpp +++ b/clang/lib/Index/IndexingAction.cpp @@ -23,39 +23,7 @@ namespace { -class IndexASTConsumer : public ASTConsumer { - std::shared_ptr PP; - std::shared_ptr IndexCtx; - -public: - IndexASTConsumer(std::shared_ptr PP, - std::shared_ptr IndexCtx) - : PP(std::move(PP)), IndexCtx(std::move(IndexCtx)) {} - -protected: - void Initialize(ASTContext &Context) override { - IndexCtx->setASTContext(Context); - IndexCtx->getDataConsumer().initialize(Context); - IndexCtx->getDataConsumer().setPreprocessor(PP); - } - - bool HandleTopLevelDecl(DeclGroupRef DG) override { - return IndexCtx->indexDeclGroupRef(DG); - } - - void HandleInterestingDecl(DeclGroupRef DG) override { - // Ignore deserialized decls. - } - - void HandleTopLevelDeclInObjCContainer(DeclGroupRef DG) override { - IndexCtx->indexDeclGroupRef(DG); - } - - void HandleTranslationUnit(ASTContext &Ctx) override { - } -}; - -class IndexPPCallbacks : public PPCallbacks { +class IndexPPCallbacks final : public PPCallbacks { std::shared_ptr IndexCtx; public: @@ -85,32 +53,66 @@ } }; -class IndexActionBase { -protected: +class IndexASTConsumer final : public ASTConsumer { std::shared_ptr DataConsumer; std::shared_ptr IndexCtx; + std::shared_ptr PP; - IndexActionBase(std::shared_ptr dataConsumer, - IndexingOptions Opts) - : DataConsumer(std::move(dataConsumer)), - IndexCtx(new IndexingContext(Opts, *DataConsumer)) {} +public: + 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); + } - std::unique_ptr - createIndexASTConsumer(CompilerInstance &CI) { - return std::make_unique(CI.getPreprocessorPtr(), - IndexCtx); +protected: + void Initialize(ASTContext &Context) override { + IndexCtx->setASTContext(Context); + IndexCtx->getDataConsumer().initialize(Context); + IndexCtx->getDataConsumer().setPreprocessor(PP); + PP->addPPCallbacks(std::make_unique(IndexCtx)); + } + + bool HandleTopLevelDecl(DeclGroupRef DG) override { + return IndexCtx->indexDeclGroupRef(DG); } - std::unique_ptr createIndexPPCallbacks() { - return std::make_unique(IndexCtx); + void HandleInterestingDecl(DeclGroupRef DG) override { + // Ignore deserialized decls. } - void finish() { + void HandleTopLevelDeclInObjCContainer(DeclGroupRef DG) override { + IndexCtx->indexDeclGroupRef(DG); + } + + void HandleTranslationUnit(ASTContext &Ctx) override { DataConsumer->finish(); } }; -class IndexAction : public ASTFrontendAction, IndexActionBase { +class IndexActionBase { +protected: + std::shared_ptr DataConsumer; + IndexingOptions Opts; + + IndexActionBase(std::shared_ptr DataConsumer, + IndexingOptions Opts) + : DataConsumer(std::move(DataConsumer)), Opts(Opts) { + assert(this->DataConsumer != nullptr); + } + + std::unique_ptr + createIndexASTConsumer(CompilerInstance &CI) { + return std::make_unique(DataConsumer, Opts, + CI.getPreprocessorPtr()); + } +}; + +class IndexAction final : public ASTFrontendAction, IndexActionBase { public: IndexAction(std::shared_ptr DataConsumer, IndexingOptions Opts) @@ -121,21 +123,10 @@ StringRef InFile) override { return createIndexASTConsumer(CI); } - - bool BeginSourceFileAction(clang::CompilerInstance &CI) override { - CI.getPreprocessor().addPPCallbacks(createIndexPPCallbacks()); - return true; - } - - void EndSourceFileAction() override { - FrontendAction::EndSourceFileAction(); - finish(); - } }; -class WrappingIndexAction : public WrapperFrontendAction, IndexActionBase { - bool IndexActionFailed = false; - +class WrappingIndexAction final : public WrapperFrontendAction, + IndexActionBase { public: WrappingIndexAction(std::unique_ptr WrappedAction, std::shared_ptr DataConsumer, @@ -148,7 +139,6 @@ StringRef InFile) override { auto OtherConsumer = WrapperFrontendAction::CreateASTConsumer(CI, InFile); if (!OtherConsumer) { - IndexActionFailed = true; return nullptr; } @@ -157,19 +147,6 @@ Consumers.push_back(createIndexASTConsumer(CI)); return std::make_unique(std::move(Consumers)); } - - bool BeginSourceFileAction(clang::CompilerInstance &CI) override { - WrapperFrontendAction::BeginSourceFileAction(CI); - CI.getPreprocessor().addPPCallbacks(createIndexPPCallbacks()); - return true; - } - - void EndSourceFileAction() override { - // Invoke wrapped action's method. - WrapperFrontendAction::EndSourceFileAction(); - if (!IndexActionFailed) - finish(); - } }; } // anonymous namespace @@ -178,10 +155,11 @@ 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); }