blob: 1fa34d076fcbfa346479f6aada5206549069204b [file] [log] [blame] [view]
andybons6eaa0c0d2015-08-26 20:12:521# Using clang-format on Chromium C++ Code
andybons3322f762015-08-24 21:37:092
Ian Barkley-Yeung5a37e2c2019-02-21 03:34:183*** note
4NOTE: This page does not apply to the Chromium OS project. See [Chromium Issue
5878506](https://bugs.chromium.org/p/chromium/issues/detail?id=878506#c10)
6for updates.
7***
8
andybons6eaa0c0d2015-08-26 20:12:529## Easiest usage, from the command line
andybons3322f762015-08-24 21:37:0910
andybons6eaa0c0d2015-08-26 20:12:5211To automatically format a pending patch according to
xiaoyin.l1003c0b2016-12-06 02:51:1712[Chromium style](https://www.chromium.org/developers/coding-style), from
andybons6eaa0c0d2015-08-26 20:12:5213the command line, simply run: ``` git cl format ``` This should work on all
14platforms _(yes, even Windows)_ without any set up or configuration: the tool
15comes with your checkout. Like other `git-cl` commands, this operates on a diff
16relative to the upstream branch. Only the lines that you've already touched in
17your patch will be reformatted. You can commit your changes to your git branch
18and then run `git cl format`, after which `git diff` will show you what
19clang-format changed. Alternatively, you can run `git cl format` with your
20changes uncommitted, and then commit your now-formatted code.
andybons3322f762015-08-24 21:37:0921
andybons6eaa0c0d2015-08-26 20:12:5222## Editor integrations
andybons3322f762015-08-24 21:37:0923
andybons6eaa0c0d2015-08-26 20:12:5224Many developers find it useful to integrate the clang-format tool with their
25editor of choice. As a convenience, the scripts for this are also available in
26your checkout of Chrome under
27[src/buildtools/clang_format/script/](https://code.google.com/p/chromium/codesearch#chromium/src/buildtools/clang_format/script/).
andybons3322f762015-08-24 21:37:0928
andybons6eaa0c0d2015-08-26 20:12:5229If you use an editor integration, you should try to make sure that you're using
30the version of clang-format that comes with your checkout. That way, you'll
31automatically get updates and be running a tool that formats consistently with
32other developers. The binary lives under `src/buildtools`, but it's also in your
33path 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`
36and the editor command runs from a working directory inside the Chromium
37checkout, the editor scripts (which anticipate clang-format on the path) should
38work.
andybons3322f762015-08-24 21:37:0939
40For further guidance on editor integration, see these specific pages:
andybons3322f762015-08-24 21:37:0941
xiaoyin.l1003c0b2016-12-06 02:51:1742* [Sublime Text](https://www.chromium.org/developers/sublime-text#TOC-Format-selection-or-area-around-cursor-using-clang-format)
andybons6eaa0c0d2015-08-26 20:12:5243* [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.
andybons3322f762015-08-24 21:37:0946
andybons6eaa0c0d2015-08-26 20:12:5247## Are robots taking over my freedom to choose where newlines go?
andybons3322f762015-08-24 21:37:0948
andybons6eaa0c0d2015-08-26 20:12:5249No. For the project as a whole, using clang-format is just one optional way to
50format your code. While it will produce style-guide conformant code, other
51formattings would also satisfy the style guide, and all are okay.
andybons3322f762015-08-24 21:37:0952
andybons6eaa0c0d2015-08-26 20:12:5253Having said that, many clang-format converts have found that relying on a tool
54saves both them and their reviewers time. The saved time can then be used to
55discover functional defects in their patch, to address style/readability
56concerns whose resolution can't be automated, or to do something else that
57matters.
andybons3322f762015-08-24 21:37:0958
andybons6eaa0c0d2015-08-26 20:12:5259In directories where most contributors have already adopted clang-format, and
60code is already consistent with what clang-format would produce, some teams
61intend to experiment with standardizing on clang-format. When these local
62standards apply, it will be enforced by a PRESUBMIT.py check.
andybons3322f762015-08-24 21:37:0963
andybons6eaa0c0d2015-08-26 20:12:5264## Reporting problems
65
66If clang-format is broken, or produces badly formatted code, please file a
Nick Carter981988b02018-09-27 18:13:1267[bug]. Assign it to thakis@chromium.org who will route it upstream.
andybons6eaa0c0d2015-08-26 20:12:5268
69[bug]:
Nico Weber1ded89f2018-11-27 02:45:5470https://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