Clang Compiler User's Manual
- Introduction
- Command Line Options
- Language and Target-Independent Features
- C Language Features
- Objective-C Language Features
- C++ Language Features
- ...
- Objective C++ Language Features
- ...
- Target-Specific Features and Limitations
Introduction
The Clang Compiler is an open-source compiler for the C family of programming languages, aiming to be the best in class implementation of these languages. Clang builds on the LLVM optimizer and code generator, allowing it to provide high-quality optimization and code generation support for many targets. For more general information, please see the Clang Web Site or the LLVM Web Site.
This document describes important notes about using Clang as a compiler for an end-user, documenting the supported features, command line options, etc. If you are interested in using Clang to build a tool that processes code, please see the Clang Internals Manual. If you are interested in the Clang Static Analyzer, please see its web page.
Clang is designed to support the C family of programming languages, which includes C, Objective-C, C++, and Objective-C++ as well as many dialects of those. For language-specific information, please see the corresponding language specific section:
- C Language: K&R C, ANSI C89, ISO C90, ISO C94 (C89+AMD1), ISO C99 (+TC1, TC2, TC3).
- Objective-C Language: ObjC 1, ObjC 2, ObjC 2.1, plus variants depending on base language.
- C++ Language Features
- Objective C++ Language
In addition to these base languages and their dialects, Clang supports a broad variety of language extensions, which are documented in the corresponding language section. These extensions are provided to be compatible with the GCC, Microsoft, and other popular compilers as well as to improve functionality through Clang-specific features. The Clang driver and language features are intentionally designed to be as compatible with the GNU GCC compiler as reasonably possible, easing migration from GCC to Clang. In most cases, code "just works".
In addition to language specific features, Clang has a variety of features that depend on what CPU architecture or operating system is being compiled for. Please see the Target-Specific Features and Limitations section for more details.
The rest of the introduction introduces some basic compiler terminology that is used throughout this manual and contains a basic introduction to using Clang as a command line compiler.
Terminology
Front end, parser, backend, preprocessor, undefined behavior, diagnostic, optimizer
Basic Usage
Intro to how to use a C compiler for newbies.
compile + link compile then link debug info enabling optimizations picking a language to use, defaults to C99 by default. Autosenses based on extension. using a makefile
Command Line Options
This section is generally an index into other sections. It does not go into depth on the ones that are covered by other sections. However, the first part introduces the language selection and other high level options like -c, -g, etc.
Options to Control Error and Warning Messages
-Werror: Turn warnings into errors.
-Werror=foo: Turn warning "foo" into an error.
-Wno-error=foo: Turn warning "foo" into an warning even if -Werror is specified.
-Wfoo: Enable warning foo
-Wno-foo: Disable warning foo
-w: Disable all warnings.
-pedantic: Warn on language extensions.
-pedantic-errors: Error on language extensions.
-Wsystem-headers: Enable warnings from system headers.
Formatting of Diagnostics
Clang aims to produce beautiful diagnostics by default, particularly for new users that first come to Clang. However, different people have different preferences, and sometimes Clang is driven by another program that wants to parse simple and consistent output, not a person. For these cases, Clang provides a wide range of options to control the exact output format of the diagnostics that it generates.
- -f[no-]show-column: Print column number in diagnostic.
- This option, which defaults to on, controls whether or not Clang prints the column number of a diagnostic. For example, when this is enabled, Clang will print something like:
test.c:28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens] #endif bad ^ //
When this is disabled, Clang will print "test.c:28: warning..." with no column number.
- -f[no-]show-source-location: Print source file/line/column information in diagnostic.
- This option, which defaults to on, controls whether or not Clang prints the filename, line number and column number of a diagnostic. For example, when this is enabled, Clang will print something like:
test.c:28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens] #endif bad ^ //
When this is disabled, Clang will not print the "test.c:28:8: " part.
- -f[no-]caret-diagnostics: Print source line and ranges from source code in diagnostic.
- This option, which defaults to on, controls whether or not Clang prints the source line, source ranges, and caret when emitting a diagnostic. For example, when this is enabled, Clang will print something like:
test.c:28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens] #endif bad ^ //
When this is disabled, Clang will just print:
test.c:28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens]
- -f[no-]diagnostics-show-option: Enable [-Woption] information in diagnostic line.
- This option, which defaults to on, controls whether or not Clang prints the associated warning group option name when outputting a warning diagnostic. For example, in this output:
test.c:28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens] #endif bad ^ //
Passing -fno-diagnostics-show-option will prevent Clang from printing the [-Wextra-tokens] information in the diagnostic. This information tells you the flag needed to enable or disable the diagnostic, either from the command line or through #pragma GCC diagnostic.
- -f[no-]diagnostics-fixit-info: Enable "FixIt" information in the diagnostics output.
- This option, which defaults to on, controls whether or not Clang prints the information on how to fix a specific diagnostic underneath it when it knows. For example, in this output:
test.c:28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens] #endif bad ^ //
Passing -fno-diagnostics-fixit-info will prevent Clang from printing the "//" line at the end of the message. This information is useful for users who may not understand what is wrong, but can be confusing for machine parsing.
- -f[no-]diagnostics-print-source-range-info: Print machine parsable information about source ranges.
- This option, which defaults to off, controls whether or not Clang prints information about source ranges in a machine parsable format after the file/line/column number information. The information is a simple sequence of brace enclosed ranges, where each range lists the start and end line/column locations. For example, in this output:
exprs.c:47:15:{47:8-47:14}{47:17-47:24}: error: invalid operands to binary expression ('int *' and '_Complex float') P = (P-42) + Gamma*4; ~~~~~~ ^ ~~~~~~~
The {}'s are generated by -fdiagnostics-print-source-range-info.
Individual Warning Groups
TODO: Generate this from tblgen. Define one anchor per warning group.
- -Wextra-tokens: Warn about excess tokens at the end of a preprocessor directive.
- This option, which defaults to on, enables warnings about extra tokens at the end of preprocessor directives. For example:
test.c:28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens] #endif bad ^
These extra tokens are not strictly conforming, and are usually best handled by commenting them out.
Language and Target-Independent Features
Controlling Errors and Warnings
Clang provides a number of ways to control which code constructs cause it to emit errors and warning messages, and how they are displayed to the console.
Controlling How Clang Displays Diagnostics
When Clang emits a diagnostic, it includes rich information in the output, and gives you fine-grain control over which information is printed. Clang has the ability to print this information, and these are the options that control it:
- A file/line/column indicator that shows exactly where the diagnostic occurs in your code [-fshow-column, -fshow-source-location].
- A categorization of the diagnostic as a note, warning, error, or fatal error.
- A text string that describes what the problem is.
- An option that indicates how to control the diagnostic (for diagnostics that support it) [-fdiagnostics-show-option].
- The line of source code that the issue occurs on, along with a caret and ranges that indicate the important locations [-fcaret-diagnostics].
- "FixIt" information, which is a concise explanation of how to fix the problem (when Clang is certain it knows) [-fdiagnostics-fixit-info].
- A machine-parsable representation of the ranges involved (off by default) [-fdiagnostics-print-source-range-info].
For more information please see Formatting of Diagnostics.
Controlling Which Diagnostics Clang Generates
mappings: ignore, note, warning, error, fatal
The two major classes are control from the command line and control via pragmas in your code.
-W flags, -pedantic, etc
pragma GCC diagnostic
Precompiled Headers
Precompiled headers are a general approach employed by many compilers to reduce compilation time. The underlying motivation of the approach is that it is common for the same (and often large) header files to be included by multiple source files. Consequently, compile times can often be greatly improved by caching some of the (redundant) work done by a compiler to process headers. Precompiled header files, which represent one of many ways to implement this optimization, are literally files that represent an on-disk cache that contains the vital information necessary to reduce some of the work needed to process a corresponding header file. While details of precompiled headers vary between compilers, precompiled headers have been shown to be a highly effective at speeding up program compilation on systems with very large system headers (e.g., Mac OS/X).
Clang supports an implementation of precompiled headers known as pre-tokenized headers (PTH). Clang's pre-tokenized headers support most of same interfaces as GCC's pre-compiled headers (as well as others) but are completely different in their implementation. If you are interested in how PTH is implemented, please see the PTH Internals document.
Generating a PTH File
To generate a PTH file using Clang, one invokes Clang with the -x <language>-header option. This mirrors the interface in GCC for generating PCH files:
$ gcc -x c-header test.h -o test.h.gch $ clang -x c-header test.h -o test.h.pth
Using a PTH File
A PTH file can then be used as a prefix header when a -include option is passed to clang:
$ clang -include test.h test.c -o test
The clang driver will first check if a PTH file for test.h is available; if so, the contents of test.h (and the files it includes) will be processed from the PTH file. Otherwise, Clang falls back to directly processing the content of test.h. This mirrors the behavior of GCC.
NOTE: Clang does not automatically use PTH files for headers that are directly included within a source file. For example:
$ clang -x c-header test.h -o test.h.pth $ cat test.c #include "test.h" $ clang test.c -o test
In this example, clang will not automatically use the PTH file for test.h since test.h was included directly in the source file and not specified on the command line using -include.
C Language Features
The support for standard C in clang is feature-complete except for the C99 floating-point pragmas.
Extensions supported by clang
See clang language extensions.
Differences between various standard modes
clang supports the -std option, which changes what language mode clang uses. The supported modes for C are c89, gnu89, c94, c99, gnu99 and various aliases for those modes. If no -std option is specified, clang defaults to gnu99 mode.
Differences between all c* and gnu* modes:
- c* modes define "__STRICT_ANSI__".
- Target-specific defines not prefixed by underscores, like "linux", are defined in gnu* modes.
- Trigraphs default to being off in gnu* modes; they can be enabled by the -trigraphs option.
- The parser recognizes "asm" and "typeof" as keywords in gnu* modes; the variants "__asm__" and "__typeof__" are recognized in all modes.
- Some warnings are different.
Differences between *89 and *99 modes:
- The *99 modes default to implementing "inline" as specified in C99, while the *89 modes implement the GNU version. This can be overridden for individual functions with the __gnu_inline__ attribute.
- Digraphs are enabled in the *99 modes.
- The scope of names defined inside a "for", "if", "switch", "while", or "do" statement is different. (example: "if ((struct x {int x;}*)0) {}".)
- __STDC_VERSION__ is not defined in *89 modes.
- "inline" and "restrict" are not recognized as keywords in c89 mode.
- Commas are allowed in integer constant expressions in *99 modes.
- Arrays which are not lvalues are not implicitly promoted to pointers in *89 modes.
- Constructs like "&*X" are always allowed in *99 modes.
- Some warnings are different.
c94 mode is identical to c89 mode except that digraphs are enabled in c94 mode (FIXME: And __STDC_VERSION__ should be defined!).
GCC extensions not implemented yet
clang tries to be compatible with gcc as much as possible, but some gcc extensions are not implemented yet:
- clang does not support __label__ (bug 3429). This is a relatively small feature, so it is likely to be implemented relatively soon.
- clang does not support attributes on function pointers (bug 2461). This is a relatively important feature, so it is likely to be implemented relatively soon.
- clang does not support #pragma weak (bug 3679). Due to the uses described in the bug, this is likely to be implemented at some point, at least partially.
- clang does not support #pragma align (bug 3811). This is a relatively small feature, so it is likely to be implemented relatively soon.
- clang does not implement overloads for the __sync_* builtins (bug 3824). The builtins only currently work with 32-bit types. This is a relatively small feature, so it is likely to be implemented relatively soon.
- clang does not support code generation for variables pinned to registers (bug 3933). This is a relatively small feature, so it is likely to be implemented relatively soon.
- clang does not support decimal floating point types (_Decimal32 and friends) or fixed-point types (_Fract and friends); nobody has expressed interest in these features yet, so it's hard to say when they will be implemented.
- clang does not support nested functions; this is a complex feature which is infrequently used, so it is unlikely to be implemented anytime soon.
- clang does not support __builtin_apply and friends; this extension requires complex code generator support that does not currently exist in LLVM, and there is very little demand, so it is unlikely to be implemented anytime soon.
This is not a complete list; if you find an unsupported extension missing from this list, please send an e-mail to cfe-dev. This list currently excludes C++; see C++ Language Features. Also, this list does not include bugs in mostly-implemented features; please see the bug tracker for known existing bugs (FIXME: Is there a section for bug-reporting guidelines somewhere?).
Intentionally unsupported GCC extensions
clang does not support the gcc extension that allows variable-length arrays in structures. This is for a few of reasons: one, it is tricky to implement, two, the extension is completely undocumented, and three, the extension appears to be very rarely used.
Microsoft extensions
clang has some experimental support for extensions from Microsoft Visual C++; to enable it, use the -fms-extensions command-line option. Eventually, this will be the default for Windows targets. These extensions are not anywhere near complete, so please do not file bugs; patches are welcome, though.
Objective-C Language Features
Intentional Incompatibilities with GCC
No cast of super, no lvalue casts.
C++ Language Features
At this point, Clang C++ is not generally useful. However, Clang C++ support is under active development and is progressing rapidly. Please see the C++ Status page for details or ask on the mailing list about how you can help.
Note that the clang driver will refuse to even try to use clang to compile C++ code unless you pass the -ccc-clang-cxx option to the driver. If you really want to play with Clang's C++ support, please pass that flag.
Objective C++ Language Features
At this point, Clang C++ support is not generally useful (and therefore, neither is Objective-C++). Please see the C++ section for more information.
Target-Specific Features and Limitations
CPU Architectures Features and Limitations
X86
Operating System Features and Limitations
Darwin (Mac OS/X)
No __thread support, 64-bit ObjC support requires SL tools.