From 4d22172b3a24a1991d6356762ec71d6e3097a49c Mon Sep 17 00:00:00 2001 From: Eric Liu Date: Tue, 18 Sep 2018 08:51:08 +0000 Subject: [Index] Add an option to collect macros from preprocesor. Summary: Also added unit tests for the index library; lit+c-index-test is painful... Reviewers: ilya-biryukov Reviewed By: ilya-biryukov Subscribers: mgorny, cfe-commits Differential Revision: https://reviews.llvm.org/D52098 llvm-svn: 342451 --- clang/lib/Index/IndexingAction.cpp | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'clang/lib/Index/IndexingAction.cpp') diff --git a/clang/lib/Index/IndexingAction.cpp b/clang/lib/Index/IndexingAction.cpp index 16f6c21745e..cb5c951f38a 100644 --- a/clang/lib/Index/IndexingAction.cpp +++ b/clang/lib/Index/IndexingAction.cpp @@ -215,23 +215,41 @@ static void indexTranslationUnit(ASTUnit &Unit, IndexingContext &IndexCtx) { Unit.visitLocalTopLevelDecls(&IndexCtx, topLevelDeclVisitor); } +static void indexPreprocessorMacros(const Preprocessor &PP, + IndexDataConsumer &DataConsumer) { + for (const auto &M : PP.macros()) + if (MacroDirective *MD = M.second.getLatest()) + DataConsumer.handleMacroOccurence( + M.first, MD->getMacroInfo(), + static_cast(index::SymbolRole::Definition), + MD->getLocation()); +} + void index::indexASTUnit(ASTUnit &Unit, IndexDataConsumer &DataConsumer, IndexingOptions Opts) { IndexingContext IndexCtx(Opts, DataConsumer); IndexCtx.setASTContext(Unit.getASTContext()); DataConsumer.initialize(Unit.getASTContext()); DataConsumer.setPreprocessor(Unit.getPreprocessorPtr()); + + if (Opts.IndexMacrosInPreprocessor) + indexPreprocessorMacros(Unit.getPreprocessor(), DataConsumer); indexTranslationUnit(Unit, IndexCtx); DataConsumer.finish(); } -void index::indexTopLevelDecls(ASTContext &Ctx, ArrayRef Decls, +void index::indexTopLevelDecls(ASTContext &Ctx, Preprocessor &PP, + ArrayRef Decls, IndexDataConsumer &DataConsumer, IndexingOptions Opts) { IndexingContext IndexCtx(Opts, DataConsumer); IndexCtx.setASTContext(Ctx); DataConsumer.initialize(Ctx); + + if (Opts.IndexMacrosInPreprocessor) + indexPreprocessorMacros(PP, DataConsumer); + for (const Decl *D : Decls) IndexCtx.indexTopLevelDecl(D); DataConsumer.finish(); @@ -251,6 +269,9 @@ void index::indexModuleFile(serialization::ModuleFile &Mod, ASTReader &Reader, IndexCtx.setASTContext(Ctx); DataConsumer.initialize(Ctx); + if (Opts.IndexMacrosInPreprocessor) + indexPreprocessorMacros(Reader.getPreprocessor(), DataConsumer); + for (const Decl *D : Reader.getModuleFileLevelDecls(Mod)) { IndexCtx.indexTopLevelDecl(D); } -- cgit v1.2.3