blob: b4968ef1036fc477931d52769f4537e447f04b66 [file] [log] [blame]
Daniel Jasper85a77c12013-01-09 21:49:28 +00001===========
2ClangFormat
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
7standalone tool and editor integrations.
8
9
10Standalone Tool
11===============
12
Daniel Jasper41723752013-03-22 12:44:20 +000013:program:`clang-format` is located in `clang/tools/clang-format` and can be used
14to format C/C++/Obj-C code.
Daniel Jasper85a77c12013-01-09 21:49:28 +000015
16.. code-block:: console
17
Alexander Kornienkod83adf32013-09-02 15:30:26 +000018 $ clang-format -help
Nico Weber187fcf12015-10-19 01:08:30 +000019 OVERVIEW: A tool to format C/C++/Java/JavaScript/Objective-C/Protobuf code.
Daniel Jasper85a77c12013-01-09 21:49:28 +000020
Daniel Jasper85a77c12013-01-09 21:49:28 +000021 If no arguments are specified, it formats the code from standard input
22 and writes the result to the standard output.
Alexander Kornienkod83adf32013-09-02 15:30:26 +000023 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 Kornienko88a0d932013-05-10 18:12:00 +000025 result is written to the standard output.
Daniel Jasper85a77c12013-01-09 21:49:28 +000026
Alexander Kornienko88a0d932013-05-10 18:12:00 +000027 USAGE: clang-format [options] [<file> ...]
Daniel Jasper85a77c12013-01-09 21:49:28 +000028
29 OPTIONS:
Alexander Kornienko88a0d932013-05-10 18:12:00 +000030
31 Clang-format options:
32
Nico Weber187fcf12015-10-19 01:08:30 +000033 -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}"
Alexander Kornienko88a0d932013-05-10 18:12:00 +000074
Nico Weber187fcf12015-10-19 01:08:30 +000075 Generic Options:
Alexander Kornienko88a0d932013-05-10 18:12:00 +000076
Nico Weber187fcf12015-10-19 01:08:30 +000077 -help - Display available options (-help-hidden for more)
78 -help-list - Display list of available options (-help-list-hidden for more)
79 -version - Display the version of this program
Daniel Jasper85a77c12013-01-09 21:49:28 +000080
81
Alexander Kornienkod278e0e2013-09-04 15:09:13 +000082When the desired code formatting style is different from the available options,
83the style can be customized using the ``-style="{key: value, ...}"`` option or
Hans Wennborg9f6581b2013-09-10 15:41:12 +000084by putting your style configuration in the ``.clang-format`` or ``_clang-format``
85file in your project's directory and using ``clang-format -style=file``.
Alexander Kornienkod278e0e2013-09-04 15:09:13 +000086
87An easy way to create the ``.clang-format`` file is:
88
89.. code-block:: console
90
91 clang-format -style=llvm -dump-config > .clang-format
92
93Available style options are described in :doc:`ClangFormatStyleOptions`.
94
95
Daniel Jasper85a77c12013-01-09 21:49:28 +000096Vim Integration
97===============
98
Dmitri Gribenkocb9ede92013-01-09 22:18:55 +000099There is an integration for :program:`vim` which lets you run the
100:program:`clang-format` standalone tool on your current buffer, optionally
Hans Wennborgc32d5132013-02-28 18:16:24 +0000101selecting regions to reformat. The integration has the form of a `python`-file
Daniel Jasper41723752013-03-22 12:44:20 +0000102which can be found under `clang/tools/clang-format/clang-format.py`.
Daniel Jasper85a77c12013-01-09 21:49:28 +0000103
Hans Wennborgc32d5132013-02-28 18:16:24 +0000104This can be integrated by adding the following to your `.vimrc`:
Daniel Jasper85a77c12013-01-09 21:49:28 +0000105
Sean Silvaedd5ca52013-03-03 15:17:35 +0000106.. code-block:: vim
Daniel Jasper85a77c12013-01-09 21:49:28 +0000107
Saleem Abdulrasool8bbed0b2014-11-02 21:27:52 +0000108 map <C-K> :pyf <path-to-this-file>/clang-format.py<cr>
109 imap <C-K> <c-o>:pyf <path-to-this-file>/clang-format.py<cr>
Daniel Jasper85a77c12013-01-09 21:49:28 +0000110
Dmitri Gribenkocb9ede92013-01-09 22:18:55 +0000111The first line enables :program:`clang-format` for NORMAL and VISUAL mode, the
Daniel Jasper41723752013-03-22 12:44:20 +0000112second line adds support for INSERT mode. Change "C-K" to another binding if
113you need :program:`clang-format` on a different key (C-K stands for Ctrl+k).
Daniel Jasper85a77c12013-01-09 21:49:28 +0000114
115With this integration you can press the bound key and clang-format will
116format the current line in NORMAL and INSERT mode or the selected region in
117VISUAL mode. The line or region is extended to the next bigger syntactic
118entity.
119
120It operates on the current, potentially unsaved buffer and does not create
121or save any files. To revert a formatting, just undo.
122
123
Daniel Jasperdd777432013-04-17 07:55:02 +0000124Emacs Integration
125=================
126
127Similar to the integration for :program:`vim`, there is an integration for
128:program:`emacs`. It can be found at `clang/tools/clang-format/clang-format.el`
129and used by adding this to your `.emacs`:
130
131.. code-block:: common-lisp
132
133 (load "<path-to-clang>/tools/clang-format/clang-format.el")
134 (global-set-key [C-M-tab] 'clang-format-region)
135
136This binds the function `clang-format-region` to C-M-tab, which then formats the
137current line or selected region.
138
139
Daniel Jasper5f997422013-05-02 17:37:36 +0000140BBEdit Integration
141==================
142
143:program:`clang-format` cannot be used as a text filter with BBEdit, but works
144well via a script. The AppleScript to do this integration can be found at
145`clang/tools/clang-format/clang-format-bbedit.applescript`; place a copy in
146`~/Library/Application Support/BBEdit/Scripts`, and edit the path within it to
147point to your local copy of :program:`clang-format`.
148
149With this integration you can select the script from the Script menu and
150:program:`clang-format` will format the selection. Note that you can rename the
151menu item by renaming the script, and can assign the menu item a keyboard
152shortcut in the BBEdit preferences, under Menus & Shortcuts.
153
154
Manuel Klimek7491eaa2013-09-29 17:49:50 +0000155Visual Studio Integration
156=========================
157
Alp Toker0e7f6da2013-12-04 00:48:22 +0000158Download the latest Visual Studio extension from the `alpha build site
Manuel Klimek7491eaa2013-09-29 17:49:50 +0000159<http://llvm.org/builds/>`_. The default key-binding is Ctrl-R,Ctrl-F.
160
161
Daniel Jasper85a77c12013-01-09 21:49:28 +0000162Script for patch reformatting
163=============================
164
Daniel Jasper41723752013-03-22 12:44:20 +0000165The python script `clang/tools/clang-format-diff.py` parses the output of
Dmitri Gribenkocb9ede92013-01-09 22:18:55 +0000166a unified diff and reformats all contained lines with :program:`clang-format`.
Daniel Jasper85a77c12013-01-09 21:49:28 +0000167
168.. code-block:: console
169
Alp Toker8c7cbdf2013-12-10 13:51:53 +0000170 usage: clang-format-diff.py [-h] [-i] [-p NUM] [-regex PATTERN] [-style STYLE]
Daniel Jasper85a77c12013-01-09 21:49:28 +0000171
Alp Toker0e7f6da2013-12-04 00:48:22 +0000172 Reformat changed lines in diff. Without -i option just output the diff that
173 would be introduced.
Daniel Jasper85a77c12013-01-09 21:49:28 +0000174
175 optional arguments:
Alp Toker8c7cbdf2013-12-10 13:51:53 +0000176 -h, --help show this help message and exit
177 -i apply edits to files instead of displaying a diff
178 -p NUM strip the smallest prefix containing P slashes
179 -regex PATTERN custom pattern selecting file paths to reformat
180 -style STYLE formatting style to apply (LLVM, Google, Chromium, Mozilla,
181 WebKit)
Daniel Jasper85a77c12013-01-09 21:49:28 +0000182
Dmitri Gribenkocb9ede92013-01-09 22:18:55 +0000183So to reformat all the lines in the latest :program:`git` commit, just do:
Daniel Jasper85a77c12013-01-09 21:49:28 +0000184
185.. code-block:: console
186
Alp Toker0e7f6da2013-12-04 00:48:22 +0000187 git diff -U0 HEAD^ | clang-format-diff.py -i -p1
Dmitri Gribenkocb9ede92013-01-09 22:18:55 +0000188
Daniel Jasper2e8600c2014-05-14 09:36:11 +0000189In an SVN client, you can do:
190
191.. code-block:: console
192
193 svn diff --diff-cmd=diff -x-U0 | clang-format-diff.py -i
194
Dmitri Gribenkocb9ede92013-01-09 22:18:55 +0000195The :option:`-U0` will create a diff without context lines (the script would format
Daniel Jasper85a77c12013-01-09 21:49:28 +0000196those as well).