[cfe-dev] [Clang Tools] Source-to-source transformations

Yitzhak Mandelbaum via cfe-dev cfe-dev at lists.llvm.org
Tue Dec 3 11:59:30 PST 2019


+1 to taking a look at clang::tooling::Transformer, which is the adaptor
that ties the libraries in clang::transformer to actual tools. I'm happy to
answer questions.

You should also consider whether your tool can be written as a clang tidy
check rather than using ASTFrontendAction, since that saves you from
needing to write your own tool around the transformation.  However, it also
means you're confined to clang-tidy's interface. So, it has pluses and
minuses.
If you decide to go with clang-tidy, you can use the adaptor
https://github.com/llvm/llvm-project/blob/master/clang-tools-extra/clang-tidy/utils/TransformerClangTidyCheck.h
to
tie to clang::transformer.

On Mon, Dec 2, 2019 at 11:56 AM Michael Kruse via cfe-dev <
cfe-dev at lists.llvm.org> wrote:

> Am So., 1. Dez. 2019 um 23:23 Uhr schrieb Marcos Horro Varela via
> cfe-dev <cfe-dev at lists.llvm.org>:
> > I have not delved deep in all the FrontendAction subclasses in Clang.
> For our purpose, is ASTFrontendAction suitable? Are there any other better
> choices?
>
> Not an expert, but I think deriving from ASTFontendAction is exactly
> intended for such as use case.
>
> > Is it fine to not modify the original AST when performing
> source-to-source transformations? My first idea was to modify it until I
> read the "immutability philosophy". On the other hand, I only care about
> the AST for parsing, not for the output (I am already performing the
> modifications with Rewriter, right?).
>
> It is always fine to **not** do something. Indeed, after parsing, the
> AST is not supposed to be modified and you would get unexpected
> results if you try (some analysis is baked in at this stage, e.g. name
> lookup, overload resolution, implicit casts, etc). You can, however,
> create a new but modified AST from the parsed one. Template
> instantiation uses this via the TreeTransform helper. I still suggest
> to avoid this if you don't need to. Instead as you mentioned you can
> use clang::Rewriter to directly modify the input source characters
> that can be found using SourceLocation. Note there is also
> clang::tooling::Transformer to help with this task.
>
> Michael
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> https://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/20191203/e1023001/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4847 bytes
Desc: S/MIME Cryptographic Signature
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20191203/e1023001/attachment.bin>


More information about the cfe-dev mailing list