Dear cfe-list,
the attached patch is a proposal for a minor change in class ConstantArrayType.
The change is motivated by clients that need to be able to investigate the syntactic structure of the array size expression, for instance, to keep track of the use of magic constant numbers. This is not possible with the current AST structure (unless one operates before Sema ...).
To this end, we propose the addition of a new private data member to the class ConstantArrayType
Expr* OrigSizeExpr;
_optionally_ referring to the "original non-canonical array size expression". The value of the Expr* can be queried by clients using public method
Expr* ConstantArrayType::getOriginalSizeExpr() const;
This Expr pointer will be 0 whenever the size expression is "canonical": namely, when the array size expression is just an integer literal expressed in decimal notation and having no suffix at all.
On the one end, the client has no actual need to investigate such a canonical array size expression (since it is completely determined by the usual getSize); on the other hand, having a null pointer for this frequently occurring case will preserve the ability of making most of the occurrences of the same array type unique.
This comes at the cost to lose the ability to reach the original array size expression and its program points if the array size is canonical, but we thought it was a reasonable tradeoff; please give some feedback if you would prefer otherwise.
Hence, a constant array type will be canonical iff its element type is canonical _and_ its array size expression is canonical (i.e., the Expr* is null). For example, int[4] will be canonical and different from int[sizeof(int)], int[2+2], and int[0x04], which are all non-canonical.
Please, let us know if we are missing something important.
Note also that we haven't modified the clang AST pretty printer to show this info instead of the computed size, because we don't know whether or not this is the desired behaviour.
As a side note, we observed that the array size in ConstantArrayType is one of the few places where the original syntax of the program was replaced and made no longer available in the AST. In our application we generally need to be able to recover the original syntax (for instance, we appreciated very much the existence of class OriginalParmVarDecl). So, if you know of other places where such a replacement is performed, please let us know ... and consider if it would be possible to avoid it ... that would be very helpful for our kind of clients.
If this patch will be accepted, we'll follow with another patch to discriminate between the case when OrigSizeExpr == 0 due to the expression being canonical, with respect to the case when OrigSizeExpr == 0 due to a missing size expression that gets filled in in Sema (e.g., int a = {2,3})
(see http://lists.cs.uiuc.edu/pipermail/cfe-dev/2009-February/004426.html).
Cheers,
Abramo Bagnara and Enea Zaffanella.
ConstantArrayType.diff (10.7 KB)