<span style="font-family:arial,sans-serif;font-size:12px;background-color:rgb(255,255,255)">There's a huge amount of boilerplate needed to do what you want, and it will still be missing things like #include paths which will render your program unusable for real code.</span><div style="font-family:arial,sans-serif;font-size:12px;background-color:rgb(255,255,255)">
<br></div><div style="font-family:arial,sans-serif;font-size:12px;background-color:rgb(255,255,255)">The dependency graph of objects that need to be created in order to get a Preprocessor is gigantic. I manually tracked it here as I was exploring the APIs myself: <a href="http://web.ics.purdue.edu/~silvas/deps.svg" target="_blank" style="color:rgb(17,85,204)">http://web.ics.purdue.edu/~silvas/deps.svg</a> . CompilerInstance reduces the amount of boilerplate needed, but there are still crucial things missing for it to be "useful" (see: <a href="https://github.com/loarabia/Clang-tutorial/blob/master/CItutorial3.cpp" target="_blank" style="color:rgb(17,85,204)">https://github.com/loarabia/Clang-tutorial/blob/master/CItutorial3.cpp</a> and try including stdio.h in test.c).</div>
<div style="font-family:arial,sans-serif;font-size:12px;background-color:rgb(255,255,255)"><br></div><div style="font-family:arial,sans-serif;font-size:12px;background-color:rgb(255,255,255)">A couple weeks ago I went down this path that you are trying to go down, and I can tell you that you are not going to achieve what you want by doing it like this (e.g., as a standalone program using the Clang libs). There are just too many little things that need to be configured in specific ways for the compiler to work "as usual, except also run my (PPCallbacks|ASTConsumer|...)". My recommendation is to write a Clang plugin, so that you can forget about all those things. There is an example plugin called PrintFunctionNames in examples/PrintFunctionNames that you can look at (although TBH I found it extremely unhelpful for some reason). If you want, I can post on github a minimal "hello world" example for how to run a PPCallbacks.</div>
<div style="font-family:arial,sans-serif;font-size:12px;background-color:rgb(255,255,255)"><br></div><div style="font-family:arial,sans-serif;font-size:12px;background-color:rgb(255,255,255)">--Sean Silva</div><br><div class="gmail_quote">
On Tue, Feb 21, 2012 at 5:35 PM, Aditya Kumar <span dir="ltr"><<a href="mailto:hiraditya@msn.com">hiraditya@msn.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">



<div><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></div>
<br>_______________________________________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@cs.uiuc.edu">cfe-dev@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev</a><br>
<br></blockquote></div><br>