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 v1.0, v1.1, v1.2, v2.0,
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 **-f[no-]show-column**
169 Print column number in diagnostic.
171 This option, which defaults to on, controls whether or not Clang
172 prints the column number of a diagnostic. For example, when this is
173 enabled, Clang will print something like:
177 test.c:28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens]
182 When this is disabled, Clang will print "test.c:28: warning..." with
185 The printed column numbers count bytes from the beginning of the
186 line; take care if your source contains multibyte characters.
188 .. _opt_fshow-source-location:
190 **-f[no-]show-source-location**
191 Print source file/line/column information in diagnostic.
193 This option, which defaults to on, controls whether or not Clang
194 prints the filename, line number and column number of a diagnostic.
195 For example, when this is enabled, Clang will print something like:
199 test.c:28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens]
204 When this is disabled, Clang will not print the "test.c:28:8: "
207 .. _opt_fcaret-diagnostics:
209 **-f[no-]caret-diagnostics**
210 Print source line and ranges from source code in diagnostic.
211 This option, which defaults to on, controls whether or not Clang
212 prints the source line, source ranges, and caret when emitting a
213 diagnostic. For example, when this is enabled, Clang will print
218 test.c:28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens]
223 **-f[no-]color-diagnostics**
224 This option, which defaults to on when a color-capable terminal is
225 detected, controls whether or not Clang prints diagnostics in color.
227 When this option is enabled, Clang will use colors to highlight
228 specific parts of the diagnostic, e.g.,
230 .. nasty hack to not lose our dignity
235 <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>
237 <span style="color:green">^</span>
238 <span style="color:green">//</span>
241 When this is disabled, Clang will just print:
245 test.c:2:8: warning: extra tokens at end of #endif directive [-Wextra-tokens]
250 **-fansi-escape-codes**
251 Controls whether ANSI escape codes are used instead of the Windows Console
252 API to output colored diagnostics. This option is only used on Windows and
255 .. option:: -fdiagnostics-format=clang/msvc/vi
257 Changes diagnostic output format to better match IDEs and command line tools.
259 This option controls the output format of the filename, line number,
260 and column printed in diagnostic messages. The options, and their
261 affect on formatting a simple conversion diagnostic, follow:
266 t.c:3:11: warning: conversion specifies type 'char *' but the argument has type 'int'
271 t.c(3,11) : warning: conversion specifies type 'char *' but the argument has type 'int'
276 t.c +3:11: warning: conversion specifies type 'char *' but the argument has type 'int'
278 .. _opt_fdiagnostics-show-option:
280 **-f[no-]diagnostics-show-option**
281 Enable ``[-Woption]`` information in diagnostic line.
283 This option, which defaults to on, controls whether or not Clang
284 prints the associated :ref:`warning group <cl_diag_warning_groups>`
285 option name when outputting a warning diagnostic. For example, in
290 test.c:28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens]
295 Passing **-fno-diagnostics-show-option** will prevent Clang from
296 printing the [:ref:`-Wextra-tokens <opt_Wextra-tokens>`] information in
297 the diagnostic. This information tells you the flag needed to enable
298 or disable the diagnostic, either from the command line or through
299 :ref:`#pragma GCC diagnostic <pragma_GCC_diagnostic>`.
301 .. _opt_fdiagnostics-show-category:
303 .. option:: -fdiagnostics-show-category=none/id/name
305 Enable printing category information in diagnostic line.
307 This option, which defaults to "none", controls whether or not Clang
308 prints the category associated with a diagnostic when emitting it.
309 Each diagnostic may or many not have an associated category, if it
310 has one, it is listed in the diagnostic categorization field of the
311 diagnostic line (in the []'s).
313 For example, a format string warning will produce these three
314 renditions based on the setting of this option:
318 t.c:3:11: warning: conversion specifies type 'char *' but the argument has type 'int' [-Wformat]
319 t.c:3:11: warning: conversion specifies type 'char *' but the argument has type 'int' [-Wformat,1]
320 t.c:3:11: warning: conversion specifies type 'char *' but the argument has type 'int' [-Wformat,Format String]
322 This category can be used by clients that want to group diagnostics
323 by category, so it should be a high level category. We want dozens
324 of these, not hundreds or thousands of them.
326 .. _opt_fsave-optimization-record:
328 .. option:: -f[no-]save-optimization-record[=<format>]
330 Enable optimization remarks during compilation and write them to a separate
333 This option, which defaults to off, controls whether Clang writes
334 optimization reports to a separate file. By recording diagnostics in a file,
335 users can parse or sort the remarks in a convenient way.
337 By default, the serialization format is YAML.
339 The supported serialization formats are:
341 - .. _opt_fsave_optimization_record_yaml:
343 ``-fsave-optimization-record=yaml``: A structured YAML format.
345 - .. _opt_fsave_optimization_record_bitstream:
347 ``-fsave-optimization-record=bitstream``: A binary format based on LLVM
350 The output file is controlled by :ref:`-foptimization-record-file <opt_foptimization-record-file>`.
352 In the absence of an explicit output file, the file is chosen using the
355 ``<base>.opt.<format>``
357 where ``<base>`` is based on the output file of the compilation (whether
358 it's explicitly specified through `-o` or not) when used with `-c` or `-S`.
361 * ``clang -fsave-optimization-record -c in.c -o out.o`` will generate
364 * ``clang -fsave-optimization-record -c in.c `` will generate
367 When targeting (Thin)LTO, the base is derived from the output filename, and
368 the extension is not dropped.
370 When targeting ThinLTO, the following scheme is used:
372 ``<base>.opt.<format>.thin.<num>.<format>``
374 Darwin-only: when used for generating a linked binary from a source file
375 (through an intermediate object file), the driver will invoke `cc1` to
376 generate a temporary object file. The temporary remark file will be emitted
377 next to the object file, which will then be picked up by `dsymutil` and
378 emitted in the .dSYM bundle. This is available for all formats except YAML.
382 ``clang -fsave-optimization-record=bitstream in.c -o out`` will generate
384 * ``/var/folders/43/9y164hh52tv_2nrdxrj31nyw0000gn/T/a-9be59b.o``
386 * ``/var/folders/43/9y164hh52tv_2nrdxrj31nyw0000gn/T/a-9be59b.opt.bitstream``
390 * ``out.dSYM/Contents/Resources/Remarks/out``
392 Darwin-only: compiling for multiple architectures will use the following
395 ``<base>-<arch>.opt.<format>``
397 Note that this is incompatible with passing the
398 :ref:`-foptimization-record-file <opt_foptimization-record-file>` option.
400 .. _opt_foptimization-record-file:
402 **-foptimization-record-file**
403 Control the file to which optimization reports are written. This implies
404 :ref:`-fsave-optimization-record <opt_fsave-optimization-record>`.
406 On Darwin platforms, this is incompatible with passing multiple
407 ``-arch <arch>`` options.
409 .. _opt_foptimization-record-passes:
411 **-foptimization-record-passes**
412 Only include passes which match a specified regular expression.
414 When optimization reports are being output (see
415 :ref:`-fsave-optimization-record <opt_fsave-optimization-record>`), this
416 option controls the passes that will be included in the final report.
418 If this option is not used, all the passes are included in the optimization
421 .. _opt_fdiagnostics-show-hotness:
423 **-f[no-]diagnostics-show-hotness**
424 Enable profile hotness information in diagnostic line.
426 This option controls whether Clang prints the profile hotness associated
427 with diagnostics in the presence of profile-guided optimization information.
428 This is currently supported with optimization remarks (see
429 :ref:`Options to Emit Optimization Reports <rpass>`). The hotness information
430 allows users to focus on the hot optimization remarks that are likely to be
431 more relevant for run-time performance.
433 For example, in this output, the block containing the callsite of `foo` was
434 executed 3000 times according to the profile data:
438 s.c:7:10: remark: foo inlined into bar (hotness: 3000) [-Rpass-analysis=inline]
439 sum += foo(x, x - 2);
442 This option is implied when
443 :ref:`-fsave-optimization-record <opt_fsave-optimization-record>` is used.
444 Otherwise, it defaults to off.
446 .. _opt_fdiagnostics-hotness-threshold:
448 **-fdiagnostics-hotness-threshold**
449 Prevent optimization remarks from being output if they do not have at least
452 This option, which defaults to zero, controls the minimum hotness an
453 optimization remark would need in order to be output by Clang. This is
454 currently supported with optimization remarks (see :ref:`Options to Emit
455 Optimization Reports <rpass>`) when profile hotness information in
456 diagnostics is enabled (see
457 :ref:`-fdiagnostics-show-hotness <opt_fdiagnostics-show-hotness>`).
459 .. _opt_fdiagnostics-fixit-info:
461 **-f[no-]diagnostics-fixit-info**
462 Enable "FixIt" information in the diagnostics output.
464 This option, which defaults to on, controls whether or not Clang
465 prints the information on how to fix a specific diagnostic
466 underneath it when it knows. For example, in this output:
470 test.c:28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens]
475 Passing **-fno-diagnostics-fixit-info** will prevent Clang from
476 printing the "//" line at the end of the message. This information
477 is useful for users who may not understand what is wrong, but can be
478 confusing for machine parsing.
480 .. _opt_fdiagnostics-print-source-range-info:
482 **-fdiagnostics-print-source-range-info**
483 Print machine parsable information about source ranges.
484 This option makes Clang print information about source ranges in a machine
485 parsable format after the file/line/column number information. The
486 information is a simple sequence of brace enclosed ranges, where each range
487 lists the start and end line/column locations. For example, in this output:
491 exprs.c:47:15:{47:8-47:14}{47:17-47:24}: error: invalid operands to binary expression ('int *' and '_Complex float')
492 P = (P-42) + Gamma*4;
495 The {}'s are generated by -fdiagnostics-print-source-range-info.
497 The printed column numbers count bytes from the beginning of the
498 line; take care if your source contains multibyte characters.
500 .. option:: -fdiagnostics-parseable-fixits
502 Print Fix-Its in a machine parseable form.
504 This option makes Clang print available Fix-Its in a machine
505 parseable format at the end of diagnostics. The following example
506 illustrates the format:
510 fix-it:"t.cpp":{7:25-7:29}:"Gamma"
512 The range printed is a half-open range, so in this example the
513 characters at column 25 up to but not including column 29 on line 7
514 in t.cpp should be replaced with the string "Gamma". Either the
515 range or the replacement string may be empty (representing strict
516 insertions and strict erasures, respectively). Both the file name
517 and the insertion string escape backslash (as "\\\\"), tabs (as
518 "\\t"), newlines (as "\\n"), double quotes(as "\\"") and
519 non-printable characters (as octal "\\xxx").
521 The printed column numbers count bytes from the beginning of the
522 line; take care if your source contains multibyte characters.
524 .. option:: -fno-elide-type
526 Turns off elision in template type printing.
528 The default for template type printing is to elide as many template
529 arguments as possible, removing those which are the same in both
530 template types, leaving only the differences. Adding this flag will
531 print all the template arguments. If supported by the terminal,
532 highlighting will still appear on differing arguments.
538 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;
544 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;
546 .. option:: -fdiagnostics-show-template-tree
548 Template type diffing prints a text tree.
550 For diffing large templated types, this option will cause Clang to
551 display the templates as an indented text tree, one argument per
552 line, with differences marked inline. This is compatible with
559 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;
561 With :option:`-fdiagnostics-show-template-tree`:
565 t.cc:4:5: note: candidate function not viable: no known conversion for 1st argument;
573 .. _cl_diag_warning_groups:
575 Individual Warning Groups
576 ^^^^^^^^^^^^^^^^^^^^^^^^^
578 TODO: Generate this from tblgen. Define one anchor per warning group.
580 .. _opt_wextra-tokens:
582 .. option:: -Wextra-tokens
584 Warn about excess tokens at the end of a preprocessor directive.
586 This option, which defaults to on, enables warnings about extra
587 tokens at the end of preprocessor directives. For example:
591 test.c:28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens]
595 These extra tokens are not strictly conforming, and are usually best
596 handled by commenting them out.
598 .. option:: -Wambiguous-member-template
600 Warn about unqualified uses of a member template whose name resolves to
601 another template at the location of the use.
603 This option, which defaults to on, enables a warning in the
608 template<typename T> struct set{};
609 template<typename T> struct trait { typedef const T& type; };
611 template<typename T> void set(typename trait<T>::type value) {}
618 C++ [basic.lookup.classref] requires this to be an error, but,
619 because it's hard to work around, Clang downgrades it to a warning
622 .. option:: -Wbind-to-temporary-copy
624 Warn about an unusable copy constructor when binding a reference to a
627 This option enables warnings about binding a
628 reference to a temporary when the temporary doesn't have a usable
629 copy constructor. For example:
636 NonCopyable(const NonCopyable&);
638 void foo(const NonCopyable&);
640 foo(NonCopyable()); // Disallowed in C++98; allowed in C++11.
645 struct NonCopyable2 {
647 NonCopyable2(NonCopyable2&);
649 void foo(const NonCopyable2&);
651 foo(NonCopyable2()); // Disallowed in C++98; allowed in C++11.
654 Note that if ``NonCopyable2::NonCopyable2()`` has a default argument
655 whose instantiation produces a compile error, that error will still
656 be a hard error in C++98 mode even if this warning is turned off.
658 Options to Control Clang Crash Diagnostics
659 ------------------------------------------
661 As unbelievable as it may sound, Clang does crash from time to time.
662 Generally, this only occurs to those living on the `bleeding
663 edge <https://llvm.org/releases/download.html#svn>`_. Clang goes to great
664 lengths to assist you in filing a bug report. Specifically, Clang
665 generates preprocessed source file(s) and associated run script(s) upon
666 a crash. These files should be attached to a bug report to ease
667 reproducibility of the failure. Below are the command line options to
668 control the crash diagnostics.
670 .. option:: -fno-crash-diagnostics
672 Disable auto-generation of preprocessed source files during a clang crash.
674 The -fno-crash-diagnostics flag can be helpful for speeding the process
675 of generating a delta reduced test case.
677 .. option:: -fcrash-diagnostics-dir=<dir>
679 Specify where to write the crash diagnostics files; defaults to the
680 usual location for temporary files.
682 Clang is also capable of generating preprocessed source file(s) and associated
683 run script(s) even without a crash. This is specially useful when trying to
684 generate a reproducer for warnings or errors while using modules.
686 .. option:: -gen-reproducer
688 Generates preprocessed source files, a reproducer script and if relevant, a
689 cache containing: built module pcm's and all headers needed to rebuild the
694 Options to Emit Optimization Reports
695 ------------------------------------
697 Optimization reports trace, at a high-level, all the major decisions
698 done by compiler transformations. For instance, when the inliner
699 decides to inline function ``foo()`` into ``bar()``, or the loop unroller
700 decides to unroll a loop N times, or the vectorizer decides to
701 vectorize a loop body.
703 Clang offers a family of flags which the optimizers can use to emit
704 a diagnostic in three cases:
706 1. When the pass makes a transformation (`-Rpass`).
708 2. When the pass fails to make a transformation (`-Rpass-missed`).
710 3. When the pass determines whether or not to make a transformation
713 NOTE: Although the discussion below focuses on `-Rpass`, the exact
714 same options apply to `-Rpass-missed` and `-Rpass-analysis`.
716 Since there are dozens of passes inside the compiler, each of these flags
717 take a regular expression that identifies the name of the pass which should
718 emit the associated diagnostic. For example, to get a report from the inliner,
719 compile the code with:
721 .. code-block:: console
723 $ clang -O2 -Rpass=inline code.cc -o code
724 code.cc:4:25: remark: foo inlined into bar [-Rpass=inline]
725 int bar(int j) { return foo(j, j - 2); }
728 Note that remarks from the inliner are identified with `[-Rpass=inline]`.
729 To request a report from every optimization pass, you should use
730 `-Rpass=.*` (in fact, you can use any valid POSIX regular
731 expression). However, do not expect a report from every transformation
732 made by the compiler. Optimization remarks do not really make sense
733 outside of the major transformations (e.g., inlining, vectorization,
734 loop optimizations) and not every optimization pass supports this
737 Note that when using profile-guided optimization information, profile hotness
738 information can be included in the remarks (see
739 :ref:`-fdiagnostics-show-hotness <opt_fdiagnostics-show-hotness>`).
744 1. Optimization remarks that refer to function names will display the
745 mangled name of the function. Since these remarks are emitted by the
746 back end of the compiler, it does not know anything about the input
747 language, nor its mangling rules.
749 2. Some source locations are not displayed correctly. The front end has
750 a more detailed source location tracking than the locations included
751 in the debug info (e.g., the front end can locate code inside macro
752 expansions). However, the locations used by `-Rpass` are
753 translated from debug annotations. That translation can be lossy,
754 which results in some remarks having no location information.
756 Options to Emit Resource Consumption Reports
757 --------------------------------------------
759 These are options that report execution time and consumed memory of different
762 .. option:: -fproc-stat-report=
764 This option requests driver to print used memory and execution time of each
765 compilation step. The ``clang`` driver during execution calls different tools,
766 like compiler, assembler, linker etc. With this option the driver reports
767 total execution time, the execution time spent in user mode and peak memory
768 usage of each the called tool. Value of the option specifies where the report
769 is sent to. If it specifies a regular file, the data are saved to this file in
772 .. code-block:: console
774 $ clang -fproc-stat-report=abc foo.c
776 clang-11,"/tmp/foo-123456.o",92000,84000,87536
777 ld,"a.out",900,8000,53568
779 The data on each row represent:
781 * file name of the tool executable,
782 * output file name in quotes,
783 * total execution time in microseconds,
784 * execution time in user mode in microseconds,
785 * peak memory usage in Kb.
787 It is possible to specify this option without any value. In this case statistics
788 are printed on standard output in human readable format:
790 .. code-block:: console
792 $ clang -fproc-stat-report foo.c
793 clang-11: output=/tmp/foo-855a8e.o, total=68.000 ms, user=60.000 ms, mem=86920 Kb
794 ld: output=a.out, total=8.000 ms, user=4.000 ms, mem=52320 Kb
796 The report file specified in the option is locked for write, so this option
797 can be used to collect statistics in parallel builds. The report file is not
798 cleared, new data is appended to it, thus making posible to accumulate build
801 You can also use environment variables to control the process statistics reporting.
802 Setting ``CC_PRINT_PROC_STAT`` to ``1`` enables the feature, the report goes to
803 stdout in human readable format.
804 Setting ``CC_PRINT_PROC_STAT_FILE`` to a fully qualified file path makes it report
805 process statistics to the given file in the CSV format. Specifying a relative
806 path will likely lead to multiple files with the same name created in different
807 directories, since the path is relative to a changing working directory.
809 These environment variables are handy when you need to request the statistics
810 report without changing your build scripts or alter the existing set of compiler
811 options. Note that ``-fproc-stat-report`` take precedence over ``CC_PRINT_PROC_STAT``
812 and ``CC_PRINT_PROC_STAT_FILE``.
814 .. code-block:: console
816 $ export CC_PRINT_PROC_STAT=1
817 $ export CC_PRINT_PROC_STAT_FILE=~/project-build-proc-stat.csv
822 Clang options that don't fit neatly into other categories.
824 .. option:: -fgnuc-version=
826 This flag controls the value of ``__GNUC__`` and related macros. This flag
827 does not enable or disable any GCC extensions implemented in Clang. Setting
828 the version to zero causes Clang to leave ``__GNUC__`` and other
829 GNU-namespaced macros, such as ``__GXX_WEAK__``, undefined.
833 When emitting a dependency file, use formatting conventions appropriate
834 for NMake or Jom. Ignored unless another option causes Clang to emit a
837 When Clang emits a dependency file (e.g., you supplied the -M option)
838 most filenames can be written to the file without any special formatting.
839 Different Make tools will treat different sets of characters as "special"
840 and use different conventions for telling the Make tool that the character
841 is actually part of the filename. Normally Clang uses backslash to "escape"
842 a special character, which is the convention used by GNU Make. The -MV
843 option tells Clang to put double-quotes around the entire filename, which
844 is the convention used by NMake and Jom.
849 Configuration files group command-line options and allow all of them to be
850 specified just by referencing the configuration file. They may be used, for
851 example, to collect options required to tune compilation for particular
852 target, such as -L, -I, -l, --sysroot, codegen options, etc.
854 The command line option `--config` can be used to specify configuration
855 file in a Clang invocation. For example:
859 clang --config /home/user/cfgs/testing.txt
860 clang --config debug.cfg
862 If the provided argument contains a directory separator, it is considered as
863 a file path, and options are read from that file. Otherwise the argument is
864 treated as a file name and is searched for sequentially in the directories:
868 - the directory where Clang executable resides.
870 Both user and system directories for configuration files are specified during
871 clang build using CMake parameters, CLANG_CONFIG_FILE_USER_DIR and
872 CLANG_CONFIG_FILE_SYSTEM_DIR respectively. The first file found is used. It is
873 an error if the required file cannot be found.
875 Another way to specify a configuration file is to encode it in executable name.
876 For example, if the Clang executable is named `armv7l-clang` (it may be a
877 symbolic link to `clang`), then Clang will search for file `armv7l.cfg` in the
878 directory where Clang resides.
880 If a driver mode is specified in invocation, Clang tries to find a file specific
881 for the specified mode. For example, if the executable file is named
882 `x86_64-clang-cl`, Clang first looks for `x86_64-cl.cfg` and if it is not found,
883 looks for `x86_64.cfg`.
885 If the command line contains options that effectively change target architecture
886 (these are -m32, -EL, and some others) and the configuration file starts with an
887 architecture name, Clang tries to load the configuration file for the effective
888 architecture. For example, invocation:
892 x86_64-clang -m32 abc.c
894 causes Clang search for a file `i368.cfg` first, and if no such file is found,
895 Clang looks for the file `x86_64.cfg`.
897 The configuration file consists of command-line options specified on one or
898 more lines. Lines composed of whitespace characters only are ignored as well as
899 lines in which the first non-blank character is `#`. Long options may be split
900 between several lines by a trailing backslash. Here is example of a
905 # Several options on line
906 -c --target=x86_64-unknown-linux-gnu
908 # Long option split between lines
909 -I/usr/lib/gcc/x86_64-linux-gnu/5.4.0/../../../../\
912 # other config files may be included
915 Files included by `@file` directives in configuration files are resolved
916 relative to the including file. For example, if a configuration file
917 `~/.llvm/target.cfg` contains the directive `@os/linux.opts`, the file
918 `linux.opts` is searched for in the directory `~/.llvm/os`.
920 Language and Target-Independent Features
921 ========================================
923 Controlling Errors and Warnings
924 -------------------------------
926 Clang provides a number of ways to control which code constructs cause
927 it to emit errors and warning messages, and how they are displayed to
930 Controlling How Clang Displays Diagnostics
931 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
933 When Clang emits a diagnostic, it includes rich information in the
934 output, and gives you fine-grain control over which information is
935 printed. Clang has the ability to print this information, and these are
936 the options that control it:
938 #. A file/line/column indicator that shows exactly where the diagnostic
939 occurs in your code [:ref:`-fshow-column <opt_fshow-column>`,
940 :ref:`-fshow-source-location <opt_fshow-source-location>`].
941 #. A categorization of the diagnostic as a note, warning, error, or
943 #. A text string that describes what the problem is.
944 #. An option that indicates how to control the diagnostic (for
945 diagnostics that support it)
946 [:ref:`-fdiagnostics-show-option <opt_fdiagnostics-show-option>`].
947 #. A :ref:`high-level category <diagnostics_categories>` for the diagnostic
948 for clients that want to group diagnostics by class (for diagnostics
950 [:ref:`-fdiagnostics-show-category <opt_fdiagnostics-show-category>`].
951 #. The line of source code that the issue occurs on, along with a caret
952 and ranges that indicate the important locations
953 [:ref:`-fcaret-diagnostics <opt_fcaret-diagnostics>`].
954 #. "FixIt" information, which is a concise explanation of how to fix the
955 problem (when Clang is certain it knows)
956 [:ref:`-fdiagnostics-fixit-info <opt_fdiagnostics-fixit-info>`].
957 #. A machine-parsable representation of the ranges involved (off by
959 [:ref:`-fdiagnostics-print-source-range-info <opt_fdiagnostics-print-source-range-info>`].
961 For more information please see :ref:`Formatting of
962 Diagnostics <cl_diag_formatting>`.
967 All diagnostics are mapped into one of these 6 classes:
976 .. _diagnostics_categories:
978 Diagnostic Categories
979 ^^^^^^^^^^^^^^^^^^^^^
981 Though not shown by default, diagnostics may each be associated with a
982 high-level category. This category is intended to make it possible to
983 triage builds that produce a large number of errors or warnings in a
986 Categories are not shown by default, but they can be turned on with the
987 :ref:`-fdiagnostics-show-category <opt_fdiagnostics-show-category>` option.
988 When set to "``name``", the category is printed textually in the
989 diagnostic output. When it is set to "``id``", a category number is
990 printed. The mapping of category names to category id's can be obtained
991 by running '``clang --print-diagnostic-categories``'.
993 Controlling Diagnostics via Command Line Flags
994 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
996 TODO: -W flags, -pedantic, etc
998 .. _pragma_gcc_diagnostic:
1000 Controlling Diagnostics via Pragmas
1001 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1003 Clang can also control what diagnostics are enabled through the use of
1004 pragmas in the source code. This is useful for turning off specific
1005 warnings in a section of source code. Clang supports GCC's pragma for
1006 compatibility with existing source code, as well as several extensions.
1008 The pragma may control any warning that can be used from the command
1009 line. Warnings may be set to ignored, warning, error, or fatal. The
1010 following example code will tell Clang or GCC to ignore the -Wall
1015 #pragma GCC diagnostic ignored "-Wall"
1017 In addition to all of the functionality provided by GCC's pragma, Clang
1018 also allows you to push and pop the current warning state. This is
1019 particularly useful when writing a header file that will be compiled by
1020 other people, because you don't know what warning flags they build with.
1022 In the below example :option:`-Wextra-tokens` is ignored for only a single line
1023 of code, after which the diagnostics return to whatever state had previously
1029 #endif foo // warning: extra tokens at end of #endif directive
1031 #pragma clang diagnostic push
1032 #pragma clang diagnostic ignored "-Wextra-tokens"
1035 #endif foo // no warning
1037 #pragma clang diagnostic pop
1039 The push and pop pragmas will save and restore the full diagnostic state
1040 of the compiler, regardless of how it was set. That means that it is
1041 possible to use push and pop around GCC compatible diagnostics and Clang
1042 will push and pop them appropriately, while GCC will ignore the pushes
1043 and pops as unknown pragmas. It should be noted that while Clang
1044 supports the GCC pragma, Clang and GCC do not support the exact same set
1045 of warnings, so even when using GCC compatible #pragmas there is no
1046 guarantee that they will have identical behaviour on both compilers.
1048 In addition to controlling warnings and errors generated by the compiler, it is
1049 possible to generate custom warning and error messages through the following
1054 // The following will produce warning messages
1055 #pragma message "some diagnostic message"
1056 #pragma GCC warning "TODO: replace deprecated feature"
1058 // The following will produce an error message
1059 #pragma GCC error "Not supported"
1061 These pragmas operate similarly to the ``#warning`` and ``#error`` preprocessor
1062 directives, except that they may also be embedded into preprocessor macros via
1063 the C99 ``_Pragma`` operator, for example:
1068 #define DEFER(M,...) M(__VA_ARGS__)
1069 #define CUSTOM_ERROR(X) _Pragma(STR(GCC error(X " at line " DEFER(STR,__LINE__))))
1071 CUSTOM_ERROR("Feature not available");
1073 Controlling Diagnostics in System Headers
1074 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1076 Warnings are suppressed when they occur in system headers. By default,
1077 an included file is treated as a system header if it is found in an
1078 include path specified by ``-isystem``, but this can be overridden in
1081 The ``system_header`` pragma can be used to mark the current file as
1082 being a system header. No warnings will be produced from the location of
1083 the pragma onwards within the same file.
1088 #endif foo // warning: extra tokens at end of #endif directive
1090 #pragma clang system_header
1093 #endif foo // no warning
1095 The `--system-header-prefix=` and `--no-system-header-prefix=`
1096 command-line arguments can be used to override whether subsets of an include
1097 path are treated as system headers. When the name in a ``#include`` directive
1098 is found within a header search path and starts with a system prefix, the
1099 header is treated as a system header. The last prefix on the
1100 command-line which matches the specified header name takes precedence.
1103 .. code-block:: console
1105 $ clang -Ifoo -isystem bar --system-header-prefix=x/ \
1106 --no-system-header-prefix=x/y/
1108 Here, ``#include "x/a.h"`` is treated as including a system header, even
1109 if the header is found in ``foo``, and ``#include "x/y/b.h"`` is treated
1110 as not including a system header, even if the header is found in
1113 A ``#include`` directive which finds a file relative to the current
1114 directory is treated as including a system header if the including file
1115 is treated as a system header.
1117 .. _diagnostics_enable_everything:
1119 Enabling All Diagnostics
1120 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1122 In addition to the traditional ``-W`` flags, one can enable **all** diagnostics
1123 by passing :option:`-Weverything`. This works as expected with
1124 :option:`-Werror`, and also includes the warnings from :option:`-pedantic`. Some
1125 diagnostics contradict each other, therefore, users of :option:`-Weverything`
1126 often disable many diagnostics such as `-Wno-c++98-compat` and `-Wno-c++-compat`
1127 because they contradict recent C++ standards.
1129 Since :option:`-Weverything` enables every diagnostic, we generally don't
1130 recommend using it. `-Wall` `-Wextra` are a better choice for most projects.
1131 Using :option:`-Weverything` means that updating your compiler is more difficult
1132 because you're exposed to experimental diagnostics which might be of lower
1133 quality than the default ones. If you do use :option:`-Weverything` then we
1134 advise that you address all new compiler diagnostics as they get added to Clang,
1135 either by fixing everything they find or explicitly disabling that diagnostic
1136 with its corresponding `Wno-` option.
1138 Note that when combined with :option:`-w` (which disables all warnings),
1139 disabling all warnings wins.
1141 Controlling Static Analyzer Diagnostics
1142 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1144 While not strictly part of the compiler, the diagnostics from Clang's
1145 `static analyzer <https://clang-analyzer.llvm.org>`_ can also be
1146 influenced by the user via changes to the source code. See the available
1147 `annotations <https://clang-analyzer.llvm.org/annotations.html>`_ and the
1149 page <https://clang-analyzer.llvm.org/faq.html#exclude_code>`_ for more
1152 .. _usersmanual-precompiled-headers:
1157 `Precompiled headers <https://en.wikipedia.org/wiki/Precompiled_header>`_
1158 are a general approach employed by many compilers to reduce compilation
1159 time. The underlying motivation of the approach is that it is common for
1160 the same (and often large) header files to be included by multiple
1161 source files. Consequently, compile times can often be greatly improved
1162 by caching some of the (redundant) work done by a compiler to process
1163 headers. Precompiled header files, which represent one of many ways to
1164 implement this optimization, are literally files that represent an
1165 on-disk cache that contains the vital information necessary to reduce
1166 some of the work needed to process a corresponding header file. While
1167 details of precompiled headers vary between compilers, precompiled
1168 headers have been shown to be highly effective at speeding up program
1169 compilation on systems with very large system headers (e.g., macOS).
1171 Generating a PCH File
1172 ^^^^^^^^^^^^^^^^^^^^^
1174 To generate a PCH file using Clang, one invokes Clang with the
1175 `-x <language>-header` option. This mirrors the interface in GCC
1176 for generating PCH files:
1178 .. code-block:: console
1180 $ gcc -x c-header test.h -o test.h.gch
1181 $ clang -x c-header test.h -o test.h.pch
1186 A PCH file can then be used as a prefix header when a :option:`-include`
1187 option is passed to ``clang``:
1189 .. code-block:: console
1191 $ clang -include test.h test.c -o test
1193 The ``clang`` driver will first check if a PCH file for ``test.h`` is
1194 available; if so, the contents of ``test.h`` (and the files it includes)
1195 will be processed from the PCH file. Otherwise, Clang falls back to
1196 directly processing the content of ``test.h``. This mirrors the behavior
1201 Clang does *not* automatically use PCH files for headers that are directly
1202 included within a source file. For example:
1204 .. code-block:: console
1206 $ clang -x c-header test.h -o test.h.pch
1209 $ clang test.c -o test
1211 In this example, ``clang`` will not automatically use the PCH file for
1212 ``test.h`` since ``test.h`` was included directly in the source file and not
1213 specified on the command line using :option:`-include`.
1215 Relocatable PCH Files
1216 ^^^^^^^^^^^^^^^^^^^^^
1218 It is sometimes necessary to build a precompiled header from headers
1219 that are not yet in their final, installed locations. For example, one
1220 might build a precompiled header within the build tree that is then
1221 meant to be installed alongside the headers. Clang permits the creation
1222 of "relocatable" precompiled headers, which are built with a given path
1223 (into the build directory) and can later be used from an installed
1226 To build a relocatable precompiled header, place your headers into a
1227 subdirectory whose structure mimics the installed location. For example,
1228 if you want to build a precompiled header for the header ``mylib.h``
1229 that will be installed into ``/usr/include``, create a subdirectory
1230 ``build/usr/include`` and place the header ``mylib.h`` into that
1231 subdirectory. If ``mylib.h`` depends on other headers, then they can be
1232 stored within ``build/usr/include`` in a way that mimics the installed
1235 Building a relocatable precompiled header requires two additional
1236 arguments. First, pass the ``--relocatable-pch`` flag to indicate that
1237 the resulting PCH file should be relocatable. Second, pass
1238 ``-isysroot /path/to/build``, which makes all includes for your library
1239 relative to the build directory. For example:
1241 .. code-block:: console
1243 # clang -x c-header --relocatable-pch -isysroot /path/to/build /path/to/build/mylib.h mylib.h.pch
1245 When loading the relocatable PCH file, the various headers used in the
1246 PCH file are found from the system header root. For example, ``mylib.h``
1247 can be found in ``/usr/include/mylib.h``. If the headers are installed
1248 in some other system root, the ``-isysroot`` option can be used provide
1249 a different system root from which the headers will be based. For
1250 example, ``-isysroot /Developer/SDKs/MacOSX10.4u.sdk`` will look for
1251 ``mylib.h`` in ``/Developer/SDKs/MacOSX10.4u.sdk/usr/include/mylib.h``.
1253 Relocatable precompiled headers are intended to be used in a limited
1254 number of cases where the compilation environment is tightly controlled
1255 and the precompiled header cannot be generated after headers have been
1258 .. _controlling-fp-behavior:
1260 Controlling Floating Point Behavior
1261 -----------------------------------
1263 Clang provides a number of ways to control floating point behavior, including
1264 with command line options and source pragmas. This section
1265 describes the various floating point semantic modes and the corresponding options.
1267 .. csv-table:: Floating Point Semantic Modes
1268 :header: "Mode", "Values"
1271 "ffp-exception-behavior", "{ignore, strict, may_trap}",
1272 "fenv_access", "{off, on}", "(none)"
1273 "frounding-math", "{dynamic, tonearest, downward, upward, towardzero}"
1274 "ffp-contract", "{on, off, fast, fast-honor-pragmas}"
1275 "fdenormal-fp-math", "{IEEE, PreserveSign, PositiveZero}"
1276 "fdenormal-fp-math-fp32", "{IEEE, PreserveSign, PositiveZero}"
1277 "fmath-errno", "{on, off}"
1278 "fhonor-nans", "{on, off}"
1279 "fhonor-infinities", "{on, off}"
1280 "fsigned-zeros", "{on, off}"
1281 "freciprocal-math", "{on, off}"
1282 "allow_approximate_fns", "{on, off}"
1283 "fassociative-math", "{on, off}"
1285 This table describes the option settings that correspond to the three
1286 floating point semantic models: precise (the default), strict, and fast.
1289 .. csv-table:: Floating Point Models
1290 :header: "Mode", "Precise", "Strict", "Fast"
1291 :widths: 25, 15, 15, 15
1293 "except_behavior", "ignore", "strict", "ignore"
1294 "fenv_access", "off", "on", "off"
1295 "rounding_mode", "tonearest", "dynamic", "tonearest"
1296 "contract", "on", "off", "fast"
1297 "denormal_fp_math", "IEEE", "IEEE", "PreserveSign"
1298 "denormal_fp32_math", "IEEE","IEEE", "PreserveSign"
1299 "support_math_errno", "on", "on", "off"
1300 "no_honor_nans", "off", "off", "on"
1301 "no_honor_infinities", "off", "off", "on"
1302 "no_signed_zeros", "off", "off", "on"
1303 "allow_reciprocal", "off", "off", "on"
1304 "allow_approximate_fns", "off", "off", "on"
1305 "allow_reassociation", "off", "off", "on"
1307 .. option:: -ffast-math
1309 Enable fast-math mode. This option lets the
1310 compiler make aggressive, potentially-lossy assumptions about
1311 floating-point math. These include:
1313 * Floating-point math obeys regular algebraic rules for real numbers (e.g.
1314 ``+`` and ``*`` are associative, ``x/y == x * (1/y)``, and
1315 ``(a + b) * c == a * c + b * c``),
1316 * Operands to floating-point operations are not equal to ``NaN`` and
1318 * ``+0`` and ``-0`` are interchangeable.
1320 ``-ffast-math`` also defines the ``__FAST_MATH__`` preprocessor
1321 macro. Some math libraries recognize this macro and change their behavior.
1322 With the exception of ``-ffp-contract=fast``, using any of the options
1323 below to disable any of the individual optimizations in ``-ffast-math``
1324 will cause ``__FAST_MATH__`` to no longer be set.
1326 This option implies:
1328 * ``-fno-honor-infinities``
1330 * ``-fno-honor-nans``
1332 * ``-fno-math-errno``
1334 * ``-ffinite-math-only``
1336 * ``-fassociative-math``
1338 * ``-freciprocal-math``
1340 * ``-fno-signed-zeros``
1342 * ``-fno-trapping-math``
1344 * ``-ffp-contract=fast``
1346 .. option:: -fdenormal-fp-math=<value>
1348 Select which denormal numbers the code is permitted to require.
1352 * ``ieee`` - IEEE 754 denormal numbers
1353 * ``preserve-sign`` - the sign of a flushed-to-zero number is preserved in the sign of 0
1354 * ``positive-zero`` - denormals are flushed to positive zero
1356 Defaults to ``ieee``.
1358 .. _opt_fstrict-float-cast-overflow:
1360 **-f[no-]strict-float-cast-overflow**
1362 When a floating-point value is not representable in a destination integer
1363 type, the code has undefined behavior according to the language standard.
1364 By default, Clang will not guarantee any particular result in that case.
1365 With the 'no-strict' option, Clang attempts to match the overflowing behavior
1366 of the target's native float-to-int conversion instructions.
1368 .. _opt_fmath-errno:
1370 **-f[no-]math-errno**
1372 Require math functions to indicate errors by setting errno.
1373 The default varies by ToolChain. ``-fno-math-errno`` allows optimizations
1374 that might cause standard C math functions to not set ``errno``.
1375 For example, on some systems, the math function ``sqrt`` is specified
1376 as setting ``errno`` to ``EDOM`` when the input is negative. On these
1377 systems, the compiler cannot normally optimize a call to ``sqrt`` to use
1378 inline code (e.g. the x86 ``sqrtsd`` instruction) without additional
1379 checking to ensure that ``errno`` is set appropriately.
1380 ``-fno-math-errno`` permits these transformations.
1382 On some targets, math library functions never set ``errno``, and so
1383 ``-fno-math-errno`` is the default. This includes most BSD-derived
1384 systems, including Darwin.
1386 .. _opt_ftrapping-math:
1388 **-f[no-]trapping-math**
1390 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.
1392 - The option ``-ftrapping-math`` behaves identically to ``-ffp-exception-behavior=strict``.
1393 - The option ``-fno-trapping-math`` behaves identically to ``-ffp-exception-behavior=ignore``. This is the default.
1395 .. option:: -ffp-contract=<value>
1397 Specify when the compiler is permitted to form fused floating-point
1398 operations, such as fused multiply-add (FMA). Fused operations are
1399 permitted to produce more precise results than performing the same
1400 operations separately.
1402 The C standard permits intermediate floating-point results within an
1403 expression to be computed with more precision than their type would
1404 normally allow. This permits operation fusing, and Clang takes advantage
1405 of this by default. This behavior can be controlled with the ``FP_CONTRACT``
1406 and ``clang fp contract`` pragmas. Please refer to the pragma documentation
1407 for a description of how the pragmas interact with this option.
1411 * ``fast`` (fuse across statements disregarding pragmas, default for CUDA)
1412 * ``on`` (fuse in the same statement unless dictated by pragmas, default for languages other than CUDA/HIP)
1413 * ``off`` (never fuse)
1414 * ``fast-honor-pragmas`` (fuse across statements unless dictated by pragmas, default for HIP)
1416 .. _opt_fhonor-infinities:
1418 **-f[no-]honor-infinities**
1420 If both ``-fno-honor-infinities`` and ``-fno-honor-nans`` are used,
1421 has the same effect as specifying ``-ffinite-math-only``.
1423 .. _opt_fhonor-nans:
1425 **-f[no-]honor-nans**
1427 If both ``-fno-honor-infinities`` and ``-fno-honor-nans`` are used,
1428 has the same effect as specifying ``-ffinite-math-only``.
1430 .. _opt_fapprox-func:
1432 **-f[no-]approx-func**
1434 Allow certain math function calls (such as ``log``, ``sqrt``, ``pow``, etc)
1435 to be replaced with an approximately equivalent set of instructions
1436 or alternative math function calls. For example, a ``pow(x, 0.25)``
1437 may be replaced with ``sqrt(sqrt(x))``, despite being an inexact result
1438 in cases where ``x`` is ``-0.0`` or ``-inf``.
1439 Defaults to ``-fno-approx-func``.
1441 .. _opt_fsigned-zeros:
1443 **-f[no-]signed-zeros**
1445 Allow optimizations that ignore the sign of floating point zeros.
1446 Defaults to ``-fno-signed-zeros``.
1448 .. _opt_fassociative-math:
1450 **-f[no-]associative-math**
1452 Allow floating point operations to be reassociated.
1453 Defaults to ``-fno-associative-math``.
1455 .. _opt_freciprocal-math:
1457 **-f[no-]reciprocal-math**
1459 Allow division operations to be transformed into multiplication by a
1460 reciprocal. This can be significantly faster than an ordinary division
1461 but can also have significantly less precision. Defaults to
1462 ``-fno-reciprocal-math``.
1464 .. _opt_funsafe-math-optimizations:
1466 **-f[no-]unsafe-math-optimizations**
1468 Allow unsafe floating-point optimizations. Also implies:
1470 * ``-fassociative-math``
1471 * ``-freciprocal-math``
1472 * ``-fno-signed-zeroes``
1473 * ``-fno-trapping-math``.
1475 Defaults to ``-fno-unsafe-math-optimizations``.
1477 .. _opt_ffinite-math-only:
1479 **-f[no-]finite-math-only**
1481 Allow floating-point optimizations that assume arguments and results are
1482 not NaNs or +-Inf. This defines the ``__FINITE_MATH_ONLY__`` preprocessor macro.
1485 * ``-fno-honor-infinities``
1486 * ``-fno-honor-nans``
1488 Defaults to ``-fno-finite-math-only``.
1490 .. _opt_frounding-math:
1492 **-f[no-]rounding-math**
1494 Force floating-point operations to honor the dynamically-set rounding mode by default.
1496 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.
1498 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``.
1500 - The option ``-fno-rounding-math`` allows the compiler to assume that the rounding mode is set to ``FE_TONEAREST``. This is the default.
1501 - 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.
1503 .. option:: -ffp-model=<value>
1505 Specify floating point behavior. ``-ffp-model`` is an umbrella
1506 option that encompasses functionality provided by other, single
1507 purpose, floating point options. Valid values are: ``precise``, ``strict``,
1511 * ``precise`` Disables optimizations that are not value-safe on floating-point data, although FP contraction (FMA) is enabled (``-ffp-contract=on``). This is the default behavior.
1512 * ``strict`` Enables ``-frounding-math`` and ``-ffp-exception-behavior=strict``, and disables contractions (FMA). All of the ``-ffast-math`` enablements are disabled. Enables ``STDC FENV_ACCESS``: by default ``FENV_ACCESS`` is disabled. This option setting behaves as though ``#pragma STDC FENV_ACESS ON`` appeared at the top of the source file.
1513 * ``fast`` Behaves identically to specifying both ``-ffast-math`` and ``ffp-contract=fast``
1515 Note: If your command line specifies multiple instances
1516 of the ``-ffp-model`` option, or if your command line option specifies
1517 ``-ffp-model`` and later on the command line selects a floating point
1518 option that has the effect of negating part of the ``ffp-model`` that
1519 has been selected, then the compiler will issue a diagnostic warning
1520 that the override has occurred.
1522 .. option:: -ffp-exception-behavior=<value>
1524 Specify the floating-point exception behavior.
1526 Valid values are: ``ignore``, ``maytrap``, and ``strict``.
1527 The default value is ``ignore``. Details:
1529 * ``ignore`` The compiler assumes that the exception status flags will not be read and that floating point exceptions will be masked.
1530 * ``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.
1531 * ``strict`` The compiler ensures that all transformations strictly preserve the floating point exception semantics of the original code.
1533 .. option:: -f[no-]protect-parens:
1535 This option pertains to floating-point types, complex types with
1536 floating-point components, and vectors of these types. Some arithmetic
1537 expression transformations that are mathematically correct and permissible
1538 according to the C and C++ language standards may be incorrect when dealing
1539 with floating-point types, such as reassociation and distribution. Further,
1540 the optimizer may ignore parentheses when computing arithmetic expressions
1541 in circumstances where the parenthesized and unparenthesized expression
1542 express the same mathematical value. For example (a+b)+c is the same
1543 mathematical value as a+(b+c), but the optimizer is free to evaluate the
1544 additions in any order regardless of the parentheses. When enabled, this
1545 option forces the optimizer to honor the order of operations with respect
1546 to parentheses in all circumstances.
1548 Note that floating-point contraction (option `-ffp-contract=`) is disabled
1549 when `-fprotect-parens` is enabled. Also note that in safe floating-point
1550 modes, such as `-ffp-model=precise` or `-ffp-model=strict`, this option
1551 has no effect because the optimizer is prohibited from making unsafe
1554 .. _fp-constant-eval:
1556 A note about Floating Point Constant Evaluation
1557 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1559 In C, the only place floating point operations are guaranteed to be evaluated
1560 during translation is in the initializers of variables of static storage
1561 duration, which are all notionally initialized before the program begins
1562 executing (and thus before a non-default floating point environment can be
1563 entered). But C++ has many more contexts where floating point constant
1564 evaluation occurs. Specifically: for static/thread-local variables,
1565 first try evaluating the initializer in a constant context, including in the
1566 constant floating point environment (just like in C), and then, if that fails,
1567 fall back to emitting runtime code to perform the initialization (which might
1568 in general be in a different floating point environment).
1570 Consider this example when compiled with ``-frounding-math``
1572 .. code-block:: console
1574 constexpr float func_01(float x, float y) {
1577 float V1 = func_01(1.0F, 0x0.000001p0F);
1579 The C++ rule is that initializers for static storage duration variables are
1580 first evaluated during translation (therefore, in the default rounding mode),
1581 and only evaluated at runtime (and therefore in the runtime rounding mode) if
1582 the compile-time evaluation fails. This is in line with the C rules;
1583 C11 F.8.5 says: *All computation for automatic initialization is done (as if)
1584 at execution time; thus, it is affected by any operative modes and raises
1585 floating-point exceptions as required by IEC 60559 (provided the state for the
1586 FENV_ACCESS pragma is ‘‘on’’). All computation for initialization of objects
1587 that have static or thread storage duration is done (as if) at translation
1588 time.* C++ generalizes this by adding another phase of initialization
1589 (at runtime) if the translation-time initialization fails, but the
1590 translation-time evaluation of the initializer of succeeds, it will be
1591 treated as a constant initializer.
1594 .. _controlling-code-generation:
1596 Controlling Code Generation
1597 ---------------------------
1599 Clang provides a number of ways to control code generation. The options
1602 **-f[no-]sanitize=check1,check2,...**
1603 Turn on runtime checks for various forms of undefined or suspicious
1606 This option controls whether Clang adds runtime checks for various
1607 forms of undefined or suspicious behavior, and is disabled by
1608 default. If a check fails, a diagnostic message is produced at
1609 runtime explaining the problem. The main checks are:
1611 - .. _opt_fsanitize_address:
1613 ``-fsanitize=address``:
1614 :doc:`AddressSanitizer`, a memory error
1616 - .. _opt_fsanitize_thread:
1618 ``-fsanitize=thread``: :doc:`ThreadSanitizer`, a data race detector.
1619 - .. _opt_fsanitize_memory:
1621 ``-fsanitize=memory``: :doc:`MemorySanitizer`,
1622 a detector of uninitialized reads. Requires instrumentation of all
1624 - .. _opt_fsanitize_undefined:
1626 ``-fsanitize=undefined``: :doc:`UndefinedBehaviorSanitizer`,
1627 a fast and compatible undefined behavior checker.
1629 - ``-fsanitize=dataflow``: :doc:`DataFlowSanitizer`, a general data
1631 - ``-fsanitize=cfi``: :doc:`control flow integrity <ControlFlowIntegrity>`
1632 checks. Requires ``-flto``.
1633 - ``-fsanitize=safe-stack``: :doc:`safe stack <SafeStack>`
1634 protection against stack-based memory corruption errors.
1636 There are more fine-grained checks available: see
1637 the :ref:`list <ubsan-checks>` of specific kinds of
1638 undefined behavior that can be detected and the :ref:`list <cfi-schemes>`
1639 of control flow integrity schemes.
1641 The ``-fsanitize=`` argument must also be provided when linking, in
1642 order to link to the appropriate runtime library.
1644 It is not possible to combine more than one of the ``-fsanitize=address``,
1645 ``-fsanitize=thread``, and ``-fsanitize=memory`` checkers in the same
1648 **-f[no-]sanitize-recover=check1,check2,...**
1650 **-f[no-]sanitize-recover[=all]**
1652 Controls which checks enabled by ``-fsanitize=`` flag are non-fatal.
1653 If the check is fatal, program will halt after the first error
1654 of this kind is detected and error report is printed.
1656 By default, non-fatal checks are those enabled by
1657 :doc:`UndefinedBehaviorSanitizer`,
1658 except for ``-fsanitize=return`` and ``-fsanitize=unreachable``. Some
1659 sanitizers may not support recovery (or not support it by default
1660 e.g. :doc:`AddressSanitizer`), and always crash the program after the issue
1663 Note that the ``-fsanitize-trap`` flag has precedence over this flag.
1664 This means that if a check has been configured to trap elsewhere on the
1665 command line, or if the check traps by default, this flag will not have
1666 any effect unless that sanitizer's trapping behavior is disabled with
1667 ``-fno-sanitize-trap``.
1669 For example, if a command line contains the flags ``-fsanitize=undefined
1670 -fsanitize-trap=undefined``, the flag ``-fsanitize-recover=alignment``
1671 will have no effect on its own; it will need to be accompanied by
1672 ``-fno-sanitize-trap=alignment``.
1674 **-f[no-]sanitize-trap=check1,check2,...**
1676 **-f[no-]sanitize-trap[=all]**
1678 Controls which checks enabled by the ``-fsanitize=`` flag trap. This
1679 option is intended for use in cases where the sanitizer runtime cannot
1680 be used (for instance, when building libc or a kernel module), or where
1681 the binary size increase caused by the sanitizer runtime is a concern.
1683 This flag is only compatible with :doc:`control flow integrity
1684 <ControlFlowIntegrity>` schemes and :doc:`UndefinedBehaviorSanitizer`
1685 checks other than ``vptr``.
1687 This flag is enabled by default for sanitizers in the ``cfi`` group.
1689 .. option:: -fsanitize-ignorelist=/path/to/ignorelist/file
1691 Disable or modify sanitizer checks for objects (source files, functions,
1692 variables, types) listed in the file. See
1693 :doc:`SanitizerSpecialCaseList` for file format description.
1695 .. option:: -fno-sanitize-ignorelist
1697 Don't use ignorelist file, if it was specified earlier in the command line.
1699 **-f[no-]sanitize-coverage=[type,features,...]**
1701 Enable simple code coverage in addition to certain sanitizers.
1702 See :doc:`SanitizerCoverage` for more details.
1704 **-f[no-]sanitize-address-outline-instrumentation**
1706 Controls how address sanitizer code is generated. If enabled will always use
1707 a function call instead of inlining the code. Turning this option on could
1708 reduce the binary size, but might result in a worse run-time performance.
1710 See :doc: `AddressSanitizer` for more details.
1712 **-f[no-]sanitize-stats**
1714 Enable simple statistics gathering for the enabled sanitizers.
1715 See :doc:`SanitizerStats` for more details.
1717 .. option:: -fsanitize-undefined-trap-on-error
1719 Deprecated alias for ``-fsanitize-trap=undefined``.
1721 .. option:: -fsanitize-cfi-cross-dso
1723 Enable cross-DSO control flow integrity checks. This flag modifies
1724 the behavior of sanitizers in the ``cfi`` group to allow checking
1725 of cross-DSO virtual and indirect calls.
1727 .. option:: -fsanitize-cfi-icall-generalize-pointers
1729 Generalize pointers in return and argument types in function type signatures
1730 checked by Control Flow Integrity indirect call checking. See
1731 :doc:`ControlFlowIntegrity` for more details.
1733 .. option:: -fstrict-vtable-pointers
1735 Enable optimizations based on the strict rules for overwriting polymorphic
1736 C++ objects, i.e. the vptr is invariant during an object's lifetime.
1737 This enables better devirtualization. Turned off by default, because it is
1740 .. option:: -fwhole-program-vtables
1742 Enable whole-program vtable optimizations, such as single-implementation
1743 devirtualization and virtual constant propagation, for classes with
1744 :doc:`hidden LTO visibility <LTOVisibility>`. Requires ``-flto``.
1746 .. option:: -fforce-emit-vtables
1748 In order to improve devirtualization, forces emitting of vtables even in
1749 modules where it isn't necessary. It causes more inline virtual functions
1752 .. option:: -fno-assume-sane-operator-new
1754 Don't assume that the C++'s new operator is sane.
1756 This option tells the compiler to do not assume that C++'s global
1757 new operator will always return a pointer that does not alias any
1758 other pointer when the function returns.
1760 .. option:: -ftrap-function=[name]
1762 Instruct code generator to emit a function call to the specified
1763 function name for ``__builtin_trap()``.
1765 LLVM code generator translates ``__builtin_trap()`` to a trap
1766 instruction if it is supported by the target ISA. Otherwise, the
1767 builtin is translated into a call to ``abort``. If this option is
1768 set, then the code generator will always lower the builtin to a call
1769 to the specified function regardless of whether the target ISA has a
1770 trap instruction. This option is useful for environments (e.g.
1771 deeply embedded) where a trap cannot be properly handled, or when
1772 some custom behavior is desired.
1774 .. option:: -ftls-model=[model]
1776 Select which TLS model to use.
1778 Valid values are: ``global-dynamic``, ``local-dynamic``,
1779 ``initial-exec`` and ``local-exec``. The default value is
1780 ``global-dynamic``. The compiler may use a different model if the
1781 selected model is not supported by the target, or if a more
1782 efficient model can be used. The TLS model can be overridden per
1783 variable using the ``tls_model`` attribute.
1785 .. option:: -femulated-tls
1787 Select emulated TLS model, which overrides all -ftls-model choices.
1789 In emulated TLS mode, all access to TLS variables are converted to
1790 calls to __emutls_get_address in the runtime library.
1792 .. option:: -mhwdiv=[values]
1794 Select the ARM modes (arm or thumb) that support hardware division
1797 Valid values are: ``arm``, ``thumb`` and ``arm,thumb``.
1798 This option is used to indicate which mode (arm or thumb) supports
1799 hardware division instructions. This only applies to the ARM
1802 .. option:: -m[no-]crc
1804 Enable or disable CRC instructions.
1806 This option is used to indicate whether CRC instructions are to
1807 be generated. This only applies to the ARM architecture.
1809 CRC instructions are enabled by default on ARMv8.
1811 .. option:: -mgeneral-regs-only
1813 Generate code which only uses the general purpose registers.
1815 This option restricts the generated code to use general registers
1816 only. This only applies to the AArch64 architecture.
1818 .. option:: -mcompact-branches=[values]
1820 Control the usage of compact branches for MIPSR6.
1822 Valid values are: ``never``, ``optimal`` and ``always``.
1823 The default value is ``optimal`` which generates compact branches
1824 when a delay slot cannot be filled. ``never`` disables the usage of
1825 compact branches and ``always`` generates compact branches whenever
1828 **-f[no-]max-type-align=[number]**
1829 Instruct the code generator to not enforce a higher alignment than the given
1830 number (of bytes) when accessing memory via an opaque pointer or reference.
1831 This cap is ignored when directly accessing a variable or when the pointee
1832 type has an explicit “aligned” attribute.
1834 The value should usually be determined by the properties of the system allocator.
1835 Some builtin types, especially vector types, have very high natural alignments;
1836 when working with values of those types, Clang usually wants to use instructions
1837 that take advantage of that alignment. However, many system allocators do
1838 not promise to return memory that is more than 8-byte or 16-byte-aligned. Use
1839 this option to limit the alignment that the compiler can assume for an arbitrary
1840 pointer, which may point onto the heap.
1842 This option does not affect the ABI alignment of types; the layout of structs and
1843 unions and the value returned by the alignof operator remain the same.
1845 This option can be overridden on a case-by-case basis by putting an explicit
1846 “aligned” alignment on a struct, union, or typedef. For example:
1848 .. code-block:: console
1850 #include <immintrin.h>
1851 // Make an aligned typedef of the AVX-512 16-int vector type.
1852 typedef __v16si __aligned_v16si __attribute__((aligned(64)));
1854 void initialize_vector(__aligned_v16si *v) {
1855 // The compiler may assume that ‘v’ is 64-byte aligned, regardless of the
1856 // value of -fmax-type-align.
1859 .. option:: -faddrsig, -fno-addrsig
1861 Controls whether Clang emits an address-significance table into the object
1862 file. Address-significance tables allow linkers to implement `safe ICF
1863 <https://research.google.com/pubs/archive/36912.pdf>`_ without the false
1864 positives that can result from other implementation techniques such as
1865 relocation scanning. Address-significance tables are enabled by default
1866 on ELF targets when using the integrated assembler. This flag currently
1867 only has an effect on ELF targets.
1869 **-f[no]-unique-internal-linkage-names**
1871 Controls whether Clang emits a unique (best-effort) symbol name for internal
1872 linkage symbols. When this option is set, compiler hashes the main source
1873 file path from the command line and appends it to all internal symbols. If a
1874 program contains multiple objects compiled with the same command-line source
1875 file path, the symbols are not guaranteed to be unique. This option is
1876 particularly useful in attributing profile information to the correct
1877 function when multiple functions with the same private linkage name exist
1880 It should be noted that this option cannot guarantee uniqueness and the
1881 following is an example where it is not unique when two modules contain
1882 symbols with the same private linkage name:
1884 .. code-block:: console
1886 $ cd $P/foo && clang -c -funique-internal-linkage-names name_conflict.c
1887 $ cd $P/bar && clang -c -funique-internal-linkage-names name_conflict.c
1888 $ cd $P && clang foo/name_conflict.o && bar/name_conflict.o
1890 **-fbasic-block-sections=[labels, all, list=<arg>, none]**
1892 Controls how Clang emits text sections for basic blocks. With values ``all``
1893 and ``list=<arg>``, each basic block or a subset of basic blocks can be placed
1894 in its own unique section. With the "labels" value, normal text sections are
1895 emitted, but a ``.bb_addr_map`` section is emitted which includes address
1896 offsets for each basic block in the program, relative to the parent function
1899 With the ``list=<arg>`` option, a file containing the subset of basic blocks
1900 that need to placed in unique sections can be specified. The format of the
1901 file is as follows. For example, ``list=spec.txt`` where ``spec.txt`` is the
1910 will place the machine basic block with ``id 2`` in function ``foo`` in a
1911 unique section. It will also place all basic blocks of functions ``bar``
1914 Further, section clusters can also be specified using the ``list=<arg>``
1915 option. For example, ``list=spec.txt`` where ``spec.txt`` contains:
1923 will create two unique sections for function ``foo`` with the first
1924 containing the odd numbered basic blocks and the second containing the
1925 even numbered basic blocks.
1927 Basic block sections allow the linker to reorder basic blocks and enables
1928 link-time optimizations like whole program inter-procedural basic block
1931 Profile Guided Optimization
1932 ---------------------------
1934 Profile information enables better optimization. For example, knowing that a
1935 branch is taken very frequently helps the compiler make better decisions when
1936 ordering basic blocks. Knowing that a function ``foo`` is called more
1937 frequently than another function ``bar`` helps the inliner. Optimization
1938 levels ``-O2`` and above are recommended for use of profile guided optimization.
1940 Clang supports profile guided optimization with two different kinds of
1941 profiling. A sampling profiler can generate a profile with very low runtime
1942 overhead, or you can build an instrumented version of the code that collects
1943 more detailed profile information. Both kinds of profiles can provide execution
1944 counts for instructions in the code and information on branches taken and
1945 function invocation.
1947 Regardless of which kind of profiling you use, be careful to collect profiles
1948 by running your code with inputs that are representative of the typical
1949 behavior. Code that is not exercised in the profile will be optimized as if it
1950 is unimportant, and the compiler may make poor optimization choices for code
1951 that is disproportionately used while profiling.
1953 Differences Between Sampling and Instrumentation
1954 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1956 Although both techniques are used for similar purposes, there are important
1957 differences between the two:
1959 1. Profile data generated with one cannot be used by the other, and there is no
1960 conversion tool that can convert one to the other. So, a profile generated
1961 via ``-fprofile-instr-generate`` must be used with ``-fprofile-instr-use``.
1962 Similarly, sampling profiles generated by external profilers must be
1963 converted and used with ``-fprofile-sample-use``.
1965 2. Instrumentation profile data can be used for code coverage analysis and
1968 3. Sampling profiles can only be used for optimization. They cannot be used for
1969 code coverage analysis. Although it would be technically possible to use
1970 sampling profiles for code coverage, sample-based profiles are too
1971 coarse-grained for code coverage purposes; it would yield poor results.
1973 4. Sampling profiles must be generated by an external tool. The profile
1974 generated by that tool must then be converted into a format that can be read
1975 by LLVM. The section on sampling profilers describes one of the supported
1976 sampling profile formats.
1979 Using Sampling Profilers
1980 ^^^^^^^^^^^^^^^^^^^^^^^^
1982 Sampling profilers are used to collect runtime information, such as
1983 hardware counters, while your application executes. They are typically
1984 very efficient and do not incur a large runtime overhead. The
1985 sample data collected by the profiler can be used during compilation
1986 to determine what the most executed areas of the code are.
1988 Using the data from a sample profiler requires some changes in the way
1989 a program is built. Before the compiler can use profiling information,
1990 the code needs to execute under the profiler. The following is the
1991 usual build cycle when using sample profilers for optimization:
1993 1. Build the code with source line table information. You can use all the
1994 usual build flags that you always build your application with. The only
1995 requirement is that you add ``-gline-tables-only`` or ``-g`` to the
1996 command line. This is important for the profiler to be able to map
1997 instructions back to source line locations.
1999 .. code-block:: console
2001 $ clang++ -O2 -gline-tables-only code.cc -o code
2003 2. Run the executable under a sampling profiler. The specific profiler
2004 you use does not really matter, as long as its output can be converted
2005 into the format that the LLVM optimizer understands. Currently, there
2006 exists a conversion tool for the Linux Perf profiler
2007 (https://perf.wiki.kernel.org/), so these examples assume that you
2008 are using Linux Perf to profile your code.
2010 .. code-block:: console
2012 $ perf record -b ./code
2014 Note the use of the ``-b`` flag. This tells Perf to use the Last Branch
2015 Record (LBR) to record call chains. While this is not strictly required,
2016 it provides better call information, which improves the accuracy of
2019 3. Convert the collected profile data to LLVM's sample profile format.
2020 This is currently supported via the AutoFDO converter ``create_llvm_prof``.
2021 It is available at https://github.com/google/autofdo. Once built and
2022 installed, you can convert the ``perf.data`` file to LLVM using
2025 .. code-block:: console
2027 $ create_llvm_prof --binary=./code --out=code.prof
2029 This will read ``perf.data`` and the binary file ``./code`` and emit
2030 the profile data in ``code.prof``. Note that if you ran ``perf``
2031 without the ``-b`` flag, you need to use ``--use_lbr=false`` when
2032 calling ``create_llvm_prof``.
2034 4. Build the code again using the collected profile. This step feeds
2035 the profile back to the optimizers. This should result in a binary
2036 that executes faster than the original one. Note that you are not
2037 required to build the code with the exact same arguments that you
2038 used in the first step. The only requirement is that you build the code
2039 with ``-gline-tables-only`` and ``-fprofile-sample-use``.
2041 .. code-block:: console
2043 $ clang++ -O2 -gline-tables-only -fprofile-sample-use=code.prof code.cc -o code
2046 Sample Profile Formats
2047 """"""""""""""""""""""
2049 Since external profilers generate profile data in a variety of custom formats,
2050 the data generated by the profiler must be converted into a format that can be
2051 read by the backend. LLVM supports three different sample profile formats:
2053 1. ASCII text. This is the easiest one to generate. The file is divided into
2054 sections, which correspond to each of the functions with profile
2055 information. The format is described below. It can also be generated from
2056 the binary or gcov formats using the ``llvm-profdata`` tool.
2058 2. Binary encoding. This uses a more efficient encoding that yields smaller
2059 profile files. This is the format generated by the ``create_llvm_prof`` tool
2060 in https://github.com/google/autofdo.
2062 3. GCC encoding. This is based on the gcov format, which is accepted by GCC. It
2063 is only interesting in environments where GCC and Clang co-exist. This
2064 encoding is only generated by the ``create_gcov`` tool in
2065 https://github.com/google/autofdo. It can be read by LLVM and
2066 ``llvm-profdata``, but it cannot be generated by either.
2068 If you are using Linux Perf to generate sampling profiles, you can use the
2069 conversion tool ``create_llvm_prof`` described in the previous section.
2070 Otherwise, you will need to write a conversion tool that converts your
2071 profiler's native format into one of these three.
2074 Sample Profile Text Format
2075 """"""""""""""""""""""""""
2077 This section describes the ASCII text format for sampling profiles. It is,
2078 arguably, the easiest one to generate. If you are interested in generating any
2079 of the other two, consult the ``ProfileData`` library in LLVM's source tree
2080 (specifically, ``include/llvm/ProfileData/SampleProfReader.h``).
2082 .. code-block:: console
2084 function1:total_samples:total_head_samples
2085 offset1[.discriminator]: number_of_samples [fn1:num fn2:num ... ]
2086 offset2[.discriminator]: number_of_samples [fn3:num fn4:num ... ]
2088 offsetN[.discriminator]: number_of_samples [fn5:num fn6:num ... ]
2089 offsetA[.discriminator]: fnA:num_of_total_samples
2090 offsetA1[.discriminator]: number_of_samples [fn7:num fn8:num ... ]
2091 offsetA1[.discriminator]: number_of_samples [fn9:num fn10:num ... ]
2092 offsetB[.discriminator]: fnB:num_of_total_samples
2093 offsetB1[.discriminator]: number_of_samples [fn11:num fn12:num ... ]
2095 This is a nested tree in which the indentation represents the nesting level
2096 of the inline stack. There are no blank lines in the file. And the spacing
2097 within a single line is fixed. Additional spaces will result in an error
2098 while reading the file.
2100 Any line starting with the '#' character is completely ignored.
2102 Inlined calls are represented with indentation. The Inline stack is a
2103 stack of source locations in which the top of the stack represents the
2104 leaf function, and the bottom of the stack represents the actual
2105 symbol to which the instruction belongs.
2107 Function names must be mangled in order for the profile loader to
2108 match them in the current translation unit. The two numbers in the
2109 function header specify how many total samples were accumulated in the
2110 function (first number), and the total number of samples accumulated
2111 in the prologue of the function (second number). This head sample
2112 count provides an indicator of how frequently the function is invoked.
2114 There are two types of lines in the function body.
2116 - Sampled line represents the profile information of a source location.
2117 ``offsetN[.discriminator]: number_of_samples [fn5:num fn6:num ... ]``
2119 - Callsite line represents the profile information of an inlined callsite.
2120 ``offsetA[.discriminator]: fnA:num_of_total_samples``
2122 Each sampled line may contain several items. Some are optional (marked
2125 a. Source line offset. This number represents the line number
2126 in the function where the sample was collected. The line number is
2127 always relative to the line where symbol of the function is
2128 defined. So, if the function has its header at line 280, the offset
2129 13 is at line 293 in the file.
2131 Note that this offset should never be a negative number. This could
2132 happen in cases like macros. The debug machinery will register the
2133 line number at the point of macro expansion. So, if the macro was
2134 expanded in a line before the start of the function, the profile
2135 converter should emit a 0 as the offset (this means that the optimizers
2136 will not be able to associate a meaningful weight to the instructions
2139 b. [OPTIONAL] Discriminator. This is used if the sampled program
2140 was compiled with DWARF discriminator support
2141 (http://wiki.dwarfstd.org/index.php?title=Path_Discriminators).
2142 DWARF discriminators are unsigned integer values that allow the
2143 compiler to distinguish between multiple execution paths on the
2144 same source line location.
2146 For example, consider the line of code ``if (cond) foo(); else bar();``.
2147 If the predicate ``cond`` is true 80% of the time, then the edge
2148 into function ``foo`` should be considered to be taken most of the
2149 time. But both calls to ``foo`` and ``bar`` are at the same source
2150 line, so a sample count at that line is not sufficient. The
2151 compiler needs to know which part of that line is taken more
2154 This is what discriminators provide. In this case, the calls to
2155 ``foo`` and ``bar`` will be at the same line, but will have
2156 different discriminator values. This allows the compiler to correctly
2157 set edge weights into ``foo`` and ``bar``.
2159 c. Number of samples. This is an integer quantity representing the
2160 number of samples collected by the profiler at this source
2163 d. [OPTIONAL] Potential call targets and samples. If present, this
2164 line contains a call instruction. This models both direct and
2165 number of samples. For example,
2167 .. code-block:: console
2169 130: 7 foo:3 bar:2 baz:7
2171 The above means that at relative line offset 130 there is a call
2172 instruction that calls one of ``foo()``, ``bar()`` and ``baz()``,
2173 with ``baz()`` being the relatively more frequently called target.
2175 As an example, consider a program with the call chain ``main -> foo -> bar``.
2176 When built with optimizations enabled, the compiler may inline the
2177 calls to ``bar`` and ``foo`` inside ``main``. The generated profile
2178 could then be something like this:
2180 .. code-block:: console
2188 This profile indicates that there were a total of 35,504 samples
2189 collected in main. All of those were at line 1 (the call to ``foo``).
2190 Of those, 31,977 were spent inside the body of ``bar``. The last line
2191 of the profile (``2: 0``) corresponds to line 2 inside ``main``. No
2192 samples were collected there.
2194 Profiling with Instrumentation
2195 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2197 Clang also supports profiling via instrumentation. This requires building a
2198 special instrumented version of the code and has some runtime
2199 overhead during the profiling, but it provides more detailed results than a
2200 sampling profiler. It also provides reproducible results, at least to the
2201 extent that the code behaves consistently across runs.
2203 Here are the steps for using profile guided optimization with
2206 1. Build an instrumented version of the code by compiling and linking with the
2207 ``-fprofile-instr-generate`` option.
2209 .. code-block:: console
2211 $ clang++ -O2 -fprofile-instr-generate code.cc -o code
2213 2. Run the instrumented executable with inputs that reflect the typical usage.
2214 By default, the profile data will be written to a ``default.profraw`` file
2215 in the current directory. You can override that default by using option
2216 ``-fprofile-instr-generate=`` or by setting the ``LLVM_PROFILE_FILE``
2217 environment variable to specify an alternate file. If non-default file name
2218 is specified by both the environment variable and the command line option,
2219 the environment variable takes precedence. The file name pattern specified
2220 can include different modifiers: ``%p``, ``%h``, and ``%m``.
2222 Any instance of ``%p`` in that file name will be replaced by the process
2223 ID, so that you can easily distinguish the profile output from multiple
2226 .. code-block:: console
2228 $ LLVM_PROFILE_FILE="code-%p.profraw" ./code
2230 The modifier ``%h`` can be used in scenarios where the same instrumented
2231 binary is run in multiple different host machines dumping profile data
2232 to a shared network based storage. The ``%h`` specifier will be substituted
2233 with the hostname so that profiles collected from different hosts do not
2236 While the use of ``%p`` specifier can reduce the likelihood for the profiles
2237 dumped from different processes to clobber each other, such clobbering can still
2238 happen because of the ``pid`` re-use by the OS. Another side-effect of using
2239 ``%p`` is that the storage requirement for raw profile data files is greatly
2240 increased. To avoid issues like this, the ``%m`` specifier can used in the profile
2241 name. When this specifier is used, the profiler runtime will substitute ``%m``
2242 with a unique integer identifier associated with the instrumented binary. Additionally,
2243 multiple raw profiles dumped from different processes that share a file system (can be
2244 on different hosts) will be automatically merged by the profiler runtime during the
2245 dumping. If the program links in multiple instrumented shared libraries, each library
2246 will dump the profile data into its own profile data file (with its unique integer
2247 id embedded in the profile name). Note that the merging enabled by ``%m`` is for raw
2248 profile data generated by profiler runtime. The resulting merged "raw" profile data
2249 file still needs to be converted to a different format expected by the compiler (
2252 .. code-block:: console
2254 $ LLVM_PROFILE_FILE="code-%m.profraw" ./code
2257 3. Combine profiles from multiple runs and convert the "raw" profile format to
2258 the input expected by clang. Use the ``merge`` command of the
2259 ``llvm-profdata`` tool to do this.
2261 .. code-block:: console
2263 $ llvm-profdata merge -output=code.profdata code-*.profraw
2265 Note that this step is necessary even when there is only one "raw" profile,
2266 since the merge operation also changes the file format.
2268 4. Build the code again using the ``-fprofile-instr-use`` option to specify the
2269 collected profile data.
2271 .. code-block:: console
2273 $ clang++ -O2 -fprofile-instr-use=code.profdata code.cc -o code
2275 You can repeat step 4 as often as you like without regenerating the
2276 profile. As you make changes to your code, clang may no longer be able to
2277 use the profile data. It will warn you when this happens.
2279 Profile generation using an alternative instrumentation method can be
2280 controlled by the GCC-compatible flags ``-fprofile-generate`` and
2281 ``-fprofile-use``. Although these flags are semantically equivalent to
2282 their GCC counterparts, they *do not* handle GCC-compatible profiles.
2283 They are only meant to implement GCC's semantics with respect to
2284 profile creation and use. Flag ``-fcs-profile-generate`` also instruments
2285 programs using the same instrumentation method as ``-fprofile-generate``.
2287 .. option:: -fprofile-generate[=<dirname>]
2289 The ``-fprofile-generate`` and ``-fprofile-generate=`` flags will use
2290 an alternative instrumentation method for profile generation. When
2291 given a directory name, it generates the profile file
2292 ``default_%m.profraw`` in the directory named ``dirname`` if specified.
2293 If ``dirname`` does not exist, it will be created at runtime. ``%m`` specifier
2294 will be substituted with a unique id documented in step 2 above. In other words,
2295 with ``-fprofile-generate[=<dirname>]`` option, the "raw" profile data automatic
2296 merging is turned on by default, so there will no longer any risk of profile
2297 clobbering from different running processes. For example,
2299 .. code-block:: console
2301 $ clang++ -O2 -fprofile-generate=yyy/zzz code.cc -o code
2303 When ``code`` is executed, the profile will be written to the file
2304 ``yyy/zzz/default_xxxx.profraw``.
2306 To generate the profile data file with the compiler readable format, the
2307 ``llvm-profdata`` tool can be used with the profile directory as the input:
2309 .. code-block:: console
2311 $ llvm-profdata merge -output=code.profdata yyy/zzz/
2313 If the user wants to turn off the auto-merging feature, or simply override the
2314 the profile dumping path specified at command line, the environment variable
2315 ``LLVM_PROFILE_FILE`` can still be used to override
2316 the directory and filename for the profile file at runtime.
2318 .. option:: -fcs-profile-generate[=<dirname>]
2320 The ``-fcs-profile-generate`` and ``-fcs-profile-generate=`` flags will use
2321 the same instrumentation method, and generate the same profile as in the
2322 ``-fprofile-generate`` and ``-fprofile-generate=`` flags. The difference is
2323 that the instrumentation is performed after inlining so that the resulted
2324 profile has a better context sensitive information. They cannot be used
2325 together with ``-fprofile-generate`` and ``-fprofile-generate=`` flags.
2326 They are typically used in conjunction with ``-fprofile-use`` flag.
2327 The profile generated by ``-fcs-profile-generate`` and ``-fprofile-generate``
2328 can be merged by llvm-profdata. A use example:
2330 .. code-block:: console
2332 $ clang++ -O2 -fprofile-generate=yyy/zzz code.cc -o code
2334 $ llvm-profdata merge -output=code.profdata yyy/zzz/
2336 The first few steps are the same as that in ``-fprofile-generate``
2337 compilation. Then perform a second round of instrumentation.
2339 .. code-block:: console
2341 $ clang++ -O2 -fprofile-use=code.profdata -fcs-profile-generate=sss/ttt \
2344 $ llvm-profdata merge -output=cs_code.profdata sss/ttt code.profdata
2346 The resulted ``cs_code.prodata`` combines ``code.profdata`` and the profile
2347 generated from binary ``cs_code``. Profile ``cs_code.profata`` can be used by
2348 ``-fprofile-use`` compilation.
2350 .. code-block:: console
2352 $ clang++ -O2 -fprofile-use=cs_code.profdata
2354 The above command will read both profiles to the compiler at the identical
2355 point of instrumentations.
2357 .. option:: -fprofile-use[=<pathname>]
2359 Without any other arguments, ``-fprofile-use`` behaves identically to
2360 ``-fprofile-instr-use``. Otherwise, if ``pathname`` is the full path to a
2361 profile file, it reads from that file. If ``pathname`` is a directory name,
2362 it reads from ``pathname/default.profdata``.
2364 .. option:: -fprofile-update[=<method>]
2366 Unless ``-fsanitize=thread`` is specified, the default is ``single``, which
2367 uses non-atomic increments. The counters can be inaccurate under thread
2368 contention. ``atomic`` uses atomic increments which is accurate but has
2369 overhead. ``prefer-atomic`` will be transformed to ``atomic`` when supported
2370 by the target, or ``single`` otherwise.
2372 This option currently works with ``-fprofile-arcs`` and ``-fprofile-instr-generate``,
2373 but not with ``-fprofile-generate``.
2375 Disabling Instrumentation
2376 ^^^^^^^^^^^^^^^^^^^^^^^^^
2378 In certain situations, it may be useful to disable profile generation or use
2379 for specific files in a build, without affecting the main compilation flags
2380 used for the other files in the project.
2382 In these cases, you can use the flag ``-fno-profile-instr-generate`` (or
2383 ``-fno-profile-generate``) to disable profile generation, and
2384 ``-fno-profile-instr-use`` (or ``-fno-profile-use``) to disable profile use.
2386 Note that these flags should appear after the corresponding profile
2387 flags to have an effect.
2391 When none of the translation units inside a binary is instrumented, in the
2392 case of Fuchsia the profile runtime will not be linked into the binary and
2393 no profile will be produced, while on other platforms the profile runtime
2394 will be linked and profile will be produced but there will not be any
2397 Instrumenting only selected files or functions
2398 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2400 Sometimes it's useful to only instrument certain files or functions. For
2401 example in automated testing infrastructure, it may be desirable to only
2402 instrument files or functions that were modified by a patch to reduce the
2403 overhead of instrumenting a full system.
2405 This can be done using the ``-fprofile-list`` option.
2407 .. option:: -fprofile-list=<pathname>
2409 This option can be used to apply profile instrumentation only to selected
2410 files or functions. ``pathname`` should point to a file in the
2411 :doc:`SanitizerSpecialCaseList` format which selects which files and
2412 functions to instrument.
2414 .. code-block:: console
2416 $ echo "fun:test" > fun.list
2417 $ clang++ -O2 -fprofile-instr-generate -fprofile-list=fun.list code.cc -o code
2419 The option can be specified multiple times to pass multiple files.
2421 .. code-block:: console
2423 $ echo "!fun:*test*" > fun.list
2424 $ echo "src:code.cc" > src.list
2425 % clang++ -O2 -fprofile-instr-generate -fcoverage-mapping -fprofile-list=fun.list -fprofile-list=code.list code.cc -o code
2427 To filter individual functions or entire source files using ``fun:<name>`` or
2428 ``src:<file>`` respectively. To exclude a function or a source file, use
2429 ``!fun:<name>`` or ``!src:<file>`` respectively. The format also supports
2430 wildcard expansion. The compiler generated functions are assumed to be located
2431 in the main source file. It is also possible to restrict the filter to a
2432 particular instrumentation type by using a named section.
2434 .. code-block:: none
2436 # all functions whose name starts with foo will be instrumented.
2439 # except for foo1 which will be excluded from instrumentation.
2442 # every function in path/to/foo.cc will be instrumented.
2445 # bar will be instrumented only when using backend instrumentation.
2446 # Recognized section names are clang, llvm and csllvm.
2450 When the file contains only excludes, all files and functions except for the
2451 excluded ones will be instrumented. Otherwise, only the files and functions
2452 specified will be instrumented.
2457 When the program is compiled after a change that affects many symbol names,
2458 pre-existing profile data may no longer match the program. For example:
2460 * switching from libstdc++ to libc++ will result in the mangled names of all
2461 functions taking standard library types to change
2462 * renaming a widely-used type in C++ will result in the mangled names of all
2463 functions that have parameters involving that type to change
2464 * moving from a 32-bit compilation to a 64-bit compilation may change the
2465 underlying type of ``size_t`` and similar types, resulting in changes to
2468 Clang allows use of a profile remapping file to specify that such differences
2469 in mangled names should be ignored when matching the profile data against the
2472 .. option:: -fprofile-remapping-file=<file>
2474 Specifies a file containing profile remapping information, that will be
2475 used to match mangled names in the profile data to mangled names in the
2478 The profile remapping file is a text file containing lines of the form
2480 .. code-block:: text
2482 fragmentkind fragment1 fragment2
2484 where ``fragmentkind`` is one of ``name``, ``type``, or ``encoding``,
2485 indicating whether the following mangled name fragments are
2486 <`name <https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangle.name>`_>s,
2487 <`type <https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangle.type>`_>s, or
2488 <`encoding <https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangle.encoding>`_>s,
2490 Blank lines and lines starting with ``#`` are ignored.
2492 For convenience, built-in <substitution>s such as ``St`` and ``Ss``
2493 are accepted as <name>s (even though they technically are not <name>s).
2495 For example, to specify that ``absl::string_view`` and ``std::string_view``
2496 should be treated as equivalent when matching profile data, the following
2497 remapping file could be used:
2499 .. code-block:: text
2501 # absl::string_view is considered equivalent to std::string_view
2502 type N4absl11string_viewE St17basic_string_viewIcSt11char_traitsIcEE
2504 # std:: might be std::__1:: in libc++ or std::__cxx11:: in libstdc++
2506 name 3std St7__cxx11
2508 Matching profile data using a profile remapping file is supported on a
2509 best-effort basis. For example, information regarding indirect call targets is
2510 currently not remapped. For best results, you are encouraged to generate new
2511 profile data matching the updated program, or to remap the profile data
2512 using the ``llvm-cxxmap`` and ``llvm-profdata merge`` tools.
2516 Profile data remapping support is currently only implemented for LLVM's
2517 new pass manager, which can be enabled with
2518 ``-fexperimental-new-pass-manager``.
2522 Profile data remapping is currently only supported for C++ mangled names
2523 following the Itanium C++ ABI mangling scheme. This covers all C++ targets
2524 supported by Clang other than Windows.
2526 GCOV-based Profiling
2527 --------------------
2529 GCOV is a test coverage program, it helps to know how often a line of code
2530 is executed. When instrumenting the code with ``--coverage`` option, some
2531 counters are added for each edge linking basic blocks.
2533 At compile time, gcno files are generated containing information about
2534 blocks and edges between them. At runtime the counters are incremented and at
2535 exit the counters are dumped in gcda files.
2537 The tool ``llvm-cov gcov`` will parse gcno, gcda and source files to generate
2538 a report ``.c.gcov``.
2540 .. option:: -fprofile-filter-files=[regexes]
2542 Define a list of regexes separated by a semi-colon.
2543 If a file name matches any of the regexes then the file is instrumented.
2545 .. code-block:: console
2547 $ clang --coverage -fprofile-filter-files=".*\.c$" foo.c
2549 For example, this will only instrument files finishing with ``.c``, skipping ``.h`` files.
2551 .. option:: -fprofile-exclude-files=[regexes]
2553 Define a list of regexes separated by a semi-colon.
2554 If a file name doesn't match all the regexes then the file is instrumented.
2556 .. code-block:: console
2558 $ clang --coverage -fprofile-exclude-files="^/usr/include/.*$" foo.c
2560 For example, this will instrument all the files except the ones in ``/usr/include``.
2562 If both options are used then a file is instrumented if its name matches any
2563 of the regexes from ``-fprofile-filter-list`` and doesn't match all the regexes
2564 from ``-fprofile-exclude-list``.
2566 .. code-block:: console
2568 $ clang --coverage -fprofile-exclude-files="^/usr/include/.*$" \
2569 -fprofile-filter-files="^/usr/.*$"
2571 In that case ``/usr/foo/oof.h`` is instrumented since it matches the filter regex and
2572 doesn't match the exclude regex, but ``/usr/include/foo.h`` doesn't since it matches
2575 Controlling Debug Information
2576 -----------------------------
2578 Controlling Size of Debug Information
2579 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2581 Debug info kind generated by Clang can be set by one of the flags listed
2582 below. If multiple flags are present, the last one is used.
2586 Don't generate any debug info (default).
2588 .. option:: -gline-tables-only
2590 Generate line number tables only.
2592 This kind of debug info allows to obtain stack traces with function names,
2593 file names and line numbers (by such tools as ``gdb`` or ``addr2line``). It
2594 doesn't contain any other data (e.g. description of local variables or
2595 function parameters).
2597 .. option:: -fstandalone-debug
2599 Clang supports a number of optimizations to reduce the size of debug
2600 information in the binary. They work based on the assumption that
2601 the debug type information can be spread out over multiple
2602 compilation units. For instance, Clang will not emit type
2603 definitions for types that are not needed by a module and could be
2604 replaced with a forward declaration. Further, Clang will only emit
2605 type info for a dynamic C++ class in the module that contains the
2606 vtable for the class.
2608 The **-fstandalone-debug** option turns off these optimizations.
2609 This is useful when working with 3rd-party libraries that don't come
2610 with debug information. Note that Clang will never emit type
2611 information for types that are not referenced at all by the program.
2613 .. option:: -fno-standalone-debug
2615 On Darwin **-fstandalone-debug** is enabled by default. The
2616 **-fno-standalone-debug** option can be used to get to turn on the
2617 vtable-based optimization described above.
2619 .. option:: -fuse-ctor-homing
2621 This optimization is similar to the optimizations that are enabled as part
2622 of -fno-standalone-debug. Here, Clang only emits type info for a
2623 non-trivial, non-aggregate C++ class in the modules that contain a
2624 definition of one of its constructors. This relies on the additional
2625 assumption that all classes that are not trivially constructible have a
2626 non-trivial constructor that is used somewhere. The negation,
2627 -fno-use-ctor-homing, ensures that constructor homing is not used.
2629 This flag is not enabled by default, and needs to be used with -cc1 or
2634 Generate complete debug info.
2636 .. option:: -feliminate-unused-debug-types
2638 By default, Clang does not emit type information for types that are defined
2639 but not used in a program. To retain the debug info for these unused types,
2640 the negation **-fno-eliminate-unused-debug-types** can be used.
2642 Controlling Macro Debug Info Generation
2643 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2645 Debug info for C preprocessor macros increases the size of debug information in
2646 the binary. Macro debug info generated by Clang can be controlled by the flags
2649 .. option:: -fdebug-macro
2651 Generate debug info for preprocessor macros. This flag is discarded when
2654 .. option:: -fno-debug-macro
2656 Do not generate debug info for preprocessor macros (default).
2658 Controlling Debugger "Tuning"
2659 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2661 While Clang generally emits standard DWARF debug info (http://dwarfstd.org),
2662 different debuggers may know how to take advantage of different specific DWARF
2663 features. You can "tune" the debug info for one of several different debuggers.
2665 .. option:: -ggdb, -glldb, -gsce, -gdbx
2667 Tune the debug info for the ``gdb``, ``lldb``, Sony PlayStation\ |reg|
2668 debugger, or ``dbx``, respectively. Each of these options implies **-g**.
2669 (Therefore, if you want both **-gline-tables-only** and debugger tuning, the
2670 tuning option must come first.)
2672 Controlling LLVM IR Output
2673 --------------------------
2675 Controlling Value Names in LLVM IR
2676 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2678 Emitting value names in LLVM IR increases the size and verbosity of the IR.
2679 By default, value names are only emitted in assertion-enabled builds of Clang.
2680 However, when reading IR it can be useful to re-enable the emission of value
2681 names to improve readability.
2683 .. option:: -fdiscard-value-names
2685 Discard value names when generating LLVM IR.
2687 .. option:: -fno-discard-value-names
2689 Do not discard value names when generating LLVM IR. This option can be used
2690 to re-enable names for release builds of Clang.
2693 Comment Parsing Options
2694 -----------------------
2696 Clang parses Doxygen and non-Doxygen style documentation comments and attaches
2697 them to the appropriate declaration nodes. By default, it only parses
2698 Doxygen-style comments and ignores ordinary comments starting with ``//`` and
2701 .. option:: -Wdocumentation
2703 Emit warnings about use of documentation comments. This warning group is off
2706 This includes checking that ``\param`` commands name parameters that actually
2707 present in the function signature, checking that ``\returns`` is used only on
2708 functions that actually return a value etc.
2710 .. option:: -Wno-documentation-unknown-command
2712 Don't warn when encountering an unknown Doxygen command.
2714 .. option:: -fparse-all-comments
2716 Parse all comments as documentation comments (including ordinary comments
2717 starting with ``//`` and ``/*``).
2719 .. option:: -fcomment-block-commands=[commands]
2721 Define custom documentation commands as block commands. This allows Clang to
2722 construct the correct AST for these custom commands, and silences warnings
2723 about unknown commands. Several commands must be separated by a comma
2724 *without trailing space*; e.g. ``-fcomment-block-commands=foo,bar`` defines
2725 custom commands ``\foo`` and ``\bar``.
2727 It is also possible to use ``-fcomment-block-commands`` several times; e.g.
2728 ``-fcomment-block-commands=foo -fcomment-block-commands=bar`` does the same
2736 The support for standard C in clang is feature-complete except for the
2737 C99 floating-point pragmas.
2739 Extensions supported by clang
2740 -----------------------------
2742 See :doc:`LanguageExtensions`.
2744 Differences between various standard modes
2745 ------------------------------------------
2747 clang supports the -std option, which changes what language mode clang uses.
2748 The supported modes for C are c89, gnu89, c94, c99, gnu99, c11, gnu11, c17,
2749 gnu17, c2x, gnu2x, and various aliases for those modes. If no -std option is
2750 specified, clang defaults to gnu17 mode. Many C99 and C11 features are
2751 supported in earlier modes as a conforming extension, with a warning. Use
2752 ``-pedantic-errors`` to request an error if a feature from a later standard
2753 revision is used in an earlier mode.
2755 Differences between all ``c*`` and ``gnu*`` modes:
2757 - ``c*`` modes define "``__STRICT_ANSI__``".
2758 - Target-specific defines not prefixed by underscores, like ``linux``,
2759 are defined in ``gnu*`` modes.
2760 - Trigraphs default to being off in ``gnu*`` modes; they can be enabled
2761 by the ``-trigraphs`` option.
2762 - The parser recognizes ``asm`` and ``typeof`` as keywords in ``gnu*`` modes;
2763 the variants ``__asm__`` and ``__typeof__`` are recognized in all modes.
2764 - The parser recognizes ``inline`` as a keyword in ``gnu*`` mode, in
2765 addition to recognizing it in the ``*99`` and later modes for which it is
2766 part of the ISO C standard. The variant ``__inline__`` is recognized in all
2768 - The Apple "blocks" extension is recognized by default in ``gnu*`` modes
2769 on some platforms; it can be enabled in any mode with the ``-fblocks``
2772 Differences between ``*89`` and ``*94`` modes:
2774 - Digraphs are not recognized in c89 mode.
2776 Differences between ``*94`` and ``*99`` modes:
2778 - The ``*99`` modes default to implementing ``inline`` / ``__inline__``
2779 as specified in C99, while the ``*89`` modes implement the GNU version.
2780 This can be overridden for individual functions with the ``__gnu_inline__``
2782 - The scope of names defined inside a ``for``, ``if``, ``switch``, ``while``,
2783 or ``do`` statement is different. (example: ``if ((struct x {int x;}*)0) {}``.)
2784 - ``__STDC_VERSION__`` is not defined in ``*89`` modes.
2785 - ``inline`` is not recognized as a keyword in ``c89`` mode.
2786 - ``restrict`` is not recognized as a keyword in ``*89`` modes.
2787 - Commas are allowed in integer constant expressions in ``*99`` modes.
2788 - Arrays which are not lvalues are not implicitly promoted to pointers
2790 - Some warnings are different.
2792 Differences between ``*99`` and ``*11`` modes:
2794 - Warnings for use of C11 features are disabled.
2795 - ``__STDC_VERSION__`` is defined to ``201112L`` rather than ``199901L``.
2797 Differences between ``*11`` and ``*17`` modes:
2799 - ``__STDC_VERSION__`` is defined to ``201710L`` rather than ``201112L``.
2801 GCC extensions not implemented yet
2802 ----------------------------------
2804 clang tries to be compatible with gcc as much as possible, but some gcc
2805 extensions are not implemented yet:
2807 - clang does not support decimal floating point types (``_Decimal32`` and
2809 - clang does not support nested functions; this is a complex feature
2810 which is infrequently used, so it is unlikely to be implemented
2811 anytime soon. In C++11 it can be emulated by assigning lambda
2812 functions to local variables, e.g:
2816 auto const local_function = [&](int parameter) {
2822 - clang only supports global register variables when the register specified
2823 is non-allocatable (e.g. the stack pointer). Support for general global
2824 register variables is unlikely to be implemented soon because it requires
2825 additional LLVM backend support.
2826 - clang does not support static initialization of flexible array
2827 members. This appears to be a rarely used extension, but could be
2828 implemented pending user demand.
2829 - clang does not support
2830 ``__builtin_va_arg_pack``/``__builtin_va_arg_pack_len``. This is
2831 used rarely, but in some potentially interesting places, like the
2832 glibc headers, so it may be implemented pending user demand. Note
2833 that because clang pretends to be like GCC 4.2, and this extension
2834 was introduced in 4.3, the glibc headers will not try to use this
2835 extension with clang at the moment.
2836 - clang does not support the gcc extension for forward-declaring
2837 function parameters; this has not shown up in any real-world code
2838 yet, though, so it might never be implemented.
2840 This is not a complete list; if you find an unsupported extension
2841 missing from this list, please send an e-mail to cfe-dev. This list
2842 currently excludes C++; see :ref:`C++ Language Features <cxx>`. Also, this
2843 list does not include bugs in mostly-implemented features; please see
2845 tracker <https://bugs.llvm.org/buglist.cgi?quicksearch=product%3Aclang+component%3A-New%2BBugs%2CAST%2CBasic%2CDriver%2CHeaders%2CLLVM%2BCodeGen%2Cparser%2Cpreprocessor%2CSemantic%2BAnalyzer>`_
2846 for known existing bugs (FIXME: Is there a section for bug-reporting
2847 guidelines somewhere?).
2849 Intentionally unsupported GCC extensions
2850 ----------------------------------------
2852 - clang does not support the gcc extension that allows variable-length
2853 arrays in structures. This is for a few reasons: one, it is tricky to
2854 implement, two, the extension is completely undocumented, and three,
2855 the extension appears to be rarely used. Note that clang *does*
2856 support flexible array members (arrays with a zero or unspecified
2857 size at the end of a structure).
2858 - GCC accepts many expression forms that are not valid integer constant
2859 expressions in bit-field widths, enumerator constants, case labels,
2860 and in array bounds at global scope. Clang also accepts additional
2861 expression forms in these contexts, but constructs that GCC accepts due to
2862 simplifications GCC performs while parsing, such as ``x - x`` (where ``x`` is a
2863 variable) will likely never be accepted by Clang.
2864 - clang does not support ``__builtin_apply`` and friends; this extension
2865 is extremely obscure and difficult to implement reliably.
2869 Microsoft extensions
2870 --------------------
2872 clang has support for many extensions from Microsoft Visual C++. To enable these
2873 extensions, use the ``-fms-extensions`` command-line option. This is the default
2874 for Windows targets. Clang does not implement every pragma or declspec provided
2875 by MSVC, but the popular ones, such as ``__declspec(dllexport)`` and ``#pragma
2876 comment(lib)`` are well supported.
2878 clang has a ``-fms-compatibility`` flag that makes clang accept enough
2879 invalid C++ to be able to parse most Microsoft headers. For example, it
2880 allows `unqualified lookup of dependent base class members
2881 <https://clang.llvm.org/compatibility.html#dep_lookup_bases>`_, which is
2882 a common compatibility issue with clang. This flag is enabled by default
2883 for Windows targets.
2885 ``-fdelayed-template-parsing`` lets clang delay parsing of function template
2886 definitions until the end of a translation unit. This flag is enabled by
2887 default for Windows targets.
2889 For compatibility with existing code that compiles with MSVC, clang defines the
2890 ``_MSC_VER`` and ``_MSC_FULL_VER`` macros. These default to the values of 1800
2891 and 180000000 respectively, making clang look like an early release of Visual
2892 C++ 2013. The ``-fms-compatibility-version=`` flag overrides these values. It
2893 accepts a dotted version tuple, such as 19.00.23506. Changing the MSVC
2894 compatibility version makes clang behave more like that version of MSVC. For
2895 example, ``-fms-compatibility-version=19`` will enable C++14 features and define
2896 ``char16_t`` and ``char32_t`` as builtin types.
2900 C++ Language Features
2901 =====================
2903 clang fully implements all of standard C++98 except for exported
2904 templates (which were removed in C++11), all of standard C++11,
2905 C++14, and C++17, and most of C++20.
2907 See the `C++ support in Clang <https://clang.llvm.org/cxx_status.html>`_ page
2908 for detailed information on C++ feature support across Clang versions.
2910 Controlling implementation limits
2911 ---------------------------------
2913 .. option:: -fbracket-depth=N
2915 Sets the limit for nested parentheses, brackets, and braces to N. The
2918 .. option:: -fconstexpr-depth=N
2920 Sets the limit for recursive constexpr function invocations to N. The
2923 .. option:: -fconstexpr-steps=N
2925 Sets the limit for the number of full-expressions evaluated in a single
2926 constant expression evaluation. The default is 1048576.
2928 .. option:: -ftemplate-depth=N
2930 Sets the limit for recursively nested template instantiations to N. The
2933 .. option:: -foperator-arrow-depth=N
2935 Sets the limit for iterative calls to 'operator->' functions to N. The
2940 Objective-C Language Features
2941 =============================
2945 Objective-C++ Language Features
2946 ===============================
2953 Clang supports all OpenMP 4.5 directives and clauses. See :doc:`OpenMPSupport`
2954 for additional details.
2956 Use `-fopenmp` to enable OpenMP. Support for OpenMP can be disabled with
2959 Use `-fopenmp-simd` to enable OpenMP simd features only, without linking
2960 the runtime library; for combined constructs
2961 (e.g. ``#pragma omp parallel for simd``) the non-simd directives and clauses
2962 will be ignored. This can be disabled with `-fno-openmp-simd`.
2964 Controlling implementation limits
2965 ---------------------------------
2967 .. option:: -fopenmp-use-tls
2969 Controls code generation for OpenMP threadprivate variables. In presence of
2970 this option all threadprivate variables are generated the same way as thread
2971 local variables, using TLS support. If `-fno-openmp-use-tls`
2972 is provided or target does not support TLS, code generation for threadprivate
2973 variables relies on OpenMP runtime library.
2980 Clang can be used to compile OpenCL kernels for execution on a device
2981 (e.g. GPU). It is possible to compile the kernel into a binary (e.g. for AMDGPU)
2982 that can be uploaded to run directly on a device (e.g. using
2983 `clCreateProgramWithBinary
2984 <https://www.khronos.org/registry/OpenCL/specs/opencl-1.1.pdf#111>`_) or
2985 into generic bitcode files loadable into other toolchains.
2987 Compiling to a binary using the default target from the installation can be done
2990 .. code-block:: console
2992 $ echo "kernel void k(){}" > test.cl
2995 Compiling for a specific target can be done by specifying the triple corresponding
2996 to the target, for example:
2998 .. code-block:: console
3000 $ clang -target nvptx64-unknown-unknown test.cl
3001 $ clang -target amdgcn-amd-amdhsa -mcpu=gfx900 test.cl
3003 Compiling to bitcode can be done as follows:
3005 .. code-block:: console
3007 $ clang -c -emit-llvm test.cl
3009 This will produce a file `test.bc` that can be used in vendor toolchains
3010 to perform machine code generation.
3012 Note that if compiled to bitcode for generic targets such as SPIR,
3013 portable IR is produced that can be used with various vendor
3014 tools as well as open source tools such as `SPIRV-LLVM Translator
3015 <https://github.com/KhronosGroup/SPIRV-LLVM-Translator>`_
3016 to produce SPIR-V binary. More details are provided in `the offline
3017 compilation from OpenCL kernel sources into SPIR-V using open source
3019 <https://github.com/KhronosGroup/OpenCL-Guide/blob/main/chapters/os_tooling.md>`_.
3021 Clang currently supports OpenCL C language standards up to v2.0. Clang mainly
3022 supports full profile. There is only very limited support of the embedded
3024 Starting from clang 9 a C++ mode is available for OpenCL (see
3025 :ref:`C++ for OpenCL <cxx_for_opencl>`).
3027 There is ongoing support for OpenCL v3.0 that is documented along with other
3028 experimental functionality and features in development on :doc:`OpenCLSupport`
3031 OpenCL Specific Options
3032 -----------------------
3034 Most of the OpenCL build options from `the specification v2.0 section 5.8.4
3035 <https://www.khronos.org/registry/cl/specs/opencl-2.0.pdf#200>`_ are available.
3039 .. code-block:: console
3041 $ clang -cl-std=CL2.0 -cl-single-precision-constant test.cl
3044 Many flags used for the compilation for C sources can also be passed while
3045 compiling for OpenCL, examples: ``-c``, ``-O<1-4|s>``, ``-o``, ``-emit-llvm``, etc.
3047 Some extra options are available to support special OpenCL features.
3049 .. _opencl_cl_no_stdinc:
3051 .. option:: -cl-no-stdinc
3053 Allows to disable all extra types and functions that are not native to the compiler.
3054 This might reduce the compilation speed marginally but many declarations from the
3055 OpenCL standard will not be accessible. For example, the following will fail to
3058 .. code-block:: console
3060 $ echo "bool is_wg_uniform(int i){return get_enqueued_local_size(i)==get_local_size(i);}" > test.cl
3061 $ clang -cl-std=CL2.0 -cl-no-stdinc test.cl
3062 error: use of undeclared identifier 'get_enqueued_local_size'
3063 error: use of undeclared identifier 'get_local_size'
3065 More information about the standard types and functions is provided in :ref:`the
3066 section on the OpenCL Header <opencl_header>`.
3071 OpenCL targets are derived from the regular Clang target classes. The OpenCL
3072 specific parts of the target representation provide address space mapping as
3073 well as a set of supported extensions.
3078 There is a set of concrete HW architectures that OpenCL can be compiled for.
3082 .. code-block:: console
3084 $ clang -target amdgcn-amd-amdhsa -mcpu=gfx900 test.cl
3086 - For Nvidia architectures:
3088 .. code-block:: console
3090 $ clang -target nvptx64-unknown-unknown test.cl
3096 - SPIR is available as a generic target to allow portable bitcode to be produced
3097 that can be used across GPU toolchains. The implementation follows `the SPIR
3098 specification <https://www.khronos.org/spir>`_. There are two flavors
3099 available for 32 and 64 bits.
3101 .. code-block:: console
3103 $ clang -target spir test.cl -emit-llvm -c
3104 $ clang -target spir64 test.cl -emit-llvm -c
3106 All known OpenCL extensions are supported in the SPIR targets. Clang will
3107 generate SPIR v1.2 compatible IR for OpenCL versions up to 2.0 and SPIR v2.0
3108 for OpenCL v2.0 or C++ for OpenCL.
3110 - x86 is used by some implementations that are x86 compatible and currently
3111 remains for backwards compatibility (with older implementations prior to
3112 SPIR target support). For "non-SPMD" targets which cannot spawn multiple
3113 work-items on the fly using hardware, which covers practically all non-GPU
3114 devices such as CPUs and DSPs, additional processing is needed for the kernels
3115 to support multiple work-item execution. For this, a 3rd party toolchain,
3116 such as for example `POCL <http://portablecl.org/>`_, can be used.
3118 This target does not support multiple memory segments and, therefore, the fake
3119 address space map can be added using the :ref:`-ffake-address-space-map
3120 <opencl_fake_address_space_map>` flag.
3127 By default Clang will include standard headers and therefore most of OpenCL
3128 builtin functions and types are available during compilation. The
3129 default declarations of non-native compiler types and functions can be disabled
3130 by using flag :ref:`-cl-no-stdinc <opencl_cl_no_stdinc>`.
3132 The following example demonstrates that OpenCL kernel sources with various
3133 standard builtin functions can be compiled without the need for an explicit
3134 includes or compiler flags.
3136 .. code-block:: console
3138 $ echo "bool is_wg_uniform(int i){return get_enqueued_local_size(i)==get_local_size(i);}" > test.cl
3139 $ clang -cl-std=CL2.0 test.cl
3141 More information about the default headers is provided in :doc:`OpenCLSupport`.
3146 Most of the ``cl_khr_*`` extensions to OpenCL C from `the official OpenCL
3147 registry <https://www.khronos.org/registry/OpenCL/>`_ are available and
3148 configured per target depending on the support available in the specific
3151 It is possible to alter the default extensions setting per target using
3152 ``-cl-ext`` flag. (See :ref:`flags description <opencl_cl_ext>` for more details).
3154 Vendor extensions can be added flexibly by declaring the list of types and
3155 functions associated with each extensions enclosed within the following
3156 compiler pragma directives:
3160 #pragma OPENCL EXTENSION the_new_extension_name : begin
3161 // declare types and functions associated with the extension here
3162 #pragma OPENCL EXTENSION the_new_extension_name : end
3164 For example, parsing the following code adds ``my_t`` type and ``my_func``
3165 function to the custom ``my_ext`` extension.
3169 #pragma OPENCL EXTENSION my_ext : begin
3174 #pragma OPENCL EXTENSION my_ext : end
3176 There is no conflict resolution for identifier clashes among extensions.
3177 It is therefore recommended that the identifiers are prefixed with a
3178 double underscore to avoid clashing with user space identifiers. Vendor
3179 extension should use reserved identifier prefix e.g. amd, arm, intel.
3181 Clang also supports language extensions documented in `The OpenCL C Language
3182 Extensions Documentation
3183 <https://github.com/KhronosGroup/Khronosdotorg/blob/main/api/opencl/assets/OpenCL_LangExt.pdf>`_.
3185 OpenCL-Specific Attributes
3186 --------------------------
3188 OpenCL support in Clang contains a set of attribute taken directly from the
3189 specification as well as additional attributes.
3191 See also :doc:`AttributeReference`.
3196 Clang supports this attribute to comply to OpenCL v2.0 conformance, but it
3197 does not have any effect on the IR. For more details reffer to the specification
3199 <https://www.khronos.org/registry/cl/specs/opencl-2.0-openclc.pdf#49>`_
3205 The implementation of this feature mirrors the unroll hint for C.
3206 More details on the syntax can be found in the specification
3208 <https://www.khronos.org/registry/cl/specs/opencl-2.0-openclc.pdf#61>`_
3213 To make sure no invalid optimizations occur for single program multiple data
3214 (SPMD) / single instruction multiple thread (SIMT) Clang provides attributes that
3215 can be used for special functions that have cross work item semantics.
3216 An example is the subgroup operations such as `intel_sub_group_shuffle
3217 <https://www.khronos.org/registry/cl/extensions/intel/cl_intel_subgroups.txt>`_
3221 // Define custom my_sub_group_shuffle(data, c)
3222 // that makes use of intel_sub_group_shuffle
3224 if (r0) r1 = computeA();
3225 // Shuffle data from r1 into r3
3226 // of threads id r2.
3227 r3 = my_sub_group_shuffle(r1, r2);
3228 if (r0) r3 = computeB();
3230 with non-SPMD semantics this is optimized to the following equivalent code:
3236 // Incorrect functionality! The data in r1
3237 // have not been computed by all threads yet.
3238 r3 = my_sub_group_shuffle(r1, r2);
3241 r3 = my_sub_group_shuffle(r1, r2);
3245 Declaring the function ``my_sub_group_shuffle`` with the convergent attribute
3250 my_sub_group_shuffle() __attribute__((convergent));
3252 Using ``convergent`` guarantees correct execution by keeping CFG equivalence
3253 wrt operations marked as ``convergent``. CFG ``G´`` is equivalent to ``G`` wrt
3254 node ``Ni`` : ``iff ∀ Nj (i≠j)`` domination and post-domination relations with
3255 respect to ``Ni`` remain the same in both ``G`` and ``G´``.
3260 ``noduplicate`` is more restrictive with respect to optimizations than
3261 ``convergent`` because a convergent function only preserves CFG equivalence.
3262 This allows some optimizations to happen as long as the control flow remains
3267 for (int i=0; i<4; i++)
3268 my_sub_group_shuffle()
3274 my_sub_group_shuffle();
3275 my_sub_group_shuffle();
3276 my_sub_group_shuffle();
3277 my_sub_group_shuffle();
3279 while using ``noduplicate`` would disallow this. Also ``noduplicate`` doesn't
3280 have the same safe semantics of CFG as ``convergent`` and can cause changes in
3281 CFG that modify semantics of the original program.
3283 ``noduplicate`` is kept for backwards compatibility only and it considered to be
3284 deprecated for future uses.
3291 Starting from clang 9 kernel code can contain C++17 features: classes, templates,
3292 function overloading, type deduction, etc. Please note that this is not an
3293 implementation of `OpenCL C++
3294 <https://www.khronos.org/registry/OpenCL/specs/2.2/pdf/OpenCL_Cxx.pdf>`_ and
3295 there is no plan to support it in clang in any new releases in the near future.
3298 Clang currently supports C++ for OpenCL v1.0.
3299 For detailed information about this language refer to the C++ for OpenCL
3300 Programming Language Documentation available
3301 in `the latest build
3302 <https://www.khronos.org/opencl/assets/CXX_for_OpenCL.html>`_
3303 or in `the official release
3304 <https://github.com/KhronosGroup/OpenCL-Docs/releases/tag/cxxforopencl-v1.0-r2>`_.
3306 To enable the C++ for OpenCL mode, pass one of following command line options when
3307 compiling ``.cl`` file ``-cl-std=clc++``, ``-cl-std=CLC++``, ``-cl-std=clc++1.0``,
3308 ``-cl-std=CLC++1.0``, ``-std=clc++``, ``-std=CLC++``, ``-std=clc++1.0`` or
3313 template<class T> T add( T x, T y )
3318 __kernel void test( __global float* a, __global float* b)
3320 auto index = get_global_id(0);
3321 a[index] = add(b[index], b[index+1]);
3325 .. code-block:: console
3327 clang -cl-std=clc++ test.cl
3329 Alternatively, files with ``.clcpp`` extension are compiled with the C++ for OpenCL
3332 .. code-block:: console
3336 C++ for OpenCL kernel sources can also be compiled online in drivers supporting
3337 `cl_ext_cxx_for_opencl
3338 <https://www.khronos.org/registry/OpenCL/extensions/ext/cl_ext_cxx_for_opencl.html>`_
3341 Constructing and destroying global objects
3342 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3344 Global objects with non-trivial constructors require the constructors to be run
3345 before the first kernel using the global objects is executed. Similarly global
3346 objects with non-trivial destructors require destructor invocation just after
3347 the last kernel using the program objects is executed.
3348 In OpenCL versions earlier than v2.2 there is no support for invoking global
3349 constructors. However, an easy workaround is to manually enqueue the
3350 constructor initialization kernel that has the following name scheme
3351 ``_GLOBAL__sub_I_<compiled file name>``.
3352 This kernel is only present if there are global objects with non-trivial
3353 constructors present in the compiled binary. One way to check this is by
3354 passing ``CL_PROGRAM_KERNEL_NAMES`` to ``clGetProgramInfo`` (OpenCL v2.0
3355 s5.8.7) and then checking whether any kernel name matches the naming scheme of
3356 global constructor initialization kernel above.
3358 Note that if multiple files are compiled and linked into libraries, multiple
3359 kernels that initialize global objects for multiple modules would have to be
3362 Applications are currently required to run initialization of global objects
3363 manually before running any kernels in which the objects are used.
3365 .. code-block:: console
3367 clang -cl-std=clc++ test.cl
3369 If there are any global objects to be initialized, the final binary will
3370 contain the ``_GLOBAL__sub_I_test.cl`` kernel to be enqueued.
3372 Note that the manual workaround only applies to objects declared at the
3373 program scope. There is no manual workaround for the construction of static
3374 objects with non-trivial constructors inside functions.
3376 Global destructors can not be invoked manually in the OpenCL v2.0 drivers.
3377 However, all memory used for program scope objects should be released on
3378 ``clReleaseProgram``.
3382 Limited experimental support of C++ standard libraries for OpenCL is
3383 described in :doc:`OpenCLSupport` page.
3385 .. _target_features:
3387 Target-Specific Features and Limitations
3388 ========================================
3390 CPU Architectures Features and Limitations
3391 ------------------------------------------
3396 The support for X86 (both 32-bit and 64-bit) is considered stable on
3397 Darwin (macOS), Linux, FreeBSD, and Dragonfly BSD: it has been tested
3398 to correctly compile many large C, C++, Objective-C, and Objective-C++
3401 On ``x86_64-mingw32``, passing i128(by value) is incompatible with the
3402 Microsoft x64 calling convention. You might need to tweak
3403 ``WinX86_64ABIInfo::classify()`` in lib/CodeGen/TargetInfo.cpp.
3405 For the X86 target, clang supports the `-m16` command line
3406 argument which enables 16-bit code output. This is broadly similar to
3407 using ``asm(".code16gcc")`` with the GNU toolchain. The generated code
3408 and the ABI remains 32-bit but the assembler emits instructions
3409 appropriate for a CPU running in 16-bit mode, with address-size and
3410 operand-size prefixes to enable 32-bit addressing and operations.
3412 Several micro-architecture levels as specified by the x86-64 psABI are defined.
3413 They are cumulative in the sense that features from previous levels are
3414 implicitly included in later levels.
3416 - ``-march=x86-64``: CMOV, CMPXCHG8B, FPU, FXSR, MMX, FXSR, SCE, SSE, SSE2
3417 - ``-march=x86-64-v2``: (close to Nehalem) CMPXCHG16B, LAHF-SAHF, POPCNT, SSE3, SSE4.1, SSE4.2, SSSE3
3418 - ``-march=x86-64-v3``: (close to Haswell) AVX, AVX2, BMI1, BMI2, F16C, FMA, LZCNT, MOVBE, XSAVE
3419 - ``-march=x86-64-v4``: AVX512F, AVX512BW, AVX512CD, AVX512DQ, AVX512VL
3424 The support for ARM (specifically ARMv6 and ARMv7) is considered stable
3425 on Darwin (iOS): it has been tested to correctly compile many large C,
3426 C++, Objective-C, and Objective-C++ codebases. Clang only supports a
3427 limited number of ARM architectures. It does not yet fully support
3433 The support for PowerPC (especially PowerPC64) is considered stable
3434 on Linux and FreeBSD: it has been tested to correctly compile many
3435 large C and C++ codebases. PowerPC (32bit) is still missing certain
3436 features (e.g. PIC code on ELF platforms).
3441 clang currently contains some support for other architectures (e.g. Sparc);
3442 however, significant pieces of code generation are still missing, and they
3443 haven't undergone significant testing.
3445 clang contains limited support for the MSP430 embedded processor, but
3446 both the clang support and the LLVM backend support are highly
3449 Other platforms are completely unsupported at the moment. Adding the
3450 minimal support needed for parsing and semantic analysis on a new
3451 platform is quite easy; see ``lib/Basic/Targets.cpp`` in the clang source
3452 tree. This level of support is also sufficient for conversion to LLVM IR
3453 for simple programs. Proper support for conversion to LLVM IR requires
3454 adding code to ``lib/CodeGen/CGCall.cpp`` at the moment; this is likely to
3455 change soon, though. Generating assembly requires a suitable LLVM
3458 Operating System Features and Limitations
3459 -----------------------------------------
3464 Clang has experimental support for targeting "Cygming" (Cygwin / MinGW)
3467 See also :ref:`Microsoft Extensions <c_ms>`.
3472 Clang works on Cygwin-1.7.
3477 Clang works on some mingw32 distributions. Clang assumes directories as
3480 - ``C:/mingw/include``
3482 - ``C:/mingw/lib/gcc/mingw32/4.[3-5].0/include/c++``
3484 On MSYS, a few tests might fail.
3489 For 32-bit (i686-w64-mingw32), and 64-bit (x86\_64-w64-mingw32), Clang
3492 - ``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)``
3493 - ``some_directory/bin/gcc.exe``
3494 - ``some_directory/bin/clang.exe``
3495 - ``some_directory/bin/clang++.exe``
3496 - ``some_directory/bin/../include/c++/GCC_version``
3497 - ``some_directory/bin/../include/c++/GCC_version/x86_64-w64-mingw32``
3498 - ``some_directory/bin/../include/c++/GCC_version/i686-w64-mingw32``
3499 - ``some_directory/bin/../include/c++/GCC_version/backward``
3500 - ``some_directory/bin/../x86_64-w64-mingw32/include``
3501 - ``some_directory/bin/../i686-w64-mingw32/include``
3502 - ``some_directory/bin/../include``
3504 This directory layout is standard for any toolchain you will find on the
3505 official `MinGW-w64 website <http://mingw-w64.sourceforge.net>`_.
3507 Clang expects the GCC executable "gcc.exe" compiled for
3508 ``i686-w64-mingw32`` (or ``x86_64-w64-mingw32``) to be present on PATH.
3510 `Some tests might fail <https://bugs.llvm.org/show_bug.cgi?id=9072>`_ on
3511 ``x86_64-w64-mingw32``.
3518 clang-cl is an alternative command-line interface to Clang, designed for
3519 compatibility with the Visual C++ compiler, cl.exe.
3521 To enable clang-cl to find system headers, libraries, and the linker when run
3522 from the command-line, it should be executed inside a Visual Studio Native Tools
3523 Command Prompt or a regular Command Prompt where the environment has been set
3524 up using e.g. `vcvarsall.bat <https://msdn.microsoft.com/en-us/library/f2ccy3wt.aspx>`_.
3526 clang-cl can also be used from inside Visual Studio by selecting the LLVM
3527 Platform Toolset. The toolset is not part of the installer, but may be installed
3529 `Visual Studio Marketplace <https://marketplace.visualstudio.com/items?itemName=LLVMExtensions.llvm-toolchain>`_.
3530 To use the toolset, select a project in Solution Explorer, open its Property
3531 Page (Alt+F7), and in the "General" section of "Configuration Properties"
3532 change "Platform Toolset" to LLVM. Doing so enables an additional Property
3533 Page for selecting the clang-cl executable to use for builds.
3535 To use the toolset with MSBuild directly, invoke it with e.g.
3536 ``/p:PlatformToolset=LLVM``. This allows trying out the clang-cl toolchain
3537 without modifying your project files.
3539 It's also possible to point MSBuild at clang-cl without changing toolset by
3540 passing ``/p:CLToolPath=c:\llvm\bin /p:CLToolExe=clang-cl.exe``.
3542 When using CMake and the Visual Studio generators, the toolset can be set with the ``-T`` flag:
3546 cmake -G"Visual Studio 15 2017" -T LLVM ..
3548 When using CMake with the Ninja generator, set the ``CMAKE_C_COMPILER`` and
3549 ``CMAKE_CXX_COMPILER`` variables to clang-cl:
3553 cmake -GNinja -DCMAKE_C_COMPILER="c:/Program Files (x86)/LLVM/bin/clang-cl.exe"
3554 -DCMAKE_CXX_COMPILER="c:/Program Files (x86)/LLVM/bin/clang-cl.exe" ..
3557 Command-Line Options
3558 --------------------
3560 To be compatible with cl.exe, clang-cl supports most of the same command-line
3561 options. Those options can start with either ``/`` or ``-``. It also supports
3562 some of Clang's core options, such as the ``-W`` options.
3564 Options that are known to clang-cl, but not currently supported, are ignored
3565 with a warning. For example:
3569 clang-cl.exe: warning: argument unused during compilation: '/AI'
3571 To suppress warnings about unused arguments, use the ``-Qunused-arguments`` option.
3573 Options that are not known to clang-cl will be ignored by default. Use the
3574 ``-Werror=unknown-argument`` option in order to treat them as errors. If these
3575 options are spelled with a leading ``/``, they will be mistaken for a filename:
3579 clang-cl.exe: error: no such file or directory: '/foobar'
3581 Please `file a bug <https://bugs.llvm.org/enter_bug.cgi?product=clang&component=Driver>`_
3582 for any valid cl.exe flags that clang-cl does not understand.
3584 Execute ``clang-cl /?`` to see a list of supported options:
3588 CL.EXE COMPATIBILITY OPTIONS:
3589 /? Display available options
3590 /arch:<value> Set architecture for code generation
3591 /Brepro- Emit an object file which cannot be reproduced over time
3592 /Brepro Emit an object file which can be reproduced over time
3593 /clang:<arg> Pass <arg> to the clang driver
3594 /C Don't discard comments when preprocessing
3596 /d1PP Retain macro definitions in /E mode
3597 /d1reportAllClassLayout Dump record layout information
3598 /diagnostics:caret Enable caret and column diagnostics (on by default)
3599 /diagnostics:classic Disable column and caret diagnostics
3600 /diagnostics:column Disable caret diagnostics but keep column info
3601 /D <macro[=value]> Define macro
3602 /EH<value> Exception handling model
3603 /EP Disable linemarker output and preprocess to stdout
3604 /execution-charset:<value>
3605 Runtime encoding, supports only UTF-8
3606 /E Preprocess to stdout
3607 /FA Output assembly code file during compilation
3608 /Fa<file or directory> Output assembly code to this file during compilation (with /FA)
3609 /Fe<file or directory> Set output executable file or directory (ends in / or \)
3610 /FI <value> Include file before parsing
3611 /Fi<file> Set preprocess output file name (with /P)
3612 /Fo<file or directory> Set output object file, or directory (ends in / or \) (with /c)
3618 /Fp<filename> Set pch filename (with /Yc and /Yu)
3619 /GA Assume thread-local variables are defined in the executable
3620 /Gd Set __cdecl as a default calling convention
3621 /GF- Disable string pooling
3622 /GF Enable string pooling (default)
3623 /GR- Disable emission of RTTI data
3624 /Gregcall Set __regcall as a default calling convention
3625 /GR Enable emission of RTTI data
3626 /Gr Set __fastcall as a default calling convention
3627 /GS- Disable buffer security check
3628 /GS Enable buffer security check (default)
3629 /Gs Use stack probes (default)
3630 /Gs<value> Set stack probe size (default 4096)
3631 /guard:<value> Enable Control Flow Guard with /guard:cf,
3632 or only the table with /guard:cf,nochecks.
3633 Enable EH Continuation Guard with /guard:ehcont
3634 /Gv Set __vectorcall as a default calling convention
3635 /Gw- Don't put each data item in its own section
3636 /Gw Put each data item in its own section
3637 /GX- Disable exception handling
3638 /GX Enable exception handling
3639 /Gy- Don't put each function in its own section (default)
3640 /Gy Put each function in its own section
3641 /Gz Set __stdcall as a default calling convention
3642 /help Display available options
3643 /imsvc <dir> Add directory to system include search path, as if part of %INCLUDE%
3644 /I <dir> Add directory to include search path
3645 /J Make char type unsigned
3646 /LDd Create debug DLL
3648 /link <options> Forward options to the linker
3649 /MDd Use DLL debug run-time
3650 /MD Use DLL run-time
3651 /MTd Use static debug run-time
3652 /MT Use static run-time
3653 /O0 Disable optimization
3654 /O1 Optimize for size (same as /Og /Os /Oy /Ob2 /GF /Gy)
3655 /O2 Optimize for speed (same as /Og /Oi /Ot /Oy /Ob2 /GF /Gy)
3656 /Ob0 Disable function inlining
3657 /Ob1 Only inline functions which are (explicitly or implicitly) marked inline
3658 /Ob2 Inline functions as deemed beneficial by the compiler
3659 /Od Disable optimization
3661 /Oi- Disable use of builtin functions
3662 /Oi Enable use of builtin functions
3663 /Os Optimize for size
3664 /Ot Optimize for speed
3665 /Ox Deprecated (same as /Og /Oi /Ot /Oy /Ob2); use /O2 instead
3666 /Oy- Disable frame pointer omission (x86 only, default)
3667 /Oy Enable frame pointer omission (x86 only)
3668 /O<flags> Set multiple /O flags at once; e.g. '/O2y-' for '/O2 /Oy-'
3669 /o <file or directory> Set output file or directory (ends in / or \)
3670 /P Preprocess to file
3671 /Qvec- Disable the loop vectorization passes
3672 /Qvec Enable the loop vectorization passes
3673 /showFilenames- Don't print the name of each compiled file (default)
3674 /showFilenames Print the name of each compiled file
3675 /showIncludes Print info about included files to stderr
3676 /source-charset:<value> Source encoding, supports only UTF-8
3677 /std:<value> Language standard to compile for
3678 /TC Treat all source files as C
3679 /Tc <filename> Specify a C source file
3680 /TP Treat all source files as C++
3681 /Tp <filename> Specify a C++ source file
3682 /utf-8 Set source and runtime encoding to UTF-8 (default)
3683 /U <macro> Undefine macro
3684 /vd<value> Control vtordisp placement
3685 /vmb Use a best-case representation method for member pointers
3686 /vmg Use a most-general representation for member pointers
3687 /vmm Set the default most-general representation to multiple inheritance
3688 /vms Set the default most-general representation to single inheritance
3689 /vmv Set the default most-general representation to virtual inheritance
3690 /volatile:iso Volatile loads and stores have standard semantics
3691 /volatile:ms Volatile loads and stores have acquire and release semantics
3692 /W0 Disable all warnings
3696 /W4 Enable -Wall and -Wextra
3697 /Wall Enable -Weverything
3698 /WX- Do not treat warnings as errors
3699 /WX Treat warnings as errors
3700 /w Disable all warnings
3701 /X Don't add %INCLUDE% to the include search path
3702 /Y- Disable precompiled headers, overrides /Yc and /Yu
3703 /Yc<filename> Generate a pch file for all code up to and including <filename>
3704 /Yu<filename> Load a pch file and use it instead of all code up to and including <filename>
3705 /Z7 Enable CodeView debug information in object files
3706 /Zc:char8_t Enable C++2a char8_t type
3707 /Zc:char8_t- Disable C++2a char8_t type
3708 /Zc:dllexportInlines- Don't dllexport/dllimport inline member functions of dllexport/import classes
3709 /Zc:dllexportInlines dllexport/dllimport inline member functions of dllexport/import classes (default)
3710 /Zc:sizedDealloc- Disable C++14 sized global deallocation functions
3711 /Zc:sizedDealloc Enable C++14 sized global deallocation functions
3712 /Zc:strictStrings Treat string literals as const
3713 /Zc:threadSafeInit- Disable thread-safe initialization of static variables
3714 /Zc:threadSafeInit Enable thread-safe initialization of static variables
3715 /Zc:trigraphs- Disable trigraphs (default)
3716 /Zc:trigraphs Enable trigraphs
3717 /Zc:twoPhase- Disable two-phase name lookup in templates
3718 /Zc:twoPhase Enable two-phase name lookup in templates
3719 /Zd Emit debug line number tables only
3720 /Zi Alias for /Z7. Does not produce PDBs.
3721 /Zl Don't mention any default libraries in the object file
3722 /Zp Set the default maximum struct packing alignment to 1
3723 /Zp<value> Specify the default maximum struct packing alignment
3724 /Zs Syntax-check only
3727 -### Print (but do not run) the commands to run for this compilation
3728 --analyze Run the static analyzer
3729 -faddrsig Emit an address-significance table
3730 -fansi-escape-codes Use ANSI escape codes for diagnostics
3731 -fblocks Enable the 'blocks' language feature
3732 -fcf-protection=<value> Instrument control-flow architecture protection. Options: return, branch, full, none.
3733 -fcf-protection Enable cf-protection in 'full' mode
3734 -fcolor-diagnostics Use colors in diagnostics
3735 -fcomplete-member-pointers
3736 Require member pointer base types to be complete if they would be significant under the Microsoft ABI
3737 -fcoverage-mapping Generate coverage mapping to enable code coverage analysis
3738 -fcrash-diagnostics-dir=<dir>
3739 Put crash-report files in <dir>
3740 -fdebug-macro Emit macro debug information
3741 -fdelayed-template-parsing
3742 Parse templated function definitions at the end of the translation unit
3743 -fdiagnostics-absolute-paths
3744 Print absolute paths in diagnostics
3745 -fdiagnostics-parseable-fixits
3746 Print fix-its in machine parseable form
3747 -flto=<value> Set LTO mode to either 'full' or 'thin'
3748 -flto Enable LTO in 'full' mode
3749 -fmerge-all-constants Allow merging of constants
3750 -fms-compatibility-version=<value>
3751 Dot-separated value representing the Microsoft compiler version
3752 number to report in _MSC_VER (0 = don't define it (default))
3753 -fms-compatibility Enable full Microsoft Visual C++ compatibility
3754 -fms-extensions Accept some non-standard constructs supported by the Microsoft compiler
3755 -fmsc-version=<value> Microsoft compiler version number to report in _MSC_VER
3756 (0 = don't define it (default))
3757 -fno-addrsig Don't emit an address-significance table
3758 -fno-builtin-<value> Disable implicit builtin knowledge of a specific function
3759 -fno-builtin Disable implicit builtin knowledge of functions
3760 -fno-complete-member-pointers
3761 Do not require member pointer base types to be complete if they would be significant under the Microsoft ABI
3762 -fno-coverage-mapping Disable code coverage analysis
3763 -fno-crash-diagnostics Disable auto-generation of preprocessed source files and a script for reproduction during a clang crash
3764 -fno-debug-macro Do not emit macro debug information
3765 -fno-delayed-template-parsing
3766 Disable delayed template parsing
3767 -fno-sanitize-address-poison-custom-array-cookie
3768 Disable poisoning array cookies when using custom operator new[] in AddressSanitizer
3769 -fno-sanitize-address-use-after-scope
3770 Disable use-after-scope detection in AddressSanitizer
3771 -fno-sanitize-address-use-odr-indicator
3772 Disable ODR indicator globals
3773 -fno-sanitize-ignorelist Don't use ignorelist file for sanitizers
3774 -fno-sanitize-cfi-cross-dso
3775 Disable control flow integrity (CFI) checks for cross-DSO calls.
3776 -fno-sanitize-coverage=<value>
3777 Disable specified features of coverage instrumentation for Sanitizers
3778 -fno-sanitize-memory-track-origins
3779 Disable origins tracking in MemorySanitizer
3780 -fno-sanitize-memory-use-after-dtor
3781 Disable use-after-destroy detection in MemorySanitizer
3782 -fno-sanitize-recover=<value>
3783 Disable recovery for specified sanitizers
3784 -fno-sanitize-stats Disable sanitizer statistics gathering.
3785 -fno-sanitize-thread-atomics
3786 Disable atomic operations instrumentation in ThreadSanitizer
3787 -fno-sanitize-thread-func-entry-exit
3788 Disable function entry/exit instrumentation in ThreadSanitizer
3789 -fno-sanitize-thread-memory-access
3790 Disable memory access instrumentation in ThreadSanitizer
3791 -fno-sanitize-trap=<value>
3792 Disable trapping for specified sanitizers
3793 -fno-standalone-debug Limit debug information produced to reduce size of debug binary
3794 -fobjc-runtime=<value> Specify the target Objective-C runtime kind and version
3795 -fprofile-exclude-files=<value>
3796 Instrument only functions from files where names don't match all the regexes separated by a semi-colon
3797 -fprofile-filter-files=<value>
3798 Instrument only functions from files where names match any regex separated by a semi-colon
3799 -fprofile-instr-generate=<file>
3800 Generate instrumented code to collect execution counts into <file>
3801 (overridden by LLVM_PROFILE_FILE env var)
3802 -fprofile-instr-generate
3803 Generate instrumented code to collect execution counts into default.profraw file
3804 (overridden by '=' form of option or LLVM_PROFILE_FILE env var)
3805 -fprofile-instr-use=<value>
3806 Use instrumentation data for profile-guided optimization
3807 -fprofile-remapping-file=<file>
3808 Use the remappings described in <file> to match the profile data against names in the program
3809 -fprofile-list=<file>
3810 Filename defining the list of functions/files to instrument
3811 -fsanitize-address-field-padding=<value>
3812 Level of field padding for AddressSanitizer
3813 -fsanitize-address-globals-dead-stripping
3814 Enable linker dead stripping of globals in AddressSanitizer
3815 -fsanitize-address-poison-custom-array-cookie
3816 Enable poisoning array cookies when using custom operator new[] in AddressSanitizer
3817 -fsanitize-address-use-after-return=<mode>
3818 Select the mode of detecting stack use-after-return in AddressSanitizer: never | runtime (default) | always
3819 -fsanitize-address-use-after-scope
3820 Enable use-after-scope detection in AddressSanitizer
3821 -fsanitize-address-use-odr-indicator
3822 Enable ODR indicator globals to avoid false ODR violation reports in partially sanitized programs at the cost of an increase in binary size
3823 -fsanitize-ignorelist=<value>
3824 Path to ignorelist file for sanitizers
3825 -fsanitize-cfi-cross-dso
3826 Enable control flow integrity (CFI) checks for cross-DSO calls.
3827 -fsanitize-cfi-icall-generalize-pointers
3828 Generalize pointers in CFI indirect call type signature checks
3829 -fsanitize-coverage=<value>
3830 Specify the type of coverage instrumentation for Sanitizers
3831 -fsanitize-hwaddress-abi=<value>
3832 Select the HWAddressSanitizer ABI to target (interceptor or platform, default interceptor)
3833 -fsanitize-memory-track-origins=<value>
3834 Enable origins tracking in MemorySanitizer
3835 -fsanitize-memory-track-origins
3836 Enable origins tracking in MemorySanitizer
3837 -fsanitize-memory-use-after-dtor
3838 Enable use-after-destroy detection in MemorySanitizer
3839 -fsanitize-recover=<value>
3840 Enable recovery for specified sanitizers
3841 -fsanitize-stats Enable sanitizer statistics gathering.
3842 -fsanitize-thread-atomics
3843 Enable atomic operations instrumentation in ThreadSanitizer (default)
3844 -fsanitize-thread-func-entry-exit
3845 Enable function entry/exit instrumentation in ThreadSanitizer (default)
3846 -fsanitize-thread-memory-access
3847 Enable memory access instrumentation in ThreadSanitizer (default)
3848 -fsanitize-trap=<value> Enable trapping for specified sanitizers
3849 -fsanitize-undefined-strip-path-components=<number>
3850 Strip (or keep only, if negative) a given number of path components when emitting check metadata.
3851 -fsanitize=<check> Turn on runtime checks for various forms of undefined or suspicious
3852 behavior. See user manual for available checks
3853 -fsplit-lto-unit Enables splitting of the LTO unit.
3854 -fstandalone-debug Emit full debug info for all types used by the program
3855 -fwhole-program-vtables Enables whole-program vtable optimization. Requires -flto
3856 -gcodeview-ghash Emit type record hashes in a .debug$H section
3857 -gcodeview Generate CodeView debug information
3858 -gline-directives-only Emit debug line info directives only
3859 -gline-tables-only Emit debug line number tables only
3860 -miamcu Use Intel MCU ABI
3861 -mllvm <value> Additional arguments to forward to LLVM's option processing
3862 -nobuiltininc Disable builtin #include directories
3863 -Qunused-arguments Don't emit warning for unused driver arguments
3864 -R<remark> Enable the specified remark
3865 --target=<value> Generate code for the given target
3866 --version Print version information
3867 -v Show commands to run and use verbose output
3868 -W<warning> Enable the specified warning
3869 -Xclang <arg> Pass <arg> to the clang compiler
3874 When clang-cl is run with a set of ``/clang:<arg>`` options, it will gather all
3875 of the ``<arg>`` arguments and process them as if they were passed to the clang
3876 driver. This mechanism allows you to pass flags that are not exposed in the
3877 clang-cl options or flags that have a different meaning when passed to the clang
3878 driver. Regardless of where they appear in the command line, the ``/clang:``
3879 arguments are treated as if they were passed at the end of the clang-cl command
3882 The /Zc:dllexportInlines- Option
3883 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3885 This causes the class-level `dllexport` and `dllimport` attributes to not apply
3886 to inline member functions, as they otherwise would. For example, in the code
3887 below `S::foo()` would normally be defined and exported by the DLL, but when
3888 using the ``/Zc:dllexportInlines-`` flag it is not:
3892 struct __declspec(dllexport) S {
3896 This has the benefit that the compiler doesn't need to emit a definition of
3897 `S::foo()` in every translation unit where the declaration is included, as it
3898 would otherwise do to ensure there's a definition in the DLL even if it's not
3899 used there. If the declaration occurs in a header file that's widely used, this
3900 can save significant compilation time and output size. It also reduces the
3901 number of functions exported by the DLL similarly to what
3902 ``-fvisibility-inlines-hidden`` does for shared objects on ELF and Mach-O.
3903 Since the function declaration comes with an inline definition, users of the
3904 library can use that definition directly instead of importing it from the DLL.
3906 Note that the Microsoft Visual C++ compiler does not support this option, and
3907 if code in a DLL is compiled with ``/Zc:dllexportInlines-``, the code using the
3908 DLL must be compiled in the same way so that it doesn't attempt to dllimport
3909 the inline member functions. The reverse scenario should generally work though:
3910 a DLL compiled without this flag (such as a system library compiled with Visual
3911 C++) can be referenced from code compiled using the flag, meaning that the
3912 referencing code will use the inline definitions instead of importing them from
3915 Also note that like when using ``-fvisibility-inlines-hidden``, the address of
3916 `S::foo()` will be different inside and outside the DLL, breaking the C/C++
3917 standard requirement that functions have a unique address.
3919 The flag does not apply to explicit class template instantiation definitions or
3920 declarations, as those are typically used to explicitly provide a single
3921 definition in a DLL, (dllexported instantiation definition) or to signal that
3922 the definition is available elsewhere (dllimport instantiation declaration). It
3923 also doesn't apply to inline members with static local variables, to ensure
3924 that the same instance of the variable is used inside and outside the DLL.
3926 Using this flag can cause problems when inline functions that would otherwise
3927 be dllexported refer to internal symbols of a DLL. For example:
3933 struct __declspec(dllimport) S {
3934 void foo() { internal(); }
3937 Normally, references to `S::foo()` would use the definition in the DLL from
3938 which it was exported, and which presumably also has the definition of
3939 `internal()`. However, when using ``/Zc:dllexportInlines-``, the inline
3940 definition of `S::foo()` is used directly, resulting in a link error since
3941 `internal()` is not available. Even worse, if there is an inline definition of
3942 `internal()` containing a static local variable, we will now refer to a
3943 different instance of that variable than in the DLL:
3947 inline int internal() { static int x; return x++; }
3949 struct __declspec(dllimport) S {
3950 int foo() { return internal(); }
3953 This could lead to very subtle bugs. Using ``-fvisibility-inlines-hidden`` can
3954 lead to the same issue. To avoid it in this case, make `S::foo()` or
3955 `internal()` non-inline, or mark them `dllimport/dllexport` explicitly.
3957 Finding Clang runtime libraries
3958 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3960 clang-cl supports several features that require runtime library support:
3962 - Address Sanitizer (ASan): ``-fsanitize=address``
3963 - Undefined Behavior Sanitizer (UBSan): ``-fsanitize=undefined``
3964 - Code coverage: ``-fprofile-instr-generate -fcoverage-mapping``
3965 - Profile Guided Optimization (PGO): ``-fprofile-instr-generate``
3966 - Certain math operations (int128 division) require the builtins library
3968 In order to use these features, the user must link the right runtime libraries
3969 into their program. These libraries are distributed alongside Clang in the
3970 library resource directory. Clang searches for the resource directory by
3971 searching relative to the Clang executable. For example, if LLVM is installed
3972 in ``C:\Program Files\LLVM``, then the profile runtime library will be located
3974 ``C:\Program Files\LLVM\lib\clang\11.0.0\lib\windows\clang_rt.profile-x86_64.lib``.
3976 For UBSan, PGO, and coverage, Clang will emit object files that auto-link the
3977 appropriate runtime library, but the user generally needs to help the linker
3978 (whether it is ``lld-link.exe`` or MSVC ``link.exe``) find the library resource
3979 directory. Using the example installation above, this would mean passing
3980 ``/LIBPATH:C:\Program Files\LLVM\lib\clang\11.0.0\lib\windows`` to the linker.
3981 If the user links the program with the ``clang`` or ``clang-cl`` drivers, the
3982 driver will pass this flag for them.
3984 If the linker cannot find the appropriate library, it will emit an error like
3987 $ clang-cl -c -fsanitize=undefined t.cpp
3989 $ lld-link t.obj -dll
3990 lld-link: error: could not open 'clang_rt.ubsan_standalone-x86_64.lib': no such file or directory
3991 lld-link: error: could not open 'clang_rt.ubsan_standalone_cxx-x86_64.lib': no such file or directory
3993 $ link t.obj -dll -nologo
3994 LINK : fatal error LNK1104: cannot open file 'clang_rt.ubsan_standalone-x86_64.lib'
3996 To fix the error, add the appropriate ``/libpath:`` flag to the link line.
3998 For ASan, as of this writing, the user is also responsible for linking against
3999 the correct ASan libraries.
4001 If the user is using the dynamic CRT (``/MD``), then they should add
4002 ``clang_rt.asan_dynamic-x86_64.lib`` to the link line as a regular input. For
4003 other architectures, replace x86_64 with the appropriate name here and below.
4005 If the user is using the static CRT (``/MT``), then different runtimes are used
4006 to produce DLLs and EXEs. To link a DLL, pass
4007 ``clang_rt.asan_dll_thunk-x86_64.lib``. To link an EXE, pass
4008 ``-wholearchive:clang_rt.asan-x86_64.lib``.