Recovering extent expressions

Consider the program:

#define m 1
#define n 2

int a[m*n];
int main() {
return a[0];
}

How can I determine whether or not an array extent is an expression of macro constants, and how can I retrieve this expression?
During AST traversal, I encounter a VarDecl whose type is ConstantArrayType, but the size of this type has already been reduced to single integer.

Look at the TypeSourceInfo of the VarDecl. It contains (through the use of TypeLoc) the expression describing the array. At that point, you'll see that the location of the integer literals is a macro instantiation, and can use the source manager to trace it back. It's not trivial, but it's doable.

  - Doug

Consider the program:

#define m 1
#define n 2

int a[m*n];
int main() {
  return a[0];
}

How can I determine whether or not an array extent is an expression of macro constants, and how can I retrieve this expression?
During AST traversal, I encounter a VarDecl whose type is ConstantArrayType, but the size of this type has already been reduced to single integer.

Look at the TypeSourceInfo of the VarDecl. It contains (through the use of TypeLoc) the expression describing the array. At that point, you'll see that the location of the integer literals is a macro instantiation, and can use the source manager to trace it back. It's not trivial, but it's doable.

What would you do if it was not macro and a was declared as a[1*2] ? It seems you are relying on macros to get the info. that is not generally
available in clang.

- Fariborz