[cfe-dev] Rewriter: expand macro to definition

Alex L via cfe-dev cfe-dev at lists.llvm.org
Thu Aug 17 04:37:02 PDT 2017


I was looking into this last week when investigating "expand macro" &
"preprocess" actions for Clang's new refactoring engine. I came to the
conclusion that the best way to solve this full preprocessing issue is to
re-preprocess the whole translation unit, use a custom PP frontend action,
and call a modified version of clang::DoPrintPreprocessedInput which
implements the -E mode. The modification in `DoPrintPreprocessedInput`
boiled down to a source range constraint that controlled which tokens
should be printed to ensure that only the relevant tokens (that actually
make-up the BAZ(test) expansion) are printed instead of the whole
preprocessed source. Btw, would you be interested in working on a patch for
Clang that implements this modification if you decide to use this approach?

I hope this helps,
Alex

On 17 August 2017 at 12:25, Alexander Graf via cfe-dev <
cfe-dev at lists.llvm.org> wrote:

> Hi all,
>
> I am currently working on a source-to-source transformation using
> libtooling/clang. I would like to selectively expand some macros to their
> definition during rewrite. Possible recursion and other macros in the
> expansion shall be expanded as well. e.g.
>
> #define FOO( name, line ) name##line
> #define BAR( name, line ) FOO( name, line )
> #define BAZ( name ) BAR( name, __LINE__ )
>
> such that
>
> BAZ( test )
>
> at line 42 can be expanded to the string "test42". How can I do that? I
> found an old, similar question on the clang list, which never received a
> proper answer[1]. It cannot be that hard, can it?
>
> Thank you!
>
> Regards,
> Alex
>
> [1] http://clang-developers.42468.n3.nabble.com/Using-Clang-to-E
> xpand-Macros-td4028424.html
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20170817/1f5d672d/attachment.html>


More information about the cfe-dev mailing list