clang frontEnd插件 常见Traverse函数、Visit函数 例子

来自 列举Traverse*, 给出Traverse*、Visit*出处 , 本地私服, 该文件内容如下:

#ifndef VarVst_H
#define VarVst_H


#include <clang/Rewrite/Core/Rewriter.h>
#include <set>
#include <clang/Frontend/CompilerInstance.h>
#include <unordered_set>
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/AST/Stmt.h"
#include "clang/Basic/SourceManager.h"
#include "base/LocId.h"

using namespace llvm;
using namespace clang;

/**
 * Visitor: 插入VarBE
 */
class VarVst
        : public RecursiveASTVisitor<VarVst> {
public:
public:
    //Rewriter:4:  Consumer将Rewriter传递给Visitor
    explicit VarVst(CompilerInstance &CI, const std::shared_ptr<Rewriter> rewriter_ptr, ASTContext *Ctx, SourceManager& SM, LangOptions &_langOptions)
    //Rewriter:5:  Consumer将Rewriter传递给Visitor, 并由Visitor.mRewriter接收
    : mRewriter_ptr(rewriter_ptr),
    Ctx(Ctx),
    CI(CI),
    SM(SM)
    {

    }



    void letLRBraceWrapRangeAftBf(SourceLocation B, SourceLocation E, const char* whoInserted="");
    void letLRBraceWrapStmtBfAfTk(Stmt *stmt, const char* whoInserted="");

    //   RecursiveASTVisitor_H="/app/llvm_release_home/clang+llvm-15.0.0-x86_64-linux-gnu-rhel-8.4/include/clang/AST/RecursiveASTVisitor.h"
    // 该函数默认空实现: grep 'DEF_TRAVERSE_STMT(CompoundStmt'  $RecursiveASTVisitor_H
    virtual bool TraverseCompoundStmt(CompoundStmt *compoundStmt );

    // 该函数默认空实现: grep 'DEF_TRAVERSE_STMT(IfStmt'  $RecursiveASTVisitor_H
    virtual bool TraverseIfStmt(IfStmt *ifStmt);

    // 该函数默认空实现: grep 'DEF_TRAVERSE_STMT(WhileStmt'  $RecursiveASTVisitor_H
    virtual bool TraverseWhileStmt(WhileStmt *whileStmt);

    //forEach和for很相似
    // 该函数默认空实现: grep 'DEF_TRAVERSE_STMT(ForStmt'  $RecursiveASTVisitor_H
    virtual bool TraverseForStmt(ForStmt *forStmt);
    // 该函数默认空实现: grep 'DEF_TRAVERSE_STMT(CXXTryStmt'  $RecursiveASTVisitor_H
    virtual bool TraverseCXXTryStmt(CXXTryStmt *cxxTryStmt);
    // 该函数默认实现: grep -A3  'DEF_TRAVERSE_STMT(CXXCatchStmt'  $RecursiveASTVisitor_H
    virtual bool TraverseCXXCatchStmt(CXXCatchStmt *cxxCatchStmt);
    // 该函数默认空实现: grep 'DEF_TRAVERSE_STMT(DoStmt'  $RecursiveASTVisitor_H
    virtual bool TraverseDoStmt(DoStmt *doStmt);
    // 该函数默认实现: grep -A10 'DEF_TRAVERSE_STMT(CXXForRangeStmt'  $RecursiveASTVisitor_H
    virtual bool TraverseCXXForRangeStmt(CXXForRangeStmt *forRangeStmt);

    // 该函数默认空实现: grep 'DEF_TRAVERSE_STMT(SwitchStmt'  $RecursiveASTVisitor_H
    virtual bool TraverseSwitchStmt(SwitchStmt *switchStmt);
    // 该函数默认空实现: grep 'DEF_TRAVERSE_STMT(CaseStmt'  $RecursiveASTVisitor_H
    virtual bool TraverseCaseStmt(CaseStmt *caseStmt);

    // 该函数默认实现: grep -A6 'DEF_TRAVERSE_DECL(FunctionDecl'  $RecursiveASTVisitor_H
    virtual bool TraverseFunctionDecl(FunctionDecl* funcDecl);
    /** CXXConstructorDecl默认遍历器: grep -A6 'DEF_TRAVERSE_DECL(CXXConstructorDecl'  $RecursiveASTVisitor_H
     bool TraverseCXXConstructorDecl(CXXConstructorDecl* cxxCnstrDecl){
     ShouldVisitChildren = false;//不访问子节点
     ReturnValue = TraverseFunctionHelper(D);
    }
     */
    virtual bool TraverseCXXConstructorDecl(CXXConstructorDecl* cxxCnstrDecl);
    // 该函数默认实现: grep -A38 'DEF_TRAVERSE_STMT(LambdaExpr'  $RecursiveASTVisitor_H
    virtual bool TraverseLambdaExpr(LambdaExpr *lambdaExpr);
    // 该函数默认实现: grep  -A6   'DEF_TRAVERSE_DECL(CXXDestructorDecl'  $RecursiveASTVisitor_H
    virtual bool TraverseCXXDestructorDecl(CXXDestructorDecl * cxxDestructorDecl);

    /**
'#define STMT'下1行有'bool Traverse##CLASS'下1行'#include "clang/AST/StmtNodes.inc"'

python /app/bash-simplify/pyFileReFindAllDotAsAll.py  '#define STMT(?:.*\n){1}  bool Traverse##CLASS(?:.*\n)#include "clang/AST/StmtNodes.inc"\n'  "$RecursiveASTVisitor_H"

文件/app/llvm_release_home/clang+llvm-15.0.0-x86_64-linux-gnu-rhel-8.4/include/clang/AST/RecursiveASTVisitor.h 匹配行号 行372~行375
 #define STMT(CLASS, PARENT) \
  bool Traverse##CLASS(CLASS *S, DataRecursionQueue *Queue = nullptr);
#include "clang/AST/StmtNodes.inc"


'#define STMT'下6行有'bool Visit##CLASS'下1行'#include "clang/AST/StmtNodes.inc"'

python /app/bash-simplify/pyFileReFindAllDotAsAll.py  '#define STMT(?:.*\n){6}  bool Visit##CLASS(?:.*\n){1}#include "clang/AST/StmtNodes.inc"\n'   "$RecursiveASTVisitor_H"

文件/app/llvm_release_home/clang+llvm-15.0.0-x86_64-linux-gnu-rhel-8.4/include/clang/AST/RecursiveASTVisitor.h 匹配行号 行380~行388
 #define STMT(CLASS, PARENT)                                                    \
  bool WalkUpFrom##CLASS(CLASS *S) {                                           \
    TRY_TO(WalkUpFrom##PARENT(S));                                             \
    TRY_TO(Visit##CLASS(S));                                                   \
    return true;                                                               \
  }                                                                            \
  bool Visit##CLASS(CLASS *S) { return true; }
#include "clang/AST/StmtNodes.inc"
*/




public:
    const std::shared_ptr<Rewriter> mRewriter_ptr;

public:
    ASTContext *Ctx;
    CompilerInstance& CI;
    SourceManager& SM;

    //一个位置若是插入了花括号,则表明此位置不需要再次插入花括号了。
    std::unordered_set<LocId,LocId> LBraceLocIdSet;

};


#endif
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ziix

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值