Cedric Venet | 3d65864 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 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 | </head> |
| 11 | <body> |
| 12 | <!--#include virtual="menu.html.incl"--> |
| 13 | <div id="content"> |
| 14 | <!--*********************************************************************--> |
| 15 | <h1>Hacking on Clang</h1> |
| 16 | <!--*********************************************************************--> |
| 17 | |
| 18 | <p>This document provides some hints for how to get started hacking |
| 19 | on Clang for developers who are new to the Clang and/or LLVM |
| 20 | codebases.</p> |
| 21 | <ul> |
| 22 | <li><a href="#docs">Developer Documentation</a></li> |
| 23 | <li><a href="#debugging">Debugging</a></li> |
| 24 | <li><a href="#testing">Testing</a></li> |
Eli Friedman | d1e1ef3 | 2009-08-03 19:42:28 +0000 | [diff] [blame] | 25 | <ul> |
| 26 | <li><a href="#testingNonWindows">Testing on Unix-like Systems</a></li> |
| 27 | <li><a href="#testingWindows">Testing using Visual Studio on Windows</a></li> |
| 28 | </ul> |
| 29 | <li><a href="#patches">Creating Patch Files</a></li> |
Cedric Venet | 3d65864 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 30 | <li><a href="#irgen">LLVM IR Generation</a></li> |
| 31 | </ul> |
| 32 | |
| 33 | <!--=====================================================================--> |
| 34 | <h2 id="docs">Developer Documentation</h2> |
| 35 | <!--=====================================================================--> |
| 36 | |
| 37 | <p>Both Clang and LLVM use doxygen to provide API documentation. Their |
| 38 | respective web pages (generated nightly) are here:</p> |
| 39 | <ul> |
| 40 | <li><a href="http://clang.llvm.org/doxygen">Clang</a></li> |
| 41 | <li><a href="http://llvm.org/doxygen">LLVM</a></li> |
| 42 | </ul> |
| 43 | |
| 44 | <p>For work on the LLVM IR generation, the LLVM assembly language |
| 45 | <a href="http://llvm.org/docs/LangRef.html">reference manual</a> is |
| 46 | also useful.</p> |
| 47 | |
| 48 | <!--=====================================================================--> |
| 49 | <h2 id="debugging">Debugging</h2> |
| 50 | <!--=====================================================================--> |
| 51 | |
| 52 | <p>Inspecting data structures in a debugger:</p> |
| 53 | <ul> |
| 54 | <li>Many LLVM and Clang data structures provide |
| 55 | a <tt>dump()</tt> method which will print a description of the |
| 56 | data structure to <tt>stderr</tt>.</li> |
| 57 | <li>The <a href="docs/InternalsManual.html#QualType"><tt>QualType</tt></a> |
| 58 | structure is used pervasively. This is a simple value class for |
| 59 | wrapping types with qualifiers; you can use |
| 60 | the <tt>isConstQualified()</tt>, for example, to get one of the |
| 61 | qualifiers, and the <tt>getTypePtr()</tt> method to get the |
| 62 | wrapped <tt>Type*</tt> which you can then dump.</li> |
| 63 | </ul> |
| 64 | |
| 65 | <!--=====================================================================--> |
| 66 | <h2 id="testing">Testing</h2> |
| 67 | <!--=====================================================================--> |
| 68 | |
Eli Friedman | d1e1ef3 | 2009-08-03 19:42:28 +0000 | [diff] [blame] | 69 | <p><i>[Note: The test running mechanism is currently under revision, so the |
| 70 | following might change shortly.]</i></p> |
| 71 | |
| 72 | <!--=====================================================================--> |
| 73 | <h3 id="testingNonWindows">Testing on Unix-like Systems</h3> |
| 74 | <!--=====================================================================--> |
| 75 | |
Cedric Venet | 3d65864 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 76 | <p>Clang includes a basic regression suite in the tree which can be |
| 77 | run with <tt>make test</tt> from the top-level clang directory, or |
Eli Friedman | d1e1ef3 | 2009-08-03 19:42:28 +0000 | [diff] [blame] | 78 | just <tt>make</tt> in the <em>test</em> sub-directory. |
| 79 | <tt>make VERBOSE=1</tt> can be used to show more detail |
Cedric Venet | 3d65864 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 80 | about what is being run.</p> |
| 81 | |
Eli Friedman | d1e1ef3 | 2009-08-03 19:42:28 +0000 | [diff] [blame] | 82 | <p>The tests primarily consist of a test runner script running the compiler |
| 83 | under test on individual test files grouped in the directories under the |
| 84 | test directory. The individual test files include comments at the |
| 85 | beginning indicating the Clang compile options to use, to be read |
| 86 | by the test runner. Embedded comments also can do things like telling |
| 87 | the test runner that an error is expected at the current line. |
| 88 | Any output files produced by the test will be placed under |
| 89 | a created Output directory.</p> |
| 90 | |
| 91 | <p>During the run of <tt>make test</tt>, the terminal output will |
| 92 | display a line similar to the following:</p> |
| 93 | |
| 94 | <ul><tt>--- Running clang tests for i686-pc-linux-gnu ---</tt></ul> |
| 95 | |
| 96 | <p>followed by a line continually overwritten with the current test |
| 97 | file being compiled, and an overall completion percentage.</p> |
| 98 | |
| 99 | <p>After the <tt>make test</tt> run completes, the absence of any |
| 100 | <tt>Failing Tests (count):</tt> message indicates that no tests |
| 101 | failed unexpectedly. If any tests did fail, the |
| 102 | <tt>Failing Tests (count):</tt> message will be followed by a list |
| 103 | of the test source file paths that failed. For example:</p> |
| 104 | |
| 105 | <tt><pre> |
| 106 | Failing Tests (3): |
| 107 | /home/john/llvm/tools/clang/test/SemaCXX/member-name-lookup.cpp |
| 108 | /home/john/llvm/tools/clang/test/SemaCXX/namespace-alias.cpp |
| 109 | /home/john/llvm/tools/clang/test/SemaCXX/using-directive.cpp |
| 110 | </pre></tt> |
| 111 | |
| 112 | <p>If you used the <tt>make VERBOSE=1</tt> option, the terminal |
| 113 | output will reflect the error messages from the compiler and |
| 114 | test runner.</p> |
| 115 | |
Cedric Venet | 3d65864 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 116 | <p>The regression suite can also be run with Valgrind by running |
| 117 | <tt>make test VG=1</tt> in the top-level clang directory.</p> |
| 118 | |
| 119 | <p>For more intensive changes, running |
| 120 | the <a href="http://llvm.org/docs/TestingGuide.html#testsuiterun">LLVM |
| 121 | Test Suite</a> with clang is recommended. Currently the best way to |
Daniel Dunbar | 12e57bc | 2009-08-06 16:47:53 +0000 | [diff] [blame] | 122 | override LLVMGCC, as in: <tt>make LLVMGCC="clang -std=gnu89" |
| 123 | TEST=nightly report</tt> (make sure <tt>clang</tt> is in your PATH or use the |
Cedric Venet | 3d65864 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 124 | full path).</p> |
Eli Friedman | d1e1ef3 | 2009-08-03 19:42:28 +0000 | [diff] [blame] | 125 | |
| 126 | <!--=====================================================================--> |
| 127 | <h3 id="testingWindows">Testing using Visual Studio on Windows</h3> |
| 128 | <!--=====================================================================--> |
| 129 | |
John Thompson | 99ff8da | 2009-11-06 00:06:29 +0000 | [diff] [blame] | 130 | <p>The Clang test suite can be run from either Visual Studio or |
| 131 | the command line.</p> |
Eli Friedman | d1e1ef3 | 2009-08-03 19:42:28 +0000 | [diff] [blame] | 132 | |
John Thompson | 99ff8da | 2009-11-06 00:06:29 +0000 | [diff] [blame] | 133 | <p>Note that the test runner is based on |
Eli Friedman | d1e1ef3 | 2009-08-03 19:42:28 +0000 | [diff] [blame] | 134 | Python, which must be installed. Find Python at: |
| 135 | <a href="http://www.python.org/download">http://www.python.org/download</a>. |
| 136 | Download the latest stable version (2.6.2 at the time of this writing).</p> |
Chris Lattner | 357f7ce | 2009-08-20 06:17:11 +0000 | [diff] [blame] | 137 | |
| 138 | <p>The GnuWin32 tools are also necessary for running the tests. |
| 139 | (Note that the grep from MSYS or Cygwin doesn't work with the tests |
| 140 | because of embedded double-quotes in the search strings. The GNU |
| 141 | grep does work in this case.) |
| 142 | Get them from <a href="http://getgnuwin32.sourceforge.net"> |
| 143 | http://getgnuwin32.sourceforge.net</a>.</p> |
John Thompson | 99ff8da | 2009-11-06 00:06:29 +0000 | [diff] [blame] | 144 | |
| 145 | <p>The cmake build tool is set up to create Visual Studio project files |
| 146 | for running the tests, "clang-test" being the root. Therefore, to |
| 147 | run the test from Visual Studio, right-click the clang-test project |
| 148 | and select "Build".</p> |
| 149 | |
| 150 | <p>To run all the tests from the command line, execute a command like |
| 151 | the following:</p> |
| 152 | |
| 153 | <tt> |
| 154 | python (path to llvm)/llvm/utils/lit/lit.py -sv --no-progress-bar |
| 155 | (path to llvm)/llvm/tools/clang/test |
| 156 | </tt> |
| 157 | |
| 158 | <p>To run a single test:</p> |
| 159 | |
| 160 | <tt> |
| 161 | python (path to llvm)/llvm/utils/lit/lit.py -sv --no-progress-bar |
| 162 | (path to llvm)/llvm/tools/clang/test/(dir)/(test) |
| 163 | </tt> |
| 164 | |
| 165 | <p>For example:</p> |
| 166 | |
| 167 | <tt> |
| 168 | python C:/Tools/llvm/utils/lit/lit.py -sv --no-progress-bar |
| 169 | C:/Tools/llvm/tools/clang/test/Sema/wchar.c |
| 170 | </tt> |
| 171 | |
| 172 | <p>The -sv option above tells the runner to show the test output if |
| 173 | any tests failed, to help you determine the cause of failure.</p> |
| 174 | |
| 175 | <p>Note that a few tests currently fail on Windows. We are working to |
| 176 | correct this. Therefore your output might look something like this:</p> |
| 177 | |
| 178 | <tt><pre>lit.py: lit.cfg:152: note: using clang: 'C:/Tools/llvm/bin/Debug\\clang.EXE' |
| 179 | lit.py: lit.cfg:157: note: using clang-cc: 'C:/Tools/llvm/bin/Debug/clang-cc.exe' |
| 180 | -- Testing: 1723 tests, 2 threads -- |
| 181 | FAIL: Clang::(test path) (659 of 1723) |
| 182 | ******************** TEST 'Clang::(test path)' FAILED ******************** |
| 183 | Script: |
| 184 | (commands run) |
| 185 | Command Output (stdout): |
| 186 | (output here) |
| 187 | Command Output (stderr): |
| 188 | (output here) |
| 189 | ******************** |
| 190 | Testing Time: 83.66s |
| 191 | ******************** |
| 192 | Failing Tests (1): |
| 193 | Clang::(test path) |
| 194 | Expected Passes : 1704 |
| 195 | Expected Failures : 18 |
| 196 | Unexpected Failures: 1 |
| 197 | </pre></tt> |
| 198 | |
| 199 | <p>The last statistic, "Unexpected Failures", is the important one.</p> |
Eli Friedman | d1e1ef3 | 2009-08-03 19:42:28 +0000 | [diff] [blame] | 200 | |
| 201 | <!--=====================================================================--> |
| 202 | <h2 id="patches">Creating Patch Files</h2> |
| 203 | <!--=====================================================================--> |
| 204 | |
| 205 | <p>To return changes to the Clang team, unless you have checkin |
| 206 | privileges, the prefered way is to send patch files to the |
| 207 | cfe-commits mailing list, with an explanation of what the patch is for. |
| 208 | Or, if you have questions, or want to have a wider discussion of what |
| 209 | you are doing, such as if you are new to Clang development, you can use |
| 210 | the cfe-dev mailing list also. |
| 211 | </p> |
| 212 | |
| 213 | <p>To create these patch files, change directory |
| 214 | to the llvm/tools/clang root and run:</p> |
| 215 | |
| 216 | <ul><tt>svn diff (relative path) >(patch file name)</tt></ul> |
| 217 | |
| 218 | <p>For example, for getting the diffs of all of clang:</p> |
| 219 | |
| 220 | <ul><tt>svn diff . >~/mypatchfile.patch</tt></ul> |
| 221 | |
| 222 | <p>For example, for getting the diffs of a single file:</p> |
| 223 | |
| 224 | <ul><tt>svn diff lib/Parse/ParseDeclCXX.cpp >~/ParseDeclCXX.patch</tt></ul> |
| 225 | |
| 226 | <p>Note that the paths embedded in the patch depend on where you run it, |
| 227 | so changing directory to the llvm/tools/clang directory is recommended.</p> |
Cedric Venet | 3d65864 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 228 | |
| 229 | <!--=====================================================================--> |
| 230 | <h2 id="irgen">LLVM IR Generation</h2> |
| 231 | <!--=====================================================================--> |
| 232 | |
| 233 | <p>The LLVM IR generation part of clang handles conversion of the |
| 234 | AST nodes output by the Sema module to the LLVM Intermediate |
| 235 | Representation (IR). Historically, this was referred to as |
| 236 | "codegen", and the Clang code for this lives |
| 237 | in <tt>lib/CodeGen</tt>.</p> |
| 238 | |
| 239 | <p>The output is most easily inspected using the <tt>-emit-llvm</tt> |
| 240 | option to clang (possibly in conjunction with <tt>-o -</tt>). You |
| 241 | can also use <tt>-emit-llvm-bc</tt> to write an LLVM bitcode file |
| 242 | which can be processed by the suite of LLVM tools |
| 243 | like <tt>llvm-dis</tt>, <tt>llvm-nm</tt>, etc. See the LLVM |
| 244 | <a href="http://llvm.org/docs/CommandGuide/">Command Guide</a> |
| 245 | for more information.</p> |
| 246 | |
| 247 | </div> |
| 248 | </body> |
| 249 | </html> |