r175800 - Consistently put {} onto the same line for empty functions.
Daniel Jasper
djasper at google.com
Thu Feb 21 13:33:56 PST 2013
Author: djasper
Date: Thu Feb 21 15:33:55 2013
New Revision: 175800
URL: http://llvm.org/viewvc/llvm-project?rev=175800&view=rev
Log:
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) {}
Modified:
cfe/trunk/lib/Format/Format.cpp
cfe/trunk/lib/Format/TokenAnnotator.cpp
cfe/trunk/unittests/Format/FormatTest.cpp
Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=175800&r1=175799&r2=175800&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Thu Feb 21 15:33:55 2013
@@ -272,8 +272,7 @@ public:
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 @@ private:
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 @@ private:
/// 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 @@ public:
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 @@ private:
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 @@ private:
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 @@ private:
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 @@ private:
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 @@ private:
}
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];
}
}
Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=175800&r1=175799&r2=175800&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Thu Feb 21 15:33:55 2013
@@ -517,8 +517,7 @@ private:
: BindingStrength(BindingStrength), LongestObjCSelectorName(0),
ColonIsForRangeExpr(false), ColonIsObjCMethodExpr(false),
FirstObjCSelectorName(NULL), IsExpression(IsExpression),
- LookForFunctionName(false) {
- }
+ LookForFunctionName(false) {}
unsigned BindingStrength;
unsigned LongestObjCSelectorName;
@@ -1104,6 +1103,7 @@ bool TokenAnnotator::canBreakBefore(cons
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)) ||
Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=175800&r1=175799&r2=175800&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Thu Feb 21 15:33:55 2013
@@ -1062,7 +1062,7 @@ TEST_F(FormatTest, PutEmptyBlocksIntoOne
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 @@ TEST_F(FormatTest, ConstructorInitialize
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 @@ TEST_F(FormatTest, ConstructorInitialize
"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 @@ TEST_F(FormatTest, ConstructorInitialize
for (unsigned i = 0, e = 80; i != e; ++i) {
input += " a,\n";
}
- input += " a) {\n}";
+ input += " a) {}";
verifyGoogleFormat(input);
}
@@ -1179,7 +1180,7 @@ TEST_F(FormatTest, BreaksDesireably) {
verifyFormat(
"aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
- " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
@@ -1300,7 +1301,7 @@ TEST_F(FormatTest, DoesNotBreakTrailingA
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, FormatsFunctionTypes)
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 @@ TEST_F(FormatTest, IncompleteParameterLi
" double *min_y,\n"
" double *max_y,\n"
" double *min_z,\n"
- " double *max_z, ) {\n"
- "}");
+ " double *max_z, ) {}");
}
TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
More information about the cfe-commits
mailing list