Taking a look at the ObjC Rewriter for examples, I want to change the operator * to << (source to source) .
I create a new BinaryOperator and replace the Expr .
rhs = new IntegerLiteral( Res, BO->getRHS()->getType(), BO->getRHS()->getExprLoc() );
// change * to <<
Replacement = new BinaryOperator(BO->getLHS(), rhs,
BO->Shl, BO->getType(), BO->getLHS()->getExprLoc());
//replace the expression
assert( !Rewrite.ReplaceStmt(BO, Replacement) );
but the output SourceLocation has some problem in the result source code, when there are two * Operateor more.
Taking a look at the ObjC Rewriter for examples, I want to change the operator * to << (source to source) .
I create a new BinaryOperator and replace the Expr .
rhs = new IntegerLiteral( Res, BO->getRHS()->getType(), BO->getRHS()->getExprLoc() );
// change * to <<
Replacement = new BinaryOperator(BO->getLHS(), rhs,
BO->Shl, BO->getType(), BO->getLHS()->getExprLoc());
//replace the expression
assert( !Rewrite.ReplaceStmt(BO, Replacement) );
but the output SourceLocation has some problem in the result source code, when there are two * Operateor more.
I assume that you mean that you have problems rewriting things like "(x*2)*2".
This is because you're deleting the AST:
// change * to <<
Replacement = new BinaryOperator(BO->getLHS(), rhs,
BO->Shl, BO->getType(), BO->getLHS()->getExprLoc());
Here the old multiply node and the << both point to the LHS AST.
delete BO;
This deletes the "*" AST and the LHS/RHS. To avoid having it delete the LHS, set the LHS of the multiply to null before you delete it.
-Chris