blob: 1b65c2542bfa22dd52c5cfd51dd864f4de1406e2 [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.
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 Jasper02925192013-03-22 12:44:20 +000036 -style=<string> - Coding style, currently supports: LLVM, Google, Chromium.
Daniel Jasper6d5b57a2013-01-09 21:49:28 +000037 -version - Display the version of this program
38
39
40Vim Integration
41===============
42
Dmitri Gribenkoee1230c2013-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 Wennborg280b9562013-02-28 18:16:24 +000045selecting regions to reformat. The integration has the form of a `python`-file
Daniel Jasper02925192013-03-22 12:44:20 +000046which can be found under `clang/tools/clang-format/clang-format.py`.
Daniel Jasper6d5b57a2013-01-09 21:49:28 +000047
Hans Wennborg280b9562013-02-28 18:16:24 +000048This can be integrated by adding the following to your `.vimrc`:
Daniel Jasper6d5b57a2013-01-09 21:49:28 +000049
Sean Silvae7259ae2013-03-03 15:17:35 +000050.. code-block:: vim
Daniel Jasper6d5b57a2013-01-09 21:49:28 +000051
Daniel Jasper02925192013-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 Jasper6d5b57a2013-01-09 21:49:28 +000054
Dmitri Gribenkoee1230c2013-01-09 22:18:55 +000055The first line enables :program:`clang-format` for NORMAL and VISUAL mode, the
Daniel Jasper02925192013-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 Jasper6d5b57a2013-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 Jaspera50b5782013-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 Jasper6d5b57a2013-01-09 21:49:28 +000084Script for patch reformatting
85=============================
86
Daniel Jasper02925192013-03-22 12:44:20 +000087The python script `clang/tools/clang-format-diff.py` parses the output of
Dmitri Gribenkoee1230c2013-01-09 22:18:55 +000088a unified diff and reformats all contained lines with :program:`clang-format`.
Daniel Jasper6d5b57a2013-01-09 21:49:28 +000089
90.. code-block:: console
91
92 usage: clang-format-diff.py [-h] [-p P] [-style STYLE]
93
94 Reformat changed lines in diff
95
96 optional arguments:
97 -h, --help show this help message and exit
98 -p P strip the smallest prefix containing P slashes
Daniel Jaspera50b5782013-04-17 07:55:02 +000099 -style STYLE formatting style to apply (LLVM, Google, Chromium)
Daniel Jasper6d5b57a2013-01-09 21:49:28 +0000100
Dmitri Gribenkoee1230c2013-01-09 22:18:55 +0000101So to reformat all the lines in the latest :program:`git` commit, just do:
Daniel Jasper6d5b57a2013-01-09 21:49:28 +0000102
103.. code-block:: console
104
105 git diff -U0 HEAD^ | clang-format-diff.py
Dmitri Gribenkoee1230c2013-01-09 22:18:55 +0000106
107The :option:`-U0` will create a diff without context lines (the script would format
Daniel Jasper6d5b57a2013-01-09 21:49:28 +0000108those as well).