Manuel Klimek | d80d484 | 2012-04-25 14:20:13 +0000 | [diff] [blame] | 1 | <!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 Klimek | b7ac658 | 2012-05-24 17:16:23 +0000 | [diff] [blame] | 10 | |
| 11 | <!--#include virtual="../menu.html.incl"--> |
| 12 | |
Manuel Klimek | d80d484 | 2012-04-25 14:20:13 +0000 | [diff] [blame] | 13 | <div id="content"> |
| 14 | |
| 15 | <h1>LibTooling</h1> |
| 16 | <p>LibTooling is a library to support writing standalone tools based on |
| 17 | Clang. This document will provide a basic walkthrough of how to write |
| 18 | a 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 |
| 25 | code. <!-- See FIXME for a tutorial on how to write FrontendActions. --> |
| 26 | In this tutorial, we'll demonstrate the different ways of running clang's |
| 27 | SyntaxOnlyAction, which runs a quick syntax check, over a bunch of |
| 28 | code.</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 |
| 35 | to unit test parts of the Clang AST, runToolOnCode is what you looked for. Let |
| 36 | me 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 |
| 52 | possibly break, it's time to create a standalone tool. For a standalone tool |
| 53 | to run clang, it first needs to figure out what command line arguments to use |
| 54 | for 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 |
| 58 | compile commands from a build directory or the command line. The following code |
| 59 | allows for both explicit specification of a compile command line, as well as |
| 60 | retrieving the compile commands lines from a database. |
| 61 | <pre> |
| 62 | int 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<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 |
| 92 | FrontendAction over some code. For example, to run the SyntaxOnlyAction over |
| 93 | the 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<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<clang::SyntaxOnlyAction>(). |
| 108 | int result = Tool.run(newFrontendActionFactory<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 |
| 114 | tool 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<std::string> BuildPath( |
| 125 | cl::Positional, |
| 126 | cl::desc("<build-path>")); |
| 127 | |
| 128 | cl::list<std::string> SourcePaths( |
| 129 | cl::Positional, |
| 130 | cl::desc("<source0> [... <sourceN>]"), |
| 131 | cl::OneOrMore); |
| 132 | |
| 133 | int main(int argc, const char **argv) { |
| 134 | llvm::OwningPtr<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<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 |
| 152 | available 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 |
| 154 | all 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 |
| 165 | database 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> |
| 173 | This creates a file called compile_commands.json in the build directory. Now |
| 174 | you can run clang-check over files in the project by specifying the build path |
| 175 | as 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 Klimek | 1e282e9 | 2012-06-06 17:51:31 +0000 | [diff] [blame^] | 183 | <h3 id="linking">Linking.</h3> |
| 184 | <p>Please note that this presents the linking requirements at the time of this |
| 185 | writing. For the most up-to-date information, look at one of the tools' |
| 186 | Makefiles (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 |
| 191 | libraries: |
| 192 | <ul> |
| 193 | <li>Analysis</li> |
| 194 | <li>AST</li> |
| 195 | <li>Basic</li> |
| 196 | <li>Driver</li> |
| 197 | <li>Edit</li> |
| 198 | <li>Frontend</li> |
| 199 | <li>Lex</li> |
| 200 | <li>Parse</li> |
| 201 | <li>Sema</li> |
| 202 | <li>Serialization</li> |
| 203 | <li>Tooling</li> |
| 204 | </ul> |
| 205 | </p> |
| 206 | |
Manuel Klimek | d80d484 | 2012-04-25 14:20:13 +0000 | [diff] [blame] | 207 | </div> |
| 208 | </body> |
| 209 | </html> |
| 210 | |