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

Marcos Horro Varela via cfe-dev cfe-dev at lists.llvm.org
Tue Dec 3 12:54:04 PST 2019

Michael, Yitzhak,

Thank you so much for your comments. I think I will go with the ASTFrontendAction in detriment of clang-tidy in order to have more "freedom" when writing the tool. Nonetheless, I am quite concerned regarding my decision of not modifying the AST. Maybe using the TreeTransform helper would be a better decision. On the other hand, I do not see the utility of performing modifications in the AST; could you provide me an example where it is useful rather than "just parsing and replacing code"?
Again, thank you so much.

Best regards,


Marcos Horro

PhD Student

Universidade da Coruña (UDC)


De: Yitzhak Mandelbaum
Enviado: Martes, 03 de decembro de 2019 20:59
Para: Michael Kruse
CC: Marcos Horro Varela; via cfe-dev
Asunto: Re: [cfe-dev] [Clang Tools] Source-to-source transformations

+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<mailto: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<mailto: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.

cfe-dev mailing list
cfe-dev at lists.llvm.org<mailto:cfe-dev at lists.llvm.org>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20191203/02b53d0c/attachment.html>

More information about the cfe-dev mailing list