diff options
author | Daniel Jasper <djasper@google.com> | 2013-10-24 10:31:50 +0000 |
---|---|---|
committer | Daniel Jasper <djasper@google.com> | 2013-10-24 10:31:50 +0000 |
commit | b596fb2be2e436ac24162c284fdbe3fe6f69be3d (patch) | |
tree | 5ba1c922ccdc2393e914a0b21cefcba3b2c93552 /clang/lib/Format | |
parent | 1ec9df3322d05a5be1144acd4d013865f9eb353f (diff) | |
download | bcm5719-llvm-b596fb2be2e436ac24162c284fdbe3fe6f69be3d.tar.gz bcm5719-llvm-b596fb2be2e436ac24162c284fdbe3fe6f69be3d.zip |
clang-format: Cleanup array initializer and dict initializer formatting.
Significant changes:
- Also recognize these literals with missing "@" for robustness.
- Reorganize tests.
llvm-svn: 193325
Diffstat (limited to 'clang/lib/Format')
-rw-r--r-- | clang/lib/Format/ContinuationIndenter.cpp | 5 | ||||
-rw-r--r-- | clang/lib/Format/FormatToken.cpp | 1 | ||||
-rw-r--r-- | clang/lib/Format/FormatToken.h | 4 | ||||
-rw-r--r-- | clang/lib/Format/TokenAnnotator.cpp | 29 |
4 files changed, 18 insertions, 21 deletions
diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index 16b1147fd58..68b440dbe3f 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -98,6 +98,7 @@ bool ContinuationIndenter::canBreak(const LineState &State) { // 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 @@ bool ContinuationIndenter::mustBreak(const LineState &State) { !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 @@ unsigned ContinuationIndenter::moveStateToNextToken(LineState &State, 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/clang/lib/Format/FormatToken.cpp b/clang/lib/Format/FormatToken.cpp index 35764ce8823..4ca4edbe158 100644 --- a/clang/lib/Format/FormatToken.cpp +++ b/clang/lib/Format/FormatToken.cpp @@ -37,6 +37,7 @@ unsigned CommaSeparatedList::format(LineState &State, 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/clang/lib/Format/FormatToken.h b/clang/lib/Format/FormatToken.h index 2001d427a69..2145ee28dcc 100644 --- a/clang/lib/Format/FormatToken.h +++ b/clang/lib/Format/FormatToken.h @@ -35,6 +35,7 @@ enum TokenType { TT_CtorInitializerColon, TT_CtorInitializerComma, TT_DesignatedInitializerPeriod, + TT_DictLiteral, TT_ImplicitStringLiteral, TT_InlineASMColon, TT_InheritanceColon, @@ -43,7 +44,6 @@ enum TokenType { TT_LineComment, TT_ObjCBlockLParen, TT_ObjCDecl, - TT_ObjCDictLiteral, TT_ObjCForIn, TT_ObjCMethodExpr, TT_ObjCMethodSpecifier, @@ -344,7 +344,7 @@ struct FormatToken { 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/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index e3984428bd3..7fa96215b11 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -235,7 +235,8 @@ private: 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 @@ private: 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 @@ private: 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 @@ private: // 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 @@ private: 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 @@ private: unsigned BindingStrength; unsigned LongestObjCSelectorName; bool ColonIsForRangeExpr; - bool ColonIsObjCDictLiteral; + bool ColonIsDictLiteral; bool ColonIsObjCMethodExpr; FormatToken *FirstObjCSelectorName; FormatToken *FirstStartOfName; @@ -1411,10 +1406,10 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line, 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; |