[cfe-dev] ASTvisitor extract only variable definitions in Function definitions only

Georgiou, Andreas via cfe-dev cfe-dev at lists.llvm.org
Wed Jul 13 17:18:25 PDT 2016


Hi all,

Here is my problem. I have implemented an AST recursive visitor and I want to extract the variable definitions in Function bodies/definitions only. Here is my AST visitor (part of):

bool VisitFunctionDecl(FunctionDecl *func)
    {
 if(astContext->getSourceManager().isInMainFile(func->getLocStart()) && func->isThisDeclarationADefinition()) //checks if the node is in the main (input) file and a function definition
 {
  FullSourceLoc FullLocation = astContext->getFullLoc(func->getLocStart());
  string funcName = func->getNameInfo().getName().getAsString();
  string funcType = func->getResultType().getAsString();
  string srcFunc = filename + "_" + funcName;
  REPORT << "[" << FullLocation.getSpellingLineNumber() << "," << FullLocation.getSpellingColumnNumber() << "]Function Declaration: " << funcName << " of type " << funcType << "\n";
  if (append == 0 && numFunctions == 0)
 APIs << srcFunc <<":";
  else
 APIs << "\n" << srcFunc <<":";
  APIs  <<funcType << ";";
  numFunctions++;
  }
  return true;
    }

    bool VisitVarDecl(VarDecl *var)
    {
if (astContext->getSourceManager().isInMainFile(var->getLocStart())) //checks if the node is in the main = input file.
{
if((var->hasLocalStorage() || var->isStaticLocal ()) && !var->isDefinedOutsideFunctionOrMethod () )
{
FullSourceLoc FullLocation = astContext->getFullLoc(var->getLocStart());
//var->dump(); //prints the corresponding line of the AST.
numVariables++;
string varName = var->getQualifiedNameAsString();
string varType = var->getType().getAsString();
REPORT << "[" << FullLocation.getSpellingLineNumber() << "," << FullLocation.getSpellingColumnNumber() << "]Variable Declaration: " << varName << " of type " << varType << "\n";
APIs << varType << ";";
}
}
        return true;
    }


As you can see I want to extract some information from the ASTnodes BUT there are some constraints:
1) I want only the function definition to be extracted and their variables
2) Not variables defines outside any function

For example when I parse the AST for the following code I get this result which is wrong:
static int LZWDecode(TIFF*, tidata_t, tsize_t, tsample_t);

As I have understand this is a function declaration. With my constraints my AST visitor will not print me the messages or extract the function name etc BUT will extract its variable definitions. The result will be something like
Variable Declaration: <anonymous> of type TIFF *

What can i do ? Is something to skip the hole "function declaration node" if it doesn't meet my requirements??
Thank you all

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


More information about the cfe-dev mailing list