1f4a2713aSLionel Sambuc============================ 2f4a2713aSLionel SambucClang Compiler User's Manual 3f4a2713aSLionel Sambuc============================ 4f4a2713aSLionel Sambuc 5f4a2713aSLionel Sambuc.. contents:: 6f4a2713aSLionel Sambuc :local: 7f4a2713aSLionel Sambuc 8f4a2713aSLionel SambucIntroduction 9f4a2713aSLionel Sambuc============ 10f4a2713aSLionel Sambuc 11f4a2713aSLionel SambucThe Clang Compiler is an open-source compiler for the C family of 12f4a2713aSLionel Sambucprogramming languages, aiming to be the best in class implementation of 13f4a2713aSLionel Sambucthese languages. Clang builds on the LLVM optimizer and code generator, 14f4a2713aSLionel Sambucallowing it to provide high-quality optimization and code generation 15f4a2713aSLionel Sambucsupport for many targets. For more general information, please see the 16f4a2713aSLionel Sambuc`Clang Web Site <http://clang.llvm.org>`_ or the `LLVM Web 17f4a2713aSLionel SambucSite <http://llvm.org>`_. 18f4a2713aSLionel Sambuc 19f4a2713aSLionel SambucThis document describes important notes about using Clang as a compiler 20f4a2713aSLionel Sambucfor an end-user, documenting the supported features, command line 21f4a2713aSLionel Sambucoptions, etc. If you are interested in using Clang to build a tool that 22f4a2713aSLionel Sambucprocesses code, please see :doc:`InternalsManual`. If you are interested in the 23f4a2713aSLionel Sambuc`Clang Static Analyzer <http://clang-analyzer.llvm.org>`_, please see its web 24f4a2713aSLionel Sambucpage. 25f4a2713aSLionel Sambuc 26f4a2713aSLionel SambucClang is designed to support the C family of programming languages, 27f4a2713aSLionel Sambucwhich includes :ref:`C <c>`, :ref:`Objective-C <objc>`, :ref:`C++ <cxx>`, and 28f4a2713aSLionel Sambuc:ref:`Objective-C++ <objcxx>` as well as many dialects of those. For 29f4a2713aSLionel Sambuclanguage-specific information, please see the corresponding language 30f4a2713aSLionel Sambucspecific section: 31f4a2713aSLionel Sambuc 32f4a2713aSLionel Sambuc- :ref:`C Language <c>`: K&R C, ANSI C89, ISO C90, ISO C94 (C89+AMD1), ISO 33f4a2713aSLionel Sambuc C99 (+TC1, TC2, TC3). 34f4a2713aSLionel Sambuc- :ref:`Objective-C Language <objc>`: ObjC 1, ObjC 2, ObjC 2.1, plus 35f4a2713aSLionel Sambuc variants depending on base language. 36f4a2713aSLionel Sambuc- :ref:`C++ Language <cxx>` 37f4a2713aSLionel Sambuc- :ref:`Objective C++ Language <objcxx>` 38f4a2713aSLionel Sambuc 39f4a2713aSLionel SambucIn addition to these base languages and their dialects, Clang supports a 40f4a2713aSLionel Sambucbroad variety of language extensions, which are documented in the 41f4a2713aSLionel Sambuccorresponding language section. These extensions are provided to be 42f4a2713aSLionel Sambuccompatible with the GCC, Microsoft, and other popular compilers as well 43f4a2713aSLionel Sambucas to improve functionality through Clang-specific features. The Clang 44f4a2713aSLionel Sambucdriver and language features are intentionally designed to be as 45f4a2713aSLionel Sambuccompatible with the GNU GCC compiler as reasonably possible, easing 46f4a2713aSLionel Sambucmigration from GCC to Clang. In most cases, code "just works". 47f4a2713aSLionel SambucClang also provides an alternative driver, :ref:`clang-cl`, that is designed 48f4a2713aSLionel Sambucto be compatible with the Visual C++ compiler, cl.exe. 49f4a2713aSLionel Sambuc 50f4a2713aSLionel SambucIn addition to language specific features, Clang has a variety of 51f4a2713aSLionel Sambucfeatures that depend on what CPU architecture or operating system is 52f4a2713aSLionel Sambucbeing compiled for. Please see the :ref:`Target-Specific Features and 53f4a2713aSLionel SambucLimitations <target_features>` section for more details. 54f4a2713aSLionel Sambuc 55f4a2713aSLionel SambucThe rest of the introduction introduces some basic :ref:`compiler 56f4a2713aSLionel Sambucterminology <terminology>` that is used throughout this manual and 57f4a2713aSLionel Sambuccontains a basic :ref:`introduction to using Clang <basicusage>` as a 58f4a2713aSLionel Sambuccommand line compiler. 59f4a2713aSLionel Sambuc 60f4a2713aSLionel Sambuc.. _terminology: 61f4a2713aSLionel Sambuc 62f4a2713aSLionel SambucTerminology 63f4a2713aSLionel Sambuc----------- 64f4a2713aSLionel Sambuc 65f4a2713aSLionel SambucFront end, parser, backend, preprocessor, undefined behavior, 66f4a2713aSLionel Sambucdiagnostic, optimizer 67f4a2713aSLionel Sambuc 68f4a2713aSLionel Sambuc.. _basicusage: 69f4a2713aSLionel Sambuc 70f4a2713aSLionel SambucBasic Usage 71f4a2713aSLionel Sambuc----------- 72f4a2713aSLionel Sambuc 73f4a2713aSLionel SambucIntro to how to use a C compiler for newbies. 74f4a2713aSLionel Sambuc 75f4a2713aSLionel Sambuccompile + link compile then link debug info enabling optimizations 76*0a6a1f1dSLionel Sambucpicking a language to use, defaults to C11 by default. Autosenses based 77f4a2713aSLionel Sambucon extension. using a makefile 78f4a2713aSLionel Sambuc 79f4a2713aSLionel SambucCommand Line Options 80f4a2713aSLionel Sambuc==================== 81f4a2713aSLionel Sambuc 82f4a2713aSLionel SambucThis section is generally an index into other sections. It does not go 83f4a2713aSLionel Sambucinto depth on the ones that are covered by other sections. However, the 84f4a2713aSLionel Sambucfirst part introduces the language selection and other high level 85f4a2713aSLionel Sambucoptions like :option:`-c`, :option:`-g`, etc. 86f4a2713aSLionel Sambuc 87f4a2713aSLionel SambucOptions to Control Error and Warning Messages 88f4a2713aSLionel Sambuc--------------------------------------------- 89f4a2713aSLionel Sambuc 90f4a2713aSLionel Sambuc.. option:: -Werror 91f4a2713aSLionel Sambuc 92f4a2713aSLionel Sambuc Turn warnings into errors. 93f4a2713aSLionel Sambuc 94f4a2713aSLionel Sambuc.. This is in plain monospaced font because it generates the same label as 95f4a2713aSLionel Sambuc.. -Werror, and Sphinx complains. 96f4a2713aSLionel Sambuc 97f4a2713aSLionel Sambuc``-Werror=foo`` 98f4a2713aSLionel Sambuc 99f4a2713aSLionel Sambuc Turn warning "foo" into an error. 100f4a2713aSLionel Sambuc 101f4a2713aSLionel Sambuc.. option:: -Wno-error=foo 102f4a2713aSLionel Sambuc 103f4a2713aSLionel Sambuc Turn warning "foo" into an warning even if :option:`-Werror` is specified. 104f4a2713aSLionel Sambuc 105f4a2713aSLionel Sambuc.. option:: -Wfoo 106f4a2713aSLionel Sambuc 107f4a2713aSLionel Sambuc Enable warning "foo". 108f4a2713aSLionel Sambuc 109f4a2713aSLionel Sambuc.. option:: -Wno-foo 110f4a2713aSLionel Sambuc 111f4a2713aSLionel Sambuc Disable warning "foo". 112f4a2713aSLionel Sambuc 113f4a2713aSLionel Sambuc.. option:: -w 114f4a2713aSLionel Sambuc 115*0a6a1f1dSLionel Sambuc Disable all diagnostics. 116f4a2713aSLionel Sambuc 117f4a2713aSLionel Sambuc.. option:: -Weverything 118f4a2713aSLionel Sambuc 119*0a6a1f1dSLionel Sambuc :ref:`Enable all diagnostics. <diagnostics_enable_everything>` 120f4a2713aSLionel Sambuc 121f4a2713aSLionel Sambuc.. option:: -pedantic 122f4a2713aSLionel Sambuc 123f4a2713aSLionel Sambuc Warn on language extensions. 124f4a2713aSLionel Sambuc 125f4a2713aSLionel Sambuc.. option:: -pedantic-errors 126f4a2713aSLionel Sambuc 127f4a2713aSLionel Sambuc Error on language extensions. 128f4a2713aSLionel Sambuc 129f4a2713aSLionel Sambuc.. option:: -Wsystem-headers 130f4a2713aSLionel Sambuc 131f4a2713aSLionel Sambuc Enable warnings from system headers. 132f4a2713aSLionel Sambuc 133f4a2713aSLionel Sambuc.. option:: -ferror-limit=123 134f4a2713aSLionel Sambuc 135f4a2713aSLionel Sambuc Stop emitting diagnostics after 123 errors have been produced. The default is 136f4a2713aSLionel Sambuc 20, and the error limit can be disabled with :option:`-ferror-limit=0`. 137f4a2713aSLionel Sambuc 138f4a2713aSLionel Sambuc.. option:: -ftemplate-backtrace-limit=123 139f4a2713aSLionel Sambuc 140f4a2713aSLionel Sambuc Only emit up to 123 template instantiation notes within the template 141f4a2713aSLionel Sambuc instantiation backtrace for a single warning or error. The default is 10, and 142f4a2713aSLionel Sambuc the limit can be disabled with :option:`-ftemplate-backtrace-limit=0`. 143f4a2713aSLionel Sambuc 144f4a2713aSLionel Sambuc.. _cl_diag_formatting: 145f4a2713aSLionel Sambuc 146f4a2713aSLionel SambucFormatting of Diagnostics 147f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^ 148f4a2713aSLionel Sambuc 149f4a2713aSLionel SambucClang aims to produce beautiful diagnostics by default, particularly for 150f4a2713aSLionel Sambucnew users that first come to Clang. However, different people have 151f4a2713aSLionel Sambucdifferent preferences, and sometimes Clang is driven by another program 152f4a2713aSLionel Sambucthat wants to parse simple and consistent output, not a person. For 153f4a2713aSLionel Sambucthese cases, Clang provides a wide range of options to control the exact 154f4a2713aSLionel Sambucoutput format of the diagnostics that it generates. 155f4a2713aSLionel Sambuc 156f4a2713aSLionel Sambuc.. _opt_fshow-column: 157f4a2713aSLionel Sambuc 158f4a2713aSLionel Sambuc**-f[no-]show-column** 159f4a2713aSLionel Sambuc Print column number in diagnostic. 160f4a2713aSLionel Sambuc 161f4a2713aSLionel Sambuc This option, which defaults to on, controls whether or not Clang 162f4a2713aSLionel Sambuc prints the column number of a diagnostic. For example, when this is 163f4a2713aSLionel Sambuc enabled, Clang will print something like: 164f4a2713aSLionel Sambuc 165f4a2713aSLionel Sambuc :: 166f4a2713aSLionel Sambuc 167f4a2713aSLionel Sambuc test.c:28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens] 168f4a2713aSLionel Sambuc #endif bad 169f4a2713aSLionel Sambuc ^ 170f4a2713aSLionel Sambuc // 171f4a2713aSLionel Sambuc 172f4a2713aSLionel Sambuc When this is disabled, Clang will print "test.c:28: warning..." with 173f4a2713aSLionel Sambuc no column number. 174f4a2713aSLionel Sambuc 175f4a2713aSLionel Sambuc The printed column numbers count bytes from the beginning of the 176f4a2713aSLionel Sambuc line; take care if your source contains multibyte characters. 177f4a2713aSLionel Sambuc 178f4a2713aSLionel Sambuc.. _opt_fshow-source-location: 179f4a2713aSLionel Sambuc 180f4a2713aSLionel Sambuc**-f[no-]show-source-location** 181f4a2713aSLionel Sambuc Print source file/line/column information in diagnostic. 182f4a2713aSLionel Sambuc 183f4a2713aSLionel Sambuc This option, which defaults to on, controls whether or not Clang 184f4a2713aSLionel Sambuc prints the filename, line number and column number of a diagnostic. 185f4a2713aSLionel Sambuc For example, when this is enabled, Clang will print something like: 186f4a2713aSLionel Sambuc 187f4a2713aSLionel Sambuc :: 188f4a2713aSLionel Sambuc 189f4a2713aSLionel Sambuc test.c:28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens] 190f4a2713aSLionel Sambuc #endif bad 191f4a2713aSLionel Sambuc ^ 192f4a2713aSLionel Sambuc // 193f4a2713aSLionel Sambuc 194f4a2713aSLionel Sambuc When this is disabled, Clang will not print the "test.c:28:8: " 195f4a2713aSLionel Sambuc part. 196f4a2713aSLionel Sambuc 197f4a2713aSLionel Sambuc.. _opt_fcaret-diagnostics: 198f4a2713aSLionel Sambuc 199f4a2713aSLionel Sambuc**-f[no-]caret-diagnostics** 200f4a2713aSLionel Sambuc Print source line and ranges from source code in diagnostic. 201f4a2713aSLionel Sambuc This option, which defaults to on, controls whether or not Clang 202f4a2713aSLionel Sambuc prints the source line, source ranges, and caret when emitting a 203f4a2713aSLionel Sambuc diagnostic. For example, when this is enabled, Clang will print 204f4a2713aSLionel Sambuc something like: 205f4a2713aSLionel Sambuc 206f4a2713aSLionel Sambuc :: 207f4a2713aSLionel Sambuc 208f4a2713aSLionel Sambuc test.c:28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens] 209f4a2713aSLionel Sambuc #endif bad 210f4a2713aSLionel Sambuc ^ 211f4a2713aSLionel Sambuc // 212f4a2713aSLionel Sambuc 213f4a2713aSLionel Sambuc**-f[no-]color-diagnostics** 214f4a2713aSLionel Sambuc This option, which defaults to on when a color-capable terminal is 215f4a2713aSLionel Sambuc detected, controls whether or not Clang prints diagnostics in color. 216f4a2713aSLionel Sambuc 217f4a2713aSLionel Sambuc When this option is enabled, Clang will use colors to highlight 218f4a2713aSLionel Sambuc specific parts of the diagnostic, e.g., 219f4a2713aSLionel Sambuc 220f4a2713aSLionel Sambuc .. nasty hack to not lose our dignity 221f4a2713aSLionel Sambuc 222f4a2713aSLionel Sambuc .. raw:: html 223f4a2713aSLionel Sambuc 224f4a2713aSLionel Sambuc <pre> 225f4a2713aSLionel Sambuc <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> 226f4a2713aSLionel Sambuc #endif bad 227f4a2713aSLionel Sambuc <span style="color:green">^</span> 228f4a2713aSLionel Sambuc <span style="color:green">//</span> 229f4a2713aSLionel Sambuc </pre> 230f4a2713aSLionel Sambuc 231f4a2713aSLionel Sambuc When this is disabled, Clang will just print: 232f4a2713aSLionel Sambuc 233f4a2713aSLionel Sambuc :: 234f4a2713aSLionel Sambuc 235f4a2713aSLionel Sambuc test.c:2:8: warning: extra tokens at end of #endif directive [-Wextra-tokens] 236f4a2713aSLionel Sambuc #endif bad 237f4a2713aSLionel Sambuc ^ 238f4a2713aSLionel Sambuc // 239f4a2713aSLionel Sambuc 240f4a2713aSLionel Sambuc**-fansi-escape-codes** 241f4a2713aSLionel Sambuc Controls whether ANSI escape codes are used instead of the Windows Console 242f4a2713aSLionel Sambuc API to output colored diagnostics. This option is only used on Windows and 243f4a2713aSLionel Sambuc defaults to off. 244f4a2713aSLionel Sambuc 245f4a2713aSLionel Sambuc.. option:: -fdiagnostics-format=clang/msvc/vi 246f4a2713aSLionel Sambuc 247f4a2713aSLionel Sambuc Changes diagnostic output format to better match IDEs and command line tools. 248f4a2713aSLionel Sambuc 249f4a2713aSLionel Sambuc This option controls the output format of the filename, line number, 250f4a2713aSLionel Sambuc and column printed in diagnostic messages. The options, and their 251f4a2713aSLionel Sambuc affect on formatting a simple conversion diagnostic, follow: 252f4a2713aSLionel Sambuc 253f4a2713aSLionel Sambuc **clang** (default) 254f4a2713aSLionel Sambuc :: 255f4a2713aSLionel Sambuc 256f4a2713aSLionel Sambuc t.c:3:11: warning: conversion specifies type 'char *' but the argument has type 'int' 257f4a2713aSLionel Sambuc 258f4a2713aSLionel Sambuc **msvc** 259f4a2713aSLionel Sambuc :: 260f4a2713aSLionel Sambuc 261f4a2713aSLionel Sambuc t.c(3,11) : warning: conversion specifies type 'char *' but the argument has type 'int' 262f4a2713aSLionel Sambuc 263f4a2713aSLionel Sambuc **vi** 264f4a2713aSLionel Sambuc :: 265f4a2713aSLionel Sambuc 266f4a2713aSLionel Sambuc t.c +3:11: warning: conversion specifies type 'char *' but the argument has type 'int' 267f4a2713aSLionel Sambuc 268f4a2713aSLionel Sambuc.. _opt_fdiagnostics-show-option: 269f4a2713aSLionel Sambuc 270f4a2713aSLionel Sambuc**-f[no-]diagnostics-show-option** 271f4a2713aSLionel Sambuc Enable ``[-Woption]`` information in diagnostic line. 272f4a2713aSLionel Sambuc 273f4a2713aSLionel Sambuc This option, which defaults to on, controls whether or not Clang 274f4a2713aSLionel Sambuc prints the associated :ref:`warning group <cl_diag_warning_groups>` 275f4a2713aSLionel Sambuc option name when outputting a warning diagnostic. For example, in 276f4a2713aSLionel Sambuc this output: 277f4a2713aSLionel Sambuc 278f4a2713aSLionel Sambuc :: 279f4a2713aSLionel Sambuc 280f4a2713aSLionel Sambuc test.c:28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens] 281f4a2713aSLionel Sambuc #endif bad 282f4a2713aSLionel Sambuc ^ 283f4a2713aSLionel Sambuc // 284f4a2713aSLionel Sambuc 285f4a2713aSLionel Sambuc Passing **-fno-diagnostics-show-option** will prevent Clang from 286f4a2713aSLionel Sambuc printing the [:ref:`-Wextra-tokens <opt_Wextra-tokens>`] information in 287f4a2713aSLionel Sambuc the diagnostic. This information tells you the flag needed to enable 288f4a2713aSLionel Sambuc or disable the diagnostic, either from the command line or through 289f4a2713aSLionel Sambuc :ref:`#pragma GCC diagnostic <pragma_GCC_diagnostic>`. 290f4a2713aSLionel Sambuc 291f4a2713aSLionel Sambuc.. _opt_fdiagnostics-show-category: 292f4a2713aSLionel Sambuc 293f4a2713aSLionel Sambuc.. option:: -fdiagnostics-show-category=none/id/name 294f4a2713aSLionel Sambuc 295f4a2713aSLionel Sambuc Enable printing category information in diagnostic line. 296f4a2713aSLionel Sambuc 297f4a2713aSLionel Sambuc This option, which defaults to "none", controls whether or not Clang 298f4a2713aSLionel Sambuc prints the category associated with a diagnostic when emitting it. 299f4a2713aSLionel Sambuc Each diagnostic may or many not have an associated category, if it 300f4a2713aSLionel Sambuc has one, it is listed in the diagnostic categorization field of the 301f4a2713aSLionel Sambuc diagnostic line (in the []'s). 302f4a2713aSLionel Sambuc 303f4a2713aSLionel Sambuc For example, a format string warning will produce these three 304f4a2713aSLionel Sambuc renditions based on the setting of this option: 305f4a2713aSLionel Sambuc 306f4a2713aSLionel Sambuc :: 307f4a2713aSLionel Sambuc 308f4a2713aSLionel Sambuc t.c:3:11: warning: conversion specifies type 'char *' but the argument has type 'int' [-Wformat] 309f4a2713aSLionel Sambuc t.c:3:11: warning: conversion specifies type 'char *' but the argument has type 'int' [-Wformat,1] 310f4a2713aSLionel Sambuc t.c:3:11: warning: conversion specifies type 'char *' but the argument has type 'int' [-Wformat,Format String] 311f4a2713aSLionel Sambuc 312f4a2713aSLionel Sambuc This category can be used by clients that want to group diagnostics 313f4a2713aSLionel Sambuc by category, so it should be a high level category. We want dozens 314f4a2713aSLionel Sambuc of these, not hundreds or thousands of them. 315f4a2713aSLionel Sambuc 316f4a2713aSLionel Sambuc.. _opt_fdiagnostics-fixit-info: 317f4a2713aSLionel Sambuc 318f4a2713aSLionel Sambuc**-f[no-]diagnostics-fixit-info** 319f4a2713aSLionel Sambuc Enable "FixIt" information in the diagnostics output. 320f4a2713aSLionel Sambuc 321f4a2713aSLionel Sambuc This option, which defaults to on, controls whether or not Clang 322f4a2713aSLionel Sambuc prints the information on how to fix a specific diagnostic 323f4a2713aSLionel Sambuc underneath it when it knows. For example, in this output: 324f4a2713aSLionel Sambuc 325f4a2713aSLionel Sambuc :: 326f4a2713aSLionel Sambuc 327f4a2713aSLionel Sambuc test.c:28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens] 328f4a2713aSLionel Sambuc #endif bad 329f4a2713aSLionel Sambuc ^ 330f4a2713aSLionel Sambuc // 331f4a2713aSLionel Sambuc 332f4a2713aSLionel Sambuc Passing **-fno-diagnostics-fixit-info** will prevent Clang from 333f4a2713aSLionel Sambuc printing the "//" line at the end of the message. This information 334f4a2713aSLionel Sambuc is useful for users who may not understand what is wrong, but can be 335f4a2713aSLionel Sambuc confusing for machine parsing. 336f4a2713aSLionel Sambuc 337f4a2713aSLionel Sambuc.. _opt_fdiagnostics-print-source-range-info: 338f4a2713aSLionel Sambuc 339f4a2713aSLionel Sambuc**-fdiagnostics-print-source-range-info** 340f4a2713aSLionel Sambuc Print machine parsable information about source ranges. 341f4a2713aSLionel Sambuc This option makes Clang print information about source ranges in a machine 342f4a2713aSLionel Sambuc parsable format after the file/line/column number information. The 343f4a2713aSLionel Sambuc information is a simple sequence of brace enclosed ranges, where each range 344f4a2713aSLionel Sambuc lists the start and end line/column locations. For example, in this output: 345f4a2713aSLionel Sambuc 346f4a2713aSLionel Sambuc :: 347f4a2713aSLionel Sambuc 348f4a2713aSLionel Sambuc exprs.c:47:15:{47:8-47:14}{47:17-47:24}: error: invalid operands to binary expression ('int *' and '_Complex float') 349f4a2713aSLionel Sambuc P = (P-42) + Gamma*4; 350f4a2713aSLionel Sambuc ~~~~~~ ^ ~~~~~~~ 351f4a2713aSLionel Sambuc 352f4a2713aSLionel Sambuc The {}'s are generated by -fdiagnostics-print-source-range-info. 353f4a2713aSLionel Sambuc 354f4a2713aSLionel Sambuc The printed column numbers count bytes from the beginning of the 355f4a2713aSLionel Sambuc line; take care if your source contains multibyte characters. 356f4a2713aSLionel Sambuc 357f4a2713aSLionel Sambuc.. option:: -fdiagnostics-parseable-fixits 358f4a2713aSLionel Sambuc 359f4a2713aSLionel Sambuc Print Fix-Its in a machine parseable form. 360f4a2713aSLionel Sambuc 361f4a2713aSLionel Sambuc This option makes Clang print available Fix-Its in a machine 362f4a2713aSLionel Sambuc parseable format at the end of diagnostics. The following example 363f4a2713aSLionel Sambuc illustrates the format: 364f4a2713aSLionel Sambuc 365f4a2713aSLionel Sambuc :: 366f4a2713aSLionel Sambuc 367f4a2713aSLionel Sambuc fix-it:"t.cpp":{7:25-7:29}:"Gamma" 368f4a2713aSLionel Sambuc 369f4a2713aSLionel Sambuc The range printed is a half-open range, so in this example the 370f4a2713aSLionel Sambuc characters at column 25 up to but not including column 29 on line 7 371f4a2713aSLionel Sambuc in t.cpp should be replaced with the string "Gamma". Either the 372f4a2713aSLionel Sambuc range or the replacement string may be empty (representing strict 373f4a2713aSLionel Sambuc insertions and strict erasures, respectively). Both the file name 374f4a2713aSLionel Sambuc and the insertion string escape backslash (as "\\\\"), tabs (as 375f4a2713aSLionel Sambuc "\\t"), newlines (as "\\n"), double quotes(as "\\"") and 376f4a2713aSLionel Sambuc non-printable characters (as octal "\\xxx"). 377f4a2713aSLionel Sambuc 378f4a2713aSLionel Sambuc The printed column numbers count bytes from the beginning of the 379f4a2713aSLionel Sambuc line; take care if your source contains multibyte characters. 380f4a2713aSLionel Sambuc 381f4a2713aSLionel Sambuc.. option:: -fno-elide-type 382f4a2713aSLionel Sambuc 383f4a2713aSLionel Sambuc Turns off elision in template type printing. 384f4a2713aSLionel Sambuc 385f4a2713aSLionel Sambuc The default for template type printing is to elide as many template 386f4a2713aSLionel Sambuc arguments as possible, removing those which are the same in both 387f4a2713aSLionel Sambuc template types, leaving only the differences. Adding this flag will 388f4a2713aSLionel Sambuc print all the template arguments. If supported by the terminal, 389f4a2713aSLionel Sambuc highlighting will still appear on differing arguments. 390f4a2713aSLionel Sambuc 391f4a2713aSLionel Sambuc Default: 392f4a2713aSLionel Sambuc 393f4a2713aSLionel Sambuc :: 394f4a2713aSLionel Sambuc 395f4a2713aSLionel Sambuc 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; 396f4a2713aSLionel Sambuc 397f4a2713aSLionel Sambuc -fno-elide-type: 398f4a2713aSLionel Sambuc 399f4a2713aSLionel Sambuc :: 400f4a2713aSLionel Sambuc 401f4a2713aSLionel Sambuc 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; 402f4a2713aSLionel Sambuc 403f4a2713aSLionel Sambuc.. option:: -fdiagnostics-show-template-tree 404f4a2713aSLionel Sambuc 405f4a2713aSLionel Sambuc Template type diffing prints a text tree. 406f4a2713aSLionel Sambuc 407f4a2713aSLionel Sambuc For diffing large templated types, this option will cause Clang to 408f4a2713aSLionel Sambuc display the templates as an indented text tree, one argument per 409f4a2713aSLionel Sambuc line, with differences marked inline. This is compatible with 410f4a2713aSLionel Sambuc -fno-elide-type. 411f4a2713aSLionel Sambuc 412f4a2713aSLionel Sambuc Default: 413f4a2713aSLionel Sambuc 414f4a2713aSLionel Sambuc :: 415f4a2713aSLionel Sambuc 416f4a2713aSLionel Sambuc 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; 417f4a2713aSLionel Sambuc 418f4a2713aSLionel Sambuc With :option:`-fdiagnostics-show-template-tree`: 419f4a2713aSLionel Sambuc 420f4a2713aSLionel Sambuc :: 421f4a2713aSLionel Sambuc 422f4a2713aSLionel Sambuc t.cc:4:5: note: candidate function not viable: no known conversion for 1st argument; 423f4a2713aSLionel Sambuc vector< 424f4a2713aSLionel Sambuc map< 425f4a2713aSLionel Sambuc [...], 426f4a2713aSLionel Sambuc map< 427f4a2713aSLionel Sambuc [float != double], 428f4a2713aSLionel Sambuc [...]>>> 429f4a2713aSLionel Sambuc 430f4a2713aSLionel Sambuc.. _cl_diag_warning_groups: 431f4a2713aSLionel Sambuc 432f4a2713aSLionel SambucIndividual Warning Groups 433f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^ 434f4a2713aSLionel Sambuc 435f4a2713aSLionel SambucTODO: Generate this from tblgen. Define one anchor per warning group. 436f4a2713aSLionel Sambuc 437f4a2713aSLionel Sambuc.. _opt_wextra-tokens: 438f4a2713aSLionel Sambuc 439f4a2713aSLionel Sambuc.. option:: -Wextra-tokens 440f4a2713aSLionel Sambuc 441f4a2713aSLionel Sambuc Warn about excess tokens at the end of a preprocessor directive. 442f4a2713aSLionel Sambuc 443f4a2713aSLionel Sambuc This option, which defaults to on, enables warnings about extra 444f4a2713aSLionel Sambuc tokens at the end of preprocessor directives. For example: 445f4a2713aSLionel Sambuc 446f4a2713aSLionel Sambuc :: 447f4a2713aSLionel Sambuc 448f4a2713aSLionel Sambuc test.c:28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens] 449f4a2713aSLionel Sambuc #endif bad 450f4a2713aSLionel Sambuc ^ 451f4a2713aSLionel Sambuc 452f4a2713aSLionel Sambuc These extra tokens are not strictly conforming, and are usually best 453f4a2713aSLionel Sambuc handled by commenting them out. 454f4a2713aSLionel Sambuc 455f4a2713aSLionel Sambuc.. option:: -Wambiguous-member-template 456f4a2713aSLionel Sambuc 457f4a2713aSLionel Sambuc Warn about unqualified uses of a member template whose name resolves to 458f4a2713aSLionel Sambuc another template at the location of the use. 459f4a2713aSLionel Sambuc 460f4a2713aSLionel Sambuc This option, which defaults to on, enables a warning in the 461f4a2713aSLionel Sambuc following code: 462f4a2713aSLionel Sambuc 463f4a2713aSLionel Sambuc :: 464f4a2713aSLionel Sambuc 465f4a2713aSLionel Sambuc template<typename T> struct set{}; 466f4a2713aSLionel Sambuc template<typename T> struct trait { typedef const T& type; }; 467f4a2713aSLionel Sambuc struct Value { 468f4a2713aSLionel Sambuc template<typename T> void set(typename trait<T>::type value) {} 469f4a2713aSLionel Sambuc }; 470f4a2713aSLionel Sambuc void foo() { 471f4a2713aSLionel Sambuc Value v; 472f4a2713aSLionel Sambuc v.set<double>(3.2); 473f4a2713aSLionel Sambuc } 474f4a2713aSLionel Sambuc 475f4a2713aSLionel Sambuc C++ [basic.lookup.classref] requires this to be an error, but, 476f4a2713aSLionel Sambuc because it's hard to work around, Clang downgrades it to a warning 477f4a2713aSLionel Sambuc as an extension. 478f4a2713aSLionel Sambuc 479f4a2713aSLionel Sambuc.. option:: -Wbind-to-temporary-copy 480f4a2713aSLionel Sambuc 481f4a2713aSLionel Sambuc Warn about an unusable copy constructor when binding a reference to a 482f4a2713aSLionel Sambuc temporary. 483f4a2713aSLionel Sambuc 484*0a6a1f1dSLionel Sambuc This option enables warnings about binding a 485f4a2713aSLionel Sambuc reference to a temporary when the temporary doesn't have a usable 486f4a2713aSLionel Sambuc copy constructor. For example: 487f4a2713aSLionel Sambuc 488f4a2713aSLionel Sambuc :: 489f4a2713aSLionel Sambuc 490f4a2713aSLionel Sambuc struct NonCopyable { 491f4a2713aSLionel Sambuc NonCopyable(); 492f4a2713aSLionel Sambuc private: 493f4a2713aSLionel Sambuc NonCopyable(const NonCopyable&); 494f4a2713aSLionel Sambuc }; 495f4a2713aSLionel Sambuc void foo(const NonCopyable&); 496f4a2713aSLionel Sambuc void bar() { 497f4a2713aSLionel Sambuc foo(NonCopyable()); // Disallowed in C++98; allowed in C++11. 498f4a2713aSLionel Sambuc } 499f4a2713aSLionel Sambuc 500f4a2713aSLionel Sambuc :: 501f4a2713aSLionel Sambuc 502f4a2713aSLionel Sambuc struct NonCopyable2 { 503f4a2713aSLionel Sambuc NonCopyable2(); 504f4a2713aSLionel Sambuc NonCopyable2(NonCopyable2&); 505f4a2713aSLionel Sambuc }; 506f4a2713aSLionel Sambuc void foo(const NonCopyable2&); 507f4a2713aSLionel Sambuc void bar() { 508f4a2713aSLionel Sambuc foo(NonCopyable2()); // Disallowed in C++98; allowed in C++11. 509f4a2713aSLionel Sambuc } 510f4a2713aSLionel Sambuc 511f4a2713aSLionel Sambuc Note that if ``NonCopyable2::NonCopyable2()`` has a default argument 512f4a2713aSLionel Sambuc whose instantiation produces a compile error, that error will still 513f4a2713aSLionel Sambuc be a hard error in C++98 mode even if this warning is turned off. 514f4a2713aSLionel Sambuc 515f4a2713aSLionel SambucOptions to Control Clang Crash Diagnostics 516f4a2713aSLionel Sambuc------------------------------------------ 517f4a2713aSLionel Sambuc 518f4a2713aSLionel SambucAs unbelievable as it may sound, Clang does crash from time to time. 519f4a2713aSLionel SambucGenerally, this only occurs to those living on the `bleeding 520f4a2713aSLionel Sambucedge <http://llvm.org/releases/download.html#svn>`_. Clang goes to great 521f4a2713aSLionel Sambuclengths to assist you in filing a bug report. Specifically, Clang 522f4a2713aSLionel Sambucgenerates preprocessed source file(s) and associated run script(s) upon 523f4a2713aSLionel Sambuca crash. These files should be attached to a bug report to ease 524f4a2713aSLionel Sambucreproducibility of the failure. Below are the command line options to 525f4a2713aSLionel Sambuccontrol the crash diagnostics. 526f4a2713aSLionel Sambuc 527f4a2713aSLionel Sambuc.. option:: -fno-crash-diagnostics 528f4a2713aSLionel Sambuc 529f4a2713aSLionel Sambuc Disable auto-generation of preprocessed source files during a clang crash. 530f4a2713aSLionel Sambuc 531f4a2713aSLionel SambucThe -fno-crash-diagnostics flag can be helpful for speeding the process 532f4a2713aSLionel Sambucof generating a delta reduced test case. 533f4a2713aSLionel Sambuc 534*0a6a1f1dSLionel SambucOptions to Emit Optimization Reports 535*0a6a1f1dSLionel Sambuc------------------------------------ 536*0a6a1f1dSLionel Sambuc 537*0a6a1f1dSLionel SambucOptimization reports trace, at a high-level, all the major decisions 538*0a6a1f1dSLionel Sambucdone by compiler transformations. For instance, when the inliner 539*0a6a1f1dSLionel Sambucdecides to inline function ``foo()`` into ``bar()``, or the loop unroller 540*0a6a1f1dSLionel Sambucdecides to unroll a loop N times, or the vectorizer decides to 541*0a6a1f1dSLionel Sambucvectorize a loop body. 542*0a6a1f1dSLionel Sambuc 543*0a6a1f1dSLionel SambucClang offers a family of flags which the optimizers can use to emit 544*0a6a1f1dSLionel Sambuca diagnostic in three cases: 545*0a6a1f1dSLionel Sambuc 546*0a6a1f1dSLionel Sambuc1. When the pass makes a transformation (:option:`-Rpass`). 547*0a6a1f1dSLionel Sambuc 548*0a6a1f1dSLionel Sambuc2. When the pass fails to make a transformation (:option:`-Rpass-missed`). 549*0a6a1f1dSLionel Sambuc 550*0a6a1f1dSLionel Sambuc3. When the pass determines whether or not to make a transformation 551*0a6a1f1dSLionel Sambuc (:option:`-Rpass-analysis`). 552*0a6a1f1dSLionel Sambuc 553*0a6a1f1dSLionel SambucNOTE: Although the discussion below focuses on :option:`-Rpass`, the exact 554*0a6a1f1dSLionel Sambucsame options apply to :option:`-Rpass-missed` and :option:`-Rpass-analysis`. 555*0a6a1f1dSLionel Sambuc 556*0a6a1f1dSLionel SambucSince there are dozens of passes inside the compiler, each of these flags 557*0a6a1f1dSLionel Sambuctake a regular expression that identifies the name of the pass which should 558*0a6a1f1dSLionel Sambucemit the associated diagnostic. For example, to get a report from the inliner, 559*0a6a1f1dSLionel Sambuccompile the code with: 560*0a6a1f1dSLionel Sambuc 561*0a6a1f1dSLionel Sambuc.. code-block:: console 562*0a6a1f1dSLionel Sambuc 563*0a6a1f1dSLionel Sambuc $ clang -O2 -Rpass=inline code.cc -o code 564*0a6a1f1dSLionel Sambuc code.cc:4:25: remark: foo inlined into bar [-Rpass=inline] 565*0a6a1f1dSLionel Sambuc int bar(int j) { return foo(j, j - 2); } 566*0a6a1f1dSLionel Sambuc ^ 567*0a6a1f1dSLionel Sambuc 568*0a6a1f1dSLionel SambucNote that remarks from the inliner are identified with `[-Rpass=inline]`. 569*0a6a1f1dSLionel SambucTo request a report from every optimization pass, you should use 570*0a6a1f1dSLionel Sambuc:option:`-Rpass=.*` (in fact, you can use any valid POSIX regular 571*0a6a1f1dSLionel Sambucexpression). However, do not expect a report from every transformation 572*0a6a1f1dSLionel Sambucmade by the compiler. Optimization remarks do not really make sense 573*0a6a1f1dSLionel Sambucoutside of the major transformations (e.g., inlining, vectorization, 574*0a6a1f1dSLionel Sambucloop optimizations) and not every optimization pass supports this 575*0a6a1f1dSLionel Sambucfeature. 576*0a6a1f1dSLionel Sambuc 577*0a6a1f1dSLionel SambucCurrent limitations 578*0a6a1f1dSLionel Sambuc^^^^^^^^^^^^^^^^^^^ 579*0a6a1f1dSLionel Sambuc 580*0a6a1f1dSLionel Sambuc1. Optimization remarks that refer to function names will display the 581*0a6a1f1dSLionel Sambuc mangled name of the function. Since these remarks are emitted by the 582*0a6a1f1dSLionel Sambuc back end of the compiler, it does not know anything about the input 583*0a6a1f1dSLionel Sambuc language, nor its mangling rules. 584*0a6a1f1dSLionel Sambuc 585*0a6a1f1dSLionel Sambuc2. Some source locations are not displayed correctly. The front end has 586*0a6a1f1dSLionel Sambuc a more detailed source location tracking than the locations included 587*0a6a1f1dSLionel Sambuc in the debug info (e.g., the front end can locate code inside macro 588*0a6a1f1dSLionel Sambuc expansions). However, the locations used by :option:`-Rpass` are 589*0a6a1f1dSLionel Sambuc translated from debug annotations. That translation can be lossy, 590*0a6a1f1dSLionel Sambuc which results in some remarks having no location information. 591*0a6a1f1dSLionel Sambuc 592*0a6a1f1dSLionel Sambuc 593f4a2713aSLionel SambucLanguage and Target-Independent Features 594f4a2713aSLionel Sambuc======================================== 595f4a2713aSLionel Sambuc 596f4a2713aSLionel SambucControlling Errors and Warnings 597f4a2713aSLionel Sambuc------------------------------- 598f4a2713aSLionel Sambuc 599f4a2713aSLionel SambucClang provides a number of ways to control which code constructs cause 600f4a2713aSLionel Sambucit to emit errors and warning messages, and how they are displayed to 601f4a2713aSLionel Sambucthe console. 602f4a2713aSLionel Sambuc 603f4a2713aSLionel SambucControlling How Clang Displays Diagnostics 604f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 605f4a2713aSLionel Sambuc 606f4a2713aSLionel SambucWhen Clang emits a diagnostic, it includes rich information in the 607f4a2713aSLionel Sambucoutput, and gives you fine-grain control over which information is 608f4a2713aSLionel Sambucprinted. Clang has the ability to print this information, and these are 609f4a2713aSLionel Sambucthe options that control it: 610f4a2713aSLionel Sambuc 611f4a2713aSLionel Sambuc#. A file/line/column indicator that shows exactly where the diagnostic 612f4a2713aSLionel Sambuc occurs in your code [:ref:`-fshow-column <opt_fshow-column>`, 613f4a2713aSLionel Sambuc :ref:`-fshow-source-location <opt_fshow-source-location>`]. 614f4a2713aSLionel Sambuc#. A categorization of the diagnostic as a note, warning, error, or 615f4a2713aSLionel Sambuc fatal error. 616f4a2713aSLionel Sambuc#. A text string that describes what the problem is. 617f4a2713aSLionel Sambuc#. An option that indicates how to control the diagnostic (for 618f4a2713aSLionel Sambuc diagnostics that support it) 619f4a2713aSLionel Sambuc [:ref:`-fdiagnostics-show-option <opt_fdiagnostics-show-option>`]. 620f4a2713aSLionel Sambuc#. A :ref:`high-level category <diagnostics_categories>` for the diagnostic 621f4a2713aSLionel Sambuc for clients that want to group diagnostics by class (for diagnostics 622f4a2713aSLionel Sambuc that support it) 623f4a2713aSLionel Sambuc [:ref:`-fdiagnostics-show-category <opt_fdiagnostics-show-category>`]. 624f4a2713aSLionel Sambuc#. The line of source code that the issue occurs on, along with a caret 625f4a2713aSLionel Sambuc and ranges that indicate the important locations 626f4a2713aSLionel Sambuc [:ref:`-fcaret-diagnostics <opt_fcaret-diagnostics>`]. 627f4a2713aSLionel Sambuc#. "FixIt" information, which is a concise explanation of how to fix the 628f4a2713aSLionel Sambuc problem (when Clang is certain it knows) 629f4a2713aSLionel Sambuc [:ref:`-fdiagnostics-fixit-info <opt_fdiagnostics-fixit-info>`]. 630f4a2713aSLionel Sambuc#. A machine-parsable representation of the ranges involved (off by 631f4a2713aSLionel Sambuc default) 632f4a2713aSLionel Sambuc [:ref:`-fdiagnostics-print-source-range-info <opt_fdiagnostics-print-source-range-info>`]. 633f4a2713aSLionel Sambuc 634f4a2713aSLionel SambucFor more information please see :ref:`Formatting of 635f4a2713aSLionel SambucDiagnostics <cl_diag_formatting>`. 636f4a2713aSLionel Sambuc 637f4a2713aSLionel SambucDiagnostic Mappings 638f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^ 639f4a2713aSLionel Sambuc 640f4a2713aSLionel SambucAll diagnostics are mapped into one of these 5 classes: 641f4a2713aSLionel Sambuc 642f4a2713aSLionel Sambuc- Ignored 643f4a2713aSLionel Sambuc- Note 644*0a6a1f1dSLionel Sambuc- Remark 645f4a2713aSLionel Sambuc- Warning 646f4a2713aSLionel Sambuc- Error 647f4a2713aSLionel Sambuc- Fatal 648f4a2713aSLionel Sambuc 649f4a2713aSLionel Sambuc.. _diagnostics_categories: 650f4a2713aSLionel Sambuc 651f4a2713aSLionel SambucDiagnostic Categories 652f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^ 653f4a2713aSLionel Sambuc 654f4a2713aSLionel SambucThough not shown by default, diagnostics may each be associated with a 655f4a2713aSLionel Sambuchigh-level category. This category is intended to make it possible to 656f4a2713aSLionel Sambuctriage builds that produce a large number of errors or warnings in a 657f4a2713aSLionel Sambucgrouped way. 658f4a2713aSLionel Sambuc 659f4a2713aSLionel SambucCategories are not shown by default, but they can be turned on with the 660f4a2713aSLionel Sambuc:ref:`-fdiagnostics-show-category <opt_fdiagnostics-show-category>` option. 661f4a2713aSLionel SambucWhen set to "``name``", the category is printed textually in the 662f4a2713aSLionel Sambucdiagnostic output. When it is set to "``id``", a category number is 663f4a2713aSLionel Sambucprinted. The mapping of category names to category id's can be obtained 664f4a2713aSLionel Sambucby running '``clang --print-diagnostic-categories``'. 665f4a2713aSLionel Sambuc 666f4a2713aSLionel SambucControlling Diagnostics via Command Line Flags 667f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 668f4a2713aSLionel Sambuc 669f4a2713aSLionel SambucTODO: -W flags, -pedantic, etc 670f4a2713aSLionel Sambuc 671f4a2713aSLionel Sambuc.. _pragma_gcc_diagnostic: 672f4a2713aSLionel Sambuc 673f4a2713aSLionel SambucControlling Diagnostics via Pragmas 674f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 675f4a2713aSLionel Sambuc 676f4a2713aSLionel SambucClang can also control what diagnostics are enabled through the use of 677f4a2713aSLionel Sambucpragmas in the source code. This is useful for turning off specific 678f4a2713aSLionel Sambucwarnings in a section of source code. Clang supports GCC's pragma for 679f4a2713aSLionel Sambuccompatibility with existing source code, as well as several extensions. 680f4a2713aSLionel Sambuc 681f4a2713aSLionel SambucThe pragma may control any warning that can be used from the command 682f4a2713aSLionel Sambucline. Warnings may be set to ignored, warning, error, or fatal. The 683f4a2713aSLionel Sambucfollowing example code will tell Clang or GCC to ignore the -Wall 684f4a2713aSLionel Sambucwarnings: 685f4a2713aSLionel Sambuc 686f4a2713aSLionel Sambuc.. code-block:: c 687f4a2713aSLionel Sambuc 688f4a2713aSLionel Sambuc #pragma GCC diagnostic ignored "-Wall" 689f4a2713aSLionel Sambuc 690f4a2713aSLionel SambucIn addition to all of the functionality provided by GCC's pragma, Clang 691f4a2713aSLionel Sambucalso allows you to push and pop the current warning state. This is 692f4a2713aSLionel Sambucparticularly useful when writing a header file that will be compiled by 693f4a2713aSLionel Sambucother people, because you don't know what warning flags they build with. 694f4a2713aSLionel Sambuc 695f4a2713aSLionel SambucIn the below example :option:`-Wmultichar` is ignored for only a single line of 696f4a2713aSLionel Sambuccode, after which the diagnostics return to whatever state had previously 697f4a2713aSLionel Sambucexisted. 698f4a2713aSLionel Sambuc 699f4a2713aSLionel Sambuc.. code-block:: c 700f4a2713aSLionel Sambuc 701f4a2713aSLionel Sambuc #pragma clang diagnostic push 702f4a2713aSLionel Sambuc #pragma clang diagnostic ignored "-Wmultichar" 703f4a2713aSLionel Sambuc 704f4a2713aSLionel Sambuc char b = 'df'; // no warning. 705f4a2713aSLionel Sambuc 706f4a2713aSLionel Sambuc #pragma clang diagnostic pop 707f4a2713aSLionel Sambuc 708f4a2713aSLionel SambucThe push and pop pragmas will save and restore the full diagnostic state 709f4a2713aSLionel Sambucof the compiler, regardless of how it was set. That means that it is 710f4a2713aSLionel Sambucpossible to use push and pop around GCC compatible diagnostics and Clang 711f4a2713aSLionel Sambucwill push and pop them appropriately, while GCC will ignore the pushes 712f4a2713aSLionel Sambucand pops as unknown pragmas. It should be noted that while Clang 713f4a2713aSLionel Sambucsupports the GCC pragma, Clang and GCC do not support the exact same set 714f4a2713aSLionel Sambucof warnings, so even when using GCC compatible #pragmas there is no 715f4a2713aSLionel Sambucguarantee that they will have identical behaviour on both compilers. 716f4a2713aSLionel Sambuc 717f4a2713aSLionel SambucIn addition to controlling warnings and errors generated by the compiler, it is 718f4a2713aSLionel Sambucpossible to generate custom warning and error messages through the following 719f4a2713aSLionel Sambucpragmas: 720f4a2713aSLionel Sambuc 721f4a2713aSLionel Sambuc.. code-block:: c 722f4a2713aSLionel Sambuc 723f4a2713aSLionel Sambuc // The following will produce warning messages 724f4a2713aSLionel Sambuc #pragma message "some diagnostic message" 725f4a2713aSLionel Sambuc #pragma GCC warning "TODO: replace deprecated feature" 726f4a2713aSLionel Sambuc 727f4a2713aSLionel Sambuc // The following will produce an error message 728f4a2713aSLionel Sambuc #pragma GCC error "Not supported" 729f4a2713aSLionel Sambuc 730f4a2713aSLionel SambucThese pragmas operate similarly to the ``#warning`` and ``#error`` preprocessor 731f4a2713aSLionel Sambucdirectives, except that they may also be embedded into preprocessor macros via 732f4a2713aSLionel Sambucthe C99 ``_Pragma`` operator, for example: 733f4a2713aSLionel Sambuc 734f4a2713aSLionel Sambuc.. code-block:: c 735f4a2713aSLionel Sambuc 736f4a2713aSLionel Sambuc #define STR(X) #X 737f4a2713aSLionel Sambuc #define DEFER(M,...) M(__VA_ARGS__) 738f4a2713aSLionel Sambuc #define CUSTOM_ERROR(X) _Pragma(STR(GCC error(X " at line " DEFER(STR,__LINE__)))) 739f4a2713aSLionel Sambuc 740f4a2713aSLionel Sambuc CUSTOM_ERROR("Feature not available"); 741f4a2713aSLionel Sambuc 742f4a2713aSLionel SambucControlling Diagnostics in System Headers 743f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 744f4a2713aSLionel Sambuc 745f4a2713aSLionel SambucWarnings are suppressed when they occur in system headers. By default, 746f4a2713aSLionel Sambucan included file is treated as a system header if it is found in an 747f4a2713aSLionel Sambucinclude path specified by ``-isystem``, but this can be overridden in 748f4a2713aSLionel Sambucseveral ways. 749f4a2713aSLionel Sambuc 750f4a2713aSLionel SambucThe ``system_header`` pragma can be used to mark the current file as 751f4a2713aSLionel Sambucbeing a system header. No warnings will be produced from the location of 752f4a2713aSLionel Sambucthe pragma onwards within the same file. 753f4a2713aSLionel Sambuc 754f4a2713aSLionel Sambuc.. code-block:: c 755f4a2713aSLionel Sambuc 756f4a2713aSLionel Sambuc char a = 'xy'; // warning 757f4a2713aSLionel Sambuc 758f4a2713aSLionel Sambuc #pragma clang system_header 759f4a2713aSLionel Sambuc 760f4a2713aSLionel Sambuc char b = 'ab'; // no warning 761f4a2713aSLionel Sambuc 762*0a6a1f1dSLionel SambucThe :option:`--system-header-prefix=` and :option:`--no-system-header-prefix=` 763*0a6a1f1dSLionel Sambuccommand-line arguments can be used to override whether subsets of an include 764*0a6a1f1dSLionel Sambucpath are treated as system headers. When the name in a ``#include`` directive 765*0a6a1f1dSLionel Sambucis found within a header search path and starts with a system prefix, the 766f4a2713aSLionel Sambucheader is treated as a system header. The last prefix on the 767f4a2713aSLionel Sambuccommand-line which matches the specified header name takes precedence. 768f4a2713aSLionel SambucFor instance: 769f4a2713aSLionel Sambuc 770f4a2713aSLionel Sambuc.. code-block:: console 771f4a2713aSLionel Sambuc 772*0a6a1f1dSLionel Sambuc $ clang -Ifoo -isystem bar --system-header-prefix=x/ \ 773*0a6a1f1dSLionel Sambuc --no-system-header-prefix=x/y/ 774f4a2713aSLionel Sambuc 775f4a2713aSLionel SambucHere, ``#include "x/a.h"`` is treated as including a system header, even 776f4a2713aSLionel Sambucif the header is found in ``foo``, and ``#include "x/y/b.h"`` is treated 777f4a2713aSLionel Sambucas not including a system header, even if the header is found in 778f4a2713aSLionel Sambuc``bar``. 779f4a2713aSLionel Sambuc 780f4a2713aSLionel SambucA ``#include`` directive which finds a file relative to the current 781f4a2713aSLionel Sambucdirectory is treated as including a system header if the including file 782f4a2713aSLionel Sambucis treated as a system header. 783f4a2713aSLionel Sambuc 784f4a2713aSLionel Sambuc.. _diagnostics_enable_everything: 785f4a2713aSLionel Sambuc 786*0a6a1f1dSLionel SambucEnabling All Diagnostics 787*0a6a1f1dSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 788f4a2713aSLionel Sambuc 789f4a2713aSLionel SambucIn addition to the traditional ``-W`` flags, one can enable **all** 790*0a6a1f1dSLionel Sambucdiagnostics by passing :option:`-Weverything`. This works as expected 791*0a6a1f1dSLionel Sambucwith 792f4a2713aSLionel Sambuc:option:`-Werror`, and also includes the warnings from :option:`-pedantic`. 793f4a2713aSLionel Sambuc 794f4a2713aSLionel SambucNote that when combined with :option:`-w` (which disables all warnings), that 795f4a2713aSLionel Sambucflag wins. 796f4a2713aSLionel Sambuc 797f4a2713aSLionel SambucControlling Static Analyzer Diagnostics 798f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 799f4a2713aSLionel Sambuc 800f4a2713aSLionel SambucWhile not strictly part of the compiler, the diagnostics from Clang's 801f4a2713aSLionel Sambuc`static analyzer <http://clang-analyzer.llvm.org>`_ can also be 802f4a2713aSLionel Sambucinfluenced by the user via changes to the source code. See the available 803f4a2713aSLionel Sambuc`annotations <http://clang-analyzer.llvm.org/annotations.html>`_ and the 804f4a2713aSLionel Sambucanalyzer's `FAQ 805f4a2713aSLionel Sambucpage <http://clang-analyzer.llvm.org/faq.html#exclude_code>`_ for more 806f4a2713aSLionel Sambucinformation. 807f4a2713aSLionel Sambuc 808f4a2713aSLionel Sambuc.. _usersmanual-precompiled-headers: 809f4a2713aSLionel Sambuc 810f4a2713aSLionel SambucPrecompiled Headers 811f4a2713aSLionel Sambuc------------------- 812f4a2713aSLionel Sambuc 813f4a2713aSLionel Sambuc`Precompiled headers <http://en.wikipedia.org/wiki/Precompiled_header>`__ 814f4a2713aSLionel Sambucare a general approach employed by many compilers to reduce compilation 815f4a2713aSLionel Sambuctime. The underlying motivation of the approach is that it is common for 816f4a2713aSLionel Sambucthe same (and often large) header files to be included by multiple 817f4a2713aSLionel Sambucsource files. Consequently, compile times can often be greatly improved 818f4a2713aSLionel Sambucby caching some of the (redundant) work done by a compiler to process 819f4a2713aSLionel Sambucheaders. Precompiled header files, which represent one of many ways to 820f4a2713aSLionel Sambucimplement this optimization, are literally files that represent an 821f4a2713aSLionel Sambucon-disk cache that contains the vital information necessary to reduce 822f4a2713aSLionel Sambucsome of the work needed to process a corresponding header file. While 823f4a2713aSLionel Sambucdetails of precompiled headers vary between compilers, precompiled 824f4a2713aSLionel Sambucheaders have been shown to be highly effective at speeding up program 825*0a6a1f1dSLionel Sambuccompilation on systems with very large system headers (e.g., Mac OS X). 826f4a2713aSLionel Sambuc 827f4a2713aSLionel SambucGenerating a PCH File 828f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^ 829f4a2713aSLionel Sambuc 830f4a2713aSLionel SambucTo generate a PCH file using Clang, one invokes Clang with the 831f4a2713aSLionel Sambuc:option:`-x <language>-header` option. This mirrors the interface in GCC 832f4a2713aSLionel Sambucfor generating PCH files: 833f4a2713aSLionel Sambuc 834f4a2713aSLionel Sambuc.. code-block:: console 835f4a2713aSLionel Sambuc 836f4a2713aSLionel Sambuc $ gcc -x c-header test.h -o test.h.gch 837f4a2713aSLionel Sambuc $ clang -x c-header test.h -o test.h.pch 838f4a2713aSLionel Sambuc 839f4a2713aSLionel SambucUsing a PCH File 840f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^ 841f4a2713aSLionel Sambuc 842f4a2713aSLionel SambucA PCH file can then be used as a prefix header when a :option:`-include` 843f4a2713aSLionel Sambucoption is passed to ``clang``: 844f4a2713aSLionel Sambuc 845f4a2713aSLionel Sambuc.. code-block:: console 846f4a2713aSLionel Sambuc 847f4a2713aSLionel Sambuc $ clang -include test.h test.c -o test 848f4a2713aSLionel Sambuc 849f4a2713aSLionel SambucThe ``clang`` driver will first check if a PCH file for ``test.h`` is 850f4a2713aSLionel Sambucavailable; if so, the contents of ``test.h`` (and the files it includes) 851f4a2713aSLionel Sambucwill be processed from the PCH file. Otherwise, Clang falls back to 852f4a2713aSLionel Sambucdirectly processing the content of ``test.h``. This mirrors the behavior 853f4a2713aSLionel Sambucof GCC. 854f4a2713aSLionel Sambuc 855f4a2713aSLionel Sambuc.. note:: 856f4a2713aSLionel Sambuc 857f4a2713aSLionel Sambuc Clang does *not* automatically use PCH files for headers that are directly 858f4a2713aSLionel Sambuc included within a source file. For example: 859f4a2713aSLionel Sambuc 860f4a2713aSLionel Sambuc .. code-block:: console 861f4a2713aSLionel Sambuc 862f4a2713aSLionel Sambuc $ clang -x c-header test.h -o test.h.pch 863f4a2713aSLionel Sambuc $ cat test.c 864f4a2713aSLionel Sambuc #include "test.h" 865f4a2713aSLionel Sambuc $ clang test.c -o test 866f4a2713aSLionel Sambuc 867f4a2713aSLionel Sambuc In this example, ``clang`` will not automatically use the PCH file for 868f4a2713aSLionel Sambuc ``test.h`` since ``test.h`` was included directly in the source file and not 869f4a2713aSLionel Sambuc specified on the command line using :option:`-include`. 870f4a2713aSLionel Sambuc 871f4a2713aSLionel SambucRelocatable PCH Files 872f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^ 873f4a2713aSLionel Sambuc 874f4a2713aSLionel SambucIt is sometimes necessary to build a precompiled header from headers 875f4a2713aSLionel Sambucthat are not yet in their final, installed locations. For example, one 876f4a2713aSLionel Sambucmight build a precompiled header within the build tree that is then 877f4a2713aSLionel Sambucmeant to be installed alongside the headers. Clang permits the creation 878f4a2713aSLionel Sambucof "relocatable" precompiled headers, which are built with a given path 879f4a2713aSLionel Sambuc(into the build directory) and can later be used from an installed 880f4a2713aSLionel Sambuclocation. 881f4a2713aSLionel Sambuc 882f4a2713aSLionel SambucTo build a relocatable precompiled header, place your headers into a 883f4a2713aSLionel Sambucsubdirectory whose structure mimics the installed location. For example, 884f4a2713aSLionel Sambucif you want to build a precompiled header for the header ``mylib.h`` 885f4a2713aSLionel Sambucthat will be installed into ``/usr/include``, create a subdirectory 886f4a2713aSLionel Sambuc``build/usr/include`` and place the header ``mylib.h`` into that 887f4a2713aSLionel Sambucsubdirectory. If ``mylib.h`` depends on other headers, then they can be 888f4a2713aSLionel Sambucstored within ``build/usr/include`` in a way that mimics the installed 889f4a2713aSLionel Sambuclocation. 890f4a2713aSLionel Sambuc 891f4a2713aSLionel SambucBuilding a relocatable precompiled header requires two additional 892f4a2713aSLionel Sambucarguments. First, pass the ``--relocatable-pch`` flag to indicate that 893f4a2713aSLionel Sambucthe resulting PCH file should be relocatable. Second, pass 894f4a2713aSLionel Sambuc:option:`-isysroot /path/to/build`, which makes all includes for your library 895f4a2713aSLionel Sambucrelative to the build directory. For example: 896f4a2713aSLionel Sambuc 897f4a2713aSLionel Sambuc.. code-block:: console 898f4a2713aSLionel Sambuc 899f4a2713aSLionel Sambuc # clang -x c-header --relocatable-pch -isysroot /path/to/build /path/to/build/mylib.h mylib.h.pch 900f4a2713aSLionel Sambuc 901f4a2713aSLionel SambucWhen loading the relocatable PCH file, the various headers used in the 902f4a2713aSLionel SambucPCH file are found from the system header root. For example, ``mylib.h`` 903f4a2713aSLionel Sambuccan be found in ``/usr/include/mylib.h``. If the headers are installed 904f4a2713aSLionel Sambucin some other system root, the :option:`-isysroot` option can be used provide 905f4a2713aSLionel Sambuca different system root from which the headers will be based. For 906f4a2713aSLionel Sambucexample, :option:`-isysroot /Developer/SDKs/MacOSX10.4u.sdk` will look for 907f4a2713aSLionel Sambuc``mylib.h`` in ``/Developer/SDKs/MacOSX10.4u.sdk/usr/include/mylib.h``. 908f4a2713aSLionel Sambuc 909f4a2713aSLionel SambucRelocatable precompiled headers are intended to be used in a limited 910f4a2713aSLionel Sambucnumber of cases where the compilation environment is tightly controlled 911f4a2713aSLionel Sambucand the precompiled header cannot be generated after headers have been 912f4a2713aSLionel Sambucinstalled. 913f4a2713aSLionel Sambuc 914f4a2713aSLionel SambucControlling Code Generation 915f4a2713aSLionel Sambuc--------------------------- 916f4a2713aSLionel Sambuc 917f4a2713aSLionel SambucClang provides a number of ways to control code generation. The options 918f4a2713aSLionel Sambucare listed below. 919f4a2713aSLionel Sambuc 920f4a2713aSLionel Sambuc**-f[no-]sanitize=check1,check2,...** 921f4a2713aSLionel Sambuc Turn on runtime checks for various forms of undefined or suspicious 922f4a2713aSLionel Sambuc behavior. 923f4a2713aSLionel Sambuc 924f4a2713aSLionel Sambuc This option controls whether Clang adds runtime checks for various 925f4a2713aSLionel Sambuc forms of undefined or suspicious behavior, and is disabled by 926f4a2713aSLionel Sambuc default. If a check fails, a diagnostic message is produced at 927f4a2713aSLionel Sambuc runtime explaining the problem. The main checks are: 928f4a2713aSLionel Sambuc 929f4a2713aSLionel Sambuc - .. _opt_fsanitize_address: 930f4a2713aSLionel Sambuc 931f4a2713aSLionel Sambuc ``-fsanitize=address``: 932f4a2713aSLionel Sambuc :doc:`AddressSanitizer`, a memory error 933f4a2713aSLionel Sambuc detector. 934f4a2713aSLionel Sambuc - ``-fsanitize=integer``: Enables checks for undefined or 935f4a2713aSLionel Sambuc suspicious integer behavior. 936f4a2713aSLionel Sambuc - .. _opt_fsanitize_thread: 937f4a2713aSLionel Sambuc 938f4a2713aSLionel Sambuc ``-fsanitize=thread``: :doc:`ThreadSanitizer`, a data race detector. 939f4a2713aSLionel Sambuc - .. _opt_fsanitize_memory: 940f4a2713aSLionel Sambuc 941f4a2713aSLionel Sambuc ``-fsanitize=memory``: :doc:`MemorySanitizer`, 942f4a2713aSLionel Sambuc an *experimental* detector of uninitialized reads. Not ready for 943f4a2713aSLionel Sambuc widespread use. 944f4a2713aSLionel Sambuc - .. _opt_fsanitize_undefined: 945f4a2713aSLionel Sambuc 946f4a2713aSLionel Sambuc ``-fsanitize=undefined``: Fast and compatible undefined behavior 947f4a2713aSLionel Sambuc checker. Enables the undefined behavior checks that have small 948f4a2713aSLionel Sambuc runtime cost and no impact on address space layout or ABI. This 949f4a2713aSLionel Sambuc includes all of the checks listed below other than 950f4a2713aSLionel Sambuc ``unsigned-integer-overflow``. 951f4a2713aSLionel Sambuc 952f4a2713aSLionel Sambuc - ``-fsanitize=undefined-trap``: This includes all sanitizers 953f4a2713aSLionel Sambuc included by ``-fsanitize=undefined``, except those that require 954f4a2713aSLionel Sambuc runtime support. This group of sanitizers is intended to be 955f4a2713aSLionel Sambuc used in conjunction with the ``-fsanitize-undefined-trap-on-error`` 956f4a2713aSLionel Sambuc flag. This includes all of the checks listed below other than 957f4a2713aSLionel Sambuc ``unsigned-integer-overflow`` and ``vptr``. 958f4a2713aSLionel Sambuc - ``-fsanitize=dataflow``: :doc:`DataFlowSanitizer`, a general data 959f4a2713aSLionel Sambuc flow analysis. 960f4a2713aSLionel Sambuc 961f4a2713aSLionel Sambuc The following more fine-grained checks are also available: 962f4a2713aSLionel Sambuc 963f4a2713aSLionel Sambuc - ``-fsanitize=alignment``: Use of a misaligned pointer or creation 964f4a2713aSLionel Sambuc of a misaligned reference. 965f4a2713aSLionel Sambuc - ``-fsanitize=bool``: Load of a ``bool`` value which is neither 966f4a2713aSLionel Sambuc ``true`` nor ``false``. 967f4a2713aSLionel Sambuc - ``-fsanitize=bounds``: Out of bounds array indexing, in cases 968f4a2713aSLionel Sambuc where the array bound can be statically determined. 969f4a2713aSLionel Sambuc - ``-fsanitize=enum``: Load of a value of an enumerated type which 970f4a2713aSLionel Sambuc is not in the range of representable values for that enumerated 971f4a2713aSLionel Sambuc type. 972f4a2713aSLionel Sambuc - ``-fsanitize=float-cast-overflow``: Conversion to, from, or 973f4a2713aSLionel Sambuc between floating-point types which would overflow the 974f4a2713aSLionel Sambuc destination. 975f4a2713aSLionel Sambuc - ``-fsanitize=float-divide-by-zero``: Floating point division by 976f4a2713aSLionel Sambuc zero. 977f4a2713aSLionel Sambuc - ``-fsanitize=function``: Indirect call of a function through a 978f4a2713aSLionel Sambuc function pointer of the wrong type (Linux, C++ and x86/x86_64 only). 979f4a2713aSLionel Sambuc - ``-fsanitize=integer-divide-by-zero``: Integer division by zero. 980*0a6a1f1dSLionel Sambuc - ``-fsanitize=nonnull-attribute``: Passing null pointer as a function 981*0a6a1f1dSLionel Sambuc parameter which is declared to never be null. 982f4a2713aSLionel Sambuc - ``-fsanitize=null``: Use of a null pointer or creation of a null 983f4a2713aSLionel Sambuc reference. 984f4a2713aSLionel Sambuc - ``-fsanitize=object-size``: An attempt to use bytes which the 985f4a2713aSLionel Sambuc optimizer can determine are not part of the object being 986f4a2713aSLionel Sambuc accessed. The sizes of objects are determined using 987f4a2713aSLionel Sambuc ``__builtin_object_size``, and consequently may be able to detect 988f4a2713aSLionel Sambuc more problems at higher optimization levels. 989f4a2713aSLionel Sambuc - ``-fsanitize=return``: In C++, reaching the end of a 990f4a2713aSLionel Sambuc value-returning function without returning a value. 991*0a6a1f1dSLionel Sambuc - ``-fsanitize=returns-nonnull-attribute``: Returning null pointer 992*0a6a1f1dSLionel Sambuc from a function which is declared to never return null. 993f4a2713aSLionel Sambuc - ``-fsanitize=shift``: Shift operators where the amount shifted is 994f4a2713aSLionel Sambuc greater or equal to the promoted bit-width of the left hand side 995f4a2713aSLionel Sambuc or less than zero, or where the left hand side is negative. For a 996f4a2713aSLionel Sambuc signed left shift, also checks for signed overflow in C, and for 997f4a2713aSLionel Sambuc unsigned overflow in C++. 998f4a2713aSLionel Sambuc - ``-fsanitize=signed-integer-overflow``: Signed integer overflow, 999f4a2713aSLionel Sambuc including all the checks added by ``-ftrapv``, and checking for 1000f4a2713aSLionel Sambuc overflow in signed division (``INT_MIN / -1``). 1001f4a2713aSLionel Sambuc - ``-fsanitize=unreachable``: If control flow reaches 1002f4a2713aSLionel Sambuc ``__builtin_unreachable``. 1003f4a2713aSLionel Sambuc - ``-fsanitize=unsigned-integer-overflow``: Unsigned integer 1004f4a2713aSLionel Sambuc overflows. 1005f4a2713aSLionel Sambuc - ``-fsanitize=vla-bound``: A variable-length array whose bound 1006f4a2713aSLionel Sambuc does not evaluate to a positive value. 1007f4a2713aSLionel Sambuc - ``-fsanitize=vptr``: Use of an object whose vptr indicates that 1008f4a2713aSLionel Sambuc it is of the wrong dynamic type, or that its lifetime has not 1009f4a2713aSLionel Sambuc begun or has ended. Incompatible with ``-fno-rtti``. 1010f4a2713aSLionel Sambuc 1011f4a2713aSLionel Sambuc You can turn off or modify checks for certain source files, functions 1012f4a2713aSLionel Sambuc or even variables by providing a special file: 1013f4a2713aSLionel Sambuc 1014f4a2713aSLionel Sambuc - ``-fsanitize-blacklist=/path/to/blacklist/file``: disable or modify 1015f4a2713aSLionel Sambuc sanitizer checks for objects listed in the file. See 1016f4a2713aSLionel Sambuc :doc:`SanitizerSpecialCaseList` for file format description. 1017f4a2713aSLionel Sambuc - ``-fno-sanitize-blacklist``: don't use blacklist file, if it was 1018f4a2713aSLionel Sambuc specified earlier in the command line. 1019f4a2713aSLionel Sambuc 1020f4a2713aSLionel Sambuc Extra features of MemorySanitizer (require explicit 1021f4a2713aSLionel Sambuc ``-fsanitize=memory``): 1022f4a2713aSLionel Sambuc 1023*0a6a1f1dSLionel Sambuc - ``-fsanitize-memory-track-origins[=level]``: Enables origin tracking in 1024f4a2713aSLionel Sambuc MemorySanitizer. Adds a second section to MemorySanitizer 1025f4a2713aSLionel Sambuc reports pointing to the heap or stack allocation the 1026f4a2713aSLionel Sambuc uninitialized bits came from. Slows down execution by additional 1027f4a2713aSLionel Sambuc 1.5x-2x. 1028f4a2713aSLionel Sambuc 1029*0a6a1f1dSLionel Sambuc Possible values for level are 0 (off), 1 (default), 2. Level 2 adds more 1030*0a6a1f1dSLionel Sambuc sections to MemorySanitizer reports describing the order of memory stores 1031*0a6a1f1dSLionel Sambuc the uninitialized value went through. Beware, this mode may use a lot of 1032*0a6a1f1dSLionel Sambuc extra memory. 1033*0a6a1f1dSLionel Sambuc 1034f4a2713aSLionel Sambuc Extra features of UndefinedBehaviorSanitizer: 1035f4a2713aSLionel Sambuc 1036f4a2713aSLionel Sambuc - ``-fsanitize-undefined-trap-on-error``: Causes traps to be emitted 1037f4a2713aSLionel Sambuc rather than calls to runtime libraries when a problem is detected. 1038f4a2713aSLionel Sambuc This option is intended for use in cases where the sanitizer runtime 1039f4a2713aSLionel Sambuc cannot be used (for instance, when building libc or a kernel module). 1040f4a2713aSLionel Sambuc This is only compatible with the sanitizers in the ``undefined-trap`` 1041f4a2713aSLionel Sambuc group. 1042f4a2713aSLionel Sambuc 1043f4a2713aSLionel Sambuc The ``-fsanitize=`` argument must also be provided when linking, in 1044f4a2713aSLionel Sambuc order to link to the appropriate runtime library. When using 1045f4a2713aSLionel Sambuc ``-fsanitize=vptr`` (or a group that includes it, such as 1046f4a2713aSLionel Sambuc ``-fsanitize=undefined``) with a C++ program, the link must be 1047f4a2713aSLionel Sambuc performed by ``clang++``, not ``clang``, in order to link against the 1048f4a2713aSLionel Sambuc C++-specific parts of the runtime library. 1049f4a2713aSLionel Sambuc 1050f4a2713aSLionel Sambuc It is not possible to combine more than one of the ``-fsanitize=address``, 1051f4a2713aSLionel Sambuc ``-fsanitize=thread``, and ``-fsanitize=memory`` checkers in the same 1052f4a2713aSLionel Sambuc program. The ``-fsanitize=undefined`` checks can be combined with other 1053f4a2713aSLionel Sambuc sanitizers. 1054f4a2713aSLionel Sambuc 1055*0a6a1f1dSLionel Sambuc**-f[no-]sanitize-recover=check1,check2,...** 1056f4a2713aSLionel Sambuc 1057*0a6a1f1dSLionel Sambuc Controls which checks enabled by ``-fsanitize=`` flag are non-fatal. 1058*0a6a1f1dSLionel Sambuc If the check is fatal, program will halt after the first error 1059*0a6a1f1dSLionel Sambuc of this kind is detected and error report is printed. 1060f4a2713aSLionel Sambuc 1061*0a6a1f1dSLionel Sambuc By default, non-fatal checks are those enabled by UndefinedBehaviorSanitizer, 1062*0a6a1f1dSLionel Sambuc except for ``-fsanitize=return`` and ``-fsanitize=unreachable``. Some 1063*0a6a1f1dSLionel Sambuc sanitizers (e.g. :doc:`AddressSanitizer`) may not support recovery, 1064*0a6a1f1dSLionel Sambuc and always crash the program after the issue is detected. 1065f4a2713aSLionel Sambuc 1066f4a2713aSLionel Sambuc.. option:: -fno-assume-sane-operator-new 1067f4a2713aSLionel Sambuc 1068f4a2713aSLionel Sambuc Don't assume that the C++'s new operator is sane. 1069f4a2713aSLionel Sambuc 1070f4a2713aSLionel Sambuc This option tells the compiler to do not assume that C++'s global 1071f4a2713aSLionel Sambuc new operator will always return a pointer that does not alias any 1072f4a2713aSLionel Sambuc other pointer when the function returns. 1073f4a2713aSLionel Sambuc 1074f4a2713aSLionel Sambuc.. option:: -ftrap-function=[name] 1075f4a2713aSLionel Sambuc 1076f4a2713aSLionel Sambuc Instruct code generator to emit a function call to the specified 1077f4a2713aSLionel Sambuc function name for ``__builtin_trap()``. 1078f4a2713aSLionel Sambuc 1079f4a2713aSLionel Sambuc LLVM code generator translates ``__builtin_trap()`` to a trap 1080f4a2713aSLionel Sambuc instruction if it is supported by the target ISA. Otherwise, the 1081f4a2713aSLionel Sambuc builtin is translated into a call to ``abort``. If this option is 1082f4a2713aSLionel Sambuc set, then the code generator will always lower the builtin to a call 1083f4a2713aSLionel Sambuc to the specified function regardless of whether the target ISA has a 1084f4a2713aSLionel Sambuc trap instruction. This option is useful for environments (e.g. 1085f4a2713aSLionel Sambuc deeply embedded) where a trap cannot be properly handled, or when 1086f4a2713aSLionel Sambuc some custom behavior is desired. 1087f4a2713aSLionel Sambuc 1088f4a2713aSLionel Sambuc.. option:: -ftls-model=[model] 1089f4a2713aSLionel Sambuc 1090f4a2713aSLionel Sambuc Select which TLS model to use. 1091f4a2713aSLionel Sambuc 1092f4a2713aSLionel Sambuc Valid values are: ``global-dynamic``, ``local-dynamic``, 1093f4a2713aSLionel Sambuc ``initial-exec`` and ``local-exec``. The default value is 1094f4a2713aSLionel Sambuc ``global-dynamic``. The compiler may use a different model if the 1095f4a2713aSLionel Sambuc selected model is not supported by the target, or if a more 1096f4a2713aSLionel Sambuc efficient model can be used. The TLS model can be overridden per 1097f4a2713aSLionel Sambuc variable using the ``tls_model`` attribute. 1098f4a2713aSLionel Sambuc 1099f4a2713aSLionel Sambuc.. option:: -mhwdiv=[values] 1100f4a2713aSLionel Sambuc 1101f4a2713aSLionel Sambuc Select the ARM modes (arm or thumb) that support hardware division 1102f4a2713aSLionel Sambuc instructions. 1103f4a2713aSLionel Sambuc 1104f4a2713aSLionel Sambuc Valid values are: ``arm``, ``thumb`` and ``arm,thumb``. 1105f4a2713aSLionel Sambuc This option is used to indicate which mode (arm or thumb) supports 1106f4a2713aSLionel Sambuc hardware division instructions. This only applies to the ARM 1107f4a2713aSLionel Sambuc architecture. 1108f4a2713aSLionel Sambuc 1109f4a2713aSLionel Sambuc.. option:: -m[no-]crc 1110f4a2713aSLionel Sambuc 1111f4a2713aSLionel Sambuc Enable or disable CRC instructions. 1112f4a2713aSLionel Sambuc 1113f4a2713aSLionel Sambuc This option is used to indicate whether CRC instructions are to 1114f4a2713aSLionel Sambuc be generated. This only applies to the ARM architecture. 1115f4a2713aSLionel Sambuc 1116f4a2713aSLionel Sambuc CRC instructions are enabled by default on ARMv8. 1117f4a2713aSLionel Sambuc 1118*0a6a1f1dSLionel Sambuc.. option:: -mgeneral-regs-only 1119*0a6a1f1dSLionel Sambuc 1120*0a6a1f1dSLionel Sambuc Generate code which only uses the general purpose registers. 1121*0a6a1f1dSLionel Sambuc 1122*0a6a1f1dSLionel Sambuc This option restricts the generated code to use general registers 1123*0a6a1f1dSLionel Sambuc only. This only applies to the AArch64 architecture. 1124*0a6a1f1dSLionel Sambuc 1125*0a6a1f1dSLionel Sambuc**-f[no-]max-unknown-pointer-align=[number]** 1126*0a6a1f1dSLionel Sambuc Instruct the code generator to not enforce a higher alignment than the given 1127*0a6a1f1dSLionel Sambuc number (of bytes) when accessing memory via an opaque pointer or reference. 1128*0a6a1f1dSLionel Sambuc This cap is ignored when directly accessing a variable or when the pointee 1129*0a6a1f1dSLionel Sambuc type has an explicit “aligned” attribute. 1130*0a6a1f1dSLionel Sambuc 1131*0a6a1f1dSLionel Sambuc The value should usually be determined by the properties of the system allocator. 1132*0a6a1f1dSLionel Sambuc Some builtin types, especially vector types, have very high natural alignments; 1133*0a6a1f1dSLionel Sambuc when working with values of those types, Clang usually wants to use instructions 1134*0a6a1f1dSLionel Sambuc that take advantage of that alignment. However, many system allocators do 1135*0a6a1f1dSLionel Sambuc not promise to return memory that is more than 8-byte or 16-byte-aligned. Use 1136*0a6a1f1dSLionel Sambuc this option to limit the alignment that the compiler can assume for an arbitrary 1137*0a6a1f1dSLionel Sambuc pointer, which may point onto the heap. 1138*0a6a1f1dSLionel Sambuc 1139*0a6a1f1dSLionel Sambuc This option does not affect the ABI alignment of types; the layout of structs and 1140*0a6a1f1dSLionel Sambuc unions and the value returned by the alignof operator remain the same. 1141*0a6a1f1dSLionel Sambuc 1142*0a6a1f1dSLionel Sambuc This option can be overridden on a case-by-case basis by putting an explicit 1143*0a6a1f1dSLionel Sambuc “aligned” alignment on a struct, union, or typedef. For example: 1144*0a6a1f1dSLionel Sambuc 1145*0a6a1f1dSLionel Sambuc .. code-block:: console 1146*0a6a1f1dSLionel Sambuc 1147*0a6a1f1dSLionel Sambuc #include <immintrin.h> 1148*0a6a1f1dSLionel Sambuc // Make an aligned typedef of the AVX-512 16-int vector type. 1149*0a6a1f1dSLionel Sambuc typedef __v16si __aligned_v16si __attribute__((aligned(64))); 1150*0a6a1f1dSLionel Sambuc 1151*0a6a1f1dSLionel Sambuc void initialize_vector(__aligned_v16si *v) { 1152*0a6a1f1dSLionel Sambuc // The compiler may assume that ‘v’ is 64-byte aligned, regardless of the 1153*0a6a1f1dSLionel Sambuc // value of -fmax-unknown-pointer-align. 1154*0a6a1f1dSLionel Sambuc } 1155*0a6a1f1dSLionel Sambuc 1156*0a6a1f1dSLionel Sambuc 1157*0a6a1f1dSLionel SambucProfile Guided Optimization 1158*0a6a1f1dSLionel Sambuc--------------------------- 1159*0a6a1f1dSLionel Sambuc 1160*0a6a1f1dSLionel SambucProfile information enables better optimization. For example, knowing that a 1161*0a6a1f1dSLionel Sambucbranch is taken very frequently helps the compiler make better decisions when 1162*0a6a1f1dSLionel Sambucordering basic blocks. Knowing that a function ``foo`` is called more 1163*0a6a1f1dSLionel Sambucfrequently than another function ``bar`` helps the inliner. 1164*0a6a1f1dSLionel Sambuc 1165*0a6a1f1dSLionel SambucClang supports profile guided optimization with two different kinds of 1166*0a6a1f1dSLionel Sambucprofiling. A sampling profiler can generate a profile with very low runtime 1167*0a6a1f1dSLionel Sambucoverhead, or you can build an instrumented version of the code that collects 1168*0a6a1f1dSLionel Sambucmore detailed profile information. Both kinds of profiles can provide execution 1169*0a6a1f1dSLionel Sambuccounts for instructions in the code and information on branches taken and 1170*0a6a1f1dSLionel Sambucfunction invocation. 1171*0a6a1f1dSLionel Sambuc 1172*0a6a1f1dSLionel SambucRegardless of which kind of profiling you use, be careful to collect profiles 1173*0a6a1f1dSLionel Sambucby running your code with inputs that are representative of the typical 1174*0a6a1f1dSLionel Sambucbehavior. Code that is not exercised in the profile will be optimized as if it 1175*0a6a1f1dSLionel Sambucis unimportant, and the compiler may make poor optimization choices for code 1176*0a6a1f1dSLionel Sambucthat is disproportionately used while profiling. 1177*0a6a1f1dSLionel Sambuc 1178*0a6a1f1dSLionel SambucUsing Sampling Profilers 1179*0a6a1f1dSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^ 1180*0a6a1f1dSLionel Sambuc 1181*0a6a1f1dSLionel SambucSampling profilers are used to collect runtime information, such as 1182*0a6a1f1dSLionel Sambuchardware counters, while your application executes. They are typically 1183*0a6a1f1dSLionel Sambucvery efficient and do not incur a large runtime overhead. The 1184*0a6a1f1dSLionel Sambucsample data collected by the profiler can be used during compilation 1185*0a6a1f1dSLionel Sambucto determine what the most executed areas of the code are. 1186*0a6a1f1dSLionel Sambuc 1187*0a6a1f1dSLionel SambucUsing the data from a sample profiler requires some changes in the way 1188*0a6a1f1dSLionel Sambuca program is built. Before the compiler can use profiling information, 1189*0a6a1f1dSLionel Sambucthe code needs to execute under the profiler. The following is the 1190*0a6a1f1dSLionel Sambucusual build cycle when using sample profilers for optimization: 1191*0a6a1f1dSLionel Sambuc 1192*0a6a1f1dSLionel Sambuc1. Build the code with source line table information. You can use all the 1193*0a6a1f1dSLionel Sambuc usual build flags that you always build your application with. The only 1194*0a6a1f1dSLionel Sambuc requirement is that you add ``-gline-tables-only`` or ``-g`` to the 1195*0a6a1f1dSLionel Sambuc command line. This is important for the profiler to be able to map 1196*0a6a1f1dSLionel Sambuc instructions back to source line locations. 1197*0a6a1f1dSLionel Sambuc 1198*0a6a1f1dSLionel Sambuc .. code-block:: console 1199*0a6a1f1dSLionel Sambuc 1200*0a6a1f1dSLionel Sambuc $ clang++ -O2 -gline-tables-only code.cc -o code 1201*0a6a1f1dSLionel Sambuc 1202*0a6a1f1dSLionel Sambuc2. Run the executable under a sampling profiler. The specific profiler 1203*0a6a1f1dSLionel Sambuc you use does not really matter, as long as its output can be converted 1204*0a6a1f1dSLionel Sambuc into the format that the LLVM optimizer understands. Currently, there 1205*0a6a1f1dSLionel Sambuc exists a conversion tool for the Linux Perf profiler 1206*0a6a1f1dSLionel Sambuc (https://perf.wiki.kernel.org/), so these examples assume that you 1207*0a6a1f1dSLionel Sambuc are using Linux Perf to profile your code. 1208*0a6a1f1dSLionel Sambuc 1209*0a6a1f1dSLionel Sambuc .. code-block:: console 1210*0a6a1f1dSLionel Sambuc 1211*0a6a1f1dSLionel Sambuc $ perf record -b ./code 1212*0a6a1f1dSLionel Sambuc 1213*0a6a1f1dSLionel Sambuc Note the use of the ``-b`` flag. This tells Perf to use the Last Branch 1214*0a6a1f1dSLionel Sambuc Record (LBR) to record call chains. While this is not strictly required, 1215*0a6a1f1dSLionel Sambuc it provides better call information, which improves the accuracy of 1216*0a6a1f1dSLionel Sambuc the profile data. 1217*0a6a1f1dSLionel Sambuc 1218*0a6a1f1dSLionel Sambuc3. Convert the collected profile data to LLVM's sample profile format. 1219*0a6a1f1dSLionel Sambuc This is currently supported via the AutoFDO converter ``create_llvm_prof``. 1220*0a6a1f1dSLionel Sambuc It is available at http://github.com/google/autofdo. Once built and 1221*0a6a1f1dSLionel Sambuc installed, you can convert the ``perf.data`` file to LLVM using 1222*0a6a1f1dSLionel Sambuc the command: 1223*0a6a1f1dSLionel Sambuc 1224*0a6a1f1dSLionel Sambuc .. code-block:: console 1225*0a6a1f1dSLionel Sambuc 1226*0a6a1f1dSLionel Sambuc $ create_llvm_prof --binary=./code --out=code.prof 1227*0a6a1f1dSLionel Sambuc 1228*0a6a1f1dSLionel Sambuc This will read ``perf.data`` and the binary file ``./code`` and emit 1229*0a6a1f1dSLionel Sambuc the profile data in ``code.prof``. Note that if you ran ``perf`` 1230*0a6a1f1dSLionel Sambuc without the ``-b`` flag, you need to use ``--use_lbr=false`` when 1231*0a6a1f1dSLionel Sambuc calling ``create_llvm_prof``. 1232*0a6a1f1dSLionel Sambuc 1233*0a6a1f1dSLionel Sambuc4. Build the code again using the collected profile. This step feeds 1234*0a6a1f1dSLionel Sambuc the profile back to the optimizers. This should result in a binary 1235*0a6a1f1dSLionel Sambuc that executes faster than the original one. Note that you are not 1236*0a6a1f1dSLionel Sambuc required to build the code with the exact same arguments that you 1237*0a6a1f1dSLionel Sambuc used in the first step. The only requirement is that you build the code 1238*0a6a1f1dSLionel Sambuc with ``-gline-tables-only`` and ``-fprofile-sample-use``. 1239*0a6a1f1dSLionel Sambuc 1240*0a6a1f1dSLionel Sambuc .. code-block:: console 1241*0a6a1f1dSLionel Sambuc 1242*0a6a1f1dSLionel Sambuc $ clang++ -O2 -gline-tables-only -fprofile-sample-use=code.prof code.cc -o code 1243*0a6a1f1dSLionel Sambuc 1244*0a6a1f1dSLionel Sambuc 1245*0a6a1f1dSLionel SambucSample Profile Format 1246*0a6a1f1dSLionel Sambuc""""""""""""""""""""" 1247*0a6a1f1dSLionel Sambuc 1248*0a6a1f1dSLionel SambucIf you are not using Linux Perf to collect profiles, you will need to 1249*0a6a1f1dSLionel Sambucwrite a conversion tool from your profiler to LLVM's format. This section 1250*0a6a1f1dSLionel Sambucexplains the file format expected by the backend. 1251*0a6a1f1dSLionel Sambuc 1252*0a6a1f1dSLionel SambucSample profiles are written as ASCII text. The file is divided into sections, 1253*0a6a1f1dSLionel Sambucwhich correspond to each of the functions executed at runtime. Each 1254*0a6a1f1dSLionel Sambucsection has the following format (taken from 1255*0a6a1f1dSLionel Sambuchttps://github.com/google/autofdo/blob/master/profile_writer.h): 1256*0a6a1f1dSLionel Sambuc 1257*0a6a1f1dSLionel Sambuc.. code-block:: console 1258*0a6a1f1dSLionel Sambuc 1259*0a6a1f1dSLionel Sambuc function1:total_samples:total_head_samples 1260*0a6a1f1dSLionel Sambuc offset1[.discriminator]: number_of_samples [fn1:num fn2:num ... ] 1261*0a6a1f1dSLionel Sambuc offset2[.discriminator]: number_of_samples [fn3:num fn4:num ... ] 1262*0a6a1f1dSLionel Sambuc ... 1263*0a6a1f1dSLionel Sambuc offsetN[.discriminator]: number_of_samples [fn5:num fn6:num ... ] 1264*0a6a1f1dSLionel Sambuc 1265*0a6a1f1dSLionel SambucThe file may contain blank lines between sections and within a 1266*0a6a1f1dSLionel Sambucsection. However, the spacing within a single line is fixed. Additional 1267*0a6a1f1dSLionel Sambucspaces will result in an error while reading the file. 1268*0a6a1f1dSLionel Sambuc 1269*0a6a1f1dSLionel SambucFunction names must be mangled in order for the profile loader to 1270*0a6a1f1dSLionel Sambucmatch them in the current translation unit. The two numbers in the 1271*0a6a1f1dSLionel Sambucfunction header specify how many total samples were accumulated in the 1272*0a6a1f1dSLionel Sambucfunction (first number), and the total number of samples accumulated 1273*0a6a1f1dSLionel Sambucin the prologue of the function (second number). This head sample 1274*0a6a1f1dSLionel Sambuccount provides an indicator of how frequently the function is invoked. 1275*0a6a1f1dSLionel Sambuc 1276*0a6a1f1dSLionel SambucEach sampled line may contain several items. Some are optional (marked 1277*0a6a1f1dSLionel Sambucbelow): 1278*0a6a1f1dSLionel Sambuc 1279*0a6a1f1dSLionel Sambuca. Source line offset. This number represents the line number 1280*0a6a1f1dSLionel Sambuc in the function where the sample was collected. The line number is 1281*0a6a1f1dSLionel Sambuc always relative to the line where symbol of the function is 1282*0a6a1f1dSLionel Sambuc defined. So, if the function has its header at line 280, the offset 1283*0a6a1f1dSLionel Sambuc 13 is at line 293 in the file. 1284*0a6a1f1dSLionel Sambuc 1285*0a6a1f1dSLionel Sambuc Note that this offset should never be a negative number. This could 1286*0a6a1f1dSLionel Sambuc happen in cases like macros. The debug machinery will register the 1287*0a6a1f1dSLionel Sambuc line number at the point of macro expansion. So, if the macro was 1288*0a6a1f1dSLionel Sambuc expanded in a line before the start of the function, the profile 1289*0a6a1f1dSLionel Sambuc converter should emit a 0 as the offset (this means that the optimizers 1290*0a6a1f1dSLionel Sambuc will not be able to associate a meaningful weight to the instructions 1291*0a6a1f1dSLionel Sambuc in the macro). 1292*0a6a1f1dSLionel Sambuc 1293*0a6a1f1dSLionel Sambucb. [OPTIONAL] Discriminator. This is used if the sampled program 1294*0a6a1f1dSLionel Sambuc was compiled with DWARF discriminator support 1295*0a6a1f1dSLionel Sambuc (http://wiki.dwarfstd.org/index.php?title=Path_Discriminators). 1296*0a6a1f1dSLionel Sambuc DWARF discriminators are unsigned integer values that allow the 1297*0a6a1f1dSLionel Sambuc compiler to distinguish between multiple execution paths on the 1298*0a6a1f1dSLionel Sambuc same source line location. 1299*0a6a1f1dSLionel Sambuc 1300*0a6a1f1dSLionel Sambuc For example, consider the line of code ``if (cond) foo(); else bar();``. 1301*0a6a1f1dSLionel Sambuc If the predicate ``cond`` is true 80% of the time, then the edge 1302*0a6a1f1dSLionel Sambuc into function ``foo`` should be considered to be taken most of the 1303*0a6a1f1dSLionel Sambuc time. But both calls to ``foo`` and ``bar`` are at the same source 1304*0a6a1f1dSLionel Sambuc line, so a sample count at that line is not sufficient. The 1305*0a6a1f1dSLionel Sambuc compiler needs to know which part of that line is taken more 1306*0a6a1f1dSLionel Sambuc frequently. 1307*0a6a1f1dSLionel Sambuc 1308*0a6a1f1dSLionel Sambuc This is what discriminators provide. In this case, the calls to 1309*0a6a1f1dSLionel Sambuc ``foo`` and ``bar`` will be at the same line, but will have 1310*0a6a1f1dSLionel Sambuc different discriminator values. This allows the compiler to correctly 1311*0a6a1f1dSLionel Sambuc set edge weights into ``foo`` and ``bar``. 1312*0a6a1f1dSLionel Sambuc 1313*0a6a1f1dSLionel Sambucc. Number of samples. This is an integer quantity representing the 1314*0a6a1f1dSLionel Sambuc number of samples collected by the profiler at this source 1315*0a6a1f1dSLionel Sambuc location. 1316*0a6a1f1dSLionel Sambuc 1317*0a6a1f1dSLionel Sambucd. [OPTIONAL] Potential call targets and samples. If present, this 1318*0a6a1f1dSLionel Sambuc line contains a call instruction. This models both direct and 1319*0a6a1f1dSLionel Sambuc number of samples. For example, 1320*0a6a1f1dSLionel Sambuc 1321*0a6a1f1dSLionel Sambuc .. code-block:: console 1322*0a6a1f1dSLionel Sambuc 1323*0a6a1f1dSLionel Sambuc 130: 7 foo:3 bar:2 baz:7 1324*0a6a1f1dSLionel Sambuc 1325*0a6a1f1dSLionel Sambuc The above means that at relative line offset 130 there is a call 1326*0a6a1f1dSLionel Sambuc instruction that calls one of ``foo()``, ``bar()`` and ``baz()``, 1327*0a6a1f1dSLionel Sambuc with ``baz()`` being the relatively more frequently called target. 1328*0a6a1f1dSLionel Sambuc 1329*0a6a1f1dSLionel Sambuc 1330*0a6a1f1dSLionel SambucProfiling with Instrumentation 1331*0a6a1f1dSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 1332*0a6a1f1dSLionel Sambuc 1333*0a6a1f1dSLionel SambucClang also supports profiling via instrumentation. This requires building a 1334*0a6a1f1dSLionel Sambucspecial instrumented version of the code and has some runtime 1335*0a6a1f1dSLionel Sambucoverhead during the profiling, but it provides more detailed results than a 1336*0a6a1f1dSLionel Sambucsampling profiler. It also provides reproducible results, at least to the 1337*0a6a1f1dSLionel Sambucextent that the code behaves consistently across runs. 1338*0a6a1f1dSLionel Sambuc 1339*0a6a1f1dSLionel SambucHere are the steps for using profile guided optimization with 1340*0a6a1f1dSLionel Sambucinstrumentation: 1341*0a6a1f1dSLionel Sambuc 1342*0a6a1f1dSLionel Sambuc1. Build an instrumented version of the code by compiling and linking with the 1343*0a6a1f1dSLionel Sambuc ``-fprofile-instr-generate`` option. 1344*0a6a1f1dSLionel Sambuc 1345*0a6a1f1dSLionel Sambuc .. code-block:: console 1346*0a6a1f1dSLionel Sambuc 1347*0a6a1f1dSLionel Sambuc $ clang++ -O2 -fprofile-instr-generate code.cc -o code 1348*0a6a1f1dSLionel Sambuc 1349*0a6a1f1dSLionel Sambuc2. Run the instrumented executable with inputs that reflect the typical usage. 1350*0a6a1f1dSLionel Sambuc By default, the profile data will be written to a ``default.profraw`` file 1351*0a6a1f1dSLionel Sambuc in the current directory. You can override that default by setting the 1352*0a6a1f1dSLionel Sambuc ``LLVM_PROFILE_FILE`` environment variable to specify an alternate file. 1353*0a6a1f1dSLionel Sambuc Any instance of ``%p`` in that file name will be replaced by the process 1354*0a6a1f1dSLionel Sambuc ID, so that you can easily distinguish the profile output from multiple 1355*0a6a1f1dSLionel Sambuc runs. 1356*0a6a1f1dSLionel Sambuc 1357*0a6a1f1dSLionel Sambuc .. code-block:: console 1358*0a6a1f1dSLionel Sambuc 1359*0a6a1f1dSLionel Sambuc $ LLVM_PROFILE_FILE="code-%p.profraw" ./code 1360*0a6a1f1dSLionel Sambuc 1361*0a6a1f1dSLionel Sambuc3. Combine profiles from multiple runs and convert the "raw" profile format to 1362*0a6a1f1dSLionel Sambuc the input expected by clang. Use the ``merge`` command of the llvm-profdata 1363*0a6a1f1dSLionel Sambuc tool to do this. 1364*0a6a1f1dSLionel Sambuc 1365*0a6a1f1dSLionel Sambuc .. code-block:: console 1366*0a6a1f1dSLionel Sambuc 1367*0a6a1f1dSLionel Sambuc $ llvm-profdata merge -output=code.profdata code-*.profraw 1368*0a6a1f1dSLionel Sambuc 1369*0a6a1f1dSLionel Sambuc Note that this step is necessary even when there is only one "raw" profile, 1370*0a6a1f1dSLionel Sambuc since the merge operation also changes the file format. 1371*0a6a1f1dSLionel Sambuc 1372*0a6a1f1dSLionel Sambuc4. Build the code again using the ``-fprofile-instr-use`` option to specify the 1373*0a6a1f1dSLionel Sambuc collected profile data. 1374*0a6a1f1dSLionel Sambuc 1375*0a6a1f1dSLionel Sambuc .. code-block:: console 1376*0a6a1f1dSLionel Sambuc 1377*0a6a1f1dSLionel Sambuc $ clang++ -O2 -fprofile-instr-use=code.profdata code.cc -o code 1378*0a6a1f1dSLionel Sambuc 1379*0a6a1f1dSLionel Sambuc You can repeat step 4 as often as you like without regenerating the 1380*0a6a1f1dSLionel Sambuc profile. As you make changes to your code, clang may no longer be able to 1381*0a6a1f1dSLionel Sambuc use the profile data. It will warn you when this happens. 1382*0a6a1f1dSLionel Sambuc 1383f4a2713aSLionel Sambuc 1384f4a2713aSLionel SambucControlling Size of Debug Information 1385f4a2713aSLionel Sambuc------------------------------------- 1386f4a2713aSLionel Sambuc 1387f4a2713aSLionel SambucDebug info kind generated by Clang can be set by one of the flags listed 1388f4a2713aSLionel Sambucbelow. If multiple flags are present, the last one is used. 1389f4a2713aSLionel Sambuc 1390f4a2713aSLionel Sambuc.. option:: -g0 1391f4a2713aSLionel Sambuc 1392f4a2713aSLionel Sambuc Don't generate any debug info (default). 1393f4a2713aSLionel Sambuc 1394f4a2713aSLionel Sambuc.. option:: -gline-tables-only 1395f4a2713aSLionel Sambuc 1396f4a2713aSLionel Sambuc Generate line number tables only. 1397f4a2713aSLionel Sambuc 1398f4a2713aSLionel Sambuc This kind of debug info allows to obtain stack traces with function names, 1399f4a2713aSLionel Sambuc file names and line numbers (by such tools as ``gdb`` or ``addr2line``). It 1400f4a2713aSLionel Sambuc doesn't contain any other data (e.g. description of local variables or 1401f4a2713aSLionel Sambuc function parameters). 1402f4a2713aSLionel Sambuc 1403*0a6a1f1dSLionel Sambuc.. option:: -fstandalone-debug 1404*0a6a1f1dSLionel Sambuc 1405*0a6a1f1dSLionel Sambuc Clang supports a number of optimizations to reduce the size of debug 1406*0a6a1f1dSLionel Sambuc information in the binary. They work based on the assumption that 1407*0a6a1f1dSLionel Sambuc the debug type information can be spread out over multiple 1408*0a6a1f1dSLionel Sambuc compilation units. For instance, Clang will not emit type 1409*0a6a1f1dSLionel Sambuc definitions for types that are not needed by a module and could be 1410*0a6a1f1dSLionel Sambuc replaced with a forward declaration. Further, Clang will only emit 1411*0a6a1f1dSLionel Sambuc type info for a dynamic C++ class in the module that contains the 1412*0a6a1f1dSLionel Sambuc vtable for the class. 1413*0a6a1f1dSLionel Sambuc 1414*0a6a1f1dSLionel Sambuc The **-fstandalone-debug** option turns off these optimizations. 1415*0a6a1f1dSLionel Sambuc This is useful when working with 3rd-party libraries that don't come 1416*0a6a1f1dSLionel Sambuc with debug information. Note that Clang will never emit type 1417*0a6a1f1dSLionel Sambuc information for types that are not referenced at all by the program. 1418*0a6a1f1dSLionel Sambuc 1419*0a6a1f1dSLionel Sambuc.. option:: -fno-standalone-debug 1420*0a6a1f1dSLionel Sambuc 1421*0a6a1f1dSLionel Sambuc On Darwin **-fstandalone-debug** is enabled by default. The 1422*0a6a1f1dSLionel Sambuc **-fno-standalone-debug** option can be used to get to turn on the 1423*0a6a1f1dSLionel Sambuc vtable-based optimization described above. 1424*0a6a1f1dSLionel Sambuc 1425f4a2713aSLionel Sambuc.. option:: -g 1426f4a2713aSLionel Sambuc 1427f4a2713aSLionel Sambuc Generate complete debug info. 1428f4a2713aSLionel Sambuc 1429f4a2713aSLionel SambucComment Parsing Options 1430*0a6a1f1dSLionel Sambuc----------------------- 1431f4a2713aSLionel Sambuc 1432f4a2713aSLionel SambucClang parses Doxygen and non-Doxygen style documentation comments and attaches 1433f4a2713aSLionel Sambucthem to the appropriate declaration nodes. By default, it only parses 1434f4a2713aSLionel SambucDoxygen-style comments and ignores ordinary comments starting with ``//`` and 1435f4a2713aSLionel Sambuc``/*``. 1436f4a2713aSLionel Sambuc 1437*0a6a1f1dSLionel Sambuc.. option:: -Wdocumentation 1438*0a6a1f1dSLionel Sambuc 1439*0a6a1f1dSLionel Sambuc Emit warnings about use of documentation comments. This warning group is off 1440*0a6a1f1dSLionel Sambuc by default. 1441*0a6a1f1dSLionel Sambuc 1442*0a6a1f1dSLionel Sambuc This includes checking that ``\param`` commands name parameters that actually 1443*0a6a1f1dSLionel Sambuc present in the function signature, checking that ``\returns`` is used only on 1444*0a6a1f1dSLionel Sambuc functions that actually return a value etc. 1445*0a6a1f1dSLionel Sambuc 1446*0a6a1f1dSLionel Sambuc.. option:: -Wno-documentation-unknown-command 1447*0a6a1f1dSLionel Sambuc 1448*0a6a1f1dSLionel Sambuc Don't warn when encountering an unknown Doxygen command. 1449*0a6a1f1dSLionel Sambuc 1450f4a2713aSLionel Sambuc.. option:: -fparse-all-comments 1451f4a2713aSLionel Sambuc 1452f4a2713aSLionel Sambuc Parse all comments as documentation comments (including ordinary comments 1453f4a2713aSLionel Sambuc starting with ``//`` and ``/*``). 1454f4a2713aSLionel Sambuc 1455*0a6a1f1dSLionel Sambuc.. option:: -fcomment-block-commands=[commands] 1456*0a6a1f1dSLionel Sambuc 1457*0a6a1f1dSLionel Sambuc Define custom documentation commands as block commands. This allows Clang to 1458*0a6a1f1dSLionel Sambuc construct the correct AST for these custom commands, and silences warnings 1459*0a6a1f1dSLionel Sambuc about unknown commands. Several commands must be separated by a comma 1460*0a6a1f1dSLionel Sambuc *without trailing space*; e.g. ``-fcomment-block-commands=foo,bar`` defines 1461*0a6a1f1dSLionel Sambuc custom commands ``\foo`` and ``\bar``. 1462*0a6a1f1dSLionel Sambuc 1463*0a6a1f1dSLionel Sambuc It is also possible to use ``-fcomment-block-commands`` several times; e.g. 1464*0a6a1f1dSLionel Sambuc ``-fcomment-block-commands=foo -fcomment-block-commands=bar`` does the same 1465*0a6a1f1dSLionel Sambuc as above. 1466*0a6a1f1dSLionel Sambuc 1467f4a2713aSLionel Sambuc.. _c: 1468f4a2713aSLionel Sambuc 1469f4a2713aSLionel SambucC Language Features 1470f4a2713aSLionel Sambuc=================== 1471f4a2713aSLionel Sambuc 1472f4a2713aSLionel SambucThe support for standard C in clang is feature-complete except for the 1473f4a2713aSLionel SambucC99 floating-point pragmas. 1474f4a2713aSLionel Sambuc 1475f4a2713aSLionel SambucExtensions supported by clang 1476f4a2713aSLionel Sambuc----------------------------- 1477f4a2713aSLionel Sambuc 1478f4a2713aSLionel SambucSee :doc:`LanguageExtensions`. 1479f4a2713aSLionel Sambuc 1480f4a2713aSLionel SambucDifferences between various standard modes 1481f4a2713aSLionel Sambuc------------------------------------------ 1482f4a2713aSLionel Sambuc 1483f4a2713aSLionel Sambucclang supports the -std option, which changes what language mode clang 1484*0a6a1f1dSLionel Sambucuses. The supported modes for C are c89, gnu89, c94, c99, gnu99, c11, 1485*0a6a1f1dSLionel Sambucgnu11, and various aliases for those modes. If no -std option is 1486*0a6a1f1dSLionel Sambucspecified, clang defaults to gnu11 mode. Many C99 and C11 features are 1487*0a6a1f1dSLionel Sambucsupported in earlier modes as a conforming extension, with a warning. Use 1488*0a6a1f1dSLionel Sambuc``-pedantic-errors`` to request an error if a feature from a later standard 1489*0a6a1f1dSLionel Sambucrevision is used in an earlier mode. 1490f4a2713aSLionel Sambuc 1491f4a2713aSLionel SambucDifferences between all ``c*`` and ``gnu*`` modes: 1492f4a2713aSLionel Sambuc 1493f4a2713aSLionel Sambuc- ``c*`` modes define "``__STRICT_ANSI__``". 1494f4a2713aSLionel Sambuc- Target-specific defines not prefixed by underscores, like "linux", 1495f4a2713aSLionel Sambuc are defined in ``gnu*`` modes. 1496f4a2713aSLionel Sambuc- Trigraphs default to being off in ``gnu*`` modes; they can be enabled by 1497f4a2713aSLionel Sambuc the -trigraphs option. 1498f4a2713aSLionel Sambuc- The parser recognizes "asm" and "typeof" as keywords in ``gnu*`` modes; 1499f4a2713aSLionel Sambuc the variants "``__asm__``" and "``__typeof__``" are recognized in all 1500f4a2713aSLionel Sambuc modes. 1501f4a2713aSLionel Sambuc- The Apple "blocks" extension is recognized by default in ``gnu*`` modes 1502f4a2713aSLionel Sambuc on some platforms; it can be enabled in any mode with the "-fblocks" 1503f4a2713aSLionel Sambuc option. 1504f4a2713aSLionel Sambuc- Arrays that are VLA's according to the standard, but which can be 1505f4a2713aSLionel Sambuc constant folded by the frontend are treated as fixed size arrays. 1506f4a2713aSLionel Sambuc This occurs for things like "int X[(1, 2)];", which is technically a 1507f4a2713aSLionel Sambuc VLA. ``c*`` modes are strictly compliant and treat these as VLAs. 1508f4a2713aSLionel Sambuc 1509f4a2713aSLionel SambucDifferences between ``*89`` and ``*99`` modes: 1510f4a2713aSLionel Sambuc 1511f4a2713aSLionel Sambuc- The ``*99`` modes default to implementing "inline" as specified in C99, 1512f4a2713aSLionel Sambuc while the ``*89`` modes implement the GNU version. This can be 1513f4a2713aSLionel Sambuc overridden for individual functions with the ``__gnu_inline__`` 1514f4a2713aSLionel Sambuc attribute. 1515f4a2713aSLionel Sambuc- Digraphs are not recognized in c89 mode. 1516f4a2713aSLionel Sambuc- The scope of names defined inside a "for", "if", "switch", "while", 1517f4a2713aSLionel Sambuc or "do" statement is different. (example: "``if ((struct x {int 1518f4a2713aSLionel Sambuc x;}*)0) {}``".) 1519f4a2713aSLionel Sambuc- ``__STDC_VERSION__`` is not defined in ``*89`` modes. 1520f4a2713aSLionel Sambuc- "inline" is not recognized as a keyword in c89 mode. 1521f4a2713aSLionel Sambuc- "restrict" is not recognized as a keyword in ``*89`` modes. 1522f4a2713aSLionel Sambuc- Commas are allowed in integer constant expressions in ``*99`` modes. 1523f4a2713aSLionel Sambuc- Arrays which are not lvalues are not implicitly promoted to pointers 1524f4a2713aSLionel Sambuc in ``*89`` modes. 1525f4a2713aSLionel Sambuc- Some warnings are different. 1526f4a2713aSLionel Sambuc 1527*0a6a1f1dSLionel SambucDifferences between ``*99`` and ``*11`` modes: 1528*0a6a1f1dSLionel Sambuc 1529*0a6a1f1dSLionel Sambuc- Warnings for use of C11 features are disabled. 1530*0a6a1f1dSLionel Sambuc- ``__STDC_VERSION__`` is defined to ``201112L`` rather than ``199901L``. 1531*0a6a1f1dSLionel Sambuc 1532f4a2713aSLionel Sambucc94 mode is identical to c89 mode except that digraphs are enabled in 1533f4a2713aSLionel Sambucc94 mode (FIXME: And ``__STDC_VERSION__`` should be defined!). 1534f4a2713aSLionel Sambuc 1535f4a2713aSLionel SambucGCC extensions not implemented yet 1536f4a2713aSLionel Sambuc---------------------------------- 1537f4a2713aSLionel Sambuc 1538f4a2713aSLionel Sambucclang tries to be compatible with gcc as much as possible, but some gcc 1539f4a2713aSLionel Sambucextensions are not implemented yet: 1540f4a2713aSLionel Sambuc 1541f4a2713aSLionel Sambuc- clang does not support #pragma weak (`bug 1542f4a2713aSLionel Sambuc 3679 <http://llvm.org/bugs/show_bug.cgi?id=3679>`_). Due to the uses 1543f4a2713aSLionel Sambuc described in the bug, this is likely to be implemented at some point, 1544f4a2713aSLionel Sambuc at least partially. 1545f4a2713aSLionel Sambuc- clang does not support decimal floating point types (``_Decimal32`` and 1546f4a2713aSLionel Sambuc friends) or fixed-point types (``_Fract`` and friends); nobody has 1547f4a2713aSLionel Sambuc expressed interest in these features yet, so it's hard to say when 1548f4a2713aSLionel Sambuc they will be implemented. 1549f4a2713aSLionel Sambuc- clang does not support nested functions; this is a complex feature 1550f4a2713aSLionel Sambuc which is infrequently used, so it is unlikely to be implemented 1551f4a2713aSLionel Sambuc anytime soon. In C++11 it can be emulated by assigning lambda 1552f4a2713aSLionel Sambuc functions to local variables, e.g: 1553f4a2713aSLionel Sambuc 1554f4a2713aSLionel Sambuc .. code-block:: cpp 1555f4a2713aSLionel Sambuc 1556f4a2713aSLionel Sambuc auto const local_function = [&](int parameter) { 1557f4a2713aSLionel Sambuc // Do something 1558f4a2713aSLionel Sambuc }; 1559f4a2713aSLionel Sambuc ... 1560f4a2713aSLionel Sambuc local_function(1); 1561f4a2713aSLionel Sambuc 1562f4a2713aSLionel Sambuc- clang does not support global register variables; this is unlikely to 1563f4a2713aSLionel Sambuc be implemented soon because it requires additional LLVM backend 1564f4a2713aSLionel Sambuc support. 1565f4a2713aSLionel Sambuc- clang does not support static initialization of flexible array 1566f4a2713aSLionel Sambuc members. This appears to be a rarely used extension, but could be 1567f4a2713aSLionel Sambuc implemented pending user demand. 1568f4a2713aSLionel Sambuc- clang does not support 1569f4a2713aSLionel Sambuc ``__builtin_va_arg_pack``/``__builtin_va_arg_pack_len``. This is 1570f4a2713aSLionel Sambuc used rarely, but in some potentially interesting places, like the 1571f4a2713aSLionel Sambuc glibc headers, so it may be implemented pending user demand. Note 1572f4a2713aSLionel Sambuc that because clang pretends to be like GCC 4.2, and this extension 1573f4a2713aSLionel Sambuc was introduced in 4.3, the glibc headers will not try to use this 1574f4a2713aSLionel Sambuc extension with clang at the moment. 1575f4a2713aSLionel Sambuc- clang does not support the gcc extension for forward-declaring 1576f4a2713aSLionel Sambuc function parameters; this has not shown up in any real-world code 1577f4a2713aSLionel Sambuc yet, though, so it might never be implemented. 1578f4a2713aSLionel Sambuc 1579f4a2713aSLionel SambucThis is not a complete list; if you find an unsupported extension 1580f4a2713aSLionel Sambucmissing from this list, please send an e-mail to cfe-dev. This list 1581f4a2713aSLionel Sambuccurrently excludes C++; see :ref:`C++ Language Features <cxx>`. Also, this 1582f4a2713aSLionel Sambuclist does not include bugs in mostly-implemented features; please see 1583f4a2713aSLionel Sambucthe `bug 1584f4a2713aSLionel Sambuctracker <http://llvm.org/bugs/buglist.cgi?quicksearch=product%3Aclang+component%3A-New%2BBugs%2CAST%2CBasic%2CDriver%2CHeaders%2CLLVM%2BCodeGen%2Cparser%2Cpreprocessor%2CSemantic%2BAnalyzer>`_ 1585f4a2713aSLionel Sambucfor known existing bugs (FIXME: Is there a section for bug-reporting 1586f4a2713aSLionel Sambucguidelines somewhere?). 1587f4a2713aSLionel Sambuc 1588f4a2713aSLionel SambucIntentionally unsupported GCC extensions 1589f4a2713aSLionel Sambuc---------------------------------------- 1590f4a2713aSLionel Sambuc 1591f4a2713aSLionel Sambuc- clang does not support the gcc extension that allows variable-length 1592f4a2713aSLionel Sambuc arrays in structures. This is for a few reasons: one, it is tricky to 1593f4a2713aSLionel Sambuc implement, two, the extension is completely undocumented, and three, 1594f4a2713aSLionel Sambuc the extension appears to be rarely used. Note that clang *does* 1595f4a2713aSLionel Sambuc support flexible array members (arrays with a zero or unspecified 1596f4a2713aSLionel Sambuc size at the end of a structure). 1597f4a2713aSLionel Sambuc- clang does not have an equivalent to gcc's "fold"; this means that 1598f4a2713aSLionel Sambuc clang doesn't accept some constructs gcc might accept in contexts 1599f4a2713aSLionel Sambuc where a constant expression is required, like "x-x" where x is a 1600f4a2713aSLionel Sambuc variable. 1601f4a2713aSLionel Sambuc- clang does not support ``__builtin_apply`` and friends; this extension 1602f4a2713aSLionel Sambuc is extremely obscure and difficult to implement reliably. 1603f4a2713aSLionel Sambuc 1604f4a2713aSLionel Sambuc.. _c_ms: 1605f4a2713aSLionel Sambuc 1606f4a2713aSLionel SambucMicrosoft extensions 1607f4a2713aSLionel Sambuc-------------------- 1608f4a2713aSLionel Sambuc 1609f4a2713aSLionel Sambucclang has some experimental support for extensions from Microsoft Visual 1610*0a6a1f1dSLionel SambucC++; to enable it, use the ``-fms-extensions`` command-line option. This is 1611f4a2713aSLionel Sambucthe default for Windows targets. Note that the support is incomplete. 1612*0a6a1f1dSLionel SambucSome constructs such as ``dllexport`` on classes are ignored with a warning, 1613f4a2713aSLionel Sambucand others such as `Microsoft IDL annotations 1614f4a2713aSLionel Sambuc<http://msdn.microsoft.com/en-us/library/8tesw2eh.aspx>`_ are silently 1615f4a2713aSLionel Sambucignored. 1616f4a2713aSLionel Sambuc 1617*0a6a1f1dSLionel Sambucclang has a ``-fms-compatibility`` flag that makes clang accept enough 1618f4a2713aSLionel Sambucinvalid C++ to be able to parse most Microsoft headers. For example, it 1619f4a2713aSLionel Sambucallows `unqualified lookup of dependent base class members 1620f4a2713aSLionel Sambuc<http://clang.llvm.org/compatibility.html#dep_lookup_bases>`_, which is 1621f4a2713aSLionel Sambuca common compatibility issue with clang. This flag is enabled by default 1622f4a2713aSLionel Sambucfor Windows targets. 1623f4a2713aSLionel Sambuc 1624*0a6a1f1dSLionel Sambuc``-fdelayed-template-parsing`` lets clang delay parsing of function template 1625*0a6a1f1dSLionel Sambucdefinitions until the end of a translation unit. This flag is enabled by 1626*0a6a1f1dSLionel Sambucdefault for Windows targets. 1627f4a2713aSLionel Sambuc 1628f4a2713aSLionel Sambuc- clang allows setting ``_MSC_VER`` with ``-fmsc-version=``. It defaults to 1629f4a2713aSLionel Sambuc 1700 which is the same as Visual C/C++ 2012. Any number is supported 1630f4a2713aSLionel Sambuc and can greatly affect what Windows SDK and c++stdlib headers clang 1631f4a2713aSLionel Sambuc can compile. 1632f4a2713aSLionel Sambuc- clang does not support the Microsoft extension where anonymous record 1633f4a2713aSLionel Sambuc members can be declared using user defined typedefs. 1634f4a2713aSLionel Sambuc- clang supports the Microsoft ``#pragma pack`` feature for controlling 1635f4a2713aSLionel Sambuc record layout. GCC also contains support for this feature, however 1636f4a2713aSLionel Sambuc where MSVC and GCC are incompatible clang follows the MSVC 1637f4a2713aSLionel Sambuc definition. 1638f4a2713aSLionel Sambuc- clang supports the Microsoft ``#pragma comment(lib, "foo.lib")`` feature for 1639f4a2713aSLionel Sambuc automatically linking against the specified library. Currently this feature 1640f4a2713aSLionel Sambuc only works with the Visual C++ linker. 1641f4a2713aSLionel Sambuc- clang supports the Microsoft ``#pragma comment(linker, "/flag:foo")`` feature 1642f4a2713aSLionel Sambuc for adding linker flags to COFF object files. The user is responsible for 1643f4a2713aSLionel Sambuc ensuring that the linker understands the flags. 1644f4a2713aSLionel Sambuc- clang defaults to C++11 for Windows targets. 1645f4a2713aSLionel Sambuc 1646f4a2713aSLionel Sambuc.. _cxx: 1647f4a2713aSLionel Sambuc 1648f4a2713aSLionel SambucC++ Language Features 1649f4a2713aSLionel Sambuc===================== 1650f4a2713aSLionel Sambuc 1651f4a2713aSLionel Sambucclang fully implements all of standard C++98 except for exported 1652*0a6a1f1dSLionel Sambuctemplates (which were removed in C++11), and all of standard C++11 1653*0a6a1f1dSLionel Sambucand the current draft standard for C++1y. 1654f4a2713aSLionel Sambuc 1655f4a2713aSLionel SambucControlling implementation limits 1656f4a2713aSLionel Sambuc--------------------------------- 1657f4a2713aSLionel Sambuc 1658f4a2713aSLionel Sambuc.. option:: -fbracket-depth=N 1659f4a2713aSLionel Sambuc 1660f4a2713aSLionel Sambuc Sets the limit for nested parentheses, brackets, and braces to N. The 1661f4a2713aSLionel Sambuc default is 256. 1662f4a2713aSLionel Sambuc 1663f4a2713aSLionel Sambuc.. option:: -fconstexpr-depth=N 1664f4a2713aSLionel Sambuc 1665f4a2713aSLionel Sambuc Sets the limit for recursive constexpr function invocations to N. The 1666f4a2713aSLionel Sambuc default is 512. 1667f4a2713aSLionel Sambuc 1668f4a2713aSLionel Sambuc.. option:: -ftemplate-depth=N 1669f4a2713aSLionel Sambuc 1670f4a2713aSLionel Sambuc Sets the limit for recursively nested template instantiations to N. The 1671f4a2713aSLionel Sambuc default is 256. 1672f4a2713aSLionel Sambuc 1673f4a2713aSLionel Sambuc.. option:: -foperator-arrow-depth=N 1674f4a2713aSLionel Sambuc 1675f4a2713aSLionel Sambuc Sets the limit for iterative calls to 'operator->' functions to N. The 1676f4a2713aSLionel Sambuc default is 256. 1677f4a2713aSLionel Sambuc 1678f4a2713aSLionel Sambuc.. _objc: 1679f4a2713aSLionel Sambuc 1680f4a2713aSLionel SambucObjective-C Language Features 1681f4a2713aSLionel Sambuc============================= 1682f4a2713aSLionel Sambuc 1683f4a2713aSLionel Sambuc.. _objcxx: 1684f4a2713aSLionel Sambuc 1685f4a2713aSLionel SambucObjective-C++ Language Features 1686f4a2713aSLionel Sambuc=============================== 1687f4a2713aSLionel Sambuc 1688f4a2713aSLionel Sambuc 1689f4a2713aSLionel Sambuc.. _target_features: 1690f4a2713aSLionel Sambuc 1691f4a2713aSLionel SambucTarget-Specific Features and Limitations 1692f4a2713aSLionel Sambuc======================================== 1693f4a2713aSLionel Sambuc 1694f4a2713aSLionel SambucCPU Architectures Features and Limitations 1695f4a2713aSLionel Sambuc------------------------------------------ 1696f4a2713aSLionel Sambuc 1697f4a2713aSLionel SambucX86 1698f4a2713aSLionel Sambuc^^^ 1699f4a2713aSLionel Sambuc 1700f4a2713aSLionel SambucThe support for X86 (both 32-bit and 64-bit) is considered stable on 1701*0a6a1f1dSLionel SambucDarwin (Mac OS X), Linux, FreeBSD, and Dragonfly BSD: it has been tested 1702f4a2713aSLionel Sambucto correctly compile many large C, C++, Objective-C, and Objective-C++ 1703f4a2713aSLionel Sambuccodebases. 1704f4a2713aSLionel Sambuc 1705*0a6a1f1dSLionel SambucOn ``x86_64-mingw32``, passing i128(by value) is incompatible with the 1706*0a6a1f1dSLionel SambucMicrosoft x64 calling convention. You might need to tweak 1707f4a2713aSLionel Sambuc``WinX86_64ABIInfo::classify()`` in lib/CodeGen/TargetInfo.cpp. 1708f4a2713aSLionel Sambuc 1709*0a6a1f1dSLionel SambucFor the X86 target, clang supports the :option:`-m16` command line 1710*0a6a1f1dSLionel Sambucargument which enables 16-bit code output. This is broadly similar to 1711*0a6a1f1dSLionel Sambucusing ``asm(".code16gcc")`` with the GNU toolchain. The generated code 1712*0a6a1f1dSLionel Sambucand the ABI remains 32-bit but the assembler emits instructions 1713*0a6a1f1dSLionel Sambucappropriate for a CPU running in 16-bit mode, with address-size and 1714*0a6a1f1dSLionel Sambucoperand-size prefixes to enable 32-bit addressing and operations. 1715*0a6a1f1dSLionel Sambuc 1716f4a2713aSLionel SambucARM 1717f4a2713aSLionel Sambuc^^^ 1718f4a2713aSLionel Sambuc 1719f4a2713aSLionel SambucThe support for ARM (specifically ARMv6 and ARMv7) is considered stable 1720f4a2713aSLionel Sambucon Darwin (iOS): it has been tested to correctly compile many large C, 1721f4a2713aSLionel SambucC++, Objective-C, and Objective-C++ codebases. Clang only supports a 1722f4a2713aSLionel Sambuclimited number of ARM architectures. It does not yet fully support 1723f4a2713aSLionel SambucARMv5, for example. 1724f4a2713aSLionel Sambuc 1725f4a2713aSLionel SambucPowerPC 1726f4a2713aSLionel Sambuc^^^^^^^ 1727f4a2713aSLionel Sambuc 1728f4a2713aSLionel SambucThe support for PowerPC (especially PowerPC64) is considered stable 1729f4a2713aSLionel Sambucon Linux and FreeBSD: it has been tested to correctly compile many 1730f4a2713aSLionel Sambuclarge C and C++ codebases. PowerPC (32bit) is still missing certain 1731f4a2713aSLionel Sambucfeatures (e.g. PIC code on ELF platforms). 1732f4a2713aSLionel Sambuc 1733f4a2713aSLionel SambucOther platforms 1734f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^ 1735f4a2713aSLionel Sambuc 1736f4a2713aSLionel Sambucclang currently contains some support for other architectures (e.g. Sparc); 1737f4a2713aSLionel Sambuchowever, significant pieces of code generation are still missing, and they 1738f4a2713aSLionel Sambuchaven't undergone significant testing. 1739f4a2713aSLionel Sambuc 1740f4a2713aSLionel Sambucclang contains limited support for the MSP430 embedded processor, but 1741f4a2713aSLionel Sambucboth the clang support and the LLVM backend support are highly 1742f4a2713aSLionel Sambucexperimental. 1743f4a2713aSLionel Sambuc 1744f4a2713aSLionel SambucOther platforms are completely unsupported at the moment. Adding the 1745f4a2713aSLionel Sambucminimal support needed for parsing and semantic analysis on a new 1746f4a2713aSLionel Sambucplatform is quite easy; see ``lib/Basic/Targets.cpp`` in the clang source 1747f4a2713aSLionel Sambuctree. This level of support is also sufficient for conversion to LLVM IR 1748f4a2713aSLionel Sambucfor simple programs. Proper support for conversion to LLVM IR requires 1749f4a2713aSLionel Sambucadding code to ``lib/CodeGen/CGCall.cpp`` at the moment; this is likely to 1750f4a2713aSLionel Sambucchange soon, though. Generating assembly requires a suitable LLVM 1751f4a2713aSLionel Sambucbackend. 1752f4a2713aSLionel Sambuc 1753f4a2713aSLionel SambucOperating System Features and Limitations 1754f4a2713aSLionel Sambuc----------------------------------------- 1755f4a2713aSLionel Sambuc 1756*0a6a1f1dSLionel SambucDarwin (Mac OS X) 1757f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^ 1758f4a2713aSLionel Sambuc 1759*0a6a1f1dSLionel SambucThread Sanitizer is not supported. 1760f4a2713aSLionel Sambuc 1761f4a2713aSLionel SambucWindows 1762f4a2713aSLionel Sambuc^^^^^^^ 1763f4a2713aSLionel Sambuc 1764*0a6a1f1dSLionel SambucClang has experimental support for targeting "Cygming" (Cygwin / MinGW) 1765*0a6a1f1dSLionel Sambucplatforms. 1766f4a2713aSLionel Sambuc 1767f4a2713aSLionel SambucSee also :ref:`Microsoft Extensions <c_ms>`. 1768f4a2713aSLionel Sambuc 1769f4a2713aSLionel SambucCygwin 1770f4a2713aSLionel Sambuc"""""" 1771f4a2713aSLionel Sambuc 1772f4a2713aSLionel SambucClang works on Cygwin-1.7. 1773f4a2713aSLionel Sambuc 1774f4a2713aSLionel SambucMinGW32 1775f4a2713aSLionel Sambuc""""""" 1776f4a2713aSLionel Sambuc 1777f4a2713aSLionel SambucClang works on some mingw32 distributions. Clang assumes directories as 1778f4a2713aSLionel Sambucbelow; 1779f4a2713aSLionel Sambuc 1780f4a2713aSLionel Sambuc- ``C:/mingw/include`` 1781f4a2713aSLionel Sambuc- ``C:/mingw/lib`` 1782f4a2713aSLionel Sambuc- ``C:/mingw/lib/gcc/mingw32/4.[3-5].0/include/c++`` 1783f4a2713aSLionel Sambuc 1784f4a2713aSLionel SambucOn MSYS, a few tests might fail. 1785f4a2713aSLionel Sambuc 1786f4a2713aSLionel SambucMinGW-w64 1787f4a2713aSLionel Sambuc""""""""" 1788f4a2713aSLionel Sambuc 1789f4a2713aSLionel SambucFor 32-bit (i686-w64-mingw32), and 64-bit (x86\_64-w64-mingw32), Clang 1790f4a2713aSLionel Sambucassumes as below; 1791f4a2713aSLionel Sambuc 1792f4a2713aSLionel Sambuc- ``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)`` 1793f4a2713aSLionel Sambuc- ``some_directory/bin/gcc.exe`` 1794f4a2713aSLionel Sambuc- ``some_directory/bin/clang.exe`` 1795f4a2713aSLionel Sambuc- ``some_directory/bin/clang++.exe`` 1796f4a2713aSLionel Sambuc- ``some_directory/bin/../include/c++/GCC_version`` 1797f4a2713aSLionel Sambuc- ``some_directory/bin/../include/c++/GCC_version/x86_64-w64-mingw32`` 1798f4a2713aSLionel Sambuc- ``some_directory/bin/../include/c++/GCC_version/i686-w64-mingw32`` 1799f4a2713aSLionel Sambuc- ``some_directory/bin/../include/c++/GCC_version/backward`` 1800f4a2713aSLionel Sambuc- ``some_directory/bin/../x86_64-w64-mingw32/include`` 1801f4a2713aSLionel Sambuc- ``some_directory/bin/../i686-w64-mingw32/include`` 1802f4a2713aSLionel Sambuc- ``some_directory/bin/../include`` 1803f4a2713aSLionel Sambuc 1804f4a2713aSLionel SambucThis directory layout is standard for any toolchain you will find on the 1805f4a2713aSLionel Sambucofficial `MinGW-w64 website <http://mingw-w64.sourceforge.net>`_. 1806f4a2713aSLionel Sambuc 1807f4a2713aSLionel SambucClang expects the GCC executable "gcc.exe" compiled for 1808f4a2713aSLionel Sambuc``i686-w64-mingw32`` (or ``x86_64-w64-mingw32``) to be present on PATH. 1809f4a2713aSLionel Sambuc 1810f4a2713aSLionel Sambuc`Some tests might fail <http://llvm.org/bugs/show_bug.cgi?id=9072>`_ on 1811f4a2713aSLionel Sambuc``x86_64-w64-mingw32``. 1812f4a2713aSLionel Sambuc 1813f4a2713aSLionel Sambuc.. _clang-cl: 1814f4a2713aSLionel Sambuc 1815f4a2713aSLionel Sambucclang-cl 1816f4a2713aSLionel Sambuc======== 1817f4a2713aSLionel Sambuc 1818f4a2713aSLionel Sambucclang-cl is an alternative command-line interface to Clang driver, designed for 1819f4a2713aSLionel Sambuccompatibility with the Visual C++ compiler, cl.exe. 1820f4a2713aSLionel Sambuc 1821f4a2713aSLionel SambucTo enable clang-cl to find system headers, libraries, and the linker when run 1822f4a2713aSLionel Sambucfrom the command-line, it should be executed inside a Visual Studio Native Tools 1823f4a2713aSLionel SambucCommand Prompt or a regular Command Prompt where the environment has been set 1824f4a2713aSLionel Sambucup using e.g. `vcvars32.bat <http://msdn.microsoft.com/en-us/library/f2ccy3wt.aspx>`_. 1825f4a2713aSLionel Sambuc 1826f4a2713aSLionel Sambucclang-cl can also be used from inside Visual Studio by using an LLVM Platform 1827f4a2713aSLionel SambucToolset. 1828f4a2713aSLionel Sambuc 1829f4a2713aSLionel SambucCommand-Line Options 1830f4a2713aSLionel Sambuc-------------------- 1831f4a2713aSLionel Sambuc 1832f4a2713aSLionel SambucTo be compatible with cl.exe, clang-cl supports most of the same command-line 1833f4a2713aSLionel Sambucoptions. Those options can start with either ``/`` or ``-``. It also supports 1834f4a2713aSLionel Sambucsome of Clang's core options, such as the ``-W`` options. 1835f4a2713aSLionel Sambuc 1836f4a2713aSLionel SambucOptions that are known to clang-cl, but not currently supported, are ignored 1837f4a2713aSLionel Sambucwith a warning. For example: 1838f4a2713aSLionel Sambuc 1839f4a2713aSLionel Sambuc :: 1840f4a2713aSLionel Sambuc 1841f4a2713aSLionel Sambuc clang-cl.exe: warning: argument unused during compilation: '/Zi' 1842f4a2713aSLionel Sambuc 1843f4a2713aSLionel SambucTo suppress warnings about unused arguments, use the ``-Qunused-arguments`` option. 1844f4a2713aSLionel Sambuc 1845f4a2713aSLionel SambucOptions that are not known to clang-cl will cause errors. If they are spelled with a 1846f4a2713aSLionel Sambucleading ``/``, they will be mistaken for a filename: 1847f4a2713aSLionel Sambuc 1848f4a2713aSLionel Sambuc :: 1849f4a2713aSLionel Sambuc 1850f4a2713aSLionel Sambuc clang-cl.exe: error: no such file or directory: '/foobar' 1851f4a2713aSLionel Sambuc 1852f4a2713aSLionel SambucPlease `file a bug <http://llvm.org/bugs/enter_bug.cgi?product=clang&component=Driver>`_ 1853f4a2713aSLionel Sambucfor any valid cl.exe flags that clang-cl does not understand. 1854f4a2713aSLionel Sambuc 1855f4a2713aSLionel SambucExecute ``clang-cl /?`` to see a list of supported options: 1856f4a2713aSLionel Sambuc 1857f4a2713aSLionel Sambuc :: 1858f4a2713aSLionel Sambuc 1859*0a6a1f1dSLionel Sambuc CL.EXE COMPATIBILITY OPTIONS: 1860f4a2713aSLionel Sambuc /? Display available options 1861*0a6a1f1dSLionel Sambuc /arch:<value> Set architecture for code generation 1862*0a6a1f1dSLionel Sambuc /C Don't discard comments when preprocessing 1863f4a2713aSLionel Sambuc /c Compile only 1864f4a2713aSLionel Sambuc /D <macro[=value]> Define macro 1865*0a6a1f1dSLionel Sambuc /EH<value> Exception handling model 1866*0a6a1f1dSLionel Sambuc /EP Disable linemarker output and preprocess to stdout 1867*0a6a1f1dSLionel Sambuc /E Preprocess to stdout 1868f4a2713aSLionel Sambuc /fallback Fall back to cl.exe if clang-cl fails to compile 1869f4a2713aSLionel Sambuc /FA Output assembly code file during compilation 1870f4a2713aSLionel Sambuc /Fa<file or directory> Output assembly code to this file during compilation 1871f4a2713aSLionel Sambuc /Fe<file or directory> Set output executable file or directory (ends in / or \) 1872f4a2713aSLionel Sambuc /FI <value> Include file before parsing 1873*0a6a1f1dSLionel Sambuc /Fi<file> Set preprocess output file name 1874f4a2713aSLionel Sambuc /Fo<file or directory> Set output object file, or directory (ends in / or \) 1875f4a2713aSLionel Sambuc /GF- Disable string pooling 1876*0a6a1f1dSLionel Sambuc /GR- Disable emission of RTTI data 1877*0a6a1f1dSLionel Sambuc /GR Enable emission of RTTI data 1878*0a6a1f1dSLionel Sambuc /Gw- Don't put each data item in its own section 1879*0a6a1f1dSLionel Sambuc /Gw Put each data item in its own section 1880*0a6a1f1dSLionel Sambuc /Gy- Don't put each function in its own section 1881*0a6a1f1dSLionel Sambuc /Gy Put each function in its own section 1882f4a2713aSLionel Sambuc /help Display available options 1883f4a2713aSLionel Sambuc /I <dir> Add directory to include search path 1884f4a2713aSLionel Sambuc /J Make char type unsigned 1885f4a2713aSLionel Sambuc /LDd Create debug DLL 1886f4a2713aSLionel Sambuc /LD Create DLL 1887f4a2713aSLionel Sambuc /link <options> Forward options to the linker 1888f4a2713aSLionel Sambuc /MDd Use DLL debug run-time 1889f4a2713aSLionel Sambuc /MD Use DLL run-time 1890f4a2713aSLionel Sambuc /MTd Use static debug run-time 1891f4a2713aSLionel Sambuc /MT Use static run-time 1892f4a2713aSLionel Sambuc /Ob0 Disable inlining 1893f4a2713aSLionel Sambuc /Od Disable optimization 1894f4a2713aSLionel Sambuc /Oi- Disable use of builtin functions 1895f4a2713aSLionel Sambuc /Oi Enable use of builtin functions 1896f4a2713aSLionel Sambuc /Os Optimize for size 1897f4a2713aSLionel Sambuc /Ot Optimize for speed 1898f4a2713aSLionel Sambuc /Ox Maximum optimization 1899f4a2713aSLionel Sambuc /Oy- Disable frame pointer omission 1900f4a2713aSLionel Sambuc /Oy Enable frame pointer omission 1901f4a2713aSLionel Sambuc /O<n> Optimization level 1902*0a6a1f1dSLionel Sambuc /P Preprocess to file 1903f4a2713aSLionel Sambuc /showIncludes Print info about included files to stderr 1904f4a2713aSLionel Sambuc /TC Treat all source files as C 1905f4a2713aSLionel Sambuc /Tc <filename> Specify a C source file 1906f4a2713aSLionel Sambuc /TP Treat all source files as C++ 1907f4a2713aSLionel Sambuc /Tp <filename> Specify a C++ source file 1908f4a2713aSLionel Sambuc /U <macro> Undefine macro 1909*0a6a1f1dSLionel Sambuc /vd<value> Control vtordisp placement 1910*0a6a1f1dSLionel Sambuc /vmb Use a best-case representation method for member pointers 1911*0a6a1f1dSLionel Sambuc /vmg Use a most-general representation for member pointers 1912*0a6a1f1dSLionel Sambuc /vmm Set the default most-general representation to multiple inheritance 1913*0a6a1f1dSLionel Sambuc /vms Set the default most-general representation to single inheritance 1914*0a6a1f1dSLionel Sambuc /vmv Set the default most-general representation to virtual inheritance 1915f4a2713aSLionel Sambuc /W0 Disable all warnings 1916f4a2713aSLionel Sambuc /W1 Enable -Wall 1917f4a2713aSLionel Sambuc /W2 Enable -Wall 1918f4a2713aSLionel Sambuc /W3 Enable -Wall 1919f4a2713aSLionel Sambuc /W4 Enable -Wall 1920f4a2713aSLionel Sambuc /Wall Enable -Wall 1921f4a2713aSLionel Sambuc /WX- Do not treat warnings as errors 1922f4a2713aSLionel Sambuc /WX Treat warnings as errors 1923f4a2713aSLionel Sambuc /w Disable all warnings 1924*0a6a1f1dSLionel Sambuc /Zi Enable debug information 1925*0a6a1f1dSLionel Sambuc /Zp Set the default maximum struct packing alignment to 1 1926*0a6a1f1dSLionel Sambuc /Zp<value> Specify the default maximum struct packing alignment 1927f4a2713aSLionel Sambuc /Zs Syntax-check only 1928f4a2713aSLionel Sambuc 1929*0a6a1f1dSLionel Sambuc OPTIONS: 1930*0a6a1f1dSLionel Sambuc -### Print (but do not run) the commands to run for this compilation 1931*0a6a1f1dSLionel Sambuc -fms-compatibility-version=<value> 1932*0a6a1f1dSLionel Sambuc Dot-separated value representing the Microsoft compiler version 1933*0a6a1f1dSLionel Sambuc number to report in _MSC_VER (0 = don't define it (default)) 1934*0a6a1f1dSLionel Sambuc -fmsc-version=<value> Microsoft compiler version number to report in _MSC_VER (0 = don't 1935*0a6a1f1dSLionel Sambuc define it (default)) 1936*0a6a1f1dSLionel Sambuc -fsanitize-blacklist=<value> 1937*0a6a1f1dSLionel Sambuc Path to blacklist file for sanitizers 1938*0a6a1f1dSLionel Sambuc -fsanitize=<check> Enable runtime instrumentation for bug detection: address (memory 1939*0a6a1f1dSLionel Sambuc errors) | thread (race detection) | undefined (miscellaneous 1940*0a6a1f1dSLionel Sambuc undefined behavior) 1941*0a6a1f1dSLionel Sambuc -mllvm <value> Additional arguments to forward to LLVM's option processing 1942*0a6a1f1dSLionel Sambuc -Qunused-arguments Don't emit warning for unused driver arguments 1943*0a6a1f1dSLionel Sambuc --target=<value> Generate code for the given target 1944*0a6a1f1dSLionel Sambuc -v Show commands to run and use verbose output 1945*0a6a1f1dSLionel Sambuc -W<warning> Enable the specified warning 1946*0a6a1f1dSLionel Sambuc -Xclang <arg> Pass <arg> to the clang compiler 1947*0a6a1f1dSLionel Sambuc 1948f4a2713aSLionel SambucThe /fallback Option 1949f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^ 1950f4a2713aSLionel Sambuc 1951f4a2713aSLionel SambucWhen clang-cl is run with the ``/fallback`` option, it will first try to 1952f4a2713aSLionel Sambuccompile files itself. For any file that it fails to compile, it will fall back 1953f4a2713aSLionel Sambucand try to compile the file by invoking cl.exe. 1954f4a2713aSLionel Sambuc 1955f4a2713aSLionel SambucThis option is intended to be used as a temporary means to build projects where 1956f4a2713aSLionel Sambucclang-cl cannot successfully compile all the files. clang-cl may fail to compile 1957f4a2713aSLionel Sambuca file either because it cannot generate code for some C++ feature, or because 1958f4a2713aSLionel Sambucit cannot parse some Microsoft language extension. 1959