当前位置:   article > 正文

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

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/589564
推荐阅读
相关标签
  

闽ICP备14008679号