diff --git a/clang-tools-extra/clangd/index/IndexAction.cpp b/clang-tools-extra/clangd/index/IndexAction.cpp --- a/clang-tools-extra/clangd/index/IndexAction.cpp +++ b/clang-tools-extra/clangd/index/IndexAction.cpp @@ -121,42 +121,32 @@ IncludeGraph &IG; }; -/// Returns an ASTConsumer that wraps \p Inner and additionally instructs the -/// parser to skip bodies of functions in the files that should not be -/// processed. -static std::unique_ptr -skipProcessedFunctions(std::unique_ptr Inner, - std::function ShouldIndexFile) { - class SkipProcessedFunctions : public ASTConsumer { - public: - SkipProcessedFunctions(std::function FileFilter) - : ShouldIndexFile(std::move(FileFilter)), Context(nullptr) { - assert(this->ShouldIndexFile); - } +/// An ASTConsumer that instructs the parser to skip bodies of functions in the +/// files that should not be processed. +class SkipProcessedFunctions : public ASTConsumer { +public: + SkipProcessedFunctions(std::function FileFilter) + : ShouldIndexFile(std::move(FileFilter)), Context(nullptr) { + assert(this->ShouldIndexFile); + } - void Initialize(ASTContext &Context) override { this->Context = &Context; } - bool shouldSkipFunctionBody(Decl *D) override { - assert(Context && "Initialize() was never called."); - auto &SM = Context->getSourceManager(); - auto FID = SM.getFileID(SM.getExpansionLoc(D->getLocation())); - if (!FID.isValid()) - return false; - return !ShouldIndexFile(FID); - } + void Initialize(ASTContext &Context) override { this->Context = &Context; } + bool shouldSkipFunctionBody(Decl *D) override { + assert(Context && "Initialize() was never called."); + auto &SM = Context->getSourceManager(); + auto FID = SM.getFileID(SM.getExpansionLoc(D->getLocation())); + if (!FID.isValid()) + return false; + return !ShouldIndexFile(FID); + } - private: - std::function ShouldIndexFile; - const ASTContext *Context; - }; - std::vector> Consumers; - Consumers.push_back( - std::make_unique(ShouldIndexFile)); - Consumers.push_back(std::move(Inner)); - return std::make_unique(std::move(Consumers)); -} +private: + std::function ShouldIndexFile; + const ASTContext *Context; +}; // Wraps the index action and reports index data after each translation unit. -class IndexAction : public WrapperFrontendAction { +class IndexAction : public ASTFrontendAction { public: IndexAction(std::shared_ptr C, std::unique_ptr Includes, @@ -165,11 +155,10 @@ std::function RefsCallback, std::function RelationsCallback, std::function IncludeGraphCallback) - : WrapperFrontendAction(index::createIndexingAction(C, Opts, nullptr)), - SymbolsCallback(SymbolsCallback), RefsCallback(RefsCallback), - RelationsCallback(RelationsCallback), + : SymbolsCallback(SymbolsCallback), + RefsCallback(RefsCallback), RelationsCallback(RelationsCallback), IncludeGraphCallback(IncludeGraphCallback), Collector(C), - Includes(std::move(Includes)), + Includes(std::move(Includes)), Opts(Opts), PragmaHandler(collectIWYUHeaderMaps(this->Includes.get())) {} std::unique_ptr @@ -179,9 +168,13 @@ if (IncludeGraphCallback != nullptr) CI.getPreprocessor().addPPCallbacks( std::make_unique(CI.getSourceManager(), IG)); - return skipProcessedFunctions( - WrapperFrontendAction::CreateASTConsumer(CI, InFile), - [this](FileID FID) { return Collector->shouldIndexFile(FID); }); + + std::vector> Consumers; + Consumers.push_back(std::make_unique( + [this](FileID FID) { return Collector->shouldIndexFile(FID); })); + Consumers.push_back(index::createIndexingASTConsumer( + Collector, Opts, CI.getPreprocessorPtr())); + return std::make_unique(std::move(Consumers)); } bool BeginInvocation(CompilerInstance &CI) override { @@ -195,13 +188,10 @@ // bodies. The ASTConsumer will take care of skipping only functions inside // the files that we have already processed. CI.getFrontendOpts().SkipFunctionBodies = true; - - return WrapperFrontendAction::BeginInvocation(CI); + return true; } void EndSourceFileAction() override { - WrapperFrontendAction::EndSourceFileAction(); - SymbolsCallback(Collector->takeSymbols()); if (RefsCallback != nullptr) RefsCallback(Collector->takeRefs()); @@ -224,6 +214,7 @@ std::function IncludeGraphCallback; std::shared_ptr Collector; std::unique_ptr Includes; + index::IndexingOptions Opts; std::unique_ptr PragmaHandler; IncludeGraph IG; }; diff --git a/clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp b/clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp --- a/clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp +++ b/clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp @@ -201,30 +201,30 @@ : COpts(std::move(COpts)), PragmaHandler(PragmaHandler) {} clang::FrontendAction *create() override { - class WrappedIndexAction : public WrapperFrontendAction { + class IndexAction : public ASTFrontendAction { public: - WrappedIndexAction(std::shared_ptr C, - const index::IndexingOptions &Opts, - CommentHandler *PragmaHandler) - : WrapperFrontendAction( - index::createIndexingAction(C, Opts, nullptr)), + IndexAction(std::shared_ptr DataConsumer, + const index::IndexingOptions &Opts, + CommentHandler *PragmaHandler) + : DataConsumer(std::move(DataConsumer)), Opts(Opts), PragmaHandler(PragmaHandler) {} std::unique_ptr CreateASTConsumer(CompilerInstance &CI, llvm::StringRef InFile) override { if (PragmaHandler) CI.getPreprocessor().addCommentHandler(PragmaHandler); - return WrapperFrontendAction::CreateASTConsumer(CI, InFile); + return createIndexingASTConsumer(DataConsumer, Opts, CI.getPreprocessorPtr()); } bool BeginInvocation(CompilerInstance &CI) override { // Make the compiler parse all comments. CI.getLangOpts().CommentOpts.ParseAllComments = true; - return WrapperFrontendAction::BeginInvocation(CI); + return true; } private: - index::IndexingOptions IndexOpts; + std::shared_ptr DataConsumer; + index::IndexingOptions Opts; CommentHandler *PragmaHandler; }; index::IndexingOptions IndexOpts; @@ -232,8 +232,7 @@ index::IndexingOptions::SystemSymbolFilterKind::All; IndexOpts.IndexFunctionLocals = false; Collector = std::make_shared(COpts); - return new WrappedIndexAction(Collector, std::move(IndexOpts), - PragmaHandler); + return new IndexAction(Collector, std::move(IndexOpts), PragmaHandler); } std::shared_ptr Collector; diff --git a/clang/include/clang/Index/IndexingAction.h b/clang/include/clang/Index/IndexingAction.h --- a/clang/include/clang/Index/IndexingAction.h +++ b/clang/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 @@ 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/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,103 +53,79 @@ } }; -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)) {} - - std::unique_ptr - createIndexASTConsumer(CompilerInstance &CI) { - return std::make_unique(CI.getPreprocessorPtr(), - IndexCtx); +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 createIndexPPCallbacks() { - return std::make_unique(IndexCtx); +protected: + void Initialize(ASTContext &Context) override { + IndexCtx->setASTContext(Context); + IndexCtx->getDataConsumer().initialize(Context); + IndexCtx->getDataConsumer().setPreprocessor(PP); + PP->addPPCallbacks(std::make_unique(IndexCtx)); } - void finish() { - DataConsumer->finish(); + bool HandleTopLevelDecl(DeclGroupRef DG) override { + return IndexCtx->indexDeclGroupRef(DG); } -}; - -class IndexAction : 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); + void HandleInterestingDecl(DeclGroupRef DG) override { + // Ignore deserialized decls. } - bool BeginSourceFileAction(clang::CompilerInstance &CI) override { - CI.getPreprocessor().addPPCallbacks(createIndexPPCallbacks()); - return true; + void HandleTopLevelDeclInObjCContainer(DeclGroupRef DG) override { + IndexCtx->indexDeclGroupRef(DG); } - void EndSourceFileAction() override { - FrontendAction::EndSourceFileAction(); - finish(); + void HandleTranslationUnit(ASTContext &Ctx) override { + DataConsumer->finish(); } }; -class WrappingIndexAction : public WrapperFrontendAction, IndexActionBase { - bool IndexActionFailed = false; +class IndexAction final : public ASTFrontendAction { + std::shared_ptr DataConsumer; + IndexingOptions Opts; public: - WrappingIndexAction(std::unique_ptr WrappedAction, - std::shared_ptr DataConsumer, - IndexingOptions Opts) - : WrapperFrontendAction(std::move(WrappedAction)), - IndexActionBase(std::move(DataConsumer), Opts) {} + IndexAction(std::shared_ptr DataConsumer, + const IndexingOptions &Opts) + : DataConsumer(std::move(DataConsumer)), Opts(Opts) { + assert(this->DataConsumer != nullptr); + } protected: std::unique_ptr CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override { - auto OtherConsumer = WrapperFrontendAction::CreateASTConsumer(CI, InFile); - if (!OtherConsumer) { - IndexActionFailed = true; - return nullptr; - } - - std::vector> Consumers; - Consumers.push_back(std::move(OtherConsumer)); - 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(); + 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) { - if (WrappedAction) - return std::make_unique(std::move(WrappedAction), - std::move(DataConsumer), - Opts); + const IndexingOptions &Opts) { + assert(DataConsumer != nullptr); return std::make_unique(std::move(DataConsumer), Opts); } diff --git a/clang/tools/c-index-test/core_main.cpp b/clang/tools/c-index-test/core_main.cpp --- a/clang/tools/c-index-test/core_main.cpp +++ b/clang/tools/c-index-test/core_main.cpp @@ -221,9 +221,8 @@ 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/clang/tools/libclang/Indexing.cpp b/clang/tools/libclang/Indexing.cpp --- a/clang/tools/libclang/Indexing.cpp +++ b/clang/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 @@ 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 @@ 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 @@ 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 @@ *static_cast(location.ptr_data[0]); return cxloc::translateSourceLocation(DataConsumer.getASTContext(), Loc); } -