From 11a0ac66e10658f9eec87db0e846f6f5eaafc9cf Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Fri, 12 Dec 2014 09:40:58 +0000 Subject: clang-format: Revamp nested block formatting. This fixed llvm.org/PR21804 and hopefully a few other strange cases. Before: if (blah_blah(whatever, whatever, [] { doo_dah(); doo_dah(); })) { } } After: if (blah_blah(whatever, whatever, [] { doo_dah(); doo_dah(); })) { } } llvm-svn: 224112 --- clang/lib/Format/UnwrappedLineFormatter.cpp | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'clang/lib/Format/UnwrappedLineFormatter.cpp') diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp index fe9cf593999..56feaf6e54e 100644 --- a/clang/lib/Format/UnwrappedLineFormatter.cpp +++ b/clang/lib/Format/UnwrappedLineFormatter.cpp @@ -593,6 +593,15 @@ unsigned UnwrappedLineFormatter::analyzeSolutionSpace(LineState &InitialState, return Penalty; } +static void printLineState(const LineState &State) { + llvm::dbgs() << "State: "; + for (const ParenState &P : State.Stack) { + llvm::dbgs() << P.Indent << "|" << P.LastSpace << "|" << P.NestedBlockIndent + << " "; + } + llvm::dbgs() << State.NextToken->TokenText << "\n"; +} + void UnwrappedLineFormatter::reconstructPath(LineState &State, StateNode *Current) { std::deque Path; @@ -608,6 +617,7 @@ void UnwrappedLineFormatter::reconstructPath(LineState &State, Penalty += Indenter->addTokenToState(State, (*I)->NewLine, false); DEBUG({ + printLineState((*I)->Previous->State); if ((*I)->NewLine) { llvm::dbgs() << "Penalty for placing " << (*I)->Previous->State.NextToken->Tok.getName() << ": " @@ -648,13 +658,8 @@ bool UnwrappedLineFormatter::formatChildren(LineState &State, bool NewLine, return true; if (NewLine) { - int AdditionalIndent = - State.FirstIndent - State.Line->Level * Style.IndentWidth; - if (State.Stack.size() < 2 || - !State.Stack[State.Stack.size() - 2].NestedBlockInlined) { - AdditionalIndent = State.Stack.back().Indent - - Previous.Children[0]->Level * Style.IndentWidth; - } + int AdditionalIndent = State.Stack.back().Indent - + Previous.Children[0]->Level * Style.IndentWidth; Penalty += format(Previous.Children, DryRun, AdditionalIndent, /*FixBadIndentation=*/true); -- cgit v1.2.3