2016-07-10 26 views
1

我開發了一個使用Clang libtooling的AST訪問器,我想區分函數原型和函數聲明。我的AST訪問者將這兩種情況作爲函數聲明。下面你可以看到我的代碼訪問函數聲明:如何在Clang 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()不能這兩件事情之間的區別。有任何想法嗎??

+0

是什麼讓你覺得這是兩回事?你能舉一些例子嗎?或者你的意思是函數聲明和函數定義? –

+0

是的,我的意思是區分函數定義和聲明。 –

+1

然後請更新問題。 –

回答

1

使用FunctionDecl::isThisDeclarationADefinition()