我使用Clang libtooling开发了一个AST访问者,我想区分函数原型和函数声明。我的AST访问者将这两种情况都作为函数声明。下面你可以看到我访问函数声明的代码:
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-&gt; hasBody()无法区分这两件事。任何想法??
答案 0 :(得分:1)
使用FunctionDecl::isThisDeclarationADefinition()
。