![](/img/trans.png)
[英]Should helper functions be excluded from header file declarations? (C++)
[英]Extracting function declarations from a C/C++ header file using clang
我有以下遞歸AST訪問者實現。
class ExampleVisitor : public clang::RecursiveASTVisitor<ExampleVisitor>{
private:
clang::ASTContext* ast_context_; // used for getting additional AST info
public:
explicit ExampleVisitor(clang::CompilerInstance* ci)
: ast_context_(&(ci->getASTContext())) // initialize private members
virtual bool VisitFunctionDecl(clang::FunctionDecl* func)
{
numFunctions++;
foo(func);
return true;
}};
函數foo打印給定輸入文件的已聲明函數的名稱。
在此實現中,foo打印輸出文件中聲明的函數,並轉儲包含的頭文件中的所有函數聲明。 如何修改此代碼以僅打印在給定輸入文件中聲明的功能?
嘗試使用SourceManager來確定FunctionDecl是否在轉換單元的主文件中:
virtual bool VisitFunctionDecl(clang::FunctionDecl* func)
{
clang::SourceManager &sm(ast_context_->getSourceManager());
bool const inMainFile(
sm.isInMainFile(sm.getExpansionLoc(func->getLocStart())));
if(inMainFile){
numFunctions++;
foo(func);
}
else{
std::cout << "Skipping '" << func->getNameAsString()
<< "' not in main file\n";
}
return true;
}};
我碰巧知道有一個名為isExpansionInMainFile
的AST Matcher。 我從源代碼中在cfe-3.9.0.src / include / clang / ASTMatchers / ASTMatchers.h第209-14行中獲得了上面的代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.