[cfe-dev] Number of elements in ArrayType?

Csaba Dabis via cfe-dev cfe-dev at lists.llvm.org
Tue Oct 29 11:41:40 PDT 2019


Erich has pointed out the hierarchy of array types [1], where a
`ConstantArrayType` has such a method `getSize()` to obtain the
concrete size directly. However, most of the time you are able to
obtain the `VariableArrayType`'s size, being a `DeclRefExpr` to a
constant integer. My solution obtains the size expression and let you
decide what you want to do and how generic your solution needs to be.
I have not worked with `DependentSizedArrayType` yet.

[1] https://clang.llvm.org/doxygen/classclang_1_1ArrayType.html

On Tue, Oct 29, 2019 at 7:20 PM Kenth Eriksson
<Kenth.Eriksson at infinera.com> wrote:
>
> On Tue, 2019-10-29 at 15:03 +0100, Csaba Dabis wrote:
> >
> > Assume that you have the desired `VarDecl` `D`:
> > ```
> >        const Expr *SizeExpr = nullptr;
> >         if (D->getType()->isArrayType())
> >           if (const TypeSourceInfo *TSI = D->getTypeSourceInfo())
> >             if (const auto ATL = TSI->getTypeLoc().getAs<ArrayTypeLoc>())
> >               SizeExpr = ATL.getSizeExpr();
> > ```
> >
> Looks as it can be done like this (arr is ConstantArrayType)
>
> arr->getSize().getZExtValue()
>
> Is my understanding correct?



More information about the cfe-dev mailing list