Consistently put {} onto the same line for empty functions.
This fixes llvm.org/PR15167.
Before:
LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL()
: AAAAAAAA(10), BBBBBBBBB(10) {
}
LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL() : AAAAAAAA(10) {}
After:
LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL()
: AAAAAAAA(10), BBBBBBBBB(10) {}
LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL() : AAAAAAAA(10) {}
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@175800 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp
index 2bd5332..880be39 100644
--- a/lib/Format/Format.cpp
+++ b/lib/Format/Format.cpp
@@ -272,8 +272,7 @@
WhitespaceManager &Whitespaces, bool StructuralError)
: Style(Style), SourceMgr(SourceMgr), Line(Line),
FirstIndent(FirstIndent), RootToken(RootToken),
- Whitespaces(Whitespaces), Count(0) {
- }
+ Whitespaces(Whitespaces), Count(0) {}
/// \brief Formats an \c UnwrappedLine.
///
@@ -332,8 +331,7 @@
BreakBeforeClosingBrace(false), QuestionColumn(0),
AvoidBinPacking(AvoidBinPacking), BreakBeforeParameter(false),
HasMultiParameterLine(HasMultiParameterLine), ColonPos(0),
- BreakBeforeThirdOperand(false) {
- }
+ BreakBeforeThirdOperand(false) {}
/// \brief The position to which a specific parenthesis level needs to be
/// indented.
@@ -766,8 +764,7 @@
/// inserting a newline dependent on the \c NewLine.
struct StateNode {
StateNode(const LineState &State, bool NewLine, StateNode *Previous)
- : State(State), NewLine(NewLine), Previous(Previous) {
- }
+ : State(State), NewLine(NewLine), Previous(Previous) {}
LineState State;
bool NewLine;
StateNode *Previous;
@@ -1038,8 +1035,7 @@
SourceManager &SourceMgr,
const std::vector<CharSourceRange> &Ranges)
: Diag(Diag), Style(Style), Lex(Lex), SourceMgr(SourceMgr),
- Whitespaces(SourceMgr), Ranges(Ranges) {
- }
+ Whitespaces(SourceMgr), Ranges(Ranges) {}
virtual ~Formatter() {}
@@ -1199,18 +1195,14 @@
void tryFitMultipleLinesInOne(unsigned Indent,
std::vector<AnnotatedLine>::iterator &I,
std::vector<AnnotatedLine>::iterator E) {
- unsigned Limit = Style.ColumnLimit - (I->InPPDirective ? 1 : 0) - Indent;
-
// We can never merge stuff if there are trailing line comments.
if (I->Last->Type == TT_LineComment)
return;
- // Check whether the UnwrappedLine can be put onto a single line. If
- // so, this is bound to be the optimal solution (by definition) and we
- // don't need to analyze the entire solution space.
- if (I->Last->TotalLength > Limit)
- return;
- Limit -= I->Last->TotalLength;
+ unsigned Limit = Style.ColumnLimit - (I->InPPDirective ? 1 : 0) - Indent;
+ // If we already exceed the column limit, we set 'Limit' to 0. The different
+ // tryMerge..() functions can then decide whether to still do merging.
+ Limit = I->Last->TotalLength > Limit ? 0 : Limit - I->Last->TotalLength;
if (I + 1 == E || (I + 1)->Type == LT_Invalid)
return;
@@ -1229,6 +1221,8 @@
void tryMergeSimplePPDirective(std::vector<AnnotatedLine>::iterator &I,
std::vector<AnnotatedLine>::iterator E,
unsigned Limit) {
+ if (Limit == 0)
+ return;
AnnotatedLine &Line = *I;
if (!(I + 1)->InPPDirective || (I + 1)->First.FormatTok.HasUnescapedNewline)
return;
@@ -1243,6 +1237,8 @@
void tryMergeSimpleIf(std::vector<AnnotatedLine>::iterator &I,
std::vector<AnnotatedLine>::iterator E,
unsigned Limit) {
+ if (Limit == 0)
+ return;
if (!Style.AllowShortIfStatementsOnASingleLine)
return;
if ((I + 1)->InPPDirective != I->InPPDirective ||
@@ -1282,11 +1278,13 @@
AnnotatedToken *Tok = &(I + 1)->First;
if (Tok->Children.empty() && Tok->is(tok::r_brace) &&
- !Tok->MustBreakBefore && Tok->TotalLength <= Limit) {
+ !Tok->MustBreakBefore) {
+ // We merge empty blocks even if the line exceeds the column limit.
Tok->SpacesRequiredBefore = 0;
+ Tok->CanBreakBefore = true;
join(Line, *(I + 1));
I += 1;
- } else {
+ } else if (Limit != 0) {
// Check that we still have three lines and they fit into the limit.
if (I + 2 == E || (I + 2)->Type == LT_Invalid ||
!nextTwoLinesFitInto(I, Limit))
@@ -1321,9 +1319,11 @@
}
void join(AnnotatedLine &A, const AnnotatedLine &B) {
+ unsigned LengthA = A.Last->TotalLength + B.First.SpacesRequiredBefore;
A.Last->Children.push_back(B.First);
while (!A.Last->Children.empty()) {
A.Last->Children[0].Parent = A.Last;
+ A.Last->Children[0].TotalLength += LengthA;
A.Last = &A.Last->Children[0];
}
}
diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp
index dcbdb78..af59a18 100644
--- a/lib/Format/TokenAnnotator.cpp
+++ b/lib/Format/TokenAnnotator.cpp
@@ -517,8 +517,7 @@
: BindingStrength(BindingStrength), LongestObjCSelectorName(0),
ColonIsForRangeExpr(false), ColonIsObjCMethodExpr(false),
FirstObjCSelectorName(NULL), IsExpression(IsExpression),
- LookForFunctionName(false) {
- }
+ LookForFunctionName(false) {}
unsigned BindingStrength;
unsigned LongestObjCSelectorName;
@@ -1104,6 +1103,7 @@
Right.is(tok::arrow) || Right.is(tok::period) ||
Right.is(tok::colon) || Left.is(tok::coloncolon) ||
Left.is(tok::semi) || Left.is(tok::l_brace) ||
+ Right.is(tok::r_brace) ||
(Left.is(tok::r_paren) && Left.Type != TT_CastRParen &&
Right.is(tok::identifier)) ||
(Left.is(tok::l_paren) && !Right.is(tok::r_paren)) ||
diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp
index 9081304..00657e0 100644
--- a/unittests/Format/FormatTest.cpp
+++ b/unittests/Format/FormatTest.cpp
@@ -1062,7 +1062,7 @@
TEST_F(FormatTest, FormatsFunctionDefinition) {
verifyFormat("void f(int a, int b, int c, int d, int e, int f, int g,"
" int h, int j, int f,\n"
- " int c, int ddddddddddddd) {\n}");
+ " int c, int ddddddddddddd) {}");
}
TEST_F(FormatTest, FormatsAwesomeMethodCall) {
@@ -1097,34 +1097,35 @@
verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
getLLVMStyleWithColumns(45));
- verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {\n}",
+ verifyFormat("Constructor()\n"
+ " : Inttializer(FitsOnTheLine) {}",
getLLVMStyleWithColumns(44));
verifyFormat("Constructor()\n"
- " : Inttializer(FitsOnTheLine) {\n}",
+ " : Inttializer(FitsOnTheLine) {}",
getLLVMStyleWithColumns(43));
verifyFormat(
"SomeClass::Constructor()\n"
- " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {\n}");
+ " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
verifyFormat(
"SomeClass::Constructor()\n"
" : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
- " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {\n}");
+ " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
verifyFormat(
"SomeClass::Constructor()\n"
" : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
- " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {\n}");
+ " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
verifyFormat("Constructor()\n"
" : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
" aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
- " aaaaaaaaaaaaaaaaaaaaaaa() {\n}");
+ " aaaaaaaaaaaaaaaaaaaaaaa() {}");
verifyFormat("Constructor()\n"
" : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
- " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
// Here a line could be saved by splitting the second initializer onto two
// lines, but that is not desireable.
@@ -1132,19 +1133,19 @@
"Constructor()\n"
" : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
" aaaaaaaaaaa(aaaaaaaaaaa),\n"
- " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
+ " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
FormatStyle OnePerLine = getLLVMStyle();
OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
verifyFormat("SomeClass::Constructor()\n"
" : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
" aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
- " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {\n}",
+ " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
OnePerLine);
verifyFormat("SomeClass::Constructor()\n"
" : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
" aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
- " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {\n}",
+ " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
OnePerLine);
verifyFormat("MyClass::MyClass(int var)\n"
" : some_var_(var), // 4 space indent\n"
@@ -1158,7 +1159,7 @@
for (unsigned i = 0, e = 80; i != e; ++i) {
input += " a,\n";
}
- input += " a) {\n}";
+ input += " a) {}";
verifyGoogleFormat(input);
}
@@ -1179,7 +1180,7 @@
verifyFormat(
"aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
- " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
@@ -1300,7 +1301,7 @@
verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
" GUARDED_BY(aaaaaaaaaaaaa);");
verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
- " GUARDED_BY(aaaaaaaaaaaaa) {\n}");
+ " GUARDED_BY(aaaaaaaaaaaaa) {}");
}
TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
@@ -1902,22 +1903,22 @@
TEST_F(FormatTest, BreaksFunctionDeclarations) {
verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
- " int LoooooooooooooooooooongParam2) {\n}");
+ " int LoooooooooooooooooooongParam2) {}");
verifyFormat(
"TypeSpecDecl *\n"
"TypeSpecDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,\n"
- " IdentifierIn *II, Type *T) {\n}");
+ " IdentifierIn *II, Type *T) {}");
verifyGoogleFormat(
"TypeSpecDecl* TypeSpecDecl::Create(\n"
- " ASTContext& C, DeclContext* DC, SourceLocation L) {\n}");
+ " ASTContext& C, DeclContext* DC, SourceLocation L) {}");
verifyGoogleFormat(
"some_namespace::LongReturnType\n"
"long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
- " int first_long_parameter, int second_parameter) {\n}");
+ " int first_long_parameter, int second_parameter) {}");
verifyGoogleFormat("template <typename T>\n"
"aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
- "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {\n}");
+ "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
}
TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
@@ -1952,8 +1953,7 @@
" double *min_y,\n"
" double *max_y,\n"
" double *min_z,\n"
- " double *max_z, ) {\n"
- "}");
+ " double *max_z, ) {}");
}
TEST_F(FormatTest, IncorrectCodeTrailingStuff) {