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 | |
Daniel Cheng | ad61d88 | 2020-06-25 16:48:03 | [diff] [blame] | 3 | [TOC] |
| 4 | |
Ian Barkley-Yeung | 5a37e2c | 2019-02-21 03:34:18 | [diff] [blame] | 5 | *** note |
| 6 | NOTE: This page does not apply to the Chromium OS project. See [Chromium Issue |
| 7 | 878506](https://bugs.chromium.org/p/chromium/issues/detail?id=878506#c10) |
| 8 | for updates. |
| 9 | *** |
| 10 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 11 | ## Easiest usage, from the command line |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 12 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 13 | To automatically format a pending patch according to |
James Lee | e6cf1a1 | 2024-03-18 09:08:10 | [diff] [blame] | 14 | [Chromium style](/styleguide/c++/c++.md), run: `git cl format` from the command |
| 15 | line. This should work on all platforms without any extra set up: the tool is |
| 16 | integrated with depot_tools and the Chromium checkout. |
Daniel Cheng | ad61d88 | 2020-06-25 16:48:03 | [diff] [blame] | 17 | |
| 18 | Like other `git-cl` commands, this operates on a diff relative to the upstream |
| 19 | branch. Only the lines that changed in a CL will be reformatted. To see what |
| 20 | clang-format would choose, commit any local changes and then run `git cl |
| 21 | format` followed by `git diff`. Alternatively, run `git cl format` and commit |
| 22 | the now-formatted code. |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 23 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 24 | ## Editor integrations |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 25 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 26 | Many developers find it useful to integrate the clang-format tool with their |
| 27 | editor of choice. As a convenience, the scripts for this are also available in |
| 28 | your checkout of Chrome under |
Mark Rowe | 1f3f17b | 2023-08-31 01:35:11 | [diff] [blame] | 29 | [src/third_party/clang-format/script/](https://source.chromium.org/chromium/chromium/src/+/HEAD:third_party/clang-format/script/). |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 30 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 31 | If you use an editor integration, you should try to make sure that you're using |
| 32 | the version of clang-format that comes with your checkout. That way, you'll |
| 33 | automatically get updates and be running a tool that formats consistently with |
| 34 | other developers. The binary lives under `src/buildtools`, but it's also in your |
| 35 | path indirectly via a `depot_tools` launcher script: |
David Sanders | c329920 | 2022-05-26 15:47:32 | [diff] [blame] | 36 | [clang-format](https://source.chromium.org/chromium/chromium/tools/depot_tools/+/HEAD:clang-format) |
| 37 | ([clang-format.bat](https://source.chromium.org/chromium/chromium/tools/depot_tools/+/HEAD:clang-format.bat) on Windows). Assuming that `depot_tools` is in your editor's `PATH` |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 38 | and the editor command runs from a working directory inside the Chromium |
| 39 | checkout, the editor scripts (which anticipate clang-format on the path) should |
| 40 | work. |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 41 | |
| 42 | For further guidance on editor integration, see these specific pages: |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 43 | |
xiaoyin.l | 1003c0b | 2016-12-06 02:51:17 | [diff] [blame] | 44 | * [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] | 45 | * [llvm's guidelines for vim, emacs, and bbedit](http://clang.llvm.org/docs/ClangFormat.html) |
Ming-Ying Chung | 9158f0d | 2022-05-23 07:32:16 | [diff] [blame] | 46 | * [Visual Studio Code](https://chromium.googlesource.com/chromium/src/+/HEAD/docs/vscode.md#useful-extensions) |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 47 | * For vim, `:so tools/vim/clang-format.vim` and then hit cmd-shift-i (mac) |
| 48 | ctrl-shift-i (elsewhere) to indent the current line or current selection. |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 49 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 50 | ## Reporting problems |
| 51 | |
| 52 | If clang-format is broken, or produces badly formatted code, please file a |
Daniel Cheng | ad61d88 | 2020-06-25 16:48:03 | [diff] [blame] | 53 | [bug]. Assign it to [email protected] or [email protected], who will route |
| 54 | it upstream. |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 55 | |
| 56 | [bug]: |
Nico Weber | 1ded89f | 2018-11-27 02:45:54 | [diff] [blame] | 57 | 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 |
Daniel Cheng | ad61d88 | 2020-06-25 16:48:03 | [diff] [blame] | 58 | |
| 59 | ## Are robots taking over my freedom to choose where newlines go? |
| 60 | |
| 61 | Mostly. At upload time, a presubmit check warns if a CL is not clang-formatted, |
| 62 | but this is a non-blocking warning, and the CL may still be submitted. Even so, |
| 63 | try to prefer clang-format's output when possible: |
| 64 | |
| 65 | - While clang-format does not necessarily format code the exact same way a human |
| 66 | might choose, it produces style-conformat code by design. This can allow |
| 67 | development and review time to be focused on discovering functional defects, |
| 68 | addressing readability/understandability concerns that can't be automatically |
| 69 | fixed by tooling, et cetera. |
| 70 | - Continually fighting the tooling is a losing battle. Most Chromium developers |
| 71 | use clang-format. Large-scale changes will simply run `git cl format` once to |
| 72 | avoid having to deal with the particulars of formatting. Over time, this will |
| 73 | likely undo any carefully-curated manual formatting of the affected lines. |
| 74 | |
| 75 | There is one notable exception where clang-format is often disabled: large |
| 76 | tables of data are often surrounded by `// clang-format off` and `// |
| 77 | clang-format on`. Try to use this option sparingly, as widespread usage makes |
| 78 | tool-assisted refactoring more difficult. |
| 79 | |
| 80 | Again, if clang-format produces something odd, please err on the side of |
| 81 | [reporting an issue](#Reporting-problems): bugs that aren't reported can't be |
| 82 | fixed. |