Retrieve value from DeclExprRef

Hello all,

Having a loop like this:

-DeclStmt 0x233fe40 <line:14:5, col:14>

| `-VarDecl 0x233fdb8 <col:5, col:13> col:9 used I ‘int’ cinit

| `-IntegerLiteral 0x233fe20 col:13 ‘int’ 0

`-ForStmt 0x236b390 <line:29:5, line:33:5>

-DeclStmt 0x2340710 <line:29:10, col:19>

`-VarDecl 0x2340670 <col:10, col:18> col:14 used i ‘int’ cinit

| `-ImplicitCastExpr 0x23406f8 col:18 ‘int’

| `-DeclRefExpr 0x23406d8 col:18 ‘int’ lvalue Var 0x233fdb8 ‘I’ ‘int’

Which basically corresponds to:

Int I;

int I = 0;

for (i = I; …)

I am trying to parse that loop such as:

const ASTContext *C = …

const DeclStmt *NameValInit = dyn_cast(ForLoop->getInit());

if (NameValInit != nullptr) {

clang::Expr::EvalResult R;

V = dyn_cast(NameValInit->getSingleDecl());

if (V != nullptr) {

if (V->getInit()->EvaluateAsInt(R, *C)) {

return R.Val.getInt().getExtValue();


I have checked this thread [1], which is very insightful. I guess that my conceptual error lies in the “EvaluateAsInt”, however, how can I retrieve the value of the IntegerLiteral of the VarDecl referenced by the DeclrRexExpr?

Thanks for your time.

Kind regards,