Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 1 | ============================ |
| 2 | Clang Compiler User's Manual |
| 3 | ============================ |
| 4 | |
Paul Robinson | 8ce9b44 | 2016-08-15 18:45:52 +0000 | [diff] [blame] | 5 | .. include:: <isonum.txt> |
| 6 | |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 7 | .. contents:: |
| 8 | :local: |
| 9 | |
| 10 | Introduction |
| 11 | ============ |
| 12 | |
| 13 | The Clang Compiler is an open-source compiler for the C family of |
| 14 | programming languages, aiming to be the best in class implementation of |
| 15 | these languages. Clang builds on the LLVM optimizer and code generator, |
| 16 | allowing it to provide high-quality optimization and code generation |
| 17 | support for many targets. For more general information, please see the |
| 18 | `Clang Web Site <http://clang.llvm.org>`_ or the `LLVM Web |
| 19 | Site <http://llvm.org>`_. |
| 20 | |
| 21 | This document describes important notes about using Clang as a compiler |
| 22 | for an end-user, documenting the supported features, command line |
| 23 | options, etc. If you are interested in using Clang to build a tool that |
Dmitri Gribenko | d9d2607 | 2012-12-15 20:41:17 +0000 | [diff] [blame] | 24 | processes code, please see :doc:`InternalsManual`. If you are interested in the |
| 25 | `Clang Static Analyzer <http://clang-analyzer.llvm.org>`_, please see its web |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 26 | page. |
| 27 | |
Richard Smith | 58e1474 | 2016-10-27 20:55:56 +0000 | [diff] [blame] | 28 | Clang is one component in a complete toolchain for C family languages. |
| 29 | A separate document describes the other pieces necessary to |
| 30 | :doc:`assemble a complete toolchain <Toolchain>`. |
| 31 | |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 32 | Clang is designed to support the C family of programming languages, |
| 33 | which includes :ref:`C <c>`, :ref:`Objective-C <objc>`, :ref:`C++ <cxx>`, and |
| 34 | :ref:`Objective-C++ <objcxx>` as well as many dialects of those. For |
| 35 | language-specific information, please see the corresponding language |
| 36 | specific section: |
| 37 | |
| 38 | - :ref:`C Language <c>`: K&R C, ANSI C89, ISO C90, ISO C94 (C89+AMD1), ISO |
| 39 | C99 (+TC1, TC2, TC3). |
| 40 | - :ref:`Objective-C Language <objc>`: ObjC 1, ObjC 2, ObjC 2.1, plus |
| 41 | variants depending on base language. |
| 42 | - :ref:`C++ Language <cxx>` |
| 43 | - :ref:`Objective C++ Language <objcxx>` |
Anastasia Stulova | 18e165f | 2017-01-12 17:52:22 +0000 | [diff] [blame] | 44 | - :ref:`OpenCL C Language <opencl>`: v1.0, v1.1, v1.2, v2.0. |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 45 | |
| 46 | In addition to these base languages and their dialects, Clang supports a |
| 47 | broad variety of language extensions, which are documented in the |
| 48 | corresponding language section. These extensions are provided to be |
| 49 | compatible with the GCC, Microsoft, and other popular compilers as well |
| 50 | as to improve functionality through Clang-specific features. The Clang |
| 51 | driver and language features are intentionally designed to be as |
| 52 | compatible with the GNU GCC compiler as reasonably possible, easing |
| 53 | migration from GCC to Clang. In most cases, code "just works". |
Hans Wennborg | 2a6e6bc | 2013-10-10 01:15:16 +0000 | [diff] [blame] | 54 | Clang also provides an alternative driver, :ref:`clang-cl`, that is designed |
| 55 | to be compatible with the Visual C++ compiler, cl.exe. |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 56 | |
| 57 | In addition to language specific features, Clang has a variety of |
| 58 | features that depend on what CPU architecture or operating system is |
| 59 | being compiled for. Please see the :ref:`Target-Specific Features and |
| 60 | Limitations <target_features>` section for more details. |
| 61 | |
| 62 | The rest of the introduction introduces some basic :ref:`compiler |
| 63 | terminology <terminology>` that is used throughout this manual and |
| 64 | contains a basic :ref:`introduction to using Clang <basicusage>` as a |
| 65 | command line compiler. |
| 66 | |
| 67 | .. _terminology: |
| 68 | |
| 69 | Terminology |
| 70 | ----------- |
| 71 | |
| 72 | Front end, parser, backend, preprocessor, undefined behavior, |
| 73 | diagnostic, optimizer |
| 74 | |
| 75 | .. _basicusage: |
| 76 | |
| 77 | Basic Usage |
| 78 | ----------- |
| 79 | |
| 80 | Intro to how to use a C compiler for newbies. |
| 81 | |
| 82 | compile + link compile then link debug info enabling optimizations |
Richard Smith | ab506ad | 2014-10-20 23:26:58 +0000 | [diff] [blame] | 83 | picking a language to use, defaults to C11 by default. Autosenses based |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 84 | on extension. using a makefile |
| 85 | |
| 86 | Command Line Options |
| 87 | ==================== |
| 88 | |
| 89 | This section is generally an index into other sections. It does not go |
| 90 | into depth on the ones that are covered by other sections. However, the |
| 91 | first part introduces the language selection and other high level |
Dmitri Gribenko | 1436ff2 | 2012-12-19 22:06:59 +0000 | [diff] [blame] | 92 | options like :option:`-c`, :option:`-g`, etc. |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 93 | |
| 94 | Options to Control Error and Warning Messages |
| 95 | --------------------------------------------- |
| 96 | |
Dmitri Gribenko | 1436ff2 | 2012-12-19 22:06:59 +0000 | [diff] [blame] | 97 | .. option:: -Werror |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 98 | |
Dmitri Gribenko | 1436ff2 | 2012-12-19 22:06:59 +0000 | [diff] [blame] | 99 | Turn warnings into errors. |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 100 | |
Dmitri Gribenko | 1436ff2 | 2012-12-19 22:06:59 +0000 | [diff] [blame] | 101 | .. This is in plain monospaced font because it generates the same label as |
| 102 | .. -Werror, and Sphinx complains. |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 103 | |
Dmitri Gribenko | 1436ff2 | 2012-12-19 22:06:59 +0000 | [diff] [blame] | 104 | ``-Werror=foo`` |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 105 | |
Dmitri Gribenko | 1436ff2 | 2012-12-19 22:06:59 +0000 | [diff] [blame] | 106 | Turn warning "foo" into an error. |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 107 | |
Dmitri Gribenko | 1436ff2 | 2012-12-19 22:06:59 +0000 | [diff] [blame] | 108 | .. option:: -Wno-error=foo |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 109 | |
Dmitri Gribenko | 1436ff2 | 2012-12-19 22:06:59 +0000 | [diff] [blame] | 110 | Turn warning "foo" into an warning even if :option:`-Werror` is specified. |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 111 | |
Dmitri Gribenko | 1436ff2 | 2012-12-19 22:06:59 +0000 | [diff] [blame] | 112 | .. option:: -Wfoo |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 113 | |
Dmitri Gribenko | 1436ff2 | 2012-12-19 22:06:59 +0000 | [diff] [blame] | 114 | Enable warning "foo". |
Richard Smith | b6a3b4b | 2016-09-12 05:58:29 +0000 | [diff] [blame] | 115 | See the :doc:`diagnostics reference <DiagnosticsReference>` for a complete |
| 116 | list of the warning flags that can be specified in this way. |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 117 | |
Dmitri Gribenko | 1436ff2 | 2012-12-19 22:06:59 +0000 | [diff] [blame] | 118 | .. option:: -Wno-foo |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 119 | |
Dmitri Gribenko | 1436ff2 | 2012-12-19 22:06:59 +0000 | [diff] [blame] | 120 | Disable warning "foo". |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 121 | |
Dmitri Gribenko | 1436ff2 | 2012-12-19 22:06:59 +0000 | [diff] [blame] | 122 | .. option:: -w |
| 123 | |
Tobias Grosser | 7416024 | 2014-02-28 09:11:08 +0000 | [diff] [blame] | 124 | Disable all diagnostics. |
Dmitri Gribenko | 1436ff2 | 2012-12-19 22:06:59 +0000 | [diff] [blame] | 125 | |
| 126 | .. option:: -Weverything |
| 127 | |
Tobias Grosser | 7416024 | 2014-02-28 09:11:08 +0000 | [diff] [blame] | 128 | :ref:`Enable all diagnostics. <diagnostics_enable_everything>` |
Dmitri Gribenko | 1436ff2 | 2012-12-19 22:06:59 +0000 | [diff] [blame] | 129 | |
| 130 | .. option:: -pedantic |
| 131 | |
| 132 | Warn on language extensions. |
| 133 | |
| 134 | .. option:: -pedantic-errors |
| 135 | |
| 136 | Error on language extensions. |
| 137 | |
| 138 | .. option:: -Wsystem-headers |
| 139 | |
| 140 | Enable warnings from system headers. |
| 141 | |
| 142 | .. option:: -ferror-limit=123 |
| 143 | |
| 144 | Stop emitting diagnostics after 123 errors have been produced. The default is |
Aaron Ballman | 4f6b3ec | 2016-07-14 17:15:06 +0000 | [diff] [blame] | 145 | 20, and the error limit can be disabled with `-ferror-limit=0`. |
Dmitri Gribenko | 1436ff2 | 2012-12-19 22:06:59 +0000 | [diff] [blame] | 146 | |
| 147 | .. option:: -ftemplate-backtrace-limit=123 |
| 148 | |
| 149 | Only emit up to 123 template instantiation notes within the template |
| 150 | instantiation backtrace for a single warning or error. The default is 10, and |
Aaron Ballman | 4f6b3ec | 2016-07-14 17:15:06 +0000 | [diff] [blame] | 151 | the limit can be disabled with `-ftemplate-backtrace-limit=0`. |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 152 | |
| 153 | .. _cl_diag_formatting: |
| 154 | |
| 155 | Formatting of Diagnostics |
| 156 | ^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 157 | |
| 158 | Clang aims to produce beautiful diagnostics by default, particularly for |
| 159 | new users that first come to Clang. However, different people have |
Douglas Katzman | 1e7bf36 | 2015-08-03 20:41:31 +0000 | [diff] [blame] | 160 | different preferences, and sometimes Clang is driven not by a human, |
| 161 | but by a program that wants consistent and easily parsable output. For |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 162 | these cases, Clang provides a wide range of options to control the exact |
| 163 | output format of the diagnostics that it generates. |
| 164 | |
| 165 | .. _opt_fshow-column: |
| 166 | |
| 167 | **-f[no-]show-column** |
| 168 | Print column number in diagnostic. |
| 169 | |
| 170 | This option, which defaults to on, controls whether or not Clang |
| 171 | prints the column number of a diagnostic. For example, when this is |
| 172 | enabled, Clang will print something like: |
| 173 | |
| 174 | :: |
| 175 | |
| 176 | test.c:28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens] |
| 177 | #endif bad |
| 178 | ^ |
| 179 | // |
| 180 | |
| 181 | When this is disabled, Clang will print "test.c:28: warning..." with |
| 182 | no column number. |
| 183 | |
| 184 | The printed column numbers count bytes from the beginning of the |
| 185 | line; take care if your source contains multibyte characters. |
| 186 | |
| 187 | .. _opt_fshow-source-location: |
| 188 | |
| 189 | **-f[no-]show-source-location** |
| 190 | Print source file/line/column information in diagnostic. |
| 191 | |
| 192 | This option, which defaults to on, controls whether or not Clang |
| 193 | prints the filename, line number and column number of a diagnostic. |
| 194 | For example, when this is enabled, Clang will print something like: |
| 195 | |
| 196 | :: |
| 197 | |
| 198 | test.c:28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens] |
| 199 | #endif bad |
| 200 | ^ |
| 201 | // |
| 202 | |
| 203 | When this is disabled, Clang will not print the "test.c:28:8: " |
| 204 | part. |
| 205 | |
| 206 | .. _opt_fcaret-diagnostics: |
| 207 | |
| 208 | **-f[no-]caret-diagnostics** |
| 209 | Print source line and ranges from source code in diagnostic. |
| 210 | This option, which defaults to on, controls whether or not Clang |
| 211 | prints the source line, source ranges, and caret when emitting a |
| 212 | diagnostic. For example, when this is enabled, Clang will print |
| 213 | something like: |
| 214 | |
| 215 | :: |
| 216 | |
| 217 | test.c:28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens] |
| 218 | #endif bad |
| 219 | ^ |
| 220 | // |
| 221 | |
| 222 | **-f[no-]color-diagnostics** |
| 223 | This option, which defaults to on when a color-capable terminal is |
| 224 | detected, controls whether or not Clang prints diagnostics in color. |
| 225 | |
| 226 | When this option is enabled, Clang will use colors to highlight |
| 227 | specific parts of the diagnostic, e.g., |
| 228 | |
| 229 | .. nasty hack to not lose our dignity |
| 230 | |
| 231 | .. raw:: html |
| 232 | |
| 233 | <pre> |
| 234 | <b><span style="color:black">test.c:28:8: <span style="color:magenta">warning</span>: extra tokens at end of #endif directive [-Wextra-tokens]</span></b> |
| 235 | #endif bad |
| 236 | <span style="color:green">^</span> |
| 237 | <span style="color:green">//</span> |
| 238 | </pre> |
| 239 | |
| 240 | When this is disabled, Clang will just print: |
| 241 | |
| 242 | :: |
| 243 | |
| 244 | test.c:2:8: warning: extra tokens at end of #endif directive [-Wextra-tokens] |
| 245 | #endif bad |
| 246 | ^ |
| 247 | // |
| 248 | |
Nico Rieck | 7857d46 | 2013-09-11 00:38:02 +0000 | [diff] [blame] | 249 | **-fansi-escape-codes** |
| 250 | Controls whether ANSI escape codes are used instead of the Windows Console |
| 251 | API to output colored diagnostics. This option is only used on Windows and |
| 252 | defaults to off. |
| 253 | |
Dmitri Gribenko | 1436ff2 | 2012-12-19 22:06:59 +0000 | [diff] [blame] | 254 | .. option:: -fdiagnostics-format=clang/msvc/vi |
| 255 | |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 256 | Changes diagnostic output format to better match IDEs and command line tools. |
| 257 | |
| 258 | This option controls the output format of the filename, line number, |
| 259 | and column printed in diagnostic messages. The options, and their |
| 260 | affect on formatting a simple conversion diagnostic, follow: |
| 261 | |
| 262 | **clang** (default) |
| 263 | :: |
| 264 | |
| 265 | t.c:3:11: warning: conversion specifies type 'char *' but the argument has type 'int' |
| 266 | |
| 267 | **msvc** |
| 268 | :: |
| 269 | |
| 270 | t.c(3,11) : warning: conversion specifies type 'char *' but the argument has type 'int' |
| 271 | |
| 272 | **vi** |
| 273 | :: |
| 274 | |
| 275 | t.c +3:11: warning: conversion specifies type 'char *' but the argument has type 'int' |
| 276 | |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 277 | .. _opt_fdiagnostics-show-option: |
| 278 | |
| 279 | **-f[no-]diagnostics-show-option** |
| 280 | Enable ``[-Woption]`` information in diagnostic line. |
| 281 | |
| 282 | This option, which defaults to on, controls whether or not Clang |
| 283 | prints the associated :ref:`warning group <cl_diag_warning_groups>` |
| 284 | option name when outputting a warning diagnostic. For example, in |
| 285 | this output: |
| 286 | |
| 287 | :: |
| 288 | |
| 289 | test.c:28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens] |
| 290 | #endif bad |
| 291 | ^ |
| 292 | // |
| 293 | |
| 294 | Passing **-fno-diagnostics-show-option** will prevent Clang from |
| 295 | printing the [:ref:`-Wextra-tokens <opt_Wextra-tokens>`] information in |
| 296 | the diagnostic. This information tells you the flag needed to enable |
| 297 | or disable the diagnostic, either from the command line or through |
| 298 | :ref:`#pragma GCC diagnostic <pragma_GCC_diagnostic>`. |
| 299 | |
| 300 | .. _opt_fdiagnostics-show-category: |
| 301 | |
Dmitri Gribenko | 1436ff2 | 2012-12-19 22:06:59 +0000 | [diff] [blame] | 302 | .. option:: -fdiagnostics-show-category=none/id/name |
| 303 | |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 304 | Enable printing category information in diagnostic line. |
| 305 | |
| 306 | This option, which defaults to "none", controls whether or not Clang |
| 307 | prints the category associated with a diagnostic when emitting it. |
| 308 | Each diagnostic may or many not have an associated category, if it |
| 309 | has one, it is listed in the diagnostic categorization field of the |
| 310 | diagnostic line (in the []'s). |
| 311 | |
| 312 | For example, a format string warning will produce these three |
| 313 | renditions based on the setting of this option: |
| 314 | |
| 315 | :: |
| 316 | |
| 317 | t.c:3:11: warning: conversion specifies type 'char *' but the argument has type 'int' [-Wformat] |
| 318 | t.c:3:11: warning: conversion specifies type 'char *' but the argument has type 'int' [-Wformat,1] |
| 319 | t.c:3:11: warning: conversion specifies type 'char *' but the argument has type 'int' [-Wformat,Format String] |
| 320 | |
| 321 | This category can be used by clients that want to group diagnostics |
| 322 | by category, so it should be a high level category. We want dozens |
| 323 | of these, not hundreds or thousands of them. |
| 324 | |
Adam Nemet | 1eea3e5 | 2016-09-13 04:32:40 +0000 | [diff] [blame] | 325 | .. _opt_fdiagnostics-show-hotness: |
| 326 | |
| 327 | **-f[no-]diagnostics-show-hotness** |
| 328 | Enable profile hotness information in diagnostic line. |
| 329 | |
| 330 | This option, which defaults to off, controls whether Clang prints the |
| 331 | profile hotness associated with a diagnostics in the presence of |
| 332 | profile-guided optimization information. This is currently supported with |
| 333 | optimization remarks (see :ref:`Options to Emit Optimization Reports |
| 334 | <rpass>`). The hotness information allows users to focus on the hot |
| 335 | optimization remarks that are likely to be more relevant for run-time |
| 336 | performance. |
| 337 | |
| 338 | For example, in this output, the block containing the callsite of `foo` was |
| 339 | executed 3000 times according to the profile data: |
| 340 | |
| 341 | :: |
| 342 | |
| 343 | s.c:7:10: remark: foo inlined into bar (hotness: 3000) [-Rpass-analysis=inline] |
| 344 | sum += foo(x, x - 2); |
| 345 | ^ |
| 346 | |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 347 | .. _opt_fdiagnostics-fixit-info: |
| 348 | |
| 349 | **-f[no-]diagnostics-fixit-info** |
| 350 | Enable "FixIt" information in the diagnostics output. |
| 351 | |
| 352 | This option, which defaults to on, controls whether or not Clang |
| 353 | prints the information on how to fix a specific diagnostic |
| 354 | underneath it when it knows. For example, in this output: |
| 355 | |
| 356 | :: |
| 357 | |
| 358 | test.c:28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens] |
| 359 | #endif bad |
| 360 | ^ |
| 361 | // |
| 362 | |
| 363 | Passing **-fno-diagnostics-fixit-info** will prevent Clang from |
| 364 | printing the "//" line at the end of the message. This information |
| 365 | is useful for users who may not understand what is wrong, but can be |
| 366 | confusing for machine parsing. |
| 367 | |
| 368 | .. _opt_fdiagnostics-print-source-range-info: |
| 369 | |
Nico Weber | 69dce49c7 | 2013-01-09 05:06:41 +0000 | [diff] [blame] | 370 | **-fdiagnostics-print-source-range-info** |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 371 | Print machine parsable information about source ranges. |
Nico Weber | 69dce49c7 | 2013-01-09 05:06:41 +0000 | [diff] [blame] | 372 | This option makes Clang print information about source ranges in a machine |
| 373 | parsable format after the file/line/column number information. The |
| 374 | information is a simple sequence of brace enclosed ranges, where each range |
| 375 | lists the start and end line/column locations. For example, in this output: |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 376 | |
| 377 | :: |
| 378 | |
| 379 | exprs.c:47:15:{47:8-47:14}{47:17-47:24}: error: invalid operands to binary expression ('int *' and '_Complex float') |
| 380 | P = (P-42) + Gamma*4; |
| 381 | ~~~~~~ ^ ~~~~~~~ |
| 382 | |
| 383 | The {}'s are generated by -fdiagnostics-print-source-range-info. |
| 384 | |
| 385 | The printed column numbers count bytes from the beginning of the |
| 386 | line; take care if your source contains multibyte characters. |
| 387 | |
Dmitri Gribenko | 1436ff2 | 2012-12-19 22:06:59 +0000 | [diff] [blame] | 388 | .. option:: -fdiagnostics-parseable-fixits |
| 389 | |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 390 | Print Fix-Its in a machine parseable form. |
| 391 | |
| 392 | This option makes Clang print available Fix-Its in a machine |
| 393 | parseable format at the end of diagnostics. The following example |
| 394 | illustrates the format: |
| 395 | |
| 396 | :: |
| 397 | |
| 398 | fix-it:"t.cpp":{7:25-7:29}:"Gamma" |
| 399 | |
| 400 | The range printed is a half-open range, so in this example the |
| 401 | characters at column 25 up to but not including column 29 on line 7 |
| 402 | in t.cpp should be replaced with the string "Gamma". Either the |
| 403 | range or the replacement string may be empty (representing strict |
| 404 | insertions and strict erasures, respectively). Both the file name |
| 405 | and the insertion string escape backslash (as "\\\\"), tabs (as |
| 406 | "\\t"), newlines (as "\\n"), double quotes(as "\\"") and |
| 407 | non-printable characters (as octal "\\xxx"). |
| 408 | |
| 409 | The printed column numbers count bytes from the beginning of the |
| 410 | line; take care if your source contains multibyte characters. |
| 411 | |
Dmitri Gribenko | 1436ff2 | 2012-12-19 22:06:59 +0000 | [diff] [blame] | 412 | .. option:: -fno-elide-type |
| 413 | |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 414 | Turns off elision in template type printing. |
| 415 | |
| 416 | The default for template type printing is to elide as many template |
| 417 | arguments as possible, removing those which are the same in both |
| 418 | template types, leaving only the differences. Adding this flag will |
| 419 | print all the template arguments. If supported by the terminal, |
| 420 | highlighting will still appear on differing arguments. |
| 421 | |
| 422 | Default: |
| 423 | |
| 424 | :: |
| 425 | |
| 426 | 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; |
| 427 | |
| 428 | -fno-elide-type: |
| 429 | |
| 430 | :: |
| 431 | |
| 432 | 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; |
| 433 | |
Dmitri Gribenko | 1436ff2 | 2012-12-19 22:06:59 +0000 | [diff] [blame] | 434 | .. option:: -fdiagnostics-show-template-tree |
| 435 | |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 436 | Template type diffing prints a text tree. |
| 437 | |
| 438 | For diffing large templated types, this option will cause Clang to |
| 439 | display the templates as an indented text tree, one argument per |
| 440 | line, with differences marked inline. This is compatible with |
| 441 | -fno-elide-type. |
| 442 | |
| 443 | Default: |
| 444 | |
| 445 | :: |
| 446 | |
| 447 | 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; |
| 448 | |
Dmitri Gribenko | 1436ff2 | 2012-12-19 22:06:59 +0000 | [diff] [blame] | 449 | With :option:`-fdiagnostics-show-template-tree`: |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 450 | |
| 451 | :: |
| 452 | |
| 453 | t.cc:4:5: note: candidate function not viable: no known conversion for 1st argument; |
| 454 | vector< |
| 455 | map< |
| 456 | [...], |
| 457 | map< |
Richard Trieu | 98ca59e | 2013-08-09 22:52:48 +0000 | [diff] [blame] | 458 | [float != double], |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 459 | [...]>>> |
| 460 | |
| 461 | .. _cl_diag_warning_groups: |
| 462 | |
| 463 | Individual Warning Groups |
| 464 | ^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 465 | |
| 466 | TODO: Generate this from tblgen. Define one anchor per warning group. |
| 467 | |
| 468 | .. _opt_wextra-tokens: |
| 469 | |
Dmitri Gribenko | 1436ff2 | 2012-12-19 22:06:59 +0000 | [diff] [blame] | 470 | .. option:: -Wextra-tokens |
| 471 | |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 472 | Warn about excess tokens at the end of a preprocessor directive. |
| 473 | |
| 474 | This option, which defaults to on, enables warnings about extra |
| 475 | tokens at the end of preprocessor directives. For example: |
| 476 | |
| 477 | :: |
| 478 | |
| 479 | test.c:28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens] |
| 480 | #endif bad |
| 481 | ^ |
| 482 | |
| 483 | These extra tokens are not strictly conforming, and are usually best |
| 484 | handled by commenting them out. |
| 485 | |
Dmitri Gribenko | 1436ff2 | 2012-12-19 22:06:59 +0000 | [diff] [blame] | 486 | .. option:: -Wambiguous-member-template |
| 487 | |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 488 | Warn about unqualified uses of a member template whose name resolves to |
| 489 | another template at the location of the use. |
| 490 | |
| 491 | This option, which defaults to on, enables a warning in the |
| 492 | following code: |
| 493 | |
| 494 | :: |
| 495 | |
| 496 | template<typename T> struct set{}; |
| 497 | template<typename T> struct trait { typedef const T& type; }; |
| 498 | struct Value { |
| 499 | template<typename T> void set(typename trait<T>::type value) {} |
| 500 | }; |
| 501 | void foo() { |
| 502 | Value v; |
| 503 | v.set<double>(3.2); |
| 504 | } |
| 505 | |
| 506 | C++ [basic.lookup.classref] requires this to be an error, but, |
| 507 | because it's hard to work around, Clang downgrades it to a warning |
| 508 | as an extension. |
| 509 | |
Dmitri Gribenko | 1436ff2 | 2012-12-19 22:06:59 +0000 | [diff] [blame] | 510 | .. option:: -Wbind-to-temporary-copy |
| 511 | |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 512 | Warn about an unusable copy constructor when binding a reference to a |
| 513 | temporary. |
| 514 | |
Nico Weber | acb35c0 | 2014-09-18 02:09:53 +0000 | [diff] [blame] | 515 | This option enables warnings about binding a |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 516 | reference to a temporary when the temporary doesn't have a usable |
| 517 | copy constructor. For example: |
| 518 | |
| 519 | :: |
| 520 | |
| 521 | struct NonCopyable { |
| 522 | NonCopyable(); |
| 523 | private: |
| 524 | NonCopyable(const NonCopyable&); |
| 525 | }; |
| 526 | void foo(const NonCopyable&); |
| 527 | void bar() { |
| 528 | foo(NonCopyable()); // Disallowed in C++98; allowed in C++11. |
| 529 | } |
| 530 | |
| 531 | :: |
| 532 | |
| 533 | struct NonCopyable2 { |
| 534 | NonCopyable2(); |
| 535 | NonCopyable2(NonCopyable2&); |
| 536 | }; |
| 537 | void foo(const NonCopyable2&); |
| 538 | void bar() { |
| 539 | foo(NonCopyable2()); // Disallowed in C++98; allowed in C++11. |
| 540 | } |
| 541 | |
| 542 | Note that if ``NonCopyable2::NonCopyable2()`` has a default argument |
| 543 | whose instantiation produces a compile error, that error will still |
| 544 | be a hard error in C++98 mode even if this warning is turned off. |
| 545 | |
| 546 | Options to Control Clang Crash Diagnostics |
| 547 | ------------------------------------------ |
| 548 | |
| 549 | As unbelievable as it may sound, Clang does crash from time to time. |
| 550 | Generally, this only occurs to those living on the `bleeding |
| 551 | edge <http://llvm.org/releases/download.html#svn>`_. Clang goes to great |
| 552 | lengths to assist you in filing a bug report. Specifically, Clang |
| 553 | generates preprocessed source file(s) and associated run script(s) upon |
| 554 | a crash. These files should be attached to a bug report to ease |
| 555 | reproducibility of the failure. Below are the command line options to |
| 556 | control the crash diagnostics. |
| 557 | |
Dmitri Gribenko | 1436ff2 | 2012-12-19 22:06:59 +0000 | [diff] [blame] | 558 | .. option:: -fno-crash-diagnostics |
| 559 | |
| 560 | Disable auto-generation of preprocessed source files during a clang crash. |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 561 | |
| 562 | The -fno-crash-diagnostics flag can be helpful for speeding the process |
| 563 | of generating a delta reduced test case. |
| 564 | |
Adam Nemet | 1eea3e5 | 2016-09-13 04:32:40 +0000 | [diff] [blame] | 565 | .. _rpass: |
| 566 | |
Diego Novillo | 263ce21 | 2014-05-29 20:13:27 +0000 | [diff] [blame] | 567 | Options to Emit Optimization Reports |
| 568 | ------------------------------------ |
| 569 | |
| 570 | Optimization reports trace, at a high-level, all the major decisions |
| 571 | done by compiler transformations. For instance, when the inliner |
| 572 | decides to inline function ``foo()`` into ``bar()``, or the loop unroller |
| 573 | decides to unroll a loop N times, or the vectorizer decides to |
| 574 | vectorize a loop body. |
| 575 | |
| 576 | Clang offers a family of flags which the optimizers can use to emit |
| 577 | a diagnostic in three cases: |
| 578 | |
Aaron Ballman | 05efec8 | 2016-07-15 12:55:47 +0000 | [diff] [blame] | 579 | 1. When the pass makes a transformation (`-Rpass`). |
Diego Novillo | 263ce21 | 2014-05-29 20:13:27 +0000 | [diff] [blame] | 580 | |
Aaron Ballman | 05efec8 | 2016-07-15 12:55:47 +0000 | [diff] [blame] | 581 | 2. When the pass fails to make a transformation (`-Rpass-missed`). |
Diego Novillo | 263ce21 | 2014-05-29 20:13:27 +0000 | [diff] [blame] | 582 | |
| 583 | 3. When the pass determines whether or not to make a transformation |
Aaron Ballman | 05efec8 | 2016-07-15 12:55:47 +0000 | [diff] [blame] | 584 | (`-Rpass-analysis`). |
Diego Novillo | 263ce21 | 2014-05-29 20:13:27 +0000 | [diff] [blame] | 585 | |
Aaron Ballman | 05efec8 | 2016-07-15 12:55:47 +0000 | [diff] [blame] | 586 | NOTE: Although the discussion below focuses on `-Rpass`, the exact |
| 587 | same options apply to `-Rpass-missed` and `-Rpass-analysis`. |
Diego Novillo | 263ce21 | 2014-05-29 20:13:27 +0000 | [diff] [blame] | 588 | |
| 589 | Since there are dozens of passes inside the compiler, each of these flags |
| 590 | take a regular expression that identifies the name of the pass which should |
| 591 | emit the associated diagnostic. For example, to get a report from the inliner, |
| 592 | compile the code with: |
| 593 | |
| 594 | .. code-block:: console |
| 595 | |
| 596 | $ clang -O2 -Rpass=inline code.cc -o code |
| 597 | code.cc:4:25: remark: foo inlined into bar [-Rpass=inline] |
| 598 | int bar(int j) { return foo(j, j - 2); } |
| 599 | ^ |
| 600 | |
| 601 | Note that remarks from the inliner are identified with `[-Rpass=inline]`. |
| 602 | To request a report from every optimization pass, you should use |
Aaron Ballman | 05efec8 | 2016-07-15 12:55:47 +0000 | [diff] [blame] | 603 | `-Rpass=.*` (in fact, you can use any valid POSIX regular |
Diego Novillo | 263ce21 | 2014-05-29 20:13:27 +0000 | [diff] [blame] | 604 | expression). However, do not expect a report from every transformation |
| 605 | made by the compiler. Optimization remarks do not really make sense |
| 606 | outside of the major transformations (e.g., inlining, vectorization, |
| 607 | loop optimizations) and not every optimization pass supports this |
| 608 | feature. |
| 609 | |
Adam Nemet | 1eea3e5 | 2016-09-13 04:32:40 +0000 | [diff] [blame] | 610 | Note that when using profile-guided optimization information, profile hotness |
| 611 | information can be included in the remarks (see |
| 612 | :ref:`-fdiagnostics-show-hotness <opt_fdiagnostics-show-hotness>`). |
| 613 | |
Diego Novillo | 263ce21 | 2014-05-29 20:13:27 +0000 | [diff] [blame] | 614 | Current limitations |
| 615 | ^^^^^^^^^^^^^^^^^^^ |
| 616 | |
Diego Novillo | 94b276d | 2014-07-10 23:29:28 +0000 | [diff] [blame] | 617 | 1. Optimization remarks that refer to function names will display the |
Diego Novillo | 263ce21 | 2014-05-29 20:13:27 +0000 | [diff] [blame] | 618 | mangled name of the function. Since these remarks are emitted by the |
| 619 | back end of the compiler, it does not know anything about the input |
| 620 | language, nor its mangling rules. |
| 621 | |
Diego Novillo | 94b276d | 2014-07-10 23:29:28 +0000 | [diff] [blame] | 622 | 2. Some source locations are not displayed correctly. The front end has |
Diego Novillo | 263ce21 | 2014-05-29 20:13:27 +0000 | [diff] [blame] | 623 | a more detailed source location tracking than the locations included |
| 624 | in the debug info (e.g., the front end can locate code inside macro |
Aaron Ballman | 05efec8 | 2016-07-15 12:55:47 +0000 | [diff] [blame] | 625 | expansions). However, the locations used by `-Rpass` are |
Diego Novillo | 263ce21 | 2014-05-29 20:13:27 +0000 | [diff] [blame] | 626 | translated from debug annotations. That translation can be lossy, |
| 627 | which results in some remarks having no location information. |
| 628 | |
Paul Robinson | d7214a7 | 2015-04-27 18:14:32 +0000 | [diff] [blame] | 629 | Other Options |
| 630 | ------------- |
| 631 | Clang options that that don't fit neatly into other categories. |
| 632 | |
| 633 | .. option:: -MV |
| 634 | |
| 635 | When emitting a dependency file, use formatting conventions appropriate |
| 636 | for NMake or Jom. Ignored unless another option causes Clang to emit a |
| 637 | dependency file. |
| 638 | |
| 639 | When Clang emits a dependency file (e.g., you supplied the -M option) |
| 640 | most filenames can be written to the file without any special formatting. |
| 641 | Different Make tools will treat different sets of characters as "special" |
| 642 | and use different conventions for telling the Make tool that the character |
| 643 | is actually part of the filename. Normally Clang uses backslash to "escape" |
| 644 | a special character, which is the convention used by GNU Make. The -MV |
| 645 | option tells Clang to put double-quotes around the entire filename, which |
| 646 | is the convention used by NMake and Jom. |
| 647 | |
Diego Novillo | 263ce21 | 2014-05-29 20:13:27 +0000 | [diff] [blame] | 648 | |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 649 | Language and Target-Independent Features |
| 650 | ======================================== |
| 651 | |
| 652 | Controlling Errors and Warnings |
| 653 | ------------------------------- |
| 654 | |
| 655 | Clang provides a number of ways to control which code constructs cause |
| 656 | it to emit errors and warning messages, and how they are displayed to |
| 657 | the console. |
| 658 | |
| 659 | Controlling How Clang Displays Diagnostics |
| 660 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 661 | |
| 662 | When Clang emits a diagnostic, it includes rich information in the |
| 663 | output, and gives you fine-grain control over which information is |
| 664 | printed. Clang has the ability to print this information, and these are |
| 665 | the options that control it: |
| 666 | |
| 667 | #. A file/line/column indicator that shows exactly where the diagnostic |
| 668 | occurs in your code [:ref:`-fshow-column <opt_fshow-column>`, |
| 669 | :ref:`-fshow-source-location <opt_fshow-source-location>`]. |
| 670 | #. A categorization of the diagnostic as a note, warning, error, or |
| 671 | fatal error. |
| 672 | #. A text string that describes what the problem is. |
| 673 | #. An option that indicates how to control the diagnostic (for |
| 674 | diagnostics that support it) |
| 675 | [:ref:`-fdiagnostics-show-option <opt_fdiagnostics-show-option>`]. |
| 676 | #. A :ref:`high-level category <diagnostics_categories>` for the diagnostic |
| 677 | for clients that want to group diagnostics by class (for diagnostics |
| 678 | that support it) |
| 679 | [:ref:`-fdiagnostics-show-category <opt_fdiagnostics-show-category>`]. |
| 680 | #. The line of source code that the issue occurs on, along with a caret |
| 681 | and ranges that indicate the important locations |
| 682 | [:ref:`-fcaret-diagnostics <opt_fcaret-diagnostics>`]. |
| 683 | #. "FixIt" information, which is a concise explanation of how to fix the |
| 684 | problem (when Clang is certain it knows) |
| 685 | [:ref:`-fdiagnostics-fixit-info <opt_fdiagnostics-fixit-info>`]. |
| 686 | #. A machine-parsable representation of the ranges involved (off by |
| 687 | default) |
| 688 | [:ref:`-fdiagnostics-print-source-range-info <opt_fdiagnostics-print-source-range-info>`]. |
| 689 | |
| 690 | For more information please see :ref:`Formatting of |
| 691 | Diagnostics <cl_diag_formatting>`. |
| 692 | |
| 693 | Diagnostic Mappings |
| 694 | ^^^^^^^^^^^^^^^^^^^ |
| 695 | |
Alex Denisov | 793e067 | 2015-02-11 07:56:16 +0000 | [diff] [blame] | 696 | All diagnostics are mapped into one of these 6 classes: |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 697 | |
| 698 | - Ignored |
| 699 | - Note |
Tobias Grosser | 7416024 | 2014-02-28 09:11:08 +0000 | [diff] [blame] | 700 | - Remark |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 701 | - Warning |
| 702 | - Error |
| 703 | - Fatal |
| 704 | |
| 705 | .. _diagnostics_categories: |
| 706 | |
| 707 | Diagnostic Categories |
| 708 | ^^^^^^^^^^^^^^^^^^^^^ |
| 709 | |
| 710 | Though not shown by default, diagnostics may each be associated with a |
| 711 | high-level category. This category is intended to make it possible to |
| 712 | triage builds that produce a large number of errors or warnings in a |
| 713 | grouped way. |
| 714 | |
| 715 | Categories are not shown by default, but they can be turned on with the |
| 716 | :ref:`-fdiagnostics-show-category <opt_fdiagnostics-show-category>` option. |
| 717 | When set to "``name``", the category is printed textually in the |
| 718 | diagnostic output. When it is set to "``id``", a category number is |
| 719 | printed. The mapping of category names to category id's can be obtained |
| 720 | by running '``clang --print-diagnostic-categories``'. |
| 721 | |
| 722 | Controlling Diagnostics via Command Line Flags |
| 723 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 724 | |
| 725 | TODO: -W flags, -pedantic, etc |
| 726 | |
| 727 | .. _pragma_gcc_diagnostic: |
| 728 | |
| 729 | Controlling Diagnostics via Pragmas |
| 730 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 731 | |
| 732 | Clang can also control what diagnostics are enabled through the use of |
| 733 | pragmas in the source code. This is useful for turning off specific |
| 734 | warnings in a section of source code. Clang supports GCC's pragma for |
| 735 | compatibility with existing source code, as well as several extensions. |
| 736 | |
| 737 | The pragma may control any warning that can be used from the command |
| 738 | line. Warnings may be set to ignored, warning, error, or fatal. The |
| 739 | following example code will tell Clang or GCC to ignore the -Wall |
| 740 | warnings: |
| 741 | |
Dmitri Gribenko | 1436ff2 | 2012-12-19 22:06:59 +0000 | [diff] [blame] | 742 | .. code-block:: c |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 743 | |
Dmitri Gribenko | 1436ff2 | 2012-12-19 22:06:59 +0000 | [diff] [blame] | 744 | #pragma GCC diagnostic ignored "-Wall" |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 745 | |
| 746 | In addition to all of the functionality provided by GCC's pragma, Clang |
| 747 | also allows you to push and pop the current warning state. This is |
| 748 | particularly useful when writing a header file that will be compiled by |
| 749 | other people, because you don't know what warning flags they build with. |
| 750 | |
George Burgess IV | bc8cc5ac | 2016-06-21 02:19:43 +0000 | [diff] [blame] | 751 | In the below example :option:`-Wextra-tokens` is ignored for only a single line |
| 752 | of code, after which the diagnostics return to whatever state had previously |
Dmitri Gribenko | 1436ff2 | 2012-12-19 22:06:59 +0000 | [diff] [blame] | 753 | existed. |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 754 | |
Dmitri Gribenko | 1436ff2 | 2012-12-19 22:06:59 +0000 | [diff] [blame] | 755 | .. code-block:: c |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 756 | |
George Burgess IV | bc8cc5ac | 2016-06-21 02:19:43 +0000 | [diff] [blame] | 757 | #if foo |
| 758 | #endif foo // warning: extra tokens at end of #endif directive |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 759 | |
Asiri Rathnayake | b0bbb7d | 2017-02-02 10:35:18 +0000 | [diff] [blame] | 760 | #pragma clang diagnostic push |
George Burgess IV | bc8cc5ac | 2016-06-21 02:19:43 +0000 | [diff] [blame] | 761 | #pragma clang diagnostic ignored "-Wextra-tokens" |
| 762 | |
| 763 | #if foo |
| 764 | #endif foo // no warning |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 765 | |
Dmitri Gribenko | 1436ff2 | 2012-12-19 22:06:59 +0000 | [diff] [blame] | 766 | #pragma clang diagnostic pop |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 767 | |
| 768 | The push and pop pragmas will save and restore the full diagnostic state |
| 769 | of the compiler, regardless of how it was set. That means that it is |
| 770 | possible to use push and pop around GCC compatible diagnostics and Clang |
| 771 | will push and pop them appropriately, while GCC will ignore the pushes |
| 772 | and pops as unknown pragmas. It should be noted that while Clang |
| 773 | supports the GCC pragma, Clang and GCC do not support the exact same set |
| 774 | of warnings, so even when using GCC compatible #pragmas there is no |
| 775 | guarantee that they will have identical behaviour on both compilers. |
| 776 | |
Andy Gibbs | 9c2ccd6 | 2013-04-17 16:16:16 +0000 | [diff] [blame] | 777 | In addition to controlling warnings and errors generated by the compiler, it is |
| 778 | possible to generate custom warning and error messages through the following |
| 779 | pragmas: |
| 780 | |
| 781 | .. code-block:: c |
| 782 | |
| 783 | // The following will produce warning messages |
| 784 | #pragma message "some diagnostic message" |
| 785 | #pragma GCC warning "TODO: replace deprecated feature" |
| 786 | |
| 787 | // The following will produce an error message |
| 788 | #pragma GCC error "Not supported" |
| 789 | |
| 790 | These pragmas operate similarly to the ``#warning`` and ``#error`` preprocessor |
| 791 | directives, except that they may also be embedded into preprocessor macros via |
| 792 | the C99 ``_Pragma`` operator, for example: |
| 793 | |
| 794 | .. code-block:: c |
| 795 | |
| 796 | #define STR(X) #X |
| 797 | #define DEFER(M,...) M(__VA_ARGS__) |
| 798 | #define CUSTOM_ERROR(X) _Pragma(STR(GCC error(X " at line " DEFER(STR,__LINE__)))) |
| 799 | |
| 800 | CUSTOM_ERROR("Feature not available"); |
| 801 | |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 802 | Controlling Diagnostics in System Headers |
| 803 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 804 | |
| 805 | Warnings are suppressed when they occur in system headers. By default, |
| 806 | an included file is treated as a system header if it is found in an |
| 807 | include path specified by ``-isystem``, but this can be overridden in |
| 808 | several ways. |
| 809 | |
| 810 | The ``system_header`` pragma can be used to mark the current file as |
| 811 | being a system header. No warnings will be produced from the location of |
| 812 | the pragma onwards within the same file. |
| 813 | |
Dmitri Gribenko | 1436ff2 | 2012-12-19 22:06:59 +0000 | [diff] [blame] | 814 | .. code-block:: c |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 815 | |
George Burgess IV | bc8cc5ac | 2016-06-21 02:19:43 +0000 | [diff] [blame] | 816 | #if foo |
| 817 | #endif foo // warning: extra tokens at end of #endif directive |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 818 | |
Dmitri Gribenko | 1436ff2 | 2012-12-19 22:06:59 +0000 | [diff] [blame] | 819 | #pragma clang system_header |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 820 | |
George Burgess IV | bc8cc5ac | 2016-06-21 02:19:43 +0000 | [diff] [blame] | 821 | #if foo |
| 822 | #endif foo // no warning |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 823 | |
Aaron Ballman | 51fb031 | 2016-07-15 13:13:45 +0000 | [diff] [blame] | 824 | The `--system-header-prefix=` and `--no-system-header-prefix=` |
Alexander Kornienko | 18fa48c | 2014-03-26 01:39:59 +0000 | [diff] [blame] | 825 | command-line arguments can be used to override whether subsets of an include |
| 826 | path are treated as system headers. When the name in a ``#include`` directive |
| 827 | is found within a header search path and starts with a system prefix, the |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 828 | header is treated as a system header. The last prefix on the |
| 829 | command-line which matches the specified header name takes precedence. |
| 830 | For instance: |
| 831 | |
Dmitri Gribenko | 1436ff2 | 2012-12-19 22:06:59 +0000 | [diff] [blame] | 832 | .. code-block:: console |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 833 | |
Alexander Kornienko | 18fa48c | 2014-03-26 01:39:59 +0000 | [diff] [blame] | 834 | $ clang -Ifoo -isystem bar --system-header-prefix=x/ \ |
| 835 | --no-system-header-prefix=x/y/ |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 836 | |
| 837 | Here, ``#include "x/a.h"`` is treated as including a system header, even |
| 838 | if the header is found in ``foo``, and ``#include "x/y/b.h"`` is treated |
| 839 | as not including a system header, even if the header is found in |
| 840 | ``bar``. |
| 841 | |
| 842 | A ``#include`` directive which finds a file relative to the current |
| 843 | directory is treated as including a system header if the including file |
| 844 | is treated as a system header. |
| 845 | |
| 846 | .. _diagnostics_enable_everything: |
| 847 | |
Tobias Grosser | 7416024 | 2014-02-28 09:11:08 +0000 | [diff] [blame] | 848 | Enabling All Diagnostics |
| 849 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 850 | |
| 851 | In addition to the traditional ``-W`` flags, one can enable **all** |
Tobias Grosser | 7416024 | 2014-02-28 09:11:08 +0000 | [diff] [blame] | 852 | diagnostics by passing :option:`-Weverything`. This works as expected |
| 853 | with |
Dmitri Gribenko | 1436ff2 | 2012-12-19 22:06:59 +0000 | [diff] [blame] | 854 | :option:`-Werror`, and also includes the warnings from :option:`-pedantic`. |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 855 | |
Dmitri Gribenko | 1436ff2 | 2012-12-19 22:06:59 +0000 | [diff] [blame] | 856 | Note that when combined with :option:`-w` (which disables all warnings), that |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 857 | flag wins. |
| 858 | |
| 859 | Controlling Static Analyzer Diagnostics |
| 860 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 861 | |
| 862 | While not strictly part of the compiler, the diagnostics from Clang's |
| 863 | `static analyzer <http://clang-analyzer.llvm.org>`_ can also be |
| 864 | influenced by the user via changes to the source code. See the available |
| 865 | `annotations <http://clang-analyzer.llvm.org/annotations.html>`_ and the |
| 866 | analyzer's `FAQ |
| 867 | page <http://clang-analyzer.llvm.org/faq.html#exclude_code>`_ for more |
| 868 | information. |
| 869 | |
Dmitri Gribenko | 7ac0cc3 | 2012-12-15 21:10:51 +0000 | [diff] [blame] | 870 | .. _usersmanual-precompiled-headers: |
| 871 | |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 872 | Precompiled Headers |
| 873 | ------------------- |
| 874 | |
| 875 | `Precompiled headers <http://en.wikipedia.org/wiki/Precompiled_header>`__ |
| 876 | are a general approach employed by many compilers to reduce compilation |
| 877 | time. The underlying motivation of the approach is that it is common for |
| 878 | the same (and often large) header files to be included by multiple |
| 879 | source files. Consequently, compile times can often be greatly improved |
| 880 | by caching some of the (redundant) work done by a compiler to process |
| 881 | headers. Precompiled header files, which represent one of many ways to |
| 882 | implement this optimization, are literally files that represent an |
| 883 | on-disk cache that contains the vital information necessary to reduce |
| 884 | some of the work needed to process a corresponding header file. While |
| 885 | details of precompiled headers vary between compilers, precompiled |
| 886 | headers have been shown to be highly effective at speeding up program |
Nico Weber | ab88f0b | 2014-03-07 18:09:57 +0000 | [diff] [blame] | 887 | compilation on systems with very large system headers (e.g., Mac OS X). |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 888 | |
| 889 | Generating a PCH File |
| 890 | ^^^^^^^^^^^^^^^^^^^^^ |
| 891 | |
| 892 | To generate a PCH file using Clang, one invokes Clang with the |
Aaron Ballman | 51fb031 | 2016-07-15 13:13:45 +0000 | [diff] [blame] | 893 | `-x <language>-header` option. This mirrors the interface in GCC |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 894 | for generating PCH files: |
| 895 | |
Dmitri Gribenko | 1436ff2 | 2012-12-19 22:06:59 +0000 | [diff] [blame] | 896 | .. code-block:: console |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 897 | |
Dmitri Gribenko | 1436ff2 | 2012-12-19 22:06:59 +0000 | [diff] [blame] | 898 | $ gcc -x c-header test.h -o test.h.gch |
| 899 | $ clang -x c-header test.h -o test.h.pch |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 900 | |
| 901 | Using a PCH File |
| 902 | ^^^^^^^^^^^^^^^^ |
| 903 | |
Dmitri Gribenko | 1436ff2 | 2012-12-19 22:06:59 +0000 | [diff] [blame] | 904 | A PCH file can then be used as a prefix header when a :option:`-include` |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 905 | option is passed to ``clang``: |
| 906 | |
Dmitri Gribenko | 1436ff2 | 2012-12-19 22:06:59 +0000 | [diff] [blame] | 907 | .. code-block:: console |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 908 | |
Dmitri Gribenko | 1436ff2 | 2012-12-19 22:06:59 +0000 | [diff] [blame] | 909 | $ clang -include test.h test.c -o test |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 910 | |
| 911 | The ``clang`` driver will first check if a PCH file for ``test.h`` is |
| 912 | available; if so, the contents of ``test.h`` (and the files it includes) |
| 913 | will be processed from the PCH file. Otherwise, Clang falls back to |
| 914 | directly processing the content of ``test.h``. This mirrors the behavior |
| 915 | of GCC. |
| 916 | |
| 917 | .. note:: |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 918 | |
Dmitri Gribenko | 1436ff2 | 2012-12-19 22:06:59 +0000 | [diff] [blame] | 919 | Clang does *not* automatically use PCH files for headers that are directly |
| 920 | included within a source file. For example: |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 921 | |
Dmitri Gribenko | 1436ff2 | 2012-12-19 22:06:59 +0000 | [diff] [blame] | 922 | .. code-block:: console |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 923 | |
Dmitri Gribenko | 1436ff2 | 2012-12-19 22:06:59 +0000 | [diff] [blame] | 924 | $ clang -x c-header test.h -o test.h.pch |
| 925 | $ cat test.c |
| 926 | #include "test.h" |
| 927 | $ clang test.c -o test |
| 928 | |
| 929 | In this example, ``clang`` will not automatically use the PCH file for |
| 930 | ``test.h`` since ``test.h`` was included directly in the source file and not |
| 931 | specified on the command line using :option:`-include`. |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 932 | |
| 933 | Relocatable PCH Files |
| 934 | ^^^^^^^^^^^^^^^^^^^^^ |
| 935 | |
| 936 | It is sometimes necessary to build a precompiled header from headers |
| 937 | that are not yet in their final, installed locations. For example, one |
| 938 | might build a precompiled header within the build tree that is then |
| 939 | meant to be installed alongside the headers. Clang permits the creation |
| 940 | of "relocatable" precompiled headers, which are built with a given path |
| 941 | (into the build directory) and can later be used from an installed |
| 942 | location. |
| 943 | |
| 944 | To build a relocatable precompiled header, place your headers into a |
| 945 | subdirectory whose structure mimics the installed location. For example, |
| 946 | if you want to build a precompiled header for the header ``mylib.h`` |
| 947 | that will be installed into ``/usr/include``, create a subdirectory |
| 948 | ``build/usr/include`` and place the header ``mylib.h`` into that |
| 949 | subdirectory. If ``mylib.h`` depends on other headers, then they can be |
| 950 | stored within ``build/usr/include`` in a way that mimics the installed |
| 951 | location. |
| 952 | |
| 953 | Building a relocatable precompiled header requires two additional |
| 954 | arguments. First, pass the ``--relocatable-pch`` flag to indicate that |
| 955 | the resulting PCH file should be relocatable. Second, pass |
Aaron Ballman | 51fb031 | 2016-07-15 13:13:45 +0000 | [diff] [blame] | 956 | `-isysroot /path/to/build`, which makes all includes for your library |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 957 | relative to the build directory. For example: |
| 958 | |
Dmitri Gribenko | 1436ff2 | 2012-12-19 22:06:59 +0000 | [diff] [blame] | 959 | .. code-block:: console |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 960 | |
Dmitri Gribenko | 1436ff2 | 2012-12-19 22:06:59 +0000 | [diff] [blame] | 961 | # clang -x c-header --relocatable-pch -isysroot /path/to/build /path/to/build/mylib.h mylib.h.pch |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 962 | |
| 963 | When loading the relocatable PCH file, the various headers used in the |
| 964 | PCH file are found from the system header root. For example, ``mylib.h`` |
| 965 | can be found in ``/usr/include/mylib.h``. If the headers are installed |
Aaron Ballman | 51fb031 | 2016-07-15 13:13:45 +0000 | [diff] [blame] | 966 | in some other system root, the `-isysroot` option can be used provide |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 967 | a different system root from which the headers will be based. For |
Aaron Ballman | 51fb031 | 2016-07-15 13:13:45 +0000 | [diff] [blame] | 968 | example, `-isysroot /Developer/SDKs/MacOSX10.4u.sdk` will look for |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 969 | ``mylib.h`` in ``/Developer/SDKs/MacOSX10.4u.sdk/usr/include/mylib.h``. |
| 970 | |
| 971 | Relocatable precompiled headers are intended to be used in a limited |
| 972 | number of cases where the compilation environment is tightly controlled |
| 973 | and the precompiled header cannot be generated after headers have been |
Argyrios Kyrtzidis | f0ad09f | 2013-02-14 00:12:44 +0000 | [diff] [blame] | 974 | installed. |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 975 | |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 976 | .. _controlling-code-generation: |
| 977 | |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 978 | Controlling Code Generation |
| 979 | --------------------------- |
| 980 | |
| 981 | Clang provides a number of ways to control code generation. The options |
| 982 | are listed below. |
| 983 | |
Sean Silva | 4c280bd | 2013-06-21 23:50:58 +0000 | [diff] [blame] | 984 | **-f[no-]sanitize=check1,check2,...** |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 985 | Turn on runtime checks for various forms of undefined or suspicious |
| 986 | behavior. |
| 987 | |
| 988 | This option controls whether Clang adds runtime checks for various |
| 989 | forms of undefined or suspicious behavior, and is disabled by |
| 990 | default. If a check fails, a diagnostic message is produced at |
| 991 | runtime explaining the problem. The main checks are: |
| 992 | |
Richard Smith | bb741f4 | 2012-12-13 07:29:23 +0000 | [diff] [blame] | 993 | - .. _opt_fsanitize_address: |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 994 | |
Richard Smith | bb741f4 | 2012-12-13 07:29:23 +0000 | [diff] [blame] | 995 | ``-fsanitize=address``: |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 996 | :doc:`AddressSanitizer`, a memory error |
| 997 | detector. |
Richard Smith | bb741f4 | 2012-12-13 07:29:23 +0000 | [diff] [blame] | 998 | - .. _opt_fsanitize_thread: |
| 999 | |
Dmitry Vyukov | 42de108 | 2012-12-21 08:21:25 +0000 | [diff] [blame] | 1000 | ``-fsanitize=thread``: :doc:`ThreadSanitizer`, a data race detector. |
Evgeniy Stepanov | 17d5590 | 2012-12-21 10:50:00 +0000 | [diff] [blame] | 1001 | - .. _opt_fsanitize_memory: |
| 1002 | |
| 1003 | ``-fsanitize=memory``: :doc:`MemorySanitizer`, |
Alexey Samsonov | 1f7051e | 2015-12-04 22:50:44 +0000 | [diff] [blame] | 1004 | a detector of uninitialized reads. Requires instrumentation of all |
| 1005 | program code. |
Richard Smith | bb741f4 | 2012-12-13 07:29:23 +0000 | [diff] [blame] | 1006 | - .. _opt_fsanitize_undefined: |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 1007 | |
Alexey Samsonov | 778fc72 | 2015-12-04 17:30:29 +0000 | [diff] [blame] | 1008 | ``-fsanitize=undefined``: :doc:`UndefinedBehaviorSanitizer`, |
| 1009 | a fast and compatible undefined behavior checker. |
Peter Collingbourne | 9881b78 | 2015-06-18 23:59:22 +0000 | [diff] [blame] | 1010 | |
Peter Collingbourne | c377275 | 2013-08-07 22:47:34 +0000 | [diff] [blame] | 1011 | - ``-fsanitize=dataflow``: :doc:`DataFlowSanitizer`, a general data |
| 1012 | flow analysis. |
Peter Collingbourne | a4ccff3 | 2015-02-20 20:30:56 +0000 | [diff] [blame] | 1013 | - ``-fsanitize=cfi``: :doc:`control flow integrity <ControlFlowIntegrity>` |
Alexey Samsonov | 907880e | 2015-06-19 19:57:46 +0000 | [diff] [blame] | 1014 | checks. Requires ``-flto``. |
Peter Collingbourne | c4122c1 | 2015-06-15 21:08:13 +0000 | [diff] [blame] | 1015 | - ``-fsanitize=safe-stack``: :doc:`safe stack <SafeStack>` |
| 1016 | protection against stack-based memory corruption errors. |
Chad Rosier | ae229d5 | 2013-01-29 23:31:22 +0000 | [diff] [blame] | 1017 | |
Alexey Samsonov | 778fc72 | 2015-12-04 17:30:29 +0000 | [diff] [blame] | 1018 | There are more fine-grained checks available: see |
| 1019 | the :ref:`list <ubsan-checks>` of specific kinds of |
Alexey Samsonov | 9eda640 | 2015-12-04 21:30:58 +0000 | [diff] [blame] | 1020 | undefined behavior that can be detected and the :ref:`list <cfi-schemes>` |
| 1021 | of control flow integrity schemes. |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 1022 | |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 1023 | The ``-fsanitize=`` argument must also be provided when linking, in |
Alexey Samsonov | b6761c2 | 2015-12-04 23:13:14 +0000 | [diff] [blame] | 1024 | order to link to the appropriate runtime library. |
Richard Smith | 83c728b | 2013-07-19 19:06:48 +0000 | [diff] [blame] | 1025 | |
| 1026 | It is not possible to combine more than one of the ``-fsanitize=address``, |
| 1027 | ``-fsanitize=thread``, and ``-fsanitize=memory`` checkers in the same |
Alexey Samsonov | 8846017 | 2015-12-04 17:35:47 +0000 | [diff] [blame] | 1028 | program. |
Richard Smith | 83c728b | 2013-07-19 19:06:48 +0000 | [diff] [blame] | 1029 | |
Alexey Samsonov | 8845952 | 2015-01-12 22:39:12 +0000 | [diff] [blame] | 1030 | **-f[no-]sanitize-recover=check1,check2,...** |
Kostya Serebryany | 40b8215 | 2016-05-04 20:24:54 +0000 | [diff] [blame] | 1031 | |
Kostya Serebryany | ceb1add | 2016-05-04 20:21:47 +0000 | [diff] [blame] | 1032 | **-f[no-]sanitize-recover=all** |
Alexey Samsonov | 8845952 | 2015-01-12 22:39:12 +0000 | [diff] [blame] | 1033 | |
| 1034 | Controls which checks enabled by ``-fsanitize=`` flag are non-fatal. |
| 1035 | If the check is fatal, program will halt after the first error |
| 1036 | of this kind is detected and error report is printed. |
| 1037 | |
Alexey Samsonov | 778fc72 | 2015-12-04 17:30:29 +0000 | [diff] [blame] | 1038 | By default, non-fatal checks are those enabled by |
| 1039 | :doc:`UndefinedBehaviorSanitizer`, |
Alexey Samsonov | 8845952 | 2015-01-12 22:39:12 +0000 | [diff] [blame] | 1040 | except for ``-fsanitize=return`` and ``-fsanitize=unreachable``. Some |
Yury Gribov | 5bfeca1 | 2015-11-11 10:45:48 +0000 | [diff] [blame] | 1041 | sanitizers may not support recovery (or not support it by default |
| 1042 | e.g. :doc:`AddressSanitizer`), and always crash the program after the issue |
| 1043 | is detected. |
Alexey Samsonov | 8845952 | 2015-01-12 22:39:12 +0000 | [diff] [blame] | 1044 | |
Peter Collingbourne | 9881b78 | 2015-06-18 23:59:22 +0000 | [diff] [blame] | 1045 | Note that the ``-fsanitize-trap`` flag has precedence over this flag. |
| 1046 | This means that if a check has been configured to trap elsewhere on the |
| 1047 | command line, or if the check traps by default, this flag will not have |
| 1048 | any effect unless that sanitizer's trapping behavior is disabled with |
| 1049 | ``-fno-sanitize-trap``. |
| 1050 | |
| 1051 | For example, if a command line contains the flags ``-fsanitize=undefined |
| 1052 | -fsanitize-trap=undefined``, the flag ``-fsanitize-recover=alignment`` |
| 1053 | will have no effect on its own; it will need to be accompanied by |
| 1054 | ``-fno-sanitize-trap=alignment``. |
| 1055 | |
| 1056 | **-f[no-]sanitize-trap=check1,check2,...** |
| 1057 | |
| 1058 | Controls which checks enabled by the ``-fsanitize=`` flag trap. This |
| 1059 | option is intended for use in cases where the sanitizer runtime cannot |
| 1060 | be used (for instance, when building libc or a kernel module), or where |
| 1061 | the binary size increase caused by the sanitizer runtime is a concern. |
| 1062 | |
Alexey Samsonov | b6761c2 | 2015-12-04 23:13:14 +0000 | [diff] [blame] | 1063 | This flag is only compatible with :doc:`control flow integrity |
| 1064 | <ControlFlowIntegrity>` schemes and :doc:`UndefinedBehaviorSanitizer` |
| 1065 | checks other than ``vptr``. If this flag |
Peter Collingbourne | 6708c4a | 2015-06-19 01:51:54 +0000 | [diff] [blame] | 1066 | is supplied together with ``-fsanitize=undefined``, the ``vptr`` sanitizer |
| 1067 | will be implicitly disabled. |
| 1068 | |
| 1069 | This flag is enabled by default for sanitizers in the ``cfi`` group. |
Peter Collingbourne | 9881b78 | 2015-06-18 23:59:22 +0000 | [diff] [blame] | 1070 | |
Alexey Samsonov | b6761c2 | 2015-12-04 23:13:14 +0000 | [diff] [blame] | 1071 | .. option:: -fsanitize-blacklist=/path/to/blacklist/file |
| 1072 | |
| 1073 | Disable or modify sanitizer checks for objects (source files, functions, |
| 1074 | variables, types) listed in the file. See |
| 1075 | :doc:`SanitizerSpecialCaseList` for file format description. |
| 1076 | |
| 1077 | .. option:: -fno-sanitize-blacklist |
| 1078 | |
| 1079 | Don't use blacklist file, if it was specified earlier in the command line. |
| 1080 | |
Alexey Samsonov | 8fffba1 | 2015-05-07 23:04:19 +0000 | [diff] [blame] | 1081 | **-f[no-]sanitize-coverage=[type,features,...]** |
| 1082 | |
| 1083 | Enable simple code coverage in addition to certain sanitizers. |
| 1084 | See :doc:`SanitizerCoverage` for more details. |
| 1085 | |
Peter Collingbourne | dc13453 | 2016-01-16 00:31:22 +0000 | [diff] [blame] | 1086 | **-f[no-]sanitize-stats** |
| 1087 | |
| 1088 | Enable simple statistics gathering for the enabled sanitizers. |
| 1089 | See :doc:`SanitizerStats` for more details. |
| 1090 | |
Peter Collingbourne | 9881b78 | 2015-06-18 23:59:22 +0000 | [diff] [blame] | 1091 | .. option:: -fsanitize-undefined-trap-on-error |
| 1092 | |
| 1093 | Deprecated alias for ``-fsanitize-trap=undefined``. |
| 1094 | |
Evgeniy Stepanov | fd6f92d | 2015-12-15 23:00:20 +0000 | [diff] [blame] | 1095 | .. option:: -fsanitize-cfi-cross-dso |
| 1096 | |
| 1097 | Enable cross-DSO control flow integrity checks. This flag modifies |
| 1098 | the behavior of sanitizers in the ``cfi`` group to allow checking |
| 1099 | of cross-DSO virtual and indirect calls. |
| 1100 | |
Piotr Padlewski | eb9dd5a | 2017-01-16 13:20:08 +0000 | [diff] [blame] | 1101 | |
| 1102 | .. option:: -fstrict-vtable-pointers |
Hans Wennborg | f6d61d4 | 2017-01-17 21:31:57 +0000 | [diff] [blame] | 1103 | |
Piotr Padlewski | eb9dd5a | 2017-01-16 13:20:08 +0000 | [diff] [blame] | 1104 | Enable optimizations based on the strict rules for overwriting polymorphic |
| 1105 | C++ objects, i.e. the vptr is invariant during an object's lifetime. |
| 1106 | This enables better devirtualization. Turned off by default, because it is |
| 1107 | still experimental. |
| 1108 | |
Justin Lebar | 84da8b2 | 2016-05-20 21:33:01 +0000 | [diff] [blame] | 1109 | .. option:: -ffast-math |
| 1110 | |
| 1111 | Enable fast-math mode. This defines the ``__FAST_MATH__`` preprocessor |
| 1112 | macro, and lets the compiler make aggressive, potentially-lossy assumptions |
| 1113 | about floating-point math. These include: |
| 1114 | |
| 1115 | * Floating-point math obeys regular algebraic rules for real numbers (e.g. |
| 1116 | ``+`` and ``*`` are associative, ``x/y == x * (1/y)``, and |
| 1117 | ``(a + b) * c == a * c + b * c``), |
| 1118 | * operands to floating-point operations are not equal to ``NaN`` and |
| 1119 | ``Inf``, and |
| 1120 | * ``+0`` and ``-0`` are interchangeable. |
| 1121 | |
Sjoerd Meijer | 0a8d421 | 2016-08-30 08:09:45 +0000 | [diff] [blame] | 1122 | .. option:: -fdenormal-fp-math=[values] |
| 1123 | |
| 1124 | Select which denormal numbers the code is permitted to require. |
| 1125 | |
| 1126 | Valid values are: ``ieee``, ``preserve-sign``, and ``positive-zero``, |
| 1127 | which correspond to IEEE 754 denormal numbers, the sign of a |
| 1128 | flushed-to-zero number is preserved in the sign of 0, denormals are |
| 1129 | flushed to positive zero, respectively. |
| 1130 | |
Peter Collingbourne | fb532b9 | 2016-02-24 20:46:36 +0000 | [diff] [blame] | 1131 | .. option:: -fwhole-program-vtables |
| 1132 | |
| 1133 | Enable whole-program vtable optimizations, such as single-implementation |
Peter Collingbourne | 3afb266 | 2016-04-28 17:09:37 +0000 | [diff] [blame] | 1134 | devirtualization and virtual constant propagation, for classes with |
| 1135 | :doc:`hidden LTO visibility <LTOVisibility>`. Requires ``-flto``. |
Peter Collingbourne | fb532b9 | 2016-02-24 20:46:36 +0000 | [diff] [blame] | 1136 | |
Dmitri Gribenko | 1436ff2 | 2012-12-19 22:06:59 +0000 | [diff] [blame] | 1137 | .. option:: -fno-assume-sane-operator-new |
| 1138 | |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 1139 | Don't assume that the C++'s new operator is sane. |
| 1140 | |
| 1141 | This option tells the compiler to do not assume that C++'s global |
| 1142 | new operator will always return a pointer that does not alias any |
| 1143 | other pointer when the function returns. |
| 1144 | |
Dmitri Gribenko | 1436ff2 | 2012-12-19 22:06:59 +0000 | [diff] [blame] | 1145 | .. option:: -ftrap-function=[name] |
| 1146 | |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 1147 | Instruct code generator to emit a function call to the specified |
| 1148 | function name for ``__builtin_trap()``. |
| 1149 | |
| 1150 | LLVM code generator translates ``__builtin_trap()`` to a trap |
| 1151 | instruction if it is supported by the target ISA. Otherwise, the |
| 1152 | builtin is translated into a call to ``abort``. If this option is |
| 1153 | set, then the code generator will always lower the builtin to a call |
| 1154 | to the specified function regardless of whether the target ISA has a |
| 1155 | trap instruction. This option is useful for environments (e.g. |
| 1156 | deeply embedded) where a trap cannot be properly handled, or when |
| 1157 | some custom behavior is desired. |
| 1158 | |
Dmitri Gribenko | 1436ff2 | 2012-12-19 22:06:59 +0000 | [diff] [blame] | 1159 | .. option:: -ftls-model=[model] |
| 1160 | |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 1161 | Select which TLS model to use. |
| 1162 | |
| 1163 | Valid values are: ``global-dynamic``, ``local-dynamic``, |
| 1164 | ``initial-exec`` and ``local-exec``. The default value is |
| 1165 | ``global-dynamic``. The compiler may use a different model if the |
| 1166 | selected model is not supported by the target, or if a more |
| 1167 | efficient model can be used. The TLS model can be overridden per |
| 1168 | variable using the ``tls_model`` attribute. |
| 1169 | |
Chih-Hung Hsieh | 2c656c9 | 2015-07-28 16:27:56 +0000 | [diff] [blame] | 1170 | .. option:: -femulated-tls |
| 1171 | |
| 1172 | Select emulated TLS model, which overrides all -ftls-model choices. |
| 1173 | |
| 1174 | In emulated TLS mode, all access to TLS variables are converted to |
| 1175 | calls to __emutls_get_address in the runtime library. |
| 1176 | |
Silviu Baranga | f9671dd | 2013-10-21 10:54:53 +0000 | [diff] [blame] | 1177 | .. option:: -mhwdiv=[values] |
| 1178 | |
| 1179 | Select the ARM modes (arm or thumb) that support hardware division |
| 1180 | instructions. |
| 1181 | |
| 1182 | Valid values are: ``arm``, ``thumb`` and ``arm,thumb``. |
| 1183 | This option is used to indicate which mode (arm or thumb) supports |
| 1184 | hardware division instructions. This only applies to the ARM |
| 1185 | architecture. |
| 1186 | |
Bernard Ogden | 18b5701 | 2013-10-29 09:47:51 +0000 | [diff] [blame] | 1187 | .. option:: -m[no-]crc |
| 1188 | |
| 1189 | Enable or disable CRC instructions. |
| 1190 | |
| 1191 | This option is used to indicate whether CRC instructions are to |
| 1192 | be generated. This only applies to the ARM architecture. |
| 1193 | |
| 1194 | CRC instructions are enabled by default on ARMv8. |
| 1195 | |
Amara Emerson | 05d816d | 2014-01-24 15:15:27 +0000 | [diff] [blame] | 1196 | .. option:: -mgeneral-regs-only |
Amara Emerson | 04e2ecf | 2014-01-23 15:48:30 +0000 | [diff] [blame] | 1197 | |
| 1198 | Generate code which only uses the general purpose registers. |
| 1199 | |
| 1200 | This option restricts the generated code to use general registers |
| 1201 | only. This only applies to the AArch64 architecture. |
| 1202 | |
Simon Dardis | d0e83ba | 2016-05-27 15:13:31 +0000 | [diff] [blame] | 1203 | .. option:: -mcompact-branches=[values] |
| 1204 | |
| 1205 | Control the usage of compact branches for MIPSR6. |
| 1206 | |
| 1207 | Valid values are: ``never``, ``optimal`` and ``always``. |
| 1208 | The default value is ``optimal`` which generates compact branches |
| 1209 | when a delay slot cannot be filled. ``never`` disables the usage of |
| 1210 | compact branches and ``always`` generates compact branches whenever |
| 1211 | possible. |
| 1212 | |
Yunzhong Gao | eecc9e97 | 2015-12-10 01:37:18 +0000 | [diff] [blame] | 1213 | **-f[no-]max-type-align=[number]** |
Fariborz Jahanian | bcd82af | 2014-08-05 18:37:48 +0000 | [diff] [blame] | 1214 | Instruct the code generator to not enforce a higher alignment than the given |
| 1215 | number (of bytes) when accessing memory via an opaque pointer or reference. |
| 1216 | This cap is ignored when directly accessing a variable or when the pointee |
| 1217 | type has an explicit “aligned” attribute. |
| 1218 | |
| 1219 | The value should usually be determined by the properties of the system allocator. |
| 1220 | Some builtin types, especially vector types, have very high natural alignments; |
| 1221 | when working with values of those types, Clang usually wants to use instructions |
| 1222 | that take advantage of that alignment. However, many system allocators do |
| 1223 | not promise to return memory that is more than 8-byte or 16-byte-aligned. Use |
| 1224 | this option to limit the alignment that the compiler can assume for an arbitrary |
| 1225 | pointer, which may point onto the heap. |
| 1226 | |
| 1227 | This option does not affect the ABI alignment of types; the layout of structs and |
| 1228 | unions and the value returned by the alignof operator remain the same. |
| 1229 | |
| 1230 | This option can be overridden on a case-by-case basis by putting an explicit |
| 1231 | “aligned” alignment on a struct, union, or typedef. For example: |
| 1232 | |
| 1233 | .. code-block:: console |
| 1234 | |
| 1235 | #include <immintrin.h> |
| 1236 | // Make an aligned typedef of the AVX-512 16-int vector type. |
| 1237 | typedef __v16si __aligned_v16si __attribute__((aligned(64))); |
| 1238 | |
| 1239 | void initialize_vector(__aligned_v16si *v) { |
| 1240 | // The compiler may assume that ‘v’ is 64-byte aligned, regardless of the |
Yunzhong Gao | eecc9e97 | 2015-12-10 01:37:18 +0000 | [diff] [blame] | 1241 | // value of -fmax-type-align. |
Fariborz Jahanian | bcd82af | 2014-08-05 18:37:48 +0000 | [diff] [blame] | 1242 | } |
| 1243 | |
Silviu Baranga | f9671dd | 2013-10-21 10:54:53 +0000 | [diff] [blame] | 1244 | |
Bob Wilson | 3f2ed17 | 2014-06-17 00:45:30 +0000 | [diff] [blame] | 1245 | Profile Guided Optimization |
| 1246 | --------------------------- |
| 1247 | |
| 1248 | Profile information enables better optimization. For example, knowing that a |
| 1249 | branch is taken very frequently helps the compiler make better decisions when |
| 1250 | ordering basic blocks. Knowing that a function ``foo`` is called more |
| 1251 | frequently than another function ``bar`` helps the inliner. |
| 1252 | |
| 1253 | Clang supports profile guided optimization with two different kinds of |
| 1254 | profiling. A sampling profiler can generate a profile with very low runtime |
| 1255 | overhead, or you can build an instrumented version of the code that collects |
| 1256 | more detailed profile information. Both kinds of profiles can provide execution |
| 1257 | counts for instructions in the code and information on branches taken and |
| 1258 | function invocation. |
| 1259 | |
| 1260 | Regardless of which kind of profiling you use, be careful to collect profiles |
| 1261 | by running your code with inputs that are representative of the typical |
| 1262 | behavior. Code that is not exercised in the profile will be optimized as if it |
| 1263 | is unimportant, and the compiler may make poor optimization choices for code |
| 1264 | that is disproportionately used while profiling. |
| 1265 | |
Diego Novillo | 46ab35d | 2015-05-28 21:30:04 +0000 | [diff] [blame] | 1266 | Differences Between Sampling and Instrumentation |
| 1267 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 1268 | |
| 1269 | Although both techniques are used for similar purposes, there are important |
| 1270 | differences between the two: |
| 1271 | |
| 1272 | 1. Profile data generated with one cannot be used by the other, and there is no |
| 1273 | conversion tool that can convert one to the other. So, a profile generated |
| 1274 | via ``-fprofile-instr-generate`` must be used with ``-fprofile-instr-use``. |
| 1275 | Similarly, sampling profiles generated by external profilers must be |
| 1276 | converted and used with ``-fprofile-sample-use``. |
| 1277 | |
| 1278 | 2. Instrumentation profile data can be used for code coverage analysis and |
| 1279 | optimization. |
| 1280 | |
| 1281 | 3. Sampling profiles can only be used for optimization. They cannot be used for |
| 1282 | code coverage analysis. Although it would be technically possible to use |
| 1283 | sampling profiles for code coverage, sample-based profiles are too |
| 1284 | coarse-grained for code coverage purposes; it would yield poor results. |
| 1285 | |
| 1286 | 4. Sampling profiles must be generated by an external tool. The profile |
| 1287 | generated by that tool must then be converted into a format that can be read |
| 1288 | by LLVM. The section on sampling profilers describes one of the supported |
| 1289 | sampling profile formats. |
| 1290 | |
| 1291 | |
Bob Wilson | 3f2ed17 | 2014-06-17 00:45:30 +0000 | [diff] [blame] | 1292 | Using Sampling Profilers |
| 1293 | ^^^^^^^^^^^^^^^^^^^^^^^^ |
Diego Novillo | a5256bf | 2014-04-23 15:21:07 +0000 | [diff] [blame] | 1294 | |
| 1295 | Sampling profilers are used to collect runtime information, such as |
| 1296 | hardware counters, while your application executes. They are typically |
Diego Novillo | 8ebff32 | 2014-04-23 15:21:20 +0000 | [diff] [blame] | 1297 | very efficient and do not incur a large runtime overhead. The |
Diego Novillo | a5256bf | 2014-04-23 15:21:07 +0000 | [diff] [blame] | 1298 | sample data collected by the profiler can be used during compilation |
Diego Novillo | 8ebff32 | 2014-04-23 15:21:20 +0000 | [diff] [blame] | 1299 | to determine what the most executed areas of the code are. |
Diego Novillo | a5256bf | 2014-04-23 15:21:07 +0000 | [diff] [blame] | 1300 | |
Diego Novillo | a5256bf | 2014-04-23 15:21:07 +0000 | [diff] [blame] | 1301 | Using the data from a sample profiler requires some changes in the way |
| 1302 | a program is built. Before the compiler can use profiling information, |
| 1303 | the code needs to execute under the profiler. The following is the |
| 1304 | usual build cycle when using sample profilers for optimization: |
| 1305 | |
| 1306 | 1. Build the code with source line table information. You can use all the |
| 1307 | usual build flags that you always build your application with. The only |
Diego Novillo | 8ebff32 | 2014-04-23 15:21:20 +0000 | [diff] [blame] | 1308 | requirement is that you add ``-gline-tables-only`` or ``-g`` to the |
Diego Novillo | a5256bf | 2014-04-23 15:21:07 +0000 | [diff] [blame] | 1309 | command line. This is important for the profiler to be able to map |
| 1310 | instructions back to source line locations. |
| 1311 | |
| 1312 | .. code-block:: console |
| 1313 | |
| 1314 | $ clang++ -O2 -gline-tables-only code.cc -o code |
| 1315 | |
| 1316 | 2. Run the executable under a sampling profiler. The specific profiler |
| 1317 | you use does not really matter, as long as its output can be converted |
| 1318 | into the format that the LLVM optimizer understands. Currently, there |
| 1319 | exists a conversion tool for the Linux Perf profiler |
| 1320 | (https://perf.wiki.kernel.org/), so these examples assume that you |
| 1321 | are using Linux Perf to profile your code. |
| 1322 | |
| 1323 | .. code-block:: console |
| 1324 | |
| 1325 | $ perf record -b ./code |
| 1326 | |
| 1327 | Note the use of the ``-b`` flag. This tells Perf to use the Last Branch |
| 1328 | Record (LBR) to record call chains. While this is not strictly required, |
| 1329 | it provides better call information, which improves the accuracy of |
| 1330 | the profile data. |
| 1331 | |
| 1332 | 3. Convert the collected profile data to LLVM's sample profile format. |
| 1333 | This is currently supported via the AutoFDO converter ``create_llvm_prof``. |
| 1334 | It is available at http://github.com/google/autofdo. Once built and |
| 1335 | installed, you can convert the ``perf.data`` file to LLVM using |
| 1336 | the command: |
| 1337 | |
| 1338 | .. code-block:: console |
| 1339 | |
| 1340 | $ create_llvm_prof --binary=./code --out=code.prof |
| 1341 | |
Diego Novillo | 9e43084 | 2014-04-23 15:21:23 +0000 | [diff] [blame] | 1342 | This will read ``perf.data`` and the binary file ``./code`` and emit |
Diego Novillo | a5256bf | 2014-04-23 15:21:07 +0000 | [diff] [blame] | 1343 | the profile data in ``code.prof``. Note that if you ran ``perf`` |
| 1344 | without the ``-b`` flag, you need to use ``--use_lbr=false`` when |
| 1345 | calling ``create_llvm_prof``. |
| 1346 | |
| 1347 | 4. Build the code again using the collected profile. This step feeds |
| 1348 | the profile back to the optimizers. This should result in a binary |
Diego Novillo | 8ebff32 | 2014-04-23 15:21:20 +0000 | [diff] [blame] | 1349 | that executes faster than the original one. Note that you are not |
| 1350 | required to build the code with the exact same arguments that you |
| 1351 | used in the first step. The only requirement is that you build the code |
| 1352 | with ``-gline-tables-only`` and ``-fprofile-sample-use``. |
Diego Novillo | a5256bf | 2014-04-23 15:21:07 +0000 | [diff] [blame] | 1353 | |
| 1354 | .. code-block:: console |
| 1355 | |
| 1356 | $ clang++ -O2 -gline-tables-only -fprofile-sample-use=code.prof code.cc -o code |
| 1357 | |
| 1358 | |
Diego Novillo | 46ab35d | 2015-05-28 21:30:04 +0000 | [diff] [blame] | 1359 | Sample Profile Formats |
| 1360 | """""""""""""""""""""" |
Diego Novillo | a5256bf | 2014-04-23 15:21:07 +0000 | [diff] [blame] | 1361 | |
Diego Novillo | 46ab35d | 2015-05-28 21:30:04 +0000 | [diff] [blame] | 1362 | Since external profilers generate profile data in a variety of custom formats, |
| 1363 | the data generated by the profiler must be converted into a format that can be |
| 1364 | read by the backend. LLVM supports three different sample profile formats: |
Diego Novillo | a5256bf | 2014-04-23 15:21:07 +0000 | [diff] [blame] | 1365 | |
Diego Novillo | 46ab35d | 2015-05-28 21:30:04 +0000 | [diff] [blame] | 1366 | 1. ASCII text. This is the easiest one to generate. The file is divided into |
| 1367 | sections, which correspond to each of the functions with profile |
Diego Novillo | 3345276 | 2015-10-14 18:37:39 +0000 | [diff] [blame] | 1368 | information. The format is described below. It can also be generated from |
| 1369 | the binary or gcov formats using the ``llvm-profdata`` tool. |
Diego Novillo | e0d289e | 2015-05-22 16:05:07 +0000 | [diff] [blame] | 1370 | |
Diego Novillo | 46ab35d | 2015-05-28 21:30:04 +0000 | [diff] [blame] | 1371 | 2. Binary encoding. This uses a more efficient encoding that yields smaller |
Diego Novillo | 3345276 | 2015-10-14 18:37:39 +0000 | [diff] [blame] | 1372 | profile files. This is the format generated by the ``create_llvm_prof`` tool |
| 1373 | in http://github.com/google/autofdo. |
Diego Novillo | 46ab35d | 2015-05-28 21:30:04 +0000 | [diff] [blame] | 1374 | |
| 1375 | 3. GCC encoding. This is based on the gcov format, which is accepted by GCC. It |
Diego Novillo | 3345276 | 2015-10-14 18:37:39 +0000 | [diff] [blame] | 1376 | is only interesting in environments where GCC and Clang co-exist. This |
| 1377 | encoding is only generated by the ``create_gcov`` tool in |
| 1378 | http://github.com/google/autofdo. It can be read by LLVM and |
| 1379 | ``llvm-profdata``, but it cannot be generated by either. |
Diego Novillo | 46ab35d | 2015-05-28 21:30:04 +0000 | [diff] [blame] | 1380 | |
| 1381 | If you are using Linux Perf to generate sampling profiles, you can use the |
| 1382 | conversion tool ``create_llvm_prof`` described in the previous section. |
| 1383 | Otherwise, you will need to write a conversion tool that converts your |
| 1384 | profiler's native format into one of these three. |
| 1385 | |
| 1386 | |
| 1387 | Sample Profile Text Format |
| 1388 | """""""""""""""""""""""""" |
| 1389 | |
| 1390 | This section describes the ASCII text format for sampling profiles. It is, |
| 1391 | arguably, the easiest one to generate. If you are interested in generating any |
| 1392 | of the other two, consult the ``ProfileData`` library in in LLVM's source tree |
Diego Novillo | 843dc6f | 2015-10-19 15:53:17 +0000 | [diff] [blame] | 1393 | (specifically, ``include/llvm/ProfileData/SampleProfReader.h``). |
Diego Novillo | a5256bf | 2014-04-23 15:21:07 +0000 | [diff] [blame] | 1394 | |
| 1395 | .. code-block:: console |
| 1396 | |
| 1397 | function1:total_samples:total_head_samples |
Diego Novillo | 3345276 | 2015-10-14 18:37:39 +0000 | [diff] [blame] | 1398 | offset1[.discriminator]: number_of_samples [fn1:num fn2:num ... ] |
| 1399 | offset2[.discriminator]: number_of_samples [fn3:num fn4:num ... ] |
| 1400 | ... |
| 1401 | offsetN[.discriminator]: number_of_samples [fn5:num fn6:num ... ] |
| 1402 | offsetA[.discriminator]: fnA:num_of_total_samples |
| 1403 | offsetA1[.discriminator]: number_of_samples [fn7:num fn8:num ... ] |
| 1404 | offsetA1[.discriminator]: number_of_samples [fn9:num fn10:num ... ] |
| 1405 | offsetB[.discriminator]: fnB:num_of_total_samples |
| 1406 | offsetB1[.discriminator]: number_of_samples [fn11:num fn12:num ... ] |
Diego Novillo | a5256bf | 2014-04-23 15:21:07 +0000 | [diff] [blame] | 1407 | |
Diego Novillo | 3345276 | 2015-10-14 18:37:39 +0000 | [diff] [blame] | 1408 | This is a nested tree in which the identation represents the nesting level |
| 1409 | of the inline stack. There are no blank lines in the file. And the spacing |
| 1410 | within a single line is fixed. Additional spaces will result in an error |
| 1411 | while reading the file. |
| 1412 | |
| 1413 | Any line starting with the '#' character is completely ignored. |
| 1414 | |
| 1415 | Inlined calls are represented with indentation. The Inline stack is a |
| 1416 | stack of source locations in which the top of the stack represents the |
| 1417 | leaf function, and the bottom of the stack represents the actual |
| 1418 | symbol to which the instruction belongs. |
Diego Novillo | 8ebff32 | 2014-04-23 15:21:20 +0000 | [diff] [blame] | 1419 | |
Diego Novillo | a5256bf | 2014-04-23 15:21:07 +0000 | [diff] [blame] | 1420 | Function names must be mangled in order for the profile loader to |
| 1421 | match them in the current translation unit. The two numbers in the |
| 1422 | function header specify how many total samples were accumulated in the |
| 1423 | function (first number), and the total number of samples accumulated |
Diego Novillo | 8ebff32 | 2014-04-23 15:21:20 +0000 | [diff] [blame] | 1424 | in the prologue of the function (second number). This head sample |
| 1425 | count provides an indicator of how frequently the function is invoked. |
Diego Novillo | a5256bf | 2014-04-23 15:21:07 +0000 | [diff] [blame] | 1426 | |
Diego Novillo | 3345276 | 2015-10-14 18:37:39 +0000 | [diff] [blame] | 1427 | There are two types of lines in the function body. |
| 1428 | |
| 1429 | - Sampled line represents the profile information of a source location. |
| 1430 | ``offsetN[.discriminator]: number_of_samples [fn5:num fn6:num ... ]`` |
| 1431 | |
| 1432 | - Callsite line represents the profile information of an inlined callsite. |
| 1433 | ``offsetA[.discriminator]: fnA:num_of_total_samples`` |
| 1434 | |
Diego Novillo | a5256bf | 2014-04-23 15:21:07 +0000 | [diff] [blame] | 1435 | Each sampled line may contain several items. Some are optional (marked |
| 1436 | below): |
| 1437 | |
| 1438 | a. Source line offset. This number represents the line number |
| 1439 | in the function where the sample was collected. The line number is |
| 1440 | always relative to the line where symbol of the function is |
| 1441 | defined. So, if the function has its header at line 280, the offset |
| 1442 | 13 is at line 293 in the file. |
| 1443 | |
Diego Novillo | 897c59c | 2014-04-23 15:21:21 +0000 | [diff] [blame] | 1444 | Note that this offset should never be a negative number. This could |
| 1445 | happen in cases like macros. The debug machinery will register the |
| 1446 | line number at the point of macro expansion. So, if the macro was |
| 1447 | expanded in a line before the start of the function, the profile |
| 1448 | converter should emit a 0 as the offset (this means that the optimizers |
| 1449 | will not be able to associate a meaningful weight to the instructions |
| 1450 | in the macro). |
| 1451 | |
Diego Novillo | a5256bf | 2014-04-23 15:21:07 +0000 | [diff] [blame] | 1452 | b. [OPTIONAL] Discriminator. This is used if the sampled program |
| 1453 | was compiled with DWARF discriminator support |
Diego Novillo | 8ebff32 | 2014-04-23 15:21:20 +0000 | [diff] [blame] | 1454 | (http://wiki.dwarfstd.org/index.php?title=Path_Discriminators). |
Diego Novillo | 897c59c | 2014-04-23 15:21:21 +0000 | [diff] [blame] | 1455 | DWARF discriminators are unsigned integer values that allow the |
| 1456 | compiler to distinguish between multiple execution paths on the |
| 1457 | same source line location. |
Diego Novillo | a5256bf | 2014-04-23 15:21:07 +0000 | [diff] [blame] | 1458 | |
Diego Novillo | 8ebff32 | 2014-04-23 15:21:20 +0000 | [diff] [blame] | 1459 | For example, consider the line of code ``if (cond) foo(); else bar();``. |
| 1460 | If the predicate ``cond`` is true 80% of the time, then the edge |
| 1461 | into function ``foo`` should be considered to be taken most of the |
| 1462 | time. But both calls to ``foo`` and ``bar`` are at the same source |
| 1463 | line, so a sample count at that line is not sufficient. The |
| 1464 | compiler needs to know which part of that line is taken more |
| 1465 | frequently. |
| 1466 | |
| 1467 | This is what discriminators provide. In this case, the calls to |
| 1468 | ``foo`` and ``bar`` will be at the same line, but will have |
| 1469 | different discriminator values. This allows the compiler to correctly |
| 1470 | set edge weights into ``foo`` and ``bar``. |
| 1471 | |
| 1472 | c. Number of samples. This is an integer quantity representing the |
| 1473 | number of samples collected by the profiler at this source |
| 1474 | location. |
Diego Novillo | a5256bf | 2014-04-23 15:21:07 +0000 | [diff] [blame] | 1475 | |
| 1476 | d. [OPTIONAL] Potential call targets and samples. If present, this |
| 1477 | line contains a call instruction. This models both direct and |
Diego Novillo | a5256bf | 2014-04-23 15:21:07 +0000 | [diff] [blame] | 1478 | number of samples. For example, |
| 1479 | |
| 1480 | .. code-block:: console |
| 1481 | |
| 1482 | 130: 7 foo:3 bar:2 baz:7 |
| 1483 | |
| 1484 | The above means that at relative line offset 130 there is a call |
Diego Novillo | 8ebff32 | 2014-04-23 15:21:20 +0000 | [diff] [blame] | 1485 | instruction that calls one of ``foo()``, ``bar()`` and ``baz()``, |
| 1486 | with ``baz()`` being the relatively more frequently called target. |
Diego Novillo | a5256bf | 2014-04-23 15:21:07 +0000 | [diff] [blame] | 1487 | |
Diego Novillo | 3345276 | 2015-10-14 18:37:39 +0000 | [diff] [blame] | 1488 | As an example, consider a program with the call chain ``main -> foo -> bar``. |
| 1489 | When built with optimizations enabled, the compiler may inline the |
| 1490 | calls to ``bar`` and ``foo`` inside ``main``. The generated profile |
| 1491 | could then be something like this: |
| 1492 | |
| 1493 | .. code-block:: console |
| 1494 | |
| 1495 | main:35504:0 |
| 1496 | 1: _Z3foov:35504 |
| 1497 | 2: _Z32bari:31977 |
| 1498 | 1.1: 31977 |
| 1499 | 2: 0 |
| 1500 | |
| 1501 | This profile indicates that there were a total of 35,504 samples |
| 1502 | collected in main. All of those were at line 1 (the call to ``foo``). |
| 1503 | Of those, 31,977 were spent inside the body of ``bar``. The last line |
| 1504 | of the profile (``2: 0``) corresponds to line 2 inside ``main``. No |
| 1505 | samples were collected there. |
Diego Novillo | a5256bf | 2014-04-23 15:21:07 +0000 | [diff] [blame] | 1506 | |
Bob Wilson | 3f2ed17 | 2014-06-17 00:45:30 +0000 | [diff] [blame] | 1507 | Profiling with Instrumentation |
| 1508 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 1509 | |
| 1510 | Clang also supports profiling via instrumentation. This requires building a |
| 1511 | special instrumented version of the code and has some runtime |
| 1512 | overhead during the profiling, but it provides more detailed results than a |
| 1513 | sampling profiler. It also provides reproducible results, at least to the |
| 1514 | extent that the code behaves consistently across runs. |
| 1515 | |
| 1516 | Here are the steps for using profile guided optimization with |
| 1517 | instrumentation: |
| 1518 | |
| 1519 | 1. Build an instrumented version of the code by compiling and linking with the |
| 1520 | ``-fprofile-instr-generate`` option. |
| 1521 | |
| 1522 | .. code-block:: console |
| 1523 | |
| 1524 | $ clang++ -O2 -fprofile-instr-generate code.cc -o code |
| 1525 | |
| 1526 | 2. Run the instrumented executable with inputs that reflect the typical usage. |
| 1527 | By default, the profile data will be written to a ``default.profraw`` file |
Xinliang David Li | 7cd5e38 | 2016-07-20 23:32:50 +0000 | [diff] [blame] | 1528 | in the current directory. You can override that default by using option |
| 1529 | ``-fprofile-instr-generate=`` or by setting the ``LLVM_PROFILE_FILE`` |
| 1530 | environment variable to specify an alternate file. If non-default file name |
| 1531 | is specified by both the environment variable and the command line option, |
| 1532 | the environment variable takes precedence. The file name pattern specified |
| 1533 | can include different modifiers: ``%p``, ``%h``, and ``%m``. |
| 1534 | |
Bob Wilson | 3f2ed17 | 2014-06-17 00:45:30 +0000 | [diff] [blame] | 1535 | Any instance of ``%p`` in that file name will be replaced by the process |
| 1536 | ID, so that you can easily distinguish the profile output from multiple |
| 1537 | runs. |
| 1538 | |
| 1539 | .. code-block:: console |
| 1540 | |
| 1541 | $ LLVM_PROFILE_FILE="code-%p.profraw" ./code |
| 1542 | |
Xinliang David Li | 7cd5e38 | 2016-07-20 23:32:50 +0000 | [diff] [blame] | 1543 | The modifier ``%h`` can be used in scenarios where the same instrumented |
| 1544 | binary is run in multiple different host machines dumping profile data |
| 1545 | to a shared network based storage. The ``%h`` specifier will be substituted |
| 1546 | with the hostname so that profiles collected from different hosts do not |
| 1547 | clobber each other. |
| 1548 | |
| 1549 | While the use of ``%p`` specifier can reduce the likelihood for the profiles |
| 1550 | dumped from different processes to clobber each other, such clobbering can still |
| 1551 | happen because of the ``pid`` re-use by the OS. Another side-effect of using |
| 1552 | ``%p`` is that the storage requirement for raw profile data files is greatly |
| 1553 | increased. To avoid issues like this, the ``%m`` specifier can used in the profile |
| 1554 | name. When this specifier is used, the profiler runtime will substitute ``%m`` |
| 1555 | with a unique integer identifier associated with the instrumented binary. Additionally, |
| 1556 | multiple raw profiles dumped from different processes that share a file system (can be |
| 1557 | on different hosts) will be automatically merged by the profiler runtime during the |
| 1558 | dumping. If the program links in multiple instrumented shared libraries, each library |
| 1559 | will dump the profile data into its own profile data file (with its unique integer |
| 1560 | id embedded in the profile name). Note that the merging enabled by ``%m`` is for raw |
| 1561 | profile data generated by profiler runtime. The resulting merged "raw" profile data |
| 1562 | file still needs to be converted to a different format expected by the compiler ( |
| 1563 | see step 3 below). |
| 1564 | |
| 1565 | .. code-block:: console |
| 1566 | |
| 1567 | $ LLVM_PROFILE_FILE="code-%m.profraw" ./code |
| 1568 | |
| 1569 | |
Bob Wilson | 3f2ed17 | 2014-06-17 00:45:30 +0000 | [diff] [blame] | 1570 | 3. Combine profiles from multiple runs and convert the "raw" profile format to |
Diego Novillo | 46ab35d | 2015-05-28 21:30:04 +0000 | [diff] [blame] | 1571 | the input expected by clang. Use the ``merge`` command of the |
| 1572 | ``llvm-profdata`` tool to do this. |
Bob Wilson | 3f2ed17 | 2014-06-17 00:45:30 +0000 | [diff] [blame] | 1573 | |
| 1574 | .. code-block:: console |
| 1575 | |
| 1576 | $ llvm-profdata merge -output=code.profdata code-*.profraw |
| 1577 | |
| 1578 | Note that this step is necessary even when there is only one "raw" profile, |
| 1579 | since the merge operation also changes the file format. |
| 1580 | |
| 1581 | 4. Build the code again using the ``-fprofile-instr-use`` option to specify the |
| 1582 | collected profile data. |
| 1583 | |
| 1584 | .. code-block:: console |
| 1585 | |
| 1586 | $ clang++ -O2 -fprofile-instr-use=code.profdata code.cc -o code |
| 1587 | |
| 1588 | You can repeat step 4 as often as you like without regenerating the |
| 1589 | profile. As you make changes to your code, clang may no longer be able to |
| 1590 | use the profile data. It will warn you when this happens. |
| 1591 | |
Sean Silva | a834ff2 | 2016-07-16 02:54:58 +0000 | [diff] [blame] | 1592 | Profile generation using an alternative instrumentation method can be |
| 1593 | controlled by the GCC-compatible flags ``-fprofile-generate`` and |
| 1594 | ``-fprofile-use``. Although these flags are semantically equivalent to |
| 1595 | their GCC counterparts, they *do not* handle GCC-compatible profiles. |
| 1596 | They are only meant to implement GCC's semantics with respect to |
| 1597 | profile creation and use. |
Diego Novillo | 578caf5 | 2015-07-09 17:23:53 +0000 | [diff] [blame] | 1598 | |
| 1599 | .. option:: -fprofile-generate[=<dirname>] |
| 1600 | |
Sean Silva | a834ff2 | 2016-07-16 02:54:58 +0000 | [diff] [blame] | 1601 | The ``-fprofile-generate`` and ``-fprofile-generate=`` flags will use |
| 1602 | an alterantive instrumentation method for profile generation. When |
| 1603 | given a directory name, it generates the profile file |
Xinliang David Li | b7b335a | 2016-07-22 22:25:01 +0000 | [diff] [blame] | 1604 | ``default_%m.profraw`` in the directory named ``dirname`` if specified. |
| 1605 | If ``dirname`` does not exist, it will be created at runtime. ``%m`` specifier |
| 1606 | will be substibuted with a unique id documented in step 2 above. In other words, |
| 1607 | with ``-fprofile-generate[=<dirname>]`` option, the "raw" profile data automatic |
| 1608 | merging is turned on by default, so there will no longer any risk of profile |
| 1609 | clobbering from different running processes. For example, |
Diego Novillo | 578caf5 | 2015-07-09 17:23:53 +0000 | [diff] [blame] | 1610 | |
| 1611 | .. code-block:: console |
| 1612 | |
| 1613 | $ clang++ -O2 -fprofile-generate=yyy/zzz code.cc -o code |
| 1614 | |
| 1615 | When ``code`` is executed, the profile will be written to the file |
Xinliang David Li | b7b335a | 2016-07-22 22:25:01 +0000 | [diff] [blame] | 1616 | ``yyy/zzz/default_xxxx.profraw``. |
Diego Novillo | 578caf5 | 2015-07-09 17:23:53 +0000 | [diff] [blame] | 1617 | |
Xinliang David Li | b7b335a | 2016-07-22 22:25:01 +0000 | [diff] [blame] | 1618 | To generate the profile data file with the compiler readable format, the |
| 1619 | ``llvm-profdata`` tool can be used with the profile directory as the input: |
Diego Novillo | 578caf5 | 2015-07-09 17:23:53 +0000 | [diff] [blame] | 1620 | |
Xinliang David Li | b7b335a | 2016-07-22 22:25:01 +0000 | [diff] [blame] | 1621 | .. code-block:: console |
Diego Novillo | 578caf5 | 2015-07-09 17:23:53 +0000 | [diff] [blame] | 1622 | |
Xinliang David Li | b7b335a | 2016-07-22 22:25:01 +0000 | [diff] [blame] | 1623 | $ llvm-profdata merge -output=code.profdata yyy/zzz/ |
| 1624 | |
| 1625 | If the user wants to turn off the auto-merging feature, or simply override the |
| 1626 | the profile dumping path specified at command line, the environment variable |
| 1627 | ``LLVM_PROFILE_FILE`` can still be used to override |
| 1628 | the directory and filename for the profile file at runtime. |
Diego Novillo | 578caf5 | 2015-07-09 17:23:53 +0000 | [diff] [blame] | 1629 | |
| 1630 | .. option:: -fprofile-use[=<pathname>] |
| 1631 | |
| 1632 | Without any other arguments, ``-fprofile-use`` behaves identically to |
| 1633 | ``-fprofile-instr-use``. Otherwise, if ``pathname`` is the full path to a |
| 1634 | profile file, it reads from that file. If ``pathname`` is a directory name, |
| 1635 | it reads from ``pathname/default.profdata``. |
| 1636 | |
Diego Novillo | 758f3f5 | 2015-08-05 21:49:51 +0000 | [diff] [blame] | 1637 | Disabling Instrumentation |
| 1638 | ^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 1639 | |
| 1640 | In certain situations, it may be useful to disable profile generation or use |
| 1641 | for specific files in a build, without affecting the main compilation flags |
| 1642 | used for the other files in the project. |
| 1643 | |
| 1644 | In these cases, you can use the flag ``-fno-profile-instr-generate`` (or |
| 1645 | ``-fno-profile-generate``) to disable profile generation, and |
| 1646 | ``-fno-profile-instr-use`` (or ``-fno-profile-use``) to disable profile use. |
| 1647 | |
| 1648 | Note that these flags should appear after the corresponding profile |
| 1649 | flags to have an effect. |
Bob Wilson | 3f2ed17 | 2014-06-17 00:45:30 +0000 | [diff] [blame] | 1650 | |
Paul Robinson | 0334a04 | 2015-12-19 19:41:48 +0000 | [diff] [blame] | 1651 | Controlling Debug Information |
| 1652 | ----------------------------- |
| 1653 | |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 1654 | Controlling Size of Debug Information |
Paul Robinson | 0334a04 | 2015-12-19 19:41:48 +0000 | [diff] [blame] | 1655 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 1656 | |
| 1657 | Debug info kind generated by Clang can be set by one of the flags listed |
| 1658 | below. If multiple flags are present, the last one is used. |
| 1659 | |
Dmitri Gribenko | 1436ff2 | 2012-12-19 22:06:59 +0000 | [diff] [blame] | 1660 | .. option:: -g0 |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 1661 | |
Dmitri Gribenko | 1436ff2 | 2012-12-19 22:06:59 +0000 | [diff] [blame] | 1662 | Don't generate any debug info (default). |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 1663 | |
Dmitri Gribenko | 1436ff2 | 2012-12-19 22:06:59 +0000 | [diff] [blame] | 1664 | .. option:: -gline-tables-only |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 1665 | |
Dmitri Gribenko | 1436ff2 | 2012-12-19 22:06:59 +0000 | [diff] [blame] | 1666 | Generate line number tables only. |
| 1667 | |
| 1668 | This kind of debug info allows to obtain stack traces with function names, |
| 1669 | file names and line numbers (by such tools as ``gdb`` or ``addr2line``). It |
| 1670 | doesn't contain any other data (e.g. description of local variables or |
| 1671 | function parameters). |
| 1672 | |
Adrian Prantl | 4ad03dc | 2014-06-13 23:35:54 +0000 | [diff] [blame] | 1673 | .. option:: -fstandalone-debug |
Adrian Prantl | 36b8067 | 2014-06-13 21:12:31 +0000 | [diff] [blame] | 1674 | |
| 1675 | Clang supports a number of optimizations to reduce the size of debug |
| 1676 | information in the binary. They work based on the assumption that |
| 1677 | the debug type information can be spread out over multiple |
| 1678 | compilation units. For instance, Clang will not emit type |
| 1679 | definitions for types that are not needed by a module and could be |
| 1680 | replaced with a forward declaration. Further, Clang will only emit |
| 1681 | type info for a dynamic C++ class in the module that contains the |
| 1682 | vtable for the class. |
| 1683 | |
Adrian Prantl | 4ad03dc | 2014-06-13 23:35:54 +0000 | [diff] [blame] | 1684 | The **-fstandalone-debug** option turns off these optimizations. |
Adrian Prantl | 36b8067 | 2014-06-13 21:12:31 +0000 | [diff] [blame] | 1685 | This is useful when working with 3rd-party libraries that don't come |
| 1686 | with debug information. Note that Clang will never emit type |
| 1687 | information for types that are not referenced at all by the program. |
| 1688 | |
Adrian Prantl | 4ad03dc | 2014-06-13 23:35:54 +0000 | [diff] [blame] | 1689 | .. option:: -fno-standalone-debug |
| 1690 | |
| 1691 | On Darwin **-fstandalone-debug** is enabled by default. The |
| 1692 | **-fno-standalone-debug** option can be used to get to turn on the |
| 1693 | vtable-based optimization described above. |
| 1694 | |
Dmitri Gribenko | 1436ff2 | 2012-12-19 22:06:59 +0000 | [diff] [blame] | 1695 | .. option:: -g |
| 1696 | |
| 1697 | Generate complete debug info. |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 1698 | |
Amjad Aboud | 546bc11 | 2017-02-09 22:07:24 +0000 | [diff] [blame] | 1699 | Controlling Macro Debug Info Generation |
| 1700 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 1701 | |
| 1702 | Debug info for C preprocessor macros increases the size of debug information in |
| 1703 | the binary. Macro debug info generated by Clang can be controlled by the flags |
| 1704 | listed below. |
| 1705 | |
| 1706 | .. option:: -fdebug-macro |
| 1707 | |
| 1708 | Generate debug info for preprocessor macros. This flag is discarded when |
| 1709 | **-g0** is enabled. |
| 1710 | |
| 1711 | .. option:: -fno-debug-macro |
| 1712 | |
| 1713 | Do not generate debug info for preprocessor macros (default). |
| 1714 | |
Paul Robinson | 0334a04 | 2015-12-19 19:41:48 +0000 | [diff] [blame] | 1715 | Controlling Debugger "Tuning" |
| 1716 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 1717 | |
| 1718 | While Clang generally emits standard DWARF debug info (http://dwarfstd.org), |
| 1719 | different debuggers may know how to take advantage of different specific DWARF |
| 1720 | features. You can "tune" the debug info for one of several different debuggers. |
| 1721 | |
| 1722 | .. option:: -ggdb, -glldb, -gsce |
| 1723 | |
Paul Robinson | 8ce9b44 | 2016-08-15 18:45:52 +0000 | [diff] [blame] | 1724 | Tune the debug info for the ``gdb``, ``lldb``, or Sony PlayStation\ |reg| |
Paul Robinson | 0334a04 | 2015-12-19 19:41:48 +0000 | [diff] [blame] | 1725 | debugger, respectively. Each of these options implies **-g**. (Therefore, if |
| 1726 | you want both **-gline-tables-only** and debugger tuning, the tuning option |
| 1727 | must come first.) |
| 1728 | |
| 1729 | |
Dmitri Gribenko | a7d16ce | 2013-04-10 15:35:17 +0000 | [diff] [blame] | 1730 | Comment Parsing Options |
Dmitri Gribenko | 28bfb48 | 2014-03-06 16:32:09 +0000 | [diff] [blame] | 1731 | ----------------------- |
Dmitri Gribenko | a7d16ce | 2013-04-10 15:35:17 +0000 | [diff] [blame] | 1732 | |
| 1733 | Clang parses Doxygen and non-Doxygen style documentation comments and attaches |
| 1734 | them to the appropriate declaration nodes. By default, it only parses |
| 1735 | Doxygen-style comments and ignores ordinary comments starting with ``//`` and |
| 1736 | ``/*``. |
| 1737 | |
Dmitri Gribenko | 28bfb48 | 2014-03-06 16:32:09 +0000 | [diff] [blame] | 1738 | .. option:: -Wdocumentation |
| 1739 | |
| 1740 | Emit warnings about use of documentation comments. This warning group is off |
| 1741 | by default. |
| 1742 | |
| 1743 | This includes checking that ``\param`` commands name parameters that actually |
| 1744 | present in the function signature, checking that ``\returns`` is used only on |
| 1745 | functions that actually return a value etc. |
| 1746 | |
| 1747 | .. option:: -Wno-documentation-unknown-command |
| 1748 | |
| 1749 | Don't warn when encountering an unknown Doxygen command. |
| 1750 | |
Dmitri Gribenko | a7d16ce | 2013-04-10 15:35:17 +0000 | [diff] [blame] | 1751 | .. option:: -fparse-all-comments |
| 1752 | |
| 1753 | Parse all comments as documentation comments (including ordinary comments |
| 1754 | starting with ``//`` and ``/*``). |
| 1755 | |
Dmitri Gribenko | 28bfb48 | 2014-03-06 16:32:09 +0000 | [diff] [blame] | 1756 | .. option:: -fcomment-block-commands=[commands] |
| 1757 | |
| 1758 | Define custom documentation commands as block commands. This allows Clang to |
| 1759 | construct the correct AST for these custom commands, and silences warnings |
| 1760 | about unknown commands. Several commands must be separated by a comma |
| 1761 | *without trailing space*; e.g. ``-fcomment-block-commands=foo,bar`` defines |
| 1762 | custom commands ``\foo`` and ``\bar``. |
| 1763 | |
| 1764 | It is also possible to use ``-fcomment-block-commands`` several times; e.g. |
| 1765 | ``-fcomment-block-commands=foo -fcomment-block-commands=bar`` does the same |
| 1766 | as above. |
| 1767 | |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 1768 | .. _c: |
| 1769 | |
| 1770 | C Language Features |
| 1771 | =================== |
| 1772 | |
| 1773 | The support for standard C in clang is feature-complete except for the |
| 1774 | C99 floating-point pragmas. |
| 1775 | |
| 1776 | Extensions supported by clang |
| 1777 | ----------------------------- |
| 1778 | |
Dmitri Gribenko | 1436ff2 | 2012-12-19 22:06:59 +0000 | [diff] [blame] | 1779 | See :doc:`LanguageExtensions`. |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 1780 | |
| 1781 | Differences between various standard modes |
| 1782 | ------------------------------------------ |
| 1783 | |
| 1784 | clang supports the -std option, which changes what language mode clang |
Richard Smith | ab506ad | 2014-10-20 23:26:58 +0000 | [diff] [blame] | 1785 | uses. The supported modes for C are c89, gnu89, c94, c99, gnu99, c11, |
| 1786 | gnu11, and various aliases for those modes. If no -std option is |
| 1787 | specified, clang defaults to gnu11 mode. Many C99 and C11 features are |
| 1788 | supported in earlier modes as a conforming extension, with a warning. Use |
| 1789 | ``-pedantic-errors`` to request an error if a feature from a later standard |
| 1790 | revision is used in an earlier mode. |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 1791 | |
| 1792 | Differences between all ``c*`` and ``gnu*`` modes: |
| 1793 | |
| 1794 | - ``c*`` modes define "``__STRICT_ANSI__``". |
| 1795 | - Target-specific defines not prefixed by underscores, like "linux", |
| 1796 | are defined in ``gnu*`` modes. |
| 1797 | - Trigraphs default to being off in ``gnu*`` modes; they can be enabled by |
| 1798 | the -trigraphs option. |
| 1799 | - The parser recognizes "asm" and "typeof" as keywords in ``gnu*`` modes; |
| 1800 | the variants "``__asm__``" and "``__typeof__``" are recognized in all |
| 1801 | modes. |
| 1802 | - The Apple "blocks" extension is recognized by default in ``gnu*`` modes |
| 1803 | on some platforms; it can be enabled in any mode with the "-fblocks" |
| 1804 | option. |
| 1805 | - Arrays that are VLA's according to the standard, but which can be |
| 1806 | constant folded by the frontend are treated as fixed size arrays. |
| 1807 | This occurs for things like "int X[(1, 2)];", which is technically a |
| 1808 | VLA. ``c*`` modes are strictly compliant and treat these as VLAs. |
| 1809 | |
| 1810 | Differences between ``*89`` and ``*99`` modes: |
| 1811 | |
| 1812 | - The ``*99`` modes default to implementing "inline" as specified in C99, |
| 1813 | while the ``*89`` modes implement the GNU version. This can be |
| 1814 | overridden for individual functions with the ``__gnu_inline__`` |
| 1815 | attribute. |
| 1816 | - Digraphs are not recognized in c89 mode. |
| 1817 | - The scope of names defined inside a "for", "if", "switch", "while", |
| 1818 | or "do" statement is different. (example: "``if ((struct x {int |
| 1819 | x;}*)0) {}``".) |
| 1820 | - ``__STDC_VERSION__`` is not defined in ``*89`` modes. |
| 1821 | - "inline" is not recognized as a keyword in c89 mode. |
| 1822 | - "restrict" is not recognized as a keyword in ``*89`` modes. |
| 1823 | - Commas are allowed in integer constant expressions in ``*99`` modes. |
| 1824 | - Arrays which are not lvalues are not implicitly promoted to pointers |
| 1825 | in ``*89`` modes. |
| 1826 | - Some warnings are different. |
| 1827 | |
Richard Smith | ab506ad | 2014-10-20 23:26:58 +0000 | [diff] [blame] | 1828 | Differences between ``*99`` and ``*11`` modes: |
| 1829 | |
| 1830 | - Warnings for use of C11 features are disabled. |
| 1831 | - ``__STDC_VERSION__`` is defined to ``201112L`` rather than ``199901L``. |
| 1832 | |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 1833 | c94 mode is identical to c89 mode except that digraphs are enabled in |
| 1834 | c94 mode (FIXME: And ``__STDC_VERSION__`` should be defined!). |
| 1835 | |
| 1836 | GCC extensions not implemented yet |
| 1837 | ---------------------------------- |
| 1838 | |
| 1839 | clang tries to be compatible with gcc as much as possible, but some gcc |
| 1840 | extensions are not implemented yet: |
| 1841 | |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 1842 | - clang does not support decimal floating point types (``_Decimal32`` and |
| 1843 | friends) or fixed-point types (``_Fract`` and friends); nobody has |
| 1844 | expressed interest in these features yet, so it's hard to say when |
| 1845 | they will be implemented. |
| 1846 | - clang does not support nested functions; this is a complex feature |
| 1847 | which is infrequently used, so it is unlikely to be implemented |
| 1848 | anytime soon. In C++11 it can be emulated by assigning lambda |
| 1849 | functions to local variables, e.g: |
| 1850 | |
Dmitri Gribenko | 1436ff2 | 2012-12-19 22:06:59 +0000 | [diff] [blame] | 1851 | .. code-block:: cpp |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 1852 | |
Dmitri Gribenko | 1436ff2 | 2012-12-19 22:06:59 +0000 | [diff] [blame] | 1853 | auto const local_function = [&](int parameter) { |
| 1854 | // Do something |
| 1855 | }; |
| 1856 | ... |
| 1857 | local_function(1); |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 1858 | |
Michael Kuperstein | 94b25ec | 2016-12-12 19:11:39 +0000 | [diff] [blame] | 1859 | - clang only supports global register variables when the register specified |
| 1860 | is non-allocatable (e.g. the stack pointer). Support for general global |
| 1861 | register variables is unlikely to be implemented soon because it requires |
| 1862 | additional LLVM backend support. |
Andrey Bokhanko | 5dfd5b6 | 2016-02-11 13:27:02 +0000 | [diff] [blame] | 1863 | - clang does not support static initialization of flexible array |
| 1864 | members. This appears to be a rarely used extension, but could be |
| 1865 | implemented pending user demand. |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 1866 | - clang does not support |
| 1867 | ``__builtin_va_arg_pack``/``__builtin_va_arg_pack_len``. This is |
| 1868 | used rarely, but in some potentially interesting places, like the |
| 1869 | glibc headers, so it may be implemented pending user demand. Note |
| 1870 | that because clang pretends to be like GCC 4.2, and this extension |
| 1871 | was introduced in 4.3, the glibc headers will not try to use this |
| 1872 | extension with clang at the moment. |
| 1873 | - clang does not support the gcc extension for forward-declaring |
| 1874 | function parameters; this has not shown up in any real-world code |
| 1875 | yet, though, so it might never be implemented. |
| 1876 | |
| 1877 | This is not a complete list; if you find an unsupported extension |
| 1878 | missing from this list, please send an e-mail to cfe-dev. This list |
| 1879 | currently excludes C++; see :ref:`C++ Language Features <cxx>`. Also, this |
| 1880 | list does not include bugs in mostly-implemented features; please see |
| 1881 | the `bug |
| 1882 | tracker <http://llvm.org/bugs/buglist.cgi?quicksearch=product%3Aclang+component%3A-New%2BBugs%2CAST%2CBasic%2CDriver%2CHeaders%2CLLVM%2BCodeGen%2Cparser%2Cpreprocessor%2CSemantic%2BAnalyzer>`_ |
| 1883 | for known existing bugs (FIXME: Is there a section for bug-reporting |
| 1884 | guidelines somewhere?). |
| 1885 | |
| 1886 | Intentionally unsupported GCC extensions |
| 1887 | ---------------------------------------- |
| 1888 | |
| 1889 | - clang does not support the gcc extension that allows variable-length |
| 1890 | arrays in structures. This is for a few reasons: one, it is tricky to |
| 1891 | implement, two, the extension is completely undocumented, and three, |
| 1892 | the extension appears to be rarely used. Note that clang *does* |
| 1893 | support flexible array members (arrays with a zero or unspecified |
| 1894 | size at the end of a structure). |
| 1895 | - clang does not have an equivalent to gcc's "fold"; this means that |
| 1896 | clang doesn't accept some constructs gcc might accept in contexts |
| 1897 | where a constant expression is required, like "x-x" where x is a |
| 1898 | variable. |
| 1899 | - clang does not support ``__builtin_apply`` and friends; this extension |
| 1900 | is extremely obscure and difficult to implement reliably. |
| 1901 | |
| 1902 | .. _c_ms: |
| 1903 | |
| 1904 | Microsoft extensions |
| 1905 | -------------------- |
| 1906 | |
Reid Kleckner | 2a5d34b | 2016-03-28 20:42:41 +0000 | [diff] [blame] | 1907 | clang has support for many extensions from Microsoft Visual C++. To enable these |
| 1908 | extensions, use the ``-fms-extensions`` command-line option. This is the default |
| 1909 | for Windows targets. Clang does not implement every pragma or declspec provided |
| 1910 | by MSVC, but the popular ones, such as ``__declspec(dllexport)`` and ``#pragma |
| 1911 | comment(lib)`` are well supported. |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 1912 | |
Richard Smith | 48d1b65 | 2013-12-12 02:42:17 +0000 | [diff] [blame] | 1913 | clang has a ``-fms-compatibility`` flag that makes clang accept enough |
Reid Kleckner | 993e72a | 2013-09-20 17:04:25 +0000 | [diff] [blame] | 1914 | invalid C++ to be able to parse most Microsoft headers. For example, it |
| 1915 | allows `unqualified lookup of dependent base class members |
Reid Kleckner | eb248d7 | 2013-09-20 17:54:39 +0000 | [diff] [blame] | 1916 | <http://clang.llvm.org/compatibility.html#dep_lookup_bases>`_, which is |
| 1917 | a common compatibility issue with clang. This flag is enabled by default |
Reid Kleckner | 993e72a | 2013-09-20 17:04:25 +0000 | [diff] [blame] | 1918 | for Windows targets. |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 1919 | |
Richard Smith | 48d1b65 | 2013-12-12 02:42:17 +0000 | [diff] [blame] | 1920 | ``-fdelayed-template-parsing`` lets clang delay parsing of function template |
| 1921 | definitions until the end of a translation unit. This flag is enabled by |
| 1922 | default for Windows targets. |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 1923 | |
Reid Kleckner | 2a5d34b | 2016-03-28 20:42:41 +0000 | [diff] [blame] | 1924 | For compatibility with existing code that compiles with MSVC, clang defines the |
| 1925 | ``_MSC_VER`` and ``_MSC_FULL_VER`` macros. These default to the values of 1800 |
| 1926 | and 180000000 respectively, making clang look like an early release of Visual |
| 1927 | C++ 2013. The ``-fms-compatibility-version=`` flag overrides these values. It |
| 1928 | accepts a dotted version tuple, such as 19.00.23506. Changing the MSVC |
| 1929 | compatibility version makes clang behave more like that version of MSVC. For |
| 1930 | example, ``-fms-compatibility-version=19`` will enable C++14 features and define |
| 1931 | ``char16_t`` and ``char32_t`` as builtin types. |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 1932 | |
| 1933 | .. _cxx: |
| 1934 | |
| 1935 | C++ Language Features |
| 1936 | ===================== |
| 1937 | |
| 1938 | clang fully implements all of standard C++98 except for exported |
Richard Smith | 48d1b65 | 2013-12-12 02:42:17 +0000 | [diff] [blame] | 1939 | templates (which were removed in C++11), and all of standard C++11 |
| 1940 | and the current draft standard for C++1y. |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 1941 | |
| 1942 | Controlling implementation limits |
| 1943 | --------------------------------- |
| 1944 | |
Richard Smith | b3a1452 | 2013-02-22 01:59:51 +0000 | [diff] [blame] | 1945 | .. option:: -fbracket-depth=N |
| 1946 | |
| 1947 | Sets the limit for nested parentheses, brackets, and braces to N. The |
| 1948 | default is 256. |
| 1949 | |
Dmitri Gribenko | 1436ff2 | 2012-12-19 22:06:59 +0000 | [diff] [blame] | 1950 | .. option:: -fconstexpr-depth=N |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 1951 | |
Dmitri Gribenko | 1436ff2 | 2012-12-19 22:06:59 +0000 | [diff] [blame] | 1952 | Sets the limit for recursive constexpr function invocations to N. The |
| 1953 | default is 512. |
| 1954 | |
| 1955 | .. option:: -ftemplate-depth=N |
| 1956 | |
| 1957 | Sets the limit for recursively nested template instantiations to N. The |
Richard Smith | 79c927b | 2013-11-06 19:31:51 +0000 | [diff] [blame] | 1958 | default is 256. |
| 1959 | |
| 1960 | .. option:: -foperator-arrow-depth=N |
| 1961 | |
| 1962 | Sets the limit for iterative calls to 'operator->' functions to N. The |
| 1963 | default is 256. |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 1964 | |
| 1965 | .. _objc: |
| 1966 | |
| 1967 | Objective-C Language Features |
| 1968 | ============================= |
| 1969 | |
| 1970 | .. _objcxx: |
| 1971 | |
| 1972 | Objective-C++ Language Features |
| 1973 | =============================== |
| 1974 | |
Alexey Bataev | ae8c17e | 2015-08-24 05:31:10 +0000 | [diff] [blame] | 1975 | .. _openmp: |
| 1976 | |
| 1977 | OpenMP Features |
| 1978 | =============== |
| 1979 | |
| 1980 | Clang supports all OpenMP 3.1 directives and clauses. In addition, some |
| 1981 | features of OpenMP 4.0 are supported. For example, ``#pragma omp simd``, |
| 1982 | ``#pragma omp for simd``, ``#pragma omp parallel for simd`` directives, extended |
| 1983 | set of atomic constructs, ``proc_bind`` clause for all parallel-based |
| 1984 | directives, ``depend`` clause for ``#pragma omp task`` directive (except for |
| 1985 | array sections), ``#pragma omp cancel`` and ``#pragma omp cancellation point`` |
| 1986 | directives, and ``#pragma omp taskgroup`` directive. |
| 1987 | |
Aaron Ballman | 51fb031 | 2016-07-15 13:13:45 +0000 | [diff] [blame] | 1988 | Use `-fopenmp` to enable OpenMP. Support for OpenMP can be disabled with |
| 1989 | `-fno-openmp`. |
Alexey Bataev | ae8c17e | 2015-08-24 05:31:10 +0000 | [diff] [blame] | 1990 | |
| 1991 | Controlling implementation limits |
| 1992 | --------------------------------- |
| 1993 | |
| 1994 | .. option:: -fopenmp-use-tls |
| 1995 | |
| 1996 | Controls code generation for OpenMP threadprivate variables. In presence of |
| 1997 | this option all threadprivate variables are generated the same way as thread |
Aaron Ballman | 51fb031 | 2016-07-15 13:13:45 +0000 | [diff] [blame] | 1998 | local variables, using TLS support. If `-fno-openmp-use-tls` |
Alexey Bataev | ae8c17e | 2015-08-24 05:31:10 +0000 | [diff] [blame] | 1999 | is provided or target does not support TLS, code generation for threadprivate |
| 2000 | variables relies on OpenMP runtime library. |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 2001 | |
Anastasia Stulova | 18e165f | 2017-01-12 17:52:22 +0000 | [diff] [blame] | 2002 | .. _opencl: |
| 2003 | |
| 2004 | OpenCL Features |
| 2005 | =============== |
| 2006 | |
| 2007 | Clang can be used to compile OpenCL kernels for execution on a device |
| 2008 | (e.g. GPU). It is possible to compile the kernel into a binary (e.g. for AMD or |
| 2009 | Nvidia targets) that can be uploaded to run directly on a device (e.g. using |
| 2010 | `clCreateProgramWithBinary |
| 2011 | <https://www.khronos.org/registry/OpenCL/specs/opencl-1.1.pdf#111>`_) or |
| 2012 | into generic bitcode files loadable into other toolchains. |
| 2013 | |
| 2014 | Compiling to a binary using the default target from the installation can be done |
| 2015 | as follows: |
| 2016 | |
| 2017 | .. code-block:: console |
| 2018 | |
| 2019 | $ echo "kernel void k(){}" > test.cl |
| 2020 | $ clang test.cl |
| 2021 | |
| 2022 | Compiling for a specific target can be done by specifying the triple corresponding |
| 2023 | to the target, for example: |
| 2024 | |
| 2025 | .. code-block:: console |
| 2026 | |
| 2027 | $ clang -target nvptx64-unknown-unknown test.cl |
| 2028 | $ clang -target amdgcn-amd-amdhsa-opencl test.cl |
| 2029 | |
| 2030 | Compiling to bitcode can be done as follows: |
| 2031 | |
| 2032 | .. code-block:: console |
| 2033 | |
| 2034 | $ clang -c -emit-llvm test.cl |
| 2035 | |
| 2036 | This will produce a generic test.bc file that can be used in vendor toolchains |
| 2037 | to perform machine code generation. |
| 2038 | |
| 2039 | Clang currently supports OpenCL C language standards up to v2.0. |
| 2040 | |
| 2041 | OpenCL Specific Options |
| 2042 | ----------------------- |
| 2043 | |
| 2044 | Most of the OpenCL build options from `the specification v2.0 section 5.8.4 |
| 2045 | <https://www.khronos.org/registry/cl/specs/opencl-2.0.pdf#200>`_ are available. |
| 2046 | |
| 2047 | Examples: |
| 2048 | |
| 2049 | .. code-block:: console |
| 2050 | |
| 2051 | $ clang -cl-std=CL2.0 -cl-single-precision-constant test.cl |
| 2052 | |
| 2053 | Some extra options are available to support special OpenCL features. |
| 2054 | |
| 2055 | .. option:: -finclude-default-header |
| 2056 | |
| 2057 | Loads standard includes during compilations. By default OpenCL headers are not |
| 2058 | loaded and therefore standard library includes are not available. To load them |
| 2059 | automatically a flag has been added to the frontend (see also :ref:`the section |
| 2060 | on the OpenCL Header <opencl_header>`): |
| 2061 | |
| 2062 | .. code-block:: console |
| 2063 | |
| 2064 | $ clang -Xclang -finclude-default-header test.cl |
| 2065 | |
| 2066 | Alternatively ``-include`` or ``-I`` followed by the path to the header location |
| 2067 | can be given manually. |
| 2068 | |
| 2069 | .. code-block:: console |
| 2070 | |
| 2071 | $ clang -I<path to clang>/lib/Headers/opencl-c.h test.cl |
| 2072 | |
| 2073 | In this case the kernel code should contain ``#include <opencl-c.h>`` just as a |
| 2074 | regular C include. |
| 2075 | |
Anastasia Stulova | b376bee | 2017-02-16 12:49:29 +0000 | [diff] [blame^] | 2076 | .. _opencl_cl_ext: |
| 2077 | |
Anastasia Stulova | 18e165f | 2017-01-12 17:52:22 +0000 | [diff] [blame] | 2078 | .. option:: -cl-ext |
| 2079 | |
| 2080 | Disables support of OpenCL extensions. All OpenCL targets provide a list |
| 2081 | of extensions that they support. Clang allows to amend this using the ``-cl-ext`` |
| 2082 | flag with a comma-separated list of extensions prefixed with ``'+'`` or ``'-'``. |
| 2083 | The syntax: ``-cl-ext=<(['-'|'+']<extension>[,])+>``, where extensions |
| 2084 | can be either one of `the OpenCL specification extensions |
| 2085 | <https://www.khronos.org/registry/cl/sdk/2.0/docs/man/xhtml/EXTENSION.html>`_ |
| 2086 | or any known vendor extension. Alternatively, ``'all'`` can be used to enable |
| 2087 | or disable all known extensions. |
| 2088 | Example disabling double support for the 64-bit SPIR target: |
| 2089 | |
| 2090 | .. code-block:: console |
| 2091 | |
| 2092 | $ clang -cc1 -triple spir64-unknown-unknown -cl-ext=-cl_khr_fp64 test.cl |
| 2093 | |
| 2094 | Enabling all extensions except double support in R600 AMD GPU can be done using: |
| 2095 | |
| 2096 | .. code-block:: console |
| 2097 | |
| 2098 | $ clang -cc1 -triple r600-unknown-unknown -cl-ext=-all,+cl_khr_fp16 test.cl |
| 2099 | |
| 2100 | .. _opencl_fake_address_space_map: |
| 2101 | |
| 2102 | .. option:: -ffake-address-space-map |
| 2103 | |
| 2104 | Overrides the target address space map with a fake map. |
| 2105 | This allows adding explicit address space IDs to the bitcode for non-segmented |
| 2106 | memory architectures that don't have separate IDs for each of the OpenCL |
| 2107 | logical address spaces by default. Passing ``-ffake-address-space-map`` will |
| 2108 | add/override address spaces of the target compiled for with the following values: |
| 2109 | ``1-global``, ``2-constant``, ``3-local``, ``4-generic``. The private address |
| 2110 | space is represented by the absence of an address space attribute in the IR (see |
| 2111 | also :ref:`the section on the address space attribute <opencl_addrsp>`). |
| 2112 | |
| 2113 | .. code-block:: console |
| 2114 | |
| 2115 | $ clang -ffake-address-space-map test.cl |
| 2116 | |
| 2117 | Some other flags used for the compilation for C can also be passed while |
| 2118 | compiling for OpenCL, examples: ``-c``, ``-O<1-4|s>``, ``-o``, ``-emit-llvm``, etc. |
| 2119 | |
| 2120 | OpenCL Targets |
| 2121 | -------------- |
| 2122 | |
| 2123 | OpenCL targets are derived from the regular Clang target classes. The OpenCL |
| 2124 | specific parts of the target representation provide address space mapping as |
| 2125 | well as a set of supported extensions. |
| 2126 | |
| 2127 | Specific Targets |
| 2128 | ^^^^^^^^^^^^^^^^ |
| 2129 | |
| 2130 | There is a set of concrete HW architectures that OpenCL can be compiled for. |
| 2131 | |
| 2132 | - For AMD target: |
| 2133 | |
| 2134 | .. code-block:: console |
| 2135 | |
| 2136 | $ clang -target amdgcn-amd-amdhsa-opencl test.cl |
| 2137 | |
| 2138 | - For Nvidia architectures: |
| 2139 | |
| 2140 | .. code-block:: console |
| 2141 | |
| 2142 | $ clang -target nvptx64-unknown-unknown test.cl |
| 2143 | |
| 2144 | |
| 2145 | Generic Targets |
| 2146 | ^^^^^^^^^^^^^^^ |
| 2147 | |
| 2148 | - SPIR is available as a generic target to allow portable bitcode to be produced |
| 2149 | that can be used across GPU toolchains. The implementation follows `the SPIR |
| 2150 | specification <https://www.khronos.org/spir>`_. There are two flavors |
| 2151 | available for 32 and 64 bits. |
| 2152 | |
| 2153 | .. code-block:: console |
| 2154 | |
| 2155 | $ clang -target spir-unknown-unknown test.cl |
| 2156 | $ clang -target spir64-unknown-unknown test.cl |
| 2157 | |
| 2158 | All known OpenCL extensions are supported in the SPIR targets. Clang will |
| 2159 | generate SPIR v1.2 compatible IR for OpenCL versions up to 2.0 and SPIR v2.0 |
| 2160 | for OpenCL v2.0. |
| 2161 | |
| 2162 | - x86 is used by some implementations that are x86 compatible and currently |
| 2163 | remains for backwards compatibility (with older implementations prior to |
| 2164 | SPIR target support). For "non-SPMD" targets which cannot spawn multiple |
| 2165 | work-items on the fly using hardware, which covers practically all non-GPU |
| 2166 | devices such as CPUs and DSPs, additional processing is needed for the kernels |
| 2167 | to support multiple work-item execution. For this, a 3rd party toolchain, |
| 2168 | such as for example `POCL <http://portablecl.org/>`_, can be used. |
| 2169 | |
| 2170 | This target does not support multiple memory segments and, therefore, the fake |
| 2171 | address space map can be added using the :ref:`-ffake-address-space-map |
| 2172 | <opencl_fake_address_space_map>` flag. |
| 2173 | |
| 2174 | .. _opencl_header: |
| 2175 | |
| 2176 | OpenCL Header |
| 2177 | ------------- |
| 2178 | |
| 2179 | By default Clang will not include standard headers and therefore OpenCL builtin |
| 2180 | functions and some types (i.e. vectors) are unknown. The default CL header is, |
| 2181 | however, provided in the Clang installation and can be enabled by passing the |
| 2182 | ``-finclude-default-header`` flag to the Clang frontend. |
| 2183 | |
| 2184 | .. code-block:: console |
| 2185 | |
| 2186 | $ echo "bool is_wg_uniform(int i){return get_enqueued_local_size(i)==get_local_size(i);}" > test.cl |
| 2187 | $ clang -Xclang -finclude-default-header -cl-std=CL2.0 test.cl |
| 2188 | |
| 2189 | Because the header is very large and long to parse, PCH (:doc:`PCHInternals`) |
| 2190 | and modules (:doc:`Modules`) are used internally to improve the compilation |
| 2191 | speed. |
| 2192 | |
| 2193 | To enable modules for OpenCL: |
| 2194 | |
| 2195 | .. code-block:: console |
| 2196 | |
| 2197 | $ clang -target spir-unknown-unknown -c -emit-llvm -Xclang -finclude-default-header -fmodules -fimplicit-module-maps -fmodules-cache-path=<path to the generated module> test.cl |
| 2198 | |
Anastasia Stulova | b376bee | 2017-02-16 12:49:29 +0000 | [diff] [blame^] | 2199 | OpenCL Extensions |
| 2200 | ----------------- |
| 2201 | |
| 2202 | All of the ``cl_khr_*`` extensions from `the official OpenCL specification |
| 2203 | <https://www.khronos.org/registry/OpenCL/sdk/2.0/docs/man/xhtml/EXTENSION.html>`_ |
| 2204 | up to and including version 2.0 are available and set per target depending on the |
| 2205 | support available in the specific architecture. |
| 2206 | |
| 2207 | It is possible to alter the default extensions setting per target using |
| 2208 | ``-cl-ext`` flag. (See :ref:`flags description <opencl_cl_ext>` for more details). |
| 2209 | |
| 2210 | Vendor extensions can be added flexibly by declaring the list of types and |
| 2211 | functions associated with each extensions enclosed within the following |
| 2212 | compiler pragma directives: |
| 2213 | |
| 2214 | .. code-block:: c |
| 2215 | |
| 2216 | #pragma OPENCL EXTENSION the_new_extension_name : begin |
| 2217 | // declare types and functions associated with the extension here |
| 2218 | #pragma OPENCL EXTENSION the_new_extension_name : end |
| 2219 | |
| 2220 | For example, parsing the following code adds ``my_t`` type and ``my_func`` |
| 2221 | function to the custom ``my_ext`` extension. |
| 2222 | |
| 2223 | .. code-block:: c |
| 2224 | |
| 2225 | #pragma OPENCL EXTENSION my_ext : begin |
| 2226 | typedef struct{ |
| 2227 | int a; |
| 2228 | }my_t; |
| 2229 | void my_func(my_t); |
| 2230 | #pragma OPENCL EXTENSION my_ext : end |
| 2231 | |
| 2232 | Declaring the same types in different vendor extensions is disallowed. |
| 2233 | |
Anastasia Stulova | 18e165f | 2017-01-12 17:52:22 +0000 | [diff] [blame] | 2234 | OpenCL Metadata |
| 2235 | --------------- |
| 2236 | |
| 2237 | Clang uses metadata to provide additional OpenCL semantics in IR needed for |
| 2238 | backends and OpenCL runtime. |
| 2239 | |
| 2240 | Each kernel will have function metadata attached to it, specifying the arguments. |
| 2241 | Kernel argument metadata is used to provide source level information for querying |
| 2242 | at runtime, for example using the `clGetKernelArgInfo |
| 2243 | <https://www.khronos.org/registry/OpenCL/specs/opencl-1.2.pdf#167>`_ |
| 2244 | call. |
| 2245 | |
| 2246 | Note that ``-cl-kernel-arg-info`` enables more information about the original CL |
| 2247 | code to be added e.g. kernel parameter names will appear in the OpenCL metadata |
| 2248 | along with other information. |
| 2249 | |
| 2250 | The IDs used to encode the OpenCL's logical address spaces in the argument info |
| 2251 | metadata follows the SPIR address space mapping as defined in the SPIR |
| 2252 | specification `section 2.2 |
| 2253 | <https://www.khronos.org/registry/spir/specs/spir_spec-2.0.pdf#18>`_ |
| 2254 | |
| 2255 | OpenCL-Specific Attributes |
| 2256 | -------------------------- |
| 2257 | |
| 2258 | OpenCL support in Clang contains a set of attribute taken directly from the |
| 2259 | specification as well as additional attributes. |
| 2260 | |
| 2261 | See also :doc:`AttributeReference`. |
| 2262 | |
| 2263 | nosvm |
| 2264 | ^^^^^ |
| 2265 | |
| 2266 | Clang supports this attribute to comply to OpenCL v2.0 conformance, but it |
| 2267 | does not have any effect on the IR. For more details reffer to the specification |
| 2268 | `section 6.7.2 |
| 2269 | <https://www.khronos.org/registry/cl/specs/opencl-2.0-openclc.pdf#49>`_ |
| 2270 | |
| 2271 | |
Anastasia Stulova | b376bee | 2017-02-16 12:49:29 +0000 | [diff] [blame^] | 2272 | opencl_unroll_hint |
Anastasia Stulova | 18e165f | 2017-01-12 17:52:22 +0000 | [diff] [blame] | 2273 | ^^^^^^^^^^^^^^^^^^ |
| 2274 | |
| 2275 | The implementation of this feature mirrors the unroll hint for C. |
| 2276 | More details on the syntax can be found in the specification |
| 2277 | `section 6.11.5 |
| 2278 | <https://www.khronos.org/registry/cl/specs/opencl-2.0-openclc.pdf#61>`_ |
| 2279 | |
| 2280 | convergent |
| 2281 | ^^^^^^^^^^ |
| 2282 | |
| 2283 | To make sure no invalid optimizations occur for single program multiple data |
| 2284 | (SPMD) / single instruction multiple thread (SIMT) Clang provides attributes that |
| 2285 | can be used for special functions that have cross work item semantics. |
| 2286 | An example is the subgroup operations such as `intel_sub_group_shuffle |
| 2287 | <https://www.khronos.org/registry/cl/extensions/intel/cl_intel_subgroups.txt>`_ |
| 2288 | |
| 2289 | .. code-block:: c |
| 2290 | |
| 2291 | // Define custom my_sub_group_shuffle(data, c) |
| 2292 | // that makes use of intel_sub_group_shuffle |
Aaron Ballman | 37ff16f | 2017-01-16 13:42:21 +0000 | [diff] [blame] | 2293 | r1 = ... |
Anastasia Stulova | 18e165f | 2017-01-12 17:52:22 +0000 | [diff] [blame] | 2294 | if (r0) r1 = computeA(); |
| 2295 | // Shuffle data from r1 into r3 |
| 2296 | // of threads id r2. |
| 2297 | r3 = my_sub_group_shuffle(r1, r2); |
| 2298 | if (r0) r3 = computeB(); |
| 2299 | |
| 2300 | with non-SPMD semantics this is optimized to the following equivalent code: |
| 2301 | |
| 2302 | .. code-block:: c |
| 2303 | |
Aaron Ballman | 37ff16f | 2017-01-16 13:42:21 +0000 | [diff] [blame] | 2304 | r1 = ... |
Anastasia Stulova | 18e165f | 2017-01-12 17:52:22 +0000 | [diff] [blame] | 2305 | if (!r0) |
| 2306 | // Incorrect functionality! The data in r1 |
| 2307 | // have not been computed by all threads yet. |
| 2308 | r3 = my_sub_group_shuffle(r1, r2); |
| 2309 | else { |
| 2310 | r1 = computeA(); |
| 2311 | r3 = my_sub_group_shuffle(r1, r2); |
| 2312 | r3 = computeB(); |
| 2313 | } |
| 2314 | |
| 2315 | Declaring the function ``my_sub_group_shuffle`` with the convergent attribute |
| 2316 | would prevent this: |
| 2317 | |
| 2318 | .. code-block:: c |
| 2319 | |
| 2320 | my_sub_group_shuffle() __attribute__((convergent)); |
| 2321 | |
| 2322 | Using ``convergent`` guarantees correct execution by keeping CFG equivalence |
| 2323 | wrt operations marked as ``convergent``. CFG ``G´`` is equivalent to ``G`` wrt |
| 2324 | node ``Ni`` : ``iff ∀ Nj (i≠j)`` domination and post-domination relations with |
| 2325 | respect to ``Ni`` remain the same in both ``G`` and ``G´``. |
| 2326 | |
| 2327 | noduplicate |
| 2328 | ^^^^^^^^^^^ |
| 2329 | |
| 2330 | ``noduplicate`` is more restrictive with respect to optimizations than |
| 2331 | ``convergent`` because a convergent function only preserves CFG equivalence. |
| 2332 | This allows some optimizations to happen as long as the control flow remains |
| 2333 | unmodified. |
| 2334 | |
| 2335 | .. code-block:: c |
| 2336 | |
| 2337 | for (int i=0; i<4; i++) |
| 2338 | my_sub_group_shuffle() |
| 2339 | |
| 2340 | can be modified to: |
| 2341 | |
| 2342 | .. code-block:: c |
| 2343 | |
| 2344 | my_sub_group_shuffle(); |
| 2345 | my_sub_group_shuffle(); |
| 2346 | my_sub_group_shuffle(); |
| 2347 | my_sub_group_shuffle(); |
| 2348 | |
| 2349 | while using ``noduplicate`` would disallow this. Also ``noduplicate`` doesn't |
| 2350 | have the same safe semantics of CFG as ``convergent`` and can cause changes in |
| 2351 | CFG that modify semantics of the original program. |
| 2352 | |
| 2353 | ``noduplicate`` is kept for backwards compatibility only and it considered to be |
| 2354 | deprecated for future uses. |
| 2355 | |
| 2356 | .. _opencl_addrsp: |
| 2357 | |
| 2358 | address_space |
| 2359 | ^^^^^^^^^^^^^ |
| 2360 | |
| 2361 | Clang has arbitrary address space support using the ``address_space(N)`` |
| 2362 | attribute, where ``N`` is an integer number in the range ``0`` to ``16777215`` |
| 2363 | (``0xffffffu``). |
| 2364 | |
| 2365 | An OpenCL implementation provides a list of standard address spaces using |
| 2366 | keywords: ``private``, ``local``, ``global``, and ``generic``. In the AST and |
| 2367 | in the IR local, global, or generic will be represented by the address space |
| 2368 | attribute with the corresponding unique number. Note that private does not have |
| 2369 | any corresponding attribute added and, therefore, is represented by the absence |
| 2370 | of an address space number. The specific IDs for an address space do not have to |
| 2371 | match between the AST and the IR. Typically in the AST address space numbers |
| 2372 | represent logical segments while in the IR they represent physical segments. |
| 2373 | Therefore, machines with flat memory segments can map all AST address space |
| 2374 | numbers to the same physical segment ID or skip address space attribute |
| 2375 | completely while generating the IR. However, if the address space information |
| 2376 | is needed by the IR passes e.g. to improve alias analysis, it is recommended |
| 2377 | to keep it and only lower to reflect physical memory segments in the late |
| 2378 | machine passes. |
| 2379 | |
| 2380 | OpenCL builtins |
| 2381 | --------------- |
| 2382 | |
| 2383 | There are some standard OpenCL functions that are implemented as Clang builtins: |
| 2384 | |
| 2385 | - All pipe functions from `section 6.13.16.2/6.13.16.3 |
| 2386 | <https://www.khronos.org/registry/cl/specs/opencl-2.0-openclc.pdf#160>`_ of |
| 2387 | the OpenCL v2.0 kernel language specification. ` |
| 2388 | |
| 2389 | - Address space qualifier conversion functions ``to_global``/``to_local``/``to_private`` |
| 2390 | from `section 6.13.9 |
| 2391 | <https://www.khronos.org/registry/cl/specs/opencl-2.0-openclc.pdf#101>`_. |
| 2392 | |
| 2393 | - All the ``enqueue_kernel`` functions from `section 6.13.17.1 |
| 2394 | <https://www.khronos.org/registry/cl/specs/opencl-2.0-openclc.pdf#164>`_ and |
| 2395 | enqueue query functions from `section 6.13.17.5 |
| 2396 | <https://www.khronos.org/registry/cl/specs/opencl-2.0-openclc.pdf#171>`_. |
| 2397 | |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 2398 | .. _target_features: |
| 2399 | |
| 2400 | Target-Specific Features and Limitations |
| 2401 | ======================================== |
| 2402 | |
| 2403 | CPU Architectures Features and Limitations |
| 2404 | ------------------------------------------ |
| 2405 | |
| 2406 | X86 |
| 2407 | ^^^ |
| 2408 | |
| 2409 | The support for X86 (both 32-bit and 64-bit) is considered stable on |
Nico Weber | ab88f0b | 2014-03-07 18:09:57 +0000 | [diff] [blame] | 2410 | Darwin (Mac OS X), Linux, FreeBSD, and Dragonfly BSD: it has been tested |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 2411 | to correctly compile many large C, C++, Objective-C, and Objective-C++ |
| 2412 | codebases. |
| 2413 | |
Richard Smith | 48d1b65 | 2013-12-12 02:42:17 +0000 | [diff] [blame] | 2414 | On ``x86_64-mingw32``, passing i128(by value) is incompatible with the |
David Woodhouse | ddf8985 | 2014-01-23 14:32:46 +0000 | [diff] [blame] | 2415 | Microsoft x64 calling convention. You might need to tweak |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 2416 | ``WinX86_64ABIInfo::classify()`` in lib/CodeGen/TargetInfo.cpp. |
| 2417 | |
Aaron Ballman | 51fb031 | 2016-07-15 13:13:45 +0000 | [diff] [blame] | 2418 | For the X86 target, clang supports the `-m16` command line |
David Woodhouse | ddf8985 | 2014-01-23 14:32:46 +0000 | [diff] [blame] | 2419 | argument which enables 16-bit code output. This is broadly similar to |
| 2420 | using ``asm(".code16gcc")`` with the GNU toolchain. The generated code |
| 2421 | and the ABI remains 32-bit but the assembler emits instructions |
| 2422 | appropriate for a CPU running in 16-bit mode, with address-size and |
| 2423 | operand-size prefixes to enable 32-bit addressing and operations. |
| 2424 | |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 2425 | ARM |
| 2426 | ^^^ |
| 2427 | |
| 2428 | The support for ARM (specifically ARMv6 and ARMv7) is considered stable |
| 2429 | on Darwin (iOS): it has been tested to correctly compile many large C, |
| 2430 | C++, Objective-C, and Objective-C++ codebases. Clang only supports a |
| 2431 | limited number of ARM architectures. It does not yet fully support |
| 2432 | ARMv5, for example. |
| 2433 | |
Roman Divacky | 786d32e | 2013-09-11 17:12:49 +0000 | [diff] [blame] | 2434 | PowerPC |
| 2435 | ^^^^^^^ |
| 2436 | |
| 2437 | The support for PowerPC (especially PowerPC64) is considered stable |
| 2438 | on Linux and FreeBSD: it has been tested to correctly compile many |
| 2439 | large C and C++ codebases. PowerPC (32bit) is still missing certain |
| 2440 | features (e.g. PIC code on ELF platforms). |
| 2441 | |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 2442 | Other platforms |
| 2443 | ^^^^^^^^^^^^^^^ |
| 2444 | |
Roman Divacky | 786d32e | 2013-09-11 17:12:49 +0000 | [diff] [blame] | 2445 | clang currently contains some support for other architectures (e.g. Sparc); |
| 2446 | however, significant pieces of code generation are still missing, and they |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 2447 | haven't undergone significant testing. |
| 2448 | |
| 2449 | clang contains limited support for the MSP430 embedded processor, but |
| 2450 | both the clang support and the LLVM backend support are highly |
| 2451 | experimental. |
| 2452 | |
| 2453 | Other platforms are completely unsupported at the moment. Adding the |
| 2454 | minimal support needed for parsing and semantic analysis on a new |
Dmitri Gribenko | 1436ff2 | 2012-12-19 22:06:59 +0000 | [diff] [blame] | 2455 | platform is quite easy; see ``lib/Basic/Targets.cpp`` in the clang source |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 2456 | tree. This level of support is also sufficient for conversion to LLVM IR |
| 2457 | for simple programs. Proper support for conversion to LLVM IR requires |
Dmitri Gribenko | 1436ff2 | 2012-12-19 22:06:59 +0000 | [diff] [blame] | 2458 | adding code to ``lib/CodeGen/CGCall.cpp`` at the moment; this is likely to |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 2459 | change soon, though. Generating assembly requires a suitable LLVM |
| 2460 | backend. |
| 2461 | |
| 2462 | Operating System Features and Limitations |
| 2463 | ----------------------------------------- |
| 2464 | |
Nico Weber | ab88f0b | 2014-03-07 18:09:57 +0000 | [diff] [blame] | 2465 | Darwin (Mac OS X) |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 2466 | ^^^^^^^^^^^^^^^^^ |
| 2467 | |
Nico Weber | c7cb940 | 2014-03-07 18:11:40 +0000 | [diff] [blame] | 2468 | Thread Sanitizer is not supported. |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 2469 | |
| 2470 | Windows |
| 2471 | ^^^^^^^ |
| 2472 | |
Richard Smith | 48d1b65 | 2013-12-12 02:42:17 +0000 | [diff] [blame] | 2473 | Clang has experimental support for targeting "Cygming" (Cygwin / MinGW) |
| 2474 | platforms. |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 2475 | |
Reid Kleckner | 725b7b3 | 2013-09-05 21:29:35 +0000 | [diff] [blame] | 2476 | See also :ref:`Microsoft Extensions <c_ms>`. |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 2477 | |
| 2478 | Cygwin |
| 2479 | """""" |
| 2480 | |
| 2481 | Clang works on Cygwin-1.7. |
| 2482 | |
| 2483 | MinGW32 |
| 2484 | """"""" |
| 2485 | |
| 2486 | Clang works on some mingw32 distributions. Clang assumes directories as |
| 2487 | below; |
| 2488 | |
| 2489 | - ``C:/mingw/include`` |
| 2490 | - ``C:/mingw/lib`` |
| 2491 | - ``C:/mingw/lib/gcc/mingw32/4.[3-5].0/include/c++`` |
| 2492 | |
| 2493 | On MSYS, a few tests might fail. |
| 2494 | |
| 2495 | MinGW-w64 |
| 2496 | """"""""" |
| 2497 | |
| 2498 | For 32-bit (i686-w64-mingw32), and 64-bit (x86\_64-w64-mingw32), Clang |
| 2499 | assumes as below; |
| 2500 | |
| 2501 | - ``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)`` |
| 2502 | - ``some_directory/bin/gcc.exe`` |
| 2503 | - ``some_directory/bin/clang.exe`` |
| 2504 | - ``some_directory/bin/clang++.exe`` |
| 2505 | - ``some_directory/bin/../include/c++/GCC_version`` |
| 2506 | - ``some_directory/bin/../include/c++/GCC_version/x86_64-w64-mingw32`` |
| 2507 | - ``some_directory/bin/../include/c++/GCC_version/i686-w64-mingw32`` |
| 2508 | - ``some_directory/bin/../include/c++/GCC_version/backward`` |
| 2509 | - ``some_directory/bin/../x86_64-w64-mingw32/include`` |
| 2510 | - ``some_directory/bin/../i686-w64-mingw32/include`` |
| 2511 | - ``some_directory/bin/../include`` |
| 2512 | |
| 2513 | This directory layout is standard for any toolchain you will find on the |
| 2514 | official `MinGW-w64 website <http://mingw-w64.sourceforge.net>`_. |
| 2515 | |
| 2516 | Clang expects the GCC executable "gcc.exe" compiled for |
| 2517 | ``i686-w64-mingw32`` (or ``x86_64-w64-mingw32``) to be present on PATH. |
| 2518 | |
| 2519 | `Some tests might fail <http://llvm.org/bugs/show_bug.cgi?id=9072>`_ on |
| 2520 | ``x86_64-w64-mingw32``. |
Hans Wennborg | 2a6e6bc | 2013-10-10 01:15:16 +0000 | [diff] [blame] | 2521 | |
| 2522 | .. _clang-cl: |
| 2523 | |
| 2524 | clang-cl |
| 2525 | ======== |
| 2526 | |
Hans Wennborg | 715dd7f | 2017-01-12 18:15:06 +0000 | [diff] [blame] | 2527 | clang-cl is an alternative command-line interface to Clang, designed for |
Hans Wennborg | 2a6e6bc | 2013-10-10 01:15:16 +0000 | [diff] [blame] | 2528 | compatibility with the Visual C++ compiler, cl.exe. |
| 2529 | |
| 2530 | To enable clang-cl to find system headers, libraries, and the linker when run |
| 2531 | from the command-line, it should be executed inside a Visual Studio Native Tools |
| 2532 | Command Prompt or a regular Command Prompt where the environment has been set |
| 2533 | up using e.g. `vcvars32.bat <http://msdn.microsoft.com/en-us/library/f2ccy3wt.aspx>`_. |
| 2534 | |
Hans Wennborg | 715dd7f | 2017-01-12 18:15:06 +0000 | [diff] [blame] | 2535 | clang-cl can also be used from inside Visual Studio by using an LLVM Platform |
Hans Wennborg | 2a6e6bc | 2013-10-10 01:15:16 +0000 | [diff] [blame] | 2536 | Toolset. |
| 2537 | |
| 2538 | Command-Line Options |
| 2539 | -------------------- |
| 2540 | |
| 2541 | To be compatible with cl.exe, clang-cl supports most of the same command-line |
| 2542 | options. Those options can start with either ``/`` or ``-``. It also supports |
| 2543 | some of Clang's core options, such as the ``-W`` options. |
| 2544 | |
| 2545 | Options that are known to clang-cl, but not currently supported, are ignored |
| 2546 | with a warning. For example: |
| 2547 | |
| 2548 | :: |
| 2549 | |
Hans Wennborg | 0d08062 | 2015-08-12 19:35:01 +0000 | [diff] [blame] | 2550 | clang-cl.exe: warning: argument unused during compilation: '/AI' |
Hans Wennborg | 2a6e6bc | 2013-10-10 01:15:16 +0000 | [diff] [blame] | 2551 | |
| 2552 | To suppress warnings about unused arguments, use the ``-Qunused-arguments`` option. |
| 2553 | |
Ehsan Akhgari | d851833 | 2016-01-25 21:14:52 +0000 | [diff] [blame] | 2554 | Options that are not known to clang-cl will be ignored by default. Use the |
| 2555 | ``-Werror=unknown-argument`` option in order to treat them as errors. If these |
| 2556 | options are spelled with a leading ``/``, they will be mistaken for a filename: |
Hans Wennborg | 2a6e6bc | 2013-10-10 01:15:16 +0000 | [diff] [blame] | 2557 | |
| 2558 | :: |
| 2559 | |
| 2560 | clang-cl.exe: error: no such file or directory: '/foobar' |
| 2561 | |
| 2562 | Please `file a bug <http://llvm.org/bugs/enter_bug.cgi?product=clang&component=Driver>`_ |
| 2563 | for any valid cl.exe flags that clang-cl does not understand. |
| 2564 | |
| 2565 | Execute ``clang-cl /?`` to see a list of supported options: |
| 2566 | |
| 2567 | :: |
| 2568 | |
Hans Wennborg | 35487d8 | 2014-08-04 21:07:58 +0000 | [diff] [blame] | 2569 | CL.EXE COMPATIBILITY OPTIONS: |
Hans Wennborg | 715dd7f | 2017-01-12 18:15:06 +0000 | [diff] [blame] | 2570 | /? Display available options |
| 2571 | /arch:<value> Set architecture for code generation |
| 2572 | /Brepro- Emit an object file which cannot be reproduced over time |
| 2573 | /Brepro Emit an object file which can be reproduced over time |
| 2574 | /C Don't discard comments when preprocessing |
| 2575 | /c Compile only |
| 2576 | /D <macro[=value]> Define macro |
| 2577 | /EH<value> Exception handling model |
| 2578 | /EP Disable linemarker output and preprocess to stdout |
| 2579 | /execution-charset:<value> |
| 2580 | Runtime encoding, supports only UTF-8 |
| 2581 | /E Preprocess to stdout |
| 2582 | /fallback Fall back to cl.exe if clang-cl fails to compile |
| 2583 | /FA Output assembly code file during compilation |
| 2584 | /Fa<file or directory> Output assembly code to this file during compilation (with /FA) |
| 2585 | /Fe<file or directory> Set output executable file or directory (ends in / or \) |
| 2586 | /FI <value> Include file before parsing |
| 2587 | /Fi<file> Set preprocess output file name (with /P) |
| 2588 | /Fo<file or directory> Set output object file, or directory (ends in / or \) (with /c) |
Hans Wennborg | 0d08062 | 2015-08-12 19:35:01 +0000 | [diff] [blame] | 2589 | /fp:except- |
| 2590 | /fp:except |
| 2591 | /fp:fast |
| 2592 | /fp:precise |
| 2593 | /fp:strict |
Hans Wennborg | 715dd7f | 2017-01-12 18:15:06 +0000 | [diff] [blame] | 2594 | /Fp<filename> Set pch filename (with /Yc and /Yu) |
| 2595 | /GA Assume thread-local variables are defined in the executable |
| 2596 | /Gd Set __cdecl as a default calling convention |
| 2597 | /GF- Disable string pooling |
| 2598 | /GR- Disable emission of RTTI data |
| 2599 | /GR Enable emission of RTTI data |
| 2600 | /Gr Set __fastcall as a default calling convention |
| 2601 | /GS- Disable buffer security check |
| 2602 | /GS Enable buffer security check |
| 2603 | /Gs<value> Set stack probe size |
| 2604 | /Gv Set __vectorcall as a default calling convention |
| 2605 | /Gw- Don't put each data item in its own section |
| 2606 | /Gw Put each data item in its own section |
| 2607 | /GX- Enable exception handling |
| 2608 | /GX Enable exception handling |
| 2609 | /Gy- Don't put each function in its own section |
| 2610 | /Gy Put each function in its own section |
| 2611 | /Gz Set __stdcall as a default calling convention |
| 2612 | /help Display available options |
| 2613 | /imsvc <dir> Add directory to system include search path, as if part of %INCLUDE% |
| 2614 | /I <dir> Add directory to include search path |
| 2615 | /J Make char type unsigned |
| 2616 | /LDd Create debug DLL |
| 2617 | /LD Create DLL |
| 2618 | /link <options> Forward options to the linker |
| 2619 | /MDd Use DLL debug run-time |
| 2620 | /MD Use DLL run-time |
| 2621 | /MTd Use static debug run-time |
| 2622 | /MT Use static run-time |
| 2623 | /Od Disable optimization |
| 2624 | /Oi- Disable use of builtin functions |
| 2625 | /Oi Enable use of builtin functions |
| 2626 | /Os Optimize for size |
| 2627 | /Ot Optimize for speed |
| 2628 | /O<value> Optimization level |
| 2629 | /o <file or directory> Set output file or directory (ends in / or \) |
| 2630 | /P Preprocess to file |
| 2631 | /Qvec- Disable the loop vectorization passes |
| 2632 | /Qvec Enable the loop vectorization passes |
| 2633 | /showIncludes Print info about included files to stderr |
| 2634 | /source-charset:<value> Source encoding, supports only UTF-8 |
| 2635 | /std:<value> Language standard to compile for |
| 2636 | /TC Treat all source files as C |
| 2637 | /Tc <filename> Specify a C source file |
| 2638 | /TP Treat all source files as C++ |
| 2639 | /Tp <filename> Specify a C++ source file |
Hans Wennborg | 9d1ed00 | 2017-01-12 19:26:54 +0000 | [diff] [blame] | 2640 | /utf-8 Set source and runtime encoding to UTF-8 (default) |
Hans Wennborg | 715dd7f | 2017-01-12 18:15:06 +0000 | [diff] [blame] | 2641 | /U <macro> Undefine macro |
| 2642 | /vd<value> Control vtordisp placement |
| 2643 | /vmb Use a best-case representation method for member pointers |
| 2644 | /vmg Use a most-general representation for member pointers |
| 2645 | /vmm Set the default most-general representation to multiple inheritance |
| 2646 | /vms Set the default most-general representation to single inheritance |
| 2647 | /vmv Set the default most-general representation to virtual inheritance |
| 2648 | /volatile:iso Volatile loads and stores have standard semantics |
| 2649 | /volatile:ms Volatile loads and stores have acquire and release semantics |
| 2650 | /W0 Disable all warnings |
| 2651 | /W1 Enable -Wall |
| 2652 | /W2 Enable -Wall |
| 2653 | /W3 Enable -Wall |
| 2654 | /W4 Enable -Wall and -Wextra |
| 2655 | /Wall Enable -Wall and -Wextra |
| 2656 | /WX- Do not treat warnings as errors |
| 2657 | /WX Treat warnings as errors |
| 2658 | /w Disable all warnings |
| 2659 | /Y- Disable precompiled headers, overrides /Yc and /Yu |
| 2660 | /Yc<filename> Generate a pch file for all code up to and including <filename> |
| 2661 | /Yu<filename> Load a pch file and use it instead of all code up to and including <filename> |
| 2662 | /Z7 Enable CodeView debug information in object files |
| 2663 | /Zc:sizedDealloc- Disable C++14 sized global deallocation functions |
| 2664 | /Zc:sizedDealloc Enable C++14 sized global deallocation functions |
| 2665 | /Zc:strictStrings Treat string literals as const |
| 2666 | /Zc:threadSafeInit- Disable thread-safe initialization of static variables |
| 2667 | /Zc:threadSafeInit Enable thread-safe initialization of static variables |
| 2668 | /Zc:trigraphs- Disable trigraphs (default) |
| 2669 | /Zc:trigraphs Enable trigraphs |
| 2670 | /Zd Emit debug line number tables only |
| 2671 | /Zi Alias for /Z7. Does not produce PDBs. |
| 2672 | /Zl Don't mention any default libraries in the object file |
| 2673 | /Zp Set the default maximum struct packing alignment to 1 |
| 2674 | /Zp<value> Specify the default maximum struct packing alignment |
| 2675 | /Zs Syntax-check only |
Hans Wennborg | 35487d8 | 2014-08-04 21:07:58 +0000 | [diff] [blame] | 2676 | |
| 2677 | OPTIONS: |
Hans Wennborg | 0d08062 | 2015-08-12 19:35:01 +0000 | [diff] [blame] | 2678 | -### Print (but do not run) the commands to run for this compilation |
| 2679 | --analyze Run the static analyzer |
| 2680 | -fansi-escape-codes Use ANSI escape codes for diagnostics |
| 2681 | -fcolor-diagnostics Use colors in diagnostics |
Hans Wennborg | 715dd7f | 2017-01-12 18:15:06 +0000 | [diff] [blame] | 2682 | -fdelayed-template-parsing |
| 2683 | Parse templated function definitions at the end of the translation unit |
| 2684 | -fdiagnostics-absolute-paths |
| 2685 | Print absolute paths in diagnostics |
Hans Wennborg | 0d08062 | 2015-08-12 19:35:01 +0000 | [diff] [blame] | 2686 | -fdiagnostics-parseable-fixits |
| 2687 | Print fix-its in machine parseable form |
Hans Wennborg | 715dd7f | 2017-01-12 18:15:06 +0000 | [diff] [blame] | 2688 | -flto Enable LTO in 'full' mode |
Hans Wennborg | 35487d8 | 2014-08-04 21:07:58 +0000 | [diff] [blame] | 2689 | -fms-compatibility-version=<value> |
Hans Wennborg | 0d08062 | 2015-08-12 19:35:01 +0000 | [diff] [blame] | 2690 | Dot-separated value representing the Microsoft compiler version |
| 2691 | number to report in _MSC_VER (0 = don't define it (default)) |
Hans Wennborg | e8178e8 | 2016-02-12 01:01:37 +0000 | [diff] [blame] | 2692 | -fms-compatibility Enable full Microsoft Visual C++ compatibility |
| 2693 | -fms-extensions Accept some non-standard constructs supported by the Microsoft compiler |
| 2694 | -fmsc-version=<value> Microsoft compiler version number to report in _MSC_VER |
| 2695 | (0 = don't define it (default)) |
Hans Wennborg | 9d1ed00 | 2017-01-12 19:26:54 +0000 | [diff] [blame] | 2696 | -fno-delayed-template-parsing |
| 2697 | Disable delayed template parsing |
Hans Wennborg | 0d08062 | 2015-08-12 19:35:01 +0000 | [diff] [blame] | 2698 | -fno-sanitize-coverage=<value> |
| 2699 | Disable specified features of coverage instrumentation for Sanitizers |
| 2700 | -fno-sanitize-recover=<value> |
| 2701 | Disable recovery for specified sanitizers |
| 2702 | -fno-sanitize-trap=<value> |
| 2703 | Disable trapping for specified sanitizers |
Hans Wennborg | 715dd7f | 2017-01-12 18:15:06 +0000 | [diff] [blame] | 2704 | -fno-standalone-debug Limit debug information produced to reduce size of debug binary |
| 2705 | -fprofile-instr-generate=<file> |
| 2706 | Generate instrumented code to collect execution counts into <file> |
| 2707 | (overridden by LLVM_PROFILE_FILE env var) |
| 2708 | -fprofile-instr-generate |
| 2709 | Generate instrumented code to collect execution counts into default.profraw file |
Sylvestre Ledru | e86ee6b | 2017-01-14 11:41:45 +0000 | [diff] [blame] | 2710 | (overridden by '=' form of option or LLVM_PROFILE_FILE env var) |
Hans Wennborg | 715dd7f | 2017-01-12 18:15:06 +0000 | [diff] [blame] | 2711 | -fprofile-instr-use=<value> |
| 2712 | Use instrumentation data for profile-guided optimization |
Hans Wennborg | 35487d8 | 2014-08-04 21:07:58 +0000 | [diff] [blame] | 2713 | -fsanitize-blacklist=<value> |
Hans Wennborg | 0d08062 | 2015-08-12 19:35:01 +0000 | [diff] [blame] | 2714 | Path to blacklist file for sanitizers |
| 2715 | -fsanitize-coverage=<value> |
| 2716 | Specify the type of coverage instrumentation for Sanitizers |
| 2717 | -fsanitize-recover=<value> |
| 2718 | Enable recovery for specified sanitizers |
| 2719 | -fsanitize-trap=<value> Enable trapping for specified sanitizers |
| 2720 | -fsanitize=<check> Turn on runtime checks for various forms of undefined or suspicious |
| 2721 | behavior. See user manual for available checks |
Hans Wennborg | 715dd7f | 2017-01-12 18:15:06 +0000 | [diff] [blame] | 2722 | -fstandalone-debug Emit full debug info for all types used by the program |
Hans Wennborg | 0d08062 | 2015-08-12 19:35:01 +0000 | [diff] [blame] | 2723 | -gcodeview Generate CodeView debug information |
Hans Wennborg | 6e70f4e | 2016-07-27 16:56:03 +0000 | [diff] [blame] | 2724 | -gline-tables-only Emit debug line number tables only |
| 2725 | -miamcu Use Intel MCU ABI |
Hans Wennborg | 0d08062 | 2015-08-12 19:35:01 +0000 | [diff] [blame] | 2726 | -mllvm <value> Additional arguments to forward to LLVM's option processing |
| 2727 | -Qunused-arguments Don't emit warning for unused driver arguments |
| 2728 | -R<remark> Enable the specified remark |
| 2729 | --target=<value> Generate code for the given target |
| 2730 | -v Show commands to run and use verbose output |
| 2731 | -W<warning> Enable the specified warning |
| 2732 | -Xclang <arg> Pass <arg> to the clang compiler |
Hans Wennborg | 2a6e6bc | 2013-10-10 01:15:16 +0000 | [diff] [blame] | 2733 | |
| 2734 | The /fallback Option |
| 2735 | ^^^^^^^^^^^^^^^^^^^^ |
| 2736 | |
| 2737 | When clang-cl is run with the ``/fallback`` option, it will first try to |
| 2738 | compile files itself. For any file that it fails to compile, it will fall back |
| 2739 | and try to compile the file by invoking cl.exe. |
| 2740 | |
| 2741 | This option is intended to be used as a temporary means to build projects where |
| 2742 | clang-cl cannot successfully compile all the files. clang-cl may fail to compile |
| 2743 | a file either because it cannot generate code for some C++ feature, or because |
| 2744 | it cannot parse some Microsoft language extension. |