Add .clang-format to help to enforce code format#12588
Add .clang-format to help to enforce code format#12588xoox wants to merge 2 commits intoopencv:masterfrom
Conversation
This is a prototype clang-format configuration file on which further discussions are needed. The main reason is that there is no consistent coding style in the current source code of OpenCV.
This file was originally generated with `clang-format -dump-config -style=WebKit`. Trailing white spaces was included unfortunately.
|
I'd like to mention clang-format-diff, which can be used to format only code touched by a patch to introduce formatting changes gradually. It proved itself useful for me on several occasions. On the current style: I think people mainly hate The second most inconsistency is IMHO in brace wrapping e.g. vs some files use the first style some use the second style. |
That's a useful utility. In addition, clang-format has an option
I also vote for
The abve one is more compact and more beautiful in my personal view. Comments on my view of Here is update to my proposal. The final decision should be made by # clang-format 3.9 later
---
Language: Cpp
BasedOnStyle: WebKit
AlignAfterOpenBracket: Align
AllowAllParametersOfDeclarationOnNextLine: false
AlwaysBreakTemplateDeclarations: true
BreakBeforeBraces: Mozilla
ColumnLimit: 78
KeepEmptyLinesAtTheStartOfBlocks: false
NamespaceIndentation: None
... |
|
Current coding style guide snippet uses mandatory braces. However, again this is largely ignored in the codebase (even in new code like dnn). From my experience, it is only feasible to enforce mandatory braces with the first brace wrapping style. With the second brace wrapping style it seems people don't want to add those two extra lines to one-liners. It is no surprise that you will find many one-liners without braces in files that use the second brace wrapping style, even when those files adhere to the rest of the code style guide. |
if ()
expr1;
expr2;clang-format will format the above code into if ()
expr1;
expr2;So this kind of human errors will be more visually detectable after |
|
BTW, recent compilers will produce a warning (GCC7): |
|
Since OpenCV 4.0 is switching to C++11/C++14, update # clang-format 3.9 later
---
Language: Cpp
BasedOnStyle: WebKit
AlignAfterOpenBracket: Align
AllowAllParametersOfDeclarationOnNextLine: false
AlwaysBreakTemplateDeclarations: true
BreakBeforeBraces: Mozilla
ColumnLimit: 78
KeepEmptyLinesAtTheStartOfBlocks: false
NamespaceIndentation: None
Standard: Cpp11
... |
|
We can compare proposed styles: For example, the config in the PR gives us: And the config from the last comment: For core, imgproc only: |
|
A commit with huge diffs will be introduced if all source code are Config in my latest comment is more aggressive to enforce a fresher There're also some interesting utilities to generate a .clang-format Ref. [1] https://github.com/mikr/whatstyle |
|
a huge commit that reformats all the OpenCV source is definitely not an option. If we could smoothly integrate clang-format-diff into our CI, probably, it will be the way to go. Possibly, we could even add a heuristic - when there are many changes in a single source file, the whole file may be reformatted. Of course, we should try it first somehow. Another question - once some parts of OpenCV are brought to the new consistent style, how do we enforce that the patches do not break it? On the braces. Maybe there are some places where we use and it's an exception. Let's use whatever the style is called. Let's call it OpenCV style, I have no problems with it. Also, I do not see anywhere in the style description that spaces should be used instead of TAB and the indentation is 4 spaces. Probably, it should be added as well. Column limit 100 may be a bit too limiting in some cases. Shall we put any restriction? Or maybe we should raise it to 120 or 128 or something like that? |
|
This ensure consecutive namespace declarations be on the same line. But Here is an example showing clang-format-3.9 format code different to if (board.type == BoardInfo::CHESSBOARD && found) {
// improve the found corners' coordinate accuracy for chessboard
cv::cornerSubPix(cvI, pointBuf, cv::Size(winSize, winSize),
- cv::Size(-1, -1),
- cv::TermCriteria(
- cv::TermCriteria::EPS + cv::TermCriteria::COUNT, 300,
- 0.0001));
+ cv::Size(-1, -1), cv::TermCriteria(cv::TermCriteria::EPS
+ + cv::TermCriteria::COUNT,
+ 300, 0.0001));
}My suggested
|
|
ok, can the file be moved to some subdirectory instead of the root directory? |
|
Probably not, example: https://github.com/google/googletest/ |
|
When using
|
|
ok, I'm fine with it. @alalek, I think, it can be put in. |
|
This PR should be further refined by core team before being merged, and |
|
@vpisarev , @alalek , please take a look at some examples of code after applying style: master...mshabunin:style-core I used the following format (differs from this PR) and clang-format 8.0.0: |
|
@mshabunin, thank you very much for doing this experiments. I looked at the produced diff files. In my opinion, ~90% or even ~95% of the changes the script made are unnecessary. I suggest to make the formatting script much, much more liberal to the current formatting. Most of the time it should just keep the existing formatting. |
|
@mshabunin Please update file in this patch with proposed changes. And I believe we can merge it (but without any forcing to existed code). |
clang (llvm) 8.0 is not officially released yet. It's recommended to use @mshabunin Could you reduce the format configuration by adding an item The leading lines are suggested as following. The first line is comment # clang-format 3.9 later
---
Language: Cpp
BasedOnStyle: WebKit
# all other options listed alphabetically bellow
... |
|
Till the formatting is checked/enforced by CI the patch is useless. @alalek @vpisarev @vladimir-dudnik @asmorkalov discussed the solution offline and decided not to merge the solution. See @vpisarev comments above. |
This is a prototype clang-format configuration file on which further
discussions are needed. The main reason is that there is no consistent
coding style in the current source code of OpenCV.
This configuration has been tested with clang-format-3.9, and may be
slightly different to other versions.
There is already a coding style guide.
But code style in the repository is not as consistent as expected.
Some useful utilities exist to help to enhance coding formats, such as
clang-tidy and clang-format.
Wish a
.clang-formatfile will be helpful to all developers.To explore better code style options, I'll explain several options for
why they are chosen. Available style options are described in
Clang-Format Style Options.
Language: CppThis style file should be used for C, C++.
BasedOnStyle: WebKitAmong predefined styles,
WebKitis the closest one to OpenCV's codingstyle. See WebKit’s style guide for comparison.
This option will set most of needed options. So only several additional
options are needed to custom style based on
WebKitstyle.AlignAfterOpenBracket: AlignAlignmight make the code looks better. But for long function namesand long parameter names, this might make the code worse.
# AlwaysBreakTemplateDeclarations: MultiLineMultiLinevalue is only supported by higherclang-formatversions.So it's commented out. For clang-format-3.9 possible values are:
falseor
true.BraceWrappingandBreakBeforeBraces: CustomBraceWrappingis only used whenBreakBeforeBracesis set toCustom. Instead of usingCustom, one of predefined style is stronglyrecommended. (
Attach,Linux,Mozilla,Stroustrup,Allman,GNU,WebKit)ColumnLimit: 100100is set according to the coding style guide of OpenCV. In mypersonal view, a better value of
ColumnLimit: 78is preferred. It'snot recommended to use a large column limit. For
git diffintraditional 80 column terminals, 78 is a reasonable maximum choice (80
minus -/+ sign and a space). Besides, as printing on an A4 paper size
media (PDF or hard-copy), column less than 80 will make sure nicer
format.
You can always use a column of 100 in your editors with a modern HD wide
screen, just clang-format before commit. There's no hurt to anybody's
personal developing experience.
NamespaceIndentation: NoneDon’t indent in namespaces.
SpaceBeforeParens: NeverNever put a space before opening parentheses. This is set based on the
example in OpenCV's coding style guide. But in the source code there are
many cases using spaces after control statement keywords. So this option
would be better if changed to
SpaceBeforeParens: ControlStatements.SpacesInParenthesesIf
true, spaces will be inserted after(and before). OpenCV'ssource code use both cases with and without these spaces. So I choose
SpacesInParentheses: falsewhich set already byBasedOnStyle: WebKit.In case of consecutive namespace declarations, I have no idea on styling
this:
Only the following style is configured.
This pull request changes
Nothing changes if no code is formatted with
clang-format. New addedsource files should be formatted using
clang-formatbefore commit.