[cfe-dev] getting int value from IntegerLiteral, float value from FloatingLiteral

Rajendra rks at cse.iitb.ac.in
Sat Dec 15 22:32:00 PST 2012


Hi,

I want to get precise int value for a variable from IntegerLiteral type 
and float value for a variable from FloatingLiteral type (do not want to 
loose precision). I don't see a function to call to get int or unsigned 
int directly.

Is following code only correct way or is there any better way to get 
these?

     const Expr *e = varDecl->getInit();
     const IntegerLiteral *IL = dyn_cast<IntegerLiteral>(e);  // e is of 
type Expr*
     double val = IL->getValue().signedRoundToDouble();
     int ival = (int) val;
     std::cerr << "\tinitial value: " << val << " ival " << ival << 
"\n";

     const Expr *e = varDecl->getInit();
     const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(e);  // e is 
of type Expr*
     double val = FL->getValue().convertToDouble();
     float fval = (float) val;
     std::cerr << "\tinitial value: " << val << " fval " << fval << 
"\n";

Please advise.

- Rajendra



More information about the cfe-dev mailing list