[cfe-dev] how to get array size from an array decl ?

Friedman, Eli via cfe-dev cfe-dev at lists.llvm.org
Mon Oct 22 13:32:03 PDT 2018


On 10/19/2018 2:04 AM, illiop via cfe-dev wrote:
> Hello,
> if the program is :
> void foo()
> {
>    int a[3] = {0,1,2};
> }
> If I get the ast nodes by the ast matcher: varDecl(hasType(arrayType()))
> I will get  int a[3] matched, the matched node is a VarDecl type.
> How can I get the array size (in this example is 3) from the VarDecl 
> node ?

getType() on the VarDecl returns a QualType.  Call 
ASTContext::getAsConstantArrayType to convert that to a 
ConstantArrayType*.  Call ConstantArrayType::getSize() to return the size.

If you need access to the original Expr*, as opposed to the computed 
size, you might need to dig through the TypeSourceInfo instead.

-Eli

-- 
Employee of Qualcomm Innovation Center, Inc.
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20181022/30a9a71c/attachment.html>


More information about the cfe-dev mailing list