Clang AST 访问者,避免遍历包含文件 [英] Clang AST visitor, avoid traversing include files

查看:45
本文介绍了Clang AST 访问者,避免遍历包含文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在尝试实现 AST Clang 访问者,这是我的代码.

Hello I am trying to implement an AST Clang visitor and this is my code.

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 << "
";
        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 << "
";
        APIs << "

" << 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 << "
";
        }
        return true;
    }
};

如何避免遍历包含的头文件,但又不丢失它们的信息.我只是不想打印有关此文件的任何信息,但我希望 clang 了解这些文件

How can I avoid traversing the included header files, but without loosing their information. I just dont want to print any information about this files but I want clang to know about these files

谢谢

推荐答案

通过使用 AST 上下文,您可以获得正在解析的代码的所有必要信息.区分AST节点在主文件或头文件中的函数称为isInMainFile(),可以如下使用.

By using AST context you can get all the nescecarry information for the code you are parsing. The function which distinguish between AST nodes being in the main file or the header files is called isInMainFile() and can be used as follows.

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 << "
";
            APIs << varType << ",";
        }
    }
    return true;
}

有关如何使用 astContext 的更多信息,请参阅 clang 网站上的官方递归 ASTvisitor 教程.

For more information on how to use astContext follow the official recursive ASTvisitor tutorial in clang website.

这篇关于Clang AST 访问者,避免遍历包含文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆