blob: 44e4123d8cfb6839db62ebcfc97411dcb91e3666 [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
24<p>Tools built with LibTooling, like Clang Plugins, run FrontendActions over
25code. <!-- See FIXME for a tutorial on how to write FrontendActions. -->
26In this tutorial, we'll demonstrate the different ways of running clang's
27SyntaxOnlyAction, which runs a quick syntax check, over a bunch of
28code.</p>
29
30<!-- ======================================================================= -->
31<h2 id="runoncode">Parsing a code snippet in memory.</h2>
32<!-- ======================================================================= -->
33
34<p>If you ever wanted to run a FrontendAction over some sample code, for example
35to unit test parts of the Clang AST, runToolOnCode is what you looked for. Let
36me give you an example:
37<pre>
38 #include "clang/Tooling/Tooling.h"
39
40 TEST(runToolOnCode, CanSyntaxCheckCode) {
41 // runToolOnCode returns whether the action was correctly run over the
42 // given code.
43 EXPECT_TRUE(runToolOnCode(new clang::SyntaxOnlyAction, "class X {};"));
44 }
45</pre>
46
47<!-- ======================================================================= -->
48<h2 id="standalonetool">Writing a standalone tool.</h2>
49<!-- ======================================================================= -->
50
51<p>Once you unit tested your FrontendAction to the point where it cannot
52possibly break, it's time to create a standalone tool. For a standalone tool
53to run clang, it first needs to figure out what command line arguments to use
54for a specified file. To that end we create a CompilationDatabase.</p>
55
56<h3 id="compilationdb">Creating a compilation database.</h3>
57<p>CompilationDatabase provides static factory functions to help with parsing
58compile commands from a build directory or the command line. The following code
59allows for both explicit specification of a compile command line, as well as
60retrieving the compile commands lines from a database.
61<pre>
62int main(int argc, const char **argv) {
63 // First, try to create a fixed compile command database from the command line
64 // arguments.
65 llvm::OwningPtr&lt;CompilationDatabase> Compilations(
66 FixedCompilationDatabase::loadFromCommandLine(argc, argv));
67
68 // Next, use normal llvm command line parsing to get the tool specific
69 // parameters.
70 cl::ParseCommandLineOptions(argc, argv);
71
72 if (!Compilations) {
73 // In case the user did not specify the compile command line via positional
74 // command line arguments after "--", try to load the compile commands from
75 // a database in the specified build directory.
76 std::string ErrorMessage;
77 Compilations.reset(CompilationDatabase::loadFromDirectory(BuildPath,
78 ErrorMessage));
79
80 // If there is still no valid compile command database, we don't know how
81 // to run the tool.
82 if (!Compilations)
83 llvm::report_fatal_error(ErrorMessage);
84 }
85...
86}
87</pre>
88</p>
89
90<h3 id="tool">Creating and running a ClangTool.</h3>
91<p>Once we have a CompilationDatabase, we can create a ClangTool and run our
92FrontendAction over some code. For example, to run the SyntaxOnlyAction over
93the files "a.cc" and "b.cc" one would write:
94<pre>
95 // A clang tool can run over a number of sources in the same process...
96 std::vector&lt;std::string> Sources;
97 Sources.push_back("a.cc");
98 Sources.push_back("b.cc");
99
100 // We hand the CompilationDatabase we created and the sources to run over into
101 // the tool constructor.
102 ClangTool Tool(*Compilations, Sources);
103
104 // The ClangTool needs a new FrontendAction for each translation unit we run
105 // on. Thus, it takes a FrontendActionFactory as parameter. To create a
106 // FrontendActionFactory from a given FrontendAction type, we call
107 // newFrontendActionFactory&lt;clang::SyntaxOnlyAction>().
108 int result = Tool.run(newFrontendActionFactory&lt;clang::SyntaxOnlyAction>());
109</pre>
110</p>
111
112<h3 id="main">Putting it together - the first tool.</h3>
113<p>Now we combine the two previous steps into our first real tool. This example
114tool is also checked into the clang tree at tools/clang-check/ClangCheck.cpp.
115<pre>
116 #include "llvm/Support/CommandLine.h"
117 #include "clang/Frontend/FrontendActions.h"
118 #include "clang/Tooling/CompilationDatabase.h"
119 #include "clang/Tooling/Tooling.h"
120
121 using namespace clang::tooling;
122 using namespace llvm;
123
124 cl::opt&lt;std::string> BuildPath(
125 cl::Positional,
126 cl::desc("&lt;build-path>"));
127
128 cl::list&lt;std::string> SourcePaths(
129 cl::Positional,
130 cl::desc("&lt;source0> [... &lt;sourceN>]"),
131 cl::OneOrMore);
132
133 int main(int argc, const char **argv) {
134 llvm::OwningPtr&lt;CompilationDatabase> Compilations(
135 FixedCompilationDatabase::loadFromCommandLine(argc, argv));
136 cl::ParseCommandLineOptions(argc, argv);
137 if (!Compilations) {
138 std::string ErrorMessage;
139 Compilations.reset(CompilationDatabase::loadFromDirectory(BuildPath,
140 ErrorMessage));
141 if (!Compilations)
142 llvm::report_fatal_error(ErrorMessage);
143 }
144 ClangTool Tool(*Compilations, SourcePaths);
145 return Tool.run(newFrontendActionFactory&lt;clang::SyntaxOnlyAction>());
146 }
147</pre>
148</p>
149
150<h3 id="running">Running the tool on some code.</h3>
151<p>When you check out and build clang, clang-check is already built and
152available to you in bin/clang-check inside your build directory.</p>
153<p>You can run clang-check on a file in the llvm repository by specifying
154all the needed parameters after a "--" separator:
155<pre>
156 $ cd /path/to/source/llvm
157 $ export BD=/path/to/build/llvm
158 $ $BD/bin/clang-check . tools/clang/tools/clang-check/ClangCheck.cpp -- \
159 clang++ -D__STDC_CONSTANT_MACROS -D__STDC_LIMIT_MACROS \
160 -Itools/clang/include -I$BD/include -Iinclude -Itools/clang/lib/Headers -c
161</pre>
162</p>
163
164<p>As an alternative, you can also configure cmake to output a compile command
165database into its build directory:
166<pre>
167 # Alternatively to calling cmake, use ccmake, toggle to advanced mode and
168 # set the parameter CMAKE_EXPORT_COMPILE_COMMANDS from the UI.
169 $ cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON .
170</pre>
171</p>
172<p>
173This creates a file called compile_commands.json in the build directory. Now
174you can run clang-check over files in the project by specifying the build path
175as first argument and some source files as further positional arguments:
176<pre>
177 $ cd /path/to/source/llvm
178 $ export BD=/path/to/build/llvm
179 $ $BD/bin/clang-check $BD tools/clang/tools/clang-check/ClangCheck.cpp
180</pre>
181</p>
182
Manuel Klimek1e282e92012-06-06 17:51:31 +0000183<h3 id="linking">Linking.</h3>
184<p>Please note that this presents the linking requirements at the time of this
185writing. For the most up-to-date information, look at one of the tools'
186Makefiles (for example
187<a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/clang-check/Makefile?view=markup">clang-check/Makefile</a>).
188</p>
189
190<p>To link a binary using the tooling infrastructure, link in the following
191libraries:
192<ul>
Manuel Klimekf38d9322012-06-06 21:30:34 +0000193<li>Tooling</li>
Manuel Klimek1e282e92012-06-06 17:51:31 +0000194<li>Frontend</li>
Manuel Klimekf38d9322012-06-06 21:30:34 +0000195<li>Driver</li>
196<li>Serialization</li>
Manuel Klimek1e282e92012-06-06 17:51:31 +0000197<li>Parse</li>
198<li>Sema</li>
Manuel Klimekf38d9322012-06-06 21:30:34 +0000199<li>Analysis</li>
200<li>Edit</li>
201<li>AST</li>
202<li>Lex</li>
203<li>Basic</li>
Manuel Klimek1e282e92012-06-06 17:51:31 +0000204</ul>
205</p>
206
Manuel Klimekd80d4842012-04-25 14:20:13 +0000207</div>
208</body>
209</html>
210