Coverage Report

Created: 2021-09-21 08:58

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