xref: /minix3/external/bsd/llvm/dist/clang/docs/LibTooling.rst (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc==========
2f4a2713aSLionel SambucLibTooling
3f4a2713aSLionel Sambuc==========
4f4a2713aSLionel Sambuc
5f4a2713aSLionel SambucLibTooling is a library to support writing standalone tools based on Clang.
6f4a2713aSLionel SambucThis document will provide a basic walkthrough of how to write a tool using
7f4a2713aSLionel SambucLibTooling.
8f4a2713aSLionel Sambuc
9f4a2713aSLionel SambucFor the information on how to setup Clang Tooling for LLVM see
10f4a2713aSLionel Sambuc:doc:`HowToSetupToolingForLLVM`
11f4a2713aSLionel Sambuc
12f4a2713aSLionel SambucIntroduction
13f4a2713aSLionel Sambuc------------
14f4a2713aSLionel Sambuc
15f4a2713aSLionel SambucTools built with LibTooling, like Clang Plugins, run ``FrontendActions`` over
16f4a2713aSLionel Sambuccode.
17f4a2713aSLionel Sambuc
18f4a2713aSLionel Sambuc..  See FIXME for a tutorial on how to write FrontendActions.
19f4a2713aSLionel Sambuc
20f4a2713aSLionel SambucIn this tutorial, we'll demonstrate the different ways of running Clang's
21f4a2713aSLionel Sambuc``SyntaxOnlyAction``, which runs a quick syntax check, over a bunch of code.
22f4a2713aSLionel Sambuc
23f4a2713aSLionel SambucParsing a code snippet in memory
24f4a2713aSLionel Sambuc--------------------------------
25f4a2713aSLionel Sambuc
26f4a2713aSLionel SambucIf you ever wanted to run a ``FrontendAction`` over some sample code, for
27f4a2713aSLionel Sambucexample to unit test parts of the Clang AST, ``runToolOnCode`` is what you
28f4a2713aSLionel Sambuclooked for.  Let me give you an example:
29f4a2713aSLionel Sambuc
30f4a2713aSLionel Sambuc.. code-block:: c++
31f4a2713aSLionel Sambuc
32f4a2713aSLionel Sambuc  #include "clang/Tooling/Tooling.h"
33f4a2713aSLionel Sambuc
34f4a2713aSLionel Sambuc  TEST(runToolOnCode, CanSyntaxCheckCode) {
35f4a2713aSLionel Sambuc    // runToolOnCode returns whether the action was correctly run over the
36f4a2713aSLionel Sambuc    // given code.
37f4a2713aSLionel Sambuc    EXPECT_TRUE(runToolOnCode(new clang::SyntaxOnlyAction, "class X {};"));
38f4a2713aSLionel Sambuc  }
39f4a2713aSLionel Sambuc
40f4a2713aSLionel SambucWriting a standalone tool
41f4a2713aSLionel Sambuc-------------------------
42f4a2713aSLionel Sambuc
43f4a2713aSLionel SambucOnce you unit tested your ``FrontendAction`` to the point where it cannot
44f4a2713aSLionel Sambucpossibly break, it's time to create a standalone tool.  For a standalone tool
45f4a2713aSLionel Sambucto run clang, it first needs to figure out what command line arguments to use
46f4a2713aSLionel Sambucfor a specified file.  To that end we create a ``CompilationDatabase``.  There
47f4a2713aSLionel Sambucare different ways to create a compilation database, and we need to support all
48f4a2713aSLionel Sambucof them depending on command-line options.  There's the ``CommonOptionsParser``
49f4a2713aSLionel Sambucclass that takes the responsibility to parse command-line parameters related to
50f4a2713aSLionel Sambuccompilation databases and inputs, so that all tools share the implementation.
51f4a2713aSLionel Sambuc
52f4a2713aSLionel SambucParsing common tools options
53f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^
54f4a2713aSLionel Sambuc
55f4a2713aSLionel Sambuc``CompilationDatabase`` can be read from a build directory or the command line.
56f4a2713aSLionel SambucUsing ``CommonOptionsParser`` allows for explicit specification of a compile
57f4a2713aSLionel Sambuccommand line, specification of build path using the ``-p`` command-line option,
58f4a2713aSLionel Sambucand automatic location of the compilation database using source files paths.
59f4a2713aSLionel Sambuc
60f4a2713aSLionel Sambuc.. code-block:: c++
61f4a2713aSLionel Sambuc
62f4a2713aSLionel Sambuc  #include "clang/Tooling/CommonOptionsParser.h"
63*0a6a1f1dSLionel Sambuc  #include "llvm/Support/CommandLine.h"
64f4a2713aSLionel Sambuc
65f4a2713aSLionel Sambuc  using namespace clang::tooling;
66f4a2713aSLionel Sambuc
67*0a6a1f1dSLionel Sambuc  // Apply a custom category to all command-line options so that they are the
68*0a6a1f1dSLionel Sambuc  // only ones displayed.
69*0a6a1f1dSLionel Sambuc  static llvm::cl::OptionCategory MyToolCategory("my-tool options");
70*0a6a1f1dSLionel Sambuc
71f4a2713aSLionel Sambuc  int main(int argc, const char **argv) {
72f4a2713aSLionel Sambuc    // CommonOptionsParser constructor will parse arguments and create a
73f4a2713aSLionel Sambuc    // CompilationDatabase.  In case of error it will terminate the program.
74*0a6a1f1dSLionel Sambuc    CommonOptionsParser OptionsParser(argc, argv, MyToolCategory);
75f4a2713aSLionel Sambuc
76f4a2713aSLionel Sambuc    // Use OptionsParser.getCompilations() and OptionsParser.getSourcePathList()
77f4a2713aSLionel Sambuc    // to retrieve CompilationDatabase and the list of input file paths.
78f4a2713aSLionel Sambuc  }
79f4a2713aSLionel Sambuc
80f4a2713aSLionel SambucCreating and running a ClangTool
81f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
82f4a2713aSLionel Sambuc
83f4a2713aSLionel SambucOnce we have a ``CompilationDatabase``, we can create a ``ClangTool`` and run
84f4a2713aSLionel Sambucour ``FrontendAction`` over some code.  For example, to run the
85f4a2713aSLionel Sambuc``SyntaxOnlyAction`` over the files "a.cc" and "b.cc" one would write:
86f4a2713aSLionel Sambuc
87f4a2713aSLionel Sambuc.. code-block:: c++
88f4a2713aSLionel Sambuc
89f4a2713aSLionel Sambuc  // A clang tool can run over a number of sources in the same process...
90f4a2713aSLionel Sambuc  std::vector<std::string> Sources;
91f4a2713aSLionel Sambuc  Sources.push_back("a.cc");
92f4a2713aSLionel Sambuc  Sources.push_back("b.cc");
93f4a2713aSLionel Sambuc
94f4a2713aSLionel Sambuc  // We hand the CompilationDatabase we created and the sources to run over into
95f4a2713aSLionel Sambuc  // the tool constructor.
96f4a2713aSLionel Sambuc  ClangTool Tool(OptionsParser.getCompilations(), Sources);
97f4a2713aSLionel Sambuc
98f4a2713aSLionel Sambuc  // The ClangTool needs a new FrontendAction for each translation unit we run
99f4a2713aSLionel Sambuc  // on.  Thus, it takes a FrontendActionFactory as parameter.  To create a
100f4a2713aSLionel Sambuc  // FrontendActionFactory from a given FrontendAction type, we call
101f4a2713aSLionel Sambuc  // newFrontendActionFactory<clang::SyntaxOnlyAction>().
102*0a6a1f1dSLionel Sambuc  int result = Tool.run(newFrontendActionFactory<clang::SyntaxOnlyAction>().get());
103f4a2713aSLionel Sambuc
104f4a2713aSLionel SambucPutting it together --- the first tool
105f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
106f4a2713aSLionel Sambuc
107*0a6a1f1dSLionel SambucNow we combine the two previous steps into our first real tool.  A more advanced
108*0a6a1f1dSLionel Sambucversion of this example tool is also checked into the clang tree at
109f4a2713aSLionel Sambuc``tools/clang-check/ClangCheck.cpp``.
110f4a2713aSLionel Sambuc
111f4a2713aSLionel Sambuc.. code-block:: c++
112f4a2713aSLionel Sambuc
113f4a2713aSLionel Sambuc  // Declares clang::SyntaxOnlyAction.
114f4a2713aSLionel Sambuc  #include "clang/Frontend/FrontendActions.h"
115f4a2713aSLionel Sambuc  #include "clang/Tooling/CommonOptionsParser.h"
116f4a2713aSLionel Sambuc  #include "clang/Tooling/Tooling.h"
117f4a2713aSLionel Sambuc  // Declares llvm::cl::extrahelp.
118f4a2713aSLionel Sambuc  #include "llvm/Support/CommandLine.h"
119f4a2713aSLionel Sambuc
120f4a2713aSLionel Sambuc  using namespace clang::tooling;
121f4a2713aSLionel Sambuc  using namespace llvm;
122f4a2713aSLionel Sambuc
123*0a6a1f1dSLionel Sambuc  // Apply a custom category to all command-line options so that they are the
124*0a6a1f1dSLionel Sambuc  // only ones displayed.
125*0a6a1f1dSLionel Sambuc  static cl::OptionCategory MyToolCategory("my-tool options");
126*0a6a1f1dSLionel Sambuc
127f4a2713aSLionel Sambuc  // CommonOptionsParser declares HelpMessage with a description of the common
128f4a2713aSLionel Sambuc  // command-line options related to the compilation database and input files.
129f4a2713aSLionel Sambuc  // It's nice to have this help message in all tools.
130f4a2713aSLionel Sambuc  static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);
131f4a2713aSLionel Sambuc
132f4a2713aSLionel Sambuc  // A help message for this specific tool can be added afterwards.
133f4a2713aSLionel Sambuc  static cl::extrahelp MoreHelp("\nMore help text...");
134f4a2713aSLionel Sambuc
135f4a2713aSLionel Sambuc  int main(int argc, const char **argv) {
136*0a6a1f1dSLionel Sambuc    CommonOptionsParser OptionsParser(argc, argv, MyToolCategory);
137f4a2713aSLionel Sambuc    ClangTool Tool(OptionsParser.getCompilations(),
138f4a2713aSLionel Sambuc                   OptionsParser.getSourcePathList());
139*0a6a1f1dSLionel Sambuc    return Tool.run(newFrontendActionFactory<clang::SyntaxOnlyAction>().get());
140f4a2713aSLionel Sambuc  }
141f4a2713aSLionel Sambuc
142f4a2713aSLionel SambucRunning the tool on some code
143f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
144f4a2713aSLionel Sambuc
145f4a2713aSLionel SambucWhen you check out and build clang, clang-check is already built and available
146f4a2713aSLionel Sambucto you in bin/clang-check inside your build directory.
147f4a2713aSLionel Sambuc
148f4a2713aSLionel SambucYou can run clang-check on a file in the llvm repository by specifying all the
149f4a2713aSLionel Sambucneeded parameters after a "``--``" separator:
150f4a2713aSLionel Sambuc
151f4a2713aSLionel Sambuc.. code-block:: bash
152f4a2713aSLionel Sambuc
153f4a2713aSLionel Sambuc  $ cd /path/to/source/llvm
154f4a2713aSLionel Sambuc  $ export BD=/path/to/build/llvm
155f4a2713aSLionel Sambuc  $ $BD/bin/clang-check tools/clang/tools/clang-check/ClangCheck.cpp -- \
156f4a2713aSLionel Sambuc        clang++ -D__STDC_CONSTANT_MACROS -D__STDC_LIMIT_MACROS \
157f4a2713aSLionel Sambuc        -Itools/clang/include -I$BD/include -Iinclude \
158f4a2713aSLionel Sambuc        -Itools/clang/lib/Headers -c
159f4a2713aSLionel Sambuc
160f4a2713aSLionel SambucAs an alternative, you can also configure cmake to output a compile command
161f4a2713aSLionel Sambucdatabase into its build directory:
162f4a2713aSLionel Sambuc
163f4a2713aSLionel Sambuc.. code-block:: bash
164f4a2713aSLionel Sambuc
165f4a2713aSLionel Sambuc  # Alternatively to calling cmake, use ccmake, toggle to advanced mode and
166f4a2713aSLionel Sambuc  # set the parameter CMAKE_EXPORT_COMPILE_COMMANDS from the UI.
167f4a2713aSLionel Sambuc  $ cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON .
168f4a2713aSLionel Sambuc
169f4a2713aSLionel SambucThis creates a file called ``compile_commands.json`` in the build directory.
170f4a2713aSLionel SambucNow you can run :program:`clang-check` over files in the project by specifying
171f4a2713aSLionel Sambucthe build path as first argument and some source files as further positional
172f4a2713aSLionel Sambucarguments:
173f4a2713aSLionel Sambuc
174f4a2713aSLionel Sambuc.. code-block:: bash
175f4a2713aSLionel Sambuc
176f4a2713aSLionel Sambuc  $ cd /path/to/source/llvm
177f4a2713aSLionel Sambuc  $ export BD=/path/to/build/llvm
178f4a2713aSLionel Sambuc  $ $BD/bin/clang-check -p $BD tools/clang/tools/clang-check/ClangCheck.cpp
179f4a2713aSLionel Sambuc
180f4a2713aSLionel Sambuc
181f4a2713aSLionel Sambuc.. _libtooling_builtin_includes:
182f4a2713aSLionel Sambuc
183f4a2713aSLionel SambucBuiltin includes
184f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^
185f4a2713aSLionel Sambuc
186f4a2713aSLionel SambucClang tools need their builtin headers and search for them the same way Clang
187f4a2713aSLionel Sambucdoes.  Thus, the default location to look for builtin headers is in a path
188*0a6a1f1dSLionel Sambuc``$(dirname /path/to/tool)/../lib/clang/3.3/include`` relative to the tool
189f4a2713aSLionel Sambucbinary.  This works out-of-the-box for tools running from llvm's toplevel
190f4a2713aSLionel Sambucbinary directory after building clang-headers, or if the tool is running from
191f4a2713aSLionel Sambucthe binary directory of a clang install next to the clang binary.
192f4a2713aSLionel Sambuc
193f4a2713aSLionel SambucTips: if your tool fails to find ``stddef.h`` or similar headers, call the tool
194f4a2713aSLionel Sambucwith ``-v`` and look at the search paths it looks through.
195f4a2713aSLionel Sambuc
196f4a2713aSLionel SambucLinking
197f4a2713aSLionel Sambuc^^^^^^^
198f4a2713aSLionel Sambuc
199f4a2713aSLionel SambucFor a list of libraries to link, look at one of the tools' Makefiles (for
200f4a2713aSLionel Sambucexample `clang-check/Makefile
201f4a2713aSLionel Sambuc<http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/clang-check/Makefile?view=markup>`_).
202