head	1.1;
branch	1.1.1;
access;
symbols
	perseant-exfatfs-base-20261102:1.1.1.9
	netbsd-11-1-RELEASE:1.1.1.9
	netbsd-10-2-RELEASE:1.1.1.9
	netbsd-9-5-RELEASE:1.1.1.8
	netbsd-11-0-RELEASE:1.1.1.9
	netbsd-11-0-RC7:1.1.1.9
	netbsd-11-0-RC6:1.1.1.9
	netbsd-11-0-RC5:1.1.1.9
	netbsd-11-0-RC4:1.1.1.9
	netbsd-11-0-RC3:1.1.1.9
	netbsd-11-0-RC2:1.1.1.9
	netbsd-11-0-RC1:1.1.1.9
	perseant-exfatfs-base-20250801:1.1.1.9
	netbsd-11:1.1.1.9.0.10
	netbsd-11-base:1.1.1.9
	netbsd-10-1-RELEASE:1.1.1.9
	perseant-exfatfs-base-20240630:1.1.1.9
	perseant-exfatfs:1.1.1.9.0.8
	perseant-exfatfs-base:1.1.1.9
	netbsd-8-3-RELEASE:1.1.1.6
	netbsd-9-4-RELEASE:1.1.1.8
	netbsd-10-0-RELEASE:1.1.1.9
	netbsd-10-0-RC6:1.1.1.9
	netbsd-10-0-RC5:1.1.1.9
	netbsd-10-0-RC4:1.1.1.9
	netbsd-10-0-RC3:1.1.1.9
	netbsd-10-0-RC2:1.1.1.9
	netbsd-10-0-RC1:1.1.1.9
	netbsd-10:1.1.1.9.0.6
	netbsd-10-base:1.1.1.9
	netbsd-9-3-RELEASE:1.1.1.8
	cjep_sun2x:1.1.1.9.0.4
	cjep_sun2x-base:1.1.1.9
	cjep_staticlib_x-base1:1.1.1.9
	netbsd-9-2-RELEASE:1.1.1.8
	cjep_staticlib_x:1.1.1.9.0.2
	cjep_staticlib_x-base:1.1.1.9
	netbsd-9-1-RELEASE:1.1.1.8
	phil-wifi-20200421:1.1.1.9
	phil-wifi-20200411:1.1.1.9
	phil-wifi-20200406:1.1.1.9
	netbsd-8-2-RELEASE:1.1.1.6
	netbsd-9-0-RELEASE:1.1.1.8
	netbsd-9-0-RC2:1.1.1.8
	netbsd-9-0-RC1:1.1.1.8
	netbsd-9:1.1.1.8.0.2
	netbsd-9-base:1.1.1.8
	phil-wifi-20190609:1.1.1.8
	netbsd-8-1-RELEASE:1.1.1.6
	netbsd-8-1-RC1:1.1.1.6
	pgoyette-compat-merge-20190127:1.1.1.7.2.1
	pgoyette-compat-20190127:1.1.1.8
	pgoyette-compat-20190118:1.1.1.8
	pgoyette-compat-1226:1.1.1.8
	pgoyette-compat-1126:1.1.1.8
	pgoyette-compat-1020:1.1.1.8
	pgoyette-compat-0930:1.1.1.8
	pgoyette-compat-0906:1.1.1.8
	netbsd-7-2-RELEASE:1.1.1.3.2.1
	pgoyette-compat-0728:1.1.1.8
	clang-337282:1.1.1.8
	netbsd-8-0-RELEASE:1.1.1.6
	phil-wifi:1.1.1.7.0.4
	phil-wifi-base:1.1.1.7
	pgoyette-compat-0625:1.1.1.7
	netbsd-8-0-RC2:1.1.1.6
	pgoyette-compat-0521:1.1.1.7
	pgoyette-compat-0502:1.1.1.7
	pgoyette-compat-0422:1.1.1.7
	netbsd-8-0-RC1:1.1.1.6
	pgoyette-compat-0415:1.1.1.7
	pgoyette-compat-0407:1.1.1.7
	pgoyette-compat-0330:1.1.1.7
	pgoyette-compat-0322:1.1.1.7
	pgoyette-compat-0315:1.1.1.7
	netbsd-7-1-2-RELEASE:1.1.1.3.2.1
	pgoyette-compat:1.1.1.7.0.2
	pgoyette-compat-base:1.1.1.7
	netbsd-7-1-1-RELEASE:1.1.1.3.2.1
	clang-319952:1.1.1.7
	matt-nb8-mediatek:1.1.1.6.0.10
	matt-nb8-mediatek-base:1.1.1.6
	clang-309604:1.1.1.7
	perseant-stdc-iso10646:1.1.1.6.0.8
	perseant-stdc-iso10646-base:1.1.1.6
	netbsd-8:1.1.1.6.0.6
	netbsd-8-base:1.1.1.6
	prg-localcount2-base3:1.1.1.6
	prg-localcount2-base2:1.1.1.6
	prg-localcount2-base1:1.1.1.6
	prg-localcount2:1.1.1.6.0.4
	prg-localcount2-base:1.1.1.6
	pgoyette-localcount-20170426:1.1.1.6
	bouyer-socketcan-base1:1.1.1.6
	pgoyette-localcount-20170320:1.1.1.6
	netbsd-7-1:1.1.1.3.2.1.0.6
	netbsd-7-1-RELEASE:1.1.1.3.2.1
	netbsd-7-1-RC2:1.1.1.3.2.1
	clang-294123:1.1.1.6
	netbsd-7-nhusb-base-20170116:1.1.1.3.2.1
	bouyer-socketcan:1.1.1.6.0.2
	bouyer-socketcan-base:1.1.1.6
	clang-291444:1.1.1.6
	pgoyette-localcount-20170107:1.1.1.5
	netbsd-7-1-RC1:1.1.1.3.2.1
	pgoyette-localcount-20161104:1.1.1.5
	netbsd-7-0-2-RELEASE:1.1.1.3.2.1
	localcount-20160914:1.1.1.5
	netbsd-7-nhusb:1.1.1.3.2.1.0.4
	netbsd-7-nhusb-base:1.1.1.3.2.1
	clang-280599:1.1.1.5
	pgoyette-localcount-20160806:1.1.1.5
	pgoyette-localcount-20160726:1.1.1.5
	pgoyette-localcount:1.1.1.5.0.2
	pgoyette-localcount-base:1.1.1.5
	netbsd-7-0-1-RELEASE:1.1.1.3.2.1
	clang-261930:1.1.1.5
	netbsd-7-0:1.1.1.3.2.1.0.2
	netbsd-7-0-RELEASE:1.1.1.3.2.1
	netbsd-7-0-RC3:1.1.1.3.2.1
	netbsd-7-0-RC2:1.1.1.3.2.1
	netbsd-7-0-RC1:1.1.1.3.2.1
	clang-237755:1.1.1.4
	clang-232565:1.1.1.4
	clang-227398:1.1.1.4
	tls-maxphys-base:1.1.1.3
	tls-maxphys:1.1.1.3.0.4
	netbsd-7:1.1.1.3.0.2
	netbsd-7-base:1.1.1.3
	clang-215315:1.1.1.3
	clang-209886:1.1.1.3
	yamt-pagecache:1.1.1.2.0.4
	yamt-pagecache-base9:1.1.1.2
	tls-earlyentropy:1.1.1.2.0.2
	tls-earlyentropy-base:1.1.1.3
	riastradh-xf86-video-intel-2-7-1-pre-2-21-15:1.1.1.2
	riastradh-drm2-base3:1.1.1.2
	clang-202566:1.1.1.2
	clang-201163:1.1.1.2
	clang-199312:1.1.1.2
	clang-198450:1.1.1.2
	clang-196603:1.1.1.1
	clang-195771:1.1.1.1
	LLVM:1.1.1;
locks; strict;
comment	@# @;


1.1
date	2013.11.28.14.14.46;	author joerg;	state Exp;
branches
	1.1.1.1;
next	;
commitid	ow8OybrawrB1f3fx;

1.1.1.1
date	2013.11.28.14.14.46;	author joerg;	state Exp;
branches;
next	1.1.1.2;
commitid	ow8OybrawrB1f3fx;

1.1.1.2
date	2014.01.05.15.37.41;	author joerg;	state Exp;
branches
	1.1.1.2.2.1
	1.1.1.2.4.1;
next	1.1.1.3;
commitid	wh3aCSIWykURqWjx;

1.1.1.3
date	2014.05.30.18.14.38;	author joerg;	state Exp;
branches
	1.1.1.3.2.1
	1.1.1.3.4.1;
next	1.1.1.4;
commitid	8q0kdlBlCn09GACx;

1.1.1.4
date	2015.01.29.19.57.42;	author joerg;	state Exp;
branches;
next	1.1.1.5;
commitid	mlISSizlPKvepX7y;

1.1.1.5
date	2016.02.27.22.10.52;	author joerg;	state Exp;
branches
	1.1.1.5.2.1;
next	1.1.1.6;
commitid	tIimz3oDlh1NpBWy;

1.1.1.6
date	2017.01.11.10.40.26;	author joerg;	state Exp;
branches;
next	1.1.1.7;
commitid	CNnUNfII1jgNmxBz;

1.1.1.7
date	2017.08.01.19.38.01;	author joerg;	state Exp;
branches
	1.1.1.7.2.1
	1.1.1.7.4.1;
next	1.1.1.8;
commitid	pMuDy65V0VicSx1A;

1.1.1.8
date	2018.07.17.18.32.11;	author joerg;	state Exp;
branches;
next	1.1.1.9;
commitid	wDzL46ALjrCZgwKA;

1.1.1.9
date	2019.11.13.22.19.10;	author joerg;	state dead;
branches;
next	;
commitid	QD8YATxuNG34YJKB;

1.1.1.2.2.1
date	2014.08.10.07.08.03;	author tls;	state Exp;
branches;
next	;
commitid	t01A1TLTYxkpGMLx;

1.1.1.2.4.1
date	2014.01.05.15.37.41;	author yamt;	state dead;
branches;
next	1.1.1.2.4.2;
commitid	WSrDtL5nYAUyiyBx;

1.1.1.2.4.2
date	2014.05.22.16.18.18;	author yamt;	state Exp;
branches;
next	;
commitid	WSrDtL5nYAUyiyBx;

1.1.1.3.2.1
date	2015.06.04.20.04.23;	author snj;	state Exp;
branches;
next	;
commitid	yRnjq9fueSo6n9oy;

1.1.1.3.4.1
date	2014.05.30.18.14.38;	author tls;	state dead;
branches;
next	1.1.1.3.4.2;
commitid	jTnpym9Qu0o4R1Nx;

1.1.1.3.4.2
date	2014.08.19.23.47.19;	author tls;	state Exp;
branches;
next	;
commitid	jTnpym9Qu0o4R1Nx;

1.1.1.5.2.1
date	2017.03.20.06.52.30;	author pgoyette;	state Exp;
branches;
next	;
commitid	jjw7cAwgyKq7RfKz;

1.1.1.7.2.1
date	2018.07.28.04.33.07;	author pgoyette;	state Exp;
branches;
next	;
commitid	1UP1xAIUxv1ZgRLA;

1.1.1.7.4.1
date	2019.06.10.21.45.08;	author christos;	state Exp;
branches;
next	1.1.1.7.4.2;
commitid	jtc8rnCzWiEEHGqB;

1.1.1.7.4.2
date	2020.04.13.07.46.20;	author martin;	state dead;
branches;
next	;
commitid	X01YhRUPVUDaec4C;


desc
@@


1.1
log
@Initial revision
@
text
@===========
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
===============

:program:`clang-format` is located in `clang/tools/clang-format` and can be used
to format C/C++/Obj-C code.

.. code-block:: console

  $ clang-format -help
  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.
  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
  result is written to the standard output.

  USAGE: clang-format [options] [<file> ...]

  OPTIONS:

  Clang-format options:

    -cursor=<uint>           - The position of the cursor when invoking
                               clang-format from an editor integration
    -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.
    -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.
    -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:
                                 LLVM, Google, Chromium, Mozilla, WebKit.
                               Use -style=file to load style configuration from
                               .clang-format file located in one of the parent
                               directories of the source file (or current
                               directory for stdin).
                               Use -style="{key: value, ...}" to set specific
                               parameters, e.g.:
                                 -style="{BasedOnStyle: llvm, IndentWidth: 8}"

  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


When the desired code formatting style is different from the available options,
the style can be customized using the ``-style="{key: value, ...}"`` option or
by putting your style configuration in the ``.clang-format`` or ``_clang-format``
file in your project's directory and using ``clang-format -style=file``.

An easy way to create the ``.clang-format`` file is:

.. code-block:: console

  clang-format -style=llvm -dump-config > .clang-format

Available style options are described in :doc:`ClangFormatStyleOptions`.


Vim Integration
===============

There is an integration for :program:`vim` which lets you run the
:program:`clang-format` standalone tool on your current buffer, optionally
selecting regions to reformat. The integration has the form of a `python`-file
which can be found under `clang/tools/clang-format/clang-format.py`.

This can be integrated by adding the following to your `.vimrc`:

.. code-block:: vim

  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

The first line enables :program:`clang-format` for NORMAL and VISUAL mode, the
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).

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.


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.


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.


Visual Studio Integration
=========================

Download the latest Visual Studio plugin from the `alpha build site
<http://llvm.org/builds/>`_. The default key-binding is Ctrl-R,Ctrl-F.


Script for patch reformatting
=============================

The python script `clang/tools/clang-format-diff.py` parses the output of
a unified diff and reformats all contained lines with :program:`clang-format`.

.. code-block:: console

  usage: clang-format-diff.py [-h] [-p P] [-style STYLE]

  Reformat changed lines in diff.

  optional arguments:
    -h, --help    show this help message and exit
    -p P          strip the smallest prefix containing P slashes
    -style STYLE  formatting style to apply (LLVM, Google, Chromium, Mozilla,
                  WebKit)

So to reformat all the lines in the latest :program:`git` commit, just do:

.. code-block:: console

  git diff -U0 HEAD^ | clang-format-diff.py -p1

The :option:`-U0` will create a diff without context lines (the script would format
those as well).
@


1.1.1.1
log
@Import Clang 3.4rc1 r195771.
@
text
@@


1.1.1.2
log
@Import clang 3.5svn r198450.
@
text
@d149 1
a149 1
Download the latest Visual Studio extension from the `alpha build site
d161 1
a161 1
  usage: clang-format-diff.py [-h] [-i] [-p NUM] [-regex PATTERN] [-style STYLE]
d163 1
a163 2
  Reformat changed lines in diff. Without -i option just output the diff that
  would be introduced.
d166 4
a169 6
    -h, --help      show this help message and exit
    -i              apply edits to files instead of displaying a diff
    -p NUM          strip the smallest prefix containing P slashes
    -regex PATTERN  custom pattern selecting file paths to reformat
    -style STYLE    formatting style to apply (LLVM, Google, Chromium, Mozilla,
                    WebKit)
d175 1
a175 1
  git diff -U0 HEAD^ | clang-format-diff.py -i -p1
@


1.1.1.2.2.1
log
@Rebase.
@
text
@a179 6
In an SVN client, you can do:

.. code-block:: console

  svn diff --diff-cmd=diff -x-U0 | clang-format-diff.py -i

@


1.1.1.3
log
@Import Clang 3.5svn r209886.
@
text
@a179 6
In an SVN client, you can do:

.. code-block:: console

  svn diff --diff-cmd=diff -x-U0 | clang-format-diff.py -i

@


1.1.1.3.2.1
log
@Update LLVM to 3.6.1, requested by joerg in ticket 824.
@
text
@d99 2
a100 2
  map <C-K> :pyf <path-to-this-file>/clang-format.py<cr>
  imap <C-K> <c-o>:pyf <path-to-this-file>/clang-format.py<cr>
@


1.1.1.4
log
@Import Clang 3.6RC1 r227398.
@
text
@d99 2
a100 2
  map <C-K> :pyf <path-to-this-file>/clang-format.py<cr>
  imap <C-K> <c-o>:pyf <path-to-this-file>/clang-format.py<cr>
@


1.1.1.5
log
@Import Clang 3.8.0rc3 r261930.
@
text
@d19 1
a19 1
  OVERVIEW: A tool to format C/C++/Java/JavaScript/Objective-C/Protobuf code.
d33 38
a70 47
    -assume-filename=<string> - When reading from stdin, clang-format assumes this
                                filename to look for a style config file (with
                                -style=file) and to determine the language.
    -cursor=<uint>            - The position of the cursor when invoking
                                clang-format from an editor integration
    -dump-config              - Dump configuration options to stdout and exit.
                                Can be used with -style option.
    -fallback-style=<string>  - The name of the predefined style used as a
                                fallback in case clang-format is invoked with
                                -style=file, but can not find the .clang-format
                                file to use.
                                Use -fallback-style=none to skip formatting.
    -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.
    -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.
    -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.
    -sort-includes            - Sort touched include lines
    -style=<string>           - Coding style, currently supports:
                                  LLVM, Google, Chromium, Mozilla, WebKit.
                                Use -style=file to load style configuration from
                                .clang-format file located in one of the parent
                                directories of the source file (or current
                                directory for stdin).
                                Use -style="{key: value, ...}" to set specific
                                parameters, e.g.:
                                  -style="{BasedOnStyle: llvm, IndentWidth: 8}"

  Generic 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
@


1.1.1.5.2.1
log
@Sync with HEAD
@
text
@d187 1
a187 1
  git diff -U0 --no-color HEAD^ | clang-format-diff.py -i -p1
d193 1
a193 1
  svn diff --diff-cmd=diff -x -U0 | clang-format-diff.py -i
d195 1
a195 1
The option `-U0` will create a diff without context lines (the script would format
@


1.1.1.6
log
@Import Clang pre-4.0.0 r291444.
@
text
@d187 1
a187 1
  git diff -U0 --no-color HEAD^ | clang-format-diff.py -i -p1
d193 1
a193 1
  svn diff --diff-cmd=diff -x -U0 | clang-format-diff.py -i
d195 1
a195 1
The option `-U0` will create a diff without context lines (the script would format
@


1.1.1.7
log
@Import clang r309604 from branches/release_50
@
text
@a122 12
An alternative option is to format changes when saving a file and thus to
have a zero-effort integration into the coding workflow. To do this, add this to
your `.vimrc`:

.. code-block:: vim

  function! Formatonsave()
    let l:formatdiff = 1
    pyf ~/llvm/tools/clang/tools/clang-format/clang-format.py
  endfunction
  autocmd BufWritePre *.h,*.cc,*.cpp call Formatonsave()

d165 2
a166 3
The python script `clang/tools/clang-format/clang-format-diff.py` parses the
output of a unified diff and reformats all contained lines with
:program:`clang-format`.
@


1.1.1.7.4.1
log
@Sync with HEAD
@
text
@d14 1
a14 1
to format C/C++/Java/JavaScript/Objective-C/Protobuf code.
a73 1
    -verbose                  - If set, shows the list of processed files
@


1.1.1.7.4.2
log
@Mostly merge changes from HEAD upto 20200411
@
text
@@


1.1.1.7.2.1
log
@Sync with HEAD
@
text
@d14 1
a14 1
to format C/C++/Java/JavaScript/Objective-C/Protobuf code.
a73 1
    -verbose                  - If set, shows the list of processed files
@


1.1.1.8
log
@Import clang r337282 from trunk
@
text
@d14 1
a14 1
to format C/C++/Java/JavaScript/Objective-C/Protobuf code.
a73 1
    -verbose                  - If set, shows the list of processed files
@


1.1.1.9
log
@Mark old LLVM instance as dead.
@
text
@@


1.1.1.3.4.1
log
@file ClangFormat.rst was added on branch tls-maxphys on 2014-08-19 23:47:19 +0000
@
text
@d1 187
@


1.1.1.3.4.2
log
@Rebase to HEAD as of a few days ago.
@
text
@a0 187
===========
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
===============

:program:`clang-format` is located in `clang/tools/clang-format` and can be used
to format C/C++/Obj-C code.

.. code-block:: console

  $ clang-format -help
  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.
  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
  result is written to the standard output.

  USAGE: clang-format [options] [<file> ...]

  OPTIONS:

  Clang-format options:

    -cursor=<uint>           - The position of the cursor when invoking
                               clang-format from an editor integration
    -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.
    -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.
    -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:
                                 LLVM, Google, Chromium, Mozilla, WebKit.
                               Use -style=file to load style configuration from
                               .clang-format file located in one of the parent
                               directories of the source file (or current
                               directory for stdin).
                               Use -style="{key: value, ...}" to set specific
                               parameters, e.g.:
                                 -style="{BasedOnStyle: llvm, IndentWidth: 8}"

  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


When the desired code formatting style is different from the available options,
the style can be customized using the ``-style="{key: value, ...}"`` option or
by putting your style configuration in the ``.clang-format`` or ``_clang-format``
file in your project's directory and using ``clang-format -style=file``.

An easy way to create the ``.clang-format`` file is:

.. code-block:: console

  clang-format -style=llvm -dump-config > .clang-format

Available style options are described in :doc:`ClangFormatStyleOptions`.


Vim Integration
===============

There is an integration for :program:`vim` which lets you run the
:program:`clang-format` standalone tool on your current buffer, optionally
selecting regions to reformat. The integration has the form of a `python`-file
which can be found under `clang/tools/clang-format/clang-format.py`.

This can be integrated by adding the following to your `.vimrc`:

.. code-block:: vim

  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

The first line enables :program:`clang-format` for NORMAL and VISUAL mode, the
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).

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.


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.


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.


Visual Studio Integration
=========================

Download the latest Visual Studio extension from the `alpha build site
<http://llvm.org/builds/>`_. The default key-binding is Ctrl-R,Ctrl-F.


Script for patch reformatting
=============================

The python script `clang/tools/clang-format-diff.py` parses the output of
a unified diff and reformats all contained lines with :program:`clang-format`.

.. code-block:: console

  usage: clang-format-diff.py [-h] [-i] [-p NUM] [-regex PATTERN] [-style STYLE]

  Reformat changed lines in diff. Without -i option just output the diff that
  would be introduced.

  optional arguments:
    -h, --help      show this help message and exit
    -i              apply edits to files instead of displaying a diff
    -p NUM          strip the smallest prefix containing P slashes
    -regex PATTERN  custom pattern selecting file paths to reformat
    -style STYLE    formatting style to apply (LLVM, Google, Chromium, Mozilla,
                    WebKit)

So to reformat all the lines in the latest :program:`git` commit, just do:

.. code-block:: console

  git diff -U0 HEAD^ | clang-format-diff.py -i -p1

In an SVN client, you can do:

.. code-block:: console

  svn diff --diff-cmd=diff -x-U0 | clang-format-diff.py -i

The :option:`-U0` will create a diff without context lines (the script would format
those as well).
@


1.1.1.2.4.1
log
@file ClangFormat.rst was added on branch yamt-pagecache on 2014-05-22 16:18:18 +0000
@
text
@d1 181
@


1.1.1.2.4.2
log
@sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs.  ("Protocol error: too many arguments")
@
text
@a0 181
===========
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
===============

:program:`clang-format` is located in `clang/tools/clang-format` and can be used
to format C/C++/Obj-C code.

.. code-block:: console

  $ clang-format -help
  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.
  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
  result is written to the standard output.

  USAGE: clang-format [options] [<file> ...]

  OPTIONS:

  Clang-format options:

    -cursor=<uint>           - The position of the cursor when invoking
                               clang-format from an editor integration
    -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.
    -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.
    -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:
                                 LLVM, Google, Chromium, Mozilla, WebKit.
                               Use -style=file to load style configuration from
                               .clang-format file located in one of the parent
                               directories of the source file (or current
                               directory for stdin).
                               Use -style="{key: value, ...}" to set specific
                               parameters, e.g.:
                                 -style="{BasedOnStyle: llvm, IndentWidth: 8}"

  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


When the desired code formatting style is different from the available options,
the style can be customized using the ``-style="{key: value, ...}"`` option or
by putting your style configuration in the ``.clang-format`` or ``_clang-format``
file in your project's directory and using ``clang-format -style=file``.

An easy way to create the ``.clang-format`` file is:

.. code-block:: console

  clang-format -style=llvm -dump-config > .clang-format

Available style options are described in :doc:`ClangFormatStyleOptions`.


Vim Integration
===============

There is an integration for :program:`vim` which lets you run the
:program:`clang-format` standalone tool on your current buffer, optionally
selecting regions to reformat. The integration has the form of a `python`-file
which can be found under `clang/tools/clang-format/clang-format.py`.

This can be integrated by adding the following to your `.vimrc`:

.. code-block:: vim

  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

The first line enables :program:`clang-format` for NORMAL and VISUAL mode, the
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).

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.


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.


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.


Visual Studio Integration
=========================

Download the latest Visual Studio extension from the `alpha build site
<http://llvm.org/builds/>`_. The default key-binding is Ctrl-R,Ctrl-F.


Script for patch reformatting
=============================

The python script `clang/tools/clang-format-diff.py` parses the output of
a unified diff and reformats all contained lines with :program:`clang-format`.

.. code-block:: console

  usage: clang-format-diff.py [-h] [-i] [-p NUM] [-regex PATTERN] [-style STYLE]

  Reformat changed lines in diff. Without -i option just output the diff that
  would be introduced.

  optional arguments:
    -h, --help      show this help message and exit
    -i              apply edits to files instead of displaying a diff
    -p NUM          strip the smallest prefix containing P slashes
    -regex PATTERN  custom pattern selecting file paths to reformat
    -style STYLE    formatting style to apply (LLVM, Google, Chromium, Mozilla,
                    WebKit)

So to reformat all the lines in the latest :program:`git` commit, just do:

.. code-block:: console

  git diff -U0 HEAD^ | clang-format-diff.py -i -p1

The :option:`-U0` will create a diff without context lines (the script would format
those as well).
@


