clang-format: Cleanup array initializer and dict initializer formatting.
Significant changes:
- Also recognize these literals with missing "@" for robustness.
- Reorganize tests.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@193325 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Format/ContinuationIndenter.cpp b/lib/Format/ContinuationIndenter.cpp
index 16b1147..68b440d 100644
--- a/lib/Format/ContinuationIndenter.cpp
+++ b/lib/Format/ContinuationIndenter.cpp
@@ -98,6 +98,7 @@
// The opening "{" of a braced list has to be on the same line as the first
// element if it is nested in another braced init list or function call.
if (!Current.MustBreakBefore && Previous.is(tok::l_brace) &&
+ Previous.Type != TT_DictLiteral &&
Previous.BlockKind == BK_BracedInit && Previous.Previous &&
Previous.Previous->isOneOf(tok::l_brace, tok::l_paren, tok::comma))
return false;
@@ -134,7 +135,7 @@
!Previous.isOneOf(tok::kw_return, tok::lessless) &&
Previous.Type != TT_InlineASMColon && NextIsMultilineString(State))
return true;
- if (((Previous.Type == TT_ObjCDictLiteral && Previous.is(tok::l_brace)) ||
+ if (((Previous.Type == TT_DictLiteral && Previous.is(tok::l_brace)) ||
Previous.Type == TT_ArrayInitializerLSquare) &&
getLengthToMatchingParen(Previous) + State.Column > getColumnLimit(State))
return true;
@@ -572,7 +573,7 @@
const FormatToken *NextNoComment = Current.getNextNonComment();
AvoidBinPacking = Current.BlockKind == BK_Block ||
Current.Type == TT_ArrayInitializerLSquare ||
- Current.Type == TT_ObjCDictLiteral ||
+ Current.Type == TT_DictLiteral ||
(NextNoComment &&
NextNoComment->Type == TT_DesignatedInitializerPeriod);
} else {
diff --git a/lib/Format/FormatToken.cpp b/lib/Format/FormatToken.cpp
index 35764ce..4ca4edb 100644
--- a/lib/Format/FormatToken.cpp
+++ b/lib/Format/FormatToken.cpp
@@ -37,6 +37,7 @@
const FormatToken *LBrace = State.NextToken->Previous->Previous;
if (LBrace->isNot(tok::l_brace) ||
LBrace->BlockKind == BK_Block ||
+ LBrace->Type == TT_DictLiteral ||
LBrace->Next->Type == TT_DesignatedInitializerPeriod)
return 0;
diff --git a/lib/Format/FormatToken.h b/lib/Format/FormatToken.h
index 2001d42..2145ee2 100644
--- a/lib/Format/FormatToken.h
+++ b/lib/Format/FormatToken.h
@@ -35,6 +35,7 @@
TT_CtorInitializerColon,
TT_CtorInitializerComma,
TT_DesignatedInitializerPeriod,
+ TT_DictLiteral,
TT_ImplicitStringLiteral,
TT_InlineASMColon,
TT_InheritanceColon,
@@ -43,7 +44,6 @@
TT_LineComment,
TT_ObjCBlockLParen,
TT_ObjCDecl,
- TT_ObjCDictLiteral,
TT_ObjCForIn,
TT_ObjCMethodExpr,
TT_ObjCMethodSpecifier,
@@ -344,7 +344,7 @@
bool opensBlockTypeList(const FormatStyle &Style) const {
return Type == TT_ArrayInitializerLSquare ||
(is(tok::l_brace) &&
- (BlockKind == BK_Block || Type == TT_ObjCDictLiteral ||
+ (BlockKind == BK_Block || Type == TT_DictLiteral ||
!Style.Cpp11BracedListStyle));
}
diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp
index e398442..7fa9621 100644
--- a/lib/Format/TokenAnnotator.cpp
+++ b/lib/Format/TokenAnnotator.cpp
@@ -235,7 +235,8 @@
if (CurrentToken->isOneOf(tok::r_paren, tok::r_brace))
return false;
if (CurrentToken->is(tok::comma) &&
- Left->Type == TT_ArraySubscriptLSquare)
+ (Left->Type == TT_ArraySubscriptLSquare ||
+ Left->Type == TT_ObjCMethodExpr))
Left->Type = TT_ArrayInitializerLSquare;
updateParameterCount(Left, CurrentToken);
if (!consumeToken())
@@ -246,20 +247,12 @@
bool parseBrace() {
if (CurrentToken != NULL) {
- ScopedContextCreator ContextCreator(*this, tok::l_brace, 1);
FormatToken *Left = CurrentToken->Previous;
-
- FormatToken *Parent = Left->getPreviousNonComment();
- bool StartsObjCDictLiteral = Parent && Parent->is(tok::at);
- if (StartsObjCDictLiteral) {
- Contexts.back().ColonIsObjCDictLiteral = true;
- Left->Type = TT_ObjCDictLiteral;
- }
+ ScopedContextCreator ContextCreator(*this, tok::l_brace, 1);
+ Contexts.back().ColonIsDictLiteral = true;
while (CurrentToken != NULL) {
if (CurrentToken->is(tok::r_brace)) {
- if (StartsObjCDictLiteral)
- CurrentToken->Type = TT_ObjCDictLiteral;
Left->MatchingParen = CurrentToken;
CurrentToken->MatchingParen = Left;
next();
@@ -268,6 +261,8 @@
if (CurrentToken->isOneOf(tok::r_paren, tok::r_square))
return false;
updateParameterCount(Left, CurrentToken);
+ if (CurrentToken->is(tok::colon))
+ Left->Type = TT_DictLiteral;
if (!consumeToken())
return false;
}
@@ -329,8 +324,8 @@
// Colons from ?: are handled in parseConditional().
if (Tok->Previous->is(tok::r_paren) && Contexts.size() == 1) {
Tok->Type = TT_CtorInitializerColon;
- } else if (Contexts.back().ColonIsObjCDictLiteral) {
- Tok->Type = TT_ObjCDictLiteral;
+ } else if (Contexts.back().ColonIsDictLiteral) {
+ Tok->Type = TT_DictLiteral;
} else if (Contexts.back().ColonIsObjCMethodExpr ||
Line.First->Type == TT_ObjCMethodSpecifier) {
Tok->Type = TT_ObjCMethodExpr;
@@ -556,7 +551,7 @@
bool IsExpression)
: ContextKind(ContextKind), BindingStrength(BindingStrength),
LongestObjCSelectorName(0), ColonIsForRangeExpr(false),
- ColonIsObjCDictLiteral(false), ColonIsObjCMethodExpr(false),
+ ColonIsDictLiteral(false), ColonIsObjCMethodExpr(false),
FirstObjCSelectorName(NULL), FirstStartOfName(NULL),
IsExpression(IsExpression), CanBeExpression(true),
InCtorInitializer(false) {}
@@ -565,7 +560,7 @@
unsigned BindingStrength;
unsigned LongestObjCSelectorName;
bool ColonIsForRangeExpr;
- bool ColonIsObjCDictLiteral;
+ bool ColonIsDictLiteral;
bool ColonIsObjCMethodExpr;
FormatToken *FirstObjCSelectorName;
FormatToken *FirstStartOfName;
@@ -1411,10 +1406,10 @@
if (Right.Type == TT_StartOfName || Right.is(tok::kw_operator))
return true;
if (Right.is(tok::colon) &&
- (Right.Type == TT_ObjCDictLiteral || Right.Type == TT_ObjCMethodExpr))
+ (Right.Type == TT_DictLiteral || Right.Type == TT_ObjCMethodExpr))
return false;
if (Left.is(tok::colon) &&
- (Left.Type == TT_ObjCDictLiteral || Left.Type == TT_ObjCMethodExpr))
+ (Left.Type == TT_DictLiteral || Left.Type == TT_ObjCMethodExpr))
return true;
if (Right.Type == TT_ObjCSelectorName)
return true;
diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp
index d8ff2fc..f6e42ca 100644
--- a/unittests/Format/FormatTest.cpp
+++ b/unittests/Format/FormatTest.cpp
@@ -5372,14 +5372,9 @@
verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);");
verifyFormat("NSNumber *favoriteColor = @(Green);");
verifyFormat("NSString *path = @(getenv(\"PATH\"));");
+}
- verifyFormat("@[");
- verifyFormat("@[]");
- verifyFormat(
- "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];");
- verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];");
- verifyFormat("NSArray *array = @[ [foo description] ];");
-
+TEST_F(FormatTest, ObjCDictLiterals) {
verifyFormat("@{");
verifyFormat("@{}");
verifyFormat("@{ @\"one\" : @1 }");
@@ -5409,25 +5404,56 @@
" NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
"regularFont,\n"
"};");
- verifyFormat(
- "NSArray *arguments = @[\n"
- " kind == kUserTicket ? @\"--user-store\" : @\"--system-store\",\n"
- " @\"--print-tickets\",\n"
- " @\"--productid\",\n"
- " @\"com.google.Chrome\"\n"
- "];");
verifyGoogleFormat(
"@{\n"
" NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
"regularFont,\n"
"};");
- verifyGoogleFormat(
- "NSArray *arguments = @[\n"
- " kind == kUserTicket ? @\"--user-store\" : @\"--system-store\",\n"
- " @\"--print-tickets\",\n"
- " @\"--productid\",\n"
- " @\"com.google.Chrome\"\n"
+
+ // We should try to be robust in case someone forgets the "@".
+ verifyFormat(
+ "NSDictionary *d = {\n"
+ " @\"nam\" : NSUserNam(),\n"
+ " @\"dte\" : [NSDate date],\n"
+ " @\"processInfo\" : [NSProcessInfo processInfo]\n"
+ "};");
+}
+
+TEST_F(FormatTest, ObjCArrayLiterals) {
+ verifyFormat("@[");
+ verifyFormat("@[]");
+ verifyFormat(
+ "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];");
+ verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];");
+ verifyFormat("NSArray *array = @[ [foo description] ];");
+
+ verifyFormat(
+ "NSArray *some_variable = @[\n"
+ " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
+ " @\"aaaaaaaaaaaaaaaaa\",\n"
+ " @\"aaaaaaaaaaaaaaaaa\",\n"
+ " @\"aaaaaaaaaaaaaaaaa\"\n"
"];");
+ verifyFormat("NSArray *some_variable = @[\n"
+ " @\"aaaaaaaaaaaaaaaaa\",\n"
+ " @\"aaaaaaaaaaaaaaaaa\",\n"
+ " @\"aaaaaaaaaaaaaaaaa\",\n"
+ " @\"aaaaaaaaaaaaaaaaa\",\n"
+ "];");
+ verifyGoogleFormat("NSArray *some_variable = @[\n"
+ " @\"aaaaaaaaaaaaaaaaa\",\n"
+ " @\"aaaaaaaaaaaaaaaaa\",\n"
+ " @\"aaaaaaaaaaaaaaaaa\",\n"
+ " @\"aaaaaaaaaaaaaaaaa\"\n"
+ "];");
+
+ // We should try to be robust in case someone forgets the "@".
+ verifyFormat("NSArray *some_variable = [\n"
+ " @\"aaaaaaaaaaaaaaaaa\",\n"
+ " @\"aaaaaaaaaaaaaaaaa\",\n"
+ " @\"aaaaaaaaaaaaaaaaa\",\n"
+ " @\"aaaaaaaaaaaaaaaaa\",\n"
+ "];");
}
TEST_F(FormatTest, ReformatRegionAdjustsIndent) {