HowToSetupToolingForLLVM.rst 7.1 KB

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