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/ --> 4<html> 5<head> 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; } 12 </style> 13</head> 14<body> 15<!--#include virtual="menu.html.incl"--> 16<div id="content"> 17 <!--*********************************************************************--> 18 <h1>Hacking on Clang</h1> 19 <!--*********************************************************************--> 20 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 23 codebases.</p> 24 <ul> 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> 29 <ul> 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> 33 </ul> 34 </li> 35 <li><a href="#patches">Creating Patch Files</a></li> 36 <li><a href="#irgen">LLVM IR Generation</a></li> 37 </ul> 38 39 <!--=====================================================================--> 40 <h2 id="style">Coding Standards</h2> 41 <!--=====================================================================--> 42 43 <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 <!--=====================================================================--> 61 <h2 id="docs">Developer Documentation</h2> 62 <!--=====================================================================--> 63 64 <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> 70 71 <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 <!--=====================================================================--> 78 79 <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> 90 </ul> 91 92 <!--=====================================================================--> 93 <h3 id="debuggingVisualStudio">Debugging using Visual Studio</h3> 94 <!--=====================================================================--> 95 96 <p>The file <tt>utils/clangVisualizers.txt</tt> provides debugger visualizers that make debugging 97 of more complex data types much easier.</p> 98 <p>There are two ways to install them:</p> 99 100 <ul> 101 <li>Put the path to <tt>clangVisualizers.txt</tt> in the environment variable called 102 <tt>_vcee_autoexp</tt>. This method should work for Visual Studio 2008 and above. 103 </li> 104 <li>Edit your local <tt>autoexp.dat</tt> (make sure you make a backup first!), 105 located in <tt>Visual Studio Directory\Common7\Packages\Debugger</tt> and append 106 the contents of <tt>clangVisuailzers.txt</tt> to it. This method should work for 107 Visual Studio 2008 and above. 108 </li> 109 </ul> 110 111 <p><i>[Note: To disable the visualizer for any specific variable, type 112 <tt>variable_name,!</tt> inside the watch window.]</i></p> 113 114 <!--=====================================================================--> 115 <h2 id="testing">Testing</h2> 116 <!--=====================================================================--> 117 118 <p><i>[Note: The test running mechanism is currently under revision, so the 119 following might change shortly.]</i></p> 120 121 <!--=====================================================================--> 122 <h3 id="testingNonWindows">Testing on Unix-like Systems</h3> 123 <!--=====================================================================--> 124 125 <p>Clang includes a basic regression suite in the tree which can be 126 run with <tt>make test</tt> from the top-level clang directory, or 127 just <tt>make</tt> in the <em>test</em> sub-directory. 128 <tt>make VERBOSE=1</tt> can be used to show more detail 129 about what is being run.</p> 130 131 <p>If you built LLVM and Clang using CMake, the test suite can be run 132 with <tt>make clang-test</tt> from the top-level LLVM directory.</p> 133 134 <p>The tests primarily consist of a test runner script running the compiler 135 under test on individual test files grouped in the directories under the 136 test directory. The individual test files include comments at the 137 beginning indicating the Clang compile options to use, to be read 138 by the test runner. Embedded comments also can do things like telling 139 the test runner that an error is expected at the current line. 140 Any output files produced by the test will be placed under 141 a created Output directory.</p> 142 143 <p>During the run of <tt>make test</tt>, the terminal output will 144 display a line similar to the following:</p> 145 146 <pre>--- Running clang tests for i686-pc-linux-gnu ---</pre> 147 148 <p>followed by a line continually overwritten with the current test 149 file being compiled, and an overall completion percentage.</p> 150 151 <p>After the <tt>make test</tt> run completes, the absence of any 152 <tt>Failing Tests (count):</tt> message indicates that no tests 153 failed unexpectedly. If any tests did fail, the 154 <tt>Failing Tests (count):</tt> message will be followed by a list 155 of the test source file paths that failed. For example:</p> 156 157 <pre> 158 Failing Tests (3): 159 /home/john/llvm/tools/clang/test/SemaCXX/member-name-lookup.cpp 160 /home/john/llvm/tools/clang/test/SemaCXX/namespace-alias.cpp 161 /home/john/llvm/tools/clang/test/SemaCXX/using-directive.cpp 162</pre> 163 164 <p>If you used the <tt>make VERBOSE=1</tt> option, the terminal 165 output will reflect the error messages from the compiler and 166 test runner.</p> 167 168 <p>The regression suite can also be run with Valgrind by running 169 <tt>make test VG=1</tt> in the top-level clang directory.</p> 170 171 <p>For more intensive changes, running 172 the <a href="http://llvm.org/docs/TestingGuide.html#testsuiterun">LLVM 173 Test Suite</a> with clang is recommended. Currently the best way to 174 override LLVMGCC, as in: <tt>make LLVMGCC="clang -std=gnu89" 175 TEST=nightly report</tt> (make sure <tt>clang</tt> is in your PATH or use the 176 full path).</p> 177 178 <!--=====================================================================--> 179 <h3 id="testingWindows">Testing using Visual Studio on Windows</h3> 180 <!--=====================================================================--> 181 182 <p>The Clang test suite can be run from either Visual Studio or 183 the command line.</p> 184 185 <p>Note that the test runner is based on 186 Python, which must be installed. Find Python at: 187 <a href="http://www.python.org/download/">http://www.python.org/download/</a>. 188 Download the latest stable version (2.6.2 at the time of this writing).</p> 189 190 <p>The GnuWin32 tools are also necessary for running the tests. 191 Get them from <a href="http://getgnuwin32.sourceforge.net/"> 192 http://getgnuwin32.sourceforge.net/</a>. 193 If the environment variable <tt>%PATH%</tt> does not have GnuWin32, 194 or if other grep(s) supercedes GnuWin32 on <tt>%PATH%,</tt> 195 you should specify <tt>LLVM_LIT_TOOLS_DIR</tt> 196 to CMake explicitly.</p> 197 198 <p>The cmake build tool is set up to create Visual Studio project files 199 for running the tests, "clang-test" being the root. Therefore, to 200 run the test from Visual Studio, right-click the clang-test project 201 and select "Build".</p> 202 203 <p> 204 Please see also 205 <a href="http://llvm.org/docs/GettingStartedVS.html">Getting Started 206 with the LLVM System using Microsoft Visual Studio</a> and 207 <a href="http://llvm.org/docs/CMake.html">Building LLVM with CMake</a>. 208 </p> 209 210 <!--=====================================================================--> 211 <h3 id="testingCommands">Testing on the Command Line</h3> 212 <!--=====================================================================--> 213 214 <p>If you want more control over how the tests are run, it may 215 be convenient to run the test harness on the command-line directly. Before 216 running tests from the command line, you will need to ensure that 217 <tt>lit.site.cfg</tt> files have been created for your build. You can do 218 this by running the tests as described in the previous sections. Once the 219 tests have started running, you can stop them with control+C, as the 220 files are generated before running any tests.</p> 221 222 <p>Once that is done, to run all the tests from the command line, 223 execute a command like the following:</p> 224 225 <pre> 226 python (path to llvm)\llvm\utils\lit\lit.py -sv 227 --param=build_mode=Win32 --param=build_config=Debug 228 --param=clang_site_config=(build dir)\tools\clang\test\lit.site.cfg 229 (path to llvm)\llvm\tools\clang\test 230</pre> 231 232 <p>For CMake builds e.g. on Windows with Visual Studio, you will need 233 to specify your build configuration (Debug, Release, etc.) via 234 <tt>--param=build_config=(build config)</tt>. You may also need to specify 235 the build mode (Win32, etc) via <tt>--param=build_mode=(build mode)</tt>.</p> 236 237 <p>Additionally, you will need to specify the lit site configuration which 238 lives in (build dir)\tools\clang\test, via 239 <tt>--param=clang_site_config=(build dir)\tools\clang\test\lit.site.cfg</tt>. 240 </p> 241 242 <p>To run a single test:</p> 243 244 <pre> 245 python (path to llvm)\llvm\utils\lit\lit.py -sv 246 --param=build_mode=Win32 --param=build_config=Debug 247 --param=clang_site_config=(build dir)\tools\clang\test\lit.site.cfg 248 (path to llvm)\llvm\tools\clang\test\(dir)\(test) 249</pre> 250 251 <p>For example:</p> 252 253 <pre> 254 python C:\Tool\llvm\utils\lit\lit.py -sv 255 --param=build_mode=Win32 --param=build_config=Debug 256 --param=clang_site_config=c:\Tools\build\tools\clang\test\lit.site.cfg 257 C:\Tools\llvm\tools\clang\test\Sema\wchar.c 258</pre> 259 260 <p>The -sv option above tells the runner to show the test output if 261 any tests failed, to help you determine the cause of failure.</p> 262 263 <p>You can also pass in the --no-progress-bar option if you wish to disable 264 progress indications while the tests are running.</p> 265 266 <p>Your output might look something like this:</p> 267 268 <pre>lit.py: lit.cfg:152: note: using clang: 'C:\Tools\llvm\bin\Release\clang.EXE' 269-- Testing: Testing: 2534 tests, 4 threads -- 270Testing: 0 .. 10.. 20.. 30.. 40.. 50.. 60.. 70.. 80.. 90.. 271Testing Time: 81.52s 272 Expected Passes : 2503 273 Expected Failures : 28 274 Unsupported Tests : 3 275</pre> 276 277 <p>The statistic, "Unexpected Failures" (not shown if all tests pass), is the important one.</p> 278 279 <!--=====================================================================--> 280 <h2 id="patches">Creating Patch Files</h2> 281 <!--=====================================================================--> 282 283 <p>To return changes to the Clang team, unless you have checkin 284 privileges, the preferred way is to send patch files to the 285 cfe-commits mailing list, with an explanation of what the patch is 286 for. If your patch requires a wider discussion (for example, 287 because it is an architectural change), you can use the cfe-dev 288 mailing list. </p> 289 290 <p>To create these patch files, change directory 291 to the llvm/tools/clang root and run:</p> 292 293 <pre>svn diff (relative path) >(patch file name)</pre> 294 295 <p>For example, for getting the diffs of all of clang:</p> 296 297 <pre>svn diff . >~/mypatchfile.patch</pre> 298 299 <p>For example, for getting the diffs of a single file:</p> 300 301 <pre>svn diff lib/Parse/ParseDeclCXX.cpp >~/ParseDeclCXX.patch</pre> 302 303 <p>Note that the paths embedded in the patch depend on where you run it, 304 so changing directory to the llvm/tools/clang directory is recommended.</p> 305 306 <!--=====================================================================--> 307 <h2 id="irgen">LLVM IR Generation</h2> 308 <!--=====================================================================--> 309 310 <p>The LLVM IR generation part of clang handles conversion of the 311 AST nodes output by the Sema module to the LLVM Intermediate 312 Representation (IR). Historically, this was referred to as 313 "codegen", and the Clang code for this lives 314 in <tt>lib/CodeGen</tt>.</p> 315 316 <p>The output is most easily inspected using the <tt>-emit-llvm</tt> 317 option to clang (possibly in conjunction with <tt>-o -</tt>). You 318 can also use <tt>-emit-llvm-bc</tt> to write an LLVM bitcode file 319 which can be processed by the suite of LLVM tools 320 like <tt>llvm-dis</tt>, <tt>llvm-nm</tt>, etc. See the LLVM 321 <a href="http://llvm.org/docs/CommandGuide/">Command Guide</a> 322 for more information.</p> 323 324</div> 325</body> 326</html> 327