From ca7487190698eb46511a983fb2f469db2bdb8836 Mon Sep 17 00:00:00 2001 From: Dmitri Gribenko Date: Thu, 29 Aug 2019 11:43:05 +0000 Subject: [PATCH] [Index] Stopped wrapping FrontendActions in libIndex and its users Exposed a new function, createIndexingASTConsumer, that creates an ASTConsumer. ASTConsumers compose well. Removed wrapping functionality from createIndexingAction. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@370337 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Index/IndexingAction.h | 11 +++-- lib/Index/IndexingAction.cpp | 62 +++++++--------------------- tools/c-index-test/core_main.cpp | 5 +-- tools/libclang/Indexing.cpp | 23 ++++++----- 4 files changed, 37 insertions(+), 64 deletions(-) diff --git a/include/clang/Index/IndexingAction.h b/include/clang/Index/IndexingAction.h index 9756f3c539..ce2ff0e105 100644 --- a/include/clang/Index/IndexingAction.h +++ b/include/clang/Index/IndexingAction.h @@ -17,6 +17,7 @@ namespace clang { class ASTContext; + class ASTConsumer; class ASTReader; class ASTUnit; class Decl; @@ -49,12 +50,16 @@ struct IndexingOptions { bool IndexTemplateParameters = false; }; +/// Creates an ASTConsumer that indexes all symbols (macros and AST decls). +std::unique_ptr +createIndexingASTConsumer(std::shared_ptr DataConsumer, + const IndexingOptions &Opts, + std::shared_ptr PP); + /// Creates a frontend action that indexes all symbols (macros and AST decls). -/// \param WrappedAction another frontend action to wrap over or null. std::unique_ptr createIndexingAction(std::shared_ptr DataConsumer, - IndexingOptions Opts, - std::unique_ptr WrappedAction); + const IndexingOptions &Opts); /// Recursively indexes all decls in the AST. void indexASTUnit(ASTUnit &Unit, IndexDataConsumer &DataConsumer, diff --git a/lib/Index/IndexingAction.cpp b/lib/Index/IndexingAction.cpp index 8605a2cfa1..e707972014 100644 --- a/lib/Index/IndexingAction.cpp +++ b/lib/Index/IndexingAction.cpp @@ -94,72 +94,38 @@ protected: } }; -class IndexActionBase { -protected: +class IndexAction final : public ASTFrontendAction { 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) - : IndexActionBase(std::move(DataConsumer), Opts) {} - -protected: - std::unique_ptr CreateASTConsumer(CompilerInstance &CI, - StringRef InFile) override { - return createIndexASTConsumer(CI); + const IndexingOptions &Opts) + : DataConsumer(std::move(DataConsumer)), Opts(Opts) { + assert(this->DataConsumer != nullptr); } -}; - -class WrappingIndexAction final : public WrapperFrontendAction, - IndexActionBase { -public: - WrappingIndexAction(std::unique_ptr WrappedAction, - std::shared_ptr DataConsumer, - IndexingOptions Opts) - : WrapperFrontendAction(std::move(WrappedAction)), - IndexActionBase(std::move(DataConsumer), Opts) {} protected: std::unique_ptr CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override { - auto OtherConsumer = WrapperFrontendAction::CreateASTConsumer(CI, InFile); - if (!OtherConsumer) { - return nullptr; - } - - std::vector> Consumers; - Consumers.push_back(std::move(OtherConsumer)); - Consumers.push_back(createIndexASTConsumer(CI)); - return std::make_unique(std::move(Consumers)); + return std::make_unique(DataConsumer, Opts, + CI.getPreprocessorPtr()); } }; } // anonymous namespace +std::unique_ptr +index::createIndexingASTConsumer(std::shared_ptr DataConsumer, + const IndexingOptions &Opts, + std::shared_ptr PP) { + return std::make_unique(DataConsumer, Opts, PP); +} + std::unique_ptr index::createIndexingAction(std::shared_ptr DataConsumer, - IndexingOptions Opts, - std::unique_ptr WrappedAction) { + const IndexingOptions &Opts) { assert(DataConsumer != nullptr); - - if (WrappedAction) - return std::make_unique(std::move(WrappedAction), - std::move(DataConsumer), Opts); return std::make_unique(std::move(DataConsumer), Opts); } diff --git a/tools/c-index-test/core_main.cpp b/tools/c-index-test/core_main.cpp index 2738e125cb..f1edce0092 100644 --- a/tools/c-index-test/core_main.cpp +++ b/tools/c-index-test/core_main.cpp @@ -221,9 +221,8 @@ static bool printSourceSymbols(const char *Executable, auto DataConsumer = std::make_shared(OS); IndexingOptions IndexOpts; IndexOpts.IndexFunctionLocals = indexLocals; - std::unique_ptr IndexAction; - IndexAction = createIndexingAction(DataConsumer, IndexOpts, - /*WrappedAction=*/nullptr); + std::unique_ptr IndexAction = + createIndexingAction(DataConsumer, IndexOpts); auto PCHContainerOps = std::make_shared(); std::unique_ptr Unit(ASTUnit::LoadFromCompilerInvocationAction( diff --git a/tools/libclang/Indexing.cpp b/tools/libclang/Indexing.cpp index ce1e6b3828..372fb214a5 100644 --- a/tools/libclang/Indexing.cpp +++ b/tools/libclang/Indexing.cpp @@ -19,6 +19,7 @@ #include "clang/Frontend/CompilerInstance.h" #include "clang/Frontend/CompilerInvocation.h" #include "clang/Frontend/FrontendAction.h" +#include "clang/Frontend/MultiplexConsumer.h" #include "clang/Frontend/Utils.h" #include "clang/Index/IndexingAction.h" #include "clang/Lex/HeaderSearch.h" @@ -367,14 +368,16 @@ public: class IndexingFrontendAction : public ASTFrontendAction { std::shared_ptr DataConsumer; + IndexingOptions Opts; SharedParsedRegionsStorage *SKData; std::unique_ptr ParsedLocsTracker; public: IndexingFrontendAction(std::shared_ptr dataConsumer, + const IndexingOptions &Opts, SharedParsedRegionsStorage *skData) - : DataConsumer(std::move(dataConsumer)), SKData(skData) {} + : DataConsumer(std::move(dataConsumer)), Opts(Opts), SKData(skData) {} std::unique_ptr CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override { @@ -398,8 +401,12 @@ public: std::make_unique(*SKData, *PPRec, PP); } - return std::make_unique(*DataConsumer, - ParsedLocsTracker.get()); + std::vector> Consumers; + Consumers.push_back(std::make_unique( + *DataConsumer, ParsedLocsTracker.get())); + Consumers.push_back( + createIndexingASTConsumer(DataConsumer, Opts, CI.getPreprocessorPtr())); + return std::make_unique(std::move(Consumers)); } TranslationUnitKind getTranslationUnitKind() override { @@ -569,12 +576,9 @@ static CXErrorCode clang_indexSourceFile_Impl( auto DataConsumer = std::make_shared(client_data, CB, index_options, CXTU->getTU()); - auto InterAction = std::make_unique(DataConsumer, - SkipBodies ? IdxSession->SkipBodyData.get() : nullptr); - std::unique_ptr IndexAction; - IndexAction = createIndexingAction(DataConsumer, - getIndexingOptionsFromCXOptions(index_options), - std::move(InterAction)); + auto IndexAction = std::make_unique( + DataConsumer, getIndexingOptionsFromCXOptions(index_options), + SkipBodies ? IdxSession->SkipBodyData.get() : nullptr); // Recover resources if we crash before exiting this method. llvm::CrashRecoveryContextCleanupRegistrar @@ -995,4 +999,3 @@ CXSourceLocation clang_indexLoc_getCXSourceLocation(CXIdxLoc location) { *static_cast(location.ptr_data[0]); return cxloc::translateSourceLocation(DataConsumer.getASTContext(), Loc); } - -- 2.40.0