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 <http://clang.llvm.org>`_ or the `LLVM Web
Site <http://llvm.org>`_.
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 <InternalsManual.html>`_. If you are interested in the `Clang
Static Analyzer <http://clang-analyzer.llvm.org>`_, please see its web
page.
Clang is designed to support the C family of programming languages,
which includes :ref:`C <c>`, :ref:`Objective-C <objc>`, :ref:`C++ <cxx>`, and
:ref:`Objective-C++ <objcxx>` as well as many dialects of those. For
language-specific information, please see the corresponding language
specific section:
-:ref:`C Language <c>`: K&R C, ANSI C89, ISO C90, ISO C94 (C89+AMD1), ISO
C99 (+TC1, TC2, TC3).
-:ref:`Objective-C Language <objc>`: ObjC 1, ObjC 2, ObjC 2.1, plus
variants depending on base language.
-:ref:`C++ Language <cxx>`
-:ref:`Objective C++ Language <objcxx>`
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 :ref:`Target-Specific Features and
Limitations <target_features>` section for more details.
The rest of the introduction introduces some basic :ref:`compiler
terminology <terminology>` that is used throughout this manual and
contains a basic :ref:`introduction to using Clang <basicusage>` as a
command line compiler.
.._terminology:
Terminology
-----------
Front end, parser, backend, preprocessor, undefined behavior,
diagnostic, optimizer
.._basicusage:
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.
**-Weverything**: :ref:`Enable **all**
warnings. <diagnostics_enable_everything>`
**-pedantic**: Warn on language extensions.
**-pedantic-errors**: Error on language extensions.
**-Wsystem-headers**: Enable warnings from system headers.
**-ferror-limit=123**: Stop emitting diagnostics after 123 errors have
been produced. The default is 20, and the error limit can be disabled
with -ferror-limit=0.
**-ftemplate-backtrace-limit=123**: 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.
.._cl_diag_formatting:
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.
.._opt_fshow-column:
**-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.
The printed column numbers count bytes from the beginning of the
line; take care if your source contains multibyte characters.
.._opt_fshow-source-location:
**-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.
.._opt_fcaret-diagnostics:
**-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
^
//
**-f[no-]color-diagnostics**
This option, which defaults to on when a color-capable terminal is
detected, controls whether or not Clang prints diagnostics in color.
When this option is enabled, Clang will use colors to highlight
specific parts of the diagnostic, e.g.,
.. nasty hack to not lose our dignity
..raw:: html
<pre>
<b><span style="color:black">test.c:28:8: <span style="color:magenta">warning</span>: extra tokens at end of #endif directive [-Wextra-tokens]</span></b>
#endif bad
<span style="color:green">^</span>
<span style="color:green">//</span>
</pre>
When this is disabled, Clang will just print:
::
test.c:2:8: warning: extra tokens at end of #endif directive [-Wextra-tokens]
#endif bad
^
//
**-fdiagnostics-format=clang/msvc/vi**
Changes diagnostic output format to better match IDEs and command line tools.
This option controls the output format of the filename, line number,
and column printed in diagnostic messages. The options, and their
affect on formatting a simple conversion diagnostic, follow:
**clang** (default)
::
t.c:3:11: warning: conversion specifies type 'char *' but the argument has type 'int'
**msvc**
::
t.c(3,11) : warning: conversion specifies type 'char *' but the argument has type 'int'
**vi**
::
t.c +3:11: warning: conversion specifies type 'char *' but the argument has type 'int'
**-f[no-]diagnostics-show-name**
Enable the display of the diagnostic name.
This option, which defaults to off, controls whether or not Clang
prints the associated name.
.._opt_fdiagnostics-show-option:
**-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 :ref:`warning group <cl_diag_warning_groups>`
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 [:ref:`-Wextra-tokens <opt_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
Enable printing category information in diagnostic line.
This option, which defaults to "none", controls whether or not Clang
prints the category associated with a diagnostic when emitting it.
Each diagnostic may or many not have an associated category, if it
has one, it is listed in the diagnostic categorization field of the
diagnostic line (in the []'s).
For example, a format string warning will produce these three
renditions based on the setting of this option:
::
t.c:3:11: warning: conversion specifies type 'char *' but the argument has type 'int' [-Wformat]
t.c:3:11: warning: conversion specifies type 'char *' but the argument has type 'int' [-Wformat,1]
t.c:3:11: warning: conversion specifies type 'char *' but the argument has type 'int' [-Wformat,Format String]
This category can be used by clients that want to group diagnostics
by category, so it should be a high level category. We want dozens
of these, not hundreds or thousands of them.
.._opt_fdiagnostics-fixit-info:
**-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.
.._opt_fdiagnostics-print-source-range-info:
**-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.
The printed column numbers count bytes from the beginning of the
line; take care if your source contains multibyte characters.
**-fdiagnostics-parseable-fixits**
Print Fix-Its in a machine parseable form.
This option makes Clang print available Fix-Its in a machine
parseable format at the end of diagnostics. The following example
illustrates the format:
::
fix-it:"t.cpp":{7:25-7:29}:"Gamma"
The range printed is a half-open range, so in this example the
characters at column 25 up to but not including column 29 on line 7
in t.cpp should be replaced with the string "Gamma". Either the
range or the replacement string may be empty (representing strict
insertions and strict erasures, respectively). Both the file name
and the insertion string escape backslash (as "\\\\"), tabs (as
"\\t"), newlines (as "\\n"), double quotes(as "\\"") and
non-printable characters (as octal "\\xxx").
The printed column numbers count bytes from the beginning of the
line; take care if your source contains multibyte characters.
**-fno-elide-type**
Turns off elision in template type printing.
The default for template type printing is to elide as many template
arguments as possible, removing those which are the same in both
template types, leaving only the differences. Adding this flag will
print all the template arguments. If supported by the terminal,
highlighting will still appear on differing arguments.
Default:
::
t.cc:4:5: note: candidate function not viable: no known conversion from 'vector<map<[...], map<float, [...]>>>' to 'vector<map<[...], map<double, [...]>>>' for 1st argument;
-fno-elide-type:
::
t.cc:4:5: note: candidate function not viable: no known conversion from 'vector<map<int, map<float, int>>>' to 'vector<map<int, map<double, int>>>' for 1st argument;
**-fdiagnostics-show-template-tree**
Template type diffing prints a text tree.
For diffing large templated types, this option will cause Clang to
display the templates as an indented text tree, one argument per
line, with differences marked inline. This is compatible with
-fno-elide-type.
Default:
::
t.cc:4:5: note: candidate function not viable: no known conversion from 'vector<map<[...], map<float, [...]>>>' to 'vector<map<[...], map<double, [...]>>>' for 1st argument;
-fdiagnostics-show-template-tree
::
t.cc:4:5: note: candidate function not viable: no known conversion for 1st argument;
vector<
map<
[...],
map<
[float != float],
[...]>>>
.._cl_diag_warning_groups:
Individual Warning Groups
^^^^^^^^^^^^^^^^^^^^^^^^^
TODO: Generate this from tblgen. Define one anchor per warning group.
.._opt_wextra-tokens:
**-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.
**-Wambiguous-member-template**
Warn about unqualified uses of a member template whose name resolves to
another template at the location of the use.
This option, which defaults to on, enables a warning in the