libclang, initializers and constant folding

There appears to be no way one can get hold of compile-time constant
initializers via the libclang API. For example, enumeration initializer values
can't be accessed: the children of an enum declaration cursor have (for values
with initializers) a cursor of type CXCursor_UnexposedExpr.

ISTM the only way of getting beyond this is to use clang_tokenize on the extent
of that cursor, but this does not help in the case where initializers are
expressions. For example, say we process the source

enum Enum1 {
    VAL1 = -1,
    VAL2,
    VAL3
};

enum Enum2 {
    VAL4 = VAL3,
    VAL5 = VAL2 + 7,
    VAL6 = VAL3 << 2
};

Is there any way using libclang to know that the values of VAL1 through VAL6
would be -1, 0, 1, 1, 7 and 4?

Regards,

Vinay Sajip

There is currently no libclang API for this. One could be added, if needed.

  - Doug

Douglas Gregor <dgregor@...> writes:

There is currently no libclang API for this. One could be added, if needed.

Well, I need such an API :slight_smile: There was also a question about this (from someone
else) on the mailing list, earlier this year.

I'm very new to the clang code base, so I tried implementing the functionality
using an experimental function like this:

int clang_getCursorValue(CXCursor C)
{
    int result = 0;
    Decl * D = getCursorDecl(C);

    if (isa<EnumConstantDecl>(D)) {
      llvm::APSInt Value = static_cast<EnumConstantDecl *>(D)->getInitVal();

      result = (int) Value.getSExtValue();
    }
    return result;
}

This works as far as it goes, giving the correct values for my earlier example,
but it's obviously incomplete.

Possibly there should be a set of functions such as

int clang_getCursorIntegerValue(CXCursor);
double clang_getCursorDoubleValue(CXCursor);
CXString clang_getCursorStringValue(CXCursor);

However, I have no idea if this gels with whoever designed the existing API.
What's the best way to move this forward?

Regards,

Vinay Sajip