1*67e74705SXin Li============================ 2*67e74705SXin LiClang Compiler User's Manual 3*67e74705SXin Li============================ 4*67e74705SXin Li 5*67e74705SXin Li.. contents:: 6*67e74705SXin Li :local: 7*67e74705SXin Li 8*67e74705SXin LiIntroduction 9*67e74705SXin Li============ 10*67e74705SXin Li 11*67e74705SXin LiThe Clang Compiler is an open-source compiler for the C family of 12*67e74705SXin Liprogramming languages, aiming to be the best in class implementation of 13*67e74705SXin Lithese languages. Clang builds on the LLVM optimizer and code generator, 14*67e74705SXin Liallowing it to provide high-quality optimization and code generation 15*67e74705SXin Lisupport for many targets. For more general information, please see the 16*67e74705SXin Li`Clang Web Site <http://clang.llvm.org>`_ or the `LLVM Web 17*67e74705SXin LiSite <http://llvm.org>`_. 18*67e74705SXin Li 19*67e74705SXin LiThis document describes important notes about using Clang as a compiler 20*67e74705SXin Lifor an end-user, documenting the supported features, command line 21*67e74705SXin Lioptions, etc. If you are interested in using Clang to build a tool that 22*67e74705SXin Liprocesses code, please see :doc:`InternalsManual`. If you are interested in the 23*67e74705SXin Li`Clang Static Analyzer <http://clang-analyzer.llvm.org>`_, please see its web 24*67e74705SXin Lipage. 25*67e74705SXin Li 26*67e74705SXin LiClang is designed to support the C family of programming languages, 27*67e74705SXin Liwhich includes :ref:`C <c>`, :ref:`Objective-C <objc>`, :ref:`C++ <cxx>`, and 28*67e74705SXin Li:ref:`Objective-C++ <objcxx>` as well as many dialects of those. For 29*67e74705SXin Lilanguage-specific information, please see the corresponding language 30*67e74705SXin Lispecific section: 31*67e74705SXin Li 32*67e74705SXin Li- :ref:`C Language <c>`: K&R C, ANSI C89, ISO C90, ISO C94 (C89+AMD1), ISO 33*67e74705SXin Li C99 (+TC1, TC2, TC3). 34*67e74705SXin Li- :ref:`Objective-C Language <objc>`: ObjC 1, ObjC 2, ObjC 2.1, plus 35*67e74705SXin Li variants depending on base language. 36*67e74705SXin Li- :ref:`C++ Language <cxx>` 37*67e74705SXin Li- :ref:`Objective C++ Language <objcxx>` 38*67e74705SXin Li 39*67e74705SXin LiIn addition to these base languages and their dialects, Clang supports a 40*67e74705SXin Libroad variety of language extensions, which are documented in the 41*67e74705SXin Licorresponding language section. These extensions are provided to be 42*67e74705SXin Licompatible with the GCC, Microsoft, and other popular compilers as well 43*67e74705SXin Lias to improve functionality through Clang-specific features. The Clang 44*67e74705SXin Lidriver and language features are intentionally designed to be as 45*67e74705SXin Licompatible with the GNU GCC compiler as reasonably possible, easing 46*67e74705SXin Limigration from GCC to Clang. In most cases, code "just works". 47*67e74705SXin LiClang also provides an alternative driver, :ref:`clang-cl`, that is designed 48*67e74705SXin Lito be compatible with the Visual C++ compiler, cl.exe. 49*67e74705SXin Li 50*67e74705SXin LiIn addition to language specific features, Clang has a variety of 51*67e74705SXin Lifeatures that depend on what CPU architecture or operating system is 52*67e74705SXin Libeing compiled for. Please see the :ref:`Target-Specific Features and 53*67e74705SXin LiLimitations <target_features>` section for more details. 54*67e74705SXin Li 55*67e74705SXin LiThe rest of the introduction introduces some basic :ref:`compiler 56*67e74705SXin Literminology <terminology>` that is used throughout this manual and 57*67e74705SXin Licontains a basic :ref:`introduction to using Clang <basicusage>` as a 58*67e74705SXin Licommand line compiler. 59*67e74705SXin Li 60*67e74705SXin Li.. _terminology: 61*67e74705SXin Li 62*67e74705SXin LiTerminology 63*67e74705SXin Li----------- 64*67e74705SXin Li 65*67e74705SXin LiFront end, parser, backend, preprocessor, undefined behavior, 66*67e74705SXin Lidiagnostic, optimizer 67*67e74705SXin Li 68*67e74705SXin Li.. _basicusage: 69*67e74705SXin Li 70*67e74705SXin LiBasic Usage 71*67e74705SXin Li----------- 72*67e74705SXin Li 73*67e74705SXin LiIntro to how to use a C compiler for newbies. 74*67e74705SXin Li 75*67e74705SXin Licompile + link compile then link debug info enabling optimizations 76*67e74705SXin Lipicking a language to use, defaults to C11 by default. Autosenses based 77*67e74705SXin Lion extension. using a makefile 78*67e74705SXin Li 79*67e74705SXin LiCommand Line Options 80*67e74705SXin Li==================== 81*67e74705SXin Li 82*67e74705SXin LiThis section is generally an index into other sections. It does not go 83*67e74705SXin Liinto depth on the ones that are covered by other sections. However, the 84*67e74705SXin Lifirst part introduces the language selection and other high level 85*67e74705SXin Lioptions like :option:`-c`, :option:`-g`, etc. 86*67e74705SXin Li 87*67e74705SXin LiOptions to Control Error and Warning Messages 88*67e74705SXin Li--------------------------------------------- 89*67e74705SXin Li 90*67e74705SXin Li.. option:: -Werror 91*67e74705SXin Li 92*67e74705SXin Li Turn warnings into errors. 93*67e74705SXin Li 94*67e74705SXin Li.. This is in plain monospaced font because it generates the same label as 95*67e74705SXin Li.. -Werror, and Sphinx complains. 96*67e74705SXin Li 97*67e74705SXin Li``-Werror=foo`` 98*67e74705SXin Li 99*67e74705SXin Li Turn warning "foo" into an error. 100*67e74705SXin Li 101*67e74705SXin Li.. option:: -Wno-error=foo 102*67e74705SXin Li 103*67e74705SXin Li Turn warning "foo" into an warning even if :option:`-Werror` is specified. 104*67e74705SXin Li 105*67e74705SXin Li.. option:: -Wfoo 106*67e74705SXin Li 107*67e74705SXin Li Enable warning "foo". 108*67e74705SXin Li 109*67e74705SXin Li.. option:: -Wno-foo 110*67e74705SXin Li 111*67e74705SXin Li Disable warning "foo". 112*67e74705SXin Li 113*67e74705SXin Li.. option:: -w 114*67e74705SXin Li 115*67e74705SXin Li Disable all diagnostics. 116*67e74705SXin Li 117*67e74705SXin Li.. option:: -Weverything 118*67e74705SXin Li 119*67e74705SXin Li :ref:`Enable all diagnostics. <diagnostics_enable_everything>` 120*67e74705SXin Li 121*67e74705SXin Li.. option:: -pedantic 122*67e74705SXin Li 123*67e74705SXin Li Warn on language extensions. 124*67e74705SXin Li 125*67e74705SXin Li.. option:: -pedantic-errors 126*67e74705SXin Li 127*67e74705SXin Li Error on language extensions. 128*67e74705SXin Li 129*67e74705SXin Li.. option:: -Wsystem-headers 130*67e74705SXin Li 131*67e74705SXin Li Enable warnings from system headers. 132*67e74705SXin Li 133*67e74705SXin Li.. option:: -ferror-limit=123 134*67e74705SXin Li 135*67e74705SXin Li Stop emitting diagnostics after 123 errors have been produced. The default is 136*67e74705SXin Li 20, and the error limit can be disabled with `-ferror-limit=0`. 137*67e74705SXin Li 138*67e74705SXin Li.. option:: -ftemplate-backtrace-limit=123 139*67e74705SXin Li 140*67e74705SXin Li Only emit up to 123 template instantiation notes within the template 141*67e74705SXin Li instantiation backtrace for a single warning or error. The default is 10, and 142*67e74705SXin Li the limit can be disabled with `-ftemplate-backtrace-limit=0`. 143*67e74705SXin Li 144*67e74705SXin Li.. _cl_diag_formatting: 145*67e74705SXin Li 146*67e74705SXin LiFormatting of Diagnostics 147*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^^^^^ 148*67e74705SXin Li 149*67e74705SXin LiClang aims to produce beautiful diagnostics by default, particularly for 150*67e74705SXin Linew users that first come to Clang. However, different people have 151*67e74705SXin Lidifferent preferences, and sometimes Clang is driven not by a human, 152*67e74705SXin Libut by a program that wants consistent and easily parsable output. For 153*67e74705SXin Lithese cases, Clang provides a wide range of options to control the exact 154*67e74705SXin Lioutput format of the diagnostics that it generates. 155*67e74705SXin Li 156*67e74705SXin Li.. _opt_fshow-column: 157*67e74705SXin Li 158*67e74705SXin Li**-f[no-]show-column** 159*67e74705SXin Li Print column number in diagnostic. 160*67e74705SXin Li 161*67e74705SXin Li This option, which defaults to on, controls whether or not Clang 162*67e74705SXin Li prints the column number of a diagnostic. For example, when this is 163*67e74705SXin Li enabled, Clang will print something like: 164*67e74705SXin Li 165*67e74705SXin Li :: 166*67e74705SXin Li 167*67e74705SXin Li test.c:28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens] 168*67e74705SXin Li #endif bad 169*67e74705SXin Li ^ 170*67e74705SXin Li // 171*67e74705SXin Li 172*67e74705SXin Li When this is disabled, Clang will print "test.c:28: warning..." with 173*67e74705SXin Li no column number. 174*67e74705SXin Li 175*67e74705SXin Li The printed column numbers count bytes from the beginning of the 176*67e74705SXin Li line; take care if your source contains multibyte characters. 177*67e74705SXin Li 178*67e74705SXin Li.. _opt_fshow-source-location: 179*67e74705SXin Li 180*67e74705SXin Li**-f[no-]show-source-location** 181*67e74705SXin Li Print source file/line/column information in diagnostic. 182*67e74705SXin Li 183*67e74705SXin Li This option, which defaults to on, controls whether or not Clang 184*67e74705SXin Li prints the filename, line number and column number of a diagnostic. 185*67e74705SXin Li For example, when this is enabled, Clang will print something like: 186*67e74705SXin Li 187*67e74705SXin Li :: 188*67e74705SXin Li 189*67e74705SXin Li test.c:28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens] 190*67e74705SXin Li #endif bad 191*67e74705SXin Li ^ 192*67e74705SXin Li // 193*67e74705SXin Li 194*67e74705SXin Li When this is disabled, Clang will not print the "test.c:28:8: " 195*67e74705SXin Li part. 196*67e74705SXin Li 197*67e74705SXin Li.. _opt_fcaret-diagnostics: 198*67e74705SXin Li 199*67e74705SXin Li**-f[no-]caret-diagnostics** 200*67e74705SXin Li Print source line and ranges from source code in diagnostic. 201*67e74705SXin Li This option, which defaults to on, controls whether or not Clang 202*67e74705SXin Li prints the source line, source ranges, and caret when emitting a 203*67e74705SXin Li diagnostic. For example, when this is enabled, Clang will print 204*67e74705SXin Li something like: 205*67e74705SXin Li 206*67e74705SXin Li :: 207*67e74705SXin Li 208*67e74705SXin Li test.c:28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens] 209*67e74705SXin Li #endif bad 210*67e74705SXin Li ^ 211*67e74705SXin Li // 212*67e74705SXin Li 213*67e74705SXin Li**-f[no-]color-diagnostics** 214*67e74705SXin Li This option, which defaults to on when a color-capable terminal is 215*67e74705SXin Li detected, controls whether or not Clang prints diagnostics in color. 216*67e74705SXin Li 217*67e74705SXin Li When this option is enabled, Clang will use colors to highlight 218*67e74705SXin Li specific parts of the diagnostic, e.g., 219*67e74705SXin Li 220*67e74705SXin Li .. nasty hack to not lose our dignity 221*67e74705SXin Li 222*67e74705SXin Li .. raw:: html 223*67e74705SXin Li 224*67e74705SXin Li <pre> 225*67e74705SXin Li <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> 226*67e74705SXin Li #endif bad 227*67e74705SXin Li <span style="color:green">^</span> 228*67e74705SXin Li <span style="color:green">//</span> 229*67e74705SXin Li </pre> 230*67e74705SXin Li 231*67e74705SXin Li When this is disabled, Clang will just print: 232*67e74705SXin Li 233*67e74705SXin Li :: 234*67e74705SXin Li 235*67e74705SXin Li test.c:2:8: warning: extra tokens at end of #endif directive [-Wextra-tokens] 236*67e74705SXin Li #endif bad 237*67e74705SXin Li ^ 238*67e74705SXin Li // 239*67e74705SXin Li 240*67e74705SXin Li**-fansi-escape-codes** 241*67e74705SXin Li Controls whether ANSI escape codes are used instead of the Windows Console 242*67e74705SXin Li API to output colored diagnostics. This option is only used on Windows and 243*67e74705SXin Li defaults to off. 244*67e74705SXin Li 245*67e74705SXin Li.. option:: -fdiagnostics-format=clang/msvc/vi 246*67e74705SXin Li 247*67e74705SXin Li Changes diagnostic output format to better match IDEs and command line tools. 248*67e74705SXin Li 249*67e74705SXin Li This option controls the output format of the filename, line number, 250*67e74705SXin Li and column printed in diagnostic messages. The options, and their 251*67e74705SXin Li affect on formatting a simple conversion diagnostic, follow: 252*67e74705SXin Li 253*67e74705SXin Li **clang** (default) 254*67e74705SXin Li :: 255*67e74705SXin Li 256*67e74705SXin Li t.c:3:11: warning: conversion specifies type 'char *' but the argument has type 'int' 257*67e74705SXin Li 258*67e74705SXin Li **msvc** 259*67e74705SXin Li :: 260*67e74705SXin Li 261*67e74705SXin Li t.c(3,11) : warning: conversion specifies type 'char *' but the argument has type 'int' 262*67e74705SXin Li 263*67e74705SXin Li **vi** 264*67e74705SXin Li :: 265*67e74705SXin Li 266*67e74705SXin Li t.c +3:11: warning: conversion specifies type 'char *' but the argument has type 'int' 267*67e74705SXin Li 268*67e74705SXin Li.. _opt_fdiagnostics-show-option: 269*67e74705SXin Li 270*67e74705SXin Li**-f[no-]diagnostics-show-option** 271*67e74705SXin Li Enable ``[-Woption]`` information in diagnostic line. 272*67e74705SXin Li 273*67e74705SXin Li This option, which defaults to on, controls whether or not Clang 274*67e74705SXin Li prints the associated :ref:`warning group <cl_diag_warning_groups>` 275*67e74705SXin Li option name when outputting a warning diagnostic. For example, in 276*67e74705SXin Li this output: 277*67e74705SXin Li 278*67e74705SXin Li :: 279*67e74705SXin Li 280*67e74705SXin Li test.c:28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens] 281*67e74705SXin Li #endif bad 282*67e74705SXin Li ^ 283*67e74705SXin Li // 284*67e74705SXin Li 285*67e74705SXin Li Passing **-fno-diagnostics-show-option** will prevent Clang from 286*67e74705SXin Li printing the [:ref:`-Wextra-tokens <opt_Wextra-tokens>`] information in 287*67e74705SXin Li the diagnostic. This information tells you the flag needed to enable 288*67e74705SXin Li or disable the diagnostic, either from the command line or through 289*67e74705SXin Li :ref:`#pragma GCC diagnostic <pragma_GCC_diagnostic>`. 290*67e74705SXin Li 291*67e74705SXin Li.. _opt_fdiagnostics-show-category: 292*67e74705SXin Li 293*67e74705SXin Li.. option:: -fdiagnostics-show-category=none/id/name 294*67e74705SXin Li 295*67e74705SXin Li Enable printing category information in diagnostic line. 296*67e74705SXin Li 297*67e74705SXin Li This option, which defaults to "none", controls whether or not Clang 298*67e74705SXin Li prints the category associated with a diagnostic when emitting it. 299*67e74705SXin Li Each diagnostic may or many not have an associated category, if it 300*67e74705SXin Li has one, it is listed in the diagnostic categorization field of the 301*67e74705SXin Li diagnostic line (in the []'s). 302*67e74705SXin Li 303*67e74705SXin Li For example, a format string warning will produce these three 304*67e74705SXin Li renditions based on the setting of this option: 305*67e74705SXin Li 306*67e74705SXin Li :: 307*67e74705SXin Li 308*67e74705SXin Li t.c:3:11: warning: conversion specifies type 'char *' but the argument has type 'int' [-Wformat] 309*67e74705SXin Li t.c:3:11: warning: conversion specifies type 'char *' but the argument has type 'int' [-Wformat,1] 310*67e74705SXin Li t.c:3:11: warning: conversion specifies type 'char *' but the argument has type 'int' [-Wformat,Format String] 311*67e74705SXin Li 312*67e74705SXin Li This category can be used by clients that want to group diagnostics 313*67e74705SXin Li by category, so it should be a high level category. We want dozens 314*67e74705SXin Li of these, not hundreds or thousands of them. 315*67e74705SXin Li 316*67e74705SXin Li.. _opt_fdiagnostics-fixit-info: 317*67e74705SXin Li 318*67e74705SXin Li**-f[no-]diagnostics-fixit-info** 319*67e74705SXin Li Enable "FixIt" information in the diagnostics output. 320*67e74705SXin Li 321*67e74705SXin Li This option, which defaults to on, controls whether or not Clang 322*67e74705SXin Li prints the information on how to fix a specific diagnostic 323*67e74705SXin Li underneath it when it knows. For example, in this output: 324*67e74705SXin Li 325*67e74705SXin Li :: 326*67e74705SXin Li 327*67e74705SXin Li test.c:28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens] 328*67e74705SXin Li #endif bad 329*67e74705SXin Li ^ 330*67e74705SXin Li // 331*67e74705SXin Li 332*67e74705SXin Li Passing **-fno-diagnostics-fixit-info** will prevent Clang from 333*67e74705SXin Li printing the "//" line at the end of the message. This information 334*67e74705SXin Li is useful for users who may not understand what is wrong, but can be 335*67e74705SXin Li confusing for machine parsing. 336*67e74705SXin Li 337*67e74705SXin Li.. _opt_fdiagnostics-print-source-range-info: 338*67e74705SXin Li 339*67e74705SXin Li**-fdiagnostics-print-source-range-info** 340*67e74705SXin Li Print machine parsable information about source ranges. 341*67e74705SXin Li This option makes Clang print information about source ranges in a machine 342*67e74705SXin Li parsable format after the file/line/column number information. The 343*67e74705SXin Li information is a simple sequence of brace enclosed ranges, where each range 344*67e74705SXin Li lists the start and end line/column locations. For example, in this output: 345*67e74705SXin Li 346*67e74705SXin Li :: 347*67e74705SXin Li 348*67e74705SXin Li exprs.c:47:15:{47:8-47:14}{47:17-47:24}: error: invalid operands to binary expression ('int *' and '_Complex float') 349*67e74705SXin Li P = (P-42) + Gamma*4; 350*67e74705SXin Li ~~~~~~ ^ ~~~~~~~ 351*67e74705SXin Li 352*67e74705SXin Li The {}'s are generated by -fdiagnostics-print-source-range-info. 353*67e74705SXin Li 354*67e74705SXin Li The printed column numbers count bytes from the beginning of the 355*67e74705SXin Li line; take care if your source contains multibyte characters. 356*67e74705SXin Li 357*67e74705SXin Li.. option:: -fdiagnostics-parseable-fixits 358*67e74705SXin Li 359*67e74705SXin Li Print Fix-Its in a machine parseable form. 360*67e74705SXin Li 361*67e74705SXin Li This option makes Clang print available Fix-Its in a machine 362*67e74705SXin Li parseable format at the end of diagnostics. The following example 363*67e74705SXin Li illustrates the format: 364*67e74705SXin Li 365*67e74705SXin Li :: 366*67e74705SXin Li 367*67e74705SXin Li fix-it:"t.cpp":{7:25-7:29}:"Gamma" 368*67e74705SXin Li 369*67e74705SXin Li The range printed is a half-open range, so in this example the 370*67e74705SXin Li characters at column 25 up to but not including column 29 on line 7 371*67e74705SXin Li in t.cpp should be replaced with the string "Gamma". Either the 372*67e74705SXin Li range or the replacement string may be empty (representing strict 373*67e74705SXin Li insertions and strict erasures, respectively). Both the file name 374*67e74705SXin Li and the insertion string escape backslash (as "\\\\"), tabs (as 375*67e74705SXin Li "\\t"), newlines (as "\\n"), double quotes(as "\\"") and 376*67e74705SXin Li non-printable characters (as octal "\\xxx"). 377*67e74705SXin Li 378*67e74705SXin Li The printed column numbers count bytes from the beginning of the 379*67e74705SXin Li line; take care if your source contains multibyte characters. 380*67e74705SXin Li 381*67e74705SXin Li.. option:: -fno-elide-type 382*67e74705SXin Li 383*67e74705SXin Li Turns off elision in template type printing. 384*67e74705SXin Li 385*67e74705SXin Li The default for template type printing is to elide as many template 386*67e74705SXin Li arguments as possible, removing those which are the same in both 387*67e74705SXin Li template types, leaving only the differences. Adding this flag will 388*67e74705SXin Li print all the template arguments. If supported by the terminal, 389*67e74705SXin Li highlighting will still appear on differing arguments. 390*67e74705SXin Li 391*67e74705SXin Li Default: 392*67e74705SXin Li 393*67e74705SXin Li :: 394*67e74705SXin Li 395*67e74705SXin Li 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; 396*67e74705SXin Li 397*67e74705SXin Li -fno-elide-type: 398*67e74705SXin Li 399*67e74705SXin Li :: 400*67e74705SXin Li 401*67e74705SXin Li 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; 402*67e74705SXin Li 403*67e74705SXin Li.. option:: -fdiagnostics-show-template-tree 404*67e74705SXin Li 405*67e74705SXin Li Template type diffing prints a text tree. 406*67e74705SXin Li 407*67e74705SXin Li For diffing large templated types, this option will cause Clang to 408*67e74705SXin Li display the templates as an indented text tree, one argument per 409*67e74705SXin Li line, with differences marked inline. This is compatible with 410*67e74705SXin Li -fno-elide-type. 411*67e74705SXin Li 412*67e74705SXin Li Default: 413*67e74705SXin Li 414*67e74705SXin Li :: 415*67e74705SXin Li 416*67e74705SXin Li 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; 417*67e74705SXin Li 418*67e74705SXin Li With :option:`-fdiagnostics-show-template-tree`: 419*67e74705SXin Li 420*67e74705SXin Li :: 421*67e74705SXin Li 422*67e74705SXin Li t.cc:4:5: note: candidate function not viable: no known conversion for 1st argument; 423*67e74705SXin Li vector< 424*67e74705SXin Li map< 425*67e74705SXin Li [...], 426*67e74705SXin Li map< 427*67e74705SXin Li [float != double], 428*67e74705SXin Li [...]>>> 429*67e74705SXin Li 430*67e74705SXin Li.. _cl_diag_warning_groups: 431*67e74705SXin Li 432*67e74705SXin LiIndividual Warning Groups 433*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^^^^^ 434*67e74705SXin Li 435*67e74705SXin LiTODO: Generate this from tblgen. Define one anchor per warning group. 436*67e74705SXin Li 437*67e74705SXin Li.. _opt_wextra-tokens: 438*67e74705SXin Li 439*67e74705SXin Li.. option:: -Wextra-tokens 440*67e74705SXin Li 441*67e74705SXin Li Warn about excess tokens at the end of a preprocessor directive. 442*67e74705SXin Li 443*67e74705SXin Li This option, which defaults to on, enables warnings about extra 444*67e74705SXin Li tokens at the end of preprocessor directives. For example: 445*67e74705SXin Li 446*67e74705SXin Li :: 447*67e74705SXin Li 448*67e74705SXin Li test.c:28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens] 449*67e74705SXin Li #endif bad 450*67e74705SXin Li ^ 451*67e74705SXin Li 452*67e74705SXin Li These extra tokens are not strictly conforming, and are usually best 453*67e74705SXin Li handled by commenting them out. 454*67e74705SXin Li 455*67e74705SXin Li.. option:: -Wambiguous-member-template 456*67e74705SXin Li 457*67e74705SXin Li Warn about unqualified uses of a member template whose name resolves to 458*67e74705SXin Li another template at the location of the use. 459*67e74705SXin Li 460*67e74705SXin Li This option, which defaults to on, enables a warning in the 461*67e74705SXin Li following code: 462*67e74705SXin Li 463*67e74705SXin Li :: 464*67e74705SXin Li 465*67e74705SXin Li template<typename T> struct set{}; 466*67e74705SXin Li template<typename T> struct trait { typedef const T& type; }; 467*67e74705SXin Li struct Value { 468*67e74705SXin Li template<typename T> void set(typename trait<T>::type value) {} 469*67e74705SXin Li }; 470*67e74705SXin Li void foo() { 471*67e74705SXin Li Value v; 472*67e74705SXin Li v.set<double>(3.2); 473*67e74705SXin Li } 474*67e74705SXin Li 475*67e74705SXin Li C++ [basic.lookup.classref] requires this to be an error, but, 476*67e74705SXin Li because it's hard to work around, Clang downgrades it to a warning 477*67e74705SXin Li as an extension. 478*67e74705SXin Li 479*67e74705SXin Li.. option:: -Wbind-to-temporary-copy 480*67e74705SXin Li 481*67e74705SXin Li Warn about an unusable copy constructor when binding a reference to a 482*67e74705SXin Li temporary. 483*67e74705SXin Li 484*67e74705SXin Li This option enables warnings about binding a 485*67e74705SXin Li reference to a temporary when the temporary doesn't have a usable 486*67e74705SXin Li copy constructor. For example: 487*67e74705SXin Li 488*67e74705SXin Li :: 489*67e74705SXin Li 490*67e74705SXin Li struct NonCopyable { 491*67e74705SXin Li NonCopyable(); 492*67e74705SXin Li private: 493*67e74705SXin Li NonCopyable(const NonCopyable&); 494*67e74705SXin Li }; 495*67e74705SXin Li void foo(const NonCopyable&); 496*67e74705SXin Li void bar() { 497*67e74705SXin Li foo(NonCopyable()); // Disallowed in C++98; allowed in C++11. 498*67e74705SXin Li } 499*67e74705SXin Li 500*67e74705SXin Li :: 501*67e74705SXin Li 502*67e74705SXin Li struct NonCopyable2 { 503*67e74705SXin Li NonCopyable2(); 504*67e74705SXin Li NonCopyable2(NonCopyable2&); 505*67e74705SXin Li }; 506*67e74705SXin Li void foo(const NonCopyable2&); 507*67e74705SXin Li void bar() { 508*67e74705SXin Li foo(NonCopyable2()); // Disallowed in C++98; allowed in C++11. 509*67e74705SXin Li } 510*67e74705SXin Li 511*67e74705SXin Li Note that if ``NonCopyable2::NonCopyable2()`` has a default argument 512*67e74705SXin Li whose instantiation produces a compile error, that error will still 513*67e74705SXin Li be a hard error in C++98 mode even if this warning is turned off. 514*67e74705SXin Li 515*67e74705SXin LiOptions to Control Clang Crash Diagnostics 516*67e74705SXin Li------------------------------------------ 517*67e74705SXin Li 518*67e74705SXin LiAs unbelievable as it may sound, Clang does crash from time to time. 519*67e74705SXin LiGenerally, this only occurs to those living on the `bleeding 520*67e74705SXin Liedge <http://llvm.org/releases/download.html#svn>`_. Clang goes to great 521*67e74705SXin Lilengths to assist you in filing a bug report. Specifically, Clang 522*67e74705SXin Ligenerates preprocessed source file(s) and associated run script(s) upon 523*67e74705SXin Lia crash. These files should be attached to a bug report to ease 524*67e74705SXin Lireproducibility of the failure. Below are the command line options to 525*67e74705SXin Licontrol the crash diagnostics. 526*67e74705SXin Li 527*67e74705SXin Li.. option:: -fno-crash-diagnostics 528*67e74705SXin Li 529*67e74705SXin Li Disable auto-generation of preprocessed source files during a clang crash. 530*67e74705SXin Li 531*67e74705SXin LiThe -fno-crash-diagnostics flag can be helpful for speeding the process 532*67e74705SXin Liof generating a delta reduced test case. 533*67e74705SXin Li 534*67e74705SXin LiOptions to Emit Optimization Reports 535*67e74705SXin Li------------------------------------ 536*67e74705SXin Li 537*67e74705SXin LiOptimization reports trace, at a high-level, all the major decisions 538*67e74705SXin Lidone by compiler transformations. For instance, when the inliner 539*67e74705SXin Lidecides to inline function ``foo()`` into ``bar()``, or the loop unroller 540*67e74705SXin Lidecides to unroll a loop N times, or the vectorizer decides to 541*67e74705SXin Livectorize a loop body. 542*67e74705SXin Li 543*67e74705SXin LiClang offers a family of flags which the optimizers can use to emit 544*67e74705SXin Lia diagnostic in three cases: 545*67e74705SXin Li 546*67e74705SXin Li1. When the pass makes a transformation (:option:`-Rpass`). 547*67e74705SXin Li 548*67e74705SXin Li2. When the pass fails to make a transformation (:option:`-Rpass-missed`). 549*67e74705SXin Li 550*67e74705SXin Li3. When the pass determines whether or not to make a transformation 551*67e74705SXin Li (:option:`-Rpass-analysis`). 552*67e74705SXin Li 553*67e74705SXin LiNOTE: Although the discussion below focuses on :option:`-Rpass`, the exact 554*67e74705SXin Lisame options apply to :option:`-Rpass-missed` and :option:`-Rpass-analysis`. 555*67e74705SXin Li 556*67e74705SXin LiSince there are dozens of passes inside the compiler, each of these flags 557*67e74705SXin Litake a regular expression that identifies the name of the pass which should 558*67e74705SXin Liemit the associated diagnostic. For example, to get a report from the inliner, 559*67e74705SXin Licompile the code with: 560*67e74705SXin Li 561*67e74705SXin Li.. code-block:: console 562*67e74705SXin Li 563*67e74705SXin Li $ clang -O2 -Rpass=inline code.cc -o code 564*67e74705SXin Li code.cc:4:25: remark: foo inlined into bar [-Rpass=inline] 565*67e74705SXin Li int bar(int j) { return foo(j, j - 2); } 566*67e74705SXin Li ^ 567*67e74705SXin Li 568*67e74705SXin LiNote that remarks from the inliner are identified with `[-Rpass=inline]`. 569*67e74705SXin LiTo request a report from every optimization pass, you should use 570*67e74705SXin Li:option:`-Rpass=.*` (in fact, you can use any valid POSIX regular 571*67e74705SXin Liexpression). However, do not expect a report from every transformation 572*67e74705SXin Limade by the compiler. Optimization remarks do not really make sense 573*67e74705SXin Lioutside of the major transformations (e.g., inlining, vectorization, 574*67e74705SXin Liloop optimizations) and not every optimization pass supports this 575*67e74705SXin Lifeature. 576*67e74705SXin Li 577*67e74705SXin LiCurrent limitations 578*67e74705SXin Li^^^^^^^^^^^^^^^^^^^ 579*67e74705SXin Li 580*67e74705SXin Li1. Optimization remarks that refer to function names will display the 581*67e74705SXin Li mangled name of the function. Since these remarks are emitted by the 582*67e74705SXin Li back end of the compiler, it does not know anything about the input 583*67e74705SXin Li language, nor its mangling rules. 584*67e74705SXin Li 585*67e74705SXin Li2. Some source locations are not displayed correctly. The front end has 586*67e74705SXin Li a more detailed source location tracking than the locations included 587*67e74705SXin Li in the debug info (e.g., the front end can locate code inside macro 588*67e74705SXin Li expansions). However, the locations used by :option:`-Rpass` are 589*67e74705SXin Li translated from debug annotations. That translation can be lossy, 590*67e74705SXin Li which results in some remarks having no location information. 591*67e74705SXin Li 592*67e74705SXin LiOther Options 593*67e74705SXin Li------------- 594*67e74705SXin LiClang options that that don't fit neatly into other categories. 595*67e74705SXin Li 596*67e74705SXin Li.. option:: -MV 597*67e74705SXin Li 598*67e74705SXin Li When emitting a dependency file, use formatting conventions appropriate 599*67e74705SXin Li for NMake or Jom. Ignored unless another option causes Clang to emit a 600*67e74705SXin Li dependency file. 601*67e74705SXin Li 602*67e74705SXin LiWhen Clang emits a dependency file (e.g., you supplied the -M option) 603*67e74705SXin Limost filenames can be written to the file without any special formatting. 604*67e74705SXin LiDifferent Make tools will treat different sets of characters as "special" 605*67e74705SXin Liand use different conventions for telling the Make tool that the character 606*67e74705SXin Liis actually part of the filename. Normally Clang uses backslash to "escape" 607*67e74705SXin Lia special character, which is the convention used by GNU Make. The -MV 608*67e74705SXin Lioption tells Clang to put double-quotes around the entire filename, which 609*67e74705SXin Liis the convention used by NMake and Jom. 610*67e74705SXin Li 611*67e74705SXin Li 612*67e74705SXin LiLanguage and Target-Independent Features 613*67e74705SXin Li======================================== 614*67e74705SXin Li 615*67e74705SXin LiControlling Errors and Warnings 616*67e74705SXin Li------------------------------- 617*67e74705SXin Li 618*67e74705SXin LiClang provides a number of ways to control which code constructs cause 619*67e74705SXin Liit to emit errors and warning messages, and how they are displayed to 620*67e74705SXin Lithe console. 621*67e74705SXin Li 622*67e74705SXin LiControlling How Clang Displays Diagnostics 623*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 624*67e74705SXin Li 625*67e74705SXin LiWhen Clang emits a diagnostic, it includes rich information in the 626*67e74705SXin Lioutput, and gives you fine-grain control over which information is 627*67e74705SXin Liprinted. Clang has the ability to print this information, and these are 628*67e74705SXin Lithe options that control it: 629*67e74705SXin Li 630*67e74705SXin Li#. A file/line/column indicator that shows exactly where the diagnostic 631*67e74705SXin Li occurs in your code [:ref:`-fshow-column <opt_fshow-column>`, 632*67e74705SXin Li :ref:`-fshow-source-location <opt_fshow-source-location>`]. 633*67e74705SXin Li#. A categorization of the diagnostic as a note, warning, error, or 634*67e74705SXin Li fatal error. 635*67e74705SXin Li#. A text string that describes what the problem is. 636*67e74705SXin Li#. An option that indicates how to control the diagnostic (for 637*67e74705SXin Li diagnostics that support it) 638*67e74705SXin Li [:ref:`-fdiagnostics-show-option <opt_fdiagnostics-show-option>`]. 639*67e74705SXin Li#. A :ref:`high-level category <diagnostics_categories>` for the diagnostic 640*67e74705SXin Li for clients that want to group diagnostics by class (for diagnostics 641*67e74705SXin Li that support it) 642*67e74705SXin Li [:ref:`-fdiagnostics-show-category <opt_fdiagnostics-show-category>`]. 643*67e74705SXin Li#. The line of source code that the issue occurs on, along with a caret 644*67e74705SXin Li and ranges that indicate the important locations 645*67e74705SXin Li [:ref:`-fcaret-diagnostics <opt_fcaret-diagnostics>`]. 646*67e74705SXin Li#. "FixIt" information, which is a concise explanation of how to fix the 647*67e74705SXin Li problem (when Clang is certain it knows) 648*67e74705SXin Li [:ref:`-fdiagnostics-fixit-info <opt_fdiagnostics-fixit-info>`]. 649*67e74705SXin Li#. A machine-parsable representation of the ranges involved (off by 650*67e74705SXin Li default) 651*67e74705SXin Li [:ref:`-fdiagnostics-print-source-range-info <opt_fdiagnostics-print-source-range-info>`]. 652*67e74705SXin Li 653*67e74705SXin LiFor more information please see :ref:`Formatting of 654*67e74705SXin LiDiagnostics <cl_diag_formatting>`. 655*67e74705SXin Li 656*67e74705SXin LiDiagnostic Mappings 657*67e74705SXin Li^^^^^^^^^^^^^^^^^^^ 658*67e74705SXin Li 659*67e74705SXin LiAll diagnostics are mapped into one of these 6 classes: 660*67e74705SXin Li 661*67e74705SXin Li- Ignored 662*67e74705SXin Li- Note 663*67e74705SXin Li- Remark 664*67e74705SXin Li- Warning 665*67e74705SXin Li- Error 666*67e74705SXin Li- Fatal 667*67e74705SXin Li 668*67e74705SXin Li.. _diagnostics_categories: 669*67e74705SXin Li 670*67e74705SXin LiDiagnostic Categories 671*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^ 672*67e74705SXin Li 673*67e74705SXin LiThough not shown by default, diagnostics may each be associated with a 674*67e74705SXin Lihigh-level category. This category is intended to make it possible to 675*67e74705SXin Litriage builds that produce a large number of errors or warnings in a 676*67e74705SXin Ligrouped way. 677*67e74705SXin Li 678*67e74705SXin LiCategories are not shown by default, but they can be turned on with the 679*67e74705SXin Li:ref:`-fdiagnostics-show-category <opt_fdiagnostics-show-category>` option. 680*67e74705SXin LiWhen set to "``name``", the category is printed textually in the 681*67e74705SXin Lidiagnostic output. When it is set to "``id``", a category number is 682*67e74705SXin Liprinted. The mapping of category names to category id's can be obtained 683*67e74705SXin Liby running '``clang --print-diagnostic-categories``'. 684*67e74705SXin Li 685*67e74705SXin LiControlling Diagnostics via Command Line Flags 686*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 687*67e74705SXin Li 688*67e74705SXin LiTODO: -W flags, -pedantic, etc 689*67e74705SXin Li 690*67e74705SXin Li.. _pragma_gcc_diagnostic: 691*67e74705SXin Li 692*67e74705SXin LiControlling Diagnostics via Pragmas 693*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 694*67e74705SXin Li 695*67e74705SXin LiClang can also control what diagnostics are enabled through the use of 696*67e74705SXin Lipragmas in the source code. This is useful for turning off specific 697*67e74705SXin Liwarnings in a section of source code. Clang supports GCC's pragma for 698*67e74705SXin Licompatibility with existing source code, as well as several extensions. 699*67e74705SXin Li 700*67e74705SXin LiThe pragma may control any warning that can be used from the command 701*67e74705SXin Liline. Warnings may be set to ignored, warning, error, or fatal. The 702*67e74705SXin Lifollowing example code will tell Clang or GCC to ignore the -Wall 703*67e74705SXin Liwarnings: 704*67e74705SXin Li 705*67e74705SXin Li.. code-block:: c 706*67e74705SXin Li 707*67e74705SXin Li #pragma GCC diagnostic ignored "-Wall" 708*67e74705SXin Li 709*67e74705SXin LiIn addition to all of the functionality provided by GCC's pragma, Clang 710*67e74705SXin Lialso allows you to push and pop the current warning state. This is 711*67e74705SXin Liparticularly useful when writing a header file that will be compiled by 712*67e74705SXin Liother people, because you don't know what warning flags they build with. 713*67e74705SXin Li 714*67e74705SXin LiIn the below example :option:`-Wextra-tokens` is ignored for only a single line 715*67e74705SXin Liof code, after which the diagnostics return to whatever state had previously 716*67e74705SXin Liexisted. 717*67e74705SXin Li 718*67e74705SXin Li.. code-block:: c 719*67e74705SXin Li 720*67e74705SXin Li #if foo 721*67e74705SXin Li #endif foo // warning: extra tokens at end of #endif directive 722*67e74705SXin Li 723*67e74705SXin Li #pragma clang diagnostic ignored "-Wextra-tokens" 724*67e74705SXin Li 725*67e74705SXin Li #if foo 726*67e74705SXin Li #endif foo // no warning 727*67e74705SXin Li 728*67e74705SXin Li #pragma clang diagnostic pop 729*67e74705SXin Li 730*67e74705SXin LiThe push and pop pragmas will save and restore the full diagnostic state 731*67e74705SXin Liof the compiler, regardless of how it was set. That means that it is 732*67e74705SXin Lipossible to use push and pop around GCC compatible diagnostics and Clang 733*67e74705SXin Liwill push and pop them appropriately, while GCC will ignore the pushes 734*67e74705SXin Liand pops as unknown pragmas. It should be noted that while Clang 735*67e74705SXin Lisupports the GCC pragma, Clang and GCC do not support the exact same set 736*67e74705SXin Liof warnings, so even when using GCC compatible #pragmas there is no 737*67e74705SXin Liguarantee that they will have identical behaviour on both compilers. 738*67e74705SXin Li 739*67e74705SXin LiIn addition to controlling warnings and errors generated by the compiler, it is 740*67e74705SXin Lipossible to generate custom warning and error messages through the following 741*67e74705SXin Lipragmas: 742*67e74705SXin Li 743*67e74705SXin Li.. code-block:: c 744*67e74705SXin Li 745*67e74705SXin Li // The following will produce warning messages 746*67e74705SXin Li #pragma message "some diagnostic message" 747*67e74705SXin Li #pragma GCC warning "TODO: replace deprecated feature" 748*67e74705SXin Li 749*67e74705SXin Li // The following will produce an error message 750*67e74705SXin Li #pragma GCC error "Not supported" 751*67e74705SXin Li 752*67e74705SXin LiThese pragmas operate similarly to the ``#warning`` and ``#error`` preprocessor 753*67e74705SXin Lidirectives, except that they may also be embedded into preprocessor macros via 754*67e74705SXin Lithe C99 ``_Pragma`` operator, for example: 755*67e74705SXin Li 756*67e74705SXin Li.. code-block:: c 757*67e74705SXin Li 758*67e74705SXin Li #define STR(X) #X 759*67e74705SXin Li #define DEFER(M,...) M(__VA_ARGS__) 760*67e74705SXin Li #define CUSTOM_ERROR(X) _Pragma(STR(GCC error(X " at line " DEFER(STR,__LINE__)))) 761*67e74705SXin Li 762*67e74705SXin Li CUSTOM_ERROR("Feature not available"); 763*67e74705SXin Li 764*67e74705SXin LiControlling Diagnostics in System Headers 765*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 766*67e74705SXin Li 767*67e74705SXin LiWarnings are suppressed when they occur in system headers. By default, 768*67e74705SXin Lian included file is treated as a system header if it is found in an 769*67e74705SXin Liinclude path specified by ``-isystem``, but this can be overridden in 770*67e74705SXin Liseveral ways. 771*67e74705SXin Li 772*67e74705SXin LiThe ``system_header`` pragma can be used to mark the current file as 773*67e74705SXin Libeing a system header. No warnings will be produced from the location of 774*67e74705SXin Lithe pragma onwards within the same file. 775*67e74705SXin Li 776*67e74705SXin Li.. code-block:: c 777*67e74705SXin Li 778*67e74705SXin Li #if foo 779*67e74705SXin Li #endif foo // warning: extra tokens at end of #endif directive 780*67e74705SXin Li 781*67e74705SXin Li #pragma clang system_header 782*67e74705SXin Li 783*67e74705SXin Li #if foo 784*67e74705SXin Li #endif foo // no warning 785*67e74705SXin Li 786*67e74705SXin LiThe :option:`--system-header-prefix=` and :option:`--no-system-header-prefix=` 787*67e74705SXin Licommand-line arguments can be used to override whether subsets of an include 788*67e74705SXin Lipath are treated as system headers. When the name in a ``#include`` directive 789*67e74705SXin Liis found within a header search path and starts with a system prefix, the 790*67e74705SXin Liheader is treated as a system header. The last prefix on the 791*67e74705SXin Licommand-line which matches the specified header name takes precedence. 792*67e74705SXin LiFor instance: 793*67e74705SXin Li 794*67e74705SXin Li.. code-block:: console 795*67e74705SXin Li 796*67e74705SXin Li $ clang -Ifoo -isystem bar --system-header-prefix=x/ \ 797*67e74705SXin Li --no-system-header-prefix=x/y/ 798*67e74705SXin Li 799*67e74705SXin LiHere, ``#include "x/a.h"`` is treated as including a system header, even 800*67e74705SXin Liif the header is found in ``foo``, and ``#include "x/y/b.h"`` is treated 801*67e74705SXin Lias not including a system header, even if the header is found in 802*67e74705SXin Li``bar``. 803*67e74705SXin Li 804*67e74705SXin LiA ``#include`` directive which finds a file relative to the current 805*67e74705SXin Lidirectory is treated as including a system header if the including file 806*67e74705SXin Liis treated as a system header. 807*67e74705SXin Li 808*67e74705SXin Li.. _diagnostics_enable_everything: 809*67e74705SXin Li 810*67e74705SXin LiEnabling All Diagnostics 811*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 812*67e74705SXin Li 813*67e74705SXin LiIn addition to the traditional ``-W`` flags, one can enable **all** 814*67e74705SXin Lidiagnostics by passing :option:`-Weverything`. This works as expected 815*67e74705SXin Liwith 816*67e74705SXin Li:option:`-Werror`, and also includes the warnings from :option:`-pedantic`. 817*67e74705SXin Li 818*67e74705SXin LiNote that when combined with :option:`-w` (which disables all warnings), that 819*67e74705SXin Liflag wins. 820*67e74705SXin Li 821*67e74705SXin LiControlling Static Analyzer Diagnostics 822*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 823*67e74705SXin Li 824*67e74705SXin LiWhile not strictly part of the compiler, the diagnostics from Clang's 825*67e74705SXin Li`static analyzer <http://clang-analyzer.llvm.org>`_ can also be 826*67e74705SXin Liinfluenced by the user via changes to the source code. See the available 827*67e74705SXin Li`annotations <http://clang-analyzer.llvm.org/annotations.html>`_ and the 828*67e74705SXin Lianalyzer's `FAQ 829*67e74705SXin Lipage <http://clang-analyzer.llvm.org/faq.html#exclude_code>`_ for more 830*67e74705SXin Liinformation. 831*67e74705SXin Li 832*67e74705SXin Li.. _usersmanual-precompiled-headers: 833*67e74705SXin Li 834*67e74705SXin LiPrecompiled Headers 835*67e74705SXin Li------------------- 836*67e74705SXin Li 837*67e74705SXin Li`Precompiled headers <http://en.wikipedia.org/wiki/Precompiled_header>`__ 838*67e74705SXin Liare a general approach employed by many compilers to reduce compilation 839*67e74705SXin Litime. The underlying motivation of the approach is that it is common for 840*67e74705SXin Lithe same (and often large) header files to be included by multiple 841*67e74705SXin Lisource files. Consequently, compile times can often be greatly improved 842*67e74705SXin Liby caching some of the (redundant) work done by a compiler to process 843*67e74705SXin Liheaders. Precompiled header files, which represent one of many ways to 844*67e74705SXin Liimplement this optimization, are literally files that represent an 845*67e74705SXin Lion-disk cache that contains the vital information necessary to reduce 846*67e74705SXin Lisome of the work needed to process a corresponding header file. While 847*67e74705SXin Lidetails of precompiled headers vary between compilers, precompiled 848*67e74705SXin Liheaders have been shown to be highly effective at speeding up program 849*67e74705SXin Licompilation on systems with very large system headers (e.g., Mac OS X). 850*67e74705SXin Li 851*67e74705SXin LiGenerating a PCH File 852*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^ 853*67e74705SXin Li 854*67e74705SXin LiTo generate a PCH file using Clang, one invokes Clang with the 855*67e74705SXin Li:option:`-x <language>-header` option. This mirrors the interface in GCC 856*67e74705SXin Lifor generating PCH files: 857*67e74705SXin Li 858*67e74705SXin Li.. code-block:: console 859*67e74705SXin Li 860*67e74705SXin Li $ gcc -x c-header test.h -o test.h.gch 861*67e74705SXin Li $ clang -x c-header test.h -o test.h.pch 862*67e74705SXin Li 863*67e74705SXin LiUsing a PCH File 864*67e74705SXin Li^^^^^^^^^^^^^^^^ 865*67e74705SXin Li 866*67e74705SXin LiA PCH file can then be used as a prefix header when a :option:`-include` 867*67e74705SXin Lioption is passed to ``clang``: 868*67e74705SXin Li 869*67e74705SXin Li.. code-block:: console 870*67e74705SXin Li 871*67e74705SXin Li $ clang -include test.h test.c -o test 872*67e74705SXin Li 873*67e74705SXin LiThe ``clang`` driver will first check if a PCH file for ``test.h`` is 874*67e74705SXin Liavailable; if so, the contents of ``test.h`` (and the files it includes) 875*67e74705SXin Liwill be processed from the PCH file. Otherwise, Clang falls back to 876*67e74705SXin Lidirectly processing the content of ``test.h``. This mirrors the behavior 877*67e74705SXin Liof GCC. 878*67e74705SXin Li 879*67e74705SXin Li.. note:: 880*67e74705SXin Li 881*67e74705SXin Li Clang does *not* automatically use PCH files for headers that are directly 882*67e74705SXin Li included within a source file. For example: 883*67e74705SXin Li 884*67e74705SXin Li .. code-block:: console 885*67e74705SXin Li 886*67e74705SXin Li $ clang -x c-header test.h -o test.h.pch 887*67e74705SXin Li $ cat test.c 888*67e74705SXin Li #include "test.h" 889*67e74705SXin Li $ clang test.c -o test 890*67e74705SXin Li 891*67e74705SXin Li In this example, ``clang`` will not automatically use the PCH file for 892*67e74705SXin Li ``test.h`` since ``test.h`` was included directly in the source file and not 893*67e74705SXin Li specified on the command line using :option:`-include`. 894*67e74705SXin Li 895*67e74705SXin LiRelocatable PCH Files 896*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^ 897*67e74705SXin Li 898*67e74705SXin LiIt is sometimes necessary to build a precompiled header from headers 899*67e74705SXin Lithat are not yet in their final, installed locations. For example, one 900*67e74705SXin Limight build a precompiled header within the build tree that is then 901*67e74705SXin Limeant to be installed alongside the headers. Clang permits the creation 902*67e74705SXin Liof "relocatable" precompiled headers, which are built with a given path 903*67e74705SXin Li(into the build directory) and can later be used from an installed 904*67e74705SXin Lilocation. 905*67e74705SXin Li 906*67e74705SXin LiTo build a relocatable precompiled header, place your headers into a 907*67e74705SXin Lisubdirectory whose structure mimics the installed location. For example, 908*67e74705SXin Liif you want to build a precompiled header for the header ``mylib.h`` 909*67e74705SXin Lithat will be installed into ``/usr/include``, create a subdirectory 910*67e74705SXin Li``build/usr/include`` and place the header ``mylib.h`` into that 911*67e74705SXin Lisubdirectory. If ``mylib.h`` depends on other headers, then they can be 912*67e74705SXin Listored within ``build/usr/include`` in a way that mimics the installed 913*67e74705SXin Lilocation. 914*67e74705SXin Li 915*67e74705SXin LiBuilding a relocatable precompiled header requires two additional 916*67e74705SXin Liarguments. First, pass the ``--relocatable-pch`` flag to indicate that 917*67e74705SXin Lithe resulting PCH file should be relocatable. Second, pass 918*67e74705SXin Li:option:`-isysroot /path/to/build`, which makes all includes for your library 919*67e74705SXin Lirelative to the build directory. For example: 920*67e74705SXin Li 921*67e74705SXin Li.. code-block:: console 922*67e74705SXin Li 923*67e74705SXin Li # clang -x c-header --relocatable-pch -isysroot /path/to/build /path/to/build/mylib.h mylib.h.pch 924*67e74705SXin Li 925*67e74705SXin LiWhen loading the relocatable PCH file, the various headers used in the 926*67e74705SXin LiPCH file are found from the system header root. For example, ``mylib.h`` 927*67e74705SXin Lican be found in ``/usr/include/mylib.h``. If the headers are installed 928*67e74705SXin Liin some other system root, the :option:`-isysroot` option can be used provide 929*67e74705SXin Lia different system root from which the headers will be based. For 930*67e74705SXin Liexample, :option:`-isysroot /Developer/SDKs/MacOSX10.4u.sdk` will look for 931*67e74705SXin Li``mylib.h`` in ``/Developer/SDKs/MacOSX10.4u.sdk/usr/include/mylib.h``. 932*67e74705SXin Li 933*67e74705SXin LiRelocatable precompiled headers are intended to be used in a limited 934*67e74705SXin Linumber of cases where the compilation environment is tightly controlled 935*67e74705SXin Liand the precompiled header cannot be generated after headers have been 936*67e74705SXin Liinstalled. 937*67e74705SXin Li 938*67e74705SXin Li.. _controlling-code-generation: 939*67e74705SXin Li 940*67e74705SXin LiControlling Code Generation 941*67e74705SXin Li--------------------------- 942*67e74705SXin Li 943*67e74705SXin LiClang provides a number of ways to control code generation. The options 944*67e74705SXin Liare listed below. 945*67e74705SXin Li 946*67e74705SXin Li**-f[no-]sanitize=check1,check2,...** 947*67e74705SXin Li Turn on runtime checks for various forms of undefined or suspicious 948*67e74705SXin Li behavior. 949*67e74705SXin Li 950*67e74705SXin Li This option controls whether Clang adds runtime checks for various 951*67e74705SXin Li forms of undefined or suspicious behavior, and is disabled by 952*67e74705SXin Li default. If a check fails, a diagnostic message is produced at 953*67e74705SXin Li runtime explaining the problem. The main checks are: 954*67e74705SXin Li 955*67e74705SXin Li - .. _opt_fsanitize_address: 956*67e74705SXin Li 957*67e74705SXin Li ``-fsanitize=address``: 958*67e74705SXin Li :doc:`AddressSanitizer`, a memory error 959*67e74705SXin Li detector. 960*67e74705SXin Li - .. _opt_fsanitize_thread: 961*67e74705SXin Li 962*67e74705SXin Li ``-fsanitize=thread``: :doc:`ThreadSanitizer`, a data race detector. 963*67e74705SXin Li - .. _opt_fsanitize_memory: 964*67e74705SXin Li 965*67e74705SXin Li ``-fsanitize=memory``: :doc:`MemorySanitizer`, 966*67e74705SXin Li a detector of uninitialized reads. Requires instrumentation of all 967*67e74705SXin Li program code. 968*67e74705SXin Li - .. _opt_fsanitize_undefined: 969*67e74705SXin Li 970*67e74705SXin Li ``-fsanitize=undefined``: :doc:`UndefinedBehaviorSanitizer`, 971*67e74705SXin Li a fast and compatible undefined behavior checker. 972*67e74705SXin Li 973*67e74705SXin Li - ``-fsanitize=dataflow``: :doc:`DataFlowSanitizer`, a general data 974*67e74705SXin Li flow analysis. 975*67e74705SXin Li - ``-fsanitize=cfi``: :doc:`control flow integrity <ControlFlowIntegrity>` 976*67e74705SXin Li checks. Requires ``-flto``. 977*67e74705SXin Li - ``-fsanitize=safe-stack``: :doc:`safe stack <SafeStack>` 978*67e74705SXin Li protection against stack-based memory corruption errors. 979*67e74705SXin Li 980*67e74705SXin Li There are more fine-grained checks available: see 981*67e74705SXin Li the :ref:`list <ubsan-checks>` of specific kinds of 982*67e74705SXin Li undefined behavior that can be detected and the :ref:`list <cfi-schemes>` 983*67e74705SXin Li of control flow integrity schemes. 984*67e74705SXin Li 985*67e74705SXin Li The ``-fsanitize=`` argument must also be provided when linking, in 986*67e74705SXin Li order to link to the appropriate runtime library. 987*67e74705SXin Li 988*67e74705SXin Li It is not possible to combine more than one of the ``-fsanitize=address``, 989*67e74705SXin Li ``-fsanitize=thread``, and ``-fsanitize=memory`` checkers in the same 990*67e74705SXin Li program. 991*67e74705SXin Li 992*67e74705SXin Li**-f[no-]sanitize-recover=check1,check2,...** 993*67e74705SXin Li 994*67e74705SXin Li**-f[no-]sanitize-recover=all** 995*67e74705SXin Li 996*67e74705SXin Li Controls which checks enabled by ``-fsanitize=`` flag are non-fatal. 997*67e74705SXin Li If the check is fatal, program will halt after the first error 998*67e74705SXin Li of this kind is detected and error report is printed. 999*67e74705SXin Li 1000*67e74705SXin Li By default, non-fatal checks are those enabled by 1001*67e74705SXin Li :doc:`UndefinedBehaviorSanitizer`, 1002*67e74705SXin Li except for ``-fsanitize=return`` and ``-fsanitize=unreachable``. Some 1003*67e74705SXin Li sanitizers may not support recovery (or not support it by default 1004*67e74705SXin Li e.g. :doc:`AddressSanitizer`), and always crash the program after the issue 1005*67e74705SXin Li is detected. 1006*67e74705SXin Li 1007*67e74705SXin Li Note that the ``-fsanitize-trap`` flag has precedence over this flag. 1008*67e74705SXin Li This means that if a check has been configured to trap elsewhere on the 1009*67e74705SXin Li command line, or if the check traps by default, this flag will not have 1010*67e74705SXin Li any effect unless that sanitizer's trapping behavior is disabled with 1011*67e74705SXin Li ``-fno-sanitize-trap``. 1012*67e74705SXin Li 1013*67e74705SXin Li For example, if a command line contains the flags ``-fsanitize=undefined 1014*67e74705SXin Li -fsanitize-trap=undefined``, the flag ``-fsanitize-recover=alignment`` 1015*67e74705SXin Li will have no effect on its own; it will need to be accompanied by 1016*67e74705SXin Li ``-fno-sanitize-trap=alignment``. 1017*67e74705SXin Li 1018*67e74705SXin Li**-f[no-]sanitize-trap=check1,check2,...** 1019*67e74705SXin Li 1020*67e74705SXin Li Controls which checks enabled by the ``-fsanitize=`` flag trap. This 1021*67e74705SXin Li option is intended for use in cases where the sanitizer runtime cannot 1022*67e74705SXin Li be used (for instance, when building libc or a kernel module), or where 1023*67e74705SXin Li the binary size increase caused by the sanitizer runtime is a concern. 1024*67e74705SXin Li 1025*67e74705SXin Li This flag is only compatible with :doc:`control flow integrity 1026*67e74705SXin Li <ControlFlowIntegrity>` schemes and :doc:`UndefinedBehaviorSanitizer` 1027*67e74705SXin Li checks other than ``vptr``. If this flag 1028*67e74705SXin Li is supplied together with ``-fsanitize=undefined``, the ``vptr`` sanitizer 1029*67e74705SXin Li will be implicitly disabled. 1030*67e74705SXin Li 1031*67e74705SXin Li This flag is enabled by default for sanitizers in the ``cfi`` group. 1032*67e74705SXin Li 1033*67e74705SXin Li.. option:: -fsanitize-blacklist=/path/to/blacklist/file 1034*67e74705SXin Li 1035*67e74705SXin Li Disable or modify sanitizer checks for objects (source files, functions, 1036*67e74705SXin Li variables, types) listed in the file. See 1037*67e74705SXin Li :doc:`SanitizerSpecialCaseList` for file format description. 1038*67e74705SXin Li 1039*67e74705SXin Li.. option:: -fno-sanitize-blacklist 1040*67e74705SXin Li 1041*67e74705SXin Li Don't use blacklist file, if it was specified earlier in the command line. 1042*67e74705SXin Li 1043*67e74705SXin Li**-f[no-]sanitize-coverage=[type,features,...]** 1044*67e74705SXin Li 1045*67e74705SXin Li Enable simple code coverage in addition to certain sanitizers. 1046*67e74705SXin Li See :doc:`SanitizerCoverage` for more details. 1047*67e74705SXin Li 1048*67e74705SXin Li**-f[no-]sanitize-stats** 1049*67e74705SXin Li 1050*67e74705SXin Li Enable simple statistics gathering for the enabled sanitizers. 1051*67e74705SXin Li See :doc:`SanitizerStats` for more details. 1052*67e74705SXin Li 1053*67e74705SXin Li.. option:: -fsanitize-undefined-trap-on-error 1054*67e74705SXin Li 1055*67e74705SXin Li Deprecated alias for ``-fsanitize-trap=undefined``. 1056*67e74705SXin Li 1057*67e74705SXin Li.. option:: -fsanitize-cfi-cross-dso 1058*67e74705SXin Li 1059*67e74705SXin Li Enable cross-DSO control flow integrity checks. This flag modifies 1060*67e74705SXin Li the behavior of sanitizers in the ``cfi`` group to allow checking 1061*67e74705SXin Li of cross-DSO virtual and indirect calls. 1062*67e74705SXin Li 1063*67e74705SXin Li.. option:: -ffast-math 1064*67e74705SXin Li 1065*67e74705SXin Li Enable fast-math mode. This defines the ``__FAST_MATH__`` preprocessor 1066*67e74705SXin Li macro, and lets the compiler make aggressive, potentially-lossy assumptions 1067*67e74705SXin Li about floating-point math. These include: 1068*67e74705SXin Li 1069*67e74705SXin Li * Floating-point math obeys regular algebraic rules for real numbers (e.g. 1070*67e74705SXin Li ``+`` and ``*`` are associative, ``x/y == x * (1/y)``, and 1071*67e74705SXin Li ``(a + b) * c == a * c + b * c``), 1072*67e74705SXin Li * operands to floating-point operations are not equal to ``NaN`` and 1073*67e74705SXin Li ``Inf``, and 1074*67e74705SXin Li * ``+0`` and ``-0`` are interchangeable. 1075*67e74705SXin Li 1076*67e74705SXin Li.. option:: -fwhole-program-vtables 1077*67e74705SXin Li 1078*67e74705SXin Li Enable whole-program vtable optimizations, such as single-implementation 1079*67e74705SXin Li devirtualization and virtual constant propagation, for classes with 1080*67e74705SXin Li :doc:`hidden LTO visibility <LTOVisibility>`. Requires ``-flto``. 1081*67e74705SXin Li 1082*67e74705SXin Li.. option:: -fno-assume-sane-operator-new 1083*67e74705SXin Li 1084*67e74705SXin Li Don't assume that the C++'s new operator is sane. 1085*67e74705SXin Li 1086*67e74705SXin Li This option tells the compiler to do not assume that C++'s global 1087*67e74705SXin Li new operator will always return a pointer that does not alias any 1088*67e74705SXin Li other pointer when the function returns. 1089*67e74705SXin Li 1090*67e74705SXin Li.. option:: -ftrap-function=[name] 1091*67e74705SXin Li 1092*67e74705SXin Li Instruct code generator to emit a function call to the specified 1093*67e74705SXin Li function name for ``__builtin_trap()``. 1094*67e74705SXin Li 1095*67e74705SXin Li LLVM code generator translates ``__builtin_trap()`` to a trap 1096*67e74705SXin Li instruction if it is supported by the target ISA. Otherwise, the 1097*67e74705SXin Li builtin is translated into a call to ``abort``. If this option is 1098*67e74705SXin Li set, then the code generator will always lower the builtin to a call 1099*67e74705SXin Li to the specified function regardless of whether the target ISA has a 1100*67e74705SXin Li trap instruction. This option is useful for environments (e.g. 1101*67e74705SXin Li deeply embedded) where a trap cannot be properly handled, or when 1102*67e74705SXin Li some custom behavior is desired. 1103*67e74705SXin Li 1104*67e74705SXin Li.. option:: -ftls-model=[model] 1105*67e74705SXin Li 1106*67e74705SXin Li Select which TLS model to use. 1107*67e74705SXin Li 1108*67e74705SXin Li Valid values are: ``global-dynamic``, ``local-dynamic``, 1109*67e74705SXin Li ``initial-exec`` and ``local-exec``. The default value is 1110*67e74705SXin Li ``global-dynamic``. The compiler may use a different model if the 1111*67e74705SXin Li selected model is not supported by the target, or if a more 1112*67e74705SXin Li efficient model can be used. The TLS model can be overridden per 1113*67e74705SXin Li variable using the ``tls_model`` attribute. 1114*67e74705SXin Li 1115*67e74705SXin Li.. option:: -femulated-tls 1116*67e74705SXin Li 1117*67e74705SXin Li Select emulated TLS model, which overrides all -ftls-model choices. 1118*67e74705SXin Li 1119*67e74705SXin Li In emulated TLS mode, all access to TLS variables are converted to 1120*67e74705SXin Li calls to __emutls_get_address in the runtime library. 1121*67e74705SXin Li 1122*67e74705SXin Li.. option:: -mhwdiv=[values] 1123*67e74705SXin Li 1124*67e74705SXin Li Select the ARM modes (arm or thumb) that support hardware division 1125*67e74705SXin Li instructions. 1126*67e74705SXin Li 1127*67e74705SXin Li Valid values are: ``arm``, ``thumb`` and ``arm,thumb``. 1128*67e74705SXin Li This option is used to indicate which mode (arm or thumb) supports 1129*67e74705SXin Li hardware division instructions. This only applies to the ARM 1130*67e74705SXin Li architecture. 1131*67e74705SXin Li 1132*67e74705SXin Li.. option:: -m[no-]crc 1133*67e74705SXin Li 1134*67e74705SXin Li Enable or disable CRC instructions. 1135*67e74705SXin Li 1136*67e74705SXin Li This option is used to indicate whether CRC instructions are to 1137*67e74705SXin Li be generated. This only applies to the ARM architecture. 1138*67e74705SXin Li 1139*67e74705SXin Li CRC instructions are enabled by default on ARMv8. 1140*67e74705SXin Li 1141*67e74705SXin Li.. option:: -mgeneral-regs-only 1142*67e74705SXin Li 1143*67e74705SXin Li Generate code which only uses the general purpose registers. 1144*67e74705SXin Li 1145*67e74705SXin Li This option restricts the generated code to use general registers 1146*67e74705SXin Li only. This only applies to the AArch64 architecture. 1147*67e74705SXin Li 1148*67e74705SXin Li.. option:: -mcompact-branches=[values] 1149*67e74705SXin Li 1150*67e74705SXin Li Control the usage of compact branches for MIPSR6. 1151*67e74705SXin Li 1152*67e74705SXin Li Valid values are: ``never``, ``optimal`` and ``always``. 1153*67e74705SXin Li The default value is ``optimal`` which generates compact branches 1154*67e74705SXin Li when a delay slot cannot be filled. ``never`` disables the usage of 1155*67e74705SXin Li compact branches and ``always`` generates compact branches whenever 1156*67e74705SXin Li possible. 1157*67e74705SXin Li 1158*67e74705SXin Li**-f[no-]max-type-align=[number]** 1159*67e74705SXin Li Instruct the code generator to not enforce a higher alignment than the given 1160*67e74705SXin Li number (of bytes) when accessing memory via an opaque pointer or reference. 1161*67e74705SXin Li This cap is ignored when directly accessing a variable or when the pointee 1162*67e74705SXin Li type has an explicit “aligned” attribute. 1163*67e74705SXin Li 1164*67e74705SXin Li The value should usually be determined by the properties of the system allocator. 1165*67e74705SXin Li Some builtin types, especially vector types, have very high natural alignments; 1166*67e74705SXin Li when working with values of those types, Clang usually wants to use instructions 1167*67e74705SXin Li that take advantage of that alignment. However, many system allocators do 1168*67e74705SXin Li not promise to return memory that is more than 8-byte or 16-byte-aligned. Use 1169*67e74705SXin Li this option to limit the alignment that the compiler can assume for an arbitrary 1170*67e74705SXin Li pointer, which may point onto the heap. 1171*67e74705SXin Li 1172*67e74705SXin Li This option does not affect the ABI alignment of types; the layout of structs and 1173*67e74705SXin Li unions and the value returned by the alignof operator remain the same. 1174*67e74705SXin Li 1175*67e74705SXin Li This option can be overridden on a case-by-case basis by putting an explicit 1176*67e74705SXin Li “aligned” alignment on a struct, union, or typedef. For example: 1177*67e74705SXin Li 1178*67e74705SXin Li .. code-block:: console 1179*67e74705SXin Li 1180*67e74705SXin Li #include <immintrin.h> 1181*67e74705SXin Li // Make an aligned typedef of the AVX-512 16-int vector type. 1182*67e74705SXin Li typedef __v16si __aligned_v16si __attribute__((aligned(64))); 1183*67e74705SXin Li 1184*67e74705SXin Li void initialize_vector(__aligned_v16si *v) { 1185*67e74705SXin Li // The compiler may assume that ‘v’ is 64-byte aligned, regardless of the 1186*67e74705SXin Li // value of -fmax-type-align. 1187*67e74705SXin Li } 1188*67e74705SXin Li 1189*67e74705SXin Li 1190*67e74705SXin LiProfile Guided Optimization 1191*67e74705SXin Li--------------------------- 1192*67e74705SXin Li 1193*67e74705SXin LiProfile information enables better optimization. For example, knowing that a 1194*67e74705SXin Libranch is taken very frequently helps the compiler make better decisions when 1195*67e74705SXin Liordering basic blocks. Knowing that a function ``foo`` is called more 1196*67e74705SXin Lifrequently than another function ``bar`` helps the inliner. 1197*67e74705SXin Li 1198*67e74705SXin LiClang supports profile guided optimization with two different kinds of 1199*67e74705SXin Liprofiling. A sampling profiler can generate a profile with very low runtime 1200*67e74705SXin Lioverhead, or you can build an instrumented version of the code that collects 1201*67e74705SXin Limore detailed profile information. Both kinds of profiles can provide execution 1202*67e74705SXin Licounts for instructions in the code and information on branches taken and 1203*67e74705SXin Lifunction invocation. 1204*67e74705SXin Li 1205*67e74705SXin LiRegardless of which kind of profiling you use, be careful to collect profiles 1206*67e74705SXin Liby running your code with inputs that are representative of the typical 1207*67e74705SXin Libehavior. Code that is not exercised in the profile will be optimized as if it 1208*67e74705SXin Liis unimportant, and the compiler may make poor optimization choices for code 1209*67e74705SXin Lithat is disproportionately used while profiling. 1210*67e74705SXin Li 1211*67e74705SXin LiDifferences Between Sampling and Instrumentation 1212*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 1213*67e74705SXin Li 1214*67e74705SXin LiAlthough both techniques are used for similar purposes, there are important 1215*67e74705SXin Lidifferences between the two: 1216*67e74705SXin Li 1217*67e74705SXin Li1. Profile data generated with one cannot be used by the other, and there is no 1218*67e74705SXin Li conversion tool that can convert one to the other. So, a profile generated 1219*67e74705SXin Li via ``-fprofile-instr-generate`` must be used with ``-fprofile-instr-use``. 1220*67e74705SXin Li Similarly, sampling profiles generated by external profilers must be 1221*67e74705SXin Li converted and used with ``-fprofile-sample-use``. 1222*67e74705SXin Li 1223*67e74705SXin Li2. Instrumentation profile data can be used for code coverage analysis and 1224*67e74705SXin Li optimization. 1225*67e74705SXin Li 1226*67e74705SXin Li3. Sampling profiles can only be used for optimization. They cannot be used for 1227*67e74705SXin Li code coverage analysis. Although it would be technically possible to use 1228*67e74705SXin Li sampling profiles for code coverage, sample-based profiles are too 1229*67e74705SXin Li coarse-grained for code coverage purposes; it would yield poor results. 1230*67e74705SXin Li 1231*67e74705SXin Li4. Sampling profiles must be generated by an external tool. The profile 1232*67e74705SXin Li generated by that tool must then be converted into a format that can be read 1233*67e74705SXin Li by LLVM. The section on sampling profilers describes one of the supported 1234*67e74705SXin Li sampling profile formats. 1235*67e74705SXin Li 1236*67e74705SXin Li 1237*67e74705SXin LiUsing Sampling Profilers 1238*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^^^^ 1239*67e74705SXin Li 1240*67e74705SXin LiSampling profilers are used to collect runtime information, such as 1241*67e74705SXin Lihardware counters, while your application executes. They are typically 1242*67e74705SXin Livery efficient and do not incur a large runtime overhead. The 1243*67e74705SXin Lisample data collected by the profiler can be used during compilation 1244*67e74705SXin Lito determine what the most executed areas of the code are. 1245*67e74705SXin Li 1246*67e74705SXin LiUsing the data from a sample profiler requires some changes in the way 1247*67e74705SXin Lia program is built. Before the compiler can use profiling information, 1248*67e74705SXin Lithe code needs to execute under the profiler. The following is the 1249*67e74705SXin Liusual build cycle when using sample profilers for optimization: 1250*67e74705SXin Li 1251*67e74705SXin Li1. Build the code with source line table information. You can use all the 1252*67e74705SXin Li usual build flags that you always build your application with. The only 1253*67e74705SXin Li requirement is that you add ``-gline-tables-only`` or ``-g`` to the 1254*67e74705SXin Li command line. This is important for the profiler to be able to map 1255*67e74705SXin Li instructions back to source line locations. 1256*67e74705SXin Li 1257*67e74705SXin Li .. code-block:: console 1258*67e74705SXin Li 1259*67e74705SXin Li $ clang++ -O2 -gline-tables-only code.cc -o code 1260*67e74705SXin Li 1261*67e74705SXin Li2. Run the executable under a sampling profiler. The specific profiler 1262*67e74705SXin Li you use does not really matter, as long as its output can be converted 1263*67e74705SXin Li into the format that the LLVM optimizer understands. Currently, there 1264*67e74705SXin Li exists a conversion tool for the Linux Perf profiler 1265*67e74705SXin Li (https://perf.wiki.kernel.org/), so these examples assume that you 1266*67e74705SXin Li are using Linux Perf to profile your code. 1267*67e74705SXin Li 1268*67e74705SXin Li .. code-block:: console 1269*67e74705SXin Li 1270*67e74705SXin Li $ perf record -b ./code 1271*67e74705SXin Li 1272*67e74705SXin Li Note the use of the ``-b`` flag. This tells Perf to use the Last Branch 1273*67e74705SXin Li Record (LBR) to record call chains. While this is not strictly required, 1274*67e74705SXin Li it provides better call information, which improves the accuracy of 1275*67e74705SXin Li the profile data. 1276*67e74705SXin Li 1277*67e74705SXin Li3. Convert the collected profile data to LLVM's sample profile format. 1278*67e74705SXin Li This is currently supported via the AutoFDO converter ``create_llvm_prof``. 1279*67e74705SXin Li It is available at http://github.com/google/autofdo. Once built and 1280*67e74705SXin Li installed, you can convert the ``perf.data`` file to LLVM using 1281*67e74705SXin Li the command: 1282*67e74705SXin Li 1283*67e74705SXin Li .. code-block:: console 1284*67e74705SXin Li 1285*67e74705SXin Li $ create_llvm_prof --binary=./code --out=code.prof 1286*67e74705SXin Li 1287*67e74705SXin Li This will read ``perf.data`` and the binary file ``./code`` and emit 1288*67e74705SXin Li the profile data in ``code.prof``. Note that if you ran ``perf`` 1289*67e74705SXin Li without the ``-b`` flag, you need to use ``--use_lbr=false`` when 1290*67e74705SXin Li calling ``create_llvm_prof``. 1291*67e74705SXin Li 1292*67e74705SXin Li4. Build the code again using the collected profile. This step feeds 1293*67e74705SXin Li the profile back to the optimizers. This should result in a binary 1294*67e74705SXin Li that executes faster than the original one. Note that you are not 1295*67e74705SXin Li required to build the code with the exact same arguments that you 1296*67e74705SXin Li used in the first step. The only requirement is that you build the code 1297*67e74705SXin Li with ``-gline-tables-only`` and ``-fprofile-sample-use``. 1298*67e74705SXin Li 1299*67e74705SXin Li .. code-block:: console 1300*67e74705SXin Li 1301*67e74705SXin Li $ clang++ -O2 -gline-tables-only -fprofile-sample-use=code.prof code.cc -o code 1302*67e74705SXin Li 1303*67e74705SXin Li 1304*67e74705SXin LiSample Profile Formats 1305*67e74705SXin Li"""""""""""""""""""""" 1306*67e74705SXin Li 1307*67e74705SXin LiSince external profilers generate profile data in a variety of custom formats, 1308*67e74705SXin Lithe data generated by the profiler must be converted into a format that can be 1309*67e74705SXin Liread by the backend. LLVM supports three different sample profile formats: 1310*67e74705SXin Li 1311*67e74705SXin Li1. ASCII text. This is the easiest one to generate. The file is divided into 1312*67e74705SXin Li sections, which correspond to each of the functions with profile 1313*67e74705SXin Li information. The format is described below. It can also be generated from 1314*67e74705SXin Li the binary or gcov formats using the ``llvm-profdata`` tool. 1315*67e74705SXin Li 1316*67e74705SXin Li2. Binary encoding. This uses a more efficient encoding that yields smaller 1317*67e74705SXin Li profile files. This is the format generated by the ``create_llvm_prof`` tool 1318*67e74705SXin Li in http://github.com/google/autofdo. 1319*67e74705SXin Li 1320*67e74705SXin Li3. GCC encoding. This is based on the gcov format, which is accepted by GCC. It 1321*67e74705SXin Li is only interesting in environments where GCC and Clang co-exist. This 1322*67e74705SXin Li encoding is only generated by the ``create_gcov`` tool in 1323*67e74705SXin Li http://github.com/google/autofdo. It can be read by LLVM and 1324*67e74705SXin Li ``llvm-profdata``, but it cannot be generated by either. 1325*67e74705SXin Li 1326*67e74705SXin LiIf you are using Linux Perf to generate sampling profiles, you can use the 1327*67e74705SXin Liconversion tool ``create_llvm_prof`` described in the previous section. 1328*67e74705SXin LiOtherwise, you will need to write a conversion tool that converts your 1329*67e74705SXin Liprofiler's native format into one of these three. 1330*67e74705SXin Li 1331*67e74705SXin Li 1332*67e74705SXin LiSample Profile Text Format 1333*67e74705SXin Li"""""""""""""""""""""""""" 1334*67e74705SXin Li 1335*67e74705SXin LiThis section describes the ASCII text format for sampling profiles. It is, 1336*67e74705SXin Liarguably, the easiest one to generate. If you are interested in generating any 1337*67e74705SXin Liof the other two, consult the ``ProfileData`` library in in LLVM's source tree 1338*67e74705SXin Li(specifically, ``include/llvm/ProfileData/SampleProfReader.h``). 1339*67e74705SXin Li 1340*67e74705SXin Li.. code-block:: console 1341*67e74705SXin Li 1342*67e74705SXin Li function1:total_samples:total_head_samples 1343*67e74705SXin Li offset1[.discriminator]: number_of_samples [fn1:num fn2:num ... ] 1344*67e74705SXin Li offset2[.discriminator]: number_of_samples [fn3:num fn4:num ... ] 1345*67e74705SXin Li ... 1346*67e74705SXin Li offsetN[.discriminator]: number_of_samples [fn5:num fn6:num ... ] 1347*67e74705SXin Li offsetA[.discriminator]: fnA:num_of_total_samples 1348*67e74705SXin Li offsetA1[.discriminator]: number_of_samples [fn7:num fn8:num ... ] 1349*67e74705SXin Li offsetA1[.discriminator]: number_of_samples [fn9:num fn10:num ... ] 1350*67e74705SXin Li offsetB[.discriminator]: fnB:num_of_total_samples 1351*67e74705SXin Li offsetB1[.discriminator]: number_of_samples [fn11:num fn12:num ... ] 1352*67e74705SXin Li 1353*67e74705SXin LiThis is a nested tree in which the identation represents the nesting level 1354*67e74705SXin Liof the inline stack. There are no blank lines in the file. And the spacing 1355*67e74705SXin Liwithin a single line is fixed. Additional spaces will result in an error 1356*67e74705SXin Liwhile reading the file. 1357*67e74705SXin Li 1358*67e74705SXin LiAny line starting with the '#' character is completely ignored. 1359*67e74705SXin Li 1360*67e74705SXin LiInlined calls are represented with indentation. The Inline stack is a 1361*67e74705SXin Listack of source locations in which the top of the stack represents the 1362*67e74705SXin Lileaf function, and the bottom of the stack represents the actual 1363*67e74705SXin Lisymbol to which the instruction belongs. 1364*67e74705SXin Li 1365*67e74705SXin LiFunction names must be mangled in order for the profile loader to 1366*67e74705SXin Limatch them in the current translation unit. The two numbers in the 1367*67e74705SXin Lifunction header specify how many total samples were accumulated in the 1368*67e74705SXin Lifunction (first number), and the total number of samples accumulated 1369*67e74705SXin Liin the prologue of the function (second number). This head sample 1370*67e74705SXin Licount provides an indicator of how frequently the function is invoked. 1371*67e74705SXin Li 1372*67e74705SXin LiThere are two types of lines in the function body. 1373*67e74705SXin Li 1374*67e74705SXin Li- Sampled line represents the profile information of a source location. 1375*67e74705SXin Li ``offsetN[.discriminator]: number_of_samples [fn5:num fn6:num ... ]`` 1376*67e74705SXin Li 1377*67e74705SXin Li- Callsite line represents the profile information of an inlined callsite. 1378*67e74705SXin Li ``offsetA[.discriminator]: fnA:num_of_total_samples`` 1379*67e74705SXin Li 1380*67e74705SXin LiEach sampled line may contain several items. Some are optional (marked 1381*67e74705SXin Libelow): 1382*67e74705SXin Li 1383*67e74705SXin Lia. Source line offset. This number represents the line number 1384*67e74705SXin Li in the function where the sample was collected. The line number is 1385*67e74705SXin Li always relative to the line where symbol of the function is 1386*67e74705SXin Li defined. So, if the function has its header at line 280, the offset 1387*67e74705SXin Li 13 is at line 293 in the file. 1388*67e74705SXin Li 1389*67e74705SXin Li Note that this offset should never be a negative number. This could 1390*67e74705SXin Li happen in cases like macros. The debug machinery will register the 1391*67e74705SXin Li line number at the point of macro expansion. So, if the macro was 1392*67e74705SXin Li expanded in a line before the start of the function, the profile 1393*67e74705SXin Li converter should emit a 0 as the offset (this means that the optimizers 1394*67e74705SXin Li will not be able to associate a meaningful weight to the instructions 1395*67e74705SXin Li in the macro). 1396*67e74705SXin Li 1397*67e74705SXin Lib. [OPTIONAL] Discriminator. This is used if the sampled program 1398*67e74705SXin Li was compiled with DWARF discriminator support 1399*67e74705SXin Li (http://wiki.dwarfstd.org/index.php?title=Path_Discriminators). 1400*67e74705SXin Li DWARF discriminators are unsigned integer values that allow the 1401*67e74705SXin Li compiler to distinguish between multiple execution paths on the 1402*67e74705SXin Li same source line location. 1403*67e74705SXin Li 1404*67e74705SXin Li For example, consider the line of code ``if (cond) foo(); else bar();``. 1405*67e74705SXin Li If the predicate ``cond`` is true 80% of the time, then the edge 1406*67e74705SXin Li into function ``foo`` should be considered to be taken most of the 1407*67e74705SXin Li time. But both calls to ``foo`` and ``bar`` are at the same source 1408*67e74705SXin Li line, so a sample count at that line is not sufficient. The 1409*67e74705SXin Li compiler needs to know which part of that line is taken more 1410*67e74705SXin Li frequently. 1411*67e74705SXin Li 1412*67e74705SXin Li This is what discriminators provide. In this case, the calls to 1413*67e74705SXin Li ``foo`` and ``bar`` will be at the same line, but will have 1414*67e74705SXin Li different discriminator values. This allows the compiler to correctly 1415*67e74705SXin Li set edge weights into ``foo`` and ``bar``. 1416*67e74705SXin Li 1417*67e74705SXin Lic. Number of samples. This is an integer quantity representing the 1418*67e74705SXin Li number of samples collected by the profiler at this source 1419*67e74705SXin Li location. 1420*67e74705SXin Li 1421*67e74705SXin Lid. [OPTIONAL] Potential call targets and samples. If present, this 1422*67e74705SXin Li line contains a call instruction. This models both direct and 1423*67e74705SXin Li number of samples. For example, 1424*67e74705SXin Li 1425*67e74705SXin Li .. code-block:: console 1426*67e74705SXin Li 1427*67e74705SXin Li 130: 7 foo:3 bar:2 baz:7 1428*67e74705SXin Li 1429*67e74705SXin Li The above means that at relative line offset 130 there is a call 1430*67e74705SXin Li instruction that calls one of ``foo()``, ``bar()`` and ``baz()``, 1431*67e74705SXin Li with ``baz()`` being the relatively more frequently called target. 1432*67e74705SXin Li 1433*67e74705SXin LiAs an example, consider a program with the call chain ``main -> foo -> bar``. 1434*67e74705SXin LiWhen built with optimizations enabled, the compiler may inline the 1435*67e74705SXin Licalls to ``bar`` and ``foo`` inside ``main``. The generated profile 1436*67e74705SXin Licould then be something like this: 1437*67e74705SXin Li 1438*67e74705SXin Li.. code-block:: console 1439*67e74705SXin Li 1440*67e74705SXin Li main:35504:0 1441*67e74705SXin Li 1: _Z3foov:35504 1442*67e74705SXin Li 2: _Z32bari:31977 1443*67e74705SXin Li 1.1: 31977 1444*67e74705SXin Li 2: 0 1445*67e74705SXin Li 1446*67e74705SXin LiThis profile indicates that there were a total of 35,504 samples 1447*67e74705SXin Licollected in main. All of those were at line 1 (the call to ``foo``). 1448*67e74705SXin LiOf those, 31,977 were spent inside the body of ``bar``. The last line 1449*67e74705SXin Liof the profile (``2: 0``) corresponds to line 2 inside ``main``. No 1450*67e74705SXin Lisamples were collected there. 1451*67e74705SXin Li 1452*67e74705SXin LiProfiling with Instrumentation 1453*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 1454*67e74705SXin Li 1455*67e74705SXin LiClang also supports profiling via instrumentation. This requires building a 1456*67e74705SXin Lispecial instrumented version of the code and has some runtime 1457*67e74705SXin Lioverhead during the profiling, but it provides more detailed results than a 1458*67e74705SXin Lisampling profiler. It also provides reproducible results, at least to the 1459*67e74705SXin Liextent that the code behaves consistently across runs. 1460*67e74705SXin Li 1461*67e74705SXin LiHere are the steps for using profile guided optimization with 1462*67e74705SXin Liinstrumentation: 1463*67e74705SXin Li 1464*67e74705SXin Li1. Build an instrumented version of the code by compiling and linking with the 1465*67e74705SXin Li ``-fprofile-instr-generate`` option. 1466*67e74705SXin Li 1467*67e74705SXin Li .. code-block:: console 1468*67e74705SXin Li 1469*67e74705SXin Li $ clang++ -O2 -fprofile-instr-generate code.cc -o code 1470*67e74705SXin Li 1471*67e74705SXin Li2. Run the instrumented executable with inputs that reflect the typical usage. 1472*67e74705SXin Li By default, the profile data will be written to a ``default.profraw`` file 1473*67e74705SXin Li in the current directory. You can override that default by setting the 1474*67e74705SXin Li ``LLVM_PROFILE_FILE`` environment variable to specify an alternate file. 1475*67e74705SXin Li Any instance of ``%p`` in that file name will be replaced by the process 1476*67e74705SXin Li ID, so that you can easily distinguish the profile output from multiple 1477*67e74705SXin Li runs. 1478*67e74705SXin Li 1479*67e74705SXin Li .. code-block:: console 1480*67e74705SXin Li 1481*67e74705SXin Li $ LLVM_PROFILE_FILE="code-%p.profraw" ./code 1482*67e74705SXin Li 1483*67e74705SXin Li3. Combine profiles from multiple runs and convert the "raw" profile format to 1484*67e74705SXin Li the input expected by clang. Use the ``merge`` command of the 1485*67e74705SXin Li ``llvm-profdata`` tool to do this. 1486*67e74705SXin Li 1487*67e74705SXin Li .. code-block:: console 1488*67e74705SXin Li 1489*67e74705SXin Li $ llvm-profdata merge -output=code.profdata code-*.profraw 1490*67e74705SXin Li 1491*67e74705SXin Li Note that this step is necessary even when there is only one "raw" profile, 1492*67e74705SXin Li since the merge operation also changes the file format. 1493*67e74705SXin Li 1494*67e74705SXin Li4. Build the code again using the ``-fprofile-instr-use`` option to specify the 1495*67e74705SXin Li collected profile data. 1496*67e74705SXin Li 1497*67e74705SXin Li .. code-block:: console 1498*67e74705SXin Li 1499*67e74705SXin Li $ clang++ -O2 -fprofile-instr-use=code.profdata code.cc -o code 1500*67e74705SXin Li 1501*67e74705SXin Li You can repeat step 4 as often as you like without regenerating the 1502*67e74705SXin Li profile. As you make changes to your code, clang may no longer be able to 1503*67e74705SXin Li use the profile data. It will warn you when this happens. 1504*67e74705SXin Li 1505*67e74705SXin LiProfile generation and use can also be controlled by the GCC-compatible flags 1506*67e74705SXin Li``-fprofile-generate`` and ``-fprofile-use``. Although these flags are 1507*67e74705SXin Lisemantically equivalent to their GCC counterparts, they *do not* handle 1508*67e74705SXin LiGCC-compatible profiles. They are only meant to implement GCC's semantics 1509*67e74705SXin Liwith respect to profile creation and use. 1510*67e74705SXin Li 1511*67e74705SXin Li.. option:: -fprofile-generate[=<dirname>] 1512*67e74705SXin Li 1513*67e74705SXin Li Without any other arguments, ``-fprofile-generate`` behaves identically to 1514*67e74705SXin Li ``-fprofile-instr-generate``. When given a directory name, it generates the 1515*67e74705SXin Li profile file ``default.profraw`` in the directory named ``dirname``. If 1516*67e74705SXin Li ``dirname`` does not exist, it will be created at runtime. The environment 1517*67e74705SXin Li variable ``LLVM_PROFILE_FILE`` can be used to override the directory and 1518*67e74705SXin Li filename for the profile file at runtime. For example, 1519*67e74705SXin Li 1520*67e74705SXin Li .. code-block:: console 1521*67e74705SXin Li 1522*67e74705SXin Li $ clang++ -O2 -fprofile-generate=yyy/zzz code.cc -o code 1523*67e74705SXin Li 1524*67e74705SXin Li When ``code`` is executed, the profile will be written to the file 1525*67e74705SXin Li ``yyy/zzz/default.profraw``. This can be altered at runtime via the 1526*67e74705SXin Li ``LLVM_PROFILE_FILE`` environment variable: 1527*67e74705SXin Li 1528*67e74705SXin Li .. code-block:: console 1529*67e74705SXin Li 1530*67e74705SXin Li $ LLVM_PROFILE_FILE=/tmp/myprofile/code.profraw ./code 1531*67e74705SXin Li 1532*67e74705SXin Li The above invocation will produce the profile file 1533*67e74705SXin Li ``/tmp/myprofile/code.profraw`` instead of ``yyy/zzz/default.profraw``. 1534*67e74705SXin Li Notice that ``LLVM_PROFILE_FILE`` overrides the directory *and* the file 1535*67e74705SXin Li name for the profile file. 1536*67e74705SXin Li 1537*67e74705SXin Li.. option:: -fprofile-use[=<pathname>] 1538*67e74705SXin Li 1539*67e74705SXin Li Without any other arguments, ``-fprofile-use`` behaves identically to 1540*67e74705SXin Li ``-fprofile-instr-use``. Otherwise, if ``pathname`` is the full path to a 1541*67e74705SXin Li profile file, it reads from that file. If ``pathname`` is a directory name, 1542*67e74705SXin Li it reads from ``pathname/default.profdata``. 1543*67e74705SXin Li 1544*67e74705SXin LiDisabling Instrumentation 1545*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^^^^^ 1546*67e74705SXin Li 1547*67e74705SXin LiIn certain situations, it may be useful to disable profile generation or use 1548*67e74705SXin Lifor specific files in a build, without affecting the main compilation flags 1549*67e74705SXin Liused for the other files in the project. 1550*67e74705SXin Li 1551*67e74705SXin LiIn these cases, you can use the flag ``-fno-profile-instr-generate`` (or 1552*67e74705SXin Li``-fno-profile-generate``) to disable profile generation, and 1553*67e74705SXin Li``-fno-profile-instr-use`` (or ``-fno-profile-use``) to disable profile use. 1554*67e74705SXin Li 1555*67e74705SXin LiNote that these flags should appear after the corresponding profile 1556*67e74705SXin Liflags to have an effect. 1557*67e74705SXin Li 1558*67e74705SXin LiControlling Debug Information 1559*67e74705SXin Li----------------------------- 1560*67e74705SXin Li 1561*67e74705SXin LiControlling Size of Debug Information 1562*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 1563*67e74705SXin Li 1564*67e74705SXin LiDebug info kind generated by Clang can be set by one of the flags listed 1565*67e74705SXin Libelow. If multiple flags are present, the last one is used. 1566*67e74705SXin Li 1567*67e74705SXin Li.. option:: -g0 1568*67e74705SXin Li 1569*67e74705SXin Li Don't generate any debug info (default). 1570*67e74705SXin Li 1571*67e74705SXin Li.. option:: -gline-tables-only 1572*67e74705SXin Li 1573*67e74705SXin Li Generate line number tables only. 1574*67e74705SXin Li 1575*67e74705SXin Li This kind of debug info allows to obtain stack traces with function names, 1576*67e74705SXin Li file names and line numbers (by such tools as ``gdb`` or ``addr2line``). It 1577*67e74705SXin Li doesn't contain any other data (e.g. description of local variables or 1578*67e74705SXin Li function parameters). 1579*67e74705SXin Li 1580*67e74705SXin Li.. option:: -fstandalone-debug 1581*67e74705SXin Li 1582*67e74705SXin Li Clang supports a number of optimizations to reduce the size of debug 1583*67e74705SXin Li information in the binary. They work based on the assumption that 1584*67e74705SXin Li the debug type information can be spread out over multiple 1585*67e74705SXin Li compilation units. For instance, Clang will not emit type 1586*67e74705SXin Li definitions for types that are not needed by a module and could be 1587*67e74705SXin Li replaced with a forward declaration. Further, Clang will only emit 1588*67e74705SXin Li type info for a dynamic C++ class in the module that contains the 1589*67e74705SXin Li vtable for the class. 1590*67e74705SXin Li 1591*67e74705SXin Li The **-fstandalone-debug** option turns off these optimizations. 1592*67e74705SXin Li This is useful when working with 3rd-party libraries that don't come 1593*67e74705SXin Li with debug information. Note that Clang will never emit type 1594*67e74705SXin Li information for types that are not referenced at all by the program. 1595*67e74705SXin Li 1596*67e74705SXin Li.. option:: -fno-standalone-debug 1597*67e74705SXin Li 1598*67e74705SXin Li On Darwin **-fstandalone-debug** is enabled by default. The 1599*67e74705SXin Li **-fno-standalone-debug** option can be used to get to turn on the 1600*67e74705SXin Li vtable-based optimization described above. 1601*67e74705SXin Li 1602*67e74705SXin Li.. option:: -g 1603*67e74705SXin Li 1604*67e74705SXin Li Generate complete debug info. 1605*67e74705SXin Li 1606*67e74705SXin LiControlling Debugger "Tuning" 1607*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 1608*67e74705SXin Li 1609*67e74705SXin LiWhile Clang generally emits standard DWARF debug info (http://dwarfstd.org), 1610*67e74705SXin Lidifferent debuggers may know how to take advantage of different specific DWARF 1611*67e74705SXin Lifeatures. You can "tune" the debug info for one of several different debuggers. 1612*67e74705SXin Li 1613*67e74705SXin Li.. option:: -ggdb, -glldb, -gsce 1614*67e74705SXin Li 1615*67e74705SXin Li Tune the debug info for the ``gdb``, ``lldb``, or Sony Computer Entertainment 1616*67e74705SXin Li debugger, respectively. Each of these options implies **-g**. (Therefore, if 1617*67e74705SXin Li you want both **-gline-tables-only** and debugger tuning, the tuning option 1618*67e74705SXin Li must come first.) 1619*67e74705SXin Li 1620*67e74705SXin Li 1621*67e74705SXin LiComment Parsing Options 1622*67e74705SXin Li----------------------- 1623*67e74705SXin Li 1624*67e74705SXin LiClang parses Doxygen and non-Doxygen style documentation comments and attaches 1625*67e74705SXin Lithem to the appropriate declaration nodes. By default, it only parses 1626*67e74705SXin LiDoxygen-style comments and ignores ordinary comments starting with ``//`` and 1627*67e74705SXin Li``/*``. 1628*67e74705SXin Li 1629*67e74705SXin Li.. option:: -Wdocumentation 1630*67e74705SXin Li 1631*67e74705SXin Li Emit warnings about use of documentation comments. This warning group is off 1632*67e74705SXin Li by default. 1633*67e74705SXin Li 1634*67e74705SXin Li This includes checking that ``\param`` commands name parameters that actually 1635*67e74705SXin Li present in the function signature, checking that ``\returns`` is used only on 1636*67e74705SXin Li functions that actually return a value etc. 1637*67e74705SXin Li 1638*67e74705SXin Li.. option:: -Wno-documentation-unknown-command 1639*67e74705SXin Li 1640*67e74705SXin Li Don't warn when encountering an unknown Doxygen command. 1641*67e74705SXin Li 1642*67e74705SXin Li.. option:: -fparse-all-comments 1643*67e74705SXin Li 1644*67e74705SXin Li Parse all comments as documentation comments (including ordinary comments 1645*67e74705SXin Li starting with ``//`` and ``/*``). 1646*67e74705SXin Li 1647*67e74705SXin Li.. option:: -fcomment-block-commands=[commands] 1648*67e74705SXin Li 1649*67e74705SXin Li Define custom documentation commands as block commands. This allows Clang to 1650*67e74705SXin Li construct the correct AST for these custom commands, and silences warnings 1651*67e74705SXin Li about unknown commands. Several commands must be separated by a comma 1652*67e74705SXin Li *without trailing space*; e.g. ``-fcomment-block-commands=foo,bar`` defines 1653*67e74705SXin Li custom commands ``\foo`` and ``\bar``. 1654*67e74705SXin Li 1655*67e74705SXin Li It is also possible to use ``-fcomment-block-commands`` several times; e.g. 1656*67e74705SXin Li ``-fcomment-block-commands=foo -fcomment-block-commands=bar`` does the same 1657*67e74705SXin Li as above. 1658*67e74705SXin Li 1659*67e74705SXin Li.. _c: 1660*67e74705SXin Li 1661*67e74705SXin LiC Language Features 1662*67e74705SXin Li=================== 1663*67e74705SXin Li 1664*67e74705SXin LiThe support for standard C in clang is feature-complete except for the 1665*67e74705SXin LiC99 floating-point pragmas. 1666*67e74705SXin Li 1667*67e74705SXin LiExtensions supported by clang 1668*67e74705SXin Li----------------------------- 1669*67e74705SXin Li 1670*67e74705SXin LiSee :doc:`LanguageExtensions`. 1671*67e74705SXin Li 1672*67e74705SXin LiDifferences between various standard modes 1673*67e74705SXin Li------------------------------------------ 1674*67e74705SXin Li 1675*67e74705SXin Liclang supports the -std option, which changes what language mode clang 1676*67e74705SXin Liuses. The supported modes for C are c89, gnu89, c94, c99, gnu99, c11, 1677*67e74705SXin Lignu11, and various aliases for those modes. If no -std option is 1678*67e74705SXin Lispecified, clang defaults to gnu11 mode. Many C99 and C11 features are 1679*67e74705SXin Lisupported in earlier modes as a conforming extension, with a warning. Use 1680*67e74705SXin Li``-pedantic-errors`` to request an error if a feature from a later standard 1681*67e74705SXin Lirevision is used in an earlier mode. 1682*67e74705SXin Li 1683*67e74705SXin LiDifferences between all ``c*`` and ``gnu*`` modes: 1684*67e74705SXin Li 1685*67e74705SXin Li- ``c*`` modes define "``__STRICT_ANSI__``". 1686*67e74705SXin Li- Target-specific defines not prefixed by underscores, like "linux", 1687*67e74705SXin Li are defined in ``gnu*`` modes. 1688*67e74705SXin Li- Trigraphs default to being off in ``gnu*`` modes; they can be enabled by 1689*67e74705SXin Li the -trigraphs option. 1690*67e74705SXin Li- The parser recognizes "asm" and "typeof" as keywords in ``gnu*`` modes; 1691*67e74705SXin Li the variants "``__asm__``" and "``__typeof__``" are recognized in all 1692*67e74705SXin Li modes. 1693*67e74705SXin Li- The Apple "blocks" extension is recognized by default in ``gnu*`` modes 1694*67e74705SXin Li on some platforms; it can be enabled in any mode with the "-fblocks" 1695*67e74705SXin Li option. 1696*67e74705SXin Li- Arrays that are VLA's according to the standard, but which can be 1697*67e74705SXin Li constant folded by the frontend are treated as fixed size arrays. 1698*67e74705SXin Li This occurs for things like "int X[(1, 2)];", which is technically a 1699*67e74705SXin Li VLA. ``c*`` modes are strictly compliant and treat these as VLAs. 1700*67e74705SXin Li 1701*67e74705SXin LiDifferences between ``*89`` and ``*99`` modes: 1702*67e74705SXin Li 1703*67e74705SXin Li- The ``*99`` modes default to implementing "inline" as specified in C99, 1704*67e74705SXin Li while the ``*89`` modes implement the GNU version. This can be 1705*67e74705SXin Li overridden for individual functions with the ``__gnu_inline__`` 1706*67e74705SXin Li attribute. 1707*67e74705SXin Li- Digraphs are not recognized in c89 mode. 1708*67e74705SXin Li- The scope of names defined inside a "for", "if", "switch", "while", 1709*67e74705SXin Li or "do" statement is different. (example: "``if ((struct x {int 1710*67e74705SXin Li x;}*)0) {}``".) 1711*67e74705SXin Li- ``__STDC_VERSION__`` is not defined in ``*89`` modes. 1712*67e74705SXin Li- "inline" is not recognized as a keyword in c89 mode. 1713*67e74705SXin Li- "restrict" is not recognized as a keyword in ``*89`` modes. 1714*67e74705SXin Li- Commas are allowed in integer constant expressions in ``*99`` modes. 1715*67e74705SXin Li- Arrays which are not lvalues are not implicitly promoted to pointers 1716*67e74705SXin Li in ``*89`` modes. 1717*67e74705SXin Li- Some warnings are different. 1718*67e74705SXin Li 1719*67e74705SXin LiDifferences between ``*99`` and ``*11`` modes: 1720*67e74705SXin Li 1721*67e74705SXin Li- Warnings for use of C11 features are disabled. 1722*67e74705SXin Li- ``__STDC_VERSION__`` is defined to ``201112L`` rather than ``199901L``. 1723*67e74705SXin Li 1724*67e74705SXin Lic94 mode is identical to c89 mode except that digraphs are enabled in 1725*67e74705SXin Lic94 mode (FIXME: And ``__STDC_VERSION__`` should be defined!). 1726*67e74705SXin Li 1727*67e74705SXin LiGCC extensions not implemented yet 1728*67e74705SXin Li---------------------------------- 1729*67e74705SXin Li 1730*67e74705SXin Liclang tries to be compatible with gcc as much as possible, but some gcc 1731*67e74705SXin Liextensions are not implemented yet: 1732*67e74705SXin Li 1733*67e74705SXin Li- clang does not support decimal floating point types (``_Decimal32`` and 1734*67e74705SXin Li friends) or fixed-point types (``_Fract`` and friends); nobody has 1735*67e74705SXin Li expressed interest in these features yet, so it's hard to say when 1736*67e74705SXin Li they will be implemented. 1737*67e74705SXin Li- clang does not support nested functions; this is a complex feature 1738*67e74705SXin Li which is infrequently used, so it is unlikely to be implemented 1739*67e74705SXin Li anytime soon. In C++11 it can be emulated by assigning lambda 1740*67e74705SXin Li functions to local variables, e.g: 1741*67e74705SXin Li 1742*67e74705SXin Li .. code-block:: cpp 1743*67e74705SXin Li 1744*67e74705SXin Li auto const local_function = [&](int parameter) { 1745*67e74705SXin Li // Do something 1746*67e74705SXin Li }; 1747*67e74705SXin Li ... 1748*67e74705SXin Li local_function(1); 1749*67e74705SXin Li 1750*67e74705SXin Li- clang does not support static initialization of flexible array 1751*67e74705SXin Li members. This appears to be a rarely used extension, but could be 1752*67e74705SXin Li implemented pending user demand. 1753*67e74705SXin Li- clang does not support 1754*67e74705SXin Li ``__builtin_va_arg_pack``/``__builtin_va_arg_pack_len``. This is 1755*67e74705SXin Li used rarely, but in some potentially interesting places, like the 1756*67e74705SXin Li glibc headers, so it may be implemented pending user demand. Note 1757*67e74705SXin Li that because clang pretends to be like GCC 4.2, and this extension 1758*67e74705SXin Li was introduced in 4.3, the glibc headers will not try to use this 1759*67e74705SXin Li extension with clang at the moment. 1760*67e74705SXin Li- clang does not support the gcc extension for forward-declaring 1761*67e74705SXin Li function parameters; this has not shown up in any real-world code 1762*67e74705SXin Li yet, though, so it might never be implemented. 1763*67e74705SXin Li 1764*67e74705SXin LiThis is not a complete list; if you find an unsupported extension 1765*67e74705SXin Limissing from this list, please send an e-mail to cfe-dev. This list 1766*67e74705SXin Licurrently excludes C++; see :ref:`C++ Language Features <cxx>`. Also, this 1767*67e74705SXin Lilist does not include bugs in mostly-implemented features; please see 1768*67e74705SXin Lithe `bug 1769*67e74705SXin Litracker <http://llvm.org/bugs/buglist.cgi?quicksearch=product%3Aclang+component%3A-New%2BBugs%2CAST%2CBasic%2CDriver%2CHeaders%2CLLVM%2BCodeGen%2Cparser%2Cpreprocessor%2CSemantic%2BAnalyzer>`_ 1770*67e74705SXin Lifor known existing bugs (FIXME: Is there a section for bug-reporting 1771*67e74705SXin Liguidelines somewhere?). 1772*67e74705SXin Li 1773*67e74705SXin LiIntentionally unsupported GCC extensions 1774*67e74705SXin Li---------------------------------------- 1775*67e74705SXin Li 1776*67e74705SXin Li- clang does not support the gcc extension that allows variable-length 1777*67e74705SXin Li arrays in structures. This is for a few reasons: one, it is tricky to 1778*67e74705SXin Li implement, two, the extension is completely undocumented, and three, 1779*67e74705SXin Li the extension appears to be rarely used. Note that clang *does* 1780*67e74705SXin Li support flexible array members (arrays with a zero or unspecified 1781*67e74705SXin Li size at the end of a structure). 1782*67e74705SXin Li- clang does not have an equivalent to gcc's "fold"; this means that 1783*67e74705SXin Li clang doesn't accept some constructs gcc might accept in contexts 1784*67e74705SXin Li where a constant expression is required, like "x-x" where x is a 1785*67e74705SXin Li variable. 1786*67e74705SXin Li- clang does not support ``__builtin_apply`` and friends; this extension 1787*67e74705SXin Li is extremely obscure and difficult to implement reliably. 1788*67e74705SXin Li 1789*67e74705SXin Li.. _c_ms: 1790*67e74705SXin Li 1791*67e74705SXin LiMicrosoft extensions 1792*67e74705SXin Li-------------------- 1793*67e74705SXin Li 1794*67e74705SXin Liclang has support for many extensions from Microsoft Visual C++. To enable these 1795*67e74705SXin Liextensions, use the ``-fms-extensions`` command-line option. This is the default 1796*67e74705SXin Lifor Windows targets. Clang does not implement every pragma or declspec provided 1797*67e74705SXin Liby MSVC, but the popular ones, such as ``__declspec(dllexport)`` and ``#pragma 1798*67e74705SXin Licomment(lib)`` are well supported. 1799*67e74705SXin Li 1800*67e74705SXin Liclang has a ``-fms-compatibility`` flag that makes clang accept enough 1801*67e74705SXin Liinvalid C++ to be able to parse most Microsoft headers. For example, it 1802*67e74705SXin Liallows `unqualified lookup of dependent base class members 1803*67e74705SXin Li<http://clang.llvm.org/compatibility.html#dep_lookup_bases>`_, which is 1804*67e74705SXin Lia common compatibility issue with clang. This flag is enabled by default 1805*67e74705SXin Lifor Windows targets. 1806*67e74705SXin Li 1807*67e74705SXin Li``-fdelayed-template-parsing`` lets clang delay parsing of function template 1808*67e74705SXin Lidefinitions until the end of a translation unit. This flag is enabled by 1809*67e74705SXin Lidefault for Windows targets. 1810*67e74705SXin Li 1811*67e74705SXin LiFor compatibility with existing code that compiles with MSVC, clang defines the 1812*67e74705SXin Li``_MSC_VER`` and ``_MSC_FULL_VER`` macros. These default to the values of 1800 1813*67e74705SXin Liand 180000000 respectively, making clang look like an early release of Visual 1814*67e74705SXin LiC++ 2013. The ``-fms-compatibility-version=`` flag overrides these values. It 1815*67e74705SXin Liaccepts a dotted version tuple, such as 19.00.23506. Changing the MSVC 1816*67e74705SXin Licompatibility version makes clang behave more like that version of MSVC. For 1817*67e74705SXin Liexample, ``-fms-compatibility-version=19`` will enable C++14 features and define 1818*67e74705SXin Li``char16_t`` and ``char32_t`` as builtin types. 1819*67e74705SXin Li 1820*67e74705SXin Li.. _cxx: 1821*67e74705SXin Li 1822*67e74705SXin LiC++ Language Features 1823*67e74705SXin Li===================== 1824*67e74705SXin Li 1825*67e74705SXin Liclang fully implements all of standard C++98 except for exported 1826*67e74705SXin Litemplates (which were removed in C++11), and all of standard C++11 1827*67e74705SXin Liand the current draft standard for C++1y. 1828*67e74705SXin Li 1829*67e74705SXin LiControlling implementation limits 1830*67e74705SXin Li--------------------------------- 1831*67e74705SXin Li 1832*67e74705SXin Li.. option:: -fbracket-depth=N 1833*67e74705SXin Li 1834*67e74705SXin Li Sets the limit for nested parentheses, brackets, and braces to N. The 1835*67e74705SXin Li default is 256. 1836*67e74705SXin Li 1837*67e74705SXin Li.. option:: -fconstexpr-depth=N 1838*67e74705SXin Li 1839*67e74705SXin Li Sets the limit for recursive constexpr function invocations to N. The 1840*67e74705SXin Li default is 512. 1841*67e74705SXin Li 1842*67e74705SXin Li.. option:: -ftemplate-depth=N 1843*67e74705SXin Li 1844*67e74705SXin Li Sets the limit for recursively nested template instantiations to N. The 1845*67e74705SXin Li default is 256. 1846*67e74705SXin Li 1847*67e74705SXin Li.. option:: -foperator-arrow-depth=N 1848*67e74705SXin Li 1849*67e74705SXin Li Sets the limit for iterative calls to 'operator->' functions to N. The 1850*67e74705SXin Li default is 256. 1851*67e74705SXin Li 1852*67e74705SXin Li.. _objc: 1853*67e74705SXin Li 1854*67e74705SXin LiObjective-C Language Features 1855*67e74705SXin Li============================= 1856*67e74705SXin Li 1857*67e74705SXin Li.. _objcxx: 1858*67e74705SXin Li 1859*67e74705SXin LiObjective-C++ Language Features 1860*67e74705SXin Li=============================== 1861*67e74705SXin Li 1862*67e74705SXin Li.. _openmp: 1863*67e74705SXin Li 1864*67e74705SXin LiOpenMP Features 1865*67e74705SXin Li=============== 1866*67e74705SXin Li 1867*67e74705SXin LiClang supports all OpenMP 3.1 directives and clauses. In addition, some 1868*67e74705SXin Lifeatures of OpenMP 4.0 are supported. For example, ``#pragma omp simd``, 1869*67e74705SXin Li``#pragma omp for simd``, ``#pragma omp parallel for simd`` directives, extended 1870*67e74705SXin Liset of atomic constructs, ``proc_bind`` clause for all parallel-based 1871*67e74705SXin Lidirectives, ``depend`` clause for ``#pragma omp task`` directive (except for 1872*67e74705SXin Liarray sections), ``#pragma omp cancel`` and ``#pragma omp cancellation point`` 1873*67e74705SXin Lidirectives, and ``#pragma omp taskgroup`` directive. 1874*67e74705SXin Li 1875*67e74705SXin LiUse :option:`-fopenmp` to enable OpenMP. Support for OpenMP can be disabled with 1876*67e74705SXin Li:option:`-fno-openmp`. 1877*67e74705SXin Li 1878*67e74705SXin LiControlling implementation limits 1879*67e74705SXin Li--------------------------------- 1880*67e74705SXin Li 1881*67e74705SXin Li.. option:: -fopenmp-use-tls 1882*67e74705SXin Li 1883*67e74705SXin Li Controls code generation for OpenMP threadprivate variables. In presence of 1884*67e74705SXin Li this option all threadprivate variables are generated the same way as thread 1885*67e74705SXin Li local variables, using TLS support. If :option:`-fno-openmp-use-tls` 1886*67e74705SXin Li is provided or target does not support TLS, code generation for threadprivate 1887*67e74705SXin Li variables relies on OpenMP runtime library. 1888*67e74705SXin Li 1889*67e74705SXin Li.. _target_features: 1890*67e74705SXin Li 1891*67e74705SXin LiTarget-Specific Features and Limitations 1892*67e74705SXin Li======================================== 1893*67e74705SXin Li 1894*67e74705SXin LiCPU Architectures Features and Limitations 1895*67e74705SXin Li------------------------------------------ 1896*67e74705SXin Li 1897*67e74705SXin LiX86 1898*67e74705SXin Li^^^ 1899*67e74705SXin Li 1900*67e74705SXin LiThe support for X86 (both 32-bit and 64-bit) is considered stable on 1901*67e74705SXin LiDarwin (Mac OS X), Linux, FreeBSD, and Dragonfly BSD: it has been tested 1902*67e74705SXin Lito correctly compile many large C, C++, Objective-C, and Objective-C++ 1903*67e74705SXin Licodebases. 1904*67e74705SXin Li 1905*67e74705SXin LiOn ``x86_64-mingw32``, passing i128(by value) is incompatible with the 1906*67e74705SXin LiMicrosoft x64 calling convention. You might need to tweak 1907*67e74705SXin Li``WinX86_64ABIInfo::classify()`` in lib/CodeGen/TargetInfo.cpp. 1908*67e74705SXin Li 1909*67e74705SXin LiFor the X86 target, clang supports the :option:`-m16` command line 1910*67e74705SXin Liargument which enables 16-bit code output. This is broadly similar to 1911*67e74705SXin Liusing ``asm(".code16gcc")`` with the GNU toolchain. The generated code 1912*67e74705SXin Liand the ABI remains 32-bit but the assembler emits instructions 1913*67e74705SXin Liappropriate for a CPU running in 16-bit mode, with address-size and 1914*67e74705SXin Lioperand-size prefixes to enable 32-bit addressing and operations. 1915*67e74705SXin Li 1916*67e74705SXin LiARM 1917*67e74705SXin Li^^^ 1918*67e74705SXin Li 1919*67e74705SXin LiThe support for ARM (specifically ARMv6 and ARMv7) is considered stable 1920*67e74705SXin Lion Darwin (iOS): it has been tested to correctly compile many large C, 1921*67e74705SXin LiC++, Objective-C, and Objective-C++ codebases. Clang only supports a 1922*67e74705SXin Lilimited number of ARM architectures. It does not yet fully support 1923*67e74705SXin LiARMv5, for example. 1924*67e74705SXin Li 1925*67e74705SXin LiPowerPC 1926*67e74705SXin Li^^^^^^^ 1927*67e74705SXin Li 1928*67e74705SXin LiThe support for PowerPC (especially PowerPC64) is considered stable 1929*67e74705SXin Lion Linux and FreeBSD: it has been tested to correctly compile many 1930*67e74705SXin Lilarge C and C++ codebases. PowerPC (32bit) is still missing certain 1931*67e74705SXin Lifeatures (e.g. PIC code on ELF platforms). 1932*67e74705SXin Li 1933*67e74705SXin LiOther platforms 1934*67e74705SXin Li^^^^^^^^^^^^^^^ 1935*67e74705SXin Li 1936*67e74705SXin Liclang currently contains some support for other architectures (e.g. Sparc); 1937*67e74705SXin Lihowever, significant pieces of code generation are still missing, and they 1938*67e74705SXin Lihaven't undergone significant testing. 1939*67e74705SXin Li 1940*67e74705SXin Liclang contains limited support for the MSP430 embedded processor, but 1941*67e74705SXin Liboth the clang support and the LLVM backend support are highly 1942*67e74705SXin Liexperimental. 1943*67e74705SXin Li 1944*67e74705SXin LiOther platforms are completely unsupported at the moment. Adding the 1945*67e74705SXin Liminimal support needed for parsing and semantic analysis on a new 1946*67e74705SXin Liplatform is quite easy; see ``lib/Basic/Targets.cpp`` in the clang source 1947*67e74705SXin Litree. This level of support is also sufficient for conversion to LLVM IR 1948*67e74705SXin Lifor simple programs. Proper support for conversion to LLVM IR requires 1949*67e74705SXin Liadding code to ``lib/CodeGen/CGCall.cpp`` at the moment; this is likely to 1950*67e74705SXin Lichange soon, though. Generating assembly requires a suitable LLVM 1951*67e74705SXin Libackend. 1952*67e74705SXin Li 1953*67e74705SXin LiOperating System Features and Limitations 1954*67e74705SXin Li----------------------------------------- 1955*67e74705SXin Li 1956*67e74705SXin LiDarwin (Mac OS X) 1957*67e74705SXin Li^^^^^^^^^^^^^^^^^ 1958*67e74705SXin Li 1959*67e74705SXin LiThread Sanitizer is not supported. 1960*67e74705SXin Li 1961*67e74705SXin LiWindows 1962*67e74705SXin Li^^^^^^^ 1963*67e74705SXin Li 1964*67e74705SXin LiClang has experimental support for targeting "Cygming" (Cygwin / MinGW) 1965*67e74705SXin Liplatforms. 1966*67e74705SXin Li 1967*67e74705SXin LiSee also :ref:`Microsoft Extensions <c_ms>`. 1968*67e74705SXin Li 1969*67e74705SXin LiCygwin 1970*67e74705SXin Li"""""" 1971*67e74705SXin Li 1972*67e74705SXin LiClang works on Cygwin-1.7. 1973*67e74705SXin Li 1974*67e74705SXin LiMinGW32 1975*67e74705SXin Li""""""" 1976*67e74705SXin Li 1977*67e74705SXin LiClang works on some mingw32 distributions. Clang assumes directories as 1978*67e74705SXin Libelow; 1979*67e74705SXin Li 1980*67e74705SXin Li- ``C:/mingw/include`` 1981*67e74705SXin Li- ``C:/mingw/lib`` 1982*67e74705SXin Li- ``C:/mingw/lib/gcc/mingw32/4.[3-5].0/include/c++`` 1983*67e74705SXin Li 1984*67e74705SXin LiOn MSYS, a few tests might fail. 1985*67e74705SXin Li 1986*67e74705SXin LiMinGW-w64 1987*67e74705SXin Li""""""""" 1988*67e74705SXin Li 1989*67e74705SXin LiFor 32-bit (i686-w64-mingw32), and 64-bit (x86\_64-w64-mingw32), Clang 1990*67e74705SXin Liassumes as below; 1991*67e74705SXin Li 1992*67e74705SXin Li- ``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)`` 1993*67e74705SXin Li- ``some_directory/bin/gcc.exe`` 1994*67e74705SXin Li- ``some_directory/bin/clang.exe`` 1995*67e74705SXin Li- ``some_directory/bin/clang++.exe`` 1996*67e74705SXin Li- ``some_directory/bin/../include/c++/GCC_version`` 1997*67e74705SXin Li- ``some_directory/bin/../include/c++/GCC_version/x86_64-w64-mingw32`` 1998*67e74705SXin Li- ``some_directory/bin/../include/c++/GCC_version/i686-w64-mingw32`` 1999*67e74705SXin Li- ``some_directory/bin/../include/c++/GCC_version/backward`` 2000*67e74705SXin Li- ``some_directory/bin/../x86_64-w64-mingw32/include`` 2001*67e74705SXin Li- ``some_directory/bin/../i686-w64-mingw32/include`` 2002*67e74705SXin Li- ``some_directory/bin/../include`` 2003*67e74705SXin Li 2004*67e74705SXin LiThis directory layout is standard for any toolchain you will find on the 2005*67e74705SXin Liofficial `MinGW-w64 website <http://mingw-w64.sourceforge.net>`_. 2006*67e74705SXin Li 2007*67e74705SXin LiClang expects the GCC executable "gcc.exe" compiled for 2008*67e74705SXin Li``i686-w64-mingw32`` (or ``x86_64-w64-mingw32``) to be present on PATH. 2009*67e74705SXin Li 2010*67e74705SXin Li`Some tests might fail <http://llvm.org/bugs/show_bug.cgi?id=9072>`_ on 2011*67e74705SXin Li``x86_64-w64-mingw32``. 2012*67e74705SXin Li 2013*67e74705SXin Li.. _clang-cl: 2014*67e74705SXin Li 2015*67e74705SXin Liclang-cl 2016*67e74705SXin Li======== 2017*67e74705SXin Li 2018*67e74705SXin Liclang-cl is an alternative command-line interface to Clang driver, designed for 2019*67e74705SXin Licompatibility with the Visual C++ compiler, cl.exe. 2020*67e74705SXin Li 2021*67e74705SXin LiTo enable clang-cl to find system headers, libraries, and the linker when run 2022*67e74705SXin Lifrom the command-line, it should be executed inside a Visual Studio Native Tools 2023*67e74705SXin LiCommand Prompt or a regular Command Prompt where the environment has been set 2024*67e74705SXin Liup using e.g. `vcvars32.bat <http://msdn.microsoft.com/en-us/library/f2ccy3wt.aspx>`_. 2025*67e74705SXin Li 2026*67e74705SXin Liclang-cl can also be used from inside Visual Studio by using an LLVM Platform 2027*67e74705SXin LiToolset. 2028*67e74705SXin Li 2029*67e74705SXin LiCommand-Line Options 2030*67e74705SXin Li-------------------- 2031*67e74705SXin Li 2032*67e74705SXin LiTo be compatible with cl.exe, clang-cl supports most of the same command-line 2033*67e74705SXin Lioptions. Those options can start with either ``/`` or ``-``. It also supports 2034*67e74705SXin Lisome of Clang's core options, such as the ``-W`` options. 2035*67e74705SXin Li 2036*67e74705SXin LiOptions that are known to clang-cl, but not currently supported, are ignored 2037*67e74705SXin Liwith a warning. For example: 2038*67e74705SXin Li 2039*67e74705SXin Li :: 2040*67e74705SXin Li 2041*67e74705SXin Li clang-cl.exe: warning: argument unused during compilation: '/AI' 2042*67e74705SXin Li 2043*67e74705SXin LiTo suppress warnings about unused arguments, use the ``-Qunused-arguments`` option. 2044*67e74705SXin Li 2045*67e74705SXin LiOptions that are not known to clang-cl will be ignored by default. Use the 2046*67e74705SXin Li``-Werror=unknown-argument`` option in order to treat them as errors. If these 2047*67e74705SXin Lioptions are spelled with a leading ``/``, they will be mistaken for a filename: 2048*67e74705SXin Li 2049*67e74705SXin Li :: 2050*67e74705SXin Li 2051*67e74705SXin Li clang-cl.exe: error: no such file or directory: '/foobar' 2052*67e74705SXin Li 2053*67e74705SXin LiPlease `file a bug <http://llvm.org/bugs/enter_bug.cgi?product=clang&component=Driver>`_ 2054*67e74705SXin Lifor any valid cl.exe flags that clang-cl does not understand. 2055*67e74705SXin Li 2056*67e74705SXin LiExecute ``clang-cl /?`` to see a list of supported options: 2057*67e74705SXin Li 2058*67e74705SXin Li :: 2059*67e74705SXin Li 2060*67e74705SXin Li CL.EXE COMPATIBILITY OPTIONS: 2061*67e74705SXin Li /? Display available options 2062*67e74705SXin Li /arch:<value> Set architecture for code generation 2063*67e74705SXin Li /Brepro- Emit an object file which cannot be reproduced over time 2064*67e74705SXin Li /Brepro Emit an object file which can be reproduced over time 2065*67e74705SXin Li /C Don't discard comments when preprocessing 2066*67e74705SXin Li /c Compile only 2067*67e74705SXin Li /D <macro[=value]> Define macro 2068*67e74705SXin Li /EH<value> Exception handling model 2069*67e74705SXin Li /EP Disable linemarker output and preprocess to stdout 2070*67e74705SXin Li /E Preprocess to stdout 2071*67e74705SXin Li /fallback Fall back to cl.exe if clang-cl fails to compile 2072*67e74705SXin Li /FA Output assembly code file during compilation 2073*67e74705SXin Li /Fa<file or directory> Output assembly code to this file during compilation (with /FA) 2074*67e74705SXin Li /Fe<file or directory> Set output executable file or directory (ends in / or \) 2075*67e74705SXin Li /FI <value> Include file before parsing 2076*67e74705SXin Li /Fi<file> Set preprocess output file name (with /P) 2077*67e74705SXin Li /Fo<file or directory> Set output object file, or directory (ends in / or \) (with /c) 2078*67e74705SXin Li /fp:except- 2079*67e74705SXin Li /fp:except 2080*67e74705SXin Li /fp:fast 2081*67e74705SXin Li /fp:precise 2082*67e74705SXin Li /fp:strict 2083*67e74705SXin Li /GA Assume thread-local variables are defined in the executable 2084*67e74705SXin Li /GF- Disable string pooling 2085*67e74705SXin Li /GR- Disable emission of RTTI data 2086*67e74705SXin Li /GR Enable emission of RTTI data 2087*67e74705SXin Li /Gs<value> Set stack probe size 2088*67e74705SXin Li /Gw- Don't put each data item in its own section 2089*67e74705SXin Li /Gw Put each data item in its own section 2090*67e74705SXin Li /Gy- Don't put each function in its own section 2091*67e74705SXin Li /Gy Put each function in its own section 2092*67e74705SXin Li /help Display available options 2093*67e74705SXin Li /I <dir> Add directory to include search path 2094*67e74705SXin Li /J Make char type unsigned 2095*67e74705SXin Li /LDd Create debug DLL 2096*67e74705SXin Li /LD Create DLL 2097*67e74705SXin Li /link <options> Forward options to the linker 2098*67e74705SXin Li /MDd Use DLL debug run-time 2099*67e74705SXin Li /MD Use DLL run-time 2100*67e74705SXin Li /MTd Use static debug run-time 2101*67e74705SXin Li /MT Use static run-time 2102*67e74705SXin Li /Ob0 Disable inlining 2103*67e74705SXin Li /Od Disable optimization 2104*67e74705SXin Li /Oi- Disable use of builtin functions 2105*67e74705SXin Li /Oi Enable use of builtin functions 2106*67e74705SXin Li /Os Optimize for size 2107*67e74705SXin Li /Ot Optimize for speed 2108*67e74705SXin Li /O<value> Optimization level 2109*67e74705SXin Li /o <file or directory> Set output file or directory (ends in / or \) 2110*67e74705SXin Li /P Preprocess to file 2111*67e74705SXin Li /Qvec- Disable the loop vectorization passes 2112*67e74705SXin Li /Qvec Enable the loop vectorization passes 2113*67e74705SXin Li /showIncludes Print info about included files to stderr 2114*67e74705SXin Li /TC Treat all source files as C 2115*67e74705SXin Li /Tc <filename> Specify a C source file 2116*67e74705SXin Li /TP Treat all source files as C++ 2117*67e74705SXin Li /Tp <filename> Specify a C++ source file 2118*67e74705SXin Li /U <macro> Undefine macro 2119*67e74705SXin Li /vd<value> Control vtordisp placement 2120*67e74705SXin Li /vmb Use a best-case representation method for member pointers 2121*67e74705SXin Li /vmg Use a most-general representation for member pointers 2122*67e74705SXin Li /vmm Set the default most-general representation to multiple inheritance 2123*67e74705SXin Li /vms Set the default most-general representation to single inheritance 2124*67e74705SXin Li /vmv Set the default most-general representation to virtual inheritance 2125*67e74705SXin Li /volatile:iso Volatile loads and stores have standard semantics 2126*67e74705SXin Li /volatile:ms Volatile loads and stores have acquire and release semantics 2127*67e74705SXin Li /W0 Disable all warnings 2128*67e74705SXin Li /W1 Enable -Wall 2129*67e74705SXin Li /W2 Enable -Wall 2130*67e74705SXin Li /W3 Enable -Wall 2131*67e74705SXin Li /W4 Enable -Wall and -Wextra 2132*67e74705SXin Li /Wall Enable -Wall and -Wextra 2133*67e74705SXin Li /WX- Do not treat warnings as errors 2134*67e74705SXin Li /WX Treat warnings as errors 2135*67e74705SXin Li /w Disable all warnings 2136*67e74705SXin Li /Z7 Enable CodeView debug information in object files 2137*67e74705SXin Li /Zc:sizedDealloc- Disable C++14 sized global deallocation functions 2138*67e74705SXin Li /Zc:sizedDealloc Enable C++14 sized global deallocation functions 2139*67e74705SXin Li /Zc:strictStrings Treat string literals as const 2140*67e74705SXin Li /Zc:threadSafeInit- Disable thread-safe initialization of static variables 2141*67e74705SXin Li /Zc:threadSafeInit Enable thread-safe initialization of static variables 2142*67e74705SXin Li /Zc:trigraphs- Disable trigraphs (default) 2143*67e74705SXin Li /Zc:trigraphs Enable trigraphs 2144*67e74705SXin Li /Zi Alias for /Z7. Does not produce PDBs. 2145*67e74705SXin Li /Zl Don't mention any default libraries in the object file 2146*67e74705SXin Li /Zp Set the default maximum struct packing alignment to 1 2147*67e74705SXin Li /Zp<value> Specify the default maximum struct packing alignment 2148*67e74705SXin Li /Zs Syntax-check only 2149*67e74705SXin Li 2150*67e74705SXin Li OPTIONS: 2151*67e74705SXin Li -### Print (but do not run) the commands to run for this compilation 2152*67e74705SXin Li --analyze Run the static analyzer 2153*67e74705SXin Li -fansi-escape-codes Use ANSI escape codes for diagnostics 2154*67e74705SXin Li -fcolor-diagnostics Use colors in diagnostics 2155*67e74705SXin Li -fdiagnostics-parseable-fixits 2156*67e74705SXin Li Print fix-its in machine parseable form 2157*67e74705SXin Li -fms-compatibility-version=<value> 2158*67e74705SXin Li Dot-separated value representing the Microsoft compiler version 2159*67e74705SXin Li number to report in _MSC_VER (0 = don't define it (default)) 2160*67e74705SXin Li -fms-compatibility Enable full Microsoft Visual C++ compatibility 2161*67e74705SXin Li -fms-extensions Accept some non-standard constructs supported by the Microsoft compiler 2162*67e74705SXin Li -fmsc-version=<value> Microsoft compiler version number to report in _MSC_VER 2163*67e74705SXin Li (0 = don't define it (default)) 2164*67e74705SXin Li -fno-sanitize-coverage=<value> 2165*67e74705SXin Li Disable specified features of coverage instrumentation for Sanitizers 2166*67e74705SXin Li -fno-sanitize-recover=<value> 2167*67e74705SXin Li Disable recovery for specified sanitizers 2168*67e74705SXin Li -fno-sanitize-trap=<value> 2169*67e74705SXin Li Disable trapping for specified sanitizers 2170*67e74705SXin Li -fsanitize-blacklist=<value> 2171*67e74705SXin Li Path to blacklist file for sanitizers 2172*67e74705SXin Li -fsanitize-coverage=<value> 2173*67e74705SXin Li Specify the type of coverage instrumentation for Sanitizers 2174*67e74705SXin Li -fsanitize-recover=<value> 2175*67e74705SXin Li Enable recovery for specified sanitizers 2176*67e74705SXin Li -fsanitize-trap=<value> Enable trapping for specified sanitizers 2177*67e74705SXin Li -fsanitize=<check> Turn on runtime checks for various forms of undefined or suspicious 2178*67e74705SXin Li behavior. See user manual for available checks 2179*67e74705SXin Li -gcodeview Generate CodeView debug information 2180*67e74705SXin Li -mllvm <value> Additional arguments to forward to LLVM's option processing 2181*67e74705SXin Li -Qunused-arguments Don't emit warning for unused driver arguments 2182*67e74705SXin Li -R<remark> Enable the specified remark 2183*67e74705SXin Li --target=<value> Generate code for the given target 2184*67e74705SXin Li -v Show commands to run and use verbose output 2185*67e74705SXin Li -W<warning> Enable the specified warning 2186*67e74705SXin Li -Xclang <arg> Pass <arg> to the clang compiler 2187*67e74705SXin Li 2188*67e74705SXin LiThe /fallback Option 2189*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^ 2190*67e74705SXin Li 2191*67e74705SXin LiWhen clang-cl is run with the ``/fallback`` option, it will first try to 2192*67e74705SXin Licompile files itself. For any file that it fails to compile, it will fall back 2193*67e74705SXin Liand try to compile the file by invoking cl.exe. 2194*67e74705SXin Li 2195*67e74705SXin LiThis option is intended to be used as a temporary means to build projects where 2196*67e74705SXin Liclang-cl cannot successfully compile all the files. clang-cl may fail to compile 2197*67e74705SXin Lia file either because it cannot generate code for some C++ feature, or because 2198*67e74705SXin Liit cannot parse some Microsoft language extension. 2199