-
Notifications
You must be signed in to change notification settings - Fork 56
Use clang-format to control coding style #46
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
Conversation
* Add makefile rule for running clang-format. Fail when the test does not pass. * Include an option to modify the files inplace.
Note that clang format is not very flexible with lambda, or I do not know how to properly set it up, so the pybind11 wrapping code style does not look perfectly.
| flake8: | ||
| make -C $(BUILD_PATH) VERBOSE=$(VERBOSE) flake8 | ||
|
|
||
| CFFILES = $(shell find src include -type f -name '*.[ch]pp' | sort) |
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.
Scan all C++ source files.
| cformat: $(CFFILES) | ||
| @for fn in $(CFFILES) ; \ | ||
| do \ | ||
| echo "$(CFCMD) $${fn}:"; \ |
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.
Show the command used.
| make -C $(BUILD_PATH) VERBOSE=$(VERBOSE) flake8 | ||
|
|
||
| CFFILES = $(shell find src include -type f -name '*.[ch]pp' | sort) | ||
| ifeq ($(CFCMD),) |
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.
CFCMD may be overridden from the command line.
| if (stride.size() != idx.size()) | ||
| { | ||
| std::ostringstream ms; | ||
| // clang-format off |
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.
There are some format that clang-format cannot handle well. Use // clang-format off/on section to fence them.
| { | ||
| array_reference this_array = f(self); | ||
| if (this_array.nbytes() != static_cast<size_t>(ndarr.nbytes())) | ||
| (*this) |
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.
clang-format is particularly limited with lambda, for which I manually change the style.
| } | ||
| makeSimpleArray(ndarr).swap(this_array); | ||
| }) | ||
| // |
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 is needed or the last semi-colon will be placed after the closing parenthsis. Copying, pasting, and swapping the wrapping code is easy when the ending semi-colon use a standalone line:
(*this)
// Swapping A and B is easy.
.def_property_readonly("A", &wrapped_type::A)
.def_property_readonly("B", &wrapped_type::B)
// The semi-colon is in a standalone line.
;When the semi-colon is at the end of each of the wrapping line, swapping is hard:
(*this)
.def_property_readonly("A", &wrapped_type::A)
.def_property_readonly("B", &wrapped_type::B); // The semi-colon makes it hard to swap with A.| ( | ||
| "me", [](py::object const &) -> wrapped_type & { return wrapped_type::me(); } | ||
| ) | ||
| .def_property_readonly_static( |
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.
We just need a lot of manual formatting for lambda for the pybind11 wrapper.
|
This is a decent weekend project and took me like a half day (time marked about 12pm today). |
In the makefile, add a target
cformatto check the coding style usingclang-format.Also add a variable
FORCE_CLANG_FORMATto control how clang-format is used:When it is not set, use
clang-format --dry-runon each file to check the format and print check results. For example:When setting
FORCE_CLANG_FORMATto anything other thaninplace, turn warning to error:When setting
FORCE_CLANG_FORMATtoinplace, format the files inplace:Note
In the pybind11 wrapping code, a lot of lambdas are used. There is a trick to keep the last semi-colon placed after the closing parenthsis. Copying, pasting, and swapping the wrapping code is easy when the ending semi-colon use a standalone line:
When the semi-colon is at the end of each of the wrapping line, swapping is hard: