andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 1 | # Using clang-format on Chromium C++ Code |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 2 | |
Ian Barkley-Yeung | 5a37e2c | 2019-02-21 03:34:18 | [diff] [blame] | 3 | *** note |
| 4 | NOTE: This page does not apply to the Chromium OS project. See [Chromium Issue |
| 5 | 878506](https://bugs.chromium.org/p/chromium/issues/detail?id=878506#c10) |
| 6 | for updates. |
| 7 | *** |
| 8 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 9 | ## Easiest usage, from the command line |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 10 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 11 | To automatically format a pending patch according to |
xiaoyin.l | 1003c0b | 2016-12-06 02:51:17 | [diff] [blame] | 12 | [Chromium style](https://www.chromium.org/developers/coding-style), from |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 13 | the command line, simply run: ``` git cl format ``` This should work on all |
| 14 | platforms _(yes, even Windows)_ without any set up or configuration: the tool |
| 15 | comes with your checkout. Like other `git-cl` commands, this operates on a diff |
| 16 | relative to the upstream branch. Only the lines that you've already touched in |
| 17 | your patch will be reformatted. You can commit your changes to your git branch |
| 18 | and then run `git cl format`, after which `git diff` will show you what |
| 19 | clang-format changed. Alternatively, you can run `git cl format` with your |
| 20 | changes uncommitted, and then commit your now-formatted code. |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 21 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 22 | ## Editor integrations |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 23 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 24 | Many developers find it useful to integrate the clang-format tool with their |
| 25 | editor of choice. As a convenience, the scripts for this are also available in |
| 26 | your checkout of Chrome under |
| 27 | [src/buildtools/clang_format/script/](https://code.google.com/p/chromium/codesearch#chromium/src/buildtools/clang_format/script/). |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 28 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 29 | If you use an editor integration, you should try to make sure that you're using |
| 30 | the version of clang-format that comes with your checkout. That way, you'll |
| 31 | automatically get updates and be running a tool that formats consistently with |
| 32 | other developers. The binary lives under `src/buildtools`, but it's also in your |
| 33 | path indirectly via a `depot_tools` launcher script: |
| 34 | [clang-format](https://code.google.com/p/chromium/codesearch#chromium/tools/depot_tools/clang-format) |
| 35 | ([clang-format.bat](https://code.google.com/p/chromium/codesearch#chromium/tools/depot_tools/clang-format.bat) on Windows). Assuming that `depot_tools` is in your editor's `PATH` |
| 36 | and the editor command runs from a working directory inside the Chromium |
| 37 | checkout, the editor scripts (which anticipate clang-format on the path) should |
| 38 | work. |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 39 | |
| 40 | For further guidance on editor integration, see these specific pages: |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 41 | |
xiaoyin.l | 1003c0b | 2016-12-06 02:51:17 | [diff] [blame] | 42 | * [Sublime Text](https://www.chromium.org/developers/sublime-text#TOC-Format-selection-or-area-around-cursor-using-clang-format) |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 43 | * [llvm's guidelines for vim, emacs, and bbedit](http://clang.llvm.org/docs/ClangFormat.html) |
| 44 | * For vim, `:so tools/vim/clang-format.vim` and then hit cmd-shift-i (mac) |
| 45 | ctrl-shift-i (elsewhere) to indent the current line or current selection. |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 46 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 47 | ## Are robots taking over my freedom to choose where newlines go? |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 48 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 49 | No. For the project as a whole, using clang-format is just one optional way to |
| 50 | format your code. While it will produce style-guide conformant code, other |
| 51 | formattings would also satisfy the style guide, and all are okay. |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 52 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 53 | Having said that, many clang-format converts have found that relying on a tool |
| 54 | saves both them and their reviewers time. The saved time can then be used to |
| 55 | discover functional defects in their patch, to address style/readability |
| 56 | concerns whose resolution can't be automated, or to do something else that |
| 57 | matters. |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 58 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 59 | In directories where most contributors have already adopted clang-format, and |
| 60 | code is already consistent with what clang-format would produce, some teams |
| 61 | intend to experiment with standardizing on clang-format. When these local |
| 62 | standards apply, it will be enforced by a PRESUBMIT.py check. |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 63 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 64 | ## Reporting problems |
| 65 | |
| 66 | If clang-format is broken, or produces badly formatted code, please file a |
Nick Carter | 981988b0 | 2018-09-27 18:13:12 | [diff] [blame] | 67 | [bug]. Assign it to thakis@chromium.org who will route it upstream. |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 68 | |
| 69 | [bug]: |
Nico Weber | 1ded89f | 2018-11-27 02:45:54 | [diff] [blame] | 70 | https://code.google.com/p/chromium/issues/entry?comment=clang-format%20produced%20code%20that%20(choose%20all%20that%20apply):%20%0A-%20Doesn%27t%20match%20Chromium%20style%0A-%20Doesn%27t%20match%20blink%20style%0A-%20Riles%20my%20finely%20honed%20stylistic%20dander%0A-%20No%20sane%20human%20would%20ever%20choose%0A%0AHere%27s%20the%20code%20before%20formatting:%0A%0A%0AHere%27s%20the%20code%20after%20formatting:%0A%0A%0AHere%27s%20how%20it%20ought%20to%20look:%0A%0A%0ACode%20review%20link%20for%20full%20files/context:&summary=clang-format%20quality%20problem&[email protected]&labels=Type-Bug,Build-Tools,OS-?,clang-format |