Daniel Jasper | 85a77c1 | 2013-01-09 21:49:28 +0000 | [diff] [blame] | 1 | =========== |
| 2 | ClangFormat |
| 3 | =========== |
| 4 | |
| 5 | `ClangFormat` describes a set of tools that are built on top of |
| 6 | :doc:`LibFormat`. It can support your workflow in a variety of ways including a |
| 7 | standalone tool and editor integrations. |
| 8 | |
| 9 | |
| 10 | Standalone Tool |
| 11 | =============== |
| 12 | |
Daniel Jasper | 4172375 | 2013-03-22 12:44:20 +0000 | [diff] [blame] | 13 | :program:`clang-format` is located in `clang/tools/clang-format` and can be used |
Malcolm Parsons | 72e5d69 | 2017-11-22 10:47:35 +0000 | [diff] [blame] | 14 | to format C/C++/Java/JavaScript/Objective-C/Protobuf code. |
Daniel Jasper | 85a77c1 | 2013-01-09 21:49:28 +0000 | [diff] [blame] | 15 | |
| 16 | .. code-block:: console |
| 17 | |
Alexander Kornienko | d83adf3 | 2013-09-02 15:30:26 +0000 | [diff] [blame] | 18 | $ clang-format -help |
Nico Weber | 187fcf1 | 2015-10-19 01:08:30 +0000 | [diff] [blame] | 19 | OVERVIEW: A tool to format C/C++/Java/JavaScript/Objective-C/Protobuf code. |
Daniel Jasper | 85a77c1 | 2013-01-09 21:49:28 +0000 | [diff] [blame] | 20 | |
Daniel Jasper | 85a77c1 | 2013-01-09 21:49:28 +0000 | [diff] [blame] | 21 | If no arguments are specified, it formats the code from standard input |
| 22 | and writes the result to the standard output. |
Alexander Kornienko | d83adf3 | 2013-09-02 15:30:26 +0000 | [diff] [blame] | 23 | If <file>s are given, it reformats the files. If -i is specified |
| 24 | together with <file>s, the files are edited in-place. Otherwise, the |
Alexander Kornienko | 88a0d93 | 2013-05-10 18:12:00 +0000 | [diff] [blame] | 25 | result is written to the standard output. |
Daniel Jasper | 85a77c1 | 2013-01-09 21:49:28 +0000 | [diff] [blame] | 26 | |
Alexander Kornienko | 88a0d93 | 2013-05-10 18:12:00 +0000 | [diff] [blame] | 27 | USAGE: clang-format [options] [<file> ...] |
Daniel Jasper | 85a77c1 | 2013-01-09 21:49:28 +0000 | [diff] [blame] | 28 | |
| 29 | OPTIONS: |
Alexander Kornienko | 88a0d93 | 2013-05-10 18:12:00 +0000 | [diff] [blame] | 30 | |
| 31 | Clang-format options: |
| 32 | |
Nico Weber | 187fcf1 | 2015-10-19 01:08:30 +0000 | [diff] [blame] | 33 | -assume-filename=<string> - When reading from stdin, clang-format assumes this |
| 34 | filename to look for a style config file (with |
| 35 | -style=file) and to determine the language. |
| 36 | -cursor=<uint> - The position of the cursor when invoking |
| 37 | clang-format from an editor integration |
| 38 | -dump-config - Dump configuration options to stdout and exit. |
| 39 | Can be used with -style option. |
| 40 | -fallback-style=<string> - The name of the predefined style used as a |
| 41 | fallback in case clang-format is invoked with |
| 42 | -style=file, but can not find the .clang-format |
| 43 | file to use. |
| 44 | Use -fallback-style=none to skip formatting. |
| 45 | -i - Inplace edit <file>s, if specified. |
| 46 | -length=<uint> - Format a range of this length (in bytes). |
| 47 | Multiple ranges can be formatted by specifying |
| 48 | several -offset and -length pairs. |
| 49 | When only a single -offset is specified without |
| 50 | -length, clang-format will format up to the end |
| 51 | of the file. |
| 52 | Can only be used with one input file. |
| 53 | -lines=<string> - <start line>:<end line> - format a range of |
| 54 | lines (both 1-based). |
| 55 | Multiple ranges can be formatted by specifying |
| 56 | several -lines arguments. |
| 57 | Can't be used with -offset and -length. |
| 58 | Can only be used with one input file. |
| 59 | -offset=<uint> - Format a range starting at this byte offset. |
| 60 | Multiple ranges can be formatted by specifying |
| 61 | several -offset and -length pairs. |
| 62 | Can only be used with one input file. |
| 63 | -output-replacements-xml - Output replacements as XML. |
| 64 | -sort-includes - Sort touched include lines |
| 65 | -style=<string> - Coding style, currently supports: |
| 66 | LLVM, Google, Chromium, Mozilla, WebKit. |
| 67 | Use -style=file to load style configuration from |
| 68 | .clang-format file located in one of the parent |
| 69 | directories of the source file (or current |
| 70 | directory for stdin). |
| 71 | Use -style="{key: value, ...}" to set specific |
| 72 | parameters, e.g.: |
| 73 | -style="{BasedOnStyle: llvm, IndentWidth: 8}" |
Sylvestre Ledru | d23dd6c | 2017-08-12 15:15:10 +0000 | [diff] [blame] | 74 | -verbose - If set, shows the list of processed files |
Alexander Kornienko | 88a0d93 | 2013-05-10 18:12:00 +0000 | [diff] [blame] | 75 | |
Nico Weber | 187fcf1 | 2015-10-19 01:08:30 +0000 | [diff] [blame] | 76 | Generic Options: |
Alexander Kornienko | 88a0d93 | 2013-05-10 18:12:00 +0000 | [diff] [blame] | 77 | |
Nico Weber | 187fcf1 | 2015-10-19 01:08:30 +0000 | [diff] [blame] | 78 | -help - Display available options (-help-hidden for more) |
| 79 | -help-list - Display list of available options (-help-list-hidden for more) |
| 80 | -version - Display the version of this program |
Daniel Jasper | 85a77c1 | 2013-01-09 21:49:28 +0000 | [diff] [blame] | 81 | |
| 82 | |
Alexander Kornienko | d278e0e | 2013-09-04 15:09:13 +0000 | [diff] [blame] | 83 | When the desired code formatting style is different from the available options, |
| 84 | the style can be customized using the ``-style="{key: value, ...}"`` option or |
Hans Wennborg | 9f6581b | 2013-09-10 15:41:12 +0000 | [diff] [blame] | 85 | by putting your style configuration in the ``.clang-format`` or ``_clang-format`` |
| 86 | file in your project's directory and using ``clang-format -style=file``. |
Alexander Kornienko | d278e0e | 2013-09-04 15:09:13 +0000 | [diff] [blame] | 87 | |
| 88 | An easy way to create the ``.clang-format`` file is: |
| 89 | |
| 90 | .. code-block:: console |
| 91 | |
| 92 | clang-format -style=llvm -dump-config > .clang-format |
| 93 | |
| 94 | Available style options are described in :doc:`ClangFormatStyleOptions`. |
| 95 | |
| 96 | |
Daniel Jasper | 85a77c1 | 2013-01-09 21:49:28 +0000 | [diff] [blame] | 97 | Vim Integration |
| 98 | =============== |
| 99 | |
Dmitri Gribenko | cb9ede9 | 2013-01-09 22:18:55 +0000 | [diff] [blame] | 100 | There is an integration for :program:`vim` which lets you run the |
| 101 | :program:`clang-format` standalone tool on your current buffer, optionally |
Hans Wennborg | c32d513 | 2013-02-28 18:16:24 +0000 | [diff] [blame] | 102 | selecting regions to reformat. The integration has the form of a `python`-file |
Daniel Jasper | 4172375 | 2013-03-22 12:44:20 +0000 | [diff] [blame] | 103 | which can be found under `clang/tools/clang-format/clang-format.py`. |
Daniel Jasper | 85a77c1 | 2013-01-09 21:49:28 +0000 | [diff] [blame] | 104 | |
Hans Wennborg | c32d513 | 2013-02-28 18:16:24 +0000 | [diff] [blame] | 105 | This can be integrated by adding the following to your `.vimrc`: |
Daniel Jasper | 85a77c1 | 2013-01-09 21:49:28 +0000 | [diff] [blame] | 106 | |
Sean Silva | edd5ca5 | 2013-03-03 15:17:35 +0000 | [diff] [blame] | 107 | .. code-block:: vim |
Daniel Jasper | 85a77c1 | 2013-01-09 21:49:28 +0000 | [diff] [blame] | 108 | |
Saleem Abdulrasool | 8bbed0b | 2014-11-02 21:27:52 +0000 | [diff] [blame] | 109 | map <C-K> :pyf <path-to-this-file>/clang-format.py<cr> |
| 110 | imap <C-K> <c-o>:pyf <path-to-this-file>/clang-format.py<cr> |
Daniel Jasper | 85a77c1 | 2013-01-09 21:49:28 +0000 | [diff] [blame] | 111 | |
Dmitri Gribenko | cb9ede9 | 2013-01-09 22:18:55 +0000 | [diff] [blame] | 112 | The first line enables :program:`clang-format` for NORMAL and VISUAL mode, the |
Daniel Jasper | 4172375 | 2013-03-22 12:44:20 +0000 | [diff] [blame] | 113 | second line adds support for INSERT mode. Change "C-K" to another binding if |
| 114 | you need :program:`clang-format` on a different key (C-K stands for Ctrl+k). |
Daniel Jasper | 85a77c1 | 2013-01-09 21:49:28 +0000 | [diff] [blame] | 115 | |
| 116 | With this integration you can press the bound key and clang-format will |
| 117 | format the current line in NORMAL and INSERT mode or the selected region in |
| 118 | VISUAL mode. The line or region is extended to the next bigger syntactic |
| 119 | entity. |
| 120 | |
| 121 | It operates on the current, potentially unsaved buffer and does not create |
| 122 | or save any files. To revert a formatting, just undo. |
| 123 | |
Daniel Jasper | 271dd06 | 2017-06-19 07:30:04 +0000 | [diff] [blame] | 124 | An alternative option is to format changes when saving a file and thus to |
| 125 | have a zero-effort integration into the coding workflow. To do this, add this to |
| 126 | your `.vimrc`: |
| 127 | |
| 128 | .. code-block:: vim |
| 129 | |
| 130 | function! Formatonsave() |
| 131 | let l:formatdiff = 1 |
| 132 | pyf ~/llvm/tools/clang/tools/clang-format/clang-format.py |
| 133 | endfunction |
| 134 | autocmd BufWritePre *.h,*.cc,*.cpp call Formatonsave() |
| 135 | |
Daniel Jasper | 85a77c1 | 2013-01-09 21:49:28 +0000 | [diff] [blame] | 136 | |
Daniel Jasper | dd77743 | 2013-04-17 07:55:02 +0000 | [diff] [blame] | 137 | Emacs Integration |
| 138 | ================= |
| 139 | |
| 140 | Similar to the integration for :program:`vim`, there is an integration for |
| 141 | :program:`emacs`. It can be found at `clang/tools/clang-format/clang-format.el` |
| 142 | and used by adding this to your `.emacs`: |
| 143 | |
| 144 | .. code-block:: common-lisp |
| 145 | |
| 146 | (load "<path-to-clang>/tools/clang-format/clang-format.el") |
| 147 | (global-set-key [C-M-tab] 'clang-format-region) |
| 148 | |
| 149 | This binds the function `clang-format-region` to C-M-tab, which then formats the |
| 150 | current line or selected region. |
| 151 | |
| 152 | |
Daniel Jasper | 5f99742 | 2013-05-02 17:37:36 +0000 | [diff] [blame] | 153 | BBEdit Integration |
| 154 | ================== |
| 155 | |
| 156 | :program:`clang-format` cannot be used as a text filter with BBEdit, but works |
| 157 | well via a script. The AppleScript to do this integration can be found at |
| 158 | `clang/tools/clang-format/clang-format-bbedit.applescript`; place a copy in |
| 159 | `~/Library/Application Support/BBEdit/Scripts`, and edit the path within it to |
| 160 | point to your local copy of :program:`clang-format`. |
| 161 | |
| 162 | With this integration you can select the script from the Script menu and |
| 163 | :program:`clang-format` will format the selection. Note that you can rename the |
| 164 | menu item by renaming the script, and can assign the menu item a keyboard |
| 165 | shortcut in the BBEdit preferences, under Menus & Shortcuts. |
| 166 | |
| 167 | |
Manuel Klimek | 7491eaa | 2013-09-29 17:49:50 +0000 | [diff] [blame] | 168 | Visual Studio Integration |
| 169 | ========================= |
| 170 | |
Alp Toker | 0e7f6da | 2013-12-04 00:48:22 +0000 | [diff] [blame] | 171 | Download the latest Visual Studio extension from the `alpha build site |
Manuel Klimek | 7491eaa | 2013-09-29 17:49:50 +0000 | [diff] [blame] | 172 | <http://llvm.org/builds/>`_. The default key-binding is Ctrl-R,Ctrl-F. |
| 173 | |
| 174 | |
Daniel Jasper | 85a77c1 | 2013-01-09 21:49:28 +0000 | [diff] [blame] | 175 | Script for patch reformatting |
| 176 | ============================= |
| 177 | |
Craig Topper | cadadab | 2017-04-24 17:39:35 +0000 | [diff] [blame] | 178 | The python script `clang/tools/clang-format/clang-format-diff.py` parses the |
| 179 | output of a unified diff and reformats all contained lines with |
| 180 | :program:`clang-format`. |
Daniel Jasper | 85a77c1 | 2013-01-09 21:49:28 +0000 | [diff] [blame] | 181 | |
| 182 | .. code-block:: console |
| 183 | |
Alp Toker | 8c7cbdf | 2013-12-10 13:51:53 +0000 | [diff] [blame] | 184 | usage: clang-format-diff.py [-h] [-i] [-p NUM] [-regex PATTERN] [-style STYLE] |
Daniel Jasper | 85a77c1 | 2013-01-09 21:49:28 +0000 | [diff] [blame] | 185 | |
Alp Toker | 0e7f6da | 2013-12-04 00:48:22 +0000 | [diff] [blame] | 186 | Reformat changed lines in diff. Without -i option just output the diff that |
| 187 | would be introduced. |
Daniel Jasper | 85a77c1 | 2013-01-09 21:49:28 +0000 | [diff] [blame] | 188 | |
| 189 | optional arguments: |
Alp Toker | 8c7cbdf | 2013-12-10 13:51:53 +0000 | [diff] [blame] | 190 | -h, --help show this help message and exit |
| 191 | -i apply edits to files instead of displaying a diff |
| 192 | -p NUM strip the smallest prefix containing P slashes |
| 193 | -regex PATTERN custom pattern selecting file paths to reformat |
| 194 | -style STYLE formatting style to apply (LLVM, Google, Chromium, Mozilla, |
| 195 | WebKit) |
Daniel Jasper | 85a77c1 | 2013-01-09 21:49:28 +0000 | [diff] [blame] | 196 | |
Dmitri Gribenko | cb9ede9 | 2013-01-09 22:18:55 +0000 | [diff] [blame] | 197 | So to reformat all the lines in the latest :program:`git` commit, just do: |
Daniel Jasper | 85a77c1 | 2013-01-09 21:49:28 +0000 | [diff] [blame] | 198 | |
| 199 | .. code-block:: console |
| 200 | |
Sylvestre Ledru | f421403 | 2016-12-03 23:22:45 +0000 | [diff] [blame] | 201 | git diff -U0 --no-color HEAD^ | clang-format-diff.py -i -p1 |
Dmitri Gribenko | cb9ede9 | 2013-01-09 22:18:55 +0000 | [diff] [blame] | 202 | |
Daniel Jasper | 2e8600c | 2014-05-14 09:36:11 +0000 | [diff] [blame] | 203 | In an SVN client, you can do: |
| 204 | |
| 205 | .. code-block:: console |
| 206 | |
Etienne Bergeron | 14ef11c | 2016-07-13 06:37:39 +0000 | [diff] [blame] | 207 | svn diff --diff-cmd=diff -x -U0 | clang-format-diff.py -i |
Daniel Jasper | 2e8600c | 2014-05-14 09:36:11 +0000 | [diff] [blame] | 208 | |
Aaron Ballman | c337faf | 2016-07-14 13:01:00 +0000 | [diff] [blame] | 209 | The option `-U0` will create a diff without context lines (the script would format |
Daniel Jasper | 85a77c1 | 2013-01-09 21:49:28 +0000 | [diff] [blame] | 210 | those as well). |