diff options
author | Daniel Jasper <djasper@google.com> | 2012-12-18 21:05:13 +0000 |
---|---|---|
committer | Daniel Jasper <djasper@google.com> | 2012-12-18 21:05:13 +0000 |
commit | 1321eb593c8e13738dcaf72e409aa466c8cf6463 (patch) | |
tree | d97bab486bdaa486a647b53182ec6a0ba36ec198 /unittests/Format/FormatTest.cpp | |
parent | 08cc03fcd97c3886b004d861aadbc28de70092c5 (diff) |
Better support for constructor initializers.
We used to format initializers like this (with a sort of hacky implementation):
Constructor()
: Val1(A),
Val2(B) {
and now format like this (with a somewhat better solution):
Constructor()
: Val1(A), Val2(B) {
assuming this would not fit on a single line. Also added tests.
As a side effect we now first analyze whether an UnwrappedLine needs to be
split at all. If not, not splitting it is the best solution by definition. As
this should be a very common case in normal code, not exploring the entire
solution space can provide significant speedup.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@170457 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'unittests/Format/FormatTest.cpp')
-rw-r--r-- | unittests/Format/FormatTest.cpp | 52 |
1 files changed, 42 insertions, 10 deletions
diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index fef56caee7..c9cd825277 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -374,6 +374,41 @@ TEST_F(FormatTest, FormatsAwesomeMethodCall) { " parameter, parameter, parameter)), SecondLongCall(parameter));"); } +TEST_F(FormatTest, ConstructorInitializers) { + verifyFormat("Constructor() : Initializer(FitsOnTheLine) {\n}"); + + verifyFormat( + "SomeClass::Constructor()\n" + " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {\n" + "}"); + + verifyFormat( + "SomeClass::Constructor()\n" + " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" + " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {\n" + "}"); + + verifyFormat("Constructor()\n" + " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" + " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" + " aaaaaaaaaaaaaaaaaaaaaaa() {\n" + "}"); + + // Here a line could be saved by splitting the second initializer onto two + // lines, but that is not desireable. + verifyFormat("Constructor()\n" + " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" + " aaaaaaaaaaa(aaaaaaaaaaa),\n" + " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" + "}"); + + verifyGoogleFormat("MyClass::MyClass(int var)\n" + " : some_var_(var), // 4 space indent\n" + " some_other_var_(var + 1) { // lined up\n" + "}"); +} + TEST_F(FormatTest, BreaksAsHighAsPossible) { verifyFormat( "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" @@ -461,13 +496,11 @@ TEST_F(FormatTest, UnderstandsEquals) { } TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { - verifyFormat( - "LoooooooooooooooooooooooooooooooooooooongObject\n" - " .looooooooooooooooooooooooooooooooooooooongFunction();"); + verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" + " .looooooooooooooooooooooooooooooooooooooongFunction();"); - verifyFormat( - "LoooooooooooooooooooooooooooooooooooooongObject\n" - " ->looooooooooooooooooooooooooooooooooooooongFunction();"); + verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" + " ->looooooooooooooooooooooooooooooooooooooongFunction();"); verifyFormat( "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" @@ -485,10 +518,9 @@ TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { "function(LoooooooooooooooooooooooooooooooooooongObject\n" " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); - verifyFormat( - "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" - " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" - "}"); + verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" + " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" + "}"); } TEST_F(FormatTest, UnderstandsTemplateParameters) { |