LibTooling
LibTooling is a library to support writing standalone tools based on Clang. This document will provide a basic walkthrough of how to write a tool using LibTooling.
For the information on how to setup Clang Tooling for LLVM see How To Setup Clang Tooling For LLVM
Introduction
Tools built with LibTooling, like Clang Plugins, run FrontendActions
over code.
In this tutorial, we’ll demonstrate the different ways of running Clang’s SyntaxOnlyAction
, which runs a quick syntax check, over a bunch of code.
Parsing a code snippet in memory
If you ever wanted to run a FrontendAction
over some sample code, for example to unit test parts of the Clang AST, runToolOnCode
is what you looked for. Let me give you an example:
Writing a standalone tool
Once you unit tested your FrontendAction
to the point where it cannot possibly break, it’s time to create a standalone tool. For a standalone tool to run clang, it first needs to figure out what command line arguments to use for a specified file. To that end we create a CompilationDatabase
. There are different ways to create a compilation database, and we need to support all of them depending on command-line options. There’s the CommonOptionsParser
class that takes the responsibility to parse command-line parameters related to compilation databases and inputs, so that all tools share the implementation.
Parsing common tools options
CompilationDatabase
can be read from a build directory or the command line. Using CommonOptionsParser
allows for explicit specification of a compile command line, specification of build path using the -p
command-line option, and automatic location of the compilation database using source files paths.
Creating and running a ClangTool
Once we have a CompilationDatabase
, we can create a ClangTool
and run our FrontendAction
over some code. For example, to run the SyntaxOnlyAction
over the files “a.cc” and “b.cc” one would write:
Putting it together — the first tool
Now we combine the two previous steps into our first real tool. A more advanced version of this example tool is also checked into the clang tree at tools/clang-check/ClangCheck.cpp
.
Running the tool on some code
When you check out and build clang, clang-check is already built and available to you in bin/clang-check inside your build directory.
You can run clang-check on a file in the llvm repository by specifying all the needed parameters after a “--
” separator:
As an alternative, you can also configure cmake to output a compile command database into its build directory:
This creates a file called compile_commands.json
in the build directory. Now you can run clang-check over files in the project by specifying the build path as first argument and some source files as further positional arguments:
Builtin includes
Clang tools need their builtin headers and search for them the same way Clang does. Thus, the default location to look for builtin headers is in a path $(dirname /path/to/tool)/../lib/clang/3.3/include
relative to the tool binary. This works out-of-the-box for tools running from llvm’s toplevel binary directory after building clang-resource-headers, or if the tool is running from the binary directory of a clang install next to the clang binary.
Tips: if your tool fails to find stddef.h
or similar headers, call the tool with -v
and look at the search paths it looks through.
Linking
For a list of libraries to link, look at one of the tools’ CMake files (for example clang-check/CMakeList.txt).