1f4a2713aSLionel Sambuc=========== 2f4a2713aSLionel SambucClangFormat 3f4a2713aSLionel Sambuc=========== 4f4a2713aSLionel Sambuc 5f4a2713aSLionel Sambuc`ClangFormat` describes a set of tools that are built on top of 6f4a2713aSLionel Sambuc:doc:`LibFormat`. It can support your workflow in a variety of ways including a 7f4a2713aSLionel Sambucstandalone tool and editor integrations. 8f4a2713aSLionel Sambuc 9f4a2713aSLionel Sambuc 10f4a2713aSLionel SambucStandalone Tool 11f4a2713aSLionel Sambuc=============== 12f4a2713aSLionel Sambuc 13f4a2713aSLionel Sambuc:program:`clang-format` is located in `clang/tools/clang-format` and can be used 14f4a2713aSLionel Sambucto format C/C++/Obj-C code. 15f4a2713aSLionel Sambuc 16f4a2713aSLionel Sambuc.. code-block:: console 17f4a2713aSLionel Sambuc 18f4a2713aSLionel Sambuc $ clang-format -help 19f4a2713aSLionel Sambuc OVERVIEW: A tool to format C/C++/Obj-C code. 20f4a2713aSLionel Sambuc 21f4a2713aSLionel Sambuc If no arguments are specified, it formats the code from standard input 22f4a2713aSLionel Sambuc and writes the result to the standard output. 23f4a2713aSLionel Sambuc If <file>s are given, it reformats the files. If -i is specified 24f4a2713aSLionel Sambuc together with <file>s, the files are edited in-place. Otherwise, the 25f4a2713aSLionel Sambuc result is written to the standard output. 26f4a2713aSLionel Sambuc 27f4a2713aSLionel Sambuc USAGE: clang-format [options] [<file> ...] 28f4a2713aSLionel Sambuc 29f4a2713aSLionel Sambuc OPTIONS: 30f4a2713aSLionel Sambuc 31f4a2713aSLionel Sambuc Clang-format options: 32f4a2713aSLionel Sambuc 33f4a2713aSLionel Sambuc -cursor=<uint> - The position of the cursor when invoking 34f4a2713aSLionel Sambuc clang-format from an editor integration 35f4a2713aSLionel Sambuc -dump-config - Dump configuration options to stdout and exit. 36f4a2713aSLionel Sambuc Can be used with -style option. 37f4a2713aSLionel Sambuc -i - Inplace edit <file>s, if specified. 38f4a2713aSLionel Sambuc -length=<uint> - Format a range of this length (in bytes). 39f4a2713aSLionel Sambuc Multiple ranges can be formatted by specifying 40f4a2713aSLionel Sambuc several -offset and -length pairs. 41f4a2713aSLionel Sambuc When only a single -offset is specified without 42f4a2713aSLionel Sambuc -length, clang-format will format up to the end 43f4a2713aSLionel Sambuc of the file. 44f4a2713aSLionel Sambuc Can only be used with one input file. 45f4a2713aSLionel Sambuc -lines=<string> - <start line>:<end line> - format a range of 46f4a2713aSLionel Sambuc lines (both 1-based). 47f4a2713aSLionel Sambuc Multiple ranges can be formatted by specifying 48f4a2713aSLionel Sambuc several -lines arguments. 49f4a2713aSLionel Sambuc Can't be used with -offset and -length. 50f4a2713aSLionel Sambuc Can only be used with one input file. 51f4a2713aSLionel Sambuc -offset=<uint> - Format a range starting at this byte offset. 52f4a2713aSLionel Sambuc Multiple ranges can be formatted by specifying 53f4a2713aSLionel Sambuc several -offset and -length pairs. 54f4a2713aSLionel Sambuc Can only be used with one input file. 55f4a2713aSLionel Sambuc -output-replacements-xml - Output replacements as XML. 56f4a2713aSLionel Sambuc -style=<string> - Coding style, currently supports: 57f4a2713aSLionel Sambuc LLVM, Google, Chromium, Mozilla, WebKit. 58f4a2713aSLionel Sambuc Use -style=file to load style configuration from 59f4a2713aSLionel Sambuc .clang-format file located in one of the parent 60f4a2713aSLionel Sambuc directories of the source file (or current 61f4a2713aSLionel Sambuc directory for stdin). 62f4a2713aSLionel Sambuc Use -style="{key: value, ...}" to set specific 63f4a2713aSLionel Sambuc parameters, e.g.: 64f4a2713aSLionel Sambuc -style="{BasedOnStyle: llvm, IndentWidth: 8}" 65f4a2713aSLionel Sambuc 66f4a2713aSLionel Sambuc General options: 67f4a2713aSLionel Sambuc 68f4a2713aSLionel Sambuc -help - Display available options (-help-hidden for more) 69f4a2713aSLionel Sambuc -help-list - Display list of available options (-help-list-hidden for more) 70f4a2713aSLionel Sambuc -version - Display the version of this program 71f4a2713aSLionel Sambuc 72f4a2713aSLionel Sambuc 73f4a2713aSLionel SambucWhen the desired code formatting style is different from the available options, 74f4a2713aSLionel Sambucthe style can be customized using the ``-style="{key: value, ...}"`` option or 75f4a2713aSLionel Sambucby putting your style configuration in the ``.clang-format`` or ``_clang-format`` 76f4a2713aSLionel Sambucfile in your project's directory and using ``clang-format -style=file``. 77f4a2713aSLionel Sambuc 78f4a2713aSLionel SambucAn easy way to create the ``.clang-format`` file is: 79f4a2713aSLionel Sambuc 80f4a2713aSLionel Sambuc.. code-block:: console 81f4a2713aSLionel Sambuc 82f4a2713aSLionel Sambuc clang-format -style=llvm -dump-config > .clang-format 83f4a2713aSLionel Sambuc 84f4a2713aSLionel SambucAvailable style options are described in :doc:`ClangFormatStyleOptions`. 85f4a2713aSLionel Sambuc 86f4a2713aSLionel Sambuc 87f4a2713aSLionel SambucVim Integration 88f4a2713aSLionel Sambuc=============== 89f4a2713aSLionel Sambuc 90f4a2713aSLionel SambucThere is an integration for :program:`vim` which lets you run the 91f4a2713aSLionel Sambuc:program:`clang-format` standalone tool on your current buffer, optionally 92f4a2713aSLionel Sambucselecting regions to reformat. The integration has the form of a `python`-file 93f4a2713aSLionel Sambucwhich can be found under `clang/tools/clang-format/clang-format.py`. 94f4a2713aSLionel Sambuc 95f4a2713aSLionel SambucThis can be integrated by adding the following to your `.vimrc`: 96f4a2713aSLionel Sambuc 97f4a2713aSLionel Sambuc.. code-block:: vim 98f4a2713aSLionel Sambuc 99*0a6a1f1dSLionel Sambuc map <C-K> :pyf <path-to-this-file>/clang-format.py<cr> 100*0a6a1f1dSLionel Sambuc imap <C-K> <c-o>:pyf <path-to-this-file>/clang-format.py<cr> 101f4a2713aSLionel Sambuc 102f4a2713aSLionel SambucThe first line enables :program:`clang-format` for NORMAL and VISUAL mode, the 103f4a2713aSLionel Sambucsecond line adds support for INSERT mode. Change "C-K" to another binding if 104f4a2713aSLionel Sambucyou need :program:`clang-format` on a different key (C-K stands for Ctrl+k). 105f4a2713aSLionel Sambuc 106f4a2713aSLionel SambucWith this integration you can press the bound key and clang-format will 107f4a2713aSLionel Sambucformat the current line in NORMAL and INSERT mode or the selected region in 108f4a2713aSLionel SambucVISUAL mode. The line or region is extended to the next bigger syntactic 109f4a2713aSLionel Sambucentity. 110f4a2713aSLionel Sambuc 111f4a2713aSLionel SambucIt operates on the current, potentially unsaved buffer and does not create 112f4a2713aSLionel Sambucor save any files. To revert a formatting, just undo. 113f4a2713aSLionel Sambuc 114f4a2713aSLionel Sambuc 115f4a2713aSLionel SambucEmacs Integration 116f4a2713aSLionel Sambuc================= 117f4a2713aSLionel Sambuc 118f4a2713aSLionel SambucSimilar to the integration for :program:`vim`, there is an integration for 119f4a2713aSLionel Sambuc:program:`emacs`. It can be found at `clang/tools/clang-format/clang-format.el` 120f4a2713aSLionel Sambucand used by adding this to your `.emacs`: 121f4a2713aSLionel Sambuc 122f4a2713aSLionel Sambuc.. code-block:: common-lisp 123f4a2713aSLionel Sambuc 124f4a2713aSLionel Sambuc (load "<path-to-clang>/tools/clang-format/clang-format.el") 125f4a2713aSLionel Sambuc (global-set-key [C-M-tab] 'clang-format-region) 126f4a2713aSLionel Sambuc 127f4a2713aSLionel SambucThis binds the function `clang-format-region` to C-M-tab, which then formats the 128f4a2713aSLionel Sambuccurrent line or selected region. 129f4a2713aSLionel Sambuc 130f4a2713aSLionel Sambuc 131f4a2713aSLionel SambucBBEdit Integration 132f4a2713aSLionel Sambuc================== 133f4a2713aSLionel Sambuc 134f4a2713aSLionel Sambuc:program:`clang-format` cannot be used as a text filter with BBEdit, but works 135f4a2713aSLionel Sambucwell via a script. The AppleScript to do this integration can be found at 136f4a2713aSLionel Sambuc`clang/tools/clang-format/clang-format-bbedit.applescript`; place a copy in 137f4a2713aSLionel Sambuc`~/Library/Application Support/BBEdit/Scripts`, and edit the path within it to 138f4a2713aSLionel Sambucpoint to your local copy of :program:`clang-format`. 139f4a2713aSLionel Sambuc 140f4a2713aSLionel SambucWith this integration you can select the script from the Script menu and 141f4a2713aSLionel Sambuc:program:`clang-format` will format the selection. Note that you can rename the 142f4a2713aSLionel Sambucmenu item by renaming the script, and can assign the menu item a keyboard 143f4a2713aSLionel Sambucshortcut in the BBEdit preferences, under Menus & Shortcuts. 144f4a2713aSLionel Sambuc 145f4a2713aSLionel Sambuc 146f4a2713aSLionel SambucVisual Studio Integration 147f4a2713aSLionel Sambuc========================= 148f4a2713aSLionel Sambuc 149*0a6a1f1dSLionel SambucDownload the latest Visual Studio extension from the `alpha build site 150f4a2713aSLionel Sambuc<http://llvm.org/builds/>`_. The default key-binding is Ctrl-R,Ctrl-F. 151f4a2713aSLionel Sambuc 152f4a2713aSLionel Sambuc 153f4a2713aSLionel SambucScript for patch reformatting 154f4a2713aSLionel Sambuc============================= 155f4a2713aSLionel Sambuc 156f4a2713aSLionel SambucThe python script `clang/tools/clang-format-diff.py` parses the output of 157f4a2713aSLionel Sambuca unified diff and reformats all contained lines with :program:`clang-format`. 158f4a2713aSLionel Sambuc 159f4a2713aSLionel Sambuc.. code-block:: console 160f4a2713aSLionel Sambuc 161*0a6a1f1dSLionel Sambuc usage: clang-format-diff.py [-h] [-i] [-p NUM] [-regex PATTERN] [-style STYLE] 162f4a2713aSLionel Sambuc 163*0a6a1f1dSLionel Sambuc Reformat changed lines in diff. Without -i option just output the diff that 164*0a6a1f1dSLionel Sambuc would be introduced. 165f4a2713aSLionel Sambuc 166f4a2713aSLionel Sambuc optional arguments: 167f4a2713aSLionel Sambuc -h, --help show this help message and exit 168*0a6a1f1dSLionel Sambuc -i apply edits to files instead of displaying a diff 169*0a6a1f1dSLionel Sambuc -p NUM strip the smallest prefix containing P slashes 170*0a6a1f1dSLionel Sambuc -regex PATTERN custom pattern selecting file paths to reformat 171f4a2713aSLionel Sambuc -style STYLE formatting style to apply (LLVM, Google, Chromium, Mozilla, 172f4a2713aSLionel Sambuc WebKit) 173f4a2713aSLionel Sambuc 174f4a2713aSLionel SambucSo to reformat all the lines in the latest :program:`git` commit, just do: 175f4a2713aSLionel Sambuc 176f4a2713aSLionel Sambuc.. code-block:: console 177f4a2713aSLionel Sambuc 178*0a6a1f1dSLionel Sambuc git diff -U0 HEAD^ | clang-format-diff.py -i -p1 179*0a6a1f1dSLionel Sambuc 180*0a6a1f1dSLionel SambucIn an SVN client, you can do: 181*0a6a1f1dSLionel Sambuc 182*0a6a1f1dSLionel Sambuc.. code-block:: console 183*0a6a1f1dSLionel Sambuc 184*0a6a1f1dSLionel Sambuc svn diff --diff-cmd=diff -x-U0 | clang-format-diff.py -i 185f4a2713aSLionel Sambuc 186f4a2713aSLionel SambucThe :option:`-U0` will create a diff without context lines (the script would format 187f4a2713aSLionel Sambucthose as well). 188