iOS front-end compiler extension - Clang

iOS front-end compiler extension - Clang

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.

  • Advantages: Can use XCode or Python for integrated development, has a stable high-level API, and is easy to use;
  • Disadvantages : Cannot fully control AST and cannot be embedded in the compilation process.

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.

  • Advantages: It can embed into the compilation process to start or interrupt compilation, print custom warnings and errors, and fully control the AST;
  • Disadvantages: Code writing is complicated, and integrating the Clang plug-in will reduce the original compilation speed .

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.

  • Advantages: Full processing of project files and full control of AST;
  • Disadvantages: cannot be embedded in the compilation process, is sensitive to Clang upgrades, and has an unstable API.

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

Recommend

There is a big game behind Youpengpule's 500 million advertising bonus

Recently, Shao Yiding, chairman of China's la...

iOS unit testing: translation - common ways to use OCMock

The API used in this article is the old version o...

Cold-resistant lithium-ion batteries, will your phone "hibernate"?

China Science and Technology News Network, Decemb...

Kuaishou Advertising Creative Guide for 2019!

Driven by multiple forces including users, platfo...

Eight open source free web screenshot/recording tools

In PPT presentations, academic research, web desi...