[cfe-dev] A newbie question on getting operator in Expr

sirinda sxp969 at psu.edu
Fri Jun 17 22:43:43 PDT 2011


Well, I have my own class MyConsumer declared as below 

class MyConsumer: public ASTConsumer,
		public DeclVisitor<MyConsumer> ,
		public StmtVisitor<MyConsumer> ,
		public TypeLocVisitor<MyConsumer> 

Then I modify VisitStmt(Stmt *Node) function. Inside the function I check if
this is an Expr or not. The code look like this: 

void VisitExpr(Expr *expr) { 
 
          // check if this expression contains a binary operation
          if (isa<clang::BinaryOperator>(expr)) {
                  BinaryOperator* biOp = (BinaryOperator *) expr;  
		  
                  // get the lhs and rhs of the operator  
                  Expr* lhs = biOp->getLHS();
		  Expr* rhs = biOp->getRHS();
                  // recursively visit each one
                 VisitExpr(lhs);
                 VisitExpr(rhs);

          }
.... // check other kinds of expr, 
    // since my test code is simple, the recursion stops when it find
DeclRefExpr
         else if (isa<clang::DeclRefExpr>(expr))  { 
			DeclRefExpr* declRef = (DeclRefExpr*)expr;
                        // get a var name
                        string name =
(declRef->getNameInfo()).getName().getAsString();
                       cout << name << endl; 
        } 
.... 

I hope this helps. I am still new to clang (and c++ ) so I am not sure if
this is an efficient way to do but it works for me now :) 




--
View this message in context: http://clang-developers.42468.n3.nabble.com/A-newbie-question-on-getting-operator-in-Expr-tp3073430p3078932.html
Sent from the Clang Developers mailing list archive at Nabble.com.



More information about the cfe-dev mailing list