[cfe-dev] Preprocessor/Parser interaction

Eli Friedman eli.friedman at gmail.com
Tue Feb 21 14:44:57 PST 2012


On Tue, Feb 21, 2012 at 2:35 PM, Aditya Kumar <hiraditya at msn.com> wrote:
> I want to use a PPCallback function (MacroExpands) to track all the
> locations where
> a macro has been invoked in the program.
> In the main function, there are a few things that I want to implement,
> details are
> given as comment inside the main function.
>
> Till now what I have done is the following:
>
> namespace clang {
>
> class MyASTAction : public FrontendAction
> {
>   public:
>     ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
>                   llvm::StringRef InFile)
>     {    return 0;   }
>     void ExecuteAction() { }
>
>     bool usesPreprocessorOnly() const
>     { return false; }
> };
>
> class TrackMacro : public PPCallbacks
> {
>   public:
>   void MacroExpands(const Token &MacroNameTok, const MacroInfo* MI,
>                     SourceRange Range)
>   {
>     std::cout<<"Macro expands to"<<MacroNameTok.getRawIdentifierData();
>   }
>   ~TrackMacro(){}
> };
>
> }
>
> using namespace clang;
> int main(int argc, const char** argv)
> {
>   MyASTAction action;
>   CompilerInstance Clang;
>
> /*****************************************
>
> How to create an instance of the preprocessor class, it seems like
> it takes too many(9) arguments which I was not able to figure out.
> Or is there a way to just create a default preprocessor and add my
> own call back function to it. And then pass the preprocessor object
> to the Parser and invoke the parser just by supplying a filename.

You don't create the preprocessor instance yourself; you grab it using
getCompilerInstance().getPreprocessor() in your FrontendAction.

-Eli




More information about the cfe-dev mailing list