[cfe-dev] Future of AST transformation

Richard Smith richard at metafoo.co.uk
Wed Jul 18 15:22:46 PDT 2012


On Wed, Jul 18, 2012 at 4:56 AM, Olaf Krzikalla <
Olaf.Krzikalla at tu-dresden.de> wrote:

> Hi @clang,
>
> I'm working at my source-to-source transformator based on clang for
> quite some time now. However, my last clang update constantly causes me
> some serious headaches. Mainly it is the new expr::EvaluateAsRValue
> framework, which asserts like hell. Of course rightly since I cannot
> preserve all - often hidden - AST invariants during transformations.
>

You should use Sema interfaces to build AST nodes, rather than building
them yourself, in order to preserve the AST invariants.


> Some assertions were fixable, but for a lot of others I have no clue.
> An example: transforming a+=b to a=a+b works like this in my framework:
>
> CompoundAssignOperator* Node;
> // compute binOpc
> Expr* firstRhs = Clone_(Node->getLHS());
> firstRhs->setValueKind(VK_RValue);
> BinaryOperator* TC = Assign_(
>    Node->getLHS(),
>    BinaryOp_(firstRhs, Node->getRHS(), binOpc));
>
> I had to figure out that I had to set the value kind.


That's not the right thing to do. You need to create an ImplicitCastExpr of
kind IK_LValueToRValue (and potentially perform other parts of the usual
arithmetic conversions). Sema::BuildBinOp will handle this for you.


> So my question is whether there is or there will be a AST transformation
> framework capable of transforming expressions like above?


Take a look at lib/Sema/TreeTransform.h. Using that, it is certainly
possible to perform AST to AST transformations in a way which is convenient
and maintains the AST invariants.


> Or whether
> clang/AST transformation itself is a dead end in the long run?


Since you said you're performing source to source transformations, AST
transformation is unlikely to be a good path to follow. A source to source
transformation tool should usually make very targeted modifications to the
code, and it's not really practical to deduce what those changes should
have been if all you have are ASTs from before and after (think about
preserving whitespace, comments, macros, templates, ...).

The usual approach for clang-based source-to-source transformation tools is
to use the AST to determine what changes should be made, then produce a
list of modifications to be made to the original source file. See
clang::Rewriter, clang::tooling::Replacement, and
clang::tooling::RefactoringTool for some components which make it easier to
build such tools.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20120718/5c00a29d/attachment.html>


More information about the cfe-dev mailing list