-
-
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 #4770
ClangFormat the C code #4770
Conversation
I really dislike this form of function definition, especially since almost all other forms of braces are inline with the end of the starting line: static PyObject*
_putdata(ImagingObject* self, PyObject* args)
{ The rest looks pretty reasonable. I think this is forcing braces around single line blocks (if/while), which is good. If it's not, then I'd say that it should. |
I think this ignores the following part of PEP 7:
I think this is also a requirement in Python code in Black. |
This config sets The options (docs with examples):
Attach is the only one not to break before that brace: static PyObject *
_putdata(ImagingObject *self, PyObject *args) { I've changed config to |
I was hoping it would, to prevent bugs like #4492, but alas it's out of scope for clang-format (https://stackoverflow.com/a/36871872/724176). @radarhere fixed up the existing code in #4618. |
d7b1699
to
a48cc59
Compare
Looks like this can be achieved with (https://stackoverflow.com/a/47097008/724176):
Docs: https://clang.llvm.org/docs/ClangFormatStyleOptions.html |
a48cc59
to
a2d204a
Compare
a2d204a
to
2278f1a
Compare
I've rebased, squashed the style modifications to a single commit, and reformatted in a single commit. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|
||
/* -------------------------------------------------------------------- */ | ||
/* OBJECT ADMINISTRATION */ | ||
/* -------------------------------------------------------------------- */ | ||
|
||
typedef struct { | ||
PyObject_HEAD | ||
Imaging image; | ||
PyObject_HEAD Imaging image; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't look right. Perhaps the following should get formatted properly?
PyObject_HEAD Imaging image; | |
PyObject_HEAD; | |
Imaging image; |
This change can be done in all files with a simple grep.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding in the semicolon causes syntax errors on Windows (AppVeyor and GHA).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah great, leave it to MSVC to apply arbitrary restrictions from a standard it doesn't even fully support yet.
After a bit of effort, I have found the following hack which works with both MSVC and GCC 4.1 and has no effect on structure size (see https://godbolt.org/z/76W7f4):
PyObject_HEAD Imaging image; | |
PyObject_HEAD int:0; | |
Imaging image; |
The extra code can be hidden in a define such as #define PILLOW_HEAD PyObject_HEAD int:0
and used similarly to my original comment. However, I'm not sure whether it is worth the extra effort.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should leave this if possible, PyObject_HEAD is the standard way to do it, and hiding it in a macro is additional complication that doesn’t make sense except for formatting.
2278f1a
to
cf2d597
Compare
I don't have strong feelings about this. |
Let’s merge after release. Fewer potential merge errors and rebase required then. |
cf2d597
to
46b7e86
Compare
Fixes #4493.
I found a "clang-format style that approximates Python's PEP 7":
(The zoneinfo repo was the reference implementation for
zoneinfo
in the stdlib (PEP 615), and is now a backport for Python 3.6+.)This style has also been used elsewhere, as is or as the basis:
Including CPython's new PEG generator (written by Guido and two others), which is the same as the zoneinfo one but with
ColumnLimit
increased from 79 to 95:Here's how it looks applied to Pillow:
clang-format -i `git ls-tree -r HEAD --name-only | grep "\.[ch]$"`
ColumnLimit
from 79 to 88, to match BlackI suggest we use this, and welcome feedback and any further tweaks.
Before merge, to make the history cleaner, I'll squash the config to a single commit, and the formatting to another single commit.
Also this will need adding to the CI.