iOS 底层原理 iOS 进阶之路

OC底层原理三十二:LLVM插件(Copy修饰符检测)

2020-11-19  本文已影响0人  markhetao

OC底层原理 学习大纲

本节:

  • 自定义Clang插件是实现Copy属性修饰符检测智能提示
    (最终的使用场景可能并没那么通用,但主要目的是彻底熟悉LLVM
  • 文末会附上最终代码使用方法
  1. 配置LLVM环境
  2. 自定义插件

1. 配置LLVM环境

⚠️ ⚠️ ⚠️ 【注意】

  1. LLVM源码2.29G编译后文件将近30G,请确保电脑硬盘空间足够
  2. 编译时,电脑温度会飙升90多度CPU资源占满,请用空调伺候着,可能黑屏
  3. 编译时间长达1个多小时,请合理安排时间。

如果以上3点,你确定能接受,那我们就开始吧。

1.1 LLVM下载

  • clangclang-tools-extracompiler-rtlibcxxlibcxxabillvm五个库:(我下载的都是11.0.0版本的)
    image.png
image.png
  1. clang-tools-extra移到clang文件夹中的clang/tools文件中
  2. clang文件夹移到llvm/tools
  3. compiler-rtlibcxxlibcxxabi都移到llvm/projects
    image.png
image.png

1.2 安装cmake

brew list
brew install cmake
  • 如果报权限错误,可sudo chown -R `whoami`:admin /usr/local/share放开权限
    image.png

1.3 编译

cd build
cmake -G Xcode ../llvm   
// 或者: cmake -G Xcode CMAKE_BUILD_TYPE="Release" ../llvm
// 或者: cmake -G Xcode CMAKE_BUILD_TYPE="debug" ../llvm 

注意:

    1. build文件夹是存放cmake生成的Xcode文件的。放哪里都可以。
    1. cmake编译的对象是llvm文件。所以使用cmake -G Xcode ../llvm编译并生成Xcode文件时,请核对llvm文件路径

选择手动创建Schemes

image.png
image.png

2. 自定义插件

2.1 添加插件

  1. CMakeLists.txt内容:add_llvm_library( HTPlugin MODULE BUILDTREE_ONLY HTPlugin.cpp)
  2. HTPlugin.cpp:空文件
    image.png
image.png
开始编译
编译完成

ps: 我的电脑系统版本是:

image.png
  • Command + 鼠标左键点击文件夹,折叠所有文件夹。 在Loadable modules中,可以看到我们自己创建的HTPlugin文件夹:
    image.png
  • 搜索选择 HTPlugin
    image.png

2.2 书写插件

2.2.1 体验单文件顶级节点解析

1. 在HTPlugn.cpp文件中加入以下代码

// 1. 声明命名空间,创建插件
namespace HTPlugin {
    
    // 4. 自定义HTConsumer,继承ASTConsumer
    // 进入`ASTConsumer`,查看它的结构,有很多选择
    //    本例重载[HandleTopLevelDecl]顶级节点的解析和[HandleTranslationUnit]文件解析结束回调
    class HTConsumer: public ASTConsumer {
    private:
    public:
        
        // 4.1 顶级节点解析中
        bool HandleTopLevelDecl(DeclGroupRef D) {
            cout<<"解析中..."<<endl;
            return true;
        }
        
        // 4.2 单个文件解析结束
        void HandleTranslationUnit(ASTContext &Ctx) {
            cout<<"文件解析完毕!"<<endl;
        }
        
    };

    // 2. 创建插件(继承PluginASTAction 实现自定义的ASTAction)
    // 【目的】读取AST语法树的所有节点。
    // 【如何重写】 进入`PluginASTAction`,查看它的结构,重载[ParseArgs]和[CreateASTConsumer]
    class HTASTAction: public PluginASTAction {
    public:
        
        // 2.1 解析成功,就返回true。 我们直接写true
        bool ParseArgs(const CompilerInstance &CI,
                       const std::vector<std::string> &arg) {
            return true;
        }
        
        // 2.2 创建一个语法树对象
        unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
                                                  StringRef InFile){
            // 使用自定义的HTConsumer,继承自ASTConsumer。
//            return unique_ptr<ASTConsumer>(new ASTConsumer());
            return unique_ptr<HTConsumer>(new HTConsumer());
        }
    };

}

// 3. 注册插件 (参数1:插件名称, 参数2:插件描述)
static FrontendPluginRegistry::Add<HTPlugin::HTASTAction> HT("HTPlugin", "this is HTPlugin");

2. Command+B 编译一次
3. 创建一个测试文件demo.m

image.png
int sum(int a);
int a = 10;
int b = 20;

int sum(int a) {
    int b = 10;
    return 10 + b;
}

int sum2(int a, int b) {
    int c = 10;
    return a + b + c;
}

4. cdClangDemo文件夹,使用我们的clang编译demo.m文件:

/Users/ht/Desktop/llvm/build/Debug/bin/clang -usysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator14.2.sdk/ -Xclang -load -Xclang /Users/ht/Desktop/llvm/build/Debug/lib/HTPlugin.dylib -Xclang -add-plugin -Xclang HTPlugin -c demo.m

格式分析:

  • /Users/ht/Desktop/llvm/build/Debug/bin/clang:
    自己插件编译后clang文件,在build/Debug/bin/文件夹中:
    image.png
  • /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator14.2.sdk/
    使用自己本机SDK绝对路径。 (我这是14.2)
    image.png
  • /Users/ht/Desktop/llvm/build/Debug/lib/HTPlugin.dylib:
    自己插件编译后HTPlugin.dylib插件动态库绝对路径,在build/Debug/lib/文件夹中:
    image.png
  • 指定使用HTPlugin 编译demo.m文件
image.png

5. 编译完成后,会生成demo.o文件。是一个object目标文件(不懂目标文件,可回顾上一节

image.png
2.2.2 观察属性修饰符节点名

1. 创建工程

@interface ViewController()

// NSString、NSArray、NSDictionary 应使用Copy修饰
@property (nonatomic, strong) NSString * name;   
@property (nonatomic, strong) NSArray * arrs;
@property (nonatomic, strong) NSDictionary *dicts;

@end

2. 使用系统clang编译ViewController.m文件

clang -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator14.2.sdk  -fmodules -fsyntax-only -Xclang -ast-dump  ViewController.m
image.png
2.2.3 过滤ObjCPropertyDecl节点
#include <iostream>
#include "clang/AST/AST.h"
#include "clang/AST/DeclObjC.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/Frontend/FrontendPluginRegistry.h"

using namespace clang;
using namespace std;
using namespace llvm;
using namespace clang::ast_matchers;

// 1. 声明命名空间,创建插件
namespace HTPlugin {
    
    // 5. 自定义HTMatchCallBack(过滤器回调类) 继承自MatchFinder的MatchCallBack
    class HTMatchCallBack: public MatchFinder::MatchCallback {
    public:
        // 5.2 重写run方法
        void run(const MatchFinder::MatchResult &Result) {
            
            // 通过Result拿到了所有节点, 通过自定义的节点标识,拿到我们标记的所有节点
            const ObjCPropertyDecl * propertyDecl = Result.Nodes.getNodeAs<ObjCPropertyDecl>("objcPropertyDecl");
            
            // 过滤空情况
            if (propertyDecl) {
                // 拿到类型,转string,打印
                string typeStr = propertyDecl->getType().getAsString();
                cout<<"--- 拿到了: 【"<<typeStr<<"】 ----"<<endl;
            }
            
        }
    };


    // 4. 自定义HTConsumer,继承ASTConsumer
    // 进入`ASTConsumer`,查看它的结构,有很多选择
    //    本例重载[HandleTopLevelDecl]顶级节点的解析和[HandleTranslationUnit]文件解析结束回调
    class HTConsumer: public ASTConsumer {
    private:
        // 4.3 添加属性:AST节点查找过滤器
        MatchFinder matcher;
        // 5.1 添加属性:MatchFinder过滤器的回调函数
        HTMatchCallBack callback;
    public:
        
        // 4.5 声明构造方法(出厂就添加一个MatchFinder过滤器)
        HTConsumer() {
            // 添加MatchFinder (参数1: 过滤的节点, 参数2, 过滤后的回调)
            //    查看MatchFinder中的MatchCallBack的结构,仿写一个回调函数
            // 此处,过滤`objcPropertyDecl`属性节点,手动添加节点标识`objcPropertyDecl`
            matcher.addMatcher(objcPropertyDecl().bind("objcPropertyDecl"), &callback);
        }
        
        // 4.1 顶级节点解析中
        bool HandleTopLevelDecl(DeclGroupRef D) {
            cout<<"解析中..."<<endl;
            return true;
        }
        
        // 4.2 单个文件解析结束
        void HandleTranslationUnit(ASTContext &Ctx) {
            cout<<"文件解析完毕!"<<endl;
            // 4.4 解析完成后,将语法树给到`matcher`过滤器
            matcher.matchAST(Ctx);
        }
        
    };

    // 2. 创建插件(继承PluginASTAction 实现自定义的ASTAction)
    // 【目的】读取AST语法树的所有节点。
    // 【如何重写】 进入`PluginASTAction`,查看它的结构,重载[ParseArgs]和[CreateASTConsumer]
    class HTASTAction: public PluginASTAction {
    public:
        
        // 2.2 解析成功,就返回true。 我们直接写true
        bool ParseArgs(const CompilerInstance &CI,
                       const std::vector<std::string> &arg) {
            return true;
        }
        
        // 2.3 创建一个语法树对象
        unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
                                                  StringRef InFile){
            // 使用自定义的HTConsumer,继承自ASTConsumer。
            // return unique_ptr<ASTConsumer>(new ASTConsumer());
            return unique_ptr<HTConsumer>(new HTConsumer());
        }
    };

}

// 3. 注册插件 (参数1:插件名称, 参数2:插件描述)
static FrontendPluginRegistry::Add<HTPlugin::HTASTAction> HT("HTPlugin", "this is HTPlugin");
/Users/ht/Desktop/llvm/build/Debug/bin/clang -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator14.2.sdk -Xclang -load -Xclang /Users/ht/Desktop/llvm/build/Debug/lib/HTPlugin.dylib  -Xclang -add-plugin -Xclang HTPlugin -c ViewController.m

分享我掉的一个坑:

  • 这是我自己,因为我之前之前写了MacOS项目,所以创建Demo时,默认创建了MacOS工程,然后我使用了iPhoneSimulator14.2.sdkiOS SDK来编译。一直报'Cocoa/Cocoa.h' file not found
    image.png

出现Cocoa/Cocoa.h的问题,都是MacOS的问题,iOS使用Fundation库。macOS使用Cocoa库。


2.2.4 过滤系统文件节点

可以通过编译器实例(文件)CompilerInstance,读取每个实例路径。代码如下:(新增6)

// 1. 声明命名空间,创建插件
namespace HTPlugin {
    
    // 5. 自定义HTMatchCallBack(过滤器回调类) 继承自MatchFinder的MatchCallBack
    class HTMatchCallBack: public MatchFinder::MatchCallback {
    private:
        // 6.2 添加属性:编译器实例
        CompilerInstance &compilerInstance;
    public:
        
        // 6.3 声明构造方法,入参新增CompilerInstance编译器实例,并赋值给compilerInstance
        HTMatchCallBack(CompilerInstance &CI):compilerInstance(CI) { }
        
        // 5.2 重写run方法 (回调的执行函数)
        void run(const MatchFinder::MatchResult &Result) {
            
            // 通过Result拿到了所有节点, 通过自定义的节点标识,拿到我们标记的所有节点
            const ObjCPropertyDecl * propertyDecl = Result.Nodes.getNodeAs<ObjCPropertyDecl>("objcPropertyDecl");
            
            // 过滤空情况
            if (propertyDecl) {
                
                // 6.4 打印文件名称
                string fileName = compilerInstance.getSourceManager().getFilename(propertyDecl->getSourceRange().getBegin()).str();
                
                // 拿到类型,转string,打印
                string typeStr = propertyDecl->getType().getAsString();
                
                cout<<"--- 拿到了: 【"<<typeStr<<"】 ----文件路径:"<<fileName<<endl;
            }
            
        }
    };

    // 4. 自定义HTConsumer,继承ASTConsumer
    // 进入`ASTConsumer`,查看它的结构,有很多选择
    //    本例重载[HandleTopLevelDecl]顶级节点的解析和[HandleTranslationUnit]文件解析结束回调
    class HTConsumer: public ASTConsumer {
    private:
        // 4.3 添加属性:AST节点查找过滤器
        MatchFinder matcher;
        // 5.1 添加属性:MatchFinder过滤器的回调函数
        HTMatchCallBack callback;
    public:
        // 4.5 声明构造方法(出厂就添加一个MatchFinder过滤器)
        //  6.4 入参新增:编译器实例CI,并赋值给callback
        HTConsumer(CompilerInstance &CI):callback(CI) {
            // 添加MatchFinder (参数1: 过滤的节点, 参数2, 过滤后的回调)
            //    查看MatchFinder中的MatchCallBack的结构,仿写一个回调函数
            // 此处,过滤`objcPropertyDecl`属性节点,手动添加节点标识`objcPropertyDecl`
            matcher.addMatcher(objcPropertyDecl().bind("objcPropertyDecl"), &callback);
        }
        
        // 4.1 顶级节点解析中
        bool HandleTopLevelDecl(DeclGroupRef D) {
            cout<<"解析中..."<<endl;
            return true;
        }
        
        // 4.2 单个文件解析结束
        void HandleTranslationUnit(ASTContext &Ctx) {
            cout<<"文件解析完毕!"<<endl;
            // 4.4 解析完成后,将语法树给到`matcher`过滤器
            matcher.matchAST(Ctx);
        }
        
    };

    // 2. 创建插件(继承PluginASTAction 实现自定义的ASTAction)
    // 【目的】读取AST语法树的所有节点。
    // 【如何重写】 进入`PluginASTAction`,查看它的结构,重载[ParseArgs]和[CreateASTConsumer]
    class HTASTAction: public PluginASTAction {
    public:
        
        // 2.2 解析成功,就返回true。 我们直接写true
        bool ParseArgs(const CompilerInstance &CI,
                       const std::vector<std::string> &arg) {
            return true;
        }
        
        // 2.3 创建一个语法树对象
        unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
                                                  StringRef InFile){
            // 使用自定义的HTConsumer,继承自ASTConsumer。
            // return unique_ptr<ASTConsumer>(new ASTConsumer());
            // 6.1 存储CompilerInstance实例(它是每个编译文件,可以通过它读取编译文件的路径,剔除系统文件的干扰)
            return unique_ptr<HTConsumer>(new HTConsumer(CI));
        }
    };

}

// 3. 注册插件 (参数1:插件名称, 参数2:插件描述)
static FrontendPluginRegistry::Add<HTPlugin::HTASTAction> HT("HTPlugin", "this is HTPlugin");

/Users/ht/Desktop/llvm/build/Debug/bin/clang -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator14.2.sdk -Xclang -load -Xclang /Users/ht/Desktop/llvm/build/Debug/lib/HTPlugin.dylib  -Xclang -add-plugin -Xclang HTPlugin -c ViewController.m
#include <iostream>
#include "clang/AST/AST.h"
#include "clang/AST/DeclObjC.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/Frontend/FrontendPluginRegistry.h"

using namespace clang;
using namespace std;
using namespace llvm;
using namespace clang::ast_matchers;

// 1. 声明命名空间,创建插件
namespace HTPlugin {
    
    // 5. 自定义HTMatchCallBack(过滤器回调类) 继承自MatchFinder的MatchCallBack
    class HTMatchCallBack: public MatchFinder::MatchCallback {
    private:
        // 6.2 添加属性:编译器实例
        CompilerInstance &compilerInstance;
        
        // 7. 判断是否是用户代码的函数(通过文件地址判断)
        bool isUserSourceCode(const string fileName) {
            
            // 空,不是用户的
            if (fileName.empty()) return false;
            
            // Xcode中的源码,都是系统的
            if (fileName.find("/Applications/Xcode.app/") == 0) return false;
            
            // 其他情况,都是用户的
            return true;
            
        }
    public:
        
        // 6.3 声明构造方法,入参新增CompilerInstance编译器实例,并赋值给compilerInstance
        HTMatchCallBack(CompilerInstance &CI):compilerInstance(CI) { }
        
        // 5.2 重写run方法 (回调的执行函数)
        void run(const MatchFinder::MatchResult &Result) {
            
            // 通过Result拿到了所有节点, 通过自定义的节点标识,拿到我们标记的所有节点
            const ObjCPropertyDecl * propertyDecl = Result.Nodes.getNodeAs<ObjCPropertyDecl>("objcPropertyDecl");
            
            // 过滤空情况
            if (propertyDecl) {
                
                // 6.4 打印文件名称
                string fileName = compilerInstance.getSourceManager().getFilename(propertyDecl->getSourceRange().getBegin()).str();
                
                // 7.1 不是用户的文件,就不往下打印
                if (!isUserSourceCode(fileName)) return;
                
                // 拿到类型,转string,打印
                string typeStr = propertyDecl->getType().getAsString();
                
                cout<<"--- 拿到了: 【"<<typeStr<<"】 ----文件路径:"<<fileName<<endl;
            }
            
        }
    };

    // 4. 自定义HTConsumer,继承ASTConsumer
    // 进入`ASTConsumer`,查看它的结构,有很多选择
    //    本例重载[HandleTopLevelDecl]顶级节点的解析和[HandleTranslationUnit]文件解析结束回调
    class HTConsumer: public ASTConsumer {
    private:
        // 4.3 添加属性:AST节点查找过滤器
        MatchFinder matcher;
        // 5.1 添加属性:MatchFinder过滤器的回调函数
        HTMatchCallBack callback;
    public:
        // 4.5 声明构造方法(出厂就添加一个MatchFinder过滤器)
        //  6.4 入参新增:编译器实例CI,并赋值给callback
        HTConsumer(CompilerInstance &CI):callback(CI) {
            // 添加MatchFinder (参数1: 过滤的节点, 参数2, 过滤后的回调)
            //    查看MatchFinder中的MatchCallBack的结构,仿写一个回调函数
            // 此处,过滤`objcPropertyDecl`属性节点,手动添加节点标识`objcPropertyDecl`
            matcher.addMatcher(objcPropertyDecl().bind("objcPropertyDecl"), &callback);
        }
        
        // 4.1 顶级节点解析中
        bool HandleTopLevelDecl(DeclGroupRef D) {
            cout<<"解析中..."<<endl;
            return true;
        }
        
        // 4.2 单个文件解析结束
        void HandleTranslationUnit(ASTContext &Ctx) {
            cout<<"文件解析完毕!"<<endl;
            // 4.4 解析完成后,将语法树给到`matcher`过滤器
            matcher.matchAST(Ctx);
        }
        
    };

    // 2. 创建插件(继承PluginASTAction 实现自定义的ASTAction)
    // 【目的】读取AST语法树的所有节点。
    // 【如何重写】 进入`PluginASTAction`,查看它的结构,重载[ParseArgs]和[CreateASTConsumer]
    class HTASTAction: public PluginASTAction {
    public:
        
        // 2.2 解析成功,就返回true。 我们直接写true
        bool ParseArgs(const CompilerInstance &CI,
                       const std::vector<std::string> &arg) {
            return true;
        }
        
        // 2.3 创建一个语法树对象
        unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
                                                  StringRef InFile){
            // 使用自定义的HTConsumer,继承自ASTConsumer。
            // return unique_ptr<ASTConsumer>(new ASTConsumer());
            // 6.1 存储CompilerInstance实例(它是每个编译文件,可以通过它读取编译文件的路径,剔除系统文件的干扰)
            return unique_ptr<HTConsumer>(new HTConsumer(CI));
        }
    };

}

// 3. 注册插件 (参数1:插件名称, 参数2:插件描述)
static FrontendPluginRegistry::Add<HTPlugin::HTASTAction> HT("HTPlugin", "this is HTPlugin");
image.png
2.2.5 定位代码错误,添加错误提示
#include <iostream>
#include "clang/AST/AST.h"
#include "clang/AST/DeclObjC.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/Frontend/FrontendPluginRegistry.h"

using namespace clang;
using namespace std;
using namespace llvm;
using namespace clang::ast_matchers;

// 1. 声明命名空间,创建插件
namespace HTPlugin {
    
    // 5. 自定义HTMatchCallBack(过滤器回调类) 继承自MatchFinder的MatchCallBack
    class HTMatchCallBack: public MatchFinder::MatchCallback {
    private:
        // 6.2 添加属性:编译器实例
        CompilerInstance &compilerInstance;
        
        // 7. 判断是否是用户代码的函数(通过文件地址判断)
        bool isUserSourceCode(const string fileName) {
            
            // 空,不是用户的
            if (fileName.empty()) return false;
            
            // Xcode中的源码,都是系统的
            if (fileName.find("/Applications/Xcode.app/") == 0) return false;
            
            // 其他情况,都是用户的
            return true;
            
        }
        
        // 8. 判断是否应该copy修饰
        bool isShouldUseCopy(const string typeStr) {
            
            if (typeStr.find("NSString") != string::npos ||
                typeStr.find("NSArray") != string::npos ||
                typeStr.find("NSDictionary") != string::npos) {
                
                return true;
                
            }
            
            return false;
            
        }
        
    public:
        
        // 6.3 声明构造方法,入参新增CompilerInstance编译器实例,并赋值给compilerInstance
        HTMatchCallBack(CompilerInstance &CI):compilerInstance(CI) { }
        
        // 5.2 重写run方法 (回调的执行函数)
        void run(const MatchFinder::MatchResult &Result) {
            
            // 通过Result拿到了所有节点, 通过自定义的节点标识,拿到我们标记的所有节点
            const ObjCPropertyDecl * propertyDecl = Result.Nodes.getNodeAs<ObjCPropertyDecl>("objcPropertyDecl");
            
            // 过滤空情况
            if (propertyDecl) {
                
                // 6.4 打印文件名称
                string fileName = compilerInstance.getSourceManager().getFilename(propertyDecl->getSourceRange().getBegin()).str();
                
                // 7.1 不是用户的文件,就不往下打印
                if (!isUserSourceCode(fileName)) return;
                
                // 拿到类型,转string,打印
                string typeStr = propertyDecl->getType().getAsString();
                
                // 8.1 找出应该使用copy的属性
                if (isShouldUseCopy(typeStr)) {
                    
                    // 拿到节点描述 (ObjCPropertyAttribute::Kind 是枚举,其中kind_copy = 0x20)
                    ObjCPropertyAttribute::Kind attrKind = propertyDecl->getPropertyAttributes();
                    
                    // 当前修饰符不为copy,提示
                    if (!(attrKind & ObjCPropertyAttribute::kind_copy)) {
                        
                        // 诊断引擎
                        DiagnosticsEngine &diag = compilerInstance.getDiagnostics();
                        
                        // Report 报告
                        // 参数1: 定位位置
                        // 参数2: 设置等级和提示文案
                        diag.Report(propertyDecl->getBeginLoc(), diag.getCustomDiagID(DiagnosticsEngine::Warning, "%0这个地方推荐使用copy!!!"))<<typeStr;
                    }
                    
                }
                
            }
            
        }
    };

    // 4. 自定义HTConsumer,继承ASTConsumer
    // 进入`ASTConsumer`,查看它的结构,有很多选择
    //    本例重载[HandleTopLevelDecl]顶级节点的解析和[HandleTranslationUnit]文件解析结束回调
    class HTConsumer: public ASTConsumer {
    private:
        // 4.3 添加属性:AST节点查找过滤器
        MatchFinder matcher;
        // 5.1 添加属性:MatchFinder过滤器的回调函数
        HTMatchCallBack callback;
    public:
        // 4.5 声明构造方法(出厂就添加一个MatchFinder过滤器)
        //  6.4 入参新增:编译器实例CI,并赋值给callback
        HTConsumer(CompilerInstance &CI):callback(CI) {
            // 添加MatchFinder (参数1: 过滤的节点, 参数2, 过滤后的回调)
            //    查看MatchFinder中的MatchCallBack的结构,仿写一个回调函数
            // 此处,过滤`objcPropertyDecl`属性节点,手动添加节点标识`objcPropertyDecl`
            matcher.addMatcher(objcPropertyDecl().bind("objcPropertyDecl"), &callback);
        }
        
        // 4.1 顶级节点解析中
        bool HandleTopLevelDecl(DeclGroupRef D) {
//            cout<<"解析中..."<<endl;
            return true;
        }
        
        // 4.2 单个文件解析结束
        void HandleTranslationUnit(ASTContext &Ctx) {
//            cout<<"文件解析完毕!"<<endl;
            // 4.4 解析完成后,将语法树给到`matcher`过滤器
            matcher.matchAST(Ctx);
        }
        
    };

    // 2. 创建插件(继承PluginASTAction 实现自定义的ASTAction)
    // 【目的】读取AST语法树的所有节点。
    // 【如何重写】 进入`PluginASTAction`,查看它的结构,重载[ParseArgs]和[CreateASTConsumer]
    class HTASTAction: public PluginASTAction {
    public:
        
        // 2.2 解析成功,就返回true。 我们直接写true
        bool ParseArgs(const CompilerInstance &CI,
                       const std::vector<std::string> &arg) {
            return true;
        }
        
        // 2.3 创建一个语法树对象
        unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
                                                  StringRef InFile){
            // 使用自定义的HTConsumer,继承自ASTConsumer。
            // return unique_ptr<ASTConsumer>(new ASTConsumer());
            // 6.1 存储CompilerInstance实例(它是每个编译文件,可以通过它读取编译文件的路径,剔除系统文件的干扰)
            return unique_ptr<HTConsumer>(new HTConsumer(CI));
        }
    };

}

// 3. 注册插件 (参数1:插件名称, 参数2:插件描述)
static FrontendPluginRegistry::Add<HTPlugin::HTASTAction> HT("HTPlugin", "this is HTPlugin");
2.2.6 Xcode集成自定义插件

1. Demo工程添加自定义插件 Build Settings ->Other C Flags 添加:

-Xclang -load -Xclang /Users/ht/Desktop/llvm/build/Debug/lib/HTPlugin.dylib -Xclang -add-plugin -Xclang HTPlugin

/Users/ht/Desktop/llvm/build/Debug/lib/HTPlugin.dylib是自己的HTPlugin.dylib绝对路径

image.png

2. Command + B编译,报错

CC : 填写clang绝对路径
CXX:填写clang++绝对路径

image.png
image.png
2.2.7 编译成功

Command+B编译,编译成功,查看ViewController.m文件:

image.png

通过这个小插件,应该对语法树编译流程,有了更深刻认识。😃

上一篇 下一篇

猜你喜欢

热点阅读