ClangFormat.rst 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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++/Java/JavaScript/Objective-C/Protobuf 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. -assume-filename=<string> - When reading from stdin, clang-format assumes this
  23. filename to look for a style config file (with
  24. -style=file) and to determine the language.
  25. -cursor=<uint> - The position of the cursor when invoking
  26. clang-format from an editor integration
  27. -dump-config - Dump configuration options to stdout and exit.
  28. Can be used with -style option.
  29. -fallback-style=<string> - The name of the predefined style used as a
  30. fallback in case clang-format is invoked with
  31. -style=file, but can not find the .clang-format
  32. file to use.
  33. Use -fallback-style=none to skip formatting.
  34. -i - Inplace edit <file>s, if specified.
  35. -length=<uint> - Format a range of this length (in bytes).
  36. Multiple ranges can be formatted by specifying
  37. several -offset and -length pairs.
  38. When only a single -offset is specified without
  39. -length, clang-format will format up to the end
  40. of the file.
  41. Can only be used with one input file.
  42. -lines=<string> - <start line>:<end line> - format a range of
  43. lines (both 1-based).
  44. Multiple ranges can be formatted by specifying
  45. several -lines arguments.
  46. Can't be used with -offset and -length.
  47. Can only be used with one input file.
  48. -offset=<uint> - Format a range starting at this byte offset.
  49. Multiple ranges can be formatted by specifying
  50. several -offset and -length pairs.
  51. Can only be used with one input file.
  52. -output-replacements-xml - Output replacements as XML.
  53. -sort-includes - Sort touched include lines
  54. -style=<string> - Coding style, currently supports:
  55. LLVM, Google, Chromium, Mozilla, WebKit.
  56. Use -style=file to load style configuration from
  57. .clang-format file located in one of the parent
  58. directories of the source file (or current
  59. directory for stdin).
  60. Use -style="{key: value, ...}" to set specific
  61. parameters, e.g.:
  62. -style="{BasedOnStyle: llvm, IndentWidth: 8}"
  63. -verbose - If set, shows the list of processed files
  64. Generic Options:
  65. -help - Display available options (-help-hidden for more)
  66. -help-list - Display list of available options (-help-list-hidden for more)
  67. -version - Display the version of this program
  68. When the desired code formatting style is different from the available options,
  69. the style can be customized using the ``-style="{key: value, ...}"`` option or
  70. by putting your style configuration in the ``.clang-format`` or ``_clang-format``
  71. file in your project's directory and using ``clang-format -style=file``.
  72. An easy way to create the ``.clang-format`` file is:
  73. .. code-block:: console
  74. clang-format -style=llvm -dump-config > .clang-format
  75. Available style options are described in :doc:`ClangFormatStyleOptions`.
  76. Vim Integration
  77. ===============
  78. There is an integration for :program:`vim` which lets you run the
  79. :program:`clang-format` standalone tool on your current buffer, optionally
  80. selecting regions to reformat. The integration has the form of a `python`-file
  81. which can be found under `clang/tools/clang-format/clang-format.py`.
  82. This can be integrated by adding the following to your `.vimrc`:
  83. .. code-block:: vim
  84. map <C-K> :pyf <path-to-this-file>/clang-format.py<cr>
  85. imap <C-K> <c-o>:pyf <path-to-this-file>/clang-format.py<cr>
  86. The first line enables :program:`clang-format` for NORMAL and VISUAL mode, the
  87. second line adds support for INSERT mode. Change "C-K" to another binding if
  88. you need :program:`clang-format` on a different key (C-K stands for Ctrl+k).
  89. With this integration you can press the bound key and clang-format will
  90. format the current line in NORMAL and INSERT mode or the selected region in
  91. VISUAL mode. The line or region is extended to the next bigger syntactic
  92. entity.
  93. It operates on the current, potentially unsaved buffer and does not create
  94. or save any files. To revert a formatting, just undo.
  95. An alternative option is to format changes when saving a file and thus to
  96. have a zero-effort integration into the coding workflow. To do this, add this to
  97. your `.vimrc`:
  98. .. code-block:: vim
  99. function! Formatonsave()
  100. let l:formatdiff = 1
  101. pyf ~/llvm/tools/clang/tools/clang-format/clang-format.py
  102. endfunction
  103. autocmd BufWritePre *.h,*.cc,*.cpp call Formatonsave()
  104. Emacs Integration
  105. =================
  106. Similar to the integration for :program:`vim`, there is an integration for
  107. :program:`emacs`. It can be found at `clang/tools/clang-format/clang-format.el`
  108. and used by adding this to your `.emacs`:
  109. .. code-block:: common-lisp
  110. (load "<path-to-clang>/tools/clang-format/clang-format.el")
  111. (global-set-key [C-M-tab] 'clang-format-region)
  112. This binds the function `clang-format-region` to C-M-tab, which then formats the
  113. current line or selected region.
  114. BBEdit Integration
  115. ==================
  116. :program:`clang-format` cannot be used as a text filter with BBEdit, but works
  117. well via a script. The AppleScript to do this integration can be found at
  118. `clang/tools/clang-format/clang-format-bbedit.applescript`; place a copy in
  119. `~/Library/Application Support/BBEdit/Scripts`, and edit the path within it to
  120. point to your local copy of :program:`clang-format`.
  121. With this integration you can select the script from the Script menu and
  122. :program:`clang-format` will format the selection. Note that you can rename the
  123. menu item by renaming the script, and can assign the menu item a keyboard
  124. shortcut in the BBEdit preferences, under Menus & Shortcuts.
  125. Visual Studio Integration
  126. =========================
  127. Download the latest Visual Studio extension from the `alpha build site
  128. <http://llvm.org/builds/>`_. The default key-binding is Ctrl-R,Ctrl-F.
  129. Script for patch reformatting
  130. =============================
  131. The python script `clang/tools/clang-format/clang-format-diff.py` parses the
  132. output of a unified diff and reformats all contained lines with
  133. :program:`clang-format`.
  134. .. code-block:: console
  135. usage: clang-format-diff.py [-h] [-i] [-p NUM] [-regex PATTERN] [-style STYLE]
  136. Reformat changed lines in diff. Without -i option just output the diff that
  137. would be introduced.
  138. optional arguments:
  139. -h, --help show this help message and exit
  140. -i apply edits to files instead of displaying a diff
  141. -p NUM strip the smallest prefix containing P slashes
  142. -regex PATTERN custom pattern selecting file paths to reformat
  143. -style STYLE formatting style to apply (LLVM, Google, Chromium, Mozilla,
  144. WebKit)
  145. So to reformat all the lines in the latest :program:`git` commit, just do:
  146. .. code-block:: console
  147. git diff -U0 --no-color HEAD^ | clang-format-diff.py -i -p1
  148. In an SVN client, you can do:
  149. .. code-block:: console
  150. svn diff --diff-cmd=diff -x -U0 | clang-format-diff.py -i
  151. The option `-U0` will create a diff without context lines (the script would format
  152. those as well).