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