-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ClangFormat the C code #4493
Comments
I made 5 branches for each predefined ClangFormat style:
For each, I first generated the config file: clang-format -style=chromium -dump-config > .clang-format Then flipped the Then changed any And then finally ran the formatter on the clang-format -i `git ls-tree -r HEAD --name-only | grep "\.[ch]$"` One thing to note, I wrote:
This isn't actually true, ClangFormat doesn't insert brackets. Any preferences? |
They are all much easier to read than the current (lack of) style! Specifically, I like how Chromium and Mozilla (unlike some others) break multiline parameters put a single parameter per line, similar to the Python style. One good place to compare this are lines 90 to 110 in There are a few places where the formatter makes the code worse no matter the style, hopefully these can be manually overriden per line. A few examples: Most styles also reference specific macros or file regexes to have special formatting, these might be a good place to customize. For example this section from llvm:
Compared with Google:
A nice option could be to, for example, put Python headers first, optional libraries second (grouped by library?), Pillow last. This could even fix the |
Not sure whether there is a predefined style for ClangFormat, but PEP 7 specifies what should be used for CPython, and NumPy has their own extension of it. It's probably not the prettiest, but while typing out what I don't like about it, I have gotten used to it already, and actually really like it now! I feel like these two points are the most important (i.e. most variable among different styles):
Without going into too much detail, I feel like the Mozilla style might be the closest. One difference I can see is that Mozilla declares pointers as |
Thanks for checking!
Agreed, it makes diffs easier to read too.
That can be flipped with: -PointerAlignment: Left
+PointerAlignment: Right (Tested: works.)
And: - BeforeElse: false
+ BeforeElse: true Tested: didn't work, must be some other config change needed too. But I'm also a bit less keen on it. |
Please see PR #4770 for a suggestion using a "clang-format style that approximates Python's PEP 7". |
Run ClangFormat on all of the C code to once and for all get rid of the mixed 8 tab/spaces -> all 4 spaces, get all the braces on a consistent style, enclose single lines in braces, and generally make it all consistent.
Re: #1770 (comment)
Will prevent bugs like #4492.
Let's do this after the 2020-04-01 release (#4354), when the PR list will be at a local minimum and to avoid merge conflicts.
There's four pre-commit hooks available, let's pick a good one to add to
.pre-commit-config.yaml
so the CI will prevent regressions.We'll need to pick a style or define our own. Is there a predefined one close to the current style, or that is commonly used for Python extensions? What do other Python projects use?
The text was updated successfully, but these errors were encountered: