📜 ⬆️ ⬇️

Clang. Part 1: introduction

What is Clang?


I spent the last few months working with Clang , the LLVM frontend. Clang can parse and analyze any source code in languages ​​of the C family (C, C ++, ObjectiveC, etc ...) and has an amazing modular structure that makes it easy to use.


If you are looking for a static code analyzer, I highly recommend Clang, it is significantly superior to other static analyzers (such as CIL ...) and is well documented. Also, the Clang mailing list is very active and useful if you are stuck on something.

Personally, I use Clang to statically analyze Linux kernel I / O drivers , including camera drivers and DRM graphics card drivers . Kernel code, especially driver code, can be very complex and difficult to analyze, but Clang allows us to easily maintain it. Let's see what can be done with it.

How does clang work?


In most cases, Clang runs the preprocessor (which expands all macros) and parses the source, turning it into an abstract syntax tree (AST). C AST is much easier to work with than source code, but you can always get links to the source. In fact, every structure in Clang used for code representation (AST, CFG, etc.) always has a link to the original source, useful for analysis, refactoring, etc.
')
if you need to analyze and modify code at the source level, Clang is better than LLVM. Analyzing with LLVM means that you can use an internal representation language LLVM, similar to assembler.

Clang AST


Virtually every compiler and static analyzer uses AST to represent source code. The AST used in Clang is very detailed and complex, but you will enjoy learning the various classes of AST elements Clang. Below is a brief introduction to Clang AST, but the easiest way to learn it is to simply make AST dumps for simple sources, and see which AST corresponds to them.

In general, the Clang AST is made of two very flexible classes: Decl and Stmt. Both have many subclasses, here are some examples:

FunctionDecl - prototype or function declaration
BinaryOperator - binary operator, for example (a + b)
CallExpr - function call, for example, foo (x);

Most classes have “talking” names, such as ForStmt, IfStmt, and ReturnStmt. You will understand the essence of AST by playing with it for several minutes. You can find documentation on AST classes by searching for something like “Clang FunctionDecl.”

How to use clang?


Clang can be used as a direct replacement for gcc and offers some cool static analysis tools. As a programmer (and not as a normal user!), You can access all the power of clang, using it as a library in one of three ways, depending on how you decide.

First, read the description of the clang interfaces . In addition to what is written in this description, I will highlight other significant differences between the various clang interfaces.

Clang plugin


Your code is a plugin, and it is re-launched each time for each source file, which means that you cannot save global information or other contextual information between two different source files (but you can run the plugin for multiple files in sequence). The plugin is launched by passing the appropriate options to the compilation system (Clang, Make, etc.) via command line arguments. This is similar to how you enable optimization in GCC (i.e. "-O1"). You cannot run any of your tasks before or after the source file has been analyzed.

LibTooling (Clang Tool)


Your code is a normal C ++ program, with normal main () function. LibTooling is used to run some analysis on the source code (with multiple files, if desired) without starting the normal compilation process. A new instance of code for analysis (and a new AST) will be created for each new source file (as is the case with Clang Plugin), but you can save contextual information between source files in your global variables. Since you have a main () function, you can run any tasks before or after the clang completes the analysis of your source files.

Libclang


LibClang is good because it is a stable API. Clang changes periodically, and if you use Plugin or Libtooling, you will need to edit your code in order to track these changes (but this is not so difficult!). If you need access to the Clang API from languages ​​other than C ++ (for example, from Python), you should use LibClang.

Note : LibClang does not give full access to AST (only high-level access), but the other two options give. As a rule, we need full access to AST.

If you cannot decide what to use, I would recommend starting with the LibTooling interface. It is simpler and works the way you expect. It offers flexibility and full access to AST, like Plugin, without losing the global context between the source files. LibTooling is not more difficult to use than Plugin.

Getting started with Clang


Now that you know the basics, let's get started! This manual will work on any version of Linux (and, possibly, OS X), but has been tested on Ubuntu. You can get LLVM and Clang by following these steps (taken from the official Clang instructions ):

Download and install (for example, using apt-get) all the necessary packages .
(A typical Linux distribution comes with everything you need except subversion).
Change the directory to the directory where you want to install LLVM (for example, ~ / static_analysis /). We will call it the top level directory. Run the following commands in the terminal:

$ svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm $ cd llvm/tools $ svn co http://llvm.org/svn/llvm-project/cfe/trunk clang $ cd clang/tools $ svn co http://llvm.org/svn/llvm-project/clang-tools-extra/trunk extra $ cd ../../../.. #go back to top directory $ cd llvm/projects $ svn co http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt $ cd ../.. #go back to top directory $ cd llvm $ ./configure $ make #this takes a few hours $ sudo make install 

Compiling LLVM and Clang will take some time.

To test run:

 $ clang --version 

You can test Clang by running the classic Hello World example:

 $ clang hello.c -o hello $ ./hello 

In this tutorial, I use Clang 3.4 on Ubuntu 13.04, but you can use other options of both.

We now turn to programming on Clang.

To be continued.

Source: https://habr.com/ru/post/354718/


All Articles