forked from OSchip/llvm-project
[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. llvm-svn: 370323
This commit is contained in:
parent
a280b63ead
commit
c65204148c
|
@ -23,38 +23,6 @@ using namespace clang::index;
|
|||
|
||||
namespace {
|
||||
|
||||
class IndexASTConsumer final : public ASTConsumer {
|
||||
std::shared_ptr<Preprocessor> PP;
|
||||
std::shared_ptr<IndexingContext> IndexCtx;
|
||||
|
||||
public:
|
||||
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);
|
||||
}
|
||||
|
||||
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 final : public PPCallbacks {
|
||||
std::shared_ptr<IndexingContext> IndexCtx;
|
||||
|
||||
|
@ -85,6 +53,39 @@ public:
|
|||
}
|
||||
};
|
||||
|
||||
class IndexASTConsumer final : public ASTConsumer {
|
||||
std::shared_ptr<Preprocessor> PP;
|
||||
std::shared_ptr<IndexingContext> IndexCtx;
|
||||
|
||||
public:
|
||||
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);
|
||||
PP->addPPCallbacks(std::make_unique<IndexPPCallbacks>(IndexCtx));
|
||||
}
|
||||
|
||||
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 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();
|
||||
|
|
Loading…
Reference in New Issue