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