Coverage Report

Created: 2020-02-15 09:57

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