[cfe-dev] Check if a variable declaration is an array and then get its type and dimensions

Georgiou, Andreas via cfe-dev cfe-dev at lists.llvm.org
Thu Jul 21 18:30:09 PDT 2016


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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20160722/b25a5fd2/attachment.html>


More information about the cfe-dev mailing list