Coverage Report

Created: 2020-11-24 06:42

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Format/WhitespaceManager.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- WhitespaceManager.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 WhitespaceManager class.
11
///
12
//===----------------------------------------------------------------------===//
13
14
#include "WhitespaceManager.h"
15
#include "llvm/ADT/STLExtras.h"
16
17
namespace clang {
18
namespace format {
19
20
bool WhitespaceManager::Change::IsBeforeInFile::operator()(
21
526k
    const Change &C1, const Change &C2) const {
22
526k
  return SourceMgr.isBeforeInTranslationUnit(
23
526k
      C1.OriginalWhitespaceRange.getBegin(),
24
526k
      C2.OriginalWhitespaceRange.getBegin());
25
526k
}
26
27
WhitespaceManager::Change::Change(const FormatToken &Tok,
28
                                  bool CreateReplacement,
29
                                  SourceRange OriginalWhitespaceRange,
30
                                  int Spaces, unsigned StartOfTokenColumn,
31
                                  unsigned NewlinesBefore,
32
                                  StringRef PreviousLinePostfix,
33
                                  StringRef CurrentLinePrefix, bool IsAligned,
34
                                  bool ContinuesPPDirective, bool IsInsideToken)
35
    : Tok(&Tok), CreateReplacement(CreateReplacement),
36
      OriginalWhitespaceRange(OriginalWhitespaceRange),
37
      StartOfTokenColumn(StartOfTokenColumn), NewlinesBefore(NewlinesBefore),
38
      PreviousLinePostfix(PreviousLinePostfix),
39
      CurrentLinePrefix(CurrentLinePrefix), IsAligned(IsAligned),
40
      ContinuesPPDirective(ContinuesPPDirective), Spaces(Spaces),
41
      IsInsideToken(IsInsideToken), IsTrailingComment(false), TokenLength(0),
42
      PreviousEndOfTokenColumn(0), EscapedNewlineColumn(0),
43
268k
      StartOfBlockComment(nullptr), IndentationOffset(0), ConditionalsLevel(0) {
44
268k
}
45
46
void WhitespaceManager::replaceWhitespace(FormatToken &Tok, unsigned Newlines,
47
                                          unsigned Spaces,
48
                                          unsigned StartOfTokenColumn,
49
246k
                                          bool IsAligned, bool InPPDirective) {
50
246k
  if (Tok.Finalized)
51
2.43k
    return;
52
243k
  Tok.setDecision((Newlines > 0) ? 
FD_Break30.6k
:
FD_Continue212k
);
53
243k
  Changes.push_back(Change(Tok, /*CreateReplacement=*/true, Tok.WhitespaceRange,
54
243k
                           Spaces, StartOfTokenColumn, Newlines, "", "",
55
243k
                           IsAligned, InPPDirective && 
!Tok.IsFirst1.69k
,
56
243k
                           /*IsInsideToken=*/false));
57
243k
}
58
59
void WhitespaceManager::addUntouchableToken(const FormatToken &Tok,
60
24.0k
                                            bool InPPDirective) {
61
24.0k
  if (Tok.Finalized)
62
24
    return;
63
24.0k
  Changes.push_back(Change(Tok, /*CreateReplacement=*/false,
64
24.0k
                           Tok.WhitespaceRange, /*Spaces=*/0,
65
24.0k
                           Tok.OriginalColumn, Tok.NewlinesBefore, "", "",
66
24.0k
                           /*IsAligned=*/false, InPPDirective && 
!Tok.IsFirst1.86k
,
67
24.0k
                           /*IsInsideToken=*/false));
68
24.0k
}
69
70
llvm::Error
71
193
WhitespaceManager::addReplacement(const tooling::Replacement &Replacement) {
72
193
  return Replaces.add(Replacement);
73
193
}
74
75
void WhitespaceManager::replaceWhitespaceInToken(
76
    const FormatToken &Tok, unsigned Offset, unsigned ReplaceChars,
77
    StringRef PreviousPostfix, StringRef CurrentPrefix, bool InPPDirective,
78
1.19k
    unsigned Newlines, int Spaces) {
79
1.19k
  if (Tok.Finalized)
80
4
    return;
81
1.18k
  SourceLocation Start = Tok.getStartOfNonWhitespace().getLocWithOffset(Offset);
82
1.18k
  Changes.push_back(
83
1.18k
      Change(Tok, /*CreateReplacement=*/true,
84
1.18k
             SourceRange(Start, Start.getLocWithOffset(ReplaceChars)), Spaces,
85
1.18k
             std::max(0, Spaces), Newlines, PreviousPostfix, CurrentPrefix,
86
1.18k
             /*IsAligned=*/true, InPPDirective && 
!Tok.IsFirst21
,
87
1.18k
             /*IsInsideToken=*/true));
88
1.18k
}
89
90
16.5k
const tooling::Replacements &WhitespaceManager::generateReplacements() {
91
16.5k
  if (Changes.empty())
92
40
    return Replaces;
93
94
16.5k
  llvm::sort(Changes, Change::IsBeforeInFile(SourceMgr));
95
16.5k
  calculateLineBreakInformation();
96
16.5k
  alignConsecutiveMacros();
97
16.5k
  alignConsecutiveDeclarations();
98
16.5k
  alignConsecutiveBitFields();
99
16.5k
  alignConsecutiveAssignments();
100
16.5k
  alignChainedConditionals();
101
16.5k
  alignTrailingComments();
102
16.5k
  alignEscapedNewlines();
103
16.5k
  generateChanges();
104
105
16.5k
  return Replaces;
106
16.5k
}
107
108
16.5k
void WhitespaceManager::calculateLineBreakInformation() {
109
16.5k
  Changes[0].PreviousEndOfTokenColumn = 0;
110
16.5k
  Change *LastOutsideTokenChange = &Changes[0];
111
268k
  for (unsigned i = 1, e = Changes.size(); i != e; 
++i252k
) {
112
252k
    SourceLocation OriginalWhitespaceStart =
113
252k
        Changes[i].OriginalWhitespaceRange.getBegin();
114
252k
    SourceLocation PreviousOriginalWhitespaceEnd =
115
252k
        Changes[i - 1].OriginalWhitespaceRange.getEnd();
116
252k
    unsigned OriginalWhitespaceStartOffset =
117
252k
        SourceMgr.getFileOffset(OriginalWhitespaceStart);
118
252k
    unsigned PreviousOriginalWhitespaceEndOffset =
119
252k
        SourceMgr.getFileOffset(PreviousOriginalWhitespaceEnd);
120
252k
    assert(PreviousOriginalWhitespaceEndOffset <=
121
252k
           OriginalWhitespaceStartOffset);
122
252k
    const char *const PreviousOriginalWhitespaceEndData =
123
252k
        SourceMgr.getCharacterData(PreviousOriginalWhitespaceEnd);
124
252k
    StringRef Text(PreviousOriginalWhitespaceEndData,
125
252k
                   SourceMgr.getCharacterData(OriginalWhitespaceStart) -
126
252k
                       PreviousOriginalWhitespaceEndData);
127
    // Usually consecutive changes would occur in consecutive tokens. This is
128
    // not the case however when analyzing some preprocessor runs of the
129
    // annotated lines. For example, in this code:
130
    //
131
    // #if A // line 1
132
    // int i = 1;
133
    // #else B // line 2
134
    // int i = 2;
135
    // #endif // line 3
136
    //
137
    // one of the runs will produce the sequence of lines marked with line 1, 2
138
    // and 3. So the two consecutive whitespace changes just before '// line 2'
139
    // and before '#endif // line 3' span multiple lines and tokens:
140
    //
141
    // #else B{change X}[// line 2
142
    // int i = 2;
143
    // ]{change Y}#endif // line 3
144
    //
145
    // For this reason, if the text between consecutive changes spans multiple
146
    // newlines, the token length must be adjusted to the end of the original
147
    // line of the token.
148
252k
    auto NewlinePos = Text.find_first_of('\n');
149
252k
    if (NewlinePos == StringRef::npos) {
150
252k
      Changes[i - 1].TokenLength = OriginalWhitespaceStartOffset -
151
252k
                                   PreviousOriginalWhitespaceEndOffset +
152
252k
                                   Changes[i].PreviousLinePostfix.size() +
153
252k
                                   Changes[i - 1].CurrentLinePrefix.size();
154
165
    } else {
155
165
      Changes[i - 1].TokenLength =
156
165
          NewlinePos + Changes[i - 1].CurrentLinePrefix.size();
157
165
    }
158
159
    // If there are multiple changes in this token, sum up all the changes until
160
    // the end of the line.
161
252k
    if (Changes[i - 1].IsInsideToken && 
Changes[i - 1].NewlinesBefore == 01.18k
)
162
250
      LastOutsideTokenChange->TokenLength +=
163
250
          Changes[i - 1].TokenLength + Changes[i - 1].Spaces;
164
252k
    else
165
252k
      LastOutsideTokenChange = &Changes[i - 1];
166
167
252k
    Changes[i].PreviousEndOfTokenColumn =
168
252k
        Changes[i - 1].StartOfTokenColumn + Changes[i - 1].TokenLength;
169
170
252k
    Changes[i - 1].IsTrailingComment =
171
252k
        (Changes[i].NewlinesBefore > 0 || 
Changes[i].Tok->is(tok::eof)216k
||
172
201k
         (Changes[i].IsInsideToken && 
Changes[i].Tok->is(tok::comment)250
)) &&
173
51.0k
        Changes[i - 1].Tok->is(tok::comment) &&
174
        // FIXME: This is a dirty hack. The problem is that
175
        // BreakableLineCommentSection does comment reflow changes and here is
176
        // the aligning of trailing comments. Consider the case where we reflow
177
        // the second line up in this example:
178
        //
179
        // // line 1
180
        // // line 2
181
        //
182
        // That amounts to 2 changes by BreakableLineCommentSection:
183
        //  - the first, delimited by (), for the whitespace between the tokens,
184
        //  - and second, delimited by [], for the whitespace at the beginning
185
        //  of the second token:
186
        //
187
        // // line 1(
188
        // )[// ]line 2
189
        //
190
        // So in the end we have two changes like this:
191
        //
192
        // // line1()[ ]line 2
193
        //
194
        // Note that the OriginalWhitespaceStart of the second change is the
195
        // same as the PreviousOriginalWhitespaceEnd of the first change.
196
        // In this case, the below check ensures that the second change doesn't
197
        // get treated as a trailing comment change here, since this might
198
        // trigger additional whitespace to be wrongly inserted before "line 2"
199
        // by the comment aligner here.
200
        //
201
        // For a proper solution we need a mechanism to say to WhitespaceManager
202
        // that a particular change breaks the current sequence of trailing
203
        // comments.
204
4.50k
        OriginalWhitespaceStart != PreviousOriginalWhitespaceEnd;
205
252k
  }
206
  // FIXME: The last token is currently not always an eof token; in those
207
  // cases, setting TokenLength of the last token to 0 is wrong.
208
16.5k
  Changes.back().TokenLength = 0;
209
16.5k
  Changes.back().IsTrailingComment = Changes.back().Tok->is(tok::comment);
210
211
16.5k
  const WhitespaceManager::Change *LastBlockComment = nullptr;
212
268k
  for (auto &Change : Changes) {
213
    // Reset the IsTrailingComment flag for changes inside of trailing comments
214
    // so they don't get realigned later. Comment line breaks however still need
215
    // to be aligned.
216
268k
    if (Change.IsInsideToken && 
Change.NewlinesBefore == 01.18k
)
217
250
      Change.IsTrailingComment = false;
218
268k
    Change.StartOfBlockComment = nullptr;
219
268k
    Change.IndentationOffset = 0;
220
268k
    if (Change.Tok->is(tok::comment)) {
221
4.92k
      if (Change.Tok->is(TT_LineComment) || 
!Change.IsInsideToken1.18k
)
222
4.37k
        LastBlockComment = &Change;
223
550
      else {
224
550
        if ((Change.StartOfBlockComment = LastBlockComment))
225
550
          Change.IndentationOffset =
226
550
              Change.StartOfTokenColumn -
227
550
              Change.StartOfBlockComment->StartOfTokenColumn;
228
550
      }
229
263k
    } else {
230
263k
      LastBlockComment = nullptr;
231
263k
    }
232
268k
  }
233
234
  // Compute conditional nesting level
235
  // Level is increased for each conditional, unless this conditional continues
236
  // a chain of conditional, i.e. starts immediately after the colon of another
237
  // conditional.
238
16.5k
  SmallVector<bool, 16> ScopeStack;
239
16.5k
  int ConditionalsLevel = 0;
240
268k
  for (auto &Change : Changes) {
241
289k
    for (unsigned i = 0, e = Change.Tok->FakeLParens.size(); i != e; 
++i20.3k
) {
242
20.3k
      bool isNestedConditional =
243
20.3k
          Change.Tok->FakeLParens[e - 1 - i] == prec::Conditional &&
244
722
          !(i == 0 && 
Change.Tok->Previous718
&&
245
691
            Change.Tok->Previous->is(TT_ConditionalExpr) &&
246
209
            Change.Tok->Previous->is(tok::colon));
247
20.3k
      if (isNestedConditional)
248
534
        ++ConditionalsLevel;
249
20.3k
      ScopeStack.push_back(isNestedConditional);
250
20.3k
    }
251
252
268k
    Change.ConditionalsLevel = ConditionalsLevel;
253
254
287k
    for (unsigned i = Change.Tok->FakeRParens; i > 0 && 
ScopeStack.size()18.6k
;
255
18.6k
         --i) {
256
18.6k
      if (ScopeStack.pop_back_val())
257
528
        --ConditionalsLevel;
258
18.6k
    }
259
268k
  }
260
16.5k
}
261
262
// Align a single sequence of tokens, see AlignTokens below.
263
template <typename F>
264
static void
265
AlignTokenSequence(unsigned Start, unsigned End, unsigned Column, F &&Matches,
266
1.18k
                   SmallVector<WhitespaceManager::Change, 16> &Changes) {
267
1.18k
  bool FoundMatchOnLine = false;
268
1.18k
  int Shift = 0;
269
270
  // ScopeStack keeps track of the current scope depth. It contains indices of
271
  // the first token on each scope.
272
  // We only run the "Matches" function on tokens from the outer-most scope.
273
  // However, we do need to pay special attention to one class of tokens
274
  // that are not in the outer-most scope, and that is function parameters
275
  // which are split across multiple lines, as illustrated by this example:
276
  //   double a(int x);
277
  //   int    b(int  y,
278
  //          double z);
279
  // In the above example, we need to take special care to ensure that
280
  // 'double z' is indented along with it's owning function 'b'.
281
  // Special handling is required for 'nested' ternary operators.
282
1.18k
  SmallVector<unsigned, 16> ScopeStack;
283
284
11.6k
  for (unsigned i = Start; i != End; 
++i10.4k
) {
285
10.4k
    if (ScopeStack.size() != 0 &&
286
2.60k
        Changes[i].indentAndNestingLevel() <
287
2.60k
            Changes[ScopeStack.back()].indentAndNestingLevel())
288
499
      ScopeStack.pop_back();
289
290
    // Compare current token to previous non-comment token to ensure whether
291
    // it is in a deeper scope or not.
292
10.4k
    unsigned PreviousNonComment = i - 1;
293
10.5k
    while (PreviousNonComment > Start &&
294
8.20k
           Changes[PreviousNonComment].Tok->is(tok::comment))
295
124
      PreviousNonComment--;
296
10.4k
    if (i != Start && Changes[i].indentAndNestingLevel() >
297
9.26k
                          Changes[PreviousNonComment].indentAndNestingLevel())
298
607
      ScopeStack.push_back(i);
299
300
10.4k
    bool InsideNestedScope = ScopeStack.size() != 0;
301
302
10.4k
    if (Changes[i].NewlinesBefore > 0 && 
!InsideNestedScope1.63k
) {
303
1.19k
      Shift = 0;
304
1.19k
      FoundMatchOnLine = false;
305
1.19k
    }
306
307
    // If this is the first matching token to be aligned, remember by how many
308
    // spaces it has to be shifted, so the rest of the changes on the line are
309
    // shifted by the same amount
310
10.4k
    if (!FoundMatchOnLine && 
!InsideNestedScope3.61k
&&
Matches(Changes[i])3.41k
) {
311
2.06k
      FoundMatchOnLine = true;
312
2.06k
      Shift = Column - Changes[i].StartOfTokenColumn;
313
2.06k
      Changes[i].Spaces += Shift;
314
2.06k
    }
315
316
    // This is for function parameters that are split across multiple lines,
317
    // as mentioned in the ScopeStack comment.
318
10.4k
    if (InsideNestedScope && 
Changes[i].NewlinesBefore > 02.70k
) {
319
436
      unsigned ScopeStart = ScopeStack.back();
320
436
      if (Changes[ScopeStart - 1].Tok->is(TT_FunctionDeclarationName) ||
321
436
          (ScopeStart > Start + 1 &&
322
409
           Changes[ScopeStart - 2].Tok->is(TT_FunctionDeclarationName)) ||
323
406
          Changes[i].Tok->is(TT_ConditionalExpr) ||
324
340
          (Changes[i].Tok->Previous &&
325
189
           Changes[i].Tok->Previous->is(TT_ConditionalExpr)))
326
150
        Changes[i].Spaces += Shift;
327
436
    }
328
329
10.4k
    assert(Shift >= 0);
330
10.4k
    Changes[i].StartOfTokenColumn += Shift;
331
10.4k
    if (i + 1 != Changes.size())
332
10.1k
      Changes[i + 1].PreviousEndOfTokenColumn += Shift;
333
10.4k
  }
334
1.18k
}
WhitespaceManager.cpp:void clang::format::AlignTokenSequence<clang::format::WhitespaceManager::alignConsecutiveAssignments()::$_1&>(unsigned int, unsigned int, unsigned int, clang::format::WhitespaceManager::alignConsecutiveAssignments()::$_1&, llvm::SmallVector<clang::format::WhitespaceManager::Change, 16u>&)
Line
Count
Source
266
269
                   SmallVector<WhitespaceManager::Change, 16> &Changes) {
267
269
  bool FoundMatchOnLine = false;
268
269
  int Shift = 0;
269
270
  // ScopeStack keeps track of the current scope depth. It contains indices of
271
  // the first token on each scope.
272
  // We only run the "Matches" function on tokens from the outer-most scope.
273
  // However, we do need to pay special attention to one class of tokens
274
  // that are not in the outer-most scope, and that is function parameters
275
  // which are split across multiple lines, as illustrated by this example:
276
  //   double a(int x);
277
  //   int    b(int  y,
278
  //          double z);
279
  // In the above example, we need to take special care to ensure that
280
  // 'double z' is indented along with it's owning function 'b'.
281
  // Special handling is required for 'nested' ternary operators.
282
269
  SmallVector<unsigned, 16> ScopeStack;
283
284
2.81k
  for (unsigned i = Start; i != End; 
++i2.54k
) {
285
2.54k
    if (ScopeStack.size() != 0 &&
286
532
        Changes[i].indentAndNestingLevel() <
287
532
            Changes[ScopeStack.back()].indentAndNestingLevel())
288
115
      ScopeStack.pop_back();
289
290
    // Compare current token to previous non-comment token to ensure whether
291
    // it is in a deeper scope or not.
292
2.54k
    unsigned PreviousNonComment = i - 1;
293
2.60k
    while (PreviousNonComment > Start &&
294
2.06k
           Changes[PreviousNonComment].Tok->is(tok::comment))
295
60
      PreviousNonComment--;
296
2.54k
    if (i != Start && Changes[i].indentAndNestingLevel() >
297
2.27k
                          Changes[PreviousNonComment].indentAndNestingLevel())
298
136
      ScopeStack.push_back(i);
299
300
2.54k
    bool InsideNestedScope = ScopeStack.size() != 0;
301
302
2.54k
    if (Changes[i].NewlinesBefore > 0 && 
!InsideNestedScope314
) {
303
239
      Shift = 0;
304
239
      FoundMatchOnLine = false;
305
239
    }
306
307
    // If this is the first matching token to be aligned, remember by how many
308
    // spaces it has to be shifted, so the rest of the changes on the line are
309
    // shifted by the same amount
310
2.54k
    if (!FoundMatchOnLine && 
!InsideNestedScope1.07k
&&
Matches(Changes[i])949
) {
311
422
      FoundMatchOnLine = true;
312
422
      Shift = Column - Changes[i].StartOfTokenColumn;
313
422
      Changes[i].Spaces += Shift;
314
422
    }
315
316
    // This is for function parameters that are split across multiple lines,
317
    // as mentioned in the ScopeStack comment.
318
2.54k
    if (InsideNestedScope && 
Changes[i].NewlinesBefore > 0553
) {
319
75
      unsigned ScopeStart = ScopeStack.back();
320
75
      if (Changes[ScopeStart - 1].Tok->is(TT_FunctionDeclarationName) ||
321
75
          (ScopeStart > Start + 1 &&
322
75
           Changes[ScopeStart - 2].Tok->is(TT_FunctionDeclarationName)) ||
323
75
          Changes[i].Tok->is(TT_ConditionalExpr) ||
324
75
          (Changes[i].Tok->Previous &&
325
39
           Changes[i].Tok->Previous->is(TT_ConditionalExpr)))
326
0
        Changes[i].Spaces += Shift;
327
75
    }
328
329
2.54k
    assert(Shift >= 0);
330
2.54k
    Changes[i].StartOfTokenColumn += Shift;
331
2.54k
    if (i + 1 != Changes.size())
332
2.44k
      Changes[i + 1].PreviousEndOfTokenColumn += Shift;
333
2.54k
  }
334
269
}
WhitespaceManager.cpp:void clang::format::AlignTokenSequence<clang::format::WhitespaceManager::alignConsecutiveBitFields()::$_2&>(unsigned int, unsigned int, unsigned int, clang::format::WhitespaceManager::alignConsecutiveBitFields()::$_2&, llvm::SmallVector<clang::format::WhitespaceManager::Change, 16u>&)
Line
Count
Source
266
30
                   SmallVector<WhitespaceManager::Change, 16> &Changes) {
267
30
  bool FoundMatchOnLine = false;
268
30
  int Shift = 0;
269
270
  // ScopeStack keeps track of the current scope depth. It contains indices of
271
  // the first token on each scope.
272
  // We only run the "Matches" function on tokens from the outer-most scope.
273
  // However, we do need to pay special attention to one class of tokens
274
  // that are not in the outer-most scope, and that is function parameters
275
  // which are split across multiple lines, as illustrated by this example:
276
  //   double a(int x);
277
  //   int    b(int  y,
278
  //          double z);
279
  // In the above example, we need to take special care to ensure that
280
  // 'double z' is indented along with it's owning function 'b'.
281
  // Special handling is required for 'nested' ternary operators.
282
30
  SmallVector<unsigned, 16> ScopeStack;
283
284
360
  for (unsigned i = Start; i != End; 
++i330
) {
285
330
    if (ScopeStack.size() != 0 &&
286
6
        Changes[i].indentAndNestingLevel() <
287
6
            Changes[ScopeStack.back()].indentAndNestingLevel())
288
3
      ScopeStack.pop_back();
289
290
    // Compare current token to previous non-comment token to ensure whether
291
    // it is in a deeper scope or not.
292
330
    unsigned PreviousNonComment = i - 1;
293
336
    while (PreviousNonComment > Start &&
294
276
           Changes[PreviousNonComment].Tok->is(tok::comment))
295
6
      PreviousNonComment--;
296
330
    if (i != Start && Changes[i].indentAndNestingLevel() >
297
300
                          Changes[PreviousNonComment].indentAndNestingLevel())
298
3
      ScopeStack.push_back(i);
299
300
330
    bool InsideNestedScope = ScopeStack.size() != 0;
301
302
330
    if (Changes[i].NewlinesBefore > 0 && 
!InsideNestedScope30
) {
303
30
      Shift = 0;
304
30
      FoundMatchOnLine = false;
305
30
    }
306
307
    // If this is the first matching token to be aligned, remember by how many
308
    // spaces it has to be shifted, so the rest of the changes on the line are
309
    // shifted by the same amount
310
330
    if (!FoundMatchOnLine && 
!InsideNestedScope120
&&
Matches(Changes[i])120
) {
311
60
      FoundMatchOnLine = true;
312
60
      Shift = Column - Changes[i].StartOfTokenColumn;
313
60
      Changes[i].Spaces += Shift;
314
60
    }
315
316
    // This is for function parameters that are split across multiple lines,
317
    // as mentioned in the ScopeStack comment.
318
330
    if (InsideNestedScope && 
Changes[i].NewlinesBefore > 06
) {
319
0
      unsigned ScopeStart = ScopeStack.back();
320
0
      if (Changes[ScopeStart - 1].Tok->is(TT_FunctionDeclarationName) ||
321
0
          (ScopeStart > Start + 1 &&
322
0
           Changes[ScopeStart - 2].Tok->is(TT_FunctionDeclarationName)) ||
323
0
          Changes[i].Tok->is(TT_ConditionalExpr) ||
324
0
          (Changes[i].Tok->Previous &&
325
0
           Changes[i].Tok->Previous->is(TT_ConditionalExpr)))
326
0
        Changes[i].Spaces += Shift;
327
0
    }
328
329
330
    assert(Shift >= 0);
330
330
    Changes[i].StartOfTokenColumn += Shift;
331
330
    if (i + 1 != Changes.size())
332
300
      Changes[i + 1].PreviousEndOfTokenColumn += Shift;
333
330
  }
334
30
}
WhitespaceManager.cpp:void clang::format::AlignTokenSequence<clang::format::WhitespaceManager::alignConsecutiveDeclarations()::$_3&>(unsigned int, unsigned int, unsigned int, clang::format::WhitespaceManager::alignConsecutiveDeclarations()::$_3&, llvm::SmallVector<clang::format::WhitespaceManager::Change, 16u>&)
Line
Count
Source
266
313
                   SmallVector<WhitespaceManager::Change, 16> &Changes) {
267
313
  bool FoundMatchOnLine = false;
268
313
  int Shift = 0;
269
270
  // ScopeStack keeps track of the current scope depth. It contains indices of
271
  // the first token on each scope.
272
  // We only run the "Matches" function on tokens from the outer-most scope.
273
  // However, we do need to pay special attention to one class of tokens
274
  // that are not in the outer-most scope, and that is function parameters
275
  // which are split across multiple lines, as illustrated by this example:
276
  //   double a(int x);
277
  //   int    b(int  y,
278
  //          double z);
279
  // In the above example, we need to take special care to ensure that
280
  // 'double z' is indented along with it's owning function 'b'.
281
  // Special handling is required for 'nested' ternary operators.
282
313
  SmallVector<unsigned, 16> ScopeStack;
283
284
4.40k
  for (unsigned i = Start; i != End; 
++i4.08k
) {
285
4.08k
    if (ScopeStack.size() != 0 &&
286
1.27k
        Changes[i].indentAndNestingLevel() <
287
1.27k
            Changes[ScopeStack.back()].indentAndNestingLevel())
288
245
      ScopeStack.pop_back();
289
290
    // Compare current token to previous non-comment token to ensure whether
291
    // it is in a deeper scope or not.
292
4.08k
    unsigned PreviousNonComment = i - 1;
293
4.13k
    while (PreviousNonComment > Start &&
294
3.50k
           Changes[PreviousNonComment].Tok->is(tok::comment))
295
45
      PreviousNonComment--;
296
4.08k
    if (i != Start && Changes[i].indentAndNestingLevel() >
297
3.77k
                          Changes[PreviousNonComment].indentAndNestingLevel())
298
297
      ScopeStack.push_back(i);
299
300
4.08k
    bool InsideNestedScope = ScopeStack.size() != 0;
301
302
4.08k
    if (Changes[i].NewlinesBefore > 0 && 
!InsideNestedScope528
) {
303
328
      Shift = 0;
304
328
      FoundMatchOnLine = false;
305
328
    }
306
307
    // If this is the first matching token to be aligned, remember by how many
308
    // spaces it has to be shifted, so the rest of the changes on the line are
309
    // shifted by the same amount
310
4.08k
    if (!FoundMatchOnLine && 
!InsideNestedScope1.12k
&&
Matches(Changes[i])1.05k
) {
311
556
      FoundMatchOnLine = true;
312
556
      Shift = Column - Changes[i].StartOfTokenColumn;
313
556
      Changes[i].Spaces += Shift;
314
556
    }
315
316
    // This is for function parameters that are split across multiple lines,
317
    // as mentioned in the ScopeStack comment.
318
4.08k
    if (InsideNestedScope && 
Changes[i].NewlinesBefore > 01.33k
) {
319
200
      unsigned ScopeStart = ScopeStack.back();
320
200
      if (Changes[ScopeStart - 1].Tok->is(TT_FunctionDeclarationName) ||
321
200
          (ScopeStart > Start + 1 &&
322
200
           Changes[ScopeStart - 2].Tok->is(TT_FunctionDeclarationName)) ||
323
170
          Changes[i].Tok->is(TT_ConditionalExpr) ||
324
170
          (Changes[i].Tok->Previous &&
325
60
           Changes[i].Tok->Previous->is(TT_ConditionalExpr)))
326
30
        Changes[i].Spaces += Shift;
327
200
    }
328
329
4.08k
    assert(Shift >= 0);
330
4.08k
    Changes[i].StartOfTokenColumn += Shift;
331
4.08k
    if (i + 1 != Changes.size())
332
3.95k
      Changes[i + 1].PreviousEndOfTokenColumn += Shift;
333
4.08k
  }
334
313
}
WhitespaceManager.cpp:void clang::format::AlignTokenSequence<clang::format::WhitespaceManager::alignChainedConditionals()::$_4&>(unsigned int, unsigned int, unsigned int, clang::format::WhitespaceManager::alignChainedConditionals()::$_4&, llvm::SmallVector<clang::format::WhitespaceManager::Change, 16u>&)
Line
Count
Source
266
361
                   SmallVector<WhitespaceManager::Change, 16> &Changes) {
267
361
  bool FoundMatchOnLine = false;
268
361
  int Shift = 0;
269
270
  // ScopeStack keeps track of the current scope depth. It contains indices of
271
  // the first token on each scope.
272
  // We only run the "Matches" function on tokens from the outer-most scope.
273
  // However, we do need to pay special attention to one class of tokens
274
  // that are not in the outer-most scope, and that is function parameters
275
  // which are split across multiple lines, as illustrated by this example:
276
  //   double a(int x);
277
  //   int    b(int  y,
278
  //          double z);
279
  // In the above example, we need to take special care to ensure that
280
  // 'double z' is indented along with it's owning function 'b'.
281
  // Special handling is required for 'nested' ternary operators.
282
361
  SmallVector<unsigned, 16> ScopeStack;
283
284
2.31k
  for (unsigned i = Start; i != End; 
++i1.95k
) {
285
1.95k
    if (ScopeStack.size() != 0 &&
286
427
        Changes[i].indentAndNestingLevel() <
287
427
            Changes[ScopeStack.back()].indentAndNestingLevel())
288
73
      ScopeStack.pop_back();
289
290
    // Compare current token to previous non-comment token to ensure whether
291
    // it is in a deeper scope or not.
292
1.95k
    unsigned PreviousNonComment = i - 1;
293
1.96k
    while (PreviousNonComment > Start &&
294
1.23k
           Changes[PreviousNonComment].Tok->is(tok::comment))
295
9
      PreviousNonComment--;
296
1.95k
    if (i != Start && Changes[i].indentAndNestingLevel() >
297
1.59k
                          Changes[PreviousNonComment].indentAndNestingLevel())
298
94
      ScopeStack.push_back(i);
299
300
1.95k
    bool InsideNestedScope = ScopeStack.size() != 0;
301
302
1.95k
    if (Changes[i].NewlinesBefore > 0 && 
!InsideNestedScope388
) {
303
301
      Shift = 0;
304
301
      FoundMatchOnLine = false;
305
301
    }
306
307
    // If this is the first matching token to be aligned, remember by how many
308
    // spaces it has to be shifted, so the rest of the changes on the line are
309
    // shifted by the same amount
310
1.95k
    if (!FoundMatchOnLine && 
!InsideNestedScope766
&&
Matches(Changes[i])766
) {
311
586
      FoundMatchOnLine = true;
312
586
      Shift = Column - Changes[i].StartOfTokenColumn;
313
586
      Changes[i].Spaces += Shift;
314
586
    }
315
316
    // This is for function parameters that are split across multiple lines,
317
    // as mentioned in the ScopeStack comment.
318
1.95k
    if (InsideNestedScope && 
Changes[i].NewlinesBefore > 0448
) {
319
87
      unsigned ScopeStart = ScopeStack.back();
320
87
      if (Changes[ScopeStart - 1].Tok->is(TT_FunctionDeclarationName) ||
321
87
          (ScopeStart > Start + 1 &&
322
75
           Changes[ScopeStart - 2].Tok->is(TT_FunctionDeclarationName)) ||
323
87
          Changes[i].Tok->is(TT_ConditionalExpr) ||
324
21
          (Changes[i].Tok->Previous &&
325
18
           Changes[i].Tok->Previous->is(TT_ConditionalExpr)))
326
66
        Changes[i].Spaces += Shift;
327
87
    }
328
329
1.95k
    assert(Shift >= 0);
330
1.95k
    Changes[i].StartOfTokenColumn += Shift;
331
1.95k
    if (i + 1 != Changes.size())
332
1.94k
      Changes[i + 1].PreviousEndOfTokenColumn += Shift;
333
1.95k
  }
334
361
}
WhitespaceManager.cpp:void clang::format::AlignTokenSequence<clang::format::WhitespaceManager::alignChainedConditionals()::$_6&>(unsigned int, unsigned int, unsigned int, clang::format::WhitespaceManager::alignChainedConditionals()::$_6&, llvm::SmallVector<clang::format::WhitespaceManager::Change, 16u>&)
Line
Count
Source
266
208
                   SmallVector<WhitespaceManager::Change, 16> &Changes) {
267
208
  bool FoundMatchOnLine = false;
268
208
  int Shift = 0;
269
270
  // ScopeStack keeps track of the current scope depth. It contains indices of
271
  // the first token on each scope.
272
  // We only run the "Matches" function on tokens from the outer-most scope.
273
  // However, we do need to pay special attention to one class of tokens
274
  // that are not in the outer-most scope, and that is function parameters
275
  // which are split across multiple lines, as illustrated by this example:
276
  //   double a(int x);
277
  //   int    b(int  y,
278
  //          double z);
279
  // In the above example, we need to take special care to ensure that
280
  // 'double z' is indented along with it's owning function 'b'.
281
  // Special handling is required for 'nested' ternary operators.
282
208
  SmallVector<unsigned, 16> ScopeStack;
283
284
1.73k
  for (unsigned i = Start; i != End; 
++i1.52k
) {
285
1.52k
    if (ScopeStack.size() != 0 &&
286
356
        Changes[i].indentAndNestingLevel() <
287
356
            Changes[ScopeStack.back()].indentAndNestingLevel())
288
63
      ScopeStack.pop_back();
289
290
    // Compare current token to previous non-comment token to ensure whether
291
    // it is in a deeper scope or not.
292
1.52k
    unsigned PreviousNonComment = i - 1;
293
1.53k
    while (PreviousNonComment > Start &&
294
1.11k
           Changes[PreviousNonComment].Tok->is(tok::comment))
295
4
      PreviousNonComment--;
296
1.52k
    if (i != Start && Changes[i].indentAndNestingLevel() >
297
1.32k
                          Changes[PreviousNonComment].indentAndNestingLevel())
298
77
      ScopeStack.push_back(i);
299
300
1.52k
    bool InsideNestedScope = ScopeStack.size() != 0;
301
302
1.52k
    if (Changes[i].NewlinesBefore > 0 && 
!InsideNestedScope374
) {
303
300
      Shift = 0;
304
300
      FoundMatchOnLine = false;
305
300
    }
306
307
    // If this is the first matching token to be aligned, remember by how many
308
    // spaces it has to be shifted, so the rest of the changes on the line are
309
    // shifted by the same amount
310
1.52k
    if (!FoundMatchOnLine && 
!InsideNestedScope533
&&
Matches(Changes[i])533
) {
311
443
      FoundMatchOnLine = true;
312
443
      Shift = Column - Changes[i].StartOfTokenColumn;
313
443
      Changes[i].Spaces += Shift;
314
443
    }
315
316
    // This is for function parameters that are split across multiple lines,
317
    // as mentioned in the ScopeStack comment.
318
1.52k
    if (InsideNestedScope && 
Changes[i].NewlinesBefore > 0370
) {
319
74
      unsigned ScopeStart = ScopeStack.back();
320
74
      if (Changes[ScopeStart - 1].Tok->is(TT_FunctionDeclarationName) ||
321
74
          (ScopeStart > Start + 1 &&
322
59
           Changes[ScopeStart - 2].Tok->is(TT_FunctionDeclarationName)) ||
323
74
          Changes[i].Tok->is(TT_ConditionalExpr) ||
324
74
          (Changes[i].Tok->Previous &&
325
72
           Changes[i].Tok->Previous->is(TT_ConditionalExpr)))
326
54
        Changes[i].Spaces += Shift;
327
74
    }
328
329
1.52k
    assert(Shift >= 0);
330
1.52k
    Changes[i].StartOfTokenColumn += Shift;
331
1.52k
    if (i + 1 != Changes.size())
332
1.52k
      Changes[i + 1].PreviousEndOfTokenColumn += Shift;
333
1.52k
  }
334
208
}
335
336
// Walk through a subset of the changes, starting at StartAt, and find
337
// sequences of matching tokens to align. To do so, keep track of the lines and
338
// whether or not a matching token was found on a line. If a matching token is
339
// found, extend the current sequence. If the current line cannot be part of a
340
// sequence, e.g. because there is an empty line before it or it contains only
341
// non-matching tokens, finalize the previous sequence.
342
// The value returned is the token on which we stopped, either because we
343
// exhausted all items inside Changes, or because we hit a scope level higher
344
// than our initial scope.
345
// This function is recursive. Each invocation processes only the scope level
346
// equal to the initial level, which is the level of Changes[StartAt].
347
// If we encounter a scope level greater than the initial level, then we call
348
// ourselves recursively, thereby avoiding the pollution of the current state
349
// with the alignment requirements of the nested sub-level. This recursive
350
// behavior is necessary for aligning function prototypes that have one or more
351
// arguments.
352
// If this function encounters a scope level less than the initial level,
353
// it returns the current position.
354
// There is a non-obvious subtlety in the recursive behavior: Even though we
355
// defer processing of nested levels to recursive invocations of this
356
// function, when it comes time to align a sequence of tokens, we run the
357
// alignment on the entire sequence, including the nested levels.
358
// When doing so, most of the nested tokens are skipped, because their
359
// alignment was already handled by the recursive invocations of this function.
360
// However, the special exception is that we do NOT skip function parameters
361
// that are split across multiple lines. See the test case in FormatTest.cpp
362
// that mentions "split function parameter alignment" for an example of this.
363
template <typename F>
364
static unsigned AlignTokens(const FormatStyle &Style, F &&Matches,
365
                            SmallVector<WhitespaceManager::Change, 16> &Changes,
366
58.7k
                            unsigned StartAt) {
367
58.7k
  unsigned MinColumn = 0;
368
58.7k
  unsigned MaxColumn = UINT_MAX;
369
370
  // Line number of the start and the end of the current token sequence.
371
58.7k
  unsigned StartOfSequence = 0;
372
58.7k
  unsigned EndOfSequence = 0;
373
374
  // Measure the scope level (i.e. depth of (), [], {}) of the first token, and
375
  // abort when we hit any token in a higher scope than the starting one.
376
58.7k
  auto IndentAndNestingLevel = StartAt < Changes.size()
377
58.7k
                                   ? Changes[StartAt].indentAndNestingLevel()
378
0
                                   : std::tuple<unsigned, unsigned, unsigned>();
379
380
  // Keep track of the number of commas before the matching tokens, we will only
381
  // align a sequence of matching tokens if they are preceded by the same number
382
  // of commas.
383
58.7k
  unsigned CommasBeforeLastMatch = 0;
384
58.7k
  unsigned CommasBeforeMatch = 0;
385
386
  // Whether a matching token has been found on the current line.
387
58.7k
  bool FoundMatchOnLine = false;
388
389
  // Aligns a sequence of matching tokens, on the MinColumn column.
390
  //
391
  // Sequences start from the first matching token to align, and end at the
392
  // first token of the first line that doesn't need to be aligned.
393
  //
394
  // We need to adjust the StartOfTokenColumn of each Change that is on a line
395
  // containing any matching token to be aligned and located after such token.
396
104k
  auto AlignCurrentSequence = [&] {
397
104k
    if (StartOfSequence > 0 && 
StartOfSequence < EndOfSequence1.34k
)
398
1.18k
      AlignTokenSequence(StartOfSequence, EndOfSequence, MinColumn, Matches,
399
1.18k
                         Changes);
400
104k
    MinColumn = 0;
401
104k
    MaxColumn = UINT_MAX;
402
104k
    StartOfSequence = 0;
403
104k
    EndOfSequence = 0;
404
104k
  };
WhitespaceManager.cpp:unsigned int clang::format::AlignTokens<clang::format::WhitespaceManager::alignConsecutiveAssignments()::$_1>(clang::format::FormatStyle const&, clang::format::WhitespaceManager::alignConsecutiveAssignments()::$_1&&, llvm::SmallVector<clang::format::WhitespaceManager::Change, 16u>&, unsigned int)::'lambda'()::operator()() const
Line
Count
Source
396
621
  auto AlignCurrentSequence = [&] {
397
621
    if (StartOfSequence > 0 && 
StartOfSequence < EndOfSequence157
)
398
154
      AlignTokenSequence(StartOfSequence, EndOfSequence, MinColumn, Matches,
399
154
                         Changes);
400
621
    MinColumn = 0;
401
621
    MaxColumn = UINT_MAX;
402
621
    StartOfSequence = 0;
403
621
    EndOfSequence = 0;
404
621
  };
WhitespaceManager.cpp:unsigned int clang::format::AlignTokens<clang::format::WhitespaceManager::alignConsecutiveAssignments()::$_1&>(clang::format::FormatStyle const&, clang::format::WhitespaceManager::alignConsecutiveAssignments()::$_1&, llvm::SmallVector<clang::format::WhitespaceManager::Change, 16u>&, unsigned int)::'lambda'()::operator()() const
Line
Count
Source
396
982
  auto AlignCurrentSequence = [&] {
397
982
    if (StartOfSequence > 0 && 
StartOfSequence < EndOfSequence127
)
398
115
      AlignTokenSequence(StartOfSequence, EndOfSequence, MinColumn, Matches,
399
115
                         Changes);
400
982
    MinColumn = 0;
401
982
    MaxColumn = UINT_MAX;
402
982
    StartOfSequence = 0;
403
982
    EndOfSequence = 0;
404
982
  };
WhitespaceManager.cpp:unsigned int clang::format::AlignTokens<clang::format::WhitespaceManager::alignConsecutiveBitFields()::$_2>(clang::format::FormatStyle const&, clang::format::WhitespaceManager::alignConsecutiveBitFields()::$_2&&, llvm::SmallVector<clang::format::WhitespaceManager::Change, 16u>&, unsigned int)::'lambda'()::operator()() const
Line
Count
Source
396
30
  auto AlignCurrentSequence = [&] {
397
30
    if (StartOfSequence > 0 && StartOfSequence < EndOfSequence)
398
30
      AlignTokenSequence(StartOfSequence, EndOfSequence, MinColumn, Matches,
399
30
                         Changes);
400
30
    MinColumn = 0;
401
30
    MaxColumn = UINT_MAX;
402
30
    StartOfSequence = 0;
403
30
    EndOfSequence = 0;
404
30
  };
WhitespaceManager.cpp:unsigned int clang::format::AlignTokens<clang::format::WhitespaceManager::alignConsecutiveBitFields()::$_2&>(clang::format::FormatStyle const&, clang::format::WhitespaceManager::alignConsecutiveBitFields()::$_2&, llvm::SmallVector<clang::format::WhitespaceManager::Change, 16u>&, unsigned int)::'lambda'()::operator()() const
Line
Count
Source
396
3
  auto AlignCurrentSequence = [&] {
397
3
    if (StartOfSequence > 0 && 
StartOfSequence < EndOfSequence0
)
398
0
      AlignTokenSequence(StartOfSequence, EndOfSequence, MinColumn, Matches,
399
0
                         Changes);
400
3
    MinColumn = 0;
401
3
    MaxColumn = UINT_MAX;
402
3
    StartOfSequence = 0;
403
3
    EndOfSequence = 0;
404
3
  };
WhitespaceManager.cpp:unsigned int clang::format::AlignTokens<clang::format::WhitespaceManager::alignConsecutiveDeclarations()::$_3>(clang::format::FormatStyle const&, clang::format::WhitespaceManager::alignConsecutiveDeclarations()::$_3&&, llvm::SmallVector<clang::format::WhitespaceManager::Change, 16u>&, unsigned int)::'lambda'()::operator()() const
Line
Count
Source
396
544
  auto AlignCurrentSequence = [&] {
397
544
    if (StartOfSequence > 0 && 
StartOfSequence < EndOfSequence177
)
398
177
      AlignTokenSequence(StartOfSequence, EndOfSequence, MinColumn, Matches,
399
177
                         Changes);
400
544
    MinColumn = 0;
401
544
    MaxColumn = UINT_MAX;
402
544
    StartOfSequence = 0;
403
544
    EndOfSequence = 0;
404
544
  };
WhitespaceManager.cpp:unsigned int clang::format::AlignTokens<clang::format::WhitespaceManager::alignConsecutiveDeclarations()::$_3&>(clang::format::FormatStyle const&, clang::format::WhitespaceManager::alignConsecutiveDeclarations()::$_3&, llvm::SmallVector<clang::format::WhitespaceManager::Change, 16u>&, unsigned int)::'lambda'()::operator()() const
Line
Count
Source
396
984
  auto AlignCurrentSequence = [&] {
397
984
    if (StartOfSequence > 0 && 
StartOfSequence < EndOfSequence148
)
398
136
      AlignTokenSequence(StartOfSequence, EndOfSequence, MinColumn, Matches,
399
136
                         Changes);
400
984
    MinColumn = 0;
401
984
    MaxColumn = UINT_MAX;
402
984
    StartOfSequence = 0;
403
984
    EndOfSequence = 0;
404
984
  };
WhitespaceManager.cpp:unsigned int clang::format::AlignTokens<clang::format::WhitespaceManager::alignChainedConditionals()::$_4>(clang::format::FormatStyle const&, clang::format::WhitespaceManager::alignChainedConditionals()::$_4&&, llvm::SmallVector<clang::format::WhitespaceManager::Change, 16u>&, unsigned int)::'lambda'()::operator()() const
Line
Count
Source
396
37.1k
  auto AlignCurrentSequence = [&] {
397
37.1k
    if (StartOfSequence > 0 && 
StartOfSequence < EndOfSequence23
)
398
16
      AlignTokenSequence(StartOfSequence, EndOfSequence, MinColumn, Matches,
399
16
                         Changes);
400
37.1k
    MinColumn = 0;
401
37.1k
    MaxColumn = UINT_MAX;
402
37.1k
    StartOfSequence = 0;
403
37.1k
    EndOfSequence = 0;
404
37.1k
  };
WhitespaceManager.cpp:unsigned int clang::format::AlignTokens<clang::format::WhitespaceManager::alignChainedConditionals()::$_4&>(clang::format::FormatStyle const&, clang::format::WhitespaceManager::alignChainedConditionals()::$_4&, llvm::SmallVector<clang::format::WhitespaceManager::Change, 16u>&, unsigned int)::'lambda'()::operator()() const
Line
Count
Source
396
56.5k
  auto AlignCurrentSequence = [&] {
397
56.5k
    if (StartOfSequence > 0 && 
StartOfSequence < EndOfSequence470
)
398
345
      AlignTokenSequence(StartOfSequence, EndOfSequence, MinColumn, Matches,
399
345
                         Changes);
400
56.5k
    MinColumn = 0;
401
56.5k
    MaxColumn = UINT_MAX;
402
56.5k
    StartOfSequence = 0;
403
56.5k
    EndOfSequence = 0;
404
56.5k
  };
WhitespaceManager.cpp:unsigned int clang::format::AlignTokens<clang::format::WhitespaceManager::alignChainedConditionals()::$_6>(clang::format::FormatStyle const&, clang::format::WhitespaceManager::alignChainedConditionals()::$_6&&, llvm::SmallVector<clang::format::WhitespaceManager::Change, 16u>&, unsigned int)::'lambda'()::operator()() const
Line
Count
Source
396
2.93k
  auto AlignCurrentSequence = [&] {
397
2.93k
    if (StartOfSequence > 0 && 
StartOfSequence < EndOfSequence14
)
398
14
      AlignTokenSequence(StartOfSequence, EndOfSequence, MinColumn, Matches,
399
14
                         Changes);
400
2.93k
    MinColumn = 0;
401
2.93k
    MaxColumn = UINT_MAX;
402
2.93k
    StartOfSequence = 0;
403
2.93k
    EndOfSequence = 0;
404
2.93k
  };
WhitespaceManager.cpp:unsigned int clang::format::AlignTokens<clang::format::WhitespaceManager::alignChainedConditionals()::$_6&>(clang::format::FormatStyle const&, clang::format::WhitespaceManager::alignChainedConditionals()::$_6&, llvm::SmallVector<clang::format::WhitespaceManager::Change, 16u>&, unsigned int)::'lambda'()::operator()() const
Line
Count
Source
396
4.66k
  auto AlignCurrentSequence = [&] {
397
4.66k
    if (StartOfSequence > 0 && 
StartOfSequence < EndOfSequence197
)
398
194
      AlignTokenSequence(StartOfSequence, EndOfSequence, MinColumn, Matches,
399
194
                         Changes);
400
4.66k
    MinColumn = 0;
401
4.66k
    MaxColumn = UINT_MAX;
402
4.66k
    StartOfSequence = 0;
403
4.66k
    EndOfSequence = 0;
404
4.66k
  };
405
406
58.7k
  unsigned i = StartAt;
407
380k
  for (unsigned e = Changes.size(); i != e; 
++i321k
) {
408
363k
    if (Changes[i].indentAndNestingLevel() < IndentAndNestingLevel)
409
41.6k
      break;
410
411
321k
    if (Changes[i].NewlinesBefore != 0) {
412
46.7k
      CommasBeforeMatch = 0;
413
46.7k
      EndOfSequence = i;
414
      // If there is a blank line, or if the last line didn't contain any
415
      // matching token, the sequence ends here.
416
46.7k
      if (Changes[i].NewlinesBefore > 1 || 
!FoundMatchOnLine46.0k
)
417
45.4k
        AlignCurrentSequence();
418
419
46.7k
      FoundMatchOnLine = false;
420
46.7k
    }
421
422
321k
    if (Changes[i].Tok->is(tok::comma)) {
423
8.40k
      ++CommasBeforeMatch;
424
312k
    } else if (Changes[i].indentAndNestingLevel() > IndentAndNestingLevel) {
425
      // Call AlignTokens recursively, skipping over this scope block.
426
41.6k
      unsigned StoppedAt = AlignTokens(Style, Matches, Changes, i);
427
41.6k
      i = StoppedAt - 1;
428
41.6k
      continue;
429
41.6k
    }
430
431
279k
    if (!Matches(Changes[i]))
432
277k
      continue;
433
434
    // If there is more than one matching token per line, or if the number of
435
    // preceding commas, do not match anymore, end the sequence.
436
2.23k
    if (FoundMatchOnLine || 
CommasBeforeMatch != CommasBeforeLastMatch2.06k
)
437
208
      AlignCurrentSequence();
438
439
2.23k
    CommasBeforeLastMatch = CommasBeforeMatch;
440
2.23k
    FoundMatchOnLine = true;
441
442
2.23k
    if (StartOfSequence == 0)
443
1.32k
      StartOfSequence = i;
444
445
2.23k
    unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn;
446
2.23k
    int LineLengthAfter = Changes[i].TokenLength;
447
10.1k
    for (unsigned j = i + 1; j != e && 
Changes[j].NewlinesBefore == 09.49k
;
++j7.88k
) {
448
7.88k
      LineLengthAfter += Changes[j].Spaces;
449
      // Changes are generally 1:1 with the tokens, but a change could also be
450
      // inside of a token, in which case it's counted more than once: once for
451
      // the whitespace surrounding the token (!IsInsideToken) and once for
452
      // each whitespace change within it (IsInsideToken).
453
      // Therefore, changes inside of a token should only count the space.
454
7.88k
      if (!Changes[j].IsInsideToken)
455
7.88k
        LineLengthAfter += Changes[j].TokenLength;
456
7.88k
    }
457
2.23k
    unsigned ChangeMaxColumn = Style.ColumnLimit - LineLengthAfter;
458
459
    // If we are restricted by the maximum column width, end the sequence.
460
2.23k
    if (ChangeMinColumn > MaxColumn || 
ChangeMaxColumn < MinColumn2.23k
||
461
2.22k
        CommasBeforeLastMatch != CommasBeforeMatch) {
462
18
      AlignCurrentSequence();
463
18
      StartOfSequence = i;
464
18
    }
465
466
2.23k
    MinColumn = std::max(MinColumn, ChangeMinColumn);
467
2.23k
    MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
468
2.23k
  }
469
470
58.7k
  EndOfSequence = i;
471
58.7k
  AlignCurrentSequence();
472
58.7k
  return i;
473
58.7k
}
WhitespaceManager.cpp:unsigned int clang::format::AlignTokens<clang::format::WhitespaceManager::alignConsecutiveAssignments()::$_1>(clang::format::FormatStyle const&, clang::format::WhitespaceManager::alignConsecutiveAssignments()::$_1&&, llvm::SmallVector<clang::format::WhitespaceManager::Change, 16u>&, unsigned int)
Line
Count
Source
366
228
                            unsigned StartAt) {
367
228
  unsigned MinColumn = 0;
368
228
  unsigned MaxColumn = UINT_MAX;
369
370
  // Line number of the start and the end of the current token sequence.
371
228
  unsigned StartOfSequence = 0;
372
228
  unsigned EndOfSequence = 0;
373
374
  // Measure the scope level (i.e. depth of (), [], {}) of the first token, and
375
  // abort when we hit any token in a higher scope than the starting one.
376
228
  auto IndentAndNestingLevel = StartAt < Changes.size()
377
228
                                   ? Changes[StartAt].indentAndNestingLevel()
378
0
                                   : std::tuple<unsigned, unsigned, unsigned>();
379
380
  // Keep track of the number of commas before the matching tokens, we will only
381
  // align a sequence of matching tokens if they are preceded by the same number
382
  // of commas.
383
228
  unsigned CommasBeforeLastMatch = 0;
384
228
  unsigned CommasBeforeMatch = 0;
385
386
  // Whether a matching token has been found on the current line.
387
228
  bool FoundMatchOnLine = false;
388
389
  // Aligns a sequence of matching tokens, on the MinColumn column.
390
  //
391
  // Sequences start from the first matching token to align, and end at the
392
  // first token of the first line that doesn't need to be aligned.
393
  //
394
  // We need to adjust the StartOfTokenColumn of each Change that is on a line
395
  // containing any matching token to be aligned and located after such token.
396
228
  auto AlignCurrentSequence = [&] {
397
228
    if (StartOfSequence > 0 && StartOfSequence < EndOfSequence)
398
228
      AlignTokenSequence(StartOfSequence, EndOfSequence, MinColumn, Matches,
399
228
                         Changes);
400
228
    MinColumn = 0;
401
228
    MaxColumn = UINT_MAX;
402
228
    StartOfSequence = 0;
403
228
    EndOfSequence = 0;
404
228
  };
405
406
228
  unsigned i = StartAt;
407
3.19k
  for (unsigned e = Changes.size(); i != e; 
++i2.96k
) {
408
2.96k
    if (Changes[i].indentAndNestingLevel() < IndentAndNestingLevel)
409
0
      break;
410
411
2.96k
    if (Changes[i].NewlinesBefore != 0) {
412
546
      CommasBeforeMatch = 0;
413
546
      EndOfSequence = i;
414
      // If there is a blank line, or if the last line didn't contain any
415
      // matching token, the sequence ends here.
416
546
      if (Changes[i].NewlinesBefore > 1 || 
!FoundMatchOnLine543
)
417
354
        AlignCurrentSequence();
418
419
546
      FoundMatchOnLine = false;
420
546
    }
421
422
2.96k
    if (Changes[i].Tok->is(tok::comma)) {
423
18
      ++CommasBeforeMatch;
424
2.94k
    } else if (Changes[i].indentAndNestingLevel() > IndentAndNestingLevel) {
425
      // Call AlignTokens recursively, skipping over this scope block.
426
327
      unsigned StoppedAt = AlignTokens(Style, Matches, Changes, i);
427
327
      i = StoppedAt - 1;
428
327
      continue;
429
327
    }
430
431
2.63k
    if (!Matches(Changes[i]))
432
2.38k
      continue;
433
434
    // If there is more than one matching token per line, or if the number of
435
    // preceding commas, do not match anymore, end the sequence.
436
258
    if (FoundMatchOnLine || 
CommasBeforeMatch != CommasBeforeLastMatch246
)
437
33
      AlignCurrentSequence();
438
439
258
    CommasBeforeLastMatch = CommasBeforeMatch;
440
258
    FoundMatchOnLine = true;
441
442
258
    if (StartOfSequence == 0)
443
151
      StartOfSequence = i;
444
445
258
    unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn;
446
258
    int LineLengthAfter = Changes[i].TokenLength;
447
1.01k
    for (unsigned j = i + 1; j != e && 
Changes[j].NewlinesBefore == 0968
;
++j761
) {
448
761
      LineLengthAfter += Changes[j].Spaces;
449
      // Changes are generally 1:1 with the tokens, but a change could also be
450
      // inside of a token, in which case it's counted more than once: once for
451
      // the whitespace surrounding the token (!IsInsideToken) and once for
452
      // each whitespace change within it (IsInsideToken).
453
      // Therefore, changes inside of a token should only count the space.
454
761
      if (!Changes[j].IsInsideToken)
455
760
        LineLengthAfter += Changes[j].TokenLength;
456
761
    }
457
258
    unsigned ChangeMaxColumn = Style.ColumnLimit - LineLengthAfter;
458
459
    // If we are restricted by the maximum column width, end the sequence.
460
258
    if (ChangeMinColumn > MaxColumn || 
ChangeMaxColumn < MinColumn255
||
461
252
        CommasBeforeLastMatch != CommasBeforeMatch) {
462
6
      AlignCurrentSequence();
463
6
      StartOfSequence = i;
464
6
    }
465
466
258
    MinColumn = std::max(MinColumn, ChangeMinColumn);
467
258
    MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
468
258
  }
469
470
228
  EndOfSequence = i;
471
228
  AlignCurrentSequence();
472
228
  return i;
473
228
}
WhitespaceManager.cpp:unsigned int clang::format::AlignTokens<clang::format::WhitespaceManager::alignConsecutiveAssignments()::$_1&>(clang::format::FormatStyle const&, clang::format::WhitespaceManager::alignConsecutiveAssignments()::$_1&, llvm::SmallVector<clang::format::WhitespaceManager::Change, 16u>&, unsigned int)
Line
Count
Source
366
604
                            unsigned StartAt) {
367
604
  unsigned MinColumn = 0;
368
604
  unsigned MaxColumn = UINT_MAX;
369
370
  // Line number of the start and the end of the current token sequence.
371
604
  unsigned StartOfSequence = 0;
372
604
  unsigned EndOfSequence = 0;
373
374
  // Measure the scope level (i.e. depth of (), [], {}) of the first token, and
375
  // abort when we hit any token in a higher scope than the starting one.
376
604
  auto IndentAndNestingLevel = StartAt < Changes.size()
377
604
                                   ? Changes[StartAt].indentAndNestingLevel()
378
0
                                   : std::tuple<unsigned, unsigned, unsigned>();
379
380
  // Keep track of the number of commas before the matching tokens, we will only
381
  // align a sequence of matching tokens if they are preceded by the same number
382
  // of commas.
383
604
  unsigned CommasBeforeLastMatch = 0;
384
604
  unsigned CommasBeforeMatch = 0;
385
386
  // Whether a matching token has been found on the current line.
387
604
  bool FoundMatchOnLine = false;
388
389
  // Aligns a sequence of matching tokens, on the MinColumn column.
390
  //
391
  // Sequences start from the first matching token to align, and end at the
392
  // first token of the first line that doesn't need to be aligned.
393
  //
394
  // We need to adjust the StartOfTokenColumn of each Change that is on a line
395
  // containing any matching token to be aligned and located after such token.
396
604
  auto AlignCurrentSequence = [&] {
397
604
    if (StartOfSequence > 0 && StartOfSequence < EndOfSequence)
398
604
      AlignTokenSequence(StartOfSequence, EndOfSequence, MinColumn, Matches,
399
604
                         Changes);
400
604
    MinColumn = 0;
401
604
    MaxColumn = UINT_MAX;
402
604
    StartOfSequence = 0;
403
604
    EndOfSequence = 0;
404
604
  };
405
406
604
  unsigned i = StartAt;
407
3.40k
  for (unsigned e = Changes.size(); i != e; 
++i2.79k
) {
408
3.40k
    if (Changes[i].indentAndNestingLevel() < IndentAndNestingLevel)
409
604
      break;
410
411
2.79k
    if (Changes[i].NewlinesBefore != 0) {
412
470
      CommasBeforeMatch = 0;
413
470
      EndOfSequence = i;
414
      // If there is a blank line, or if the last line didn't contain any
415
      // matching token, the sequence ends here.
416
470
      if (Changes[i].NewlinesBefore > 1 || 
!FoundMatchOnLine469
)
417
357
        AlignCurrentSequence();
418
419
470
      FoundMatchOnLine = false;
420
470
    }
421
422
2.79k
    if (Changes[i].Tok->is(tok::comma)) {
423
126
      ++CommasBeforeMatch;
424
2.67k
    } else if (Changes[i].indentAndNestingLevel() > IndentAndNestingLevel) {
425
      // Call AlignTokens recursively, skipping over this scope block.
426
277
      unsigned StoppedAt = AlignTokens(Style, Matches, Changes, i);
427
277
      i = StoppedAt - 1;
428
277
      continue;
429
277
    }
430
431
2.51k
    if (!Matches(Changes[i]))
432
2.33k
      continue;
433
434
    // If there is more than one matching token per line, or if the number of
435
    // preceding commas, do not match anymore, end the sequence.
436
188
    if (FoundMatchOnLine || 
CommasBeforeMatch != CommasBeforeLastMatch176
)
437
21
      AlignCurrentSequence();
438
439
188
    CommasBeforeLastMatch = CommasBeforeMatch;
440
188
    FoundMatchOnLine = true;
441
442
188
    if (StartOfSequence == 0)
443
127
      StartOfSequence = i;
444
445
188
    unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn;
446
188
    int LineLengthAfter = Changes[i].TokenLength;
447
856
    for (unsigned j = i + 1; j != e && 
Changes[j].NewlinesBefore == 0847
;
++j668
) {
448
668
      LineLengthAfter += Changes[j].Spaces;
449
      // Changes are generally 1:1 with the tokens, but a change could also be
450
      // inside of a token, in which case it's counted more than once: once for
451
      // the whitespace surrounding the token (!IsInsideToken) and once for
452
      // each whitespace change within it (IsInsideToken).
453
      // Therefore, changes inside of a token should only count the space.
454
668
      if (!Changes[j].IsInsideToken)
455
668
        LineLengthAfter += Changes[j].TokenLength;
456
668
    }
457
188
    unsigned ChangeMaxColumn = Style.ColumnLimit - LineLengthAfter;
458
459
    // If we are restricted by the maximum column width, end the sequence.
460
188
    if (ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn ||
461
188
        CommasBeforeLastMatch != CommasBeforeMatch) {
462
0
      AlignCurrentSequence();
463
0
      StartOfSequence = i;
464
0
    }
465
466
188
    MinColumn = std::max(MinColumn, ChangeMinColumn);
467
188
    MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
468
188
  }
469
470
604
  EndOfSequence = i;
471
604
  AlignCurrentSequence();
472
604
  return i;
473
604
}
WhitespaceManager.cpp:unsigned int clang::format::AlignTokens<clang::format::WhitespaceManager::alignConsecutiveBitFields()::$_2>(clang::format::FormatStyle const&, clang::format::WhitespaceManager::alignConsecutiveBitFields()::$_2&&, llvm::SmallVector<clang::format::WhitespaceManager::Change, 16u>&, unsigned int)
Line
Count
Source
366
30
                            unsigned StartAt) {
367
30
  unsigned MinColumn = 0;
368
30
  unsigned MaxColumn = UINT_MAX;
369
370
  // Line number of the start and the end of the current token sequence.
371
30
  unsigned StartOfSequence = 0;
372
30
  unsigned EndOfSequence = 0;
373
374
  // Measure the scope level (i.e. depth of (), [], {}) of the first token, and
375
  // abort when we hit any token in a higher scope than the starting one.
376
30
  auto IndentAndNestingLevel = StartAt < Changes.size()
377
30
                                   ? Changes[StartAt].indentAndNestingLevel()
378
0
                                   : std::tuple<unsigned, unsigned, unsigned>();
379
380
  // Keep track of the number of commas before the matching tokens, we will only
381
  // align a sequence of matching tokens if they are preceded by the same number
382
  // of commas.
383
30
  unsigned CommasBeforeLastMatch = 0;
384
30
  unsigned CommasBeforeMatch = 0;
385
386
  // Whether a matching token has been found on the current line.
387
30
  bool FoundMatchOnLine = false;
388
389
  // Aligns a sequence of matching tokens, on the MinColumn column.
390
  //
391
  // Sequences start from the first matching token to align, and end at the
392
  // first token of the first line that doesn't need to be aligned.
393
  //
394
  // We need to adjust the StartOfTokenColumn of each Change that is on a line
395
  // containing any matching token to be aligned and located after such token.
396
30
  auto AlignCurrentSequence = [&] {
397
30
    if (StartOfSequence > 0 && StartOfSequence < EndOfSequence)
398
30
      AlignTokenSequence(StartOfSequence, EndOfSequence, MinColumn, Matches,
399
30
                         Changes);
400
30
    MinColumn = 0;
401
30
    MaxColumn = UINT_MAX;
402
30
    StartOfSequence = 0;
403
30
    EndOfSequence = 0;
404
30
  };
405
406
30
  unsigned i = StartAt;
407
447
  for (unsigned e = Changes.size(); i != e; 
++i417
) {
408
417
    if (Changes[i].indentAndNestingLevel() < IndentAndNestingLevel)
409
0
      break;
410
411
417
    if (Changes[i].NewlinesBefore != 0) {
412
30
      CommasBeforeMatch = 0;
413
30
      EndOfSequence = i;
414
      // If there is a blank line, or if the last line didn't contain any
415
      // matching token, the sequence ends here.
416
30
      if (Changes[i].NewlinesBefore > 1 || !FoundMatchOnLine)
417
0
        AlignCurrentSequence();
418
419
30
      FoundMatchOnLine = false;
420
30
    }
421
422
417
    if (Changes[i].Tok->is(tok::comma)) {
423
0
      ++CommasBeforeMatch;
424
417
    } else if (Changes[i].indentAndNestingLevel() > IndentAndNestingLevel) {
425
      // Call AlignTokens recursively, skipping over this scope block.
426
3
      unsigned StoppedAt = AlignTokens(Style, Matches, Changes, i);
427
3
      i = StoppedAt - 1;
428
3
      continue;
429
3
    }
430
431
414
    if (!Matches(Changes[i]))
432
354
      continue;
433
434
    // If there is more than one matching token per line, or if the number of
435
    // preceding commas, do not match anymore, end the sequence.
436
60
    if (FoundMatchOnLine || CommasBeforeMatch != CommasBeforeLastMatch)
437
0
      AlignCurrentSequence();
438
439
60
    CommasBeforeLastMatch = CommasBeforeMatch;
440
60
    FoundMatchOnLine = true;
441
442
60
    if (StartOfSequence == 0)
443
30
      StartOfSequence = i;
444
445
60
    unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn;
446
60
    int LineLengthAfter = Changes[i].TokenLength;
447
270
    for (unsigned j = i + 1; j != e && 
Changes[j].NewlinesBefore == 0240
;
++j210
) {
448
210
      LineLengthAfter += Changes[j].Spaces;
449
      // Changes are generally 1:1 with the tokens, but a change could also be
450
      // inside of a token, in which case it's counted more than once: once for
451
      // the whitespace surrounding the token (!IsInsideToken) and once for
452
      // each whitespace change within it (IsInsideToken).
453
      // Therefore, changes inside of a token should only count the space.
454
210
      if (!Changes[j].IsInsideToken)
455
210
        LineLengthAfter += Changes[j].TokenLength;
456
210
    }
457
60
    unsigned ChangeMaxColumn = Style.ColumnLimit - LineLengthAfter;
458
459
    // If we are restricted by the maximum column width, end the sequence.
460
60
    if (ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn ||
461
60
        CommasBeforeLastMatch != CommasBeforeMatch) {
462
0
      AlignCurrentSequence();
463
0
      StartOfSequence = i;
464
0
    }
465
466
60
    MinColumn = std::max(MinColumn, ChangeMinColumn);
467
60
    MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
468
60
  }
469
470
30
  EndOfSequence = i;
471
30
  AlignCurrentSequence();
472
30
  return i;
473
30
}
WhitespaceManager.cpp:unsigned int clang::format::AlignTokens<clang::format::WhitespaceManager::alignConsecutiveBitFields()::$_2&>(clang::format::FormatStyle const&, clang::format::WhitespaceManager::alignConsecutiveBitFields()::$_2&, llvm::SmallVector<clang::format::WhitespaceManager::Change, 16u>&, unsigned int)
Line
Count
Source
366
3
                            unsigned StartAt) {
367
3
  unsigned MinColumn = 0;
368
3
  unsigned MaxColumn = UINT_MAX;
369
370
  // Line number of the start and the end of the current token sequence.
371
3
  unsigned StartOfSequence = 0;
372
3
  unsigned EndOfSequence = 0;
373
374
  // Measure the scope level (i.e. depth of (), [], {}) of the first token, and
375
  // abort when we hit any token in a higher scope than the starting one.
376
3
  auto IndentAndNestingLevel = StartAt < Changes.size()
377
3
                                   ? Changes[StartAt].indentAndNestingLevel()
378
0
                                   : std::tuple<unsigned, unsigned, unsigned>();
379
380
  // Keep track of the number of commas before the matching tokens, we will only
381
  // align a sequence of matching tokens if they are preceded by the same number
382
  // of commas.
383
3
  unsigned CommasBeforeLastMatch = 0;
384
3
  unsigned CommasBeforeMatch = 0;
385
386
  // Whether a matching token has been found on the current line.
387
3
  bool FoundMatchOnLine = false;
388
389
  // Aligns a sequence of matching tokens, on the MinColumn column.
390
  //
391
  // Sequences start from the first matching token to align, and end at the
392
  // first token of the first line that doesn't need to be aligned.
393
  //
394
  // We need to adjust the StartOfTokenColumn of each Change that is on a line
395
  // containing any matching token to be aligned and located after such token.
396
3
  auto AlignCurrentSequence = [&] {
397
3
    if (StartOfSequence > 0 && StartOfSequence < EndOfSequence)
398
3
      AlignTokenSequence(StartOfSequence, EndOfSequence, MinColumn, Matches,
399
3
                         Changes);
400
3
    MinColumn = 0;
401
3
    MaxColumn = UINT_MAX;
402
3
    StartOfSequence = 0;
403
3
    EndOfSequence = 0;
404
3
  };
405
406
3
  unsigned i = StartAt;
407
9
  for (unsigned e = Changes.size(); i != e; 
++i6
) {
408
9
    if (Changes[i].indentAndNestingLevel() < IndentAndNestingLevel)
409
3
      break;
410
411
6
    if (Changes[i].NewlinesBefore != 0) {
412
0
      CommasBeforeMatch = 0;
413
0
      EndOfSequence = i;
414
      // If there is a blank line, or if the last line didn't contain any
415
      // matching token, the sequence ends here.
416
0
      if (Changes[i].NewlinesBefore > 1 || !FoundMatchOnLine)
417
0
        AlignCurrentSequence();
418
419
0
      FoundMatchOnLine = false;
420
0
    }
421
422
6
    if (Changes[i].Tok->is(tok::comma)) {
423
0
      ++CommasBeforeMatch;
424
6
    } else if (Changes[i].indentAndNestingLevel() > IndentAndNestingLevel) {
425
      // Call AlignTokens recursively, skipping over this scope block.
426
0
      unsigned StoppedAt = AlignTokens(Style, Matches, Changes, i);
427
0
      i = StoppedAt - 1;
428
0
      continue;
429
0
    }
430
431
6
    if (!Matches(Changes[i]))
432
6
      continue;
433
434
    // If there is more than one matching token per line, or if the number of
435
    // preceding commas, do not match anymore, end the sequence.
436
0
    if (FoundMatchOnLine || CommasBeforeMatch != CommasBeforeLastMatch)
437
0
      AlignCurrentSequence();
438
439
0
    CommasBeforeLastMatch = CommasBeforeMatch;
440
0
    FoundMatchOnLine = true;
441
442
0
    if (StartOfSequence == 0)
443
0
      StartOfSequence = i;
444
445
0
    unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn;
446
0
    int LineLengthAfter = Changes[i].TokenLength;
447
0
    for (unsigned j = i + 1; j != e && Changes[j].NewlinesBefore == 0; ++j) {
448
0
      LineLengthAfter += Changes[j].Spaces;
449
      // Changes are generally 1:1 with the tokens, but a change could also be
450
      // inside of a token, in which case it's counted more than once: once for
451
      // the whitespace surrounding the token (!IsInsideToken) and once for
452
      // each whitespace change within it (IsInsideToken).
453
      // Therefore, changes inside of a token should only count the space.
454
0
      if (!Changes[j].IsInsideToken)
455
0
        LineLengthAfter += Changes[j].TokenLength;
456
0
    }
457
0
    unsigned ChangeMaxColumn = Style.ColumnLimit - LineLengthAfter;
458
459
    // If we are restricted by the maximum column width, end the sequence.
460
0
    if (ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn ||
461
0
        CommasBeforeLastMatch != CommasBeforeMatch) {
462
0
      AlignCurrentSequence();
463
0
      StartOfSequence = i;
464
0
    }
465
466
0
    MinColumn = std::max(MinColumn, ChangeMinColumn);
467
0
    MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
468
0
  }
469
470
3
  EndOfSequence = i;
471
3
  AlignCurrentSequence();
472
3
  return i;
473
3
}
WhitespaceManager.cpp:unsigned int clang::format::AlignTokens<clang::format::WhitespaceManager::alignConsecutiveDeclarations()::$_3>(clang::format::FormatStyle const&, clang::format::WhitespaceManager::alignConsecutiveDeclarations()::$_3&&, llvm::SmallVector<clang::format::WhitespaceManager::Change, 16u>&, unsigned int)
Line
Count
Source
366
243
                            unsigned StartAt) {
367
243
  unsigned MinColumn = 0;
368
243
  unsigned MaxColumn = UINT_MAX;
369
370
  // Line number of the start and the end of the current token sequence.
371
243
  unsigned StartOfSequence = 0;
372
243
  unsigned EndOfSequence = 0;
373
374
  // Measure the scope level (i.e. depth of (), [], {}) of the first token, and
375
  // abort when we hit any token in a higher scope than the starting one.
376
243
  auto IndentAndNestingLevel = StartAt < Changes.size()
377
243
                                   ? Changes[StartAt].indentAndNestingLevel()
378
0
                                   : std::tuple<unsigned, unsigned, unsigned>();
379
380
  // Keep track of the number of commas before the matching tokens, we will only
381
  // align a sequence of matching tokens if they are preceded by the same number
382
  // of commas.
383
243
  unsigned CommasBeforeLastMatch = 0;
384
243
  unsigned CommasBeforeMatch = 0;
385
386
  // Whether a matching token has been found on the current line.
387
243
  bool FoundMatchOnLine = false;
388
389
  // Aligns a sequence of matching tokens, on the MinColumn column.
390
  //
391
  // Sequences start from the first matching token to align, and end at the
392
  // first token of the first line that doesn't need to be aligned.
393
  //
394
  // We need to adjust the StartOfTokenColumn of each Change that is on a line
395
  // containing any matching token to be aligned and located after such token.
396
243
  auto AlignCurrentSequence = [&] {
397
243
    if (StartOfSequence > 0 && StartOfSequence < EndOfSequence)
398
243
      AlignTokenSequence(StartOfSequence, EndOfSequence, MinColumn, Matches,
399
243
                         Changes);
400
243
    MinColumn = 0;
401
243
    MaxColumn = UINT_MAX;
402
243
    StartOfSequence = 0;
403
243
    EndOfSequence = 0;
404
243
  };
405
406
243
  unsigned i = StartAt;
407
3.32k
  for (unsigned e = Changes.size(); i != e; 
++i3.08k
) {
408
3.08k
    if (Changes[i].indentAndNestingLevel() < IndentAndNestingLevel)
409
0
      break;
410
411
3.08k
    if (Changes[i].NewlinesBefore != 0) {
412
531
      CommasBeforeMatch = 0;
413
531
      EndOfSequence = i;
414
      // If there is a blank line, or if the last line didn't contain any
415
      // matching token, the sequence ends here.
416
531
      if (Changes[i].NewlinesBefore > 1 || 
!FoundMatchOnLine528
)
417
295
        AlignCurrentSequence();
418
419
531
      FoundMatchOnLine = false;
420
531
    }
421
422
3.08k
    if (Changes[i].Tok->is(tok::comma)) {
423
18
      ++CommasBeforeMatch;
424
3.06k
    } else if (Changes[i].indentAndNestingLevel() > IndentAndNestingLevel) {
425
      // Call AlignTokens recursively, skipping over this scope block.
426
353
      unsigned StoppedAt = AlignTokens(Style, Matches, Changes, i);
427
353
      i = StoppedAt - 1;
428
353
      continue;
429
353
    }
430
431
2.72k
    if (!Matches(Changes[i]))
432
2.41k
      continue;
433
434
    // If there is more than one matching token per line, or if the number of
435
    // preceding commas, do not match anymore, end the sequence.
436
314
    if (FoundMatchOnLine || CommasBeforeMatch != CommasBeforeLastMatch)
437
0
      AlignCurrentSequence();
438
439
314
    CommasBeforeLastMatch = CommasBeforeMatch;
440
314
    FoundMatchOnLine = true;
441
442
314
    if (StartOfSequence == 0)
443
171
      StartOfSequence = i;
444
445
314
    unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn;
446
314
    int LineLengthAfter = Changes[i].TokenLength;
447
1.76k
    for (unsigned j = i + 1; j != e && 
Changes[j].NewlinesBefore == 01.68k
;
++j1.44k
) {
448
1.44k
      LineLengthAfter += Changes[j].Spaces;
449
      // Changes are generally 1:1 with the tokens, but a change could also be
450
      // inside of a token, in which case it's counted more than once: once for
451
      // the whitespace surrounding the token (!IsInsideToken) and once for
452
      // each whitespace change within it (IsInsideToken).
453
      // Therefore, changes inside of a token should only count the space.
454
1.44k
      if (!Changes[j].IsInsideToken)
455
1.44k
        LineLengthAfter += Changes[j].TokenLength;
456
1.44k
    }
457
314
    unsigned ChangeMaxColumn = Style.ColumnLimit - LineLengthAfter;
458
459
    // If we are restricted by the maximum column width, end the sequence.
460
314
    if (ChangeMinColumn > MaxColumn || 
ChangeMaxColumn < MinColumn311
||
461
308
        CommasBeforeLastMatch != CommasBeforeMatch) {
462
6
      AlignCurrentSequence();
463
6
      StartOfSequence = i;
464
6
    }
465
466
314
    MinColumn = std::max(MinColumn, ChangeMinColumn);
467
314
    MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
468
314
  }
469
470
243
  EndOfSequence = i;
471
243
  AlignCurrentSequence();
472
243
  return i;
473
243
}
WhitespaceManager.cpp:unsigned int clang::format::AlignTokens<clang::format::WhitespaceManager::alignConsecutiveDeclarations()::$_3&>(clang::format::FormatStyle const&, clang::format::WhitespaceManager::alignConsecutiveDeclarations()::$_3&, llvm::SmallVector<clang::format::WhitespaceManager::Change, 16u>&, unsigned int)
Line
Count
Source
366
639
                            unsigned StartAt) {
367
639
  unsigned MinColumn = 0;
368
639
  unsigned MaxColumn = UINT_MAX;
369
370
  // Line number of the start and the end of the current token sequence.
371
639
  unsigned StartOfSequence = 0;
372
639
  unsigned EndOfSequence = 0;
373
374
  // Measure the scope level (i.e. depth of (), [], {}) of the first token, and
375
  // abort when we hit any token in a higher scope than the starting one.
376
639
  auto IndentAndNestingLevel = StartAt < Changes.size()
377
639
                                   ? Changes[StartAt].indentAndNestingLevel()
378
0
                                   : std::tuple<unsigned, unsigned, unsigned>();
379
380
  // Keep track of the number of commas before the matching tokens, we will only
381
  // align a sequence of matching tokens if they are preceded by the same number
382
  // of commas.
383
639
  unsigned CommasBeforeLastMatch = 0;
384
639
  unsigned CommasBeforeMatch = 0;
385
386
  // Whether a matching token has been found on the current line.
387
639
  bool FoundMatchOnLine = false;
388
389
  // Aligns a sequence of matching tokens, on the MinColumn column.
390
  //
391
  // Sequences start from the first matching token to align, and end at the
392
  // first token of the first line that doesn't need to be aligned.
393
  //
394
  // We need to adjust the StartOfTokenColumn of each Change that is on a line
395
  // containing any matching token to be aligned and located after such token.
396
639
  auto AlignCurrentSequence = [&] {
397
639
    if (StartOfSequence > 0 && StartOfSequence < EndOfSequence)
398
639
      AlignTokenSequence(StartOfSequence, EndOfSequence, MinColumn, Matches,
399
639
                         Changes);
400
639
    MinColumn = 0;
401
639
    MaxColumn = UINT_MAX;
402
639
    StartOfSequence = 0;
403
639
    EndOfSequence = 0;
404
639
  };
405
406
639
  unsigned i = StartAt;
407
3.58k
  for (unsigned e = Changes.size(); i != e; 
++i2.94k
) {
408
3.58k
    if (Changes[i].indentAndNestingLevel() < IndentAndNestingLevel)
409
639
      break;
410
411
2.94k
    if (Changes[i].NewlinesBefore != 0) {
412
491
      CommasBeforeMatch = 0;
413
491
      EndOfSequence = i;
414
      // If there is a blank line, or if the last line didn't contain any
415
      // matching token, the sequence ends here.
416
491
      if (Changes[i].NewlinesBefore > 1 || 
!FoundMatchOnLine490
)
417
330
        AlignCurrentSequence();
418
419
491
      FoundMatchOnLine = false;
420
491
    }
421
422
2.94k
    if (Changes[i].Tok->is(tok::comma)) {
423
150
      ++CommasBeforeMatch;
424
2.79k
    } else if (Changes[i].indentAndNestingLevel() > IndentAndNestingLevel) {
425
      // Call AlignTokens recursively, skipping over this scope block.
426
286
      unsigned StoppedAt = AlignTokens(Style, Matches, Changes, i);
427
286
      i = StoppedAt - 1;
428
286
      continue;
429
286
    }
430
431
2.65k
    if (!Matches(Changes[i]))
432
2.40k
      continue;
433
434
    // If there is more than one matching token per line, or if the number of
435
    // preceding commas, do not match anymore, end the sequence.
436
254
    if (FoundMatchOnLine || 
CommasBeforeMatch != CommasBeforeLastMatch242
)
437
15
      AlignCurrentSequence();
438
439
254
    CommasBeforeLastMatch = CommasBeforeMatch;
440
254
    FoundMatchOnLine = true;
441
442
254
    if (StartOfSequence == 0)
443
148
      StartOfSequence = i;
444
445
254
    unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn;
446
254
    int LineLengthAfter = Changes[i].TokenLength;
447
1.19k
    for (unsigned j = i + 1; j != e && 
Changes[j].NewlinesBefore == 01.17k
;
++j939
) {
448
939
      LineLengthAfter += Changes[j].Spaces;
449
      // Changes are generally 1:1 with the tokens, but a change could also be
450
      // inside of a token, in which case it's counted more than once: once for
451
      // the whitespace surrounding the token (!IsInsideToken) and once for
452
      // each whitespace change within it (IsInsideToken).
453
      // Therefore, changes inside of a token should only count the space.
454
939
      if (!Changes[j].IsInsideToken)
455
939
        LineLengthAfter += Changes[j].TokenLength;
456
939
    }
457
254
    unsigned ChangeMaxColumn = Style.ColumnLimit - LineLengthAfter;
458
459
    // If we are restricted by the maximum column width, end the sequence.
460
254
    if (ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn ||
461
254
        CommasBeforeLastMatch != CommasBeforeMatch) {
462
0
      AlignCurrentSequence();
463
0
      StartOfSequence = i;
464
0
    }
465
466
254
    MinColumn = std::max(MinColumn, ChangeMinColumn);
467
254
    MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
468
254
  }
469
470
639
  EndOfSequence = i;
471
639
  AlignCurrentSequence();
472
639
  return i;
473
639
}
WhitespaceManager.cpp:unsigned int clang::format::AlignTokens<clang::format::WhitespaceManager::alignChainedConditionals()::$_4>(clang::format::FormatStyle const&, clang::format::WhitespaceManager::alignChainedConditionals()::$_4&&, llvm::SmallVector<clang::format::WhitespaceManager::Change, 16u>&, unsigned int)
Line
Count
Source
366
15.0k
                            unsigned StartAt) {
367
15.0k
  unsigned MinColumn = 0;
368
15.0k
  unsigned MaxColumn = UINT_MAX;
369
370
  // Line number of the start and the end of the current token sequence.
371
15.0k
  unsigned StartOfSequence = 0;
372
15.0k
  unsigned EndOfSequence = 0;
373
374
  // Measure the scope level (i.e. depth of (), [], {}) of the first token, and
375
  // abort when we hit any token in a higher scope than the starting one.
376
15.0k
  auto IndentAndNestingLevel = StartAt < Changes.size()
377
15.0k
                                   ? Changes[StartAt].indentAndNestingLevel()
378
0
                                   : std::tuple<unsigned, unsigned, unsigned>();
379
380
  // Keep track of the number of commas before the matching tokens, we will only
381
  // align a sequence of matching tokens if they are preceded by the same number
382
  // of commas.
383
15.0k
  unsigned CommasBeforeLastMatch = 0;
384
15.0k
  unsigned CommasBeforeMatch = 0;
385
386
  // Whether a matching token has been found on the current line.
387
15.0k
  bool FoundMatchOnLine = false;
388
389
  // Aligns a sequence of matching tokens, on the MinColumn column.
390
  //
391
  // Sequences start from the first matching token to align, and end at the
392
  // first token of the first line that doesn't need to be aligned.
393
  //
394
  // We need to adjust the StartOfTokenColumn of each Change that is on a line
395
  // containing any matching token to be aligned and located after such token.
396
15.0k
  auto AlignCurrentSequence = [&] {
397
15.0k
    if (StartOfSequence > 0 && StartOfSequence < EndOfSequence)
398
15.0k
      AlignTokenSequence(StartOfSequence, EndOfSequence, MinColumn, Matches,
399
15.0k
                         Changes);
400
15.0k
    MinColumn = 0;
401
15.0k
    MaxColumn = UINT_MAX;
402
15.0k
    StartOfSequence = 0;
403
15.0k
    EndOfSequence = 0;
404
15.0k
  };
405
406
15.0k
  unsigned i = StartAt;
407
161k
  for (unsigned e = Changes.size(); i != e; 
++i146k
) {
408
146k
    if (Changes[i].indentAndNestingLevel() < IndentAndNestingLevel)
409
24
      break;
410
411
146k
    if (Changes[i].NewlinesBefore != 0) {
412
22.1k
      CommasBeforeMatch = 0;
413
22.1k
      EndOfSequence = i;
414
      // If there is a blank line, or if the last line didn't contain any
415
      // matching token, the sequence ends here.
416
22.1k
      if (Changes[i].NewlinesBefore > 1 || 
!FoundMatchOnLine21.5k
)
417
22.0k
        AlignCurrentSequence();
418
419
22.1k
      FoundMatchOnLine = false;
420
22.1k
    }
421
422
146k
    if (Changes[i].Tok->is(tok::comma)) {
423
778
      ++CommasBeforeMatch;
424
145k
    } else if (Changes[i].indentAndNestingLevel() > IndentAndNestingLevel) {
425
      // Call AlignTokens recursively, skipping over this scope block.
426
22.9k
      unsigned StoppedAt = AlignTokens(Style, Matches, Changes, i);
427
22.9k
      i = StoppedAt - 1;
428
22.9k
      continue;
429
22.9k
    }
430
431
123k
    if (!Matches(Changes[i]))
432
123k
      continue;
433
434
    // If there is more than one matching token per line, or if the number of
435
    // preceding commas, do not match anymore, end the sequence.
436
24
    if (FoundMatchOnLine || 
CommasBeforeMatch != CommasBeforeLastMatch17
)
437
7
      AlignCurrentSequence();
438
439
24
    CommasBeforeLastMatch = CommasBeforeMatch;
440
24
    FoundMatchOnLine = true;
441
442
24
    if (StartOfSequence == 0)
443
24
      StartOfSequence = i;
444
445
24
    unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn;
446
24
    int LineLengthAfter = Changes[i].TokenLength;
447
158
    for (unsigned j = i + 1; j != e && 
Changes[j].NewlinesBefore == 0137
;
++j134
) {
448
134
      LineLengthAfter += Changes[j].Spaces;
449
      // Changes are generally 1:1 with the tokens, but a change could also be
450
      // inside of a token, in which case it's counted more than once: once for
451
      // the whitespace surrounding the token (!IsInsideToken) and once for
452
      // each whitespace change within it (IsInsideToken).
453
      // Therefore, changes inside of a token should only count the space.
454
134
      if (!Changes[j].IsInsideToken)
455
134
        LineLengthAfter += Changes[j].TokenLength;
456
134
    }
457
24
    unsigned ChangeMaxColumn = Style.ColumnLimit - LineLengthAfter;
458
459
    // If we are restricted by the maximum column width, end the sequence.
460
24
    if (ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn ||
461
24
        CommasBeforeLastMatch != CommasBeforeMatch) {
462
0
      AlignCurrentSequence();
463
0
      StartOfSequence = i;
464
0
    }
465
466
24
    MinColumn = std::max(MinColumn, ChangeMinColumn);
467
24
    MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
468
24
  }
469
470
15.0k
  EndOfSequence = i;
471
15.0k
  AlignCurrentSequence();
472
15.0k
  return i;
473
15.0k
}
WhitespaceManager.cpp:unsigned int clang::format::AlignTokens<clang::format::WhitespaceManager::alignChainedConditionals()::$_4&>(clang::format::FormatStyle const&, clang::format::WhitespaceManager::alignChainedConditionals()::$_4&, llvm::SmallVector<clang::format::WhitespaceManager::Change, 16u>&, unsigned int)
Line
Count
Source
366
37.5k
                            unsigned StartAt) {
367
37.5k
  unsigned MinColumn = 0;
368
37.5k
  unsigned MaxColumn = UINT_MAX;
369
370
  // Line number of the start and the end of the current token sequence.
371
37.5k
  unsigned StartOfSequence = 0;
372
37.5k
  unsigned EndOfSequence = 0;
373
374
  // Measure the scope level (i.e. depth of (), [], {}) of the first token, and
375
  // abort when we hit any token in a higher scope than the starting one.
376
37.5k
  auto IndentAndNestingLevel = StartAt < Changes.size()
377
37.5k
                                   ? Changes[StartAt].indentAndNestingLevel()
378
0
                                   : std::tuple<unsigned, unsigned, unsigned>();
379
380
  // Keep track of the number of commas before the matching tokens, we will only
381
  // align a sequence of matching tokens if they are preceded by the same number
382
  // of commas.
383
37.5k
  unsigned CommasBeforeLastMatch = 0;
384
37.5k
  unsigned CommasBeforeMatch = 0;
385
386
  // Whether a matching token has been found on the current line.
387
37.5k
  bool FoundMatchOnLine = false;
388
389
  // Aligns a sequence of matching tokens, on the MinColumn column.
390
  //
391
  // Sequences start from the first matching token to align, and end at the
392
  // first token of the first line that doesn't need to be aligned.
393
  //
394
  // We need to adjust the StartOfTokenColumn of each Change that is on a line
395
  // containing any matching token to be aligned and located after such token.
396
37.5k
  auto AlignCurrentSequence = [&] {
397
37.5k
    if (StartOfSequence > 0 && StartOfSequence < EndOfSequence)
398
37.5k
      AlignTokenSequence(StartOfSequence, EndOfSequence, MinColumn, Matches,
399
37.5k
                         Changes);
400
37.5k
    MinColumn = 0;
401
37.5k
    MaxColumn = UINT_MAX;
402
37.5k
    StartOfSequence = 0;
403
37.5k
    EndOfSequence = 0;
404
37.5k
  };
405
406
37.5k
  unsigned i = StartAt;
407
178k
  for (unsigned e = Changes.size(); i != e; 
++i140k
) {
408
178k
    if (Changes[i].indentAndNestingLevel() < IndentAndNestingLevel)
409
37.5k
      break;
410
411
140k
    if (Changes[i].NewlinesBefore != 0) {
412
19.0k
      CommasBeforeMatch = 0;
413
19.0k
      EndOfSequence = i;
414
      // If there is a blank line, or if the last line didn't contain any
415
      // matching token, the sequence ends here.
416
19.0k
      if (Changes[i].NewlinesBefore > 1 || 
!FoundMatchOnLine18.9k
)
417
18.8k
        AlignCurrentSequence();
418
419
19.0k
      FoundMatchOnLine = false;
420
19.0k
    }
421
422
140k
    if (Changes[i].Tok->is(tok::comma)) {
423
6.70k
      ++CommasBeforeMatch;
424
133k
    } else if (Changes[i].indentAndNestingLevel() > IndentAndNestingLevel) {
425
      // Call AlignTokens recursively, skipping over this scope block.
426
14.5k
      unsigned StoppedAt = AlignTokens(Style, Matches, Changes, i);
427
14.5k
      i = StoppedAt - 1;
428
14.5k
      continue;
429
14.5k
    }
430
431
126k
    if (!Matches(Changes[i]))
432
125k
      continue;
433
434
    // If there is more than one matching token per line, or if the number of
435
    // preceding commas, do not match anymore, end the sequence.
436
695
    if (FoundMatchOnLine || 
CommasBeforeMatch != CommasBeforeLastMatch570
)
437
129
      AlignCurrentSequence();
438
439
695
    CommasBeforeLastMatch = CommasBeforeMatch;
440
695
    FoundMatchOnLine = true;
441
442
695
    if (StartOfSequence == 0)
443
470
      StartOfSequence = i;
444
445
695
    unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn;
446
695
    int LineLengthAfter = Changes[i].TokenLength;
447
3.24k
    for (unsigned j = i + 1; j != e && 
Changes[j].NewlinesBefore == 02.93k
;
++j2.54k
) {
448
2.54k
      LineLengthAfter += Changes[j].Spaces;
449
      // Changes are generally 1:1 with the tokens, but a change could also be
450
      // inside of a token, in which case it's counted more than once: once for
451
      // the whitespace surrounding the token (!IsInsideToken) and once for
452
      // each whitespace change within it (IsInsideToken).
453
      // Therefore, changes inside of a token should only count the space.
454
2.54k
      if (!Changes[j].IsInsideToken)
455
2.54k
        LineLengthAfter += Changes[j].TokenLength;
456
2.54k
    }
457
695
    unsigned ChangeMaxColumn = Style.ColumnLimit - LineLengthAfter;
458
459
    // If we are restricted by the maximum column width, end the sequence.
460
695
    if (ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn ||
461
695
        CommasBeforeLastMatch != CommasBeforeMatch) {
462
0
      AlignCurrentSequence();
463
0
      StartOfSequence = i;
464
0
    }
465
466
695
    MinColumn = std::max(MinColumn, ChangeMinColumn);
467
695
    MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
468
695
  }
469
470
37.5k
  EndOfSequence = i;
471
37.5k
  AlignCurrentSequence();
472
37.5k
  return i;
473
37.5k
}
WhitespaceManager.cpp:unsigned int clang::format::AlignTokens<clang::format::WhitespaceManager::alignChainedConditionals()::$_6>(clang::format::FormatStyle const&, clang::format::WhitespaceManager::alignChainedConditionals()::$_6&&, llvm::SmallVector<clang::format::WhitespaceManager::Change, 16u>&, unsigned int)
Line
Count
Source
366
1.50k
                            unsigned StartAt) {
367
1.50k
  unsigned MinColumn = 0;
368
1.50k
  unsigned MaxColumn = UINT_MAX;
369
370
  // Line number of the start and the end of the current token sequence.
371
1.50k
  unsigned StartOfSequence = 0;
372
1.50k
  unsigned EndOfSequence = 0;
373
374
  // Measure the scope level (i.e. depth of (), [], {}) of the first token, and
375
  // abort when we hit any token in a higher scope than the starting one.
376
1.50k
  auto IndentAndNestingLevel = StartAt < Changes.size()
377
1.50k
                                   ? Changes[StartAt].indentAndNestingLevel()
378
0
                                   : std::tuple<unsigned, unsigned, unsigned>();
379
380
  // Keep track of the number of commas before the matching tokens, we will only
381
  // align a sequence of matching tokens if they are preceded by the same number
382
  // of commas.
383
1.50k
  unsigned CommasBeforeLastMatch = 0;
384
1.50k
  unsigned CommasBeforeMatch = 0;
385
386
  // Whether a matching token has been found on the current line.
387
1.50k
  bool FoundMatchOnLine = false;
388
389
  // Aligns a sequence of matching tokens, on the MinColumn column.
390
  //
391
  // Sequences start from the first matching token to align, and end at the
392
  // first token of the first line that doesn't need to be aligned.
393
  //
394
  // We need to adjust the StartOfTokenColumn of each Change that is on a line
395
  // containing any matching token to be aligned and located after such token.
396
1.50k
  auto AlignCurrentSequence = [&] {
397
1.50k
    if (StartOfSequence > 0 && StartOfSequence < EndOfSequence)
398
1.50k
      AlignTokenSequence(StartOfSequence, EndOfSequence, MinColumn, Matches,
399
1.50k
                         Changes);
400
1.50k
    MinColumn = 0;
401
1.50k
    MaxColumn = UINT_MAX;
402
1.50k
    StartOfSequence = 0;
403
1.50k
    EndOfSequence = 0;
404
1.50k
  };
405
406
1.50k
  unsigned i = StartAt;
407
12.7k
  for (unsigned e = Changes.size(); i != e; 
++i11.2k
) {
408
11.2k
    if (Changes[i].indentAndNestingLevel() < IndentAndNestingLevel)
409
11
      break;
410
411
11.2k
    if (Changes[i].NewlinesBefore != 0) {
412
1.44k
      CommasBeforeMatch = 0;
413
1.44k
      EndOfSequence = i;
414
      // If there is a blank line, or if the last line didn't contain any
415
      // matching token, the sequence ends here.
416
1.44k
      if (Changes[i].NewlinesBefore > 1 || 
!FoundMatchOnLine1.42k
)
417
1.43k
        AlignCurrentSequence();
418
419
1.44k
      FoundMatchOnLine = false;
420
1.44k
    }
421
422
11.2k
    if (Changes[i].Tok->is(tok::comma)) {
423
8
      ++CommasBeforeMatch;
424
11.2k
    } else if (Changes[i].indentAndNestingLevel() > IndentAndNestingLevel) {
425
      // Call AlignTokens recursively, skipping over this scope block.
426
1.66k
      unsigned StoppedAt = AlignTokens(Style, Matches, Changes, i);
427
1.66k
      i = StoppedAt - 1;
428
1.66k
      continue;
429
1.66k
    }
430
431
9.58k
    if (!Matches(Changes[i]))
432
9.56k
      continue;
433
434
    // If there is more than one matching token per line, or if the number of
435
    // preceding commas, do not match anymore, end the sequence.
436
20
    if (FoundMatchOnLine || CommasBeforeMatch != CommasBeforeLastMatch)
437
0
      AlignCurrentSequence();
438
439
20
    CommasBeforeLastMatch = CommasBeforeMatch;
440
20
    FoundMatchOnLine = true;
441
442
20
    if (StartOfSequence == 0)
443
14
      StartOfSequence = i;
444
445
20
    unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn;
446
20
    int LineLengthAfter = Changes[i].TokenLength;
447
54
    for (unsigned j = i + 1; j != e && 
Changes[j].NewlinesBefore == 046
;
++j34
) {
448
34
      LineLengthAfter += Changes[j].Spaces;
449
      // Changes are generally 1:1 with the tokens, but a change could also be
450
      // inside of a token, in which case it's counted more than once: once for
451
      // the whitespace surrounding the token (!IsInsideToken) and once for
452
      // each whitespace change within it (IsInsideToken).
453
      // Therefore, changes inside of a token should only count the space.
454
34
      if (!Changes[j].IsInsideToken)
455
34
        LineLengthAfter += Changes[j].TokenLength;
456
34
    }
457
20
    unsigned ChangeMaxColumn = Style.ColumnLimit - LineLengthAfter;
458
459
    // If we are restricted by the maximum column width, end the sequence.
460
20
    if (ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn ||
461
20
        CommasBeforeLastMatch != CommasBeforeMatch) {
462
0
      AlignCurrentSequence();
463
0
      StartOfSequence = i;
464
0
    }
465
466
20
    MinColumn = std::max(MinColumn, ChangeMinColumn);
467
20
    MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
468
20
  }
469
470
1.50k
  EndOfSequence = i;
471
1.50k
  AlignCurrentSequence();
472
1.50k
  return i;
473
1.50k
}
WhitespaceManager.cpp:unsigned int clang::format::AlignTokens<clang::format::WhitespaceManager::alignChainedConditionals()::$_6&>(clang::format::FormatStyle const&, clang::format::WhitespaceManager::alignChainedConditionals()::$_6&, llvm::SmallVector<clang::format::WhitespaceManager::Change, 16u>&, unsigned int)
Line
Count
Source
366
2.88k
                            unsigned StartAt) {
367
2.88k
  unsigned MinColumn = 0;
368
2.88k
  unsigned MaxColumn = UINT_MAX;
369
370
  // Line number of the start and the end of the current token sequence.
371
2.88k
  unsigned StartOfSequence = 0;
372
2.88k
  unsigned EndOfSequence = 0;
373
374
  // Measure the scope level (i.e. depth of (), [], {}) of the first token, and
375
  // abort when we hit any token in a higher scope than the starting one.
376
2.88k
  auto IndentAndNestingLevel = StartAt < Changes.size()
377
2.88k
                                   ? Changes[StartAt].indentAndNestingLevel()
378
0
                                   : std::tuple<unsigned, unsigned, unsigned>();
379
380
  // Keep track of the number of commas before the matching tokens, we will only
381
  // align a sequence of matching tokens if they are preceded by the same number
382
  // of commas.
383
2.88k
  unsigned CommasBeforeLastMatch = 0;
384
2.88k
  unsigned CommasBeforeMatch = 0;
385
386
  // Whether a matching token has been found on the current line.
387
2.88k
  bool FoundMatchOnLine = false;
388
389
  // Aligns a sequence of matching tokens, on the MinColumn column.
390
  //
391
  // Sequences start from the first matching token to align, and end at the
392
  // first token of the first line that doesn't need to be aligned.
393
  //
394
  // We need to adjust the StartOfTokenColumn of each Change that is on a line
395
  // containing any matching token to be aligned and located after such token.
396
2.88k
  auto AlignCurrentSequence = [&] {
397
2.88k
    if (StartOfSequence > 0 && StartOfSequence < EndOfSequence)
398
2.88k
      AlignTokenSequence(StartOfSequence, EndOfSequence, MinColumn, Matches,
399
2.88k
                         Changes);
400
2.88k
    MinColumn = 0;
401
2.88k
    MaxColumn = UINT_MAX;
402
2.88k
    StartOfSequence = 0;
403
2.88k
    EndOfSequence = 0;
404
2.88k
  };
405
406
2.88k
  unsigned i = StartAt;
407
13.3k
  for (unsigned e = Changes.size(); i != e; 
++i10.4k
) {
408
13.3k
    if (Changes[i].indentAndNestingLevel() < IndentAndNestingLevel)
409
2.87k
      break;
410
411
10.4k
    if (Changes[i].NewlinesBefore != 0) {
412
2.02k
      CommasBeforeMatch = 0;
413
2.02k
      EndOfSequence = i;
414
      // If there is a blank line, or if the last line didn't contain any
415
      // matching token, the sequence ends here.
416
2.02k
      if (Changes[i].NewlinesBefore > 1 || !FoundMatchOnLine)
417
1.77k
        AlignCurrentSequence();
418
419
2.02k
      FoundMatchOnLine = false;
420
2.02k
    }
421
422
10.4k
    if (Changes[i].Tok->is(tok::comma)) {
423
603
      ++CommasBeforeMatch;
424
9.83k
    } else if (Changes[i].indentAndNestingLevel() > IndentAndNestingLevel) {
425
      // Call AlignTokens recursively, skipping over this scope block.
426
1.21k
      unsigned StoppedAt = AlignTokens(Style, Matches, Changes, i);
427
1.21k
      i = StoppedAt - 1;
428
1.21k
      continue;
429
1.21k
    }
430
431
9.21k
    if (!Matches(Changes[i]))
432
8.79k
      continue;
433
434
    // If there is more than one matching token per line, or if the number of
435
    // preceding commas, do not match anymore, end the sequence.
436
426
    if (FoundMatchOnLine || 
CommasBeforeMatch != CommasBeforeLastMatch423
)
437
3
      AlignCurrentSequence();
438
439
426
    CommasBeforeLastMatch = CommasBeforeMatch;
440
426
    FoundMatchOnLine = true;
441
442
426
    if (StartOfSequence == 0)
443
191
      StartOfSequence = i;
444
445
426
    unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn;
446
426
    int LineLengthAfter = Changes[i].TokenLength;
447
1.56k
    for (unsigned j = i + 1; j != e && 
Changes[j].NewlinesBefore == 01.46k
;
++j1.14k
) {
448
1.14k
      LineLengthAfter += Changes[j].Spaces;
449
      // Changes are generally 1:1 with the tokens, but a change could also be
450
      // inside of a token, in which case it's counted more than once: once for
451
      // the whitespace surrounding the token (!IsInsideToken) and once for
452
      // each whitespace change within it (IsInsideToken).
453
      // Therefore, changes inside of a token should only count the space.
454
1.14k
      if (!Changes[j].IsInsideToken)
455
1.14k
        LineLengthAfter += Changes[j].TokenLength;
456
1.14k
    }
457
426
    unsigned ChangeMaxColumn = Style.ColumnLimit - LineLengthAfter;
458
459
    // If we are restricted by the maximum column width, end the sequence.
460
426
    if (ChangeMinColumn > MaxColumn || 
ChangeMaxColumn < MinColumn423
||
461
420
        CommasBeforeLastMatch != CommasBeforeMatch) {
462
6
      AlignCurrentSequence();
463
6
      StartOfSequence = i;
464
6
    }
465
466
426
    MinColumn = std::max(MinColumn, ChangeMinColumn);
467
426
    MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
468
426
  }
469
470
2.88k
  EndOfSequence = i;
471
2.88k
  AlignCurrentSequence();
472
2.88k
  return i;
473
2.88k
}
474
475
// Aligns a sequence of matching tokens, on the MinColumn column.
476
//
477
// Sequences start from the first matching token to align, and end at the
478
// first token of the first line that doesn't need to be aligned.
479
//
480
// We need to adjust the StartOfTokenColumn of each Change that is on a line
481
// containing any matching token to be aligned and located after such token.
482
static void AlignMacroSequence(
483
    unsigned &StartOfSequence, unsigned &EndOfSequence, unsigned &MinColumn,
484
    unsigned &MaxColumn, bool &FoundMatchOnLine,
485
    std::function<bool(const WhitespaceManager::Change &C)> AlignMacrosMatches,
486
1.00k
    SmallVector<WhitespaceManager::Change, 16> &Changes) {
487
1.00k
  if (StartOfSequence > 0 && 
StartOfSequence < EndOfSequence15
) {
488
489
15
    FoundMatchOnLine = false;
490
15
    int Shift = 0;
491
492
459
    for (unsigned I = StartOfSequence; I != EndOfSequence; 
++I444
) {
493
444
      if (Changes[I].NewlinesBefore > 0) {
494
39
        Shift = 0;
495
39
        FoundMatchOnLine = false;
496
39
      }
497
498
      // If this is the first matching token to be aligned, remember by how many
499
      // spaces it has to be shifted, so the rest of the changes on the line are
500
      // shifted by the same amount
501
444
      if (!FoundMatchOnLine && 
AlignMacrosMatches(Changes[I])279
) {
502
51
        FoundMatchOnLine = true;
503
51
        Shift = MinColumn - Changes[I].StartOfTokenColumn;
504
51
        Changes[I].Spaces += Shift;
505
51
      }
506
507
444
      assert(Shift >= 0);
508
444
      Changes[I].StartOfTokenColumn += Shift;
509
444
      if (I + 1 != Changes.size())
510
432
        Changes[I + 1].PreviousEndOfTokenColumn += Shift;
511
444
    }
512
15
  }
513
514
1.00k
  MinColumn = 0;
515
1.00k
  MaxColumn = UINT_MAX;
516
1.00k
  StartOfSequence = 0;
517
1.00k
  EndOfSequence = 0;
518
1.00k
}
519
520
16.5k
void WhitespaceManager::alignConsecutiveMacros() {
521
16.5k
  if (!Style.AlignConsecutiveMacros)
522
16.2k
    return;
523
524
5.98k
  
auto AlignMacrosMatches = [](const Change &C) 235
{
525
5.98k
    const FormatToken *Current = C.Tok;
526
5.98k
    unsigned SpacesRequiredBefore = 1;
527
528
5.98k
    if (Current->SpacesRequiredBefore == 0 || 
!Current->Previous4.11k
)
529
3.00k
      return false;
530
531
2.98k
    Current = Current->Previous;
532
533
    // If token is a ")", skip over the parameter list, to the
534
    // token that precedes the "("
535
2.98k
    if (Current->is(tok::r_paren) && 
Current->MatchingParen121
) {
536
121
      Current = Current->MatchingParen->Previous;
537
121
      SpacesRequiredBefore = 0;
538
121
    }
539
540
2.98k
    if (!Current || !Current->is(tok::identifier))
541
1.94k
      return false;
542
543
1.03k
    if (!Current->Previous || 
!Current->Previous->is(tok::pp_define)945
)
544
933
      return false;
545
546
    // For a macro function, 0 spaces are required between the
547
    // identifier and the lparen that opens the parameter list.
548
    // For a simple macro, 1 space is required between the
549
    // identifier and the first token of the defined value.
550
102
    return Current->Next->SpacesRequiredBefore == SpacesRequiredBefore;
551
102
  };
552
553
235
  unsigned MinColumn = 0;
554
235
  unsigned MaxColumn = UINT_MAX;
555
556
  // Start and end of the token sequence we're processing.
557
235
  unsigned StartOfSequence = 0;
558
235
  unsigned EndOfSequence = 0;
559
560
  // Whether a matching token has been found on the current line.
561
235
  bool FoundMatchOnLine = false;
562
563
235
  unsigned I = 0;
564
5.94k
  for (unsigned E = Changes.size(); I != E; 
++I5.70k
) {
565
5.70k
    if (Changes[I].NewlinesBefore != 0) {
566
809
      EndOfSequence = I;
567
      // If there is a blank line, or if the last line didn't contain any
568
      // matching token, the sequence ends here.
569
809
      if (Changes[I].NewlinesBefore > 1 || 
!FoundMatchOnLine802
)
570
770
        AlignMacroSequence(StartOfSequence, EndOfSequence, MinColumn, MaxColumn,
571
770
                           FoundMatchOnLine, AlignMacrosMatches, Changes);
572
573
809
      FoundMatchOnLine = false;
574
809
    }
575
576
5.70k
    if (!AlignMacrosMatches(Changes[I]))
577
5.65k
      continue;
578
579
51
    FoundMatchOnLine = true;
580
581
51
    if (StartOfSequence == 0)
582
15
      StartOfSequence = I;
583
584
51
    unsigned ChangeMinColumn = Changes[I].StartOfTokenColumn;
585
51
    int LineLengthAfter = -Changes[I].Spaces;
586
267
    for (unsigned j = I; j != E && 
Changes[j].NewlinesBefore == 0255
;
++j216
)
587
216
      LineLengthAfter += Changes[j].Spaces + Changes[j].TokenLength;
588
51
    unsigned ChangeMaxColumn = Style.ColumnLimit - LineLengthAfter;
589
590
51
    MinColumn = std::max(MinColumn, ChangeMinColumn);
591
51
    MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
592
51
  }
593
594
235
  EndOfSequence = I;
595
235
  AlignMacroSequence(StartOfSequence, EndOfSequence, MinColumn, MaxColumn,
596
235
                     FoundMatchOnLine, AlignMacrosMatches, Changes);
597
235
}
598
599
16.5k
void WhitespaceManager::alignConsecutiveAssignments() {
600
16.5k
  if (!Style.AlignConsecutiveAssignments)
601
16.2k
    return;
602
603
228
  AlignTokens(
604
228
      Style,
605
6.10k
      [&](const Change &C) {
606
        // Do not align on equal signs that are first on a line.
607
6.10k
        if (C.NewlinesBefore > 0)
608
1.08k
          return false;
609
610
        // Do not align on equal signs that are last on a line.
611
5.02k
        if (&C != &Changes.back() && 
(&C + 1)->NewlinesBefore > 04.83k
)
612
792
          return false;
613
614
4.22k
        return C.Tok->is(tok::equal);
615
4.22k
      },
616
228
      Changes, /*StartAt=*/0);
617
228
}
618
619
16.5k
void WhitespaceManager::alignConsecutiveBitFields() {
620
16.5k
  if (!Style.AlignConsecutiveBitFields)
621
16.4k
    return;
622
623
30
  AlignTokens(
624
30
      Style,
625
540
      [&](Change const &C) {
626
        // Do not align on ':' that is first on a line.
627
540
        if (C.NewlinesBefore > 0)
628
60
          return false;
629
630
        // Do not align on ':' that is last on a line.
631
480
        if (&C != &Changes.back() && 
(&C + 1)->NewlinesBefore > 0450
)
632
30
          return false;
633
634
450
        return C.Tok->is(TT_BitFieldColon);
635
450
      },
636
30
      Changes, /*StartAt=*/0);
637
30
}
638
639
16.5k
void WhitespaceManager::alignConsecutiveDeclarations() {
640
16.5k
  if (!Style.AlignConsecutiveDeclarations)
641
16.2k
    return;
642
643
  // FIXME: Currently we don't handle properly the PointerAlignment: Right
644
  // The * and & are not aligned and are left dangling. Something has to be done
645
  // about it, but it raises the question of alignment of code like:
646
  //   const char* const* v1;
647
  //   float const* v2;
648
  //   SomeVeryLongType const& v3;
649
243
  AlignTokens(
650
243
      Style,
651
6.43k
      [](Change const &C) {
652
        // tok::kw_operator is necessary for aligning operator overload
653
        // definitions.
654
6.43k
        if (C.Tok->isOneOf(TT_FunctionDeclarationName, tok::kw_operator))
655
186
          return true;
656
6.24k
        if (C.Tok->isNot(TT_StartOfName))
657
5.30k
          return false;
658
        // Check if there is a subsequent name that starts the same declaration.
659
942
        
for (FormatToken *Next = C.Tok->Next; 940
Next;
Next = Next->Next2
) {
660
942
          if (Next->is(tok::comment))
661
2
            continue;
662
940
          if (!Next->Tok.getIdentifierInfo())
663
938
            break;
664
2
          if (Next->isOneOf(TT_StartOfName, TT_FunctionDeclarationName,
665
2
                            tok::kw_operator))
666
2
            return false;
667
2
        }
668
938
        return true;
669
940
      },
670
243
      Changes, /*StartAt=*/0);
671
243
}
672
673
16.5k
void WhitespaceManager::alignChainedConditionals() {
674
16.5k
  if (Style.BreakBeforeTernaryOperators) {
675
15.0k
    AlignTokens(
676
15.0k
        Style,
677
250k
        [](Change const &C) {
678
          // Align question operators and last colon
679
250k
          return C.Tok->is(TT_ConditionalExpr) &&
680
1.57k
                 ((C.Tok->is(tok::question) && 
!C.NewlinesBefore705
) ||
681
952
                  (C.Tok->is(tok::colon) && 
C.Tok->Next870
&&
682
870
                   (C.Tok->Next->FakeLParens.size() == 0 ||
683
254
                    C.Tok->Next->FakeLParens.back() != prec::Conditional)));
684
250k
        },
685
15.0k
        Changes, /*StartAt=*/0);
686
1.50k
  } else {
687
37.7k
    static auto AlignWrappedOperand = [](Change const &C) {
688
37.7k
      auto Previous = C.Tok->getPreviousNonComment(); // Previous;
689
37.7k
      return C.NewlinesBefore && 
Previous5.32k
&&
Previous->is(TT_ConditionalExpr)2.93k
&&
690
909
             (Previous->is(tok::question) ||
691
753
              (Previous->is(tok::colon) &&
692
753
               (C.Tok->FakeLParens.size() == 0 ||
693
282
                C.Tok->FakeLParens.back() != prec::Conditional)));
694
37.7k
    };
695
    // Ensure we keep alignment of wrapped operands with non-wrapped operands
696
    // Since we actually align the operators, the wrapped operands need the
697
    // extra offset to be properly aligned.
698
18.8k
    for (Change &C : Changes) {
699
18.8k
      if (AlignWrappedOperand(C))
700
219
        C.StartOfTokenColumn -= 2;
701
18.8k
    }
702
1.50k
    AlignTokens(
703
1.50k
        Style,
704
19.3k
        [this](Change const &C) {
705
          // Align question operators if next operand is not wrapped, as
706
          // well as wrapped operands after question operator or last
707
          // colon in conditional sequence
708
19.3k
          return (C.Tok->is(TT_ConditionalExpr) && 
C.Tok->is(tok::question)790
&&
709
510
                  &C != &Changes.back() && (&C + 1)->NewlinesBefore == 0 &&
710
469
                  !(&C + 1)->IsTrailingComment) ||
711
18.8k
                 AlignWrappedOperand(C);
712
19.3k
        },
713
1.50k
        Changes, /*StartAt=*/0);
714
1.50k
  }
715
16.5k
}
716
717
16.5k
void WhitespaceManager::alignTrailingComments() {
718
16.5k
  unsigned MinColumn = 0;
719
16.5k
  unsigned MaxColumn = UINT_MAX;
720
16.5k
  unsigned StartOfSequence = 0;
721
16.5k
  bool BreakBeforeNext = false;
722
16.5k
  unsigned Newlines = 0;
723
285k
  for (unsigned i = 0, e = Changes.size(); i != e; 
++i268k
) {
724
268k
    if (Changes[i].StartOfBlockComment)
725
550
      continue;
726
268k
    Newlines += Changes[i].NewlinesBefore;
727
268k
    if (!Changes[i].IsTrailingComment)
728
264k
      continue;
729
730
3.59k
    unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn;
731
3.59k
    unsigned ChangeMaxColumn;
732
733
3.59k
    if (Style.ColumnLimit == 0)
734
158
      ChangeMaxColumn = UINT_MAX;
735
3.44k
    else if (Style.ColumnLimit >= Changes[i].TokenLength)
736
3.39k
      ChangeMaxColumn = Style.ColumnLimit - Changes[i].TokenLength;
737
49
    else
738
49
      ChangeMaxColumn = ChangeMinColumn;
739
740
    // If we don't create a replacement for this change, we have to consider
741
    // it to be immovable.
742
3.59k
    if (!Changes[i].CreateReplacement)
743
469
      ChangeMaxColumn = ChangeMinColumn;
744
745
3.59k
    if (i + 1 != e && 
Changes[i + 1].ContinuesPPDirective3.59k
)
746
68
      ChangeMaxColumn -= 2;
747
    // If this comment follows an } in column 0, it probably documents the
748
    // closing of a namespace and we don't want to align it.
749
3.59k
    bool FollowsRBraceInColumn0 = i > 0 && 
Changes[i].NewlinesBefore == 03.20k
&&
750
1.87k
                                  Changes[i - 1].Tok->is(tok::r_brace) &&
751
841
                                  Changes[i - 1].StartOfTokenColumn == 0;
752
3.59k
    bool WasAlignedWithStartOfNextLine = false;
753
3.59k
    if (Changes[i].NewlinesBefore == 1) { // A comment on its own line.
754
1.29k
      unsigned CommentColumn = SourceMgr.getSpellingColumnNumber(
755
1.29k
          Changes[i].OriginalWhitespaceRange.getEnd());
756
4.79k
      for (unsigned j = i + 1; j != e; 
++j3.50k
) {
757
4.79k
        if (Changes[j].Tok->is(tok::comment))
758
3.50k
          continue;
759
760
1.29k
        unsigned NextColumn = SourceMgr.getSpellingColumnNumber(
761
1.29k
            Changes[j].OriginalWhitespaceRange.getEnd());
762
        // The start of the next token was previously aligned with the
763
        // start of this comment.
764
1.29k
        WasAlignedWithStartOfNextLine =
765
1.29k
            CommentColumn == NextColumn ||
766
622
            CommentColumn == NextColumn + Style.IndentWidth;
767
1.29k
        break;
768
1.29k
      }
769
1.29k
    }
770
3.59k
    if (!Style.AlignTrailingComments || 
FollowsRBraceInColumn03.51k
) {
771
427
      alignTrailingComments(StartOfSequence, i, MinColumn);
772
427
      MinColumn = ChangeMinColumn;
773
427
      MaxColumn = ChangeMinColumn;
774
427
      StartOfSequence = i;
775
3.17k
    } else if (BreakBeforeNext || 
Newlines > 12.49k
||
776
1.74k
               (ChangeMinColumn > MaxColumn || 
ChangeMaxColumn < MinColumn1.73k
) ||
777
               // Break the comment sequence if the previous line did not end
778
               // in a trailing comment.
779
1.72k
               (Changes[i].NewlinesBefore == 1 && 
i > 0429
&&
780
416
                !Changes[i - 1].IsTrailingComment) ||
781
1.80k
               
WasAlignedWithStartOfNextLine1.41k
) {
782
1.80k
      alignTrailingComments(StartOfSequence, i, MinColumn);
783
1.80k
      MinColumn = ChangeMinColumn;
784
1.80k
      MaxColumn = ChangeMaxColumn;
785
1.80k
      StartOfSequence = i;
786
1.36k
    } else {
787
1.36k
      MinColumn = std::max(MinColumn, ChangeMinColumn);
788
1.36k
      MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
789
1.36k
    }
790
3.59k
    BreakBeforeNext = (i == 0) || 
(Changes[i].NewlinesBefore > 1)3.20k
||
791
                      // Never start a sequence with a comment at the beginning
792
                      // of the line.
793
3.16k
                      (Changes[i].NewlinesBefore == 1 && 
StartOfSequence == i1.28k
);
794
3.59k
    Newlines = 0;
795
3.59k
  }
796
16.5k
  alignTrailingComments(StartOfSequence, Changes.size(), MinColumn);
797
16.5k
}
798
799
void WhitespaceManager::alignTrailingComments(unsigned Start, unsigned End,
800
18.7k
                                              unsigned Column) {
801
287k
  for (unsigned i = Start; i != End; 
++i268k
) {
802
268k
    int Shift = 0;
803
268k
    if (Changes[i].IsTrailingComment) {
804
4.09k
      Shift = Column - Changes[i].StartOfTokenColumn;
805
4.09k
    }
806
268k
    if (Changes[i].StartOfBlockComment) {
807
550
      Shift = Changes[i].IndentationOffset +
808
550
              Changes[i].StartOfBlockComment->StartOfTokenColumn -
809
550
              Changes[i].StartOfTokenColumn;
810
550
    }
811
268k
    assert(Shift >= 0);
812
268k
    Changes[i].Spaces += Shift;
813
268k
    if (i + 1 != Changes.size())
814
252k
      Changes[i + 1].PreviousEndOfTokenColumn += Shift;
815
268k
    Changes[i].StartOfTokenColumn += Shift;
816
268k
  }
817
18.7k
}
818
819
16.5k
void WhitespaceManager::alignEscapedNewlines() {
820
16.5k
  if (Style.AlignEscapedNewlines == FormatStyle::ENAS_DontAlign)
821
7
    return;
822
823
16.5k
  bool AlignLeft = Style.AlignEscapedNewlines == FormatStyle::ENAS_Left;
824
12.9k
  unsigned MaxEndOfLine = AlignLeft ? 
03.52k
: Style.ColumnLimit;
825
16.5k
  unsigned StartOfMacro = 0;
826
268k
  for (unsigned i = 1, e = Changes.size(); i < e; 
++i252k
) {
827
252k
    Change &C = Changes[i];
828
252k
    if (C.NewlinesBefore > 0) {
829
35.6k
      if (C.ContinuesPPDirective) {
830
600
        MaxEndOfLine = std::max(C.PreviousEndOfTokenColumn + 2, MaxEndOfLine);
831
35.0k
      } else {
832
35.0k
        alignEscapedNewlines(StartOfMacro + 1, i, MaxEndOfLine);
833
28.0k
        MaxEndOfLine = AlignLeft ? 
07.05k
: Style.ColumnLimit;
834
35.0k
        StartOfMacro = i;
835
35.0k
      }
836
35.6k
    }
837
252k
  }
838
16.5k
  alignEscapedNewlines(StartOfMacro + 1, Changes.size(), MaxEndOfLine);
839
16.5k
}
840
841
void WhitespaceManager::alignEscapedNewlines(unsigned Start, unsigned End,
842
51.5k
                                             unsigned Column) {
843
268k
  for (unsigned i = Start; i < End; 
++i217k
) {
844
217k
    Change &C = Changes[i];
845
217k
    if (C.NewlinesBefore > 0) {
846
600
      assert(C.ContinuesPPDirective);
847
600
      if (C.PreviousEndOfTokenColumn + 1 > Column)
848
0
        C.EscapedNewlineColumn = 0;
849
600
      else
850
600
        C.EscapedNewlineColumn = Column;
851
600
    }
852
217k
  }
853
51.5k
}
854
855
16.5k
void WhitespaceManager::generateChanges() {
856
285k
  for (unsigned i = 0, e = Changes.size(); i != e; 
++i268k
) {
857
268k
    const Change &C = Changes[i];
858
268k
    if (i > 0) {
859
252k
      assert(Changes[i - 1].OriginalWhitespaceRange.getBegin() !=
860
252k
                 C.OriginalWhitespaceRange.getBegin() &&
861
252k
             "Generating two replacements for the same location");
862
252k
    }
863
268k
    if (C.CreateReplacement) {
864
244k
      std::string ReplacementText = C.PreviousLinePostfix;
865
244k
      if (C.ContinuesPPDirective)
866
569
        appendEscapedNewlineText(ReplacementText, C.NewlinesBefore,
867
569
                                 C.PreviousEndOfTokenColumn,
868
569
                                 C.EscapedNewlineColumn);
869
244k
      else
870
244k
        appendNewlineText(ReplacementText, C.NewlinesBefore);
871
244k
      appendIndentText(
872
244k
          ReplacementText, C.Tok->IndentLevel, std::max(0, C.Spaces),
873
244k
          C.StartOfTokenColumn - std::max(0, C.Spaces), C.IsAligned);
874
244k
      ReplacementText.append(C.CurrentLinePrefix);
875
244k
      storeReplacement(C.OriginalWhitespaceRange, ReplacementText);
876
244k
    }
877
268k
  }
878
16.5k
}
879
880
244k
void WhitespaceManager::storeReplacement(SourceRange Range, StringRef Text) {
881
244k
  unsigned WhitespaceLength = SourceMgr.getFileOffset(Range.getEnd()) -
882
244k
                              SourceMgr.getFileOffset(Range.getBegin());
883
  // Don't create a replacement, if it does not change anything.
884
244k
  if (StringRef(SourceMgr.getCharacterData(Range.getBegin()),
885
244k
                WhitespaceLength) == Text)
886
224k
    return;
887
20.5k
  auto Err = Replaces.add(tooling::Replacement(
888
20.5k
      SourceMgr, CharSourceRange::getCharRange(Range), Text));
889
  // FIXME: better error handling. For now, just print an error message in the
890
  // release version.
891
20.5k
  if (Err) {
892
0
    llvm::errs() << llvm::toString(std::move(Err)) << "\n";
893
0
    assert(false);
894
0
  }
895
20.5k
}
896
897
void WhitespaceManager::appendNewlineText(std::string &Text,
898
244k
                                          unsigned Newlines) {
899
275k
  for (unsigned i = 0; i < Newlines; 
++i31.3k
)
900
31.3k
    Text.append(UseCRLF ? 
"\r\n"40
:
"\n"31.3k
);
901
244k
}
902
903
void WhitespaceManager::appendEscapedNewlineText(
904
    std::string &Text, unsigned Newlines, unsigned PreviousEndOfTokenColumn,
905
569
    unsigned EscapedNewlineColumn) {
906
569
  if (Newlines > 0) {
907
566
    unsigned Spaces =
908
566
        std::max<int>(1, EscapedNewlineColumn - PreviousEndOfTokenColumn - 1);
909
1.13k
    for (unsigned i = 0; i < Newlines; 
++i571
) {
910
571
      Text.append(Spaces, ' ');
911
564
      Text.append(UseCRLF ? 
"\\\r\n"7
: "\\\n");
912
571
      Spaces = std::max<int>(0, EscapedNewlineColumn - 1);
913
571
    }
914
566
  }
915
569
}
916
917
void WhitespaceManager::appendIndentText(std::string &Text,
918
                                         unsigned IndentLevel, unsigned Spaces,
919
                                         unsigned WhitespaceStartColumn,
920
244k
                                         bool IsAligned) {
921
244k
  switch (Style.UseTab) {
922
242k
  case FormatStyle::UT_Never:
923
242k
    Text.append(Spaces, ' ');
924
242k
    break;
925
481
  case FormatStyle::UT_Always: {
926
481
    if (Style.TabWidth) {
927
463
      unsigned FirstTabWidth =
928
463
          Style.TabWidth - WhitespaceStartColumn % Style.TabWidth;
929
930
      // Insert only spaces when we want to end up before the next tab.
931
463
      if (Spaces < FirstTabWidth || 
Spaces == 1116
) {
932
368
        Text.append(Spaces, ' ');
933
368
        break;
934
368
      }
935
      // Align to the next tab.
936
95
      Spaces -= FirstTabWidth;
937
95
      Text.append("\t");
938
939
95
      Text.append(Spaces / Style.TabWidth, '\t');
940
95
      Text.append(Spaces % Style.TabWidth, ' ');
941
18
    } else if (Spaces == 1) {
942
2
      Text.append(Spaces, ' ');
943
2
    }
944
113
    break;
945
481
  }
946
304
  case FormatStyle::UT_ForIndentation:
947
304
    if (WhitespaceStartColumn == 0) {
948
144
      unsigned Indentation = IndentLevel * Style.IndentWidth;
949
144
      Spaces = appendTabIndent(Text, Spaces, Indentation);
950
144
    }
951
304
    Text.append(Spaces, ' ');
952
304
    break;
953
738
  case FormatStyle::UT_ForContinuationAndIndentation:
954
738
    if (WhitespaceStartColumn == 0)
955
319
      Spaces = appendTabIndent(Text, Spaces, Spaces);
956
738
    Text.append(Spaces, ' ');
957
738
    break;
958
840
  case FormatStyle::UT_AlignWithSpaces:
959
840
    if (WhitespaceStartColumn == 0) {
960
358
      unsigned Indentation =
961
301
          IsAligned ? 
IndentLevel * Style.IndentWidth57
: Spaces;
962
358
      Spaces = appendTabIndent(Text, Spaces, Indentation);
963
358
    }
964
840
    Text.append(Spaces, ' ');
965
840
    break;
966
244k
  }
967
244k
}
968
969
unsigned WhitespaceManager::appendTabIndent(std::string &Text, unsigned Spaces,
970
821
                                            unsigned Indentation) {
971
  // This happens, e.g. when a line in a block comment is indented less than the
972
  // first one.
973
821
  if (Indentation > Spaces)
974
4
    Indentation = Spaces;
975
821
  if (Style.TabWidth) {
976
797
    unsigned Tabs = Indentation / Style.TabWidth;
977
797
    Text.append(Tabs, '\t');
978
797
    Spaces -= Tabs * Style.TabWidth;
979
797
  }
980
821
  return Spaces;
981
821
}
982
983
} // namespace format
984
} // namespace clang