blob: 200518b674bfd6e22ce4ae25f4b82331f925ec1c [file] [log] [blame] [view]
andybons6eaa0c0d2015-08-26 20:12:521# Using clang-format on Chromium C++ Code
andybons3322f762015-08-24 21:37:092
Daniel Chengad61d882020-06-25 16:48:033[TOC]
4
Ian Barkley-Yeung5a37e2c2019-02-21 03:34:185*** note
6NOTE: This page does not apply to the Chromium OS project. See [Chromium Issue
7878506](https://bugs.chromium.org/p/chromium/issues/detail?id=878506#c10)
8for updates.
9***
10
andybons6eaa0c0d2015-08-26 20:12:5211## Easiest usage, from the command line
andybons3322f762015-08-24 21:37:0912
andybons6eaa0c0d2015-08-26 20:12:5213To automatically format a pending patch according to
James Leee6cf1a12024-03-18 09:08:1014[Chromium style](/styleguide/c++/c++.md), run: `git cl format` from the command
15line. This should work on all platforms without any extra set up: the tool is
16integrated with depot_tools and the Chromium checkout.
Daniel Chengad61d882020-06-25 16:48:0317
18Like other `git-cl` commands, this operates on a diff relative to the upstream
19branch. Only the lines that changed in a CL will be reformatted. To see what
20clang-format would choose, commit any local changes and then run `git cl
21format` followed by `git diff`. Alternatively, run `git cl format` and commit
22the now-formatted code.
andybons3322f762015-08-24 21:37:0923
andybons6eaa0c0d2015-08-26 20:12:5224## Editor integrations
andybons3322f762015-08-24 21:37:0925
andybons6eaa0c0d2015-08-26 20:12:5226Many developers find it useful to integrate the clang-format tool with their
27editor of choice. As a convenience, the scripts for this are also available in
28your checkout of Chrome under
Mark Rowe1f3f17b2023-08-31 01:35:1129[src/third_party/clang-format/script/](https://source.chromium.org/chromium/chromium/src/+/HEAD:third_party/clang-format/script/).
andybons3322f762015-08-24 21:37:0930
andybons6eaa0c0d2015-08-26 20:12:5231If you use an editor integration, you should try to make sure that you're using
32the version of clang-format that comes with your checkout. That way, you'll
33automatically get updates and be running a tool that formats consistently with
34other developers. The binary lives under `src/buildtools`, but it's also in your
35path indirectly via a `depot_tools` launcher script:
David Sandersc3299202022-05-26 15:47:3236[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`
andybons6eaa0c0d2015-08-26 20:12:5238and the editor command runs from a working directory inside the Chromium
39checkout, the editor scripts (which anticipate clang-format on the path) should
40work.
andybons3322f762015-08-24 21:37:0941
42For further guidance on editor integration, see these specific pages:
andybons3322f762015-08-24 21:37:0943
xiaoyin.l1003c0b2016-12-06 02:51:1744* [Sublime Text](https://www.chromium.org/developers/sublime-text#TOC-Format-selection-or-area-around-cursor-using-clang-format)
andybons6eaa0c0d2015-08-26 20:12:5245* [llvm's guidelines for vim, emacs, and bbedit](http://clang.llvm.org/docs/ClangFormat.html)
Ming-Ying Chung9158f0d2022-05-23 07:32:1646* [Visual Studio Code](https://chromium.googlesource.com/chromium/src/+/HEAD/docs/vscode.md#useful-extensions)
andybons6eaa0c0d2015-08-26 20:12:5247* 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.
andybons3322f762015-08-24 21:37:0949
andybons6eaa0c0d2015-08-26 20:12:5250## Reporting problems
51
52If clang-format is broken, or produces badly formatted code, please file a
Daniel Chengad61d882020-06-25 16:48:0353[bug]. Assign it to [email protected] or [email protected], who will route
54it upstream.
andybons6eaa0c0d2015-08-26 20:12:5255
56[bug]:
Nico Weber1ded89f2018-11-27 02:45:5457https://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 Chengad61d882020-06-25 16:48:0358
59## Are robots taking over my freedom to choose where newlines go?
60
61Mostly. At upload time, a presubmit check warns if a CL is not clang-formatted,
62but this is a non-blocking warning, and the CL may still be submitted. Even so,
63try 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
75There is one notable exception where clang-format is often disabled: large
76tables of data are often surrounded by `// clang-format off` and `//
77clang-format on`. Try to use this option sparingly, as widespread usage makes
78tool-assisted refactoring more difficult.
79
80Again, 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
82fixed.