<div class="gmail_quote">On Wed, Jul 18, 2012 at 4:56 AM, Olaf Krzikalla <span dir="ltr"><<a href="mailto:Olaf.Krzikalla@tu-dresden.de" target="_blank">Olaf.Krzikalla@tu-dresden.de</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi @clang,<br>
<br>
I'm working at my source-to-source transformator based on clang for<br>
quite some time now. However, my last clang update constantly causes me<br>
some serious headaches. Mainly it is the new expr::EvaluateAsRValue<br>
framework, which asserts like hell. Of course rightly since I cannot<br>
preserve all - often hidden - AST invariants during transformations.<br></blockquote><div><br></div><div>You should use Sema interfaces to build AST nodes, rather than building them yourself, in order to preserve the AST invariants.</div>
<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Some assertions were fixable, but for a lot of others I have no clue.<br>
An example: transforming a+=b to a=a+b works like this in my framework:<br>
<br>
CompoundAssignOperator* Node;<br>
// compute binOpc<br>
Expr* firstRhs = Clone_(Node->getLHS());<br>
firstRhs->setValueKind(VK_RValue);<br>
BinaryOperator* TC = Assign_(<br>
Node->getLHS(),<br>
BinaryOp_(firstRhs, Node->getRHS(), binOpc));<br>
<br>
I had to figure out that I had to set the value kind.</blockquote><div><br></div><div>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.</div>
<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">So my question is whether there is or there will be a AST transformation<br>
framework capable of transforming expressions like above?</blockquote><div><br></div><div>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.</div>
<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Or whether<br>
clang/AST transformation itself is a dead end in the long run?</blockquote><div><br></div><div>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, ...).</div>
<div><br></div><div>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.</div>
</div>