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

宜春做网站网站点击量 哪里查询

宜春做网站,网站点击量 哪里查询,哪儿能做邯郸网站建设,深圳影视广告公司原文 基于RecursiveASTVisitor的ASTFrontendActions. 创建用RecursiveASTVisitor查找特定名字的CXXRecordDeclAST节点的FrontendAction. 创建FrontendAction 编写基于clang的工具(如Clang插件或基于LibTooling的独立工具)时,常见入口是允许在编译过程中执行用户特定操作的F…

原文

基于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.tj-hxxt.cn/news/26707.html

相关文章:

  • 公司产品网站谷歌海外推广
  • 弹窗网站制作器现在最好的免费的建站平台
  • 公司网站彩页怎么做软文免费发布平台
  • 2008iis搭建网站百度关键词排名怎么做
  • 做电销哪些网站可以找到客户端今天的新闻主要内容
  • 有哪些网站可以做简历热点新闻事件及评论
  • 丰台建站公司网络推广是什么专业
  • 武威网站建设优化网站视频
  • 自做淘宝客网站seo关键词优化推广
  • 免费独立网站建设交换友情链接前后必须要注意的几点
  • 网站怎么做长尾词seo运营是做什么的
  • 视频投票网站怎么做软文广告营销
  • 网站快速备案多少钱电子制作网站
  • 做系统下载网站建设百度快速收录技术
  • 网站开发合同范本渠道网官网
  • WordPress付费阅读文章seo完整教程视频教程
  • 做独立网站需要注意什么手续代推广app下载
  • 深圳图派做的网站后台加什么营销战略
  • 做简历的网站叫什么软件一键优化下载安装
  • 恋爱网站建设网络营销是以什么为中心
  • 12306网站的建设历程网络营销成功的原因
  • wordpress主题转typechoseo综合查询是什么
  • 网页设计素材收集关键词优化是怎样收费的
  • 自己电脑做网站教程浅谈一下网络营销的几个误区
  • 建立一个做笔记的网站免费b站推广网站有哪些
  • 源代码如何做网站软文推广的好处
  • 自己做的网页怎么连接到网站网站推广上首页
  • 建设网站分析如何制作自己的网页链接
  • wordpress表单设计seo流量增加软件
  • 简历制作免费模板网站网络最有效的推广方法