[M68k] always use movem for register spills (#106715)
[llvm-project.git] / clang / docs / CommandGuide / clang.rst
blobca8176f854729b5b18000d4d48c256473eeaaa31
1 clang - the Clang C, C++, and Objective-C compiler
2 ==================================================
4 SYNOPSIS
5 --------
7 :program:`clang` [*options*] *filename ...*
9 DESCRIPTION
10 -----------
12 :program:`clang` is a C, C++, and Objective-C compiler which encompasses
13 preprocessing, parsing, optimization, code generation, assembly, and linking.
14 Depending on which high-level mode setting is passed, Clang will stop before
15 doing a full link.  While Clang is highly integrated, it is important to
16 understand the stages of compilation, to understand how to invoke it.  These
17 stages are:
19 Driver
20     The clang executable is actually a small driver which controls the overall
21     execution of other tools such as the compiler, assembler and linker.
22     Typically you do not need to interact with the driver, but you
23     transparently use it to run the other tools.
25 Preprocessing
26     This stage handles tokenization of the input source file, macro expansion,
27     #include expansion and handling of other preprocessor directives.  The
28     output of this stage is typically called a ".i" (for C), ".ii" (for C++),
29     ".mi" (for Objective-C), or ".mii" (for Objective-C++) file.
31 Parsing and Semantic Analysis
32     This stage parses the input file, translating preprocessor tokens into a
33     parse tree.  Once in the form of a parse tree, it applies semantic
34     analysis to compute types for expressions as well and determine whether
35     the code is well formed. This stage is responsible for generating most of
36     the compiler warnings as well as parse errors. The output of this stage is
37     an "Abstract Syntax Tree" (AST).
39 Code Generation and Optimization
40     This stage translates an AST into low-level intermediate code (known as
41     "LLVM IR") and ultimately to machine code.  This phase is responsible for
42     optimizing the generated code and handling target-specific code generation.
43     The output of this stage is typically called a ".s" file or "assembly" file.
45     Clang also supports the use of an integrated assembler, in which the code
46     generator produces object files directly. This avoids the overhead of
47     generating the ".s" file and of calling the target assembler.
49 Assembler
50     This stage runs the target assembler to translate the output of the
51     compiler into a target object file. The output of this stage is typically
52     called a ".o" file or "object" file.
54 Linker
55     This stage runs the target linker to merge multiple object files into an
56     executable or dynamic library. The output of this stage is typically called
57     an "a.out", ".dylib" or ".so" file.
59 :program:`Clang Static Analyzer`
61 The Clang Static Analyzer is a tool that scans source code to try to find bugs
62 through code analysis.  This tool uses many parts of Clang and is built into
63 the same driver.  Please see <https://clang-analyzer.llvm.org> for more details
64 on how to use the static analyzer.
66 OPTIONS
67 -------
69 Stage Selection Options
70 ~~~~~~~~~~~~~~~~~~~~~~~
72 .. option:: -E
74  Run the preprocessor stage.
76 .. option:: -fsyntax-only
78  Run the preprocessor, parser and semantic analysis stages.
80 .. option:: -S
82  Run the previous stages as well as LLVM generation and optimization stages
83  and target-specific code generation, producing an assembly file.
85 .. option:: -c
87  Run all of the above, plus the assembler, generating a target ".o" object file.
89 .. option:: no stage selection option
91  If no stage selection option is specified, all stages above are run, and the
92  linker is run to combine the results into an executable or shared library.
94 Language Selection and Mode Options
95 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
97 .. option:: -x <language>
99  Treat subsequent input files as having type language.
101 .. option:: -std=<standard>
103  Specify the language standard to compile for.
105  Supported values for the C language are:
107   | ``c89``
108   | ``c90``
109   | ``iso9899:1990``
111    ISO C 1990
113   | ``iso9899:199409``
115    ISO C 1990 with amendment 1
117   | ``gnu89``
118   | ``gnu90``
120    ISO C 1990 with GNU extensions
122   | ``c99``
123   | ``iso9899:1999``
125    ISO C 1999
127   | ``gnu99``
129    ISO C 1999 with GNU extensions
131   | ``c11``
132   | ``iso9899:2011``
134    ISO C 2011
136   | ``gnu11``
138    ISO C 2011 with GNU extensions
140   | ``c17``
141   | ``iso9899:2017``
143    ISO C 2017
145   | ``gnu17``
147    ISO C 2017 with GNU extensions
149  The default C language standard is ``gnu17``, except on PS4, where it is
150  ``gnu99``.
152  Supported values for the C++ language are:
154   | ``c++98``
155   | ``c++03``
157    ISO C++ 1998 with amendments
159   | ``gnu++98``
160   | ``gnu++03``
162    ISO C++ 1998 with amendments and GNU extensions
164   | ``c++11``
166    ISO C++ 2011 with amendments
168   | ``gnu++11``
170     ISO C++ 2011 with amendments and GNU extensions
172   | ``c++14``
174    ISO C++ 2014 with amendments
176   | ``gnu++14``
178    ISO C++ 2014 with amendments and GNU extensions
180   | ``c++17``
182    ISO C++ 2017 with amendments
184   | ``gnu++17``
186    ISO C++ 2017 with amendments and GNU extensions
188   | ``c++20``
190    ISO C++ 2020 with amendments
192   | ``gnu++20``
194    ISO C++ 2020 with amendments and GNU extensions
196   | ``c++23``
198    ISO C++ 2023 with amendments
200   | ``gnu++23``
202    ISO C++ 2023 with amendments and GNU extensions
204   | ``c++2c``
206    Working draft for C++2c
208   | ``gnu++2c``
210    Working draft for C++2c with GNU extensions
212  The default C++ language standard is ``gnu++17``.
214  Supported values for the OpenCL language are:
216   | ``cl1.0``
218    OpenCL 1.0
220   | ``cl1.1``
222    OpenCL 1.1
224   | ``cl1.2``
226    OpenCL 1.2
228   | ``cl2.0``
230    OpenCL 2.0
232  The default OpenCL language standard is ``cl1.0``.
234  Supported values for the CUDA language are:
236   | ``cuda``
238    NVIDIA CUDA(tm)
240 .. option:: -stdlib=<library>
242  Specify the C++ standard library to use; supported options are libstdc++ and
243  libc++. If not specified, platform default will be used.
245 .. option:: -rtlib=<library>
247  Specify the compiler runtime library to use; supported options are libgcc and
248  compiler-rt. If not specified, platform default will be used.
250 .. option:: -ansi
252  Same as -std=c89.
254 .. option:: -ObjC, -ObjC++
256  Treat source input files as Objective-C and Object-C++ inputs respectively.
258 .. option:: -trigraphs
260  Enable trigraphs.
262 .. option:: -ffreestanding
264  Indicate that the file should be compiled for a freestanding, not a hosted,
265  environment. Note that it is assumed that a freestanding environment will
266  additionally provide `memcpy`, `memmove`, `memset` and `memcmp`
267  implementations, as these are needed for efficient codegen for many programs.
269 .. option:: -fno-builtin
271  Disable special handling and optimizations of well-known library functions,
272  like :c:func:`strlen` and :c:func:`malloc`.
274 .. option:: -fno-builtin-<function>
276  Disable special handling and optimizations for the specific library function.
277  For example, ``-fno-builtin-strlen`` removes any special handling for the
278  :c:func:`strlen` library function.
280 .. option:: -fno-builtin-std-<function>
282  Disable special handling and optimizations for the specific C++ standard
283  library function in namespace ``std``. For example,
284  ``-fno-builtin-std-move_if_noexcept`` removes any special handling for the
285  :cpp:func:`std::move_if_noexcept` library function.
287  For C standard library functions that the C++ standard library also provides
288  in namespace ``std``, use :option:`-fno-builtin-\<function\>` instead.
290 .. option:: -fmath-errno
292  Indicate that math functions should be treated as updating :c:data:`errno`.
294 .. option:: -fpascal-strings
296  Enable support for Pascal-style strings with "\\pfoo".
298 .. option:: -fms-extensions
300  Enable support for Microsoft extensions.
302 .. option:: -fmsc-version=
304  Set ``_MSC_VER``. When on Windows, this defaults to either the same value as
305  the currently installed version of cl.exe, or ``1933``. Not set otherwise.
307 .. option:: -fborland-extensions
309  Enable support for Borland extensions.
311 .. option:: -fwritable-strings
313  Make all string literals default to writable.  This disables uniquing of
314  strings and other optimizations.
316 .. option:: -flax-vector-conversions, -flax-vector-conversions=<kind>, -fno-lax-vector-conversions
318  Allow loose type checking rules for implicit vector conversions.
319  Possible values of <kind>:
321  - ``none``: allow no implicit conversions between vectors
322  - ``integer``: allow implicit bitcasts between integer vectors of the same
323    overall bit-width
324  - ``all``: allow implicit bitcasts between any vectors of the same
325    overall bit-width
327  <kind> defaults to ``integer`` if unspecified.
329 .. option:: -fblocks
331  Enable the "Blocks" language feature.
333 .. option:: -fobjc-abi-version=version
335  Select the Objective-C ABI version to use. Available versions are 1 (legacy
336  "fragile" ABI), 2 (non-fragile ABI 1), and 3 (non-fragile ABI 2).
338 .. option:: -fobjc-nonfragile-abi-version=<version>
340  Select the Objective-C non-fragile ABI version to use by default. This will
341  only be used as the Objective-C ABI when the non-fragile ABI is enabled
342  (either via :option:`-fobjc-nonfragile-abi`, or because it is the platform
343  default).
345 .. option:: -fobjc-nonfragile-abi, -fno-objc-nonfragile-abi
347  Enable use of the Objective-C non-fragile ABI. On platforms for which this is
348  the default ABI, it can be disabled with :option:`-fno-objc-nonfragile-abi`.
350 Target Selection Options
351 ~~~~~~~~~~~~~~~~~~~~~~~~
353 Clang fully supports cross compilation as an inherent part of its design.
354 Depending on how your version of Clang is configured, it may have support for a
355 number of cross compilers, or may only support a native target.
357 .. option:: -arch <architecture>
359   Specify the architecture to build for (Mac OS X specific).
361 .. option:: -target <architecture>
363   Specify the architecture to build for (all platforms).
365 .. option:: -mmacos-version-min=<version>
367   When building for macOS, specify the minimum version supported by your
368   application.
370 .. option:: -miphoneos-version-min
372   When building for iPhone OS, specify the minimum version supported by your
373   application.
375 .. option:: --print-supported-cpus
377   Print out a list of supported processors for the given target (specified
378   through ``--target=<architecture>`` or :option:`-arch` ``<architecture>``). If no
379   target is specified, the system default target will be used.
381 .. option:: -mcpu=?, -mtune=?
383   Acts as an alias for :option:`--print-supported-cpus`.
385 .. option:: -mcpu=help, -mtune=help
387   Acts as an alias for :option:`--print-supported-cpus`.
389 .. option:: -march=<cpu>
391   Specify that Clang should generate code for a specific processor family
392   member and later.  For example, if you specify -march=i486, the compiler is
393   allowed to generate instructions that are valid on i486 and later processors,
394   but which may not exist on earlier ones.
396 .. option:: --print-enabled-extensions
398   Prints the list of extensions that are enabled for the target specified by the
399   combination of `--target`, `-march`, and `-mcpu` values. Currently, this
400   option is only supported on AArch64 and RISC-V. On RISC-V, this option also
401   prints out the ISA string of enabled extensions.
403 .. option:: --print-supported-extensions
405   Prints the list of all extensions that are supported for every CPU target
406   for an architecture (specified through ``--target=<architecture>`` or
407   :option:`-arch` ``<architecture>``). If no target is specified, the system
408   default target will be used. Currently, this option is only supported on
409   AArch64 and RISC-V.
411 Code Generation Options
412 ~~~~~~~~~~~~~~~~~~~~~~~
414 .. option:: -O0, -O1, -O2, -O3, -Ofast, -Os, -Oz, -Og, -O, -O4
416   Specify which optimization level to use:
418     :option:`-O0` Means "no optimization": this level compiles the fastest and
419     generates the most debuggable code.
421     :option:`-O1` Somewhere between :option:`-O0` and :option:`-O2`.
423     :option:`-O2` Moderate level of optimization which enables most
424     optimizations.
426     :option:`-O3` Like :option:`-O2`, except that it enables optimizations that
427     take longer to perform or that may generate larger code (in an attempt to
428     make the program run faster).
430     :option:`-Ofast` Enables all the optimizations from :option:`-O3` along
431     with other aggressive optimizations that may violate strict compliance with
432     language standards. This is deprecated in Clang 19 and a warning is emitted
433     that :option:`-O3` in combination with :option:`-ffast-math` should be used
434     instead if the request for non-standard math behavior is intended. There
435     is no timeline yet for removal; the aim is to discourage use of
436     :option:`-Ofast` due to the surprising behavior of an optimization flag
437     changing the observable behavior of correct code.
439     :option:`-Os` Like :option:`-O2` with extra optimizations to reduce code
440     size.
442     :option:`-Oz` Like :option:`-Os` (and thus :option:`-O2`), but reduces code
443     size further.
445     :option:`-Og` Like :option:`-O1`. In future versions, this option might
446     disable different optimizations in order to improve debuggability.
448     :option:`-O` Equivalent to :option:`-O1`.
450     :option:`-O4` and higher
452       Currently equivalent to :option:`-O3`
454 .. option:: -g, -gline-tables-only, -gmodules
456   Control debug information output.  Note that Clang debug information works
457   best at :option:`-O0`.  When more than one option starting with `-g` is
458   specified, the last one wins:
460     :option:`-g` Generate debug information.
462     :option:`-gline-tables-only` Generate only line table debug information. This
463     allows for symbolicated backtraces with inlining information, but does not
464     include any information about variables, their locations or types.
466     :option:`-gmodules` Generate debug information that contains external
467     references to types defined in Clang modules or precompiled headers instead
468     of emitting redundant debug type information into every object file.  This
469     option transparently switches the Clang module format to object file
470     containers that hold the Clang module together with the debug information.
471     When compiling a program that uses Clang modules or precompiled headers,
472     this option produces complete debug information with faster compile
473     times and much smaller object files.
475     This option should not be used when building static libraries for
476     distribution to other machines because the debug info will contain
477     references to the module cache on the machine the object files in the
478     library were built on.
480 .. option:: -fstandalone-debug -fno-standalone-debug
482   Clang supports a number of optimizations to reduce the size of debug
483   information in the binary. They work based on the assumption that the
484   debug type information can be spread out over multiple compilation units.
485   For instance, Clang will not emit type definitions for types that are not
486   needed by a module and could be replaced with a forward declaration.
487   Further, Clang will only emit type info for a dynamic C++ class in the
488   module that contains the vtable for the class.
490   The :option:`-fstandalone-debug` option turns off these optimizations.
491   This is useful when working with 3rd-party libraries that don't come with
492   debug information.  This is the default on Darwin.  Note that Clang will
493   never emit type information for types that are not referenced at all by the
494   program.
496 .. option:: -feliminate-unused-debug-types
498   By default, Clang does not emit type information for types that are defined
499   but not used in a program. To retain the debug info for these unused types,
500   the negation **-fno-eliminate-unused-debug-types** can be used.
502 .. option:: -fexceptions
504   Allow exceptions to be thrown through Clang compiled stack frames (on many
505   targets, this will enable unwind information for functions that might have
506   an exception thrown through them). For most targets, this is enabled by
507   default for C++.
509 .. option:: -ftrapv
511   Generate code to catch integer overflow errors.  Signed integer overflow is
512   undefined in C. With this flag, extra code is generated to detect this and
513   abort when it happens.
515 .. option:: -fvisibility
517   This flag sets the default visibility level.
519 .. option:: -fcommon, -fno-common
521   This flag specifies that variables without initializers get common linkage.
522   It can be disabled with :option:`-fno-common`.
524 .. option:: -ftls-model=<model>
526   Set the default thread-local storage (TLS) model to use for thread-local
527   variables. Valid values are: "global-dynamic", "local-dynamic",
528   "initial-exec" and "local-exec". The default is "global-dynamic". The default
529   model can be overridden with the tls_model attribute. The compiler will try
530   to choose a more efficient model if possible.
532 .. option:: -flto, -flto=full, -flto=thin, -emit-llvm
534   Generate output files in LLVM formats, suitable for link time optimization.
535   When used with :option:`-S` this generates LLVM intermediate language
536   assembly files, otherwise this generates LLVM bitcode format object files
537   (which may be passed to the linker depending on the stage selection options).
539   The default for :option:`-flto` is "full", in which the
540   LLVM bitcode is suitable for monolithic Link Time Optimization (LTO), where
541   the linker merges all such modules into a single combined module for
542   optimization. With "thin", :doc:`ThinLTO <../ThinLTO>`
543   compilation is invoked instead.
545   .. note::
547      On Darwin, when using :option:`-flto` along with :option:`-g` and
548      compiling and linking in separate steps, you also need to pass
549      ``-Wl,-object_path_lto,<lto-filename>.o`` at the linking step to instruct the
550      ld64 linker not to delete the temporary object file generated during Link
551      Time Optimization (this flag is automatically passed to the linker by Clang
552      if compilation and linking are done in a single step). This allows debugging
553      the executable as well as generating the ``.dSYM`` bundle using :manpage:`dsymutil(1)`.
555 Driver Options
556 ~~~~~~~~~~~~~~
558 .. option:: -###
560   Print (but do not run) the commands to run for this compilation.
562 .. option:: --help
564   Display available options.
566 .. option:: -Qunused-arguments
568   Do not emit any warnings for unused driver arguments.
570 .. option:: -Wa,<args>
572   Pass the comma separated arguments in args to the assembler.
574 .. option:: -Wl,<args>
576   Pass the comma separated arguments in args to the linker.
578 .. option:: -Wp,<args>
580   Pass the comma separated arguments in args to the preprocessor.
582 .. option:: -Xanalyzer <arg>
584   Pass arg to the static analyzer.
586 .. option:: -Xassembler <arg>
588   Pass arg to the assembler.
590 .. option:: -Xlinker <arg>
592   Pass arg to the linker.
594 .. option:: -Xpreprocessor <arg>
596   Pass arg to the preprocessor.
598 .. option:: -o <file>
600   Write output to file.
602 .. option:: -print-file-name=<file>
604   Print the full library path of file.
606 .. option:: -print-libgcc-file-name
608   Print the library path for the currently used compiler runtime library
609   ("libgcc.a" or "libclang_rt.builtins.*.a").
611 .. option:: -print-prog-name=<name>
613   Print the full program path of name.
615 .. option:: -print-search-dirs
617   Print the paths used for finding libraries and programs.
619 .. option:: -save-temps
621   Save intermediate compilation results.
623 .. option:: -save-stats, -save-stats=cwd, -save-stats=obj
625   Save internal code generation (LLVM) statistics to a file in the current
626   directory (:option:`-save-stats`/"-save-stats=cwd") or the directory
627   of the output file ("-save-state=obj").
629   You can also use environment variables to control the statistics reporting.
630   Setting ``CC_PRINT_INTERNAL_STAT`` to ``1`` enables the feature, the report
631   goes to stdout in JSON format.
633   Setting ``CC_PRINT_INTERNAL_STAT_FILE`` to a file path makes it report
634   statistics to the given file in the JSON format.
636   Note that ``-save-stats`` take precedence over ``CC_PRINT_INTERNAL_STAT``
637   and ``CC_PRINT_INTERNAL_STAT_FILE``.
639 .. option:: -integrated-as, -no-integrated-as
641   Used to enable and disable, respectively, the use of the integrated
642   assembler. Whether the integrated assembler is on by default is target
643   dependent.
645 .. option:: -time
647   Time individual commands.
649 .. option:: -ftime-report
651   Print timing summary of each stage of compilation.
653 .. option:: -v
655   Show commands to run and use verbose output.
658 Diagnostics Options
659 ~~~~~~~~~~~~~~~~~~~
661 .. option:: -fshow-column, -fshow-source-location, -fcaret-diagnostics, -fdiagnostics-fixit-info, -fdiagnostics-parseable-fixits, -fdiagnostics-print-source-range-info, -fprint-source-range-info, -fdiagnostics-show-option, -fmessage-length
663   These options control how Clang prints out information about diagnostics
664   (errors and warnings). Please see the Clang User's Manual for more information.
666 Preprocessor Options
667 ~~~~~~~~~~~~~~~~~~~~
669 .. option:: -D<macroname>=<value>
671   Adds an implicit #define into the predefines buffer which is read before the
672   source file is preprocessed.
674 .. option:: -U<macroname>
676   Adds an implicit #undef into the predefines buffer which is read before the
677   source file is preprocessed.
679 .. option:: -include <filename>
681   Adds an implicit #include into the predefines buffer which is read before the
682   source file is preprocessed.
684 .. option:: -I<directory>
686   Add the specified directory to the search path for include files.
688 .. option:: -F<directory>
690   Add the specified directory to the search path for framework include files.
692 .. option:: -nostdinc
694   Do not search the standard system directories or compiler builtin directories
695   for include files.
697 .. option:: -nostdlibinc
699   Do not search the standard system directories for include files, but do
700   search compiler builtin include directories.
702 .. option:: -nobuiltininc
704   Do not search clang's builtin directory for include files.
706 .. option:: -nostdinc++
708   Do not search the system C++ standard library directory for include files.
710 .. option:: -fkeep-system-includes
712   Usable only with :option:`-E`. Do not copy the preprocessed content of
713   "system" headers to the output; instead, preserve the #include directive.
714   This can greatly reduce the volume of text produced by :option:`-E` which
715   can be helpful when trying to produce a "small" reproduceable test case.
717   This option does not guarantee reproduceability, however. If the including
718   source defines preprocessor symbols that influence the behavior of system
719   headers (for example, ``_XOPEN_SOURCE``) the operation of :option:`-E` will
720   remove that definition and thus can change the semantics of the included
721   header. Also, using a different version of the system headers (especially a
722   different version of the STL) may result in different behavior. Always verify
723   the preprocessed file by compiling it separately.
726 ENVIRONMENT
727 -----------
729 .. envvar:: TMPDIR, TEMP, TMP
731   These environment variables are checked, in order, for the location to write
732   temporary files used during the compilation process.
734 .. envvar:: CPATH
736   If this environment variable is present, it is treated as a delimited list of
737   paths to be added to the default system include path list. The delimiter is
738   the platform dependent delimiter, as used in the PATH environment variable.
740   Empty components in the environment variable are ignored.
742 .. envvar:: C_INCLUDE_PATH, OBJC_INCLUDE_PATH, CPLUS_INCLUDE_PATH, OBJCPLUS_INCLUDE_PATH
744   These environment variables specify additional paths, as for :envvar:`CPATH`, which are
745   only used when processing the appropriate language.
747 .. envvar:: MACOSX_DEPLOYMENT_TARGET
749   If :option:`-mmacos-version-min` is unspecified, the default deployment
750   target is read from this environment variable. This option only affects
751   Darwin targets.
753 BUGS
754 ----
756 To report bugs, please visit <https://github.com/llvm/llvm-project/issues/>.  Most bug reports should
757 include preprocessed source files (use the :option:`-E` option) and the full
758 output of the compiler, along with information to reproduce.
760 SEE ALSO
761 --------
763 :manpage:`as(1)`, :manpage:`ld(1)`