【问题标题】:How to distinguish function definitions and function declarations in Clang AST visitors如何区分 Clang AST 访问者中的函数定义和函数声明
【发布时间】:2016-07-10 19:05:10
【问题描述】:

我使用 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() 无法区分这两件事。有什么想法吗??

【问题讨论】:

  • 是什么让你认为这是两个不同的东西?你能举一些例子吗?还是你的意思是函数声明和函数定义?
  • 是的,我的意思是区分函数定义和声明。
  • 那么请更新问题。

标签: c++ clang abstract-syntax-tree llvm-clang


【解决方案1】:

使用FunctionDecl::isThisDeclarationADefinition()。

【讨论】:

    猜你喜欢
    • 2016-10-21
    • 1970-01-01
    • 2020-03-21
    • 1970-01-01
    • 2012-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-31
    相关资源
    最近更新 更多