Author: gribozavr Date: Thu Aug 29 03:23:29 2019 New Revision: 370323 URL: http://llvm.org/viewvc/llvm-project?rev=370323&view=rev Log: [Index] Create PP callbacks in the ASTConsumer
Doing so removes one reason to create a custom FrontendAction. FrontendActions are not desirable because they are difficult to compose. ASTConsumers are much easier to compose. Modified: cfe/trunk/lib/Index/IndexingAction.cpp Modified: cfe/trunk/lib/Index/IndexingAction.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Index/IndexingAction.cpp?rev=370323&r1=370322&r2=370323&view=diff ============================================================================== --- cfe/trunk/lib/Index/IndexingAction.cpp (original) +++ cfe/trunk/lib/Index/IndexingAction.cpp Thu Aug 29 03:23:29 2019 @@ -23,6 +23,36 @@ using namespace clang::index; namespace { +class IndexPPCallbacks final : public PPCallbacks { + std::shared_ptr<IndexingContext> IndexCtx; + +public: + IndexPPCallbacks(std::shared_ptr<IndexingContext> IndexCtx) + : IndexCtx(std::move(IndexCtx)) {} + + void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD, + SourceRange Range, const MacroArgs *Args) override { + IndexCtx->handleMacroReference(*MacroNameTok.getIdentifierInfo(), + Range.getBegin(), *MD.getMacroInfo()); + } + + void MacroDefined(const Token &MacroNameTok, + const MacroDirective *MD) override { + IndexCtx->handleMacroDefined(*MacroNameTok.getIdentifierInfo(), + MacroNameTok.getLocation(), + *MD->getMacroInfo()); + } + + void MacroUndefined(const Token &MacroNameTok, const MacroDefinition &MD, + const MacroDirective *Undef) override { + if (!MD.getMacroInfo()) // Ignore noop #undef. + return; + IndexCtx->handleMacroUndefined(*MacroNameTok.getIdentifierInfo(), + MacroNameTok.getLocation(), + *MD.getMacroInfo()); + } +}; + class IndexASTConsumer final : public ASTConsumer { std::shared_ptr<Preprocessor> PP; std::shared_ptr<IndexingContext> IndexCtx; @@ -37,6 +67,7 @@ protected: IndexCtx->setASTContext(Context); IndexCtx->getDataConsumer().initialize(Context); IndexCtx->getDataConsumer().setPreprocessor(PP); + PP->addPPCallbacks(std::make_unique<IndexPPCallbacks>(IndexCtx)); } bool HandleTopLevelDecl(DeclGroupRef DG) override { @@ -55,36 +86,6 @@ protected: } }; -class IndexPPCallbacks final : public PPCallbacks { - std::shared_ptr<IndexingContext> IndexCtx; - -public: - IndexPPCallbacks(std::shared_ptr<IndexingContext> IndexCtx) - : IndexCtx(std::move(IndexCtx)) {} - - void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD, - SourceRange Range, const MacroArgs *Args) override { - IndexCtx->handleMacroReference(*MacroNameTok.getIdentifierInfo(), - Range.getBegin(), *MD.getMacroInfo()); - } - - void MacroDefined(const Token &MacroNameTok, - const MacroDirective *MD) override { - IndexCtx->handleMacroDefined(*MacroNameTok.getIdentifierInfo(), - MacroNameTok.getLocation(), - *MD->getMacroInfo()); - } - - void MacroUndefined(const Token &MacroNameTok, const MacroDefinition &MD, - const MacroDirective *Undef) override { - if (!MD.getMacroInfo()) // Ignore noop #undef. - return; - IndexCtx->handleMacroUndefined(*MacroNameTok.getIdentifierInfo(), - MacroNameTok.getLocation(), - *MD.getMacroInfo()); - } -}; - class IndexActionBase { protected: std::shared_ptr<IndexDataConsumer> DataConsumer; @@ -101,10 +102,6 @@ protected: IndexCtx); } - std::unique_ptr<PPCallbacks> createIndexPPCallbacks() { - return std::make_unique<IndexPPCallbacks>(IndexCtx); - } - void finish() { DataConsumer->finish(); } @@ -122,11 +119,6 @@ protected: return createIndexASTConsumer(CI); } - bool BeginSourceFileAction(clang::CompilerInstance &CI) override { - CI.getPreprocessor().addPPCallbacks(createIndexPPCallbacks()); - return true; - } - void EndSourceFileAction() override { FrontendAction::EndSourceFileAction(); finish(); @@ -158,12 +150,6 @@ protected: return std::make_unique<MultiplexConsumer>(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(); _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits