Easy(er) wine patch formatting with git clang-format.

Rémi Bernon rbernon at codeweavers.com
Mon Sep 21 17:02:59 CDT 2020


Hi!

I had enough of always having small white-space issues in my patches 
lately, so I've hacked clang-format a bit to make it better handle Wine 
specific style(s), and make it more readily usable for contributing to 
Wine, and I'm now sharing it as it could be useful to others.


I also discovered the git clang-format helper, which formats source but 
only keeps the lines that a patch or a commit previously changed, making 
it easier to get correctly formatted patches without messing with the 
source code around.

Now it's still not completely a perfect fit and there's a few cases 
where the tool doesn't give very good results, but I find that git 
clang-format is very well done for hand-picking good style fixes.

For instance, you can just add all your changes before committing, then 
call `git clang-format`, which formats your changes without touching the 
staged changes. And you can then add the style fixes one by one with 
`git add -p`.

Or, if you already have a commit that you want to check or fixup, run 
`git clang-format HEAD^` and `git add -p` then `git commit --am`.


The patches for clang-format are attached (on llvm source, currently on 
45344cf7ac5b848f77825ffa37b0cb3b69b9b07b HEAD), and a small explanation 
/ usage for each one:

* PATCH 1/4 adds a new SpacesInFunctionParentheses configuration option 
to support some Wine-specific style for function calls, where spaces are 
used but only within parentheses of a function call. There was already 
some support for spaces around paren, but not specifically for function 
calls.

* PATCH 2/4 introduces a new way to lookup for .clang-format config 
files. There's already one that looks such files up a source file path, 
recursively, but as we don't include such files in Wine source, it was 
hard to maintain locally --I'm a heavy git clean -fdx user.

This instead introduces a new -style-dir command-line argument, and a 
corresponding clangFormat.styleDir git-clang-format configuration 
parameter, that will replace the current working dir when looking for 
such files.

For instance, I use clangFormat.styleDir = ~/Code/.wine-clang-format.d, 
which currently contains:

/home/rbernon/Code/.wine-clang-format.d/
├── dlls
│   ├── gdi32
│   │   └── .clang-format
│   ├── user32
│   │   └── .clang-format
│   └── windowscodecs
│       └── .clang-format
├── server
│   └── .clang-format
└── tools
     └── widl
         └── .clang-format

And clang-format will use the .clang-format that best matches to the 
source file that is formatted in the wine source tree.

For reference and illustration, I have this user32 .clang-format, which 
works more or less correctly:

---
DisableFormat: false
SortIncludes: false
Language: Cpp
StatementMacros:
   - todo_wine
   - todo_wine_if
TypenameMacros:
   - MAKE_FUNCPTR
   - MAKELANGID
   - LOAD_FUNCPTR
   - ARRAY_SIZE
   - debugstr_a
   - debugstr_an
   - debugstr_w
   - debugstr_wn
   - debugstr_us
   - debugstr_faceid
   - debugstr_fontsignature
IndentWidth: 4
ColumnLimit: 100
BreakBeforeBraces: Allman
AlignAfterOpenBracket: Align
AllowAllArgumentsOnNextLine: true
AllowShortIfStatementsOnASingleLine: Always
AllowShortLoopsOnASingleLine: true
BinPackArguments: true
BinPackParameters: true
SpacesInFunctionParentheses: true
PenaltyExcessCharacter: 1
...

I currently abuse StatementMacros and TypenameMacros to tweak the 
resulting style, as it's still not perfect and the source is also not 
always consistent already.

Of course, this wouldn't be needed if these files could be added to Wine 
source tree.

* PATCH 3/4 and 4/4 are small style mix-ups fixes that occurred too may 
times already.
-- 
Rémi Bernon <rbernon at codeweavers.com>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-clang-format-Add-new-SpacesInFunctionParentheses-con.patch
Type: text/x-patch
Size: 5112 bytes
Desc: not available
URL: <http://www.winehq.org/pipermail/wine-devel/attachments/20200922/76d225fc/attachment-0004.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0002-clang-format-Add-style-dir-command-line-argument.patch
Type: text/x-patch
Size: 6007 bytes
Desc: not available
URL: <http://www.winehq.org/pipermail/wine-devel/attachments/20200922/76d225fc/attachment-0005.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0003-clang-format-Don-t-break-every-element-of-a-long-nes.patch
Type: text/x-patch
Size: 1930 bytes
Desc: not available
URL: <http://www.winehq.org/pipermail/wine-devel/attachments/20200922/76d225fc/attachment-0006.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0004-clang-format-Also-allow-one-line-else-if-AllowShortI.patch
Type: text/x-patch
Size: 1585 bytes
Desc: not available
URL: <http://www.winehq.org/pipermail/wine-devel/attachments/20200922/76d225fc/attachment-0007.bin>


More information about the wine-devel mailing list