[cfe-dev] Future of AST transformation

Olaf Krzikalla Olaf.Krzikalla at tu-dresden.de
Tue Jul 24 07:00:33 PDT 2012


Hi @clang,

 > Yes, that's unfortunate for your use case, but you should at least be
 > able to call the relevant Sema::Build* methods directly.
Below is some apparently working code:

// a += b -> a = a + b
struct CompoundAssignTransform : TreeTransform<CompoundAssignTransform>
{
   CompoundAssignTransform (Sema& s) :
     TreeTransform<CompoundAssignTransform>(s) {}

   bool AlwaysRebuild() { return true; }  // needed for cloning the lhs

   ExprResult TransformCompoundAssignOperator(CompoundAssignOperator *E)
   {
     BinaryOperator::Opcode binOpc = computeOpc(E); // a switch-case

     ExprResult lhsClone = TransformExpr(E->getLHS());
     if (lhsClone.isInvalid()) return lhsClone;

     ExprResult rhs = RebuildBinaryOperator(
       E->getOperatorLoc(), binOpc, lhsClone.get(), E->getRHS());
     if (rhs.isInvalid()) return rhs;

     return RebuildBinaryOperator(
       E->getOperatorLoc(), BO_Assign, E->getLHS(), rhs.get());
   }
};

Is this the way to go? Esp. the part cloning the lhs via TransformExpr 
seems a littly bit sketchy to me. However it works with AlwaysRebuild == 
true. And the cloning stuff is the only used portion of TreeTransform. 
The remains are redirected calls to Sema. So I could go without 
TreeTransform, if I have another cloning means. But maybe this is indeed 
how TreeTransform is intended to use. In this case I strongly vote for a 
move of TreeTransform.h to the include directory :-)

Best regards
Olaf Krzikalla



More information about the cfe-dev mailing list