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