[cfe-users] Retrieve value from DeclExprRef

Marcos Horro Varela via cfe-users cfe-users at lists.llvm.org
Wed Mar 3 13:29:36 PST 2021

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' <LValueToRValue>

      |     `-DeclRefExpr 0x23406d8 <col:18> 'int' lvalue Var 0x233fdb8 'I'


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<DeclStmt>(ForLoop->getInit());


  if (NameValInit != nullptr) {

    clang::Expr::EvalResult R;

    V = dyn_cast<VarDecl>(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


Thanks for your time.


Kind regards,








-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-users/attachments/20210303/3e3a5c0a/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 5997 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-users/attachments/20210303/3e3a5c0a/attachment.bin>

More information about the cfe-users mailing list