blob: 45ea32717995877a2f01d411779b2823967717d7 [file] [log] [blame]
Daniel Jasper6d5b57a2013-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 Jasper02925192013-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 Jasper6d5b57a2013-01-09 21:49:28 +000015
16.. code-block:: console
17
Alexander Kornienko7de13bb2013-09-02 15:30:26 +000018 $ clang-format -help
Daniel Jasper6d5b57a2013-01-09 21:49:28 +000019 OVERVIEW: A tool to format C/C++/Obj-C code.
20
Daniel Jasper6d5b57a2013-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 Kornienko7de13bb2013-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 Kornienko46529e52013-05-10 18:12:00 +000025 result is written to the standard output.
Daniel Jasper6d5b57a2013-01-09 21:49:28 +000026
Alexander Kornienko46529e52013-05-10 18:12:00 +000027 USAGE: clang-format [options] [<file> ...]
Daniel Jasper6d5b57a2013-01-09 21:49:28 +000028
29 OPTIONS:
Alexander Kornienko46529e52013-05-10 18:12:00 +000030
31 Clang-format options:
32
Alexander Kornienko7de13bb2013-09-02 15:30:26 +000033 -cursor=<uint> - The position of the cursor when invoking
34 clang-format from an editor integration
Alexander Kornienko46529e52013-05-10 18:12:00 +000035 -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 Kornienko7de13bb2013-09-02 15:30:26 +000045 -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 Kornienko46529e52013-05-10 18:12:00 +000051 -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 Kornienko4e65c982013-09-02 16:39:23 +000057 LLVM, Google, Chromium, Mozilla, WebKit.
Alexander Kornienko885f87b2013-05-19 00:53:30 +000058 Use -style=file to load style configuration from
Alexander Kornienko46529e52013-05-10 18:12:00 +000059 .clang-format file located in one of the parent
60 directories of the source file (or current
61 directory for stdin).
Alexander Kornienko885f87b2013-05-19 00:53:30 +000062 Use -style="{key: value, ...}" to set specific
63 parameters, e.g.:
64 -style="{BasedOnStyle: llvm, IndentWidth: 8}"
Alexander Kornienko46529e52013-05-10 18:12:00 +000065
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 Jasper6d5b57a2013-01-09 21:49:28 +000071
72
Alexander Kornienko62d06b72013-09-04 15:09:13 +000073When the desired code formatting style is different from the available options,
74the style can be customized using the ``-style="{key: value, ...}"`` option or
Hans Wennborg9a7a50e2013-09-10 15:41:12 +000075by putting your style configuration in the ``.clang-format`` or ``_clang-format``
76file in your project's directory and using ``clang-format -style=file``.
Alexander Kornienko62d06b72013-09-04 15:09:13 +000077
78An easy way to create the ``.clang-format`` file is:
79
80.. code-block:: console
81
82 clang-format -style=llvm -dump-config > .clang-format
83
84Available style options are described in :doc:`ClangFormatStyleOptions`.
85
86
Daniel Jasper6d5b57a2013-01-09 21:49:28 +000087Vim Integration
88===============
89
Dmitri Gribenkoee1230c2013-01-09 22:18:55 +000090There is an integration for :program:`vim` which lets you run the
91:program:`clang-format` standalone tool on your current buffer, optionally
Hans Wennborg280b9562013-02-28 18:16:24 +000092selecting regions to reformat. The integration has the form of a `python`-file
Daniel Jasper02925192013-03-22 12:44:20 +000093which can be found under `clang/tools/clang-format/clang-format.py`.
Daniel Jasper6d5b57a2013-01-09 21:49:28 +000094
Hans Wennborg280b9562013-02-28 18:16:24 +000095This can be integrated by adding the following to your `.vimrc`:
Daniel Jasper6d5b57a2013-01-09 21:49:28 +000096
Sean Silvae7259ae2013-03-03 15:17:35 +000097.. code-block:: vim
Daniel Jasper6d5b57a2013-01-09 21:49:28 +000098
Stephen Hines176edba2014-12-01 14:53:08 -080099 map <C-K> :pyf <path-to-this-file>/clang-format.py<cr>
100 imap <C-K> <c-o>:pyf <path-to-this-file>/clang-format.py<cr>
Daniel Jasper6d5b57a2013-01-09 21:49:28 +0000101
Dmitri Gribenkoee1230c2013-01-09 22:18:55 +0000102The first line enables :program:`clang-format` for NORMAL and VISUAL mode, the
Daniel Jasper02925192013-03-22 12:44:20 +0000103second line adds support for INSERT mode. Change "C-K" to another binding if
104you need :program:`clang-format` on a different key (C-K stands for Ctrl+k).
Daniel Jasper6d5b57a2013-01-09 21:49:28 +0000105
106With this integration you can press the bound key and clang-format will
107format the current line in NORMAL and INSERT mode or the selected region in
108VISUAL mode. The line or region is extended to the next bigger syntactic
109entity.
110
111It operates on the current, potentially unsaved buffer and does not create
112or save any files. To revert a formatting, just undo.
113
114
Daniel Jaspera50b5782013-04-17 07:55:02 +0000115Emacs Integration
116=================
117
118Similar to the integration for :program:`vim`, there is an integration for
119:program:`emacs`. It can be found at `clang/tools/clang-format/clang-format.el`
120and used by adding this to your `.emacs`:
121
122.. code-block:: common-lisp
123
124 (load "<path-to-clang>/tools/clang-format/clang-format.el")
125 (global-set-key [C-M-tab] 'clang-format-region)
126
127This binds the function `clang-format-region` to C-M-tab, which then formats the
128current line or selected region.
129
130
Daniel Jasper82e3b472013-05-02 17:37:36 +0000131BBEdit Integration
132==================
133
134:program:`clang-format` cannot be used as a text filter with BBEdit, but works
135well via a script. The AppleScript to do this integration can be found at
136`clang/tools/clang-format/clang-format-bbedit.applescript`; place a copy in
137`~/Library/Application Support/BBEdit/Scripts`, and edit the path within it to
138point to your local copy of :program:`clang-format`.
139
140With this integration you can select the script from the Script menu and
141:program:`clang-format` will format the selection. Note that you can rename the
142menu item by renaming the script, and can assign the menu item a keyboard
143shortcut in the BBEdit preferences, under Menus & Shortcuts.
144
145
Manuel Klimek7454cdc2013-09-29 17:49:50 +0000146Visual Studio Integration
147=========================
148
Stephen Hines651f13c2014-04-23 16:59:28 -0700149Download the latest Visual Studio extension from the `alpha build site
Manuel Klimek7454cdc2013-09-29 17:49:50 +0000150<http://llvm.org/builds/>`_. The default key-binding is Ctrl-R,Ctrl-F.
151
152
Daniel Jasper6d5b57a2013-01-09 21:49:28 +0000153Script for patch reformatting
154=============================
155
Daniel Jasper02925192013-03-22 12:44:20 +0000156The python script `clang/tools/clang-format-diff.py` parses the output of
Dmitri Gribenkoee1230c2013-01-09 22:18:55 +0000157a unified diff and reformats all contained lines with :program:`clang-format`.
Daniel Jasper6d5b57a2013-01-09 21:49:28 +0000158
159.. code-block:: console
160
Stephen Hines651f13c2014-04-23 16:59:28 -0700161 usage: clang-format-diff.py [-h] [-i] [-p NUM] [-regex PATTERN] [-style STYLE]
Daniel Jasper6d5b57a2013-01-09 21:49:28 +0000162
Stephen Hines651f13c2014-04-23 16:59:28 -0700163 Reformat changed lines in diff. Without -i option just output the diff that
164 would be introduced.
Daniel Jasper6d5b57a2013-01-09 21:49:28 +0000165
166 optional arguments:
Stephen Hines651f13c2014-04-23 16:59:28 -0700167 -h, --help show this help message and exit
168 -i apply edits to files instead of displaying a diff
169 -p NUM strip the smallest prefix containing P slashes
170 -regex PATTERN custom pattern selecting file paths to reformat
171 -style STYLE formatting style to apply (LLVM, Google, Chromium, Mozilla,
172 WebKit)
Daniel Jasper6d5b57a2013-01-09 21:49:28 +0000173
Dmitri Gribenkoee1230c2013-01-09 22:18:55 +0000174So to reformat all the lines in the latest :program:`git` commit, just do:
Daniel Jasper6d5b57a2013-01-09 21:49:28 +0000175
176.. code-block:: console
177
Stephen Hines651f13c2014-04-23 16:59:28 -0700178 git diff -U0 HEAD^ | clang-format-diff.py -i -p1
Dmitri Gribenkoee1230c2013-01-09 22:18:55 +0000179
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700180In an SVN client, you can do:
181
182.. code-block:: console
183
184 svn diff --diff-cmd=diff -x-U0 | clang-format-diff.py -i
185
Dmitri Gribenkoee1230c2013-01-09 22:18:55 +0000186The :option:`-U0` will create a diff without context lines (the script would format
Daniel Jasper6d5b57a2013-01-09 21:49:28 +0000187those as well).