[RFC] Formatting our .c and .h files with clang-format

Matt Mackall mpm at selenic.com
Fri Sep 25 19:29:27 CDT 2015


On Mon, 2015-09-14 at 15:03 -0400, Augie Fackler wrote:
> http://clang.llvm.org/docs/ClangFormat.html is a handy tool for having
> a machine format your (C/C++/etc) source code rather than having
> error-prone humans perform the same tasks. Right now we've got a
> handful of rules in check-code for this, but they're not actually
> sufficient for all our code to be substantially similar style. I've
> created a clang-format file that is *close* to what we already do
> (http://hg.durin42.com/hg-wip/rev/aaeda7971cdb) and have wired up
> tests etc that would let us keep files formatted. As a sample of the
> result, I've reformatted:
> 
> * manifest.c: http://hg.durin42.com/hg-wip/rev/ca90a7cff2e1
>     This is a nice comprehensive example, and includes working around the
>      formatter in a couple spots where Python's macros fool the formatter.
> 
> base85.c: http://hg.durin42.com/hg-wip/rev/177e9d2e3cc5
>     This is a nice example of a file that was in its own style that is trivially
>     brought in line with the rest of the codebase.
> 
> Note that the resulting code tweaks check-code's "don't use spaces to
> indent" check. My opinion there is that if we start using clang-format
> we should just drop anything in check-code that conflicts with
> clang-format, since the latter is trivial to apply.
> 
> Thoughts?

In general, I'm fine with this idea, but the devil is in the details.
The resulting changes look pretty good but are too big to evaluate all
at once though. It's certainly finding some things that are definitely
wrong though.

The way forward is probably to nibble away at it by:

- running the tool against the code
- generating a large patch
- identifying a common "class" of unoffensive change
- send that as a patch
- repeat until the overall diff is smallish

-- 
Mathematics is the supreme nostalgia of our time.



More information about the Mercurial-devel mailing list