2013-01-10 05:49:28 +08:00
|
|
|
===========
|
|
|
|
ClangFormat
|
|
|
|
===========
|
|
|
|
|
|
|
|
`ClangFormat` describes a set of tools that are built on top of
|
|
|
|
:doc:`LibFormat`. It can support your workflow in a variety of ways including a
|
|
|
|
standalone tool and editor integrations.
|
|
|
|
|
|
|
|
|
|
|
|
Standalone Tool
|
|
|
|
===============
|
|
|
|
|
2013-03-22 20:44:20 +08:00
|
|
|
:program:`clang-format` is located in `clang/tools/clang-format` and can be used
|
|
|
|
to format C/C++/Obj-C code.
|
2013-01-10 05:49:28 +08:00
|
|
|
|
|
|
|
.. code-block:: console
|
|
|
|
|
2013-09-02 23:30:26 +08:00
|
|
|
$ clang-format -help
|
2013-01-10 05:49:28 +08:00
|
|
|
OVERVIEW: A tool to format C/C++/Obj-C code.
|
|
|
|
|
|
|
|
If no arguments are specified, it formats the code from standard input
|
|
|
|
and writes the result to the standard output.
|
2013-09-02 23:30:26 +08:00
|
|
|
If <file>s are given, it reformats the files. If -i is specified
|
|
|
|
together with <file>s, the files are edited in-place. Otherwise, the
|
2013-05-11 02:12:00 +08:00
|
|
|
result is written to the standard output.
|
2013-01-10 05:49:28 +08:00
|
|
|
|
2013-05-11 02:12:00 +08:00
|
|
|
USAGE: clang-format [options] [<file> ...]
|
2013-01-10 05:49:28 +08:00
|
|
|
|
|
|
|
OPTIONS:
|
2013-05-11 02:12:00 +08:00
|
|
|
|
|
|
|
Clang-format options:
|
|
|
|
|
2013-09-02 23:30:26 +08:00
|
|
|
-cursor=<uint> - The position of the cursor when invoking
|
|
|
|
clang-format from an editor integration
|
2013-05-11 02:12:00 +08:00
|
|
|
-dump-config - Dump configuration options to stdout and exit.
|
|
|
|
Can be used with -style option.
|
|
|
|
-i - Inplace edit <file>s, if specified.
|
|
|
|
-length=<uint> - Format a range of this length (in bytes).
|
|
|
|
Multiple ranges can be formatted by specifying
|
|
|
|
several -offset and -length pairs.
|
|
|
|
When only a single -offset is specified without
|
|
|
|
-length, clang-format will format up to the end
|
|
|
|
of the file.
|
|
|
|
Can only be used with one input file.
|
2013-09-02 23:30:26 +08:00
|
|
|
-lines=<string> - <start line>:<end line> - format a range of
|
|
|
|
lines (both 1-based).
|
|
|
|
Multiple ranges can be formatted by specifying
|
|
|
|
several -lines arguments.
|
|
|
|
Can't be used with -offset and -length.
|
|
|
|
Can only be used with one input file.
|
2013-05-11 02:12:00 +08:00
|
|
|
-offset=<uint> - Format a range starting at this byte offset.
|
|
|
|
Multiple ranges can be formatted by specifying
|
|
|
|
several -offset and -length pairs.
|
|
|
|
Can only be used with one input file.
|
|
|
|
-output-replacements-xml - Output replacements as XML.
|
|
|
|
-style=<string> - Coding style, currently supports:
|
2013-09-03 00:39:23 +08:00
|
|
|
LLVM, Google, Chromium, Mozilla, WebKit.
|
2013-05-19 08:53:30 +08:00
|
|
|
Use -style=file to load style configuration from
|
2013-05-11 02:12:00 +08:00
|
|
|
.clang-format file located in one of the parent
|
|
|
|
directories of the source file (or current
|
|
|
|
directory for stdin).
|
2013-05-19 08:53:30 +08:00
|
|
|
Use -style="{key: value, ...}" to set specific
|
|
|
|
parameters, e.g.:
|
|
|
|
-style="{BasedOnStyle: llvm, IndentWidth: 8}"
|
2013-05-11 02:12:00 +08:00
|
|
|
|
|
|
|
General options:
|
|
|
|
|
|
|
|
-help - Display available options (-help-hidden for more)
|
|
|
|
-help-list - Display list of available options (-help-list-hidden for more)
|
|
|
|
-version - Display the version of this program
|
2013-01-10 05:49:28 +08:00
|
|
|
|
|
|
|
|
|
|
|
Vim Integration
|
|
|
|
===============
|
|
|
|
|
2013-01-10 06:18:55 +08:00
|
|
|
There is an integration for :program:`vim` which lets you run the
|
|
|
|
:program:`clang-format` standalone tool on your current buffer, optionally
|
2013-03-01 02:16:24 +08:00
|
|
|
selecting regions to reformat. The integration has the form of a `python`-file
|
2013-03-22 20:44:20 +08:00
|
|
|
which can be found under `clang/tools/clang-format/clang-format.py`.
|
2013-01-10 05:49:28 +08:00
|
|
|
|
2013-03-01 02:16:24 +08:00
|
|
|
This can be integrated by adding the following to your `.vimrc`:
|
2013-01-10 05:49:28 +08:00
|
|
|
|
2013-03-03 23:17:35 +08:00
|
|
|
.. code-block:: vim
|
2013-01-10 05:49:28 +08:00
|
|
|
|
2013-03-22 20:44:20 +08:00
|
|
|
map <C-K> :pyf <path-to-this-file>/clang-format.py<CR>
|
|
|
|
imap <C-K> <ESC>:pyf <path-to-this-file>/clang-format.py<CR>i
|
2013-01-10 05:49:28 +08:00
|
|
|
|
2013-01-10 06:18:55 +08:00
|
|
|
The first line enables :program:`clang-format` for NORMAL and VISUAL mode, the
|
2013-03-22 20:44:20 +08:00
|
|
|
second line adds support for INSERT mode. Change "C-K" to another binding if
|
|
|
|
you need :program:`clang-format` on a different key (C-K stands for Ctrl+k).
|
2013-01-10 05:49:28 +08:00
|
|
|
|
|
|
|
With this integration you can press the bound key and clang-format will
|
|
|
|
format the current line in NORMAL and INSERT mode or the selected region in
|
|
|
|
VISUAL mode. The line or region is extended to the next bigger syntactic
|
|
|
|
entity.
|
|
|
|
|
|
|
|
It operates on the current, potentially unsaved buffer and does not create
|
|
|
|
or save any files. To revert a formatting, just undo.
|
|
|
|
|
|
|
|
|
2013-04-17 15:55:02 +08:00
|
|
|
Emacs Integration
|
|
|
|
=================
|
|
|
|
|
|
|
|
Similar to the integration for :program:`vim`, there is an integration for
|
|
|
|
:program:`emacs`. It can be found at `clang/tools/clang-format/clang-format.el`
|
|
|
|
and used by adding this to your `.emacs`:
|
|
|
|
|
|
|
|
.. code-block:: common-lisp
|
|
|
|
|
|
|
|
(load "<path-to-clang>/tools/clang-format/clang-format.el")
|
|
|
|
(global-set-key [C-M-tab] 'clang-format-region)
|
|
|
|
|
|
|
|
This binds the function `clang-format-region` to C-M-tab, which then formats the
|
|
|
|
current line or selected region.
|
|
|
|
|
|
|
|
|
2013-05-03 01:37:36 +08:00
|
|
|
BBEdit Integration
|
|
|
|
==================
|
|
|
|
|
|
|
|
:program:`clang-format` cannot be used as a text filter with BBEdit, but works
|
|
|
|
well via a script. The AppleScript to do this integration can be found at
|
|
|
|
`clang/tools/clang-format/clang-format-bbedit.applescript`; place a copy in
|
|
|
|
`~/Library/Application Support/BBEdit/Scripts`, and edit the path within it to
|
|
|
|
point to your local copy of :program:`clang-format`.
|
|
|
|
|
|
|
|
With this integration you can select the script from the Script menu and
|
|
|
|
:program:`clang-format` will format the selection. Note that you can rename the
|
|
|
|
menu item by renaming the script, and can assign the menu item a keyboard
|
|
|
|
shortcut in the BBEdit preferences, under Menus & Shortcuts.
|
|
|
|
|
|
|
|
|
2013-01-10 05:49:28 +08:00
|
|
|
Script for patch reformatting
|
|
|
|
=============================
|
|
|
|
|
2013-03-22 20:44:20 +08:00
|
|
|
The python script `clang/tools/clang-format-diff.py` parses the output of
|
2013-01-10 06:18:55 +08:00
|
|
|
a unified diff and reformats all contained lines with :program:`clang-format`.
|
2013-01-10 05:49:28 +08:00
|
|
|
|
|
|
|
.. code-block:: console
|
|
|
|
|
|
|
|
usage: clang-format-diff.py [-h] [-p P] [-style STYLE]
|
|
|
|
|
2013-09-03 00:39:23 +08:00
|
|
|
Reformat changed lines in diff.
|
2013-01-10 05:49:28 +08:00
|
|
|
|
|
|
|
optional arguments:
|
|
|
|
-h, --help show this help message and exit
|
|
|
|
-p P strip the smallest prefix containing P slashes
|
2013-09-03 00:39:23 +08:00
|
|
|
-style STYLE formatting style to apply (LLVM, Google, Chromium, Mozilla,
|
|
|
|
WebKit)
|
2013-01-10 05:49:28 +08:00
|
|
|
|
2013-01-10 06:18:55 +08:00
|
|
|
So to reformat all the lines in the latest :program:`git` commit, just do:
|
2013-01-10 05:49:28 +08:00
|
|
|
|
|
|
|
.. code-block:: console
|
|
|
|
|
2013-06-14 10:12:06 +08:00
|
|
|
git diff -U0 HEAD^ | clang-format-diff.py -p1
|
2013-01-10 06:18:55 +08:00
|
|
|
|
|
|
|
The :option:`-U0` will create a diff without context lines (the script would format
|
2013-01-10 05:49:28 +08:00
|
|
|
those as well).
|