1 <!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01//EN"
2 "http://www.w3.org/TR/html4/strict.dtd">
3 <!-- Material used from: HTML 4.01 specs: http://www.w3.org/TR/html401/ -->
6 <META http-equiv=
"Content-Type" content=
"text/html; charset=ISO-8859-1">
7 <title>Hacking on clang
</title>
8 <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; }
15 <!--#include virtual="menu.html.incl"-->
17 <!--*********************************************************************-->
18 <h1>Hacking on Clang
</h1>
19 <!--*********************************************************************-->
21 <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
25 <li><a href=
"#style">Coding Standards
</a></li>
26 <li><a href=
"#docs">Developer Documentation
</a></li>
27 <li><a href=
"#debugging">Debugging
</a></li>
28 <li><a href=
"#testing">Testing
</a>
30 <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>
35 <li><a href=
"#patches">Creating Patch Files
</a></li>
36 <li><a href=
"#irgen">LLVM IR Generation
</a></li>
39 <!--=====================================================================-->
40 <h2 id=
"style">Coding Standards
</h2>
41 <!--=====================================================================-->
44 LLVM
<a href=
"https://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>
49 <p>Clang has a few additional coding standards:
</p>
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=
"https://llvm.org/docs/CodingStandards.html#use-raw-ostream">Coding
60 <!--=====================================================================-->
61 <h2 id=
"docs">Developer Documentation
</h2>
62 <!--=====================================================================-->
64 <p>Both Clang and LLVM use doxygen to provide API documentation. Their
65 respective web pages (generated nightly) are here:
</p>
67 <li><a href=
"https://clang.llvm.org/doxygen">Clang
</a></li>
68 <li><a href=
"https://llvm.org/doxygen">LLVM
</a></li>
71 <p>For work on the LLVM IR generation, the LLVM assembly language
72 <a href=
"https://llvm.org/docs/LangRef.html">reference manual
</a> is
75 <!--=====================================================================-->
76 <h2 id=
"debugging">Debugging
</h2>
77 <!--=====================================================================-->
79 <p>Inspecting data structures in a debugger:
</p>
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>
90 <li>For
<a href=
"https://lldb.llvm.org"> <tt>LLDB
</tt></a> users there are
91 data formatters for clang data structures in
92 <a href=
"https://github.com/llvm/llvm-project/blob/main/clang/utils/ClangDataFormat.py">
93 <tt>clang/utils/ClangDataFormat.py
</tt></a>.
</li>
96 <!--=====================================================================-->
97 <h3 id=
"debuggingVisualStudio">Debugging using Visual Studio
</h3>
98 <!--=====================================================================-->
101 <a href=
"https://github.com/llvm/llvm-project/blob/main/llvm/utils/LLVMVisualizers/llvm.natvis">
102 <tt>llvm/utils/LLVMVisualizers/llvm.natvis
</tt></a> and
103 <a href=
"https://github.com/llvm/llvm-project/blob/main/clang/utils/ClangVisualizers/clang.natvis">
104 <tt>clang/utils/ClangVisualizers/clang.natvis
</tt></a> provide debugger visualizers
105 that make debugging of more complex data types much easier.
</p>
106 <p>For Visual Studio
2013 only, put the files into
107 <tt>%USERPROFILE%\Documents\Visual Studio
2013\Visualizers
</tt> or
108 create a symbolic link so they update automatically.
</p>
109 <p>For later versions of Visual Studio, no installation is required.
110 Note also that later versions of Visual Studio also display better visualizations.
</p>
112 <!--=====================================================================-->
113 <h2 id=
"testing">Testing
</h2>
114 <!--=====================================================================-->
116 <!--=====================================================================-->
117 <h3 id=
"testingNonWindows">Testing on Unix-like Systems
</h3>
118 <!--=====================================================================-->
120 <p>Clang includes a basic regression suite in the tree which can be
121 run with
<tt>make test
</tt> from the top-level clang directory, or
122 just
<tt>make
</tt> in the
<em>test
</em> sub-directory.
123 <tt>make VERBOSE=
1</tt> can be used to show more detail
124 about what is being run.
</p>
126 <p>If you built LLVM and Clang using CMake, the test suite can be run
127 with
<tt>make clang-test
</tt> from the top-level LLVM directory.
</p>
129 <p>The tests primarily consist of a test runner script running the compiler
130 under test on individual test files grouped in the directories under the
131 test directory. The individual test files include comments at the
132 beginning indicating the Clang compile options to use, to be read
133 by the test runner. Embedded comments also can do things like telling
134 the test runner that an error is expected at the current line.
135 Any output files produced by the test will be placed under
136 a created Output directory.
</p>
138 <p>During the run of
<tt>make test
</tt>, the terminal output will
139 display a line similar to the following:
</p>
141 <pre>--- Running clang tests for i686-pc-linux-gnu ---
</pre>
143 <p>followed by a line continually overwritten with the current test
144 file being compiled, and an overall completion percentage.
</p>
146 <p>After the
<tt>make test
</tt> run completes, the absence of any
147 <tt>Failing Tests (count):
</tt> message indicates that no tests
148 failed unexpectedly. If any tests did fail, the
149 <tt>Failing Tests (count):
</tt> message will be followed by a list
150 of the test source file paths that failed. For example:
</p>
154 /home/john/llvm/tools/clang/test/SemaCXX/member-name-lookup.cpp
155 /home/john/llvm/tools/clang/test/SemaCXX/namespace-alias.cpp
156 /home/john/llvm/tools/clang/test/SemaCXX/using-directive.cpp
159 <p>If you used the
<tt>make VERBOSE=
1</tt> option, the terminal
160 output will reflect the error messages from the compiler and
163 <p>The regression suite can also be run with Valgrind by running
164 <tt>make test VG=
1</tt> in the top-level clang directory.
</p>
166 <p>For more intensive changes, running
167 the
<a href=
"https://llvm.org/docs/TestingGuide.html#quick-start">LLVM
168 Test Suite
</a> with clang is recommended. Currently the best way to
169 override LLVMGCC, as in:
<tt>make
LLVMGCC=
"clang -std=gnu89"
170 TEST=nightly report
</tt> (make sure
<tt>clang
</tt> is in your PATH or use the
173 <!--=====================================================================-->
174 <h3 id=
"testingWindows">Testing using Visual Studio on Windows
</h3>
175 <!--=====================================================================-->
177 <p>The Clang test suite can be run from either Visual Studio or
178 the command line.
</p>
180 <p>Note that the test runner is based on
181 Python, which must be installed. Find Python at:
182 <a href=
"https://www.python.org/downloads/">https://www.python.org/downloads/
</a>.
183 Download the latest stable version.
</p>
185 <p>The GnuWin32 tools are also necessary for running the tests.
186 Get them from
<a href=
"http://getgnuwin32.sourceforge.net/">
187 http://getgnuwin32.sourceforge.net/
</a>.
188 If the environment variable
<tt>%PATH%
</tt> does not have GnuWin32,
189 or if other grep(s) supercedes GnuWin32 on
<tt>%PATH%,
</tt>
190 you should specify
<tt>LLVM_LIT_TOOLS_DIR
</tt>
191 to CMake explicitly.
</p>
193 <p>The cmake build tool is set up to create Visual Studio project files
194 for running the tests,
"clang-test" being the root. Therefore, to
195 run the test from Visual Studio, right-click the clang-test project
196 and select
"Build".
</p>
200 <a href=
"https://llvm.org/docs/GettingStartedVS.html">Getting Started
201 with the LLVM System using Microsoft Visual Studio
</a> and
202 <a href=
"https://llvm.org/docs/CMake.html">Building LLVM with CMake
</a>.
205 <!--=====================================================================-->
206 <h3 id=
"testingCommands">Testing on the Command Line
</h3>
207 <!--=====================================================================-->
209 <p>If you want more control over how the tests are run, it may
210 be convenient to run the test harness on the command-line directly. Before
211 running tests from the command line, you will need to ensure that
212 <tt>lit.site.cfg
</tt> files have been created for your build. You can do
213 this by running the tests as described in the previous sections. Once the
214 tests have started running, you can stop them with control+C, as the
215 files are generated before running any tests.
</p>
217 <p>Once that is done, to run all the tests from the command line,
218 execute a command like the following:
</p>
221 python (path to llvm)\llvm\utils\lit\lit.py -sv
222 --param=build_mode=Win32 --param=build_config=Debug
223 --param=clang_site_config=(build dir)\tools\clang\test\lit.site.cfg
224 (path to llvm)\llvm\tools\clang\test
227 <p>For CMake builds e.g. on Windows with Visual Studio, you will need
228 to specify your build configuration (Debug, Release, etc.) via
229 <tt>--param=build_config=(build config)
</tt>. You may also need to specify
230 the build mode (Win32, etc) via
<tt>--param=build_mode=(build mode)
</tt>.
</p>
232 <p>Additionally, you will need to specify the lit site configuration which
233 lives in (build dir)\tools\clang\test, via
234 <tt>--param=clang_site_config=(build dir)\tools\clang\test\lit.site.cfg
</tt>.
237 <p>To run a single test:
</p>
240 python (path to llvm)\llvm\utils\lit\lit.py -sv
241 --param=build_mode=Win32 --param=build_config=Debug
242 --param=clang_site_config=(build dir)\tools\clang\test\lit.site.cfg
243 (path to llvm)\llvm\tools\clang\test\(dir)\(test)
249 python C:\Tools\llvm\utils\lit\lit.py -sv
250 --param=build_mode=Win32 --param=build_config=Debug
251 --param=clang_site_config=C:\Tools\build\tools\clang\test\lit.site.cfg
252 C:\Tools\llvm\tools\clang\test\Sema\wchar.c
255 <p>The -sv option above tells the runner to show the test output if
256 any tests failed, to help you determine the cause of failure.
</p>
258 <p>You can also pass in the --no-progress-bar option if you wish to disable
259 progress indications while the tests are running.
</p>
261 <p>Your output might look something like this:
</p>
263 <pre>lit.py: lit.cfg:
152: note: using clang: 'C:\Tools\llvm\bin\Release\clang.EXE'
264 -- Testing: Testing:
2534 tests,
4 threads --
265 Testing:
0 ..
10..
20..
30..
40..
50..
60..
70..
80..
90..
268 Expectedly Failed:
28
272 <p>The statistic,
"Failed" (not shown if all tests pass), is the important one.
</p>
274 <!--=====================================================================-->
275 <h2 id=
"patches">Creating Patch Files
</h2>
276 <!--=====================================================================-->
278 <p>To contribute changes to Clang see
279 <a href=
"https://llvm.org/docs/GettingStarted.html#sending-patches">LLVM's Getting Started page
</a></p>
281 <!--=====================================================================-->
282 <h2 id=
"irgen">LLVM IR Generation
</h2>
283 <!--=====================================================================-->
285 <p>The LLVM IR generation part of clang handles conversion of the
286 AST nodes output by the Sema module to the LLVM Intermediate
287 Representation (IR). Historically, this was referred to as
288 "codegen", and the Clang code for this lives
289 in
<tt>lib/CodeGen
</tt>.
</p>
291 <p>The output is most easily inspected using the
<tt>-emit-llvm
</tt>
292 option to clang (possibly in conjunction with
<tt>-o -
</tt>). You
293 can also use
<tt>-emit-llvm-bc
</tt> to write an LLVM bitcode file
294 which can be processed by the suite of LLVM tools
295 like
<tt>llvm-dis
</tt>,
<tt>llvm-nm
</tt>, etc. See the LLVM
296 <a href=
"https://llvm.org/docs/CommandGuide/">Command Guide
</a>
297 for more information.
</p>