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