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
Self-driving cars are already a hot topic that ev...
I don't want comfort. I want poetry, I want r...
Today, taking Volcano as an example, let’s take a...
Recently, Shao Yiding, chairman of China's la...
Exploring the Secrets of the Twelve Constellation...
When I was a child, there was one activity that w...
The API used in this article is the old version o...
China Science and Technology News Network, Decemb...
The effect is amazing! Make your friends envious ...
Driven by multiple forces including users, platfo...
Some graduate students are not good at finding re...
When a cool title is paired with a bad landing pa...
Expert of this article: Wang Weibao, Endocrinolog...
In PPT presentations, academic research, web desi...
July 28, 10:00 The Central Meteorological Observa...