From 43eeb3bb6b36465b7e9aa0942242494103cde580 Mon Sep 17 00:00:00 2001 From: Argyrios Kyrtzidis Date: Sun, 14 Feb 2016 06:39:03 +0000 Subject: [PATCH] [index] Allow calling createIndexingAction() without passing another action to wrap over. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@260841 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Index/IndexingAction.h | 7 +-- lib/Index/IndexingAction.cpp | 78 ++++++++++++++++++++-------- tools/libclang/Indexing.cpp | 5 +- 3 files changed, 63 insertions(+), 27 deletions(-) diff --git a/include/clang/Index/IndexingAction.h b/include/clang/Index/IndexingAction.h index dfc363a049..3bb427e36d 100644 --- a/include/clang/Index/IndexingAction.h +++ b/include/clang/Index/IndexingAction.h @@ -32,10 +32,11 @@ struct IndexingOptions { bool IndexFunctionLocals = false; }; +/// \param WrappedAction another frontend action to wrap over or null. std::unique_ptr -createIndexingAction(std::unique_ptr WrappedAction, - std::shared_ptr DataConsumer, - IndexingOptions Opts); +createIndexingAction(std::shared_ptr DataConsumer, + IndexingOptions Opts, + std::unique_ptr WrappedAction = nullptr); void indexASTUnit(ASTUnit &Unit, std::shared_ptr DataConsumer, diff --git a/lib/Index/IndexingAction.cpp b/lib/Index/IndexingAction.cpp index 3f7ef43e7d..46c96a35b6 100644 --- a/lib/Index/IndexingAction.cpp +++ b/lib/Index/IndexingAction.cpp @@ -68,18 +68,52 @@ protected: } }; -class IndexAction : public WrapperFrontendAction { - IndexingOptions IndexOpts; +class IndexActionBase { +protected: std::shared_ptr DataConsumer; - std::unique_ptr IndexCtx; + IndexingContext IndexCtx; + + IndexActionBase(std::shared_ptr dataConsumer, + IndexingOptions Opts) + : DataConsumer(std::move(dataConsumer)), + IndexCtx(Opts, *DataConsumer) {} + + std::unique_ptr createIndexASTConsumer() { + return llvm::make_unique(IndexCtx); + } + void finish() { + DataConsumer->finish(); + } +}; + +class IndexAction : public ASTFrontendAction, IndexActionBase { public: - IndexAction(std::unique_ptr WrappedAction, - std::shared_ptr DataConsumer, + IndexAction(std::shared_ptr DataConsumer, IndexingOptions Opts) + : IndexActionBase(std::move(DataConsumer), Opts) {} + +protected: + std::unique_ptr 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 WrappedAction, + std::shared_ptr DataConsumer, + IndexingOptions Opts) : WrapperFrontendAction(std::move(WrappedAction)), - IndexOpts(Opts), - DataConsumer(std::move(DataConsumer)) {} + IndexActionBase(std::move(DataConsumer), Opts) {} protected: std::unique_ptr CreateASTConsumer(CompilerInstance &CI, @@ -89,36 +123,36 @@ protected: } // 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 -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> Consumers; Consumers.push_back(std::move(OtherConsumer)); - Consumers.push_back(llvm::make_unique(*IndexCtx)); + Consumers.push_back(createIndexASTConsumer()); return llvm::make_unique(std::move(Consumers)); } std::unique_ptr -index::createIndexingAction(std::unique_ptr WrappedAction, - std::shared_ptr DataConsumer, - IndexingOptions Opts) { - return llvm::make_unique(std::move(WrappedAction), - std::move(DataConsumer), - Opts); +index::createIndexingAction(std::shared_ptr DataConsumer, + IndexingOptions Opts, + std::unique_ptr WrappedAction) { + if (WrappedAction) + return llvm::make_unique(std::move(WrappedAction), + std::move(DataConsumer), + Opts); + return llvm::make_unique(std::move(DataConsumer), Opts); } diff --git a/tools/libclang/Indexing.cpp b/tools/libclang/Indexing.cpp index d901b5315d..64f4b147d9 100644 --- a/tools/libclang/Indexing.cpp +++ b/tools/libclang/Indexing.cpp @@ -542,8 +542,9 @@ static CXErrorCode clang_indexSourceFile_Impl( auto InterAction = llvm::make_unique(DataConsumer, SkipBodies ? IdxSession->SkipBodyData.get() : nullptr); std::unique_ptr IndexAction; - IndexAction = createIndexingAction(std::move(InterAction), DataConsumer, - getIndexingOptionsFromCXOptions(index_options)); + IndexAction = createIndexingAction(DataConsumer, + getIndexingOptionsFromCXOptions(index_options), + std::move(InterAction)); // Recover resources if we crash before exiting this method. llvm::CrashRecoveryContextCleanupRegistrar -- 2.40.0