blob: a9687bc9e4e540135af155895fea4fc7cb295ccf [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
18 $ clang-format --help
19 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 Kornienko46529e52013-05-10 18:12:00 +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
25 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
33 -dump-config - Dump configuration options to stdout and exit.
34 Can be used with -style option.
35 -i - Inplace edit <file>s, if specified.
36 -length=<uint> - Format a range of this length (in bytes).
37 Multiple ranges can be formatted by specifying
38 several -offset and -length pairs.
39 When only a single -offset is specified without
40 -length, clang-format will format up to the end
41 of the file.
42 Can only be used with one input file.
43 -offset=<uint> - Format a range starting at this byte offset.
44 Multiple ranges can be formatted by specifying
45 several -offset and -length pairs.
46 Can only be used with one input file.
47 -output-replacements-xml - Output replacements as XML.
48 -style=<string> - Coding style, currently supports:
49 LLVM, Google, Chromium, Mozilla.
50 Use '-style file' to load style configuration from
51 .clang-format file located in one of the parent
52 directories of the source file (or current
53 directory for stdin).
54
55 General options:
56
57 -help - Display available options (-help-hidden for more)
58 -help-list - Display list of available options (-help-list-hidden for more)
59 -version - Display the version of this program
Daniel Jasper6d5b57a2013-01-09 21:49:28 +000060
61
62Vim Integration
63===============
64
Dmitri Gribenkoee1230c2013-01-09 22:18:55 +000065There is an integration for :program:`vim` which lets you run the
66:program:`clang-format` standalone tool on your current buffer, optionally
Hans Wennborg280b9562013-02-28 18:16:24 +000067selecting regions to reformat. The integration has the form of a `python`-file
Daniel Jasper02925192013-03-22 12:44:20 +000068which can be found under `clang/tools/clang-format/clang-format.py`.
Daniel Jasper6d5b57a2013-01-09 21:49:28 +000069
Hans Wennborg280b9562013-02-28 18:16:24 +000070This can be integrated by adding the following to your `.vimrc`:
Daniel Jasper6d5b57a2013-01-09 21:49:28 +000071
Sean Silvae7259ae2013-03-03 15:17:35 +000072.. code-block:: vim
Daniel Jasper6d5b57a2013-01-09 21:49:28 +000073
Daniel Jasper02925192013-03-22 12:44:20 +000074 map <C-K> :pyf <path-to-this-file>/clang-format.py<CR>
75 imap <C-K> <ESC>:pyf <path-to-this-file>/clang-format.py<CR>i
Daniel Jasper6d5b57a2013-01-09 21:49:28 +000076
Dmitri Gribenkoee1230c2013-01-09 22:18:55 +000077The first line enables :program:`clang-format` for NORMAL and VISUAL mode, the
Daniel Jasper02925192013-03-22 12:44:20 +000078second line adds support for INSERT mode. Change "C-K" to another binding if
79you need :program:`clang-format` on a different key (C-K stands for Ctrl+k).
Daniel Jasper6d5b57a2013-01-09 21:49:28 +000080
81With this integration you can press the bound key and clang-format will
82format the current line in NORMAL and INSERT mode or the selected region in
83VISUAL mode. The line or region is extended to the next bigger syntactic
84entity.
85
86It operates on the current, potentially unsaved buffer and does not create
87or save any files. To revert a formatting, just undo.
88
89
Daniel Jaspera50b5782013-04-17 07:55:02 +000090Emacs Integration
91=================
92
93Similar to the integration for :program:`vim`, there is an integration for
94:program:`emacs`. It can be found at `clang/tools/clang-format/clang-format.el`
95and used by adding this to your `.emacs`:
96
97.. code-block:: common-lisp
98
99 (load "<path-to-clang>/tools/clang-format/clang-format.el")
100 (global-set-key [C-M-tab] 'clang-format-region)
101
102This binds the function `clang-format-region` to C-M-tab, which then formats the
103current line or selected region.
104
105
Daniel Jasper82e3b472013-05-02 17:37:36 +0000106BBEdit Integration
107==================
108
109:program:`clang-format` cannot be used as a text filter with BBEdit, but works
110well via a script. The AppleScript to do this integration can be found at
111`clang/tools/clang-format/clang-format-bbedit.applescript`; place a copy in
112`~/Library/Application Support/BBEdit/Scripts`, and edit the path within it to
113point to your local copy of :program:`clang-format`.
114
115With this integration you can select the script from the Script menu and
116:program:`clang-format` will format the selection. Note that you can rename the
117menu item by renaming the script, and can assign the menu item a keyboard
118shortcut in the BBEdit preferences, under Menus & Shortcuts.
119
120
Daniel Jasper6d5b57a2013-01-09 21:49:28 +0000121Script for patch reformatting
122=============================
123
Daniel Jasper02925192013-03-22 12:44:20 +0000124The python script `clang/tools/clang-format-diff.py` parses the output of
Dmitri Gribenkoee1230c2013-01-09 22:18:55 +0000125a unified diff and reformats all contained lines with :program:`clang-format`.
Daniel Jasper6d5b57a2013-01-09 21:49:28 +0000126
127.. code-block:: console
128
129 usage: clang-format-diff.py [-h] [-p P] [-style STYLE]
130
131 Reformat changed lines in diff
132
133 optional arguments:
134 -h, --help show this help message and exit
135 -p P strip the smallest prefix containing P slashes
Daniel Jaspera50b5782013-04-17 07:55:02 +0000136 -style STYLE formatting style to apply (LLVM, Google, Chromium)
Daniel Jasper6d5b57a2013-01-09 21:49:28 +0000137
Dmitri Gribenkoee1230c2013-01-09 22:18:55 +0000138So to reformat all the lines in the latest :program:`git` commit, just do:
Daniel Jasper6d5b57a2013-01-09 21:49:28 +0000139
140.. code-block:: console
141
142 git diff -U0 HEAD^ | clang-format-diff.py
Dmitri Gribenkoee1230c2013-01-09 22:18:55 +0000143
144The :option:`-U0` will create a diff without context lines (the script would format
Daniel Jasper6d5b57a2013-01-09 21:49:28 +0000145those as well).