| Daniel Jasper | 6d5b57a | 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 | 0292519 | 2013-03-22 12:44:20 +0000 | [diff] [blame] | 13 | :program:`clang-format` is located in `clang/tools/clang-format` and can be used | 
|  | 14 | to format C/C++/Obj-C code. | 
| Daniel Jasper | 6d5b57a | 2013-01-09 21:49:28 +0000 | [diff] [blame] | 15 |  | 
|  | 16 | .. code-block:: console | 
|  | 17 |  | 
| Alexander Kornienko | 7de13bb | 2013-09-02 15:30:26 +0000 | [diff] [blame] | 18 | $ clang-format -help | 
| Daniel Jasper | 6d5b57a | 2013-01-09 21:49:28 +0000 | [diff] [blame] | 19 | OVERVIEW: A tool to format C/C++/Obj-C code. | 
|  | 20 |  | 
| Daniel Jasper | 6d5b57a | 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 | 7de13bb | 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 | 46529e5 | 2013-05-10 18:12:00 +0000 | [diff] [blame] | 25 | result is written to the standard output. | 
| Daniel Jasper | 6d5b57a | 2013-01-09 21:49:28 +0000 | [diff] [blame] | 26 |  | 
| Alexander Kornienko | 46529e5 | 2013-05-10 18:12:00 +0000 | [diff] [blame] | 27 | USAGE: clang-format [options] [<file> ...] | 
| Daniel Jasper | 6d5b57a | 2013-01-09 21:49:28 +0000 | [diff] [blame] | 28 |  | 
|  | 29 | OPTIONS: | 
| Alexander Kornienko | 46529e5 | 2013-05-10 18:12:00 +0000 | [diff] [blame] | 30 |  | 
|  | 31 | Clang-format options: | 
|  | 32 |  | 
| Alexander Kornienko | 7de13bb | 2013-09-02 15:30:26 +0000 | [diff] [blame] | 33 | -cursor=<uint>           - The position of the cursor when invoking | 
|  | 34 | clang-format from an editor integration | 
| Alexander Kornienko | 46529e5 | 2013-05-10 18:12:00 +0000 | [diff] [blame] | 35 | -dump-config             - Dump configuration options to stdout and exit. | 
|  | 36 | Can be used with -style option. | 
|  | 37 | -i                       - Inplace edit <file>s, if specified. | 
|  | 38 | -length=<uint>           - Format a range of this length (in bytes). | 
|  | 39 | Multiple ranges can be formatted by specifying | 
|  | 40 | several -offset and -length pairs. | 
|  | 41 | When only a single -offset is specified without | 
|  | 42 | -length, clang-format will format up to the end | 
|  | 43 | of the file. | 
|  | 44 | Can only be used with one input file. | 
| Alexander Kornienko | 7de13bb | 2013-09-02 15:30:26 +0000 | [diff] [blame] | 45 | -lines=<string>          - <start line>:<end line> - format a range of | 
|  | 46 | lines (both 1-based). | 
|  | 47 | Multiple ranges can be formatted by specifying | 
|  | 48 | several -lines arguments. | 
|  | 49 | Can't be used with -offset and -length. | 
|  | 50 | Can only be used with one input file. | 
| Alexander Kornienko | 46529e5 | 2013-05-10 18:12:00 +0000 | [diff] [blame] | 51 | -offset=<uint>           - Format a range starting at this byte offset. | 
|  | 52 | Multiple ranges can be formatted by specifying | 
|  | 53 | several -offset and -length pairs. | 
|  | 54 | Can only be used with one input file. | 
|  | 55 | -output-replacements-xml - Output replacements as XML. | 
|  | 56 | -style=<string>          - Coding style, currently supports: | 
| Alexander Kornienko | 4e65c98 | 2013-09-02 16:39:23 +0000 | [diff] [blame^] | 57 | LLVM, Google, Chromium, Mozilla, WebKit. | 
| Alexander Kornienko | 885f87b | 2013-05-19 00:53:30 +0000 | [diff] [blame] | 58 | Use -style=file to load style configuration from | 
| Alexander Kornienko | 46529e5 | 2013-05-10 18:12:00 +0000 | [diff] [blame] | 59 | .clang-format file located in one of the parent | 
|  | 60 | directories of the source file (or current | 
|  | 61 | directory for stdin). | 
| Alexander Kornienko | 885f87b | 2013-05-19 00:53:30 +0000 | [diff] [blame] | 62 | Use -style="{key: value, ...}" to set specific | 
|  | 63 | parameters, e.g.: | 
|  | 64 | -style="{BasedOnStyle: llvm, IndentWidth: 8}" | 
| Alexander Kornienko | 46529e5 | 2013-05-10 18:12:00 +0000 | [diff] [blame] | 65 |  | 
|  | 66 | General options: | 
|  | 67 |  | 
|  | 68 | -help                    - Display available options (-help-hidden for more) | 
|  | 69 | -help-list               - Display list of available options (-help-list-hidden for more) | 
|  | 70 | -version                 - Display the version of this program | 
| Daniel Jasper | 6d5b57a | 2013-01-09 21:49:28 +0000 | [diff] [blame] | 71 |  | 
|  | 72 |  | 
|  | 73 | Vim Integration | 
|  | 74 | =============== | 
|  | 75 |  | 
| Dmitri Gribenko | ee1230c | 2013-01-09 22:18:55 +0000 | [diff] [blame] | 76 | There is an integration for :program:`vim` which lets you run the | 
|  | 77 | :program:`clang-format` standalone tool on your current buffer, optionally | 
| Hans Wennborg | 280b956 | 2013-02-28 18:16:24 +0000 | [diff] [blame] | 78 | selecting regions to reformat. The integration has the form of a `python`-file | 
| Daniel Jasper | 0292519 | 2013-03-22 12:44:20 +0000 | [diff] [blame] | 79 | which can be found under `clang/tools/clang-format/clang-format.py`. | 
| Daniel Jasper | 6d5b57a | 2013-01-09 21:49:28 +0000 | [diff] [blame] | 80 |  | 
| Hans Wennborg | 280b956 | 2013-02-28 18:16:24 +0000 | [diff] [blame] | 81 | This can be integrated by adding the following to your `.vimrc`: | 
| Daniel Jasper | 6d5b57a | 2013-01-09 21:49:28 +0000 | [diff] [blame] | 82 |  | 
| Sean Silva | e7259ae | 2013-03-03 15:17:35 +0000 | [diff] [blame] | 83 | .. code-block:: vim | 
| Daniel Jasper | 6d5b57a | 2013-01-09 21:49:28 +0000 | [diff] [blame] | 84 |  | 
| Daniel Jasper | 0292519 | 2013-03-22 12:44:20 +0000 | [diff] [blame] | 85 | map <C-K> :pyf <path-to-this-file>/clang-format.py<CR> | 
|  | 86 | imap <C-K> <ESC>:pyf <path-to-this-file>/clang-format.py<CR>i | 
| Daniel Jasper | 6d5b57a | 2013-01-09 21:49:28 +0000 | [diff] [blame] | 87 |  | 
| Dmitri Gribenko | ee1230c | 2013-01-09 22:18:55 +0000 | [diff] [blame] | 88 | The first line enables :program:`clang-format` for NORMAL and VISUAL mode, the | 
| Daniel Jasper | 0292519 | 2013-03-22 12:44:20 +0000 | [diff] [blame] | 89 | second line adds support for INSERT mode. Change "C-K" to another binding if | 
|  | 90 | you need :program:`clang-format` on a different key (C-K stands for Ctrl+k). | 
| Daniel Jasper | 6d5b57a | 2013-01-09 21:49:28 +0000 | [diff] [blame] | 91 |  | 
|  | 92 | With this integration you can press the bound key and clang-format will | 
|  | 93 | format the current line in NORMAL and INSERT mode or the selected region in | 
|  | 94 | VISUAL mode. The line or region is extended to the next bigger syntactic | 
|  | 95 | entity. | 
|  | 96 |  | 
|  | 97 | It operates on the current, potentially unsaved buffer and does not create | 
|  | 98 | or save any files. To revert a formatting, just undo. | 
|  | 99 |  | 
|  | 100 |  | 
| Daniel Jasper | a50b578 | 2013-04-17 07:55:02 +0000 | [diff] [blame] | 101 | Emacs Integration | 
|  | 102 | ================= | 
|  | 103 |  | 
|  | 104 | Similar to the integration for :program:`vim`, there is an integration for | 
|  | 105 | :program:`emacs`. It can be found at `clang/tools/clang-format/clang-format.el` | 
|  | 106 | and used by adding this to your `.emacs`: | 
|  | 107 |  | 
|  | 108 | .. code-block:: common-lisp | 
|  | 109 |  | 
|  | 110 | (load "<path-to-clang>/tools/clang-format/clang-format.el") | 
|  | 111 | (global-set-key [C-M-tab] 'clang-format-region) | 
|  | 112 |  | 
|  | 113 | This binds the function `clang-format-region` to C-M-tab, which then formats the | 
|  | 114 | current line or selected region. | 
|  | 115 |  | 
|  | 116 |  | 
| Daniel Jasper | 82e3b47 | 2013-05-02 17:37:36 +0000 | [diff] [blame] | 117 | BBEdit Integration | 
|  | 118 | ================== | 
|  | 119 |  | 
|  | 120 | :program:`clang-format` cannot be used as a text filter with BBEdit, but works | 
|  | 121 | well via a script. The AppleScript to do this integration can be found at | 
|  | 122 | `clang/tools/clang-format/clang-format-bbedit.applescript`; place a copy in | 
|  | 123 | `~/Library/Application Support/BBEdit/Scripts`, and edit the path within it to | 
|  | 124 | point to your local copy of :program:`clang-format`. | 
|  | 125 |  | 
|  | 126 | With this integration you can select the script from the Script menu and | 
|  | 127 | :program:`clang-format` will format the selection. Note that you can rename the | 
|  | 128 | menu item by renaming the script, and can assign the menu item a keyboard | 
|  | 129 | shortcut in the BBEdit preferences, under Menus & Shortcuts. | 
|  | 130 |  | 
|  | 131 |  | 
| Daniel Jasper | 6d5b57a | 2013-01-09 21:49:28 +0000 | [diff] [blame] | 132 | Script for patch reformatting | 
|  | 133 | ============================= | 
|  | 134 |  | 
| Daniel Jasper | 0292519 | 2013-03-22 12:44:20 +0000 | [diff] [blame] | 135 | The python script `clang/tools/clang-format-diff.py` parses the output of | 
| Dmitri Gribenko | ee1230c | 2013-01-09 22:18:55 +0000 | [diff] [blame] | 136 | a unified diff and reformats all contained lines with :program:`clang-format`. | 
| Daniel Jasper | 6d5b57a | 2013-01-09 21:49:28 +0000 | [diff] [blame] | 137 |  | 
|  | 138 | .. code-block:: console | 
|  | 139 |  | 
|  | 140 | usage: clang-format-diff.py [-h] [-p P] [-style STYLE] | 
|  | 141 |  | 
| Alexander Kornienko | 4e65c98 | 2013-09-02 16:39:23 +0000 | [diff] [blame^] | 142 | Reformat changed lines in diff. | 
| Daniel Jasper | 6d5b57a | 2013-01-09 21:49:28 +0000 | [diff] [blame] | 143 |  | 
|  | 144 | optional arguments: | 
|  | 145 | -h, --help    show this help message and exit | 
|  | 146 | -p P          strip the smallest prefix containing P slashes | 
| Alexander Kornienko | 4e65c98 | 2013-09-02 16:39:23 +0000 | [diff] [blame^] | 147 | -style STYLE  formatting style to apply (LLVM, Google, Chromium, Mozilla, | 
|  | 148 | WebKit) | 
| Daniel Jasper | 6d5b57a | 2013-01-09 21:49:28 +0000 | [diff] [blame] | 149 |  | 
| Dmitri Gribenko | ee1230c | 2013-01-09 22:18:55 +0000 | [diff] [blame] | 150 | So to reformat all the lines in the latest :program:`git` commit, just do: | 
| Daniel Jasper | 6d5b57a | 2013-01-09 21:49:28 +0000 | [diff] [blame] | 151 |  | 
|  | 152 | .. code-block:: console | 
|  | 153 |  | 
| Rui Ueyama | 0b588cd | 2013-06-14 02:12:06 +0000 | [diff] [blame] | 154 | git diff -U0 HEAD^ | clang-format-diff.py -p1 | 
| Dmitri Gribenko | ee1230c | 2013-01-09 22:18:55 +0000 | [diff] [blame] | 155 |  | 
|  | 156 | The :option:`-U0` will create a diff without context lines (the script would format | 
| Daniel Jasper | 6d5b57a | 2013-01-09 21:49:28 +0000 | [diff] [blame] | 157 | those as well). |