r182767 - Initial support for designated initializers.
Daniel Jasper
djasper at google.com
Tue May 28 04:30:49 PDT 2013
Author: djasper
Date: Tue May 28 06:30:49 2013
New Revision: 182767
URL: http://llvm.org/viewvc/llvm-project?rev=182767&view=rev
Log:
Initial support for designated initializers.
Modified:
cfe/trunk/lib/Format/Format.cpp
cfe/trunk/lib/Format/TokenAnnotator.cpp
cfe/trunk/lib/Format/TokenAnnotator.h
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=182767&r1=182766&r2=182767&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Tue May 28 06:30:49 2013
@@ -502,7 +502,8 @@ private:
} else if (Current.is(tok::lessless) &&
State.Stack.back().FirstLessLess != 0) {
State.Column = State.Stack.back().FirstLessLess;
- } else if (Current.isOneOf(tok::period, tok::arrow)) {
+ } else if (Current.isOneOf(tok::period, tok::arrow) &&
+ Current.Type != TT_DesignatedInitializerPeriod) {
if (State.Stack.back().CallContinuation == 0) {
State.Column = ContinuationIndent;
State.Stack.back().CallContinuation = State.Column;
@@ -559,7 +560,8 @@ private:
}
State.Stack.back().LastSpace = State.Column;
- if (Current.isOneOf(tok::arrow, tok::period))
+ if (Current.isOneOf(tok::arrow, tok::period) &&
+ Current.Type != TT_DesignatedInitializerPeriod)
State.Stack.back().LastSpace += Current.FormatTok.TokenLength;
State.StartOfLineLevel = State.ParenLevel;
State.LowestLevelOnLine = State.ParenLevel;
@@ -734,7 +736,9 @@ private:
bool AvoidBinPacking;
if (Current.is(tok::l_brace)) {
NewIndent = Style.IndentWidth + LastSpace;
- AvoidBinPacking = false;
+ const AnnotatedToken *NextNoComment = Current.getNextNoneComment();
+ AvoidBinPacking = NextNoComment &&
+ NextNoComment->Type == TT_DesignatedInitializerPeriod;
} else {
NewIndent =
4 + std::max(LastSpace, State.Stack.back().StartOfFunctionCall);
Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=182767&r1=182766&r2=182767&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Tue May 28 06:30:49 2013
@@ -685,6 +685,11 @@ private:
default:
break;
}
+ } else if (Current.is(tok::period)) {
+ AnnotatedToken *PreviousNoComment= Current.getPreviousNoneComment();
+ if (PreviousNoComment &&
+ PreviousNoComment->isOneOf(tok::comma, tok::l_brace))
+ Current.Type = TT_DesignatedInitializerPeriod;
}
}
}
@@ -963,6 +968,11 @@ unsigned TokenAnnotator::splitPenalty(co
const AnnotatedToken &Left = *Tok.Parent;
const AnnotatedToken &Right = Tok;
+ if (Left.is(tok::semi))
+ return 0;
+ if (Left.is(tok::comma))
+ return 1;
+
if (Right.Type == TT_StartOfName) {
if (Line.First.is(tok::kw_for) && Right.PartOfMultiVariableDeclStmt)
return 3;
@@ -983,7 +993,8 @@ unsigned TokenAnnotator::splitPenalty(co
Left.Type == TT_InheritanceColon)
return 2;
- if (Right.isOneOf(tok::arrow, tok::period)) {
+ if (Right.isOneOf(tok::arrow, tok::period) &&
+ Right.Type != TT_DesignatedInitializerPeriod) {
if (Line.Type == LT_BuilderTypeCall)
return prec::PointerToMember;
if (Left.isOneOf(tok::r_paren, tok::r_square) && Left.MatchingParen &&
@@ -1000,11 +1011,6 @@ unsigned TokenAnnotator::splitPenalty(co
if (Line.First.is(tok::kw_for) && Left.is(tok::equal))
return 4;
- if (Left.is(tok::semi))
- return 0;
- if (Left.is(tok::comma))
- return 1;
-
// In Objective-C method expressions, prefer breaking before "param:" over
// breaking after it.
if (Right.Type == TT_ObjCSelectorName)
@@ -1087,8 +1093,6 @@ bool TokenAnnotator::spaceRequiredBetwee
return Right.Type == TT_ObjCArrayLiteral;
if (Right.is(tok::l_square) && Right.Type != TT_ObjCMethodExpr)
return false;
- if (Left.is(tok::period) || Right.is(tok::period))
- return false;
if (Left.is(tok::colon))
return Left.Type != TT_ObjCMethodExpr;
if (Right.is(tok::colon))
@@ -1116,6 +1120,8 @@ bool TokenAnnotator::spaceRequiredBetwee
return false;
if (Right.is(tok::ellipsis))
return false;
+ if (Left.is(tok::period) || Right.is(tok::period))
+ return false;
return true;
}
Modified: cfe/trunk/lib/Format/TokenAnnotator.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.h?rev=182767&r1=182766&r2=182767&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.h (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.h Tue May 28 06:30:49 2013
@@ -33,6 +33,7 @@ enum TokenType {
TT_CastRParen,
TT_ConditionalExpr,
TT_CtorInitializerColon,
+ TT_DesignatedInitializerPeriod,
TT_ImplicitStringLiteral,
TT_InlineASMColon,
TT_InheritanceColon,
Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=182767&r1=182766&r2=182767&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Tue May 28 06:30:49 2013
@@ -1456,6 +1456,24 @@ TEST_F(FormatTest, StaticInitializers) {
getLLVMStyleWithColumns(40));
}
+TEST_F(FormatTest, DesignatedInitializers) {
+ verifyFormat("const struct A a = { .a = 1, .b = 2 };");
+ verifyFormat("const struct A a = { .aaaaaaaaaa = 1,\n"
+ " .bbbbbbbbbb = 2,\n"
+ " .cccccccccc = 3,\n"
+ " .dddddddddd = 4,\n"
+ " .eeeeeeeeee = 5 };");
+ verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
+ " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
+ " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
+ " .ccccccccccccccccccccccccccc = 3,\n"
+ " .ddddddddddddddddddddddddddd = 4,\n"
+ " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5\n"
+ "};");
+
+ verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
+}
+
TEST_F(FormatTest, NestedStaticInitializers) {
verifyFormat("static A x = { { {} } };\n");
verifyFormat("static A x = { { { init1, init2, init3, init4 },\n"
More information about the cfe-commits
mailing list