blob: 4535ef4a137ebcd22d6fa6101125b4dc50ce098d [file] [log] [blame]
NAKAMURA Takumi6048f802011-03-18 03:21:38 +00001<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
Cedric Venet084381332009-02-14 20:20:19 +00002 "http://www.w3.org/TR/html4/strict.dtd">
3<!-- Material used from: HTML 4.01 specs: http://www.w3.org/TR/html401/ -->
4<html>
5<head>
Benjamin Kramereaa262b2012-01-15 15:26:07 +00006 <META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
Cedric Venet084381332009-02-14 20:20:19 +00007 <title>Hacking on clang</title>
Benjamin Kramereaa262b2012-01-15 15:26:07 +00008 <link type="text/css" rel="stylesheet" href="menu.css">
9 <link type="text/css" rel="stylesheet" href="content.css">
10 <style type="text/css">
11 pre { margin-left: 1.5em; }
12 </style>
Cedric Venet084381332009-02-14 20:20:19 +000013</head>
14<body>
15<!--#include virtual="menu.html.incl"-->
16<div id="content">
17 <!--*********************************************************************-->
18 <h1>Hacking on Clang</h1>
19 <!--*********************************************************************-->
NAKAMURA Takumi6048f802011-03-18 03:21:38 +000020
Cedric Venet084381332009-02-14 20:20:19 +000021 <p>This document provides some hints for how to get started hacking
22 on Clang for developers who are new to the Clang and/or LLVM
23 codebases.</p>
24 <ul>
Daniel Dunbard6f13c32010-01-22 02:04:46 +000025 <li><a href="#style">Coding Standards</a></li>
Cedric Venet084381332009-02-14 20:20:19 +000026 <li><a href="#docs">Developer Documentation</a></li>
27 <li><a href="#debugging">Debugging</a></li>
Benjamin Kramereaa262b2012-01-15 15:26:07 +000028 <li><a href="#testing">Testing</a>
Eli Friedman05194922009-08-03 19:42:28 +000029 <ul>
NAKAMURA Takumi6048f802011-03-18 03:21:38 +000030 <li><a href="#testingNonWindows">Testing on Unix-like Systems</a></li>
31 <li><a href="#testingWindows">Testing using Visual Studio on Windows</a></li>
32 <li><a href="#testingCommands">Testing on the Command Line</a></li>
33 </ul>
Benjamin Kramereaa262b2012-01-15 15:26:07 +000034 </li>
Eli Friedman05194922009-08-03 19:42:28 +000035 <li><a href="#patches">Creating Patch Files</a></li>
Cedric Venet084381332009-02-14 20:20:19 +000036 <li><a href="#irgen">LLVM IR Generation</a></li>
37 </ul>
NAKAMURA Takumi6048f802011-03-18 03:21:38 +000038
Cedric Venet084381332009-02-14 20:20:19 +000039 <!--=====================================================================-->
Benjamin Kramereaa262b2012-01-15 15:26:07 +000040 <h2 id="style">Coding Standards</h2>
Daniel Dunbard6f13c32010-01-22 02:04:46 +000041 <!--=====================================================================-->
NAKAMURA Takumi6048f802011-03-18 03:21:38 +000042
Daniel Dunbard6f13c32010-01-22 02:04:46 +000043 <p>Clang follows the
44 LLVM <a href="http://llvm.org/docs/CodingStandards.html">Coding
45 Standards</a>. When submitting patches, please take care to follow these standards
46 and to match the style of the code to that present in Clang (for example, in
47 terms of indentation, bracing, and statement spacing).</p>
48
49 <p>Clang has a few additional coding standards:</p>
50 <ul>
51 <li><i>cstdio is forbidden</i>: library code should not output diagnostics
52 or other information using <tt>cstdio</tt>; debugging routines should
53 use <tt>llvm::errs()</tt>. Other uses of <tt>cstdio</tt> impose behavior
54 upon clients and block integrating Clang as a library. Libraries should
55 support <tt>raw_ostream</tt> based interfaces for textual
56 output. See <a href="http://llvm.org/docs/CodingStandards.html#ll_raw_ostream">Coding
57 Standards</a>.</li>
58 </ul>
59
60 <!--=====================================================================-->
Cedric Venet084381332009-02-14 20:20:19 +000061 <h2 id="docs">Developer Documentation</h2>
62 <!--=====================================================================-->
NAKAMURA Takumi6048f802011-03-18 03:21:38 +000063
Cedric Venet084381332009-02-14 20:20:19 +000064 <p>Both Clang and LLVM use doxygen to provide API documentation. Their
65 respective web pages (generated nightly) are here:</p>
66 <ul>
67 <li><a href="http://clang.llvm.org/doxygen">Clang</a></li>
68 <li><a href="http://llvm.org/doxygen">LLVM</a></li>
69 </ul>
NAKAMURA Takumi6048f802011-03-18 03:21:38 +000070
Cedric Venet084381332009-02-14 20:20:19 +000071 <p>For work on the LLVM IR generation, the LLVM assembly language
72 <a href="http://llvm.org/docs/LangRef.html">reference manual</a> is
73 also useful.</p>
74
75 <!--=====================================================================-->
76 <h2 id="debugging">Debugging</h2>
77 <!--=====================================================================-->
NAKAMURA Takumi6048f802011-03-18 03:21:38 +000078
Cedric Venet084381332009-02-14 20:20:19 +000079 <p>Inspecting data structures in a debugger:</p>
80 <ul>
81 <li>Many LLVM and Clang data structures provide
82 a <tt>dump()</tt> method which will print a description of the
83 data structure to <tt>stderr</tt>.</li>
84 <li>The <a href="docs/InternalsManual.html#QualType"><tt>QualType</tt></a>
85 structure is used pervasively. This is a simple value class for
86 wrapping types with qualifiers; you can use
87 the <tt>isConstQualified()</tt>, for example, to get one of the
88 qualifiers, and the <tt>getTypePtr()</tt> method to get the
89 wrapped <tt>Type*</tt> which you can then dump.</li>
Argyrios Kyrtzidis28b907d2012-11-16 00:25:28 +000090 <li>For <a href="http://lldb.llvm.org"> <tt>LLDB</tt></a> users there are
91 data formatters for clang data structures in
92 <a href="http://llvm.org/svn/llvm-project/cfe/trunk/utils/ClangDataFormat.py">
93 <tt>utils/ClangDataFormat.py</tt></a>.</li>
Cedric Venet084381332009-02-14 20:20:19 +000094 </ul>
NAKAMURA Takumi6048f802011-03-18 03:21:38 +000095
Cedric Venet084381332009-02-14 20:20:19 +000096 <!--=====================================================================-->
Douglas Gregord99d49c2011-07-27 05:22:46 +000097 <h3 id="debuggingVisualStudio">Debugging using Visual Studio</h3>
98 <!--=====================================================================-->
99
Nikola Smiljanicd8fe6312014-01-30 07:15:53 +0000100 <p>The files
101 <a href="http://llvm.org/svn/llvm-project/llvm/trunk/utils/llvm.natvis">
102 <tt>utils/llvm.natvis</tt></a> and
103 <a href="http://llvm.org/svn/llvm-project/cfe/trunk/utils/clang.natvis">
104 <tt>utils/clang.natvis</tt></a> provide debugger visualizers
105 that make debugging of more complex data types much easier.</p>
106 <p>Put the files into
107 <tt>%USERPROFILE%\Documents\Visual Studio 2012\Visualizers</tt> or
108 create a symbolic link so they update automatically.</p>
Douglas Gregord99d49c2011-07-27 05:22:46 +0000109
110 <!--=====================================================================-->
Cedric Venet084381332009-02-14 20:20:19 +0000111 <h2 id="testing">Testing</h2>
112 <!--=====================================================================-->
NAKAMURA Takumi6048f802011-03-18 03:21:38 +0000113
Eli Friedman05194922009-08-03 19:42:28 +0000114 <!--=====================================================================-->
115 <h3 id="testingNonWindows">Testing on Unix-like Systems</h3>
116 <!--=====================================================================-->
NAKAMURA Takumi6048f802011-03-18 03:21:38 +0000117
Cedric Venet084381332009-02-14 20:20:19 +0000118 <p>Clang includes a basic regression suite in the tree which can be
119 run with <tt>make test</tt> from the top-level clang directory, or
Eli Friedman05194922009-08-03 19:42:28 +0000120 just <tt>make</tt> in the <em>test</em> sub-directory.
121 <tt>make VERBOSE=1</tt> can be used to show more detail
Cedric Venet084381332009-02-14 20:20:19 +0000122 about what is being run.</p>
123
Daniel Dunbar40e640c2010-06-10 17:01:45 +0000124 <p>If you built LLVM and Clang using CMake, the test suite can be run
125 with <tt>make clang-test</tt> from the top-level LLVM directory.</p>
126
Eli Friedman05194922009-08-03 19:42:28 +0000127 <p>The tests primarily consist of a test runner script running the compiler
128 under test on individual test files grouped in the directories under the
129 test directory. The individual test files include comments at the
130 beginning indicating the Clang compile options to use, to be read
131 by the test runner. Embedded comments also can do things like telling
132 the test runner that an error is expected at the current line.
133 Any output files produced by the test will be placed under
134 a created Output directory.</p>
NAKAMURA Takumi6048f802011-03-18 03:21:38 +0000135
Eli Friedman05194922009-08-03 19:42:28 +0000136 <p>During the run of <tt>make test</tt>, the terminal output will
137 display a line similar to the following:</p>
NAKAMURA Takumi6048f802011-03-18 03:21:38 +0000138
Benjamin Kramereaa262b2012-01-15 15:26:07 +0000139 <pre>--- Running clang tests for i686-pc-linux-gnu ---</pre>
NAKAMURA Takumi6048f802011-03-18 03:21:38 +0000140
Eli Friedman05194922009-08-03 19:42:28 +0000141 <p>followed by a line continually overwritten with the current test
142 file being compiled, and an overall completion percentage.</p>
NAKAMURA Takumi6048f802011-03-18 03:21:38 +0000143
Eli Friedman05194922009-08-03 19:42:28 +0000144 <p>After the <tt>make test</tt> run completes, the absence of any
145 <tt>Failing Tests (count):</tt> message indicates that no tests
146 failed unexpectedly. If any tests did fail, the
147 <tt>Failing Tests (count):</tt> message will be followed by a list
148 of the test source file paths that failed. For example:</p>
149
Benjamin Kramereaa262b2012-01-15 15:26:07 +0000150 <pre>
Eli Friedman05194922009-08-03 19:42:28 +0000151 Failing Tests (3):
152 /home/john/llvm/tools/clang/test/SemaCXX/member-name-lookup.cpp
153 /home/john/llvm/tools/clang/test/SemaCXX/namespace-alias.cpp
154 /home/john/llvm/tools/clang/test/SemaCXX/using-directive.cpp
Benjamin Kramereaa262b2012-01-15 15:26:07 +0000155</pre>
Eli Friedman05194922009-08-03 19:42:28 +0000156
157 <p>If you used the <tt>make VERBOSE=1</tt> option, the terminal
158 output will reflect the error messages from the compiler and
159 test runner.</p>
160
Cedric Venet084381332009-02-14 20:20:19 +0000161 <p>The regression suite can also be run with Valgrind by running
162 <tt>make test VG=1</tt> in the top-level clang directory.</p>
163
164 <p>For more intensive changes, running
165 the <a href="http://llvm.org/docs/TestingGuide.html#testsuiterun">LLVM
166 Test Suite</a> with clang is recommended. Currently the best way to
Daniel Dunbar0c4a9592009-08-06 16:47:53 +0000167 override LLVMGCC, as in: <tt>make LLVMGCC="clang -std=gnu89"
168 TEST=nightly report</tt> (make sure <tt>clang</tt> is in your PATH or use the
Cedric Venet084381332009-02-14 20:20:19 +0000169 full path).</p>
NAKAMURA Takumi6048f802011-03-18 03:21:38 +0000170
Eli Friedman05194922009-08-03 19:42:28 +0000171 <!--=====================================================================-->
172 <h3 id="testingWindows">Testing using Visual Studio on Windows</h3>
173 <!--=====================================================================-->
174
John Thompsona8cb86b2009-11-06 00:06:29 +0000175 <p>The Clang test suite can be run from either Visual Studio or
176 the command line.</p>
NAKAMURA Takumi6048f802011-03-18 03:21:38 +0000177
John Thompsona8cb86b2009-11-06 00:06:29 +0000178 <p>Note that the test runner is based on
Eli Friedman05194922009-08-03 19:42:28 +0000179 Python, which must be installed. Find Python at:
NAKAMURA Takumi6048f802011-03-18 03:21:38 +0000180 <a href="http://www.python.org/download/">http://www.python.org/download/</a>.
Eli Friedman05194922009-08-03 19:42:28 +0000181 Download the latest stable version (2.6.2 at the time of this writing).</p>
NAKAMURA Takumi6048f802011-03-18 03:21:38 +0000182
Chris Lattnerf6bad862009-08-20 06:17:11 +0000183 <p>The GnuWin32 tools are also necessary for running the tests.
NAKAMURA Takumi6048f802011-03-18 03:21:38 +0000184 Get them from <a href="http://getgnuwin32.sourceforge.net/">
NAKAMURA Takumi2d60add2011-03-18 03:21:44 +0000185 http://getgnuwin32.sourceforge.net/</a>.
186 If the environment variable <tt>%PATH%</tt> does not have GnuWin32,
187 or if other grep(s) supercedes GnuWin32 on <tt>%PATH%,</tt>
188 you should specify <tt>LLVM_LIT_TOOLS_DIR</tt>
189 to CMake explicitly.</p>
NAKAMURA Takumi6048f802011-03-18 03:21:38 +0000190
John Thompsona8cb86b2009-11-06 00:06:29 +0000191 <p>The cmake build tool is set up to create Visual Studio project files
192 for running the tests, "clang-test" being the root. Therefore, to
193 run the test from Visual Studio, right-click the clang-test project
194 and select "Build".</p>
NAKAMURA Takumi6048f802011-03-18 03:21:38 +0000195
NAKAMURA Takumi2d60add2011-03-18 03:21:44 +0000196 <p>
197 Please see also
198 <a href="http://llvm.org/docs/GettingStartedVS.html">Getting Started
199 with the LLVM System using Microsoft Visual Studio</a> and
200 <a href="http://llvm.org/docs/CMake.html">Building LLVM with CMake</a>.
201 </p>
202
Dawn Perchik90ca3162010-09-03 17:01:13 +0000203 <!--=====================================================================-->
204 <h3 id="testingCommands">Testing on the Command Line</h3>
205 <!--=====================================================================-->
206
Eli Friedman250ae4a2012-04-16 05:04:45 +0000207 <p>If you want more control over how the tests are run, it may
208 be convenient to run the test harness on the command-line directly. Before
209 running tests from the command line, you will need to ensure that
210 <tt>lit.site.cfg</tt> files have been created for your build. You can do
211 this by running the tests as described in the previous sections. Once the
212 tests have started running, you can stop them with control+C, as the
213 files are generated before running any tests.</p>
214
215 <p>Once that is done, to run all the tests from the command line,
216 execute a command like the following:</p>
NAKAMURA Takumi6048f802011-03-18 03:21:38 +0000217
Benjamin Kramereaa262b2012-01-15 15:26:07 +0000218 <pre>
Aaron Ballman759c2a42012-02-09 15:23:18 +0000219 python (path to llvm)\llvm\utils\lit\lit.py -sv
220 --param=build_mode=Win32 --param=build_config=Debug
221 --param=clang_site_config=(build dir)\tools\clang\test\lit.site.cfg
222 (path to llvm)\llvm\tools\clang\test
Benjamin Kramereaa262b2012-01-15 15:26:07 +0000223</pre>
John Thompsona8cb86b2009-11-06 00:06:29 +0000224
Douglas Gregor52552c82011-01-24 18:04:58 +0000225 <p>For CMake builds e.g. on Windows with Visual Studio, you will need
226 to specify your build configuration (Debug, Release, etc.) via
Aaron Ballman759c2a42012-02-09 15:23:18 +0000227 <tt>--param=build_config=(build config)</tt>. You may also need to specify
228 the build mode (Win32, etc) via <tt>--param=build_mode=(build mode)</tt>.</p>
229
230 <p>Additionally, you will need to specify the lit site configuration which
231 lives in (build dir)\tools\clang\test, via
232 <tt>--param=clang_site_config=(build dir)\tools\clang\test\lit.site.cfg</tt>.
233 </p>
Douglas Gregor52552c82011-01-24 18:04:58 +0000234
John Thompsona8cb86b2009-11-06 00:06:29 +0000235 <p>To run a single test:</p>
NAKAMURA Takumi6048f802011-03-18 03:21:38 +0000236
Benjamin Kramereaa262b2012-01-15 15:26:07 +0000237 <pre>
Aaron Ballman759c2a42012-02-09 15:23:18 +0000238 python (path to llvm)\llvm\utils\lit\lit.py -sv
239 --param=build_mode=Win32 --param=build_config=Debug
240 --param=clang_site_config=(build dir)\tools\clang\test\lit.site.cfg
241 (path to llvm)\llvm\tools\clang\test\(dir)\(test)
Benjamin Kramereaa262b2012-01-15 15:26:07 +0000242</pre>
John Thompsona8cb86b2009-11-06 00:06:29 +0000243
244 <p>For example:</p>
NAKAMURA Takumi6048f802011-03-18 03:21:38 +0000245
Benjamin Kramereaa262b2012-01-15 15:26:07 +0000246 <pre>
Aaron Ballman759c2a42012-02-09 15:23:18 +0000247 python C:\Tool\llvm\utils\lit\lit.py -sv
248 --param=build_mode=Win32 --param=build_config=Debug
249 --param=clang_site_config=c:\Tools\build\tools\clang\test\lit.site.cfg
250 C:\Tools\llvm\tools\clang\test\Sema\wchar.c
Benjamin Kramereaa262b2012-01-15 15:26:07 +0000251</pre>
John Thompsona8cb86b2009-11-06 00:06:29 +0000252
253 <p>The -sv option above tells the runner to show the test output if
254 any tests failed, to help you determine the cause of failure.</p>
255
Aaron Ballman759c2a42012-02-09 15:23:18 +0000256 <p>You can also pass in the --no-progress-bar option if you wish to disable
257 progress indications while the tests are running.</p>
258
Francois Pichet96723472010-09-11 21:16:11 +0000259 <p>Your output might look something like this:</p>
John Thompsona8cb86b2009-11-06 00:06:29 +0000260
Aaron Ballman759c2a42012-02-09 15:23:18 +0000261 <pre>lit.py: lit.cfg:152: note: using clang: 'C:\Tools\llvm\bin\Release\clang.EXE'
Francois Pichet96723472010-09-11 21:16:11 +0000262-- Testing: Testing: 2534 tests, 4 threads --
263Testing: 0 .. 10.. 20.. 30.. 40.. 50.. 60.. 70.. 80.. 90..
264Testing Time: 81.52s
265 Expected Passes : 2503
266 Expected Failures : 28
267 Unsupported Tests : 3
Benjamin Kramereaa262b2012-01-15 15:26:07 +0000268</pre>
John Thompsona8cb86b2009-11-06 00:06:29 +0000269
Francois Pichet96723472010-09-11 21:16:11 +0000270 <p>The statistic, "Unexpected Failures" (not shown if all tests pass), is the important one.</p>
Eli Friedman05194922009-08-03 19:42:28 +0000271
272 <!--=====================================================================-->
273 <h2 id="patches">Creating Patch Files</h2>
274 <!--=====================================================================-->
275
276 <p>To return changes to the Clang team, unless you have checkin
Chris Lattner57540c52011-04-15 05:22:18 +0000277 privileges, the preferred way is to send patch files to the
Douglas Gregor328d8d22011-11-19 19:14:26 +0000278 cfe-commits mailing list, with an explanation of what the patch is
Nico Weber39f17b62014-06-12 21:15:10 +0000279 for. clang follows <a
280 href="http://llvm.org/docs/DeveloperPolicy.html">LLVM's developer policy</a>.
281 If your patch requires a wider discussion (for example, because it is an
282 architectural change), you can use the cfe-dev mailing list.</p>
NAKAMURA Takumi6048f802011-03-18 03:21:38 +0000283
Eli Friedman05194922009-08-03 19:42:28 +0000284 <p>To create these patch files, change directory
285 to the llvm/tools/clang root and run:</p>
NAKAMURA Takumi6048f802011-03-18 03:21:38 +0000286
Benjamin Kramereaa262b2012-01-15 15:26:07 +0000287 <pre>svn diff (relative path) >(patch file name)</pre>
Eli Friedman05194922009-08-03 19:42:28 +0000288
289 <p>For example, for getting the diffs of all of clang:</p>
NAKAMURA Takumi6048f802011-03-18 03:21:38 +0000290
Benjamin Kramereaa262b2012-01-15 15:26:07 +0000291 <pre>svn diff . >~/mypatchfile.patch</pre>
Eli Friedman05194922009-08-03 19:42:28 +0000292
293 <p>For example, for getting the diffs of a single file:</p>
NAKAMURA Takumi6048f802011-03-18 03:21:38 +0000294
Benjamin Kramereaa262b2012-01-15 15:26:07 +0000295 <pre>svn diff lib/Parse/ParseDeclCXX.cpp >~/ParseDeclCXX.patch</pre>
Eli Friedman05194922009-08-03 19:42:28 +0000296
297 <p>Note that the paths embedded in the patch depend on where you run it,
298 so changing directory to the llvm/tools/clang directory is recommended.</p>
Cedric Venet084381332009-02-14 20:20:19 +0000299
300 <!--=====================================================================-->
301 <h2 id="irgen">LLVM IR Generation</h2>
302 <!--=====================================================================-->
303
304 <p>The LLVM IR generation part of clang handles conversion of the
305 AST nodes output by the Sema module to the LLVM Intermediate
306 Representation (IR). Historically, this was referred to as
307 "codegen", and the Clang code for this lives
308 in <tt>lib/CodeGen</tt>.</p>
NAKAMURA Takumi6048f802011-03-18 03:21:38 +0000309
Cedric Venet084381332009-02-14 20:20:19 +0000310 <p>The output is most easily inspected using the <tt>-emit-llvm</tt>
311 option to clang (possibly in conjunction with <tt>-o -</tt>). You
312 can also use <tt>-emit-llvm-bc</tt> to write an LLVM bitcode file
313 which can be processed by the suite of LLVM tools
314 like <tt>llvm-dis</tt>, <tt>llvm-nm</tt>, etc. See the LLVM
315 <a href="http://llvm.org/docs/CommandGuide/">Command Guide</a>
316 for more information.</p>
317
318</div>
319</body>
320</html>