How To Setup Clang Tooling For LLVM

Clang Tooling provides infrastructure to write tools that need syntactic and semantic infomation about a program. This term also relates to a set of specific tools using this infrastructure (e.g. clang-check). This document provides information on how to set up and use Clang Tooling for the LLVM source code.

Introduction

Clang Tooling needs a compilation database to figure out specific build options for each file. Currently it can create a compilation database from the compilation_commands.json file, generated by CMake. When invoking clang tools, you can either specify a path to a build directory using a command line parameter -p or let Clang Tooling find this file in your source tree. In either case you need to configure your build using CMake to use clang tools.

Setup Clang Tooling Using CMake and Make

If you intend to use make to build LLVM, you should have CMake 2.8.6 or later installed (can be found here).

First, you need to generate Makefiles for LLVM with CMake. You need to make a build directory and run CMake from it:

  mkdir your/build/directory
  cd your/build/directory
  cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON path/to/llvm/sources

If you want to use clang instead of GCC, you can add -DCMAKE_C_COMPILER=/path/to/clang -DCMAKE_CXX_COMPILER=/path/to/clang++. You can also use ccmake, which provides a curses interface to configure CMake variables for lazy people.

As a result, the new compile_commands.json file should appear in the current directory. You should link it to the LLVM source tree so that Clang Tooling is able to use it:

  ln -s $PWD/compile_commands.json path/to/llvm/source/

Now you are ready to build and test LLVM using make:

  make check-all

Using Clang Tools

After you completed the previous steps, you are ready to run clang tools. If you have a recent clang installed, you should have clang-check in $PATH. Try to run it on any .cpp file inside the LLVM source tree:

  clang-check tools/clang/lib/Tooling/CompilationDatabase.cpp

If you're using vim, it's convenient to have clang-check integrated. Put this into your .vimrc:

  set makeprg=clang-check\ %
  map <F5> :make<CR><CR>

When editing C++ code, hit F5 to reparse the current buffer. The output will go into the error window, which you can enable with :cope.

(Experimental) Using Ninja Build System

Optionally you can use the Ninja build system instead of make. It is aimed at making your builds faster. Currently this step will require building Ninja from sources and using a development version of CMake.

To take advantage of using Clang Tools along with Ninja build you need at least CMake 2.8.9. At the moment CMake 2.8.9 is still under development, so you can get latest development sources and build it yourself:

  git clone git://cmake.org/cmake.git
  cd cmake
  ./bootstrap
  make
  sudo make install

Having the correct version of CMake, you can clone the Ninja git repository and build Ninja from sources:

  git clone git://github.com/martine/ninja.git
  cd ninja/
  ./bootstrap.py

This will result in a single binary ninja in the current directory. It doesn't require installation and can just be copied to any location inside $PATH, say /usr/local/bin/:

  sudo cp ninja /usr/local/bin/
  sudo chmod a+rx /usr/local/bin/ninja

After doing all of this, you'll need to generate Ninja build files for LLVM with CMake. You need to make a build directory and run CMake from it:

  mkdir your/build/directory
  cd your/build/directory
  cmake -G Ninja -DCMAKE_EXPORT_COMPILE_COMMANDS=ON path/to/llvm/sources

If you want to use clang instead of GCC, you can add -DCMAKE_C_COMPILER=/path/to/clang -DCMAKE_CXX_COMPILER=/path/to/clang++. You can also use ccmake, which provides a curses interface to configure CMake variables in an interactive manner.

As a result, the new compile_commands.json file should appear in the current directory. You should link it to the LLVM source tree so that Clang Tooling is able to use it:

  ln -s $PWD/compile_commands.json path/to/llvm/source/

Now you are ready to build and test LLVM using Ninja:

  ninja check-all

Other target names can be used in the same way as with make.