From: Daniel Jasper Date: Tue, 27 Oct 2015 13:42:08 +0000 (+0000) Subject: clang-format: Undo unwanted format change done in r251405. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ec740cd3972b42c9064837cb2ab31d6f9c0959a0;p=clang clang-format: Undo unwanted format change done in r251405. Specifically, don't wrap between the {} of an empty constructor if the "}" falls on column 81 and ConstructorInitializerAllOnOneLineOrOnePerLine is set. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@251406 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/ContinuationIndenter.cpp b/lib/Format/ContinuationIndenter.cpp index 030f67ef8d..72157bd641 100644 --- a/lib/Format/ContinuationIndenter.cpp +++ b/lib/Format/ContinuationIndenter.cpp @@ -95,7 +95,7 @@ bool ContinuationIndenter::canBreak(const LineState &State) { assert(&Previous == Current.Previous); if (!Current.CanBreakBefore && !(State.Stack.back().BreakBeforeClosingBrace && - Current.closesBlockTypeList(Style))) + Current.closesBlockOrBlockTypeList(Style))) return false; // 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. @@ -139,7 +139,7 @@ bool ContinuationIndenter::mustBreak(const LineState &State) { if (Current.MustBreakBefore || Current.is(TT_InlineASMColon)) return true; if (State.Stack.back().BreakBeforeClosingBrace && - Current.closesBlockTypeList(Style)) + Current.closesBlockOrBlockTypeList(Style)) return true; if (Previous.is(tok::semi) && State.LineContainsContinuedForLoopSection) return true; @@ -574,7 +574,7 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) { return Current.NestingLevel == 0 ? State.FirstIndent : State.Stack.back().Indent; if (Current.isOneOf(tok::r_brace, tok::r_square) && State.Stack.size() > 1) { - if (Current.closesBlockTypeList(Style)) + if (Current.closesBlockOrBlockTypeList(Style)) return State.Stack[State.Stack.size() - 2].NestedBlockIndent; if (Current.MatchingParen && Current.MatchingParen->BlockKind == BK_BracedInit) @@ -883,15 +883,8 @@ void ContinuationIndenter::moveStatePastScopeOpener(LineState &State, bool BreakBeforeParameter = false; unsigned NestedBlockIndent = std::max(State.Stack.back().StartOfFunctionCall, State.Stack.back().NestedBlockIndent); - // Generally inherit NoLineBreak from the current scope to nested scope. - // However, don't do this for nested blocks, e.g. lambdas as these follow - // different indentation rules. - bool NoLineBreak = State.Stack.back().NoLineBreak || - (Current.is(TT_TemplateOpener) && - State.Stack.back().ContainsUnwrappedBuilder); if (Current.isOneOf(tok::l_brace, TT_ArrayInitializerLSquare)) { - if (Current.opensBlockTypeList(Style)) { - NoLineBreak = false; + if (Current.opensBlockOrBlockTypeList(Style)) { NewIndent = State.Stack.back().NestedBlockIndent + Style.IndentWidth; NewIndent = std::min(State.Column + 2, NewIndent); ++NewIndentLevel; @@ -949,6 +942,15 @@ void ContinuationIndenter::moveStatePastScopeOpener(LineState &State, } } } + // Generally inherit NoLineBreak from the current scope to nested scope. + // However, don't do this for non-empty nested blocks, dict literals and + // array literals as these follow different indentation rules. + bool NoLineBreak = + Current.Children.empty() && + !Current.isOneOf(TT_DictLiteral, TT_ArrayInitializerLSquare) && + (State.Stack.back().NoLineBreak || + (Current.is(TT_TemplateOpener) && + State.Stack.back().ContainsUnwrappedBuilder)); State.Stack.push_back(ParenState(NewIndent, NewIndentLevel, LastSpace, AvoidBinPacking, NoLineBreak)); State.Stack.back().NestedBlockIndent = NestedBlockIndent; diff --git a/lib/Format/FormatToken.h b/lib/Format/FormatToken.h index c78f6a65bd..002ff25bfa 100644 --- a/lib/Format/FormatToken.h +++ b/lib/Format/FormatToken.h @@ -412,16 +412,16 @@ struct FormatToken { /// \brief Returns \c true if this tokens starts a block-type list, i.e. a /// list that should be indented with a block indent. - bool opensBlockTypeList(const FormatStyle &Style) const { + bool opensBlockOrBlockTypeList(const FormatStyle &Style) const { return is(TT_ArrayInitializerLSquare) || (is(tok::l_brace) && (BlockKind == BK_Block || is(TT_DictLiteral) || (!Style.Cpp11BracedListStyle && NestingLevel == 0))); } - /// \brief Same as opensBlockTypeList, but for the closing token. - bool closesBlockTypeList(const FormatStyle &Style) const { - return MatchingParen && MatchingParen->opensBlockTypeList(Style); + /// \brief Same as opensBlockOrBlockTypeList, but for the closing token. + bool closesBlockOrBlockTypeList(const FormatStyle &Style) const { + return MatchingParen && MatchingParen->opensBlockOrBlockTypeList(Style); } private: diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 8f5a95240d..eaa31b1ebf 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -3607,6 +3607,11 @@ TEST_F(FormatTest, ConstructorInitializers) { " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" " aaaaaaaaaaaaaaaaaaaaaa) {}", OnePerLine); + OnePerLine.ColumnLimit = 60; + verifyFormat("Constructor()\n" + " : aaaaaaaaaaaaaaaaaaaa(a),\n" + " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", + OnePerLine); EXPECT_EQ("Constructor()\n" " : // Comment forcing unwanted break.\n"