[cfe-dev] Questions on implementing a custom preprocessor

Dave Butler via cfe-dev cfe-dev at lists.llvm.org
Wed Feb 8 16:47:55 PST 2017


Hello:

I just started looking at libClang recently, so please pardon me if this is
a noobish question.

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:
 1. Insert statements in selected functions
 2. Insert members in a struct

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.

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.

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...

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.

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...

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...

Any feedback would be appreciated.  Thanks :)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20170209/84ef1858/attachment.html>


More information about the cfe-dev mailing list