diff options
author | Alexander Kornienko <alexfh@google.com> | 2013-12-16 14:35:51 +0000 |
---|---|---|
committer | Alexander Kornienko <alexfh@google.com> | 2013-12-16 14:35:51 +0000 |
commit | a594ba8a76c75c0ec293cfae028bd45c093bae2c (patch) | |
tree | 538b8d36d35776c61945a02a70ba3d1adf61e27e | |
parent | e8323a88ea252a0b16f2d54a129cfc8ac7ab744c (diff) | |
download | bcm5719-llvm-a594ba8a76c75c0ec293cfae028bd45c093bae2c.tar.gz bcm5719-llvm-a594ba8a76c75c0ec293cfae028bd45c093bae2c.zip |
Always break before the colon in constructor initializers, when
BreakConstructorInitializersBeforeComma is true.
This option is used in WebKit style, so this also ensures initializer lists are
not put on a single line, as per the WebKit coding guidelines.
Patch by Florian Sowade!
llvm-svn: 197386
-rw-r--r-- | clang/lib/Format/ContinuationIndenter.cpp | 9 | ||||
-rw-r--r-- | clang/lib/Format/Format.cpp | 8 | ||||
-rw-r--r-- | clang/lib/Format/TokenAnnotator.cpp | 3 | ||||
-rw-r--r-- | clang/unittests/Format/FormatTest.cpp | 68 |
4 files changed, 67 insertions, 21 deletions
diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index e63f72d65e6..e8dc8d4a7f1 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -183,9 +183,12 @@ bool ContinuationIndenter::mustBreak(const LineState &State) { Current.LongestObjCSelectorName == 0 && State.Stack.back().BreakBeforeParameter) return true; - if ((Current.Type == TT_CtorInitializerColon || - (Previous.ClosesTemplateDeclaration && State.ParenLevel == 0 && - !Current.isTrailingComment()))) + if (Current.Type == TT_CtorInitializerColon && + (!Style.AllowShortFunctionsOnASingleLine || + Style.BreakConstructorInitializersBeforeComma || Style.ColumnLimit != 0)) + return true; + if (Previous.ClosesTemplateDeclaration && State.ParenLevel == 0 && + !Current.isTrailingComment()) return true; if ((Current.Type == TT_StartOfName || Current.is(tok::kw_operator)) && diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index 87c2fd70aee..86619270b36 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -455,13 +455,12 @@ public: /// \brief Formats the line starting at \p State, simply keeping all of the /// input's line breaking decisions. - void format(unsigned FirstIndent, const AnnotatedLine *Line, - bool LineIsMerged) { + void format(unsigned FirstIndent, const AnnotatedLine *Line) { LineState State = Indenter->getInitialState(FirstIndent, Line, /*DryRun=*/false); while (State.NextToken != NULL) { bool Newline = - (!LineIsMerged && Indenter->mustBreak(State)) || + Indenter->mustBreak(State) || (Indenter->canBreak(State) && State.NextToken->NewlinesBefore > 0); Indenter->addTokenToState(State, Newline, /*DryRun=*/false); } @@ -728,8 +727,7 @@ public: // FIXME: Implement nested blocks for ColumnLimit = 0. NoColumnLimitFormatter Formatter(Indenter); if (!DryRun) - Formatter.format(Indent, &TheLine, - /*LineIsMerged=*/MergedLines > 0); + Formatter.format(Indent, &TheLine); } else { Penalty += format(TheLine, Indent, DryRun); } diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 19759afc854..e2615dbb6a2 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -1407,7 +1407,8 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line, Right.Previous->MatchingParen->NestingLevel == 0 && Style.AlwaysBreakTemplateDeclarations) { return true; - } else if (Right.Type == TT_CtorInitializerComma && + } else if ((Right.Type == TT_CtorInitializerComma || + Right.Type == TT_CtorInitializerColon) && Style.BreakConstructorInitializersBeforeComma && !Style.ConstructorInitializerAllOnOneLineOrOnePerLine) { return true; diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index bd9d1083576..5e38f5974a7 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -7524,7 +7524,38 @@ TEST_F(FormatTest, ConstructorInitializerIndentWidth) { " , b(b)\n" " , c(c) {}", Style); + verifyFormat("SomeClass::Constructor()\n" + " : a(a) {}", + Style); + + Style.ColumnLimit = 0; + verifyFormat("SomeClass::Constructor()\n" + " : a(a) {}", + Style); + verifyFormat("SomeClass::Constructor()\n" + " : a(a)\n" + " , b(b)\n" + " , c(c) {}", + Style); + verifyFormat("SomeClass::Constructor()\n" + " : a(a) {\n" + " foo();\n" + " bar();\n" + "}", + Style); + + Style.AllowShortFunctionsOnASingleLine = false; + verifyFormat("SomeClass::Constructor()\n" + " : a(a)\n" + " , b(b)\n" + " , c(c) {\n}", + Style); + verifyFormat("SomeClass::Constructor()\n" + " : a(a) {\n}", + Style); + Style.ColumnLimit = 80; + Style.AllowShortFunctionsOnASingleLine = true; Style.ConstructorInitializerIndentWidth = 2; verifyFormat("SomeClass::Constructor()\n" " : a(a)\n" @@ -7541,6 +7572,10 @@ TEST_F(FormatTest, ConstructorInitializerIndentWidth) { Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; Style.ConstructorInitializerIndentWidth = 4; + verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style); + verifyFormat( + "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n", + Style); verifyFormat( "SomeClass::Constructor()\n" " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}", @@ -7600,18 +7635,27 @@ TEST_F(FormatTest, FormatsWithWebKitStyle) { // Constructor initializers are formatted one per line with the "," on the // new line. - EXPECT_EQ( - "Constructor()\n" - " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" - " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n" - " aaaaaaaaaaaaaa)\n" - " , aaaaaaaaaaaaaaaaaaaaaaa() {}", - format("Constructor()\n" - " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" - " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n" - " aaaaaaaaaaaaaa)\n" - " , aaaaaaaaaaaaaaaaaaaaaaa() {}", - Style)); + verifyFormat("Constructor()\n" + " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" + " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n" + " aaaaaaaaaaaaaa)\n" + " , aaaaaaaaaaaaaaaaaaaaaaa() {}", + Style); + verifyFormat("SomeClass::Constructor()\n" + " : a(a) {}", + Style); + verifyFormat("SomeClass::Constructor()\n" + " : a(a)\n" + " , b(b)\n" + " , c(c) {}", + Style); + verifyFormat("SomeClass::Constructor()\n" + " : a(a)\n" + "{\n" + " foo();\n" + " bar();\n" + "}", + Style); // Access specifiers should be aligned left. verifyFormat("class C {\n" |