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

Michael Kruse via cfe-dev cfe-dev at lists.llvm.org
Mon Dec 2 08:55:41 PST 2019


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



More information about the cfe-dev mailing list