-
Notifications
You must be signed in to change notification settings - Fork 7.4k
Consider integrating a style-verification tool into CI #21392
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
Comments
+1 for this idea, I've actually always thought about this. Should we then remove |
Checkpatch verifies things outside coding style (like commit message content) so it would still be necessary. |
Thought about this multiple times in the past, it is not straightforward but definitely feasible with some effort needed:
|
Just a comment, because i used uncrustify in a pre-commit-hook and on a ci in the past. The pre-commit-hook is something similar to https://github.com/ddddavidmartin/Pre-commit-hooks/blob/master/pre-commit-uncrustify
|
dev-review (Mar 26): @nashif we need a tool that is able to deal with a diff, since we shouldn't warn about changes not introduced by the actual PR. uncrustify config in zephyr is inconsistent with checkpatch, so we need to update the config to be consistent. We have 3 means today between clang-format, uncrustify, and checkpatch. Need to see if we can get to one tool. @pabigot will look into clang-format. |
We might be able to utilize https://github.com/DoozyX/clang-format-lint-action, I haven't looked at it in great detail. |
We do need something that can run this: https://github.com/llvm-mirror/clang/blob/master/tools/clang-format/clang-format-diff.py |
As a follow-up: I've confirmed to my satisfaction that clang-format will always insert a space between a ForEach macro used as a loop construct and the opening parenthesis of its argument:
This will generate a diagnostic from checkpatch:
Options:
|
Another good option is There is also already very good git integration with git-clang-format. So setting up pre-commit hooks (automatically formatting code before a commit) and server side hooks (rejecting code that is not correctly formatted) is fairly straight forward. Also, with the format on / off tags, it's easy to perform workarounds /* clang-format off */
/*
* something free-form
*/
/* clang-format on */ But really, in the end, whitespace shouldn't even be a consideration for developers / reviewers. To quote myself, "we don't make money selling whitespace." Also, whitespace can be a fairly personal thing. There really is no "correct" way to format it. Some devs with poor vision might feel more comfortable with more whitespace - e.g. those with astigmatism. It might make other devs' skin crawl. If it can be "corrected" with a git hook or simple command, essentially for free, and every dev is then able to format the code whatever way they like prior to commit, then the project should really adopt a convention that works everywhere. |
I've updated the issue description to avoid referencing The original driver for my opening this was to have an objective rule for whether a style change was necessary: if the as-submitted code gets throught the style checker without any changes, then variations are personal taste. |
This seems to be done now (for foreach macros), see https://reviews.llvm.org/D78869 and in https://clang.llvm.org/docs/ClangFormatStyleOptions.html search for "ControlStatementsExceptForEachMacros" This seems to be included starting with clang-format-11. |
Great, thanks for the pointer. The feature was merged 2020-04-26 to the clang-11 development branch. Based on previous LLVM releases clang-12 should be available in the second half of 2020-09. Once it's out we can make further progress. |
Please see #27058 and give it a try if you have (or can install) a clang-12 pre-release. |
I like the idea, but I think that this will inevitably result is some purely cosmetic commits (a good thing IMHO). Therefore, I'd like to see this project to start maintaining a .git-blame-ignore-revs file so that the git blame option --ignore-revs-file can be used in the future. |
I infer that the rationale for |
I have had a look into this and have made the following observations: A larger issue I have identified is how clang-format treats (or mangles) lists of nested values. For example:
This only gets worse with more complicated structures. The Bluetooth subsystem at a minimum uses this pattern to declare things, and none of the default styles (llvm, google, webkit) formatted this example in a sane way. The formatter also likes moving fields in struct instantiations on to the same line, which I have not identified a way to stop:
|
There was another issue related with clang-format and checkpach that was solved: bit fields formatting, In past clang-format had inserted spaces before and after a colon.
I'm interested in update the .clang-format config file to more recent version of the tool. Besides CI it is also useful for contributors. Probably most of IDEs may be configured to format whole files or modification on save. That would increase productivity. What do you think about enabling all features that are aligned with Coding Style and do not make commits to fail on checkpatch ? |
We need at least 1 script doing some amount of checking against the coding style, currently this is If we can resolve that, and to a lesser extent the struct instantiation issue, I think it would be easy to sell the benefits to everyone. |
@JordanYates Is there a list of incompatibility items of clang-format and Coding Style rules? Have you got knowledge about such list? |
@pabigot The issue with foreach macros is solved with: SpaceBeforeParens : 'SBPO_ControlStatementsExceptForEachMacros'. Sorry... I didn't spot your draft PR where it is already in use. |
I've ctreated and issue for:
in clang/llvm bug tracked: https://bugs.llvm.org/show_bug.cgi?id=50679 |
Could clang-tidy be used with clang-format to get full coverage? This is stopping me dead in my tracks. |
Do you mean, use of clang-tidy to solve issues in coding style that are not supported by clang-format itself? |
I've also recently run Intel's open source ControlFlag tool (https://github.com/IntelLabs/control-flag) on the Zephyr codebase, to see if it is worth looking into. It did not find many things (and most of those were false positives it looks like), and unfortunately it takes quite a while to run (about 13h on 8/16 cores for the Logs from the run: Based on my experience, the tool did not catch enough nor runs fast enough for consideration. |
outdated, closing. Please open new issue with updated information if this is still needed. |
Zephyr has had for a very long time an uncrustify configuration that is recommended to ensure code conforms to the Coding Style. However there is no QA infrastructure to detect violations of this style.
Part of the plan for coding guidelines includes an enforcement stage. Tools like Coccinelle may be very helpful in ensuring conformance. However, Coccinelle is unaware of coding style, and left to itself will produce changes that are not compliant. See, for example, #21391 where Coccinelle output must be post-processed to eliminate a space from cast expression.
It would be significantly easier to use tools to ensure code guideline conformance if their output could be automatically transformed to restore style conformance. For #21391 uncrustify might have been able to fix the resulting long lines.
Painful though it would be, I propose that steps be taken to run all Zephyr implementation and header files through uncrustify, and that the CI infrastructure be updated to emit warnings when newly committed code is not style-compliant.
There would be several benefits:
The text was updated successfully, but these errors were encountered: