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