<DIV>Taking a look at the ObjC Rewriter for examples, I want to change the operator * to << (source to source) . </DIV>
<DIV> </DIV>
<DIV>I create a new BinaryOperator and replace the Expr .</DIV>
<DIV> </DIV>
<DIV> </DIV>
<DIV> rhs = new IntegerLiteral( Res, BO->getRHS()->getType(), BO->getRHS()->getExprLoc() );<BR></DIV>
<DIV>// change * to << <BR> Replacement = new BinaryOperator(BO->getLHS(), rhs, <BR> BO->Shl, BO->getType(), BO->getLHS()->getExprLoc());<BR>//replace the expression <BR> assert( !Rewrite.ReplaceStmt(BO, Replacement) );</DIV>
<DIV> </DIV>
<DIV> </DIV>
<DIV>but the output SourceLocation has some problem in the result source code, when there are two * Operateor more.</DIV>
<DIV><BR> </DIV>
<DIV> </DIV>
<DIV>==================================</DIV>
<DIV> </DIV>
<DIV> Stmt *RewriteCai::RewriteBinaryOperator(BinaryOperator *BO)<BR>{</DIV>
<DIV> Expr *Replacement, *rhs, *temp;<BR> <BR> <BR> temp = BO->getRHS();</DIV>
<DIV> </DIV>
<DIV> if( BO->isMultiplicativeOp() )<BR> { </DIV>
<DIV> llvm::APSInt Res(32);</DIV>
<DIV> <BR> SourceLocation ll = BO->getRHS()->getExprLoc();</DIV>
<DIV><BR> if( (BO->getRHS())->isIntegerConstantExpr( Res, *Context, &ll, true) )<BR> {<BR> int a = Res.getZExtValue();<BR> if((a&(a-1))==0) Res = Res.logBase2();<BR> <BR> //new right-child subExpr<BR> rhs = new IntegerLiteral( Res, BO->getRHS()->getType(), BO->getRHS()->getExprLoc() );<BR> <BR> assert(rhs);<BR> <BR> }<BR> else<BR> {<BR> rhs = temp;<BR> <BR> }<BR> // change * to << <BR> Replacement = new BinaryOperator(BO->getLHS(), rhs, <BR> BO->Shl, BO->getType(), BO->getLHS()->getExprLoc());<BR> <BR> assert(!Rewrite.ReplaceStmt(BO, Replacement));<BR> <BR> <BR> delete BO;<BR> return Replacement;<BR><BR> }</DIV>
<DIV> <BR> return 0;</DIV>
<DIV>}<BR></DIV><br><!-- footer --><br>
<hr>
<font style="font-size:12px;line-height:15px;">ËÍ£¡ËÍ£¡ËÍ£¡</font><a style="font-size:12px;line-height:15px; color:blue; text-decoration:underline;" href="http://popme.163.com/link/004290_0422_5595.html">Õý°æÈðÐÇ2008°ëÄêÃâ·Ñ£¡</a>