2012-11-02 21 views
3

我有以下的覆盖方法VisitBinaryOperator()代码铛值:如何让整型变量名称及其从Expr的*在铛

Expr* lhs = E->getLHS(); 
Expr* rhs = E->getRHS(); 

我想提取整数变量名称,并从表达lhs其价值和rhs

说我有,那么我想从rhslhs10获得标识符x
如果我有x = x + 10;那么我想从rhs

而且类型我得到这个: int identifierlhsx + 10作为子表达式得到标识符x当我倾倒lhs

QualType type_lhs = lhs->getType(); 
type_lhs->dump(); 

这怎么可以为叮当完成吗?

回答

4

使用dyn_cast来确定你对LHS什么样的表情:

if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(lhs)) { 
    // It's a reference to a declaration... 
    if (VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) { 
    // It's a reference to a variable (a local, function parameter, global, or static data member). 
    std::cout << "LHS is " << VD->getQualifiedNameAsString() << std::endl; 
    } 
} 

,如果你要处理的LHS其他表现形式你需要更复杂的代码。如果你想在那里处理任意代码,看看RecursiveASTVisitor

为了评估右边的值(假设它的东西,铛可以评估,如10,不喜欢x + 10),使用ExprEvaluate*功能之一:

llvm::APSInt Result; 
if (rhs->EvaluateAsInt(Result, ASTContext)) { 
    std::cout << "RHS has value " << Result.toString(10) << std::endl; 
} 
相关问题