Coverage Report

Created: 2021-01-23 06:44

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Format/ContinuationIndenter.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- ContinuationIndenter.cpp - Format C++ code -----------------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
///
9
/// \file
10
/// This file implements the continuation indenter.
11
///
12
//===----------------------------------------------------------------------===//
13
14
#include "ContinuationIndenter.h"
15
#include "BreakableToken.h"
16
#include "FormatInternal.h"
17
#include "WhitespaceManager.h"
18
#include "clang/Basic/OperatorPrecedence.h"
19
#include "clang/Basic/SourceManager.h"
20
#include "clang/Format/Format.h"
21
#include "llvm/Support/Debug.h"
22
23
#define DEBUG_TYPE "format-indenter"
24
25
namespace clang {
26
namespace format {
27
28
// Returns true if a TT_SelectorName should be indented when wrapped,
29
// false otherwise.
30
static bool shouldIndentWrappedSelectorName(const FormatStyle &Style,
31
1.04k
                                            LineType LineType) {
32
1.04k
  return Style.IndentWrappedFunctionNames || 
LineType == LT_ObjCMethodDecl1.01k
;
33
1.04k
}
34
35
// Returns the length of everything up to the first possible line break after
36
// the ), ], } or > matching \c Tok.
37
static unsigned getLengthToMatchingParen(const FormatToken &Tok,
38
2.79k
                                         const std::vector<ParenState> &Stack) {
39
  // Normally whether or not a break before T is possible is calculated and
40
  // stored in T.CanBreakBefore. Braces, array initializers and text proto
41
  // messages like `key: < ... >` are an exception: a break is possible
42
  // before a closing brace R if a break was inserted after the corresponding
43
  // opening brace. The information about whether or not a break is needed
44
  // before a closing brace R is stored in the ParenState field
45
  // S.BreakBeforeClosingBrace where S is the state that R closes.
46
  //
47
  // In order to decide whether there can be a break before encountered right
48
  // braces, this implementation iterates over the sequence of tokens and over
49
  // the paren stack in lockstep, keeping track of the stack level which visited
50
  // right braces correspond to in MatchingStackIndex.
51
  //
52
  // For example, consider:
53
  // L. <- line number
54
  // 1. {
55
  // 2. {1},
56
  // 3. {2},
57
  // 4. {{3}}}
58
  //     ^ where we call this method with this token.
59
  // The paren stack at this point contains 3 brace levels:
60
  //  0. { at line 1, BreakBeforeClosingBrace: true
61
  //  1. first { at line 4, BreakBeforeClosingBrace: false
62
  //  2. second { at line 4, BreakBeforeClosingBrace: false,
63
  //  where there might be fake parens levels in-between these levels.
64
  // The algorithm will start at the first } on line 4, which is the matching
65
  // brace of the initial left brace and at level 2 of the stack. Then,
66
  // examining BreakBeforeClosingBrace: false at level 2, it will continue to
67
  // the second } on line 4, and will traverse the stack downwards until it
68
  // finds the matching { on level 1. Then, examining BreakBeforeClosingBrace:
69
  // false at level 1, it will continue to the third } on line 4 and will
70
  // traverse the stack downwards until it finds the matching { on level 0.
71
  // Then, examining BreakBeforeClosingBrace: true at level 0, the algorithm
72
  // will stop and will use the second } on line 4 to determine the length to
73
  // return, as in this example the range will include the tokens: {3}}
74
  //
75
  // The algorithm will only traverse the stack if it encounters braces, array
76
  // initializer squares or text proto angle brackets.
77
2.79k
  if (!Tok.MatchingParen)
78
1
    return 0;
79
2.79k
  FormatToken *End = Tok.MatchingParen;
80
  // Maintains a stack level corresponding to the current End token.
81
2.79k
  int MatchingStackIndex = Stack.size() - 1;
82
  // Traverses the stack downwards, looking for the level to which LBrace
83
  // corresponds. Returns either a pointer to the matching level or nullptr if
84
  // LParen is not found in the initial portion of the stack up to
85
  // MatchingStackIndex.
86
521
  auto FindParenState = [&](const FormatToken *LBrace) -> const ParenState * {
87
1.52k
    while (MatchingStackIndex >= 0 && Stack[MatchingStackIndex].Tok != LBrace)
88
1.00k
      --MatchingStackIndex;
89
521
    return MatchingStackIndex >= 0 ? &Stack[MatchingStackIndex] : 
nullptr0
;
90
521
  };
91
4.87k
  for (; End->Next; 
End = End->Next2.08k
) {
92
3.38k
    if (End->Next->CanBreakBefore)
93
891
      break;
94
2.49k
    if (!End->Next->closesScope())
95
1.60k
      continue;
96
891
    if (End->Next->MatchingParen &&
97
890
        End->Next->MatchingParen->isOneOf(
98
521
            tok::l_brace, TT_ArrayInitializerLSquare, tok::less)) {
99
521
      const ParenState *State = FindParenState(End->Next->MatchingParen);
100
521
      if (State && State->BreakBeforeClosingBrace)
101
411
        break;
102
521
    }
103
891
  }
104
2.79k
  return End->TotalLength - Tok.TotalLength + 1;
105
2.79k
}
106
107
4.28k
static unsigned getLengthToNextOperator(const FormatToken &Tok) {
108
4.28k
  if (!Tok.NextOperator)
109
2.52k
    return 0;
110
1.75k
  return Tok.NextOperator->TotalLength - Tok.TotalLength;
111
1.75k
}
112
113
// Returns \c true if \c Tok is the "." or "->" of a call and starts the next
114
// segment of a builder type call.
115
1.15M
static bool startsSegmentOfBuilderTypeCall(const FormatToken &Tok) {
116
1.15M
  return Tok.isMemberAccess() && 
Tok.Previous8.93k
&&
Tok.Previous->closesScope()8.93k
;
117
1.15M
}
118
119
// Returns \c true if \c Current starts a new parameter.
120
static bool startsNextParameter(const FormatToken &Current,
121
521k
                                const FormatStyle &Style) {
122
521k
  const FormatToken &Previous = *Current.Previous;
123
521k
  if (Current.is(TT_CtorInitializerComma) &&
124
768
      Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma)
125
126
    return true;
126
521k
  if (Style.Language == FormatStyle::LK_Proto && 
Current.is(TT_SelectorName)11.5k
)
127
1.78k
    return true;
128
519k
  return Previous.is(tok::comma) && 
!Current.isTrailingComment()63.3k
&&
129
62.2k
         ((Previous.isNot(TT_CtorInitializerComma) ||
130
624
           Style.BreakConstructorInitializers !=
131
624
               FormatStyle::BCIS_BeforeComma) &&
132
62.0k
          (Previous.isNot(TT_InheritanceComma) ||
133
176
           Style.BreakInheritanceList != FormatStyle::BILS_BeforeComma));
134
519k
}
135
136
static bool opensProtoMessageField(const FormatToken &LessTok,
137
861k
                                   const FormatStyle &Style) {
138
861k
  if (LessTok.isNot(tok::less))
139
852k
    return false;
140
9.43k
  return Style.Language == FormatStyle::LK_TextProto ||
141
8.52k
         (Style.Language == FormatStyle::LK_Proto &&
142
952
          (LessTok.NestingLevel > 0 ||
143
388
           (LessTok.Previous && LessTok.Previous->is(tok::equal))));
144
9.43k
}
145
146
// Returns the delimiter of a raw string literal, or None if TokenText is not
147
// the text of a raw string literal. The delimiter could be the empty string.
148
// For example, the delimiter of R"deli(cont)deli" is deli.
149
6.84k
static llvm::Optional<StringRef> getRawStringDelimiter(StringRef TokenText) {
150
6.84k
  if (TokenText.size() < 5 // The smallest raw string possible is 'R"()"'.
151
5.56k
      || !TokenText.startswith("R\"") || 
!TokenText.endswith("\"")704
)
152
6.13k
    return None;
153
154
  // A raw string starts with 'R"<delimiter>(' and delimiter is ascii and has
155
  // size at most 16 by the standard, so the first '(' must be among the first
156
  // 19 bytes.
157
704
  size_t LParenPos = TokenText.substr(0, 19).find_first_of('(');
158
704
  if (LParenPos == StringRef::npos)
159
0
    return None;
160
704
  StringRef Delimiter = TokenText.substr(2, LParenPos - 2);
161
162
  // Check that the string ends in ')Delimiter"'.
163
704
  size_t RParenPos = TokenText.size() - Delimiter.size() - 2;
164
704
  if (TokenText[RParenPos] != ')')
165
0
    return None;
166
704
  if (!TokenText.substr(RParenPos + 1).startswith(Delimiter))
167
0
    return None;
168
704
  return Delimiter;
169
704
}
170
171
// Returns the canonical delimiter for \p Language, or the empty string if no
172
// canonical delimiter is specified.
173
static StringRef
174
getCanonicalRawStringDelimiter(const FormatStyle &Style,
175
340
                               FormatStyle::LanguageKind Language) {
176
340
  for (const auto &Format : Style.RawStringFormats) {
177
340
    if (Format.Language == Language)
178
340
      return StringRef(Format.CanonicalDelimiter);
179
340
  }
180
0
  return "";
181
340
}
182
183
RawStringFormatStyleManager::RawStringFormatStyleManager(
184
16.8k
    const FormatStyle &CodeStyle) {
185
6.69k
  for (const auto &RawStringFormat : CodeStyle.RawStringFormats) {
186
6.69k
    llvm::Optional<FormatStyle> LanguageStyle =
187
6.69k
        CodeStyle.GetLanguageStyle(RawStringFormat.Language);
188
6.69k
    if (!LanguageStyle) {
189
6.68k
      FormatStyle PredefinedStyle;
190
6.68k
      if (!getPredefinedStyle(RawStringFormat.BasedOnStyle,
191
1
                              RawStringFormat.Language, &PredefinedStyle)) {
192
1
        PredefinedStyle = getLLVMStyle();
193
1
        PredefinedStyle.Language = RawStringFormat.Language;
194
1
      }
195
6.68k
      LanguageStyle = PredefinedStyle;
196
6.68k
    }
197
6.69k
    LanguageStyle->ColumnLimit = CodeStyle.ColumnLimit;
198
36.4k
    for (StringRef Delimiter : RawStringFormat.Delimiters) {
199
36.4k
      DelimiterStyle.insert({Delimiter, *LanguageStyle});
200
36.4k
    }
201
29.8k
    for (StringRef EnclosingFunction : RawStringFormat.EnclosingFunctions) {
202
29.8k
      EnclosingFunctionStyle.insert({EnclosingFunction, *LanguageStyle});
203
29.8k
    }
204
6.69k
  }
205
16.8k
}
206
207
llvm::Optional<FormatStyle>
208
364
RawStringFormatStyleManager::getDelimiterStyle(StringRef Delimiter) const {
209
364
  auto It = DelimiterStyle.find(Delimiter);
210
364
  if (It == DelimiterStyle.end())
211
36
    return None;
212
328
  return It->second;
213
328
}
214
215
llvm::Optional<FormatStyle>
216
RawStringFormatStyleManager::getEnclosingFunctionStyle(
217
14
    StringRef EnclosingFunction) const {
218
14
  auto It = EnclosingFunctionStyle.find(EnclosingFunction);
219
14
  if (It == EnclosingFunctionStyle.end())
220
1
    return None;
221
13
  return It->second;
222
13
}
223
224
ContinuationIndenter::ContinuationIndenter(const FormatStyle &Style,
225
                                           const AdditionalKeywords &Keywords,
226
                                           const SourceManager &SourceMgr,
227
                                           WhitespaceManager &Whitespaces,
228
                                           encoding::Encoding Encoding,
229
                                           bool BinPackInconclusiveFunctions)
230
    : Style(Style), Keywords(Keywords), SourceMgr(SourceMgr),
231
      Whitespaces(Whitespaces), Encoding(Encoding),
232
      BinPackInconclusiveFunctions(BinPackInconclusiveFunctions),
233
16.8k
      CommentPragmasRegex(Style.CommentPragmas), RawStringFormats(Style) {}
234
235
LineState ContinuationIndenter::getInitialState(unsigned FirstIndent,
236
                                                unsigned FirstStartColumn,
237
                                                const AnnotatedLine *Line,
238
53.3k
                                                bool DryRun) {
239
53.3k
  LineState State;
240
53.3k
  State.FirstIndent = FirstIndent;
241
53.3k
  if (FirstStartColumn && 
Line->First->NewlinesBefore == 0340
)
242
264
    State.Column = FirstStartColumn;
243
53.0k
  else
244
53.0k
    State.Column = FirstIndent;
245
  // With preprocessor directive indentation, the line starts on column 0
246
  // since it's indented after the hash, but FirstIndent is set to the
247
  // preprocessor indent.
248
53.3k
  if (Style.IndentPPDirectives == FormatStyle::PPDIS_AfterHash &&
249
511
      (Line->Type == LT_PreprocessorDirective ||
250
191
       Line->Type == LT_ImportStatement))
251
332
    State.Column = 0;
252
53.3k
  State.Line = Line;
253
53.3k
  State.NextToken = Line->First;
254
53.3k
  State.Stack.push_back(ParenState(/*Tok=*/nullptr, FirstIndent, FirstIndent,
255
53.3k
                                   /*AvoidBinPacking=*/false,
256
53.3k
                                   /*NoLineBreak=*/false));
257
53.3k
  State.LineContainsContinuedForLoopSection = false;
258
53.3k
  State.NoContinuation = false;
259
53.3k
  State.StartOfStringLiteral = 0;
260
53.3k
  State.StartOfLineLevel = 0;
261
53.3k
  State.LowestLevelOnLine = 0;
262
53.3k
  State.IgnoreStackForComparison = false;
263
264
53.3k
  if (Style.Language == FormatStyle::LK_TextProto) {
265
    // We need this in order to deal with the bin packing of text fields at
266
    // global scope.
267
1.22k
    State.Stack.back().AvoidBinPacking = true;
268
1.22k
    State.Stack.back().BreakBeforeParameter = true;
269
1.22k
    State.Stack.back().AlignColons = false;
270
1.22k
  }
271
272
  // The first token has already been indented and thus consumed.
273
53.3k
  moveStateToNextToken(State, DryRun, /*Newline=*/false);
274
53.3k
  return State;
275
53.3k
}
276
277
504k
bool ContinuationIndenter::canBreak(const LineState &State) {
278
504k
  const FormatToken &Current = *State.NextToken;
279
504k
  const FormatToken &Previous = *Current.Previous;
280
504k
  assert(&Previous == Current.Previous);
281
504k
  if (!Current.CanBreakBefore && 
!(279k
State.Stack.back().BreakBeforeClosingBrace279k
&&
282
13.7k
                                   Current.closesBlockOrBlockTypeList(Style)))
283
277k
    return false;
284
  // The opening "{" of a braced list has to be on the same line as the first
285
  // element if it is nested in another braced init list or function call.
286
226k
  if (!Current.MustBreakBefore && 
Previous.is(tok::l_brace)217k
&&
287
5.53k
      Previous.isNot(TT_DictLiteral) && 
Previous.is(BK_BracedInit)4.63k
&&
288
2.57k
      Previous.Previous &&
289
2.57k
      Previous.Previous->isOneOf(tok::l_brace, tok::l_paren, tok::comma))
290
2.17k
    return false;
291
  // This prevents breaks like:
292
  //   ...
293
  //   SomeParameter, OtherParameter).DoSomething(
294
  //   ...
295
  // As they hide "DoSomething" and are generally bad for readability.
296
224k
  if (Previous.opensScope() && 
Previous.isNot(tok::l_brace)126k
&&
297
121k
      State.LowestLevelOnLine < State.StartOfLineLevel &&
298
161
      State.LowestLevelOnLine < Current.NestingLevel)
299
161
    return false;
300
224k
  if (Current.isMemberAccess() && 
State.Stack.back().ContainsUnwrappedBuilder4.33k
)
301
161
    return false;
302
303
  // Don't create a 'hanging' indent if there are multiple blocks in a single
304
  // statement.
305
224k
  if (Previous.is(tok::l_brace) && 
State.Stack.size() > 15.09k
&&
306
5.09k
      State.Stack[State.Stack.size() - 2].NestedBlockInlined &&
307
1.82k
      State.Stack[State.Stack.size() - 2].HasMultipleNestedBlocks)
308
110
    return false;
309
310
  // Don't break after very short return types (e.g. "void") as that is often
311
  // unexpected.
312
224k
  if (Current.is(TT_FunctionDeclarationName) && 
State.Column < 6855
) {
313
462
    if (Style.AlwaysBreakAfterReturnType == FormatStyle::RTBS_None)
314
351
      return false;
315
223k
  }
316
317
  // If binary operators are moved to the next line (including commas for some
318
  // styles of constructor initializers), that's always ok.
319
223k
  if (!Current.isOneOf(TT_BinaryOperator, tok::comma) &&
320
221k
      State.Stack.back().NoLineBreakInOperand)
321
1.47k
    return false;
322
323
222k
  if (Previous.is(tok::l_square) && 
Previous.is(TT_ObjCMethodExpr)694
)
324
58
    return false;
325
326
222k
  return !State.Stack.back().NoLineBreak;
327
222k
}
328
329
502k
bool ContinuationIndenter::mustBreak(const LineState &State) {
330
502k
  const FormatToken &Current = *State.NextToken;
331
502k
  const FormatToken &Previous = *Current.Previous;
332
502k
  if (Style.BraceWrapping.BeforeLambdaBody && 
Current.CanBreakBefore4.19k
&&
333
1.57k
      Current.is(TT_LambdaLBrace) && 
Previous.isNot(TT_LineComment)267
) {
334
231
    auto LambdaBodyLength = getLengthToMatchingParen(Current, State.Stack);
335
231
    return (LambdaBodyLength > getColumnLimit(State));
336
231
  }
337
501k
  if (Current.MustBreakBefore || 
Current.is(TT_InlineASMColon)493k
)
338
8.39k
    return true;
339
493k
  if (State.Stack.back().BreakBeforeClosingBrace &&
340
21.7k
      Current.closesBlockOrBlockTypeList(Style))
341
1.39k
    return true;
342
492k
  if (Previous.is(tok::semi) && 
State.LineContainsContinuedForLoopSection264
)
343
0
    return true;
344
492k
  if (Style.Language == FormatStyle::LK_ObjC &&
345
154k
      Style.ObjCBreakBeforeNestedBlockParam &&
346
153k
      Current.ObjCSelectorNameParts > 1 &&
347
142
      Current.startsSequence(TT_SelectorName, tok::colon, tok::caret)) {
348
8
    return true;
349
8
  }
350
  // Avoid producing inconsistent states by requiring breaks where they are not
351
  // permitted for C# generic type constraints.
352
492k
  if (State.Stack.back().IsCSharpGenericTypeConstraint &&
353
60
      Previous.isNot(TT_CSharpGenericTypeConstraintComma))
354
52
    return false;
355
491k
  if ((startsNextParameter(Current, Style) || 
Previous.is(tok::semi)431k
||
356
431k
       (Previous.is(TT_TemplateCloser) && 
Current.is(TT_StartOfName)1.45k
&&
357
303
        Style.isCpp() &&
358
        // FIXME: This is a temporary workaround for the case where clang-format
359
        // sets BreakBeforeParameter to avoid bin packing and this creates a
360
        // completely unnecessary line break after a template type that isn't
361
        // line-wrapped.
362
269
        (Previous.NestingLevel == 1 || 
Style.BinPackParameters61
)) ||
363
431k
       (Style.BreakBeforeTernaryOperators && 
Current.is(TT_ConditionalExpr)416k
&&
364
2.06k
        Previous.isNot(tok::question)) ||
365
429k
       (!Style.BreakBeforeTernaryOperators &&
366
15.0k
        Previous.is(TT_ConditionalExpr))) &&
367
64.3k
      State.Stack.back().BreakBeforeParameter && 
!Current.isTrailingComment()11.3k
&&
368
11.3k
      !Current.isOneOf(tok::r_paren, tok::r_brace))
369
11.2k
    return true;
370
480k
  if (State.Stack.back().IsChainedConditional &&
371
2.43k
      ((Style.BreakBeforeTernaryOperators && 
Current.is(TT_ConditionalExpr)1.26k
&&
372
588
        Current.is(tok::colon)) ||
373
2.16k
       (!Style.BreakBeforeTernaryOperators && 
Previous.is(TT_ConditionalExpr)1.16k
&&
374
507
        Previous.is(tok::colon))))
375
495
    return true;
376
480k
  if (((Previous.is(TT_DictLiteral) && 
Previous.is(tok::l_brace)4.49k
) ||
377
479k
       (Previous.is(TT_ArrayInitializerLSquare) &&
378
484
        Previous.ParameterCount > 1) ||
379
478k
       opensProtoMessageField(Previous, Style)) &&
380
1.58k
      Style.ColumnLimit > 0 &&
381
1.58k
      getLengthToMatchingParen(Previous, State.Stack) + State.Column - 1 >
382
1.58k
          getColumnLimit(State))
383
1.08k
    return true;
384
385
479k
  const FormatToken &BreakConstructorInitializersToken =
386
479k
      Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon
387
2.31k
          ? Previous
388
476k
          : Current;
389
479k
  if (BreakConstructorInitializersToken.is(TT_CtorInitializerColon) &&
390
352
      (State.Column + State.Line->Last->TotalLength - Previous.TotalLength >
391
352
           getColumnLimit(State) ||
392
12
       State.Stack.back().BreakBeforeParameter) &&
393
346
      (Style.AllowShortFunctionsOnASingleLine != FormatStyle::SFS_All ||
394
338
       Style.BreakConstructorInitializers != FormatStyle::BCIS_BeforeColon ||
395
160
       Style.ColumnLimit != 0))
396
343
    return true;
397
398
478k
  if (Current.is(TT_ObjCMethodExpr) && 
!Previous.is(TT_SelectorName)1.38k
&&
399
530
      State.Line->startsWith(TT_ObjCMethodSpecifier))
400
10
    return true;
401
478k
  if (Current.is(TT_SelectorName) && 
!Previous.is(tok::at)1.63k
&&
402
1.62k
      State.Stack.back().ObjCSelectorNameFound &&
403
773
      State.Stack.back().BreakBeforeParameter &&
404
680
      (Style.ObjCBreakBeforeNestedBlockParam ||
405
18
       !Current.startsSequence(TT_SelectorName, tok::colon, tok::caret)))
406
664
    return true;
407
408
478k
  unsigned NewLineColumn = getNewLineColumn(State);
409
478k
  if (Current.isMemberAccess() && 
Style.ColumnLimit != 04.30k
&&
410
4.28k
      State.Column + getLengthToNextOperator(Current) > Style.ColumnLimit &&
411
521
      (State.Column > NewLineColumn ||
412
5
       Current.NestingLevel < State.StartOfLineLevel))
413
518
    return true;
414
415
477k
  if (startsSegmentOfBuilderTypeCall(Current) &&
416
954
      (State.Stack.back().CallContinuation != 0 ||
417
623
       State.Stack.back().BreakBeforeParameter) &&
418
      // JavaScript is treated different here as there is a frequent pattern:
419
      //   SomeFunction(function() {
420
      //     ...
421
      //   }.bind(...));
422
      // FIXME: We should find a more generic solution to this problem.
423
564
      !(State.Column <= NewLineColumn &&
424
31
        Style.Language == FormatStyle::LK_JavaScript) &&
425
554
      !(Previous.closesScopeAfterBlock() && 
State.Column <= NewLineColumn59
))
426
545
    return true;
427
428
  // If the template declaration spans multiple lines, force wrap before the
429
  // function/class declaration
430
477k
  if (Previous.ClosesTemplateDeclaration &&
431
233
      State.Stack.back().BreakBeforeParameter && 
Current.CanBreakBefore72
)
432
43
    return true;
433
434
476k
  if (!State.Line->First->is(tok::kw_enum) && 
State.Column <= NewLineColumn475k
)
435
54.4k
    return false;
436
437
422k
  if (Style.AlwaysBreakBeforeMultilineStrings &&
438
15.7k
      (NewLineColumn == State.FirstIndent + Style.ContinuationIndentWidth ||
439
13.4k
       Previous.is(tok::comma) || 
Current.NestingLevel < 211.1k
) &&
440
8.28k
      !Previous.isOneOf(tok::kw_return, tok::lessless, tok::at,
441
8.28k
                        Keywords.kw_dollar) &&
442
8.26k
      !Previous.isOneOf(TT_InlineASMColon, TT_ConditionalExpr) &&
443
8.25k
      nextIsMultilineString(State))
444
35
    return true;
445
446
  // Using CanBreakBefore here and below takes care of the decision whether the
447
  // current style uses wrapping before or after operators for the given
448
  // operator.
449
422k
  if (Previous.is(TT_BinaryOperator) && 
Current.CanBreakBefore7.54k
) {
450
    // If we need to break somewhere inside the LHS of a binary expression, we
451
    // should also break after the operator. Otherwise, the formatting would
452
    // hide the operator precedence, e.g. in:
453
    //   if (aaaaaaaaaaaaaa ==
454
    //           bbbbbbbbbbbbbb && c) {..
455
    // For comparisons, we only apply this rule, if the LHS is a binary
456
    // expression itself as otherwise, the line breaks seem superfluous.
457
    // We need special cases for ">>" which we have split into two ">" while
458
    // lexing in order to make template parsing easier.
459
4.80k
    bool IsComparison = (Previous.getPrecedence() == prec::Relational ||
460
4.53k
                         Previous.getPrecedence() == prec::Equality ||
461
4.33k
                         Previous.getPrecedence() == prec::Spaceship) &&
462
479
                        Previous.Previous &&
463
479
                        Previous.Previous->isNot(TT_BinaryOperator); // For >>.
464
4.80k
    bool LHSIsBinaryExpr =
465
4.80k
        Previous.Previous && Previous.Previous->EndsBinaryExpression;
466
4.80k
    if ((!IsComparison || 
LHSIsBinaryExpr463
) &&
!Current.isTrailingComment()4.38k
&&
467
4.38k
        Previous.getPrecedence() != prec::Assignment &&
468
1.77k
        State.Stack.back().BreakBeforeParameter)
469
243
      return true;
470
417k
  } else if (Current.is(TT_BinaryOperator) && 
Current.CanBreakBefore7.49k
&&
471
1.55k
             State.Stack.back().BreakBeforeParameter) {
472
181
    return true;
473
181
  }
474
475
  // Same as above, but for the first "<<" operator.
476
422k
  if (Current.is(tok::lessless) && 
Current.isNot(TT_OverloadedOperator)462
&&
477
450
      State.Stack.back().BreakBeforeParameter &&
478
0
      State.Stack.back().FirstLessLess == 0)
479
0
    return true;
480
481
422k
  if (Current.NestingLevel == 0 && 
!Current.isTrailingComment()34.8k
) {
482
    // Always break after "template <...>" and leading annotations. This is only
483
    // for cases where the entire line does not fit on a single line as a
484
    // different LineFormatter would be used otherwise.
485
33.9k
    if (Previous.ClosesTemplateDeclaration)
486
99
      return Style.AlwaysBreakTemplateDeclarations != FormatStyle::BTDS_No;
487
33.8k
    if (Previous.is(TT_FunctionAnnotationRParen))
488
24
      return true;
489
33.8k
    if (Previous.is(TT_LeadingJavaAnnotation) && 
Current.isNot(tok::l_paren)180
&&
490
120
        Current.isNot(TT_LeadingJavaAnnotation))
491
36
      return true;
492
421k
  }
493
494
  // If the return type spans multiple lines, wrap before the function name.
495
421k
  if (((Current.is(TT_FunctionDeclarationName) &&
496
        // Don't break before a C# function when no break after return type
497
705
        (!Style.isCSharp() ||
498
0
         Style.AlwaysBreakAfterReturnType != FormatStyle::RTBS_None)) ||
499
421k
       (Current.is(tok::kw_operator) && 
!Previous.is(tok::coloncolon)50
)) &&
500
705
      !Previous.is(tok::kw_template) && 
State.Stack.back().BreakBeforeParameter702
)
501
26
    return true;
502
503
  // The following could be precomputed as they do not depend on the state.
504
  // However, as they should take effect only if the UnwrappedLine does not fit
505
  // into the ColumnLimit, they are checked here in the ContinuationIndenter.
506
421k
  if (Style.ColumnLimit != 0 && 
Previous.is(BK_Block)419k
&&
507
1.70k
      Previous.is(tok::l_brace) && 
!Current.isOneOf(tok::r_brace, tok::comment)1.64k
)
508
95
    return true;
509
510
421k
  if (Current.is(tok::lessless) &&
511
462
      ((Previous.is(tok::identifier) && 
Previous.TokenText == "endl"225
) ||
512
456
       (Previous.Tok.isLiteral() && 
(133
Previous.TokenText.endswith("\\n\"")133
||
513
121
                                     Previous.TokenText == "\'\\n\'"))))
514
24
    return true;
515
516
421k
  if (Previous.is(TT_BlockComment) && 
Previous.IsMultiline147
)
517
26
    return true;
518
519
421k
  if (State.NoContinuation)
520
43
    return true;
521
522
421k
  return false;
523
421k
}
524
525
unsigned ContinuationIndenter::addTokenToState(LineState &State, bool Newline,
526
                                               bool DryRun,
527
913k
                                               unsigned ExtraSpaces) {
528
913k
  const FormatToken &Current = *State.NextToken;
529
530
913k
  assert(!State.Stack.empty());
531
913k
  State.NoContinuation = false;
532
533
913k
  if ((Current.is(TT_ImplicitStringLiteral) &&
534
804
       (Current.Previous->Tok.getIdentifierInfo() == nullptr ||
535
390
        Current.Previous->Tok.getIdentifierInfo()->getPPKeywordID() ==
536
796
            tok::pp_not_keyword))) {
537
796
    unsigned EndColumn =
538
796
        SourceMgr.getSpellingColumnNumber(Current.WhitespaceRange.getEnd());
539
796
    if (Current.LastNewlineOffset != 0) {
540
      // If there is a newline within this token, the final column will solely
541
      // determined by the current end column.
542
4
      State.Column = EndColumn;
543
792
    } else {
544
792
      unsigned StartColumn =
545
792
          SourceMgr.getSpellingColumnNumber(Current.WhitespaceRange.getBegin());
546
792
      assert(EndColumn >= StartColumn);
547
792
      State.Column += EndColumn - StartColumn;
548
792
    }
549
796
    moveStateToNextToken(State, DryRun, /*Newline=*/false);
550
796
    return 0;
551
796
  }
552
553
912k
  unsigned Penalty = 0;
554
912k
  if (Newline)
555
230k
    Penalty = addTokenOnNewLine(State, DryRun);
556
682k
  else
557
682k
    addTokenOnCurrentLine(State, DryRun, ExtraSpaces);
558
559
912k
  return moveStateToNextToken(State, DryRun, Newline) + Penalty;
560
912k
}
561
562
void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
563
682k
                                                 unsigned ExtraSpaces) {
564
682k
  FormatToken &Current = *State.NextToken;
565
682k
  const FormatToken &Previous = *State.NextToken->Previous;
566
682k
  if (Current.is(tok::equal) &&
567
9.32k
      (State.Line->First->is(tok::kw_for) || 
Current.NestingLevel == 09.09k
) &&
568
7.09k
      State.Stack.back().VariablePos == 0) {
569
6.71k
    State.Stack.back().VariablePos = State.Column;
570
    // Move over * and & if they are bound to the variable name.
571
6.71k
    const FormatToken *Tok = &Previous;
572
10.7k
    while (Tok && State.Stack.back().VariablePos >= Tok->ColumnWidth) {
573
10.6k
      State.Stack.back().VariablePos -= Tok->ColumnWidth;
574
10.6k
      if (Tok->SpacesRequiredBefore != 0)
575
6.60k
        break;
576
4.00k
      Tok = Tok->Previous;
577
4.00k
    }
578
6.71k
    if (Previous.PartOfMultiVariableDeclStmt)
579
117
      State.Stack.back().LastSpace = State.Stack.back().VariablePos;
580
6.71k
  }
581
582
682k
  unsigned Spaces = Current.SpacesRequiredBefore + ExtraSpaces;
583
584
  // Indent preprocessor directives after the hash if required.
585
682k
  int PPColumnCorrection = 0;
586
682k
  if (Style.IndentPPDirectives == FormatStyle::PPDIS_AfterHash &&
587
881
      Previous.is(tok::hash) && 
State.FirstIndent > 0332
&&
588
128
      (State.Line->Type == LT_PreprocessorDirective ||
589
128
       
State.Line->Type == LT_ImportStatement12
)) {
590
128
    Spaces += State.FirstIndent;
591
592
    // For preprocessor indent with tabs, State.Column will be 1 because of the
593
    // hash. This causes second-level indents onward to have an extra space
594
    // after the tabs. We avoid this misalignment by subtracting 1 from the
595
    // column value passed to replaceWhitespace().
596
128
    if (Style.UseTab != FormatStyle::UT_Never)
597
42
      PPColumnCorrection = -1;
598
128
  }
599
600
682k
  if (!DryRun)
601
188k
    Whitespaces.replaceWhitespace(Current, /*Newlines=*/0, Spaces,
602
188k
                                  State.Column + Spaces + PPColumnCorrection);
603
604
  // If "BreakBeforeInheritanceComma" mode, don't break within the inheritance
605
  // declaration unless there is multiple inheritance.
606
682k
  if (Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma &&
607
381
      Current.is(TT_InheritanceColon))
608
21
    State.Stack.back().NoLineBreak = true;
609
682k
  if (Style.BreakInheritanceList == FormatStyle::BILS_AfterColon &&
610
288
      Previous.is(TT_InheritanceColon))
611
21
    State.Stack.back().NoLineBreak = true;
612
613
682k
  if (Current.is(TT_SelectorName) &&
614
2.23k
      !State.Stack.back().ObjCSelectorNameFound) {
615
1.93k
    unsigned MinIndent =
616
1.93k
        std::max(State.FirstIndent + Style.ContinuationIndentWidth,
617
1.93k
                 State.Stack.back().Indent);
618
1.93k
    unsigned FirstColonPos = State.Column + Spaces + Current.ColumnWidth;
619
1.93k
    if (Current.LongestObjCSelectorName == 0)
620
1.65k
      State.Stack.back().AlignColons = false;
621
287
    else if (MinIndent + Current.LongestObjCSelectorName > FirstColonPos)
622
28
      State.Stack.back().ColonPos = MinIndent + Current.LongestObjCSelectorName;
623
259
    else
624
259
      State.Stack.back().ColonPos = FirstColonPos;
625
1.93k
  }
626
627
  // In "AlwaysBreak" mode, enforce wrapping directly after the parenthesis by
628
  // disallowing any further line breaks if there is no line break after the
629
  // opening parenthesis. Don't break if it doesn't conserve columns.
630
682k
  if (Style.AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak &&
631
26.2k
      (Previous.isOneOf(tok::l_paren, TT_TemplateOpener, tok::l_square) ||
632
23.2k
       (Previous.is(tok::l_brace) && 
Previous.isNot(BK_Block)674
&&
633
181
        Style.Cpp11BracedListStyle)) &&
634
3.20k
      State.Column > getNewLineColumn(State) &&
635
2.82k
      (!Previous.Previous || !Previous.Previous->isOneOf(
636
2.82k
                                 tok::kw_for, tok::kw_while, tok::kw_switch)) &&
637
      // Don't do this for simple (no expressions) one-argument function calls
638
      // as that feels like needlessly wasting whitespace, e.g.:
639
      //
640
      //   caaaaaaaaaaaall(
641
      //       caaaaaaaaaaaall(
642
      //           caaaaaaaaaaaall(
643
      //               caaaaaaaaaaaaaaaaaaaaaaall(aaaaaaaaaaaaaa, aaaaaaaaa))));
644
2.78k
      Current.FakeLParens.size() > 0 &&
645
516
      Current.FakeLParens.back() > prec::Unknown)
646
460
    State.Stack.back().NoLineBreak = true;
647
682k
  if (Previous.is(TT_TemplateString) && 
Previous.opensScope()296
)
648
170
    State.Stack.back().NoLineBreak = true;
649
650
682k
  if (Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign &&
651
674k
      !State.Stack.back().IsCSharpGenericTypeConstraint &&
652
673k
      Previous.opensScope() && 
Previous.isNot(TT_ObjCMethodExpr)164k
&&
653
163k
      (Current.isNot(TT_LineComment) || 
Previous.is(BK_BracedInit)494
)) {
654
162k
    State.Stack.back().Indent = State.Column + Spaces;
655
162k
    State.Stack.back().IsAligned = true;
656
162k
  }
657
682k
  if (State.Stack.back().AvoidBinPacking && 
startsNextParameter(Current, Style)29.1k
)
658
3.79k
    State.Stack.back().NoLineBreak = true;
659
682k
  if (startsSegmentOfBuilderTypeCall(Current) &&
660
684
      State.Column > getNewLineColumn(State))
661
622
    State.Stack.back().ContainsUnwrappedBuilder = true;
662
663
682k
  if (Current.is(TT_LambdaArrow) && 
Style.Language == FormatStyle::LK_Java273
)
664
18
    State.Stack.back().NoLineBreak = true;
665
682k
  if (Current.isMemberAccess() && 
Previous.is(tok::r_paren)5.15k
&&
666
583
      (Previous.MatchingParen &&
667
583
       (Previous.TotalLength - Previous.MatchingParen->TotalLength > 10)))
668
    // If there is a function call with long parameters, break before trailing
669
    // calls. This prevents things like:
670
    //   EXPECT_CALL(SomeLongParameter).Times(
671
    //       2);
672
    // We don't want to do this for short parameters as they can just be
673
    // indexes.
674
95
    State.Stack.back().NoLineBreak = true;
675
676
  // Don't allow the RHS of an operator to be split over multiple lines unless
677
  // there is a line-break right after the operator.
678
  // Exclude relational operators, as there, it is always more desirable to
679
  // have the LHS 'left' of the RHS.
680
682k
  const FormatToken *P = Current.getPreviousNonComment();
681
682k
  if (!Current.is(tok::comment) && 
P677k
&&
682
677k
      (P->isOneOf(TT_BinaryOperator, tok::comma) ||
683
600k
       (P->is(TT_ConditionalExpr) && 
P->is(tok::colon)4.32k
)) &&
684
79.2k
      !P->isOneOf(TT_OverloadedOperator, TT_CtorInitializerComma) &&
685
78.8k
      P->getPrecedence() != prec::Assignment &&
686
69.9k
      P->getPrecedence() != prec::Relational &&
687
69.0k
      P->getPrecedence() != prec::Spaceship) {
688
69.0k
    bool BreakBeforeOperator =
689
69.0k
        P->MustBreakBefore || 
P->is(tok::lessless)68.7k
||
690
67.6k
        (P->is(TT_BinaryOperator) &&
691
5.20k
         Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None) ||
692
66.0k
        (P->is(TT_ConditionalExpr) && 
Style.BreakBeforeTernaryOperators1.70k
);
693
    // Don't do this if there are only two operands. In these cases, there is
694
    // always a nice vertical separation between them and the extra line break
695
    // does not help.
696
69.0k
    bool HasTwoOperands =
697
69.0k
        P->OperatorIndex == 0 && 
!P->NextOperator51.1k
&&
!P->is(TT_ConditionalExpr)46.0k
;
698
69.0k
    if ((!BreakBeforeOperator &&
699
64.6k
         !(HasTwoOperands &&
700
43.4k
           Style.AlignOperands != FormatStyle::OAS_DontAlign)) ||
701
47.4k
        (!State.Stack.back().LastOperatorWrapped && 
BreakBeforeOperator4.40k
))
702
23.3k
      State.Stack.back().NoLineBreakInOperand = true;
703
69.0k
  }
704
705
682k
  State.Column += Spaces;
706
682k
  if (Current.isNot(tok::comment) && 
Previous.is(tok::l_paren)677k
&&
707
144k
      Previous.Previous &&
708
144k
      (Previous.Previous->is(tok::kw_for) || 
Previous.Previous->isIf()143k
)) {
709
    // Treat the condition inside an if as if it was a second function
710
    // parameter, i.e. let nested calls have a continuation indent.
711
1.60k
    State.Stack.back().LastSpace = State.Column;
712
1.60k
    State.Stack.back().NestedBlockIndent = State.Column;
713
680k
  } else if (!Current.isOneOf(tok::comment, tok::caret) &&
714
675k
             ((Previous.is(tok::comma) &&
715
61.2k
               !Previous.is(TT_OverloadedOperator)) ||
716
614k
              (Previous.is(tok::colon) && 
Previous.is(TT_ObjCMethodExpr)10.2k
))) {
717
62.7k
    State.Stack.back().LastSpace = State.Column;
718
617k
  } else if (Previous.is(TT_CtorInitializerColon) &&
719
546
             Style.BreakConstructorInitializers ==
720
12
                 FormatStyle::BCIS_AfterColon) {
721
12
    State.Stack.back().Indent = State.Column;
722
12
    State.Stack.back().LastSpace = State.Column;
723
617k
  } else if ((Previous.isOneOf(TT_BinaryOperator, TT_ConditionalExpr,
724
617k
                               TT_CtorInitializerColon)) &&
725
21.4k
             ((Previous.getPrecedence() != prec::Assignment &&
726
12.5k
               (Previous.isNot(tok::lessless) || 
Previous.OperatorIndex != 01.09k
||
727
388
                Previous.NextOperator)) ||
728
13.3k
              
Current.StartsBinaryExpression9.07k
)) {
729
    // Indent relative to the RHS of the expression unless this is a simple
730
    // assignment without binary expression on the RHS. Also indent relative to
731
    // unary operators and the colons of constructor initializers.
732
13.3k
    if (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None)
733
11.1k
      State.Stack.back().LastSpace = State.Column;
734
604k
  } else if (Previous.is(TT_InheritanceColon)) {
735
333
    State.Stack.back().Indent = State.Column;
736
333
    State.Stack.back().LastSpace = State.Column;
737
604k
  } else if (Current.is(TT_CSharpGenericTypeConstraintColon)) {
738
14
    State.Stack.back().ColonPos = State.Column;
739
604k
  } else if (Previous.opensScope()) {
740
    // If a function has a trailing call, indent all parameters from the
741
    // opening parenthesis. This avoids confusing indents like:
742
    //   OuterFunction(InnerFunctionCall( // break
743
    //       ParameterToInnerFunction))   // break
744
    //       .SecondInnerFunctionCall();
745
163k
    bool HasTrailingCall = false;
746
163k
    if (Previous.MatchingParen) {
747
159k
      const FormatToken *Next = Previous.MatchingParen->getNextNonComment();
748
159k
      HasTrailingCall = Next && 
Next->isMemberAccess()157k
;
749
159k
    }
750
163k
    if (HasTrailingCall && 
State.Stack.size() > 11.25k
&&
751
1.25k
        State.Stack[State.Stack.size() - 2].CallContinuation == 0)
752
884
      State.Stack.back().LastSpace = State.Column;
753
163k
  }
754
682k
}
755
756
unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State,
757
230k
                                                 bool DryRun) {
758
230k
  FormatToken &Current = *State.NextToken;
759
230k
  const FormatToken &Previous = *State.NextToken->Previous;
760
761
  // Extra penalty that needs to be added because of the way certain line
762
  // breaks are chosen.
763
230k
  unsigned Penalty = 0;
764
765
230k
  const FormatToken *PreviousNonComment = Current.getPreviousNonComment();
766
230k
  const FormatToken *NextNonComment = Previous.getNextNonComment();
767
230k
  if (!NextNonComment)
768
101
    NextNonComment = &Current;
769
  // The first line break on any NestingLevel causes an extra penalty in order
770
  // prefer similar line breaks.
771
230k
  if (!State.Stack.back().ContainsLineBreak)
772
202k
    Penalty += 15;
773
230k
  State.Stack.back().ContainsLineBreak = true;
774
775
230k
  Penalty += State.NextToken->SplitPenalty;
776
777
  // Breaking before the first "<<" is generally not desirable if the LHS is
778
  // short. Also always add the penalty if the LHS is split over multiple lines
779
  // to avoid unnecessary line breaks that just work around this penalty.
780
230k
  if (NextNonComment->is(tok::lessless) &&
781
717
      State.Stack.back().FirstLessLess == 0 &&
782
239
      (State.Column <= Style.ColumnLimit / 3 ||
783
117
       State.Stack.back().BreakBeforeParameter))
784
137
    Penalty += Style.PenaltyBreakFirstLessLess;
785
786
230k
  State.Column = getNewLineColumn(State);
787
788
  // Add Penalty proportional to amount of whitespace away from FirstColumn
789
  // This tends to penalize several lines that are far-right indented,
790
  // and prefers a line-break prior to such a block, e.g:
791
  //
792
  // Constructor() :
793
  //   member(value), looooooooooooooooong_member(
794
  //                      looooooooooong_call(param_1, param_2, param_3))
795
  // would then become
796
  // Constructor() :
797
  //   member(value),
798
  //   looooooooooooooooong_member(
799
  //       looooooooooong_call(param_1, param_2, param_3))
800
230k
  if (State.Column > State.FirstIndent)
801
224k
    Penalty +=
802
224k
        Style.PenaltyIndentedWhitespace * (State.Column - State.FirstIndent);
803
804
  // Indent nested blocks relative to this column, unless in a very specific
805
  // JavaScript special case where:
806
  //
807
  //   var loooooong_name =
808
  //       function() {
809
  //     // code
810
  //   }
811
  //
812
  // is common and should be formatted like a free-standing function. The same
813
  // goes for wrapping before the lambda return type arrow.
814
230k
  if (!Current.is(TT_LambdaArrow) &&
815
230k
      (Style.Language != FormatStyle::LK_JavaScript ||
816
5.80k
       Current.NestingLevel != 0 || 
!PreviousNonComment1.38k
||
817
1.38k
       !PreviousNonComment->is(tok::equal) ||
818
411
       !Current.isOneOf(Keywords.kw_async, Keywords.kw_function)))
819
230k
    State.Stack.back().NestedBlockIndent = State.Column;
820
821
230k
  if (NextNonComment->isMemberAccess()) {
822
4.24k
    if (State.Stack.back().CallContinuation == 0)
823
2.97k
      State.Stack.back().CallContinuation = State.Column;
824
225k
  } else if (NextNonComment->is(TT_SelectorName)) {
825
6.77k
    if (!State.Stack.back().ObjCSelectorNameFound) {
826
2.38k
      if (NextNonComment->LongestObjCSelectorName == 0) {
827
2.19k
        State.Stack.back().AlignColons = false;
828
193
      } else {
829
193
        State.Stack.back().ColonPos =
830
193
            (shouldIndentWrappedSelectorName(Style, State.Line->Type)
831
75
                 ? std::max(State.Stack.back().Indent,
832
75
                            State.FirstIndent + Style.ContinuationIndentWidth)
833
118
                 : State.Stack.back().Indent) +
834
193
            std::max(NextNonComment->LongestObjCSelectorName,
835
193
                     NextNonComment->ColumnWidth);
836
193
      }
837
4.38k
    } else if (State.Stack.back().AlignColons &&
838
642
               State.Stack.back().ColonPos <= NextNonComment->ColumnWidth) {
839
0
      State.Stack.back().ColonPos = State.Column + NextNonComment->ColumnWidth;
840
0
    }
841
219k
  } else if (PreviousNonComment && 
PreviousNonComment->is(tok::colon)218k
&&
842
4.63k
             PreviousNonComment->isOneOf(TT_ObjCMethodExpr, TT_DictLiteral)) {
843
    // FIXME: This is hacky, find a better way. The problem is that in an ObjC
844
    // method expression, the block should be aligned to the line starting it,
845
    // e.g.:
846
    //   [aaaaaaaaaaaaaaa aaaaaaaaa: \\ break for some reason
847
    //                        ^(int *i) {
848
    //                            // ...
849
    //                        }];
850
    // Thus, we set LastSpace of the next higher NestingLevel, to which we move
851
    // when we consume all of the "}"'s FakeRParens at the "{".
852
2.89k
    if (State.Stack.size() > 1)
853
2.83k
      State.Stack[State.Stack.size() - 2].LastSpace =
854
2.83k
          std::max(State.Stack.back().LastSpace, State.Stack.back().Indent) +
855
2.83k
          Style.ContinuationIndentWidth;
856
2.89k
  }
857
858
230k
  if ((PreviousNonComment &&
859
230k
       PreviousNonComment->isOneOf(tok::comma, tok::semi) &&
860
63.3k
       !State.Stack.back().AvoidBinPacking) ||
861
176k
      Previous.is(TT_BinaryOperator))
862
59.5k
    State.Stack.back().BreakBeforeParameter = false;
863
230k
  if (PreviousNonComment &&
864
230k
      PreviousNonComment->isOneOf(TT_TemplateCloser, TT_JavaAnnotation) &&
865
1.15k
      Current.NestingLevel == 0)
866
930
    State.Stack.back().BreakBeforeParameter = false;
867
230k
  if (NextNonComment->is(tok::question) ||
868
229k
      (PreviousNonComment && 
PreviousNonComment->is(tok::question)229k
))
869
1.63k
    State.Stack.back().BreakBeforeParameter = true;
870
230k
  if (Current.is(TT_BinaryOperator) && 
Current.CanBreakBefore2.51k
)
871
2.51k
    State.Stack.back().BreakBeforeParameter = false;
872
873
230k
  if (!DryRun) {
874
11.8k
    unsigned MaxEmptyLinesToKeep = Style.MaxEmptyLinesToKeep + 1;
875
11.8k
    if (Current.is(tok::r_brace) && 
Current.MatchingParen1.31k
&&
876
        // Only strip trailing empty lines for l_braces that have children, i.e.
877
        // for function expressions (lambdas, arrows, etc).
878
1.26k
        !Current.MatchingParen->Children.empty()) {
879
      // lambdas and arrow functions are expressions, thus their r_brace is not
880
      // on its own line, and thus not covered by UnwrappedLineFormatter's logic
881
      // about removing empty lines on closing blocks. Special case them here.
882
567
      MaxEmptyLinesToKeep = 1;
883
567
    }
884
11.8k
    unsigned Newlines =
885
11.8k
        std::max(1u, std::min(Current.NewlinesBefore, MaxEmptyLinesToKeep));
886
11.8k
    bool ContinuePPDirective =
887
11.8k
        State.Line->InPPDirective && 
State.Line->Type != LT_ImportStatement84
;
888
11.8k
    Whitespaces.replaceWhitespace(Current, Newlines, State.Column, State.Column,
889
11.8k
                                  State.Stack.back().IsAligned,
890
11.8k
                                  ContinuePPDirective);
891
11.8k
  }
892
893
230k
  if (!Current.isTrailingComment())
894
229k
    State.Stack.back().LastSpace = State.Column;
895
230k
  if (Current.is(tok::lessless))
896
    // If we are breaking before a "<<", we always want to indent relative to
897
    // RHS. This is necessary only for "<<", as we special-case it and don't
898
    // always indent relative to the RHS.
899
717
    State.Stack.back().LastSpace += 3; // 3 -> width of "<< ".
900
901
230k
  State.StartOfLineLevel = Current.NestingLevel;
902
230k
  State.LowestLevelOnLine = Current.NestingLevel;
903
904
  // Any break on this level means that the parent level has been broken
905
  // and we need to avoid bin packing there.
906
230k
  bool NestedBlockSpecialCase =
907
230k
      (!Style.isCpp() && 
Current.is(tok::r_brace)20.1k
&&
State.Stack.size() > 12.53k
&&
908
2.53k
       State.Stack[State.Stack.size() - 2].NestedBlockInlined) ||
909
229k
      (Style.Language == FormatStyle::LK_ObjC && 
Current.is(tok::r_brace)70.9k
&&
910
897
       State.Stack.size() > 1 && !Style.ObjCBreakBeforeNestedBlockParam);
911
230k
  if (!NestedBlockSpecialCase)
912
3.68M
    
for (unsigned i = 0, e = State.Stack.size() - 1; 229k
i != e;
++i3.45M
)
913
3.45M
      State.Stack[i].BreakBeforeParameter = true;
914
915
230k
  if (PreviousNonComment &&
916
230k
      !PreviousNonComment->isOneOf(tok::comma, tok::colon, tok::semi) &&
917
162k
      (PreviousNonComment->isNot(TT_TemplateCloser) ||
918
1.11k
       Current.NestingLevel != 0) &&
919
161k
      !PreviousNonComment->isOneOf(
920
161k
          TT_BinaryOperator, TT_FunctionAnnotationRParen, TT_JavaAnnotation,
921
161k
          TT_LeadingJavaAnnotation) &&
922
154k
      Current.isNot(TT_BinaryOperator) && 
!PreviousNonComment->opensScope()152k
)
923
23.9k
    State.Stack.back().BreakBeforeParameter = true;
924
925
  // If we break after { or the [ of an array initializer, we should also break
926
  // before the corresponding } or ].
927
230k
  if (PreviousNonComment &&
928
230k
      (PreviousNonComment->isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) ||
929
222k
       opensProtoMessageField(*PreviousNonComment, Style)))
930
7.96k
    State.Stack.back().BreakBeforeClosingBrace = true;
931
932
230k
  if (State.Stack.back().AvoidBinPacking) {
933
    // If we are breaking after '(', '{', '<', or this is the break after a ':'
934
    // to start a member initializater list in a constructor, this should not
935
    // be considered bin packing unless the relevant AllowAll option is false or
936
    // this is a dict/object literal.
937
21.8k
    bool PreviousIsBreakingCtorInitializerColon =
938
21.8k
        Previous.is(TT_CtorInitializerColon) &&
939
135
        Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon;
940
21.8k
    if (!(Previous.isOneOf(tok::l_paren, tok::l_brace, TT_BinaryOperator) ||
941
16.4k
          PreviousIsBreakingCtorInitializerColon) ||
942
5.54k
        (!Style.AllowAllParametersOfDeclarationOnNextLine &&
943
241
         State.Line->MustBeDeclaration) ||
944
5.38k
        (!Style.AllowAllArgumentsOnNextLine &&
945
129
         !State.Line->MustBeDeclaration) ||
946
5.30k
        (!Style.AllowAllConstructorInitializersOnNextLine &&
947
207
         PreviousIsBreakingCtorInitializerColon) ||
948
5.22k
        Previous.is(TT_DictLiteral))
949
18.3k
      State.Stack.back().BreakBeforeParameter = true;
950
951
    // If we are breaking after a ':' to start a member initializer list,
952
    // and we allow all arguments on the next line, we should not break
953
    // before the next parameter.
954
21.8k
    if (PreviousIsBreakingCtorInitializerColon &&
955
135
        Style.AllowAllConstructorInitializersOnNextLine)
956
36
      State.Stack.back().BreakBeforeParameter = false;
957
21.8k
  }
958
959
230k
  return Penalty;
960
230k
}
961
962
712k
unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) {
963
712k
  if (!State.NextToken || !State.NextToken->Previous)
964
0
    return 0;
965
966
712k
  FormatToken &Current = *State.NextToken;
967
968
712k
  if (State.Stack.back().IsCSharpGenericTypeConstraint &&
969
16
      Current.isNot(TT_CSharpGenericTypeConstraint))
970
16
    return State.Stack.back().ColonPos + 2;
971
972
712k
  const FormatToken &Previous = *Current.Previous;
973
  // If we are continuing an expression, we want to use the continuation indent.
974
712k
  unsigned ContinuationIndent =
975
712k
      std::max(State.Stack.back().LastSpace, State.Stack.back().Indent) +
976
712k
      Style.ContinuationIndentWidth;
977
712k
  const FormatToken *PreviousNonComment = Current.getPreviousNonComment();
978
712k
  const FormatToken *NextNonComment = Previous.getNextNonComment();
979
712k
  if (!NextNonComment)
980
307
    NextNonComment = &Current;
981
982
  // Java specific bits.
983
712k
  if (Style.Language == FormatStyle::LK_Java &&
984
4.28k
      Current.isOneOf(Keywords.kw_implements, Keywords.kw_extends))
985
86
    return std::max(State.Stack.back().LastSpace,
986
86
                    State.Stack.back().Indent + Style.ContinuationIndentWidth);
987
988
711k
  if (Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths &&
989
1.15k
      State.Line->First->is(tok::kw_enum))
990
75
    return (Style.IndentWidth * State.Line->First->IndentLevel) +
991
75
           Style.IndentWidth;
992
993
711k
  if (NextNonComment->is(tok::l_brace) && 
NextNonComment->is(BK_Block)11.6k
)
994
3.65k
    return Current.NestingLevel == 0 ? 
State.FirstIndent2.11k
995
1.54k
                                     : State.Stack.back().Indent;
996
708k
  if ((Current.isOneOf(tok::r_brace, tok::r_square) ||
997
694k
       (Current.is(tok::greater) &&
998
2.65k
        (Style.Language == FormatStyle::LK_Proto ||
999
2.35k
         Style.Language == FormatStyle::LK_TextProto))) &&
1000
14.5k
      State.Stack.size() > 1) {
1001
14.5k
    if (Current.closesBlockOrBlockTypeList(Style))
1002
6.51k
      return State.Stack[State.Stack.size() - 2].NestedBlockIndent;
1003
8.04k
    if (Current.MatchingParen && 
Current.MatchingParen->is(BK_BracedInit)6.47k
)
1004
4.25k
      return State.Stack[State.Stack.size() - 2].LastSpace;
1005
3.79k
    return State.FirstIndent;
1006
3.79k
  }
1007
  // Indent a closing parenthesis at the previous level if followed by a semi,
1008
  // const, or opening brace. This allows indentations such as:
1009
  //     foo(
1010
  //       a,
1011
  //     );
1012
  //     int Foo::getter(
1013
  //         //
1014
  //     ) const {
1015
  //       return foo;
1016
  //     }
1017
  //     function foo(
1018
  //       a,
1019
  //     ) {
1020
  //       code(); //
1021
  //     }
1022
693k
  if (Current.is(tok::r_paren) && 
State.Stack.size() > 132.4k
&&
1023
32.4k
      (!Current.Next ||
1024
32.2k
       Current.Next->isOneOf(tok::semi, tok::kw_const, tok::l_brace)))
1025
5.91k
    return State.Stack[State.Stack.size() - 2].LastSpace;
1026
687k
  if (NextNonComment->is(TT_TemplateString) && 
NextNonComment->closesScope()238
)
1027
92
    return State.Stack[State.Stack.size() - 2].LastSpace;
1028
687k
  if (Current.is(tok::identifier) && 
Current.Next385k
&&
1029
385k
      (Current.Next->is(TT_DictLiteral) ||
1030
378k
       ((Style.Language == FormatStyle::LK_Proto ||
1031
375k
         Style.Language == FormatStyle::LK_TextProto) &&
1032
3.99k
        Current.Next->isOneOf(tok::less, tok::l_brace))))
1033
6.21k
    return State.Stack.back().Indent;
1034
681k
  if (NextNonComment->is(TT_ObjCStringLiteral) &&
1035
434
      State.StartOfStringLiteral != 0)
1036
66
    return State.StartOfStringLiteral - 1;
1037
681k
  if (NextNonComment->isStringLiteral() && 
State.StartOfStringLiteral != 04.06k
)
1038
361
    return State.StartOfStringLiteral;
1039
681k
  if (NextNonComment->is(tok::lessless) &&
1040
1.24k
      State.Stack.back().FirstLessLess != 0)
1041
793
    return State.Stack.back().FirstLessLess;
1042
680k
  if (NextNonComment->isMemberAccess()) {
1043
9.24k
    if (State.Stack.back().CallContinuation == 0)
1044
6.84k
      return ContinuationIndent;
1045
2.40k
    return State.Stack.back().CallContinuation;
1046
2.40k
  }
1047
671k
  if (State.Stack.back().QuestionColumn != 0 &&
1048
8.90k
      ((NextNonComment->is(tok::colon) &&
1049
2.82k
        NextNonComment->is(TT_ConditionalExpr)) ||
1050
7.07k
       
Previous.is(TT_ConditionalExpr)6.08k
)) {
1051
7.07k
    if (((NextNonComment->is(tok::colon) && 
NextNonComment->Next2.82k
&&
1052
2.82k
          !NextNonComment->Next->FakeLParens.empty() &&
1053
948
          NextNonComment->Next->FakeLParens.back() == prec::Conditional) ||
1054
6.20k
         (Previous.is(tok::colon) && 
!Current.FakeLParens.empty()2.43k
&&
1055
921
          Current.FakeLParens.back() == prec::Conditional)) &&
1056
1.72k
        !State.Stack.back().IsWrappedConditional) {
1057
      // NOTE: we may tweak this slightly:
1058
      //    * not remove the 'lead' ContinuationIndentWidth
1059
      //    * always un-indent by the operator when
1060
      //    BreakBeforeTernaryOperators=true
1061
1.67k
      unsigned Indent = State.Stack.back().Indent;
1062
1.67k
      if (Style.AlignOperands != FormatStyle::OAS_DontAlign) {
1063
1.63k
        Indent -= Style.ContinuationIndentWidth;
1064
1.63k
      }
1065
1.67k
      if (Style.BreakBeforeTernaryOperators &&
1066
900
          State.Stack.back().UnindentOperator)
1067
42
        Indent -= 2;
1068
1.67k
      return Indent;
1069
1.67k
    }
1070
5.40k
    return State.Stack.back().QuestionColumn;
1071
5.40k
  }
1072
663k
  if (Previous.is(tok::comma) && 
State.Stack.back().VariablePos != 0110k
)
1073
246
    return State.Stack.back().VariablePos;
1074
663k
  if ((PreviousNonComment &&
1075
663k
       (PreviousNonComment->ClosesTemplateDeclaration ||
1076
662k
        PreviousNonComment->isOneOf(
1077
662k
            TT_AttributeParen, TT_AttributeSquare, TT_FunctionAnnotationRParen,
1078
662k
            TT_JavaAnnotation, TT_LeadingJavaAnnotation))) ||
1079
662k
      (!Style.IndentWrappedFunctionNames &&
1080
661k
       NextNonComment->isOneOf(tok::kw_operator, TT_FunctionDeclarationName)))
1081
3.34k
    return std::max(State.Stack.back().LastSpace, State.Stack.back().Indent);
1082
660k
  if (NextNonComment->is(TT_SelectorName)) {
1083
1.81k
    if (!State.Stack.back().ObjCSelectorNameFound) {
1084
848
      unsigned MinIndent = State.Stack.back().Indent;
1085
848
      if (shouldIndentWrappedSelectorName(Style, State.Line->Type))
1086
170
        MinIndent = std::max(MinIndent,
1087
170
                             State.FirstIndent + Style.ContinuationIndentWidth);
1088
      // If LongestObjCSelectorName is 0, we are indenting the first
1089
      // part of an ObjC selector (or a selector component which is
1090
      // not colon-aligned due to block formatting).
1091
      //
1092
      // Otherwise, we are indenting a subsequent part of an ObjC
1093
      // selector which should be colon-aligned to the longest
1094
      // component of the ObjC selector.
1095
      //
1096
      // In either case, we want to respect Style.IndentWrappedFunctionNames.
1097
848
      return MinIndent +
1098
848
             std::max(NextNonComment->LongestObjCSelectorName,
1099
848
                      NextNonComment->ColumnWidth) -
1100
848
             NextNonComment->ColumnWidth;
1101
848
    }
1102
970
    if (!State.Stack.back().AlignColons)
1103
275
      return State.Stack.back().Indent;
1104
695
    if (State.Stack.back().ColonPos > NextNonComment->ColumnWidth)
1105
695
      return State.Stack.back().ColonPos - NextNonComment->ColumnWidth;
1106
0
    return State.Stack.back().Indent;
1107
0
  }
1108
658k
  if (NextNonComment->is(tok::colon) && 
NextNonComment->is(TT_ObjCMethodExpr)5.20k
)
1109
870
    return State.Stack.back().ColonPos;
1110
657k
  if (NextNonComment->is(TT_ArraySubscriptLSquare)) {
1111
907
    if (State.Stack.back().StartOfArraySubscripts != 0)
1112
120
      return State.Stack.back().StartOfArraySubscripts;
1113
787
    else if (Style.isCSharp()) // C# allows `["key"] = value` inside object
1114
                               // initializers.
1115
78
      return State.Stack.back().Indent;
1116
709
    return ContinuationIndent;
1117
709
  }
1118
1119
  // This ensure that we correctly format ObjC methods calls without inputs,
1120
  // i.e. where the last element isn't selector like: [callee method];
1121
656k
  if (NextNonComment->is(tok::identifier) && 
NextNonComment->FakeRParens == 0373k
&&
1122
327k
      NextNonComment->Next && 
NextNonComment->Next->is(TT_ObjCMethodExpr)327k
)
1123
256
    return State.Stack.back().Indent;
1124
1125
656k
  if (NextNonComment->isOneOf(TT_StartOfName, TT_PointerOrReference) ||
1126
648k
      Previous.isOneOf(tok::coloncolon, tok::equal, TT_JsTypeColon))
1127
20.1k
    return ContinuationIndent;
1128
636k
  if (PreviousNonComment && 
PreviousNonComment->is(tok::colon)636k
&&
1129
7.44k
      PreviousNonComment->isOneOf(TT_ObjCMethodExpr, TT_DictLiteral))
1130
6.68k
    return ContinuationIndent;
1131
629k
  if (NextNonComment->is(TT_CtorInitializerComma))
1132
489
    return State.Stack.back().Indent;
1133
629k
  if (PreviousNonComment && 
PreviousNonComment->is(TT_CtorInitializerColon)629k
&&
1134
484
      Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon)
1135
253
    return State.Stack.back().Indent;
1136
628k
  if (PreviousNonComment && 
PreviousNonComment->is(TT_InheritanceColon)628k
&&
1137
156
      Style.BreakInheritanceList == FormatStyle::BILS_AfterColon)
1138
42
    return State.Stack.back().Indent;
1139
628k
  if (NextNonComment->isOneOf(TT_CtorInitializerColon, TT_InheritanceColon,
1140
628k
                              TT_InheritanceComma))
1141
896
    return State.FirstIndent + Style.ConstructorInitializerIndentWidth;
1142
628k
  if (Previous.is(tok::r_paren) && 
!Current.isBinaryOperator()25.5k
&&
1143
24.0k
      !Current.isOneOf(tok::colon, tok::comment))
1144
23.5k
    return ContinuationIndent;
1145
604k
  if (Current.is(TT_ProtoExtensionLSquare))
1146
260
    return State.Stack.back().Indent;
1147
604k
  if (Current.isBinaryOperator() && 
State.Stack.back().UnindentOperator16.2k
)
1148
348
    return State.Stack.back().Indent - Current.Tok.getLength() -
1149
348
           Current.SpacesRequiredBefore;
1150
603k
  if (Current.isOneOf(tok::comment, TT_BlockComment, TT_LineComment) &&
1151
3.26k
      NextNonComment->isBinaryOperator() && 
State.Stack.back().UnindentOperator619
)
1152
141
    return State.Stack.back().Indent - NextNonComment->Tok.getLength() -
1153
141
           NextNonComment->SpacesRequiredBefore;
1154
603k
  if (State.Stack.back().Indent == State.FirstIndent && 
PreviousNonComment15.8k
&&
1155
15.7k
      !PreviousNonComment->isOneOf(tok::r_brace, TT_CtorInitializerComma))
1156
    // Ensure that we fall back to the continuation indent width instead of
1157
    // just flushing continuations left.
1158
14.9k
    return State.Stack.back().Indent + Style.ContinuationIndentWidth;
1159
588k
  return State.Stack.back().Indent;
1160
588k
}
1161
1162
static bool hasNestedBlockInlined(const FormatToken *Previous,
1163
                                  const FormatToken &Current,
1164
229k
                                  const FormatStyle &Style) {
1165
229k
  if (Previous->isNot(tok::l_paren))
1166
85.4k
    return true;
1167
144k
  if (Previous->ParameterCount > 1)
1168
61.5k
    return true;
1169
1170
  // Also a nested block if contains a lambda inside function with 1 parameter
1171
82.7k
  return (Style.BraceWrapping.BeforeLambdaBody && 
Current.is(TT_LambdaLSquare)909
);
1172
82.7k
}
1173
1174
unsigned ContinuationIndenter::moveStateToNextToken(LineState &State,
1175
966k
                                                    bool DryRun, bool Newline) {
1176
966k
  assert(State.Stack.size());
1177
966k
  const FormatToken &Current = *State.NextToken;
1178
1179
966k
  if (Current.is(TT_CSharpGenericTypeConstraint))
1180
12
    State.Stack.back().IsCSharpGenericTypeConstraint = true;
1181
966k
  if (Current.isOneOf(tok::comma, TT_BinaryOperator))
1182
97.9k
    State.Stack.back().NoLineBreakInOperand = false;
1183
966k
  if (Current.isOneOf(TT_InheritanceColon, TT_CSharpGenericTypeConstraintColon))
1184
627
    State.Stack.back().AvoidBinPacking = true;
1185
966k
  if (Current.is(tok::lessless) && 
Current.isNot(TT_OverloadedOperator)1.41k
) {
1186
1.39k
    if (State.Stack.back().FirstLessLess == 0)
1187
517
      State.Stack.back().FirstLessLess = State.Column;
1188
873
    else
1189
873
      State.Stack.back().LastOperatorWrapped = Newline;
1190
1.39k
  }
1191
966k
  if (Current.is(TT_BinaryOperator) && 
Current.isNot(tok::lessless)18.9k
)
1192
17.5k
    State.Stack.back().LastOperatorWrapped = Newline;
1193
966k
  if (Current.is(TT_ConditionalExpr) && 
Current.Previous6.26k
&&
1194
6.26k
      !Current.Previous->is(TT_ConditionalExpr))
1195
6.20k
    State.Stack.back().LastOperatorWrapped = Newline;
1196
966k
  if (Current.is(TT_ArraySubscriptLSquare) &&
1197
1.56k
      State.Stack.back().StartOfArraySubscripts == 0)
1198
1.40k
    State.Stack.back().StartOfArraySubscripts = State.Column;
1199
966k
  if (Current.is(TT_ConditionalExpr) && 
Current.is(tok::question)6.26k
&&
1200
3.03k
      ((Current.MustBreakBefore) ||
1201
2.92k
       (Current.getNextNonComment() &&
1202
2.92k
        Current.getNextNonComment()->MustBreakBefore)))
1203
186
    State.Stack.back().IsWrappedConditional = true;
1204
966k
  if (Style.BreakBeforeTernaryOperators && 
Current.is(tok::question)923k
)
1205
2.06k
    State.Stack.back().QuestionColumn = State.Column;
1206
966k
  if (!Style.BreakBeforeTernaryOperators && 
Current.isNot(tok::colon)42.5k
) {
1207
40.2k
    const FormatToken *Previous = Current.Previous;
1208
40.6k
    while (Previous && 
Previous->isTrailingComment()36.1k
)
1209
422
      Previous = Previous->Previous;
1210
40.2k
    if (Previous && 
Previous->is(tok::question)35.7k
)
1211
1.60k
      State.Stack.back().QuestionColumn = State.Column;
1212
40.2k
  }
1213
966k
  if (!Current.opensScope() && 
!Current.closesScope()782k
&&
1214
698k
      !Current.is(TT_PointerOrReference))
1215
694k
    State.LowestLevelOnLine =
1216
694k
        std::min(State.LowestLevelOnLine, Current.NestingLevel);
1217
966k
  if (Current.isMemberAccess())
1218
9.44k
    State.Stack.back().StartOfFunctionCall =
1219
6.03k
        !Current.NextOperator ? 0 : 
State.Column3.40k
;
1220
966k
  if (Current.is(TT_SelectorName))
1221
9.51k
    State.Stack.back().ObjCSelectorNameFound = true;
1222
966k
  if (Current.is(TT_CtorInitializerColon) &&
1223
821
      Style.BreakConstructorInitializers != FormatStyle::BCIS_AfterColon) {
1224
    // Indent 2 from the column, so:
1225
    // SomeClass::SomeClass()
1226
    //     : First(...), ...
1227
    //       Next(...)
1228
    //       ^ line up here.
1229
564
    State.Stack.back().Indent =
1230
564
        State.Column +
1231
564
        (Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma
1232
172
             ? 0
1233
392
             : 2);
1234
564
    State.Stack.back().NestedBlockIndent = State.Stack.back().Indent;
1235
564
    if (Style.ConstructorInitializerAllOnOneLineOrOnePerLine) {
1236
255
      State.Stack.back().AvoidBinPacking = true;
1237
255
      State.Stack.back().BreakBeforeParameter =
1238
255
          !Style.AllowAllConstructorInitializersOnNextLine;
1239
309
    } else {
1240
309
      State.Stack.back().BreakBeforeParameter = false;
1241
309
    }
1242
564
  }
1243
966k
  if (Current.is(TT_CtorInitializerColon) &&
1244
821
      Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon) {
1245
257
    State.Stack.back().Indent =
1246
257
        State.FirstIndent + Style.ConstructorInitializerIndentWidth;
1247
257
    State.Stack.back().NestedBlockIndent = State.Stack.back().Indent;
1248
257
    if (Style.ConstructorInitializerAllOnOneLineOrOnePerLine)
1249
135
      State.Stack.back().AvoidBinPacking = true;
1250
257
  }
1251
966k
  if (Current.is(TT_InheritanceColon))
1252
613
    State.Stack.back().Indent =
1253
613
        State.FirstIndent + Style.ConstructorInitializerIndentWidth;
1254
966k
  if (Current.isOneOf(TT_BinaryOperator, TT_ConditionalExpr) && 
Newline25.1k
)
1255
4.89k
    State.Stack.back().NestedBlockIndent =
1256
4.89k
        State.Column + Current.ColumnWidth + 1;
1257
966k
  if (Current.isOneOf(TT_LambdaLSquare, TT_LambdaArrow))
1258
1.79k
    State.Stack.back().LastSpace = State.Column;
1259
1260
  // Insert scopes created by fake parenthesis.
1261
966k
  const FormatToken *Previous = Current.getPreviousNonComment();
1262
1263
  // Add special behavior to support a format commonly used for JavaScript
1264
  // closures:
1265
  //   SomeFunction(function() {
1266
  //     foo();
1267
  //     bar();
1268
  //   }, a, b, c);
1269
966k
  if (Current.isNot(tok::comment) && 
Previous959k
&&
1270
907k
      Previous->isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) &&
1271
18.2k
      !Previous->is(TT_DictLiteral) && 
State.Stack.size() > 115.9k
&&
1272
15.9k
      !State.Stack.back().HasMultipleNestedBlocks) {
1273
15.9k
    if (State.Stack[State.Stack.size() - 2].NestedBlockInlined && 
Newline4.58k
)
1274
5.65k
      
for (unsigned i = 0, e = State.Stack.size() - 1; 1.67k
i != e;
++i3.98k
)
1275
3.98k
        State.Stack[i].NoLineBreak = true;
1276
15.9k
    State.Stack[State.Stack.size() - 2].NestedBlockInlined = false;
1277
15.9k
  }
1278
966k
  if (Previous &&
1279
912k
      (Previous->isOneOf(tok::l_paren, tok::comma, tok::colon) ||
1280
509k
       Previous->isOneOf(TT_BinaryOperator, TT_ConditionalExpr)) &&
1281
430k
      !Previous->isOneOf(TT_DictLiteral, TT_ObjCMethodExpr)) {
1282
420k
    State.Stack.back().NestedBlockInlined =
1283
420k
        !Newline && 
hasNestedBlockInlined(Previous, Current, Style)229k
;
1284
420k
  }
1285
1286
966k
  moveStatePastFakeLParens(State, Newline);
1287
966k
  moveStatePastScopeCloser(State);
1288
966k
  bool AllowBreak = !State.Stack.back().NoLineBreak &&
1289
942k
                    !State.Stack.back().NoLineBreakInOperand;
1290
966k
  moveStatePastScopeOpener(State, Newline);
1291
966k
  moveStatePastFakeRParens(State);
1292
1293
966k
  if (Current.is(TT_ObjCStringLiteral) && 
State.StartOfStringLiteral == 0513
)
1294
447
    State.StartOfStringLiteral = State.Column + 1;
1295
966k
  if (Current.is(TT_CSharpStringLiteral) && 
State.StartOfStringLiteral == 00
)
1296
0
    State.StartOfStringLiteral = State.Column + 1;
1297
966k
  else if (Current.isStringLiteral() && 
State.StartOfStringLiteral == 06.50k
)
1298
5.52k
    State.StartOfStringLiteral = State.Column;
1299
960k
  else if (!Current.isOneOf(tok::comment, tok::identifier, tok::hash) &&
1300
506k
           !Current.isStringLiteral())
1301
505k
    State.StartOfStringLiteral = 0;
1302
1303
966k
  State.Column += Current.ColumnWidth;
1304
966k
  State.NextToken = State.NextToken->Next;
1305
1306
966k
  unsigned Penalty =
1307
966k
      handleEndOfLine(Current, State, DryRun, AllowBreak, Newline);
1308
1309
966k
  if (Current.Role)
1310
72.3k
    Current.Role->formatFromToken(State, this, DryRun);
1311
  // If the previous has a special role, let it consume tokens as appropriate.
1312
  // It is necessary to start at the previous token for the only implemented
1313
  // role (comma separated list). That way, the decision whether or not to break
1314
  // after the "{" is already done and both options are tried and evaluated.
1315
  // FIXME: This is ugly, find a better way.
1316
966k
  if (Previous && 
Previous->Role912k
)
1317
130k
    Penalty += Previous->Role->formatAfterToken(State, this, DryRun);
1318
1319
966k
  return Penalty;
1320
966k
}
1321
1322
void ContinuationIndenter::moveStatePastFakeLParens(LineState &State,
1323
966k
                                                    bool Newline) {
1324
966k
  const FormatToken &Current = *State.NextToken;
1325
966k
  const FormatToken *Previous = Current.getPreviousNonComment();
1326
1327
  // Don't add extra indentation for the first fake parenthesis after
1328
  // 'return', assignments or opening <({[. The indentation for these cases
1329
  // is special cased.
1330
966k
  bool SkipFirstExtraIndent =
1331
966k
      (Previous && 
(912k
Previous->opensScope()912k
||
1332
619k
                    Previous->isOneOf(tok::semi, tok::kw_return) ||
1333
614k
                    (Previous->getPrecedence() == prec::Assignment &&
1334
12.4k
                     Style.AlignOperands != FormatStyle::OAS_DontAlign) ||
1335
603k
                    Previous->is(TT_ObjCMethodExpr)));
1336
966k
  for (SmallVectorImpl<prec::Level>::const_reverse_iterator
1337
966k
           I = Current.FakeLParens.rbegin(),
1338
966k
           E = Current.FakeLParens.rend();
1339
1.13M
       I != E; 
++I164k
) {
1340
164k
    ParenState NewParenState = State.Stack.back();
1341
164k
    NewParenState.Tok = nullptr;
1342
164k
    NewParenState.ContainsLineBreak = false;
1343
164k
    NewParenState.LastOperatorWrapped = true;
1344
164k
    NewParenState.IsChainedConditional = false;
1345
164k
    NewParenState.IsWrappedConditional = false;
1346
164k
    NewParenState.UnindentOperator = false;
1347
164k
    NewParenState.NoLineBreak =
1348
164k
        NewParenState.NoLineBreak || 
State.Stack.back().NoLineBreakInOperand161k
;
1349
1350
    // Don't propagate AvoidBinPacking into subexpressions of arg/param lists.
1351
164k
    if (*I > prec::Comma)
1352
24.3k
      NewParenState.AvoidBinPacking = false;
1353
1354
    // Indent from 'LastSpace' unless these are fake parentheses encapsulating
1355
    // a builder type call after 'return' or, if the alignment after opening
1356
    // brackets is disabled.
1357
164k
    if (!Current.isTrailingComment() &&
1358
164k
        (Style.AlignOperands != FormatStyle::OAS_DontAlign ||
1359
4.40k
         *I < prec::Assignment) &&
1360
162k
        (!Previous || 
Previous->isNot(tok::kw_return)156k
||
1361
722
         (Style.Language != FormatStyle::LK_Java && 
*I > 0714
)) &&
1362
162k
        (Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign ||
1363
161k
         
*I != prec::Comma545
||
Current.NestingLevel == 0309
)) {
1364
161k
      NewParenState.Indent =
1365
161k
          std::max(std::max(State.Column, NewParenState.Indent),
1366
161k
                   State.Stack.back().LastSpace);
1367
161k
    }
1368
1369
164k
    if (Previous &&
1370
157k
        (Previous->getPrecedence() == prec::Assignment ||
1371
155k
         Previous->is(tok::kw_return) ||
1372
154k
         (*I == prec::Conditional && 
Previous->is(tok::question)1.98k
&&
1373
69
          Previous->is(TT_ConditionalExpr))) &&
1374
3.37k
        !Newline) {
1375
      // If BreakBeforeBinaryOperators is set, un-indent a bit to account for
1376
      // the operator and keep the operands aligned
1377
2.61k
      if (Style.AlignOperands == FormatStyle::OAS_AlignAfterOperator)
1378
129
        NewParenState.UnindentOperator = true;
1379
      // Mark indentation as alignment if the expression is aligned.
1380
2.61k
      if (Style.AlignOperands != FormatStyle::OAS_DontAlign)
1381
2.22k
        NewParenState.IsAligned = true;
1382
2.61k
    }
1383
1384
    // Do not indent relative to the fake parentheses inserted for "." or "->".
1385
    // This is a special case to make the following to statements consistent:
1386
    //   OuterFunction(InnerFunctionCall( // break
1387
    //       ParameterToInnerFunction));
1388
    //   OuterFunction(SomeObject.InnerFunctionCall( // break
1389
    //       ParameterToInnerFunction));
1390
164k
    if (*I > prec::Unknown)
1391
155k
      NewParenState.LastSpace = std::max(NewParenState.LastSpace, State.Column);
1392
164k
    if (*I != prec::Conditional && 
!Current.is(TT_UnaryOperator)162k
&&
1393
159k
        Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign)
1394
158k
      NewParenState.StartOfFunctionCall = State.Column;
1395
1396
    // Indent conditional expressions, unless they are chained "else-if"
1397
    // conditionals. Never indent expression where the 'operator' is ',', ';' or
1398
    // an assignment (i.e. *I <= prec::Assignment) as those have different
1399
    // indentation rules. Indent other expression, unless the indentation needs
1400
    // to be skipped.
1401
164k
    if (*I == prec::Conditional && 
Previous2.57k
&&
Previous->is(tok::colon)2.54k
&&
1402
979
        Previous->is(TT_ConditionalExpr) && 
I == Current.FakeLParens.rbegin()953
&&
1403
953
        !State.Stack.back().IsWrappedConditional) {
1404
923
      NewParenState.IsChainedConditional = true;
1405
923
      NewParenState.UnindentOperator = State.Stack.back().UnindentOperator;
1406
163k
    } else if (*I == prec::Conditional ||
1407
162k
               (!SkipFirstExtraIndent && 
*I > prec::Assignment21.2k
&&
1408
4.43k
                
!Current.isTrailingComment()2.78k
)) {
1409
4.43k
      NewParenState.Indent += Style.ContinuationIndentWidth;
1410
4.43k
    }
1411
164k
    if ((Previous && 
!Previous->opensScope()157k
) ||
*I != prec::Comma147k
)
1412
33.7k
      NewParenState.BreakBeforeParameter = false;
1413
164k
    State.Stack.push_back(NewParenState);
1414
164k
    SkipFirstExtraIndent = false;
1415
164k
  }
1416
966k
}
1417
1418
966k
void ContinuationIndenter::moveStatePastFakeRParens(LineState &State) {
1419
1.04M
  for (unsigned i = 0, e = State.NextToken->FakeRParens; i != e; 
++i82.1k
) {
1420
82.1k
    unsigned VariablePos = State.Stack.back().VariablePos;
1421
82.1k
    if (State.Stack.size() == 1) {
1422
      // Do not pop the last element.
1423
3
      break;
1424
3
    }
1425
82.1k
    State.Stack.pop_back();
1426
82.1k
    State.Stack.back().VariablePos = VariablePos;
1427
82.1k
  }
1428
966k
}
1429
1430
void ContinuationIndenter::moveStatePastScopeOpener(LineState &State,
1431
966k
                                                    bool Newline) {
1432
966k
  const FormatToken &Current = *State.NextToken;
1433
966k
  if (!Current.opensScope())
1434
782k
    return;
1435
1436
  // Don't allow '<' or '(' in C# generic type constraints to start new scopes.
1437
184k
  if (Current.isOneOf(tok::less, tok::l_paren) &&
1438
154k
      State.Stack.back().IsCSharpGenericTypeConstraint)
1439
30
    return;
1440
1441
184k
  if (Current.MatchingParen && 
Current.is(BK_Block)174k
) {
1442
3.64k
    moveStateToNewBlock(State);
1443
3.64k
    return;
1444
3.64k
  }
1445
1446
180k
  unsigned NewIndent;
1447
180k
  unsigned LastSpace = State.Stack.back().LastSpace;
1448
180k
  bool AvoidBinPacking;
1449
180k
  bool BreakBeforeParameter = false;
1450
180k
  unsigned NestedBlockIndent = std::max(State.Stack.back().StartOfFunctionCall,
1451
180k
                                        State.Stack.back().NestedBlockIndent);
1452
180k
  if (Current.isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) ||
1453
160k
      opensProtoMessageField(Current, Style)) {
1454
21.3k
    if (Current.opensBlockOrBlockTypeList(Style)) {
1455
13.4k
      NewIndent = Style.IndentWidth +
1456
13.4k
                  std::min(State.Column, State.Stack.back().NestedBlockIndent);
1457
7.90k
    } else {
1458
7.90k
      NewIndent = State.Stack.back().LastSpace + Style.ContinuationIndentWidth;
1459
7.90k
    }
1460
21.3k
    const FormatToken *NextNoComment = Current.getNextNonComment();
1461
21.3k
    bool EndsInComma = Current.MatchingParen &&
1462
12.0k
                       Current.MatchingParen->Previous &&
1463
12.0k
                       Current.MatchingParen->Previous->is(tok::comma);
1464
21.3k
    AvoidBinPacking = EndsInComma || 
Current.is(TT_DictLiteral)20.6k
||
1465
18.1k
                      Style.Language == FormatStyle::LK_Proto ||
1466
17.6k
                      Style.Language == FormatStyle::LK_TextProto ||
1467
17.3k
                      !Style.BinPackArguments ||
1468
17.1k
                      (NextNoComment &&
1469
12.0k
                       NextNoComment->isOneOf(TT_DesignatedInitializerPeriod,
1470
12.0k
                                              TT_DesignatedInitializerLSquare));
1471
21.3k
    BreakBeforeParameter = EndsInComma;
1472
21.3k
    if (Current.ParameterCount > 1)
1473
8.01k
      NestedBlockIndent = std::max(NestedBlockIndent, State.Column + 1);
1474
159k
  } else {
1475
159k
    NewIndent = Style.ContinuationIndentWidth +
1476
159k
                std::max(State.Stack.back().LastSpace,
1477
159k
                         State.Stack.back().StartOfFunctionCall);
1478
1479
    // Ensure that different different brackets force relative alignment, e.g.:
1480
    // void SomeFunction(vector<  // break
1481
    //                       int> v);
1482
    // FIXME: We likely want to do this for more combinations of brackets.
1483
159k
    if (Current.is(tok::less) && 
Current.ParentBracket == tok::l_paren4.57k
) {
1484
399
      NewIndent = std::max(NewIndent, State.Stack.back().Indent);
1485
399
      LastSpace = std::max(LastSpace, State.Stack.back().Indent);
1486
399
    }
1487
1488
159k
    bool EndsInComma =
1489
159k
        Current.MatchingParen &&
1490
159k
        Current.MatchingParen->getPreviousNonComment() &&
1491
159k
        Current.MatchingParen->getPreviousNonComment()->is(tok::comma);
1492
1493
    // If ObjCBinPackProtocolList is unspecified, fall back to BinPackParameters
1494
    // for backwards compatibility.
1495
159k
    bool ObjCBinPackProtocolList =
1496
159k
        (Style.ObjCBinPackProtocolList == FormatStyle::BPS_Auto &&
1497
152k
         Style.BinPackParameters) ||
1498
8.63k
        Style.ObjCBinPackProtocolList == FormatStyle::BPS_Always;
1499
1500
159k
    bool BinPackDeclaration =
1501
159k
        (State.Line->Type != LT_ObjCDecl && 
Style.BinPackParameters159k
) ||
1502
1.82k
        (State.Line->Type == LT_ObjCDecl && 
ObjCBinPackProtocolList138
);
1503
1504
159k
    AvoidBinPacking =
1505
159k
        (State.Stack.back().IsCSharpGenericTypeConstraint) ||
1506
159k
        (Style.Language == FormatStyle::LK_JavaScript && 
EndsInComma3.07k
) ||
1507
159k
        (State.Line->MustBeDeclaration && 
!BinPackDeclaration151k
) ||
1508
157k
        (!State.Line->MustBeDeclaration && 
!Style.BinPackArguments7.76k
) ||
1509
157k
        (Style.ExperimentalAutoDetectBinPacking &&
1510
8
         (Current.is(PPK_OnePerLine) ||
1511
6
          (!BinPackInconclusiveFunctions && 
Current.is(PPK_Inconclusive)2
)));
1512
1513
159k
    if (Current.is(TT_ObjCMethodExpr) && 
Current.MatchingParen962
&&
1514
961
        Style.ObjCBreakBeforeNestedBlockParam) {
1515
929
      if (Style.ColumnLimit) {
1516
        // If this '[' opens an ObjC call, determine whether all parameters fit
1517
        // into one line and put one per line if they don't.
1518
892
        if (getLengthToMatchingParen(Current, State.Stack) + State.Column >
1519
892
            getColumnLimit(State))
1520
246
          BreakBeforeParameter = true;
1521
37
      } else {
1522
        // For ColumnLimit = 0, we have to figure out whether there is or has to
1523
        // be a line break within this call.
1524
37
        for (const FormatToken *Tok = &Current;
1525
301
             Tok && Tok != Current.MatchingParen; 
Tok = Tok->Next264
) {
1526
279
          if (Tok->MustBreakBefore ||
1527
271
              (Tok->CanBreakBefore && 
Tok->NewlinesBefore > 0134
)) {
1528
15
            BreakBeforeParameter = true;
1529
15
            break;
1530
15
          }
1531
279
        }
1532
37
      }
1533
929
    }
1534
1535
159k
    if (Style.Language == FormatStyle::LK_JavaScript && 
EndsInComma3.07k
)
1536
16
      BreakBeforeParameter = true;
1537
159k
  }
1538
  // Generally inherit NoLineBreak from the current scope to nested scope.
1539
  // However, don't do this for non-empty nested blocks, dict literals and
1540
  // array literals as these follow different indentation rules.
1541
180k
  bool NoLineBreak =
1542
180k
      Current.Children.empty() &&
1543
180k
      !Current.isOneOf(TT_DictLiteral, TT_ArrayInitializerLSquare) &&
1544
177k
      (State.Stack.back().NoLineBreak ||
1545
174k
       State.Stack.back().NoLineBreakInOperand ||
1546
174k
       (Current.is(TT_TemplateOpener) &&
1547
4.72k
        State.Stack.back().ContainsUnwrappedBuilder));
1548
180k
  State.Stack.push_back(
1549
180k
      ParenState(&Current, NewIndent, LastSpace, AvoidBinPacking, NoLineBreak));
1550
180k
  State.Stack.back().NestedBlockIndent = NestedBlockIndent;
1551
180k
  State.Stack.back().BreakBeforeParameter = BreakBeforeParameter;
1552
180k
  State.Stack.back().HasMultipleNestedBlocks =
1553
180k
      (Current.BlockParameterCount > 1);
1554
1555
180k
  if (Style.BraceWrapping.BeforeLambdaBody && 
Current.Next != nullptr1.94k
&&
1556
1.64k
      Current.Tok.is(tok::l_paren)) {
1557
    // Search for any parameter that is a lambda
1558
1.10k
    FormatToken const *next = Current.Next;
1559
5.27k
    while (next != nullptr) {
1560
4.38k
      if (next->is(TT_LambdaLSquare)) {
1561
219
        State.Stack.back().HasMultipleNestedBlocks = true;
1562
219
        break;
1563
219
      }
1564
4.17k
      next = next->Next;
1565
4.17k
    }
1566
1.10k
  }
1567
1568
180k
  State.Stack.back().IsInsideObjCArrayLiteral =
1569
180k
      Current.is(TT_ArrayInitializerLSquare) && 
Current.Previous901
&&
1570
901
      Current.Previous->is(tok::at);
1571
180k
}
1572
1573
966k
void ContinuationIndenter::moveStatePastScopeCloser(LineState &State) {
1574
966k
  const FormatToken &Current = *State.NextToken;
1575
966k
  if (!Current.closesScope())
1576
882k
    return;
1577
1578
  // If we encounter a closing ), ], } or >, we can remove a level from our
1579
  // stacks.
1580
83.6k
  if (State.Stack.size() > 1 &&
1581
78.8k
      (Current.isOneOf(tok::r_paren, tok::r_square, TT_TemplateString) ||
1582
22.8k
       (Current.is(tok::r_brace) && 
State.NextToken != State.Line->First17.1k
) ||
1583
5.66k
       State.NextToken->is(TT_TemplateCloser) ||
1584
824
       (Current.is(tok::greater) && 
Current.is(TT_DictLiteral)803
)))
1585
78.8k
    State.Stack.pop_back();
1586
1587
  // Reevaluate whether ObjC message arguments fit into one line.
1588
  // If a receiver spans multiple lines, e.g.:
1589
  //   [[object block:^{
1590
  //     return 42;
1591
  //   }] a:42 b:42];
1592
  // BreakBeforeParameter is calculated based on an incorrect assumption
1593
  // (it is checked whether the whole expression fits into one line without
1594
  // considering a line break inside a message receiver).
1595
  // We check whether arguements fit after receiver scope closer (into the same
1596
  // line).
1597
83.6k
  if (State.Stack.back().BreakBeforeParameter && 
Current.MatchingParen27.7k
&&
1598
25.9k
      Current.MatchingParen->Previous) {
1599
25.5k
    const FormatToken &CurrentScopeOpener = *Current.MatchingParen->Previous;
1600
25.5k
    if (CurrentScopeOpener.is(TT_ObjCMethodExpr) &&
1601
216
        CurrentScopeOpener.MatchingParen) {
1602
87
      int NecessarySpaceInLine =
1603
87
          getLengthToMatchingParen(CurrentScopeOpener, State.Stack) +
1604
87
          CurrentScopeOpener.TotalLength - Current.TotalLength - 1;
1605
87
      if (State.Column + Current.ColumnWidth + NecessarySpaceInLine <=
1606
87
          Style.ColumnLimit)
1607
12
        State.Stack.back().BreakBeforeParameter = false;
1608
87
    }
1609
25.5k
  }
1610
1611
83.6k
  if (Current.is(tok::r_square)) {
1612
    // If this ends the array subscript expr, reset the corresponding value.
1613
5.31k
    const FormatToken *NextNonComment = Current.getNextNonComment();
1614
5.31k
    if (NextNonComment && 
NextNonComment->isNot(tok::l_square)5.20k
)
1615
5.07k
      State.Stack.back().StartOfArraySubscripts = 0;
1616
5.31k
  }
1617
83.6k
}
1618
1619
3.64k
void ContinuationIndenter::moveStateToNewBlock(LineState &State) {
1620
3.64k
  unsigned NestedBlockIndent = State.Stack.back().NestedBlockIndent;
1621
  // ObjC block sometimes follow special indentation rules.
1622
3.64k
  unsigned NewIndent =
1623
3.64k
      NestedBlockIndent + (State.NextToken->is(TT_ObjCBlockLBrace)
1624
380
                               ? Style.ObjCBlockIndentWidth
1625
3.26k
                               : Style.IndentWidth);
1626
3.64k
  State.Stack.push_back(ParenState(State.NextToken, NewIndent,
1627
3.64k
                                   State.Stack.back().LastSpace,
1628
3.64k
                                   /*AvoidBinPacking=*/true,
1629
3.64k
                                   /*NoLineBreak=*/false));
1630
3.64k
  State.Stack.back().NestedBlockIndent = NestedBlockIndent;
1631
3.64k
  State.Stack.back().BreakBeforeParameter = true;
1632
3.64k
}
1633
1634
static unsigned getLastLineEndColumn(StringRef Text, unsigned StartColumn,
1635
                                     unsigned TabWidth,
1636
340
                                     encoding::Encoding Encoding) {
1637
340
  size_t LastNewlinePos = Text.find_last_of("\n");
1638
340
  if (LastNewlinePos == StringRef::npos) {
1639
186
    return StartColumn +
1640
186
           encoding::columnWidthWithTabs(Text, StartColumn, TabWidth, Encoding);
1641
154
  } else {
1642
154
    return encoding::columnWidthWithTabs(Text.substr(LastNewlinePos),
1643
154
                                         /*StartColumn=*/0, TabWidth, Encoding);
1644
154
  }
1645
340
}
1646
1647
unsigned ContinuationIndenter::reformatRawStringLiteral(
1648
    const FormatToken &Current, LineState &State,
1649
340
    const FormatStyle &RawStringStyle, bool DryRun, bool Newline) {
1650
340
  unsigned StartColumn = State.Column - Current.ColumnWidth;
1651
340
  StringRef OldDelimiter = *getRawStringDelimiter(Current.TokenText);
1652
340
  StringRef NewDelimiter =
1653
340
      getCanonicalRawStringDelimiter(Style, RawStringStyle.Language);
1654
340
  if (NewDelimiter.empty() || 
OldDelimiter.empty()4
)
1655
336
    NewDelimiter = OldDelimiter;
1656
  // The text of a raw string is between the leading 'R"delimiter(' and the
1657
  // trailing 'delimiter)"'.
1658
340
  unsigned OldPrefixSize = 3 + OldDelimiter.size();
1659
340
  unsigned OldSuffixSize = 2 + OldDelimiter.size();
1660
  // We create a virtual text environment which expects a null-terminated
1661
  // string, so we cannot use StringRef.
1662
340
  std::string RawText = std::string(
1663
340
      Current.TokenText.substr(OldPrefixSize).drop_back(OldSuffixSize));
1664
340
  if (NewDelimiter != OldDelimiter) {
1665
    // Don't update to the canonical delimiter 'deli' if ')deli"' occurs in the
1666
    // raw string.
1667
4
    std::string CanonicalDelimiterSuffix = (")" + NewDelimiter + "\"").str();
1668
4
    if (StringRef(RawText).contains(CanonicalDelimiterSuffix))
1669
3
      NewDelimiter = OldDelimiter;
1670
4
  }
1671
1672
340
  unsigned NewPrefixSize = 3 + NewDelimiter.size();
1673
340
  unsigned NewSuffixSize = 2 + NewDelimiter.size();
1674
1675
  // The first start column is the column the raw text starts after formatting.
1676
340
  unsigned FirstStartColumn = StartColumn + NewPrefixSize;
1677
1678
  // The next start column is the intended indentation a line break inside
1679
  // the raw string at level 0. It is determined by the following rules:
1680
  //   - if the content starts on newline, it is one level more than the current
1681
  //     indent, and
1682
  //   - if the content does not start on a newline, it is the first start
1683
  //     column.
1684
  // These rules have the advantage that the formatted content both does not
1685
  // violate the rectangle rule and visually flows within the surrounding
1686
  // source.
1687
340
  bool ContentStartsOnNewline = Current.TokenText[OldPrefixSize] == '\n';
1688
  // If this token is the last parameter (checked by looking if it's followed by
1689
  // `)` and is not on a newline, the base the indent off the line's nested
1690
  // block indent. Otherwise, base the indent off the arguments indent, so we
1691
  // can achieve:
1692
  //
1693
  // fffffffffff(1, 2, 3, R"pb(
1694
  //     key1: 1  #
1695
  //     key2: 2)pb");
1696
  //
1697
  // fffffffffff(1, 2, 3,
1698
  //             R"pb(
1699
  //               key1: 1  #
1700
  //               key2: 2
1701
  //             )pb");
1702
  //
1703
  // fffffffffff(1, 2, 3,
1704
  //             R"pb(
1705
  //               key1: 1  #
1706
  //               key2: 2
1707
  //             )pb",
1708
  //             5);
1709
340
  unsigned CurrentIndent =
1710
340
      (!Newline && 
Current.Next199
&&
Current.Next->is(tok::r_paren)196
)
1711
82
          ? State.Stack.back().NestedBlockIndent
1712
258
          : State.Stack.back().Indent;
1713
340
  unsigned NextStartColumn = ContentStartsOnNewline
1714
76
                                 ? CurrentIndent + Style.IndentWidth
1715
264
                                 : FirstStartColumn;
1716
1717
  // The last start column is the column the raw string suffix starts if it is
1718
  // put on a newline.
1719
  // The last start column is the intended indentation of the raw string postfix
1720
  // if it is put on a newline. It is determined by the following rules:
1721
  //   - if the raw string prefix starts on a newline, it is the column where
1722
  //     that raw string prefix starts, and
1723
  //   - if the raw string prefix does not start on a newline, it is the current
1724
  //     indent.
1725
340
  unsigned LastStartColumn =
1726
330
      Current.NewlinesBefore ? 
FirstStartColumn - NewPrefixSize10
: CurrentIndent;
1727
1728
340
  std::pair<tooling::Replacements, unsigned> Fixes = internal::reformat(
1729
340
      RawStringStyle, RawText, {tooling::Range(0, RawText.size())},
1730
340
      FirstStartColumn, NextStartColumn, LastStartColumn, "<stdin>",
1731
340
      /*Status=*/nullptr);
1732
1733
340
  auto NewCode = applyAllReplacements(RawText, Fixes.first);
1734
340
  tooling::Replacements NoFixes;
1735
340
  if (!NewCode) {
1736
0
    return addMultilineToken(Current, State);
1737
0
  }
1738
340
  if (!DryRun) {
1739
100
    if (NewDelimiter != OldDelimiter) {
1740
      // In 'R"delimiter(...', the delimiter starts 2 characters after the start
1741
      // of the token.
1742
1
      SourceLocation PrefixDelimiterStart =
1743
1
          Current.Tok.getLocation().getLocWithOffset(2);
1744
1
      auto PrefixErr = Whitespaces.addReplacement(tooling::Replacement(
1745
1
          SourceMgr, PrefixDelimiterStart, OldDelimiter.size(), NewDelimiter));
1746
1
      if (PrefixErr) {
1747
0
        llvm::errs()
1748
0
            << "Failed to update the prefix delimiter of a raw string: "
1749
0
            << llvm::toString(std::move(PrefixErr)) << "\n";
1750
0
      }
1751
      // In 'R"delimiter(...)delimiter"', the suffix delimiter starts at
1752
      // position length - 1 - |delimiter|.
1753
1
      SourceLocation SuffixDelimiterStart =
1754
1
          Current.Tok.getLocation().getLocWithOffset(Current.TokenText.size() -
1755
1
                                                     1 - OldDelimiter.size());
1756
1
      auto SuffixErr = Whitespaces.addReplacement(tooling::Replacement(
1757
1
          SourceMgr, SuffixDelimiterStart, OldDelimiter.size(), NewDelimiter));
1758
1
      if (SuffixErr) {
1759
0
        llvm::errs()
1760
0
            << "Failed to update the suffix delimiter of a raw string: "
1761
0
            << llvm::toString(std::move(SuffixErr)) << "\n";
1762
0
      }
1763
1
    }
1764
100
    SourceLocation OriginLoc =
1765
100
        Current.Tok.getLocation().getLocWithOffset(OldPrefixSize);
1766
191
    for (const tooling::Replacement &Fix : Fixes.first) {
1767
191
      auto Err = Whitespaces.addReplacement(tooling::Replacement(
1768
191
          SourceMgr, OriginLoc.getLocWithOffset(Fix.getOffset()),
1769
191
          Fix.getLength(), Fix.getReplacementText()));
1770
191
      if (Err) {
1771
0
        llvm::errs() << "Failed to reformat raw string: "
1772
0
                     << llvm::toString(std::move(Err)) << "\n";
1773
0
      }
1774
191
    }
1775
100
  }
1776
340
  unsigned RawLastLineEndColumn = getLastLineEndColumn(
1777
340
      *NewCode, FirstStartColumn, Style.TabWidth, Encoding);
1778
340
  State.Column = RawLastLineEndColumn + NewSuffixSize;
1779
  // Since we're updating the column to after the raw string literal here, we
1780
  // have to manually add the penalty for the prefix R"delim( over the column
1781
  // limit.
1782
340
  unsigned PrefixExcessCharacters =
1783
340
      StartColumn + NewPrefixSize > Style.ColumnLimit
1784
5
          ? StartColumn + NewPrefixSize - Style.ColumnLimit
1785
335
          : 0;
1786
340
  bool IsMultiline =
1787
340
      ContentStartsOnNewline || 
(NewCode->find('\n') != std::string::npos)264
;
1788
340
  if (IsMultiline) {
1789
    // Break before further function parameters on all levels.
1790
636
    for (unsigned i = 0, e = State.Stack.size(); i != e; 
++i482
)
1791
482
      State.Stack[i].BreakBeforeParameter = true;
1792
154
  }
1793
340
  return Fixes.second + PrefixExcessCharacters * Style.PenaltyExcessCharacter;
1794
340
}
1795
1796
unsigned ContinuationIndenter::addMultilineToken(const FormatToken &Current,
1797
114
                                                 LineState &State) {
1798
  // Break before further function parameters on all levels.
1799
405
  for (unsigned i = 0, e = State.Stack.size(); i != e; 
++i291
)
1800
291
    State.Stack[i].BreakBeforeParameter = true;
1801
1802
114
  unsigned ColumnsUsed = State.Column;
1803
  // We can only affect layout of the first and the last line, so the penalty
1804
  // for all other lines is constant, and we ignore it.
1805
114
  State.Column = Current.LastLineColumnWidth;
1806
1807
114
  if (ColumnsUsed > getColumnLimit(State))
1808
8
    return Style.PenaltyExcessCharacter * (ColumnsUsed - getColumnLimit(State));
1809
106
  return 0;
1810
106
}
1811
1812
unsigned ContinuationIndenter::handleEndOfLine(const FormatToken &Current,
1813
                                               LineState &State, bool DryRun,
1814
966k
                                               bool AllowBreak, bool Newline) {
1815
966k
  unsigned Penalty = 0;
1816
  // Compute the raw string style to use in case this is a raw string literal
1817
  // that can be reformatted.
1818
966k
  auto RawStringStyle = getRawStringStyle(Current, State);
1819
966k
  if (RawStringStyle && 
!Current.Finalized341
) {
1820
340
    Penalty = reformatRawStringLiteral(Current, State, *RawStringStyle, DryRun,
1821
340
                                       Newline);
1822
966k
  } else if (Current.IsMultiline && 
Current.isNot(TT_BlockComment)381
) {
1823
    // Don't break multi-line tokens other than block comments and raw string
1824
    // literals. Instead, just update the state.
1825
114
    Penalty = addMultilineToken(Current, State);
1826
966k
  } else if (State.Line->Type != LT_ImportStatement) {
1827
    // We generally don't break import statements.
1828
961k
    LineState OriginalState = State;
1829
1830
    // Whether we force the reflowing algorithm to stay strictly within the
1831
    // column limit.
1832
961k
    bool Strict = false;
1833
    // Whether the first non-strict attempt at reflowing did intentionally
1834
    // exceed the column limit.
1835
961k
    bool Exceeded = false;
1836
961k
    std::tie(Penalty, Exceeded) = breakProtrudingToken(
1837
961k
        Current, State, AllowBreak, /*DryRun=*/true, Strict);
1838
961k
    if (Exceeded) {
1839
      // If non-strict reflowing exceeds the column limit, try whether strict
1840
      // reflowing leads to an overall lower penalty.
1841
22
      LineState StrictState = OriginalState;
1842
22
      unsigned StrictPenalty =
1843
22
          breakProtrudingToken(Current, StrictState, AllowBreak,
1844
22
                               /*DryRun=*/true, /*Strict=*/true)
1845
22
              .first;
1846
22
      Strict = StrictPenalty <= Penalty;
1847
22
      if (Strict) {
1848
1
        Penalty = StrictPenalty;
1849
1
        State = StrictState;
1850
1
      }
1851
22
    }
1852
961k
    if (!DryRun) {
1853
      // If we're not in dry-run mode, apply the changes with the decision on
1854
      // strictness made above.
1855
248k
      breakProtrudingToken(Current, OriginalState, AllowBreak, /*DryRun=*/false,
1856
248k
                           Strict);
1857
248k
    }
1858
961k
  }
1859
966k
  if (State.Column > getColumnLimit(State)) {
1860
57.6k
    unsigned ExcessCharacters = State.Column - getColumnLimit(State);
1861
57.6k
    Penalty += Style.PenaltyExcessCharacter * ExcessCharacters;
1862
57.6k
  }
1863
966k
  return Penalty;
1864
966k
}
1865
1866
// Returns the enclosing function name of a token, or the empty string if not
1867
// found.
1868
14
static StringRef getEnclosingFunctionName(const FormatToken &Current) {
1869
  // Look for: 'function(' or 'function<templates>(' before Current.
1870
14
  auto Tok = Current.getPreviousNonComment();
1871
14
  if (!Tok || !Tok->is(tok::l_paren))
1872
0
    return "";
1873
14
  Tok = Tok->getPreviousNonComment();
1874
14
  if (!Tok)
1875
0
    return "";
1876
14
  if (Tok->is(TT_TemplateCloser)) {
1877
5
    Tok = Tok->MatchingParen;
1878
5
    if (Tok)
1879
5
      Tok = Tok->getPreviousNonComment();
1880
5
  }
1881
14
  if (!Tok || !Tok->is(tok::identifier))
1882
0
    return "";
1883
14
  return Tok->TokenText;
1884
14
}
1885
1886
llvm::Optional<FormatStyle>
1887
ContinuationIndenter::getRawStringStyle(const FormatToken &Current,
1888
966k
                                        const LineState &State) {
1889
966k
  if (!Current.isStringLiteral())
1890
959k
    return None;
1891
6.50k
  auto Delimiter = getRawStringDelimiter(Current.TokenText);
1892
6.50k
  if (!Delimiter)
1893
6.13k
    return None;
1894
364
  auto RawStringStyle = RawStringFormats.getDelimiterStyle(*Delimiter);
1895
364
  if (!RawStringStyle && 
Delimiter->empty()36
)
1896
14
    RawStringStyle = RawStringFormats.getEnclosingFunctionStyle(
1897
14
        getEnclosingFunctionName(Current));
1898
364
  if (!RawStringStyle)
1899
23
    return None;
1900
341
  RawStringStyle->ColumnLimit = getColumnLimit(State);
1901
341
  return RawStringStyle;
1902
341
}
1903
1904
std::unique_ptr<BreakableToken>
1905
ContinuationIndenter::createBreakableToken(const FormatToken &Current,
1906
1.20M
                                           LineState &State, bool AllowBreak) {
1907
1.20M
  unsigned StartColumn = State.Column - Current.ColumnWidth;
1908
1.20M
  if (Current.isStringLiteral()) {
1909
    // FIXME: String literal breaking is currently disabled for C#, Java and
1910
    // JavaScript, as it requires strings to be merged using "+" which we
1911
    // don't support.
1912
7.13k
    if (Style.Language == FormatStyle::LK_Java ||
1913
6.92k
        Style.Language == FormatStyle::LK_JavaScript || 
Style.isCSharp()5.74k
||
1914
5.51k
        !Style.BreakStringLiterals || 
!AllowBreak4.08k
)
1915
3.51k
      return nullptr;
1916
1917
    // Don't break string literals inside preprocessor directives (except for
1918
    // #define directives, as their contents are stored in separate lines and
1919
    // are not affected by this check).
1920
    // This way we avoid breaking code with line directives and unknown
1921
    // preprocessor directives that contain long string literals.
1922
3.62k
    if (State.Line->Type == LT_PreprocessorDirective)
1923
46
      return nullptr;
1924
    // Exempts unterminated string literals from line breaking. The user will
1925
    // likely want to terminate the string before any line breaking is done.
1926
3.57k
    if (Current.IsUnterminatedLiteral)
1927
15
      return nullptr;
1928
    // Don't break string literals inside Objective-C array literals (doing so
1929
    // raises the warning -Wobjc-string-concatenation).
1930
3.56k
    if (State.Stack.back().IsInsideObjCArrayLiteral) {
1931
210
      return nullptr;
1932
210
    }
1933
1934
3.35k
    StringRef Text = Current.TokenText;
1935
3.35k
    StringRef Prefix;
1936
3.35k
    StringRef Postfix;
1937
    // FIXME: Handle whitespace between '_T', '(', '"..."', and ')'.
1938
    // FIXME: Store Prefix and Suffix (or PrefixLength and SuffixLength to
1939
    // reduce the overhead) for each FormatToken, which is a string, so that we
1940
    // don't run multiple checks here on the hot path.
1941
3.35k
    if ((Text.endswith(Postfix = "\"") &&
1942
3.33k
         (Text.startswith(Prefix = "@\"") || 
Text.startswith(Prefix = "\"")2.93k
||
1943
93
          Text.startswith(Prefix = "u\"") || 
Text.startswith(Prefix = "U\"")91
||
1944
89
          Text.startswith(Prefix = "u8\"") ||
1945
87
          Text.startswith(Prefix = "L\""))) ||
1946
3.33k
        
(32
Text.startswith(Prefix = "_T(\"")32
&&
Text.endswith(Postfix = "\")")12
)) {
1947
      // We need this to address the case where there is an unbreakable tail
1948
      // only if certain other formatting decisions have been taken. The
1949
      // UnbreakableTailLength of Current is an overapproximation is that case
1950
      // and we need to be correct here.
1951
3.33k
      unsigned UnbreakableTailLength = (State.NextToken && 
canBreak(State)3.12k
)
1952
882
                                           ? 0
1953
2.44k
                                           : Current.UnbreakableTailLength;
1954
3.33k
      return std::make_unique<BreakableStringLiteral>(
1955
3.33k
          Current, StartColumn, Prefix, Postfix, UnbreakableTailLength,
1956
3.33k
          State.Line->InPPDirective, Encoding, Style);
1957
3.33k
    }
1958
1.20M
  } else if (Current.is(TT_BlockComment)) {
1959
1.54k
    if (!Style.ReflowComments ||
1960
        // If a comment token switches formatting, like
1961
        // /* clang-format on */, we don't want to break it further,
1962
        // but we may still want to adjust its indentation.
1963
1.53k
        switchesFormatting(Current)) {
1964
20
      return nullptr;
1965
20
    }
1966
1.52k
    return std::make_unique<BreakableBlockComment>(
1967
1.52k
        Current, StartColumn, Current.OriginalColumn, !Current.Previous,
1968
1.52k
        State.Line->InPPDirective, Encoding, Style, Whitespaces.useCRLF());
1969
1.20M
  } else if (Current.is(TT_LineComment) &&
1970
8.06k
             (Current.Previous == nullptr ||
1971
8.06k
              
Current.Previous->isNot(TT_ImplicitStringLiteral)6.37k
)) {
1972
8.06k
    if (!Style.ReflowComments ||
1973
8.06k
        CommentPragmasRegex.match(Current.TokenText.substr(2)) ||
1974
8.04k
        switchesFormatting(Current))
1975
59
      return nullptr;
1976
8.00k
    return std::make_unique<BreakableLineCommentSection>(
1977
8.00k
        Current, StartColumn, Current.OriginalColumn, !Current.Previous,
1978
8.00k
        /*InPPDirective=*/false, Encoding, Style);
1979
8.00k
  }
1980
1.19M
  return nullptr;
1981
1.19M
}
1982
1983
std::pair<unsigned, bool>
1984
ContinuationIndenter::breakProtrudingToken(const FormatToken &Current,
1985
                                           LineState &State, bool AllowBreak,
1986
1.20M
                                           bool DryRun, bool Strict) {
1987
1.20M
  std::unique_ptr<const BreakableToken> Token =
1988
1.20M
      createBreakableToken(Current, State, AllowBreak);
1989
1.20M
  if (!Token)
1990
1.19M
    return {0, false};
1991
12.8k
  assert(Token->getLineCount() > 0);
1992
12.8k
  unsigned ColumnLimit = getColumnLimit(State);
1993
12.8k
  if (Current.is(TT_LineComment)) {
1994
    // We don't insert backslashes when breaking line comments.
1995
8.00k
    ColumnLimit = Style.ColumnLimit;
1996
8.00k
  }
1997
12.8k
  if (Current.UnbreakableTailLength >= ColumnLimit)
1998
340
    return {0, false};
1999
  // ColumnWidth was already accounted into State.Column before calling
2000
  // breakProtrudingToken.
2001
12.5k
  unsigned StartColumn = State.Column - Current.ColumnWidth;
2002
12.5k
  unsigned NewBreakPenalty = Current.isStringLiteral()
2003
3.30k
                                 ? Style.PenaltyBreakString
2004
9.21k
                                 : Style.PenaltyBreakComment;
2005
  // Stores whether we intentionally decide to let a line exceed the column
2006
  // limit.
2007
12.5k
  bool Exceeded = false;
2008
  // Stores whether we introduce a break anywhere in the token.
2009
12.5k
  bool BreakInserted = Token->introducesBreakBeforeToken();
2010
  // Store whether we inserted a new line break at the end of the previous
2011
  // logical line.
2012
12.5k
  bool NewBreakBefore = false;
2013
  // We use a conservative reflowing strategy. Reflow starts after a line is
2014
  // broken or the corresponding whitespace compressed. Reflow ends as soon as a
2015
  // line that doesn't get reflown with the previous line is reached.
2016
12.5k
  bool Reflow = false;
2017
  // Keep track of where we are in the token:
2018
  // Where we are in the content of the current logical line.
2019
12.5k
  unsigned TailOffset = 0;
2020
  // The column number we're currently at.
2021
12.5k
  unsigned ContentStartColumn =
2022
12.5k
      Token->getContentStartColumn(0, /*Break=*/false);
2023
  // The number of columns left in the current logical line after TailOffset.
2024
12.5k
  unsigned RemainingTokenColumns =
2025
12.5k
      Token->getRemainingLength(0, TailOffset, ContentStartColumn);
2026
  // Adapt the start of the token, for example indent.
2027
12.5k
  if (!DryRun)
2028
3.61k
    Token->adaptStartOfLine(0, Whitespaces);
2029
2030
12.5k
  unsigned ContentIndent = 0;
2031
12.5k
  unsigned Penalty = 0;
2032
12.5k
  LLVM_DEBUG(llvm::dbgs() << "Breaking protruding token at column "
2033
12.5k
                          << StartColumn << ".\n");
2034
12.5k
  for (unsigned LineIndex = 0, EndIndex = Token->getLineCount();
2035
26.9k
       LineIndex != EndIndex; 
++LineIndex14.4k
) {
2036
14.4k
    LLVM_DEBUG(llvm::dbgs()
2037
14.4k
               << "  Line: " << LineIndex << " (Reflow: " << Reflow << ")\n");
2038
14.4k
    NewBreakBefore = false;
2039
    // If we did reflow the previous line, we'll try reflowing again. Otherwise
2040
    // we'll start reflowing if the current line is broken or whitespace is
2041
    // compressed.
2042
14.4k
    bool TryReflow = Reflow;
2043
    // Break the current token until we can fit the rest of the line.
2044
16.1k
    while (ContentStartColumn + RemainingTokenColumns > ColumnLimit) {
2045
1.88k
      LLVM_DEBUG(llvm::dbgs() << "    Over limit, need: "
2046
1.88k
                              << (ContentStartColumn + RemainingTokenColumns)
2047
1.88k
                              << ", space: " << ColumnLimit
2048
1.88k
                              << ", reflown prefix: " << ContentStartColumn
2049
1.88k
                              << ", offset in line: " << TailOffset << "\n");
2050
      // If the current token doesn't fit, find the latest possible split in the
2051
      // current line so that breaking at it will be under the column limit.
2052
      // FIXME: Use the earliest possible split while reflowing to correctly
2053
      // compress whitespace within a line.
2054
1.88k
      BreakableToken::Split Split =
2055
1.88k
          Token->getSplit(LineIndex, TailOffset, ColumnLimit,
2056
1.88k
                          ContentStartColumn, CommentPragmasRegex);
2057
1.88k
      if (Split.first == StringRef::npos) {
2058
        // No break opportunity - update the penalty and continue with the next
2059
        // logical line.
2060
214
        if (LineIndex < EndIndex - 1)
2061
          // The last line's penalty is handled in addNextStateToQueue() or when
2062
          // calling replaceWhitespaceAfterLastLine below.
2063
69
          Penalty += Style.PenaltyExcessCharacter *
2064
69
                     (ContentStartColumn + RemainingTokenColumns - ColumnLimit);
2065
214
        LLVM_DEBUG(llvm::dbgs() << "    No break opportunity.\n");
2066
214
        break;
2067
214
      }
2068
1.67k
      assert(Split.first != 0);
2069
2070
1.67k
      if (Token->supportsReflow()) {
2071
        // Check whether the next natural split point after the current one can
2072
        // still fit the line, either because we can compress away whitespace,
2073
        // or because the penalty the excess characters introduce is lower than
2074
        // the break penalty.
2075
        // We only do this for tokens that support reflowing, and thus allow us
2076
        // to change the whitespace arbitrarily (e.g. comments).
2077
        // Other tokens, like string literals, can be broken on arbitrary
2078
        // positions.
2079
2080
        // First, compute the columns from TailOffset to the next possible split
2081
        // position.
2082
        // For example:
2083
        // ColumnLimit:     |
2084
        // // Some text   that    breaks
2085
        //    ^ tail offset
2086
        //             ^-- split
2087
        //    ^-------- to split columns
2088
        //                    ^--- next split
2089
        //    ^--------------- to next split columns
2090
1.29k
        unsigned ToSplitColumns = Token->getRangeLength(
2091
1.29k
            LineIndex, TailOffset, Split.first, ContentStartColumn);
2092
1.29k
        LLVM_DEBUG(llvm::dbgs() << "    ToSplit: " << ToSplitColumns << "\n");
2093
2094
1.29k
        BreakableToken::Split NextSplit = Token->getSplit(
2095
1.29k
            LineIndex, TailOffset + Split.first + Split.second, ColumnLimit,
2096
1.29k
            ContentStartColumn + ToSplitColumns + 1, CommentPragmasRegex);
2097
        // Compute the columns necessary to fit the next non-breakable sequence
2098
        // into the current line.
2099
1.29k
        unsigned ToNextSplitColumns = 0;
2100
1.29k
        if (NextSplit.first == StringRef::npos) {
2101
967
          ToNextSplitColumns = Token->getRemainingLength(LineIndex, TailOffset,
2102
967
                                                         ContentStartColumn);
2103
323
        } else {
2104
323
          ToNextSplitColumns = Token->getRangeLength(
2105
323
              LineIndex, TailOffset,
2106
323
              Split.first + Split.second + NextSplit.first, ContentStartColumn);
2107
323
        }
2108
        // Compress the whitespace between the break and the start of the next
2109
        // unbreakable sequence.
2110
1.29k
        ToNextSplitColumns =
2111
1.29k
            Token->getLengthAfterCompression(ToNextSplitColumns, Split);
2112
1.29k
        LLVM_DEBUG(llvm::dbgs()
2113
1.29k
                   << "    ContentStartColumn: " << ContentStartColumn << "\n");
2114
1.29k
        LLVM_DEBUG(llvm::dbgs()
2115
1.29k
                   << "    ToNextSplit: " << ToNextSplitColumns << "\n");
2116
        // If the whitespace compression makes us fit, continue on the current
2117
        // line.
2118
1.29k
        bool ContinueOnLine =
2119
1.29k
            ContentStartColumn + ToNextSplitColumns <= ColumnLimit;
2120
1.29k
        unsigned ExcessCharactersPenalty = 0;
2121
1.29k
        if (!ContinueOnLine && 
!Strict1.25k
) {
2122
          // Similarly, if the excess characters' penalty is lower than the
2123
          // penalty of introducing a new break, continue on the current line.
2124
1.21k
          ExcessCharactersPenalty =
2125
1.21k
              (ContentStartColumn + ToNextSplitColumns - ColumnLimit) *
2126
1.21k
              Style.PenaltyExcessCharacter;
2127
1.21k
          LLVM_DEBUG(llvm::dbgs()
2128
1.21k
                     << "    Penalty excess: " << ExcessCharactersPenalty
2129
1.21k
                     << "\n            break : " << NewBreakPenalty << "\n");
2130
1.21k
          if (ExcessCharactersPenalty < NewBreakPenalty) {
2131
39
            Exceeded = true;
2132
39
            ContinueOnLine = true;
2133
39
          }
2134
1.21k
        }
2135
1.29k
        if (ContinueOnLine) {
2136
74
          LLVM_DEBUG(llvm::dbgs() << "    Continuing on line...\n");
2137
          // The current line fits after compressing the whitespace - reflow
2138
          // the next line into it if possible.
2139
74
          TryReflow = true;
2140
74
          if (!DryRun)
2141
30
            Token->compressWhitespace(LineIndex, TailOffset, Split,
2142
30
                                      Whitespaces);
2143
          // When we continue on the same line, leave one space between content.
2144
74
          ContentStartColumn += ToSplitColumns + 1;
2145
74
          Penalty += ExcessCharactersPenalty;
2146
74
          TailOffset += Split.first + Split.second;
2147
74
          RemainingTokenColumns = Token->getRemainingLength(
2148
74
              LineIndex, TailOffset, ContentStartColumn);
2149
74
          continue;
2150
74
        }
2151
1.59k
      }
2152
1.59k
      LLVM_DEBUG(llvm::dbgs() << "    Breaking...\n");
2153
      // Update the ContentIndent only if the current line was not reflown with
2154
      // the previous line, since in that case the previous line should still
2155
      // determine the ContentIndent. Also never intent the last line.
2156
1.59k
      if (!Reflow)
2157
1.20k
        ContentIndent = Token->getContentIndent(LineIndex);
2158
1.59k
      LLVM_DEBUG(llvm::dbgs()
2159
1.59k
                 << "    ContentIndent: " << ContentIndent << "\n");
2160
1.59k
      ContentStartColumn = ContentIndent + Token->getContentStartColumn(
2161
1.59k
                                               LineIndex, /*Break=*/true);
2162
2163
1.59k
      unsigned NewRemainingTokenColumns = Token->getRemainingLength(
2164
1.59k
          LineIndex, TailOffset + Split.first + Split.second,
2165
1.59k
          ContentStartColumn);
2166
1.59k
      if (NewRemainingTokenColumns == 0) {
2167
        // No content to indent.
2168
18
        ContentIndent = 0;
2169
18
        ContentStartColumn =
2170
18
            Token->getContentStartColumn(LineIndex, /*Break=*/true);
2171
18
        NewRemainingTokenColumns = Token->getRemainingLength(
2172
18
            LineIndex, TailOffset + Split.first + Split.second,
2173
18
            ContentStartColumn);
2174
18
      }
2175
2176
      // When breaking before a tab character, it may be moved by a few columns,
2177
      // but will still be expanded to the next tab stop, so we don't save any
2178
      // columns.
2179
1.59k
      if (NewRemainingTokenColumns == RemainingTokenColumns) {
2180
        // FIXME: Do we need to adjust the penalty?
2181
1
        break;
2182
1
      }
2183
1.59k
      assert(NewRemainingTokenColumns < RemainingTokenColumns);
2184
2185
1.59k
      LLVM_DEBUG(llvm::dbgs() << "    Breaking at: " << TailOffset + Split.first
2186
1.59k
                              << ", " << Split.second << "\n");
2187
1.59k
      if (!DryRun)
2188
578
        Token->insertBreak(LineIndex, TailOffset, Split, ContentIndent,
2189
578
                           Whitespaces);
2190
2191
1.59k
      Penalty += NewBreakPenalty;
2192
1.59k
      TailOffset += Split.first + Split.second;
2193
1.59k
      RemainingTokenColumns = NewRemainingTokenColumns;
2194
1.59k
      BreakInserted = true;
2195
1.59k
      NewBreakBefore = true;
2196
1.59k
    }
2197
    // In case there's another line, prepare the state for the start of the next
2198
    // line.
2199
14.4k
    if (LineIndex + 1 != EndIndex) {
2200
1.91k
      unsigned NextLineIndex = LineIndex + 1;
2201
1.91k
      if (NewBreakBefore)
2202
        // After breaking a line, try to reflow the next line into the current
2203
        // one once RemainingTokenColumns fits.
2204
537
        TryReflow = true;
2205
1.91k
      if (TryReflow) {
2206
        // We decided that we want to try reflowing the next line into the
2207
        // current one.
2208
        // We will now adjust the state as if the reflow is successful (in
2209
        // preparation for the next line), and see whether that works. If we
2210
        // decide that we cannot reflow, we will later reset the state to the
2211
        // start of the next line.
2212
643
        Reflow = false;
2213
        // As we did not continue breaking the line, RemainingTokenColumns is
2214
        // known to fit after ContentStartColumn. Adapt ContentStartColumn to
2215
        // the position at which we want to format the next line if we do
2216
        // actually reflow.
2217
        // When we reflow, we need to add a space between the end of the current
2218
        // line and the next line's start column.
2219
643
        ContentStartColumn += RemainingTokenColumns + 1;
2220
        // Get the split that we need to reflow next logical line into the end
2221
        // of the current one; the split will include any leading whitespace of
2222
        // the next logical line.
2223
643
        BreakableToken::Split SplitBeforeNext =
2224
643
            Token->getReflowSplit(NextLineIndex, CommentPragmasRegex);
2225
643
        LLVM_DEBUG(llvm::dbgs()
2226
643
                   << "    Size of reflown text: " << ContentStartColumn
2227
643
                   << "\n    Potential reflow split: ");
2228
643
        if (SplitBeforeNext.first != StringRef::npos) {
2229
474
          LLVM_DEBUG(llvm::dbgs() << SplitBeforeNext.first << ", "
2230
474
                                  << SplitBeforeNext.second << "\n");
2231
474
          TailOffset = SplitBeforeNext.first + SplitBeforeNext.second;
2232
          // If the rest of the next line fits into the current line below the
2233
          // column limit, we can safely reflow.
2234
474
          RemainingTokenColumns = Token->getRemainingLength(
2235
474
              NextLineIndex, TailOffset, ContentStartColumn);
2236
474
          Reflow = true;
2237
474
          if (ContentStartColumn + RemainingTokenColumns > ColumnLimit) {
2238
319
            LLVM_DEBUG(llvm::dbgs()
2239
319
                       << "    Over limit after reflow, need: "
2240
319
                       << (ContentStartColumn + RemainingTokenColumns)
2241
319
                       << ", space: " << ColumnLimit
2242
319
                       << ", reflown prefix: " << ContentStartColumn
2243
319
                       << ", offset in line: " << TailOffset << "\n");
2244
            // If the whole next line does not fit, try to find a point in
2245
            // the next line at which we can break so that attaching the part
2246
            // of the next line to that break point onto the current line is
2247
            // below the column limit.
2248
319
            BreakableToken::Split Split =
2249
319
                Token->getSplit(NextLineIndex, TailOffset, ColumnLimit,
2250
319
                                ContentStartColumn, CommentPragmasRegex);
2251
319
            if (Split.first == StringRef::npos) {
2252
43
              LLVM_DEBUG(llvm::dbgs() << "    Did not find later break\n");
2253
43
              Reflow = false;
2254
276
            } else {
2255
              // Check whether the first split point gets us below the column
2256
              // limit. Note that we will execute this split below as part of
2257
              // the normal token breaking and reflow logic within the line.
2258
276
              unsigned ToSplitColumns = Token->getRangeLength(
2259
276
                  NextLineIndex, TailOffset, Split.first, ContentStartColumn);
2260
276
              if (ContentStartColumn + ToSplitColumns > ColumnLimit) {
2261
4
                LLVM_DEBUG(llvm::dbgs() << "    Next split protrudes, need: "
2262
4
                                        << (ContentStartColumn + ToSplitColumns)
2263
4
                                        << ", space: " << ColumnLimit);
2264
4
                unsigned ExcessCharactersPenalty =
2265
4
                    (ContentStartColumn + ToSplitColumns - ColumnLimit) *
2266
4
                    Style.PenaltyExcessCharacter;
2267
4
                if (NewBreakPenalty < ExcessCharactersPenalty) {
2268
4
                  Reflow = false;
2269
4
                }
2270
4
              }
2271
276
            }
2272
319
          }
2273
169
        } else {
2274
169
          LLVM_DEBUG(llvm::dbgs() << "not found.\n");
2275
169
        }
2276
643
      }
2277
1.91k
      if (!Reflow) {
2278
        // If we didn't reflow into the next line, the only space to consider is
2279
        // the next logical line. Reset our state to match the start of the next
2280
        // line.
2281
1.48k
        TailOffset = 0;
2282
1.48k
        ContentStartColumn =
2283
1.48k
            Token->getContentStartColumn(NextLineIndex, /*Break=*/false);
2284
1.48k
        RemainingTokenColumns = Token->getRemainingLength(
2285
1.48k
            NextLineIndex, TailOffset, ContentStartColumn);
2286
        // Adapt the start of the token, for example indent.
2287
1.48k
        if (!DryRun)
2288
643
          Token->adaptStartOfLine(NextLineIndex, Whitespaces);
2289
427
      } else {
2290
        // If we found a reflow split and have added a new break before the next
2291
        // line, we are going to remove the line break at the start of the next
2292
        // logical line. For example, here we'll add a new line break after
2293
        // 'text', and subsequently delete the line break between 'that' and
2294
        // 'reflows'.
2295
        //   // some text that
2296
        //   // reflows
2297
        // ->
2298
        //   // some text
2299
        //   // that reflows
2300
        // When adding the line break, we also added the penalty for it, so we
2301
        // need to subtract that penalty again when we remove the line break due
2302
        // to reflowing.
2303
427
        if (NewBreakBefore) {
2304
371
          assert(Penalty >= NewBreakPenalty);
2305
371
          Penalty -= NewBreakPenalty;
2306
371
        }
2307
427
        if (!DryRun)
2308
199
          Token->reflow(NextLineIndex, Whitespaces);
2309
427
      }
2310
1.91k
    }
2311
14.4k
  }
2312
2313
12.5k
  BreakableToken::Split SplitAfterLastLine =
2314
12.5k
      Token->getSplitAfterLastLine(TailOffset);
2315
12.5k
  if (SplitAfterLastLine.first != StringRef::npos) {
2316
12
    LLVM_DEBUG(llvm::dbgs() << "Replacing whitespace after last line.\n");
2317
2318
    // We add the last line's penalty here, since that line is going to be split
2319
    // now.
2320
12
    Penalty += Style.PenaltyExcessCharacter *
2321
12
               (ContentStartColumn + RemainingTokenColumns - ColumnLimit);
2322
2323
12
    if (!DryRun)
2324
6
      Token->replaceWhitespaceAfterLastLine(TailOffset, SplitAfterLastLine,
2325
6
                                            Whitespaces);
2326
12
    ContentStartColumn =
2327
12
        Token->getContentStartColumn(Token->getLineCount() - 1, /*Break=*/true);
2328
12
    RemainingTokenColumns = Token->getRemainingLength(
2329
12
        Token->getLineCount() - 1,
2330
12
        TailOffset + SplitAfterLastLine.first + SplitAfterLastLine.second,
2331
12
        ContentStartColumn);
2332
12
  }
2333
2334
12.5k
  State.Column = ContentStartColumn + RemainingTokenColumns -
2335
12.5k
                 Current.UnbreakableTailLength;
2336
2337
12.5k
  if (BreakInserted) {
2338
    // If we break the token inside a parameter list, we need to break before
2339
    // the next parameter on all levels, so that the next parameter is clearly
2340
    // visible. Line comments already introduce a break.
2341
933
    if (Current.isNot(TT_LineComment)) {
2342
1.70k
      for (unsigned i = 0, e = State.Stack.size(); i != e; 
++i1.13k
)
2343
1.13k
        State.Stack[i].BreakBeforeParameter = true;
2344
566
    }
2345
2346
933
    if (Current.is(TT_BlockComment))
2347
288
      State.NoContinuation = true;
2348
2349
933
    State.Stack.back().LastSpace = StartColumn;
2350
933
  }
2351
2352
12.5k
  Token->updateNextToken(State);
2353
2354
12.5k
  return {Penalty, Exceeded};
2355
12.5k
}
2356
2357
1.04M
unsigned ContinuationIndenter::getColumnLimit(const LineState &State) const {
2358
  // In preprocessor directives reserve two chars for trailing " \"
2359
1.02M
  return Style.ColumnLimit - (State.Line->InPPDirective ? 
214.1k
: 0);
2360
1.04M
}
2361
2362
8.25k
bool ContinuationIndenter::nextIsMultilineString(const LineState &State) {
2363
8.25k
  const FormatToken &Current = *State.NextToken;
2364
8.25k
  if (!Current.isStringLiteral() || 
Current.is(TT_ImplicitStringLiteral)61
)
2365
8.18k
    return false;
2366
  // We never consider raw string literals "multiline" for the purpose of
2367
  // AlwaysBreakBeforeMultilineStrings implementation as they are special-cased
2368
  // (see TokenAnnotator::mustBreakBefore().
2369
61
  if (Current.TokenText.startswith("R\""))
2370
2
    return false;
2371
59
  if (Current.IsMultiline)
2372
1
    return true;
2373
58
  if (Current.getNextNonComment() &&
2374
55
      Current.getNextNonComment()->isStringLiteral())
2375
29
    return true; // Implicit concatenation.
2376
29
  if (Style.ColumnLimit != 0 && 
Style.BreakStringLiterals23
&&
2377
11
      State.Column + Current.ColumnWidth + Current.UnbreakableTailLength >
2378
11
          Style.ColumnLimit)
2379
5
    return true; // String will be split.
2380
24
  return false;
2381
24
}
2382
2383
} // namespace format
2384
} // namespace clang