r190674 - clang-format: Fix incorrect enum parsing / layouting.
Daniel Jasper
djasper at google.com
Fri Sep 13 02:20:45 PDT 2013
Author: djasper
Date: Fri Sep 13 04:20:45 2013
New Revision: 190674
URL: http://llvm.org/viewvc/llvm-project?rev=190674&view=rev
Log:
clang-format: Fix incorrect enum parsing / layouting.
Before:
enum {
Bar = Foo < int,
int > ::value
};
After:
enum {
Bar = Foo<int, int>::value
};
Modified:
cfe/trunk/lib/Format/ContinuationIndenter.cpp
cfe/trunk/lib/Format/Format.cpp
cfe/trunk/lib/Format/FormatToken.cpp
cfe/trunk/lib/Format/TokenAnnotator.cpp
cfe/trunk/lib/Format/UnwrappedLineParser.cpp
cfe/trunk/lib/Format/UnwrappedLineParser.h
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=190674&r1=190673&r2=190674&view=diff
==============================================================================
--- cfe/trunk/lib/Format/ContinuationIndenter.cpp (original)
+++ cfe/trunk/lib/Format/ContinuationIndenter.cpp Fri Sep 13 04:20:45 2013
@@ -106,8 +106,10 @@ bool ContinuationIndenter::mustBreak(con
const FormatToken &Previous = *Current.Previous;
if (Current.MustBreakBefore || Current.Type == TT_InlineASMColon)
return true;
- if (!Style.Cpp11BracedListStyle && Current.is(tok::r_brace) &&
- State.Stack.back().BreakBeforeClosingBrace)
+ if ((!Style.Cpp11BracedListStyle ||
+ (Current.MatchingParen &&
+ Current.MatchingParen->BlockKind == BK_Block)) &&
+ Current.is(tok::r_brace) && State.Stack.back().BreakBeforeClosingBrace)
return true;
if (Previous.is(tok::semi) && State.LineContainsContinuedForLoopSection)
return true;
@@ -224,7 +226,9 @@ unsigned ContinuationIndenter::addTokenT
State.Column <= Style.ColumnLimit / 2)
Penalty += Style.PenaltyBreakFirstLessLess;
- if (Current.is(tok::r_brace)) {
+ if (Current.is(tok::l_brace) && Current.BlockKind == BK_Block) {
+ State.Column = State.FirstIndent;
+ } else if (Current.is(tok::r_brace)) {
if (Current.MatchingParen &&
(Current.MatchingParen->BlockKind == BK_BracedInit ||
!Current.MatchingParen->Children.empty()))
@@ -524,14 +528,15 @@ unsigned ContinuationIndenter::moveState
// });
for (unsigned i = 0; i != Current.MatchingParen->FakeRParens; ++i)
State.Stack.pop_back();
- NewIndent = State.Stack.back().LastSpace;
+ NewIndent = State.Stack.back().LastSpace + Style.IndentWidth;
} else {
NewIndent = State.Stack.back().LastSpace +
(Style.Cpp11BracedListStyle ? 4 : Style.IndentWidth);
}
const FormatToken *NextNoComment = Current.getNextNonComment();
- AvoidBinPacking = NextNoComment &&
- NextNoComment->Type == TT_DesignatedInitializerPeriod;
+ AvoidBinPacking = Current.BlockKind == BK_Block ||
+ (NextNoComment &&
+ NextNoComment->Type == TT_DesignatedInitializerPeriod);
} else {
NewIndent = 4 + std::max(State.Stack.back().LastSpace,
State.Stack.back().StartOfFunctionCall);
@@ -545,6 +550,7 @@ unsigned ContinuationIndenter::moveState
State.Stack.push_back(ParenState(NewIndent, State.Stack.back().LastSpace,
AvoidBinPacking,
State.Stack.back().NoLineBreak));
+ State.Stack.back().BreakBeforeParameter = Current.BlockKind == BK_Block;
++State.ParenLevel;
}
Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=190674&r1=190673&r2=190674&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Fri Sep 13 04:20:45 2013
@@ -529,7 +529,7 @@ private:
E = LBrace.Children.end();
I != E; ++I) {
unsigned Indent =
- ParentIndent + ((*I)->Level - Line.Level) * Style.IndentWidth;
+ ParentIndent + ((*I)->Level - Line.Level - 1) * Style.IndentWidth;
if (!DryRun) {
unsigned Newlines = std::min((*I)->First->NewlinesBefore,
Style.MaxEmptyLinesToKeep + 1);
Modified: cfe/trunk/lib/Format/FormatToken.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/FormatToken.cpp?rev=190674&r1=190673&r2=190674&view=diff
==============================================================================
--- cfe/trunk/lib/Format/FormatToken.cpp (original)
+++ cfe/trunk/lib/Format/FormatToken.cpp Fri Sep 13 04:20:45 2013
@@ -36,6 +36,7 @@ unsigned CommaSeparatedList::format(Line
// Ensure that we start on the opening brace.
const FormatToken *LBrace = State.NextToken->Previous->Previous;
if (LBrace->isNot(tok::l_brace) ||
+ LBrace->BlockKind == BK_Block ||
LBrace->Next->Type == TT_DesignatedInitializerPeriod)
return 0;
Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=190674&r1=190673&r2=190674&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Fri Sep 13 04:20:45 2013
@@ -1057,6 +1057,12 @@ void TokenAnnotator::calculateFormatting
} else if (Current->Type == TT_CtorInitializerComma &&
Style.BreakConstructorInitializersBeforeComma) {
Current->MustBreakBefore = true;
+ } else if (Current->Previous->BlockKind == BK_Block &&
+ Current->isNot(tok::r_brace)) {
+ Current->MustBreakBefore = true;
+ } else if (Current->is(tok::l_brace) && (Current->BlockKind == BK_Block)) {
+ Current->MustBreakBefore =
+ Style.BreakBeforeBraces == FormatStyle::BS_Allman;
}
Current->CanBreakBefore =
Current->MustBreakBefore || canBreakBefore(Line, *Current);
Modified: cfe/trunk/lib/Format/UnwrappedLineParser.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineParser.cpp?rev=190674&r1=190673&r2=190674&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UnwrappedLineParser.cpp (original)
+++ cfe/trunk/lib/Format/UnwrappedLineParser.cpp Fri Sep 13 04:20:45 2013
@@ -759,7 +759,8 @@ bool UnwrappedLineParser::tryToParseBrac
return true;
}
-void UnwrappedLineParser::parseBracedList() {
+bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons) {
+ bool HasError = false;
nextToken();
// FIXME: Once we have an expression parser in the UnwrappedLineParser,
@@ -786,10 +787,13 @@ void UnwrappedLineParser::parseBracedLis
break;
case tok::r_brace:
nextToken();
- return;
+ return !HasError;
case tok::semi:
- // Probably a missing closing brace. Bail out.
- return;
+ HasError = true;
+ if (!ContinueOnSemicolons)
+ return !HasError;
+ nextToken();
+ break;
case tok::comma:
nextToken();
break;
@@ -798,6 +802,7 @@ void UnwrappedLineParser::parseBracedLis
break;
}
} while (!eof());
+ return false;
}
void UnwrappedLineParser::parseReturn() {
@@ -1046,42 +1051,14 @@ void UnwrappedLineParser::parseEnum() {
if (FormatTok->Tok.is(tok::identifier))
nextToken();
}
- bool HasError = false;
if (FormatTok->Tok.is(tok::l_brace)) {
- if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
- addUnwrappedLine();
- nextToken();
- addUnwrappedLine();
- ++Line->Level;
- do {
- switch (FormatTok->Tok.getKind()) {
- case tok::l_paren:
- parseParens();
- break;
- case tok::r_brace:
- addUnwrappedLine();
- nextToken();
- --Line->Level;
- if (HasError) {
- if (FormatTok->is(tok::semi))
- nextToken();
- addUnwrappedLine();
- }
- return;
- case tok::semi:
- HasError = true;
- nextToken();
- addUnwrappedLine();
- break;
- case tok::comma:
+ FormatTok->BlockKind = BK_Block;
+ bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true);
+ if (HasError) {
+ if (FormatTok->is(tok::semi))
nextToken();
- addUnwrappedLine();
- break;
- default:
- nextToken();
- break;
- }
- } while (!eof());
+ addUnwrappedLine();
+ }
}
// We fall through to parsing a structural element afterwards, so that in
// enum A {} n, m;
Modified: cfe/trunk/lib/Format/UnwrappedLineParser.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineParser.h?rev=190674&r1=190673&r2=190674&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UnwrappedLineParser.h (original)
+++ cfe/trunk/lib/Format/UnwrappedLineParser.h Fri Sep 13 04:20:45 2013
@@ -79,7 +79,7 @@ private:
void parsePPUnknown();
void parseStructuralElement();
bool tryToParseBracedList();
- void parseBracedList();
+ bool parseBracedList(bool ContinueOnSemicolons = false);
void parseReturn();
void parseParens();
void parseIfThenElse();
Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=190674&r1=190673&r2=190674&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Fri Sep 13 04:20:45 2013
@@ -1530,12 +1530,23 @@ TEST_F(FormatTest, FormatsEnum) {
" Four = (Zero && (One ^ Two)) | (One << Two),\n"
" Five = (One, Two, Three, Four, 5)\n"
"};");
+ verifyGoogleFormat("enum {\n"
+ " Zero,\n"
+ " One = 1,\n"
+ " Two = One + 1,\n"
+ " Three = (One + Two),\n"
+ " Four = (Zero && (One ^ Two)) | (One << Two),\n"
+ " Five = (One, Two, Three, Four, 5)\n"
+ "};");
verifyFormat("enum Enum {};");
verifyFormat("enum {};");
- verifyFormat("enum X E {\n} d;");
- verifyFormat("enum __attribute__((...)) E {\n} d;");
- verifyFormat("enum __declspec__((...)) E {\n} d;");
+ verifyFormat("enum X E {} d;");
+ verifyFormat("enum __attribute__((...)) E {} d;");
+ verifyFormat("enum __declspec__((...)) E {} d;");
verifyFormat("enum X f() {\n a();\n return 42;\n}");
+ verifyFormat("enum {\n"
+ " Bar = Foo<int, int>::value\n"
+ "};");
}
TEST_F(FormatTest, FormatsEnumsWithErrors) {
@@ -1563,9 +1574,9 @@ TEST_F(FormatTest, FormatsEnumStruct) {
"};");
verifyFormat("enum struct Enum {};");
verifyFormat("enum struct {};");
- verifyFormat("enum struct X E {\n} d;");
- verifyFormat("enum struct __attribute__((...)) E {\n} d;");
- verifyFormat("enum struct __declspec__((...)) E {\n} d;");
+ verifyFormat("enum struct X E {} d;");
+ verifyFormat("enum struct __attribute__((...)) E {} d;");
+ verifyFormat("enum struct __declspec__((...)) E {} d;");
verifyFormat("enum struct X f() {\n a();\n return 42;\n}");
}
@@ -1580,9 +1591,9 @@ TEST_F(FormatTest, FormatsEnumClass) {
"};");
verifyFormat("enum class Enum {};");
verifyFormat("enum class {};");
- verifyFormat("enum class X E {\n} d;");
- verifyFormat("enum class __attribute__((...)) E {\n} d;");
- verifyFormat("enum class __declspec__((...)) E {\n} d;");
+ verifyFormat("enum class X E {} d;");
+ verifyFormat("enum class __attribute__((...)) E {} d;");
+ verifyFormat("enum class __declspec__((...)) E {} d;");
verifyFormat("enum class X f() {\n a();\n return 42;\n}");
}
More information about the cfe-commits
mailing list