blob: 964fc84d7bcabe9b7ccd34d92529678fdbfb0d76 [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
18 $ clang-format --help
19 OVERVIEW: A tool to format C/C++/Obj-C code.
20
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.
23 If <file> is given, it reformats the file. If -i is specified together
24 with <file>, the file is edited in-place. Otherwise, the result is
25 written to the standard output.
26
27 USAGE: clang-format [options] [<file>]
28
29 OPTIONS:
30 -fatal-assembler-warnings - Consider warnings as error
31 -help - Display available options (-help-hidden for more)
32 -i - Inplace edit <file>, if specified.
33 -length=<int> - Format a range of this length, -1 for end of file.
34 -offset=<int> - Format a range starting at this file offset.
35 -stats - Enable statistics output from program
Daniel Jasper41723752013-03-22 12:44:20 +000036 -style=<string> - Coding style, currently supports: LLVM, Google, Chromium.
Daniel Jasper85a77c12013-01-09 21:49:28 +000037 -version - Display the version of this program
38
39
40Vim Integration
41===============
42
Dmitri Gribenkocb9ede92013-01-09 22:18:55 +000043There is an integration for :program:`vim` which lets you run the
44:program:`clang-format` standalone tool on your current buffer, optionally
Hans Wennborgc32d5132013-02-28 18:16:24 +000045selecting regions to reformat. The integration has the form of a `python`-file
Daniel Jasper41723752013-03-22 12:44:20 +000046which can be found under `clang/tools/clang-format/clang-format.py`.
Daniel Jasper85a77c12013-01-09 21:49:28 +000047
Hans Wennborgc32d5132013-02-28 18:16:24 +000048This can be integrated by adding the following to your `.vimrc`:
Daniel Jasper85a77c12013-01-09 21:49:28 +000049
Sean Silvaedd5ca52013-03-03 15:17:35 +000050.. code-block:: vim
Daniel Jasper85a77c12013-01-09 21:49:28 +000051
Daniel Jasper41723752013-03-22 12:44:20 +000052 map <C-K> :pyf <path-to-this-file>/clang-format.py<CR>
53 imap <C-K> <ESC>:pyf <path-to-this-file>/clang-format.py<CR>i
Daniel Jasper85a77c12013-01-09 21:49:28 +000054
Dmitri Gribenkocb9ede92013-01-09 22:18:55 +000055The first line enables :program:`clang-format` for NORMAL and VISUAL mode, the
Daniel Jasper41723752013-03-22 12:44:20 +000056second line adds support for INSERT mode. Change "C-K" to another binding if
57you need :program:`clang-format` on a different key (C-K stands for Ctrl+k).
Daniel Jasper85a77c12013-01-09 21:49:28 +000058
59With this integration you can press the bound key and clang-format will
60format the current line in NORMAL and INSERT mode or the selected region in
61VISUAL mode. The line or region is extended to the next bigger syntactic
62entity.
63
64It operates on the current, potentially unsaved buffer and does not create
65or save any files. To revert a formatting, just undo.
66
67
Daniel Jasperdd777432013-04-17 07:55:02 +000068Emacs Integration
69=================
70
71Similar to the integration for :program:`vim`, there is an integration for
72:program:`emacs`. It can be found at `clang/tools/clang-format/clang-format.el`
73and used by adding this to your `.emacs`:
74
75.. code-block:: common-lisp
76
77 (load "<path-to-clang>/tools/clang-format/clang-format.el")
78 (global-set-key [C-M-tab] 'clang-format-region)
79
80This binds the function `clang-format-region` to C-M-tab, which then formats the
81current line or selected region.
82
83
Daniel Jasper5f997422013-05-02 17:37:36 +000084BBEdit Integration
85==================
86
87:program:`clang-format` cannot be used as a text filter with BBEdit, but works
88well via a script. The AppleScript to do this integration can be found at
89`clang/tools/clang-format/clang-format-bbedit.applescript`; place a copy in
90`~/Library/Application Support/BBEdit/Scripts`, and edit the path within it to
91point to your local copy of :program:`clang-format`.
92
93With this integration you can select the script from the Script menu and
94:program:`clang-format` will format the selection. Note that you can rename the
95menu item by renaming the script, and can assign the menu item a keyboard
96shortcut in the BBEdit preferences, under Menus & Shortcuts.
97
98
Daniel Jasper85a77c12013-01-09 21:49:28 +000099Script for patch reformatting
100=============================
101
Daniel Jasper41723752013-03-22 12:44:20 +0000102The python script `clang/tools/clang-format-diff.py` parses the output of
Dmitri Gribenkocb9ede92013-01-09 22:18:55 +0000103a unified diff and reformats all contained lines with :program:`clang-format`.
Daniel Jasper85a77c12013-01-09 21:49:28 +0000104
105.. code-block:: console
106
107 usage: clang-format-diff.py [-h] [-p P] [-style STYLE]
108
109 Reformat changed lines in diff
110
111 optional arguments:
112 -h, --help show this help message and exit
113 -p P strip the smallest prefix containing P slashes
Daniel Jasperdd777432013-04-17 07:55:02 +0000114 -style STYLE formatting style to apply (LLVM, Google, Chromium)
Daniel Jasper85a77c12013-01-09 21:49:28 +0000115
Dmitri Gribenkocb9ede92013-01-09 22:18:55 +0000116So to reformat all the lines in the latest :program:`git` commit, just do:
Daniel Jasper85a77c12013-01-09 21:49:28 +0000117
118.. code-block:: console
119
120 git diff -U0 HEAD^ | clang-format-diff.py
Dmitri Gribenkocb9ede92013-01-09 22:18:55 +0000121
122The :option:`-U0` will create a diff without context lines (the script would format
Daniel Jasper85a77c12013-01-09 21:49:28 +0000123those as well).