Part 01: Understanding Clang As we all know, compilers are generally divided into front-end and back-end. The front-end of the compiler is mainly responsible for preprocessing, lexical analysis, syntax analysis, syntax checking, generating intermediate codes and other tasks that are not related to the underlying computer architecture. The backend takes the intermediate code as input, first performs architecture-independent code optimization, then generates different machine codes for different machine architectures, and performs assembly and linking. Clang is mainly used for front-end compilation of C/C++ and Objective-C in iOS code compilation. Clang is part of the llvm compilation chain and is the front-end compiler of llvm. We can customize the source code through the API interface opened by Clang, such as static code checking, compilation process control, code search prompt completion and other functions. The object of the Clang tool is exactly AST - the result of syntax analysis, abstract syntax tree (abstract syntax tree). A simple example of an AST:
The Clang tool can traverse and read each node on the AST, and query and modify the code corresponding to the node. The Clang plug-in can be directly integrated into the iOS compilation process to control the output of customized compilation warnings and errors and control the compilation process. Part 02. Clang tool selection Clang generally consists of three different tools: libClang, Clang plugin, and libTooling. The Clang plugin is similar to the libTooling code. All information about the AST is returned through the ASTContext context, and it has full control over the AST. However, libClang is different. It is accessed through a packaged stable high-level C API, using Cursor and Token recursive traversal, and cannot fully control the AST. The advantages and disadvantages of the three are as follows: 1. libClang libClang is a stable high-level C language wrapper for Clang. When you do not need to fully control the AST, libClang is the simplest and most suitable tool to use and should be considered first. Secondly, it can only be used as a standalone tool and cannot be embedded in the compilation process of the current project.
2. Clang plug-in The Clang plug-in allows you to insert some additional operations into the compilation process of the code, such as printing special characters or warnings during the compilation process, or even interrupting the compilation.
3. libTooling libTooling is a C-based Clang tool for writing independent tools. It is similar to libClang, but it can only be written in C and is more powerful, with full control over AST.
From top to bottom, these three tools have increasingly poor compatibility, are increasingly sensitive to Clang upgrades and changes, are increasingly complex to use, but are increasingly powerful. Part 03. Specific Application of Clang The application of Clang in Hejiaqin is being initially carried out. We use libClang to traverse every source file in the project and find all string descriptions of image names in the project. Image names often appear in the form of fixed strings. This allows us to determine which images have been used and which have not been used in our project, and optimize the package size. We use the Clang plug-in to warn about classes and methods that have been defined in the project but not used in the project. The method is: first use the VisitObjCInterfaceDecl and VisitObjCMethodDecl methods of the Clang plug-in to find all class definitions and method definitions in the project, and then use VisitObjCMessageExpr and VisitObjCSelectorExpr to find all message sending. In iOS, method calls are made in the form of message sending. For those classes and methods that do not appear in the message sending list, we believe that these classes and methods are not used, so we directly warn during compilation. Integrate the plug-in in the compiler and use it. |
<<: iOS 16.2 finally supports 120Hz high refresh rate!
>>: A Brief Analysis of Mobile APP Security and Compliance Testing Technology
Does your doll have this situation? As soon as I ...
Yesterday I shared with you some hidden money-mak...
Autumn Dusk in the Mountain Wang Wei (Tang Dynast...
Why do dessert ads always say "Second half p...
First of all, everyone must understand that this ...
How to rent a server cabinet? The rental of a ser...
As we all know, data tracking and conversion is a...
How to become a beauty influencer from scratch? A...
By using the fission method to build a growth sys...
There are many types of private cars, with prices...
On February 3, there were rumors in the domain na...
When it comes to promotion , the first step that ...
The user journey refers to the entire process of ...
If the verification code cannot be displayed norm...
Tencent is currently testing a feature that allow...