blob: f5c0b4c1fb9cfc9811fc1ddfa111fc6431fdf4a7 [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
Dmitri Gribenkoee1230c2013-01-09 22:18:55 +000013:program:`clang-format` is part of the `clang/tools/extra` (see
Daniel Jasper6d5b57a2013-01-09 21:49:28 +000014:doc:`ClangTools <ClangTools>`) repository and can be used to format
15C/C++/Obj-C code.
16
17.. code-block:: console
18
19 $ clang-format --help
20 OVERVIEW: A tool to format C/C++/Obj-C code.
21
22 Currently supports LLVM and Google style guides.
23 If no arguments are specified, it formats the code from standard input
24 and writes the result to the standard output.
25 If <file> is given, it reformats the file. If -i is specified together
26 with <file>, the file is edited in-place. Otherwise, the result is
27 written to the standard output.
28
29 USAGE: clang-format [options] [<file>]
30
31 OPTIONS:
32 -fatal-assembler-warnings - Consider warnings as error
33 -help - Display available options (-help-hidden for more)
34 -i - Inplace edit <file>, if specified.
35 -length=<int> - Format a range of this length, -1 for end of file.
36 -offset=<int> - Format a range starting at this file offset.
37 -stats - Enable statistics output from program
38 -style=<string> - Coding style, currently supports: LLVM, Google.
39 -version - Display the version of this program
40
41
42Vim Integration
43===============
44
Dmitri Gribenkoee1230c2013-01-09 22:18:55 +000045There is an integration for :program:`vim` which lets you run the
46:program:`clang-format` standalone tool on your current buffer, optionally
Hans Wennborg280b9562013-02-28 18:16:24 +000047selecting regions to reformat. The integration has the form of a `python`-file
Dmitri Gribenkoee1230c2013-01-09 22:18:55 +000048which can be found under `clang/tools/extra/clang-format/clang-format.py`.
Daniel Jasper6d5b57a2013-01-09 21:49:28 +000049
Hans Wennborg280b9562013-02-28 18:16:24 +000050This can be integrated by adding the following to your `.vimrc`:
Daniel Jasper6d5b57a2013-01-09 21:49:28 +000051
Sean Silvae7259ae2013-03-03 15:17:35 +000052.. code-block:: vim
Daniel Jasper6d5b57a2013-01-09 21:49:28 +000053
54 map <C-I> :pyf <path-to-this-file>/clang-format.py<CR>
55 imap <C-I> <ESC>:pyf <path-to-this-file>/clang-format.py<CR>i
56
Dmitri Gribenkoee1230c2013-01-09 22:18:55 +000057The first line enables :program:`clang-format` for NORMAL and VISUAL mode, the
58second line adds support for INSERT mode. Change "C-I" to another binding if
59you need :program:`clang-format` on a different key (C-I stands for Ctrl+i).
Daniel Jasper6d5b57a2013-01-09 21:49:28 +000060
61With this integration you can press the bound key and clang-format will
62format the current line in NORMAL and INSERT mode or the selected region in
63VISUAL mode. The line or region is extended to the next bigger syntactic
64entity.
65
66It operates on the current, potentially unsaved buffer and does not create
67or save any files. To revert a formatting, just undo.
68
69
70Script for patch reformatting
71=============================
72
73The python script `clang/tools/extra/clang-format-diff.py` parses the output of
Dmitri Gribenkoee1230c2013-01-09 22:18:55 +000074a unified diff and reformats all contained lines with :program:`clang-format`.
Daniel Jasper6d5b57a2013-01-09 21:49:28 +000075
76.. code-block:: console
77
78 usage: clang-format-diff.py [-h] [-p P] [-style STYLE]
79
80 Reformat changed lines in diff
81
82 optional arguments:
83 -h, --help show this help message and exit
84 -p P strip the smallest prefix containing P slashes
85 -style STYLE formatting style to apply (LLVM, Google)
86
Dmitri Gribenkoee1230c2013-01-09 22:18:55 +000087So to reformat all the lines in the latest :program:`git` commit, just do:
Daniel Jasper6d5b57a2013-01-09 21:49:28 +000088
89.. code-block:: console
90
91 git diff -U0 HEAD^ | clang-format-diff.py
Dmitri Gribenkoee1230c2013-01-09 22:18:55 +000092
93The :option:`-U0` will create a diff without context lines (the script would format
Daniel Jasper6d5b57a2013-01-09 21:49:28 +000094those as well).