<div dir="ltr">Hello:<div><br></div><div>I just started looking at libClang recently, so please pardon me if this is a noobish question.</div><div><br></div><div>I want to essentially build a custom preprocessing stage and I am not sure where to get started.  I want to be able to generate some code based on some code in my source files, things like:</div><div> 1. Insert statements in selected functions</div><div> 2. Insert members in a struct</div><div><br></div><div>And I want to be able to to this as part of the compilation, like, in other words, I don't see this as a refactoring, i.e. the original source file should remain intact, and all compilation error messages should reference line numbers for the original file, and I'm not really concerned with keeping the intermediate/preprocessed version anywhere.  Ideally this could be done as a single step.</div><div><br></div><div>I have found the documentation for C api for libclang and it looks like it is mainly for reading the AST, like you can't actually start a compilation or alter the AST.</div><div><br></div><div>I also found the C++ API for the "Driver" code, and that looks more functional, but it isn't mentioned as a recommended API, so I wanted to check to see if maybe I am missing something in the C api... </div><div><br></div><div>There are also the plugins API, and I found some examples on how to rewrite code there using the "Rewriter" class but that looks like its designed for refactoring, not preprocessing.  Specifically it doesn't output or keep track of line markers, and after you rewrite the code there doesn't seem to be a way to compile the new version of the code.  </div><div><br></div><div>The best strategy that I have for moving forward is to try to use the "Driver" c++ api to run a custom plugin and do a preprocessor stage only, then use a "RecursiveASTVisitor"  to go through the whole AST, and output that into a temporary buffer/file, optionally making inserts/edits based on conditions, and manually implementing line markers by using "getSourceRange" and then looping that process until there is no more changes then feeding the temporary buffer into the compilation stage...</div><div><br></div><div>There is also the issue that sometimes my messages to the preprocessor aren't actually valid statements (undeclared identifiers), and it seems that the AST Visiting functionality completely ignores any error statements... The best way that I can think to work around this is to just add an option to define them as some kind of internal function/variable that I can filter for...</div><div><br></div><div>Any feedback would be appreciated.  Thanks :)</div><div><br></div><div><br></div></div>