Details
Diff Detail
- Repository
- rG LLVM Github Monorepo
Event Timeline
Test code:
// test.cpp #include <algorithm> #include <cstdio> #include <cstring> struct Foo { int a,b,c; }; namespace Ns { class Bar { public: struct Foobar { int a; int b; }; private: int t; int method1() { // Intentional test for different // line breaking mode of brackets } template<typename T> int method2(T x) { // ... } int method3(int par) {} }; class C { }; } namespace Ns2 { }
Tests ran:
# pwd = build directory bin/clang-format test.cpp # LLVM Style bin/clang-format -style='{SeparateDefinitionBlocks: false}' test.cpp # Disable functionality bin/clang-format -style='{BasedOnStyle: Google}' test.cpp # Disable by default bin/clang-format -style='{BasedOnStyle: Google, SeparateDefinitionBlocks: true}' test.cpp # Override default bin/clang-format -style='{BasedOnStyle: WebKit}' test.cpp # Opening bracket in new line (WebKit style has this functionality enabled)
clang/lib/Format/Format.cpp | ||
---|---|---|
1209 | Not so sure about this as it would break some tests. |
clang/lib/Format/Format.cpp | ||
---|---|---|
1209 | As CI shows some original tests does not expect this empty line, but it looks like LLVM is generally adding empty lines between blocks. cpp template <typename T> class B; + class A { public: int f(); -}; +};\n |
clang/lib/Format/Format.cpp | ||
---|---|---|
1209 | This needs to be an enumeration where leave is the default |
- Change bool type of the option to enum type
- Generate empty line for enum block
Testfile:
// test2.cpp #include <algorithm> #include <cstdio> #include <cstring> struct Foo { int a,b,c; }; namespace Ns { class Bar { public: struct Foobar { int a; int b; }; private: int t; int method1() { // Intentional test for different // line breaking mode of brackets } template<typename T> int method2(T x) { // ... } enum Foo { FOO, BAR }; int method3(int par) {} }; class C { }; } namespace Ns2 { }
Tests:
bin/clang-format test2.cpp # Test default (leave as it is) bin/clang-format -style='{SeparateDefinitionBlocks: Between}' test2.cpp # Test normal functionality bin/clang-format -style='{SeparateDefinitionBlocks: Leave}' test2.cpp # Test turning off bin/clang-format -style='{SeparateDefinitionBlocks: Between, BasedOnStyle: WebKit}' test2.cpp # Case: opening bracket { has its own line
Thanks for working on this.
Please add unit tests that verify the expected behaviour. Keep them simple. You can inspire yourself from existing tests.
clang/docs/ClangFormatStyleOptions.rst | ||
---|---|---|
3405 | Unless it takes a lot of time to review this patch 🙂 | |
clang/lib/Format/Format.cpp | ||
2026 | Please consider moving it to a different file. |
clang/docs/ClangFormatStyleOptions.rst | ||
---|---|---|
3411 | This enumeration surely needs 3 settings Leave, // don't touch the code, if I have a line leave it alone if not don't add one Always, // always add a line Never, // never add a line and remove the lint if one exists |
- Change version from 15 to 14
- Add option SDS_Never, rename SDS_Between => SDS_Always
- Add unit tests
- Separate class into new file
clang/lib/Format/Format.cpp | ||
---|---|---|
2113 | This worked well as the Error class add method returned has overloaded boolean operator, returning true (nonzero) when failing, which simulates program exit code scheme? (ref: Doxygen/Error/operator bool) |
clang/include/clang/Format/Format.h | ||
---|---|---|
3058 | Add full stop at the end of sentences. | |
clang/lib/Format/DefinitionBlockSeparator.cpp | ||
56 | Here also full stop, please. And following comments. | |
clang/lib/Format/DefinitionBlockSeparator.h | ||
39–40 | I know you copied it. It is wrong where you copied it from. :) | |
clang/lib/Format/Format.cpp | ||
2113 | Okay, but maybe safe the result in Error to make it clearer. |
- Apply suggestions from clangfmt
- Add missing period of comments
- Fix namespace ending comment
- Add comment for notes on return value of WhitespaceManager's method
Nice job. Some minor comments.
Please don't forget to reformat too.
clang/docs/ClangFormatStyleOptions.rst | ||
---|---|---|
3422 | Keep it consistent please. | |
3425 | Please reformat the code, e.g. on the left misses spaces (near braces and commas for instance). The difference between left and right should only be blank lines between blocks. | |
clang/include/clang/Format/Format.h | ||
3058 | ||
3067 | Why not C and other similar languages? | |
3069 | Actuay, do you need this line? | |
4094 | ||
4096 | ? | |
clang/unittests/Format/DefinitionBlockSeparatorTest.cpp | ||
49 | Maybe you can add verifyFormat helper as in other files? So that you don't need to repeat the code in some cases and we test the code formatting stability as well. |
clang/lib/Format/DefinitionBlockSeparator.h | ||
---|---|---|
39–40 | Not done. |
- Improve compatibility to other languages
- Improve unit test by test format stability, inverse result, and generate some input from expected output
- Reformat sample code
- Use more concise word for SDS_Leave
Only some small changes, then it looks good to me. And I will definitely use it, once it is landed.
clang/include/clang/Format/Format.h | ||
---|---|---|
3060 | Right? | |
4102 | The only use of this function I found is in the tests and it sets the argument, or am I mistaken? | |
clang/lib/Format/DefinitionBlockSeparator.cpp | ||
34–35 | It should never be instantiated with that. | |
36 | ||
60 | ||
64 | ||
77 | ||
81 | ||
94 | ||
129 | ||
clang/lib/Format/Format.cpp | ||
464 | Please try to find a better place, regarding sorting (yeah the are some mismatches). | |
786 | Please sort before ShortNamespaceLines | |
3062 | ||
clang/lib/Format/WhitespaceManager.h | ||
48 |
- Apply review suggestions.
- Assert the style is not SDS_Leave in private method, while return no change in public method if so.
- Rename loop variable Line to CurrentLine
clang/include/clang/Format/Format.h | ||
---|---|---|
4102 | I suppose this function might also be called from some users of the library other than clangfmt itself? Besides, the declarations nearby are setting this default value. | |
clang/lib/Format/DefinitionBlockSeparator.cpp | ||
36 | Some other lambdas I found use lowercased leading letter, as it looks like a function (call). |
clang/lib/Format/DefinitionBlockSeparator.cpp | ||
---|---|---|
26 | Better, but I still think we should have the assert here. The class should not be instantiated at all, if you don't want to use it. | |
36 | Okay, then I don't know what the LLVM Style is, probably there is none in this regard. I think the lambdas I saw in clang-format are with a capital letter. |
clang/lib/Format/DefinitionBlockSeparator.cpp | ||
---|---|---|
26 | Who are the users? clang-format and maybe its tests. And the only one that really matters is clang-format, which does the check. | |
clang/unittests/Format/DefinitionBlockSeparatorTest.cpp | ||
46 | Maybe one should either use the normal reformat function, or additionally. Because right now we do not test any interferences of these two functions. |
clang/lib/Format/DefinitionBlockSeparator.cpp | ||
---|---|---|
26 | Does there exist the possibility that some developer just include the header and link to library to obtain replacement analysis result for their other parts of program to use, instead of only clangfmt itself is using this class? |
clang/lib/Format/DefinitionBlockSeparator.cpp | ||
---|---|---|
26 | Of course the possibility exists, but I don't think there is anyone. And at least for me those aren't the audience. And even then, they can make sure that it's not SDS_Leave. |
LGTM thank you for this patch, I'll also definately use this myself.
clang/docs/ClangFormatStyleOptions.rst | ||
---|---|---|
3405 | EmptyLinesBetweenBlocks? |
clang/docs/ClangFormatStyleOptions.rst | ||
---|---|---|
3405 | It feel like for me that it is implying that there will also be empty lines surrounding blocks of, for example, for and if? |
I'm seeing some odd behaviour here..
void foo() {} /* ABC */ void bar() {}
becomes
void foo() {} /* ABC */ void bar() {}
If the ABC comment is a doxygen style comment it doesn't make sense to separate it from the function below
Did you see this issue https://github.com/llvm/llvm-project/issues/52976
In its current form, this patch causes huge amounts of flux in projects
that document code like (anyone using doxygen likely, + most normal people
;-) )
...
}
// My function
<<<<-------- extra newline added
void
foo()
{
}
as it will place and undesirable additional newline between the function
and the return type.
We really need this to be resolved before we branch out v14 (which may be
imminent)
MyDeveloperDay
Thanks for the feedback! I have proposed some fix in D116663, and please see if this works.
Unless it takes a lot of time to review this patch 🙂