Coverage Report

Created: 2020-09-22 08:39

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