Author: akirtzidis Date: Sun Feb 14 00:39:03 2016 New Revision: 260841 URL: http://llvm.org/viewvc/llvm-project?rev=260841&view=rev Log: [index] Allow calling createIndexingAction() without passing another action to wrap over.
Modified: cfe/trunk/include/clang/Index/IndexingAction.h cfe/trunk/lib/Index/IndexingAction.cpp cfe/trunk/tools/libclang/Indexing.cpp Modified: cfe/trunk/include/clang/Index/IndexingAction.h URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Index/IndexingAction.h?rev=260841&r1=260840&r2=260841&view=diff ============================================================================== --- cfe/trunk/include/clang/Index/IndexingAction.h (original) +++ cfe/trunk/include/clang/Index/IndexingAction.h Sun Feb 14 00:39:03 2016 @@ -32,10 +32,11 @@ struct IndexingOptions { bool IndexFunctionLocals = false; }; +/// \param WrappedAction another frontend action to wrap over or null. std::unique_ptr<FrontendAction> -createIndexingAction(std::unique_ptr<FrontendAction> WrappedAction, - std::shared_ptr<IndexDataConsumer> DataConsumer, - IndexingOptions Opts); +createIndexingAction(std::shared_ptr<IndexDataConsumer> DataConsumer, + IndexingOptions Opts, + std::unique_ptr<FrontendAction> WrappedAction = nullptr); void indexASTUnit(ASTUnit &Unit, std::shared_ptr<IndexDataConsumer> DataConsumer, Modified: cfe/trunk/lib/Index/IndexingAction.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Index/IndexingAction.cpp?rev=260841&r1=260840&r2=260841&view=diff ============================================================================== --- cfe/trunk/lib/Index/IndexingAction.cpp (original) +++ cfe/trunk/lib/Index/IndexingAction.cpp Sun Feb 14 00:39:03 2016 @@ -68,18 +68,52 @@ protected: } }; -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, @@ -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<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); } Modified: cfe/trunk/tools/libclang/Indexing.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/Indexing.cpp?rev=260841&r1=260840&r2=260841&view=diff ============================================================================== --- cfe/trunk/tools/libclang/Indexing.cpp (original) +++ cfe/trunk/tools/libclang/Indexing.cpp Sun Feb 14 00:39:03 2016 @@ -542,8 +542,9 @@ static CXErrorCode clang_indexSourceFile auto InterAction = llvm::make_unique<IndexingFrontendAction>(DataConsumer, SkipBodies ? IdxSession->SkipBodyData.get() : nullptr); std::unique_ptr<FrontendAction> 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<FrontendAction> _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits