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