ClangFormat.rst 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. ===========
  2. ClangFormat
  3. ===========
  4. `ClangFormat` describes a set of tools that are built on top of
  5. :doc:`LibFormat`. It can support your workflow in a variety of ways including a
  6. standalone tool and editor integrations.
  7. Standalone Tool
  8. ===============
  9. :program:`clang-format` is located in `clang/tools/clang-format` and can be used
  10. to format C/C++/Obj-C code.
  11. .. code-block:: console
  12. $ clang-format -help
  13. OVERVIEW: A tool to format C/C++/Obj-C code.
  14. If no arguments are specified, it formats the code from standard input
  15. and writes the result to the standard output.
  16. If <file>s are given, it reformats the files. If -i is specified
  17. together with <file>s, the files are edited in-place. Otherwise, the
  18. result is written to the standard output.
  19. USAGE: clang-format [options] [<file> ...]
  20. OPTIONS:
  21. Clang-format options:
  22. -cursor=<uint> - The position of the cursor when invoking
  23. clang-format from an editor integration
  24. -dump-config - Dump configuration options to stdout and exit.
  25. Can be used with -style option.
  26. -i - Inplace edit <file>s, if specified.
  27. -length=<uint> - Format a range of this length (in bytes).
  28. Multiple ranges can be formatted by specifying
  29. several -offset and -length pairs.
  30. When only a single -offset is specified without
  31. -length, clang-format will format up to the end
  32. of the file.
  33. Can only be used with one input file.
  34. -lines=<string> - <start line>:<end line> - format a range of
  35. lines (both 1-based).
  36. Multiple ranges can be formatted by specifying
  37. several -lines arguments.
  38. Can't be used with -offset and -length.
  39. Can only be used with one input file.
  40. -offset=<uint> - Format a range starting at this byte offset.
  41. Multiple ranges can be formatted by specifying
  42. several -offset and -length pairs.
  43. Can only be used with one input file.
  44. -output-replacements-xml - Output replacements as XML.
  45. -style=<string> - Coding style, currently supports:
  46. LLVM, Google, Chromium, Mozilla, WebKit.
  47. Use -style=file to load style configuration from
  48. .clang-format file located in one of the parent
  49. directories of the source file (or current
  50. directory for stdin).
  51. Use -style="{key: value, ...}" to set specific
  52. parameters, e.g.:
  53. -style="{BasedOnStyle: llvm, IndentWidth: 8}"
  54. General options:
  55. -help - Display available options (-help-hidden for more)
  56. -help-list - Display list of available options (-help-list-hidden for more)
  57. -version - Display the version of this program
  58. When the desired code formatting style is different from the available options,
  59. the style can be customized using the ``-style="{key: value, ...}"`` option or
  60. by putting your style configuration in the ``.clang-format`` or ``_clang-format``
  61. file in your project's directory and using ``clang-format -style=file``.
  62. An easy way to create the ``.clang-format`` file is:
  63. .. code-block:: console
  64. clang-format -style=llvm -dump-config > .clang-format
  65. Available style options are described in :doc:`ClangFormatStyleOptions`.
  66. Vim Integration
  67. ===============
  68. There is an integration for :program:`vim` which lets you run the
  69. :program:`clang-format` standalone tool on your current buffer, optionally
  70. selecting regions to reformat. The integration has the form of a `python`-file
  71. which can be found under `clang/tools/clang-format/clang-format.py`.
  72. This can be integrated by adding the following to your `.vimrc`:
  73. .. code-block:: vim
  74. map <C-K> :pyf <path-to-this-file>/clang-format.py<cr>
  75. imap <C-K> <c-o>:pyf <path-to-this-file>/clang-format.py<cr>
  76. The first line enables :program:`clang-format` for NORMAL and VISUAL mode, the
  77. second line adds support for INSERT mode. Change "C-K" to another binding if
  78. you need :program:`clang-format` on a different key (C-K stands for Ctrl+k).
  79. With this integration you can press the bound key and clang-format will
  80. format the current line in NORMAL and INSERT mode or the selected region in
  81. VISUAL mode. The line or region is extended to the next bigger syntactic
  82. entity.
  83. It operates on the current, potentially unsaved buffer and does not create
  84. or save any files. To revert a formatting, just undo.
  85. Emacs Integration
  86. =================
  87. Similar to the integration for :program:`vim`, there is an integration for
  88. :program:`emacs`. It can be found at `clang/tools/clang-format/clang-format.el`
  89. and used by adding this to your `.emacs`:
  90. .. code-block:: common-lisp
  91. (load "<path-to-clang>/tools/clang-format/clang-format.el")
  92. (global-set-key [C-M-tab] 'clang-format-region)
  93. This binds the function `clang-format-region` to C-M-tab, which then formats the
  94. current line or selected region.
  95. BBEdit Integration
  96. ==================
  97. :program:`clang-format` cannot be used as a text filter with BBEdit, but works
  98. well via a script. The AppleScript to do this integration can be found at
  99. `clang/tools/clang-format/clang-format-bbedit.applescript`; place a copy in
  100. `~/Library/Application Support/BBEdit/Scripts`, and edit the path within it to
  101. point to your local copy of :program:`clang-format`.
  102. With this integration you can select the script from the Script menu and
  103. :program:`clang-format` will format the selection. Note that you can rename the
  104. menu item by renaming the script, and can assign the menu item a keyboard
  105. shortcut in the BBEdit preferences, under Menus & Shortcuts.
  106. Visual Studio Integration
  107. =========================
  108. Download the latest Visual Studio extension from the `alpha build site
  109. <http://llvm.org/builds/>`_. The default key-binding is Ctrl-R,Ctrl-F.
  110. Script for patch reformatting
  111. =============================
  112. The python script `clang/tools/clang-format-diff.py` parses the output of
  113. a unified diff and reformats all contained lines with :program:`clang-format`.
  114. .. code-block:: console
  115. usage: clang-format-diff.py [-h] [-i] [-p NUM] [-regex PATTERN] [-style STYLE]
  116. Reformat changed lines in diff. Without -i option just output the diff that
  117. would be introduced.
  118. optional arguments:
  119. -h, --help show this help message and exit
  120. -i apply edits to files instead of displaying a diff
  121. -p NUM strip the smallest prefix containing P slashes
  122. -regex PATTERN custom pattern selecting file paths to reformat
  123. -style STYLE formatting style to apply (LLVM, Google, Chromium, Mozilla,
  124. WebKit)
  125. So to reformat all the lines in the latest :program:`git` commit, just do:
  126. .. code-block:: console
  127. git diff -U0 HEAD^ | clang-format-diff.py -i -p1
  128. In an SVN client, you can do:
  129. .. code-block:: console
  130. svn diff --diff-cmd=diff -x-U0 | clang-format-diff.py -i
  131. The :option:`-U0` will create a diff without context lines (the script would format
  132. those as well).