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 C11 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`` 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``. 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``. 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: 1355 1356**-f[no-]finite-math** 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``. 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 Controls which checks enabled by the ``-fsanitize=`` flag trap. This 1496 option is intended for use in cases where the sanitizer runtime cannot 1497 be used (for instance, when building libc or a kernel module), or where 1498 the binary size increase caused by the sanitizer runtime is a concern. 1499 1500 This flag is only compatible with :doc:`control flow integrity 1501 <ControlFlowIntegrity>` schemes and :doc:`UndefinedBehaviorSanitizer` 1502 checks other than ``vptr``. If this flag 1503 is supplied together with ``-fsanitize=undefined``, the ``vptr`` sanitizer 1504 will be implicitly disabled. 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 1680Profile Guided Optimization 1681--------------------------- 1682 1683Profile information enables better optimization. For example, knowing that a 1684branch is taken very frequently helps the compiler make better decisions when 1685ordering basic blocks. Knowing that a function ``foo`` is called more 1686frequently than another function ``bar`` helps the inliner. Optimization 1687levels ``-O2`` and above are recommended for use of profile guided optimization. 1688 1689Clang supports profile guided optimization with two different kinds of 1690profiling. A sampling profiler can generate a profile with very low runtime 1691overhead, or you can build an instrumented version of the code that collects 1692more detailed profile information. Both kinds of profiles can provide execution 1693counts for instructions in the code and information on branches taken and 1694function invocation. 1695 1696Regardless of which kind of profiling you use, be careful to collect profiles 1697by running your code with inputs that are representative of the typical 1698behavior. Code that is not exercised in the profile will be optimized as if it 1699is unimportant, and the compiler may make poor optimization choices for code 1700that is disproportionately used while profiling. 1701 1702Differences Between Sampling and Instrumentation 1703^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 1704 1705Although both techniques are used for similar purposes, there are important 1706differences between the two: 1707 17081. Profile data generated with one cannot be used by the other, and there is no 1709 conversion tool that can convert one to the other. So, a profile generated 1710 via ``-fprofile-instr-generate`` must be used with ``-fprofile-instr-use``. 1711 Similarly, sampling profiles generated by external profilers must be 1712 converted and used with ``-fprofile-sample-use``. 1713 17142. Instrumentation profile data can be used for code coverage analysis and 1715 optimization. 1716 17173. Sampling profiles can only be used for optimization. They cannot be used for 1718 code coverage analysis. Although it would be technically possible to use 1719 sampling profiles for code coverage, sample-based profiles are too 1720 coarse-grained for code coverage purposes; it would yield poor results. 1721 17224. Sampling profiles must be generated by an external tool. The profile 1723 generated by that tool must then be converted into a format that can be read 1724 by LLVM. The section on sampling profilers describes one of the supported 1725 sampling profile formats. 1726 1727 1728Using Sampling Profilers 1729^^^^^^^^^^^^^^^^^^^^^^^^ 1730 1731Sampling profilers are used to collect runtime information, such as 1732hardware counters, while your application executes. They are typically 1733very efficient and do not incur a large runtime overhead. The 1734sample data collected by the profiler can be used during compilation 1735to determine what the most executed areas of the code are. 1736 1737Using the data from a sample profiler requires some changes in the way 1738a program is built. Before the compiler can use profiling information, 1739the code needs to execute under the profiler. The following is the 1740usual build cycle when using sample profilers for optimization: 1741 17421. Build the code with source line table information. You can use all the 1743 usual build flags that you always build your application with. The only 1744 requirement is that you add ``-gline-tables-only`` or ``-g`` to the 1745 command line. This is important for the profiler to be able to map 1746 instructions back to source line locations. 1747 1748 .. code-block:: console 1749 1750 $ clang++ -O2 -gline-tables-only code.cc -o code 1751 17522. Run the executable under a sampling profiler. The specific profiler 1753 you use does not really matter, as long as its output can be converted 1754 into the format that the LLVM optimizer understands. Currently, there 1755 exists a conversion tool for the Linux Perf profiler 1756 (https://perf.wiki.kernel.org/), so these examples assume that you 1757 are using Linux Perf to profile your code. 1758 1759 .. code-block:: console 1760 1761 $ perf record -b ./code 1762 1763 Note the use of the ``-b`` flag. This tells Perf to use the Last Branch 1764 Record (LBR) to record call chains. While this is not strictly required, 1765 it provides better call information, which improves the accuracy of 1766 the profile data. 1767 17683. Convert the collected profile data to LLVM's sample profile format. 1769 This is currently supported via the AutoFDO converter ``create_llvm_prof``. 1770 It is available at https://github.com/google/autofdo. Once built and 1771 installed, you can convert the ``perf.data`` file to LLVM using 1772 the command: 1773 1774 .. code-block:: console 1775 1776 $ create_llvm_prof --binary=./code --out=code.prof 1777 1778 This will read ``perf.data`` and the binary file ``./code`` and emit 1779 the profile data in ``code.prof``. Note that if you ran ``perf`` 1780 without the ``-b`` flag, you need to use ``--use_lbr=false`` when 1781 calling ``create_llvm_prof``. 1782 17834. Build the code again using the collected profile. This step feeds 1784 the profile back to the optimizers. This should result in a binary 1785 that executes faster than the original one. Note that you are not 1786 required to build the code with the exact same arguments that you 1787 used in the first step. The only requirement is that you build the code 1788 with ``-gline-tables-only`` and ``-fprofile-sample-use``. 1789 1790 .. code-block:: console 1791 1792 $ clang++ -O2 -gline-tables-only -fprofile-sample-use=code.prof code.cc -o code 1793 1794 1795Sample Profile Formats 1796"""""""""""""""""""""" 1797 1798Since external profilers generate profile data in a variety of custom formats, 1799the data generated by the profiler must be converted into a format that can be 1800read by the backend. LLVM supports three different sample profile formats: 1801 18021. ASCII text. This is the easiest one to generate. The file is divided into 1803 sections, which correspond to each of the functions with profile 1804 information. The format is described below. It can also be generated from 1805 the binary or gcov formats using the ``llvm-profdata`` tool. 1806 18072. Binary encoding. This uses a more efficient encoding that yields smaller 1808 profile files. This is the format generated by the ``create_llvm_prof`` tool 1809 in https://github.com/google/autofdo. 1810 18113. GCC encoding. This is based on the gcov format, which is accepted by GCC. It 1812 is only interesting in environments where GCC and Clang co-exist. This 1813 encoding is only generated by the ``create_gcov`` tool in 1814 https://github.com/google/autofdo. It can be read by LLVM and 1815 ``llvm-profdata``, but it cannot be generated by either. 1816 1817If you are using Linux Perf to generate sampling profiles, you can use the 1818conversion tool ``create_llvm_prof`` described in the previous section. 1819Otherwise, you will need to write a conversion tool that converts your 1820profiler's native format into one of these three. 1821 1822 1823Sample Profile Text Format 1824"""""""""""""""""""""""""" 1825 1826This section describes the ASCII text format for sampling profiles. It is, 1827arguably, the easiest one to generate. If you are interested in generating any 1828of the other two, consult the ``ProfileData`` library in LLVM's source tree 1829(specifically, ``include/llvm/ProfileData/SampleProfReader.h``). 1830 1831.. code-block:: console 1832 1833 function1:total_samples:total_head_samples 1834 offset1[.discriminator]: number_of_samples [fn1:num fn2:num ... ] 1835 offset2[.discriminator]: number_of_samples [fn3:num fn4:num ... ] 1836 ... 1837 offsetN[.discriminator]: number_of_samples [fn5:num fn6:num ... ] 1838 offsetA[.discriminator]: fnA:num_of_total_samples 1839 offsetA1[.discriminator]: number_of_samples [fn7:num fn8:num ... ] 1840 offsetA1[.discriminator]: number_of_samples [fn9:num fn10:num ... ] 1841 offsetB[.discriminator]: fnB:num_of_total_samples 1842 offsetB1[.discriminator]: number_of_samples [fn11:num fn12:num ... ] 1843 1844This is a nested tree in which the indentation represents the nesting level 1845of the inline stack. There are no blank lines in the file. And the spacing 1846within a single line is fixed. Additional spaces will result in an error 1847while reading the file. 1848 1849Any line starting with the '#' character is completely ignored. 1850 1851Inlined calls are represented with indentation. The Inline stack is a 1852stack of source locations in which the top of the stack represents the 1853leaf function, and the bottom of the stack represents the actual 1854symbol to which the instruction belongs. 1855 1856Function names must be mangled in order for the profile loader to 1857match them in the current translation unit. The two numbers in the 1858function header specify how many total samples were accumulated in the 1859function (first number), and the total number of samples accumulated 1860in the prologue of the function (second number). This head sample 1861count provides an indicator of how frequently the function is invoked. 1862 1863There are two types of lines in the function body. 1864 1865- Sampled line represents the profile information of a source location. 1866 ``offsetN[.discriminator]: number_of_samples [fn5:num fn6:num ... ]`` 1867 1868- Callsite line represents the profile information of an inlined callsite. 1869 ``offsetA[.discriminator]: fnA:num_of_total_samples`` 1870 1871Each sampled line may contain several items. Some are optional (marked 1872below): 1873 1874a. Source line offset. This number represents the line number 1875 in the function where the sample was collected. The line number is 1876 always relative to the line where symbol of the function is 1877 defined. So, if the function has its header at line 280, the offset 1878 13 is at line 293 in the file. 1879 1880 Note that this offset should never be a negative number. This could 1881 happen in cases like macros. The debug machinery will register the 1882 line number at the point of macro expansion. So, if the macro was 1883 expanded in a line before the start of the function, the profile 1884 converter should emit a 0 as the offset (this means that the optimizers 1885 will not be able to associate a meaningful weight to the instructions 1886 in the macro). 1887 1888b. [OPTIONAL] Discriminator. This is used if the sampled program 1889 was compiled with DWARF discriminator support 1890 (http://wiki.dwarfstd.org/index.php?title=Path_Discriminators). 1891 DWARF discriminators are unsigned integer values that allow the 1892 compiler to distinguish between multiple execution paths on the 1893 same source line location. 1894 1895 For example, consider the line of code ``if (cond) foo(); else bar();``. 1896 If the predicate ``cond`` is true 80% of the time, then the edge 1897 into function ``foo`` should be considered to be taken most of the 1898 time. But both calls to ``foo`` and ``bar`` are at the same source 1899 line, so a sample count at that line is not sufficient. The 1900 compiler needs to know which part of that line is taken more 1901 frequently. 1902 1903 This is what discriminators provide. In this case, the calls to 1904 ``foo`` and ``bar`` will be at the same line, but will have 1905 different discriminator values. This allows the compiler to correctly 1906 set edge weights into ``foo`` and ``bar``. 1907 1908c. Number of samples. This is an integer quantity representing the 1909 number of samples collected by the profiler at this source 1910 location. 1911 1912d. [OPTIONAL] Potential call targets and samples. If present, this 1913 line contains a call instruction. This models both direct and 1914 number of samples. For example, 1915 1916 .. code-block:: console 1917 1918 130: 7 foo:3 bar:2 baz:7 1919 1920 The above means that at relative line offset 130 there is a call 1921 instruction that calls one of ``foo()``, ``bar()`` and ``baz()``, 1922 with ``baz()`` being the relatively more frequently called target. 1923 1924As an example, consider a program with the call chain ``main -> foo -> bar``. 1925When built with optimizations enabled, the compiler may inline the 1926calls to ``bar`` and ``foo`` inside ``main``. The generated profile 1927could then be something like this: 1928 1929.. code-block:: console 1930 1931 main:35504:0 1932 1: _Z3foov:35504 1933 2: _Z32bari:31977 1934 1.1: 31977 1935 2: 0 1936 1937This profile indicates that there were a total of 35,504 samples 1938collected in main. All of those were at line 1 (the call to ``foo``). 1939Of those, 31,977 were spent inside the body of ``bar``. The last line 1940of the profile (``2: 0``) corresponds to line 2 inside ``main``. No 1941samples were collected there. 1942 1943Profiling with Instrumentation 1944^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 1945 1946Clang also supports profiling via instrumentation. This requires building a 1947special instrumented version of the code and has some runtime 1948overhead during the profiling, but it provides more detailed results than a 1949sampling profiler. It also provides reproducible results, at least to the 1950extent that the code behaves consistently across runs. 1951 1952Here are the steps for using profile guided optimization with 1953instrumentation: 1954 19551. Build an instrumented version of the code by compiling and linking with the 1956 ``-fprofile-instr-generate`` option. 1957 1958 .. code-block:: console 1959 1960 $ clang++ -O2 -fprofile-instr-generate code.cc -o code 1961 19622. Run the instrumented executable with inputs that reflect the typical usage. 1963 By default, the profile data will be written to a ``default.profraw`` file 1964 in the current directory. You can override that default by using option 1965 ``-fprofile-instr-generate=`` or by setting the ``LLVM_PROFILE_FILE`` 1966 environment variable to specify an alternate file. If non-default file name 1967 is specified by both the environment variable and the command line option, 1968 the environment variable takes precedence. The file name pattern specified 1969 can include different modifiers: ``%p``, ``%h``, and ``%m``. 1970 1971 Any instance of ``%p`` in that file name will be replaced by the process 1972 ID, so that you can easily distinguish the profile output from multiple 1973 runs. 1974 1975 .. code-block:: console 1976 1977 $ LLVM_PROFILE_FILE="code-%p.profraw" ./code 1978 1979 The modifier ``%h`` can be used in scenarios where the same instrumented 1980 binary is run in multiple different host machines dumping profile data 1981 to a shared network based storage. The ``%h`` specifier will be substituted 1982 with the hostname so that profiles collected from different hosts do not 1983 clobber each other. 1984 1985 While the use of ``%p`` specifier can reduce the likelihood for the profiles 1986 dumped from different processes to clobber each other, such clobbering can still 1987 happen because of the ``pid`` re-use by the OS. Another side-effect of using 1988 ``%p`` is that the storage requirement for raw profile data files is greatly 1989 increased. To avoid issues like this, the ``%m`` specifier can used in the profile 1990 name. When this specifier is used, the profiler runtime will substitute ``%m`` 1991 with a unique integer identifier associated with the instrumented binary. Additionally, 1992 multiple raw profiles dumped from different processes that share a file system (can be 1993 on different hosts) will be automatically merged by the profiler runtime during the 1994 dumping. If the program links in multiple instrumented shared libraries, each library 1995 will dump the profile data into its own profile data file (with its unique integer 1996 id embedded in the profile name). Note that the merging enabled by ``%m`` is for raw 1997 profile data generated by profiler runtime. The resulting merged "raw" profile data 1998 file still needs to be converted to a different format expected by the compiler ( 1999 see step 3 below). 2000 2001 .. code-block:: console 2002 2003 $ LLVM_PROFILE_FILE="code-%m.profraw" ./code 2004 2005 20063. Combine profiles from multiple runs and convert the "raw" profile format to 2007 the input expected by clang. Use the ``merge`` command of the 2008 ``llvm-profdata`` tool to do this. 2009 2010 .. code-block:: console 2011 2012 $ llvm-profdata merge -output=code.profdata code-*.profraw 2013 2014 Note that this step is necessary even when there is only one "raw" profile, 2015 since the merge operation also changes the file format. 2016 20174. Build the code again using the ``-fprofile-instr-use`` option to specify the 2018 collected profile data. 2019 2020 .. code-block:: console 2021 2022 $ clang++ -O2 -fprofile-instr-use=code.profdata code.cc -o code 2023 2024 You can repeat step 4 as often as you like without regenerating the 2025 profile. As you make changes to your code, clang may no longer be able to 2026 use the profile data. It will warn you when this happens. 2027 2028Profile generation using an alternative instrumentation method can be 2029controlled by the GCC-compatible flags ``-fprofile-generate`` and 2030``-fprofile-use``. Although these flags are semantically equivalent to 2031their GCC counterparts, they *do not* handle GCC-compatible profiles. 2032They are only meant to implement GCC's semantics with respect to 2033profile creation and use. Flag ``-fcs-profile-generate`` also instruments 2034programs using the same instrumentation method as ``-fprofile-generate``. 2035 2036.. option:: -fprofile-generate[=<dirname>] 2037 2038 The ``-fprofile-generate`` and ``-fprofile-generate=`` flags will use 2039 an alternative instrumentation method for profile generation. When 2040 given a directory name, it generates the profile file 2041 ``default_%m.profraw`` in the directory named ``dirname`` if specified. 2042 If ``dirname`` does not exist, it will be created at runtime. ``%m`` specifier 2043 will be substituted with a unique id documented in step 2 above. In other words, 2044 with ``-fprofile-generate[=<dirname>]`` option, the "raw" profile data automatic 2045 merging is turned on by default, so there will no longer any risk of profile 2046 clobbering from different running processes. For example, 2047 2048 .. code-block:: console 2049 2050 $ clang++ -O2 -fprofile-generate=yyy/zzz code.cc -o code 2051 2052 When ``code`` is executed, the profile will be written to the file 2053 ``yyy/zzz/default_xxxx.profraw``. 2054 2055 To generate the profile data file with the compiler readable format, the 2056 ``llvm-profdata`` tool can be used with the profile directory as the input: 2057 2058 .. code-block:: console 2059 2060 $ llvm-profdata merge -output=code.profdata yyy/zzz/ 2061 2062 If the user wants to turn off the auto-merging feature, or simply override the 2063 the profile dumping path specified at command line, the environment variable 2064 ``LLVM_PROFILE_FILE`` can still be used to override 2065 the directory and filename for the profile file at runtime. 2066 2067.. option:: -fcs-profile-generate[=<dirname>] 2068 2069 The ``-fcs-profile-generate`` and ``-fcs-profile-generate=`` flags will use 2070 the same instrumentation method, and generate the same profile as in the 2071 ``-fprofile-generate`` and ``-fprofile-generate=`` flags. The difference is 2072 that the instrumentation is performed after inlining so that the resulted 2073 profile has a better context sensitive information. They cannot be used 2074 together with ``-fprofile-generate`` and ``-fprofile-generate=`` flags. 2075 They are typically used in conjunction with ``-fprofile-use`` flag. 2076 The profile generated by ``-fcs-profile-generate`` and ``-fprofile-generate`` 2077 can be merged by llvm-profdata. A use example: 2078 2079 .. code-block:: console 2080 2081 $ clang++ -O2 -fprofile-generate=yyy/zzz code.cc -o code 2082 $ ./code 2083 $ llvm-profdata merge -output=code.profdata yyy/zzz/ 2084 2085 The first few steps are the same as that in ``-fprofile-generate`` 2086 compilation. Then perform a second round of instrumentation. 2087 2088 .. code-block:: console 2089 2090 $ clang++ -O2 -fprofile-use=code.profdata -fcs-profile-generate=sss/ttt \ 2091 -o cs_code 2092 $ ./cs_code 2093 $ llvm-profdata merge -output=cs_code.profdata sss/ttt code.profdata 2094 2095 The resulted ``cs_code.prodata`` combines ``code.profdata`` and the profile 2096 generated from binary ``cs_code``. Profile ``cs_code.profata`` can be used by 2097 ``-fprofile-use`` compilaton. 2098 2099 .. code-block:: console 2100 2101 $ clang++ -O2 -fprofile-use=cs_code.profdata 2102 2103 The above command will read both profiles to the compiler at the identical 2104 point of instrumenations. 2105 2106.. option:: -fprofile-use[=<pathname>] 2107 2108 Without any other arguments, ``-fprofile-use`` behaves identically to 2109 ``-fprofile-instr-use``. Otherwise, if ``pathname`` is the full path to a 2110 profile file, it reads from that file. If ``pathname`` is a directory name, 2111 it reads from ``pathname/default.profdata``. 2112 2113Disabling Instrumentation 2114^^^^^^^^^^^^^^^^^^^^^^^^^ 2115 2116In certain situations, it may be useful to disable profile generation or use 2117for specific files in a build, without affecting the main compilation flags 2118used for the other files in the project. 2119 2120In these cases, you can use the flag ``-fno-profile-instr-generate`` (or 2121``-fno-profile-generate``) to disable profile generation, and 2122``-fno-profile-instr-use`` (or ``-fno-profile-use``) to disable profile use. 2123 2124Note that these flags should appear after the corresponding profile 2125flags to have an effect. 2126 2127Profile remapping 2128^^^^^^^^^^^^^^^^^ 2129 2130When the program is compiled after a change that affects many symbol names, 2131pre-existing profile data may no longer match the program. For example: 2132 2133 * switching from libstdc++ to libc++ will result in the mangled names of all 2134 functions taking standard library types to change 2135 * renaming a widely-used type in C++ will result in the mangled names of all 2136 functions that have parameters involving that type to change 2137 * moving from a 32-bit compilation to a 64-bit compilation may change the 2138 underlying type of ``size_t`` and similar types, resulting in changes to 2139 manglings 2140 2141Clang allows use of a profile remapping file to specify that such differences 2142in mangled names should be ignored when matching the profile data against the 2143program. 2144 2145.. option:: -fprofile-remapping-file=<file> 2146 2147 Specifies a file containing profile remapping information, that will be 2148 used to match mangled names in the profile data to mangled names in the 2149 program. 2150 2151The profile remapping file is a text file containing lines of the form 2152 2153.. code-block:: text 2154 2155 fragmentkind fragment1 fragment2 2156 2157where ``fragmentkind`` is one of ``name``, ``type``, or ``encoding``, 2158indicating whether the following mangled name fragments are 2159<`name <https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangle.name>`_>s, 2160<`type <https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangle.type>`_>s, or 2161<`encoding <https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangle.encoding>`_>s, 2162respectively. 2163Blank lines and lines starting with ``#`` are ignored. 2164 2165For convenience, built-in <substitution>s such as ``St`` and ``Ss`` 2166are accepted as <name>s (even though they technically are not <name>s). 2167 2168For example, to specify that ``absl::string_view`` and ``std::string_view`` 2169should be treated as equivalent when matching profile data, the following 2170remapping file could be used: 2171 2172.. code-block:: text 2173 2174 # absl::string_view is considered equivalent to std::string_view 2175 type N4absl11string_viewE St17basic_string_viewIcSt11char_traitsIcEE 2176 2177 # std:: might be std::__1:: in libc++ or std::__cxx11:: in libstdc++ 2178 name 3std St3__1 2179 name 3std St7__cxx11 2180 2181Matching profile data using a profile remapping file is supported on a 2182best-effort basis. For example, information regarding indirect call targets is 2183currently not remapped. For best results, you are encouraged to generate new 2184profile data matching the updated program, or to remap the profile data 2185using the ``llvm-cxxmap`` and ``llvm-profdata merge`` tools. 2186 2187.. note:: 2188 2189 Profile data remapping support is currently only implemented for LLVM's 2190 new pass manager, which can be enabled with 2191 ``-fexperimental-new-pass-manager``. 2192 2193.. note:: 2194 2195 Profile data remapping is currently only supported for C++ mangled names 2196 following the Itanium C++ ABI mangling scheme. This covers all C++ targets 2197 supported by Clang other than Windows. 2198 2199GCOV-based Profiling 2200-------------------- 2201 2202GCOV is a test coverage program, it helps to know how often a line of code 2203is executed. When instrumenting the code with ``--coverage`` option, some 2204counters are added for each edge linking basic blocks. 2205 2206At compile time, gcno files are generated containing information about 2207blocks and edges between them. At runtime the counters are incremented and at 2208exit the counters are dumped in gcda files. 2209 2210The tool ``llvm-cov gcov`` will parse gcno, gcda and source files to generate 2211a report ``.c.gcov``. 2212 2213.. option:: -fprofile-filter-files=[regexes] 2214 2215 Define a list of regexes separated by a semi-colon. 2216 If a file name matches any of the regexes then the file is instrumented. 2217 2218 .. code-block:: console 2219 2220 $ clang --coverage -fprofile-filter-files=".*\.c$" foo.c 2221 2222 For example, this will only instrument files finishing with ``.c``, skipping ``.h`` files. 2223 2224.. option:: -fprofile-exclude-files=[regexes] 2225 2226 Define a list of regexes separated by a semi-colon. 2227 If a file name doesn't match all the regexes then the file is instrumented. 2228 2229 .. code-block:: console 2230 2231 $ clang --coverage -fprofile-exclude-files="^/usr/include/.*$" foo.c 2232 2233 For example, this will instrument all the files except the ones in ``/usr/include``. 2234 2235If both options are used then a file is instrumented if its name matches any 2236of the regexes from ``-fprofile-filter-list`` and doesn't match all the regexes 2237from ``-fprofile-exclude-list``. 2238 2239.. code-block:: console 2240 2241 $ clang --coverage -fprofile-exclude-files="^/usr/include/.*$" \ 2242 -fprofile-filter-files="^/usr/.*$" 2243 2244In that case ``/usr/foo/oof.h`` is instrumented since it matches the filter regex and 2245doesn't match the exclude regex, but ``/usr/include/foo.h`` doesn't since it matches 2246the exclude regex. 2247 2248Controlling Debug Information 2249----------------------------- 2250 2251Controlling Size of Debug Information 2252^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2253 2254Debug info kind generated by Clang can be set by one of the flags listed 2255below. If multiple flags are present, the last one is used. 2256 2257.. option:: -g0 2258 2259 Don't generate any debug info (default). 2260 2261.. option:: -gline-tables-only 2262 2263 Generate line number tables only. 2264 2265 This kind of debug info allows to obtain stack traces with function names, 2266 file names and line numbers (by such tools as ``gdb`` or ``addr2line``). It 2267 doesn't contain any other data (e.g. description of local variables or 2268 function parameters). 2269 2270.. option:: -fstandalone-debug 2271 2272 Clang supports a number of optimizations to reduce the size of debug 2273 information in the binary. They work based on the assumption that 2274 the debug type information can be spread out over multiple 2275 compilation units. For instance, Clang will not emit type 2276 definitions for types that are not needed by a module and could be 2277 replaced with a forward declaration. Further, Clang will only emit 2278 type info for a dynamic C++ class in the module that contains the 2279 vtable for the class. 2280 2281 The **-fstandalone-debug** option turns off these optimizations. 2282 This is useful when working with 3rd-party libraries that don't come 2283 with debug information. Note that Clang will never emit type 2284 information for types that are not referenced at all by the program. 2285 2286.. option:: -fno-standalone-debug 2287 2288 On Darwin **-fstandalone-debug** is enabled by default. The 2289 **-fno-standalone-debug** option can be used to get to turn on the 2290 vtable-based optimization described above. 2291 2292.. option:: -g 2293 2294 Generate complete debug info. 2295 2296Controlling Macro Debug Info Generation 2297^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2298 2299Debug info for C preprocessor macros increases the size of debug information in 2300the binary. Macro debug info generated by Clang can be controlled by the flags 2301listed below. 2302 2303.. option:: -fdebug-macro 2304 2305 Generate debug info for preprocessor macros. This flag is discarded when 2306 **-g0** is enabled. 2307 2308.. option:: -fno-debug-macro 2309 2310 Do not generate debug info for preprocessor macros (default). 2311 2312Controlling Debugger "Tuning" 2313^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2314 2315While Clang generally emits standard DWARF debug info (http://dwarfstd.org), 2316different debuggers may know how to take advantage of different specific DWARF 2317features. You can "tune" the debug info for one of several different debuggers. 2318 2319.. option:: -ggdb, -glldb, -gsce 2320 2321 Tune the debug info for the ``gdb``, ``lldb``, or Sony PlayStation\ |reg| 2322 debugger, respectively. Each of these options implies **-g**. (Therefore, if 2323 you want both **-gline-tables-only** and debugger tuning, the tuning option 2324 must come first.) 2325 2326 2327Controlling LLVM IR Output 2328-------------------------- 2329 2330Controlling Value Names in LLVM IR 2331^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2332 2333Emitting value names in LLVM IR increases the size and verbosity of the IR. 2334By default, value names are only emitted in assertion-enabled builds of Clang. 2335However, when reading IR it can be useful to re-enable the emission of value 2336names to improve readability. 2337 2338.. option:: -fdiscard-value-names 2339 2340 Discard value names when generating LLVM IR. 2341 2342.. option:: -fno-discard-value-names 2343 2344 Do not discard value names when generating LLVM IR. This option can be used 2345 to re-enable names for release builds of Clang. 2346 2347 2348Comment Parsing Options 2349----------------------- 2350 2351Clang parses Doxygen and non-Doxygen style documentation comments and attaches 2352them to the appropriate declaration nodes. By default, it only parses 2353Doxygen-style comments and ignores ordinary comments starting with ``//`` and 2354``/*``. 2355 2356.. option:: -Wdocumentation 2357 2358 Emit warnings about use of documentation comments. This warning group is off 2359 by default. 2360 2361 This includes checking that ``\param`` commands name parameters that actually 2362 present in the function signature, checking that ``\returns`` is used only on 2363 functions that actually return a value etc. 2364 2365.. option:: -Wno-documentation-unknown-command 2366 2367 Don't warn when encountering an unknown Doxygen command. 2368 2369.. option:: -fparse-all-comments 2370 2371 Parse all comments as documentation comments (including ordinary comments 2372 starting with ``//`` and ``/*``). 2373 2374.. option:: -fcomment-block-commands=[commands] 2375 2376 Define custom documentation commands as block commands. This allows Clang to 2377 construct the correct AST for these custom commands, and silences warnings 2378 about unknown commands. Several commands must be separated by a comma 2379 *without trailing space*; e.g. ``-fcomment-block-commands=foo,bar`` defines 2380 custom commands ``\foo`` and ``\bar``. 2381 2382 It is also possible to use ``-fcomment-block-commands`` several times; e.g. 2383 ``-fcomment-block-commands=foo -fcomment-block-commands=bar`` does the same 2384 as above. 2385 2386.. _c: 2387 2388C Language Features 2389=================== 2390 2391The support for standard C in clang is feature-complete except for the 2392C99 floating-point pragmas. 2393 2394Extensions supported by clang 2395----------------------------- 2396 2397See :doc:`LanguageExtensions`. 2398 2399Differences between various standard modes 2400------------------------------------------ 2401 2402clang supports the -std option, which changes what language mode clang 2403uses. The supported modes for C are c89, gnu89, c99, gnu99, c11, gnu11, 2404c17, gnu17, and various aliases for those modes. If no -std option is 2405specified, clang defaults to gnu11 mode. Many C99 and C11 features are 2406supported in earlier modes as a conforming extension, with a warning. Use 2407``-pedantic-errors`` to request an error if a feature from a later standard 2408revision is used in an earlier mode. 2409 2410Differences between all ``c*`` and ``gnu*`` modes: 2411 2412- ``c*`` modes define "``__STRICT_ANSI__``". 2413- Target-specific defines not prefixed by underscores, like "linux", 2414 are defined in ``gnu*`` modes. 2415- Trigraphs default to being off in ``gnu*`` modes; they can be enabled by 2416 the -trigraphs option. 2417- The parser recognizes "asm" and "typeof" as keywords in ``gnu*`` modes; 2418 the variants "``__asm__``" and "``__typeof__``" are recognized in all 2419 modes. 2420- The Apple "blocks" extension is recognized by default in ``gnu*`` modes 2421 on some platforms; it can be enabled in any mode with the "-fblocks" 2422 option. 2423- Arrays that are VLA's according to the standard, but which can be 2424 constant folded by the frontend are treated as fixed size arrays. 2425 This occurs for things like "int X[(1, 2)];", which is technically a 2426 VLA. ``c*`` modes are strictly compliant and treat these as VLAs. 2427 2428Differences between ``*89`` and ``*99`` modes: 2429 2430- The ``*99`` modes default to implementing "inline" as specified in C99, 2431 while the ``*89`` modes implement the GNU version. This can be 2432 overridden for individual functions with the ``__gnu_inline__`` 2433 attribute. 2434- Digraphs are not recognized in c89 mode. 2435- The scope of names defined inside a "for", "if", "switch", "while", 2436 or "do" statement is different. (example: "``if ((struct x {int 2437 x;}*)0) {}``".) 2438- ``__STDC_VERSION__`` is not defined in ``*89`` modes. 2439- "inline" is not recognized as a keyword in c89 mode. 2440- "restrict" is not recognized as a keyword in ``*89`` modes. 2441- Commas are allowed in integer constant expressions in ``*99`` modes. 2442- Arrays which are not lvalues are not implicitly promoted to pointers 2443 in ``*89`` modes. 2444- Some warnings are different. 2445 2446Differences between ``*99`` and ``*11`` modes: 2447 2448- Warnings for use of C11 features are disabled. 2449- ``__STDC_VERSION__`` is defined to ``201112L`` rather than ``199901L``. 2450 2451Differences between ``*11`` and ``*17`` modes: 2452 2453- ``__STDC_VERSION__`` is defined to ``201710L`` rather than ``201112L``. 2454 2455GCC extensions not implemented yet 2456---------------------------------- 2457 2458clang tries to be compatible with gcc as much as possible, but some gcc 2459extensions are not implemented yet: 2460 2461- clang does not support decimal floating point types (``_Decimal32`` and 2462 friends) or fixed-point types (``_Fract`` and friends); nobody has 2463 expressed interest in these features yet, so it's hard to say when 2464 they will be implemented. 2465- clang does not support nested functions; this is a complex feature 2466 which is infrequently used, so it is unlikely to be implemented 2467 anytime soon. In C++11 it can be emulated by assigning lambda 2468 functions to local variables, e.g: 2469 2470 .. code-block:: cpp 2471 2472 auto const local_function = [&](int parameter) { 2473 // Do something 2474 }; 2475 ... 2476 local_function(1); 2477 2478- clang only supports global register variables when the register specified 2479 is non-allocatable (e.g. the stack pointer). Support for general global 2480 register variables is unlikely to be implemented soon because it requires 2481 additional LLVM backend support. 2482- clang does not support static initialization of flexible array 2483 members. This appears to be a rarely used extension, but could be 2484 implemented pending user demand. 2485- clang does not support 2486 ``__builtin_va_arg_pack``/``__builtin_va_arg_pack_len``. This is 2487 used rarely, but in some potentially interesting places, like the 2488 glibc headers, so it may be implemented pending user demand. Note 2489 that because clang pretends to be like GCC 4.2, and this extension 2490 was introduced in 4.3, the glibc headers will not try to use this 2491 extension with clang at the moment. 2492- clang does not support the gcc extension for forward-declaring 2493 function parameters; this has not shown up in any real-world code 2494 yet, though, so it might never be implemented. 2495 2496This is not a complete list; if you find an unsupported extension 2497missing from this list, please send an e-mail to cfe-dev. This list 2498currently excludes C++; see :ref:`C++ Language Features <cxx>`. Also, this 2499list does not include bugs in mostly-implemented features; please see 2500the `bug 2501tracker <https://bugs.llvm.org/buglist.cgi?quicksearch=product%3Aclang+component%3A-New%2BBugs%2CAST%2CBasic%2CDriver%2CHeaders%2CLLVM%2BCodeGen%2Cparser%2Cpreprocessor%2CSemantic%2BAnalyzer>`_ 2502for known existing bugs (FIXME: Is there a section for bug-reporting 2503guidelines somewhere?). 2504 2505Intentionally unsupported GCC extensions 2506---------------------------------------- 2507 2508- clang does not support the gcc extension that allows variable-length 2509 arrays in structures. This is for a few reasons: one, it is tricky to 2510 implement, two, the extension is completely undocumented, and three, 2511 the extension appears to be rarely used. Note that clang *does* 2512 support flexible array members (arrays with a zero or unspecified 2513 size at the end of a structure). 2514- clang does not have an equivalent to gcc's "fold"; this means that 2515 clang doesn't accept some constructs gcc might accept in contexts 2516 where a constant expression is required, like "x-x" where x is a 2517 variable. 2518- clang does not support ``__builtin_apply`` and friends; this extension 2519 is extremely obscure and difficult to implement reliably. 2520 2521.. _c_ms: 2522 2523Microsoft extensions 2524-------------------- 2525 2526clang has support for many extensions from Microsoft Visual C++. To enable these 2527extensions, use the ``-fms-extensions`` command-line option. This is the default 2528for Windows targets. Clang does not implement every pragma or declspec provided 2529by MSVC, but the popular ones, such as ``__declspec(dllexport)`` and ``#pragma 2530comment(lib)`` are well supported. 2531 2532clang has a ``-fms-compatibility`` flag that makes clang accept enough 2533invalid C++ to be able to parse most Microsoft headers. For example, it 2534allows `unqualified lookup of dependent base class members 2535<https://clang.llvm.org/compatibility.html#dep_lookup_bases>`_, which is 2536a common compatibility issue with clang. This flag is enabled by default 2537for Windows targets. 2538 2539``-fdelayed-template-parsing`` lets clang delay parsing of function template 2540definitions until the end of a translation unit. This flag is enabled by 2541default for Windows targets. 2542 2543For compatibility with existing code that compiles with MSVC, clang defines the 2544``_MSC_VER`` and ``_MSC_FULL_VER`` macros. These default to the values of 1800 2545and 180000000 respectively, making clang look like an early release of Visual 2546C++ 2013. The ``-fms-compatibility-version=`` flag overrides these values. It 2547accepts a dotted version tuple, such as 19.00.23506. Changing the MSVC 2548compatibility version makes clang behave more like that version of MSVC. For 2549example, ``-fms-compatibility-version=19`` will enable C++14 features and define 2550``char16_t`` and ``char32_t`` as builtin types. 2551 2552.. _cxx: 2553 2554C++ Language Features 2555===================== 2556 2557clang fully implements all of standard C++98 except for exported 2558templates (which were removed in C++11), and all of standard C++11 2559and the current draft standard for C++1y. 2560 2561Controlling implementation limits 2562--------------------------------- 2563 2564.. option:: -fbracket-depth=N 2565 2566 Sets the limit for nested parentheses, brackets, and braces to N. The 2567 default is 256. 2568 2569.. option:: -fconstexpr-depth=N 2570 2571 Sets the limit for recursive constexpr function invocations to N. The 2572 default is 512. 2573 2574.. option:: -fconstexpr-steps=N 2575 2576 Sets the limit for the number of full-expressions evaluated in a single 2577 constant expression evaluation. The default is 1048576. 2578 2579.. option:: -ftemplate-depth=N 2580 2581 Sets the limit for recursively nested template instantiations to N. The 2582 default is 1024. 2583 2584.. option:: -foperator-arrow-depth=N 2585 2586 Sets the limit for iterative calls to 'operator->' functions to N. The 2587 default is 256. 2588 2589.. _objc: 2590 2591Objective-C Language Features 2592============================= 2593 2594.. _objcxx: 2595 2596Objective-C++ Language Features 2597=============================== 2598 2599.. _openmp: 2600 2601OpenMP Features 2602=============== 2603 2604Clang supports all OpenMP 4.5 directives and clauses. See :doc:`OpenMPSupport` 2605for additional details. 2606 2607Use `-fopenmp` to enable OpenMP. Support for OpenMP can be disabled with 2608`-fno-openmp`. 2609 2610Use `-fopenmp-simd` to enable OpenMP simd features only, without linking 2611the runtime library; for combined constructs 2612(e.g. ``#pragma omp parallel for simd``) the non-simd directives and clauses 2613will be ignored. This can be disabled with `-fno-openmp-simd`. 2614 2615Controlling implementation limits 2616--------------------------------- 2617 2618.. option:: -fopenmp-use-tls 2619 2620 Controls code generation for OpenMP threadprivate variables. In presence of 2621 this option all threadprivate variables are generated the same way as thread 2622 local variables, using TLS support. If `-fno-openmp-use-tls` 2623 is provided or target does not support TLS, code generation for threadprivate 2624 variables relies on OpenMP runtime library. 2625 2626.. _opencl: 2627 2628OpenCL Features 2629=============== 2630 2631Clang can be used to compile OpenCL kernels for execution on a device 2632(e.g. GPU). It is possible to compile the kernel into a binary (e.g. for AMD or 2633Nvidia targets) that can be uploaded to run directly on a device (e.g. using 2634`clCreateProgramWithBinary 2635<https://www.khronos.org/registry/OpenCL/specs/opencl-1.1.pdf#111>`_) or 2636into generic bitcode files loadable into other toolchains. 2637 2638Compiling to a binary using the default target from the installation can be done 2639as follows: 2640 2641 .. code-block:: console 2642 2643 $ echo "kernel void k(){}" > test.cl 2644 $ clang test.cl 2645 2646Compiling for a specific target can be done by specifying the triple corresponding 2647to the target, for example: 2648 2649 .. code-block:: console 2650 2651 $ clang -target nvptx64-unknown-unknown test.cl 2652 $ clang -target amdgcn-amd-amdhsa -mcpu=gfx900 test.cl 2653 2654Compiling to bitcode can be done as follows: 2655 2656 .. code-block:: console 2657 2658 $ clang -c -emit-llvm test.cl 2659 2660This will produce a generic test.bc file that can be used in vendor toolchains 2661to perform machine code generation. 2662 2663Clang currently supports OpenCL C language standards up to v2.0. Starting from 2664clang 9 a C++ mode is available for OpenCL (see 2665:ref:`C++ for OpenCL <cxx_for_opencl>`). 2666 2667OpenCL Specific Options 2668----------------------- 2669 2670Most of the OpenCL build options from `the specification v2.0 section 5.8.4 2671<https://www.khronos.org/registry/cl/specs/opencl-2.0.pdf#200>`_ are available. 2672 2673Examples: 2674 2675 .. code-block:: console 2676 2677 $ clang -cl-std=CL2.0 -cl-single-precision-constant test.cl 2678 2679Some extra options are available to support special OpenCL features. 2680 2681.. option:: -finclude-default-header 2682 2683Loads standard includes during compilations. By default OpenCL headers are not 2684loaded and therefore standard library includes are not available. To load them 2685automatically a flag has been added to the frontend (see also :ref:`the section 2686on the OpenCL Header <opencl_header>`): 2687 2688 .. code-block:: console 2689 2690 $ clang -Xclang -finclude-default-header test.cl 2691 2692Alternatively ``-include`` or ``-I`` followed by the path to the header location 2693can be given manually. 2694 2695 .. code-block:: console 2696 2697 $ clang -I<path to clang>/lib/Headers/opencl-c.h test.cl 2698 2699In this case the kernel code should contain ``#include <opencl-c.h>`` just as a 2700regular C include. 2701 2702.. _opencl_cl_ext: 2703 2704.. option:: -cl-ext 2705 2706Disables support of OpenCL extensions. All OpenCL targets provide a list 2707of extensions that they support. Clang allows to amend this using the ``-cl-ext`` 2708flag with a comma-separated list of extensions prefixed with ``'+'`` or ``'-'``. 2709The syntax: ``-cl-ext=<(['-'|'+']<extension>[,])+>``, where extensions 2710can be either one of `the OpenCL specification extensions 2711<https://www.khronos.org/registry/cl/sdk/2.0/docs/man/xhtml/EXTENSION.html>`_ 2712or any known vendor extension. Alternatively, ``'all'`` can be used to enable 2713or disable all known extensions. 2714Example disabling double support for the 64-bit SPIR target: 2715 2716 .. code-block:: console 2717 2718 $ clang -cc1 -triple spir64-unknown-unknown -cl-ext=-cl_khr_fp64 test.cl 2719 2720Enabling all extensions except double support in R600 AMD GPU can be done using: 2721 2722 .. code-block:: console 2723 2724 $ clang -cc1 -triple r600-unknown-unknown -cl-ext=-all,+cl_khr_fp16 test.cl 2725 2726.. _opencl_fake_address_space_map: 2727 2728.. option:: -ffake-address-space-map 2729 2730Overrides the target address space map with a fake map. 2731This allows adding explicit address space IDs to the bitcode for non-segmented 2732memory architectures that don't have separate IDs for each of the OpenCL 2733logical address spaces by default. Passing ``-ffake-address-space-map`` will 2734add/override address spaces of the target compiled for with the following values: 2735``1-global``, ``2-constant``, ``3-local``, ``4-generic``. The private address 2736space is represented by the absence of an address space attribute in the IR (see 2737also :ref:`the section on the address space attribute <opencl_addrsp>`). 2738 2739 .. code-block:: console 2740 2741 $ clang -ffake-address-space-map test.cl 2742 2743Some other flags used for the compilation for C can also be passed while 2744compiling for OpenCL, examples: ``-c``, ``-O<1-4|s>``, ``-o``, ``-emit-llvm``, etc. 2745 2746OpenCL Targets 2747-------------- 2748 2749OpenCL targets are derived from the regular Clang target classes. The OpenCL 2750specific parts of the target representation provide address space mapping as 2751well as a set of supported extensions. 2752 2753Specific Targets 2754^^^^^^^^^^^^^^^^ 2755 2756There is a set of concrete HW architectures that OpenCL can be compiled for. 2757 2758- For AMD target: 2759 2760 .. code-block:: console 2761 2762 $ clang -target amdgcn-amd-amdhsa -mcpu=gfx900 test.cl 2763 2764- For Nvidia architectures: 2765 2766 .. code-block:: console 2767 2768 $ clang -target nvptx64-unknown-unknown test.cl 2769 2770 2771Generic Targets 2772^^^^^^^^^^^^^^^ 2773 2774- SPIR is available as a generic target to allow portable bitcode to be produced 2775 that can be used across GPU toolchains. The implementation follows `the SPIR 2776 specification <https://www.khronos.org/spir>`_. There are two flavors 2777 available for 32 and 64 bits. 2778 2779 .. code-block:: console 2780 2781 $ clang -target spir-unknown-unknown test.cl 2782 $ clang -target spir64-unknown-unknown test.cl 2783 2784 All known OpenCL extensions are supported in the SPIR targets. Clang will 2785 generate SPIR v1.2 compatible IR for OpenCL versions up to 2.0 and SPIR v2.0 2786 for OpenCL v2.0. 2787 2788- x86 is used by some implementations that are x86 compatible and currently 2789 remains for backwards compatibility (with older implementations prior to 2790 SPIR target support). For "non-SPMD" targets which cannot spawn multiple 2791 work-items on the fly using hardware, which covers practically all non-GPU 2792 devices such as CPUs and DSPs, additional processing is needed for the kernels 2793 to support multiple work-item execution. For this, a 3rd party toolchain, 2794 such as for example `POCL <http://portablecl.org/>`_, can be used. 2795 2796 This target does not support multiple memory segments and, therefore, the fake 2797 address space map can be added using the :ref:`-ffake-address-space-map 2798 <opencl_fake_address_space_map>` flag. 2799 2800.. _opencl_header: 2801 2802OpenCL Header 2803------------- 2804 2805By default Clang will not include standard headers and therefore OpenCL builtin 2806functions and some types (i.e. vectors) are unknown. The default CL header is, 2807however, provided in the Clang installation and can be enabled by passing the 2808``-finclude-default-header`` flag to the Clang frontend. 2809 2810 .. code-block:: console 2811 2812 $ echo "bool is_wg_uniform(int i){return get_enqueued_local_size(i)==get_local_size(i);}" > test.cl 2813 $ clang -Xclang -finclude-default-header -cl-std=CL2.0 test.cl 2814 2815Because the header is very large and long to parse, PCH (:doc:`PCHInternals`) 2816and modules (:doc:`Modules`) are used internally to improve the compilation 2817speed. 2818 2819To enable modules for OpenCL: 2820 2821 .. code-block:: console 2822 2823 $ 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 2824 2825OpenCL Extensions 2826----------------- 2827 2828All of the ``cl_khr_*`` extensions from `the official OpenCL specification 2829<https://www.khronos.org/registry/OpenCL/sdk/2.0/docs/man/xhtml/EXTENSION.html>`_ 2830up to and including version 2.0 are available and set per target depending on the 2831support available in the specific architecture. 2832 2833It is possible to alter the default extensions setting per target using 2834``-cl-ext`` flag. (See :ref:`flags description <opencl_cl_ext>` for more details). 2835 2836Vendor extensions can be added flexibly by declaring the list of types and 2837functions associated with each extensions enclosed within the following 2838compiler pragma directives: 2839 2840 .. code-block:: c 2841 2842 #pragma OPENCL EXTENSION the_new_extension_name : begin 2843 // declare types and functions associated with the extension here 2844 #pragma OPENCL EXTENSION the_new_extension_name : end 2845 2846For example, parsing the following code adds ``my_t`` type and ``my_func`` 2847function to the custom ``my_ext`` extension. 2848 2849 .. code-block:: c 2850 2851 #pragma OPENCL EXTENSION my_ext : begin 2852 typedef struct{ 2853 int a; 2854 }my_t; 2855 void my_func(my_t); 2856 #pragma OPENCL EXTENSION my_ext : end 2857 2858Declaring the same types in different vendor extensions is disallowed. 2859 2860Clang also supports language extensions documented in `The OpenCL C Language 2861Extensions Documentation 2862<https://github.com/KhronosGroup/Khronosdotorg/blob/master/api/opencl/assets/OpenCL_LangExt.pdf>`_. 2863 2864OpenCL Metadata 2865--------------- 2866 2867Clang uses metadata to provide additional OpenCL semantics in IR needed for 2868backends and OpenCL runtime. 2869 2870Each kernel will have function metadata attached to it, specifying the arguments. 2871Kernel argument metadata is used to provide source level information for querying 2872at runtime, for example using the `clGetKernelArgInfo 2873<https://www.khronos.org/registry/OpenCL/specs/opencl-1.2.pdf#167>`_ 2874call. 2875 2876Note that ``-cl-kernel-arg-info`` enables more information about the original CL 2877code to be added e.g. kernel parameter names will appear in the OpenCL metadata 2878along with other information. 2879 2880The IDs used to encode the OpenCL's logical address spaces in the argument info 2881metadata follows the SPIR address space mapping as defined in the SPIR 2882specification `section 2.2 2883<https://www.khronos.org/registry/spir/specs/spir_spec-2.0.pdf#18>`_ 2884 2885OpenCL-Specific Attributes 2886-------------------------- 2887 2888OpenCL support in Clang contains a set of attribute taken directly from the 2889specification as well as additional attributes. 2890 2891See also :doc:`AttributeReference`. 2892 2893nosvm 2894^^^^^ 2895 2896Clang supports this attribute to comply to OpenCL v2.0 conformance, but it 2897does not have any effect on the IR. For more details reffer to the specification 2898`section 6.7.2 2899<https://www.khronos.org/registry/cl/specs/opencl-2.0-openclc.pdf#49>`_ 2900 2901 2902opencl_unroll_hint 2903^^^^^^^^^^^^^^^^^^ 2904 2905The implementation of this feature mirrors the unroll hint for C. 2906More details on the syntax can be found in the specification 2907`section 6.11.5 2908<https://www.khronos.org/registry/cl/specs/opencl-2.0-openclc.pdf#61>`_ 2909 2910convergent 2911^^^^^^^^^^ 2912 2913To make sure no invalid optimizations occur for single program multiple data 2914(SPMD) / single instruction multiple thread (SIMT) Clang provides attributes that 2915can be used for special functions that have cross work item semantics. 2916An example is the subgroup operations such as `intel_sub_group_shuffle 2917<https://www.khronos.org/registry/cl/extensions/intel/cl_intel_subgroups.txt>`_ 2918 2919 .. code-block:: c 2920 2921 // Define custom my_sub_group_shuffle(data, c) 2922 // that makes use of intel_sub_group_shuffle 2923 r1 = ... 2924 if (r0) r1 = computeA(); 2925 // Shuffle data from r1 into r3 2926 // of threads id r2. 2927 r3 = my_sub_group_shuffle(r1, r2); 2928 if (r0) r3 = computeB(); 2929 2930with non-SPMD semantics this is optimized to the following equivalent code: 2931 2932 .. code-block:: c 2933 2934 r1 = ... 2935 if (!r0) 2936 // Incorrect functionality! The data in r1 2937 // have not been computed by all threads yet. 2938 r3 = my_sub_group_shuffle(r1, r2); 2939 else { 2940 r1 = computeA(); 2941 r3 = my_sub_group_shuffle(r1, r2); 2942 r3 = computeB(); 2943 } 2944 2945Declaring the function ``my_sub_group_shuffle`` with the convergent attribute 2946would prevent this: 2947 2948 .. code-block:: c 2949 2950 my_sub_group_shuffle() __attribute__((convergent)); 2951 2952Using ``convergent`` guarantees correct execution by keeping CFG equivalence 2953wrt operations marked as ``convergent``. CFG ``G´`` is equivalent to ``G`` wrt 2954node ``Ni`` : ``iff ∀ Nj (i≠j)`` domination and post-domination relations with 2955respect to ``Ni`` remain the same in both ``G`` and ``G´``. 2956 2957noduplicate 2958^^^^^^^^^^^ 2959 2960``noduplicate`` is more restrictive with respect to optimizations than 2961``convergent`` because a convergent function only preserves CFG equivalence. 2962This allows some optimizations to happen as long as the control flow remains 2963unmodified. 2964 2965 .. code-block:: c 2966 2967 for (int i=0; i<4; i++) 2968 my_sub_group_shuffle() 2969 2970can be modified to: 2971 2972 .. code-block:: c 2973 2974 my_sub_group_shuffle(); 2975 my_sub_group_shuffle(); 2976 my_sub_group_shuffle(); 2977 my_sub_group_shuffle(); 2978 2979while using ``noduplicate`` would disallow this. Also ``noduplicate`` doesn't 2980have the same safe semantics of CFG as ``convergent`` and can cause changes in 2981CFG that modify semantics of the original program. 2982 2983``noduplicate`` is kept for backwards compatibility only and it considered to be 2984deprecated for future uses. 2985 2986.. _opencl_addrsp: 2987 2988address_space 2989^^^^^^^^^^^^^ 2990 2991Clang has arbitrary address space support using the ``address_space(N)`` 2992attribute, where ``N`` is an integer number in the range ``0`` to ``16777215`` 2993(``0xffffffu``). 2994 2995An OpenCL implementation provides a list of standard address spaces using 2996keywords: ``private``, ``local``, ``global``, and ``generic``. In the AST and 2997in the IR local, global, or generic will be represented by the address space 2998attribute with the corresponding unique number. Note that private does not have 2999any corresponding attribute added and, therefore, is represented by the absence 3000of an address space number. The specific IDs for an address space do not have to 3001match between the AST and the IR. Typically in the AST address space numbers 3002represent logical segments while in the IR they represent physical segments. 3003Therefore, machines with flat memory segments can map all AST address space 3004numbers to the same physical segment ID or skip address space attribute 3005completely while generating the IR. However, if the address space information 3006is needed by the IR passes e.g. to improve alias analysis, it is recommended 3007to keep it and only lower to reflect physical memory segments in the late 3008machine passes. 3009 3010OpenCL builtins 3011--------------- 3012 3013There are some standard OpenCL functions that are implemented as Clang builtins: 3014 3015- All pipe functions from `section 6.13.16.2/6.13.16.3 3016 <https://www.khronos.org/registry/cl/specs/opencl-2.0-openclc.pdf#160>`_ of 3017 the OpenCL v2.0 kernel language specification. ` 3018 3019- Address space qualifier conversion functions ``to_global``/``to_local``/``to_private`` 3020 from `section 6.13.9 3021 <https://www.khronos.org/registry/cl/specs/opencl-2.0-openclc.pdf#101>`_. 3022 3023- All the ``enqueue_kernel`` functions from `section 6.13.17.1 3024 <https://www.khronos.org/registry/cl/specs/opencl-2.0-openclc.pdf#164>`_ and 3025 enqueue query functions from `section 6.13.17.5 3026 <https://www.khronos.org/registry/cl/specs/opencl-2.0-openclc.pdf#171>`_. 3027 3028.. _cxx_for_opencl: 3029 3030C++ for OpenCL 3031-------------- 3032 3033Starting from clang 9 kernel code can contain C++17 features: classes, templates, 3034function overloading, type deduction, etc. Please note that this is not an 3035implementation of `OpenCL C++ 3036<https://www.khronos.org/registry/OpenCL/specs/2.2/pdf/OpenCL_Cxx.pdf>`_ and 3037there is no plan to support it in clang in any new releases in the near future. 3038 3039For detailed information about this language refer to `The C++ for OpenCL 3040Programming Language Documentation 3041<https://github.com/KhronosGroup/Khronosdotorg/blob/master/api/opencl/assets/CXX_for_OpenCL.pdf>`_. 3042 3043Since C++ features are to be used on top of OpenCL C functionality, all existing 3044restrictions from OpenCL C v2.0 will inherently apply. All OpenCL C builtin types 3045and function libraries are supported and can be used in this mode. 3046 3047To enable the C++ for OpenCL mode, pass one of following command line options when 3048compiling ``.cl`` file ``-cl-std=clc++``, ``-cl-std=CLC++``, ``-std=clc++`` or 3049``-std=CLC++``. 3050 3051 .. code-block:: c++ 3052 3053 template<class T> T add( T x, T y ) 3054 { 3055 return x + y; 3056 } 3057 3058 __kernel void test( __global float* a, __global float* b) 3059 { 3060 auto index = get_global_id(0); 3061 a[index] = add(b[index], b[index+1]); 3062 } 3063 3064 3065 .. code-block:: console 3066 3067 clang -cl-std=clc++ test.cl 3068 3069Constructing and destroying global objects 3070^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 3071 3072Global objects must be constructed before the first kernel using the global objects 3073is executed and destroyed just after the last kernel using the program objects is 3074executed. In OpenCL v2.0 drivers there is no specific API for invoking global 3075constructors. However, an easy workaround would be to enqueue a constructor 3076initialization kernel that has a name ``@_GLOBAL__sub_I_<compiled file name>``. 3077This kernel is only present if there are any global objects to be initialized in 3078the compiled binary. One way to check this is by passing ``CL_PROGRAM_KERNEL_NAMES`` 3079to ``clGetProgramInfo`` (OpenCL v2.0 s5.8.7). 3080 3081Note that if multiple files are compiled and linked into libraries, multiple kernels 3082that initialize global objects for multiple modules would have to be invoked. 3083 3084Applications are currently required to run initialization of global objects manually 3085before running any kernels in which the objects are used. 3086 3087 .. code-block:: console 3088 3089 clang -cl-std=clc++ test.cl 3090 3091If there are any global objects to be initialized, the final binary will contain 3092the ``@_GLOBAL__sub_I_test.cl`` kernel to be enqueued. 3093 3094Global destructors can not be invoked in OpenCL v2.0 drivers. However, all memory used 3095for program scope objects is released on ``clReleaseProgram``. 3096 3097 3098.. _target_features: 3099 3100Target-Specific Features and Limitations 3101======================================== 3102 3103CPU Architectures Features and Limitations 3104------------------------------------------ 3105 3106X86 3107^^^ 3108 3109The support for X86 (both 32-bit and 64-bit) is considered stable on 3110Darwin (macOS), Linux, FreeBSD, and Dragonfly BSD: it has been tested 3111to correctly compile many large C, C++, Objective-C, and Objective-C++ 3112codebases. 3113 3114On ``x86_64-mingw32``, passing i128(by value) is incompatible with the 3115Microsoft x64 calling convention. You might need to tweak 3116``WinX86_64ABIInfo::classify()`` in lib/CodeGen/TargetInfo.cpp. 3117 3118For the X86 target, clang supports the `-m16` command line 3119argument which enables 16-bit code output. This is broadly similar to 3120using ``asm(".code16gcc")`` with the GNU toolchain. The generated code 3121and the ABI remains 32-bit but the assembler emits instructions 3122appropriate for a CPU running in 16-bit mode, with address-size and 3123operand-size prefixes to enable 32-bit addressing and operations. 3124 3125ARM 3126^^^ 3127 3128The support for ARM (specifically ARMv6 and ARMv7) is considered stable 3129on Darwin (iOS): it has been tested to correctly compile many large C, 3130C++, Objective-C, and Objective-C++ codebases. Clang only supports a 3131limited number of ARM architectures. It does not yet fully support 3132ARMv5, for example. 3133 3134PowerPC 3135^^^^^^^ 3136 3137The support for PowerPC (especially PowerPC64) is considered stable 3138on Linux and FreeBSD: it has been tested to correctly compile many 3139large C and C++ codebases. PowerPC (32bit) is still missing certain 3140features (e.g. PIC code on ELF platforms). 3141 3142Other platforms 3143^^^^^^^^^^^^^^^ 3144 3145clang currently contains some support for other architectures (e.g. Sparc); 3146however, significant pieces of code generation are still missing, and they 3147haven't undergone significant testing. 3148 3149clang contains limited support for the MSP430 embedded processor, but 3150both the clang support and the LLVM backend support are highly 3151experimental. 3152 3153Other platforms are completely unsupported at the moment. Adding the 3154minimal support needed for parsing and semantic analysis on a new 3155platform is quite easy; see ``lib/Basic/Targets.cpp`` in the clang source 3156tree. This level of support is also sufficient for conversion to LLVM IR 3157for simple programs. Proper support for conversion to LLVM IR requires 3158adding code to ``lib/CodeGen/CGCall.cpp`` at the moment; this is likely to 3159change soon, though. Generating assembly requires a suitable LLVM 3160backend. 3161 3162Operating System Features and Limitations 3163----------------------------------------- 3164 3165Darwin (macOS) 3166^^^^^^^^^^^^^^ 3167 3168Thread Sanitizer is not supported. 3169 3170Windows 3171^^^^^^^ 3172 3173Clang has experimental support for targeting "Cygming" (Cygwin / MinGW) 3174platforms. 3175 3176See also :ref:`Microsoft Extensions <c_ms>`. 3177 3178Cygwin 3179"""""" 3180 3181Clang works on Cygwin-1.7. 3182 3183MinGW32 3184""""""" 3185 3186Clang works on some mingw32 distributions. Clang assumes directories as 3187below; 3188 3189- ``C:/mingw/include`` 3190- ``C:/mingw/lib`` 3191- ``C:/mingw/lib/gcc/mingw32/4.[3-5].0/include/c++`` 3192 3193On MSYS, a few tests might fail. 3194 3195MinGW-w64 3196""""""""" 3197 3198For 32-bit (i686-w64-mingw32), and 64-bit (x86\_64-w64-mingw32), Clang 3199assumes as below; 3200 3201- ``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)`` 3202- ``some_directory/bin/gcc.exe`` 3203- ``some_directory/bin/clang.exe`` 3204- ``some_directory/bin/clang++.exe`` 3205- ``some_directory/bin/../include/c++/GCC_version`` 3206- ``some_directory/bin/../include/c++/GCC_version/x86_64-w64-mingw32`` 3207- ``some_directory/bin/../include/c++/GCC_version/i686-w64-mingw32`` 3208- ``some_directory/bin/../include/c++/GCC_version/backward`` 3209- ``some_directory/bin/../x86_64-w64-mingw32/include`` 3210- ``some_directory/bin/../i686-w64-mingw32/include`` 3211- ``some_directory/bin/../include`` 3212 3213This directory layout is standard for any toolchain you will find on the 3214official `MinGW-w64 website <http://mingw-w64.sourceforge.net>`_. 3215 3216Clang expects the GCC executable "gcc.exe" compiled for 3217``i686-w64-mingw32`` (or ``x86_64-w64-mingw32``) to be present on PATH. 3218 3219`Some tests might fail <https://bugs.llvm.org/show_bug.cgi?id=9072>`_ on 3220``x86_64-w64-mingw32``. 3221 3222.. _clang-cl: 3223 3224clang-cl 3225======== 3226 3227clang-cl is an alternative command-line interface to Clang, designed for 3228compatibility with the Visual C++ compiler, cl.exe. 3229 3230To enable clang-cl to find system headers, libraries, and the linker when run 3231from the command-line, it should be executed inside a Visual Studio Native Tools 3232Command Prompt or a regular Command Prompt where the environment has been set 3233up using e.g. `vcvarsall.bat <https://msdn.microsoft.com/en-us/library/f2ccy3wt.aspx>`_. 3234 3235clang-cl can also be used from inside Visual Studio by selecting the LLVM 3236Platform Toolset. The toolset is not part of the installer, but may be installed 3237separately from the 3238`Visual Studio Marketplace <https://marketplace.visualstudio.com/items?itemName=LLVMExtensions.llvm-toolchain>`_. 3239To use the toolset, select a project in Solution Explorer, open its Property 3240Page (Alt+F7), and in the "General" section of "Configuration Properties" 3241change "Platform Toolset" to LLVM. Doing so enables an additional Property 3242Page for selecting the clang-cl executable to use for builds. 3243 3244To use the toolset with MSBuild directly, invoke it with e.g. 3245``/p:PlatformToolset=LLVM``. This allows trying out the clang-cl toolchain 3246without modifying your project files. 3247 3248It's also possible to point MSBuild at clang-cl without changing toolset by 3249passing ``/p:CLToolPath=c:\llvm\bin /p:CLToolExe=clang-cl.exe``. 3250 3251When using CMake and the Visual Studio generators, the toolset can be set with the ``-T`` flag: 3252 3253 :: 3254 3255 cmake -G"Visual Studio 15 2017" -T LLVM .. 3256 3257When using CMake with the Ninja generator, set the ``CMAKE_C_COMPILER`` and 3258``CMAKE_CXX_COMPILER`` variables to clang-cl: 3259 3260 :: 3261 3262 cmake -GNinja -DCMAKE_C_COMPILER="c:/Program Files (x86)/LLVM/bin/clang-cl.exe" 3263 -DCMAKE_CXX_COMPILER="c:/Program Files (x86)/LLVM/bin/clang-cl.exe" .. 3264 3265 3266Command-Line Options 3267-------------------- 3268 3269To be compatible with cl.exe, clang-cl supports most of the same command-line 3270options. Those options can start with either ``/`` or ``-``. It also supports 3271some of Clang's core options, such as the ``-W`` options. 3272 3273Options that are known to clang-cl, but not currently supported, are ignored 3274with a warning. For example: 3275 3276 :: 3277 3278 clang-cl.exe: warning: argument unused during compilation: '/AI' 3279 3280To suppress warnings about unused arguments, use the ``-Qunused-arguments`` option. 3281 3282Options that are not known to clang-cl will be ignored by default. Use the 3283``-Werror=unknown-argument`` option in order to treat them as errors. If these 3284options are spelled with a leading ``/``, they will be mistaken for a filename: 3285 3286 :: 3287 3288 clang-cl.exe: error: no such file or directory: '/foobar' 3289 3290Please `file a bug <https://bugs.llvm.org/enter_bug.cgi?product=clang&component=Driver>`_ 3291for any valid cl.exe flags that clang-cl does not understand. 3292 3293Execute ``clang-cl /?`` to see a list of supported options: 3294 3295 :: 3296 3297 CL.EXE COMPATIBILITY OPTIONS: 3298 /? Display available options 3299 /arch:<value> Set architecture for code generation 3300 /Brepro- Write current time into COFF output (default) 3301 /Brepro Do not write current time into COFF output (breaks link.exe /incremental) 3302 /clang:<arg> Pass <arg> to the clang driver 3303 /C Do not discard comments when preprocessing 3304 /c Compile only 3305 /d1PP Retain macro definitions in /E mode 3306 /d1reportAllClassLayout Dump record layout information 3307 /diagnostics:caret Enable caret and column diagnostics (default) 3308 /diagnostics:classic Disable column and caret diagnostics 3309 /diagnostics:column Disable caret diagnostics but keep column info 3310 /D <macro[=value]> Define macro 3311 /EH<value> Set exception handling model 3312 /EP Disable linemarker output and preprocess to stdout 3313 /execution-charset:<value> 3314 Set runtime encoding, supports only UTF-8 3315 /E Preprocess to stdout 3316 /fallback Fall back to cl.exe if clang-cl fails to compile 3317 /FA Output assembly code file during compilation 3318 /Fa<file or dir/> Set assembly output file name (with /FA) 3319 /Fe<file or dir/> Set output executable file name 3320 /FI <value> Include file before parsing 3321 /Fi<file> Set preprocess output file name (with /P) 3322 /Fo<file or dir/> Set output object file (with /c) 3323 /fp:except- 3324 /fp:except 3325 /fp:fast 3326 /fp:precise 3327 /fp:strict 3328 /Fp<file> Set pch file name (with /Yc and /Yu) 3329 /GA Assume thread-local variables are defined in the executable 3330 /Gd Set __cdecl as a default calling convention 3331 /GF- Disable string pooling 3332 /GF Enable string pooling (default) 3333 /GR- Do not emit RTTI data 3334 /Gregcall Set __regcall as a default calling convention 3335 /GR Emit RTTI data (default) 3336 /Gr Set __fastcall as a default calling convention 3337 /GS- Disable buffer security check 3338 /GS Enable buffer security check (default) 3339 /Gs Use stack probes (default) 3340 /Gs<value> Set stack probe size (default 4096) 3341 /guard:<value> Enable Control Flow Guard with /guard:cf, or only the table with /guard:cf,nochecks 3342 /Gv Set __vectorcall as a default calling convention 3343 /Gw- Do not put each data item in its own section (default) 3344 /Gw Put each data item in its own section 3345 /GX- Deprecated (like not passing /EH) 3346 /GX Deprecated; use /EHsc 3347 /Gy- Do not put each function in its own section (default) 3348 /Gy Put each function in its own section 3349 /Gz Set __stdcall as a default calling convention 3350 /help Display available options 3351 /imsvc <dir> Add <dir> to system include search path, as if in %INCLUDE% 3352 /I <dir> Add directory to include search path 3353 /J Make char type unsigned 3354 /LDd Create debug DLL 3355 /LD Create DLL 3356 /link <options> Forward options to the linker 3357 /MDd Use DLL debug run-time 3358 /MD Use DLL run-time 3359 /MTd Use static debug run-time 3360 /MT Use static run-time 3361 /O1 Optimize for size (like /Og /Os /Oy /Ob2 /GF /Gy) 3362 /O2 Optimize for speed (like /Og /Oi /Ot /Oy /Ob2 /GF /Gy) 3363 /Ob0 Disable function inlining 3364 /Ob1 Only inline functions explicitly or implicitly marked inline 3365 /Ob2 Inline functions as deemed beneficial by the compiler 3366 /Od Disable optimization 3367 /Og No effect 3368 /Oi- Disable use of builtin functions 3369 /Oi Enable use of builtin functions 3370 /openmp- Disable OpenMP support 3371 /openmp:experimental Enable OpenMP support with experimental SIMD support 3372 /openmp Enable OpenMP support 3373 /Os Optimize for size 3374 /Ot Optimize for speed 3375 /Ox Deprecated (like /Og /Oi /Ot /Oy /Ob2); use /O2 3376 /Oy- Disable frame pointer omission (x86 only, default) 3377 /Oy Enable frame pointer omission (x86 only) 3378 /O<flags> Set multiple /O flags at once; e.g. '/O2y-' for '/O2 /Oy-' 3379 /o <file or dir/> Deprecated (set output file name); use /Fe or /Fe 3380 /P Preprocess to file 3381 /Qvec- Disable the loop vectorization passes 3382 /Qvec Enable the loop vectorization passes 3383 /showFilenames- Do not print the name of each compiled file (default) 3384 /showFilenames Print the name of each compiled file 3385 /showIncludes Print info about included files to stderr 3386 /source-charset:<value> Set source encoding, supports only UTF-8 3387 /std:<value> Set C++ version (c++14,c++17,c++latest) 3388 /TC Treat all source files as C 3389 /Tc <file> Treat <file> as C source file 3390 /TP Treat all source files as C++ 3391 /Tp <file> Treat <file> as C++ source file 3392 /utf-8 Set source and runtime encoding to UTF-8 (default) 3393 /U <macro> Undefine macro 3394 /vd<value> Control vtordisp placement 3395 /vmb Use a best-case representation method for member pointers 3396 /vmg Use a most-general representation for member pointers 3397 /vmm Set the default most-general representation to multiple inheritance 3398 /vms Set the default most-general representation to single inheritance 3399 /vmv Set the default most-general representation to virtual inheritance 3400 /volatile:iso Volatile loads and stores have standard semantics 3401 /volatile:ms Volatile loads and stores have acquire and release semantics 3402 /W0 Disable all warnings 3403 /W1 Enable -Wall 3404 /W2 Enable -Wall 3405 /W3 Enable -Wall 3406 /W4 Enable -Wall and -Wextra 3407 /Wall Enable -Weverything 3408 /WX- Do not treat warnings as errors (default) 3409 /WX Treat warnings as errors 3410 /w Disable all warnings 3411 /X Do not add %INCLUDE% to include search path 3412 /Y- Disable precompiled headers, overrides /Yc and /Yu 3413 /Yc<filename> Generate a pch file for all code up to and including <filename> 3414 /Yu<filename> Load a pch file and use it instead of all code up to and including <filename> 3415 /Z7 Enable CodeView debug information in object files 3416 /Zc:alignedNew- Disable C++17 aligned allocation functions 3417 /Zc:alignedNew Enable C++17 aligned allocation functions 3418 /Zc:char8_t- Disable char8_t from c++2a 3419 /Zc:char8_t Enable char8_t from C++2a 3420 /Zc:dllexportInlines- Do not dllexport/dllimport inline member functions of dllexport/import classes 3421 /Zc:dllexportInlines dllexport/dllimport inline member functions of dllexport/import classes (default) 3422 /Zc:sizedDealloc- Disable C++14 sized global deallocation functions 3423 /Zc:sizedDealloc Enable C++14 sized global deallocation functions 3424 /Zc:strictStrings Treat string literals as const 3425 /Zc:threadSafeInit- Disable thread-safe initialization of static variables 3426 /Zc:threadSafeInit Enable thread-safe initialization of static variables 3427 /Zc:trigraphs- Disable trigraphs (default) 3428 /Zc:trigraphs Enable trigraphs 3429 /Zc:twoPhase- Disable two-phase name lookup in templates (default) 3430 /Zc:twoPhase Enable two-phase name lookup in templates 3431 /Zd Emit debug line number tables only 3432 /Zi Like /Z7 3433 /Zl Do not let object file auto-link default libraries 3434 /Zp Set default maximum struct packing alignment to 1 3435 /Zp<value> Set default maximum struct packing alignment 3436 /Zs Syntax-check only 3437 3438 OPTIONS: 3439 -### Print (but do not run) the commands to run for this compilation 3440 --analyze Run the static analyzer 3441 -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang 3442 Trivial automatic variable initialization to zero is only here for benchmarks, it'll 3443 eventually be removed, and I'm OK with that because I'm only using it to benchmark 3444 -faddrsig Emit an address-significance table 3445 -fansi-escape-codes Use ANSI escape codes for diagnostics 3446 -fblocks Enable the 'blocks' language feature 3447 -fcf-protection=<value> Instrument control-flow architecture protection. Options: return, branch, full, none. 3448 -fcf-protection Enable cf-protection in 'full' mode 3449 -fcolor-diagnostics Use colors in diagnostics 3450 -fcomplete-member-pointers 3451 Require member pointer base types to be complete if they would be significant under the Microsoft ABI 3452 -fcoverage-mapping Generate coverage mapping to enable code coverage analysis 3453 -fcs-profile-generate=<directory> 3454 Generate instrumented code to collect context sensitive execution counts into 3455 <directory>/default.profraw (overridden by LLVM_PROFILE_FILE env var) 3456 -fcs-profile-generate Generate instrumented code to collect context sensitive execution counts into 3457 default.profraw (overridden by LLVM_PROFILE_FILE env var) 3458 -fdebug-compilation-dir <value> 3459 The compilation directory to embed in the debug info. 3460 -fdebug-macro Emit macro debug information 3461 -fdelayed-template-parsing 3462 Parse templated function definitions at the end of the translation unit 3463 -fdiagnostics-absolute-paths 3464 Print absolute paths in diagnostics 3465 -fdiagnostics-parseable-fixits 3466 Print fix-its in machine parseable form 3467 -fgnuc-version=<value> Sets various macros to claim compatibility with the given GCC version (default is 4.2.1) 3468 -fintegrated-cc1 Run cc1 in-process 3469 -flto=<value> Set LTO mode to either 'full' or 'thin' 3470 -flto Enable LTO in 'full' mode 3471 -fmerge-all-constants Allow merging of constants 3472 -fms-compatibility-version=<value> 3473 Dot-separated value representing the Microsoft compiler version number to report in 3474 _MSC_VER (0 = don't define it (default)) 3475 -fms-compatibility Enable full Microsoft Visual C++ compatibility 3476 -fms-extensions Accept some non-standard constructs supported by the Microsoft compiler 3477 -fmsc-version=<value> Microsoft compiler version number to report in _MSC_VER (0 = don't define it (default)) 3478 -fno-addrsig Don't emit an address-significance table 3479 -fno-builtin-<value> Disable implicit builtin knowledge of a specific function 3480 -fno-builtin Disable implicit builtin knowledge of functions 3481 -fno-complete-member-pointers 3482 Do not require member pointer base types to be complete if they would be significant under the Microsoft ABI 3483 -fno-coverage-mapping Disable code coverage analysis 3484 -fno-crash-diagnostics Disable auto-generation of preprocessed source files and a script for reproduction during a clang crash 3485 -fno-debug-macro Do not emit macro debug information 3486 -fno-delayed-template-parsing 3487 Disable delayed template parsing 3488 -fno-integrated-cc1 Spawn a separate process for each cc1 3489 -fno-profile-generate Disable generation of profile instrumentation. 3490 -fno-profile-instr-generate 3491 Disable generation of profile instrumentation. 3492 -fno-profile-instr-use Disable using instrumentation data for profile-guided optimization 3493 -fno-sanitize-address-poison-custom-array-cookie 3494 Disable poisoning array cookies when using custom operator new[] in AddressSanitizer 3495 -fno-sanitize-address-use-after-scope 3496 Disable use-after-scope detection in AddressSanitizer 3497 -fno-sanitize-address-use-odr-indicator 3498 Disable ODR indicator globals 3499 -fno-sanitize-blacklist Don't use blacklist file for sanitizers 3500 -fno-sanitize-cfi-canonical-jump-tables 3501 Do not make the jump table addresses canonical in the symbol table 3502 -fno-sanitize-cfi-cross-dso 3503 Disable control flow integrity (CFI) checks for cross-DSO calls. 3504 -fno-sanitize-coverage=<value> 3505 Disable specified features of coverage instrumentation for Sanitizers 3506 -fno-sanitize-memory-track-origins 3507 Disable origins tracking in MemorySanitizer 3508 -fno-sanitize-memory-use-after-dtor 3509 Disable use-after-destroy detection in MemorySanitizer 3510 -fno-sanitize-recover=<value> 3511 Disable recovery for specified sanitizers 3512 -fno-sanitize-stats Disable sanitizer statistics gathering. 3513 -fno-sanitize-thread-atomics 3514 Disable atomic operations instrumentation in ThreadSanitizer 3515 -fno-sanitize-thread-func-entry-exit 3516 Disable function entry/exit instrumentation in ThreadSanitizer 3517 -fno-sanitize-thread-memory-access 3518 Disable memory access instrumentation in ThreadSanitizer 3519 -fno-sanitize-trap=<value> 3520 Disable trapping for specified sanitizers 3521 -fno-standalone-debug Limit debug information produced to reduce size of debug binary 3522 -fno-temp-file Directly create compilation output files. This may lead to incorrect incremental builds if the compiler crashes 3523 -fobjc-runtime=<value> Specify the target Objective-C runtime kind and version 3524 -forder-file-instrumentation 3525 Generate instrumented code to collect order file into default.profraw file 3526 (overridden by '=' form of option or LLVM_PROFILE_FILE env var) 3527 -fprofile-exclude-files=<value> 3528 Instrument only functions from files where names don't match all the regexes separated by a semi-colon 3529 -fprofile-filter-files=<value> 3530 Instrument only functions from files where names match any regex separated by a semi-colon 3531 -fprofile-generate=<directory> 3532 Generate instrumented code to collect execution counts into 3533 <directory>/default.profraw (overridden by LLVM_PROFILE_FILE env var) 3534 -fprofile-generate Generate instrumented code to collect execution counts into 3535 default.profraw (overridden by LLVM_PROFILE_FILE env var) 3536 -fprofile-instr-generate=<file> 3537 Generate instrumented code to collect execution counts into 3538 <file> (overridden by LLVM_PROFILE_FILE env var) 3539 -fprofile-instr-generate 3540 Generate instrumented code to collect execution counts into 3541 default.profraw file (overridden by '=' form of option or LLVM_PROFILE_FILE env var) 3542 -fprofile-instr-use=<value> 3543 Use instrumentation data for profile-guided optimization 3544 -fprofile-remapping-file=<file> 3545 Use the remappings described in <file> to match the profile data against names in the program 3546 -fsanitize-address-field-padding=<value> 3547 Level of field padding for AddressSanitizer 3548 -fsanitize-address-globals-dead-stripping 3549 Enable linker dead stripping of globals in AddressSanitizer 3550 -fsanitize-address-poison-custom-array-cookie 3551 Enable poisoning array cookies when using custom operator new[] in AddressSanitizer 3552 -fsanitize-address-use-after-scope 3553 Enable use-after-scope detection in AddressSanitizer 3554 -fsanitize-address-use-odr-indicator 3555 Enable ODR indicator globals to avoid false ODR violation reports in partially sanitized 3556 programs at the cost of an increase in binary size 3557 -fsanitize-blacklist=<value> 3558 Path to blacklist file for sanitizers 3559 -fsanitize-cfi-canonical-jump-tables 3560 Make the jump table addresses canonical in the symbol table 3561 -fsanitize-cfi-cross-dso 3562 Enable control flow integrity (CFI) checks for cross-DSO calls. 3563 -fsanitize-cfi-icall-generalize-pointers 3564 Generalize pointers in CFI indirect call type signature checks 3565 -fsanitize-coverage=<value> 3566 Specify the type of coverage instrumentation for Sanitizers 3567 -fsanitize-hwaddress-abi=<value> 3568 Select the HWAddressSanitizer ABI to target (interceptor or platform, 3569 default interceptor). This option is currently unused. 3570 -fsanitize-memory-track-origins=<value> 3571 Enable origins tracking in MemorySanitizer 3572 -fsanitize-memory-track-origins 3573 Enable origins tracking in MemorySanitizer 3574 -fsanitize-memory-use-after-dtor 3575 Enable use-after-destroy detection in MemorySanitizer 3576 -fsanitize-recover=<value> 3577 Enable recovery for specified sanitizers 3578 -fsanitize-stats Enable sanitizer statistics gathering. 3579 -fsanitize-system-blacklist=<value> 3580 Path to system blacklist file for sanitizers 3581 -fsanitize-thread-atomics 3582 Enable atomic operations instrumentation in ThreadSanitizer (default) 3583 -fsanitize-thread-func-entry-exit 3584 Enable function entry/exit instrumentation in ThreadSanitizer (default) 3585 -fsanitize-thread-memory-access 3586 Enable memory access instrumentation in ThreadSanitizer (default) 3587 -fsanitize-trap=<value> Enable trapping for specified sanitizers 3588 -fsanitize-undefined-strip-path-components=<number> 3589 Strip (or keep only, if negative) a given number of path components when emitting check metadata. 3590 -fsanitize=<check> Turn on runtime checks for various forms of undefined or suspicious behavior. See user manual for available checks 3591 -fsplit-lto-unit Enables splitting of the LTO unit. 3592 -fstandalone-debug Emit full debug info for all types used by the program 3593 -fthin-link-bitcode=<value> 3594 Write minimized bitcode to <file> for the ThinLTO thin link only 3595 -fthinlto-index=<value> Perform ThinLTO importing using provided function summary index 3596 -ftime-trace-granularity=<value> 3597 Minimum time granularity (in microseconds) traced by time profiler 3598 -ftime-trace Turn on time profiler. Generates JSON file based on output filename. 3599 -ftrivial-auto-var-init=<value> 3600 Initialize trivial automatic stack variables: uninitialized (default) | pattern 3601 -fvirtual-function-elimination 3602 Enables dead virtual function elimination optimization. Requires -flto=full 3603 -fwhole-program-vtables Enables whole-program vtable optimization. Requires -flto 3604 -gcodeview-ghash Emit type record hashes in a .debug$H section 3605 -gcodeview Generate CodeView debug information 3606 -gdwarf Generate source-level debug information with the default dwarf version 3607 -gline-directives-only Emit debug line info directives only 3608 -gline-tables-only Emit debug line number tables only 3609 -gno-inline-line-tables Don't emit inline line tables 3610 -miamcu Use Intel MCU ABI 3611 -mllvm <value> Additional arguments to forward to LLVM's option processing 3612 -nobuiltininc Disable builtin #include directories 3613 -print-supported-cpus Print supported cpu models for the given target 3614 (if target is not specified, it will print the supported cpus for the default target) 3615 -Qunused-arguments Don't emit warning for unused driver arguments 3616 -R<remark> Enable the specified remark 3617 --target=<value> Generate code for the given target 3618 --version Print version information 3619 -v Show commands to run and use verbose output 3620 -W<warning> Enable the specified warning 3621 -Xclang <arg> Pass <arg> to the clang compiler 3622 3623The /clang: Option 3624^^^^^^^^^^^^^^^^^^ 3625 3626When clang-cl is run with a set of ``/clang:<arg>`` options, it will gather all 3627of the ``<arg>`` arguments and process them as if they were passed to the clang 3628driver. This mechanism allows you to pass flags that are not exposed in the 3629clang-cl options or flags that have a different meaning when passed to the clang 3630driver. Regardless of where they appear in the command line, the ``/clang:`` 3631arguments are treated as if they were passed at the end of the clang-cl command 3632line. 3633 3634The /Zc:dllexportInlines- Option 3635^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 3636 3637This causes the class-level `dllexport` and `dllimport` attributes to not apply 3638to inline member functions, as they otherwise would. For example, in the code 3639below `S::foo()` would normally be defined and exported by the DLL, but when 3640using the ``/Zc:dllexportInlines-`` flag it is not: 3641 3642.. code-block:: c 3643 3644 struct __declspec(dllexport) S { 3645 void foo() {} 3646 } 3647 3648This has the benefit that the compiler doesn't need to emit a definition of 3649`S::foo()` in every translation unit where the declaration is included, as it 3650would otherwise do to ensure there's a definition in the DLL even if it's not 3651used there. If the declaration occurs in a header file that's widely used, this 3652can save significant compilation time and output size. It also reduces the 3653number of functions exported by the DLL similarly to what 3654``-fvisibility-inlines-hidden`` does for shared objects on ELF and Mach-O. 3655Since the function declaration comes with an inline definition, users of the 3656library can use that definition directly instead of importing it from the DLL. 3657 3658Note that the Microsoft Visual C++ compiler does not support this option, and 3659if code in a DLL is compiled with ``/Zc:dllexportInlines-``, the code using the 3660DLL must be compiled in the same way so that it doesn't attempt to dllimport 3661the inline member functions. The reverse scenario should generally work though: 3662a DLL compiled without this flag (such as a system library compiled with Visual 3663C++) can be referenced from code compiled using the flag, meaning that the 3664referencing code will use the inline definitions instead of importing them from 3665the DLL. 3666 3667Also note that like when using ``-fvisibility-inlines-hidden``, the address of 3668`S::foo()` will be different inside and outside the DLL, breaking the C/C++ 3669standard requirement that functions have a unique address. 3670 3671The flag does not apply to explicit class template instantiation definitions or 3672declarations, as those are typically used to explicitly provide a single 3673definition in a DLL, (dllexported instantiation definition) or to signal that 3674the definition is available elsewhere (dllimport instantiation declaration). It 3675also doesn't apply to inline members with static local variables, to ensure 3676that the same instance of the variable is used inside and outside the DLL. 3677 3678Using this flag can cause problems when inline functions that would otherwise 3679be dllexported refer to internal symbols of a DLL. For example: 3680 3681.. code-block:: c 3682 3683 void internal(); 3684 3685 struct __declspec(dllimport) S { 3686 void foo() { internal(); } 3687 } 3688 3689Normally, references to `S::foo()` would use the definition in the DLL from 3690which it was exported, and which presumably also has the definition of 3691`internal()`. However, when using ``/Zc:dllexportInlines-``, the inline 3692definition of `S::foo()` is used directly, resulting in a link error since 3693`internal()` is not available. Even worse, if there is an inline definition of 3694`internal()` containing a static local variable, we will now refer to a 3695different instance of that variable than in the DLL: 3696 3697.. code-block:: c 3698 3699 inline int internal() { static int x; return x++; } 3700 3701 struct __declspec(dllimport) S { 3702 int foo() { return internal(); } 3703 } 3704 3705This could lead to very subtle bugs. Using ``-fvisibility-inlines-hidden`` can 3706lead to the same issue. To avoid it in this case, make `S::foo()` or 3707`internal()` non-inline, or mark them `dllimport/dllexport` explicitly. 3708 3709The /fallback Option 3710^^^^^^^^^^^^^^^^^^^^ 3711 3712When clang-cl is run with the ``/fallback`` option, it will first try to 3713compile files itself. For any file that it fails to compile, it will fall back 3714and try to compile the file by invoking cl.exe. 3715 3716This option is intended to be used as a temporary means to build projects where 3717clang-cl cannot successfully compile all the files. clang-cl may fail to compile 3718a file either because it cannot generate code for some C++ feature, or because 3719it cannot parse some Microsoft language extension. 3720