LibTooling.rst 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. ==========
  2. LibTooling
  3. ==========
  4. NOTE: this document applies to the original Clang project, not the DirectX
  5. Compiler. It's made available for informational purposes only.
  6. LibTooling is a library to support writing standalone tools based on Clang.
  7. This document will provide a basic walkthrough of how to write a tool using
  8. LibTooling.
  9. For the information on how to setup Clang Tooling for LLVM see
  10. :doc:`HowToSetupToolingForLLVM`
  11. Introduction
  12. ------------
  13. Tools built with LibTooling, like Clang Plugins, run ``FrontendActions`` over
  14. code.
  15. .. See FIXME for a tutorial on how to write FrontendActions.
  16. In this tutorial, we'll demonstrate the different ways of running Clang's
  17. ``SyntaxOnlyAction``, which runs a quick syntax check, over a bunch of code.
  18. Parsing a code snippet in memory
  19. --------------------------------
  20. If you ever wanted to run a ``FrontendAction`` over some sample code, for
  21. example to unit test parts of the Clang AST, ``runToolOnCode`` is what you
  22. looked for. Let me give you an example:
  23. .. code-block:: c++
  24. #include "clang/Tooling/Tooling.h"
  25. TEST(runToolOnCode, CanSyntaxCheckCode) {
  26. // runToolOnCode returns whether the action was correctly run over the
  27. // given code.
  28. EXPECT_TRUE(runToolOnCode(new clang::SyntaxOnlyAction, "class X {};"));
  29. }
  30. Writing a standalone tool
  31. -------------------------
  32. Once you unit tested your ``FrontendAction`` to the point where it cannot
  33. possibly break, it's time to create a standalone tool. For a standalone tool
  34. to run clang, it first needs to figure out what command line arguments to use
  35. for a specified file. To that end we create a ``CompilationDatabase``. There
  36. are different ways to create a compilation database, and we need to support all
  37. of them depending on command-line options. There's the ``CommonOptionsParser``
  38. class that takes the responsibility to parse command-line parameters related to
  39. compilation databases and inputs, so that all tools share the implementation.
  40. Parsing common tools options
  41. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  42. ``CompilationDatabase`` can be read from a build directory or the command line.
  43. Using ``CommonOptionsParser`` allows for explicit specification of a compile
  44. command line, specification of build path using the ``-p`` command-line option,
  45. and automatic location of the compilation database using source files paths.
  46. .. code-block:: c++
  47. #include "clang/Tooling/CommonOptionsParser.h"
  48. #include "llvm/Support/CommandLine.h"
  49. using namespace clang::tooling;
  50. // Apply a custom category to all command-line options so that they are the
  51. // only ones displayed.
  52. static llvm::cl::OptionCategory MyToolCategory("my-tool options");
  53. int main(int argc, const char **argv) {
  54. // CommonOptionsParser constructor will parse arguments and create a
  55. // CompilationDatabase. In case of error it will terminate the program.
  56. CommonOptionsParser OptionsParser(argc, argv, MyToolCategory);
  57. // Use OptionsParser.getCompilations() and OptionsParser.getSourcePathList()
  58. // to retrieve CompilationDatabase and the list of input file paths.
  59. }
  60. Creating and running a ClangTool
  61. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  62. Once we have a ``CompilationDatabase``, we can create a ``ClangTool`` and run
  63. our ``FrontendAction`` over some code. For example, to run the
  64. ``SyntaxOnlyAction`` over the files "a.cc" and "b.cc" one would write:
  65. .. code-block:: c++
  66. // A clang tool can run over a number of sources in the same process...
  67. std::vector<std::string> Sources;
  68. Sources.push_back("a.cc");
  69. Sources.push_back("b.cc");
  70. // We hand the CompilationDatabase we created and the sources to run over into
  71. // the tool constructor.
  72. ClangTool Tool(OptionsParser.getCompilations(), Sources);
  73. // The ClangTool needs a new FrontendAction for each translation unit we run
  74. // on. Thus, it takes a FrontendActionFactory as parameter. To create a
  75. // FrontendActionFactory from a given FrontendAction type, we call
  76. // newFrontendActionFactory<clang::SyntaxOnlyAction>().
  77. int result = Tool.run(newFrontendActionFactory<clang::SyntaxOnlyAction>().get());
  78. Putting it together --- the first tool
  79. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  80. Now we combine the two previous steps into our first real tool. A more advanced
  81. version of this example tool is also checked into the clang tree at
  82. ``tools/clang-check/ClangCheck.cpp``.
  83. .. code-block:: c++
  84. // Declares clang::SyntaxOnlyAction.
  85. #include "clang/Frontend/FrontendActions.h"
  86. #include "clang/Tooling/CommonOptionsParser.h"
  87. #include "clang/Tooling/Tooling.h"
  88. // Declares llvm::cl::extrahelp.
  89. #include "llvm/Support/CommandLine.h"
  90. using namespace clang::tooling;
  91. using namespace llvm;
  92. // Apply a custom category to all command-line options so that they are the
  93. // only ones displayed.
  94. static cl::OptionCategory MyToolCategory("my-tool options");
  95. // CommonOptionsParser declares HelpMessage with a description of the common
  96. // command-line options related to the compilation database and input files.
  97. // It's nice to have this help message in all tools.
  98. static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);
  99. // A help message for this specific tool can be added afterwards.
  100. static cl::extrahelp MoreHelp("\nMore help text...");
  101. int main(int argc, const char **argv) {
  102. CommonOptionsParser OptionsParser(argc, argv, MyToolCategory);
  103. ClangTool Tool(OptionsParser.getCompilations(),
  104. OptionsParser.getSourcePathList());
  105. return Tool.run(newFrontendActionFactory<clang::SyntaxOnlyAction>().get());
  106. }
  107. Running the tool on some code
  108. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  109. When you check out and build clang, clang-check is already built and available
  110. to you in bin/clang-check inside your build directory.
  111. You can run clang-check on a file in the llvm repository by specifying all the
  112. needed parameters after a "``--``" separator:
  113. .. code-block:: bash
  114. $ cd /path/to/source/llvm
  115. $ export BD=/path/to/build/llvm
  116. $ $BD/bin/clang-check tools/clang/tools/clang-check/ClangCheck.cpp -- \
  117. clang++ -D__STDC_CONSTANT_MACROS -D__STDC_LIMIT_MACROS \
  118. -Itools/clang/include -I$BD/include -Iinclude \
  119. -Itools/clang/lib/Headers -c
  120. As an alternative, you can also configure cmake to output a compile command
  121. database into its build directory:
  122. .. code-block:: bash
  123. # Alternatively to calling cmake, use ccmake, toggle to advanced mode and
  124. # set the parameter CMAKE_EXPORT_COMPILE_COMMANDS from the UI.
  125. $ cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON .
  126. This creates a file called ``compile_commands.json`` in the build directory.
  127. Now you can run :program:`clang-check` over files in the project by specifying
  128. the build path as first argument and some source files as further positional
  129. arguments:
  130. .. code-block:: bash
  131. $ cd /path/to/source/llvm
  132. $ export BD=/path/to/build/llvm
  133. $ $BD/bin/clang-check -p $BD tools/clang/tools/clang-check/ClangCheck.cpp
  134. .. _libtooling_builtin_includes:
  135. Builtin includes
  136. ^^^^^^^^^^^^^^^^
  137. Clang tools need their builtin headers and search for them the same way Clang
  138. does. Thus, the default location to look for builtin headers is in a path
  139. ``$(dirname /path/to/tool)/../lib/clang/3.3/include`` relative to the tool
  140. binary. This works out-of-the-box for tools running from llvm's toplevel
  141. binary directory after building clang-headers, or if the tool is running from
  142. the binary directory of a clang install next to the clang binary.
  143. Tips: if your tool fails to find ``stddef.h`` or similar headers, call the tool
  144. with ``-v`` and look at the search paths it looks through.
  145. Linking
  146. ^^^^^^^
  147. For a list of libraries to link, look at one of the tools' Makefiles (for
  148. example `clang-check/Makefile
  149. <http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/clang-check/Makefile?view=markup>`_).