Index: include/clang/Index/IndexDataConsumer.h =================================================================== --- include/clang/Index/IndexDataConsumer.h +++ include/clang/Index/IndexDataConsumer.h @@ -11,6 +11,7 @@ #define LLVM_CLANG_INDEX_INDEXDATACONSUMER_H #include "clang/Index/IndexSymbol.h" +#include "clang/Lex/Preprocessor.h" namespace clang { class ASTContext; @@ -36,6 +37,8 @@ virtual void initialize(ASTContext &Ctx) {} + virtual void setPreprocessor(std::shared_ptr PP) {} + /// \returns true to continue indexing, or false to abort. virtual bool handleDeclOccurence(const Decl *D, SymbolRoleSet Roles, ArrayRef Relations, Index: lib/Index/IndexingAction.cpp =================================================================== --- lib/Index/IndexingAction.cpp +++ lib/Index/IndexingAction.cpp @@ -8,10 +8,11 @@ //===----------------------------------------------------------------------===// #include "clang/Index/IndexingAction.h" -#include "clang/Index/IndexDataConsumer.h" #include "IndexingContext.h" +#include "clang/Frontend/CompilerInstance.h" #include "clang/Frontend/FrontendAction.h" #include "clang/Frontend/MultiplexConsumer.h" +#include "clang/Index/IndexDataConsumer.h" #include "clang/Lex/Preprocessor.h" #include "clang/Serialization/ASTReader.h" @@ -42,16 +43,18 @@ namespace { class IndexASTConsumer : public ASTConsumer { + std::shared_ptr PP; IndexingContext &IndexCtx; public: - IndexASTConsumer(IndexingContext &IndexCtx) - : IndexCtx(IndexCtx) {} + IndexASTConsumer(std::shared_ptr PP, IndexingContext &IndexCtx) + : PP(std::move(PP)), IndexCtx(IndexCtx) {} protected: void Initialize(ASTContext &Context) override { IndexCtx.setASTContext(Context); IndexCtx.getDataConsumer().initialize(Context); + IndexCtx.getDataConsumer().setPreprocessor(PP); } bool HandleTopLevelDecl(DeclGroupRef DG) override { @@ -80,8 +83,10 @@ : DataConsumer(std::move(dataConsumer)), IndexCtx(Opts, *DataConsumer) {} - std::unique_ptr createIndexASTConsumer() { - return llvm::make_unique(IndexCtx); + std::unique_ptr + createIndexASTConsumer(CompilerInstance &CI) { + return llvm::make_unique(CI.getPreprocessorPtr(), + IndexCtx); } void finish() { @@ -98,7 +103,7 @@ protected: std::unique_ptr CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override { - return createIndexASTConsumer(); + return createIndexASTConsumer(CI); } void EndSourceFileAction() override { @@ -142,7 +147,7 @@ std::vector> Consumers; Consumers.push_back(std::move(OtherConsumer)); - Consumers.push_back(createIndexASTConsumer()); + Consumers.push_back(createIndexASTConsumer(CI)); return llvm::make_unique(std::move(Consumers)); } @@ -173,6 +178,7 @@ IndexingContext IndexCtx(Opts, *DataConsumer); IndexCtx.setASTContext(Unit.getASTContext()); DataConsumer->initialize(Unit.getASTContext()); + DataConsumer->setPreprocessor(Unit.getPreprocessorPtr()); indexTranslationUnit(Unit, IndexCtx); DataConsumer->finish(); } @@ -198,7 +204,7 @@ IndexCtx.setASTContext(Ctx); DataConsumer->initialize(Ctx); - for (const Decl *D :Reader.getModuleFileLevelDecls(Mod)) { + for (const Decl *D : Reader.getModuleFileLevelDecls(Mod)) { IndexCtx.indexTopLevelDecl(D); } DataConsumer->finish(); Index: tools/libclang/CXIndexDataConsumer.h =================================================================== --- tools/libclang/CXIndexDataConsumer.h +++ tools/libclang/CXIndexDataConsumer.h @@ -342,7 +342,7 @@ CXTranslationUnit getCXTU() const { return CXTU; } void setASTContext(ASTContext &ctx); - void setPreprocessor(std::shared_ptr PP); + void setPreprocessor(std::shared_ptr PP) override; bool shouldSuppressRefs() const { return IndexOptions & CXIndexOpt_SuppressRedundantRefs;