Search code examples
c++clangabstract-syntax-treellvm-clang

How to distinguish function definitions and function declarations in Clang AST visitors


I have developed an AST visitor using Clang libtooling and I would like to distinguish between function prototypes and function declarations. My AST visitor takes both cases as function declarations. Below you can see my code for visiting function declarations:

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

func->hasBody() cannot distinguish between those two things. Any ideas??


Solution

  • Use FunctionDecl::isThisDeclarationADefinition().