[cfe-dev] SourceLocation TODO
caiwei
cworce at 126.com
Thu May 8 20:26:26 PDT 2008
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.
==================================
Stmt *RewriteCai::RewriteBinaryOperator(BinaryOperator *BO)
{
Expr *Replacement, *rhs, *temp;
temp = BO->getRHS();
if( BO->isMultiplicativeOp() )
{
llvm::APSInt Res(32);
SourceLocation ll = BO->getRHS()->getExprLoc();
if( (BO->getRHS())->isIntegerConstantExpr( Res, *Context, &ll, true) )
{
int a = Res.getZExtValue();
if((a&(a-1))==0) Res = Res.logBase2();
//new right-child subExpr
rhs = new IntegerLiteral( Res, BO->getRHS()->getType(), BO->getRHS()->getExprLoc() );
assert(rhs);
}
else
{
rhs = temp;
}
// change * to <<
Replacement = new BinaryOperator(BO->getLHS(), rhs,
BO->Shl, BO->getType(), BO->getLHS()->getExprLoc());
assert(!Rewrite.ReplaceStmt(BO, Replacement));
delete BO;
return Replacement;
}
return 0;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20080509/ae8393e4/attachment.html>
More information about the cfe-dev
mailing list