Check if a variable declaration is an array and then get its type and dimensions

Hi all,

I am trying to determine whether a variable declaration in ASTvisitor is an array and if is an array I want to determine the number of dimensions of the array. Below you can find my code.

bool VisitVarDecl(VarDecl *var)
{
if (astContext->getSourceManager().isInMainFile(var->getLocStart())) //checks if the node is in the main = input file.
{
FullSourceLoc FullLocation = astContext->getFullLoc(var->getLocStart());
if((var->hasLocalStorage() || var->isStaticLocal ()))
{
if (!var->isDefinedOutsideFunctionOrMethod())
{
if(avoid == 0)
{
numVariables++;
string varName = var->getQualifiedNameAsString();
string varType = var->getType().getAsString();
const Type *type = var->getType().getTypePtr();
if(type->isConstantArrayType())
{
const ArrayType *Array = type->castAsArrayTypeUnsafe();
cout << "Is array of type: " << Array->getElementType().getAsString() << endl;
}
REPORT << “[” << FullLocation.getSpellingLineNumber() << “,” << FullLocation.getSpellingColumnNumber() << "]Variable Declaration: " << varName << " of type " << varType << “\n”;
APIs << varType << “;”;
}
else
{
avoid–;
REPORT << "Avoid is: " << avoid << endl;
}
}
}
}
return true;
}

I don’t know if I have correctly did the “casting” from VarDecl to ArrayType. If you have a better, safer, and less sloppy way to do it please am open to any opinions.

Furthermore my main problem right now is how to get the number of dimensions of the array and even its size in terms of cells.

Thank you all.