annotate clang/docs/HowToSetupToolingForLLVM.rst @ 221:79ff65ed7e25

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