Getting the value of a IntegerLiteral

Hi,

I'm trying to figure out how I can obtain the value of an IntegerLiteral. I tried the following:

const IntegerLiteral *lit = ...;
if (lit != 0)
{
     int64_t n = lit->getValue().getSExtValue();
     // do something with n
}

This seems to work sometimes, but frequently crashes. I've tried to figure out how to use the Evaluate*-functions in the Expr class, but I cannot figure out how they should be called.

It's ok if obtaining the value sometimes fails, but it would be nice if it didn't crash. :slight_smile:

Are you using a +asserts build? What kind of crash are you observing?

Are you using a +asserts build? What kind of crash are you observing?

wtd-analyzer: /home/jesperes/dev/clang-install/include/llvm/ADT/APInt.h:1316: int64_t llvm::APInt::getSExtValue() const: Assertion `getMinSignedBits() <= 64 && "Too many bits for int64_t"' failed.
      [exec] Stack dump:
      [exec] 0. <eof> parser at end of file

Not sure what that means.

Are you using a +asserts build? What kind of crash are you observing?

wtd-analyzer:
/home/jesperes/dev/clang-install/include/llvm/ADT/APInt.h:1316: int64_t
llvm::APInt::getSExtValue() const: Assertion `getMinSignedBits() <= 64
&& "Too many bits for int64_t"' failed.
       [exec] Stack dump:
       [exec] 0. <eof> parser at end of file

Not sure what that means.

I discovered that I really don't need the integer literal as an integer, since it will be converted to a string anyway, so I went with

getValue().toString(10, true);

instead.