Skip to content

[WIP] tools: use clang-format to format C++ code

I think we have discussed briefly about this during the collaboration summit @BridgeAR @Fishrock123 This is just an attempt to see how everybody feels about using an automatic formatting tool to reduce style inconsistencies and nits in PRs. We use clang-format in llnode and v8 uses it as well so this sounds like a good candidate.

  • The first commit just initializes the.clang-format file based on Google styles (since previously we use cpplint.py that lints C++ code using the Google style guide). We have our own "extensions" to the style guide (e.g. the ones being added in https://github.com/nodejs/node/pull/16090) so we need to work out more rules in the .clang-format based on our needs.
    • If we want more granular control over the rules we can do a clang-format -style=google -dump-config > .clang-format and start from that as well.
    • This PR is first about seeing if there are any concerns on the idea of using a auto-formatting tool at all. And the rules will kinda depend on what https://github.com/nodejs/node/pull/16090 comes down to.
  • The second commit formats every .cc under src with the initalized .clang-format (clang-format -style=file -i src/**/*.cc)
  • I am using the clang-format npm module with pre-built binaries maintained by the Angular team, though clang-format can be obtained by compiling from the clang source code as well. Also you can get a python wrapper if you happen to have depot_tools and a V8/Chromium checkout on your machine. I am not sure whether we should bundle it in the codebase like what we do with eslint, but definitely doesn't hurt to add some commands to the Makefile (maybe along with eslint --fix).
Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • commit message follows commit guidelines
Affected core subsystem(s)

src, tools

Merge request reports

Loading