Shi Ningning
Today's clang is not just a compiler front-end, but can also be used as a library at the same time. When used as a library, you can use it to analyze the C/C ++/objectc language code, analyze the source code to get the AST, and obtain the ast that has been analyzed, it can also traverse the AST and obtain the physical source code location of the basic elements in the ast. This is libclang.
Libclang provides a series of C language interfaces, but these interfaces do not provide all the information stored in clang C ++ AST, and only provide some basic information, however, these basic information can be used in general cases. The main purpose is to ensure stability and support the basic functions of development tools. (Clang/include/clang-C/index. the staring original text in H is as follows: This C interface to clang will never provide all of the Information Representation stored in clang's c ++ AST, nor shoshould it: the intent is to maintain an API that is relatively stable from one release to the next, providing only the basic functionality needed to support development tools .)
At the same time, libclang supports binding to Python, that is, you can use python to call libclang. Clang provides the files required for binding under the clang/bindings/Python/folder. The following three folders are available in addition to readme: clang, examples, and tests. The files below the clang folder are required for binding, and the examples folder below are two samples of the ghost trial after binding. The following files in the test folder are required by the test (the files below this folder have not been carefully analyzed yet ). You only need to put the bound file below the python library file (including the clang folder), and then ensure that the system installs clang to the system folder, and then the sample can be executed.
Libclang has obvious advantages. libclang can be easily used in many different environments. It is just a library that provides parser functions and can be conveniently and quickly embedded into your own tools, this avoids a lot of repetitive work. At the same time, you do not need to analyze the clang internal code, and you do not need to be familiar with a large number of internal Ast-related code, which can save a lot of time. Of course, those who want to dig deep into clang code can also regard this as a breakthrough.
---------------------------------------------------------------- For reprinting, please specify the source region --------------------------------------------------------------------------------------