HowToSetupToolingForLLVM.rst 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. ===================================
  2. How To Setup Clang Tooling For LLVM
  3. ===================================
  4. Clang Tooling provides infrastructure to write tools that need syntactic
  5. and semantic information about a program. This term also relates to a set
  6. of specific tools using this infrastructure (e.g. ``clang-check``). This
  7. document provides information on how to set up and use Clang Tooling for
  8. the LLVM source code.
  9. Introduction
  10. ============
  11. Clang Tooling needs a compilation database to figure out specific build
  12. options for each file. Currently it can create a compilation database
  13. from the ``compilation_commands.json`` file, generated by CMake. When
  14. invoking clang tools, you can either specify a path to a build directory
  15. using a command line parameter ``-p`` or let Clang Tooling find this
  16. file in your source tree. In either case you need to configure your
  17. build using CMake to use clang tools.
  18. Setup Clang Tooling Using CMake and Make
  19. ========================================
  20. If you intend to use make to build LLVM, you should have CMake 2.8.6 or
  21. later installed (can be found `here <http://cmake.org>`_).
  22. First, you need to generate Makefiles for LLVM with CMake. You need to
  23. make a build directory and run CMake from it:
  24. .. code-block:: console
  25. $ mkdir your/build/directory
  26. $ cd your/build/directory
  27. $ cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON path/to/llvm/sources
  28. If you want to use clang instead of GCC, you can add
  29. ``-DCMAKE_C_COMPILER=/path/to/clang -DCMAKE_CXX_COMPILER=/path/to/clang++``.
  30. You can also use ``ccmake``, which provides a curses interface to configure
  31. CMake variables for lazy people.
  32. As a result, the new ``compile_commands.json`` file should appear in the
  33. current directory. You should link it to the LLVM source tree so that
  34. Clang Tooling is able to use it:
  35. .. code-block:: console
  36. $ ln -s $PWD/compile_commands.json path/to/llvm/source/
  37. Now you are ready to build and test LLVM using make:
  38. .. code-block:: console
  39. $ make check-all
  40. Using Clang Tools
  41. =================
  42. After you completed the previous steps, you are ready to run clang tools. If
  43. you have a recent clang installed, you should have ``clang-check`` in
  44. ``$PATH``. Try to run it on any ``.cpp`` file inside the LLVM source tree:
  45. .. code-block:: console
  46. $ clang-check tools/clang/lib/Tooling/CompilationDatabase.cpp
  47. If you're using vim, it's convenient to have clang-check integrated. Put
  48. this into your ``.vimrc``:
  49. ::
  50. function! ClangCheckImpl(cmd)
  51. if &autowrite | wall | endif
  52. echo "Running " . a:cmd . " ..."
  53. let l:output = system(a:cmd)
  54. cexpr l:output
  55. cwindow
  56. let w:quickfix_title = a:cmd
  57. if v:shell_error != 0
  58. cc
  59. endif
  60. let g:clang_check_last_cmd = a:cmd
  61. endfunction
  62. function! ClangCheck()
  63. let l:filename = expand('%')
  64. if l:filename =~ '\.\(cpp\|cxx\|cc\|c\)$'
  65. call ClangCheckImpl("clang-check " . l:filename)
  66. elseif exists("g:clang_check_last_cmd")
  67. call ClangCheckImpl(g:clang_check_last_cmd)
  68. else
  69. echo "Can't detect file's compilation arguments and no previous clang-check invocation!"
  70. endif
  71. endfunction
  72. nmap <silent> <F5> :call ClangCheck()<CR><CR>
  73. When editing a .cpp/.cxx/.cc/.c file, hit F5 to reparse the file. In
  74. case the current file has a different extension (for example, .h), F5
  75. will re-run the last clang-check invocation made from this vim instance
  76. (if any). The output will go into the error window, which is opened
  77. automatically when clang-check finds errors, and can be re-opened with
  78. ``:cope``.
  79. Other ``clang-check`` options that can be useful when working with clang
  80. AST:
  81. * ``-ast-print`` --- Build ASTs and then pretty-print them.
  82. * ``-ast-dump`` --- Build ASTs and then debug dump them.
  83. * ``-ast-dump-filter=<string>`` --- Use with ``-ast-dump`` or ``-ast-print`` to
  84. dump/print only AST declaration nodes having a certain substring in a
  85. qualified name. Use ``-ast-list`` to list all filterable declaration node
  86. names.
  87. * ``-ast-list`` --- Build ASTs and print the list of declaration node qualified
  88. names.
  89. Examples:
  90. .. code-block:: console
  91. $ clang-check tools/clang/tools/clang-check/ClangCheck.cpp -ast-dump -ast-dump-filter ActionFactory::newASTConsumer
  92. Processing: tools/clang/tools/clang-check/ClangCheck.cpp.
  93. Dumping ::ActionFactory::newASTConsumer:
  94. clang::ASTConsumer *newASTConsumer() (CompoundStmt 0x44da290 </home/alexfh/local/llvm/tools/clang/tools/clang-check/ClangCheck.cpp:64:40, line:72:3>
  95. (IfStmt 0x44d97c8 <line:65:5, line:66:45>
  96. <<<NULL>>>
  97. (ImplicitCastExpr 0x44d96d0 <line:65:9> '_Bool':'_Bool' <UserDefinedConversion>
  98. ...
  99. $ clang-check tools/clang/tools/clang-check/ClangCheck.cpp -ast-print -ast-dump-filter ActionFactory::newASTConsumer
  100. Processing: tools/clang/tools/clang-check/ClangCheck.cpp.
  101. Printing <anonymous namespace>::ActionFactory::newASTConsumer:
  102. clang::ASTConsumer *newASTConsumer() {
  103. if (this->ASTList.operator _Bool())
  104. return clang::CreateASTDeclNodeLister();
  105. if (this->ASTDump.operator _Bool())
  106. return clang::CreateASTDumper(this->ASTDumpFilter);
  107. if (this->ASTPrint.operator _Bool())
  108. return clang::CreateASTPrinter(&llvm::outs(), this->ASTDumpFilter);
  109. return new clang::ASTConsumer();
  110. }
  111. (Experimental) Using Ninja Build System
  112. =======================================
  113. Optionally you can use the `Ninja <https://github.com/martine/ninja>`_
  114. build system instead of make. It is aimed at making your builds faster.
  115. Currently this step will require building Ninja from sources.
  116. To take advantage of using Clang Tools along with Ninja build you need
  117. at least CMake 2.8.9.
  118. Clone the Ninja git repository and build Ninja from sources:
  119. .. code-block:: console
  120. $ git clone git://github.com/martine/ninja.git
  121. $ cd ninja/
  122. $ ./bootstrap.py
  123. This will result in a single binary ``ninja`` in the current directory.
  124. It doesn't require installation and can just be copied to any location
  125. inside ``$PATH``, say ``/usr/local/bin/``:
  126. .. code-block:: console
  127. $ sudo cp ninja /usr/local/bin/
  128. $ sudo chmod a+rx /usr/local/bin/ninja
  129. After doing all of this, you'll need to generate Ninja build files for
  130. LLVM with CMake. You need to make a build directory and run CMake from
  131. it:
  132. .. code-block:: console
  133. $ mkdir your/build/directory
  134. $ cd your/build/directory
  135. $ cmake -G Ninja -DCMAKE_EXPORT_COMPILE_COMMANDS=ON path/to/llvm/sources
  136. If you want to use clang instead of GCC, you can add
  137. ``-DCMAKE_C_COMPILER=/path/to/clang -DCMAKE_CXX_COMPILER=/path/to/clang++``.
  138. You can also use ``ccmake``, which provides a curses interface to configure
  139. CMake variables in an interactive manner.
  140. As a result, the new ``compile_commands.json`` file should appear in the
  141. current directory. You should link it to the LLVM source tree so that
  142. Clang Tooling is able to use it:
  143. .. code-block:: console
  144. $ ln -s $PWD/compile_commands.json path/to/llvm/source/
  145. Now you are ready to build and test LLVM using Ninja:
  146. .. code-block:: console
  147. $ ninja check-all
  148. Other target names can be used in the same way as with make.