[cfe-dev] Adding access to array size expressions in ConstantArrayType.

Enea Zaffanella zaffanella at cs.unipr.it
Wed Jun 24 02:45:28 PDT 2009


Chris Lattner wrote:
> On Jun 15, 2009, at 1:55 PM, Enea Zaffanella wrote:
>> Overall, there are 1072 array type declarations. Of these, 588 have a
>> decimal unsuffixed integer literal as size expression; some other 72
>> have a single macro name as size expression, which might be expanded to
>> such a "canonical" integer constant.
>>
>> All considered, you are basically right in saying that the optimization
>> is not worth the effort. So, if you say it is OK, we will proceed as you
>> suggested and add the original size expression pointer unconditionally.
> 
> That makes sense to me.  One other request: please make the 
> non-canonical ConstantArrayType and the canonical ConstantArrayType have 
> a different class name.  The canonical one should be our current 
> ConstantArrayType that has an APInt for the size.  I'd recommend naming 
> the new one ConstantArrayWithExprType or something like that.  While 
> you're at it, you can put SourceLocation's for the [ and ] in there too :)
> 
> -Chris
> 
> 

Here we are with the patch.

We have added _two_ new classes deriving from ConstantArraySize:
   - ConstantArraySizeWithExpr
   - ConstantArraySizeWithoutExpr

The latter is for constructs such as
   int a[] = { 0, 1, 2, 3 ];
   char s[] = "A string";
that the parser initially builds as IncompleteArrayType objects and Sema 
later transforms to ConstantArrayWithoutExpr objects.

Regarding pretty-printing, we have added a flag in PrintingPolicy 
(ConstantArraySizeAsWritten, defaulting to false) that guides methods 
getAsStringInternal.

We have an issue we do not know how to solve, but we believe should be 
routine for someone knowing more about clang internals (there are a 
couple of FIXME in the patch). The problem is in SemaDecl.cpp:

static QualType
TryToFixInvalidVariablyModifiedType(QualType T,
                                     ASTContext &Context,
                                     bool &SizeIsNegative)

Here we need to transform a VariableArray into a ConstantArrayWithExpr. 
To avoid resource ownership issues, we should either steal or clone the 
VLA size expression and put it in the ConstantArrayWithExpr.
Since we do not know how to do it, in the proposed patch we simply copy 
the SizeExpr pointer as is ... leading to a shared resource. To avoid 
the double-deletion problem, we do *not* delete it in the destructor of 
ConstantArrayWithExpr.

The patch passes all of the clang tests
(in debugging mode; 14 expected failures).

Regarding the addition of SourceLocation's for the array brackets: we 
haven't implemented it (yet). I guess that, in order to do it, we should 
change something in the parsing process (i.e., before the AST build 
process) so as to gather and propagate the required info.
Could you please provide us with a hint regarding the portions of code 
we should modify?

Cheers,
Enea.

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: ConstantArrayWithExpr.patch
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20090624/f6e292b6/attachment.ksh>


More information about the cfe-dev mailing list