当前位置: 首页 > news >正文

2312clang,基于访问者的前端动作

原文

基于RecursiveASTVisitorASTFrontendActions.

创建用RecursiveASTVisitor查找特定名字的CXXRecordDeclAST节点的FrontendAction.

创建FrontendAction

编写基于clang的工具(如Clang插件或基于LibTooling的独立工具)时,常见入口是允许在编译过程中执行用户特定操作的FrontendAction接口.

为了在ASTclang上运行工具,提供了方便的负责执行操作的ASTFrontendAction接口.你只需要实现对每个转换单元返回一个ASTConsumerCreateASTConsumer方法.

class FindNamedClassAction : public clang::ASTFrontendAction {
public:virtual std::unique_ptr<clang::ASTConsumer> CreateASTConsumer(clang::CompilerInstance &Compiler, llvm::StringRef InFile) {return std::make_unique<FindNamedClassConsumer>();}//FindNamedClassAction
};

创建ASTConsumer

ASTConsumer是一个,不管如何生成的AST,在AST编写的通用操作接口.ASTConsumer提供了许多不同的入口,但在此,只需要用ASTContext调用翻译单元HandleTranslationUnit.

class FindNamedClassConsumer : public clang::ASTConsumer {
public:virtual void HandleTranslationUnit(clang::ASTContext &Context) {//通过`RecursiveASTVisitor`遍历翻译单元声明,会访问`AST`中的所有节点.Visitor.TraverseDecl(Context.getTranslationUnitDecl());}
private://`RecursiveASTVisitor`实现.FindNamedClassVisitor Visitor;
};

使用RecursiveASTVisitor

现在已连接了,下一步是实现RecursiveASTVisitor以从AST中提取相关信息.

除了按值传递的TypeLoc节点,RecursiveASTVisitor为大多数AST节点提供bool VisitNodeType(NodeType*)形式的勾挂.只需要为相关节点类型实现方法,就可以了.
首先编写一个访问所有CXXRecordDeclRecursiveASTVisitor.

class FindNamedClassVisitor: public RecursiveASTVisitor<FindNamedClassVisitor> {
public:bool VisitCXXRecordDecl(CXXRecordDecl *Declaration) {//为了调试,转储`AST`节点,会显示已访问的节点.Declaration->dump();//返回值指示是否想继续访问.返回`假`以停止`AST`的遍历.return true;}
};

RecursiveASTVisitor的方法中,现在可用ClangAST全部功能来深入感兴趣部分.如,要查找带特定名字的所有类声明,可检查全名:

bool VisitCXXRecordDecl(CXXRecordDecl *Declaration) {if (Declaration->getQualifiedNameAsString() == "n::m::C")Declaration->dump();return true;
}

访问SourceManagerASTContext

有关AST的某些信息(如源位置和全局标识信息)不在AST节点自身中,而是在ASTContext及其关联的源管理器中存储.

要提取它们,需要传递ASTContextRecursiveASTVisitor实现中.

调用CreateASTConsumer时,CompilerInstance可访问ASTContext.因此,可从那里提取,并把它交给新创建的FindNamedClassConsumer:

virtual std::unique_ptr<clang::ASTConsumer> CreateASTConsumer(clang::CompilerInstance &Compiler, llvm::StringRef InFile) {return std::make_unique<FindNamedClassConsumer>(&Compiler.getASTContext());
}

现在在RecursiveASTVisitor中,可访问ASTContext,可利用AST节点,查找源位置等:

bool VisitCXXRecordDecl(CXXRecordDecl *Declaration) {if (Declaration->getQualifiedNameAsString() == "n::m::C") {//`getFullLoc`使用`ASTContext`的`SourceManager`来解析源位置,并分解为`行和列`部分.FullSourceLoc FullLocation = Context->getFullLoc(Declaration->getBeginLoc());if (FullLocation.isValid())llvm::outs() << "Found declaration at "<< FullLocation.getSpellingLineNumber() << ":"<< FullLocation.getSpellingColumnNumber() << "\n";}return true;
}

组合在一起

如下:

#include "clang/AST/ASTConsumer.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendAction.h"
#include "clang/Tooling/Tooling.h"
using namespace clang;
class FindNamedClassVisitor: public RecursiveASTVisitor<FindNamedClassVisitor> {
public:explicit FindNamedClassVisitor(ASTContext *Context): Context(Context) {}bool VisitCXXRecordDecl(CXXRecordDecl *Declaration) {if (Declaration->getQualifiedNameAsString() == "n::m::C") {FullSourceLoc FullLocation = Context->getFullLoc(Declaration->getBeginLoc());if (FullLocation.isValid())llvm::outs() << "Found declaration at "<< FullLocation.getSpellingLineNumber() << ":"<< FullLocation.getSpellingColumnNumber() << "\n";}return true;}
private:ASTContext *Context;
};
class FindNamedClassConsumer : public clang::ASTConsumer {
public:explicit FindNamedClassConsumer(ASTContext *Context): Visitor(Context) {}virtual void HandleTranslationUnit(clang::ASTContext &Context) {Visitor.TraverseDecl(Context.getTranslationUnitDecl());}
private:FindNamedClassVisitor Visitor;
};
class FindNamedClassAction : public clang::ASTFrontendAction {
public:virtual std::unique_ptr<clang::ASTConsumer> CreateASTConsumer(clang::CompilerInstance &Compiler, llvm::StringRef InFile) {return std::make_unique<FindNamedClassConsumer>(&Compiler.getASTContext());}
};
int main(int argc, char **argv) {if (argc > 1) {clang::tooling::runToolOnCode(std::make_unique<FindNamedClassAction>(), argv[1]);}
}

FindClassDecls.cpp文件中存储它,并创建以下CMakeLists.txt来链接它:

set(LLVM_LINK_COMPONENTSSupport)
add_clang_executable(find-class-decls FindClassDecls.cpp)
target_link_libraries(find-class-declsPRIVATEclangASTclangBasicclangFrontendclangSerializationclangTooling)

对代码片运行此工具时,输出找到的n::m::C类的所有声明:

$ ./bin/find-class-decls "namespace n { namespace m { class C {}; } }"

1:29找到声明

http://www.lryc.cn/news/267292.html

相关文章:

  • 怎么搭建实时渲染云传输服务器
  • 如何在生产环境正确使用Redis
  • LeetCode-环形链表问题
  • C# 读取Word表格到DataSet
  • 构建外卖系统:从技术到实战
  • 城市之眼:数据可视化在智慧城市的角色
  • Nature | Baker团队用AI设计出史上最高互作强度的蛋白质
  • C# 初识System.IO.Pipelines
  • 嵌入式——RTC内置实时时钟
  • nodejs微信小程序+python+PHP的热带野生动物园景点预约订票系统的设计与实现-计算机毕业设计推荐
  • ASP.NET MVC的5种AuthorizationFilter
  • C语言初学8:函数和作用域
  • 2024年科技盛宴“上海智博会·上海软博会”招商工作接近尾声
  • 深圳锐科达SIP矿用电话模块SV-2801VP
  • 【Qt-数据库】
  • windows文件名命名规范(文件名规范、命名规则、避免特殊字符、注意文件名长度限制260个字符)
  • 如何修改MySQL的默认端口
  • Android笔记(二十一):Room组件实现Android应用的持久化处理
  • uniapp中各种状态的按钮
  • 模式识别与机器学习-判别式分类器
  • c++11 标准模板(STL)(std::pair)(七)访问 pair 的一个元素
  • IP 地址归属地查询
  • 实战经验分享:在Java中灵活应用Excel注释和批注
  • AUTOSAR从入门到精通-车载以太网(三)
  • 【自然语言处理】用Python从文本中删除个人信息-第二部分
  • 设计模式之-中介者模式,快速掌握中介者模式,通俗易懂的讲解中介者模式以及它的使用场景
  • 12.25
  • MySQL5.7的几种安装方式总结(排错踩坑呕心沥血的经历)
  • zookeeper基本使用
  • 【华为机试】2023年真题B卷(python)-分月饼