Coverage Report

Created: 2022-01-25 06:29

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