diff options
| author | Daniel Jasper <djasper@google.com> | 2013-09-13 09:20:45 +0000 |
|---|---|---|
| committer | Daniel Jasper <djasper@google.com> | 2013-09-13 09:20:45 +0000 |
| commit | 015ed028cf6922059f3da337a7fbfeb874a51051 (patch) | |
| tree | 9d6a6b46217bf7b13a366cfe5ad73208fbcc8a68 /clang/lib/Format | |
| parent | d816320809937e4e202e83e9e7b53d64b0521038 (diff) | |
| download | bcm5719-llvm-015ed028cf6922059f3da337a7fbfeb874a51051.tar.gz bcm5719-llvm-015ed028cf6922059f3da337a7fbfeb874a51051.zip | |
clang-format: Fix incorrect enum parsing / layouting.
Before:
enum {
Bar = Foo < int,
int > ::value
};
After:
enum {
Bar = Foo<int, int>::value
};
llvm-svn: 190674
Diffstat (limited to 'clang/lib/Format')
| -rw-r--r-- | clang/lib/Format/ContinuationIndenter.cpp | 18 | ||||
| -rw-r--r-- | clang/lib/Format/Format.cpp | 2 | ||||
| -rw-r--r-- | clang/lib/Format/FormatToken.cpp | 1 | ||||
| -rw-r--r-- | clang/lib/Format/TokenAnnotator.cpp | 6 | ||||
| -rw-r--r-- | clang/lib/Format/UnwrappedLineParser.cpp | 53 | ||||
| -rw-r--r-- | clang/lib/Format/UnwrappedLineParser.h | 2 |
6 files changed, 36 insertions, 46 deletions
diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index f1924c3776d..7066e15d5de 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -106,8 +106,10 @@ bool ContinuationIndenter::mustBreak(const LineState &State) { 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::addTokenToState(LineState &State, bool Newline, 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::moveStateToNextToken(LineState &State, // }); 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::moveStateToNextToken(LineState &State, 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; } diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index 52f8b07475d..034f6e6ee87 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -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); diff --git a/clang/lib/Format/FormatToken.cpp b/clang/lib/Format/FormatToken.cpp index 49e0ce83375..7469216a147 100644 --- a/clang/lib/Format/FormatToken.cpp +++ b/clang/lib/Format/FormatToken.cpp @@ -36,6 +36,7 @@ unsigned CommaSeparatedList::format(LineState &State, // 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; diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index dec631fcfb3..db17135a0c7 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -1057,6 +1057,12 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) { } 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); diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index f70de5f6d88..3fb46f2f56f 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -759,7 +759,8 @@ bool UnwrappedLineParser::tryToParseBracedList() { 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::parseBracedList() { 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::parseBracedList() { 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: - nextToken(); - addUnwrappedLine(); - break; - default: + FormatTok->BlockKind = BK_Block; + bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true); + if (HasError) { + if (FormatTok->is(tok::semi)) nextToken(); - break; - } - } while (!eof()); + addUnwrappedLine(); + } } // We fall through to parsing a structural element afterwards, so that in // enum A {} n, m; diff --git a/clang/lib/Format/UnwrappedLineParser.h b/clang/lib/Format/UnwrappedLineParser.h index c79c35e1b9d..3acdbd60a58 100644 --- a/clang/lib/Format/UnwrappedLineParser.h +++ b/clang/lib/Format/UnwrappedLineParser.h @@ -79,7 +79,7 @@ private: void parsePPUnknown(); void parseStructuralElement(); bool tryToParseBracedList(); - void parseBracedList(); + bool parseBracedList(bool ContinueOnSemicolons = false); void parseReturn(); void parseParens(); void parseIfThenElse(); |

