<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 10pt;
font-family:Tahoma
}
--></style></head>
<body class='hmmessage'><div dir='ltr'>
<div>I want to use a PPCallback function (MacroExpands) to track all the locations where </div><div>a macro has been invoked in the program. </div><div>In the main function, there are a few things that I want to implement, details are </div><div>given as comment inside the main function.</div><div><br></div><div>Till now what I have done is the following:</div><div><br></div><div><div>namespace clang {</div></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;"><div><div>class MyASTAction : public FrontendAction</div></div><div><div>{                 </div></div><div><div>  public:</div></div><div><div>    ASTConsumer *CreateASTConsumer(CompilerInstance &CI,</div></div><div><div>                  llvm::StringRef InFile)</div></div><div><div>    {<span style="font-size: 10pt; ">    return 0;</span><span style="font-size: 10pt; ">   }</span></div></div><div><div>    void ExecuteAction()<span style="font-size: 10pt; "> { }</span></div></div><div><div><br></div></div><div><div>    bool usesPreprocessorOnly() const</div></div><div><div>    { return false; }</div></div><div><div>};</div></div><div><div>  </div></div><div><div>class TrackMacro : public PPCallbacks</div></div><div><div>{ </div></div><div><div>  public:           <span style="font-size: 10pt; ">  </span></div></div><div><div>  void MacroExpands(const Token &MacroNameTok, const MacroInfo* MI,</div></div><div><div>                    SourceRange Range)</div></div><div><div>  {</div></div><div><div>    std::cout<<"Macro expands to"<<MacroNameTok.getRawIdentifierData();</div></div><div><div>  }</div></div><div><div>  ~TrackMacro(){}</div></div><div><div>};</div></div></blockquote><div>}</div><div><br></div><div><div>using namespace clang;</div><div>int main(int argc, const char** argv)</div><div>{</div><div>  <span style="font-size: 10pt; ">My</span><span style="font-size: 10pt; ">ASTAction action;</span></div><div>  CompilerInstance Clang;</div><div><br></div><div>/*****************************************</div><div><br></div><div>How to create an instance of the preprocessor class, it seems like </div><div>it takes too many(9) arguments which I was not able to figure out.</div><div>Or is there a way to just create a default preprocessor and add my </div><div>own call back function to it. And then pass the preprocessor object </div><div>to the Parser and invoke the parser just by supplying a filename.</div><div><br></div><div>*****************************************/</div><div>  TrackMacro track_macro;</div><div>  PP.addPPCallbacks(&track_macro);</div><div><br></div><div>  Clang.setPreprocessor(PP);</div><div><br></div><div>   MyASTAction* <span style="font-size: 10pt; ">Act = new </span><span style="font-size: 10pt; ">My</span><span style="font-size: 10pt; ">ASTAction();</span></div><div>   Clang.ExecuteAction(*Act);</div><div><br></div><div>return 0;</div><div>}</div><div><br></div></div><div><br></div>                                    </div></body>
</html>