clang ast访问者,避免遍历文件
Clang AST visitor, avoid traversing include files
你好,我正在尝试实现AST Clang访问者,这是我的代码。
class ExampleVisitor : public RecursiveASTVisitor<ExampleVisitor> {
private:
ASTContext *astContext; // used for getting additional AST info
public:
virtual bool VisitVarDecl(VarDecl *var)
{
numVariables++;
string varName = var->getQualifiedNameAsString();
string varType = var->getType().getAsString();
cout << "Found variable declaration: " << varName << " of type " << varType << "n";
APIs << varType << ", ";
return true;
}
virtual bool VisitFunctionDecl(FunctionDecl *func)
{
numFunctions++;
string funcName = func->getNameInfo().getName().getAsString();
string funcType = func->getResultType().getAsString();
cout << "Found function declaration: " << funcName << " of type " << funcType << "n";
APIs << "nn" << funcName <<": ";
APIs << funcType << ", ";
return true;
}
virtual bool VisitStmt(Stmt *st)
{
if (CallExpr *call = dyn_cast<CallExpr>(st))
{
numFuncCalls++;
FunctionDecl *func_decl = call->getDirectCallee();
string funcCall = func_decl->getNameInfo().getName().getAsString();
cout << "Found function call: " << funcCall << " with arguments ";
APIs << funcCall << ", ";
for(int i=0, j = call->getNumArgs(); i<j; i++)
{
string TypeS;
raw_string_ostream s(TypeS);
call->getArg(i)->printPretty(s, 0, Policy);
cout<< s.str() << ", ";
APIs<< s.str() << ", ";
}
cout << "n";
}
return true;
}
};
我如何避免穿越随附的标头文件,而不会丢失信息。我只是不想打印有关此文件的任何信息,但我希望Clang了解这些文件
谢谢
通过使用AST上下文,您可以获取您正在解析的代码的所有NesceCarry信息。区分主文件或标题文件中的AST节点的函数称为isInmainFile(),可以使用如下。
bool VisitVarDecl(VarDecl *var)
{
if (astContext->getSourceManager().isInMainFile(var->getLocStart())) //checks if the node is in the main = input file.
{
if(var->hasLocalStorage() || var->isStaticLocal())
{
//var->dump(); //prints the corresponding line of the AST.
FullSourceLoc FullLocation = astContext->getFullLoc(var->getLocStart());
numVariables++;
string varName = var->getQualifiedNameAsString();
string varType = var->getType().getAsString();
REPORT << "Variable Declaration [" << FullLocation.getSpellingLineNumber() << "," << FullLocation.getSpellingColumnNumber() << "]: " << varName << " of type " << varType << "n";
APIs << varType << ",";
}
}
return true;
}
有关如何使用AstContext的更多信息,请遵循Clang网站上的官方递归Astvisitor教程。
相关文章:
- C++RapidXml-使用first_node()遍历以修改XML文件中节点的值
- 遍历顺序由 std::文件系统directory_iterator给出
- 如何遍历几个每小时一次的根(.root)文件,并将它们组合成更大的每日数据.root文件?
- 遍历对象向量,并找到与从文本文件中提取的对象匹配的变量
- 我该如何循环遍历我的数组(缓冲区——包含一个文本文件),并将其打印成30字节的块
- 从带有 std::ifstream::read() 的文件中读取 int 遍历 char * 二进制数据
- 使用 stdin 读取文本文件,然后遍历多个数组
- 是否可以遍历文本文件行并使用字符串流来解析每一行
- clang ast访问者,避免遍历文件
- 遍历指定文件夹的目录?Qt 和 c++
- 在qt中遍历文件夹中的文件非常慢
- 遍历文件夹中的文件(C++)
- 垂直遍历文件
- 遍历文件时跳过最后K行
- 在不遍历文件内容的情况下从文件中获取信息
- 在Makefile中循环遍历文件
- 文件输入没有遍历文件
- 如何遍历文件中的一行
- c++ getline()在文件行中读取,无限循环遍历文件
- 在c++中遍历文件中的所有字符