[cfe-dev] questions regarding using addPPCallbacks

Farzad Sadeghi via cfe-dev cfe-dev at lists.llvm.org
Wed Dec 7 15:51:28 PST 2016


Lets say I want to add a PP callback every time the PP runs into an
InclusionDirective.
going through the docs, I find that clang::PPCallbacks has the virtual
member functions for me implement to do just that. So far so good.
Now, how to tell the PP to use my callbacks?
I came across addPPCallbacks in clang::Preprocessor.
The code already has a class that derives from ASTConsumer to add and
run the matchers.
There is also a class that derives from ASTFrontendAction that is
passed to newFrontendActionFactory to create an instance of, to be
passed to a ClangTool instance to run the action.
Now the questions:
Do i need to cerate a new FrontendAction for the PPCallbacks? If yes,
then does that mean that the inputs get lexed and parsed twice?

There is only one preprocessor instance used for each frontendaction,right?
So i need to get a hold of that instance and add my PPCallbacks there.
I went through the docs again and found that I could do that with a
clang::CompilerInstace.
Now in my class that derives from ASTFrontendAction, I figured I'm
getting the CompilerInstance, so maybe i can get the preprocessor
there:
  std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
StringRef file) override
{
    Preprocessor &PP = CI.getPreprocessor();
    PP.addCallbacks(std::unique_ptr<PPCallback> PPInclusion)

    TheRewriter.setSourceMgr(CI.getSourceManager(), CI.getLangOpts());
    return llvm::make_unique<MyASTConsumer>(TheRewriter);
 }

PPInclusio is the class that derives from clang::PPCallbacks.
But the compiler tells me "error: member access into incomplete type"
which basically means that the Preprocessor i got from
CompilerInstance is a forward declaration.
How can I actually add a preprocessor callback then?


-- 
Farzad Sadeghi



More information about the cfe-dev mailing list