xref: /netbsd-src/external/apache2/llvm/dist/clang/docs/UsersManual.rst (revision e038c9c4676b0f19b1b7dd08a940c6ed64a6d5ae)
1============================
2Clang Compiler User's Manual
3============================
4
5.. include:: <isonum.txt>
6
7.. contents::
8   :local:
9
10Introduction
11============
12
13The Clang Compiler is an open-source compiler for the C family of
14programming languages, aiming to be the best in class implementation of
15these languages. Clang builds on the LLVM optimizer and code generator,
16allowing it to provide high-quality optimization and code generation
17support for many targets. For more general information, please see the
18`Clang Web Site <https://clang.llvm.org>`_ or the `LLVM Web
19Site <https://llvm.org>`_.
20
21This document describes important notes about using Clang as a compiler
22for an end-user, documenting the supported features, command line
23options, etc. If you are interested in using Clang to build a tool that
24processes code, please see :doc:`InternalsManual`. If you are interested in the
25`Clang Static Analyzer <https://clang-analyzer.llvm.org>`_, please see its web
26page.
27
28Clang is one component in a complete toolchain for C family languages.
29A separate document describes the other pieces necessary to
30:doc:`assemble a complete toolchain <Toolchain>`.
31
32Clang is designed to support the C family of programming languages,
33which 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
35language-specific information, please see the corresponding language
36specific section:
37
38-  :ref:`C Language <c>`: K&R C, ANSI C89, ISO C90, ISO C94 (C89+AMD1), ISO
39   C99 (+TC1, TC2, TC3).
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,
45   plus C++ for OpenCL.
46
47In addition to these base languages and their dialects, Clang supports a
48broad variety of language extensions, which are documented in the
49corresponding language section. These extensions are provided to be
50compatible with the GCC, Microsoft, and other popular compilers as well
51as to improve functionality through Clang-specific features. The Clang
52driver and language features are intentionally designed to be as
53compatible with the GNU GCC compiler as reasonably possible, easing
54migration from GCC to Clang. In most cases, code "just works".
55Clang also provides an alternative driver, :ref:`clang-cl`, that is designed
56to be compatible with the Visual C++ compiler, cl.exe.
57
58In addition to language specific features, Clang has a variety of
59features that depend on what CPU architecture or operating system is
60being compiled for. Please see the :ref:`Target-Specific Features and
61Limitations <target_features>` section for more details.
62
63The rest of the introduction introduces some basic :ref:`compiler
64terminology <terminology>` that is used throughout this manual and
65contains a basic :ref:`introduction to using Clang <basicusage>` as a
66command line compiler.
67
68.. _terminology:
69
70Terminology
71-----------
72
73Front end, parser, backend, preprocessor, undefined behavior,
74diagnostic, optimizer
75
76.. _basicusage:
77
78Basic Usage
79-----------
80
81Intro to how to use a C compiler for newbies.
82
83compile + link compile then link debug info enabling optimizations
84picking a language to use, defaults to C17 by default. Autosenses based
85on extension. using a makefile
86
87Command Line Options
88====================
89
90This section is generally an index into other sections. It does not go
91into depth on the ones that are covered by other sections. However, the
92first part introduces the language selection and other high level
93options like :option:`-c`, :option:`-g`, etc.
94
95Options to Control Error and Warning Messages
96---------------------------------------------
97
98.. option:: -Werror
99
100  Turn warnings into errors.
101
102.. This is in plain monospaced font because it generates the same label as
103.. -Werror, and Sphinx complains.
104
105``-Werror=foo``
106
107  Turn warning "foo" into an error.
108
109.. option:: -Wno-error=foo
110
111  Turn warning "foo" into a warning even if :option:`-Werror` is specified.
112
113.. option:: -Wfoo
114
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.
118
119.. option:: -Wno-foo
120
121  Disable warning "foo".
122
123.. option:: -w
124
125  Disable all diagnostics.
126
127.. option:: -Weverything
128
129  :ref:`Enable all diagnostics. <diagnostics_enable_everything>`
130
131.. option:: -pedantic
132
133  Warn on language extensions.
134
135.. option:: -pedantic-errors
136
137  Error on language extensions.
138
139.. option:: -Wsystem-headers
140
141  Enable warnings from system headers.
142
143.. option:: -ferror-limit=123
144
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`.
147
148.. option:: -ftemplate-backtrace-limit=123
149
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`.
153
154.. _cl_diag_formatting:
155
156Formatting of Diagnostics
157^^^^^^^^^^^^^^^^^^^^^^^^^
158
159Clang aims to produce beautiful diagnostics by default, particularly for
160new users that first come to Clang. However, different people have
161different preferences, and sometimes Clang is driven not by a human,
162but by a program that wants consistent and easily parsable output. For
163these cases, Clang provides a wide range of options to control the exact
164output format of the diagnostics that it generates.
165
166.. _opt_fshow-column:
167
168**-f[no-]show-column**
169   Print column number in diagnostic.
170
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:
174
175   ::
176
177         test.c:28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens]
178         #endif bad
179                ^
180                //
181
182   When this is disabled, Clang will print "test.c:28: warning..." with
183   no column number.
184
185   The printed column numbers count bytes from the beginning of the
186   line; take care if your source contains multibyte characters.
187
188.. _opt_fshow-source-location:
189
190**-f[no-]show-source-location**
191   Print source file/line/column information in diagnostic.
192
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:
196
197   ::
198
199         test.c:28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens]
200         #endif bad
201                ^
202                //
203
204   When this is disabled, Clang will not print the "test.c:28:8: "
205   part.
206
207.. _opt_fcaret-diagnostics:
208
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
214   something like:
215
216   ::
217
218         test.c:28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens]
219         #endif bad
220                ^
221                //
222
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.
226
227   When this option is enabled, Clang will use colors to highlight
228   specific parts of the diagnostic, e.g.,
229
230   .. nasty hack to not lose our dignity
231
232   .. raw:: html
233
234       <pre>
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>
236         #endif bad
237                <span style="color:green">^</span>
238                <span style="color:green">//</span>
239       </pre>
240
241   When this is disabled, Clang will just print:
242
243   ::
244
245         test.c:2:8: warning: extra tokens at end of #endif directive [-Wextra-tokens]
246         #endif bad
247                ^
248                //
249
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
253   defaults to off.
254
255.. option:: -fdiagnostics-format=clang/msvc/vi
256
257   Changes diagnostic output format to better match IDEs and command line tools.
258
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:
262
263   **clang** (default)
264       ::
265
266           t.c:3:11: warning: conversion specifies type 'char *' but the argument has type 'int'
267
268   **msvc**
269       ::
270
271           t.c(3,11) : warning: conversion specifies type 'char *' but the argument has type 'int'
272
273   **vi**
274       ::
275
276           t.c +3:11: warning: conversion specifies type 'char *' but the argument has type 'int'
277
278.. _opt_fdiagnostics-show-option:
279
280**-f[no-]diagnostics-show-option**
281   Enable ``[-Woption]`` information in diagnostic line.
282
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
286   this output:
287
288   ::
289
290         test.c:28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens]
291         #endif bad
292                ^
293                //
294
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>`.
300
301.. _opt_fdiagnostics-show-category:
302
303.. option:: -fdiagnostics-show-category=none/id/name
304
305   Enable printing category information in diagnostic line.
306
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).
312
313   For example, a format string warning will produce these three
314   renditions based on the setting of this option:
315
316   ::
317
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]
321
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.
325
326.. _opt_fsave-optimization-record:
327
328.. option:: -f[no-]save-optimization-record[=<format>]
329
330   Enable optimization remarks during compilation and write them to a separate
331   file.
332
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.
336
337   By default, the serialization format is YAML.
338
339   The supported serialization formats are:
340
341   -  .. _opt_fsave_optimization_record_yaml:
342
343      ``-fsave-optimization-record=yaml``: A structured YAML format.
344
345   -  .. _opt_fsave_optimization_record_bitstream:
346
347      ``-fsave-optimization-record=bitstream``: A binary format based on LLVM
348      Bitstream.
349
350   The output file is controlled by :ref:`-foptimization-record-file <opt_foptimization-record-file>`.
351
352   In the absence of an explicit output file, the file is chosen using the
353   following scheme:
354
355   ``<base>.opt.<format>``
356
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`.
359   For example:
360
361   * ``clang -fsave-optimization-record -c in.c -o out.o`` will generate
362     ``out.opt.yaml``
363
364   * ``clang -fsave-optimization-record -c in.c `` will generate
365     ``in.opt.yaml``
366
367   When targeting (Thin)LTO, the base is derived from the output filename, and
368   the extension is not dropped.
369
370   When targeting ThinLTO, the following scheme is used:
371
372   ``<base>.opt.<format>.thin.<num>.<format>``
373
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.
379
380   For example:
381
382   ``clang -fsave-optimization-record=bitstream in.c -o out`` will generate
383
384   * ``/var/folders/43/9y164hh52tv_2nrdxrj31nyw0000gn/T/a-9be59b.o``
385
386   * ``/var/folders/43/9y164hh52tv_2nrdxrj31nyw0000gn/T/a-9be59b.opt.bitstream``
387
388   * ``out``
389
390   * ``out.dSYM/Contents/Resources/Remarks/out``
391
392   Darwin-only: compiling for multiple architectures will use the following
393   scheme:
394
395   ``<base>-<arch>.opt.<format>``
396
397   Note that this is incompatible with passing the
398   :ref:`-foptimization-record-file <opt_foptimization-record-file>` option.
399
400.. _opt_foptimization-record-file:
401
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>`.
405
406    On Darwin platforms, this is incompatible with passing multiple
407    ``-arch <arch>`` options.
408
409.. _opt_foptimization-record-passes:
410
411**-foptimization-record-passes**
412   Only include passes which match a specified regular expression.
413
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.
417
418   If this option is not used, all the passes are included in the optimization
419   record.
420
421.. _opt_fdiagnostics-show-hotness:
422
423**-f[no-]diagnostics-show-hotness**
424   Enable profile hotness information in diagnostic line.
425
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.
432
433   For example, in this output, the block containing the callsite of `foo` was
434   executed 3000 times according to the profile data:
435
436   ::
437
438         s.c:7:10: remark: foo inlined into bar (hotness: 3000) [-Rpass-analysis=inline]
439           sum += foo(x, x - 2);
440                  ^
441
442   This option is implied when
443   :ref:`-fsave-optimization-record <opt_fsave-optimization-record>` is used.
444   Otherwise, it defaults to off.
445
446.. _opt_fdiagnostics-hotness-threshold:
447
448**-fdiagnostics-hotness-threshold**
449   Prevent optimization remarks from being output if they do not have at least
450   this hotness value.
451
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>`).
458
459.. _opt_fdiagnostics-fixit-info:
460
461**-f[no-]diagnostics-fixit-info**
462   Enable "FixIt" information in the diagnostics output.
463
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:
467
468   ::
469
470         test.c:28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens]
471         #endif bad
472                ^
473                //
474
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.
479
480.. _opt_fdiagnostics-print-source-range-info:
481
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:
488
489   ::
490
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;
493              ~~~~~~ ^ ~~~~~~~
494
495   The {}'s are generated by -fdiagnostics-print-source-range-info.
496
497   The printed column numbers count bytes from the beginning of the
498   line; take care if your source contains multibyte characters.
499
500.. option:: -fdiagnostics-parseable-fixits
501
502   Print Fix-Its in a machine parseable form.
503
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:
507
508   ::
509
510        fix-it:"t.cpp":{7:25-7:29}:"Gamma"
511
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").
520
521   The printed column numbers count bytes from the beginning of the
522   line; take care if your source contains multibyte characters.
523
524.. option:: -fno-elide-type
525
526   Turns off elision in template type printing.
527
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.
533
534   Default:
535
536   ::
537
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;
539
540   -fno-elide-type:
541
542   ::
543
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;
545
546.. option:: -fdiagnostics-show-template-tree
547
548   Template type diffing prints a text tree.
549
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
553   -fno-elide-type.
554
555   Default:
556
557   ::
558
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;
560
561   With :option:`-fdiagnostics-show-template-tree`:
562
563   ::
564
565       t.cc:4:5: note: candidate function not viable: no known conversion for 1st argument;
566         vector<
567           map<
568             [...],
569             map<
570               [float != double],
571               [...]>>>
572
573.. _cl_diag_warning_groups:
574
575Individual Warning Groups
576^^^^^^^^^^^^^^^^^^^^^^^^^
577
578TODO: Generate this from tblgen. Define one anchor per warning group.
579
580.. _opt_wextra-tokens:
581
582.. option:: -Wextra-tokens
583
584   Warn about excess tokens at the end of a preprocessor directive.
585
586   This option, which defaults to on, enables warnings about extra
587   tokens at the end of preprocessor directives. For example:
588
589   ::
590
591         test.c:28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens]
592         #endif bad
593                ^
594
595   These extra tokens are not strictly conforming, and are usually best
596   handled by commenting them out.
597
598.. option:: -Wambiguous-member-template
599
600   Warn about unqualified uses of a member template whose name resolves to
601   another template at the location of the use.
602
603   This option, which defaults to on, enables a warning in the
604   following code:
605
606   ::
607
608       template<typename T> struct set{};
609       template<typename T> struct trait { typedef const T& type; };
610       struct Value {
611         template<typename T> void set(typename trait<T>::type value) {}
612       };
613       void foo() {
614         Value v;
615         v.set<double>(3.2);
616       }
617
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
620   as an extension.
621
622.. option:: -Wbind-to-temporary-copy
623
624   Warn about an unusable copy constructor when binding a reference to a
625   temporary.
626
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:
630
631   ::
632
633         struct NonCopyable {
634           NonCopyable();
635         private:
636           NonCopyable(const NonCopyable&);
637         };
638         void foo(const NonCopyable&);
639         void bar() {
640           foo(NonCopyable());  // Disallowed in C++98; allowed in C++11.
641         }
642
643   ::
644
645         struct NonCopyable2 {
646           NonCopyable2();
647           NonCopyable2(NonCopyable2&);
648         };
649         void foo(const NonCopyable2&);
650         void bar() {
651           foo(NonCopyable2());  // Disallowed in C++98; allowed in C++11.
652         }
653
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.
657
658Options to Control Clang Crash Diagnostics
659------------------------------------------
660
661As unbelievable as it may sound, Clang does crash from time to time.
662Generally, this only occurs to those living on the `bleeding
663edge <https://llvm.org/releases/download.html#svn>`_. Clang goes to great
664lengths to assist you in filing a bug report. Specifically, Clang
665generates preprocessed source file(s) and associated run script(s) upon
666a crash. These files should be attached to a bug report to ease
667reproducibility of the failure. Below are the command line options to
668control the crash diagnostics.
669
670.. option:: -fno-crash-diagnostics
671
672  Disable auto-generation of preprocessed source files during a clang crash.
673
674The -fno-crash-diagnostics flag can be helpful for speeding the process
675of generating a delta reduced test case.
676
677.. option:: -fcrash-diagnostics-dir=<dir>
678
679  Specify where to write the crash diagnostics files; defaults to the
680  usual location for temporary files.
681
682Clang is also capable of generating preprocessed source file(s) and associated
683run script(s) even without a crash. This is specially useful when trying to
684generate a reproducer for warnings or errors while using modules.
685
686.. option:: -gen-reproducer
687
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
690  same modules.
691
692.. _rpass:
693
694Options to Emit Optimization Reports
695------------------------------------
696
697Optimization reports trace, at a high-level, all the major decisions
698done by compiler transformations. For instance, when the inliner
699decides to inline function ``foo()`` into ``bar()``, or the loop unroller
700decides to unroll a loop N times, or the vectorizer decides to
701vectorize a loop body.
702
703Clang offers a family of flags which the optimizers can use to emit
704a diagnostic in three cases:
705
7061. When the pass makes a transformation (`-Rpass`).
707
7082. When the pass fails to make a transformation (`-Rpass-missed`).
709
7103. When the pass determines whether or not to make a transformation
711   (`-Rpass-analysis`).
712
713NOTE: Although the discussion below focuses on `-Rpass`, the exact
714same options apply to `-Rpass-missed` and `-Rpass-analysis`.
715
716Since there are dozens of passes inside the compiler, each of these flags
717take a regular expression that identifies the name of the pass which should
718emit the associated diagnostic. For example, to get a report from the inliner,
719compile the code with:
720
721.. code-block:: console
722
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); }
726                           ^
727
728Note that remarks from the inliner are identified with `[-Rpass=inline]`.
729To request a report from every optimization pass, you should use
730`-Rpass=.*` (in fact, you can use any valid POSIX regular
731expression). However, do not expect a report from every transformation
732made by the compiler. Optimization remarks do not really make sense
733outside of the major transformations (e.g., inlining, vectorization,
734loop optimizations) and not every optimization pass supports this
735feature.
736
737Note that when using profile-guided optimization information, profile hotness
738information can be included in the remarks (see
739:ref:`-fdiagnostics-show-hotness <opt_fdiagnostics-show-hotness>`).
740
741Current limitations
742^^^^^^^^^^^^^^^^^^^
743
7441. 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.
748
7492. 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.
755
756Options to Emit Resource Consumption Reports
757--------------------------------------------
758
759These are options that report execution time and consumed memory of different
760compilations steps.
761
762.. option:: -fproc-stat-report=
763
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
770  CSV format:
771
772  .. code-block:: console
773
774    $ clang -fproc-stat-report=abc foo.c
775    $ cat abc
776    clang-11,"/tmp/foo-123456.o",92000,84000,87536
777    ld,"a.out",900,8000,53568
778
779  The data on each row represent:
780
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.
786
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:
789
790  .. code-block:: console
791
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
795
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
799  statistics.
800
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.
808
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``.
813
814  .. code-block:: console
815
816    $ export CC_PRINT_PROC_STAT=1
817    $ export CC_PRINT_PROC_STAT_FILE=~/project-build-proc-stat.csv
818    $ make
819
820Other Options
821-------------
822Clang options that don't fit neatly into other categories.
823
824.. option:: -fgnuc-version=
825
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.
830
831.. option:: -MV
832
833  When emitting a dependency file, use formatting conventions appropriate
834  for NMake or Jom. Ignored unless another option causes Clang to emit a
835  dependency file.
836
837When Clang emits a dependency file (e.g., you supplied the -M option)
838most filenames can be written to the file without any special formatting.
839Different Make tools will treat different sets of characters as "special"
840and use different conventions for telling the Make tool that the character
841is actually part of the filename. Normally Clang uses backslash to "escape"
842a special character, which is the convention used by GNU Make. The -MV
843option tells Clang to put double-quotes around the entire filename, which
844is the convention used by NMake and Jom.
845
846Configuration files
847-------------------
848
849Configuration files group command-line options and allow all of them to be
850specified just by referencing the configuration file. They may be used, for
851example, to collect options required to tune compilation for particular
852target, such as -L, -I, -l, --sysroot, codegen options, etc.
853
854The command line option `--config` can be used to specify configuration
855file in a Clang invocation. For example:
856
857::
858
859    clang --config /home/user/cfgs/testing.txt
860    clang --config debug.cfg
861
862If the provided argument contains a directory separator, it is considered as
863a file path, and options are read from that file. Otherwise the argument is
864treated as a file name and is searched for sequentially in the directories:
865
866    - user directory,
867    - system directory,
868    - the directory where Clang executable resides.
869
870Both user and system directories for configuration files are specified during
871clang build using CMake parameters, CLANG_CONFIG_FILE_USER_DIR and
872CLANG_CONFIG_FILE_SYSTEM_DIR respectively. The first file found is used. It is
873an error if the required file cannot be found.
874
875Another way to specify a configuration file is to encode it in executable name.
876For example, if the Clang executable is named `armv7l-clang` (it may be a
877symbolic link to `clang`), then Clang will search for file `armv7l.cfg` in the
878directory where Clang resides.
879
880If a driver mode is specified in invocation, Clang tries to find a file specific
881for 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,
883looks for `x86_64.cfg`.
884
885If 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
887architecture name, Clang tries to load the configuration file for the effective
888architecture. For example, invocation:
889
890::
891
892    x86_64-clang -m32 abc.c
893
894causes Clang search for a file `i368.cfg` first, and if no such file is found,
895Clang looks for the file `x86_64.cfg`.
896
897The configuration file consists of command-line options specified on one or
898more lines. Lines composed of whitespace characters only are ignored as well as
899lines in which the first non-blank character is `#`. Long options may be split
900between several lines by a trailing backslash. Here is example of a
901configuration file:
902
903::
904
905    # Several options on line
906    -c --target=x86_64-unknown-linux-gnu
907
908    # Long option split between lines
909    -I/usr/lib/gcc/x86_64-linux-gnu/5.4.0/../../../../\
910    include/c++/5.4.0
911
912    # other config files may be included
913    @linux.options
914
915Files included by `@file` directives in configuration files are resolved
916relative 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`.
919
920Language and Target-Independent Features
921========================================
922
923Controlling Errors and Warnings
924-------------------------------
925
926Clang provides a number of ways to control which code constructs cause
927it to emit errors and warning messages, and how they are displayed to
928the console.
929
930Controlling How Clang Displays Diagnostics
931^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
932
933When Clang emits a diagnostic, it includes rich information in the
934output, and gives you fine-grain control over which information is
935printed. Clang has the ability to print this information, and these are
936the options that control it:
937
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
942   fatal error.
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
949   that support it)
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
958   default)
959   [:ref:`-fdiagnostics-print-source-range-info <opt_fdiagnostics-print-source-range-info>`].
960
961For more information please see :ref:`Formatting of
962Diagnostics <cl_diag_formatting>`.
963
964Diagnostic Mappings
965^^^^^^^^^^^^^^^^^^^
966
967All diagnostics are mapped into one of these 6 classes:
968
969-  Ignored
970-  Note
971-  Remark
972-  Warning
973-  Error
974-  Fatal
975
976.. _diagnostics_categories:
977
978Diagnostic Categories
979^^^^^^^^^^^^^^^^^^^^^
980
981Though not shown by default, diagnostics may each be associated with a
982high-level category. This category is intended to make it possible to
983triage builds that produce a large number of errors or warnings in a
984grouped way.
985
986Categories are not shown by default, but they can be turned on with the
987:ref:`-fdiagnostics-show-category <opt_fdiagnostics-show-category>` option.
988When set to "``name``", the category is printed textually in the
989diagnostic output. When it is set to "``id``", a category number is
990printed. The mapping of category names to category id's can be obtained
991by running '``clang   --print-diagnostic-categories``'.
992
993Controlling Diagnostics via Command Line Flags
994^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
995
996TODO: -W flags, -pedantic, etc
997
998.. _pragma_gcc_diagnostic:
999
1000Controlling Diagnostics via Pragmas
1001^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1002
1003Clang can also control what diagnostics are enabled through the use of
1004pragmas in the source code. This is useful for turning off specific
1005warnings in a section of source code. Clang supports GCC's pragma for
1006compatibility with existing source code, as well as several extensions.
1007
1008The pragma may control any warning that can be used from the command
1009line. Warnings may be set to ignored, warning, error, or fatal. The
1010following example code will tell Clang or GCC to ignore the -Wall
1011warnings:
1012
1013.. code-block:: c
1014
1015  #pragma GCC diagnostic ignored "-Wall"
1016
1017In addition to all of the functionality provided by GCC's pragma, Clang
1018also allows you to push and pop the current warning state. This is
1019particularly useful when writing a header file that will be compiled by
1020other people, because you don't know what warning flags they build with.
1021
1022In the below example :option:`-Wextra-tokens` is ignored for only a single line
1023of code, after which the diagnostics return to whatever state had previously
1024existed.
1025
1026.. code-block:: c
1027
1028  #if foo
1029  #endif foo // warning: extra tokens at end of #endif directive
1030
1031  #pragma clang diagnostic push
1032  #pragma clang diagnostic ignored "-Wextra-tokens"
1033
1034  #if foo
1035  #endif foo // no warning
1036
1037  #pragma clang diagnostic pop
1038
1039The push and pop pragmas will save and restore the full diagnostic state
1040of the compiler, regardless of how it was set. That means that it is
1041possible to use push and pop around GCC compatible diagnostics and Clang
1042will push and pop them appropriately, while GCC will ignore the pushes
1043and pops as unknown pragmas. It should be noted that while Clang
1044supports the GCC pragma, Clang and GCC do not support the exact same set
1045of warnings, so even when using GCC compatible #pragmas there is no
1046guarantee that they will have identical behaviour on both compilers.
1047
1048In addition to controlling warnings and errors generated by the compiler, it is
1049possible to generate custom warning and error messages through the following
1050pragmas:
1051
1052.. code-block:: c
1053
1054  // The following will produce warning messages
1055  #pragma message "some diagnostic message"
1056  #pragma GCC warning "TODO: replace deprecated feature"
1057
1058  // The following will produce an error message
1059  #pragma GCC error "Not supported"
1060
1061These pragmas operate similarly to the ``#warning`` and ``#error`` preprocessor
1062directives, except that they may also be embedded into preprocessor macros via
1063the C99 ``_Pragma`` operator, for example:
1064
1065.. code-block:: c
1066
1067  #define STR(X) #X
1068  #define DEFER(M,...) M(__VA_ARGS__)
1069  #define CUSTOM_ERROR(X) _Pragma(STR(GCC error(X " at line " DEFER(STR,__LINE__))))
1070
1071  CUSTOM_ERROR("Feature not available");
1072
1073Controlling Diagnostics in System Headers
1074^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1075
1076Warnings are suppressed when they occur in system headers. By default,
1077an included file is treated as a system header if it is found in an
1078include path specified by ``-isystem``, but this can be overridden in
1079several ways.
1080
1081The ``system_header`` pragma can be used to mark the current file as
1082being a system header. No warnings will be produced from the location of
1083the pragma onwards within the same file.
1084
1085.. code-block:: c
1086
1087  #if foo
1088  #endif foo // warning: extra tokens at end of #endif directive
1089
1090  #pragma clang system_header
1091
1092  #if foo
1093  #endif foo // no warning
1094
1095The `--system-header-prefix=` and `--no-system-header-prefix=`
1096command-line arguments can be used to override whether subsets of an include
1097path are treated as system headers. When the name in a ``#include`` directive
1098is found within a header search path and starts with a system prefix, the
1099header is treated as a system header. The last prefix on the
1100command-line which matches the specified header name takes precedence.
1101For instance:
1102
1103.. code-block:: console
1104
1105  $ clang -Ifoo -isystem bar --system-header-prefix=x/ \
1106      --no-system-header-prefix=x/y/
1107
1108Here, ``#include "x/a.h"`` is treated as including a system header, even
1109if the header is found in ``foo``, and ``#include "x/y/b.h"`` is treated
1110as not including a system header, even if the header is found in
1111``bar``.
1112
1113A ``#include`` directive which finds a file relative to the current
1114directory is treated as including a system header if the including file
1115is treated as a system header.
1116
1117.. _diagnostics_enable_everything:
1118
1119Enabling All Diagnostics
1120^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1121
1122In addition to the traditional ``-W`` flags, one can enable **all** diagnostics
1123by passing :option:`-Weverything`. This works as expected with
1124:option:`-Werror`, and also includes the warnings from :option:`-pedantic`. Some
1125diagnostics contradict each other, therefore, users of :option:`-Weverything`
1126often disable many diagnostics such as `-Wno-c++98-compat` and `-Wno-c++-compat`
1127because they contradict recent C++ standards.
1128
1129Since :option:`-Weverything` enables every diagnostic, we generally don't
1130recommend using it. `-Wall` `-Wextra` are a better choice for most projects.
1131Using :option:`-Weverything` means that updating your compiler is more difficult
1132because you're exposed to experimental diagnostics which might be of lower
1133quality than the default ones. If you do use :option:`-Weverything` then we
1134advise that you address all new compiler diagnostics as they get added to Clang,
1135either by fixing everything they find or explicitly disabling that diagnostic
1136with its corresponding `Wno-` option.
1137
1138Note that when combined with :option:`-w` (which disables all warnings),
1139disabling all warnings wins.
1140
1141Controlling Static Analyzer Diagnostics
1142^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1143
1144While not strictly part of the compiler, the diagnostics from Clang's
1145`static analyzer <https://clang-analyzer.llvm.org>`_ can also be
1146influenced by the user via changes to the source code. See the available
1147`annotations <https://clang-analyzer.llvm.org/annotations.html>`_ and the
1148analyzer's `FAQ
1149page <https://clang-analyzer.llvm.org/faq.html#exclude_code>`_ for more
1150information.
1151
1152.. _usersmanual-precompiled-headers:
1153
1154Precompiled Headers
1155-------------------
1156
1157`Precompiled headers <https://en.wikipedia.org/wiki/Precompiled_header>`_
1158are a general approach employed by many compilers to reduce compilation
1159time. The underlying motivation of the approach is that it is common for
1160the same (and often large) header files to be included by multiple
1161source files. Consequently, compile times can often be greatly improved
1162by caching some of the (redundant) work done by a compiler to process
1163headers. Precompiled header files, which represent one of many ways to
1164implement this optimization, are literally files that represent an
1165on-disk cache that contains the vital information necessary to reduce
1166some of the work needed to process a corresponding header file. While
1167details of precompiled headers vary between compilers, precompiled
1168headers have been shown to be highly effective at speeding up program
1169compilation on systems with very large system headers (e.g., macOS).
1170
1171Generating a PCH File
1172^^^^^^^^^^^^^^^^^^^^^
1173
1174To generate a PCH file using Clang, one invokes Clang with the
1175`-x <language>-header` option. This mirrors the interface in GCC
1176for generating PCH files:
1177
1178.. code-block:: console
1179
1180  $ gcc -x c-header test.h -o test.h.gch
1181  $ clang -x c-header test.h -o test.h.pch
1182
1183Using a PCH File
1184^^^^^^^^^^^^^^^^
1185
1186A PCH file can then be used as a prefix header when a :option:`-include`
1187option is passed to ``clang``:
1188
1189.. code-block:: console
1190
1191  $ clang -include test.h test.c -o test
1192
1193The ``clang`` driver will first check if a PCH file for ``test.h`` is
1194available; if so, the contents of ``test.h`` (and the files it includes)
1195will be processed from the PCH file. Otherwise, Clang falls back to
1196directly processing the content of ``test.h``. This mirrors the behavior
1197of GCC.
1198
1199.. note::
1200
1201  Clang does *not* automatically use PCH files for headers that are directly
1202  included within a source file. For example:
1203
1204  .. code-block:: console
1205
1206    $ clang -x c-header test.h -o test.h.pch
1207    $ cat test.c
1208    #include "test.h"
1209    $ clang test.c -o test
1210
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`.
1214
1215Relocatable PCH Files
1216^^^^^^^^^^^^^^^^^^^^^
1217
1218It is sometimes necessary to build a precompiled header from headers
1219that are not yet in their final, installed locations. For example, one
1220might build a precompiled header within the build tree that is then
1221meant to be installed alongside the headers. Clang permits the creation
1222of "relocatable" precompiled headers, which are built with a given path
1223(into the build directory) and can later be used from an installed
1224location.
1225
1226To build a relocatable precompiled header, place your headers into a
1227subdirectory whose structure mimics the installed location. For example,
1228if you want to build a precompiled header for the header ``mylib.h``
1229that will be installed into ``/usr/include``, create a subdirectory
1230``build/usr/include`` and place the header ``mylib.h`` into that
1231subdirectory. If ``mylib.h`` depends on other headers, then they can be
1232stored within ``build/usr/include`` in a way that mimics the installed
1233location.
1234
1235Building a relocatable precompiled header requires two additional
1236arguments. First, pass the ``--relocatable-pch`` flag to indicate that
1237the resulting PCH file should be relocatable. Second, pass
1238``-isysroot /path/to/build``, which makes all includes for your library
1239relative to the build directory. For example:
1240
1241.. code-block:: console
1242
1243  # clang -x c-header --relocatable-pch -isysroot /path/to/build /path/to/build/mylib.h mylib.h.pch
1244
1245When loading the relocatable PCH file, the various headers used in the
1246PCH file are found from the system header root. For example, ``mylib.h``
1247can be found in ``/usr/include/mylib.h``. If the headers are installed
1248in some other system root, the ``-isysroot`` option can be used provide
1249a different system root from which the headers will be based. For
1250example, ``-isysroot /Developer/SDKs/MacOSX10.4u.sdk`` will look for
1251``mylib.h`` in ``/Developer/SDKs/MacOSX10.4u.sdk/usr/include/mylib.h``.
1252
1253Relocatable precompiled headers are intended to be used in a limited
1254number of cases where the compilation environment is tightly controlled
1255and the precompiled header cannot be generated after headers have been
1256installed.
1257
1258.. _controlling-fp-behavior:
1259
1260Controlling Floating Point Behavior
1261-----------------------------------
1262
1263Clang provides a number of ways to control floating point behavior. The options
1264are listed below.
1265
1266.. option:: -ffast-math
1267
1268   Enable fast-math mode.  This option lets the
1269   compiler make aggressive, potentially-lossy assumptions about
1270   floating-point math.  These include:
1271
1272   * Floating-point math obeys regular algebraic rules for real numbers (e.g.
1273     ``+`` and ``*`` are associative, ``x/y == x * (1/y)``, and
1274     ``(a + b) * c == a * c + b * c``),
1275   * Operands to floating-point operations are not equal to ``NaN`` and
1276     ``Inf``, and
1277   * ``+0`` and ``-0`` are interchangeable.
1278
1279   ``-ffast-math`` also defines the ``__FAST_MATH__`` preprocessor
1280   macro. Some math libraries recognize this macro and change their behavior.
1281   With the exception of ``-ffp-contract=fast``, using any of the options
1282   below to disable any of the individual optimizations in ``-ffast-math``
1283   will cause ``__FAST_MATH__`` to no longer be set.
1284
1285  This option implies:
1286
1287   * ``-fno-honor-infinities``
1288
1289   * ``-fno-honor-nans``
1290
1291   * ``-fno-math-errno``
1292
1293   * ``-ffinite-math-only``
1294
1295   * ``-fassociative-math``
1296
1297   * ``-freciprocal-math``
1298
1299   * ``-fno-signed-zeros``
1300
1301   * ``-fno-trapping-math``
1302
1303   * ``-ffp-contract=fast``
1304
1305.. option:: -fdenormal-fp-math=<value>
1306
1307   Select which denormal numbers the code is permitted to require.
1308
1309   Valid values are:
1310
1311   * ``ieee`` - IEEE 754 denormal numbers
1312   * ``preserve-sign`` - the sign of a flushed-to-zero number is preserved in the sign of 0
1313   * ``positive-zero`` - denormals are flushed to positive zero
1314
1315   Defaults to ``ieee``.
1316
1317.. _opt_fstrict-float-cast-overflow:
1318
1319**-f[no-]strict-float-cast-overflow**
1320
1321   When a floating-point value is not representable in a destination integer
1322   type, the code has undefined behavior according to the language standard.
1323   By default, Clang will not guarantee any particular result in that case.
1324   With the 'no-strict' option, Clang attempts to match the overflowing behavior
1325   of the target's native float-to-int conversion instructions.
1326
1327.. _opt_fmath-errno:
1328
1329**-f[no-]math-errno**
1330
1331   Require math functions to indicate errors by setting errno.
1332   The default varies by ToolChain.  ``-fno-math-errno`` allows optimizations
1333   that might cause standard C math functions to not set ``errno``.
1334   For example, on some systems, the math function ``sqrt`` is specified
1335   as setting ``errno`` to ``EDOM`` when the input is negative. On these
1336   systems, the compiler cannot normally optimize a call to ``sqrt`` to use
1337   inline code (e.g. the x86 ``sqrtsd`` instruction) without additional
1338   checking to ensure that ``errno`` is set appropriately.
1339   ``-fno-math-errno`` permits these transformations.
1340
1341   On some targets, math library functions never set ``errno``, and so
1342   ``-fno-math-errno`` is the default. This includes most BSD-derived
1343   systems, including Darwin.
1344
1345.. _opt_ftrapping-math:
1346
1347**-f[no-]trapping-math**
1348
1349   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.
1350
1351- The option ``-ftrapping-math`` behaves identically to ``-ffp-exception-behavior=strict``.
1352- The option ``-fno-trapping-math`` behaves identically to ``-ffp-exception-behavior=ignore``.   This is the default.
1353
1354.. option:: -ffp-contract=<value>
1355
1356   Specify when the compiler is permitted to form fused floating-point
1357   operations, such as fused multiply-add (FMA). Fused operations are
1358   permitted to produce more precise results than performing the same
1359   operations separately.
1360
1361   The C standard permits intermediate floating-point results within an
1362   expression to be computed with more precision than their type would
1363   normally allow. This permits operation fusing, and Clang takes advantage
1364   of this by default. This behavior can be controlled with the ``FP_CONTRACT``
1365   and ``clang fp contract`` pragmas. Please refer to the pragma documentation
1366   for a description of how the pragmas interact with this option.
1367
1368   Valid values are:
1369
1370   * ``fast`` (fuse across statements disregarding pragmas, default for CUDA)
1371   * ``on`` (fuse in the same statement unless dictated by pragmas, default for languages other than CUDA/HIP)
1372   * ``off`` (never fuse)
1373   * ``fast-honor-pragmas`` (fuse across statements unless dictated by pragmas, default for HIP)
1374
1375.. _opt_fhonor-infinities:
1376
1377**-f[no-]honor-infinities**
1378
1379   If both ``-fno-honor-infinities`` and ``-fno-honor-nans`` are used,
1380   has the same effect as specifying ``-ffinite-math-only``.
1381
1382.. _opt_fhonor-nans:
1383
1384**-f[no-]honor-nans**
1385
1386   If both ``-fno-honor-infinities`` and ``-fno-honor-nans`` are used,
1387   has the same effect as specifying ``-ffinite-math-only``.
1388
1389.. _opt_fsigned-zeros:
1390
1391**-f[no-]signed-zeros**
1392
1393   Allow optimizations that ignore the sign of floating point zeros.
1394   Defaults to ``-fno-signed-zeros``.
1395
1396.. _opt_fassociative-math:
1397
1398**-f[no-]associative-math**
1399
1400  Allow floating point operations to be reassociated.
1401  Defaults to ``-fno-associative-math``.
1402
1403.. _opt_freciprocal-math:
1404
1405**-f[no-]reciprocal-math**
1406
1407  Allow division operations to be transformed into multiplication by a
1408  reciprocal. This can be significantly faster than an ordinary division
1409  but can also have significantly less precision. Defaults to
1410  ``-fno-reciprocal-math``.
1411
1412.. _opt_funsafe-math-optimizations:
1413
1414**-f[no-]unsafe-math-optimizations**
1415
1416   Allow unsafe floating-point optimizations. Also implies:
1417
1418   * ``-fassociative-math``
1419   * ``-freciprocal-math``
1420   * ``-fno-signed-zeroes``
1421   * ``-fno-trapping-math``.
1422
1423   Defaults to ``-fno-unsafe-math-optimizations``.
1424
1425.. _opt_ffinite-math-only:
1426
1427**-f[no-]finite-math-only**
1428
1429   Allow floating-point optimizations that assume arguments and results are
1430   not NaNs or +-Inf.  This defines the ``__FINITE_MATH_ONLY__`` preprocessor macro.
1431   Also implies:
1432
1433   * ``-fno-honor-infinities``
1434   * ``-fno-honor-nans``
1435
1436   Defaults to ``-fno-finite-math-only``.
1437
1438.. _opt_frounding-math:
1439
1440**-f[no-]rounding-math**
1441
1442Force floating-point operations to honor the dynamically-set rounding mode by default.
1443
1444The 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.
1445
1446Note 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``.
1447
1448- The option ``-fno-rounding-math`` allows the compiler to assume that the rounding mode is set to ``FE_TONEAREST``.  This is the default.
1449- 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.
1450
1451.. option:: -ffp-model=<value>
1452
1453   Specify floating point behavior. ``-ffp-model`` is an umbrella
1454   option that encompasses functionality provided by other, single
1455   purpose, floating point options.  Valid values are: ``precise``, ``strict``,
1456   and ``fast``.
1457   Details:
1458
1459   * ``precise`` Disables optimizations that are not value-safe on floating-point data, although FP contraction (FMA) is enabled (``-ffp-contract=fast``).  This is the default behavior.
1460   * ``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.
1461   * ``fast`` Behaves identically to specifying both ``-ffast-math`` and ``ffp-contract=fast``
1462
1463   Note: If your command line specifies multiple instances
1464   of the ``-ffp-model`` option, or if your command line option specifies
1465   ``-ffp-model`` and later on the command line selects a floating point
1466   option that has the effect of negating part of the  ``ffp-model`` that
1467   has been selected, then the compiler will issue a diagnostic warning
1468   that the override has occurred.
1469
1470.. option:: -ffp-exception-behavior=<value>
1471
1472   Specify the floating-point exception behavior.
1473
1474   Valid values are: ``ignore``, ``maytrap``, and ``strict``.
1475   The default value is ``ignore``.  Details:
1476
1477   * ``ignore`` The compiler assumes that the exception status flags will not be read and that floating point exceptions will be masked.
1478   * ``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.
1479   * ``strict`` The compiler ensures that all transformations strictly preserve the floating point exception semantics of the original code.
1480
1481
1482.. _fp-constant-eval:
1483
1484A note about Floating Point Constant Evaluation
1485^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1486
1487In C, the only place floating point operations are guaranteed to be evaluated
1488during translation is in the initializers of variables of static storage
1489duration, which are all notionally initialized before the program begins
1490executing (and thus before a non-default floating point environment can be
1491entered).  But C++ has many more contexts where floating point constant
1492evaluation occurs.  Specifically: for static/thread-local variables,
1493first try evaluating the initializer in a constant context, including in the
1494constant floating point environment (just like in C), and then, if that fails,
1495fall back to emitting runtime code to perform the initialization (which might
1496in general be in a different floating point environment).
1497
1498Consider this example when compiled with ``-frounding-math``
1499
1500   .. code-block:: console
1501
1502     constexpr float func_01(float x, float y) {
1503       return x + y;
1504     }
1505     float V1 = func_01(1.0F, 0x0.000001p0F);
1506
1507The C++ rule is that initializers for static storage duration variables are
1508first evaluated during translation (therefore, in the default rounding mode),
1509and only evaluated at runtime (and therefore in the runtime rounding mode) if
1510the compile-time evaluation fails. This is in line with the C rules;
1511C11 F.8.5 says: *All computation for automatic initialization is done (as if)
1512at execution time; thus, it is affected by any operative modes and raises
1513floating-point exceptions as required by IEC 60559 (provided the state for the
1514FENV_ACCESS pragma is ‘‘on’’). All computation for initialization of objects
1515that have static or thread storage duration is done (as if) at translation
1516time.* C++ generalizes this by adding another phase of initialization
1517(at runtime) if the translation-time initialization fails, but the
1518translation-time evaluation of the initializer of succeeds, it will be
1519treated as a constant initializer.
1520
1521
1522.. _controlling-code-generation:
1523
1524Controlling Code Generation
1525---------------------------
1526
1527Clang provides a number of ways to control code generation. The options
1528are listed below.
1529
1530**-f[no-]sanitize=check1,check2,...**
1531   Turn on runtime checks for various forms of undefined or suspicious
1532   behavior.
1533
1534   This option controls whether Clang adds runtime checks for various
1535   forms of undefined or suspicious behavior, and is disabled by
1536   default. If a check fails, a diagnostic message is produced at
1537   runtime explaining the problem. The main checks are:
1538
1539   -  .. _opt_fsanitize_address:
1540
1541      ``-fsanitize=address``:
1542      :doc:`AddressSanitizer`, a memory error
1543      detector.
1544   -  .. _opt_fsanitize_thread:
1545
1546      ``-fsanitize=thread``: :doc:`ThreadSanitizer`, a data race detector.
1547   -  .. _opt_fsanitize_memory:
1548
1549      ``-fsanitize=memory``: :doc:`MemorySanitizer`,
1550      a detector of uninitialized reads. Requires instrumentation of all
1551      program code.
1552   -  .. _opt_fsanitize_undefined:
1553
1554      ``-fsanitize=undefined``: :doc:`UndefinedBehaviorSanitizer`,
1555      a fast and compatible undefined behavior checker.
1556
1557   -  ``-fsanitize=dataflow``: :doc:`DataFlowSanitizer`, a general data
1558      flow analysis.
1559   -  ``-fsanitize=cfi``: :doc:`control flow integrity <ControlFlowIntegrity>`
1560      checks. Requires ``-flto``.
1561   -  ``-fsanitize=safe-stack``: :doc:`safe stack <SafeStack>`
1562      protection against stack-based memory corruption errors.
1563
1564   There are more fine-grained checks available: see
1565   the :ref:`list <ubsan-checks>` of specific kinds of
1566   undefined behavior that can be detected and the :ref:`list <cfi-schemes>`
1567   of control flow integrity schemes.
1568
1569   The ``-fsanitize=`` argument must also be provided when linking, in
1570   order to link to the appropriate runtime library.
1571
1572   It is not possible to combine more than one of the ``-fsanitize=address``,
1573   ``-fsanitize=thread``, and ``-fsanitize=memory`` checkers in the same
1574   program.
1575
1576**-f[no-]sanitize-recover=check1,check2,...**
1577
1578**-f[no-]sanitize-recover[=all]**
1579
1580   Controls which checks enabled by ``-fsanitize=`` flag are non-fatal.
1581   If the check is fatal, program will halt after the first error
1582   of this kind is detected and error report is printed.
1583
1584   By default, non-fatal checks are those enabled by
1585   :doc:`UndefinedBehaviorSanitizer`,
1586   except for ``-fsanitize=return`` and ``-fsanitize=unreachable``. Some
1587   sanitizers may not support recovery (or not support it by default
1588   e.g. :doc:`AddressSanitizer`), and always crash the program after the issue
1589   is detected.
1590
1591   Note that the ``-fsanitize-trap`` flag has precedence over this flag.
1592   This means that if a check has been configured to trap elsewhere on the
1593   command line, or if the check traps by default, this flag will not have
1594   any effect unless that sanitizer's trapping behavior is disabled with
1595   ``-fno-sanitize-trap``.
1596
1597   For example, if a command line contains the flags ``-fsanitize=undefined
1598   -fsanitize-trap=undefined``, the flag ``-fsanitize-recover=alignment``
1599   will have no effect on its own; it will need to be accompanied by
1600   ``-fno-sanitize-trap=alignment``.
1601
1602**-f[no-]sanitize-trap=check1,check2,...**
1603
1604**-f[no-]sanitize-trap[=all]**
1605
1606   Controls which checks enabled by the ``-fsanitize=`` flag trap. This
1607   option is intended for use in cases where the sanitizer runtime cannot
1608   be used (for instance, when building libc or a kernel module), or where
1609   the binary size increase caused by the sanitizer runtime is a concern.
1610
1611   This flag is only compatible with :doc:`control flow integrity
1612   <ControlFlowIntegrity>` schemes and :doc:`UndefinedBehaviorSanitizer`
1613   checks other than ``vptr``.
1614
1615   This flag is enabled by default for sanitizers in the ``cfi`` group.
1616
1617.. option:: -fsanitize-ignorelist=/path/to/ignorelist/file
1618
1619   Disable or modify sanitizer checks for objects (source files, functions,
1620   variables, types) listed in the file. See
1621   :doc:`SanitizerSpecialCaseList` for file format description.
1622
1623.. option:: -fno-sanitize-ignorelist
1624
1625   Don't use ignorelist file, if it was specified earlier in the command line.
1626
1627**-f[no-]sanitize-coverage=[type,features,...]**
1628
1629   Enable simple code coverage in addition to certain sanitizers.
1630   See :doc:`SanitizerCoverage` for more details.
1631
1632**-f[no-]sanitize-stats**
1633
1634   Enable simple statistics gathering for the enabled sanitizers.
1635   See :doc:`SanitizerStats` for more details.
1636
1637.. option:: -fsanitize-undefined-trap-on-error
1638
1639   Deprecated alias for ``-fsanitize-trap=undefined``.
1640
1641.. option:: -fsanitize-cfi-cross-dso
1642
1643   Enable cross-DSO control flow integrity checks. This flag modifies
1644   the behavior of sanitizers in the ``cfi`` group to allow checking
1645   of cross-DSO virtual and indirect calls.
1646
1647.. option:: -fsanitize-cfi-icall-generalize-pointers
1648
1649   Generalize pointers in return and argument types in function type signatures
1650   checked by Control Flow Integrity indirect call checking. See
1651   :doc:`ControlFlowIntegrity` for more details.
1652
1653.. option:: -fstrict-vtable-pointers
1654
1655   Enable optimizations based on the strict rules for overwriting polymorphic
1656   C++ objects, i.e. the vptr is invariant during an object's lifetime.
1657   This enables better devirtualization. Turned off by default, because it is
1658   still experimental.
1659
1660.. option:: -fwhole-program-vtables
1661
1662   Enable whole-program vtable optimizations, such as single-implementation
1663   devirtualization and virtual constant propagation, for classes with
1664   :doc:`hidden LTO visibility <LTOVisibility>`. Requires ``-flto``.
1665
1666.. option:: -fforce-emit-vtables
1667
1668   In order to improve devirtualization, forces emitting of vtables even in
1669   modules where it isn't necessary. It causes more inline virtual functions
1670   to be emitted.
1671
1672.. option:: -fno-assume-sane-operator-new
1673
1674   Don't assume that the C++'s new operator is sane.
1675
1676   This option tells the compiler to do not assume that C++'s global
1677   new operator will always return a pointer that does not alias any
1678   other pointer when the function returns.
1679
1680.. option:: -ftrap-function=[name]
1681
1682   Instruct code generator to emit a function call to the specified
1683   function name for ``__builtin_trap()``.
1684
1685   LLVM code generator translates ``__builtin_trap()`` to a trap
1686   instruction if it is supported by the target ISA. Otherwise, the
1687   builtin is translated into a call to ``abort``. If this option is
1688   set, then the code generator will always lower the builtin to a call
1689   to the specified function regardless of whether the target ISA has a
1690   trap instruction. This option is useful for environments (e.g.
1691   deeply embedded) where a trap cannot be properly handled, or when
1692   some custom behavior is desired.
1693
1694.. option:: -ftls-model=[model]
1695
1696   Select which TLS model to use.
1697
1698   Valid values are: ``global-dynamic``, ``local-dynamic``,
1699   ``initial-exec`` and ``local-exec``. The default value is
1700   ``global-dynamic``. The compiler may use a different model if the
1701   selected model is not supported by the target, or if a more
1702   efficient model can be used. The TLS model can be overridden per
1703   variable using the ``tls_model`` attribute.
1704
1705.. option:: -femulated-tls
1706
1707   Select emulated TLS model, which overrides all -ftls-model choices.
1708
1709   In emulated TLS mode, all access to TLS variables are converted to
1710   calls to __emutls_get_address in the runtime library.
1711
1712.. option:: -mhwdiv=[values]
1713
1714   Select the ARM modes (arm or thumb) that support hardware division
1715   instructions.
1716
1717   Valid values are: ``arm``, ``thumb`` and ``arm,thumb``.
1718   This option is used to indicate which mode (arm or thumb) supports
1719   hardware division instructions. This only applies to the ARM
1720   architecture.
1721
1722.. option:: -m[no-]crc
1723
1724   Enable or disable CRC instructions.
1725
1726   This option is used to indicate whether CRC instructions are to
1727   be generated. This only applies to the ARM architecture.
1728
1729   CRC instructions are enabled by default on ARMv8.
1730
1731.. option:: -mgeneral-regs-only
1732
1733   Generate code which only uses the general purpose registers.
1734
1735   This option restricts the generated code to use general registers
1736   only. This only applies to the AArch64 architecture.
1737
1738.. option:: -mcompact-branches=[values]
1739
1740   Control the usage of compact branches for MIPSR6.
1741
1742   Valid values are: ``never``, ``optimal`` and ``always``.
1743   The default value is ``optimal`` which generates compact branches
1744   when a delay slot cannot be filled. ``never`` disables the usage of
1745   compact branches and ``always`` generates compact branches whenever
1746   possible.
1747
1748**-f[no-]max-type-align=[number]**
1749   Instruct the code generator to not enforce a higher alignment than the given
1750   number (of bytes) when accessing memory via an opaque pointer or reference.
1751   This cap is ignored when directly accessing a variable or when the pointee
1752   type has an explicit “aligned” attribute.
1753
1754   The value should usually be determined by the properties of the system allocator.
1755   Some builtin types, especially vector types, have very high natural alignments;
1756   when working with values of those types, Clang usually wants to use instructions
1757   that take advantage of that alignment.  However, many system allocators do
1758   not promise to return memory that is more than 8-byte or 16-byte-aligned.  Use
1759   this option to limit the alignment that the compiler can assume for an arbitrary
1760   pointer, which may point onto the heap.
1761
1762   This option does not affect the ABI alignment of types; the layout of structs and
1763   unions and the value returned by the alignof operator remain the same.
1764
1765   This option can be overridden on a case-by-case basis by putting an explicit
1766   “aligned” alignment on a struct, union, or typedef.  For example:
1767
1768   .. code-block:: console
1769
1770      #include <immintrin.h>
1771      // Make an aligned typedef of the AVX-512 16-int vector type.
1772      typedef __v16si __aligned_v16si __attribute__((aligned(64)));
1773
1774      void initialize_vector(__aligned_v16si *v) {
1775        // The compiler may assume that ‘v’ is 64-byte aligned, regardless of the
1776        // value of -fmax-type-align.
1777      }
1778
1779.. option:: -faddrsig, -fno-addrsig
1780
1781   Controls whether Clang emits an address-significance table into the object
1782   file. Address-significance tables allow linkers to implement `safe ICF
1783   <https://research.google.com/pubs/archive/36912.pdf>`_ without the false
1784   positives that can result from other implementation techniques such as
1785   relocation scanning. Address-significance tables are enabled by default
1786   on ELF targets when using the integrated assembler. This flag currently
1787   only has an effect on ELF targets.
1788
1789**-f[no]-unique-internal-linkage-names**
1790
1791   Controls whether Clang emits a unique (best-effort) symbol name for internal
1792   linkage symbols.  When this option is set, compiler hashes the main source
1793   file path from the command line and appends it to all internal symbols. If a
1794   program contains multiple objects compiled with the same command-line source
1795   file path, the symbols are not guaranteed to be unique.  This option is
1796   particularly useful in attributing profile information to the correct
1797   function when multiple functions with the same private linkage name exist
1798   in the binary.
1799
1800   It should be noted that this option cannot guarantee uniqueness and the
1801   following is an example where it is not unique when two modules contain
1802   symbols with the same private linkage name:
1803
1804   .. code-block:: console
1805
1806     $ cd $P/foo && clang -c -funique-internal-linkage-names name_conflict.c
1807     $ cd $P/bar && clang -c -funique-internal-linkage-names name_conflict.c
1808     $ cd $P && clang foo/name_conflict.o && bar/name_conflict.o
1809
1810**-fbasic-block-sections=[labels, all, list=<arg>, none]**
1811
1812  Controls how Clang emits text sections for basic blocks. With values ``all``
1813  and ``list=<arg>``, each basic block or a subset of basic blocks can be placed
1814  in its own unique section. With the "labels" value, normal text sections are
1815  emitted, but a ``.bb_addr_map`` section is emitted which includes address
1816  offsets for each basic block in the program, relative to the parent function
1817  address.
1818
1819  With the ``list=<arg>`` option, a file containing the subset of basic blocks
1820  that need to placed in unique sections can be specified.  The format of the
1821  file is as follows.  For example, ``list=spec.txt`` where ``spec.txt`` is the
1822  following:
1823
1824  ::
1825
1826        !foo
1827        !!2
1828        !_Z3barv
1829
1830  will place the machine basic block with ``id 2`` in function ``foo`` in a
1831  unique section.  It will also place all basic blocks of functions ``bar``
1832  in unique sections.
1833
1834  Further, section clusters can also be specified using the ``list=<arg>``
1835  option.  For example, ``list=spec.txt`` where ``spec.txt`` contains:
1836
1837  ::
1838
1839        !foo
1840        !!1 !!3 !!5
1841        !!2 !!4 !!6
1842
1843  will create two unique sections for function ``foo`` with the first
1844  containing the odd numbered basic blocks and the second containing the
1845  even numbered basic blocks.
1846
1847  Basic block sections allow the linker to reorder basic blocks and enables
1848  link-time optimizations like whole program inter-procedural basic block
1849  reordering.
1850
1851Profile Guided Optimization
1852---------------------------
1853
1854Profile information enables better optimization. For example, knowing that a
1855branch is taken very frequently helps the compiler make better decisions when
1856ordering basic blocks. Knowing that a function ``foo`` is called more
1857frequently than another function ``bar`` helps the inliner. Optimization
1858levels ``-O2`` and above are recommended for use of profile guided optimization.
1859
1860Clang supports profile guided optimization with two different kinds of
1861profiling. A sampling profiler can generate a profile with very low runtime
1862overhead, or you can build an instrumented version of the code that collects
1863more detailed profile information. Both kinds of profiles can provide execution
1864counts for instructions in the code and information on branches taken and
1865function invocation.
1866
1867Regardless of which kind of profiling you use, be careful to collect profiles
1868by running your code with inputs that are representative of the typical
1869behavior. Code that is not exercised in the profile will be optimized as if it
1870is unimportant, and the compiler may make poor optimization choices for code
1871that is disproportionately used while profiling.
1872
1873Differences Between Sampling and Instrumentation
1874^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1875
1876Although both techniques are used for similar purposes, there are important
1877differences between the two:
1878
18791. Profile data generated with one cannot be used by the other, and there is no
1880   conversion tool that can convert one to the other. So, a profile generated
1881   via ``-fprofile-instr-generate`` must be used with ``-fprofile-instr-use``.
1882   Similarly, sampling profiles generated by external profilers must be
1883   converted and used with ``-fprofile-sample-use``.
1884
18852. Instrumentation profile data can be used for code coverage analysis and
1886   optimization.
1887
18883. Sampling profiles can only be used for optimization. They cannot be used for
1889   code coverage analysis. Although it would be technically possible to use
1890   sampling profiles for code coverage, sample-based profiles are too
1891   coarse-grained for code coverage purposes; it would yield poor results.
1892
18934. Sampling profiles must be generated by an external tool. The profile
1894   generated by that tool must then be converted into a format that can be read
1895   by LLVM. The section on sampling profilers describes one of the supported
1896   sampling profile formats.
1897
1898
1899Using Sampling Profilers
1900^^^^^^^^^^^^^^^^^^^^^^^^
1901
1902Sampling profilers are used to collect runtime information, such as
1903hardware counters, while your application executes. They are typically
1904very efficient and do not incur a large runtime overhead. The
1905sample data collected by the profiler can be used during compilation
1906to determine what the most executed areas of the code are.
1907
1908Using the data from a sample profiler requires some changes in the way
1909a program is built. Before the compiler can use profiling information,
1910the code needs to execute under the profiler. The following is the
1911usual build cycle when using sample profilers for optimization:
1912
19131. Build the code with source line table information. You can use all the
1914   usual build flags that you always build your application with. The only
1915   requirement is that you add ``-gline-tables-only`` or ``-g`` to the
1916   command line. This is important for the profiler to be able to map
1917   instructions back to source line locations.
1918
1919   .. code-block:: console
1920
1921     $ clang++ -O2 -gline-tables-only code.cc -o code
1922
19232. Run the executable under a sampling profiler. The specific profiler
1924   you use does not really matter, as long as its output can be converted
1925   into the format that the LLVM optimizer understands. Currently, there
1926   exists a conversion tool for the Linux Perf profiler
1927   (https://perf.wiki.kernel.org/), so these examples assume that you
1928   are using Linux Perf to profile your code.
1929
1930   .. code-block:: console
1931
1932     $ perf record -b ./code
1933
1934   Note the use of the ``-b`` flag. This tells Perf to use the Last Branch
1935   Record (LBR) to record call chains. While this is not strictly required,
1936   it provides better call information, which improves the accuracy of
1937   the profile data.
1938
19393. Convert the collected profile data to LLVM's sample profile format.
1940   This is currently supported via the AutoFDO converter ``create_llvm_prof``.
1941   It is available at https://github.com/google/autofdo. Once built and
1942   installed, you can convert the ``perf.data`` file to LLVM using
1943   the command:
1944
1945   .. code-block:: console
1946
1947     $ create_llvm_prof --binary=./code --out=code.prof
1948
1949   This will read ``perf.data`` and the binary file ``./code`` and emit
1950   the profile data in ``code.prof``. Note that if you ran ``perf``
1951   without the ``-b`` flag, you need to use ``--use_lbr=false`` when
1952   calling ``create_llvm_prof``.
1953
19544. Build the code again using the collected profile. This step feeds
1955   the profile back to the optimizers. This should result in a binary
1956   that executes faster than the original one. Note that you are not
1957   required to build the code with the exact same arguments that you
1958   used in the first step. The only requirement is that you build the code
1959   with ``-gline-tables-only`` and ``-fprofile-sample-use``.
1960
1961   .. code-block:: console
1962
1963     $ clang++ -O2 -gline-tables-only -fprofile-sample-use=code.prof code.cc -o code
1964
1965
1966Sample Profile Formats
1967""""""""""""""""""""""
1968
1969Since external profilers generate profile data in a variety of custom formats,
1970the data generated by the profiler must be converted into a format that can be
1971read by the backend. LLVM supports three different sample profile formats:
1972
19731. ASCII text. This is the easiest one to generate. The file is divided into
1974   sections, which correspond to each of the functions with profile
1975   information. The format is described below. It can also be generated from
1976   the binary or gcov formats using the ``llvm-profdata`` tool.
1977
19782. Binary encoding. This uses a more efficient encoding that yields smaller
1979   profile files. This is the format generated by the ``create_llvm_prof`` tool
1980   in https://github.com/google/autofdo.
1981
19823. GCC encoding. This is based on the gcov format, which is accepted by GCC. It
1983   is only interesting in environments where GCC and Clang co-exist. This
1984   encoding is only generated by the ``create_gcov`` tool in
1985   https://github.com/google/autofdo. It can be read by LLVM and
1986   ``llvm-profdata``, but it cannot be generated by either.
1987
1988If you are using Linux Perf to generate sampling profiles, you can use the
1989conversion tool ``create_llvm_prof`` described in the previous section.
1990Otherwise, you will need to write a conversion tool that converts your
1991profiler's native format into one of these three.
1992
1993
1994Sample Profile Text Format
1995""""""""""""""""""""""""""
1996
1997This section describes the ASCII text format for sampling profiles. It is,
1998arguably, the easiest one to generate. If you are interested in generating any
1999of the other two, consult the ``ProfileData`` library in LLVM's source tree
2000(specifically, ``include/llvm/ProfileData/SampleProfReader.h``).
2001
2002.. code-block:: console
2003
2004    function1:total_samples:total_head_samples
2005     offset1[.discriminator]: number_of_samples [fn1:num fn2:num ... ]
2006     offset2[.discriminator]: number_of_samples [fn3:num fn4:num ... ]
2007     ...
2008     offsetN[.discriminator]: number_of_samples [fn5:num fn6:num ... ]
2009     offsetA[.discriminator]: fnA:num_of_total_samples
2010      offsetA1[.discriminator]: number_of_samples [fn7:num fn8:num ... ]
2011      offsetA1[.discriminator]: number_of_samples [fn9:num fn10:num ... ]
2012      offsetB[.discriminator]: fnB:num_of_total_samples
2013       offsetB1[.discriminator]: number_of_samples [fn11:num fn12:num ... ]
2014
2015This is a nested tree in which the indentation represents the nesting level
2016of the inline stack. There are no blank lines in the file. And the spacing
2017within a single line is fixed. Additional spaces will result in an error
2018while reading the file.
2019
2020Any line starting with the '#' character is completely ignored.
2021
2022Inlined calls are represented with indentation. The Inline stack is a
2023stack of source locations in which the top of the stack represents the
2024leaf function, and the bottom of the stack represents the actual
2025symbol to which the instruction belongs.
2026
2027Function names must be mangled in order for the profile loader to
2028match them in the current translation unit. The two numbers in the
2029function header specify how many total samples were accumulated in the
2030function (first number), and the total number of samples accumulated
2031in the prologue of the function (second number). This head sample
2032count provides an indicator of how frequently the function is invoked.
2033
2034There are two types of lines in the function body.
2035
2036-  Sampled line represents the profile information of a source location.
2037   ``offsetN[.discriminator]: number_of_samples [fn5:num fn6:num ... ]``
2038
2039-  Callsite line represents the profile information of an inlined callsite.
2040   ``offsetA[.discriminator]: fnA:num_of_total_samples``
2041
2042Each sampled line may contain several items. Some are optional (marked
2043below):
2044
2045a. Source line offset. This number represents the line number
2046   in the function where the sample was collected. The line number is
2047   always relative to the line where symbol of the function is
2048   defined. So, if the function has its header at line 280, the offset
2049   13 is at line 293 in the file.
2050
2051   Note that this offset should never be a negative number. This could
2052   happen in cases like macros. The debug machinery will register the
2053   line number at the point of macro expansion. So, if the macro was
2054   expanded in a line before the start of the function, the profile
2055   converter should emit a 0 as the offset (this means that the optimizers
2056   will not be able to associate a meaningful weight to the instructions
2057   in the macro).
2058
2059b. [OPTIONAL] Discriminator. This is used if the sampled program
2060   was compiled with DWARF discriminator support
2061   (http://wiki.dwarfstd.org/index.php?title=Path_Discriminators).
2062   DWARF discriminators are unsigned integer values that allow the
2063   compiler to distinguish between multiple execution paths on the
2064   same source line location.
2065
2066   For example, consider the line of code ``if (cond) foo(); else bar();``.
2067   If the predicate ``cond`` is true 80% of the time, then the edge
2068   into function ``foo`` should be considered to be taken most of the
2069   time. But both calls to ``foo`` and ``bar`` are at the same source
2070   line, so a sample count at that line is not sufficient. The
2071   compiler needs to know which part of that line is taken more
2072   frequently.
2073
2074   This is what discriminators provide. In this case, the calls to
2075   ``foo`` and ``bar`` will be at the same line, but will have
2076   different discriminator values. This allows the compiler to correctly
2077   set edge weights into ``foo`` and ``bar``.
2078
2079c. Number of samples. This is an integer quantity representing the
2080   number of samples collected by the profiler at this source
2081   location.
2082
2083d. [OPTIONAL] Potential call targets and samples. If present, this
2084   line contains a call instruction. This models both direct and
2085   number of samples. For example,
2086
2087   .. code-block:: console
2088
2089     130: 7  foo:3  bar:2  baz:7
2090
2091   The above means that at relative line offset 130 there is a call
2092   instruction that calls one of ``foo()``, ``bar()`` and ``baz()``,
2093   with ``baz()`` being the relatively more frequently called target.
2094
2095As an example, consider a program with the call chain ``main -> foo -> bar``.
2096When built with optimizations enabled, the compiler may inline the
2097calls to ``bar`` and ``foo`` inside ``main``. The generated profile
2098could then be something like this:
2099
2100.. code-block:: console
2101
2102    main:35504:0
2103    1: _Z3foov:35504
2104      2: _Z32bari:31977
2105      1.1: 31977
2106    2: 0
2107
2108This profile indicates that there were a total of 35,504 samples
2109collected in main. All of those were at line 1 (the call to ``foo``).
2110Of those, 31,977 were spent inside the body of ``bar``. The last line
2111of the profile (``2: 0``) corresponds to line 2 inside ``main``. No
2112samples were collected there.
2113
2114Profiling with Instrumentation
2115^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2116
2117Clang also supports profiling via instrumentation. This requires building a
2118special instrumented version of the code and has some runtime
2119overhead during the profiling, but it provides more detailed results than a
2120sampling profiler. It also provides reproducible results, at least to the
2121extent that the code behaves consistently across runs.
2122
2123Here are the steps for using profile guided optimization with
2124instrumentation:
2125
21261. Build an instrumented version of the code by compiling and linking with the
2127   ``-fprofile-instr-generate`` option.
2128
2129   .. code-block:: console
2130
2131     $ clang++ -O2 -fprofile-instr-generate code.cc -o code
2132
21332. Run the instrumented executable with inputs that reflect the typical usage.
2134   By default, the profile data will be written to a ``default.profraw`` file
2135   in the current directory. You can override that default by using option
2136   ``-fprofile-instr-generate=`` or by setting the ``LLVM_PROFILE_FILE``
2137   environment variable to specify an alternate file. If non-default file name
2138   is specified by both the environment variable and the command line option,
2139   the environment variable takes precedence. The file name pattern specified
2140   can include different modifiers: ``%p``, ``%h``, and ``%m``.
2141
2142   Any instance of ``%p`` in that file name will be replaced by the process
2143   ID, so that you can easily distinguish the profile output from multiple
2144   runs.
2145
2146   .. code-block:: console
2147
2148     $ LLVM_PROFILE_FILE="code-%p.profraw" ./code
2149
2150   The modifier ``%h`` can be used in scenarios where the same instrumented
2151   binary is run in multiple different host machines dumping profile data
2152   to a shared network based storage. The ``%h`` specifier will be substituted
2153   with the hostname so that profiles collected from different hosts do not
2154   clobber each other.
2155
2156   While the use of ``%p`` specifier can reduce the likelihood for the profiles
2157   dumped from different processes to clobber each other, such clobbering can still
2158   happen because of the ``pid`` re-use by the OS. Another side-effect of using
2159   ``%p`` is that the storage requirement for raw profile data files is greatly
2160   increased.  To avoid issues like this, the ``%m`` specifier can used in the profile
2161   name.  When this specifier is used, the profiler runtime will substitute ``%m``
2162   with a unique integer identifier associated with the instrumented binary. Additionally,
2163   multiple raw profiles dumped from different processes that share a file system (can be
2164   on different hosts) will be automatically merged by the profiler runtime during the
2165   dumping. If the program links in multiple instrumented shared libraries, each library
2166   will dump the profile data into its own profile data file (with its unique integer
2167   id embedded in the profile name). Note that the merging enabled by ``%m`` is for raw
2168   profile data generated by profiler runtime. The resulting merged "raw" profile data
2169   file still needs to be converted to a different format expected by the compiler (
2170   see step 3 below).
2171
2172   .. code-block:: console
2173
2174     $ LLVM_PROFILE_FILE="code-%m.profraw" ./code
2175
2176
21773. Combine profiles from multiple runs and convert the "raw" profile format to
2178   the input expected by clang. Use the ``merge`` command of the
2179   ``llvm-profdata`` tool to do this.
2180
2181   .. code-block:: console
2182
2183     $ llvm-profdata merge -output=code.profdata code-*.profraw
2184
2185   Note that this step is necessary even when there is only one "raw" profile,
2186   since the merge operation also changes the file format.
2187
21884. Build the code again using the ``-fprofile-instr-use`` option to specify the
2189   collected profile data.
2190
2191   .. code-block:: console
2192
2193     $ clang++ -O2 -fprofile-instr-use=code.profdata code.cc -o code
2194
2195   You can repeat step 4 as often as you like without regenerating the
2196   profile. As you make changes to your code, clang may no longer be able to
2197   use the profile data. It will warn you when this happens.
2198
2199Profile generation using an alternative instrumentation method can be
2200controlled by the GCC-compatible flags ``-fprofile-generate`` and
2201``-fprofile-use``. Although these flags are semantically equivalent to
2202their GCC counterparts, they *do not* handle GCC-compatible profiles.
2203They are only meant to implement GCC's semantics with respect to
2204profile creation and use. Flag ``-fcs-profile-generate`` also instruments
2205programs using the same instrumentation method as ``-fprofile-generate``.
2206
2207.. option:: -fprofile-generate[=<dirname>]
2208
2209  The ``-fprofile-generate`` and ``-fprofile-generate=`` flags will use
2210  an alternative instrumentation method for profile generation. When
2211  given a directory name, it generates the profile file
2212  ``default_%m.profraw`` in the directory named ``dirname`` if specified.
2213  If ``dirname`` does not exist, it will be created at runtime. ``%m`` specifier
2214  will be substituted with a unique id documented in step 2 above. In other words,
2215  with ``-fprofile-generate[=<dirname>]`` option, the "raw" profile data automatic
2216  merging is turned on by default, so there will no longer any risk of profile
2217  clobbering from different running processes.  For example,
2218
2219  .. code-block:: console
2220
2221    $ clang++ -O2 -fprofile-generate=yyy/zzz code.cc -o code
2222
2223  When ``code`` is executed, the profile will be written to the file
2224  ``yyy/zzz/default_xxxx.profraw``.
2225
2226  To generate the profile data file with the compiler readable format, the
2227  ``llvm-profdata`` tool can be used with the profile directory as the input:
2228
2229   .. code-block:: console
2230
2231     $ llvm-profdata merge -output=code.profdata yyy/zzz/
2232
2233 If the user wants to turn off the auto-merging feature, or simply override the
2234 the profile dumping path specified at command line, the environment variable
2235 ``LLVM_PROFILE_FILE`` can still be used to override
2236 the directory and filename for the profile file at runtime.
2237
2238.. option:: -fcs-profile-generate[=<dirname>]
2239
2240  The ``-fcs-profile-generate`` and ``-fcs-profile-generate=`` flags will use
2241  the same instrumentation method, and generate the same profile as in the
2242  ``-fprofile-generate`` and ``-fprofile-generate=`` flags. The difference is
2243  that the instrumentation is performed after inlining so that the resulted
2244  profile has a better context sensitive information. They cannot be used
2245  together with ``-fprofile-generate`` and ``-fprofile-generate=`` flags.
2246  They are typically used in conjunction with ``-fprofile-use`` flag.
2247  The profile generated by ``-fcs-profile-generate`` and ``-fprofile-generate``
2248  can be merged by llvm-profdata. A use example:
2249
2250  .. code-block:: console
2251
2252    $ clang++ -O2 -fprofile-generate=yyy/zzz code.cc -o code
2253    $ ./code
2254    $ llvm-profdata merge -output=code.profdata yyy/zzz/
2255
2256  The first few steps are the same as that in ``-fprofile-generate``
2257  compilation. Then perform a second round of instrumentation.
2258
2259  .. code-block:: console
2260
2261    $ clang++ -O2 -fprofile-use=code.profdata -fcs-profile-generate=sss/ttt \
2262      -o cs_code
2263    $ ./cs_code
2264    $ llvm-profdata merge -output=cs_code.profdata sss/ttt code.profdata
2265
2266  The resulted ``cs_code.prodata`` combines ``code.profdata`` and the profile
2267  generated from binary ``cs_code``. Profile ``cs_code.profata`` can be used by
2268  ``-fprofile-use`` compilation.
2269
2270  .. code-block:: console
2271
2272    $ clang++ -O2 -fprofile-use=cs_code.profdata
2273
2274  The above command will read both profiles to the compiler at the identical
2275  point of instrumentations.
2276
2277.. option:: -fprofile-use[=<pathname>]
2278
2279  Without any other arguments, ``-fprofile-use`` behaves identically to
2280  ``-fprofile-instr-use``. Otherwise, if ``pathname`` is the full path to a
2281  profile file, it reads from that file. If ``pathname`` is a directory name,
2282  it reads from ``pathname/default.profdata``.
2283
2284.. option:: -fprofile-update[=<method>]
2285
2286  Unless ``-fsanitize=thread`` is specified, the default is ``single``, which
2287  uses non-atomic increments. The counters can be inaccurate under thread
2288  contention. ``atomic`` uses atomic increments which is accurate but has
2289  overhead. ``prefer-atomic`` will be transformed to ``atomic`` when supported
2290  by the target, or ``single`` otherwise.
2291
2292  This option currently works with ``-fprofile-arcs`` and ``-fprofile-instr-generate``,
2293  but not with ``-fprofile-generate``.
2294
2295Disabling Instrumentation
2296^^^^^^^^^^^^^^^^^^^^^^^^^
2297
2298In certain situations, it may be useful to disable profile generation or use
2299for specific files in a build, without affecting the main compilation flags
2300used for the other files in the project.
2301
2302In these cases, you can use the flag ``-fno-profile-instr-generate`` (or
2303``-fno-profile-generate``) to disable profile generation, and
2304``-fno-profile-instr-use`` (or ``-fno-profile-use``) to disable profile use.
2305
2306Note that these flags should appear after the corresponding profile
2307flags to have an effect.
2308
2309Instrumenting only selected files or functions
2310^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2311
2312Sometimes it's useful to only instrument certain files or functions.  For
2313example in automated testing infrastructure, it may be desirable to only
2314instrument files or functions that were modified by a patch to reduce the
2315overhead of instrumenting a full system.
2316
2317This can be done using the ``-fprofile-list`` option.
2318
2319.. option:: -fprofile-list=<pathname>
2320
2321  This option can be used to apply profile instrumentation only to selected
2322  files or functions. ``pathname`` should point to a file in the
2323  :doc:`SanitizerSpecialCaseList` format which selects which files and
2324  functions to instrument.
2325
2326  .. code-block:: console
2327
2328    $ echo "fun:test" > fun.list
2329    $ clang++ -O2 -fprofile-instr-generate -fprofile-list=fun.list code.cc -o code
2330
2331The option can be specified multiple times to pass multiple files.
2332
2333.. code-block:: console
2334
2335    $ echo "!fun:*test*" > fun.list
2336    $ echo "src:code.cc" > src.list
2337    % clang++ -O2 -fprofile-instr-generate -fcoverage-mapping -fprofile-list=fun.list -fprofile-list=code.list code.cc -o code
2338
2339To filter individual functions or entire source files using ``fun:<name>`` or
2340``src:<file>`` respectively. To exclude a function or a source file, use
2341``!fun:<name>`` or ``!src:<file>`` respectively. The format also supports
2342wildcard expansion. The compiler generated functions are assumed to be located
2343in the main source file.  It is also possible to restrict the filter to a
2344particular instrumentation type by using a named section.
2345
2346.. code-block:: none
2347
2348  # all functions whose name starts with foo will be instrumented.
2349  fun:foo*
2350
2351  # except for foo1 which will be excluded from instrumentation.
2352  !fun:foo1
2353
2354  # every function in path/to/foo.cc will be instrumented.
2355  src:path/to/foo.cc
2356
2357  # bar will be instrumented only when using backend instrumentation.
2358  # Recognized section names are clang, llvm and csllvm.
2359  [llvm]
2360  fun:bar
2361
2362When the file contains only excludes, all files and functions except for the
2363excluded ones will be instrumented. Otherwise, only the files and functions
2364specified will be instrumented.
2365
2366Profile remapping
2367^^^^^^^^^^^^^^^^^
2368
2369When the program is compiled after a change that affects many symbol names,
2370pre-existing profile data may no longer match the program. For example:
2371
2372 * switching from libstdc++ to libc++ will result in the mangled names of all
2373   functions taking standard library types to change
2374 * renaming a widely-used type in C++ will result in the mangled names of all
2375   functions that have parameters involving that type to change
2376 * moving from a 32-bit compilation to a 64-bit compilation may change the
2377   underlying type of ``size_t`` and similar types, resulting in changes to
2378   manglings
2379
2380Clang allows use of a profile remapping file to specify that such differences
2381in mangled names should be ignored when matching the profile data against the
2382program.
2383
2384.. option:: -fprofile-remapping-file=<file>
2385
2386  Specifies a file containing profile remapping information, that will be
2387  used to match mangled names in the profile data to mangled names in the
2388  program.
2389
2390The profile remapping file is a text file containing lines of the form
2391
2392.. code-block:: text
2393
2394  fragmentkind fragment1 fragment2
2395
2396where ``fragmentkind`` is one of ``name``, ``type``, or ``encoding``,
2397indicating whether the following mangled name fragments are
2398<`name <https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangle.name>`_>s,
2399<`type <https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangle.type>`_>s, or
2400<`encoding <https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangle.encoding>`_>s,
2401respectively.
2402Blank lines and lines starting with ``#`` are ignored.
2403
2404For convenience, built-in <substitution>s such as ``St`` and ``Ss``
2405are accepted as <name>s (even though they technically are not <name>s).
2406
2407For example, to specify that ``absl::string_view`` and ``std::string_view``
2408should be treated as equivalent when matching profile data, the following
2409remapping file could be used:
2410
2411.. code-block:: text
2412
2413  # absl::string_view is considered equivalent to std::string_view
2414  type N4absl11string_viewE St17basic_string_viewIcSt11char_traitsIcEE
2415
2416  # std:: might be std::__1:: in libc++ or std::__cxx11:: in libstdc++
2417  name 3std St3__1
2418  name 3std St7__cxx11
2419
2420Matching profile data using a profile remapping file is supported on a
2421best-effort basis. For example, information regarding indirect call targets is
2422currently not remapped. For best results, you are encouraged to generate new
2423profile data matching the updated program, or to remap the profile data
2424using the ``llvm-cxxmap`` and ``llvm-profdata merge`` tools.
2425
2426.. note::
2427
2428  Profile data remapping support is currently only implemented for LLVM's
2429  new pass manager, which can be enabled with
2430  ``-fexperimental-new-pass-manager``.
2431
2432.. note::
2433
2434  Profile data remapping is currently only supported for C++ mangled names
2435  following the Itanium C++ ABI mangling scheme. This covers all C++ targets
2436  supported by Clang other than Windows.
2437
2438GCOV-based Profiling
2439--------------------
2440
2441GCOV is a test coverage program, it helps to know how often a line of code
2442is executed. When instrumenting the code with ``--coverage`` option, some
2443counters are added for each edge linking basic blocks.
2444
2445At compile time, gcno files are generated containing information about
2446blocks and edges between them. At runtime the counters are incremented and at
2447exit the counters are dumped in gcda files.
2448
2449The tool ``llvm-cov gcov`` will parse gcno, gcda and source files to generate
2450a report ``.c.gcov``.
2451
2452.. option:: -fprofile-filter-files=[regexes]
2453
2454  Define a list of regexes separated by a semi-colon.
2455  If a file name matches any of the regexes then the file is instrumented.
2456
2457   .. code-block:: console
2458
2459     $ clang --coverage -fprofile-filter-files=".*\.c$" foo.c
2460
2461  For example, this will only instrument files finishing with ``.c``, skipping ``.h`` files.
2462
2463.. option:: -fprofile-exclude-files=[regexes]
2464
2465  Define a list of regexes separated by a semi-colon.
2466  If a file name doesn't match all the regexes then the file is instrumented.
2467
2468  .. code-block:: console
2469
2470     $ clang --coverage -fprofile-exclude-files="^/usr/include/.*$" foo.c
2471
2472  For example, this will instrument all the files except the ones in ``/usr/include``.
2473
2474If both options are used then a file is instrumented if its name matches any
2475of the regexes from ``-fprofile-filter-list`` and doesn't match all the regexes
2476from ``-fprofile-exclude-list``.
2477
2478.. code-block:: console
2479
2480   $ clang --coverage -fprofile-exclude-files="^/usr/include/.*$" \
2481           -fprofile-filter-files="^/usr/.*$"
2482
2483In that case ``/usr/foo/oof.h`` is instrumented since it matches the filter regex and
2484doesn't match the exclude regex, but ``/usr/include/foo.h`` doesn't since it matches
2485the exclude regex.
2486
2487Controlling Debug Information
2488-----------------------------
2489
2490Controlling Size of Debug Information
2491^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2492
2493Debug info kind generated by Clang can be set by one of the flags listed
2494below. If multiple flags are present, the last one is used.
2495
2496.. option:: -g0
2497
2498  Don't generate any debug info (default).
2499
2500.. option:: -gline-tables-only
2501
2502  Generate line number tables only.
2503
2504  This kind of debug info allows to obtain stack traces with function names,
2505  file names and line numbers (by such tools as ``gdb`` or ``addr2line``).  It
2506  doesn't contain any other data (e.g. description of local variables or
2507  function parameters).
2508
2509.. option:: -fstandalone-debug
2510
2511  Clang supports a number of optimizations to reduce the size of debug
2512  information in the binary. They work based on the assumption that
2513  the debug type information can be spread out over multiple
2514  compilation units.  For instance, Clang will not emit type
2515  definitions for types that are not needed by a module and could be
2516  replaced with a forward declaration.  Further, Clang will only emit
2517  type info for a dynamic C++ class in the module that contains the
2518  vtable for the class.
2519
2520  The **-fstandalone-debug** option turns off these optimizations.
2521  This is useful when working with 3rd-party libraries that don't come
2522  with debug information.  Note that Clang will never emit type
2523  information for types that are not referenced at all by the program.
2524
2525.. option:: -fno-standalone-debug
2526
2527   On Darwin **-fstandalone-debug** is enabled by default. The
2528   **-fno-standalone-debug** option can be used to get to turn on the
2529   vtable-based optimization described above.
2530
2531.. option:: -fuse-ctor-homing
2532
2533   This optimization is similar to the optimizations that are enabled as part
2534   of -fno-standalone-debug. Here, Clang only emits type info for a
2535   non-trivial, non-aggregate C++ class in the modules that contain a
2536   definition of one of its constructors. This relies on the additional
2537   assumption that all classes that are not trivially constructible have a
2538   non-trivial constructor that is used somewhere.
2539
2540   This flag is not enabled by default, and needs to be used with -cc1 or
2541   -Xclang.
2542
2543.. option:: -g
2544
2545  Generate complete debug info.
2546
2547.. option:: -feliminate-unused-debug-types
2548
2549  By default, Clang does not emit type information for types that are defined
2550  but not used in a program. To retain the debug info for these unused types,
2551  the negation **-fno-eliminate-unused-debug-types** can be used.
2552
2553Controlling Macro Debug Info Generation
2554^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2555
2556Debug info for C preprocessor macros increases the size of debug information in
2557the binary. Macro debug info generated by Clang can be controlled by the flags
2558listed below.
2559
2560.. option:: -fdebug-macro
2561
2562  Generate debug info for preprocessor macros. This flag is discarded when
2563  **-g0** is enabled.
2564
2565.. option:: -fno-debug-macro
2566
2567  Do not generate debug info for preprocessor macros (default).
2568
2569Controlling Debugger "Tuning"
2570^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2571
2572While Clang generally emits standard DWARF debug info (http://dwarfstd.org),
2573different debuggers may know how to take advantage of different specific DWARF
2574features. You can "tune" the debug info for one of several different debuggers.
2575
2576.. option:: -ggdb, -glldb, -gsce, -gdbx
2577
2578  Tune the debug info for the ``gdb``, ``lldb``, Sony PlayStation\ |reg|
2579  debugger, or ``dbx``, respectively. Each of these options implies **-g**.
2580  (Therefore, if you want both **-gline-tables-only** and debugger tuning, the
2581  tuning option must come first.)
2582
2583Controlling LLVM IR Output
2584--------------------------
2585
2586Controlling Value Names in LLVM IR
2587^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2588
2589Emitting value names in LLVM IR increases the size and verbosity of the IR.
2590By default, value names are only emitted in assertion-enabled builds of Clang.
2591However, when reading IR it can be useful to re-enable the emission of value
2592names to improve readability.
2593
2594.. option:: -fdiscard-value-names
2595
2596  Discard value names when generating LLVM IR.
2597
2598.. option:: -fno-discard-value-names
2599
2600  Do not discard value names when generating LLVM IR. This option can be used
2601  to re-enable names for release builds of Clang.
2602
2603
2604Comment Parsing Options
2605-----------------------
2606
2607Clang parses Doxygen and non-Doxygen style documentation comments and attaches
2608them to the appropriate declaration nodes.  By default, it only parses
2609Doxygen-style comments and ignores ordinary comments starting with ``//`` and
2610``/*``.
2611
2612.. option:: -Wdocumentation
2613
2614  Emit warnings about use of documentation comments.  This warning group is off
2615  by default.
2616
2617  This includes checking that ``\param`` commands name parameters that actually
2618  present in the function signature, checking that ``\returns`` is used only on
2619  functions that actually return a value etc.
2620
2621.. option:: -Wno-documentation-unknown-command
2622
2623  Don't warn when encountering an unknown Doxygen command.
2624
2625.. option:: -fparse-all-comments
2626
2627  Parse all comments as documentation comments (including ordinary comments
2628  starting with ``//`` and ``/*``).
2629
2630.. option:: -fcomment-block-commands=[commands]
2631
2632  Define custom documentation commands as block commands.  This allows Clang to
2633  construct the correct AST for these custom commands, and silences warnings
2634  about unknown commands.  Several commands must be separated by a comma
2635  *without trailing space*; e.g. ``-fcomment-block-commands=foo,bar`` defines
2636  custom commands ``\foo`` and ``\bar``.
2637
2638  It is also possible to use ``-fcomment-block-commands`` several times; e.g.
2639  ``-fcomment-block-commands=foo -fcomment-block-commands=bar`` does the same
2640  as above.
2641
2642.. _c:
2643
2644C Language Features
2645===================
2646
2647The support for standard C in clang is feature-complete except for the
2648C99 floating-point pragmas.
2649
2650Extensions supported by clang
2651-----------------------------
2652
2653See :doc:`LanguageExtensions`.
2654
2655Differences between various standard modes
2656------------------------------------------
2657
2658clang supports the -std option, which changes what language mode clang uses.
2659The supported modes for C are c89, gnu89, c94, c99, gnu99, c11, gnu11, c17,
2660gnu17, c2x, gnu2x, and various aliases for those modes. If no -std option is
2661specified, clang defaults to gnu17 mode. Many C99 and C11 features are
2662supported in earlier modes as a conforming extension, with a warning. Use
2663``-pedantic-errors`` to request an error if a feature from a later standard
2664revision is used in an earlier mode.
2665
2666Differences between all ``c*`` and ``gnu*`` modes:
2667
2668-  ``c*`` modes define "``__STRICT_ANSI__``".
2669-  Target-specific defines not prefixed by underscores, like ``linux``,
2670   are defined in ``gnu*`` modes.
2671-  Trigraphs default to being off in ``gnu*`` modes; they can be enabled
2672   by the ``-trigraphs`` option.
2673-  The parser recognizes ``asm`` and ``typeof`` as keywords in ``gnu*`` modes;
2674   the variants ``__asm__`` and ``__typeof__`` are recognized in all modes.
2675-  The parser recognizes ``inline`` as a keyword in ``gnu*`` mode, in
2676   addition to recognizing it in the ``*99`` and later modes for which it is
2677   part of the ISO C standard. The variant ``__inline__`` is recognized in all
2678   modes.
2679-  The Apple "blocks" extension is recognized by default in ``gnu*`` modes
2680   on some platforms; it can be enabled in any mode with the ``-fblocks``
2681   option.
2682
2683Differences between ``*89`` and ``*94`` modes:
2684
2685-  Digraphs are not recognized in c89 mode.
2686
2687Differences between ``*94`` and ``*99`` modes:
2688
2689-  The ``*99`` modes default to implementing ``inline`` / ``__inline__``
2690   as specified in C99, while the ``*89`` modes implement the GNU version.
2691   This can be overridden for individual functions with the ``__gnu_inline__``
2692   attribute.
2693-  The scope of names defined inside a ``for``, ``if``, ``switch``, ``while``,
2694   or ``do`` statement is different. (example: ``if ((struct x {int x;}*)0) {}``.)
2695-  ``__STDC_VERSION__`` is not defined in ``*89`` modes.
2696-  ``inline`` is not recognized as a keyword in ``c89`` mode.
2697-  ``restrict`` is not recognized as a keyword in ``*89`` modes.
2698-  Commas are allowed in integer constant expressions in ``*99`` modes.
2699-  Arrays which are not lvalues are not implicitly promoted to pointers
2700   in ``*89`` modes.
2701-  Some warnings are different.
2702
2703Differences between ``*99`` and ``*11`` modes:
2704
2705-  Warnings for use of C11 features are disabled.
2706-  ``__STDC_VERSION__`` is defined to ``201112L`` rather than ``199901L``.
2707
2708Differences between ``*11`` and ``*17`` modes:
2709
2710-  ``__STDC_VERSION__`` is defined to ``201710L`` rather than ``201112L``.
2711
2712GCC extensions not implemented yet
2713----------------------------------
2714
2715clang tries to be compatible with gcc as much as possible, but some gcc
2716extensions are not implemented yet:
2717
2718-  clang does not support decimal floating point types (``_Decimal32`` and
2719   friends) yet.
2720-  clang does not support nested functions; this is a complex feature
2721   which is infrequently used, so it is unlikely to be implemented
2722   anytime soon. In C++11 it can be emulated by assigning lambda
2723   functions to local variables, e.g:
2724
2725   .. code-block:: cpp
2726
2727     auto const local_function = [&](int parameter) {
2728       // Do something
2729     };
2730     ...
2731     local_function(1);
2732
2733-  clang only supports global register variables when the register specified
2734   is non-allocatable (e.g. the stack pointer). Support for general global
2735   register variables is unlikely to be implemented soon because it requires
2736   additional LLVM backend support.
2737-  clang does not support static initialization of flexible array
2738   members. This appears to be a rarely used extension, but could be
2739   implemented pending user demand.
2740-  clang does not support
2741   ``__builtin_va_arg_pack``/``__builtin_va_arg_pack_len``. This is
2742   used rarely, but in some potentially interesting places, like the
2743   glibc headers, so it may be implemented pending user demand. Note
2744   that because clang pretends to be like GCC 4.2, and this extension
2745   was introduced in 4.3, the glibc headers will not try to use this
2746   extension with clang at the moment.
2747-  clang does not support the gcc extension for forward-declaring
2748   function parameters; this has not shown up in any real-world code
2749   yet, though, so it might never be implemented.
2750
2751This is not a complete list; if you find an unsupported extension
2752missing from this list, please send an e-mail to cfe-dev. This list
2753currently excludes C++; see :ref:`C++ Language Features <cxx>`. Also, this
2754list does not include bugs in mostly-implemented features; please see
2755the `bug
2756tracker <https://bugs.llvm.org/buglist.cgi?quicksearch=product%3Aclang+component%3A-New%2BBugs%2CAST%2CBasic%2CDriver%2CHeaders%2CLLVM%2BCodeGen%2Cparser%2Cpreprocessor%2CSemantic%2BAnalyzer>`_
2757for known existing bugs (FIXME: Is there a section for bug-reporting
2758guidelines somewhere?).
2759
2760Intentionally unsupported GCC extensions
2761----------------------------------------
2762
2763-  clang does not support the gcc extension that allows variable-length
2764   arrays in structures. This is for a few reasons: one, it is tricky to
2765   implement, two, the extension is completely undocumented, and three,
2766   the extension appears to be rarely used. Note that clang *does*
2767   support flexible array members (arrays with a zero or unspecified
2768   size at the end of a structure).
2769-  GCC accepts many expression forms that are not valid integer constant
2770   expressions in bit-field widths, enumerator constants, case labels,
2771   and in array bounds at global scope. Clang also accepts additional
2772   expression forms in these contexts, but constructs that GCC accepts due to
2773   simplifications GCC performs while parsing, such as ``x - x`` (where ``x`` is a
2774   variable) will likely never be accepted by Clang.
2775-  clang does not support ``__builtin_apply`` and friends; this extension
2776   is extremely obscure and difficult to implement reliably.
2777
2778.. _c_ms:
2779
2780Microsoft extensions
2781--------------------
2782
2783clang has support for many extensions from Microsoft Visual C++. To enable these
2784extensions, use the ``-fms-extensions`` command-line option. This is the default
2785for Windows targets. Clang does not implement every pragma or declspec provided
2786by MSVC, but the popular ones, such as ``__declspec(dllexport)`` and ``#pragma
2787comment(lib)`` are well supported.
2788
2789clang has a ``-fms-compatibility`` flag that makes clang accept enough
2790invalid C++ to be able to parse most Microsoft headers. For example, it
2791allows `unqualified lookup of dependent base class members
2792<https://clang.llvm.org/compatibility.html#dep_lookup_bases>`_, which is
2793a common compatibility issue with clang. This flag is enabled by default
2794for Windows targets.
2795
2796``-fdelayed-template-parsing`` lets clang delay parsing of function template
2797definitions until the end of a translation unit. This flag is enabled by
2798default for Windows targets.
2799
2800For compatibility with existing code that compiles with MSVC, clang defines the
2801``_MSC_VER`` and ``_MSC_FULL_VER`` macros. These default to the values of 1800
2802and 180000000 respectively, making clang look like an early release of Visual
2803C++ 2013. The ``-fms-compatibility-version=`` flag overrides these values.  It
2804accepts a dotted version tuple, such as 19.00.23506. Changing the MSVC
2805compatibility version makes clang behave more like that version of MSVC. For
2806example, ``-fms-compatibility-version=19`` will enable C++14 features and define
2807``char16_t`` and ``char32_t`` as builtin types.
2808
2809.. _cxx:
2810
2811C++ Language Features
2812=====================
2813
2814clang fully implements all of standard C++98 except for exported
2815templates (which were removed in C++11), all of standard C++11,
2816C++14, and C++17, and most of C++20.
2817
2818See the `C++ support in Clang <https://clang.llvm.org/cxx_status.html>` page
2819for detailed information on C++ feature support across Clang versions.
2820
2821Controlling implementation limits
2822---------------------------------
2823
2824.. option:: -fbracket-depth=N
2825
2826  Sets the limit for nested parentheses, brackets, and braces to N.  The
2827  default is 256.
2828
2829.. option:: -fconstexpr-depth=N
2830
2831  Sets the limit for recursive constexpr function invocations to N.  The
2832  default is 512.
2833
2834.. option:: -fconstexpr-steps=N
2835
2836  Sets the limit for the number of full-expressions evaluated in a single
2837  constant expression evaluation.  The default is 1048576.
2838
2839.. option:: -ftemplate-depth=N
2840
2841  Sets the limit for recursively nested template instantiations to N.  The
2842  default is 1024.
2843
2844.. option:: -foperator-arrow-depth=N
2845
2846  Sets the limit for iterative calls to 'operator->' functions to N.  The
2847  default is 256.
2848
2849.. _objc:
2850
2851Objective-C Language Features
2852=============================
2853
2854.. _objcxx:
2855
2856Objective-C++ Language Features
2857===============================
2858
2859.. _openmp:
2860
2861OpenMP Features
2862===============
2863
2864Clang supports all OpenMP 4.5 directives and clauses. See :doc:`OpenMPSupport`
2865for additional details.
2866
2867Use `-fopenmp` to enable OpenMP. Support for OpenMP can be disabled with
2868`-fno-openmp`.
2869
2870Use `-fopenmp-simd` to enable OpenMP simd features only, without linking
2871the runtime library; for combined constructs
2872(e.g. ``#pragma omp parallel for simd``) the non-simd directives and clauses
2873will be ignored. This can be disabled with `-fno-openmp-simd`.
2874
2875Controlling implementation limits
2876---------------------------------
2877
2878.. option:: -fopenmp-use-tls
2879
2880 Controls code generation for OpenMP threadprivate variables. In presence of
2881 this option all threadprivate variables are generated the same way as thread
2882 local variables, using TLS support. If `-fno-openmp-use-tls`
2883 is provided or target does not support TLS, code generation for threadprivate
2884 variables relies on OpenMP runtime library.
2885
2886.. _opencl:
2887
2888OpenCL Features
2889===============
2890
2891Clang can be used to compile OpenCL kernels for execution on a device
2892(e.g. GPU). It is possible to compile the kernel into a binary (e.g. for AMDGPU)
2893that can be uploaded to run directly on a device (e.g. using
2894`clCreateProgramWithBinary
2895<https://www.khronos.org/registry/OpenCL/specs/opencl-1.1.pdf#111>`_) or
2896into generic bitcode files loadable into other toolchains.
2897
2898Compiling to a binary using the default target from the installation can be done
2899as follows:
2900
2901   .. code-block:: console
2902
2903     $ echo "kernel void k(){}" > test.cl
2904     $ clang test.cl
2905
2906Compiling for a specific target can be done by specifying the triple corresponding
2907to the target, for example:
2908
2909   .. code-block:: console
2910
2911     $ clang -target nvptx64-unknown-unknown test.cl
2912     $ clang -target amdgcn-amd-amdhsa -mcpu=gfx900 test.cl
2913
2914Compiling to bitcode can be done as follows:
2915
2916   .. code-block:: console
2917
2918     $ clang -c -emit-llvm test.cl
2919
2920This will produce a file `test.bc` that can be used in vendor toolchains
2921to perform machine code generation.
2922
2923Note that if compiled to bitcode for generic targets such as SPIR,
2924portable IR is produced that can be used with various vendor
2925tools as well as open source tools such as `SPIRV-LLVM Translator
2926<https://github.com/KhronosGroup/SPIRV-LLVM-Translator>`_
2927to produce SPIR-V binary. More details are provided in `the offline
2928compilation from OpenCL kernel sources into SPIR-V using open source
2929tools
2930<https://github.com/KhronosGroup/OpenCL-Guide/blob/main/chapters/os_tooling.md>`_.
2931
2932Clang currently supports OpenCL C language standards up to v2.0. Clang mainly
2933supports full profile. There is only very limited support of the embedded
2934profile.
2935Starting from clang 9 a C++ mode is available for OpenCL (see
2936:ref:`C++ for OpenCL <cxx_for_opencl>`).
2937
2938There is ongoing support for OpenCL v3.0 that is documented along with other
2939experimental functionality and features in development on :doc:`OpenCLSupport`
2940page.
2941
2942OpenCL Specific Options
2943-----------------------
2944
2945Most of the OpenCL build options from `the specification v2.0 section 5.8.4
2946<https://www.khronos.org/registry/cl/specs/opencl-2.0.pdf#200>`_ are available.
2947
2948Examples:
2949
2950   .. code-block:: console
2951
2952     $ clang -cl-std=CL2.0 -cl-single-precision-constant test.cl
2953
2954
2955Many flags used for the compilation for C sources can also be passed while
2956compiling for OpenCL, examples: ``-c``, ``-O<1-4|s>``, ``-o``, ``-emit-llvm``, etc.
2957
2958Some extra options are available to support special OpenCL features.
2959
2960.. _opencl_cl_no_stdinc:
2961
2962.. option:: -cl-no-stdinc
2963
2964Allows to disable all extra types and functions that are not native to the compiler.
2965This might reduce the compilation speed marginally but many declarations from the
2966OpenCL standard will not be accessible. For example, the following will fail to
2967compile.
2968
2969   .. code-block:: console
2970
2971     $ echo "bool is_wg_uniform(int i){return get_enqueued_local_size(i)==get_local_size(i);}" > test.cl
2972     $ clang -cl-std=CL2.0 -cl-no-stdinc test.cl
2973     error: use of undeclared identifier 'get_enqueued_local_size'
2974     error: use of undeclared identifier 'get_local_size'
2975
2976More information about the standard types and functions is provided in :ref:`the
2977section on the OpenCL Header <opencl_header>`.
2978
2979OpenCL Targets
2980--------------
2981
2982OpenCL targets are derived from the regular Clang target classes. The OpenCL
2983specific parts of the target representation provide address space mapping as
2984well as a set of supported extensions.
2985
2986Specific Targets
2987^^^^^^^^^^^^^^^^
2988
2989There is a set of concrete HW architectures that OpenCL can be compiled for.
2990
2991- For AMD target:
2992
2993   .. code-block:: console
2994
2995     $ clang -target amdgcn-amd-amdhsa -mcpu=gfx900 test.cl
2996
2997- For Nvidia architectures:
2998
2999   .. code-block:: console
3000
3001     $ clang -target nvptx64-unknown-unknown test.cl
3002
3003
3004Generic Targets
3005^^^^^^^^^^^^^^^
3006
3007- SPIR is available as a generic target to allow portable bitcode to be produced
3008  that can be used across GPU toolchains. The implementation follows `the SPIR
3009  specification <https://www.khronos.org/spir>`_. There are two flavors
3010  available for 32 and 64 bits.
3011
3012   .. code-block:: console
3013
3014    $ clang -target spir test.cl -emit-llvm -c
3015    $ clang -target spir64 test.cl -emit-llvm -c
3016
3017  All known OpenCL extensions are supported in the SPIR targets. Clang will
3018  generate SPIR v1.2 compatible IR for OpenCL versions up to 2.0 and SPIR v2.0
3019  for OpenCL v2.0 or C++ for OpenCL.
3020
3021- x86 is used by some implementations that are x86 compatible and currently
3022  remains for backwards compatibility (with older implementations prior to
3023  SPIR target support). For "non-SPMD" targets which cannot spawn multiple
3024  work-items on the fly using hardware, which covers practically all non-GPU
3025  devices such as CPUs and DSPs, additional processing is needed for the kernels
3026  to support multiple work-item execution. For this, a 3rd party toolchain,
3027  such as for example `POCL <http://portablecl.org/>`_, can be used.
3028
3029  This target does not support multiple memory segments and, therefore, the fake
3030  address space map can be added using the :ref:`-ffake-address-space-map
3031  <opencl_fake_address_space_map>` flag.
3032
3033.. _opencl_header:
3034
3035OpenCL Header
3036-------------
3037
3038By default Clang will include standard headers and therefore most of OpenCL
3039builtin functions and types are available during compilation. The
3040default declarations of non-native compiler types and functions can be disabled
3041by using flag :ref:`-cl-no-stdinc <opencl_cl_no_stdinc>`.
3042
3043The following example demonstrates that OpenCL kernel sources with various
3044standard builtin functions can be compiled without the need for an explicit
3045includes or compiler flags.
3046
3047   .. code-block:: console
3048
3049     $ echo "bool is_wg_uniform(int i){return get_enqueued_local_size(i)==get_local_size(i);}" > test.cl
3050     $ clang -cl-std=CL2.0 test.cl
3051
3052More information about the default headers is provided in :doc:`OpenCLSupport`.
3053
3054OpenCL Extensions
3055-----------------
3056
3057Most of the ``cl_khr_*`` extensions to OpenCL C from `the official OpenCL
3058registry <https://www.khronos.org/registry/OpenCL/>`_ are available and
3059configured per target depending on the support available in the specific
3060architecture.
3061
3062It is possible to alter the default extensions setting per target using
3063``-cl-ext`` flag. (See :ref:`flags description <opencl_cl_ext>` for more details).
3064
3065Vendor extensions can be added flexibly by declaring the list of types and
3066functions associated with each extensions enclosed within the following
3067compiler pragma directives:
3068
3069  .. code-block:: c
3070
3071       #pragma OPENCL EXTENSION the_new_extension_name : begin
3072       // declare types and functions associated with the extension here
3073       #pragma OPENCL EXTENSION the_new_extension_name : end
3074
3075For example, parsing the following code adds ``my_t`` type and ``my_func``
3076function to the custom ``my_ext`` extension.
3077
3078  .. code-block:: c
3079
3080       #pragma OPENCL EXTENSION my_ext : begin
3081       typedef struct{
3082         int a;
3083       }my_t;
3084       void my_func(my_t);
3085       #pragma OPENCL EXTENSION my_ext : end
3086
3087There is no conflict resolution for identifier clashes among extensions.
3088It is therefore recommended that the identifiers are prefixed with a
3089double underscore to avoid clashing with user space identifiers. Vendor
3090extension should use reserved identifier prefix e.g. amd, arm, intel.
3091
3092Clang also supports language extensions documented in `The OpenCL C Language
3093Extensions Documentation
3094<https://github.com/KhronosGroup/Khronosdotorg/blob/master/api/opencl/assets/OpenCL_LangExt.pdf>`_.
3095
3096OpenCL-Specific Attributes
3097--------------------------
3098
3099OpenCL support in Clang contains a set of attribute taken directly from the
3100specification as well as additional attributes.
3101
3102See also :doc:`AttributeReference`.
3103
3104nosvm
3105^^^^^
3106
3107Clang supports this attribute to comply to OpenCL v2.0 conformance, but it
3108does not have any effect on the IR. For more details reffer to the specification
3109`section 6.7.2
3110<https://www.khronos.org/registry/cl/specs/opencl-2.0-openclc.pdf#49>`_
3111
3112
3113opencl_unroll_hint
3114^^^^^^^^^^^^^^^^^^
3115
3116The implementation of this feature mirrors the unroll hint for C.
3117More details on the syntax can be found in the specification
3118`section 6.11.5
3119<https://www.khronos.org/registry/cl/specs/opencl-2.0-openclc.pdf#61>`_
3120
3121convergent
3122^^^^^^^^^^
3123
3124To make sure no invalid optimizations occur for single program multiple data
3125(SPMD) / single instruction multiple thread (SIMT) Clang provides attributes that
3126can be used for special functions that have cross work item semantics.
3127An example is the subgroup operations such as `intel_sub_group_shuffle
3128<https://www.khronos.org/registry/cl/extensions/intel/cl_intel_subgroups.txt>`_
3129
3130   .. code-block:: c
3131
3132     // Define custom my_sub_group_shuffle(data, c)
3133     // that makes use of intel_sub_group_shuffle
3134     r1 = ...
3135     if (r0) r1 = computeA();
3136     // Shuffle data from r1 into r3
3137     // of threads id r2.
3138     r3 = my_sub_group_shuffle(r1, r2);
3139     if (r0) r3 = computeB();
3140
3141with non-SPMD semantics this is optimized to the following equivalent code:
3142
3143   .. code-block:: c
3144
3145     r1 = ...
3146     if (!r0)
3147       // Incorrect functionality! The data in r1
3148       // have not been computed by all threads yet.
3149       r3 = my_sub_group_shuffle(r1, r2);
3150     else {
3151       r1 = computeA();
3152       r3 = my_sub_group_shuffle(r1, r2);
3153       r3 = computeB();
3154     }
3155
3156Declaring the function ``my_sub_group_shuffle`` with the convergent attribute
3157would prevent this:
3158
3159   .. code-block:: c
3160
3161     my_sub_group_shuffle() __attribute__((convergent));
3162
3163Using ``convergent`` guarantees correct execution by keeping CFG equivalence
3164wrt operations marked as ``convergent``. CFG ``G´`` is equivalent to ``G`` wrt
3165node ``Ni`` : ``iff ∀ Nj (i≠j)`` domination and post-domination relations with
3166respect to ``Ni`` remain the same in both ``G`` and ``G´``.
3167
3168noduplicate
3169^^^^^^^^^^^
3170
3171``noduplicate`` is more restrictive with respect to optimizations than
3172``convergent`` because a convergent function only preserves CFG equivalence.
3173This allows some optimizations to happen as long as the control flow remains
3174unmodified.
3175
3176   .. code-block:: c
3177
3178     for (int i=0; i<4; i++)
3179       my_sub_group_shuffle()
3180
3181can be modified to:
3182
3183   .. code-block:: c
3184
3185     my_sub_group_shuffle();
3186     my_sub_group_shuffle();
3187     my_sub_group_shuffle();
3188     my_sub_group_shuffle();
3189
3190while using ``noduplicate`` would disallow this. Also ``noduplicate`` doesn't
3191have the same safe semantics of CFG as ``convergent`` and can cause changes in
3192CFG that modify semantics of the original program.
3193
3194``noduplicate`` is kept for backwards compatibility only and it considered to be
3195deprecated for future uses.
3196
3197.. _cxx_for_opencl:
3198
3199C++ for OpenCL
3200--------------
3201
3202Starting from clang 9 kernel code can contain C++17 features: classes, templates,
3203function overloading, type deduction, etc. Please note that this is not an
3204implementation of `OpenCL C++
3205<https://www.khronos.org/registry/OpenCL/specs/2.2/pdf/OpenCL_Cxx.pdf>`_ and
3206there is no plan to support it in clang in any new releases in the near future.
3207
3208
3209Clang currently supports C++ for OpenCL v1.0.
3210For detailed information about this language refer to the C++ for OpenCL
3211Programming Language Documentation available
3212in `the latest build
3213<https://www.khronos.org/opencl/assets/CXX_for_OpenCL.html>`_
3214or in `the official release
3215<https://github.com/KhronosGroup/OpenCL-Docs/releases/tag/cxxforopencl-v1.0-r2>`_.
3216
3217To enable the C++ for OpenCL mode, pass one of following command line options when
3218compiling ``.cl`` file ``-cl-std=clc++``, ``-cl-std=CLC++``, ``-std=clc++`` or
3219``-std=CLC++``.
3220
3221   .. code-block:: c++
3222
3223     template<class T> T add( T x, T y )
3224     {
3225       return x + y;
3226     }
3227
3228     __kernel void test( __global float* a, __global float* b)
3229     {
3230       auto index = get_global_id(0);
3231       a[index] = add(b[index], b[index+1]);
3232     }
3233
3234
3235   .. code-block:: console
3236
3237     clang -cl-std=clc++ test.cl
3238
3239Alternatively, files with ``.clcpp`` extension are compiled with the C++ for OpenCL
3240mode.
3241
3242   .. code-block:: console
3243
3244     clang test.clcpp
3245
3246C++ for OpenCL kernel sources can also be compiled online in drivers supporting
3247`cl_ext_cxx_for_opencl
3248<https://www.khronos.org/registry/OpenCL/extensions/ext/cl_ext_cxx_for_opencl.html>`_
3249extension.
3250
3251Constructing and destroying global objects
3252^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3253
3254Global objects with non-trivial constructors require the constructors to be run
3255before the first kernel using the global objects is executed. Similarly global
3256objects with non-trivial destructors require destructor invocation just after
3257the last kernel using the program objects is executed.
3258In OpenCL versions earlier than v2.2 there is no support for invoking global
3259constructors. However, an easy workaround is to manually enqueue the
3260constructor initialization kernel that has the following name scheme
3261``_GLOBAL__sub_I_<compiled file name>``.
3262This kernel is only present if there are global objects with non-trivial
3263constructors present in the compiled binary. One way to check this is by
3264passing ``CL_PROGRAM_KERNEL_NAMES`` to ``clGetProgramInfo`` (OpenCL v2.0
3265s5.8.7) and then checking whether any kernel name matches the naming scheme of
3266global constructor initialization kernel above.
3267
3268Note that if multiple files are compiled and linked into libraries, multiple
3269kernels that initialize global objects for multiple modules would have to be
3270invoked.
3271
3272Applications are currently required to run initialization of global objects
3273manually before running any kernels in which the objects are used.
3274
3275   .. code-block:: console
3276
3277     clang -cl-std=clc++ test.cl
3278
3279If there are any global objects to be initialized, the final binary will
3280contain the ``_GLOBAL__sub_I_test.cl`` kernel to be enqueued.
3281
3282Note that the manual workaround only applies to objects declared at the
3283program scope. There is no manual workaround for the construction of static
3284objects with non-trivial constructors inside functions.
3285
3286Global destructors can not be invoked manually in the OpenCL v2.0 drivers.
3287However, all memory used for program scope objects should be released on
3288``clReleaseProgram``.
3289
3290Libraries
3291^^^^^^^^^
3292Limited experimental support of C++ standard libraries for OpenCL is
3293described in :doc:`OpenCLSupport` page.
3294
3295.. _target_features:
3296
3297Target-Specific Features and Limitations
3298========================================
3299
3300CPU Architectures Features and Limitations
3301------------------------------------------
3302
3303X86
3304^^^
3305
3306The support for X86 (both 32-bit and 64-bit) is considered stable on
3307Darwin (macOS), Linux, FreeBSD, and Dragonfly BSD: it has been tested
3308to correctly compile many large C, C++, Objective-C, and Objective-C++
3309codebases.
3310
3311On ``x86_64-mingw32``, passing i128(by value) is incompatible with the
3312Microsoft x64 calling convention. You might need to tweak
3313``WinX86_64ABIInfo::classify()`` in lib/CodeGen/TargetInfo.cpp.
3314
3315For the X86 target, clang supports the `-m16` command line
3316argument which enables 16-bit code output. This is broadly similar to
3317using ``asm(".code16gcc")`` with the GNU toolchain. The generated code
3318and the ABI remains 32-bit but the assembler emits instructions
3319appropriate for a CPU running in 16-bit mode, with address-size and
3320operand-size prefixes to enable 32-bit addressing and operations.
3321
3322Several micro-architecture levels as specified by the x86-64 psABI are defined.
3323They are cumulative in the sense that features from previous levels are
3324implicitly included in later levels.
3325
3326- ``-march=x86-64``: CMOV, CMPXCHG8B, FPU, FXSR, MMX, FXSR, SCE, SSE, SSE2
3327- ``-march=x86-64-v2``: (close to Nehalem) CMPXCHG16B, LAHF-SAHF, POPCNT, SSE3, SSE4.1, SSE4.2, SSSE3
3328- ``-march=x86-64-v3``: (close to Haswell) AVX, AVX2, BMI1, BMI2, F16C, FMA, LZCNT, MOVBE, XSAVE
3329- ``-march=x86-64-v4``: AVX512F, AVX512BW, AVX512CD, AVX512DQ, AVX512VL
3330
3331ARM
3332^^^
3333
3334The support for ARM (specifically ARMv6 and ARMv7) is considered stable
3335on Darwin (iOS): it has been tested to correctly compile many large C,
3336C++, Objective-C, and Objective-C++ codebases. Clang only supports a
3337limited number of ARM architectures. It does not yet fully support
3338ARMv5, for example.
3339
3340PowerPC
3341^^^^^^^
3342
3343The support for PowerPC (especially PowerPC64) is considered stable
3344on Linux and FreeBSD: it has been tested to correctly compile many
3345large C and C++ codebases. PowerPC (32bit) is still missing certain
3346features (e.g. PIC code on ELF platforms).
3347
3348Other platforms
3349^^^^^^^^^^^^^^^
3350
3351clang currently contains some support for other architectures (e.g. Sparc);
3352however, significant pieces of code generation are still missing, and they
3353haven't undergone significant testing.
3354
3355clang contains limited support for the MSP430 embedded processor, but
3356both the clang support and the LLVM backend support are highly
3357experimental.
3358
3359Other platforms are completely unsupported at the moment. Adding the
3360minimal support needed for parsing and semantic analysis on a new
3361platform is quite easy; see ``lib/Basic/Targets.cpp`` in the clang source
3362tree. This level of support is also sufficient for conversion to LLVM IR
3363for simple programs. Proper support for conversion to LLVM IR requires
3364adding code to ``lib/CodeGen/CGCall.cpp`` at the moment; this is likely to
3365change soon, though. Generating assembly requires a suitable LLVM
3366backend.
3367
3368Operating System Features and Limitations
3369-----------------------------------------
3370
3371Windows
3372^^^^^^^
3373
3374Clang has experimental support for targeting "Cygming" (Cygwin / MinGW)
3375platforms.
3376
3377See also :ref:`Microsoft Extensions <c_ms>`.
3378
3379Cygwin
3380""""""
3381
3382Clang works on Cygwin-1.7.
3383
3384MinGW32
3385"""""""
3386
3387Clang works on some mingw32 distributions. Clang assumes directories as
3388below;
3389
3390-  ``C:/mingw/include``
3391-  ``C:/mingw/lib``
3392-  ``C:/mingw/lib/gcc/mingw32/4.[3-5].0/include/c++``
3393
3394On MSYS, a few tests might fail.
3395
3396MinGW-w64
3397"""""""""
3398
3399For 32-bit (i686-w64-mingw32), and 64-bit (x86\_64-w64-mingw32), Clang
3400assumes as below;
3401
3402-  ``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)``
3403-  ``some_directory/bin/gcc.exe``
3404-  ``some_directory/bin/clang.exe``
3405-  ``some_directory/bin/clang++.exe``
3406-  ``some_directory/bin/../include/c++/GCC_version``
3407-  ``some_directory/bin/../include/c++/GCC_version/x86_64-w64-mingw32``
3408-  ``some_directory/bin/../include/c++/GCC_version/i686-w64-mingw32``
3409-  ``some_directory/bin/../include/c++/GCC_version/backward``
3410-  ``some_directory/bin/../x86_64-w64-mingw32/include``
3411-  ``some_directory/bin/../i686-w64-mingw32/include``
3412-  ``some_directory/bin/../include``
3413
3414This directory layout is standard for any toolchain you will find on the
3415official `MinGW-w64 website <http://mingw-w64.sourceforge.net>`_.
3416
3417Clang expects the GCC executable "gcc.exe" compiled for
3418``i686-w64-mingw32`` (or ``x86_64-w64-mingw32``) to be present on PATH.
3419
3420`Some tests might fail <https://bugs.llvm.org/show_bug.cgi?id=9072>`_ on
3421``x86_64-w64-mingw32``.
3422
3423.. _clang-cl:
3424
3425clang-cl
3426========
3427
3428clang-cl is an alternative command-line interface to Clang, designed for
3429compatibility with the Visual C++ compiler, cl.exe.
3430
3431To enable clang-cl to find system headers, libraries, and the linker when run
3432from the command-line, it should be executed inside a Visual Studio Native Tools
3433Command Prompt or a regular Command Prompt where the environment has been set
3434up using e.g. `vcvarsall.bat <https://msdn.microsoft.com/en-us/library/f2ccy3wt.aspx>`_.
3435
3436clang-cl can also be used from inside Visual Studio by selecting the LLVM
3437Platform Toolset. The toolset is not part of the installer, but may be installed
3438separately from the
3439`Visual Studio Marketplace <https://marketplace.visualstudio.com/items?itemName=LLVMExtensions.llvm-toolchain>`_.
3440To use the toolset, select a project in Solution Explorer, open its Property
3441Page (Alt+F7), and in the "General" section of "Configuration Properties"
3442change "Platform Toolset" to LLVM.  Doing so enables an additional Property
3443Page for selecting the clang-cl executable to use for builds.
3444
3445To use the toolset with MSBuild directly, invoke it with e.g.
3446``/p:PlatformToolset=LLVM``. This allows trying out the clang-cl toolchain
3447without modifying your project files.
3448
3449It's also possible to point MSBuild at clang-cl without changing toolset by
3450passing ``/p:CLToolPath=c:\llvm\bin /p:CLToolExe=clang-cl.exe``.
3451
3452When using CMake and the Visual Studio generators, the toolset can be set with the ``-T`` flag:
3453
3454  ::
3455
3456    cmake -G"Visual Studio 15 2017" -T LLVM ..
3457
3458When using CMake with the Ninja generator, set the ``CMAKE_C_COMPILER`` and
3459``CMAKE_CXX_COMPILER`` variables to clang-cl:
3460
3461  ::
3462
3463    cmake -GNinja -DCMAKE_C_COMPILER="c:/Program Files (x86)/LLVM/bin/clang-cl.exe"
3464        -DCMAKE_CXX_COMPILER="c:/Program Files (x86)/LLVM/bin/clang-cl.exe" ..
3465
3466
3467Command-Line Options
3468--------------------
3469
3470To be compatible with cl.exe, clang-cl supports most of the same command-line
3471options. Those options can start with either ``/`` or ``-``. It also supports
3472some of Clang's core options, such as the ``-W`` options.
3473
3474Options that are known to clang-cl, but not currently supported, are ignored
3475with a warning. For example:
3476
3477  ::
3478
3479    clang-cl.exe: warning: argument unused during compilation: '/AI'
3480
3481To suppress warnings about unused arguments, use the ``-Qunused-arguments`` option.
3482
3483Options that are not known to clang-cl will be ignored by default. Use the
3484``-Werror=unknown-argument`` option in order to treat them as errors. If these
3485options are spelled with a leading ``/``, they will be mistaken for a filename:
3486
3487  ::
3488
3489    clang-cl.exe: error: no such file or directory: '/foobar'
3490
3491Please `file a bug <https://bugs.llvm.org/enter_bug.cgi?product=clang&component=Driver>`_
3492for any valid cl.exe flags that clang-cl does not understand.
3493
3494Execute ``clang-cl /?`` to see a list of supported options:
3495
3496  ::
3497
3498    CL.EXE COMPATIBILITY OPTIONS:
3499      /?                      Display available options
3500      /arch:<value>           Set architecture for code generation
3501      /Brepro-                Emit an object file which cannot be reproduced over time
3502      /Brepro                 Emit an object file which can be reproduced over time
3503      /clang:<arg>            Pass <arg> to the clang driver
3504      /C                      Don't discard comments when preprocessing
3505      /c                      Compile only
3506      /d1PP                   Retain macro definitions in /E mode
3507      /d1reportAllClassLayout Dump record layout information
3508      /diagnostics:caret      Enable caret and column diagnostics (on by default)
3509      /diagnostics:classic    Disable column and caret diagnostics
3510      /diagnostics:column     Disable caret diagnostics but keep column info
3511      /D <macro[=value]>      Define macro
3512      /EH<value>              Exception handling model
3513      /EP                     Disable linemarker output and preprocess to stdout
3514      /execution-charset:<value>
3515                              Runtime encoding, supports only UTF-8
3516      /E                      Preprocess to stdout
3517      /FA                     Output assembly code file during compilation
3518      /Fa<file or directory>  Output assembly code to this file during compilation (with /FA)
3519      /Fe<file or directory>  Set output executable file or directory (ends in / or \)
3520      /FI <value>             Include file before parsing
3521      /Fi<file>               Set preprocess output file name (with /P)
3522      /Fo<file or directory>  Set output object file, or directory (ends in / or \) (with /c)
3523      /fp:except-
3524      /fp:except
3525      /fp:fast
3526      /fp:precise
3527      /fp:strict
3528      /Fp<filename>           Set pch filename (with /Yc and /Yu)
3529      /GA                     Assume thread-local variables are defined in the executable
3530      /Gd                     Set __cdecl as a default calling convention
3531      /GF-                    Disable string pooling
3532      /GF                     Enable string pooling (default)
3533      /GR-                    Disable emission of RTTI data
3534      /Gregcall               Set __regcall as a default calling convention
3535      /GR                     Enable emission of RTTI data
3536      /Gr                     Set __fastcall as a default calling convention
3537      /GS-                    Disable buffer security check
3538      /GS                     Enable buffer security check (default)
3539      /Gs                     Use stack probes (default)
3540      /Gs<value>              Set stack probe size (default 4096)
3541      /guard:<value>          Enable Control Flow Guard with /guard:cf,
3542                              or only the table with /guard:cf,nochecks.
3543                              Enable EH Continuation Guard with /guard:ehcont
3544      /Gv                     Set __vectorcall as a default calling convention
3545      /Gw-                    Don't put each data item in its own section
3546      /Gw                     Put each data item in its own section
3547      /GX-                    Disable exception handling
3548      /GX                     Enable exception handling
3549      /Gy-                    Don't put each function in its own section (default)
3550      /Gy                     Put each function in its own section
3551      /Gz                     Set __stdcall as a default calling convention
3552      /help                   Display available options
3553      /imsvc <dir>            Add directory to system include search path, as if part of %INCLUDE%
3554      /I <dir>                Add directory to include search path
3555      /J                      Make char type unsigned
3556      /LDd                    Create debug DLL
3557      /LD                     Create DLL
3558      /link <options>         Forward options to the linker
3559      /MDd                    Use DLL debug run-time
3560      /MD                     Use DLL run-time
3561      /MTd                    Use static debug run-time
3562      /MT                     Use static run-time
3563      /O0                     Disable optimization
3564      /O1                     Optimize for size  (same as /Og     /Os /Oy /Ob2 /GF /Gy)
3565      /O2                     Optimize for speed (same as /Og /Oi /Ot /Oy /Ob2 /GF /Gy)
3566      /Ob0                    Disable function inlining
3567      /Ob1                    Only inline functions which are (explicitly or implicitly) marked inline
3568      /Ob2                    Inline functions as deemed beneficial by the compiler
3569      /Od                     Disable optimization
3570      /Og                     No effect
3571      /Oi-                    Disable use of builtin functions
3572      /Oi                     Enable use of builtin functions
3573      /Os                     Optimize for size
3574      /Ot                     Optimize for speed
3575      /Ox                     Deprecated (same as /Og /Oi /Ot /Oy /Ob2); use /O2 instead
3576      /Oy-                    Disable frame pointer omission (x86 only, default)
3577      /Oy                     Enable frame pointer omission (x86 only)
3578      /O<flags>               Set multiple /O flags at once; e.g. '/O2y-' for '/O2 /Oy-'
3579      /o <file or directory>  Set output file or directory (ends in / or \)
3580      /P                      Preprocess to file
3581      /Qvec-                  Disable the loop vectorization passes
3582      /Qvec                   Enable the loop vectorization passes
3583      /showFilenames-         Don't print the name of each compiled file (default)
3584      /showFilenames          Print the name of each compiled file
3585      /showIncludes           Print info about included files to stderr
3586      /source-charset:<value> Source encoding, supports only UTF-8
3587      /std:<value>            Language standard to compile for
3588      /TC                     Treat all source files as C
3589      /Tc <filename>          Specify a C source file
3590      /TP                     Treat all source files as C++
3591      /Tp <filename>          Specify a C++ source file
3592      /utf-8                  Set source and runtime encoding to UTF-8 (default)
3593      /U <macro>              Undefine macro
3594      /vd<value>              Control vtordisp placement
3595      /vmb                    Use a best-case representation method for member pointers
3596      /vmg                    Use a most-general representation for member pointers
3597      /vmm                    Set the default most-general representation to multiple inheritance
3598      /vms                    Set the default most-general representation to single inheritance
3599      /vmv                    Set the default most-general representation to virtual inheritance
3600      /volatile:iso           Volatile loads and stores have standard semantics
3601      /volatile:ms            Volatile loads and stores have acquire and release semantics
3602      /W0                     Disable all warnings
3603      /W1                     Enable -Wall
3604      /W2                     Enable -Wall
3605      /W3                     Enable -Wall
3606      /W4                     Enable -Wall and -Wextra
3607      /Wall                   Enable -Weverything
3608      /WX-                    Do not treat warnings as errors
3609      /WX                     Treat warnings as errors
3610      /w                      Disable all warnings
3611      /X                      Don't add %INCLUDE% to the include search path
3612      /Y-                     Disable precompiled headers, overrides /Yc and /Yu
3613      /Yc<filename>           Generate a pch file for all code up to and including <filename>
3614      /Yu<filename>           Load a pch file and use it instead of all code up to and including <filename>
3615      /Z7                     Enable CodeView debug information in object files
3616      /Zc:char8_t             Enable C++2a char8_t type
3617      /Zc:char8_t-            Disable C++2a char8_t type
3618      /Zc:dllexportInlines-   Don't dllexport/dllimport inline member functions of dllexport/import classes
3619      /Zc:dllexportInlines    dllexport/dllimport inline member functions of dllexport/import classes (default)
3620      /Zc:sizedDealloc-       Disable C++14 sized global deallocation functions
3621      /Zc:sizedDealloc        Enable C++14 sized global deallocation functions
3622      /Zc:strictStrings       Treat string literals as const
3623      /Zc:threadSafeInit-     Disable thread-safe initialization of static variables
3624      /Zc:threadSafeInit      Enable thread-safe initialization of static variables
3625      /Zc:trigraphs-          Disable trigraphs (default)
3626      /Zc:trigraphs           Enable trigraphs
3627      /Zc:twoPhase-           Disable two-phase name lookup in templates
3628      /Zc:twoPhase            Enable two-phase name lookup in templates
3629      /Zd                     Emit debug line number tables only
3630      /Zi                     Alias for /Z7. Does not produce PDBs.
3631      /Zl                     Don't mention any default libraries in the object file
3632      /Zp                     Set the default maximum struct packing alignment to 1
3633      /Zp<value>              Specify the default maximum struct packing alignment
3634      /Zs                     Syntax-check only
3635
3636    OPTIONS:
3637      -###                    Print (but do not run) the commands to run for this compilation
3638      --analyze               Run the static analyzer
3639      -faddrsig               Emit an address-significance table
3640      -fansi-escape-codes     Use ANSI escape codes for diagnostics
3641      -fblocks                Enable the 'blocks' language feature
3642      -fcf-protection=<value> Instrument control-flow architecture protection. Options: return, branch, full, none.
3643      -fcf-protection         Enable cf-protection in 'full' mode
3644      -fcolor-diagnostics     Use colors in diagnostics
3645      -fcomplete-member-pointers
3646                              Require member pointer base types to be complete if they would be significant under the Microsoft ABI
3647      -fcoverage-mapping      Generate coverage mapping to enable code coverage analysis
3648      -fcrash-diagnostics-dir=<dir>
3649                              Put crash-report files in <dir>
3650      -fdebug-macro           Emit macro debug information
3651      -fdelayed-template-parsing
3652                              Parse templated function definitions at the end of the translation unit
3653      -fdiagnostics-absolute-paths
3654                              Print absolute paths in diagnostics
3655      -fdiagnostics-parseable-fixits
3656                              Print fix-its in machine parseable form
3657      -flto=<value>           Set LTO mode to either 'full' or 'thin'
3658      -flto                   Enable LTO in 'full' mode
3659      -fmerge-all-constants   Allow merging of constants
3660      -fms-compatibility-version=<value>
3661                              Dot-separated value representing the Microsoft compiler version
3662                              number to report in _MSC_VER (0 = don't define it (default))
3663      -fms-compatibility      Enable full Microsoft Visual C++ compatibility
3664      -fms-extensions         Accept some non-standard constructs supported by the Microsoft compiler
3665      -fmsc-version=<value>   Microsoft compiler version number to report in _MSC_VER
3666                              (0 = don't define it (default))
3667      -fno-addrsig            Don't emit an address-significance table
3668      -fno-builtin-<value>    Disable implicit builtin knowledge of a specific function
3669      -fno-builtin            Disable implicit builtin knowledge of functions
3670      -fno-complete-member-pointers
3671                              Do not require member pointer base types to be complete if they would be significant under the Microsoft ABI
3672      -fno-coverage-mapping   Disable code coverage analysis
3673      -fno-crash-diagnostics  Disable auto-generation of preprocessed source files and a script for reproduction during a clang crash
3674      -fno-debug-macro        Do not emit macro debug information
3675      -fno-delayed-template-parsing
3676                              Disable delayed template parsing
3677      -fno-sanitize-address-poison-custom-array-cookie
3678                              Disable poisoning array cookies when using custom operator new[] in AddressSanitizer
3679      -fno-sanitize-address-use-after-scope
3680                              Disable use-after-scope detection in AddressSanitizer
3681      -fno-sanitize-address-use-odr-indicator
3682                               Disable ODR indicator globals
3683      -fno-sanitize-ignorelist Don't use ignorelist file for sanitizers
3684      -fno-sanitize-cfi-cross-dso
3685                              Disable control flow integrity (CFI) checks for cross-DSO calls.
3686      -fno-sanitize-coverage=<value>
3687                              Disable specified features of coverage instrumentation for Sanitizers
3688      -fno-sanitize-memory-track-origins
3689                              Disable origins tracking in MemorySanitizer
3690      -fno-sanitize-memory-use-after-dtor
3691                              Disable use-after-destroy detection in MemorySanitizer
3692      -fno-sanitize-recover=<value>
3693                              Disable recovery for specified sanitizers
3694      -fno-sanitize-stats     Disable sanitizer statistics gathering.
3695      -fno-sanitize-thread-atomics
3696                              Disable atomic operations instrumentation in ThreadSanitizer
3697      -fno-sanitize-thread-func-entry-exit
3698                              Disable function entry/exit instrumentation in ThreadSanitizer
3699      -fno-sanitize-thread-memory-access
3700                              Disable memory access instrumentation in ThreadSanitizer
3701      -fno-sanitize-trap=<value>
3702                              Disable trapping for specified sanitizers
3703      -fno-standalone-debug   Limit debug information produced to reduce size of debug binary
3704      -fobjc-runtime=<value>  Specify the target Objective-C runtime kind and version
3705      -fprofile-exclude-files=<value>
3706                              Instrument only functions from files where names don't match all the regexes separated by a semi-colon
3707      -fprofile-filter-files=<value>
3708                              Instrument only functions from files where names match any regex separated by a semi-colon
3709      -fprofile-instr-generate=<file>
3710                              Generate instrumented code to collect execution counts into <file>
3711                              (overridden by LLVM_PROFILE_FILE env var)
3712      -fprofile-instr-generate
3713                              Generate instrumented code to collect execution counts into default.profraw file
3714                              (overridden by '=' form of option or LLVM_PROFILE_FILE env var)
3715      -fprofile-instr-use=<value>
3716                              Use instrumentation data for profile-guided optimization
3717      -fprofile-remapping-file=<file>
3718                              Use the remappings described in <file> to match the profile data against names in the program
3719      -fprofile-list=<file>
3720                              Filename defining the list of functions/files to instrument
3721      -fsanitize-address-field-padding=<value>
3722                              Level of field padding for AddressSanitizer
3723      -fsanitize-address-globals-dead-stripping
3724                              Enable linker dead stripping of globals in AddressSanitizer
3725      -fsanitize-address-poison-custom-array-cookie
3726                              Enable poisoning array cookies when using custom operator new[] in AddressSanitizer
3727      -fsanitize-address-use-after-scope
3728                              Enable use-after-scope detection in AddressSanitizer
3729      -fsanitize-address-use-odr-indicator
3730                              Enable ODR indicator globals to avoid false ODR violation reports in partially sanitized programs at the cost of an increase in binary size
3731      -fsanitize-ignorelist=<value>
3732                              Path to ignorelist file for sanitizers
3733      -fsanitize-cfi-cross-dso
3734                              Enable control flow integrity (CFI) checks for cross-DSO calls.
3735      -fsanitize-cfi-icall-generalize-pointers
3736                              Generalize pointers in CFI indirect call type signature checks
3737      -fsanitize-coverage=<value>
3738                              Specify the type of coverage instrumentation for Sanitizers
3739      -fsanitize-hwaddress-abi=<value>
3740                              Select the HWAddressSanitizer ABI to target (interceptor or platform, default interceptor)
3741      -fsanitize-memory-track-origins=<value>
3742                              Enable origins tracking in MemorySanitizer
3743      -fsanitize-memory-track-origins
3744                              Enable origins tracking in MemorySanitizer
3745      -fsanitize-memory-use-after-dtor
3746                              Enable use-after-destroy detection in MemorySanitizer
3747      -fsanitize-recover=<value>
3748                              Enable recovery for specified sanitizers
3749      -fsanitize-stats        Enable sanitizer statistics gathering.
3750      -fsanitize-thread-atomics
3751                              Enable atomic operations instrumentation in ThreadSanitizer (default)
3752      -fsanitize-thread-func-entry-exit
3753                              Enable function entry/exit instrumentation in ThreadSanitizer (default)
3754      -fsanitize-thread-memory-access
3755                              Enable memory access instrumentation in ThreadSanitizer (default)
3756      -fsanitize-trap=<value> Enable trapping for specified sanitizers
3757      -fsanitize-undefined-strip-path-components=<number>
3758                              Strip (or keep only, if negative) a given number of path components when emitting check metadata.
3759      -fsanitize=<check>      Turn on runtime checks for various forms of undefined or suspicious
3760                              behavior. See user manual for available checks
3761      -fsplit-lto-unit        Enables splitting of the LTO unit.
3762      -fstandalone-debug      Emit full debug info for all types used by the program
3763      -fwhole-program-vtables Enables whole-program vtable optimization. Requires -flto
3764      -gcodeview-ghash        Emit type record hashes in a .debug$H section
3765      -gcodeview              Generate CodeView debug information
3766      -gline-directives-only  Emit debug line info directives only
3767      -gline-tables-only      Emit debug line number tables only
3768      -miamcu                 Use Intel MCU ABI
3769      -mllvm <value>          Additional arguments to forward to LLVM's option processing
3770      -nobuiltininc           Disable builtin #include directories
3771      -Qunused-arguments      Don't emit warning for unused driver arguments
3772      -R<remark>              Enable the specified remark
3773      --target=<value>        Generate code for the given target
3774      --version               Print version information
3775      -v                      Show commands to run and use verbose output
3776      -W<warning>             Enable the specified warning
3777      -Xclang <arg>           Pass <arg> to the clang compiler
3778
3779The /clang: Option
3780^^^^^^^^^^^^^^^^^^
3781
3782When clang-cl is run with a set of ``/clang:<arg>`` options, it will gather all
3783of the ``<arg>`` arguments and process them as if they were passed to the clang
3784driver. This mechanism allows you to pass flags that are not exposed in the
3785clang-cl options or flags that have a different meaning when passed to the clang
3786driver. Regardless of where they appear in the command line, the ``/clang:``
3787arguments are treated as if they were passed at the end of the clang-cl command
3788line.
3789
3790The /Zc:dllexportInlines- Option
3791^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3792
3793This causes the class-level `dllexport` and `dllimport` attributes to not apply
3794to inline member functions, as they otherwise would. For example, in the code
3795below `S::foo()` would normally be defined and exported by the DLL, but when
3796using the ``/Zc:dllexportInlines-`` flag it is not:
3797
3798.. code-block:: c
3799
3800  struct __declspec(dllexport) S {
3801    void foo() {}
3802  }
3803
3804This has the benefit that the compiler doesn't need to emit a definition of
3805`S::foo()` in every translation unit where the declaration is included, as it
3806would otherwise do to ensure there's a definition in the DLL even if it's not
3807used there. If the declaration occurs in a header file that's widely used, this
3808can save significant compilation time and output size. It also reduces the
3809number of functions exported by the DLL similarly to what
3810``-fvisibility-inlines-hidden`` does for shared objects on ELF and Mach-O.
3811Since the function declaration comes with an inline definition, users of the
3812library can use that definition directly instead of importing it from the DLL.
3813
3814Note that the Microsoft Visual C++ compiler does not support this option, and
3815if code in a DLL is compiled with ``/Zc:dllexportInlines-``, the code using the
3816DLL must be compiled in the same way so that it doesn't attempt to dllimport
3817the inline member functions. The reverse scenario should generally work though:
3818a DLL compiled without this flag (such as a system library compiled with Visual
3819C++) can be referenced from code compiled using the flag, meaning that the
3820referencing code will use the inline definitions instead of importing them from
3821the DLL.
3822
3823Also note that like when using ``-fvisibility-inlines-hidden``, the address of
3824`S::foo()` will be different inside and outside the DLL, breaking the C/C++
3825standard requirement that functions have a unique address.
3826
3827The flag does not apply to explicit class template instantiation definitions or
3828declarations, as those are typically used to explicitly provide a single
3829definition in a DLL, (dllexported instantiation definition) or to signal that
3830the definition is available elsewhere (dllimport instantiation declaration). It
3831also doesn't apply to inline members with static local variables, to ensure
3832that the same instance of the variable is used inside and outside the DLL.
3833
3834Using this flag can cause problems when inline functions that would otherwise
3835be dllexported refer to internal symbols of a DLL. For example:
3836
3837.. code-block:: c
3838
3839  void internal();
3840
3841  struct __declspec(dllimport) S {
3842    void foo() { internal(); }
3843  }
3844
3845Normally, references to `S::foo()` would use the definition in the DLL from
3846which it was exported, and which presumably also has the definition of
3847`internal()`. However, when using ``/Zc:dllexportInlines-``, the inline
3848definition of `S::foo()` is used directly, resulting in a link error since
3849`internal()` is not available. Even worse, if there is an inline definition of
3850`internal()` containing a static local variable, we will now refer to a
3851different instance of that variable than in the DLL:
3852
3853.. code-block:: c
3854
3855  inline int internal() { static int x; return x++; }
3856
3857  struct __declspec(dllimport) S {
3858    int foo() { return internal(); }
3859  }
3860
3861This could lead to very subtle bugs. Using ``-fvisibility-inlines-hidden`` can
3862lead to the same issue. To avoid it in this case, make `S::foo()` or
3863`internal()` non-inline, or mark them `dllimport/dllexport` explicitly.
3864
3865Finding Clang runtime libraries
3866^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3867
3868clang-cl supports several features that require runtime library support:
3869
3870- Address Sanitizer (ASan): ``-fsanitize=address``
3871- Undefined Behavior Sanitizer (UBSan): ``-fsanitize=undefined``
3872- Code coverage: ``-fprofile-instr-generate -fcoverage-mapping``
3873- Profile Guided Optimization (PGO): ``-fprofile-instr-generate``
3874- Certain math operations (int128 division) require the builtins library
3875
3876In order to use these features, the user must link the right runtime libraries
3877into their program. These libraries are distributed alongside Clang in the
3878library resource directory. Clang searches for the resource directory by
3879searching relative to the Clang executable. For example, if LLVM is installed
3880in ``C:\Program Files\LLVM``, then the profile runtime library will be located
3881at the path
3882``C:\Program Files\LLVM\lib\clang\11.0.0\lib\windows\clang_rt.profile-x86_64.lib``.
3883
3884For UBSan, PGO, and coverage, Clang will emit object files that auto-link the
3885appropriate runtime library, but the user generally needs to help the linker
3886(whether it is ``lld-link.exe`` or MSVC ``link.exe``) find the library resource
3887directory. Using the example installation above, this would mean passing
3888``/LIBPATH:C:\Program Files\LLVM\lib\clang\11.0.0\lib\windows`` to the linker.
3889If the user links the program with the ``clang`` or ``clang-cl`` drivers, the
3890driver will pass this flag for them.
3891
3892If the linker cannot find the appropriate library, it will emit an error like
3893this::
3894
3895  $ clang-cl -c -fsanitize=undefined t.cpp
3896
3897  $ lld-link t.obj -dll
3898  lld-link: error: could not open 'clang_rt.ubsan_standalone-x86_64.lib': no such file or directory
3899  lld-link: error: could not open 'clang_rt.ubsan_standalone_cxx-x86_64.lib': no such file or directory
3900
3901  $ link t.obj -dll -nologo
3902  LINK : fatal error LNK1104: cannot open file 'clang_rt.ubsan_standalone-x86_64.lib'
3903
3904To fix the error, add the appropriate ``/libpath:`` flag to the link line.
3905
3906For ASan, as of this writing, the user is also responsible for linking against
3907the correct ASan libraries.
3908
3909If the user is using the dynamic CRT (``/MD``), then they should add
3910``clang_rt.asan_dynamic-x86_64.lib`` to the link line as a regular input. For
3911other architectures, replace x86_64 with the appropriate name here and below.
3912
3913If the user is using the static CRT (``/MT``), then different runtimes are used
3914to produce DLLs and EXEs. To link a DLL, pass
3915``clang_rt.asan_dll_thunk-x86_64.lib``. To link an EXE, pass
3916``-wholearchive:clang_rt.asan-x86_64.lib``.
3917