1 ============================
2 Clang Compiler User's Manual
3 ============================
5 .. include:: <isonum.txt>
13 The Clang Compiler is an open-source compiler for the C family of
14 programming languages, aiming to be the best in class implementation of
15 these languages. Clang builds on the LLVM optimizer and code generator,
16 allowing it to provide high-quality optimization and code generation
17 support for many targets. For more general information, please see the
18 `Clang Web Site <https://clang.llvm.org>`_ or the `LLVM Web
19 Site <https://llvm.org>`_.
21 This document describes important notes about using Clang as a compiler
22 for an end-user, documenting the supported features, command line
23 options, etc. If you are interested in using Clang to build a tool that
24 processes code, please see :doc:`InternalsManual`. If you are interested in the
25 `Clang Static Analyzer <https://clang-analyzer.llvm.org>`_, please see its web
28 Clang is one component in a complete toolchain for C family languages.
29 A separate document describes the other pieces necessary to
30 :doc:`assemble a complete toolchain <Toolchain>`.
32 Clang is designed to support the C family of programming languages,
33 which includes :ref:`C <c>`, :ref:`Objective-C <objc>`, :ref:`C++ <cxx>`, and
34 :ref:`Objective-C++ <objcxx>` as well as many dialects of those. For
35 language-specific information, please see the corresponding language
38 - :ref:`C Language <c>`: K&R C, ANSI C89, ISO C90, ISO C94 (C89+AMD1), ISO
40 - :ref:`Objective-C Language <objc>`: ObjC 1, ObjC 2, ObjC 2.1, plus
41 variants depending on base language.
42 - :ref:`C++ Language <cxx>`
43 - :ref:`Objective C++ Language <objcxx>`
44 - :ref:`OpenCL Kernel Language <opencl>`: OpenCL C 1.0, 1.1, 1.2, 2.0, 3.0,
45 and C++ for OpenCL 1.0 and 2021.
47 In addition to these base languages and their dialects, Clang supports a
48 broad variety of language extensions, which are documented in the
49 corresponding language section. These extensions are provided to be
50 compatible with the GCC, Microsoft, and other popular compilers as well
51 as to improve functionality through Clang-specific features. The Clang
52 driver and language features are intentionally designed to be as
53 compatible with the GNU GCC compiler as reasonably possible, easing
54 migration from GCC to Clang. In most cases, code "just works".
55 Clang also provides an alternative driver, :ref:`clang-cl`, that is designed
56 to be compatible with the Visual C++ compiler, cl.exe.
58 In addition to language specific features, Clang has a variety of
59 features that depend on what CPU architecture or operating system is
60 being compiled for. Please see the :ref:`Target-Specific Features and
61 Limitations <target_features>` section for more details.
63 The rest of the introduction introduces some basic :ref:`compiler
64 terminology <terminology>` that is used throughout this manual and
65 contains a basic :ref:`introduction to using Clang <basicusage>` as a
66 command line compiler.
73 Front end, parser, backend, preprocessor, undefined behavior,
81 Intro to how to use a C compiler for newbies.
83 compile + link compile then link debug info enabling optimizations
84 picking a language to use, defaults to C17 by default. Autosenses based
85 on extension. using a makefile
90 This section is generally an index into other sections. It does not go
91 into depth on the ones that are covered by other sections. However, the
92 first part introduces the language selection and other high level
93 options like :option:`-c`, :option:`-g`, etc.
95 Options to Control Error and Warning Messages
96 ---------------------------------------------
100 Turn warnings into errors.
102 .. This is in plain monospaced font because it generates the same label as
103 .. -Werror, and Sphinx complains.
107 Turn warning "foo" into an error.
109 .. option:: -Wno-error=foo
111 Turn warning "foo" into a warning even if :option:`-Werror` is specified.
115 Enable warning "foo".
116 See the :doc:`diagnostics reference <DiagnosticsReference>` for a complete
117 list of the warning flags that can be specified in this way.
121 Disable warning "foo".
125 Disable all diagnostics.
127 .. option:: -Weverything
129 :ref:`Enable all diagnostics. <diagnostics_enable_everything>`
131 .. option:: -pedantic
133 Warn on language extensions.
135 .. option:: -pedantic-errors
137 Error on language extensions.
139 .. option:: -Wsystem-headers
141 Enable warnings from system headers.
143 .. option:: -ferror-limit=123
145 Stop emitting diagnostics after 123 errors have been produced. The default is
146 20, and the error limit can be disabled with `-ferror-limit=0`.
148 .. option:: -ftemplate-backtrace-limit=123
150 Only emit up to 123 template instantiation notes within the template
151 instantiation backtrace for a single warning or error. The default is 10, and
152 the limit can be disabled with `-ftemplate-backtrace-limit=0`.
154 .. _cl_diag_formatting:
156 Formatting of Diagnostics
157 ^^^^^^^^^^^^^^^^^^^^^^^^^
159 Clang aims to produce beautiful diagnostics by default, particularly for
160 new users that first come to Clang. However, different people have
161 different preferences, and sometimes Clang is driven not by a human,
162 but by a program that wants consistent and easily parsable output. For
163 these cases, Clang provides a wide range of options to control the exact
164 output format of the diagnostics that it generates.
166 .. _opt_fshow-column:
168 .. option:: -f[no-]show-column
170 Print column number in diagnostic.
172 This option, which defaults to on, controls whether or not Clang
173 prints the column number of a diagnostic. For example, when this is
174 enabled, Clang will print something like:
178 test.c:28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens]
183 When this is disabled, Clang will print "test.c:28: warning..." with
186 The printed column numbers count bytes from the beginning of the
187 line; take care if your source contains multibyte characters.
189 .. _opt_fshow-source-location:
191 .. option:: -f[no-]show-source-location
193 Print source file/line/column information in diagnostic.
195 This option, which defaults to on, controls whether or not Clang
196 prints the filename, line number and column number of a diagnostic.
197 For example, when this is enabled, Clang will print something like:
201 test.c:28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens]
206 When this is disabled, Clang will not print the "test.c:28:8: "
209 .. _opt_fcaret-diagnostics:
211 .. option:: -f[no-]caret-diagnostics
213 Print source line and ranges from source code in diagnostic.
214 This option, which defaults to on, controls whether or not Clang
215 prints the source line, source ranges, and caret when emitting a
216 diagnostic. For example, when this is enabled, Clang will print
221 test.c:28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens]
226 .. option:: -f[no-]color-diagnostics
228 This option, which defaults to on when a color-capable terminal is
229 detected, controls whether or not Clang prints diagnostics in color.
231 When this option is enabled, Clang will use colors to highlight
232 specific parts of the diagnostic, e.g.,
234 .. nasty hack to not lose our dignity
239 <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>
241 <span style="color:green">^</span>
242 <span style="color:green">//</span>
245 When this is disabled, Clang will just print:
249 test.c:2:8: warning: extra tokens at end of #endif directive [-Wextra-tokens]
254 If the ``NO_COLOR`` environment variable is defined and not empty
255 (regardless of value), color diagnostics are disabled. If ``NO_COLOR`` is
256 defined and ``-fcolor-diagnostics`` is passed on the command line, Clang
257 will honor the command line argument.
259 .. option:: -fansi-escape-codes
261 Controls whether ANSI escape codes are used instead of the Windows Console
262 API to output colored diagnostics. This option is only used on Windows and
265 .. option:: -fdiagnostics-format=clang/msvc/vi
267 Changes diagnostic output format to better match IDEs and command line tools.
269 This option controls the output format of the filename, line number,
270 and column printed in diagnostic messages. The options, and their
271 affect on formatting a simple conversion diagnostic, follow:
276 t.c:3:11: warning: conversion specifies type 'char *' but the argument has type 'int'
281 t.c(3,11) : warning: conversion specifies type 'char *' but the argument has type 'int'
286 t.c +3:11: warning: conversion specifies type 'char *' but the argument has type 'int'
288 .. _opt_fdiagnostics-show-option:
290 .. option:: -f[no-]diagnostics-show-option
292 Enable ``[-Woption]`` information in diagnostic line.
294 This option, which defaults to on, controls whether or not Clang
295 prints the associated :ref:`warning group <cl_diag_warning_groups>`
296 option name when outputting a warning diagnostic. For example, in
301 test.c:28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens]
306 Passing **-fno-diagnostics-show-option** will prevent Clang from
307 printing the [:option:`-Wextra-tokens`] information in
308 the diagnostic. This information tells you the flag needed to enable
309 or disable the diagnostic, either from the command line or through
310 :ref:`#pragma GCC diagnostic <pragma_GCC_diagnostic>`.
312 .. option:: -fdiagnostics-show-category=none/id/name
314 Enable printing category information in diagnostic line.
316 This option, which defaults to "none", controls whether or not Clang
317 prints the category associated with a diagnostic when emitting it.
318 Each diagnostic may or many not have an associated category, if it
319 has one, it is listed in the diagnostic categorization field of the
320 diagnostic line (in the []'s).
322 For example, a format string warning will produce these three
323 renditions based on the setting of this option:
327 t.c:3:11: warning: conversion specifies type 'char *' but the argument has type 'int' [-Wformat]
328 t.c:3:11: warning: conversion specifies type 'char *' but the argument has type 'int' [-Wformat,1]
329 t.c:3:11: warning: conversion specifies type 'char *' but the argument has type 'int' [-Wformat,Format String]
331 This category can be used by clients that want to group diagnostics
332 by category, so it should be a high level category. We want dozens
333 of these, not hundreds or thousands of them.
335 .. _opt_fsave-optimization-record:
337 .. option:: -f[no-]save-optimization-record[=<format>]
339 Enable optimization remarks during compilation and write them to a separate
342 This option, which defaults to off, controls whether Clang writes
343 optimization reports to a separate file. By recording diagnostics in a file,
344 users can parse or sort the remarks in a convenient way.
346 By default, the serialization format is YAML.
348 The supported serialization formats are:
350 - .. _opt_fsave_optimization_record_yaml:
352 ``-fsave-optimization-record=yaml``: A structured YAML format.
354 - .. _opt_fsave_optimization_record_bitstream:
356 ``-fsave-optimization-record=bitstream``: A binary format based on LLVM
359 The output file is controlled by :option:`-foptimization-record-file`.
361 In the absence of an explicit output file, the file is chosen using the
364 ``<base>.opt.<format>``
366 where ``<base>`` is based on the output file of the compilation (whether
367 it's explicitly specified through `-o` or not) when used with `-c` or `-S`.
370 * ``clang -fsave-optimization-record -c in.c -o out.o`` will generate
373 * ``clang -fsave-optimization-record -c in.c`` will generate
376 When targeting (Thin)LTO, the base is derived from the output filename, and
377 the extension is not dropped.
379 When targeting ThinLTO, the following scheme is used:
381 ``<base>.opt.<format>.thin.<num>.<format>``
383 Darwin-only: when used for generating a linked binary from a source file
384 (through an intermediate object file), the driver will invoke `cc1` to
385 generate a temporary object file. The temporary remark file will be emitted
386 next to the object file, which will then be picked up by `dsymutil` and
387 emitted in the .dSYM bundle. This is available for all formats except YAML.
391 ``clang -fsave-optimization-record=bitstream in.c -o out`` will generate
393 * ``/var/folders/43/9y164hh52tv_2nrdxrj31nyw0000gn/T/a-9be59b.o``
395 * ``/var/folders/43/9y164hh52tv_2nrdxrj31nyw0000gn/T/a-9be59b.opt.bitstream``
399 * ``out.dSYM/Contents/Resources/Remarks/out``
401 Darwin-only: compiling for multiple architectures will use the following
404 ``<base>-<arch>.opt.<format>``
406 Note that this is incompatible with passing the
407 :option:`-foptimization-record-file` option.
409 .. option:: -foptimization-record-file
411 Control the file to which optimization reports are written. This implies
412 :ref:`-fsave-optimization-record <opt_fsave-optimization-record>`.
414 On Darwin platforms, this is incompatible with passing multiple
415 ``-arch <arch>`` options.
417 .. option:: -foptimization-record-passes
419 Only include passes which match a specified regular expression.
421 When optimization reports are being output (see
422 :ref:`-fsave-optimization-record <opt_fsave-optimization-record>`), this
423 option controls the passes that will be included in the final report.
425 If this option is not used, all the passes are included in the optimization
428 .. _opt_fdiagnostics-show-hotness:
430 .. option:: -f[no-]diagnostics-show-hotness
432 Enable profile hotness information in diagnostic line.
434 This option controls whether Clang prints the profile hotness associated
435 with diagnostics in the presence of profile-guided optimization information.
436 This is currently supported with optimization remarks (see
437 :ref:`Options to Emit Optimization Reports <rpass>`). The hotness information
438 allows users to focus on the hot optimization remarks that are likely to be
439 more relevant for run-time performance.
441 For example, in this output, the block containing the callsite of `foo` was
442 executed 3000 times according to the profile data:
446 s.c:7:10: remark: foo inlined into bar (hotness: 3000) [-Rpass-analysis=inline]
447 sum += foo(x, x - 2);
450 This option is implied when
451 :ref:`-fsave-optimization-record <opt_fsave-optimization-record>` is used.
452 Otherwise, it defaults to off.
454 .. option:: -fdiagnostics-hotness-threshold
456 Prevent optimization remarks from being output if they do not have at least
459 This option, which defaults to zero, controls the minimum hotness an
460 optimization remark would need in order to be output by Clang. This is
461 currently supported with optimization remarks (see :ref:`Options to Emit
462 Optimization Reports <rpass>`) when profile hotness information in
463 diagnostics is enabled (see
464 :ref:`-fdiagnostics-show-hotness <opt_fdiagnostics-show-hotness>`).
466 .. _opt_fdiagnostics-fixit-info:
468 .. option:: -f[no-]diagnostics-fixit-info
470 Enable "FixIt" information in the diagnostics output.
472 This option, which defaults to on, controls whether or not Clang
473 prints the information on how to fix a specific diagnostic
474 underneath it when it knows. For example, in this output:
478 test.c:28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens]
483 Passing **-fno-diagnostics-fixit-info** will prevent Clang from
484 printing the "//" line at the end of the message. This information
485 is useful for users who may not understand what is wrong, but can be
486 confusing for machine parsing.
488 .. _opt_fdiagnostics-print-source-range-info:
490 .. option:: -fdiagnostics-print-source-range-info
492 Print machine parsable information about source ranges.
493 This option makes Clang print information about source ranges in a machine
494 parsable format after the file/line/column number information. The
495 information is a simple sequence of brace enclosed ranges, where each range
496 lists the start and end line/column locations. For example, in this output:
500 exprs.c:47:15:{47:8-47:14}{47:17-47:24}: error: invalid operands to binary expression ('int *' and '_Complex float')
501 P = (P-42) + Gamma*4;
504 The {}'s are generated by -fdiagnostics-print-source-range-info.
506 The printed column numbers count bytes from the beginning of the
507 line; take care if your source contains multibyte characters.
509 .. option:: -fdiagnostics-parseable-fixits
511 Print Fix-Its in a machine parseable form.
513 This option makes Clang print available Fix-Its in a machine
514 parseable format at the end of diagnostics. The following example
515 illustrates the format:
519 fix-it:"t.cpp":{7:25-7:29}:"Gamma"
521 The range printed is a half-open range, so in this example the
522 characters at column 25 up to but not including column 29 on line 7
523 in t.cpp should be replaced with the string "Gamma". Either the
524 range or the replacement string may be empty (representing strict
525 insertions and strict erasures, respectively). Both the file name
526 and the insertion string escape backslash (as "\\\\"), tabs (as
527 "\\t"), newlines (as "\\n"), double quotes(as "\\"") and
528 non-printable characters (as octal "\\xxx").
530 The printed column numbers count bytes from the beginning of the
531 line; take care if your source contains multibyte characters.
533 .. option:: -fno-elide-type
535 Turns off elision in template type printing.
537 The default for template type printing is to elide as many template
538 arguments as possible, removing those which are the same in both
539 template types, leaving only the differences. Adding this flag will
540 print all the template arguments. If supported by the terminal,
541 highlighting will still appear on differing arguments.
547 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;
553 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;
555 .. option:: -fdiagnostics-show-template-tree
557 Template type diffing prints a text tree.
559 For diffing large templated types, this option will cause Clang to
560 display the templates as an indented text tree, one argument per
561 line, with differences marked inline. This is compatible with
568 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;
570 With :option:`-fdiagnostics-show-template-tree`:
574 t.cc:4:5: note: candidate function not viable: no known conversion for 1st argument;
583 .. option:: -fcaret-diagnostics-max-lines:
585 Controls how many lines of code clang prints for diagnostics. By default,
586 clang prints a maximum of 16 lines of code.
589 .. option:: -fdiagnostics-show-line-numbers:
591 Controls whether clang will print a margin containing the line number on
592 the left of each line of code it prints for diagnostics.
598 test.cpp:5:1: error: 'main' must return 'int'
604 With -fno-diagnostics-show-line-numbers:
608 test.cpp:5:1: error: 'main' must return 'int'
615 .. _cl_diag_warning_groups:
617 Individual Warning Groups
618 ^^^^^^^^^^^^^^^^^^^^^^^^^
620 TODO: Generate this from tblgen. Define one anchor per warning group.
622 .. option:: -Wextra-tokens
624 Warn about excess tokens at the end of a preprocessor directive.
626 This option, which defaults to on, enables warnings about extra
627 tokens at the end of preprocessor directives. For example:
631 test.c:28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens]
635 These extra tokens are not strictly conforming, and are usually best
636 handled by commenting them out.
638 .. option:: -Wambiguous-member-template
640 Warn about unqualified uses of a member template whose name resolves to
641 another template at the location of the use.
643 This option, which defaults to on, enables a warning in the
648 template<typename T> struct set{};
649 template<typename T> struct trait { typedef const T& type; };
651 template<typename T> void set(typename trait<T>::type value) {}
658 C++ [basic.lookup.classref] requires this to be an error, but,
659 because it's hard to work around, Clang downgrades it to a warning
662 .. option:: -Wbind-to-temporary-copy
664 Warn about an unusable copy constructor when binding a reference to a
667 This option enables warnings about binding a
668 reference to a temporary when the temporary doesn't have a usable
669 copy constructor. For example:
676 NonCopyable(const NonCopyable&);
678 void foo(const NonCopyable&);
680 foo(NonCopyable()); // Disallowed in C++98; allowed in C++11.
685 struct NonCopyable2 {
687 NonCopyable2(NonCopyable2&);
689 void foo(const NonCopyable2&);
691 foo(NonCopyable2()); // Disallowed in C++98; allowed in C++11.
694 Note that if ``NonCopyable2::NonCopyable2()`` has a default argument
695 whose instantiation produces a compile error, that error will still
696 be a hard error in C++98 mode even if this warning is turned off.
698 Options to Control Clang Crash Diagnostics
699 ------------------------------------------
701 As unbelievable as it may sound, Clang does crash from time to time.
702 Generally, this only occurs to those living on the `bleeding
703 edge <https://llvm.org/releases/download.html#svn>`_. Clang goes to great
704 lengths to assist you in filing a bug report. Specifically, Clang
705 generates preprocessed source file(s) and associated run script(s) upon
706 a crash. These files should be attached to a bug report to ease
707 reproducibility of the failure. Below are the command line options to
708 control the crash diagnostics.
710 .. option:: -fcrash-diagnostics=<val>
714 * ``off`` (Disable auto-generation of preprocessed source files during a clang crash.)
715 * ``compiler`` (Generate diagnostics for compiler crashes (default))
716 * ``all`` (Generate diagnostics for all tools which support it)
718 .. option:: -fno-crash-diagnostics
720 Disable auto-generation of preprocessed source files during a clang crash.
722 The -fno-crash-diagnostics flag can be helpful for speeding the process
723 of generating a delta reduced test case.
725 .. option:: -fcrash-diagnostics-dir=<dir>
727 Specify where to write the crash diagnostics files; defaults to the
728 usual location for temporary files.
730 .. envvar:: CLANG_CRASH_DIAGNOSTICS_DIR=<dir>
732 Like ``-fcrash-diagnostics-dir=<dir>``, specifies where to write the
733 crash diagnostics files, but with lower precedence than the option.
735 Clang is also capable of generating preprocessed source file(s) and associated
736 run script(s) even without a crash. This is specially useful when trying to
737 generate a reproducer for warnings or errors while using modules.
739 .. option:: -gen-reproducer
741 Generates preprocessed source files, a reproducer script and if relevant, a
742 cache containing: built module pcm's and all headers needed to rebuild the
747 Options to Emit Optimization Reports
748 ------------------------------------
750 Optimization reports trace, at a high-level, all the major decisions
751 done by compiler transformations. For instance, when the inliner
752 decides to inline function ``foo()`` into ``bar()``, or the loop unroller
753 decides to unroll a loop N times, or the vectorizer decides to
754 vectorize a loop body.
756 Clang offers a family of flags which the optimizers can use to emit
757 a diagnostic in three cases:
759 1. When the pass makes a transformation (`-Rpass`).
761 2. When the pass fails to make a transformation (`-Rpass-missed`).
763 3. When the pass determines whether or not to make a transformation
766 NOTE: Although the discussion below focuses on `-Rpass`, the exact
767 same options apply to `-Rpass-missed` and `-Rpass-analysis`.
769 Since there are dozens of passes inside the compiler, each of these flags
770 take a regular expression that identifies the name of the pass which should
771 emit the associated diagnostic. For example, to get a report from the inliner,
772 compile the code with:
774 .. code-block:: console
776 $ clang -O2 -Rpass=inline code.cc -o code
777 code.cc:4:25: remark: foo inlined into bar [-Rpass=inline]
778 int bar(int j) { return foo(j, j - 2); }
781 Note that remarks from the inliner are identified with `[-Rpass=inline]`.
782 To request a report from every optimization pass, you should use
783 `-Rpass=.*` (in fact, you can use any valid POSIX regular
784 expression). However, do not expect a report from every transformation
785 made by the compiler. Optimization remarks do not really make sense
786 outside of the major transformations (e.g., inlining, vectorization,
787 loop optimizations) and not every optimization pass supports this
790 Note that when using profile-guided optimization information, profile hotness
791 information can be included in the remarks (see
792 :ref:`-fdiagnostics-show-hotness <opt_fdiagnostics-show-hotness>`).
797 1. Optimization remarks that refer to function names will display the
798 mangled name of the function. Since these remarks are emitted by the
799 back end of the compiler, it does not know anything about the input
800 language, nor its mangling rules.
802 2. Some source locations are not displayed correctly. The front end has
803 a more detailed source location tracking than the locations included
804 in the debug info (e.g., the front end can locate code inside macro
805 expansions). However, the locations used by `-Rpass` are
806 translated from debug annotations. That translation can be lossy,
807 which results in some remarks having no location information.
809 Options to Emit Resource Consumption Reports
810 --------------------------------------------
812 These are options that report execution time and consumed memory of different
815 .. option:: -fproc-stat-report=
817 This option requests driver to print used memory and execution time of each
818 compilation step. The ``clang`` driver during execution calls different tools,
819 like compiler, assembler, linker etc. With this option the driver reports
820 total execution time, the execution time spent in user mode and peak memory
821 usage of each the called tool. Value of the option specifies where the report
822 is sent to. If it specifies a regular file, the data are saved to this file in
825 .. code-block:: console
827 $ clang -fproc-stat-report=abc foo.c
829 clang-11,"/tmp/foo-123456.o",92000,84000,87536
830 ld,"a.out",900,8000,53568
832 The data on each row represent:
834 * file name of the tool executable,
835 * output file name in quotes,
836 * total execution time in microseconds,
837 * execution time in user mode in microseconds,
838 * peak memory usage in Kb.
840 It is possible to specify this option without any value. In this case statistics
841 are printed on standard output in human readable format:
843 .. code-block:: console
845 $ clang -fproc-stat-report foo.c
846 clang-11: output=/tmp/foo-855a8e.o, total=68.000 ms, user=60.000 ms, mem=86920 Kb
847 ld: output=a.out, total=8.000 ms, user=4.000 ms, mem=52320 Kb
849 The report file specified in the option is locked for write, so this option
850 can be used to collect statistics in parallel builds. The report file is not
851 cleared, new data is appended to it, thus making possible to accumulate build
854 You can also use environment variables to control the process statistics reporting.
855 Setting ``CC_PRINT_PROC_STAT`` to ``1`` enables the feature, the report goes to
856 stdout in human readable format.
857 Setting ``CC_PRINT_PROC_STAT_FILE`` to a fully qualified file path makes it report
858 process statistics to the given file in the CSV format. Specifying a relative
859 path will likely lead to multiple files with the same name created in different
860 directories, since the path is relative to a changing working directory.
862 These environment variables are handy when you need to request the statistics
863 report without changing your build scripts or alter the existing set of compiler
864 options. Note that ``-fproc-stat-report`` take precedence over ``CC_PRINT_PROC_STAT``
865 and ``CC_PRINT_PROC_STAT_FILE``.
867 .. code-block:: console
869 $ export CC_PRINT_PROC_STAT=1
870 $ export CC_PRINT_PROC_STAT_FILE=~/project-build-proc-stat.csv
875 Clang options that don't fit neatly into other categories.
877 .. option:: -fgnuc-version=
879 This flag controls the value of ``__GNUC__`` and related macros. This flag
880 does not enable or disable any GCC extensions implemented in Clang. Setting
881 the version to zero causes Clang to leave ``__GNUC__`` and other
882 GNU-namespaced macros, such as ``__GXX_WEAK__``, undefined.
886 When emitting a dependency file, use formatting conventions appropriate
887 for NMake or Jom. Ignored unless another option causes Clang to emit a
890 When Clang emits a dependency file (e.g., you supplied the -M option)
891 most filenames can be written to the file without any special formatting.
892 Different Make tools will treat different sets of characters as "special"
893 and use different conventions for telling the Make tool that the character
894 is actually part of the filename. Normally Clang uses backslash to "escape"
895 a special character, which is the convention used by GNU Make. The -MV
896 option tells Clang to put double-quotes around the entire filename, which
897 is the convention used by NMake and Jom.
899 .. option:: -femit-dwarf-unwind=<value>
901 When to emit DWARF unwind (EH frame) info. This is a Mach-O-specific option.
905 * ``no-compact-unwind`` - Only emit DWARF unwind when compact unwind encodings
906 aren't available. This is the default for arm64.
907 * ``always`` - Always emit DWARF unwind regardless.
908 * ``default`` - Use the platform-specific default (``always`` for all
909 non-arm64-platforms).
911 ``no-compact-unwind`` is a performance optimization -- Clang will emit smaller
912 object files that are more quickly processed by the linker. This may cause
913 binary compatibility issues on older x86_64 targets, however, so use it with
916 .. _configuration-files:
921 Configuration files group command-line options and allow all of them to be
922 specified just by referencing the configuration file. They may be used, for
923 example, to collect options required to tune compilation for particular
924 target, such as ``-L``, ``-I``, ``-l``, ``--sysroot``, codegen options, etc.
926 Configuration files can be either specified on the command line or loaded
927 from default locations. If both variants are present, the default configuration
928 files are loaded first.
930 The command line option ``--config=`` can be used to specify explicit
931 configuration files in a Clang invocation. If the option is used multiple times,
932 all specified files are loaded, in order. For example:
936 clang --config=/home/user/cfgs/testing.txt
937 clang --config=debug.cfg --config=runtimes.cfg
939 If the provided argument contains a directory separator, it is considered as
940 a file path, and options are read from that file. Otherwise the argument is
941 treated as a file name and is searched for sequentially in the directories:
945 - the directory where Clang executable resides.
947 Both user and system directories for configuration files are specified during
948 clang build using CMake parameters, ``CLANG_CONFIG_FILE_USER_DIR`` and
949 ``CLANG_CONFIG_FILE_SYSTEM_DIR`` respectively. The first file found is used.
950 It is an error if the required file cannot be found.
952 The default configuration files are searched for in the same directories
953 following the rules described in the next paragraphs. Loading default
954 configuration files can be disabled entirely via passing
955 the ``--no-default-config`` flag.
957 First, the algorithm searches for a configuration file named
958 ``<triple>-<driver>.cfg`` where `triple` is the triple for the target being
959 built for, and `driver` is the name of the currently used driver. The algorithm
960 first attempts to use the canonical name for the driver used, then falls back
961 to the one found in the executable name.
963 The following canonical driver names are used:
965 - ``clang`` for the ``gcc`` driver (used to compile C programs)
966 - ``clang++`` for the ``gxx`` driver (used to compile C++ programs)
967 - ``clang-cpp`` for the ``cpp`` driver (pure preprocessor)
968 - ``clang-cl`` for the ``cl`` driver
969 - ``flang`` for the ``flang`` driver
970 - ``clang-dxc`` for the ``dxc`` driver
972 For example, when calling ``x86_64-pc-linux-gnu-clang-g++``,
973 the driver will first attempt to use the configuration file named::
975 x86_64-pc-linux-gnu-clang++.cfg
977 If this file is not found, it will attempt to use the name found
978 in the executable instead::
980 x86_64-pc-linux-gnu-clang-g++.cfg
982 Note that options such as ``--driver-mode=``, ``--target=``, ``-m32`` affect
983 the search algorithm. For example, the aforementioned executable called with
984 ``-m32`` argument will instead search for::
986 i386-pc-linux-gnu-clang++.cfg
988 If none of the aforementioned files are found, the driver will instead search
989 for separate driver and target configuration files and attempt to load both.
990 The former is named ``<driver>.cfg`` while the latter is named
991 ``<triple>.cfg``. Similarly to the previous variants, the canonical driver name
992 will be preferred, and the compiler will fall back to the actual name.
994 For example, ``x86_64-pc-linux-gnu-clang-g++`` will attempt to load two
995 configuration files named respectively::
998 x86_64-pc-linux-gnu.cfg
1000 with fallback to trying::
1003 x86_64-pc-linux-gnu.cfg
1005 It is not an error if either of these files is not found.
1007 The configuration file consists of command-line options specified on one or
1008 more lines. Lines composed of whitespace characters only are ignored as well as
1009 lines in which the first non-blank character is ``#``. Long options may be split
1010 between several lines by a trailing backslash. Here is example of a
1015 # Several options on line
1016 -c --target=x86_64-unknown-linux-gnu
1018 # Long option split between lines
1019 -I/usr/lib/gcc/x86_64-linux-gnu/5.4.0/../../../../\
1022 # other config files may be included
1025 Files included by ``@file`` directives in configuration files are resolved
1026 relative to the including file. For example, if a configuration file
1027 ``~/.llvm/target.cfg`` contains the directive ``@os/linux.opts``, the file
1028 ``linux.opts`` is searched for in the directory ``~/.llvm/os``. Another way to
1029 include a file content is using the command line option ``--config=``. It works
1030 similarly but the included file is searched for using the rules for configuration
1033 To generate paths relative to the configuration file, the ``<CFGDIR>`` token may
1034 be used. This will expand to the absolute path of the directory containing the
1037 In cases where a configuration file is deployed alongside SDK contents, the
1038 SDK directory can remain fully portable by using ``<CFGDIR>`` prefixed paths.
1039 In this way, the user may only need to specify a root configuration file with
1040 ``--config=`` to establish every aspect of the SDK with the compiler:
1045 -isystem <CFGDIR>/include
1047 -T <CFGDIR>/ldscripts/link.ld
1049 Language and Target-Independent Features
1050 ========================================
1052 Controlling Errors and Warnings
1053 -------------------------------
1055 Clang provides a number of ways to control which code constructs cause
1056 it to emit errors and warning messages, and how they are displayed to
1059 Controlling How Clang Displays Diagnostics
1060 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1062 When Clang emits a diagnostic, it includes rich information in the
1063 output, and gives you fine-grain control over which information is
1064 printed. Clang has the ability to print this information, and these are
1065 the options that control it:
1067 #. A file/line/column indicator that shows exactly where the diagnostic
1068 occurs in your code [:ref:`-fshow-column <opt_fshow-column>`,
1069 :ref:`-fshow-source-location <opt_fshow-source-location>`].
1070 #. A categorization of the diagnostic as a note, warning, error, or
1072 #. A text string that describes what the problem is.
1073 #. An option that indicates how to control the diagnostic (for
1074 diagnostics that support it)
1075 [:ref:`-fdiagnostics-show-option <opt_fdiagnostics-show-option>`].
1076 #. A :ref:`high-level category <diagnostics_categories>` for the diagnostic
1077 for clients that want to group diagnostics by class (for diagnostics
1079 [:option:`-fdiagnostics-show-category`].
1080 #. The line of source code that the issue occurs on, along with a caret
1081 and ranges that indicate the important locations
1082 [:ref:`-fcaret-diagnostics <opt_fcaret-diagnostics>`].
1083 #. "FixIt" information, which is a concise explanation of how to fix the
1084 problem (when Clang is certain it knows)
1085 [:ref:`-fdiagnostics-fixit-info <opt_fdiagnostics-fixit-info>`].
1086 #. A machine-parsable representation of the ranges involved (off by
1088 [:ref:`-fdiagnostics-print-source-range-info <opt_fdiagnostics-print-source-range-info>`].
1090 For more information please see :ref:`Formatting of
1091 Diagnostics <cl_diag_formatting>`.
1096 All diagnostics are mapped into one of these 6 classes:
1105 .. _diagnostics_categories:
1107 Diagnostic Categories
1108 ^^^^^^^^^^^^^^^^^^^^^
1110 Though not shown by default, diagnostics may each be associated with a
1111 high-level category. This category is intended to make it possible to
1112 triage builds that produce a large number of errors or warnings in a
1115 Categories are not shown by default, but they can be turned on with the
1116 :option:`-fdiagnostics-show-category` option.
1117 When set to "``name``", the category is printed textually in the
1118 diagnostic output. When it is set to "``id``", a category number is
1119 printed. The mapping of category names to category id's can be obtained
1120 by running '``clang --print-diagnostic-categories``'.
1122 Controlling Diagnostics via Command Line Flags
1123 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1125 TODO: -W flags, -pedantic, etc
1127 .. _pragma_gcc_diagnostic:
1129 Controlling Diagnostics via Pragmas
1130 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1132 Clang can also control what diagnostics are enabled through the use of
1133 pragmas in the source code. This is useful for turning off specific
1134 warnings in a section of source code. Clang supports GCC's pragma for
1135 compatibility with existing source code, as well as several extensions.
1137 The pragma may control any warning that can be used from the command
1138 line. Warnings may be set to ignored, warning, error, or fatal. The
1139 following example code will tell Clang or GCC to ignore the -Wall
1144 #pragma GCC diagnostic ignored "-Wall"
1146 In addition to all of the functionality provided by GCC's pragma, Clang
1147 also allows you to push and pop the current warning state. This is
1148 particularly useful when writing a header file that will be compiled by
1149 other people, because you don't know what warning flags they build with.
1151 In the below example :option:`-Wextra-tokens` is ignored for only a single line
1152 of code, after which the diagnostics return to whatever state had previously
1158 #endif foo // warning: extra tokens at end of #endif directive
1160 #pragma clang diagnostic push
1161 #pragma clang diagnostic ignored "-Wextra-tokens"
1164 #endif foo // no warning
1166 #pragma clang diagnostic pop
1168 The push and pop pragmas will save and restore the full diagnostic state
1169 of the compiler, regardless of how it was set. That means that it is
1170 possible to use push and pop around GCC compatible diagnostics and Clang
1171 will push and pop them appropriately, while GCC will ignore the pushes
1172 and pops as unknown pragmas. It should be noted that while Clang
1173 supports the GCC pragma, Clang and GCC do not support the exact same set
1174 of warnings, so even when using GCC compatible #pragmas there is no
1175 guarantee that they will have identical behaviour on both compilers.
1177 In addition to controlling warnings and errors generated by the compiler, it is
1178 possible to generate custom warning and error messages through the following
1183 // The following will produce warning messages
1184 #pragma message "some diagnostic message"
1185 #pragma GCC warning "TODO: replace deprecated feature"
1187 // The following will produce an error message
1188 #pragma GCC error "Not supported"
1190 These pragmas operate similarly to the ``#warning`` and ``#error`` preprocessor
1191 directives, except that they may also be embedded into preprocessor macros via
1192 the C99 ``_Pragma`` operator, for example:
1197 #define DEFER(M,...) M(__VA_ARGS__)
1198 #define CUSTOM_ERROR(X) _Pragma(STR(GCC error(X " at line " DEFER(STR,__LINE__))))
1200 CUSTOM_ERROR("Feature not available");
1202 Controlling Diagnostics in System Headers
1203 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1205 Warnings are suppressed when they occur in system headers. By default,
1206 an included file is treated as a system header if it is found in an
1207 include path specified by ``-isystem``, but this can be overridden in
1210 The ``system_header`` pragma can be used to mark the current file as
1211 being a system header. No warnings will be produced from the location of
1212 the pragma onwards within the same file.
1217 #endif foo // warning: extra tokens at end of #endif directive
1219 #pragma clang system_header
1222 #endif foo // no warning
1224 The `--system-header-prefix=` and `--no-system-header-prefix=`
1225 command-line arguments can be used to override whether subsets of an include
1226 path are treated as system headers. When the name in a ``#include`` directive
1227 is found within a header search path and starts with a system prefix, the
1228 header is treated as a system header. The last prefix on the
1229 command-line which matches the specified header name takes precedence.
1232 .. code-block:: console
1234 $ clang -Ifoo -isystem bar --system-header-prefix=x/ \
1235 --no-system-header-prefix=x/y/
1237 Here, ``#include "x/a.h"`` is treated as including a system header, even
1238 if the header is found in ``foo``, and ``#include "x/y/b.h"`` is treated
1239 as not including a system header, even if the header is found in
1242 A ``#include`` directive which finds a file relative to the current
1243 directory is treated as including a system header if the including file
1244 is treated as a system header.
1246 Controlling Deprecation Diagnostics in Clang-Provided C Runtime Headers
1247 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1249 Clang is responsible for providing some of the C runtime headers that cannot be
1250 provided by a platform CRT, such as implementation limits or when compiling in
1251 freestanding mode. Define the ``_CLANG_DISABLE_CRT_DEPRECATION_WARNINGS`` macro
1252 prior to including such a C runtime header to disable the deprecation warnings.
1253 Note that the C Standard Library headers are allowed to transitively include
1254 other standard library headers (see 7.1.2p5), and so the most appropriate use
1255 of this macro is to set it within the build system using ``-D`` or before any
1256 include directives in the translation unit.
1260 #define _CLANG_DISABLE_CRT_DEPRECATION_WARNINGS
1261 #include <stdint.h> // Clang CRT deprecation warnings are disabled.
1262 #include <stdatomic.h> // Clang CRT deprecation warnings are disabled.
1264 .. _diagnostics_enable_everything:
1266 Enabling All Diagnostics
1267 ^^^^^^^^^^^^^^^^^^^^^^^^
1269 In addition to the traditional ``-W`` flags, one can enable **all** diagnostics
1270 by passing :option:`-Weverything`. This works as expected with
1271 :option:`-Werror`, and also includes the warnings from :option:`-pedantic`. Some
1272 diagnostics contradict each other, therefore, users of :option:`-Weverything`
1273 often disable many diagnostics such as `-Wno-c++98-compat` and `-Wno-c++-compat`
1274 because they contradict recent C++ standards.
1276 Since :option:`-Weverything` enables every diagnostic, we generally don't
1277 recommend using it. `-Wall` `-Wextra` are a better choice for most projects.
1278 Using :option:`-Weverything` means that updating your compiler is more difficult
1279 because you're exposed to experimental diagnostics which might be of lower
1280 quality than the default ones. If you do use :option:`-Weverything` then we
1281 advise that you address all new compiler diagnostics as they get added to Clang,
1282 either by fixing everything they find or explicitly disabling that diagnostic
1283 with its corresponding `Wno-` option.
1285 Note that when combined with :option:`-w` (which disables all warnings),
1286 disabling all warnings wins.
1288 Controlling Static Analyzer Diagnostics
1289 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1291 While not strictly part of the compiler, the diagnostics from Clang's
1292 `static analyzer <https://clang-analyzer.llvm.org>`_ can also be
1293 influenced by the user via changes to the source code. See the available
1294 `annotations <https://clang-analyzer.llvm.org/annotations.html>`_ and the
1296 page <https://clang-analyzer.llvm.org/faq.html#exclude_code>`_ for more
1299 .. _usersmanual-precompiled-headers:
1304 `Precompiled headers <https://en.wikipedia.org/wiki/Precompiled_header>`_
1305 are a general approach employed by many compilers to reduce compilation
1306 time. The underlying motivation of the approach is that it is common for
1307 the same (and often large) header files to be included by multiple
1308 source files. Consequently, compile times can often be greatly improved
1309 by caching some of the (redundant) work done by a compiler to process
1310 headers. Precompiled header files, which represent one of many ways to
1311 implement this optimization, are literally files that represent an
1312 on-disk cache that contains the vital information necessary to reduce
1313 some of the work needed to process a corresponding header file. While
1314 details of precompiled headers vary between compilers, precompiled
1315 headers have been shown to be highly effective at speeding up program
1316 compilation on systems with very large system headers (e.g., macOS).
1318 Generating a PCH File
1319 ^^^^^^^^^^^^^^^^^^^^^
1321 To generate a PCH file using Clang, one invokes Clang with the
1322 `-x <language>-header` option. This mirrors the interface in GCC
1323 for generating PCH files:
1325 .. code-block:: console
1327 $ gcc -x c-header test.h -o test.h.gch
1328 $ clang -x c-header test.h -o test.h.pch
1333 A PCH file can then be used as a prefix header when a ``-include-pch``
1334 option is passed to ``clang``:
1336 .. code-block:: console
1338 $ clang -include-pch test.h.pch test.c -o test
1340 The ``clang`` driver will check if the PCH file ``test.h.pch`` is
1341 available; if so, the contents of ``test.h`` (and the files it includes)
1342 will be processed from the PCH file. Otherwise, Clang will report an error.
1346 Clang does *not* automatically use PCH files for headers that are directly
1347 included within a source file or indirectly via :option:`-include`.
1350 .. code-block:: console
1352 $ clang -x c-header test.h -o test.h.pch
1355 $ clang test.c -o test
1357 In this example, ``clang`` will not automatically use the PCH file for
1358 ``test.h`` since ``test.h`` was included directly in the source file and not
1359 specified on the command line using ``-include-pch``.
1361 Relocatable PCH Files
1362 ^^^^^^^^^^^^^^^^^^^^^
1364 It is sometimes necessary to build a precompiled header from headers
1365 that are not yet in their final, installed locations. For example, one
1366 might build a precompiled header within the build tree that is then
1367 meant to be installed alongside the headers. Clang permits the creation
1368 of "relocatable" precompiled headers, which are built with a given path
1369 (into the build directory) and can later be used from an installed
1372 To build a relocatable precompiled header, place your headers into a
1373 subdirectory whose structure mimics the installed location. For example,
1374 if you want to build a precompiled header for the header ``mylib.h``
1375 that will be installed into ``/usr/include``, create a subdirectory
1376 ``build/usr/include`` and place the header ``mylib.h`` into that
1377 subdirectory. If ``mylib.h`` depends on other headers, then they can be
1378 stored within ``build/usr/include`` in a way that mimics the installed
1381 Building a relocatable precompiled header requires two additional
1382 arguments. First, pass the ``--relocatable-pch`` flag to indicate that
1383 the resulting PCH file should be relocatable. Second, pass
1384 ``-isysroot /path/to/build``, which makes all includes for your library
1385 relative to the build directory. For example:
1387 .. code-block:: console
1389 # clang -x c-header --relocatable-pch -isysroot /path/to/build /path/to/build/mylib.h mylib.h.pch
1391 When loading the relocatable PCH file, the various headers used in the
1392 PCH file are found from the system header root. For example, ``mylib.h``
1393 can be found in ``/usr/include/mylib.h``. If the headers are installed
1394 in some other system root, the ``-isysroot`` option can be used provide
1395 a different system root from which the headers will be based. For
1396 example, ``-isysroot /Developer/SDKs/MacOSX10.4u.sdk`` will look for
1397 ``mylib.h`` in ``/Developer/SDKs/MacOSX10.4u.sdk/usr/include/mylib.h``.
1399 Relocatable precompiled headers are intended to be used in a limited
1400 number of cases where the compilation environment is tightly controlled
1401 and the precompiled header cannot be generated after headers have been
1404 .. _controlling-fp-behavior:
1406 Controlling Floating Point Behavior
1407 -----------------------------------
1409 Clang provides a number of ways to control floating point behavior, including
1410 with command line options and source pragmas. This section
1411 describes the various floating point semantic modes and the corresponding options.
1413 .. csv-table:: Floating Point Semantic Modes
1414 :header: "Mode", "Values"
1417 "ffp-exception-behavior", "{ignore, strict, maytrap}",
1418 "fenv_access", "{off, on}", "(none)"
1419 "frounding-math", "{dynamic, tonearest, downward, upward, towardzero}"
1420 "ffp-contract", "{on, off, fast, fast-honor-pragmas}"
1421 "fdenormal-fp-math", "{IEEE, PreserveSign, PositiveZero}"
1422 "fdenormal-fp-math-fp32", "{IEEE, PreserveSign, PositiveZero}"
1423 "fmath-errno", "{on, off}"
1424 "fhonor-nans", "{on, off}"
1425 "fhonor-infinities", "{on, off}"
1426 "fsigned-zeros", "{on, off}"
1427 "freciprocal-math", "{on, off}"
1428 "allow_approximate_fns", "{on, off}"
1429 "fassociative-math", "{on, off}"
1431 This table describes the option settings that correspond to the three
1432 floating point semantic models: precise (the default), strict, and fast.
1435 .. csv-table:: Floating Point Models
1436 :header: "Mode", "Precise", "Strict", "Fast"
1437 :widths: 25, 15, 15, 15
1439 "except_behavior", "ignore", "strict", "ignore"
1440 "fenv_access", "off", "on", "off"
1441 "rounding_mode", "tonearest", "dynamic", "tonearest"
1442 "contract", "on", "off", "fast"
1443 "denormal_fp_math", "IEEE", "IEEE", "IEEE"
1444 "denormal_fp32_math", "IEEE","IEEE", "IEEE"
1445 "support_math_errno", "on", "on", "off"
1446 "no_honor_nans", "off", "off", "on"
1447 "no_honor_infinities", "off", "off", "on"
1448 "no_signed_zeros", "off", "off", "on"
1449 "allow_reciprocal", "off", "off", "on"
1450 "allow_approximate_fns", "off", "off", "on"
1451 "allow_reassociation", "off", "off", "on"
1453 .. option:: -ffast-math
1455 Enable fast-math mode. This option lets the
1456 compiler make aggressive, potentially-lossy assumptions about
1457 floating-point math. These include:
1459 * Floating-point math obeys regular algebraic rules for real numbers (e.g.
1460 ``+`` and ``*`` are associative, ``x/y == x * (1/y)``, and
1461 ``(a + b) * c == a * c + b * c``),
1462 * Operands to floating-point operations are not equal to ``NaN`` and
1464 * ``+0`` and ``-0`` are interchangeable.
1466 ``-ffast-math`` also defines the ``__FAST_MATH__`` preprocessor
1467 macro. Some math libraries recognize this macro and change their behavior.
1468 With the exception of ``-ffp-contract=fast``, using any of the options
1469 below to disable any of the individual optimizations in ``-ffast-math``
1470 will cause ``__FAST_MATH__`` to no longer be set.
1471 ``-ffast-math`` enables ``-fcx-limited-range``.
1473 This option implies:
1475 * ``-fno-honor-infinities``
1477 * ``-fno-honor-nans``
1481 * ``-fno-math-errno``
1483 * ``-ffinite-math-only``
1485 * ``-fassociative-math``
1487 * ``-freciprocal-math``
1489 * ``-fno-signed-zeros``
1491 * ``-fno-trapping-math``
1493 * ``-fno-rounding-math``
1495 * ``-ffp-contract=fast``
1497 Note: ``-ffast-math`` causes ``crtfastmath.o`` to be linked with code. See
1498 :ref:`crtfastmath.o` for more details.
1500 .. option:: -fno-fast-math
1502 Disable fast-math mode. This options disables unsafe floating-point
1503 optimizations by preventing the compiler from making any transformations that
1504 could affect the results.
1506 This option implies:
1508 * ``-fhonor-infinities``
1512 * ``-fno-approx-func``
1514 * ``-fno-finite-math-only``
1516 * ``-fno-associative-math``
1518 * ``-fno-reciprocal-math``
1520 * ``-fsigned-zeros``
1522 * ``-ffp-contract=on``
1524 Also, this option resets following options to their target-dependent defaults.
1526 * ``-f[no-]math-errno``
1527 * ``-fdenormal-fp-math=<value>``
1529 There is ambiguity about how ``-ffp-contract``, ``-ffast-math``,
1530 and ``-fno-fast-math`` behave when combined. To keep the value of
1531 ``-ffp-contract`` consistent, we define this set of rules:
1533 * ``-ffast-math`` sets ``ffp-contract`` to ``fast``.
1535 * ``-fno-fast-math`` sets ``-ffp-contract`` to ``on`` (``fast`` for CUDA and
1538 * If ``-ffast-math`` and ``-ffp-contract`` are both seen, but
1539 ``-ffast-math`` is not followed by ``-fno-fast-math``, ``ffp-contract``
1540 will be given the value of whichever option was last seen.
1542 * If ``-fno-fast-math`` is seen and ``-ffp-contract`` has been seen at least
1543 once, the ``ffp-contract`` will get the value of the last seen value of
1546 * If ``-fno-fast-math`` is seen and ``-ffp-contract`` has not been seen, the
1547 ``-ffp-contract`` setting is determined by the default value of
1550 Note: ``-fno-fast-math`` implies ``-fdenormal-fp-math=ieee``.
1551 ``-fno-fast-math`` causes ``crtfastmath.o`` to not be linked with code.
1553 .. option:: -fdenormal-fp-math=<value>
1555 Select which denormal numbers the code is permitted to require.
1559 * ``ieee`` - IEEE 754 denormal numbers
1560 * ``preserve-sign`` - the sign of a flushed-to-zero number is preserved in the sign of 0
1561 * ``positive-zero`` - denormals are flushed to positive zero
1563 The default value depends on the target. For most targets, defaults to
1566 .. option:: -f[no-]strict-float-cast-overflow
1568 When a floating-point value is not representable in a destination integer
1569 type, the code has undefined behavior according to the language standard.
1570 By default, Clang will not guarantee any particular result in that case.
1571 With the 'no-strict' option, Clang will saturate towards the smallest and
1572 largest representable integer values instead. NaNs will be converted to zero.
1573 Defaults to ``-fstrict-float-cast-overflow``.
1575 .. option:: -f[no-]math-errno
1577 Require math functions to indicate errors by setting errno.
1578 The default varies by ToolChain. ``-fno-math-errno`` allows optimizations
1579 that might cause standard C math functions to not set ``errno``.
1580 For example, on some systems, the math function ``sqrt`` is specified
1581 as setting ``errno`` to ``EDOM`` when the input is negative. On these
1582 systems, the compiler cannot normally optimize a call to ``sqrt`` to use
1583 inline code (e.g. the x86 ``sqrtsd`` instruction) without additional
1584 checking to ensure that ``errno`` is set appropriately.
1585 ``-fno-math-errno`` permits these transformations.
1587 On some targets, math library functions never set ``errno``, and so
1588 ``-fno-math-errno`` is the default. This includes most BSD-derived
1589 systems, including Darwin.
1591 .. option:: -f[no-]trapping-math
1593 Control floating point exception behavior. ``-fno-trapping-math`` allows optimizations that assume that floating point operations cannot generate traps such as divide-by-zero, overflow and underflow.
1595 - The option ``-ftrapping-math`` behaves identically to ``-ffp-exception-behavior=strict``.
1596 - The option ``-fno-trapping-math`` behaves identically to ``-ffp-exception-behavior=ignore``. This is the default.
1598 .. option:: -ffp-contract=<value>
1600 Specify when the compiler is permitted to form fused floating-point
1601 operations, such as fused multiply-add (FMA). Fused operations are
1602 permitted to produce more precise results than performing the same
1603 operations separately.
1605 The C standard permits intermediate floating-point results within an
1606 expression to be computed with more precision than their type would
1607 normally allow. This permits operation fusing, and Clang takes advantage
1608 of this by default. This behavior can be controlled with the ``FP_CONTRACT``
1609 and ``clang fp contract`` pragmas. Please refer to the pragma documentation
1610 for a description of how the pragmas interact with this option.
1614 * ``fast`` (fuse across statements disregarding pragmas, default for CUDA)
1615 * ``on`` (fuse in the same statement unless dictated by pragmas, default for languages other than CUDA/HIP)
1616 * ``off`` (never fuse)
1617 * ``fast-honor-pragmas`` (fuse across statements unless dictated by pragmas, default for HIP)
1619 .. option:: -f[no-]honor-infinities
1621 Allow floating-point optimizations that assume arguments and results are
1623 Defaults to ``-fhonor-infinities``.
1625 If both ``-fno-honor-infinities`` and ``-fno-honor-nans`` are used,
1626 has the same effect as specifying ``-ffinite-math-only``.
1628 .. option:: -f[no-]honor-nans
1630 Allow floating-point optimizations that assume arguments and results are
1632 Defaults to ``-fhonor-nans``.
1634 If both ``-fno-honor-infinities`` and ``-fno-honor-nans`` are used,
1635 has the same effect as specifying ``-ffinite-math-only``.
1637 .. option:: -f[no-]approx-func
1639 Allow certain math function calls (such as ``log``, ``sqrt``, ``pow``, etc)
1640 to be replaced with an approximately equivalent set of instructions
1641 or alternative math function calls. For example, a ``pow(x, 0.25)``
1642 may be replaced with ``sqrt(sqrt(x))``, despite being an inexact result
1643 in cases where ``x`` is ``-0.0`` or ``-inf``.
1644 Defaults to ``-fno-approx-func``.
1646 .. option:: -f[no-]signed-zeros
1648 Allow optimizations that ignore the sign of floating point zeros.
1649 Defaults to ``-fsigned-zeros``.
1651 .. option:: -f[no-]associative-math
1653 Allow floating point operations to be reassociated.
1654 Defaults to ``-fno-associative-math``.
1656 .. option:: -f[no-]reciprocal-math
1658 Allow division operations to be transformed into multiplication by a
1659 reciprocal. This can be significantly faster than an ordinary division
1660 but can also have significantly less precision. Defaults to
1661 ``-fno-reciprocal-math``.
1663 .. option:: -f[no-]unsafe-math-optimizations
1665 Allow unsafe floating-point optimizations.
1666 ``-funsafe-math-optimizations`` also implies:
1669 * ``-fassociative-math``
1670 * ``-freciprocal-math``
1671 * ``-fno-signed-zeros``
1672 * ``-fno-trapping-math``
1673 * ``-ffp-contract=fast``
1675 ``-fno-unsafe-math-optimizations`` implies:
1677 * ``-fno-approx-func``
1678 * ``-fno-associative-math``
1679 * ``-fno-reciprocal-math``
1680 * ``-fsigned-zeros``
1681 * ``-ftrapping-math``
1682 * ``-ffp-contract=on``
1683 * ``-fdenormal-fp-math=ieee``
1685 There is ambiguity about how ``-ffp-contract``,
1686 ``-funsafe-math-optimizations``, and ``-fno-unsafe-math-optimizations``
1687 behave when combined. Explanation in :option:`-fno-fast-math` also applies
1690 Defaults to ``-fno-unsafe-math-optimizations``.
1692 .. option:: -f[no-]finite-math-only
1694 Allow floating-point optimizations that assume arguments and results are
1695 not NaNs or +-Inf. ``-ffinite-math-only`` defines the
1696 ``__FINITE_MATH_ONLY__`` preprocessor macro.
1697 ``-ffinite-math-only`` implies:
1699 * ``-fno-honor-infinities``
1700 * ``-fno-honor-nans``
1702 ``-ffno-inite-math-only`` implies:
1704 * ``-fhonor-infinities``
1707 Defaults to ``-fno-finite-math-only``.
1709 .. option:: -f[no-]rounding-math
1711 Force floating-point operations to honor the dynamically-set rounding mode by default.
1713 The result of a floating-point operation often cannot be exactly represented in the result type and therefore must be rounded. IEEE 754 describes different rounding modes that control how to perform this rounding, not all of which are supported by all implementations. C provides interfaces (``fesetround`` and ``fesetenv``) for dynamically controlling the rounding mode, and while it also recommends certain conventions for changing the rounding mode, these conventions are not typically enforced in the ABI. Since the rounding mode changes the numerical result of operations, the compiler must understand something about it in order to optimize floating point operations.
1715 Note that floating-point operations performed as part of constant initialization are formally performed prior to the start of the program and are therefore not subject to the current rounding mode. This includes the initialization of global variables and local ``static`` variables. Floating-point operations in these contexts will be rounded using ``FE_TONEAREST``.
1717 - The option ``-fno-rounding-math`` allows the compiler to assume that the rounding mode is set to ``FE_TONEAREST``. This is the default.
1718 - The option ``-frounding-math`` forces the compiler to honor the dynamically-set rounding mode. This prevents optimizations which might affect results if the rounding mode changes or is different from the default; for example, it prevents floating-point operations from being reordered across most calls and prevents constant-folding when the result is not exactly representable.
1720 .. option:: -ffp-model=<value>
1722 Specify floating point behavior. ``-ffp-model`` is an umbrella
1723 option that encompasses functionality provided by other, single
1724 purpose, floating point options. Valid values are: ``precise``, ``strict``,
1728 * ``precise`` Disables optimizations that are not value-safe on
1729 floating-point data, although FP contraction (FMA) is enabled
1730 (``-ffp-contract=on``). This is the default behavior. This value resets
1731 ``-fmath-errno`` to its target-dependent default.
1732 * ``strict`` Enables ``-frounding-math`` and
1733 ``-ffp-exception-behavior=strict``, and disables contractions (FMA). All
1734 of the ``-ffast-math`` enablements are disabled. Enables
1735 ``STDC FENV_ACCESS``: by default ``FENV_ACCESS`` is disabled. This option
1736 setting behaves as though ``#pragma STDC FENV_ACCESS ON`` appeared at the
1737 top of the source file.
1738 * ``fast`` Behaves identically to specifying both ``-ffast-math`` and
1739 ``ffp-contract=fast``
1741 Note: If your command line specifies multiple instances
1742 of the ``-ffp-model`` option, or if your command line option specifies
1743 ``-ffp-model`` and later on the command line selects a floating point
1744 option that has the effect of negating part of the ``ffp-model`` that
1745 has been selected, then the compiler will issue a diagnostic warning
1746 that the override has occurred.
1748 .. option:: -ffp-exception-behavior=<value>
1750 Specify the floating-point exception behavior.
1752 Valid values are: ``ignore``, ``maytrap``, and ``strict``.
1753 The default value is ``ignore``. Details:
1755 * ``ignore`` The compiler assumes that the exception status flags will not be read and that floating point exceptions will be masked.
1756 * ``maytrap`` The compiler avoids transformations that may raise exceptions that would not have been raised by the original code. Constant folding performed by the compiler is exempt from this option.
1757 * ``strict`` The compiler ensures that all transformations strictly preserve the floating point exception semantics of the original code.
1759 .. option:: -ffp-eval-method=<value>
1761 Specify the floating-point evaluation method for intermediate results within
1762 a single expression of the code.
1764 Valid values are: ``source``, ``double``, and ``extended``.
1765 For 64-bit targets, the default value is ``source``. For 32-bit x86 targets
1766 however, in the case of NETBSD 6.99.26 and under, the default value is
1767 ``double``; in the case of NETBSD greater than 6.99.26, with NoSSE, the
1768 default value is ``extended``, with SSE the default value is ``source``.
1771 * ``source`` The compiler uses the floating-point type declared in the source program as the evaluation method.
1772 * ``double`` The compiler uses ``double`` as the floating-point evaluation method for all float expressions of type that is narrower than ``double``.
1773 * ``extended`` The compiler uses ``long double`` as the floating-point evaluation method for all float expressions of type that is narrower than ``long double``.
1775 .. option:: -f[no-]protect-parens
1777 This option pertains to floating-point types, complex types with
1778 floating-point components, and vectors of these types. Some arithmetic
1779 expression transformations that are mathematically correct and permissible
1780 according to the C and C++ language standards may be incorrect when dealing
1781 with floating-point types, such as reassociation and distribution. Further,
1782 the optimizer may ignore parentheses when computing arithmetic expressions
1783 in circumstances where the parenthesized and unparenthesized expression
1784 express the same mathematical value. For example (a+b)+c is the same
1785 mathematical value as a+(b+c), but the optimizer is free to evaluate the
1786 additions in any order regardless of the parentheses. When enabled, this
1787 option forces the optimizer to honor the order of operations with respect
1788 to parentheses in all circumstances.
1789 Defaults to ``-fno-protect-parens``.
1791 Note that floating-point contraction (option `-ffp-contract=`) is disabled
1792 when `-fprotect-parens` is enabled. Also note that in safe floating-point
1793 modes, such as `-ffp-model=precise` or `-ffp-model=strict`, this option
1794 has no effect because the optimizer is prohibited from making unsafe
1797 .. option:: -fexcess-precision:
1799 The C and C++ standards allow floating-point expressions to be computed as if
1800 intermediate results had more precision (and/or a wider range) than the type
1801 of the expression strictly allows. This is called excess precision
1803 Excess precision arithmetic can improve the accuracy of results (although not
1804 always), and it can make computation significantly faster if the target lacks
1805 direct hardware support for arithmetic in a particular type. However, it can
1806 also undermine strict floating-point reproducibility.
1808 Under the standards, assignments and explicit casts force the operand to be
1809 converted to its formal type, discarding any excess precision. Because data
1810 can only flow between statements via an assignment, this means that the use
1811 of excess precision arithmetic is a reliable local property of a single
1812 statement, and results do not change based on optimization. However, when
1813 excess precision arithmetic is in use, Clang does not guarantee strict
1814 reproducibility, and future compiler releases may recognize more
1815 opportunities to use excess precision arithmetic, e.g. with floating-point
1818 Clang does not use excess precision arithmetic for most types or on most
1819 targets. For example, even on pre-SSE X86 targets where ``float`` and
1820 ``double`` computations must be performed in the 80-bit X87 format, Clang
1821 rounds all intermediate results correctly for their type. Clang currently
1822 uses excess precision arithmetic by default only for the following types and
1825 * ``_Float16`` on X86 targets without ``AVX512-FP16``.
1827 The ``-fexcess-precision=<value>`` option can be used to control the use of
1828 excess precision arithmetic. Valid values are:
1830 * ``standard`` - The default. Allow the use of excess precision arithmetic
1831 under the constraints of the C and C++ standards. Has no effect except on
1832 the types and targets listed above.
1833 * ``fast`` - Accepted for GCC compatibility, but currently treated as an
1834 alias for ``standard``.
1835 * ``16`` - Forces ``_Float16`` operations to be emitted without using excess
1836 precision arithmetic.
1838 .. option:: -fcx-limited-range:
1840 This option enables the naive mathematical formulas for complex division and
1841 multiplication with no NaN checking of results. The default is
1842 ``-fno-cx-limited-range``, but this option is enabled by the ``-ffast-math``
1845 .. option:: -fcx-fortran-rules:
1847 This option enables the naive mathematical formulas for complex
1848 multiplication and enables application of Smith's algorithm for complex
1849 division. See SMITH, R. L. Algorithm 116: Complex division. Commun.
1850 ACM 5, 8 (1962). The default is ``-fno-cx-fortran-rules``.
1852 .. _floating-point-environment:
1854 Accessing the floating point environment
1855 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1856 Many targets allow floating point operations to be configured to control things
1857 such as how inexact results should be rounded and how exceptional conditions
1858 should be handled. This configuration is called the floating point environment.
1859 C and C++ restrict access to the floating point environment by default, and the
1860 compiler is allowed to assume that all operations are performed in the default
1861 environment. When code is compiled in this default mode, operations that depend
1862 on the environment (such as floating-point arithmetic and `FLT_ROUNDS`) may have
1863 undefined behavior if the dynamic environment is not the default environment; for
1864 example, `FLT_ROUNDS` may or may not simply return its default value for the target
1865 instead of reading the dynamic environment, and floating-point operations may be
1866 optimized as if the dynamic environment were the default. Similarly, it is undefined
1867 behavior to change the floating point environment in this default mode, for example
1868 by calling the `fesetround` function.
1869 C provides two pragmas to allow code to dynamically modify the floating point environment:
1871 - ``#pragma STDC FENV_ACCESS ON`` allows dynamic changes to the entire floating
1874 - ``#pragma STDC FENV_ROUND FE_DYNAMIC`` allows dynamic changes to just the floating
1875 point rounding mode. This may be more optimizable than ``FENV_ACCESS ON`` because
1876 the compiler can still ignore the possibility of floating-point exceptions by default.
1878 Both of these can be used either at the start of a block scope, in which case
1879 they cover all code in that scope (unless they're turned off in a child scope),
1880 or at the top level in a file, in which case they cover all subsequent function
1881 bodies until they're turned off. Note that it is undefined behavior to enter
1882 code that is *not* covered by one of these pragmas from code that *is* covered
1883 by one of these pragmas unless the floating point environment has been restored
1884 to its default state. See the C standard for more information about these pragmas.
1886 The command line option ``-frounding-math`` behaves as if the translation unit
1887 began with ``#pragma STDC FENV_ROUND FE_DYNAMIC``. The command line option
1888 ``-ffp-model=strict`` behaves as if the translation unit began with ``#pragma STDC FENV_ACCESS ON``.
1890 Code that just wants to use a specific rounding mode for specific floating point
1891 operations can avoid most of the hazards of the dynamic floating point environment
1892 by using ``#pragma STDC FENV_ROUND`` with a value other than ``FE_DYNAMIC``.
1896 A note about ``crtfastmath.o``
1897 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1898 ``-ffast-math`` and ``-funsafe-math-optimizations`` cause ``crtfastmath.o`` to be
1899 automatically linked, which adds a static constructor that sets the FTZ/DAZ
1900 bits in MXCSR, affecting not only the current compilation unit but all static
1901 and shared libraries included in the program.
1903 .. _FLT_EVAL_METHOD:
1905 A note about ``__FLT_EVAL_METHOD__``
1906 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1907 The ``__FLT_EVAL_METHOD__`` is not defined as a traditional macro, and so it
1908 will not appear when dumping preprocessor macros. Instead, the value
1909 ``__FLT_EVAL_METHOD__`` expands to is determined at the point of expansion
1910 either from the value set by the ``-ffp-eval-method`` command line option or
1911 from the target. This is because the ``__FLT_EVAL_METHOD__`` macro
1912 cannot expand to the correct evaluation method in the presence of a ``#pragma``
1913 which alters the evaluation method. An error is issued if
1914 ``__FLT_EVAL_METHOD__`` is expanded inside a scope modified by
1915 ``#pragma clang fp eval_method``.
1917 .. _fp-constant-eval:
1919 A note about Floating Point Constant Evaluation
1920 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1922 In C, the only place floating point operations are guaranteed to be evaluated
1923 during translation is in the initializers of variables of static storage
1924 duration, which are all notionally initialized before the program begins
1925 executing (and thus before a non-default floating point environment can be
1926 entered). But C++ has many more contexts where floating point constant
1927 evaluation occurs. Specifically: for static/thread-local variables,
1928 first try evaluating the initializer in a constant context, including in the
1929 constant floating point environment (just like in C), and then, if that fails,
1930 fall back to emitting runtime code to perform the initialization (which might
1931 in general be in a different floating point environment).
1933 Consider this example when compiled with ``-frounding-math``
1935 .. code-block:: console
1937 constexpr float func_01(float x, float y) {
1940 float V1 = func_01(1.0F, 0x0.000001p0F);
1942 The C++ rule is that initializers for static storage duration variables are
1943 first evaluated during translation (therefore, in the default rounding mode),
1944 and only evaluated at runtime (and therefore in the runtime rounding mode) if
1945 the compile-time evaluation fails. This is in line with the C rules;
1946 C11 F.8.5 says: *All computation for automatic initialization is done (as if)
1947 at execution time; thus, it is affected by any operative modes and raises
1948 floating-point exceptions as required by IEC 60559 (provided the state for the
1949 FENV_ACCESS pragma is ‘‘on’’). All computation for initialization of objects
1950 that have static or thread storage duration is done (as if) at translation
1951 time.* C++ generalizes this by adding another phase of initialization
1952 (at runtime) if the translation-time initialization fails, but the
1953 translation-time evaluation of the initializer of succeeds, it will be
1954 treated as a constant initializer.
1957 .. _controlling-code-generation:
1959 Controlling Code Generation
1960 ---------------------------
1962 Clang provides a number of ways to control code generation. The options
1965 .. option:: -f[no-]sanitize=check1,check2,...
1967 Turn on runtime checks for various forms of undefined or suspicious
1970 This option controls whether Clang adds runtime checks for various
1971 forms of undefined or suspicious behavior, and is disabled by
1972 default. If a check fails, a diagnostic message is produced at
1973 runtime explaining the problem. The main checks are:
1975 - .. _opt_fsanitize_address:
1977 ``-fsanitize=address``:
1978 :doc:`AddressSanitizer`, a memory error
1980 - .. _opt_fsanitize_thread:
1982 ``-fsanitize=thread``: :doc:`ThreadSanitizer`, a data race detector.
1983 - .. _opt_fsanitize_memory:
1985 ``-fsanitize=memory``: :doc:`MemorySanitizer`,
1986 a detector of uninitialized reads. Requires instrumentation of all
1988 - .. _opt_fsanitize_undefined:
1990 ``-fsanitize=undefined``: :doc:`UndefinedBehaviorSanitizer`,
1991 a fast and compatible undefined behavior checker.
1993 - ``-fsanitize=dataflow``: :doc:`DataFlowSanitizer`, a general data
1995 - ``-fsanitize=cfi``: :doc:`control flow integrity <ControlFlowIntegrity>`
1996 checks. Requires ``-flto``.
1997 - ``-fsanitize=kcfi``: kernel indirect call forward-edge control flow
1999 - ``-fsanitize=safe-stack``: :doc:`safe stack <SafeStack>`
2000 protection against stack-based memory corruption errors.
2002 There are more fine-grained checks available: see
2003 the :ref:`list <ubsan-checks>` of specific kinds of
2004 undefined behavior that can be detected and the :ref:`list <cfi-schemes>`
2005 of control flow integrity schemes.
2007 The ``-fsanitize=`` argument must also be provided when linking, in
2008 order to link to the appropriate runtime library.
2010 It is not possible to combine more than one of the ``-fsanitize=address``,
2011 ``-fsanitize=thread``, and ``-fsanitize=memory`` checkers in the same
2014 .. option:: -f[no-]sanitize-recover=check1,check2,...
2016 .. option:: -f[no-]sanitize-recover[=all]
2018 Controls which checks enabled by ``-fsanitize=`` flag are non-fatal.
2019 If the check is fatal, program will halt after the first error
2020 of this kind is detected and error report is printed.
2022 By default, non-fatal checks are those enabled by
2023 :doc:`UndefinedBehaviorSanitizer`,
2024 except for ``-fsanitize=return`` and ``-fsanitize=unreachable``. Some
2025 sanitizers may not support recovery (or not support it by default
2026 e.g. :doc:`AddressSanitizer`), and always crash the program after the issue
2029 Note that the ``-fsanitize-trap`` flag has precedence over this flag.
2030 This means that if a check has been configured to trap elsewhere on the
2031 command line, or if the check traps by default, this flag will not have
2032 any effect unless that sanitizer's trapping behavior is disabled with
2033 ``-fno-sanitize-trap``.
2035 For example, if a command line contains the flags ``-fsanitize=undefined
2036 -fsanitize-trap=undefined``, the flag ``-fsanitize-recover=alignment``
2037 will have no effect on its own; it will need to be accompanied by
2038 ``-fno-sanitize-trap=alignment``.
2040 .. option:: -f[no-]sanitize-trap=check1,check2,...
2042 .. option:: -f[no-]sanitize-trap[=all]
2044 Controls which checks enabled by the ``-fsanitize=`` flag trap. This
2045 option is intended for use in cases where the sanitizer runtime cannot
2046 be used (for instance, when building libc or a kernel module), or where
2047 the binary size increase caused by the sanitizer runtime is a concern.
2049 This flag is only compatible with :doc:`control flow integrity
2050 <ControlFlowIntegrity>` schemes and :doc:`UndefinedBehaviorSanitizer`
2051 checks other than ``vptr``.
2053 This flag is enabled by default for sanitizers in the ``cfi`` group.
2055 .. option:: -fsanitize-ignorelist=/path/to/ignorelist/file
2057 Disable or modify sanitizer checks for objects (source files, functions,
2058 variables, types) listed in the file. See
2059 :doc:`SanitizerSpecialCaseList` for file format description.
2061 .. option:: -fno-sanitize-ignorelist
2063 Don't use ignorelist file, if it was specified earlier in the command line.
2065 .. option:: -f[no-]sanitize-coverage=[type,features,...]
2067 Enable simple code coverage in addition to certain sanitizers.
2068 See :doc:`SanitizerCoverage` for more details.
2070 .. option:: -f[no-]sanitize-address-outline-instrumentation
2072 Controls how address sanitizer code is generated. If enabled will always use
2073 a function call instead of inlining the code. Turning this option on could
2074 reduce the binary size, but might result in a worse run-time performance.
2076 See :doc: `AddressSanitizer` for more details.
2078 .. option:: -f[no-]sanitize-stats
2080 Enable simple statistics gathering for the enabled sanitizers.
2081 See :doc:`SanitizerStats` for more details.
2083 .. option:: -fsanitize-undefined-trap-on-error
2085 Deprecated alias for ``-fsanitize-trap=undefined``.
2087 .. option:: -fsanitize-cfi-cross-dso
2089 Enable cross-DSO control flow integrity checks. This flag modifies
2090 the behavior of sanitizers in the ``cfi`` group to allow checking
2091 of cross-DSO virtual and indirect calls.
2093 .. option:: -fsanitize-cfi-icall-generalize-pointers
2095 Generalize pointers in return and argument types in function type signatures
2096 checked by Control Flow Integrity indirect call checking. See
2097 :doc:`ControlFlowIntegrity` for more details.
2099 .. option:: -fsanitize-cfi-icall-experimental-normalize-integers
2101 Normalize integers in return and argument types in function type signatures
2102 checked by Control Flow Integrity indirect call checking. See
2103 :doc:`ControlFlowIntegrity` for more details.
2105 This option is currently experimental.
2107 .. option:: -fstrict-vtable-pointers
2109 Enable optimizations based on the strict rules for overwriting polymorphic
2110 C++ objects, i.e. the vptr is invariant during an object's lifetime.
2111 This enables better devirtualization. Turned off by default, because it is
2114 .. option:: -fwhole-program-vtables
2116 Enable whole-program vtable optimizations, such as single-implementation
2117 devirtualization and virtual constant propagation, for classes with
2118 :doc:`hidden LTO visibility <LTOVisibility>`. Requires ``-flto``.
2120 .. option:: -f[no]split-lto-unit
2122 Controls splitting the :doc:`LTO unit <LTOVisibility>` into regular LTO and
2123 :doc:`ThinLTO` portions, when compiling with -flto=thin. Defaults to false
2124 unless ``-fsanitize=cfi`` or ``-fwhole-program-vtables`` are specified, in
2125 which case it defaults to true. Splitting is required with ``fsanitize=cfi``,
2126 and it is an error to disable via ``-fno-split-lto-unit``. Splitting is
2127 optional with ``-fwhole-program-vtables``, however, it enables more
2128 aggressive whole program vtable optimizations (specifically virtual constant
2131 When enabled, vtable definitions and select virtual functions are placed
2132 in the split regular LTO module, enabling more aggressive whole program
2133 vtable optimizations required for CFI and virtual constant propagation.
2134 However, this can increase the LTO link time and memory requirements over
2135 pure ThinLTO, as all split regular LTO modules are merged and LTO linked
2138 .. option:: -fforce-emit-vtables
2140 In order to improve devirtualization, forces emitting of vtables even in
2141 modules where it isn't necessary. It causes more inline virtual functions
2144 .. option:: -fno-assume-sane-operator-new
2146 Don't assume that the C++'s new operator is sane.
2148 This option tells the compiler to do not assume that C++'s global
2149 new operator will always return a pointer that does not alias any
2150 other pointer when the function returns.
2152 .. option:: -fassume-nothrow-exception-dtor
2154 Assume that an exception object' destructor will not throw, and generate
2155 less code for catch handlers. A throw expression of a type with a
2156 potentially-throwing destructor will lead to an error.
2158 By default, Clang assumes that the exception object may have a throwing
2159 destructor. For the Itanium C++ ABI, Clang generates a landing pad to
2160 destroy local variables and call ``_Unwind_Resume`` for the code
2161 ``catch (...) { ... }``. This option tells Clang that an exception object's
2162 destructor will not throw and code simplification is possible.
2164 .. option:: -ftrap-function=[name]
2166 Instruct code generator to emit a function call to the specified
2167 function name for ``__builtin_trap()``.
2169 LLVM code generator translates ``__builtin_trap()`` to a trap
2170 instruction if it is supported by the target ISA. Otherwise, the
2171 builtin is translated into a call to ``abort``. If this option is
2172 set, then the code generator will always lower the builtin to a call
2173 to the specified function regardless of whether the target ISA has a
2174 trap instruction. This option is useful for environments (e.g.
2175 deeply embedded) where a trap cannot be properly handled, or when
2176 some custom behavior is desired.
2178 .. option:: -ftls-model=[model]
2180 Select which TLS model to use.
2182 Valid values are: ``global-dynamic``, ``local-dynamic``,
2183 ``initial-exec`` and ``local-exec``. The default value is
2184 ``global-dynamic``. The compiler may use a different model if the
2185 selected model is not supported by the target, or if a more
2186 efficient model can be used. The TLS model can be overridden per
2187 variable using the ``tls_model`` attribute.
2189 .. option:: -femulated-tls
2191 Select emulated TLS model, which overrides all -ftls-model choices.
2193 In emulated TLS mode, all access to TLS variables are converted to
2194 calls to __emutls_get_address in the runtime library.
2196 .. option:: -mhwdiv=[values]
2198 Select the ARM modes (arm or thumb) that support hardware division
2201 Valid values are: ``arm``, ``thumb`` and ``arm,thumb``.
2202 This option is used to indicate which mode (arm or thumb) supports
2203 hardware division instructions. This only applies to the ARM
2206 .. option:: -m[no-]crc
2208 Enable or disable CRC instructions.
2210 This option is used to indicate whether CRC instructions are to
2211 be generated. This only applies to the ARM architecture.
2213 CRC instructions are enabled by default on ARMv8.
2215 .. option:: -mgeneral-regs-only
2217 Generate code which only uses the general purpose registers.
2219 This option restricts the generated code to use general registers
2220 only. This only applies to the AArch64 architecture.
2222 .. option:: -mcompact-branches=[values]
2224 Control the usage of compact branches for MIPSR6.
2226 Valid values are: ``never``, ``optimal`` and ``always``.
2227 The default value is ``optimal`` which generates compact branches
2228 when a delay slot cannot be filled. ``never`` disables the usage of
2229 compact branches and ``always`` generates compact branches whenever
2232 .. option:: -f[no-]max-type-align=[number]
2234 Instruct the code generator to not enforce a higher alignment than the given
2235 number (of bytes) when accessing memory via an opaque pointer or reference.
2236 This cap is ignored when directly accessing a variable or when the pointee
2237 type has an explicit “aligned” attribute.
2239 The value should usually be determined by the properties of the system allocator.
2240 Some builtin types, especially vector types, have very high natural alignments;
2241 when working with values of those types, Clang usually wants to use instructions
2242 that take advantage of that alignment. However, many system allocators do
2243 not promise to return memory that is more than 8-byte or 16-byte-aligned. Use
2244 this option to limit the alignment that the compiler can assume for an arbitrary
2245 pointer, which may point onto the heap.
2247 This option does not affect the ABI alignment of types; the layout of structs and
2248 unions and the value returned by the alignof operator remain the same.
2250 This option can be overridden on a case-by-case basis by putting an explicit
2251 “aligned” alignment on a struct, union, or typedef. For example:
2253 .. code-block:: console
2255 #include <immintrin.h>
2256 // Make an aligned typedef of the AVX-512 16-int vector type.
2257 typedef __v16si __aligned_v16si __attribute__((aligned(64)));
2259 void initialize_vector(__aligned_v16si *v) {
2260 // The compiler may assume that ‘v’ is 64-byte aligned, regardless of the
2261 // value of -fmax-type-align.
2264 .. option:: -faddrsig, -fno-addrsig
2266 Controls whether Clang emits an address-significance table into the object
2267 file. Address-significance tables allow linkers to implement `safe ICF
2268 <https://research.google.com/pubs/archive/36912.pdf>`_ without the false
2269 positives that can result from other implementation techniques such as
2270 relocation scanning. Address-significance tables are enabled by default
2271 on ELF targets when using the integrated assembler. This flag currently
2272 only has an effect on ELF targets.
2274 .. option:: -f[no]-unique-internal-linkage-names
2276 Controls whether Clang emits a unique (best-effort) symbol name for internal
2277 linkage symbols. When this option is set, compiler hashes the main source
2278 file path from the command line and appends it to all internal symbols. If a
2279 program contains multiple objects compiled with the same command-line source
2280 file path, the symbols are not guaranteed to be unique. This option is
2281 particularly useful in attributing profile information to the correct
2282 function when multiple functions with the same private linkage name exist
2285 It should be noted that this option cannot guarantee uniqueness and the
2286 following is an example where it is not unique when two modules contain
2287 symbols with the same private linkage name:
2289 .. code-block:: console
2291 $ cd $P/foo && clang -c -funique-internal-linkage-names name_conflict.c
2292 $ cd $P/bar && clang -c -funique-internal-linkage-names name_conflict.c
2293 $ cd $P && clang foo/name_conflict.o && bar/name_conflict.o
2295 .. option:: -fbasic-block-sections=[labels, all, list=<arg>, none]
2297 Controls how Clang emits text sections for basic blocks. With values ``all``
2298 and ``list=<arg>``, each basic block or a subset of basic blocks can be placed
2299 in its own unique section. With the "labels" value, normal text sections are
2300 emitted, but a ``.bb_addr_map`` section is emitted which includes address
2301 offsets for each basic block in the program, relative to the parent function
2304 With the ``list=<arg>`` option, a file containing the subset of basic blocks
2305 that need to placed in unique sections can be specified. The format of the
2306 file is as follows. For example, ``list=spec.txt`` where ``spec.txt`` is the
2315 will place the machine basic block with ``id 2`` in function ``foo`` in a
2316 unique section. It will also place all basic blocks of functions ``bar``
2319 Further, section clusters can also be specified using the ``list=<arg>``
2320 option. For example, ``list=spec.txt`` where ``spec.txt`` contains:
2328 will create two unique sections for function ``foo`` with the first
2329 containing the odd numbered basic blocks and the second containing the
2330 even numbered basic blocks.
2332 Basic block sections allow the linker to reorder basic blocks and enables
2333 link-time optimizations like whole program inter-procedural basic block
2336 Profile Guided Optimization
2337 ---------------------------
2339 Profile information enables better optimization. For example, knowing that a
2340 branch is taken very frequently helps the compiler make better decisions when
2341 ordering basic blocks. Knowing that a function ``foo`` is called more
2342 frequently than another function ``bar`` helps the inliner. Optimization
2343 levels ``-O2`` and above are recommended for use of profile guided optimization.
2345 Clang supports profile guided optimization with two different kinds of
2346 profiling. A sampling profiler can generate a profile with very low runtime
2347 overhead, or you can build an instrumented version of the code that collects
2348 more detailed profile information. Both kinds of profiles can provide execution
2349 counts for instructions in the code and information on branches taken and
2350 function invocation.
2352 Regardless of which kind of profiling you use, be careful to collect profiles
2353 by running your code with inputs that are representative of the typical
2354 behavior. Code that is not exercised in the profile will be optimized as if it
2355 is unimportant, and the compiler may make poor optimization choices for code
2356 that is disproportionately used while profiling.
2358 Differences Between Sampling and Instrumentation
2359 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2361 Although both techniques are used for similar purposes, there are important
2362 differences between the two:
2364 1. Profile data generated with one cannot be used by the other, and there is no
2365 conversion tool that can convert one to the other. So, a profile generated
2366 via ``-fprofile-generate`` or ``-fprofile-instr-generate`` must be used with
2367 ``-fprofile-use`` or ``-fprofile-instr-use``. Similarly, sampling profiles
2368 generated by external profilers must be converted and used with ``-fprofile-sample-use``
2369 or ``-fauto-profile``.
2371 2. Instrumentation profile data can be used for code coverage analysis and
2374 3. Sampling profiles can only be used for optimization. They cannot be used for
2375 code coverage analysis. Although it would be technically possible to use
2376 sampling profiles for code coverage, sample-based profiles are too
2377 coarse-grained for code coverage purposes; it would yield poor results.
2379 4. Sampling profiles must be generated by an external tool. The profile
2380 generated by that tool must then be converted into a format that can be read
2381 by LLVM. The section on sampling profilers describes one of the supported
2382 sampling profile formats.
2385 Using Sampling Profilers
2386 ^^^^^^^^^^^^^^^^^^^^^^^^
2388 Sampling profilers are used to collect runtime information, such as
2389 hardware counters, while your application executes. They are typically
2390 very efficient and do not incur a large runtime overhead. The
2391 sample data collected by the profiler can be used during compilation
2392 to determine what the most executed areas of the code are.
2394 Using the data from a sample profiler requires some changes in the way
2395 a program is built. Before the compiler can use profiling information,
2396 the code needs to execute under the profiler. The following is the
2397 usual build cycle when using sample profilers for optimization:
2399 1. Build the code with source line table information. You can use all the
2400 usual build flags that you always build your application with. The only
2401 requirement is that you add ``-gline-tables-only`` or ``-g`` to the
2402 command line. This is important for the profiler to be able to map
2403 instructions back to source line locations.
2405 .. code-block:: console
2407 $ clang++ -O2 -gline-tables-only code.cc -o code
2409 2. Run the executable under a sampling profiler. The specific profiler
2410 you use does not really matter, as long as its output can be converted
2411 into the format that the LLVM optimizer understands. Currently, there
2412 exists a conversion tool for the Linux Perf profiler
2413 (https://perf.wiki.kernel.org/), so these examples assume that you
2414 are using Linux Perf to profile your code.
2416 .. code-block:: console
2418 $ perf record -b ./code
2420 Note the use of the ``-b`` flag. This tells Perf to use the Last Branch
2421 Record (LBR) to record call chains. While this is not strictly required,
2422 it provides better call information, which improves the accuracy of
2425 3. Convert the collected profile data to LLVM's sample profile format.
2426 This is currently supported via the AutoFDO converter ``create_llvm_prof``.
2427 It is available at https://github.com/google/autofdo. Once built and
2428 installed, you can convert the ``perf.data`` file to LLVM using
2431 .. code-block:: console
2433 $ create_llvm_prof --binary=./code --out=code.prof
2435 This will read ``perf.data`` and the binary file ``./code`` and emit
2436 the profile data in ``code.prof``. Note that if you ran ``perf``
2437 without the ``-b`` flag, you need to use ``--use_lbr=false`` when
2438 calling ``create_llvm_prof``.
2440 Alternatively, the LLVM tool ``llvm-profgen`` can also be used to generate
2441 the LLVM sample profile:
2443 .. code-block:: console
2445 $ llvm-profgen --binary=./code --output=code.prof--perfdata=perf.data
2448 4. Build the code again using the collected profile. This step feeds
2449 the profile back to the optimizers. This should result in a binary
2450 that executes faster than the original one. Note that you are not
2451 required to build the code with the exact same arguments that you
2452 used in the first step. The only requirement is that you build the code
2453 with ``-gline-tables-only`` and ``-fprofile-sample-use``.
2455 .. code-block:: console
2457 $ clang++ -O2 -gline-tables-only -fprofile-sample-use=code.prof code.cc -o code
2459 [OPTIONAL] Sampling-based profiles can have inaccuracies or missing block/
2460 edge counters. The profile inference algorithm (profi) can be used to infer
2461 missing blocks and edge counts, and improve the quality of profile data.
2462 Enable it with ``-fsample-profile-use-profi``.
2464 .. code-block:: console
2466 $ clang++ -O2 -gline-tables-only -fprofile-sample-use=code.prof \
2467 -fsample-profile-use-profi code.cc -o code
2469 Sample Profile Formats
2470 """"""""""""""""""""""
2472 Since external profilers generate profile data in a variety of custom formats,
2473 the data generated by the profiler must be converted into a format that can be
2474 read by the backend. LLVM supports three different sample profile formats:
2476 1. ASCII text. This is the easiest one to generate. The file is divided into
2477 sections, which correspond to each of the functions with profile
2478 information. The format is described below. It can also be generated from
2479 the binary or gcov formats using the ``llvm-profdata`` tool.
2481 2. Binary encoding. This uses a more efficient encoding that yields smaller
2482 profile files. This is the format generated by the ``create_llvm_prof`` tool
2483 in https://github.com/google/autofdo.
2485 3. GCC encoding. This is based on the gcov format, which is accepted by GCC. It
2486 is only interesting in environments where GCC and Clang co-exist. This
2487 encoding is only generated by the ``create_gcov`` tool in
2488 https://github.com/google/autofdo. It can be read by LLVM and
2489 ``llvm-profdata``, but it cannot be generated by either.
2491 If you are using Linux Perf to generate sampling profiles, you can use the
2492 conversion tool ``create_llvm_prof`` described in the previous section.
2493 Otherwise, you will need to write a conversion tool that converts your
2494 profiler's native format into one of these three.
2497 Sample Profile Text Format
2498 """"""""""""""""""""""""""
2500 This section describes the ASCII text format for sampling profiles. It is,
2501 arguably, the easiest one to generate. If you are interested in generating any
2502 of the other two, consult the ``ProfileData`` library in LLVM's source tree
2503 (specifically, ``include/llvm/ProfileData/SampleProfReader.h``).
2505 .. code-block:: console
2507 function1:total_samples:total_head_samples
2508 offset1[.discriminator]: number_of_samples [fn1:num fn2:num ... ]
2509 offset2[.discriminator]: number_of_samples [fn3:num fn4:num ... ]
2511 offsetN[.discriminator]: number_of_samples [fn5:num fn6:num ... ]
2512 offsetA[.discriminator]: fnA:num_of_total_samples
2513 offsetA1[.discriminator]: number_of_samples [fn7:num fn8:num ... ]
2514 offsetA1[.discriminator]: number_of_samples [fn9:num fn10:num ... ]
2515 offsetB[.discriminator]: fnB:num_of_total_samples
2516 offsetB1[.discriminator]: number_of_samples [fn11:num fn12:num ... ]
2518 This is a nested tree in which the indentation represents the nesting level
2519 of the inline stack. There are no blank lines in the file. And the spacing
2520 within a single line is fixed. Additional spaces will result in an error
2521 while reading the file.
2523 Any line starting with the '#' character is completely ignored.
2525 Inlined calls are represented with indentation. The Inline stack is a
2526 stack of source locations in which the top of the stack represents the
2527 leaf function, and the bottom of the stack represents the actual
2528 symbol to which the instruction belongs.
2530 Function names must be mangled in order for the profile loader to
2531 match them in the current translation unit. The two numbers in the
2532 function header specify how many total samples were accumulated in the
2533 function (first number), and the total number of samples accumulated
2534 in the prologue of the function (second number). This head sample
2535 count provides an indicator of how frequently the function is invoked.
2537 There are two types of lines in the function body.
2539 - Sampled line represents the profile information of a source location.
2540 ``offsetN[.discriminator]: number_of_samples [fn5:num fn6:num ... ]``
2542 - Callsite line represents the profile information of an inlined callsite.
2543 ``offsetA[.discriminator]: fnA:num_of_total_samples``
2545 Each sampled line may contain several items. Some are optional (marked
2548 a. Source line offset. This number represents the line number
2549 in the function where the sample was collected. The line number is
2550 always relative to the line where symbol of the function is
2551 defined. So, if the function has its header at line 280, the offset
2552 13 is at line 293 in the file.
2554 Note that this offset should never be a negative number. This could
2555 happen in cases like macros. The debug machinery will register the
2556 line number at the point of macro expansion. So, if the macro was
2557 expanded in a line before the start of the function, the profile
2558 converter should emit a 0 as the offset (this means that the optimizers
2559 will not be able to associate a meaningful weight to the instructions
2562 b. [OPTIONAL] Discriminator. This is used if the sampled program
2563 was compiled with DWARF discriminator support
2564 (http://wiki.dwarfstd.org/index.php?title=Path_Discriminators).
2565 DWARF discriminators are unsigned integer values that allow the
2566 compiler to distinguish between multiple execution paths on the
2567 same source line location.
2569 For example, consider the line of code ``if (cond) foo(); else bar();``.
2570 If the predicate ``cond`` is true 80% of the time, then the edge
2571 into function ``foo`` should be considered to be taken most of the
2572 time. But both calls to ``foo`` and ``bar`` are at the same source
2573 line, so a sample count at that line is not sufficient. The
2574 compiler needs to know which part of that line is taken more
2577 This is what discriminators provide. In this case, the calls to
2578 ``foo`` and ``bar`` will be at the same line, but will have
2579 different discriminator values. This allows the compiler to correctly
2580 set edge weights into ``foo`` and ``bar``.
2582 c. Number of samples. This is an integer quantity representing the
2583 number of samples collected by the profiler at this source
2586 d. [OPTIONAL] Potential call targets and samples. If present, this
2587 line contains a call instruction. This models both direct and
2588 number of samples. For example,
2590 .. code-block:: console
2592 130: 7 foo:3 bar:2 baz:7
2594 The above means that at relative line offset 130 there is a call
2595 instruction that calls one of ``foo()``, ``bar()`` and ``baz()``,
2596 with ``baz()`` being the relatively more frequently called target.
2598 As an example, consider a program with the call chain ``main -> foo -> bar``.
2599 When built with optimizations enabled, the compiler may inline the
2600 calls to ``bar`` and ``foo`` inside ``main``. The generated profile
2601 could then be something like this:
2603 .. code-block:: console
2611 This profile indicates that there were a total of 35,504 samples
2612 collected in main. All of those were at line 1 (the call to ``foo``).
2613 Of those, 31,977 were spent inside the body of ``bar``. The last line
2614 of the profile (``2: 0``) corresponds to line 2 inside ``main``. No
2615 samples were collected there.
2619 Profiling with Instrumentation
2620 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2622 Clang also supports profiling via instrumentation. This requires building a
2623 special instrumented version of the code and has some runtime
2624 overhead during the profiling, but it provides more detailed results than a
2625 sampling profiler. It also provides reproducible results, at least to the
2626 extent that the code behaves consistently across runs.
2628 Clang supports two types of instrumentation: frontend-based and IR-based.
2629 Frontend-based instrumentation can be enabled with the option ``-fprofile-instr-generate``,
2630 and IR-based instrumentation can be enabled with the option ``-fprofile-generate``.
2631 For best performance with PGO, IR-based instrumentation should be used. It has
2632 the benefits of lower instrumentation overhead, smaller raw profile size, and
2633 better runtime performance. Frontend-based instrumentation, on the other hand,
2634 has better source correlation, so it should be used with source line-based
2637 The flag ``-fcs-profile-generate`` also instruments programs using the same
2638 instrumentation method as ``-fprofile-generate``. However, it performs a
2639 post-inline late instrumentation and can produce context-sensitive profiles.
2642 Here are the steps for using profile guided optimization with
2645 1. Build an instrumented version of the code by compiling and linking with the
2646 ``-fprofile-generate`` or ``-fprofile-instr-generate`` option.
2648 .. code-block:: console
2650 $ clang++ -O2 -fprofile-instr-generate code.cc -o code
2652 2. Run the instrumented executable with inputs that reflect the typical usage.
2653 By default, the profile data will be written to a ``default.profraw`` file
2654 in the current directory. You can override that default by using option
2655 ``-fprofile-instr-generate=`` or by setting the ``LLVM_PROFILE_FILE``
2656 environment variable to specify an alternate file. If non-default file name
2657 is specified by both the environment variable and the command line option,
2658 the environment variable takes precedence. The file name pattern specified
2659 can include different modifiers: ``%p``, ``%h``, ``%m``, ``%t``, and ``%c``.
2661 Any instance of ``%p`` in that file name will be replaced by the process
2662 ID, so that you can easily distinguish the profile output from multiple
2665 .. code-block:: console
2667 $ LLVM_PROFILE_FILE="code-%p.profraw" ./code
2669 The modifier ``%h`` can be used in scenarios where the same instrumented
2670 binary is run in multiple different host machines dumping profile data
2671 to a shared network based storage. The ``%h`` specifier will be substituted
2672 with the hostname so that profiles collected from different hosts do not
2675 While the use of ``%p`` specifier can reduce the likelihood for the profiles
2676 dumped from different processes to clobber each other, such clobbering can still
2677 happen because of the ``pid`` re-use by the OS. Another side-effect of using
2678 ``%p`` is that the storage requirement for raw profile data files is greatly
2679 increased. To avoid issues like this, the ``%m`` specifier can used in the profile
2680 name. When this specifier is used, the profiler runtime will substitute ``%m``
2681 with a unique integer identifier associated with the instrumented binary. Additionally,
2682 multiple raw profiles dumped from different processes that share a file system (can be
2683 on different hosts) will be automatically merged by the profiler runtime during the
2684 dumping. If the program links in multiple instrumented shared libraries, each library
2685 will dump the profile data into its own profile data file (with its unique integer
2686 id embedded in the profile name). Note that the merging enabled by ``%m`` is for raw
2687 profile data generated by profiler runtime. The resulting merged "raw" profile data
2688 file still needs to be converted to a different format expected by the compiler (
2691 .. code-block:: console
2693 $ LLVM_PROFILE_FILE="code-%m.profraw" ./code
2695 See `this <SourceBasedCodeCoverage.html#running-the-instrumented-program>`_ section
2696 about the ``%t``, and ``%c`` modifiers.
2698 3. Combine profiles from multiple runs and convert the "raw" profile format to
2699 the input expected by clang. Use the ``merge`` command of the
2700 ``llvm-profdata`` tool to do this.
2702 .. code-block:: console
2704 $ llvm-profdata merge -output=code.profdata code-*.profraw
2706 Note that this step is necessary even when there is only one "raw" profile,
2707 since the merge operation also changes the file format.
2709 4. Build the code again using the ``-fprofile-use`` or ``-fprofile-instr-use``
2710 option to specify the collected profile data.
2712 .. code-block:: console
2714 $ clang++ -O2 -fprofile-instr-use=code.profdata code.cc -o code
2716 You can repeat step 4 as often as you like without regenerating the
2717 profile. As you make changes to your code, clang may no longer be able to
2718 use the profile data. It will warn you when this happens.
2720 Note that ``-fprofile-use`` option is semantically equivalent to
2721 its GCC counterpart, it *does not* handle profile formats produced by GCC.
2722 Both ``-fprofile-use`` and ``-fprofile-instr-use`` accept profiles in the
2723 indexed format, regardeless whether it is produced by frontend or the IR pass.
2725 .. option:: -fprofile-generate[=<dirname>]
2727 The ``-fprofile-generate`` and ``-fprofile-generate=`` flags will use
2728 an alternative instrumentation method for profile generation. When
2729 given a directory name, it generates the profile file
2730 ``default_%m.profraw`` in the directory named ``dirname`` if specified.
2731 If ``dirname`` does not exist, it will be created at runtime. ``%m`` specifier
2732 will be substituted with a unique id documented in step 2 above. In other words,
2733 with ``-fprofile-generate[=<dirname>]`` option, the "raw" profile data automatic
2734 merging is turned on by default, so there will no longer any risk of profile
2735 clobbering from different running processes. For example,
2737 .. code-block:: console
2739 $ clang++ -O2 -fprofile-generate=yyy/zzz code.cc -o code
2741 When ``code`` is executed, the profile will be written to the file
2742 ``yyy/zzz/default_xxxx.profraw``.
2744 To generate the profile data file with the compiler readable format, the
2745 ``llvm-profdata`` tool can be used with the profile directory as the input:
2747 .. code-block:: console
2749 $ llvm-profdata merge -output=code.profdata yyy/zzz/
2751 If the user wants to turn off the auto-merging feature, or simply override the
2752 the profile dumping path specified at command line, the environment variable
2753 ``LLVM_PROFILE_FILE`` can still be used to override
2754 the directory and filename for the profile file at runtime.
2755 To override the path and filename at compile time, use
2756 ``-Xclang -fprofile-instrument-path=/path/to/file_pattern.profraw``.
2758 .. option:: -fcs-profile-generate[=<dirname>]
2760 The ``-fcs-profile-generate`` and ``-fcs-profile-generate=`` flags will use
2761 the same instrumentation method, and generate the same profile as in the
2762 ``-fprofile-generate`` and ``-fprofile-generate=`` flags. The difference is
2763 that the instrumentation is performed after inlining so that the resulted
2764 profile has a better context sensitive information. They cannot be used
2765 together with ``-fprofile-generate`` and ``-fprofile-generate=`` flags.
2766 They are typically used in conjunction with ``-fprofile-use`` flag.
2767 The profile generated by ``-fcs-profile-generate`` and ``-fprofile-generate``
2768 can be merged by llvm-profdata. A use example:
2770 .. code-block:: console
2772 $ clang++ -O2 -fprofile-generate=yyy/zzz code.cc -o code
2774 $ llvm-profdata merge -output=code.profdata yyy/zzz/
2776 The first few steps are the same as that in ``-fprofile-generate``
2777 compilation. Then perform a second round of instrumentation.
2779 .. code-block:: console
2781 $ clang++ -O2 -fprofile-use=code.profdata -fcs-profile-generate=sss/ttt \
2784 $ llvm-profdata merge -output=cs_code.profdata sss/ttt code.profdata
2786 The resulted ``cs_code.prodata`` combines ``code.profdata`` and the profile
2787 generated from binary ``cs_code``. Profile ``cs_code.profata`` can be used by
2788 ``-fprofile-use`` compilation.
2790 .. code-block:: console
2792 $ clang++ -O2 -fprofile-use=cs_code.profdata
2794 The above command will read both profiles to the compiler at the identical
2795 point of instrumentations.
2797 .. option:: -fprofile-use[=<pathname>]
2799 Without any other arguments, ``-fprofile-use`` behaves identically to
2800 ``-fprofile-instr-use``. Otherwise, if ``pathname`` is the full path to a
2801 profile file, it reads from that file. If ``pathname`` is a directory name,
2802 it reads from ``pathname/default.profdata``.
2804 .. option:: -fprofile-update[=<method>]
2806 Unless ``-fsanitize=thread`` is specified, the default is ``single``, which
2807 uses non-atomic increments. The counters can be inaccurate under thread
2808 contention. ``atomic`` uses atomic increments which is accurate but has
2809 overhead. ``prefer-atomic`` will be transformed to ``atomic`` when supported
2810 by the target, or ``single`` otherwise.
2812 Disabling Instrumentation
2813 ^^^^^^^^^^^^^^^^^^^^^^^^^
2815 In certain situations, it may be useful to disable profile generation or use
2816 for specific files in a build, without affecting the main compilation flags
2817 used for the other files in the project.
2819 In these cases, you can use the flag ``-fno-profile-instr-generate`` (or
2820 ``-fno-profile-generate``) to disable profile generation, and
2821 ``-fno-profile-instr-use`` (or ``-fno-profile-use``) to disable profile use.
2823 Note that these flags should appear after the corresponding profile
2824 flags to have an effect.
2828 When none of the translation units inside a binary is instrumented, in the
2829 case of Fuchsia the profile runtime will not be linked into the binary and
2830 no profile will be produced, while on other platforms the profile runtime
2831 will be linked and profile will be produced but there will not be any
2834 Instrumenting only selected files or functions
2835 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2837 Sometimes it's useful to only instrument certain files or functions. For
2838 example in automated testing infrastructure, it may be desirable to only
2839 instrument files or functions that were modified by a patch to reduce the
2840 overhead of instrumenting a full system.
2842 This can be done using the ``-fprofile-list`` option.
2844 .. option:: -fprofile-list=<pathname>
2846 This option can be used to apply profile instrumentation only to selected
2847 files or functions. ``pathname`` should point to a file in the
2848 :doc:`SanitizerSpecialCaseList` format which selects which files and
2849 functions to instrument.
2851 .. code-block:: console
2853 $ clang++ -O2 -fprofile-instr-generate -fprofile-list=fun.list code.cc -o code
2855 The option can be specified multiple times to pass multiple files.
2857 .. code-block:: console
2859 $ clang++ -O2 -fprofile-instr-generate -fcoverage-mapping -fprofile-list=fun.list -fprofile-list=code.list code.cc -o code
2861 Supported sections are ``[clang]``, ``[llvm]``, and ``[csllvm]`` representing
2862 clang PGO, IRPGO, and CSIRPGO, respectively. Supported prefixes are ``function``
2863 and ``source``. Supported categories are ``allow``, ``skip``, and ``forbid``.
2864 ``skip`` adds the ``skipprofile`` attribute while ``forbid`` adds the
2865 ``noprofile`` attribute to the appropriate function. Use
2866 ``default:<allow|skip|forbid>`` to specify the default category.
2868 .. code-block:: console
2871 # The following cases are for clang instrumentation.
2874 # We might not want to profile functions that are inlined in many places.
2875 function:inlinedLots=skip
2877 # We want to forbid profiling where it might be dangerous.
2878 source:lib/unsafe/*.cc=forbid
2880 # Otherwise we allow profiling.
2885 An older format is also supported, but it is only able to add the
2886 ``noprofile`` attribute.
2887 To filter individual functions or entire source files use ``fun:<name>`` or
2888 ``src:<file>`` respectively. To exclude a function or a source file, use
2889 ``!fun:<name>`` or ``!src:<file>`` respectively. The format also supports
2890 wildcard expansion. The compiler generated functions are assumed to be located
2891 in the main source file. It is also possible to restrict the filter to a
2892 particular instrumentation type by using a named section.
2894 .. code-block:: none
2896 # all functions whose name starts with foo will be instrumented.
2899 # except for foo1 which will be excluded from instrumentation.
2902 # every function in path/to/foo.cc will be instrumented.
2905 # bar will be instrumented only when using backend instrumentation.
2906 # Recognized section names are clang, llvm and csllvm.
2910 When the file contains only excludes, all files and functions except for the
2911 excluded ones will be instrumented. Otherwise, only the files and functions
2912 specified will be instrumented.
2914 Instrument function groups
2915 ^^^^^^^^^^^^^^^^^^^^^^^^^^
2917 Sometimes it is desirable to minimize the size overhead of instrumented
2918 binaries. One way to do this is to partition functions into groups and only
2919 instrument functions in a specified group. This can be done using the
2920 `-fprofile-function-groups` and `-fprofile-selected-function-group` options.
2922 .. option:: -fprofile-function-groups=<N>, -fprofile-selected-function-group=<i>
2924 The following uses 3 groups
2926 .. code-block:: console
2928 $ clang++ -Oz -fprofile-generate=group_0/ -fprofile-function-groups=3 -fprofile-selected-function-group=0 code.cc -o code.0
2929 $ clang++ -Oz -fprofile-generate=group_1/ -fprofile-function-groups=3 -fprofile-selected-function-group=1 code.cc -o code.1
2930 $ clang++ -Oz -fprofile-generate=group_2/ -fprofile-function-groups=3 -fprofile-selected-function-group=2 code.cc -o code.2
2932 After collecting raw profiles from the three binaries, they can be merged into
2933 a single profile like normal.
2935 .. code-block:: console
2937 $ llvm-profdata merge -output=code.profdata group_*/*.profraw
2943 When the program is compiled after a change that affects many symbol names,
2944 pre-existing profile data may no longer match the program. For example:
2946 * switching from libstdc++ to libc++ will result in the mangled names of all
2947 functions taking standard library types to change
2948 * renaming a widely-used type in C++ will result in the mangled names of all
2949 functions that have parameters involving that type to change
2950 * moving from a 32-bit compilation to a 64-bit compilation may change the
2951 underlying type of ``size_t`` and similar types, resulting in changes to
2954 Clang allows use of a profile remapping file to specify that such differences
2955 in mangled names should be ignored when matching the profile data against the
2958 .. option:: -fprofile-remapping-file=<file>
2960 Specifies a file containing profile remapping information, that will be
2961 used to match mangled names in the profile data to mangled names in the
2964 The profile remapping file is a text file containing lines of the form
2966 .. code-block:: text
2968 fragmentkind fragment1 fragment2
2970 where ``fragmentkind`` is one of ``name``, ``type``, or ``encoding``,
2971 indicating whether the following mangled name fragments are
2972 <`name <https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangle.name>`_>s,
2973 <`type <https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangle.type>`_>s, or
2974 <`encoding <https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangle.encoding>`_>s,
2976 Blank lines and lines starting with ``#`` are ignored.
2978 For convenience, built-in <substitution>s such as ``St`` and ``Ss``
2979 are accepted as <name>s (even though they technically are not <name>s).
2981 For example, to specify that ``absl::string_view`` and ``std::string_view``
2982 should be treated as equivalent when matching profile data, the following
2983 remapping file could be used:
2985 .. code-block:: text
2987 # absl::string_view is considered equivalent to std::string_view
2988 type N4absl11string_viewE St17basic_string_viewIcSt11char_traitsIcEE
2990 # std:: might be std::__1:: in libc++ or std::__cxx11:: in libstdc++
2992 name 3std St7__cxx11
2994 Matching profile data using a profile remapping file is supported on a
2995 best-effort basis. For example, information regarding indirect call targets is
2996 currently not remapped. For best results, you are encouraged to generate new
2997 profile data matching the updated program, or to remap the profile data
2998 using the ``llvm-cxxmap`` and ``llvm-profdata merge`` tools.
3002 Profile data remapping is currently only supported for C++ mangled names
3003 following the Itanium C++ ABI mangling scheme. This covers all C++ targets
3004 supported by Clang other than Windows.
3006 GCOV-based Profiling
3007 --------------------
3009 GCOV is a test coverage program, it helps to know how often a line of code
3010 is executed. When instrumenting the code with ``--coverage`` option, some
3011 counters are added for each edge linking basic blocks.
3013 At compile time, gcno files are generated containing information about
3014 blocks and edges between them. At runtime the counters are incremented and at
3015 exit the counters are dumped in gcda files.
3017 The tool ``llvm-cov gcov`` will parse gcno, gcda and source files to generate
3018 a report ``.c.gcov``.
3020 .. option:: -fprofile-filter-files=[regexes]
3022 Define a list of regexes separated by a semi-colon.
3023 If a file name matches any of the regexes then the file is instrumented.
3025 .. code-block:: console
3027 $ clang --coverage -fprofile-filter-files=".*\.c$" foo.c
3029 For example, this will only instrument files finishing with ``.c``, skipping ``.h`` files.
3031 .. option:: -fprofile-exclude-files=[regexes]
3033 Define a list of regexes separated by a semi-colon.
3034 If a file name doesn't match all the regexes then the file is instrumented.
3036 .. code-block:: console
3038 $ clang --coverage -fprofile-exclude-files="^/usr/include/.*$" foo.c
3040 For example, this will instrument all the files except the ones in ``/usr/include``.
3042 If both options are used then a file is instrumented if its name matches any
3043 of the regexes from ``-fprofile-filter-list`` and doesn't match all the regexes
3044 from ``-fprofile-exclude-list``.
3046 .. code-block:: console
3048 $ clang --coverage -fprofile-exclude-files="^/usr/include/.*$" \
3049 -fprofile-filter-files="^/usr/.*$"
3051 In that case ``/usr/foo/oof.h`` is instrumented since it matches the filter regex and
3052 doesn't match the exclude regex, but ``/usr/include/foo.h`` doesn't since it matches
3055 Controlling Debug Information
3056 -----------------------------
3058 Controlling Size of Debug Information
3059 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3061 Debug info kind generated by Clang can be set by one of the flags listed
3062 below. If multiple flags are present, the last one is used.
3066 Don't generate any debug info (default).
3068 .. option:: -gline-tables-only
3070 Generate line number tables only.
3072 This kind of debug info allows to obtain stack traces with function names,
3073 file names and line numbers (by such tools as ``gdb`` or ``addr2line``). It
3074 doesn't contain any other data (e.g. description of local variables or
3075 function parameters).
3077 .. option:: -fstandalone-debug
3079 Clang supports a number of optimizations to reduce the size of debug
3080 information in the binary. They work based on the assumption that
3081 the debug type information can be spread out over multiple
3082 compilation units. Specifically, the optimizations are:
3084 - will not emit type definitions for types that are not needed by a
3085 module and could be replaced with a forward declaration.
3086 - will only emit type info for a dynamic C++ class in the module that
3087 contains the vtable for the class.
3088 - will only emit type info for a C++ class (non-trivial, non-aggregate)
3089 in the modules that contain a definition for one of its constructors.
3090 - will only emit type definitions for types that are the subject of explicit
3091 template instantiation declarations in the presence of an explicit
3092 instantiation definition for the type.
3094 The **-fstandalone-debug** option turns off these optimizations.
3095 This is useful when working with 3rd-party libraries that don't come
3096 with debug information. Note that Clang will never emit type
3097 information for types that are not referenced at all by the program.
3099 .. option:: -fno-standalone-debug
3101 On Darwin **-fstandalone-debug** is enabled by default. The
3102 **-fno-standalone-debug** option can be used to get to turn on the
3103 vtable-based optimization described above.
3107 Generate complete debug info.
3109 .. option:: -feliminate-unused-debug-types
3111 By default, Clang does not emit type information for types that are defined
3112 but not used in a program. To retain the debug info for these unused types,
3113 the negation **-fno-eliminate-unused-debug-types** can be used.
3115 Controlling Macro Debug Info Generation
3116 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3118 Debug info for C preprocessor macros increases the size of debug information in
3119 the binary. Macro debug info generated by Clang can be controlled by the flags
3122 .. option:: -fdebug-macro
3124 Generate debug info for preprocessor macros. This flag is discarded when
3127 .. option:: -fno-debug-macro
3129 Do not generate debug info for preprocessor macros (default).
3131 Controlling Debugger "Tuning"
3132 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3134 While Clang generally emits standard DWARF debug info (http://dwarfstd.org),
3135 different debuggers may know how to take advantage of different specific DWARF
3136 features. You can "tune" the debug info for one of several different debuggers.
3138 .. option:: -ggdb, -glldb, -gsce, -gdbx
3140 Tune the debug info for the ``gdb``, ``lldb``, Sony PlayStation\ |reg|
3141 debugger, or ``dbx``, respectively. Each of these options implies **-g**.
3142 (Therefore, if you want both **-gline-tables-only** and debugger tuning, the
3143 tuning option must come first.)
3145 Controlling LLVM IR Output
3146 --------------------------
3148 Controlling Value Names in LLVM IR
3149 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3151 Emitting value names in LLVM IR increases the size and verbosity of the IR.
3152 By default, value names are only emitted in assertion-enabled builds of Clang.
3153 However, when reading IR it can be useful to re-enable the emission of value
3154 names to improve readability.
3156 .. option:: -fdiscard-value-names
3158 Discard value names when generating LLVM IR.
3160 .. option:: -fno-discard-value-names
3162 Do not discard value names when generating LLVM IR. This option can be used
3163 to re-enable names for release builds of Clang.
3166 Comment Parsing Options
3167 -----------------------
3169 Clang parses Doxygen and non-Doxygen style documentation comments and attaches
3170 them to the appropriate declaration nodes. By default, it only parses
3171 Doxygen-style comments and ignores ordinary comments starting with ``//`` and
3174 .. option:: -Wdocumentation
3176 Emit warnings about use of documentation comments. This warning group is off
3179 This includes checking that ``\param`` commands name parameters that actually
3180 present in the function signature, checking that ``\returns`` is used only on
3181 functions that actually return a value etc.
3183 .. option:: -Wno-documentation-unknown-command
3185 Don't warn when encountering an unknown Doxygen command.
3187 .. option:: -fparse-all-comments
3189 Parse all comments as documentation comments (including ordinary comments
3190 starting with ``//`` and ``/*``).
3192 .. option:: -fcomment-block-commands=[commands]
3194 Define custom documentation commands as block commands. This allows Clang to
3195 construct the correct AST for these custom commands, and silences warnings
3196 about unknown commands. Several commands must be separated by a comma
3197 *without trailing space*; e.g. ``-fcomment-block-commands=foo,bar`` defines
3198 custom commands ``\foo`` and ``\bar``.
3200 It is also possible to use ``-fcomment-block-commands`` several times; e.g.
3201 ``-fcomment-block-commands=foo -fcomment-block-commands=bar`` does the same
3209 The support for standard C in clang is feature-complete except for the
3210 C99 floating-point pragmas.
3212 Extensions supported by clang
3213 -----------------------------
3215 See :doc:`LanguageExtensions`.
3217 Differences between various standard modes
3218 ------------------------------------------
3220 clang supports the -std option, which changes what language mode clang uses.
3221 The supported modes for C are c89, gnu89, c94, c99, gnu99, c11, gnu11, c17,
3222 gnu17, c23, gnu23, and various aliases for those modes. If no -std option is
3223 specified, clang defaults to gnu17 mode. Many C99 and C11 features are
3224 supported in earlier modes as a conforming extension, with a warning. Use
3225 ``-pedantic-errors`` to request an error if a feature from a later standard
3226 revision is used in an earlier mode.
3228 Differences between all ``c*`` and ``gnu*`` modes:
3230 - ``c*`` modes define "``__STRICT_ANSI__``".
3231 - Target-specific defines not prefixed by underscores, like ``linux``,
3232 are defined in ``gnu*`` modes.
3233 - Trigraphs default to being off in ``gnu*`` modes; they can be enabled
3234 by the ``-trigraphs`` option.
3235 - The parser recognizes ``asm`` and ``typeof`` as keywords in ``gnu*`` modes;
3236 the variants ``__asm__`` and ``__typeof__`` are recognized in all modes.
3237 - The parser recognizes ``inline`` as a keyword in ``gnu*`` mode, in
3238 addition to recognizing it in the ``*99`` and later modes for which it is
3239 part of the ISO C standard. The variant ``__inline__`` is recognized in all
3241 - The Apple "blocks" extension is recognized by default in ``gnu*`` modes
3242 on some platforms; it can be enabled in any mode with the ``-fblocks``
3245 Differences between ``*89`` and ``*94`` modes:
3247 - Digraphs are not recognized in c89 mode.
3249 Differences between ``*94`` and ``*99`` modes:
3251 - The ``*99`` modes default to implementing ``inline`` / ``__inline__``
3252 as specified in C99, while the ``*89`` modes implement the GNU version.
3253 This can be overridden for individual functions with the ``__gnu_inline__``
3255 - The scope of names defined inside a ``for``, ``if``, ``switch``, ``while``,
3256 or ``do`` statement is different. (example: ``if ((struct x {int x;}*)0) {}``.)
3257 - ``__STDC_VERSION__`` is not defined in ``*89`` modes.
3258 - ``inline`` is not recognized as a keyword in ``c89`` mode.
3259 - ``restrict`` is not recognized as a keyword in ``*89`` modes.
3260 - Commas are allowed in integer constant expressions in ``*99`` modes.
3261 - Arrays which are not lvalues are not implicitly promoted to pointers
3263 - Some warnings are different.
3265 Differences between ``*99`` and ``*11`` modes:
3267 - Warnings for use of C11 features are disabled.
3268 - ``__STDC_VERSION__`` is defined to ``201112L`` rather than ``199901L``.
3270 Differences between ``*11`` and ``*17`` modes:
3272 - ``__STDC_VERSION__`` is defined to ``201710L`` rather than ``201112L``.
3274 Differences between ``*17`` and ``*23`` modes:
3276 - ``__STDC_VERSION__`` is defined to ``202311L`` rather than ``201710L``.
3277 - ``nullptr`` and ``nullptr_t`` are supported, only in ``*23`` mode.
3278 - ``ATOMIC_VAR_INIT`` is removed from ``*23`` mode.
3279 - ``bool``, ``true``, ``false``, ``alignas``, ``alignof``, ``static_assert``,
3280 and ``thread_local` are now first-class keywords, only in ``*23`` mode.
3281 - ``typeof`` and ``typeof_unqual`` are supported, only ``*23`` mode.
3282 - Bit-precise integers (``_BitInt(N)``) are supported by default in ``*23``
3283 mode, and as an extension in ``*17`` and earlier modes.
3284 - ``[[]]`` attributes are supported by default in ``*23`` mode, and as an
3285 extension in ``*17`` and earlier modes.
3287 GCC extensions not implemented yet
3288 ----------------------------------
3290 clang tries to be compatible with gcc as much as possible, but some gcc
3291 extensions are not implemented yet:
3293 - clang does not support decimal floating point types (``_Decimal32`` and
3295 - clang does not support nested functions; this is a complex feature
3296 which is infrequently used, so it is unlikely to be implemented
3297 anytime soon. In C++11 it can be emulated by assigning lambda
3298 functions to local variables, e.g:
3302 auto const local_function = [&](int parameter) {
3308 - clang only supports global register variables when the register specified
3309 is non-allocatable (e.g. the stack pointer). Support for general global
3310 register variables is unlikely to be implemented soon because it requires
3311 additional LLVM backend support.
3312 - clang does not support static initialization of flexible array
3313 members. This appears to be a rarely used extension, but could be
3314 implemented pending user demand.
3315 - clang does not support
3316 ``__builtin_va_arg_pack``/``__builtin_va_arg_pack_len``. This is
3317 used rarely, but in some potentially interesting places, like the
3318 glibc headers, so it may be implemented pending user demand. Note
3319 that because clang pretends to be like GCC 4.2, and this extension
3320 was introduced in 4.3, the glibc headers will not try to use this
3321 extension with clang at the moment.
3322 - clang does not support the gcc extension for forward-declaring
3323 function parameters; this has not shown up in any real-world code
3324 yet, though, so it might never be implemented.
3326 This is not a complete list; if you find an unsupported extension
3327 missing from this list, please send an e-mail to cfe-dev. This list
3328 currently excludes C++; see :ref:`C++ Language Features <cxx>`. Also, this
3329 list does not include bugs in mostly-implemented features; please see
3331 tracker <https://bugs.llvm.org/buglist.cgi?quicksearch=product%3Aclang+component%3A-New%2BBugs%2CAST%2CBasic%2CDriver%2CHeaders%2CLLVM%2BCodeGen%2Cparser%2Cpreprocessor%2CSemantic%2BAnalyzer>`_
3332 for known existing bugs (FIXME: Is there a section for bug-reporting
3333 guidelines somewhere?).
3335 Intentionally unsupported GCC extensions
3336 ----------------------------------------
3338 - clang does not support the gcc extension that allows variable-length
3339 arrays in structures. This is for a few reasons: one, it is tricky to
3340 implement, two, the extension is completely undocumented, and three,
3341 the extension appears to be rarely used. Note that clang *does*
3342 support flexible array members (arrays with a zero or unspecified
3343 size at the end of a structure).
3344 - GCC accepts many expression forms that are not valid integer constant
3345 expressions in bit-field widths, enumerator constants, case labels,
3346 and in array bounds at global scope. Clang also accepts additional
3347 expression forms in these contexts, but constructs that GCC accepts due to
3348 simplifications GCC performs while parsing, such as ``x - x`` (where ``x`` is a
3349 variable) will likely never be accepted by Clang.
3350 - clang does not support ``__builtin_apply`` and friends; this extension
3351 is extremely obscure and difficult to implement reliably.
3355 Microsoft extensions
3356 --------------------
3358 clang has support for many extensions from Microsoft Visual C++. To enable these
3359 extensions, use the ``-fms-extensions`` command-line option. This is the default
3360 for Windows targets. Clang does not implement every pragma or declspec provided
3361 by MSVC, but the popular ones, such as ``__declspec(dllexport)`` and ``#pragma
3362 comment(lib)`` are well supported.
3364 clang has a ``-fms-compatibility`` flag that makes clang accept enough
3365 invalid C++ to be able to parse most Microsoft headers. For example, it
3366 allows `unqualified lookup of dependent base class members
3367 <https://clang.llvm.org/compatibility.html#dep_lookup_bases>`_, which is
3368 a common compatibility issue with clang. This flag is enabled by default
3369 for Windows targets.
3371 ``-fdelayed-template-parsing`` lets clang delay parsing of function template
3372 definitions until the end of a translation unit. This flag is enabled by
3373 default for Windows targets.
3375 For compatibility with existing code that compiles with MSVC, clang defines the
3376 ``_MSC_VER`` and ``_MSC_FULL_VER`` macros. When on Windows, these default to
3377 either the same value as the currently installed version of cl.exe, or ``1933``
3378 and ``193300000`` (respectively). The ``-fms-compatibility-version=`` flag
3379 overrides these values. It accepts a dotted version tuple, such as 19.00.23506.
3380 Changing the MSVC compatibility version makes clang behave more like that
3381 version of MSVC. For example, ``-fms-compatibility-version=19`` will enable
3382 C++14 features and define ``char16_t`` and ``char32_t`` as builtin types.
3386 C++ Language Features
3387 =====================
3389 clang fully implements all of standard C++98 except for exported
3390 templates (which were removed in C++11), all of standard C++11,
3391 C++14, and C++17, and most of C++20.
3393 See the `C++ support in Clang <https://clang.llvm.org/cxx_status.html>`_ page
3394 for detailed information on C++ feature support across Clang versions.
3396 Controlling implementation limits
3397 ---------------------------------
3399 .. option:: -fbracket-depth=N
3401 Sets the limit for nested parentheses, brackets, and braces to N. The
3404 .. option:: -fconstexpr-depth=N
3406 Sets the limit for constexpr function invocations to N. The default is 512.
3408 .. option:: -fconstexpr-steps=N
3410 Sets the limit for the number of full-expressions evaluated in a single
3411 constant expression evaluation. This also controls the maximum size
3412 of array and dynamic array allocation that can be constant evaluated.
3413 The default is 1048576.
3415 .. option:: -ftemplate-depth=N
3417 Sets the limit for recursively nested template instantiations to N. The
3420 .. option:: -foperator-arrow-depth=N
3422 Sets the limit for iterative calls to 'operator->' functions to N. The
3427 Objective-C Language Features
3428 =============================
3432 Objective-C++ Language Features
3433 ===============================
3440 Clang supports all OpenMP 4.5 directives and clauses. See :doc:`OpenMPSupport`
3441 for additional details.
3443 Use `-fopenmp` to enable OpenMP. Support for OpenMP can be disabled with
3446 Use `-fopenmp-simd` to enable OpenMP simd features only, without linking
3447 the runtime library; for combined constructs
3448 (e.g. ``#pragma omp parallel for simd``) the non-simd directives and clauses
3449 will be ignored. This can be disabled with `-fno-openmp-simd`.
3451 Controlling implementation limits
3452 ---------------------------------
3454 .. option:: -fopenmp-use-tls
3456 Controls code generation for OpenMP threadprivate variables. In presence of
3457 this option all threadprivate variables are generated the same way as thread
3458 local variables, using TLS support. If `-fno-openmp-use-tls`
3459 is provided or target does not support TLS, code generation for threadprivate
3460 variables relies on OpenMP runtime library.
3467 Clang can be used to compile OpenCL kernels for execution on a device
3468 (e.g. GPU). It is possible to compile the kernel into a binary (e.g. for AMDGPU)
3469 that can be uploaded to run directly on a device (e.g. using
3470 `clCreateProgramWithBinary
3471 <https://www.khronos.org/registry/OpenCL/specs/opencl-1.1.pdf#111>`_) or
3472 into generic bitcode files loadable into other toolchains.
3474 Compiling to a binary using the default target from the installation can be done
3477 .. code-block:: console
3479 $ echo "kernel void k(){}" > test.cl
3482 Compiling for a specific target can be done by specifying the triple corresponding
3483 to the target, for example:
3485 .. code-block:: console
3487 $ clang --target=nvptx64-unknown-unknown test.cl
3488 $ clang --target=amdgcn-amd-amdhsa -mcpu=gfx900 test.cl
3490 Compiling to bitcode can be done as follows:
3492 .. code-block:: console
3494 $ clang -c -emit-llvm test.cl
3496 This will produce a file `test.bc` that can be used in vendor toolchains
3497 to perform machine code generation.
3499 Note that if compiled to bitcode for generic targets such as SPIR/SPIR-V,
3500 portable IR is produced that can be used with various vendor
3501 tools as well as open source tools such as `SPIRV-LLVM Translator
3502 <https://github.com/KhronosGroup/SPIRV-LLVM-Translator>`_
3503 to produce SPIR-V binary. More details are provided in `the offline
3504 compilation from OpenCL kernel sources into SPIR-V using open source
3506 <https://github.com/KhronosGroup/OpenCL-Guide/blob/main/chapters/os_tooling.md>`_.
3507 From clang 14 onwards SPIR-V can be generated directly as detailed in
3508 :ref:`the SPIR-V support section <spir-v>`.
3510 Clang currently supports OpenCL C language standards up to v2.0. Clang mainly
3511 supports full profile. There is only very limited support of the embedded
3513 From clang 9 a C++ mode is available for OpenCL (see
3514 :ref:`C++ for OpenCL <cxx_for_opencl>`).
3516 OpenCL v3.0 support is complete but it remains in experimental state, see more
3517 details about the experimental features and limitations in :doc:`OpenCLSupport`
3520 OpenCL Specific Options
3521 -----------------------
3523 Most of the OpenCL build options from `the specification v2.0 section 5.8.4
3524 <https://www.khronos.org/registry/cl/specs/opencl-2.0.pdf#200>`_ are available.
3528 .. code-block:: console
3530 $ clang -cl-std=CL2.0 -cl-single-precision-constant test.cl
3533 Many flags used for the compilation for C sources can also be passed while
3534 compiling for OpenCL, examples: ``-c``, ``-O<1-4|s>``, ``-o``, ``-emit-llvm``, etc.
3536 Some extra options are available to support special OpenCL features.
3538 .. option:: -cl-no-stdinc
3540 Allows to disable all extra types and functions that are not native to the compiler.
3541 This might reduce the compilation speed marginally but many declarations from the
3542 OpenCL standard will not be accessible. For example, the following will fail to
3545 .. code-block:: console
3547 $ echo "bool is_wg_uniform(int i){return get_enqueued_local_size(i)==get_local_size(i);}" > test.cl
3548 $ clang -cl-std=CL2.0 -cl-no-stdinc test.cl
3549 error: use of undeclared identifier 'get_enqueued_local_size'
3550 error: use of undeclared identifier 'get_local_size'
3552 More information about the standard types and functions is provided in :ref:`the
3553 section on the OpenCL Header <opencl_header>`.
3559 Enables/Disables support of OpenCL extensions and optional features. All OpenCL
3560 targets set a list of extensions that they support. Clang allows to amend this using
3561 the ``-cl-ext`` flag with a comma-separated list of extensions prefixed with
3562 ``'+'`` or ``'-'``. The syntax: ``-cl-ext=<(['-'|'+']<extension>[,])+>``, where
3563 extensions can be either one of `the OpenCL published extensions
3564 <https://www.khronos.org/registry/OpenCL>`_
3565 or any vendor extension. Alternatively, ``'all'`` can be used to enable
3566 or disable all known extensions.
3568 Example disabling double support for the 64-bit SPIR-V target:
3570 .. code-block:: console
3572 $ clang -c --target=spirv64 -cl-ext=-cl_khr_fp64 test.cl
3574 Enabling all extensions except double support in R600 AMD GPU can be done using:
3576 .. code-block:: console
3578 $ clang --target=r600 -cl-ext=-all,+cl_khr_fp16 test.cl
3580 Note that some generic targets e.g. SPIR/SPIR-V enable all extensions/features in
3586 OpenCL targets are derived from the regular Clang target classes. The OpenCL
3587 specific parts of the target representation provide address space mapping as
3588 well as a set of supported extensions.
3593 There is a set of concrete HW architectures that OpenCL can be compiled for.
3597 .. code-block:: console
3599 $ clang --target=amdgcn-amd-amdhsa -mcpu=gfx900 test.cl
3601 - For Nvidia architectures:
3603 .. code-block:: console
3605 $ clang --target=nvptx64-unknown-unknown test.cl
3611 - A SPIR-V binary can be produced for 32 or 64 bit targets.
3613 .. code-block:: console
3615 $ clang --target=spirv32 -c test.cl
3616 $ clang --target=spirv64 -c test.cl
3618 More details can be found in :ref:`the SPIR-V support section <spir-v>`.
3620 - SPIR is available as a generic target to allow portable bitcode to be produced
3621 that can be used across GPU toolchains. The implementation follows `the SPIR
3622 specification <https://www.khronos.org/spir>`_. There are two flavors
3623 available for 32 and 64 bits.
3625 .. code-block:: console
3627 $ clang --target=spir test.cl -emit-llvm -c
3628 $ clang --target=spir64 test.cl -emit-llvm -c
3630 Clang will generate SPIR v1.2 compatible IR for OpenCL versions up to 2.0 and
3631 SPIR v2.0 for OpenCL v2.0 or C++ for OpenCL.
3633 - x86 is used by some implementations that are x86 compatible and currently
3634 remains for backwards compatibility (with older implementations prior to
3635 SPIR target support). For "non-SPMD" targets which cannot spawn multiple
3636 work-items on the fly using hardware, which covers practically all non-GPU
3637 devices such as CPUs and DSPs, additional processing is needed for the kernels
3638 to support multiple work-item execution. For this, a 3rd party toolchain,
3639 such as for example `POCL <http://portablecl.org/>`_, can be used.
3641 This target does not support multiple memory segments and, therefore, the fake
3642 address space map can be added using the :ref:`-ffake-address-space-map
3643 <opencl_fake_address_space_map>` flag.
3645 All known OpenCL extensions and features are set to supported in the generic targets,
3646 however :option:`-cl-ext` flag can be used to toggle individual extensions and
3654 By default Clang will include standard headers and therefore most of OpenCL
3655 builtin functions and types are available during compilation. The
3656 default declarations of non-native compiler types and functions can be disabled
3657 by using flag :option:`-cl-no-stdinc`.
3659 The following example demonstrates that OpenCL kernel sources with various
3660 standard builtin functions can be compiled without the need for an explicit
3661 includes or compiler flags.
3663 .. code-block:: console
3665 $ echo "bool is_wg_uniform(int i){return get_enqueued_local_size(i)==get_local_size(i);}" > test.cl
3666 $ clang -cl-std=CL2.0 test.cl
3668 More information about the default headers is provided in :doc:`OpenCLSupport`.
3673 Most of the ``cl_khr_*`` extensions to OpenCL C from `the official OpenCL
3674 registry <https://www.khronos.org/registry/OpenCL/>`_ are available and
3675 configured per target depending on the support available in the specific
3678 It is possible to alter the default extensions setting per target using
3679 ``-cl-ext`` flag. (See :ref:`flags description <opencl_cl_ext>` for more details).
3681 Vendor extensions can be added flexibly by declaring the list of types and
3682 functions associated with each extensions enclosed within the following
3683 compiler pragma directives:
3687 #pragma OPENCL EXTENSION the_new_extension_name : begin
3688 // declare types and functions associated with the extension here
3689 #pragma OPENCL EXTENSION the_new_extension_name : end
3691 For example, parsing the following code adds ``my_t`` type and ``my_func``
3692 function to the custom ``my_ext`` extension.
3696 #pragma OPENCL EXTENSION my_ext : begin
3701 #pragma OPENCL EXTENSION my_ext : end
3703 There is no conflict resolution for identifier clashes among extensions.
3704 It is therefore recommended that the identifiers are prefixed with a
3705 double underscore to avoid clashing with user space identifiers. Vendor
3706 extension should use reserved identifier prefix e.g. amd, arm, intel.
3708 Clang also supports language extensions documented in `The OpenCL C Language
3709 Extensions Documentation
3710 <https://github.com/KhronosGroup/Khronosdotorg/blob/main/api/opencl/assets/OpenCL_LangExt.pdf>`_.
3712 OpenCL-Specific Attributes
3713 --------------------------
3715 OpenCL support in Clang contains a set of attribute taken directly from the
3716 specification as well as additional attributes.
3718 See also :doc:`AttributeReference`.
3723 Clang supports this attribute to comply to OpenCL v2.0 conformance, but it
3724 does not have any effect on the IR. For more details reffer to the specification
3726 <https://www.khronos.org/registry/cl/specs/opencl-2.0-openclc.pdf#49>`_
3732 The implementation of this feature mirrors the unroll hint for C.
3733 More details on the syntax can be found in the specification
3735 <https://www.khronos.org/registry/cl/specs/opencl-2.0-openclc.pdf#61>`_
3740 To make sure no invalid optimizations occur for single program multiple data
3741 (SPMD) / single instruction multiple thread (SIMT) Clang provides attributes that
3742 can be used for special functions that have cross work item semantics.
3743 An example is the subgroup operations such as `intel_sub_group_shuffle
3744 <https://www.khronos.org/registry/cl/extensions/intel/cl_intel_subgroups.txt>`_
3748 // Define custom my_sub_group_shuffle(data, c)
3749 // that makes use of intel_sub_group_shuffle
3751 if (r0) r1 = computeA();
3752 // Shuffle data from r1 into r3
3753 // of threads id r2.
3754 r3 = my_sub_group_shuffle(r1, r2);
3755 if (r0) r3 = computeB();
3757 with non-SPMD semantics this is optimized to the following equivalent code:
3763 // Incorrect functionality! The data in r1
3764 // have not been computed by all threads yet.
3765 r3 = my_sub_group_shuffle(r1, r2);
3768 r3 = my_sub_group_shuffle(r1, r2);
3772 Declaring the function ``my_sub_group_shuffle`` with the convergent attribute
3777 my_sub_group_shuffle() __attribute__((convergent));
3779 Using ``convergent`` guarantees correct execution by keeping CFG equivalence
3780 wrt operations marked as ``convergent``. CFG ``G´`` is equivalent to ``G`` wrt
3781 node ``Ni`` : ``iff ∀ Nj (i≠j)`` domination and post-domination relations with
3782 respect to ``Ni`` remain the same in both ``G`` and ``G´``.
3787 ``noduplicate`` is more restrictive with respect to optimizations than
3788 ``convergent`` because a convergent function only preserves CFG equivalence.
3789 This allows some optimizations to happen as long as the control flow remains
3794 for (int i=0; i<4; i++)
3795 my_sub_group_shuffle()
3801 my_sub_group_shuffle();
3802 my_sub_group_shuffle();
3803 my_sub_group_shuffle();
3804 my_sub_group_shuffle();
3806 while using ``noduplicate`` would disallow this. Also ``noduplicate`` doesn't
3807 have the same safe semantics of CFG as ``convergent`` and can cause changes in
3808 CFG that modify semantics of the original program.
3810 ``noduplicate`` is kept for backwards compatibility only and it considered to be
3811 deprecated for future uses.
3818 Starting from clang 9 kernel code can contain C++17 features: classes, templates,
3819 function overloading, type deduction, etc. Please note that this is not an
3820 implementation of `OpenCL C++
3821 <https://www.khronos.org/registry/OpenCL/specs/2.2/pdf/OpenCL_Cxx.pdf>`_ and
3822 there is no plan to support it in clang in any new releases in the near future.
3824 Clang currently supports C++ for OpenCL 1.0 and 2021.
3825 For detailed information about this language refer to the C++ for OpenCL
3826 Programming Language Documentation available
3827 in `the latest build
3828 <https://www.khronos.org/opencl/assets/CXX_for_OpenCL.html>`_
3829 or in `the official release
3830 <https://github.com/KhronosGroup/OpenCL-Docs/releases/tag/cxxforopencl-docrev2021.12>`_.
3832 To enable the C++ for OpenCL mode, pass one of following command line options when
3833 compiling ``.clcpp`` file:
3835 - C++ for OpenCL 1.0: ``-cl-std=clc++``, ``-cl-std=CLC++``, ``-cl-std=clc++1.0``,
3836 ``-cl-std=CLC++1.0``, ``-std=clc++``, ``-std=CLC++``, ``-std=clc++1.0`` or
3839 - C++ for OpenCL 2021: ``-cl-std=clc++2021``, ``-cl-std=CLC++2021``,
3840 ``-std=clc++2021``, ``-std=CLC++2021``.
3845 template<class T> T add( T x, T y )
3850 __kernel void test( __global float* a, __global float* b)
3852 auto index = get_global_id(0);
3853 a[index] = add(b[index], b[index+1]);
3857 .. code-block:: console
3859 clang -cl-std=clc++1.0 test.clcpp
3860 clang -cl-std=clc++ -c --target=spirv64 test.cl
3863 By default, files with ``.clcpp`` extension are compiled with the C++ for
3866 .. code-block:: console
3870 For backward compatibility files with ``.cl`` extensions can also be compiled
3871 in C++ for OpenCL mode but the desirable language mode must be activated with
3874 .. code-block:: console
3876 clang -cl-std=clc++ test.cl
3878 Support of C++ for OpenCL 2021 is currently in experimental phase, refer to
3879 :doc:`OpenCLSupport` for more details.
3881 C++ for OpenCL kernel sources can also be compiled online in drivers supporting
3882 `cl_ext_cxx_for_opencl
3883 <https://www.khronos.org/registry/OpenCL/extensions/ext/cl_ext_cxx_for_opencl.html>`_
3886 Constructing and destroying global objects
3887 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3889 Global objects with non-trivial constructors require the constructors to be run
3890 before the first kernel using the global objects is executed. Similarly global
3891 objects with non-trivial destructors require destructor invocation just after
3892 the last kernel using the program objects is executed.
3893 In OpenCL versions earlier than v2.2 there is no support for invoking global
3894 constructors. However, an easy workaround is to manually enqueue the
3895 constructor initialization kernel that has the following name scheme
3896 ``_GLOBAL__sub_I_<compiled file name>``.
3897 This kernel is only present if there are global objects with non-trivial
3898 constructors present in the compiled binary. One way to check this is by
3899 passing ``CL_PROGRAM_KERNEL_NAMES`` to ``clGetProgramInfo`` (OpenCL v2.0
3900 s5.8.7) and then checking whether any kernel name matches the naming scheme of
3901 global constructor initialization kernel above.
3903 Note that if multiple files are compiled and linked into libraries, multiple
3904 kernels that initialize global objects for multiple modules would have to be
3907 Applications are currently required to run initialization of global objects
3908 manually before running any kernels in which the objects are used.
3910 .. code-block:: console
3912 clang -cl-std=clc++ test.cl
3914 If there are any global objects to be initialized, the final binary will
3915 contain the ``_GLOBAL__sub_I_test.cl`` kernel to be enqueued.
3917 Note that the manual workaround only applies to objects declared at the
3918 program scope. There is no manual workaround for the construction of static
3919 objects with non-trivial constructors inside functions.
3921 Global destructors can not be invoked manually in the OpenCL v2.0 drivers.
3922 However, all memory used for program scope objects should be released on
3923 ``clReleaseProgram``.
3927 Limited experimental support of C++ standard libraries for OpenCL is
3928 described in :doc:`OpenCLSupport` page.
3930 .. _target_features:
3932 Target-Specific Features and Limitations
3933 ========================================
3935 CPU Architectures Features and Limitations
3936 ------------------------------------------
3941 The support for X86 (both 32-bit and 64-bit) is considered stable on
3942 Darwin (macOS), Linux, FreeBSD, and Dragonfly BSD: it has been tested
3943 to correctly compile many large C, C++, Objective-C, and Objective-C++
3946 On ``x86_64-mingw32``, passing i128(by value) is incompatible with the
3947 Microsoft x64 calling convention. You might need to tweak
3948 ``WinX86_64ABIInfo::classify()`` in lib/CodeGen/Targets/X86.cpp.
3950 For the X86 target, clang supports the `-m16` command line
3951 argument which enables 16-bit code output. This is broadly similar to
3952 using ``asm(".code16gcc")`` with the GNU toolchain. The generated code
3953 and the ABI remains 32-bit but the assembler emits instructions
3954 appropriate for a CPU running in 16-bit mode, with address-size and
3955 operand-size prefixes to enable 32-bit addressing and operations.
3957 Several micro-architecture levels as specified by the x86-64 psABI are defined.
3958 They are cumulative in the sense that features from previous levels are
3959 implicitly included in later levels.
3961 - ``-march=x86-64``: CMOV, CMPXCHG8B, FPU, FXSR, MMX, FXSR, SCE, SSE, SSE2
3962 - ``-march=x86-64-v2``: (close to Nehalem) CMPXCHG16B, LAHF-SAHF, POPCNT, SSE3, SSE4.1, SSE4.2, SSSE3
3963 - ``-march=x86-64-v3``: (close to Haswell) AVX, AVX2, BMI1, BMI2, F16C, FMA, LZCNT, MOVBE, XSAVE
3964 - ``-march=x86-64-v4``: AVX512F, AVX512BW, AVX512CD, AVX512DQ, AVX512VL
3969 The support for ARM (specifically ARMv6 and ARMv7) is considered stable
3970 on Darwin (iOS): it has been tested to correctly compile many large C,
3971 C++, Objective-C, and Objective-C++ codebases. Clang only supports a
3972 limited number of ARM architectures. It does not yet fully support
3978 The support for PowerPC (especially PowerPC64) is considered stable
3979 on Linux and FreeBSD: it has been tested to correctly compile many
3980 large C and C++ codebases. PowerPC (32bit) is still missing certain
3981 features (e.g. PIC code on ELF platforms).
3986 clang currently contains some support for other architectures (e.g. Sparc);
3987 however, significant pieces of code generation are still missing, and they
3988 haven't undergone significant testing.
3990 clang contains limited support for the MSP430 embedded processor, but
3991 both the clang support and the LLVM backend support are highly
3994 Other platforms are completely unsupported at the moment. Adding the
3995 minimal support needed for parsing and semantic analysis on a new
3996 platform is quite easy; see ``lib/Basic/Targets.cpp`` in the clang source
3997 tree. This level of support is also sufficient for conversion to LLVM IR
3998 for simple programs. Proper support for conversion to LLVM IR requires
3999 adding code to ``lib/CodeGen/CGCall.cpp`` at the moment; this is likely to
4000 change soon, though. Generating assembly requires a suitable LLVM
4003 Operating System Features and Limitations
4004 -----------------------------------------
4009 Clang has experimental support for targeting "Cygming" (Cygwin / MinGW)
4012 See also :ref:`Microsoft Extensions <c_ms>`.
4017 Clang works on Cygwin-1.7.
4022 Clang works on some mingw32 distributions. Clang assumes directories as
4025 - ``C:/mingw/include``
4027 - ``C:/mingw/lib/gcc/mingw32/4.[3-5].0/include/c++``
4029 On MSYS, a few tests might fail.
4034 For 32-bit (i686-w64-mingw32), and 64-bit (x86\_64-w64-mingw32), Clang
4037 - ``GCC versions 4.5.0 to 4.5.3, 4.6.0 to 4.6.2, or 4.7.0 (for the C++ header search path)``
4038 - ``some_directory/bin/gcc.exe``
4039 - ``some_directory/bin/clang.exe``
4040 - ``some_directory/bin/clang++.exe``
4041 - ``some_directory/bin/../include/c++/GCC_version``
4042 - ``some_directory/bin/../include/c++/GCC_version/x86_64-w64-mingw32``
4043 - ``some_directory/bin/../include/c++/GCC_version/i686-w64-mingw32``
4044 - ``some_directory/bin/../include/c++/GCC_version/backward``
4045 - ``some_directory/bin/../x86_64-w64-mingw32/include``
4046 - ``some_directory/bin/../i686-w64-mingw32/include``
4047 - ``some_directory/bin/../include``
4049 This directory layout is standard for any toolchain you will find on the
4050 official `MinGW-w64 website <http://mingw-w64.sourceforge.net>`_.
4052 Clang expects the GCC executable "gcc.exe" compiled for
4053 ``i686-w64-mingw32`` (or ``x86_64-w64-mingw32``) to be present on PATH.
4055 `Some tests might fail <https://bugs.llvm.org/show_bug.cgi?id=9072>`_ on
4056 ``x86_64-w64-mingw32``.
4061 The ``-mdefault-visibility-export-mapping=`` option can be used to control
4062 mapping of default visibility to an explicit shared object export
4063 (i.e. XCOFF exported visibility). Three values are provided for the option:
4065 * ``-mdefault-visibility-export-mapping=none``: no additional export
4066 information is created for entities with default visibility.
4067 * ``-mdefault-visibility-export-mapping=explicit``: mark entities for export
4068 if they have explicit (e.g. via an attribute) default visibility from the
4069 source, including RTTI.
4070 * ``-mdefault-visibility-export-mapping=all``: set XCOFF exported visibility
4071 for all entities with default visibility from any source. This gives a
4072 export behavior similar to ELF platforms where all entities with default
4073 visibility are exported.
4080 Clang supports generation of SPIR-V conformant to `the OpenCL Environment
4082 <https://www.khronos.org/registry/OpenCL/specs/3.0-unified/html/OpenCL_Env.html>`_.
4084 To generate SPIR-V binaries, Clang uses the external ``llvm-spirv`` tool from the
4085 `SPIRV-LLVM-Translator repo
4086 <https://github.com/KhronosGroup/SPIRV-LLVM-Translator>`_.
4088 Prior to the generation of SPIR-V binary with Clang, ``llvm-spirv``
4089 should be built or installed. Please refer to `the following instructions
4090 <https://github.com/KhronosGroup/SPIRV-LLVM-Translator#build-instructions>`_
4091 for more details. Clang will expect the ``llvm-spirv`` executable to
4092 be present in the ``PATH`` environment variable. Clang uses ``llvm-spirv``
4093 with `the widely adopted assembly syntax package
4094 <https://github.com/KhronosGroup/SPIRV-LLVM-Translator/#build-with-spirv-tools>`_.
4097 <https://github.com/KhronosGroup/SPIRV-LLVM-Translator/releases>`_ of
4098 ``llvm-spirv`` is aligned with Clang major releases. The same applies to the
4099 main development branch. It is therefore important to ensure the ``llvm-spirv``
4100 version is in alignment with the Clang version. For troubleshooting purposes
4101 ``llvm-spirv`` can be `tested in isolation
4102 <https://github.com/KhronosGroup/SPIRV-LLVM-Translator#test-instructions>`_.
4104 Example usage for OpenCL kernel compilation:
4106 .. code-block:: console
4108 $ clang --target=spirv32 -c test.cl
4109 $ clang --target=spirv64 -c test.cl
4111 Both invocations of Clang will result in the generation of a SPIR-V binary file
4112 `test.o` for 32 bit and 64 bit respectively. This file can be imported
4113 by an OpenCL driver that support SPIR-V consumption or it can be compiled
4114 further by offline SPIR-V consumer tools.
4116 Converting to SPIR-V produced with the optimization levels other than `-O0` is
4117 currently available as an experimental feature and it is not guaranteed to work
4120 Clang also supports integrated generation of SPIR-V without use of ``llvm-spirv``
4121 tool as an experimental feature when ``-fintegrated-objemitter`` flag is passed in
4124 .. code-block:: console
4126 $ clang --target=spirv32 -fintegrated-objemitter -c test.cl
4128 Note that only very basic functionality is supported at this point and therefore
4129 it is not suitable for arbitrary use cases. This feature is only enabled when clang
4130 build is configured with ``-DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD=SPIRV`` option.
4132 Linking is done using ``spirv-link`` from `the SPIRV-Tools project
4133 <https://github.com/KhronosGroup/SPIRV-Tools#linker>`_. Similar to other external
4134 linkers, Clang will expect ``spirv-link`` to be installed separately and to be
4135 present in the ``PATH`` environment variable. Please refer to `the build and
4136 installation instructions
4137 <https://github.com/KhronosGroup/SPIRV-Tools#build>`_.
4139 .. code-block:: console
4141 $ clang --target=spirv64 test1.cl test2.cl
4143 More information about the SPIR-V target settings and supported versions of SPIR-V
4144 format can be found in `the SPIR-V target guide
4145 <https://llvm.org/docs/SPIRVUsage.html>`__.
4152 clang-cl is an alternative command-line interface to Clang, designed for
4153 compatibility with the Visual C++ compiler, cl.exe.
4155 To enable clang-cl to find system headers, libraries, and the linker when run
4156 from the command-line, it should be executed inside a Visual Studio Native Tools
4157 Command Prompt or a regular Command Prompt where the environment has been set
4158 up using e.g. `vcvarsall.bat <https://msdn.microsoft.com/en-us/library/f2ccy3wt.aspx>`_.
4160 clang-cl can also be used from inside Visual Studio by selecting the LLVM
4161 Platform Toolset. The toolset is not part of the installer, but may be installed
4163 `Visual Studio Marketplace <https://marketplace.visualstudio.com/items?itemName=LLVMExtensions.llvm-toolchain>`_.
4164 To use the toolset, select a project in Solution Explorer, open its Property
4165 Page (Alt+F7), and in the "General" section of "Configuration Properties"
4166 change "Platform Toolset" to LLVM. Doing so enables an additional Property
4167 Page for selecting the clang-cl executable to use for builds.
4169 To use the toolset with MSBuild directly, invoke it with e.g.
4170 ``/p:PlatformToolset=LLVM``. This allows trying out the clang-cl toolchain
4171 without modifying your project files.
4173 It's also possible to point MSBuild at clang-cl without changing toolset by
4174 passing ``/p:CLToolPath=c:\llvm\bin /p:CLToolExe=clang-cl.exe``.
4176 When using CMake and the Visual Studio generators, the toolset can be set with the ``-T`` flag:
4180 cmake -G"Visual Studio 16 2019" -T LLVM ..
4182 When using CMake with the Ninja generator, set the ``CMAKE_C_COMPILER`` and
4183 ``CMAKE_CXX_COMPILER`` variables to clang-cl:
4187 cmake -GNinja -DCMAKE_C_COMPILER="c:/Program Files (x86)/LLVM/bin/clang-cl.exe"
4188 -DCMAKE_CXX_COMPILER="c:/Program Files (x86)/LLVM/bin/clang-cl.exe" ..
4191 Command-Line Options
4192 --------------------
4194 To be compatible with cl.exe, clang-cl supports most of the same command-line
4195 options. Those options can start with either ``/`` or ``-``. It also supports
4196 some of Clang's core options, such as the ``-W`` options.
4198 Options that are known to clang-cl, but not currently supported, are ignored
4199 with a warning. For example:
4203 clang-cl.exe: warning: argument unused during compilation: '/AI'
4205 To suppress warnings about unused arguments, use the ``-Qunused-arguments`` option.
4207 Options that are not known to clang-cl will be ignored by default. Use the
4208 ``-Werror=unknown-argument`` option in order to treat them as errors. If these
4209 options are spelled with a leading ``/``, they will be mistaken for a filename:
4213 clang-cl.exe: error: no such file or directory: '/foobar'
4215 Please `file a bug <https://github.com/llvm/llvm-project/issues/new?labels=clang-cl>`_
4216 for any valid cl.exe flags that clang-cl does not understand.
4218 Execute ``clang-cl /?`` to see a list of supported options:
4222 CL.EXE COMPATIBILITY OPTIONS:
4223 /? Display available options
4224 /arch:<value> Set architecture for code generation
4225 /Brepro- Emit an object file which cannot be reproduced over time
4226 /Brepro Emit an object file which can be reproduced over time
4227 /clang:<arg> Pass <arg> to the clang driver
4228 /C Don't discard comments when preprocessing
4230 /d1PP Retain macro definitions in /E mode
4231 /d1reportAllClassLayout Dump record layout information
4232 /diagnostics:caret Enable caret and column diagnostics (on by default)
4233 /diagnostics:classic Disable column and caret diagnostics
4234 /diagnostics:column Disable caret diagnostics but keep column info
4235 /D <macro[=value]> Define macro
4236 /EH<value> Exception handling model
4237 /EP Disable linemarker output and preprocess to stdout
4238 /execution-charset:<value>
4239 Runtime encoding, supports only UTF-8
4240 /E Preprocess to stdout
4241 /FA Output assembly code file during compilation
4242 /Fa<file or directory> Output assembly code to this file during compilation (with /FA)
4243 /Fe<file or directory> Set output executable file or directory (ends in / or \)
4244 /FI <value> Include file before parsing
4245 /Fi<file> Set preprocess output file name (with /P)
4246 /Fo<file or directory> Set output object file, or directory (ends in / or \) (with /c)
4252 /Fp<filename> Set pch filename (with /Yc and /Yu)
4253 /GA Assume thread-local variables are defined in the executable
4254 /Gd Set __cdecl as a default calling convention
4255 /GF- Disable string pooling
4256 /GF Enable string pooling (default)
4257 /GR- Disable emission of RTTI data
4258 /Gregcall Set __regcall as a default calling convention
4259 /GR Enable emission of RTTI data
4260 /Gr Set __fastcall as a default calling convention
4261 /GS- Disable buffer security check
4262 /GS Enable buffer security check (default)
4263 /Gs Use stack probes (default)
4264 /Gs<value> Set stack probe size (default 4096)
4265 /guard:<value> Enable Control Flow Guard with /guard:cf,
4266 or only the table with /guard:cf,nochecks.
4267 Enable EH Continuation Guard with /guard:ehcont
4268 /Gv Set __vectorcall as a default calling convention
4269 /Gw- Don't put each data item in its own section
4270 /Gw Put each data item in its own section
4271 /GX- Disable exception handling
4272 /GX Enable exception handling
4273 /Gy- Don't put each function in its own section (default)
4274 /Gy Put each function in its own section
4275 /Gz Set __stdcall as a default calling convention
4276 /help Display available options
4277 /imsvc <dir> Add directory to system include search path, as if part of %INCLUDE%
4278 /I <dir> Add directory to include search path
4279 /J Make char type unsigned
4280 /LDd Create debug DLL
4282 /link <options> Forward options to the linker
4283 /MDd Use DLL debug run-time
4284 /MD Use DLL run-time
4285 /MTd Use static debug run-time
4286 /MT Use static run-time
4287 /O0 Disable optimization
4288 /O1 Optimize for size (same as /Og /Os /Oy /Ob2 /GF /Gy)
4289 /O2 Optimize for speed (same as /Og /Oi /Ot /Oy /Ob2 /GF /Gy)
4290 /Ob0 Disable function inlining
4291 /Ob1 Only inline functions which are (explicitly or implicitly) marked inline
4292 /Ob2 Inline functions as deemed beneficial by the compiler
4293 /Od Disable optimization
4295 /Oi- Disable use of builtin functions
4296 /Oi Enable use of builtin functions
4297 /Os Optimize for size
4298 /Ot Optimize for speed
4299 /Ox Deprecated (same as /Og /Oi /Ot /Oy /Ob2); use /O2 instead
4300 /Oy- Disable frame pointer omission (x86 only, default)
4301 /Oy Enable frame pointer omission (x86 only)
4302 /O<flags> Set multiple /O flags at once; e.g. '/O2y-' for '/O2 /Oy-'
4303 /o <file or directory> Set output file or directory (ends in / or \)
4304 /P Preprocess to file
4305 /Qvec- Disable the loop vectorization passes
4306 /Qvec Enable the loop vectorization passes
4307 /showFilenames- Don't print the name of each compiled file (default)
4308 /showFilenames Print the name of each compiled file
4309 /showIncludes Print info about included files to stderr
4310 /source-charset:<value> Source encoding, supports only UTF-8
4311 /std:<value> Language standard to compile for
4312 /TC Treat all source files as C
4313 /Tc <filename> Specify a C source file
4314 /TP Treat all source files as C++
4315 /Tp <filename> Specify a C++ source file
4316 /utf-8 Set source and runtime encoding to UTF-8 (default)
4317 /U <macro> Undefine macro
4318 /vd<value> Control vtordisp placement
4319 /vmb Use a best-case representation method for member pointers
4320 /vmg Use a most-general representation for member pointers
4321 /vmm Set the default most-general representation to multiple inheritance
4322 /vms Set the default most-general representation to single inheritance
4323 /vmv Set the default most-general representation to virtual inheritance
4324 /volatile:iso Volatile loads and stores have standard semantics
4325 /volatile:ms Volatile loads and stores have acquire and release semantics
4326 /W0 Disable all warnings
4330 /W4 Enable -Wall and -Wextra
4331 /Wall Enable -Weverything
4332 /WX- Do not treat warnings as errors
4333 /WX Treat warnings as errors
4334 /w Disable all warnings
4335 /X Don't add %INCLUDE% to the include search path
4336 /Y- Disable precompiled headers, overrides /Yc and /Yu
4337 /Yc<filename> Generate a pch file for all code up to and including <filename>
4338 /Yu<filename> Load a pch file and use it instead of all code up to and including <filename>
4339 /Z7 Enable CodeView debug information in object files
4340 /Zc:char8_t Enable C++20 char8_t type
4341 /Zc:char8_t- Disable C++20 char8_t type
4342 /Zc:dllexportInlines- Don't dllexport/dllimport inline member functions of dllexport/import classes
4343 /Zc:dllexportInlines dllexport/dllimport inline member functions of dllexport/import classes (default)
4344 /Zc:sizedDealloc- Disable C++14 sized global deallocation functions
4345 /Zc:sizedDealloc Enable C++14 sized global deallocation functions
4346 /Zc:strictStrings Treat string literals as const
4347 /Zc:threadSafeInit- Disable thread-safe initialization of static variables
4348 /Zc:threadSafeInit Enable thread-safe initialization of static variables
4349 /Zc:trigraphs- Disable trigraphs (default)
4350 /Zc:trigraphs Enable trigraphs
4351 /Zc:twoPhase- Disable two-phase name lookup in templates
4352 /Zc:twoPhase Enable two-phase name lookup in templates
4353 /Zi Alias for /Z7. Does not produce PDBs.
4354 /Zl Don't mention any default libraries in the object file
4355 /Zp Set the default maximum struct packing alignment to 1
4356 /Zp<value> Specify the default maximum struct packing alignment
4357 /Zs Run the preprocessor, parser and semantic analysis stages
4360 -### Print (but do not run) the commands to run for this compilation
4361 --analyze Run the static analyzer
4362 -faddrsig Emit an address-significance table
4363 -fansi-escape-codes Use ANSI escape codes for diagnostics
4364 -fblocks Enable the 'blocks' language feature
4365 -fcf-protection=<value> Instrument control-flow architecture protection. Options: return, branch, full, none.
4366 -fcf-protection Enable cf-protection in 'full' mode
4367 -fcolor-diagnostics Use colors in diagnostics
4368 -fcomplete-member-pointers
4369 Require member pointer base types to be complete if they would be significant under the Microsoft ABI
4370 -fcoverage-mapping Generate coverage mapping to enable code coverage analysis
4371 -fcrash-diagnostics-dir=<dir>
4372 Put crash-report files in <dir>
4373 -fdebug-macro Emit macro debug information
4374 -fdelayed-template-parsing
4375 Parse templated function definitions at the end of the translation unit
4376 -fdiagnostics-absolute-paths
4377 Print absolute paths in diagnostics
4378 -fdiagnostics-parseable-fixits
4379 Print fix-its in machine parseable form
4380 -flto=<value> Set LTO mode to either 'full' or 'thin'
4381 -flto Enable LTO in 'full' mode
4382 -fmerge-all-constants Allow merging of constants
4383 -fms-compatibility-version=<value>
4384 Dot-separated value representing the Microsoft compiler version
4385 number to report in _MSC_VER (0 = don't define it (default))
4386 -fms-compatibility Enable full Microsoft Visual C++ compatibility
4387 -fms-extensions Accept some non-standard constructs supported by the Microsoft compiler
4388 -fmsc-version=<value> Microsoft compiler version number to report in _MSC_VER
4389 (0 = don't define it (default))
4390 -fno-addrsig Don't emit an address-significance table
4391 -fno-builtin-<value> Disable implicit builtin knowledge of a specific function
4392 -fno-builtin Disable implicit builtin knowledge of functions
4393 -fno-complete-member-pointers
4394 Do not require member pointer base types to be complete if they would be significant under the Microsoft ABI
4395 -fno-coverage-mapping Disable code coverage analysis
4396 -fno-crash-diagnostics Disable auto-generation of preprocessed source files and a script for reproduction during a clang crash
4397 -fno-debug-macro Do not emit macro debug information
4398 -fno-delayed-template-parsing
4399 Disable delayed template parsing
4400 -fno-sanitize-address-poison-custom-array-cookie
4401 Disable poisoning array cookies when using custom operator new[] in AddressSanitizer
4402 -fno-sanitize-address-use-after-scope
4403 Disable use-after-scope detection in AddressSanitizer
4404 -fno-sanitize-address-use-odr-indicator
4405 Disable ODR indicator globals
4406 -fno-sanitize-ignorelist Don't use ignorelist file for sanitizers
4407 -fno-sanitize-cfi-cross-dso
4408 Disable control flow integrity (CFI) checks for cross-DSO calls.
4409 -fno-sanitize-coverage=<value>
4410 Disable specified features of coverage instrumentation for Sanitizers
4411 -fno-sanitize-memory-track-origins
4412 Disable origins tracking in MemorySanitizer
4413 -fno-sanitize-memory-use-after-dtor
4414 Disable use-after-destroy detection in MemorySanitizer
4415 -fno-sanitize-recover=<value>
4416 Disable recovery for specified sanitizers
4417 -fno-sanitize-stats Disable sanitizer statistics gathering.
4418 -fno-sanitize-thread-atomics
4419 Disable atomic operations instrumentation in ThreadSanitizer
4420 -fno-sanitize-thread-func-entry-exit
4421 Disable function entry/exit instrumentation in ThreadSanitizer
4422 -fno-sanitize-thread-memory-access
4423 Disable memory access instrumentation in ThreadSanitizer
4424 -fno-sanitize-trap=<value>
4425 Disable trapping for specified sanitizers
4426 -fno-standalone-debug Limit debug information produced to reduce size of debug binary
4427 -fno-strict-aliasing Disable optimizations based on strict aliasing rules (default)
4428 -fobjc-runtime=<value> Specify the target Objective-C runtime kind and version
4429 -fprofile-exclude-files=<value>
4430 Instrument only functions from files where names don't match all the regexes separated by a semi-colon
4431 -fprofile-filter-files=<value>
4432 Instrument only functions from files where names match any regex separated by a semi-colon
4433 -fprofile-generate=<dirname>
4434 Generate instrumented code to collect execution counts into a raw profile file in the directory specified by the argument. The filename uses default_%m.profraw pattern
4435 (overridden by LLVM_PROFILE_FILE env var)
4437 Generate instrumented code to collect execution counts into default_%m.profraw file
4438 (overridden by '=' form of option or LLVM_PROFILE_FILE env var)
4439 -fprofile-instr-generate=<file_name_pattern>
4440 Generate instrumented code to collect execution counts into the file whose name pattern is specified as the argument
4441 (overridden by LLVM_PROFILE_FILE env var)
4442 -fprofile-instr-generate
4443 Generate instrumented code to collect execution counts into default.profraw file
4444 (overridden by '=' form of option or LLVM_PROFILE_FILE env var)
4445 -fprofile-instr-use=<value>
4446 Use instrumentation data for coverage testing or profile-guided optimization
4447 -fprofile-use=<value>
4448 Use instrumentation data for profile-guided optimization
4449 -fprofile-remapping-file=<file>
4450 Use the remappings described in <file> to match the profile data against names in the program
4451 -fprofile-list=<file>
4452 Filename defining the list of functions/files to instrument
4453 -fsanitize-address-field-padding=<value>
4454 Level of field padding for AddressSanitizer
4455 -fsanitize-address-globals-dead-stripping
4456 Enable linker dead stripping of globals in AddressSanitizer
4457 -fsanitize-address-poison-custom-array-cookie
4458 Enable poisoning array cookies when using custom operator new[] in AddressSanitizer
4459 -fsanitize-address-use-after-return=<mode>
4460 Select the mode of detecting stack use-after-return in AddressSanitizer: never | runtime (default) | always
4461 -fsanitize-address-use-after-scope
4462 Enable use-after-scope detection in AddressSanitizer
4463 -fsanitize-address-use-odr-indicator
4464 Enable ODR indicator globals to avoid false ODR violation reports in partially sanitized programs at the cost of an increase in binary size
4465 -fsanitize-ignorelist=<value>
4466 Path to ignorelist file for sanitizers
4467 -fsanitize-cfi-cross-dso
4468 Enable control flow integrity (CFI) checks for cross-DSO calls.
4469 -fsanitize-cfi-icall-generalize-pointers
4470 Generalize pointers in CFI indirect call type signature checks
4471 -fsanitize-coverage=<value>
4472 Specify the type of coverage instrumentation for Sanitizers
4473 -fsanitize-hwaddress-abi=<value>
4474 Select the HWAddressSanitizer ABI to target (interceptor or platform, default interceptor)
4475 -fsanitize-memory-track-origins=<value>
4476 Enable origins tracking in MemorySanitizer
4477 -fsanitize-memory-track-origins
4478 Enable origins tracking in MemorySanitizer
4479 -fsanitize-memory-use-after-dtor
4480 Enable use-after-destroy detection in MemorySanitizer
4481 -fsanitize-recover=<value>
4482 Enable recovery for specified sanitizers
4483 -fsanitize-stats Enable sanitizer statistics gathering.
4484 -fsanitize-thread-atomics
4485 Enable atomic operations instrumentation in ThreadSanitizer (default)
4486 -fsanitize-thread-func-entry-exit
4487 Enable function entry/exit instrumentation in ThreadSanitizer (default)
4488 -fsanitize-thread-memory-access
4489 Enable memory access instrumentation in ThreadSanitizer (default)
4490 -fsanitize-trap=<value> Enable trapping for specified sanitizers
4491 -fsanitize-undefined-strip-path-components=<number>
4492 Strip (or keep only, if negative) a given number of path components when emitting check metadata.
4493 -fsanitize=<check> Turn on runtime checks for various forms of undefined or suspicious
4494 behavior. See user manual for available checks
4495 -fsplit-lto-unit Enables splitting of the LTO unit.
4496 -fstandalone-debug Emit full debug info for all types used by the program
4497 -fstrict-aliasing Enable optimizations based on strict aliasing rules
4498 -fsyntax-only Run the preprocessor, parser and semantic analysis stages
4499 -fwhole-program-vtables Enables whole-program vtable optimization. Requires -flto
4500 -gcodeview-ghash Emit type record hashes in a .debug$H section
4501 -gcodeview Generate CodeView debug information
4502 -gline-directives-only Emit debug line info directives only
4503 -gline-tables-only Emit debug line number tables only
4504 -miamcu Use Intel MCU ABI
4505 -mllvm <value> Additional arguments to forward to LLVM's option processing
4506 -nobuiltininc Disable builtin #include directories
4507 -Qunused-arguments Don't emit warning for unused driver arguments
4508 -R<remark> Enable the specified remark
4509 --target=<value> Generate code for the given target
4510 --version Print version information
4511 -v Show commands to run and use verbose output
4512 -W<warning> Enable the specified warning
4513 -Xclang <arg> Pass <arg> to the clang compiler
4518 When clang-cl is run with a set of ``/clang:<arg>`` options, it will gather all
4519 of the ``<arg>`` arguments and process them as if they were passed to the clang
4520 driver. This mechanism allows you to pass flags that are not exposed in the
4521 clang-cl options or flags that have a different meaning when passed to the clang
4522 driver. Regardless of where they appear in the command line, the ``/clang:``
4523 arguments are treated as if they were passed at the end of the clang-cl command
4526 The /Zc:dllexportInlines- Option
4527 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4529 This causes the class-level `dllexport` and `dllimport` attributes to not apply
4530 to inline member functions, as they otherwise would. For example, in the code
4531 below `S::foo()` would normally be defined and exported by the DLL, but when
4532 using the ``/Zc:dllexportInlines-`` flag it is not:
4536 struct __declspec(dllexport) S {
4540 This has the benefit that the compiler doesn't need to emit a definition of
4541 `S::foo()` in every translation unit where the declaration is included, as it
4542 would otherwise do to ensure there's a definition in the DLL even if it's not
4543 used there. If the declaration occurs in a header file that's widely used, this
4544 can save significant compilation time and output size. It also reduces the
4545 number of functions exported by the DLL similarly to what
4546 ``-fvisibility-inlines-hidden`` does for shared objects on ELF and Mach-O.
4547 Since the function declaration comes with an inline definition, users of the
4548 library can use that definition directly instead of importing it from the DLL.
4550 Note that the Microsoft Visual C++ compiler does not support this option, and
4551 if code in a DLL is compiled with ``/Zc:dllexportInlines-``, the code using the
4552 DLL must be compiled in the same way so that it doesn't attempt to dllimport
4553 the inline member functions. The reverse scenario should generally work though:
4554 a DLL compiled without this flag (such as a system library compiled with Visual
4555 C++) can be referenced from code compiled using the flag, meaning that the
4556 referencing code will use the inline definitions instead of importing them from
4559 Also note that like when using ``-fvisibility-inlines-hidden``, the address of
4560 `S::foo()` will be different inside and outside the DLL, breaking the C/C++
4561 standard requirement that functions have a unique address.
4563 The flag does not apply to explicit class template instantiation definitions or
4564 declarations, as those are typically used to explicitly provide a single
4565 definition in a DLL, (dllexported instantiation definition) or to signal that
4566 the definition is available elsewhere (dllimport instantiation declaration). It
4567 also doesn't apply to inline members with static local variables, to ensure
4568 that the same instance of the variable is used inside and outside the DLL.
4570 Using this flag can cause problems when inline functions that would otherwise
4571 be dllexported refer to internal symbols of a DLL. For example:
4577 struct __declspec(dllimport) S {
4578 void foo() { internal(); }
4581 Normally, references to `S::foo()` would use the definition in the DLL from
4582 which it was exported, and which presumably also has the definition of
4583 `internal()`. However, when using ``/Zc:dllexportInlines-``, the inline
4584 definition of `S::foo()` is used directly, resulting in a link error since
4585 `internal()` is not available. Even worse, if there is an inline definition of
4586 `internal()` containing a static local variable, we will now refer to a
4587 different instance of that variable than in the DLL:
4591 inline int internal() { static int x; return x++; }
4593 struct __declspec(dllimport) S {
4594 int foo() { return internal(); }
4597 This could lead to very subtle bugs. Using ``-fvisibility-inlines-hidden`` can
4598 lead to the same issue. To avoid it in this case, make `S::foo()` or
4599 `internal()` non-inline, or mark them `dllimport/dllexport` explicitly.
4601 Finding Clang runtime libraries
4602 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4604 clang-cl supports several features that require runtime library support:
4606 - Address Sanitizer (ASan): ``-fsanitize=address``
4607 - Undefined Behavior Sanitizer (UBSan): ``-fsanitize=undefined``
4608 - Code coverage: ``-fprofile-instr-generate -fcoverage-mapping``
4609 - Profile Guided Optimization (PGO): ``-fprofile-generate``
4610 - Certain math operations (int128 division) require the builtins library
4612 In order to use these features, the user must link the right runtime libraries
4613 into their program. These libraries are distributed alongside Clang in the
4614 library resource directory. Clang searches for the resource directory by
4615 searching relative to the Clang executable. For example, if LLVM is installed
4616 in ``C:\Program Files\LLVM``, then the profile runtime library will be located
4618 ``C:\Program Files\LLVM\lib\clang\11.0.0\lib\windows\clang_rt.profile-x86_64.lib``.
4620 For UBSan, PGO, and coverage, Clang will emit object files that auto-link the
4621 appropriate runtime library, but the user generally needs to help the linker
4622 (whether it is ``lld-link.exe`` or MSVC ``link.exe``) find the library resource
4623 directory. Using the example installation above, this would mean passing
4624 ``/LIBPATH:C:\Program Files\LLVM\lib\clang\11.0.0\lib\windows`` to the linker.
4625 If the user links the program with the ``clang`` or ``clang-cl`` drivers, the
4626 driver will pass this flag for them.
4628 If the linker cannot find the appropriate library, it will emit an error like
4631 $ clang-cl -c -fsanitize=undefined t.cpp
4633 $ lld-link t.obj -dll
4634 lld-link: error: could not open 'clang_rt.ubsan_standalone-x86_64.lib': no such file or directory
4635 lld-link: error: could not open 'clang_rt.ubsan_standalone_cxx-x86_64.lib': no such file or directory
4637 $ link t.obj -dll -nologo
4638 LINK : fatal error LNK1104: cannot open file 'clang_rt.ubsan_standalone-x86_64.lib'
4640 To fix the error, add the appropriate ``/libpath:`` flag to the link line.
4642 For ASan, as of this writing, the user is also responsible for linking against
4643 the correct ASan libraries.
4645 If the user is using the dynamic CRT (``/MD``), then they should add
4646 ``clang_rt.asan_dynamic-x86_64.lib`` to the link line as a regular input. For
4647 other architectures, replace x86_64 with the appropriate name here and below.
4649 If the user is using the static CRT (``/MT``), then different runtimes are used
4650 to produce DLLs and EXEs. To link a DLL, pass
4651 ``clang_rt.asan_dll_thunk-x86_64.lib``. To link an EXE, pass
4652 ``-wholearchive:clang_rt.asan-x86_64.lib``.
4654 Windows System Headers and Library Lookup
4655 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4657 clang-cl uses a set of different approaches to locate the right system libraries
4658 to link against when building code. The Windows environment uses libraries from
4659 three distinct sources:
4662 2. UCRT (Universal C Runtime)
4663 3. Visual C++ Tools (VCRuntime)
4665 The Windows SDK provides the import libraries and headers required to build
4666 programs against the Windows system packages. Underlying the Windows SDK is the
4667 UCRT, the universal C runtime.
4669 This difference is best illustrated by the various headers that one would find
4670 in the different categories. The WinSDK would contain headers such as
4671 `WinSock2.h` which is part of the Windows API surface, providing the Windows
4672 socketing interfaces for networking. UCRT provides the C library headers,
4673 including e.g. `stdio.h`. Finally, the Visual C++ tools provides the underlying
4674 Visual C++ Runtime headers such as `stdint.h` or `crtdefs.h`.
4676 There are various controls that allow the user control over where clang-cl will
4677 locate these headers. The default behaviour for the Windows SDK and UCRT is as
4680 1. Consult the command line.
4682 Anything the user specifies is always given precedence. The following
4683 extensions are part of the clang-cl toolset:
4687 The `/winsysroot:` is used as an equivalent to `-sysroot` on Unix
4688 environments. It allows the control of an alternate location to be treated
4689 as a system root. When specified, it will be used as the root where the
4690 `Windows Kits` is located.
4695 If `/winsysroot:` is not specified, the `/winsdkdir:` argument is consulted
4696 as a location to identify where the Windows SDK is located. Contrary to
4697 `/winsysroot:`, `/winsdkdir:` is expected to be the complete path rather
4698 than a root to locate `Windows Kits`.
4700 The `/winsdkversion:` flag allows the user to specify a version identifier
4701 for the SDK to prefer. When this is specified, no additional validation is
4702 performed and this version is preferred. If the version is not specified,
4703 the highest detected version number will be used.
4705 2. Consult the environment.
4707 TODO: This is not yet implemented.
4709 This will consult the environment variables:
4714 3. Fallback to the registry.
4716 If no arguments are used to indicate where the SDK is present, and the
4717 compiler is running on Windows, the registry is consulted to locate the
4720 The Visual C++ Toolset has a slightly more elaborate mechanism for detection.
4722 1. Consult the command line.
4726 The `/winsysroot:` is used as an equivalent to `-sysroot` on Unix
4727 environments. It allows the control of an alternate location to be treated
4728 as a system root. When specified, it will be used as the root where the
4729 `VC` directory is located.
4732 - `/vctoolsversion:`
4734 If `/winsysroot:` is not specified, the `/vctoolsdir:` argument is consulted
4735 as a location to identify where the Visual C++ Tools are located. If
4736 `/vctoolsversion:` is specified, that version is preferred, otherwise, the
4737 highest version detected is used.
4739 2. Consult the environment.
4741 - `/external:[VARIABLE]`
4743 This specifies a user identified environment variable which is treated as
4744 a path delimiter (`;`) separated list of paths to map into `-imsvc`
4745 arguments which are treated as `-isystem`.
4747 - `INCLUDE` and `EXTERNAL_INCLUDE`
4749 The path delimiter (`;`) separated list of paths will be mapped to
4750 `-imsvc` arguments which are treated as `-isystem`.
4752 - `LIB` (indirectly)
4754 The linker `link.exe` or `lld-link.exe` will honour the environment
4755 variable `LIB` which is a path delimiter (`;`) set of paths to consult for
4756 the import libraries to use when linking the final target.
4758 The following environment variables will be consulted and used to form paths
4759 to validate and load content from as appropriate:
4761 - `VCToolsInstallDir`
4765 3. Consult `ISetupConfiguration` [Windows Only]
4767 Assuming that the toolchain is built with `USE_MSVC_SETUP_API` defined and
4768 is running on Windows, the Visual Studio COM interface `ISetupConfiguration`
4769 will be used to locate the installation of the MSVC toolset.
4771 4. Fallback to the registry [DEPRECATED]
4773 The registry information is used to help locate the installation as a final
4774 fallback. This is only possible for pre-VS2017 installations and is
4775 considered deprecated.
4777 Restrictions and Limitations compared to Clang
4778 ----------------------------------------------
4783 Strict aliasing (TBAA) is always off by default in clang-cl. Whereas in clang,
4784 strict aliasing is turned on by default for all optimization levels.
4786 To enable LLVM optimizations based on strict aliasing rules (e.g., optimizations
4787 based on type of expressions in C/C++), user will need to explicitly pass
4788 `-fstrict-aliasing` to clang-cl.