blob: 7d7f2631ab5eee73558a6c469dc20cc504b1ccce [file] [log] [blame]
Chris Lattnercf17d9d2009-04-20 04:23:09 +00001<html>
2<head>
3<title>Clang Compiler User's Manual</title>
4<link type="text/css" rel="stylesheet" href="../menu.css" />
5<link type="text/css" rel="stylesheet" href="../content.css" />
6<style type="text/css">
7td {
8 vertical-align: top;
9}
10</style>
11</head>
12<body>
13
14<!--#include virtual="../menu.html.incl"-->
15
16<div id="content">
17
18<h1>Clang Compiler User's Manual</h1>
19
20<ul>
21<li><a href="#intro">Introduction</a>
22 <ul>
23 <li><a href="#terminology">Terminology</a></li>
24 <li><a href="#basicusage">Basic Usage</a></li>
25 </ul>
26</li>
Chris Lattner65a795b2009-04-20 06:00:23 +000027<li><a href="#commandline">Command Line Options</a>
28 <ul>
29 <li><a href="#cl_diagnostics">Options to Control Error and Warning
30 Messages</a></li>
31 </ul>
32</li>
Chris Lattnercf17d9d2009-04-20 04:23:09 +000033<li><a href="#general_features">Language and Target-Independent Features</a>
34 <ul>
35 <li><a href="#diagnostics">Controlling Errors and Warnings</a></li>
Chris Lattner3401cf82009-07-12 21:22:10 +000036 <ul>
37 <li><a href="#diagnostics_display">Controlling How Clang Displays Diagnostics</a></li>
38 <li><a href="#diagnostics_mappings">Diagnostic Mappings</a></li>
39 <li><a href="#diagnostics_commandline">Controlling Diagnostics via Command Line Flags</a></li>
40 <li><a href="#diagnostics_pragmas">Controlling Diagnostics via Pragmas</a></li>
41 </ul>
Chris Lattnercf17d9d2009-04-20 04:23:09 +000042 <li><a href="#precompiledheaders">Precompiled Headers</a></li>
Mike Stump53664ca2009-12-14 23:53:10 +000043 <li><a href="#codegen">Controlling Code Generation</a></li>
Chris Lattnercf17d9d2009-04-20 04:23:09 +000044 </ul>
45</li>
46<li><a href="#c">C Language Features</a>
47 <ul>
Eli Friedman660a5ac2009-04-28 18:48:34 +000048 <li><a href="#c_ext">Extensions supported by clang</a></li>
49 <li><a href="#c_modes">Differences between various standard modes</a></li>
50 <li><a href="#c_unimpl_gcc">GCC extensions not implemented yet</a></li>
51 <li><a href="#c_unsupp_gcc">Intentionally unsupported GCC extensions</a></li>
52 <li><a href="#c_ms">Microsoft extensions</a></li>
Chris Lattnercf17d9d2009-04-20 04:23:09 +000053 </ul>
54</li>
55<li><a href="#objc">Objective-C Language Features</a>
56 <ul>
57 <li><a href="#objc_incompatibilities">Intentional Incompatibilities with
58 GCC</a></li>
59 </ul>
60</li>
61<li><a href="#cxx">C++ Language Features</a>
Chris Lattnercf17d9d2009-04-20 04:23:09 +000062</li>
63<li><a href="#objcxx">Objective C++ Language Features</a>
Chris Lattnercf17d9d2009-04-20 04:23:09 +000064</li>
65<li><a href="#target_features">Target-Specific Features and Limitations</a>
66 <ul>
67 <li><a href="#target_arch">CPU Architectures Features and Limitations</a>
68 <ul>
69 <li><a href="#target_arch_x86">X86</a></li>
Eli Friedman3b658d32009-06-08 05:12:39 +000070 <li><a href="#target_arch_arm">ARM</a></li>
71 <li><a href="#target_arch_other">Other platforms</a></li>
Chris Lattnercf17d9d2009-04-20 04:23:09 +000072 </ul>
73 </li>
74 <li><a href="#target_os">Operating System Features and Limitations</a>
75 <ul>
76 <li><a href="#target_os_darwin">Darwin (Mac OS/X)</a></li>
77 <li>Linux, etc.</li>
78 </ul>
79
80 </li>
81 </ul>
82</li>
83</ul>
84
85
86<!-- ======================================================================= -->
87<h2 id="intro">Introduction</h2>
88<!-- ======================================================================= -->
89
90<p>The Clang Compiler is an open-source compiler for the C family of programming
91languages, aiming to be the best in class implementation of these languages.
92Clang builds on the LLVM optimizer and code generator, allowing it to provide
93high-quality optimization and code generation support for many targets. For
94more general information, please see the <a href="http://clang.llvm.org">Clang
95Web Site</a> or the <a href="http://llvm.org">LLVM Web Site</a>.</p>
96
97<p>This document describes important notes about using Clang as a compiler for
98an end-user, documenting the supported features, command line options, etc. If
99you are interested in using Clang to build a tool that processes code, please
100see <a href="InternalsManual.html">the Clang Internals Manual</a>. If you are
101interested in the <a href="http://clang.llvm.org/StaticAnalysis.html">Clang
102Static Analyzer</a>, please see its web page.</p>
103
104<p>Clang is designed to support the C family of programming languages, which
105includes <a href="#c">C</a>, <a href="#objc">Objective-C</a>, <a
106href="#cxx">C++</a>, and <a href="#objcxx">Objective-C++</a> as well as many
107dialects of those. For language-specific information, please see the
108corresponding language specific section:</p>
109
110<ul>
111<li><a href="#c">C Language</a>: K&amp;R C, ANSI C89, ISO C90, ISO C94
112 (C89+AMD1), ISO C99 (+TC1, TC2, TC3). </li>
113<li><a href="#objc">Objective-C Language</a>: ObjC 1, ObjC 2, ObjC 2.1, plus
114 variants depending on base language.</li>
115<li><a href="#cxx">C++ Language Features</a></li>
116<li><a href="#objcxx">Objective C++ Language</a></li>
117</ul>
118
119<p>In addition to these base languages and their dialects, Clang supports a
120broad variety of language extensions, which are documented in the corresponding
121language section. These extensions are provided to be compatible with the GCC,
122Microsoft, and other popular compilers as well as to improve functionality
123through Clang-specific features. The Clang driver and language features are
124intentionally designed to be as compatible with the GNU GCC compiler as
125reasonably possible, easing migration from GCC to Clang. In most cases, code
126"just works".</p>
127
128<p>In addition to language specific features, Clang has a variety of features
129that depend on what CPU architecture or operating system is being compiled for.
Douglas Gregorcd5a5052009-11-09 15:15:41 +0000130Please see the <a href="#target_features">Target-Specific Features and
Chris Lattnercf17d9d2009-04-20 04:23:09 +0000131Limitations</a> section for more details.</p>
132
133<p>The rest of the introduction introduces some basic <a
134href="#terminology">compiler terminology</a> that is used throughout this manual
135and contains a basic <a href="#basicusage">introduction to using Clang</a>
136as a command line compiler.</p>
137
138<!-- = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = -->
139<h3 id="terminology">Terminology</h3>
140<!-- = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = -->
141
142<p>Front end, parser, backend, preprocessor, undefined behavior, diagnostic,
143 optimizer</p>
144
145<!-- = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = -->
146<h3 id="basicusage">Basic Usage</h3>
147<!-- = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = -->
148
149<p>Intro to how to use a C compiler for newbies.</p>
150<p>
151compile + link
152
153compile then link
154
155debug info
156
157enabling optimizations
158
159picking a language to use, defaults to C99 by default. Autosenses based on
160extension.
161
162using a makefile
163</p>
164
165
166<!-- ======================================================================= -->
167<h2 id="commandline">Command Line Options</h2>
168<!-- ======================================================================= -->
169
170<p>
171This section is generally an index into other sections. It does not go into
Chris Lattner65a795b2009-04-20 06:00:23 +0000172depth on the ones that are covered by other sections. However, the first part
173introduces the language selection and other high level options like -c, -g, etc.
Chris Lattnercf17d9d2009-04-20 04:23:09 +0000174</p>
175
Chris Lattner65a795b2009-04-20 06:00:23 +0000176
177<!-- = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = -->
178<h3 id="cl_diagnostics">Options to Control Error and Warning Messages</h3>
179<!-- = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = -->
180
181<p><b>-Werror</b>: Turn warnings into errors.</p>
182<p><b>-Werror=foo</b>: Turn warning "foo" into an error.</p>
183<p><b>-Wno-error=foo</b>: Turn warning "foo" into an warning even if -Werror is
184 specified.</p>
185<p><b>-Wfoo</b>: Enable warning foo</p>
186<p><b>-Wno-foo</b>: Disable warning foo</p>
187<p><b>-w</b>: Disable all warnings.</p>
188<p><b>-pedantic</b>: Warn on language extensions.</p>
189<p><b>-pedantic-errors</b>: Error on language extensions.</p>
190<p><b>-Wsystem-headers</b>: Enable warnings from system headers.</p>
191
Chris Lattner0f0c9632010-04-07 20:49:23 +0000192<p><b>-ferror-limit=123</b>: Stop emitting diagnostics after 123 errors have
193 been produced. The default is 20, and the error limit can be disabled with
194 -ferror-limit=0.</p>
195
Douglas Gregor575cf372010-04-20 07:18:24 +0000196<p><b>-ftemplate-backtrace-limit=123</b>: Only emit up to 123 template instantiation notes within the template instantiation backtrace for a single warning or error. The default is 10, and the limit can be disabled with -ftemplate-backtrace-limit=0.</p>
197
Chris Lattner65a795b2009-04-20 06:00:23 +0000198<!-- ================================================= -->
199<h4 id="cl_diag_formatting">Formatting of Diagnostics</h4>
200<!-- ================================================= -->
201
202<p>Clang aims to produce beautiful diagnostics by default, particularly for new
203users that first come to Clang. However, different people have different
Chris Lattner8217f4e2009-04-20 06:26:18 +0000204preferences, and sometimes Clang is driven by another program that wants to
205parse simple and consistent output, not a person. For these cases, Clang
206provides a wide range of options to control the exact output format of the
207diagnostics that it generates.</p>
Chris Lattner65a795b2009-04-20 06:00:23 +0000208
209<dl>
210
211<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
212<dt id="opt_fshow-column"><b>-f[no-]show-column</b>: Print column number in
213diagnostic.</dt>
214<dd>This option, which defaults to on, controls whether or not Clang prints the
215column number of a diagnostic. For example, when this is enabled, Clang will
216print something like:</p>
217
218<pre>
219 test.c:28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens]
220 #endif bad
221 ^
222 //
223</pre>
224
225<p>When this is disabled, Clang will print "test.c:28: warning..." with no
226column number.</p>
227</dd>
228
229<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
230<dt id="opt_fshow-source-location"><b>-f[no-]show-source-location</b>: Print
231source file/line/column information in diagnostic.</dt>
232<dd>This option, which defaults to on, controls whether or not Clang prints the
233filename, line number and column number of a diagnostic. For example,
234when this is enabled, Clang will print something like:</p>
235
236<pre>
237 test.c:28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens]
238 #endif bad
239 ^
240 //
241</pre>
242
243<p>When this is disabled, Clang will not print the "test.c:28:8: " part.</p>
244</dd>
245
246<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
247<dt id="opt_fcaret-diagnostics"><b>-f[no-]caret-diagnostics</b>: Print source
248line and ranges from source code in diagnostic.</dt>
249<dd>This option, which defaults to on, controls whether or not Clang prints the
250source line, source ranges, and caret when emitting a diagnostic. For example,
251when this is enabled, Clang will print something like:</p>
252
253<pre>
254 test.c:28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens]
255 #endif bad
256 ^
257 //
258</pre>
259
260<p>When this is disabled, Clang will just print:</p>
261
262<pre>
263 test.c:28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens]
264</pre>
265
266</dd>
267
268<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
269<dt id="opt_fdiagnostics-show-option"><b>-f[no-]diagnostics-show-option</b>:
270Enable <tt>[-Woption]</tt> information in diagnostic line.</dt>
271<dd>This option, which defaults to on,
272controls whether or not Clang prints the associated <A
273href="#cl_diag_warning_groups">warning group</a> option name when outputting
274a warning diagnostic. For example, in this output:</p>
275
276<pre>
277 test.c:28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens]
278 #endif bad
279 ^
280 //
281</pre>
282
283<p>Passing <b>-fno-diagnostics-show-option</b> will prevent Clang from printing
284the [<a href="#opt_Wextra-tokens">-Wextra-tokens</a>] information in the
285diagnostic. This information tells you the flag needed to enable or disable the
286diagnostic, either from the command line or through <a
287href="#pragma_GCC_diagnostic">#pragma GCC diagnostic</a>.</dd>
288
289
290<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
291<dt id="opt_fdiagnostics-fixit-info"><b>-f[no-]diagnostics-fixit-info</b>:
292Enable "FixIt" information in the diagnostics output.</dt>
293<dd>This option, which defaults to on, controls whether or not Clang prints the
294information on how to fix a specific diagnostic underneath it when it knows.
295For example, in this output:</p>
296
297<pre>
298 test.c:28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens]
299 #endif bad
300 ^
301 //
302</pre>
303
304<p>Passing <b>-fno-diagnostics-fixit-info</b> will prevent Clang from printing
305the "//" line at the end of the message. This information is useful for users
306who may not understand what is wrong, but can be confusing for machine
307parsing.</p>
308</dd>
309
310<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
Chris Lattner2a9cc232009-04-21 05:35:32 +0000311<dt id="opt_fdiagnostics-print-source-range-info">
312<b>-f[no-]diagnostics-print-source-range-info</b>:
Chris Lattner65a795b2009-04-20 06:00:23 +0000313Print machine parsable information about source ranges.</dt>
314<dd>This option, which defaults to off, controls whether or not Clang prints
315information about source ranges in a machine parsable format after the
316file/line/column number information. The information is a simple sequence of
317brace enclosed ranges, where each range lists the start and end line/column
318locations. For example, in this output:</p>
319
320<pre>
321exprs.c:47:15:{47:8-47:14}{47:17-47:24}: error: invalid operands to binary expression ('int *' and '_Complex float')
322 P = (P-42) + Gamma*4;
323 ~~~~~~ ^ ~~~~~~~
324</pre>
325
Chris Lattner2a9cc232009-04-21 05:35:32 +0000326<p>The {}'s are generated by -fdiagnostics-print-source-range-info.</p>
Chris Lattner65a795b2009-04-20 06:00:23 +0000327</dd>
328
329
330</dl>
331
332
333
334
335<!-- ===================================================== -->
336<h4 id="cl_diag_warning_groups">Individual Warning Groups</h4>
337<!-- ===================================================== -->
338
339<p>TODO: Generate this from tblgen. Define one anchor per warning group.</p>
340
341
342<dl>
343
344
345<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
346<dt id="opt_Wextra-tokens"><b>-Wextra-tokens</b>: Warn about excess tokens at
347 the end of a preprocessor directive.</dt>
348<dd>This option, which defaults to on, enables warnings about extra tokens at
349the end of preprocessor directives. For example:</p>
350
351<pre>
352 test.c:28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens]
353 #endif bad
354 ^
355</pre>
356
357<p>These extra tokens are not strictly conforming, and are usually best handled
358by commenting them out.</p>
359
360<p>This option is also enabled by <a href="">-Wfoo</a>, <a href="">-Wbar</a>,
361 and <a href="">-Wbaz</a>.</p>
362</dd>
363
364</dl>
365
Chris Lattnercf17d9d2009-04-20 04:23:09 +0000366<!-- ======================================================================= -->
367<h2 id="general_features">Language and Target-Independent Features</h2>
368<!-- ======================================================================= -->
369
370
371<!-- = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = -->
372<h3 id="diagnostics">Controlling Errors and Warnings</h3>
373<!-- = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = -->
374
Chris Lattner65a795b2009-04-20 06:00:23 +0000375<p>Clang provides a number of ways to control which code constructs cause it to
Chris Lattner8217f4e2009-04-20 06:26:18 +0000376emit errors and warning messages, and how they are displayed to the console.</p>
Chris Lattner65a795b2009-04-20 06:00:23 +0000377
Chris Lattner3401cf82009-07-12 21:22:10 +0000378<h4 id="diagnostics_display">Controlling How Clang Displays Diagnostics</h4>
Chris Lattner65a795b2009-04-20 06:00:23 +0000379
Chris Lattner8217f4e2009-04-20 06:26:18 +0000380<p>When Clang emits a diagnostic, it includes rich information in the output,
381and gives you fine-grain control over which information is printed. Clang has
382the ability to print this information, and these are the options that control
383it:</p>
384
385<p>
386<ol>
387<li>A file/line/column indicator that shows exactly where the diagnostic occurs
388 in your code [<a href="#opt_fshow-column">-fshow-column</a>, <a
389 href="#opt_fshow-source-location">-fshow-source-location</a>].</li>
390<li>A categorization of the diagnostic as a note, warning, error, or fatal
391 error.</li>
392<li>A text string that describes what the problem is.</li>
393<li>An option that indicates how to control the diagnostic (for diagnostics that
394 support it) [<a
395 href="#opt_fdiagnostics-show-option">-fdiagnostics-show-option</a>].</li>
396<li>The line of source code that the issue occurs on, along with a caret and
397 ranges that indicate the important locations [<a
398 href="opt_fcaret-diagnostics">-fcaret-diagnostics</a>].</li>
399<li>"FixIt" information, which is a concise explanation of how to fix the
400 problem (when Clang is certain it knows) [<a
401 href="opt_fdiagnostics-fixit-info">-fdiagnostics-fixit-info</a>].</li>
402<li>A machine-parsable representation of the ranges involved (off by
403 default) [<a
Chris Lattner2a9cc232009-04-21 05:35:32 +0000404 href="opt_fdiagnostics-print-source-range-info">-fdiagnostics-print-source-range-info</a>].</li>
Chris Lattner8217f4e2009-04-20 06:26:18 +0000405</ol></p>
406
407<p>For more information please see <a href="#cl_diag_formatting">Formatting of
408Diagnostics</a>.</p>
409
Chris Lattner3401cf82009-07-12 21:22:10 +0000410<h4 id="diagnostics_mappings">Diagnostic Mappings</h4>
Chris Lattner8217f4e2009-04-20 06:26:18 +0000411
Chris Lattner3401cf82009-07-12 21:22:10 +0000412<p>All diagnostics are mapped into one of these 5 classes:</p>
Chris Lattner8217f4e2009-04-20 06:26:18 +0000413
414<p>
Chris Lattner3401cf82009-07-12 21:22:10 +0000415<ul>
416<li>Ignored</li>
417<li>Note</li>
418<li>Warning</li>
419<li>Error</li>
420<li>Fatal</li>
421</ul></p>
Chris Lattner8217f4e2009-04-20 06:26:18 +0000422
Chris Lattner3401cf82009-07-12 21:22:10 +0000423<h4 id="diagnostics_commandline">Controlling Diagnostics via Command Line Flags</h4>
Chris Lattner8217f4e2009-04-20 06:26:18 +0000424
425<p>-W flags, -pedantic, etc</p>
426
Chris Lattner3401cf82009-07-12 21:22:10 +0000427<h4 id="diagnostics_pragmas">Controlling Diagnostics via Pragmas</h4>
428
429<p>Clang can also control what diagnostics are enabled through the use of
430pragmas in the source code. This is useful for turning off specific warnings
431in a section of source code. Clang supports GCC's pragma for compatibility
432with existing source code, as well as several extensions. </p>
433
434<p>The pragma may control any warning that can be used from the command line.
435Warnings may be set to ignored, warning, error, or fatal. The following
436example code will tell Clang or GCC to ignore the -Wall warnings:</p>
437
438<pre>
439#pragma GCC diagnostic ignored "-Wall"
440</pre>
441
442<p>In addition to all of the functionality of provided by GCC's pragma, Clang
443also allows you to push and pop the current warning state. This is particularly
444useful when writing a header file that will be compiled by other people, because
445you don't know what warning flags they build with.</p>
446
447<p>In the below example
448-Wmultichar is ignored for only a single line of code, after which the
449diagnostics return to whatever state had previously existed.</p>
450
451<pre>
452#pragma clang diagnostic push
453#pragma clang diagnostic ignored "-Wmultichar"
454
455char b = 'df'; // no warning.
456
457#pragma clang diagnostic pop
458</pre>
459
460<p>The push and pop pragmas will save and restore the full diagnostic state of
461the compiler, regardless of how it was set. That means that it is possible to
462use push and pop around GCC compatible diagnostics and Clang will push and pop
463them appropriately, while GCC will ignore the pushes and pops as unknown
464pragmas. It should be noted that while Clang supports the GCC pragma, Clang and
465GCC do not support the exact same set of warnings, so even when using GCC
466compatible #pragmas there is no guarantee that they will have identical behaviour
467on both compilers. </p>
Chris Lattnercf17d9d2009-04-20 04:23:09 +0000468
469<!-- = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = -->
470<h3 id="precompiledheaders">Precompiled Headers</h3>
471<!-- = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = -->
472
Chris Lattner5c3074f2009-04-20 04:37:38 +0000473<p><a href="http://en.wikipedia.org/wiki/Precompiled_header">Precompiled
474headers</a> are a general approach employed by many compilers to reduce
475compilation time. The underlying motivation of the approach is that it is
476common for the same (and often large) header files to be included by
477multiple source files. Consequently, compile times can often be greatly improved
478by caching some of the (redundant) work done by a compiler to process headers.
479Precompiled header files, which represent one of many ways to implement
480this optimization, are literally files that represent an on-disk cache that
481contains the vital information necessary to reduce some of the work
482needed to process a corresponding header file. While details of precompiled
483headers vary between compilers, precompiled headers have been shown to be a
484highly effective at speeding up program compilation on systems with very large
485system headers (e.g., Mac OS/X).</p>
486
Douglas Gregorf4d59532009-06-03 22:37:00 +0000487<h4>Generating a PCH File</h4>
Chris Lattner5c3074f2009-04-20 04:37:38 +0000488
Douglas Gregorf4d59532009-06-03 22:37:00 +0000489<p>To generate a PCH file using Clang, one invokes Clang with
Chris Lattner5c3074f2009-04-20 04:37:38 +0000490the <b><tt>-x <i>&lt;language&gt;</i>-header</tt></b> option. This mirrors the
491interface in GCC for generating PCH files:</p>
492
493<pre>
494 $ gcc -x c-header test.h -o test.h.gch
Douglas Gregorf4d59532009-06-03 22:37:00 +0000495 $ clang -x c-header test.h -o test.h.pch
Chris Lattner5c3074f2009-04-20 04:37:38 +0000496</pre>
497
Douglas Gregorf4d59532009-06-03 22:37:00 +0000498<h4>Using a PCH File</h4>
Chris Lattner5c3074f2009-04-20 04:37:38 +0000499
Douglas Gregorf4d59532009-06-03 22:37:00 +0000500<p>A PCH file can then be used as a prefix header when a
Chris Lattner5c3074f2009-04-20 04:37:38 +0000501<b><tt>-include</tt></b> option is passed to <tt>clang</tt>:</p>
502
503<pre>
504 $ clang -include test.h test.c -o test
505</pre>
506
Douglas Gregorf4d59532009-06-03 22:37:00 +0000507<p>The <tt>clang</tt> driver will first check if a PCH file for <tt>test.h</tt>
Chris Lattner5c3074f2009-04-20 04:37:38 +0000508is available; if so, the contents of <tt>test.h</tt> (and the files it includes)
Douglas Gregorf4d59532009-06-03 22:37:00 +0000509will be processed from the PCH file. Otherwise, Clang falls back to
Chris Lattner5c3074f2009-04-20 04:37:38 +0000510directly processing the content of <tt>test.h</tt>. This mirrors the behavior of
511GCC.</p>
512
Douglas Gregorf4d59532009-06-03 22:37:00 +0000513<p><b>NOTE:</b> Clang does <em>not</em> automatically use PCH files
Chris Lattner5c3074f2009-04-20 04:37:38 +0000514for headers that are directly included within a source file. For example:</p>
515
516<pre>
Chris Lattnere42ec542009-06-13 20:35:58 +0000517 $ clang -x c-header test.h -o test.h.pch
Chris Lattner5c3074f2009-04-20 04:37:38 +0000518 $ cat test.c
519 #include "test.h"
520 $ clang test.c -o test
521</pre>
522
Douglas Gregorf4d59532009-06-03 22:37:00 +0000523<p>In this example, <tt>clang</tt> will not automatically use the PCH file for
Chris Lattner5c3074f2009-04-20 04:37:38 +0000524<tt>test.h</tt> since <tt>test.h</tt> was included directly in the source file
525and not specified on the command line using <tt>-include</tt>.</p>
Chris Lattnercf17d9d2009-04-20 04:23:09 +0000526
Douglas Gregore650c8c2009-07-07 00:12:59 +0000527<h4>Relocatable PCH Files</h4>
528<p>It is sometimes necessary to build a precompiled header from headers that
529are not yet in their final, installed locations. For example, one might build a
530precompiled header within the build tree that is then meant to be installed
531alongside the headers. Clang permits the creation of "relocatable" precompiled
532headers, which are built with a given path (into the build directory) and can
533later be used from an installed location.</p>
534
535<p>To build a relocatable precompiled header, place your headers into a
536subdirectory whose structure mimics the installed location. For example, if you
537want to build a precompiled header for the header <code>mylib.h</code> that
538will be installed into <code>/usr/include</code>, create a subdirectory
539<code>build/usr/include</code> and place the header <code>mylib.h</code> into
540that subdirectory. If <code>mylib.h</code> depends on other headers, then
541they can be stored within <code>build/usr/include</code> in a way that mimics
542the installed location.</p>
543
544<p>Building a relocatable precompiled header requires two additional arguments.
545First, pass the <code>--relocatable-pch</code> flag to indicate that the
546resulting PCH file should be relocatable. Second, pass
547<code>-isysroot /path/to/build</code>, which makes all includes for your
548library relative to the build directory. For example:</p>
549
550<pre>
551 # clang -x c-header --relocatable-pch -isysroot /path/to/build /path/to/build/mylib.h mylib.h.pch
552</pre>
553
554<p>When loading the relocatable PCH file, the various headers used in the PCH
555file are found from the system header root. For example, <code>mylib.h</code>
556can be found in <code>/usr/include/mylib.h</code>. If the headers are installed
557in some other system root, the <code>-isysroot</code> option can be used provide
558a different system root from which the headers will be based. For example,
559<code>-isysroot /Developer/SDKs/MacOSX10.4u.sdk</code> will look for
560<code>mylib.h</code> in
561<code>/Developer/SDKs/MacOSX10.4u.sdk/usr/include/mylib.h</code>.</p>
562
563<p>Relocatable precompiled headers are intended to be used in a limited number
564of cases where the compilation environment is tightly controlled and the
565precompiled header cannot be generated after headers have been installed.
566Relocatable precompiled headers also have some performance impact, because
567the difference in location between the header locations at PCH build time vs.
568at the time of PCH use requires one of the PCH optimizations,
569<code>stat()</code> caching, to be disabled. However, this change is only
570likely to affect PCH files that reference a large number of headers.</p>
Chris Lattnercf17d9d2009-04-20 04:23:09 +0000571
Mike Stump53664ca2009-12-14 23:53:10 +0000572<!-- = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = -->
573<h3 id="codegen">Controlling Code Generation</h3>
574<!-- = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = -->
575
576<p>Clang provides a number of ways to control code generation. The options are listed below.</p>
577
578<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
579<dt id="opt_fcatch-undefined-behavior"><b>-fcatch-undefined-behavior</b>: Turn
580on runtime code generation to check for undefined behavior.</dt>
581
582<dd>This option, which defaults to off, controls whether or not Clang
583adds runtime checks for undefined runtime behavior. If the check fails,
584<tt>__builtin_trap()</tt> is used to indicate failure.
585The checks are:
586<p>
Mike Stump88b2a172009-12-16 03:25:12 +0000587<li>Subscripting where the static type of one operand is variable
588 which is decayed from an array type and the other operand is
589 greater than the size of the array or less than zero.</li>
Mike Stump53664ca2009-12-14 23:53:10 +0000590<li>Shift operators where the amount shifted is greater or equal to the
591 promoted bit-width of the left-hand-side or less than zero.</li>
Mike Stump8f6a3ed2009-12-16 03:18:14 +0000592<li>If control flow reaches __builtin_unreachable.
593<li>When llvm implements more __builtin_object_size support, reads and
594 writes for objects that __builtin_object_size indicates we aren't
595 accessing valid memory. Bit-fields and vectors are not yet checked.
Mike Stump53664ca2009-12-14 23:53:10 +0000596</p>
597</dd>
598
Nuno Lopesaa526242009-12-17 10:00:52 +0000599<dt id="opt_fno-assume-sane-operator-new"><b>-fno-assume-sane-operator-new</b>:
600Don't assume that the C++'s new operator is sane.</dt>
Nuno Lopesb23f20d2009-12-17 10:15:49 +0000601<dd>This option tells the compiler to do not assume that C++'s global new
602operator will always return a pointer that do not
Nuno Lopesaa526242009-12-17 10:00:52 +0000603alias any other pointer when the function returns.</dd>
604
Chris Lattnercf17d9d2009-04-20 04:23:09 +0000605<!-- ======================================================================= -->
606<h2 id="c">C Language Features</h2>
607<!-- ======================================================================= -->
608
Eli Friedman660a5ac2009-04-28 18:48:34 +0000609<p>The support for standard C in clang is feature-complete except for the C99
610floating-point pragmas.</p>
Chris Lattnercf17d9d2009-04-20 04:23:09 +0000611
612<!-- = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = -->
Eli Friedman660a5ac2009-04-28 18:48:34 +0000613<h3 id="c_ext">Extensions supported by clang</h3>
Chris Lattnercf17d9d2009-04-20 04:23:09 +0000614<!-- = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = -->
615
Eli Friedman660a5ac2009-04-28 18:48:34 +0000616<p>See <a href="LanguageExtensions.html">clang language extensions</a>.</p>
Chris Lattnercf17d9d2009-04-20 04:23:09 +0000617
Eli Friedman660a5ac2009-04-28 18:48:34 +0000618<!-- = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = -->
619<h3 id="c_modes">Differences between various standard modes</h3>
620<!-- = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = -->
Chris Lattnercf17d9d2009-04-20 04:23:09 +0000621
Eli Friedman660a5ac2009-04-28 18:48:34 +0000622<p>clang supports the -std option, which changes what language mode clang uses.
623The supported modes for C are c89, gnu89, c94, c99, gnu99 and various aliases
624for those modes. If no -std option is specified, clang defaults to gnu99 mode.
625</p>
Chris Lattnercf17d9d2009-04-20 04:23:09 +0000626
Eli Friedman660a5ac2009-04-28 18:48:34 +0000627<p>Differences between all c* and gnu* modes:</p>
628<ul>
629<li>c* modes define "__STRICT_ANSI__".</li>
Eli Friedman26fa0ed2009-05-27 23:02:57 +0000630<li>Target-specific defines not prefixed by underscores, like "linux", are
631defined in gnu* modes.</li>
Eli Friedman660a5ac2009-04-28 18:48:34 +0000632<li>Trigraphs default to being off in gnu* modes; they can be enabled by the
633-trigraphs option.</li>
634<li>The parser recognizes "asm" and "typeof" as keywords in gnu* modes; the
635variants "__asm__" and "__typeof__" are recognized in all modes.</li>
Eli Friedmanb0ac5452009-05-16 23:17:30 +0000636<li>The Apple "blocks" extension is recognized by default in gnu* modes
637on some platforms; it can be enabled in any mode with the "-fblocks"
638option.</li>
Eli Friedman660a5ac2009-04-28 18:48:34 +0000639</ul>
640
641<p>Differences between *89 and *99 modes:</p>
642<ul>
643<li>The *99 modes default to implementing "inline" as specified in C99, while
644the *89 modes implement the GNU version. This can be overridden for individual
645functions with the __gnu_inline__ attribute.</li>
Eli Friedman26fa0ed2009-05-27 23:02:57 +0000646<li>Digraphs are not recognized in c89 mode.</li>
Eli Friedman660a5ac2009-04-28 18:48:34 +0000647<li>The scope of names defined inside a "for", "if", "switch", "while", or "do"
648statement is different. (example: "if ((struct x {int x;}*)0) {}".)</li>
649<li>__STDC_VERSION__ is not defined in *89 modes.</li>
Eli Friedman26fa0ed2009-05-27 23:02:57 +0000650<li>"inline" is not recognized as a keyword in c89 mode.</li>
651<li>"restrict" is not recognized as a keyword in *89 modes.</li>
Eli Friedman660a5ac2009-04-28 18:48:34 +0000652<li>Commas are allowed in integer constant expressions in *99 modes.</li>
653<li>Arrays which are not lvalues are not implicitly promoted to pointers in
654*89 modes.</li>
Eli Friedman660a5ac2009-04-28 18:48:34 +0000655<li>Some warnings are different.</li>
656</ul>
657
658<p>c94 mode is identical to c89 mode except that digraphs are enabled in
659c94 mode (FIXME: And __STDC_VERSION__ should be defined!).</p>
660
661<!-- = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = -->
662<h3 id="c_unimpl_gcc">GCC extensions not implemented yet</h3>
663<!-- = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = -->
664
665<p>clang tries to be compatible with gcc as much as possible, but some gcc
666extensions are not implemented yet:</p>
667
668<ul>
669<li>clang does not support __label__
670(<a href="http://llvm.org/bugs/show_bug.cgi?id=3429">bug 3429</a>). This is
671a relatively small feature, so it is likely to be implemented relatively
672soon.</li>
673
674<li>clang does not support attributes on function pointers
675(<a href="http://llvm.org/bugs/show_bug.cgi?id=2461">bug 2461</a>). This is
676a relatively important feature, so it is likely to be implemented relatively
677soon.</li>
678
679<li>clang does not support #pragma weak
Eli Friedman4da92552009-06-02 08:21:31 +0000680(<a href="http://llvm.org/bugs/show_bug.cgi?id=3679">bug 3679</a>). Due to
Eli Friedman660a5ac2009-04-28 18:48:34 +0000681the uses described in the bug, this is likely to be implemented at some
682point, at least partially.</li>
683
684<li>clang does not support #pragma align
Eli Friedman4da92552009-06-02 08:21:31 +0000685(<a href="http://llvm.org/bugs/show_bug.cgi?id=3811">bug 3811</a>). This is a
Eli Friedman660a5ac2009-04-28 18:48:34 +0000686relatively small feature, so it is likely to be implemented relatively
687soon.</li>
688
Eli Friedman4da92552009-06-02 08:21:31 +0000689<li>clang does not support code generation for local variables pinned to
690registers (<a href="http://llvm.org/bugs/show_bug.cgi?id=3933">bug 3933</a>).
691This is a relatively small feature, so it is likely to be implemented
692relatively soon.</li>
Eli Friedman660a5ac2009-04-28 18:48:34 +0000693
694<li>clang does not support decimal floating point types (_Decimal32 and
695friends) or fixed-point types (_Fract and friends); nobody has expressed
696interest in these features yet, so it's hard to say when they will be
697implemented.</li>
698
699<li>clang does not support nested functions; this is a complex feature which
700is infrequently used, so it is unlikely to be implemented anytime soon.</li>
701
Daniel Dunbar05fa6292009-05-12 23:12:07 +0000702<li>clang does not support global register variables, this is unlikely
Eli Friedmanca130bd2009-06-12 20:04:25 +0000703to be implemented soon because it requires additional LLVM backend support.
704</li>
Daniel Dunbar05fa6292009-05-12 23:12:07 +0000705
706<li>clang does not support static initialization of flexible array
707members. This appears to be a rarely used extension, but could be
708implemented pending user demand.</li>
709
Eli Friedmanca130bd2009-06-12 20:04:25 +0000710<li>clang does not support __builtin_va_arg_pack/__builtin_va_arg_pack_len.
711This is used rarely, but in some potentially interesting places, like the
712glibc headers, so it may be implemented pending user demand. Note that
713because clang pretends to be like GCC 4.2, and this extension was introduced
714in 4.3, the glibc headers will not try to use this extension with clang at
715the moment.</li>
716
717<li>clang does not support the gcc extension for forward-declaring function
718parameters; this has not showed up in any real-world code yet, though, so it
719might never be implemented.</li>
720
Eli Friedman660a5ac2009-04-28 18:48:34 +0000721</ul>
722
723<p>This is not a complete list; if you find an unsupported extension
724missing from this list, please send an e-mail to cfe-dev. This list
725currently excludes C++; see <a href="#cxx">C++ Language Features</a>.
726Also, this list does not include bugs in mostly-implemented features; please
727see the <a href="http://llvm.org/bugs/buglist.cgi?quicksearch=product%3Aclang+component%3A-New%2BBugs%2CAST%2CBasic%2CDriver%2CHeaders%2CLLVM%2BCodeGen%2Cparser%2Cpreprocessor%2CSemantic%2BAnalyzer">
728bug tracker</a> for known existing bugs (FIXME: Is there a section for
729bug-reporting guidelines somewhere?).</p>
730
731<!-- = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = -->
732<h3 id="c_unsupp_gcc">Intentionally unsupported GCC extensions</h3>
733<!-- = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = -->
734
Eli Friedman0b326002009-06-12 20:11:05 +0000735<ul>
736
Eli Friedman2f3fca02009-06-12 20:08:48 +0000737<li>clang does not support the gcc extension that allows variable-length arrays
Eli Friedman660a5ac2009-04-28 18:48:34 +0000738in structures. This is for a few of reasons: one, it is tricky
739to implement, two, the extension is completely undocumented, and three, the
Eli Friedman2f3fca02009-06-12 20:08:48 +0000740extension appears to be rarely used.</li>
Eli Friedman660a5ac2009-04-28 18:48:34 +0000741
Eli Friedman2f3fca02009-06-12 20:08:48 +0000742<li>clang does not support duplicate definitions of a function where one is
Daniel Dunbarc5f928c2009-06-04 18:37:52 +0000743inline. This complicates clients of the AST which normally can expect there is
744at most one definition for each function. Source code using this feature should
745be changed to define the inline and out-of-line definitions in separate
Eli Friedman2f3fca02009-06-12 20:08:48 +0000746translation units.</li>
Daniel Dunbarc5f928c2009-06-04 18:37:52 +0000747
Eli Friedmanca130bd2009-06-12 20:04:25 +0000748<li>clang does not have an equivalent to gcc's "fold"; this means that
749clang doesn't accept some constructs gcc might accept in contexts where a
750constant expression is required, like "x-x" where x is a variable, or calls
751to C library functions like strlen.</li>
752
Eli Friedman2f3fca02009-06-12 20:08:48 +0000753<li>clang does not support multiple alternative constraints in inline asm; this
Eli Friedmanca130bd2009-06-12 20:04:25 +0000754is an extremely obscure feature which would be complicated to implement
Eli Friedman2f3fca02009-06-12 20:08:48 +0000755correctly.</li>
Eli Friedmanca130bd2009-06-12 20:04:25 +0000756
757<li>clang does not support __builtin_apply and friends; this extension is
758extremely obscure and difficult to implement reliably.</li>
759
Eli Friedman0b326002009-06-12 20:11:05 +0000760</ul>
761
Eli Friedman660a5ac2009-04-28 18:48:34 +0000762<!-- = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = -->
763<h3 id="c_ms">Microsoft extensions</h3>
764<!-- = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = -->
765
766<p>clang has some experimental support for extensions from
767Microsoft Visual C++; to enable it, use the -fms-extensions command-line
Eli Friedmana1821b52009-06-08 06:21:03 +0000768option. This is the default for Windows targets. Note that the
769support is incomplete; enabling Microsoft extensions will silently drop
770certain constructs (including __declspec and Microsoft-style asm statements).
771</p>
Chris Lattnercf17d9d2009-04-20 04:23:09 +0000772
Daniel Dunbar05fa6292009-05-12 23:12:07 +0000773<li>clang does not support the Microsoft extension where anonymous
Daniel Dunbar869e0542009-05-13 00:23:53 +0000774record members can be declared using user defined typedefs.</li>
Daniel Dunbar05fa6292009-05-12 23:12:07 +0000775
Daniel Dunbar9375ed12009-05-13 21:40:49 +0000776<li>clang supports the Microsoft "#pragma pack" feature for
777controlling record layout. GCC also contains support for this feature,
778however where MSVC and GCC are incompatible clang follows the MSVC
779definition.</li>
780
Chris Lattnercf17d9d2009-04-20 04:23:09 +0000781<!-- ======================================================================= -->
782<h2 id="objc">Objective-C Language Features</h2>
783<!-- ======================================================================= -->
784
785
786<!-- = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = -->
787<h3 id="objc_incompatibilities">Intentional Incompatibilities with GCC</h3>
788<!-- = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = -->
789
790<p>No cast of super, no lvalue casts.</p>
791
792
793
794<!-- ======================================================================= -->
795<h2 id="cxx">C++ Language Features</h2>
796<!-- ======================================================================= -->
797
Douglas Gregor57f1a002010-02-05 23:51:14 +0000798<p>At this point, Clang C++ is not production-quality and is not recommended for use beyond experimentation. However, Clang C++ support
Chris Lattnercf17d9d2009-04-20 04:23:09 +0000799is under active development and is progressing rapidly. Please see the <a
800href="http://clang.llvm.org/cxx_status.html">C++ Status</a> page for details or
801ask on the mailing list about how you can help.</p>
802
Douglas Gregor57f1a002010-02-05 23:51:14 +0000803<p>Note that released Clang compilers will refuse to even try to use clang to compile C++ code unless you pass the <tt>-ccc-clang-cxx</tt> option to the driver. To turn on Clang's C++ support, please pass that flag. Clang compilers built from the Subversion trunk enable C++ support by default, and do not require the <tt>-ccc-clang-cxx</tt> flag.</p>
Chris Lattnercf17d9d2009-04-20 04:23:09 +0000804
Rafael Espindola9b2fc952010-03-17 04:31:53 +0000805<p>Clang strives to strictly conform to the C++ standard. That means
806it will reject invalid C++ code that another compiler may accept. If
807Clang reports errors in your code, please check
808the <a href="http://clang.llvm.org/cxx_compatibility.html">C++
809Compatibility</a> page to see whether they are C++-conformance bugs
810and how you can fix them.</p>
811
Chris Lattnercf17d9d2009-04-20 04:23:09 +0000812<!-- ======================================================================= -->
813<h2 id="objcxx">Objective C++ Language Features</h2>
814<!-- ======================================================================= -->
815
816<p>At this point, Clang C++ support is not generally useful (and therefore,
817neither is Objective-C++). Please see the <a href="#cxx">C++ section</a> for
818more information.</p>
819
820<!-- ======================================================================= -->
821<h2 id="target_features">Target-Specific Features and Limitations</h2>
822<!-- ======================================================================= -->
823
824
825<!-- = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = -->
826<h3 id="target_arch">CPU Architectures Features and Limitations</h3>
827<!-- = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = -->
828
829<!-- ======================== -->
830<h4 id="target_arch_x86">X86</h4>
831<!-- ======================== -->
Eli Friedman3b658d32009-06-08 05:12:39 +0000832<p>The support for X86 (both 32-bit and 64-bit) is considered stable
833on Darwin (Mac OS/X), Linux, FreeBSD, and Dragonfly BSD: it has been tested to
834correctly compile large C and Objective-C codebases. (FIXME: Anything specific
835we want to say here? Possibly mention some LLVM x86 limitations?)
Chris Lattnercf17d9d2009-04-20 04:23:09 +0000836
Eli Friedman3b658d32009-06-08 05:12:39 +0000837<!-- ======================== -->
838<h4 id="target_arch_arm">ARM</h4>
839<!-- ======================== -->
840ARM support is mostly feature-complete, but still experimental; it hasn't
841undergone significant testing.
842
843<!-- ======================== -->
844<h4 id="target_arch_other">Other platforms</h4>
845<!-- ======================== -->
846clang currently contains some support for PPC and Sparc; however, significant
847pieces of code generation are still missing, and they haven't undergone
848significant testing.
849
850<p>clang contains some support for the embedded PIC16 processor
851(FIXME: I haven't been keeping track of this; what should this say?).
852
853<p>clang contains limited support for the MSP430 embedded processor, but both
854the clang support and the LLVM backend support are highly experimental.
855
856<p>Other platforms are completely unsupported at the moment. Adding the
857minimal support needed for parsing and semantic analysis on a new platform
858is quite easy; see lib/Basic/Targets.cpp in the clang source tree. This level
859of support is also sufficient for conversion to LLVM IR for simple programs.
860Proper support for conversion to LLVM IR requires adding code to
861lib/CodeGen/CGCall.cpp at the moment; this is likely to change soon, though.
862Generating assembly requires a suitable LLVM backend.
Chris Lattnercf17d9d2009-04-20 04:23:09 +0000863
864<!-- = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = -->
865<h3 id="target_os">Operating System Features and Limitations</h3>
866<!-- = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = -->
867
868<!-- ======================================= -->
869<h4 id="target_os_darwin">Darwin (Mac OS/X)</h4>
870<!-- ======================================= -->
871
872<p>No __thread support, 64-bit ObjC support requires SL tools.</p>
873
874</div>
875</body>
876</html>