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