Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 1 | //===--- ContinuationIndenter.cpp - Format C++ code -----------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | /// |
| 10 | /// \file |
| 11 | /// \brief This file implements the continuation indenter. |
| 12 | /// |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #define DEBUG_TYPE "format-formatter" |
| 16 | |
| 17 | #include "BreakableToken.h" |
| 18 | #include "ContinuationIndenter.h" |
| 19 | #include "WhitespaceManager.h" |
| 20 | #include "clang/Basic/OperatorPrecedence.h" |
| 21 | #include "clang/Basic/SourceManager.h" |
| 22 | #include "clang/Format/Format.h" |
| 23 | #include "llvm/Support/Debug.h" |
| 24 | #include <string> |
| 25 | |
| 26 | namespace clang { |
| 27 | namespace format { |
| 28 | |
| 29 | // Returns the length of everything up to the first possible line break after |
| 30 | // the ), ], } or > matching \c Tok. |
| 31 | static unsigned getLengthToMatchingParen(const FormatToken &Tok) { |
| 32 | if (Tok.MatchingParen == NULL) |
| 33 | return 0; |
| 34 | FormatToken *End = Tok.MatchingParen; |
| 35 | while (End->Next && !End->Next->CanBreakBefore) { |
| 36 | End = End->Next; |
| 37 | } |
| 38 | return End->TotalLength - Tok.TotalLength + 1; |
| 39 | } |
| 40 | |
Daniel Jasper | d3fef0f | 2013-08-27 14:24:43 +0000 | [diff] [blame] | 41 | // Returns \c true if \c Tok is the "." or "->" of a call and starts the next |
| 42 | // segment of a builder type call. |
| 43 | static bool startsSegmentOfBuilderTypeCall(const FormatToken &Tok) { |
| 44 | return Tok.isMemberAccess() && Tok.Previous && Tok.Previous->closesScope(); |
| 45 | } |
| 46 | |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 47 | ContinuationIndenter::ContinuationIndenter(const FormatStyle &Style, |
| 48 | SourceManager &SourceMgr, |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 49 | WhitespaceManager &Whitespaces, |
| 50 | encoding::Encoding Encoding, |
| 51 | bool BinPackInconclusiveFunctions) |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 52 | : Style(Style), SourceMgr(SourceMgr), Whitespaces(Whitespaces), |
| 53 | Encoding(Encoding), |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 54 | BinPackInconclusiveFunctions(BinPackInconclusiveFunctions) {} |
| 55 | |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 56 | LineState ContinuationIndenter::getInitialState(unsigned FirstIndent, |
Daniel Jasper | b77d741 | 2013-09-06 07:54:20 +0000 | [diff] [blame] | 57 | const AnnotatedLine *Line, |
| 58 | bool DryRun) { |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 59 | LineState State; |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 60 | State.FirstIndent = FirstIndent; |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 61 | State.Column = FirstIndent; |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 62 | State.Line = Line; |
| 63 | State.NextToken = Line->First; |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 64 | State.Stack.push_back(ParenState(FirstIndent, FirstIndent, |
| 65 | /*AvoidBinPacking=*/false, |
| 66 | /*NoLineBreak=*/false)); |
| 67 | State.LineContainsContinuedForLoopSection = false; |
| 68 | State.ParenLevel = 0; |
| 69 | State.StartOfStringLiteral = 0; |
| 70 | State.StartOfLineLevel = State.ParenLevel; |
| 71 | State.LowestLevelOnLine = State.ParenLevel; |
| 72 | State.IgnoreStackForComparison = false; |
| 73 | |
| 74 | // The first token has already been indented and thus consumed. |
Daniel Jasper | b77d741 | 2013-09-06 07:54:20 +0000 | [diff] [blame] | 75 | moveStateToNextToken(State, DryRun, /*Newline=*/false); |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 76 | return State; |
| 77 | } |
| 78 | |
| 79 | bool ContinuationIndenter::canBreak(const LineState &State) { |
| 80 | const FormatToken &Current = *State.NextToken; |
| 81 | const FormatToken &Previous = *Current.Previous; |
| 82 | assert(&Previous == Current.Previous); |
| 83 | if (!Current.CanBreakBefore && |
| 84 | !(Current.is(tok::r_brace) && State.Stack.back().BreakBeforeClosingBrace)) |
| 85 | return false; |
| 86 | // The opening "{" of a braced list has to be on the same line as the first |
| 87 | // element if it is nested in another braced init list or function call. |
| 88 | if (!Current.MustBreakBefore && Previous.is(tok::l_brace) && |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 89 | Previous.BlockKind == BK_BracedInit && Previous.Previous && |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 90 | Previous.Previous->isOneOf(tok::l_brace, tok::l_paren, tok::comma)) |
| 91 | return false; |
| 92 | // This prevents breaks like: |
| 93 | // ... |
| 94 | // SomeParameter, OtherParameter).DoSomething( |
| 95 | // ... |
| 96 | // As they hide "DoSomething" and are generally bad for readability. |
| 97 | if (Previous.opensScope() && State.LowestLevelOnLine < State.StartOfLineLevel) |
| 98 | return false; |
Daniel Jasper | d3fef0f | 2013-08-27 14:24:43 +0000 | [diff] [blame] | 99 | if (Current.isMemberAccess() && State.Stack.back().ContainsUnwrappedBuilder) |
| 100 | return false; |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 101 | return !State.Stack.back().NoLineBreak; |
| 102 | } |
| 103 | |
| 104 | bool ContinuationIndenter::mustBreak(const LineState &State) { |
| 105 | const FormatToken &Current = *State.NextToken; |
| 106 | const FormatToken &Previous = *Current.Previous; |
| 107 | if (Current.MustBreakBefore || Current.Type == TT_InlineASMColon) |
| 108 | return true; |
Daniel Jasper | 5798120 | 2013-09-13 09:20:45 +0000 | [diff] [blame] | 109 | if ((!Style.Cpp11BracedListStyle || |
| 110 | (Current.MatchingParen && |
| 111 | Current.MatchingParen->BlockKind == BK_Block)) && |
| 112 | Current.is(tok::r_brace) && State.Stack.back().BreakBeforeClosingBrace) |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 113 | return true; |
| 114 | if (Previous.is(tok::semi) && State.LineContainsContinuedForLoopSection) |
| 115 | return true; |
| 116 | if (Style.BreakConstructorInitializersBeforeComma) { |
| 117 | if (Previous.Type == TT_CtorInitializerComma) |
| 118 | return false; |
| 119 | if (Current.Type == TT_CtorInitializerComma) |
| 120 | return true; |
| 121 | } |
| 122 | if ((Previous.isOneOf(tok::comma, tok::semi) || Current.is(tok::question) || |
| 123 | (Current.Type == TT_ConditionalExpr && |
| 124 | !(Current.is(tok::colon) && Previous.is(tok::question)))) && |
| 125 | State.Stack.back().BreakBeforeParameter && !Current.isTrailingComment() && |
| 126 | !Current.isOneOf(tok::r_paren, tok::r_brace)) |
| 127 | return true; |
| 128 | if (Style.AlwaysBreakBeforeMultilineStrings && |
Daniel Jasper | 4df1ff9 | 2013-08-23 11:57:34 +0000 | [diff] [blame] | 129 | State.Column > State.Stack.back().Indent && // Breaking saves columns. |
| 130 | Previous.isNot(tok::lessless) && Previous.Type != TT_InlineASMColon && |
| 131 | NextIsMultilineString(State)) |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 132 | return true; |
| 133 | |
| 134 | if (!Style.BreakBeforeBinaryOperators) { |
| 135 | // If we need to break somewhere inside the LHS of a binary expression, we |
| 136 | // should also break after the operator. Otherwise, the formatting would |
| 137 | // hide the operator precedence, e.g. in: |
| 138 | // if (aaaaaaaaaaaaaa == |
| 139 | // bbbbbbbbbbbbbb && c) {.. |
| 140 | // For comparisons, we only apply this rule, if the LHS is a binary |
| 141 | // expression itself as otherwise, the line breaks seem superfluous. |
| 142 | // We need special cases for ">>" which we have split into two ">" while |
| 143 | // lexing in order to make template parsing easier. |
| 144 | // |
| 145 | // FIXME: We'll need something similar for styles that break before binary |
| 146 | // operators. |
| 147 | bool IsComparison = (Previous.getPrecedence() == prec::Relational || |
| 148 | Previous.getPrecedence() == prec::Equality) && |
| 149 | Previous.Previous && |
| 150 | Previous.Previous->Type != TT_BinaryOperator; // For >>. |
| 151 | bool LHSIsBinaryExpr = |
Daniel Jasper | db4813a | 2013-09-06 08:08:14 +0000 | [diff] [blame] | 152 | Previous.Previous && Previous.Previous->EndsBinaryExpression; |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 153 | if (Previous.Type == TT_BinaryOperator && |
| 154 | (!IsComparison || LHSIsBinaryExpr) && |
| 155 | Current.Type != TT_BinaryOperator && // For >>. |
| 156 | !Current.isTrailingComment() && |
| 157 | !Previous.isOneOf(tok::lessless, tok::question) && |
| 158 | Previous.getPrecedence() != prec::Assignment && |
| 159 | State.Stack.back().BreakBeforeParameter) |
| 160 | return true; |
| 161 | } |
| 162 | |
| 163 | // Same as above, but for the first "<<" operator. |
| 164 | if (Current.is(tok::lessless) && State.Stack.back().BreakBeforeParameter && |
| 165 | State.Stack.back().FirstLessLess == 0) |
| 166 | return true; |
| 167 | |
| 168 | // FIXME: Comparing LongestObjCSelectorName to 0 is a hacky way of finding |
| 169 | // out whether it is the first parameter. Clean this up. |
| 170 | if (Current.Type == TT_ObjCSelectorName && |
| 171 | Current.LongestObjCSelectorName == 0 && |
| 172 | State.Stack.back().BreakBeforeParameter) |
| 173 | return true; |
| 174 | if ((Current.Type == TT_CtorInitializerColon || |
| 175 | (Previous.ClosesTemplateDeclaration && State.ParenLevel == 0))) |
| 176 | return true; |
| 177 | |
| 178 | if ((Current.Type == TT_StartOfName || Current.is(tok::kw_operator)) && |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 179 | State.Line->MightBeFunctionDecl && |
| 180 | State.Stack.back().BreakBeforeParameter && State.ParenLevel == 0) |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 181 | return true; |
Daniel Jasper | d3fef0f | 2013-08-27 14:24:43 +0000 | [diff] [blame] | 182 | if (startsSegmentOfBuilderTypeCall(Current) && |
Daniel Jasper | eb33183 | 2013-08-30 07:12:40 +0000 | [diff] [blame] | 183 | (State.Stack.back().CallContinuation != 0 || |
| 184 | (State.Stack.back().BreakBeforeParameter && |
| 185 | State.Stack.back().ContainsUnwrappedBuilder))) |
Daniel Jasper | d3fef0f | 2013-08-27 14:24:43 +0000 | [diff] [blame] | 186 | return true; |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 187 | return false; |
| 188 | } |
| 189 | |
| 190 | unsigned ContinuationIndenter::addTokenToState(LineState &State, bool Newline, |
Daniel Jasper | d4a03db | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 191 | bool DryRun, |
| 192 | unsigned ExtraSpaces) { |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 193 | const FormatToken &Current = *State.NextToken; |
| 194 | const FormatToken &Previous = *State.NextToken->Previous; |
Daniel Jasper | 48c099f | 2013-09-27 07:49:08 +0000 | [diff] [blame] | 195 | const FormatToken *PreviousNonComment = |
| 196 | State.NextToken->getPreviousNonComment(); |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 197 | |
| 198 | // Extra penalty that needs to be added because of the way certain line |
| 199 | // breaks are chosen. |
Daniel Jasper | d4a03db | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 200 | unsigned Penalty = 0; |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 201 | |
| 202 | if (State.Stack.size() == 0 || Current.Type == TT_ImplicitStringLiteral) { |
| 203 | // FIXME: Is this correct? |
| 204 | int WhitespaceLength = SourceMgr.getSpellingColumnNumber( |
| 205 | State.NextToken->WhitespaceRange.getEnd()) - |
| 206 | SourceMgr.getSpellingColumnNumber( |
| 207 | State.NextToken->WhitespaceRange.getBegin()); |
Alexander Kornienko | 83a7dcd | 2013-09-10 09:38:25 +0000 | [diff] [blame] | 208 | State.Column += WhitespaceLength + State.NextToken->ColumnWidth; |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 209 | State.NextToken = State.NextToken->Next; |
| 210 | return 0; |
| 211 | } |
| 212 | |
| 213 | // If we are continuing an expression, we want to indent an extra 4 spaces. |
| 214 | unsigned ContinuationIndent = |
| 215 | std::max(State.Stack.back().LastSpace, State.Stack.back().Indent) + 4; |
| 216 | if (Newline) { |
Daniel Jasper | d4a03db | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 217 | // The first line break on any ParenLevel causes an extra penalty in order |
| 218 | // prefer similar line breaks. |
| 219 | if (!State.Stack.back().ContainsLineBreak) |
| 220 | Penalty += 15; |
| 221 | State.Stack.back().ContainsLineBreak = true; |
| 222 | |
| 223 | Penalty += State.NextToken->SplitPenalty; |
| 224 | |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 225 | // Breaking before the first "<<" is generally not desirable if the LHS is |
| 226 | // short. |
| 227 | if (Current.is(tok::lessless) && State.Stack.back().FirstLessLess == 0 && |
| 228 | State.Column <= Style.ColumnLimit / 2) |
Daniel Jasper | d4a03db | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 229 | Penalty += Style.PenaltyBreakFirstLessLess; |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 230 | |
Daniel Jasper | 5798120 | 2013-09-13 09:20:45 +0000 | [diff] [blame] | 231 | if (Current.is(tok::l_brace) && Current.BlockKind == BK_Block) { |
| 232 | State.Column = State.FirstIndent; |
| 233 | } else if (Current.is(tok::r_brace)) { |
Daniel Jasper | 00e0f43 | 2013-09-06 21:46:41 +0000 | [diff] [blame] | 234 | if (Current.MatchingParen && |
| 235 | (Current.MatchingParen->BlockKind == BK_BracedInit || |
| 236 | !Current.MatchingParen->Children.empty())) |
| 237 | State.Column = State.Stack[State.Stack.size() - 2].LastSpace; |
| 238 | else |
| 239 | State.Column = State.FirstIndent; |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 240 | } else if (Current.is(tok::string_literal) && |
| 241 | State.StartOfStringLiteral != 0) { |
| 242 | State.Column = State.StartOfStringLiteral; |
| 243 | State.Stack.back().BreakBeforeParameter = true; |
| 244 | } else if (Current.is(tok::lessless) && |
| 245 | State.Stack.back().FirstLessLess != 0) { |
| 246 | State.Column = State.Stack.back().FirstLessLess; |
Daniel Jasper | d3fef0f | 2013-08-27 14:24:43 +0000 | [diff] [blame] | 247 | } else if (Current.isMemberAccess()) { |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 248 | if (State.Stack.back().CallContinuation == 0) { |
| 249 | State.Column = ContinuationIndent; |
| 250 | State.Stack.back().CallContinuation = State.Column; |
| 251 | } else { |
| 252 | State.Column = State.Stack.back().CallContinuation; |
| 253 | } |
| 254 | } else if (Current.Type == TT_ConditionalExpr) { |
| 255 | State.Column = State.Stack.back().QuestionColumn; |
| 256 | } else if (Previous.is(tok::comma) && State.Stack.back().VariablePos != 0) { |
| 257 | State.Column = State.Stack.back().VariablePos; |
Daniel Jasper | 48c099f | 2013-09-27 07:49:08 +0000 | [diff] [blame] | 258 | } else if ((PreviousNonComment && |
| 259 | PreviousNonComment->ClosesTemplateDeclaration) || |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 260 | ((Current.Type == TT_StartOfName || |
| 261 | Current.is(tok::kw_operator)) && |
| 262 | State.ParenLevel == 0 && |
| 263 | (!Style.IndentFunctionDeclarationAfterType || |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 264 | State.Line->StartsDefinition))) { |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 265 | State.Column = State.Stack.back().Indent; |
| 266 | } else if (Current.Type == TT_ObjCSelectorName) { |
Alexander Kornienko | 83a7dcd | 2013-09-10 09:38:25 +0000 | [diff] [blame] | 267 | if (State.Stack.back().ColonPos > Current.ColumnWidth) { |
| 268 | State.Column = State.Stack.back().ColonPos - Current.ColumnWidth; |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 269 | } else { |
| 270 | State.Column = State.Stack.back().Indent; |
Alexander Kornienko | 83a7dcd | 2013-09-10 09:38:25 +0000 | [diff] [blame] | 271 | State.Stack.back().ColonPos = State.Column + Current.ColumnWidth; |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 272 | } |
Daniel Jasper | ac2c974 | 2013-09-05 10:04:31 +0000 | [diff] [blame] | 273 | } else if (Current.is(tok::l_square) && Current.Type != TT_ObjCMethodExpr && |
| 274 | Current.Type != TT_LambdaLSquare) { |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 275 | if (State.Stack.back().StartOfArraySubscripts != 0) |
| 276 | State.Column = State.Stack.back().StartOfArraySubscripts; |
| 277 | else |
| 278 | State.Column = ContinuationIndent; |
| 279 | } else if (Current.Type == TT_StartOfName || |
| 280 | Previous.isOneOf(tok::coloncolon, tok::equal) || |
| 281 | Previous.Type == TT_ObjCMethodExpr) { |
| 282 | State.Column = ContinuationIndent; |
| 283 | } else if (Current.Type == TT_CtorInitializerColon) { |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 284 | State.Column = |
| 285 | State.FirstIndent + Style.ConstructorInitializerIndentWidth; |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 286 | } else if (Current.Type == TT_CtorInitializerComma) { |
| 287 | State.Column = State.Stack.back().Indent; |
| 288 | } else { |
| 289 | State.Column = State.Stack.back().Indent; |
| 290 | // Ensure that we fall back to indenting 4 spaces instead of just |
| 291 | // flushing continuations left. |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 292 | if (State.Column == State.FirstIndent) |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 293 | State.Column += 4; |
| 294 | } |
| 295 | |
| 296 | if (Current.is(tok::question)) |
| 297 | State.Stack.back().BreakBeforeParameter = true; |
| 298 | if ((Previous.isOneOf(tok::comma, tok::semi) && |
| 299 | !State.Stack.back().AvoidBinPacking) || |
| 300 | Previous.Type == TT_BinaryOperator) |
| 301 | State.Stack.back().BreakBeforeParameter = false; |
| 302 | if (Previous.Type == TT_TemplateCloser && State.ParenLevel == 0) |
| 303 | State.Stack.back().BreakBeforeParameter = false; |
| 304 | |
| 305 | if (!DryRun) { |
| 306 | unsigned NewLines = 1; |
| 307 | if (Current.is(tok::comment)) |
| 308 | NewLines = std::max(NewLines, std::min(Current.NewlinesBefore, |
| 309 | Style.MaxEmptyLinesToKeep + 1)); |
| 310 | Whitespaces.replaceWhitespace(Current, NewLines, State.Column, |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 311 | State.Column, State.Line->InPPDirective); |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | if (!Current.isTrailingComment()) |
| 315 | State.Stack.back().LastSpace = State.Column; |
Daniel Jasper | d3fef0f | 2013-08-27 14:24:43 +0000 | [diff] [blame] | 316 | if (Current.isMemberAccess()) |
Alexander Kornienko | 83a7dcd | 2013-09-10 09:38:25 +0000 | [diff] [blame] | 317 | State.Stack.back().LastSpace += Current.ColumnWidth; |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 318 | State.StartOfLineLevel = State.ParenLevel; |
| 319 | State.LowestLevelOnLine = State.ParenLevel; |
| 320 | |
| 321 | // Any break on this level means that the parent level has been broken |
| 322 | // and we need to avoid bin packing there. |
| 323 | for (unsigned i = 0, e = State.Stack.size() - 1; i != e; ++i) { |
| 324 | State.Stack[i].BreakBeforeParameter = true; |
| 325 | } |
| 326 | const FormatToken *TokenBefore = Current.getPreviousNonComment(); |
| 327 | if (TokenBefore && !TokenBefore->isOneOf(tok::comma, tok::semi) && |
| 328 | TokenBefore->Type != TT_TemplateCloser && |
| 329 | TokenBefore->Type != TT_BinaryOperator && !TokenBefore->opensScope()) |
| 330 | State.Stack.back().BreakBeforeParameter = true; |
| 331 | |
| 332 | // If we break after {, we should also break before the corresponding }. |
| 333 | if (Previous.is(tok::l_brace)) |
| 334 | State.Stack.back().BreakBeforeClosingBrace = true; |
| 335 | |
| 336 | if (State.Stack.back().AvoidBinPacking) { |
| 337 | // If we are breaking after '(', '{', '<', this is not bin packing |
| 338 | // unless AllowAllParametersOfDeclarationOnNextLine is false. |
| 339 | if (!(Previous.isOneOf(tok::l_paren, tok::l_brace) || |
| 340 | Previous.Type == TT_BinaryOperator) || |
| 341 | (!Style.AllowAllParametersOfDeclarationOnNextLine && |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 342 | State.Line->MustBeDeclaration)) |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 343 | State.Stack.back().BreakBeforeParameter = true; |
| 344 | } |
| 345 | |
| 346 | } else { |
| 347 | if (Current.is(tok::equal) && |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 348 | (State.Line->First->is(tok::kw_for) || State.ParenLevel == 0) && |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 349 | State.Stack.back().VariablePos == 0) { |
| 350 | State.Stack.back().VariablePos = State.Column; |
| 351 | // Move over * and & if they are bound to the variable name. |
| 352 | const FormatToken *Tok = &Previous; |
Alexander Kornienko | 83a7dcd | 2013-09-10 09:38:25 +0000 | [diff] [blame] | 353 | while (Tok && State.Stack.back().VariablePos >= Tok->ColumnWidth) { |
| 354 | State.Stack.back().VariablePos -= Tok->ColumnWidth; |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 355 | if (Tok->SpacesRequiredBefore != 0) |
| 356 | break; |
| 357 | Tok = Tok->Previous; |
| 358 | } |
| 359 | if (Previous.PartOfMultiVariableDeclStmt) |
| 360 | State.Stack.back().LastSpace = State.Stack.back().VariablePos; |
| 361 | } |
| 362 | |
Daniel Jasper | d4a03db | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 363 | unsigned Spaces = State.NextToken->SpacesRequiredBefore + ExtraSpaces; |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 364 | |
| 365 | if (!DryRun) |
| 366 | Whitespaces.replaceWhitespace(Current, 0, Spaces, State.Column + Spaces); |
| 367 | |
| 368 | if (Current.Type == TT_ObjCSelectorName && |
| 369 | State.Stack.back().ColonPos == 0) { |
| 370 | if (State.Stack.back().Indent + Current.LongestObjCSelectorName > |
Alexander Kornienko | 83a7dcd | 2013-09-10 09:38:25 +0000 | [diff] [blame] | 371 | State.Column + Spaces + Current.ColumnWidth) |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 372 | State.Stack.back().ColonPos = |
| 373 | State.Stack.back().Indent + Current.LongestObjCSelectorName; |
| 374 | else |
| 375 | State.Stack.back().ColonPos = |
Alexander Kornienko | 83a7dcd | 2013-09-10 09:38:25 +0000 | [diff] [blame] | 376 | State.Column + Spaces + Current.ColumnWidth; |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | if (Previous.opensScope() && Previous.Type != TT_ObjCMethodExpr && |
| 380 | Current.Type != TT_LineComment) |
| 381 | State.Stack.back().Indent = State.Column + Spaces; |
| 382 | if (Previous.is(tok::comma) && !Current.isTrailingComment() && |
| 383 | State.Stack.back().AvoidBinPacking) |
| 384 | State.Stack.back().NoLineBreak = true; |
Daniel Jasper | d3fef0f | 2013-08-27 14:24:43 +0000 | [diff] [blame] | 385 | if (startsSegmentOfBuilderTypeCall(Current)) |
| 386 | State.Stack.back().ContainsUnwrappedBuilder = true; |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 387 | |
| 388 | State.Column += Spaces; |
| 389 | if (Current.is(tok::l_paren) && Previous.isOneOf(tok::kw_if, tok::kw_for)) |
| 390 | // Treat the condition inside an if as if it was a second function |
| 391 | // parameter, i.e. let nested calls have an indent of 4. |
| 392 | State.Stack.back().LastSpace = State.Column + 1; // 1 is length of "(". |
| 393 | else if (Previous.is(tok::comma)) |
| 394 | State.Stack.back().LastSpace = State.Column; |
| 395 | else if ((Previous.Type == TT_BinaryOperator || |
| 396 | Previous.Type == TT_ConditionalExpr || |
Daniel Jasper | 34f3d05 | 2013-08-21 08:39:01 +0000 | [diff] [blame] | 397 | Previous.Type == TT_UnaryOperator || |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 398 | Previous.Type == TT_CtorInitializerColon) && |
Daniel Jasper | d489f8c | 2013-08-27 11:09:05 +0000 | [diff] [blame] | 399 | (Previous.getPrecedence() != prec::Assignment || |
Daniel Jasper | db4813a | 2013-09-06 08:08:14 +0000 | [diff] [blame] | 400 | Current.StartsBinaryExpression)) |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 401 | // Always indent relative to the RHS of the expression unless this is a |
Daniel Jasper | 34f3d05 | 2013-08-21 08:39:01 +0000 | [diff] [blame] | 402 | // simple assignment without binary expression on the RHS. Also indent |
| 403 | // relative to unary operators and the colons of constructor initializers. |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 404 | State.Stack.back().LastSpace = State.Column; |
| 405 | else if (Previous.Type == TT_InheritanceColon) |
| 406 | State.Stack.back().Indent = State.Column; |
| 407 | else if (Previous.opensScope()) { |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 408 | // If a function has a trailing call, indent all parameters from the |
| 409 | // opening parenthesis. This avoids confusing indents like: |
| 410 | // OuterFunction(InnerFunctionCall( // break |
| 411 | // ParameterToInnerFunction)) // break |
| 412 | // .SecondInnerFunctionCall(); |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 413 | bool HasTrailingCall = false; |
| 414 | if (Previous.MatchingParen) { |
| 415 | const FormatToken *Next = Previous.MatchingParen->getNextNonComment(); |
Daniel Jasper | d3fef0f | 2013-08-27 14:24:43 +0000 | [diff] [blame] | 416 | HasTrailingCall = Next && Next->isMemberAccess(); |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 417 | } |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 418 | if (HasTrailingCall && |
| 419 | State.Stack[State.Stack.size() - 2].CallContinuation == 0) |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 420 | State.Stack.back().LastSpace = State.Column; |
| 421 | } |
| 422 | } |
| 423 | |
Daniel Jasper | d4a03db | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 424 | return moveStateToNextToken(State, DryRun, Newline) + Penalty; |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | unsigned ContinuationIndenter::moveStateToNextToken(LineState &State, |
| 428 | bool DryRun, bool Newline) { |
| 429 | const FormatToken &Current = *State.NextToken; |
| 430 | assert(State.Stack.size()); |
| 431 | |
| 432 | if (Current.Type == TT_InheritanceColon) |
| 433 | State.Stack.back().AvoidBinPacking = true; |
| 434 | if (Current.is(tok::lessless) && State.Stack.back().FirstLessLess == 0) |
| 435 | State.Stack.back().FirstLessLess = State.Column; |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 436 | if (Current.is(tok::l_square) && Current.Type != TT_LambdaLSquare && |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 437 | State.Stack.back().StartOfArraySubscripts == 0) |
| 438 | State.Stack.back().StartOfArraySubscripts = State.Column; |
| 439 | if (Current.is(tok::question)) |
| 440 | State.Stack.back().QuestionColumn = State.Column; |
| 441 | if (!Current.opensScope() && !Current.closesScope()) |
| 442 | State.LowestLevelOnLine = |
| 443 | std::min(State.LowestLevelOnLine, State.ParenLevel); |
Daniel Jasper | d3fef0f | 2013-08-27 14:24:43 +0000 | [diff] [blame] | 444 | if (Current.isMemberAccess()) |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 445 | State.Stack.back().StartOfFunctionCall = |
Alexander Kornienko | 83a7dcd | 2013-09-10 09:38:25 +0000 | [diff] [blame] | 446 | Current.LastInChainOfCalls ? 0 : State.Column + Current.ColumnWidth; |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 447 | if (Current.Type == TT_CtorInitializerColon) { |
| 448 | // Indent 2 from the column, so: |
| 449 | // SomeClass::SomeClass() |
| 450 | // : First(...), ... |
| 451 | // Next(...) |
| 452 | // ^ line up here. |
| 453 | State.Stack.back().Indent = |
| 454 | State.Column + (Style.BreakConstructorInitializersBeforeComma ? 0 : 2); |
| 455 | if (Style.ConstructorInitializerAllOnOneLineOrOnePerLine) |
| 456 | State.Stack.back().AvoidBinPacking = true; |
| 457 | State.Stack.back().BreakBeforeParameter = false; |
| 458 | } |
| 459 | |
| 460 | // If return returns a binary expression, align after it. |
Daniel Jasper | db4813a | 2013-09-06 08:08:14 +0000 | [diff] [blame] | 461 | if (Current.is(tok::kw_return) && Current.StartsBinaryExpression) |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 462 | State.Stack.back().LastSpace = State.Column + 7; |
| 463 | |
| 464 | // In ObjC method declaration we align on the ":" of parameters, but we need |
| 465 | // to ensure that we indent parameters on subsequent lines by at least 4. |
| 466 | if (Current.Type == TT_ObjCMethodSpecifier) |
| 467 | State.Stack.back().Indent += 4; |
| 468 | |
| 469 | // Insert scopes created by fake parenthesis. |
| 470 | const FormatToken *Previous = Current.getPreviousNonComment(); |
| 471 | // Don't add extra indentation for the first fake parenthesis after |
| 472 | // 'return', assignements or opening <({[. The indentation for these cases |
| 473 | // is special cased. |
| 474 | bool SkipFirstExtraIndent = |
| 475 | Current.is(tok::kw_return) || |
| 476 | (Previous && (Previous->opensScope() || |
| 477 | Previous->getPrecedence() == prec::Assignment)); |
| 478 | for (SmallVectorImpl<prec::Level>::const_reverse_iterator |
| 479 | I = Current.FakeLParens.rbegin(), |
| 480 | E = Current.FakeLParens.rend(); |
| 481 | I != E; ++I) { |
| 482 | ParenState NewParenState = State.Stack.back(); |
| 483 | NewParenState.ContainsLineBreak = false; |
| 484 | NewParenState.Indent = |
| 485 | std::max(std::max(State.Column, NewParenState.Indent), |
| 486 | State.Stack.back().LastSpace); |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 487 | // Do not indent relative to the fake parentheses inserted for "." or "->". |
| 488 | // This is a special case to make the following to statements consistent: |
| 489 | // OuterFunction(InnerFunctionCall( // break |
| 490 | // ParameterToInnerFunction)); |
| 491 | // OuterFunction(SomeObject.InnerFunctionCall( // break |
| 492 | // ParameterToInnerFunction)); |
| 493 | if (*I > prec::Unknown) |
| 494 | NewParenState.LastSpace = std::max(NewParenState.LastSpace, State.Column); |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 495 | |
| 496 | // Always indent conditional expressions. Never indent expression where |
| 497 | // the 'operator' is ',', ';' or an assignment (i.e. *I <= |
| 498 | // prec::Assignment) as those have different indentation rules. Indent |
| 499 | // other expression, unless the indentation needs to be skipped. |
| 500 | if (*I == prec::Conditional || |
| 501 | (!SkipFirstExtraIndent && *I > prec::Assignment && |
| 502 | !Style.BreakBeforeBinaryOperators)) |
| 503 | NewParenState.Indent += 4; |
| 504 | if (Previous && !Previous->opensScope()) |
| 505 | NewParenState.BreakBeforeParameter = false; |
| 506 | State.Stack.push_back(NewParenState); |
| 507 | SkipFirstExtraIndent = false; |
| 508 | } |
| 509 | |
| 510 | // If we encounter an opening (, [, { or <, we add a level to our stacks to |
| 511 | // prepare for the following tokens. |
| 512 | if (Current.opensScope()) { |
| 513 | unsigned NewIndent; |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 514 | bool AvoidBinPacking; |
| 515 | if (Current.is(tok::l_brace)) { |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 516 | if (Current.MatchingParen && Current.BlockKind == BK_Block) { |
Daniel Jasper | 1a925bc | 2013-09-05 10:48:50 +0000 | [diff] [blame] | 517 | // If this is an l_brace starting a nested block, we pretend (wrt. to |
| 518 | // indentation) that we already consumed the corresponding r_brace. |
| 519 | // Thus, we remove all ParenStates caused bake fake parentheses that end |
| 520 | // at the r_brace. The net effect of this is that we don't indent |
| 521 | // relative to the l_brace, if the nested block is the last parameter of |
| 522 | // a function. For example, this formats: |
| 523 | // |
| 524 | // SomeFunction(a, [] { |
| 525 | // f(); // break |
| 526 | // }); |
| 527 | // |
| 528 | // instead of: |
| 529 | // SomeFunction(a, [] { |
| 530 | // f(); // break |
| 531 | // }); |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 532 | for (unsigned i = 0; i != Current.MatchingParen->FakeRParens; ++i) |
| 533 | State.Stack.pop_back(); |
Daniel Jasper | 5798120 | 2013-09-13 09:20:45 +0000 | [diff] [blame] | 534 | NewIndent = State.Stack.back().LastSpace + Style.IndentWidth; |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 535 | } else { |
| 536 | NewIndent = State.Stack.back().LastSpace + |
| 537 | (Style.Cpp11BracedListStyle ? 4 : Style.IndentWidth); |
| 538 | } |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 539 | const FormatToken *NextNoComment = Current.getNextNonComment(); |
Daniel Jasper | 5798120 | 2013-09-13 09:20:45 +0000 | [diff] [blame] | 540 | AvoidBinPacking = Current.BlockKind == BK_Block || |
| 541 | (NextNoComment && |
| 542 | NextNoComment->Type == TT_DesignatedInitializerPeriod); |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 543 | } else { |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 544 | NewIndent = 4 + std::max(State.Stack.back().LastSpace, |
| 545 | State.Stack.back().StartOfFunctionCall); |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 546 | AvoidBinPacking = !Style.BinPackParameters || |
| 547 | (Style.ExperimentalAutoDetectBinPacking && |
| 548 | (Current.PackingKind == PPK_OnePerLine || |
| 549 | (!BinPackInconclusiveFunctions && |
| 550 | Current.PackingKind == PPK_Inconclusive))); |
| 551 | } |
| 552 | |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 553 | State.Stack.push_back(ParenState(NewIndent, State.Stack.back().LastSpace, |
| 554 | AvoidBinPacking, |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 555 | State.Stack.back().NoLineBreak)); |
Daniel Jasper | 5798120 | 2013-09-13 09:20:45 +0000 | [diff] [blame] | 556 | State.Stack.back().BreakBeforeParameter = Current.BlockKind == BK_Block; |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 557 | ++State.ParenLevel; |
| 558 | } |
| 559 | |
| 560 | // If this '[' opens an ObjC call, determine whether all parameters fit into |
| 561 | // one line and put one per line if they don't. |
| 562 | if (Current.is(tok::l_square) && Current.Type == TT_ObjCMethodExpr && |
| 563 | Current.MatchingParen != NULL) { |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 564 | if (getLengthToMatchingParen(Current) + State.Column > |
| 565 | getColumnLimit(State)) |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 566 | State.Stack.back().BreakBeforeParameter = true; |
| 567 | } |
| 568 | |
| 569 | // If we encounter a closing ), ], } or >, we can remove a level from our |
| 570 | // stacks. |
Daniel Jasper | 7143a21 | 2013-08-28 09:17:37 +0000 | [diff] [blame] | 571 | if (State.Stack.size() > 1 && |
| 572 | (Current.isOneOf(tok::r_paren, tok::r_square) || |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 573 | (Current.is(tok::r_brace) && State.NextToken != State.Line->First) || |
Daniel Jasper | 7143a21 | 2013-08-28 09:17:37 +0000 | [diff] [blame] | 574 | State.NextToken->Type == TT_TemplateCloser)) { |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 575 | State.Stack.pop_back(); |
| 576 | --State.ParenLevel; |
| 577 | } |
| 578 | if (Current.is(tok::r_square)) { |
| 579 | // If this ends the array subscript expr, reset the corresponding value. |
| 580 | const FormatToken *NextNonComment = Current.getNextNonComment(); |
| 581 | if (NextNonComment && NextNonComment->isNot(tok::l_square)) |
| 582 | State.Stack.back().StartOfArraySubscripts = 0; |
| 583 | } |
| 584 | |
| 585 | // Remove scopes created by fake parenthesis. |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 586 | if (Current.isNot(tok::r_brace) || |
| 587 | (Current.MatchingParen && Current.MatchingParen->BlockKind != BK_Block)) { |
Daniel Jasper | 1a925bc | 2013-09-05 10:48:50 +0000 | [diff] [blame] | 588 | // Don't remove FakeRParens attached to r_braces that surround nested blocks |
| 589 | // as they will have been removed early (see above). |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 590 | for (unsigned i = 0, e = Current.FakeRParens; i != e; ++i) { |
| 591 | unsigned VariablePos = State.Stack.back().VariablePos; |
| 592 | State.Stack.pop_back(); |
| 593 | State.Stack.back().VariablePos = VariablePos; |
| 594 | } |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | if (Current.is(tok::string_literal) && State.StartOfStringLiteral == 0) { |
| 598 | State.StartOfStringLiteral = State.Column; |
| 599 | } else if (!Current.isOneOf(tok::comment, tok::identifier, tok::hash, |
| 600 | tok::string_literal)) { |
| 601 | State.StartOfStringLiteral = 0; |
| 602 | } |
| 603 | |
Alexander Kornienko | 83a7dcd | 2013-09-10 09:38:25 +0000 | [diff] [blame] | 604 | State.Column += Current.ColumnWidth; |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 605 | State.NextToken = State.NextToken->Next; |
Daniel Jasper | d489f8c | 2013-08-27 11:09:05 +0000 | [diff] [blame] | 606 | unsigned Penalty = breakProtrudingToken(Current, State, DryRun); |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 607 | if (State.Column > getColumnLimit(State)) { |
| 608 | unsigned ExcessCharacters = State.Column - getColumnLimit(State); |
| 609 | Penalty += Style.PenaltyExcessCharacter * ExcessCharacters; |
| 610 | } |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 611 | |
Daniel Jasper | d4a03db | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 612 | // If the previous has a special role, let it consume tokens as appropriate. |
| 613 | // It is necessary to start at the previous token for the only implemented |
| 614 | // role (comma separated list). That way, the decision whether or not to break |
| 615 | // after the "{" is already done and both options are tried and evaluated. |
| 616 | // FIXME: This is ugly, find a better way. |
| 617 | if (Previous && Previous->Role) |
| 618 | Penalty += Previous->Role->format(State, this, DryRun); |
| 619 | |
| 620 | return Penalty; |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 621 | } |
| 622 | |
Alexander Kornienko | 6f6154c | 2013-09-10 12:29:48 +0000 | [diff] [blame] | 623 | unsigned ContinuationIndenter::addMultilineToken(const FormatToken &Current, |
| 624 | LineState &State) { |
Alexander Kornienko | dcc0c5b | 2013-08-29 17:32:57 +0000 | [diff] [blame] | 625 | // Break before further function parameters on all levels. |
| 626 | for (unsigned i = 0, e = State.Stack.size(); i != e; ++i) |
| 627 | State.Stack[i].BreakBeforeParameter = true; |
| 628 | |
Alexander Kornienko | 83a7dcd | 2013-09-10 09:38:25 +0000 | [diff] [blame] | 629 | unsigned ColumnsUsed = State.Column; |
Alexander Kornienko | 4b762a9 | 2013-09-02 13:58:14 +0000 | [diff] [blame] | 630 | // We can only affect layout of the first and the last line, so the penalty |
| 631 | // for all other lines is constant, and we ignore it. |
Alexander Kornienko | 0b62cc3 | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 632 | State.Column = Current.LastLineColumnWidth; |
Alexander Kornienko | 4b762a9 | 2013-09-02 13:58:14 +0000 | [diff] [blame] | 633 | |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 634 | if (ColumnsUsed > getColumnLimit(State)) |
| 635 | return Style.PenaltyExcessCharacter * (ColumnsUsed - getColumnLimit(State)); |
Alexander Kornienko | dcc0c5b | 2013-08-29 17:32:57 +0000 | [diff] [blame] | 636 | return 0; |
| 637 | } |
| 638 | |
Alexander Kornienko | 2c2f729 | 2013-09-16 20:20:49 +0000 | [diff] [blame] | 639 | static bool getRawStringLiteralPrefixPostfix(StringRef Text, |
| 640 | StringRef &Prefix, |
| 641 | StringRef &Postfix) { |
| 642 | if (Text.startswith(Prefix = "R\"") || Text.startswith(Prefix = "uR\"") || |
| 643 | Text.startswith(Prefix = "UR\"") || Text.startswith(Prefix = "u8R\"") || |
| 644 | Text.startswith(Prefix = "LR\"")) { |
| 645 | size_t ParenPos = Text.find('('); |
| 646 | if (ParenPos != StringRef::npos) { |
| 647 | StringRef Delimiter = |
| 648 | Text.substr(Prefix.size(), ParenPos - Prefix.size()); |
| 649 | Prefix = Text.substr(0, ParenPos + 1); |
| 650 | Postfix = Text.substr(Text.size() - 2 - Delimiter.size()); |
| 651 | return Postfix.front() == ')' && Postfix.back() == '"' && |
| 652 | Postfix.substr(1).startswith(Delimiter); |
| 653 | } |
| 654 | } |
| 655 | return false; |
| 656 | } |
| 657 | |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 658 | unsigned ContinuationIndenter::breakProtrudingToken(const FormatToken &Current, |
| 659 | LineState &State, |
| 660 | bool DryRun) { |
Alexander Kornienko | 6f6154c | 2013-09-10 12:29:48 +0000 | [diff] [blame] | 661 | // Don't break multi-line tokens other than block comments. Instead, just |
| 662 | // update the state. |
| 663 | if (Current.Type != TT_BlockComment && Current.IsMultiline) |
| 664 | return addMultilineToken(Current, State); |
| 665 | |
Alexander Kornienko | 2c2f729 | 2013-09-16 20:20:49 +0000 | [diff] [blame] | 666 | if (!Current.isOneOf(tok::string_literal, tok::wide_string_literal, |
| 667 | tok::utf8_string_literal, tok::utf16_string_literal, |
| 668 | tok::utf32_string_literal, tok::comment)) |
Daniel Jasper | ed51c02 | 2013-08-23 10:05:49 +0000 | [diff] [blame] | 669 | return 0; |
| 670 | |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 671 | llvm::OwningPtr<BreakableToken> Token; |
Alexander Kornienko | 83a7dcd | 2013-09-10 09:38:25 +0000 | [diff] [blame] | 672 | unsigned StartColumn = State.Column - Current.ColumnWidth; |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 673 | |
Alexander Kornienko | 2c2f729 | 2013-09-16 20:20:49 +0000 | [diff] [blame] | 674 | if (Current.isOneOf(tok::string_literal, tok::wide_string_literal, |
| 675 | tok::utf8_string_literal, tok::utf16_string_literal, |
| 676 | tok::utf32_string_literal) && |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 677 | Current.Type != TT_ImplicitStringLiteral) { |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 678 | // Exempts unterminated string literals from line breaking. The user will |
| 679 | // likely want to terminate the string before any line breaking is done. |
| 680 | if (Current.IsUnterminatedLiteral) |
| 681 | return 0; |
| 682 | |
Alexander Kornienko | 2c2f729 | 2013-09-16 20:20:49 +0000 | [diff] [blame] | 683 | StringRef Text = Current.TokenText; |
| 684 | StringRef Prefix; |
| 685 | StringRef Postfix; |
| 686 | // FIXME: Handle whitespace between '_T', '(', '"..."', and ')'. |
| 687 | // FIXME: Store Prefix and Suffix (or PrefixLength and SuffixLength to |
| 688 | // reduce the overhead) for each FormatToken, which is a string, so that we |
| 689 | // don't run multiple checks here on the hot path. |
| 690 | if ((Text.endswith(Postfix = "\"") && |
| 691 | (Text.startswith(Prefix = "\"") || Text.startswith(Prefix = "u\"") || |
| 692 | Text.startswith(Prefix = "U\"") || Text.startswith(Prefix = "u8\"") || |
| 693 | Text.startswith(Prefix = "L\""))) || |
| 694 | (Text.startswith(Prefix = "_T(\"") && Text.endswith(Postfix = "\")")) || |
| 695 | getRawStringLiteralPrefixPostfix(Text, Prefix, Postfix)) { |
| 696 | Token.reset(new BreakableStringLiteral(Current, StartColumn, Prefix, |
| 697 | Postfix, State.Line->InPPDirective, |
| 698 | Encoding, Style)); |
| 699 | } else { |
| 700 | return 0; |
| 701 | } |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 702 | } else if (Current.Type == TT_BlockComment && Current.isTrailingComment()) { |
| 703 | Token.reset(new BreakableBlockComment( |
Alexander Kornienko | 83a7dcd | 2013-09-10 09:38:25 +0000 | [diff] [blame] | 704 | Current, StartColumn, Current.OriginalColumn, !Current.Previous, |
Alexander Kornienko | 0b62cc3 | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 705 | State.Line->InPPDirective, Encoding, Style)); |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 706 | } else if (Current.Type == TT_LineComment && |
| 707 | (Current.Previous == NULL || |
| 708 | Current.Previous->Type != TT_ImplicitStringLiteral)) { |
Alexander Kornienko | 0b62cc3 | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 709 | Token.reset(new BreakableLineComment( |
| 710 | Current, StartColumn, State.Line->InPPDirective, Encoding, Style)); |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 711 | } else { |
| 712 | return 0; |
| 713 | } |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 714 | if (Current.UnbreakableTailLength >= getColumnLimit(State)) |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 715 | return 0; |
| 716 | |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 717 | unsigned RemainingSpace = |
| 718 | getColumnLimit(State) - Current.UnbreakableTailLength; |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 719 | bool BreakInserted = false; |
| 720 | unsigned Penalty = 0; |
| 721 | unsigned RemainingTokenColumns = 0; |
| 722 | for (unsigned LineIndex = 0, EndIndex = Token->getLineCount(); |
| 723 | LineIndex != EndIndex; ++LineIndex) { |
| 724 | if (!DryRun) |
| 725 | Token->replaceWhitespaceBefore(LineIndex, Whitespaces); |
| 726 | unsigned TailOffset = 0; |
| 727 | RemainingTokenColumns = |
| 728 | Token->getLineLengthAfterSplit(LineIndex, TailOffset, StringRef::npos); |
| 729 | while (RemainingTokenColumns > RemainingSpace) { |
| 730 | BreakableToken::Split Split = |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 731 | Token->getSplit(LineIndex, TailOffset, getColumnLimit(State)); |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 732 | if (Split.first == StringRef::npos) { |
| 733 | // The last line's penalty is handled in addNextStateToQueue(). |
| 734 | if (LineIndex < EndIndex - 1) |
| 735 | Penalty += Style.PenaltyExcessCharacter * |
| 736 | (RemainingTokenColumns - RemainingSpace); |
| 737 | break; |
| 738 | } |
| 739 | assert(Split.first != 0); |
| 740 | unsigned NewRemainingTokenColumns = Token->getLineLengthAfterSplit( |
| 741 | LineIndex, TailOffset + Split.first + Split.second, StringRef::npos); |
| 742 | assert(NewRemainingTokenColumns < RemainingTokenColumns); |
| 743 | if (!DryRun) |
| 744 | Token->insertBreak(LineIndex, TailOffset, Split, Whitespaces); |
Daniel Jasper | f546178 | 2013-08-28 10:03:58 +0000 | [diff] [blame] | 745 | Penalty += Current.SplitPenalty; |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 746 | unsigned ColumnsUsed = |
| 747 | Token->getLineLengthAfterSplit(LineIndex, TailOffset, Split.first); |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 748 | if (ColumnsUsed > getColumnLimit(State)) { |
| 749 | Penalty += Style.PenaltyExcessCharacter * |
| 750 | (ColumnsUsed - getColumnLimit(State)); |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 751 | } |
| 752 | TailOffset += Split.first + Split.second; |
| 753 | RemainingTokenColumns = NewRemainingTokenColumns; |
| 754 | BreakInserted = true; |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | State.Column = RemainingTokenColumns; |
| 759 | |
| 760 | if (BreakInserted) { |
| 761 | // If we break the token inside a parameter list, we need to break before |
| 762 | // the next parameter on all levels, so that the next parameter is clearly |
| 763 | // visible. Line comments already introduce a break. |
| 764 | if (Current.Type != TT_LineComment) { |
| 765 | for (unsigned i = 0, e = State.Stack.size(); i != e; ++i) |
| 766 | State.Stack[i].BreakBeforeParameter = true; |
| 767 | } |
| 768 | |
Daniel Jasper | f546178 | 2013-08-28 10:03:58 +0000 | [diff] [blame] | 769 | Penalty += Current.is(tok::string_literal) ? Style.PenaltyBreakString |
| 770 | : Style.PenaltyBreakComment; |
| 771 | |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 772 | State.Stack.back().LastSpace = StartColumn; |
| 773 | } |
| 774 | return Penalty; |
| 775 | } |
| 776 | |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 777 | unsigned ContinuationIndenter::getColumnLimit(const LineState &State) const { |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 778 | // In preprocessor directives reserve two chars for trailing " \" |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 779 | return Style.ColumnLimit - (State.Line->InPPDirective ? 2 : 0); |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 780 | } |
| 781 | |
Daniel Jasper | 4df1ff9 | 2013-08-23 11:57:34 +0000 | [diff] [blame] | 782 | bool ContinuationIndenter::NextIsMultilineString(const LineState &State) { |
| 783 | const FormatToken &Current = *State.NextToken; |
| 784 | if (!Current.is(tok::string_literal)) |
| 785 | return false; |
Alexander Kornienko | dcc0c5b | 2013-08-29 17:32:57 +0000 | [diff] [blame] | 786 | // We never consider raw string literals "multiline" for the purpose of |
| 787 | // AlwaysBreakBeforeMultilineStrings implementation. |
| 788 | if (Current.TokenText.startswith("R\"")) |
| 789 | return false; |
Alexander Kornienko | 83a7dcd | 2013-09-10 09:38:25 +0000 | [diff] [blame] | 790 | if (Current.IsMultiline) |
Alexander Kornienko | dcc0c5b | 2013-08-29 17:32:57 +0000 | [diff] [blame] | 791 | return true; |
Daniel Jasper | 4df1ff9 | 2013-08-23 11:57:34 +0000 | [diff] [blame] | 792 | if (Current.getNextNonComment() && |
| 793 | Current.getNextNonComment()->is(tok::string_literal)) |
| 794 | return true; // Implicit concatenation. |
Alexander Kornienko | 83a7dcd | 2013-09-10 09:38:25 +0000 | [diff] [blame] | 795 | if (State.Column + Current.ColumnWidth + Current.UnbreakableTailLength > |
Daniel Jasper | 4df1ff9 | 2013-08-23 11:57:34 +0000 | [diff] [blame] | 796 | Style.ColumnLimit) |
| 797 | return true; // String will be split. |
Alexander Kornienko | dcc0c5b | 2013-08-29 17:32:57 +0000 | [diff] [blame] | 798 | return false; |
Daniel Jasper | 4df1ff9 | 2013-08-23 11:57:34 +0000 | [diff] [blame] | 799 | } |
| 800 | |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 801 | } // namespace format |
| 802 | } // namespace clang |