r197386 - Always break before the colon in constructor initializers, when
Alexander Kornienko
alexfh at google.com
Mon Dec 16 06:35:51 PST 2013
Author: alexfh
Date: Mon Dec 16 08:35:51 2013
New Revision: 197386
URL: http://llvm.org/viewvc/llvm-project?rev=197386&view=rev
Log:
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!
Modified:
cfe/trunk/lib/Format/ContinuationIndenter.cpp
cfe/trunk/lib/Format/Format.cpp
cfe/trunk/lib/Format/TokenAnnotator.cpp
cfe/trunk/unittests/Format/FormatTest.cpp
Modified: cfe/trunk/lib/Format/ContinuationIndenter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/ContinuationIndenter.cpp?rev=197386&r1=197385&r2=197386&view=diff
==============================================================================
--- cfe/trunk/lib/Format/ContinuationIndenter.cpp (original)
+++ cfe/trunk/lib/Format/ContinuationIndenter.cpp Mon Dec 16 08:35:51 2013
@@ -183,9 +183,12 @@ bool ContinuationIndenter::mustBreak(con
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)) &&
Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=197386&r1=197385&r2=197386&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Mon Dec 16 08:35:51 2013
@@ -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);
}
Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=197386&r1=197385&r2=197386&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Mon Dec 16 08:35:51 2013
@@ -1407,7 +1407,8 @@ bool TokenAnnotator::mustBreakBefore(con
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;
Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=197386&r1=197385&r2=197386&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Mon Dec 16 08:35:51 2013
@@ -7524,7 +7524,38 @@ TEST_F(FormatTest, ConstructorInitialize
" , 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, ConstructorInitialize
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, FormatsWithWebKitStyl
// 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"
More information about the cfe-commits
mailing list