diff options
author | Eric Liu <ioeric@google.com> | 2018-07-09 08:44:05 +0000 |
---|---|---|
committer | Eric Liu <ioeric@google.com> | 2018-07-09 08:44:05 +0000 |
commit | 8c97195cc891335316de345689c6b25b9d1eb2fb (patch) | |
tree | d6239356cc0b1cecb4720f345172ef9e0f5f9652 /clang/lib/Index/IndexingAction.cpp | |
parent | a0010472cac3f8e3d39ec081c421c866e5781738 (diff) | |
download | bcm5719-llvm-8c97195cc891335316de345689c6b25b9d1eb2fb.tar.gz bcm5719-llvm-8c97195cc891335316de345689c6b25b9d1eb2fb.zip |
[Index] Add indexing support for MACROs.
Reviewers: akyrtzi, arphaman, sammccall
Reviewed By: sammccall
Subscribers: malaperle, sammccall, cfe-commits
Differential Revision: https://reviews.llvm.org/D48961
llvm-svn: 336524
Diffstat (limited to 'clang/lib/Index/IndexingAction.cpp')
-rw-r--r-- | clang/lib/Index/IndexingAction.cpp | 116 |
1 files changed, 82 insertions, 34 deletions
diff --git a/clang/lib/Index/IndexingAction.cpp b/clang/lib/Index/IndexingAction.cpp index e378a092bea..b8faf7075e7 100644 --- a/clang/lib/Index/IndexingAction.cpp +++ b/clang/lib/Index/IndexingAction.cpp @@ -13,8 +13,11 @@ #include "clang/Frontend/FrontendAction.h" #include "clang/Frontend/MultiplexConsumer.h" #include "clang/Index/IndexDataConsumer.h" +#include "clang/Lex/PPCallbacks.h" #include "clang/Lex/Preprocessor.h" #include "clang/Serialization/ASTReader.h" +#include "llvm/ADT/STLExtras.h" +#include <memory> using namespace clang; using namespace clang::index; @@ -43,21 +46,22 @@ namespace { class IndexASTConsumer : public ASTConsumer { std::shared_ptr<Preprocessor> PP; - IndexingContext &IndexCtx; + std::shared_ptr<IndexingContext> IndexCtx; public: - IndexASTConsumer(std::shared_ptr<Preprocessor> PP, IndexingContext &IndexCtx) - : PP(std::move(PP)), IndexCtx(IndexCtx) {} + IndexASTConsumer(std::shared_ptr<Preprocessor> PP, + std::shared_ptr<IndexingContext> 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); + IndexCtx->setASTContext(Context); + IndexCtx->getDataConsumer().initialize(Context); + IndexCtx->getDataConsumer().setPreprocessor(PP); } bool HandleTopLevelDecl(DeclGroupRef DG) override { - return IndexCtx.indexDeclGroupRef(DG); + return IndexCtx->indexDeclGroupRef(DG); } void HandleInterestingDecl(DeclGroupRef DG) override { @@ -65,22 +69,50 @@ protected: } void HandleTopLevelDeclInObjCContainer(DeclGroupRef DG) override { - IndexCtx.indexDeclGroupRef(DG); + IndexCtx->indexDeclGroupRef(DG); } void HandleTranslationUnit(ASTContext &Ctx) override { } }; +class IndexPPCallbacks : 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 { + IndexCtx->handleMacroUndefined(*MacroNameTok.getIdentifierInfo(), + MacroNameTok.getLocation(), + *MD.getMacroInfo()); + } +}; + class IndexActionBase { protected: std::shared_ptr<IndexDataConsumer> DataConsumer; - IndexingContext IndexCtx; + std::shared_ptr<IndexingContext> IndexCtx; IndexActionBase(std::shared_ptr<IndexDataConsumer> dataConsumer, IndexingOptions Opts) - : DataConsumer(std::move(dataConsumer)), - IndexCtx(Opts, *DataConsumer) {} + : DataConsumer(std::move(dataConsumer)), + IndexCtx(new IndexingContext(Opts, *DataConsumer)) {} std::unique_ptr<IndexASTConsumer> createIndexASTConsumer(CompilerInstance &CI) { @@ -88,6 +120,10 @@ protected: IndexCtx); } + std::unique_ptr<PPCallbacks> createIndexPPCallbacks() { + return llvm::make_unique<IndexPPCallbacks>(IndexCtx); + } + void finish() { DataConsumer->finish(); } @@ -105,6 +141,11 @@ protected: return createIndexASTConsumer(CI); } + bool BeginSourceFileAction(clang::CompilerInstance &CI) override { + CI.getPreprocessor().addPPCallbacks(createIndexPPCallbacks()); + return true; + } + void EndSourceFileAction() override { FrontendAction::EndSourceFileAction(); finish(); @@ -123,32 +164,34 @@ public: protected: std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI, - StringRef InFile) override; - void EndSourceFileAction() override; -}; - -} // anonymous namespace + StringRef InFile) override { + auto OtherConsumer = WrapperFrontendAction::CreateASTConsumer(CI, InFile); + if (!OtherConsumer) { + IndexActionFailed = true; + return nullptr; + } + + std::vector<std::unique_ptr<ASTConsumer>> Consumers; + Consumers.push_back(std::move(OtherConsumer)); + Consumers.push_back(createIndexASTConsumer(CI)); + return llvm::make_unique<MultiplexConsumer>(std::move(Consumers)); + } -void WrappingIndexAction::EndSourceFileAction() { - // Invoke wrapped action's method. - WrapperFrontendAction::EndSourceFileAction(); - if (!IndexActionFailed) - finish(); -} + bool BeginSourceFileAction(clang::CompilerInstance &CI) override { + WrapperFrontendAction::BeginSourceFileAction(CI); + CI.getPreprocessor().addPPCallbacks(createIndexPPCallbacks()); + return true; + } -std::unique_ptr<ASTConsumer> -WrappingIndexAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { - auto OtherConsumer = WrapperFrontendAction::CreateASTConsumer(CI, InFile); - if (!OtherConsumer) { - IndexActionFailed = true; - return nullptr; + void EndSourceFileAction() override { + // Invoke wrapped action's method. + WrapperFrontendAction::EndSourceFileAction(); + if (!IndexActionFailed) + finish(); } +}; - std::vector<std::unique_ptr<ASTConsumer>> Consumers; - Consumers.push_back(std::move(OtherConsumer)); - Consumers.push_back(createIndexASTConsumer(CI)); - return llvm::make_unique<MultiplexConsumer>(std::move(Consumers)); -} +} // anonymous namespace std::unique_ptr<FrontendAction> index::createIndexingAction(std::shared_ptr<IndexDataConsumer> DataConsumer, @@ -161,7 +204,6 @@ index::createIndexingAction(std::shared_ptr<IndexDataConsumer> DataConsumer, return llvm::make_unique<IndexAction>(std::move(DataConsumer), Opts); } - static bool topLevelDeclVisitor(void *context, const Decl *D) { IndexingContext &IndexCtx = *static_cast<IndexingContext*>(context); return IndexCtx.indexTopLevelDecl(D); @@ -193,6 +235,12 @@ void index::indexTopLevelDecls(ASTContext &Ctx, ArrayRef<const Decl *> Decls, DataConsumer.finish(); } +std::unique_ptr<PPCallbacks> +index::indexMacrosCallback(IndexDataConsumer &Consumer, IndexingOptions Opts) { + return llvm::make_unique<IndexPPCallbacks>( + std::make_shared<IndexingContext>(Opts, Consumer)); +} + void index::indexModuleFile(serialization::ModuleFile &Mod, ASTReader &Reader, IndexDataConsumer &DataConsumer, IndexingOptions Opts) { |