如何区分 Clang AST 访问者中的函数定义和函数声明
How to distinguish function definitions and function declarations in Clang AST visitors
我使用 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->hasBody() 无法区分这两者。有什么想法吗??
使用FunctionDecl::isThisDeclarationADefinition()
.
我使用 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->hasBody() 无法区分这两者。有什么想法吗??
使用FunctionDecl::isThisDeclarationADefinition()
.