blob: d896a5f8857620767837d8d1c8194ee255d1efa7 [file] [log] [blame]
Manuel Klimekd80d4842012-04-25 14:20:13 +00001<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2 "http://www.w3.org/TR/html4/strict.dtd">
3<html>
4<head>
5<title>LibTooling</title>
6<link type="text/css" rel="stylesheet" href="../menu.css">
7<link type="text/css" rel="stylesheet" href="../content.css">
8</head>
9<body>
Manuel Klimekb7ac6582012-05-24 17:16:23 +000010
11<!--#include virtual="../menu.html.incl"-->
12
Manuel Klimekd80d4842012-04-25 14:20:13 +000013<div id="content">
14
15<h1>LibTooling</h1>
16<p>LibTooling is a library to support writing standalone tools based on
17Clang. This document will provide a basic walkthrough of how to write
18a tool using LibTooling.</p>
19
20<!-- ======================================================================= -->
21<h2 id="intro">Introduction</h2>
22<!-- ======================================================================= -->
23
Alexander Kornienko6fbe9822012-08-24 00:39:14 +000024<p>Tools built with LibTooling, like Clang Plugins, run
25<code>FrontendActions</code> over code.
26<!-- See FIXME for a tutorial on how to write FrontendActions. -->
Manuel Klimekd80d4842012-04-25 14:20:13 +000027In this tutorial, we'll demonstrate the different ways of running clang's
Alexander Kornienko6fbe9822012-08-24 00:39:14 +000028<code>SyntaxOnlyAction</code>, which runs a quick syntax check, over a bunch of
Manuel Klimekd80d4842012-04-25 14:20:13 +000029code.</p>
30
31<!-- ======================================================================= -->
32<h2 id="runoncode">Parsing a code snippet in memory.</h2>
33<!-- ======================================================================= -->
34
Alexander Kornienko6fbe9822012-08-24 00:39:14 +000035<p>If you ever wanted to run a <code>FrontendAction</code> over some sample
36code, for example to unit test parts of the Clang AST,
37<code>runToolOnCode</code> is what you looked for. Let me give you an example:
Manuel Klimekd80d4842012-04-25 14:20:13 +000038<pre>
39 #include "clang/Tooling/Tooling.h"
40
41 TEST(runToolOnCode, CanSyntaxCheckCode) {
42 // runToolOnCode returns whether the action was correctly run over the
43 // given code.
44 EXPECT_TRUE(runToolOnCode(new clang::SyntaxOnlyAction, "class X {};"));
45 }
46</pre>
47
48<!-- ======================================================================= -->
49<h2 id="standalonetool">Writing a standalone tool.</h2>
50<!-- ======================================================================= -->
51
Alexander Kornienko6fbe9822012-08-24 00:39:14 +000052<p>Once you unit tested your <code>FrontendAction</code> to the point where it
53cannot possibly break, it's time to create a standalone tool. For a standalone
54tool to run clang, it first needs to figure out what command line arguments to
55use for a specified file. To that end we create a
56<code>CompilationDatabase</code>. There are different ways to create a
57compilation database, and we need to support all of them depending on
58command-line options. There's the <code>CommonOptionsParser</code> class
59that takes the responsibility to parse command-line parameters related to
60compilation databases and inputs, so that all tools share the implementation.
61</p>
Manuel Klimekd80d4842012-04-25 14:20:13 +000062
Alexander Kornienko6fbe9822012-08-24 00:39:14 +000063<h3 id="parsingcommonoptions">Parsing common tools options.</h3>
64<p><code>CompilationDatabase</code> can be read from a build directory or the
65command line. Using <code>CommonOptionsParser</code> allows for explicit
66specification of a compile command line, specification of build path using the
67<code>-p</code> command-line option, and automatic location of the compilation
68database using source files paths.
Manuel Klimekd80d4842012-04-25 14:20:13 +000069<pre>
Alexander Kornienko6fbe9822012-08-24 00:39:14 +000070#include "clang/Tooling/CommonOptionsParser.h"
71
72using namespace clang::tooling;
73
Manuel Klimekd80d4842012-04-25 14:20:13 +000074int main(int argc, const char **argv) {
Alexander Kornienko6fbe9822012-08-24 00:39:14 +000075 // CommonOptionsParser constructor will parse arguments and create a
76 // CompilationDatabase. In case of error it will terminate the program.
77 CommonOptionsParser OptionsParser(argc, argv);
Manuel Klimekd80d4842012-04-25 14:20:13 +000078
Alexander Kornienko6fbe9822012-08-24 00:39:14 +000079 // Use OptionsParser.GetCompilations() and OptionsParser.GetSourcePathList()
80 // to retrieve CompilationDatabase and the list of input file paths.
Manuel Klimekd80d4842012-04-25 14:20:13 +000081}
82</pre>
83</p>
84
85<h3 id="tool">Creating and running a ClangTool.</h3>
Alexander Kornienko6fbe9822012-08-24 00:39:14 +000086<p>Once we have a <code>CompilationDatabase</code>, we can create a
87<code>ClangTool</code> and run our <code>FrontendAction</code> over some code.
88For example, to run the <code>SyntaxOnlyAction</code> over the files "a.cc" and
89"b.cc" one would write:
Manuel Klimekd80d4842012-04-25 14:20:13 +000090<pre>
91 // A clang tool can run over a number of sources in the same process...
92 std::vector&lt;std::string> Sources;
93 Sources.push_back("a.cc");
94 Sources.push_back("b.cc");
95
96 // We hand the CompilationDatabase we created and the sources to run over into
97 // the tool constructor.
Alexander Kornienko6fbe9822012-08-24 00:39:14 +000098 ClangTool Tool(OptionsParser.GetCompilations(), Sources);
Manuel Klimekd80d4842012-04-25 14:20:13 +000099
100 // The ClangTool needs a new FrontendAction for each translation unit we run
101 // on. Thus, it takes a FrontendActionFactory as parameter. To create a
102 // FrontendActionFactory from a given FrontendAction type, we call
103 // newFrontendActionFactory&lt;clang::SyntaxOnlyAction>().
104 int result = Tool.run(newFrontendActionFactory&lt;clang::SyntaxOnlyAction>());
105</pre>
106</p>
107
108<h3 id="main">Putting it together - the first tool.</h3>
109<p>Now we combine the two previous steps into our first real tool. This example
110tool is also checked into the clang tree at tools/clang-check/ClangCheck.cpp.
111<pre>
Alexander Kornienko6fbe9822012-08-24 00:39:14 +0000112// Declares clang::SyntaxOnlyAction.
Manuel Klimek59d7cc92012-07-12 18:32:50 +0000113#include "clang/Frontend/FrontendActions.h"
Alexander Kornienko6fbe9822012-08-24 00:39:14 +0000114#include "clang/Tooling/CommonOptionsParser.h"
115// Declares llvm::cl::extrahelp.
116#include "llvm/Support/CommandLine.h"
Manuel Klimekd80d4842012-04-25 14:20:13 +0000117
Manuel Klimek59d7cc92012-07-12 18:32:50 +0000118using namespace clang::tooling;
119using namespace llvm;
Manuel Klimekd80d4842012-04-25 14:20:13 +0000120
Alexander Kornienko6fbe9822012-08-24 00:39:14 +0000121// CommonOptionsParser declares HelpMessage with a description of the common
122// command-line options related to the compilation database and input files.
123// It's nice to have this help message in all tools.
124static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);
Manuel Klimekd80d4842012-04-25 14:20:13 +0000125
Alexander Kornienko6fbe9822012-08-24 00:39:14 +0000126// A help message for this specific tool can be added afterwards.
127static cl::extrahelp MoreHelp("\nMore help text...");
Manuel Klimekd80d4842012-04-25 14:20:13 +0000128
Manuel Klimek59d7cc92012-07-12 18:32:50 +0000129int main(int argc, const char **argv) {
Alexander Kornienko6fbe9822012-08-24 00:39:14 +0000130 CommonOptionsParser OptionsParser(argc, argv);
131 ClangTool Tool(OptionsParser.GetCompilations(),
132 OptionsParser.GetSourcePathList());
133 return Tool.run(newFrontendActionFactory&lt;clang::SyntaxOnlyAction&gt;());
Manuel Klimek59d7cc92012-07-12 18:32:50 +0000134}
Manuel Klimekd80d4842012-04-25 14:20:13 +0000135</pre>
136</p>
137
138<h3 id="running">Running the tool on some code.</h3>
139<p>When you check out and build clang, clang-check is already built and
140available to you in bin/clang-check inside your build directory.</p>
141<p>You can run clang-check on a file in the llvm repository by specifying
142all the needed parameters after a "--" separator:
143<pre>
144 $ cd /path/to/source/llvm
145 $ export BD=/path/to/build/llvm
Manuel Klimek59d7cc92012-07-12 18:32:50 +0000146 $ $BD/bin/clang-check tools/clang/tools/clang-check/ClangCheck.cpp -- \
Manuel Klimekd80d4842012-04-25 14:20:13 +0000147 clang++ -D__STDC_CONSTANT_MACROS -D__STDC_LIMIT_MACROS \
148 -Itools/clang/include -I$BD/include -Iinclude -Itools/clang/lib/Headers -c
149</pre>
150</p>
151
152<p>As an alternative, you can also configure cmake to output a compile command
153database into its build directory:
154<pre>
155 # Alternatively to calling cmake, use ccmake, toggle to advanced mode and
156 # set the parameter CMAKE_EXPORT_COMPILE_COMMANDS from the UI.
157 $ cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON .
158</pre>
159</p>
160<p>
161This creates a file called compile_commands.json in the build directory. Now
162you can run clang-check over files in the project by specifying the build path
163as first argument and some source files as further positional arguments:
164<pre>
165 $ cd /path/to/source/llvm
166 $ export BD=/path/to/build/llvm
Manuel Klimek59d7cc92012-07-12 18:32:50 +0000167 $ $BD/bin/clang-check -p $BD tools/clang/tools/clang-check/ClangCheck.cpp
Manuel Klimekd80d4842012-04-25 14:20:13 +0000168</pre>
169</p>
170
Manuel Klimek59d7cc92012-07-12 18:32:50 +0000171<h3 id="builtin">Builtin includes.</h3>
172<p>Clang tools need their builtin headers and search for them the same way clang
173does. Thus, the default location to look for builtin headers is in a path
174$(dirname /path/to/tool)/../lib/clang/3.2/include relative to the tool
175binary. This works out-of-the-box for tools running from llvm's toplevel
176binary directory after building clang-headers, or if the tool is running
177from the binary directory of a clang install next to the clang binary.</p>
178
179<p>Tips: if your tool fails to find stddef.h or similar headers, call
180the tool with -v and look at the search paths it looks through.</p>
181
Manuel Klimek1e282e92012-06-06 17:51:31 +0000182<h3 id="linking">Linking.</h3>
183<p>Please note that this presents the linking requirements at the time of this
184writing. For the most up-to-date information, look at one of the tools'
185Makefiles (for example
186<a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/clang-check/Makefile?view=markup">clang-check/Makefile</a>).
187</p>
188
189<p>To link a binary using the tooling infrastructure, link in the following
190libraries:
191<ul>
Manuel Klimekf38d9322012-06-06 21:30:34 +0000192<li>Tooling</li>
Manuel Klimek1e282e92012-06-06 17:51:31 +0000193<li>Frontend</li>
Manuel Klimekf38d9322012-06-06 21:30:34 +0000194<li>Driver</li>
195<li>Serialization</li>
Manuel Klimek1e282e92012-06-06 17:51:31 +0000196<li>Parse</li>
197<li>Sema</li>
Manuel Klimekf38d9322012-06-06 21:30:34 +0000198<li>Analysis</li>
199<li>Edit</li>
200<li>AST</li>
201<li>Lex</li>
202<li>Basic</li>
Manuel Klimek1e282e92012-06-06 17:51:31 +0000203</ul>
204</p>
205
Manuel Klimekd80d4842012-04-25 14:20:13 +0000206</div>
207</body>
208</html>
209