Coverage Report

Created: 2022-01-18 06:27

/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
#include "llvm/ADT/SmallVector.h"
17
#include <algorithm>
18
19
namespace clang {
20
namespace format {
21
22
bool WhitespaceManager::Change::IsBeforeInFile::operator()(
23
675k
    const Change &C1, const Change &C2) const {
24
675k
  return SourceMgr.isBeforeInTranslationUnit(
25
675k
      C1.OriginalWhitespaceRange.getBegin(),
26
675k
      C2.OriginalWhitespaceRange.getBegin());
27
675k
}
28
29
WhitespaceManager::Change::Change(const FormatToken &Tok,
30
                                  bool CreateReplacement,
31
                                  SourceRange OriginalWhitespaceRange,
32
                                  int Spaces, unsigned StartOfTokenColumn,
33
                                  unsigned NewlinesBefore,
34
                                  StringRef PreviousLinePostfix,
35
                                  StringRef CurrentLinePrefix, bool IsAligned,
36
                                  bool ContinuesPPDirective, bool IsInsideToken)
37
    : Tok(&Tok), CreateReplacement(CreateReplacement),
38
      OriginalWhitespaceRange(OriginalWhitespaceRange),
39
      StartOfTokenColumn(StartOfTokenColumn), NewlinesBefore(NewlinesBefore),
40
      PreviousLinePostfix(PreviousLinePostfix),
41
      CurrentLinePrefix(CurrentLinePrefix), IsAligned(IsAligned),
42
      ContinuesPPDirective(ContinuesPPDirective), Spaces(Spaces),
43
      IsInsideToken(IsInsideToken), IsTrailingComment(false), TokenLength(0),
44
      PreviousEndOfTokenColumn(0), EscapedNewlineColumn(0),
45
344k
      StartOfBlockComment(nullptr), IndentationOffset(0), ConditionalsLevel(0) {
46
344k
}
47
48
void WhitespaceManager::replaceWhitespace(FormatToken &Tok, unsigned Newlines,
49
                                          unsigned Spaces,
50
                                          unsigned StartOfTokenColumn,
51
322k
                                          bool IsAligned, bool InPPDirective) {
52
322k
  if (Tok.Finalized)
53
4.05k
    return;
54
318k
  Tok.setDecision((Newlines > 0) ? 
FD_Break41.1k
:
FD_Continue277k
);
55
318k
  Changes.push_back(Change(Tok, /*CreateReplacement=*/true, Tok.WhitespaceRange,
56
318k
                           Spaces, StartOfTokenColumn, Newlines, "", "",
57
318k
                           IsAligned, InPPDirective && 
!Tok.IsFirst1.88k
,
58
318k
                           /*IsInsideToken=*/false));
59
318k
}
60
61
void WhitespaceManager::addUntouchableToken(const FormatToken &Tok,
62
24.1k
                                            bool InPPDirective) {
63
24.1k
  if (Tok.Finalized)
64
24
    return;
65
24.1k
  Changes.push_back(Change(Tok, /*CreateReplacement=*/false,
66
24.1k
                           Tok.WhitespaceRange, /*Spaces=*/0,
67
24.1k
                           Tok.OriginalColumn, Tok.NewlinesBefore, "", "",
68
24.1k
                           /*IsAligned=*/false, InPPDirective && 
!Tok.IsFirst1.86k
,
69
24.1k
                           /*IsInsideToken=*/false));
70
24.1k
}
71
72
llvm::Error
73
196
WhitespaceManager::addReplacement(const tooling::Replacement &Replacement) {
74
196
  return Replaces.add(Replacement);
75
196
}
76
77
20.9k
bool WhitespaceManager::inputUsesCRLF(StringRef Text, bool DefaultToCRLF) {
78
20.9k
  size_t LF = Text.count('\n');
79
20.9k
  size_t CR = Text.count('\r') * 2;
80
20.9k
  return LF == CR ? 
DefaultToCRLF13.3k
:
CR > LF7.54k
;
81
20.9k
}
82
83
void WhitespaceManager::replaceWhitespaceInToken(
84
    const FormatToken &Tok, unsigned Offset, unsigned ReplaceChars,
85
    StringRef PreviousPostfix, StringRef CurrentPrefix, bool InPPDirective,
86
1.53k
    unsigned Newlines, int Spaces) {
87
1.53k
  if (Tok.Finalized)
88
4
    return;
89
1.53k
  SourceLocation Start = Tok.getStartOfNonWhitespace().getLocWithOffset(Offset);
90
1.53k
  Changes.push_back(
91
1.53k
      Change(Tok, /*CreateReplacement=*/true,
92
1.53k
             SourceRange(Start, Start.getLocWithOffset(ReplaceChars)), Spaces,
93
1.53k
             std::max(0, Spaces), Newlines, PreviousPostfix, CurrentPrefix,
94
1.53k
             /*IsAligned=*/true, InPPDirective && 
!Tok.IsFirst21
,
95
1.53k
             /*IsInsideToken=*/true));
96
1.53k
}
97
98
20.9k
const tooling::Replacements &WhitespaceManager::generateReplacements() {
99
20.9k
  if (Changes.empty())
100
64
    return Replaces;
101
102
20.8k
  llvm::sort(Changes, Change::IsBeforeInFile(SourceMgr));
103
20.8k
  calculateLineBreakInformation();
104
20.8k
  alignConsecutiveMacros();
105
20.8k
  alignConsecutiveDeclarations();
106
20.8k
  alignConsecutiveBitFields();
107
20.8k
  alignConsecutiveAssignments();
108
20.8k
  alignChainedConditionals();
109
20.8k
  alignTrailingComments();
110
20.8k
  alignEscapedNewlines();
111
20.8k
  alignArrayInitializers();
112
20.8k
  generateChanges();
113
114
20.8k
  return Replaces;
115
20.9k
}
116
117
20.8k
void WhitespaceManager::calculateLineBreakInformation() {
118
20.8k
  Changes[0].PreviousEndOfTokenColumn = 0;
119
20.8k
  Change *LastOutsideTokenChange = &Changes[0];
120
344k
  for (unsigned i = 1, e = Changes.size(); i != e; 
++i323k
) {
121
323k
    SourceLocation OriginalWhitespaceStart =
122
323k
        Changes[i].OriginalWhitespaceRange.getBegin();
123
323k
    SourceLocation PreviousOriginalWhitespaceEnd =
124
323k
        Changes[i - 1].OriginalWhitespaceRange.getEnd();
125
323k
    unsigned OriginalWhitespaceStartOffset =
126
323k
        SourceMgr.getFileOffset(OriginalWhitespaceStart);
127
323k
    unsigned PreviousOriginalWhitespaceEndOffset =
128
323k
        SourceMgr.getFileOffset(PreviousOriginalWhitespaceEnd);
129
323k
    assert(PreviousOriginalWhitespaceEndOffset <=
130
323k
           OriginalWhitespaceStartOffset);
131
0
    const char *const PreviousOriginalWhitespaceEndData =
132
323k
        SourceMgr.getCharacterData(PreviousOriginalWhitespaceEnd);
133
323k
    StringRef Text(PreviousOriginalWhitespaceEndData,
134
323k
                   SourceMgr.getCharacterData(OriginalWhitespaceStart) -
135
323k
                       PreviousOriginalWhitespaceEndData);
136
    // Usually consecutive changes would occur in consecutive tokens. This is
137
    // not the case however when analyzing some preprocessor runs of the
138
    // annotated lines. For example, in this code:
139
    //
140
    // #if A // line 1
141
    // int i = 1;
142
    // #else B // line 2
143
    // int i = 2;
144
    // #endif // line 3
145
    //
146
    // one of the runs will produce the sequence of lines marked with line 1, 2
147
    // and 3. So the two consecutive whitespace changes just before '// line 2'
148
    // and before '#endif // line 3' span multiple lines and tokens:
149
    //
150
    // #else B{change X}[// line 2
151
    // int i = 2;
152
    // ]{change Y}#endif // line 3
153
    //
154
    // For this reason, if the text between consecutive changes spans multiple
155
    // newlines, the token length must be adjusted to the end of the original
156
    // line of the token.
157
323k
    auto NewlinePos = Text.find_first_of('\n');
158
323k
    if (NewlinePos == StringRef::npos) {
159
323k
      Changes[i - 1].TokenLength = OriginalWhitespaceStartOffset -
160
323k
                                   PreviousOriginalWhitespaceEndOffset +
161
323k
                                   Changes[i].PreviousLinePostfix.size() +
162
323k
                                   Changes[i - 1].CurrentLinePrefix.size();
163
323k
    } else {
164
390
      Changes[i - 1].TokenLength =
165
390
          NewlinePos + Changes[i - 1].CurrentLinePrefix.size();
166
390
    }
167
168
    // If there are multiple changes in this token, sum up all the changes until
169
    // the end of the line.
170
323k
    if (Changes[i - 1].IsInsideToken && 
Changes[i - 1].NewlinesBefore == 01.53k
)
171
393
      LastOutsideTokenChange->TokenLength +=
172
393
          Changes[i - 1].TokenLength + Changes[i - 1].Spaces;
173
323k
    else
174
323k
      LastOutsideTokenChange = &Changes[i - 1];
175
176
323k
    Changes[i].PreviousEndOfTokenColumn =
177
323k
        Changes[i - 1].StartOfTokenColumn + Changes[i - 1].TokenLength;
178
179
323k
    Changes[i - 1].IsTrailingComment =
180
323k
        (Changes[i].NewlinesBefore > 0 || 
Changes[i].Tok->is(tok::eof)277k
||
181
323k
         
(258k
Changes[i].IsInsideToken258k
&&
Changes[i].Tok->is(tok::comment)393
)) &&
182
323k
        
Changes[i - 1].Tok->is(tok::comment)65.6k
&&
183
        // FIXME: This is a dirty hack. The problem is that
184
        // BreakableLineCommentSection does comment reflow changes and here is
185
        // the aligning of trailing comments. Consider the case where we reflow
186
        // the second line up in this example:
187
        //
188
        // // line 1
189
        // // line 2
190
        //
191
        // That amounts to 2 changes by BreakableLineCommentSection:
192
        //  - the first, delimited by (), for the whitespace between the tokens,
193
        //  - and second, delimited by [], for the whitespace at the beginning
194
        //  of the second token:
195
        //
196
        // // line 1(
197
        // )[// ]line 2
198
        //
199
        // So in the end we have two changes like this:
200
        //
201
        // // line1()[ ]line 2
202
        //
203
        // Note that the OriginalWhitespaceStart of the second change is the
204
        // same as the PreviousOriginalWhitespaceEnd of the first change.
205
        // In this case, the below check ensures that the second change doesn't
206
        // get treated as a trailing comment change here, since this might
207
        // trigger additional whitespace to be wrongly inserted before "line 2"
208
        // by the comment aligner here.
209
        //
210
        // For a proper solution we need a mechanism to say to WhitespaceManager
211
        // that a particular change breaks the current sequence of trailing
212
        // comments.
213
323k
        
OriginalWhitespaceStart != PreviousOriginalWhitespaceEnd5.91k
;
214
323k
  }
215
  // FIXME: The last token is currently not always an eof token; in those
216
  // cases, setting TokenLength of the last token to 0 is wrong.
217
20.8k
  Changes.back().TokenLength = 0;
218
20.8k
  Changes.back().IsTrailingComment = Changes.back().Tok->is(tok::comment);
219
220
20.8k
  const WhitespaceManager::Change *LastBlockComment = nullptr;
221
344k
  for (auto &Change : Changes) {
222
    // Reset the IsTrailingComment flag for changes inside of trailing comments
223
    // so they don't get realigned later. Comment line breaks however still need
224
    // to be aligned.
225
344k
    if (Change.IsInsideToken && 
Change.NewlinesBefore == 01.53k
)
226
393
      Change.IsTrailingComment = false;
227
344k
    Change.StartOfBlockComment = nullptr;
228
344k
    Change.IndentationOffset = 0;
229
344k
    if (Change.Tok->is(tok::comment)) {
230
6.42k
      if (Change.Tok->is(TT_LineComment) || 
!Change.IsInsideToken1.56k
)
231
5.75k
        LastBlockComment = &Change;
232
666
      else {
233
666
        if ((Change.StartOfBlockComment = LastBlockComment))
234
666
          Change.IndentationOffset =
235
666
              Change.StartOfTokenColumn -
236
666
              Change.StartOfBlockComment->StartOfTokenColumn;
237
666
      }
238
338k
    } else {
239
338k
      LastBlockComment = nullptr;
240
338k
    }
241
344k
  }
242
243
  // Compute conditional nesting level
244
  // Level is increased for each conditional, unless this conditional continues
245
  // a chain of conditional, i.e. starts immediately after the colon of another
246
  // conditional.
247
20.8k
  SmallVector<bool, 16> ScopeStack;
248
20.8k
  int ConditionalsLevel = 0;
249
344k
  for (auto &Change : Changes) {
250
370k
    for (unsigned i = 0, e = Change.Tok->FakeLParens.size(); i != e; 
++i25.8k
) {
251
25.8k
      bool isNestedConditional =
252
25.8k
          Change.Tok->FakeLParens[e - 1 - i] == prec::Conditional &&
253
25.8k
          
!(747
i == 0747
&&
Change.Tok->Previous743
&&
254
747
            
Change.Tok->Previous->is(TT_ConditionalExpr)716
&&
255
747
            
Change.Tok->Previous->is(tok::colon)209
);
256
25.8k
      if (isNestedConditional)
257
559
        ++ConditionalsLevel;
258
25.8k
      ScopeStack.push_back(isNestedConditional);
259
25.8k
    }
260
261
344k
    Change.ConditionalsLevel = ConditionalsLevel;
262
263
368k
    for (unsigned i = Change.Tok->FakeRParens; i > 0 && 
ScopeStack.size()23.8k
;
264
344k
         
--i23.8k
) {
265
23.8k
      if (ScopeStack.pop_back_val())
266
544
        --ConditionalsLevel;
267
23.8k
    }
268
344k
  }
269
20.8k
}
270
271
// Align a single sequence of tokens, see AlignTokens below.
272
template <typename F>
273
static void
274
AlignTokenSequence(const FormatStyle &Style, unsigned Start, unsigned End,
275
                   unsigned Column, F &&Matches,
276
1.94k
                   SmallVector<WhitespaceManager::Change, 16> &Changes) {
277
1.94k
  bool FoundMatchOnLine = false;
278
1.94k
  int Shift = 0;
279
280
  // ScopeStack keeps track of the current scope depth. It contains indices of
281
  // the first token on each scope.
282
  // We only run the "Matches" function on tokens from the outer-most scope.
283
  // However, we do need to pay special attention to one class of tokens
284
  // that are not in the outer-most scope, and that is function parameters
285
  // which are split across multiple lines, as illustrated by this example:
286
  //   double a(int x);
287
  //   int    b(int  y,
288
  //          double z);
289
  // In the above example, we need to take special care to ensure that
290
  // 'double z' is indented along with it's owning function 'b'.
291
  // The same holds for calling a function:
292
  //   double a = foo(x);
293
  //   int    b = bar(foo(y),
294
  //            foor(z));
295
  // Similar for broken string literals:
296
  //   double x = 3.14;
297
  //   auto s   = "Hello"
298
  //          "World";
299
  // Special handling is required for 'nested' ternary operators.
300
1.94k
  SmallVector<unsigned, 16> ScopeStack;
301
302
25.0k
  for (unsigned i = Start; i != End; 
++i23.1k
) {
303
23.1k
    if (ScopeStack.size() != 0 &&
304
23.1k
        Changes[i].indentAndNestingLevel() <
305
9.20k
            Changes[ScopeStack.back()].indentAndNestingLevel())
306
1.53k
      ScopeStack.pop_back();
307
308
    // Compare current token to previous non-comment token to ensure whether
309
    // it is in a deeper scope or not.
310
23.1k
    unsigned PreviousNonComment = i - 1;
311
23.4k
    while (PreviousNonComment > Start &&
312
23.4k
           
Changes[PreviousNonComment].Tok->is(tok::comment)19.5k
)
313
277
      --PreviousNonComment;
314
23.1k
    if (i != Start && Changes[i].indentAndNestingLevel() >
315
21.2k
                          Changes[PreviousNonComment].indentAndNestingLevel())
316
1.68k
      ScopeStack.push_back(i);
317
318
23.1k
    bool InsideNestedScope = ScopeStack.size() != 0;
319
23.1k
    bool ContinuedStringLiteral = i > Start &&
320
23.1k
                                  
Changes[i].Tok->is(tok::string_literal)21.2k
&&
321
23.1k
                                  
Changes[i - 1].Tok->is(tok::string_literal)423
;
322
23.1k
    bool SkipMatchCheck = InsideNestedScope || 
ContinuedStringLiteral13.7k
;
323
324
23.1k
    if (Changes[i].NewlinesBefore > 0 && 
!SkipMatchCheck2.76k
) {
325
1.79k
      Shift = 0;
326
1.79k
      FoundMatchOnLine = false;
327
1.79k
    }
328
329
    // If this is the first matching token to be aligned, remember by how many
330
    // spaces it has to be shifted, so the rest of the changes on the line are
331
    // shifted by the same amount
332
23.1k
    if (!FoundMatchOnLine && 
!SkipMatchCheck6.43k
&&
Matches(Changes[i])6.12k
) {
333
3.34k
      FoundMatchOnLine = true;
334
3.34k
      Shift = Column - Changes[i].StartOfTokenColumn;
335
3.34k
      Changes[i].Spaces += Shift;
336
3.34k
    }
337
338
    // This is for function parameters that are split across multiple lines,
339
    // as mentioned in the ScopeStack comment.
340
23.1k
    if (InsideNestedScope && 
Changes[i].NewlinesBefore > 09.35k
) {
341
949
      unsigned ScopeStart = ScopeStack.back();
342
949
      auto ShouldShiftBeAdded = [&] {
343
        // Function declaration
344
949
        if (Changes[ScopeStart - 1].Tok->is(TT_FunctionDeclarationName))
345
0
          return true;
346
347
        // Continued function declaration
348
949
        if (ScopeStart > Start + 1 &&
349
949
            
Changes[ScopeStart - 2].Tok->is(TT_FunctionDeclarationName)922
)
350
30
          return true;
351
352
        // Continued function call
353
919
        if (ScopeStart > Start + 1 &&
354
919
            
Changes[ScopeStart - 2].Tok->is(tok::identifier)892
&&
355
919
            
Changes[ScopeStart - 1].Tok->is(tok::l_paren)190
)
356
108
          return Style.BinPackArguments;
357
358
        // Ternary operator
359
811
        if (Changes[i].Tok->is(TT_ConditionalExpr))
360
66
          return true;
361
362
        // Period Initializer .XXX = 1.
363
745
        if (Changes[i].Tok->is(TT_DesignatedInitializerPeriod))
364
36
          return true;
365
366
        // Continued ternary operator
367
709
        if (Changes[i].Tok->Previous &&
368
709
            
Changes[i].Tok->Previous->is(TT_ConditionalExpr)431
)
369
48
          return true;
370
371
        // Continued braced list.
372
661
        if (ScopeStart > Start + 1 &&
373
661
            
Changes[ScopeStart - 2].Tok->isNot(tok::identifier)658
&&
374
661
            
Changes[ScopeStart - 1].Tok->is(tok::l_brace)582
&&
375
661
            
Changes[i].Tok->isNot(tok::r_brace)552
)
376
515
          return true;
377
378
146
        return false;
379
661
      };
WhitespaceManager.cpp:void clang::format::AlignTokenSequence<clang::format::WhitespaceManager::alignConsecutiveAssignments()::$_1&>(clang::format::FormatStyle const&, unsigned int, unsigned int, unsigned int, clang::format::WhitespaceManager::alignConsecutiveAssignments()::$_1&, llvm::SmallVector<clang::format::WhitespaceManager::Change, 16u>&)::'lambda'()::operator()() const
Line
Count
Source
342
291
      auto ShouldShiftBeAdded = [&] {
343
        // Function declaration
344
291
        if (Changes[ScopeStart - 1].Tok->is(TT_FunctionDeclarationName))
345
0
          return true;
346
347
        // Continued function declaration
348
291
        if (ScopeStart > Start + 1 &&
349
291
            Changes[ScopeStart - 2].Tok->is(TT_FunctionDeclarationName))
350
0
          return true;
351
352
        // Continued function call
353
291
        if (ScopeStart > Start + 1 &&
354
291
            Changes[ScopeStart - 2].Tok->is(tok::identifier) &&
355
291
            
Changes[ScopeStart - 1].Tok->is(tok::l_paren)85
)
356
36
          return Style.BinPackArguments;
357
358
        // Ternary operator
359
255
        if (Changes[i].Tok->is(TT_ConditionalExpr))
360
0
          return true;
361
362
        // Period Initializer .XXX = 1.
363
255
        if (Changes[i].Tok->is(TT_DesignatedInitializerPeriod))
364
12
          return true;
365
366
        // Continued ternary operator
367
243
        if (Changes[i].Tok->Previous &&
368
243
            
Changes[i].Tok->Previous->is(TT_ConditionalExpr)195
)
369
0
          return true;
370
371
        // Continued braced list.
372
243
        if (ScopeStart > Start + 1 &&
373
243
            Changes[ScopeStart - 2].Tok->isNot(tok::identifier) &&
374
243
            
Changes[ScopeStart - 1].Tok->is(tok::l_brace)194
&&
375
243
            
Changes[i].Tok->isNot(tok::r_brace)176
)
376
156
          return true;
377
378
87
        return false;
379
243
      };
Unexecuted instantiation: WhitespaceManager.cpp:void clang::format::AlignTokenSequence<clang::format::WhitespaceManager::alignConsecutiveBitFields()::$_2&>(clang::format::FormatStyle const&, unsigned int, unsigned int, unsigned int, clang::format::WhitespaceManager::alignConsecutiveBitFields()::$_2&, llvm::SmallVector<clang::format::WhitespaceManager::Change, 16u>&)::'lambda'()::operator()() const
WhitespaceManager.cpp:void clang::format::AlignTokenSequence<clang::format::WhitespaceManager::alignConsecutiveDeclarations()::$_3&>(clang::format::FormatStyle const&, unsigned int, unsigned int, unsigned int, clang::format::WhitespaceManager::alignConsecutiveDeclarations()::$_3&, llvm::SmallVector<clang::format::WhitespaceManager::Change, 16u>&)::'lambda'()::operator()() const
Line
Count
Source
342
509
      auto ShouldShiftBeAdded = [&] {
343
        // Function declaration
344
509
        if (Changes[ScopeStart - 1].Tok->is(TT_FunctionDeclarationName))
345
0
          return true;
346
347
        // Continued function declaration
348
509
        if (ScopeStart > Start + 1 &&
349
509
            Changes[ScopeStart - 2].Tok->is(TT_FunctionDeclarationName))
350
30
          return true;
351
352
        // Continued function call
353
479
        if (ScopeStart > Start + 1 &&
354
479
            Changes[ScopeStart - 2].Tok->is(tok::identifier) &&
355
479
            
Changes[ScopeStart - 1].Tok->is(tok::l_paren)78
)
356
54
          return Style.BinPackArguments;
357
358
        // Ternary operator
359
425
        if (Changes[i].Tok->is(TT_ConditionalExpr))
360
0
          return true;
361
362
        // Period Initializer .XXX = 1.
363
425
        if (Changes[i].Tok->is(TT_DesignatedInitializerPeriod))
364
24
          return true;
365
366
        // Continued ternary operator
367
401
        if (Changes[i].Tok->Previous &&
368
401
            
Changes[i].Tok->Previous->is(TT_ConditionalExpr)176
)
369
0
          return true;
370
371
        // Continued braced list.
372
401
        if (ScopeStart > Start + 1 &&
373
401
            Changes[ScopeStart - 2].Tok->isNot(tok::identifier) &&
374
401
            
Changes[ScopeStart - 1].Tok->is(tok::l_brace)377
&&
375
401
            
Changes[i].Tok->isNot(tok::r_brace)365
)
376
348
          return true;
377
378
53
        return false;
379
401
      };
WhitespaceManager.cpp:void clang::format::AlignTokenSequence<clang::format::WhitespaceManager::alignChainedConditionals()::$_4&>(clang::format::FormatStyle const&, unsigned int, unsigned int, unsigned int, clang::format::WhitespaceManager::alignChainedConditionals()::$_4&, llvm::SmallVector<clang::format::WhitespaceManager::Change, 16u>&)::'lambda'()::operator()() const
Line
Count
Source
342
87
      auto ShouldShiftBeAdded = [&] {
343
        // Function declaration
344
87
        if (Changes[ScopeStart - 1].Tok->is(TT_FunctionDeclarationName))
345
0
          return true;
346
347
        // Continued function declaration
348
87
        if (ScopeStart > Start + 1 &&
349
87
            
Changes[ScopeStart - 2].Tok->is(TT_FunctionDeclarationName)75
)
350
0
          return true;
351
352
        // Continued function call
353
87
        if (ScopeStart > Start + 1 &&
354
87
            
Changes[ScopeStart - 2].Tok->is(tok::identifier)75
&&
355
87
            
Changes[ScopeStart - 1].Tok->is(tok::l_paren)18
)
356
9
          return Style.BinPackArguments;
357
358
        // Ternary operator
359
78
        if (Changes[i].Tok->is(TT_ConditionalExpr))
360
66
          return true;
361
362
        // Period Initializer .XXX = 1.
363
12
        if (Changes[i].Tok->is(TT_DesignatedInitializerPeriod))
364
0
          return true;
365
366
        // Continued ternary operator
367
12
        if (Changes[i].Tok->Previous &&
368
12
            
Changes[i].Tok->Previous->is(TT_ConditionalExpr)9
)
369
0
          return true;
370
371
        // Continued braced list.
372
12
        if (ScopeStart > Start + 1 &&
373
12
            Changes[ScopeStart - 2].Tok->isNot(tok::identifier) &&
374
12
            
Changes[ScopeStart - 1].Tok->is(tok::l_brace)9
&&
375
12
            
Changes[i].Tok->isNot(tok::r_brace)9
)
376
9
          return true;
377
378
3
        return false;
379
12
      };
WhitespaceManager.cpp:void clang::format::AlignTokenSequence<clang::format::WhitespaceManager::alignChainedConditionals()::$_6&>(clang::format::FormatStyle const&, unsigned int, unsigned int, unsigned int, clang::format::WhitespaceManager::alignChainedConditionals()::$_6&, llvm::SmallVector<clang::format::WhitespaceManager::Change, 16u>&)::'lambda'()::operator()() const
Line
Count
Source
342
62
      auto ShouldShiftBeAdded = [&] {
343
        // Function declaration
344
62
        if (Changes[ScopeStart - 1].Tok->is(TT_FunctionDeclarationName))
345
0
          return true;
346
347
        // Continued function declaration
348
62
        if (ScopeStart > Start + 1 &&
349
62
            
Changes[ScopeStart - 2].Tok->is(TT_FunctionDeclarationName)47
)
350
0
          return true;
351
352
        // Continued function call
353
62
        if (ScopeStart > Start + 1 &&
354
62
            
Changes[ScopeStart - 2].Tok->is(tok::identifier)47
&&
355
62
            
Changes[ScopeStart - 1].Tok->is(tok::l_paren)9
)
356
9
          return Style.BinPackArguments;
357
358
        // Ternary operator
359
53
        if (Changes[i].Tok->is(TT_ConditionalExpr))
360
0
          return true;
361
362
        // Period Initializer .XXX = 1.
363
53
        if (Changes[i].Tok->is(TT_DesignatedInitializerPeriod))
364
0
          return true;
365
366
        // Continued ternary operator
367
53
        if (Changes[i].Tok->Previous &&
368
53
            
Changes[i].Tok->Previous->is(TT_ConditionalExpr)51
)
369
48
          return true;
370
371
        // Continued braced list.
372
5
        if (ScopeStart > Start + 1 &&
373
5
            
Changes[ScopeStart - 2].Tok->isNot(tok::identifier)2
&&
374
5
            
Changes[ScopeStart - 1].Tok->is(tok::l_brace)2
&&
375
5
            
Changes[i].Tok->isNot(tok::r_brace)2
)
376
2
          return true;
377
378
3
        return false;
379
5
      };
380
381
949
      if (ShouldShiftBeAdded())
382
794
        Changes[i].Spaces += Shift;
383
949
    }
384
385
23.1k
    if (ContinuedStringLiteral)
386
47
      Changes[i].Spaces += Shift;
387
388
23.1k
    Changes[i].StartOfTokenColumn += Shift;
389
23.1k
    if (i + 1 != Changes.size())
390
22.6k
      Changes[i + 1].PreviousEndOfTokenColumn += Shift;
391
392
    // If PointerAlignment is PAS_Right, keep *s or &s next to the token
393
23.1k
    if (Style.PointerAlignment == FormatStyle::PAS_Right &&
394
23.1k
        
Changes[i].Spaces != 019.1k
) {
395
11.0k
      for (int Previous = i - 1;
396
11.1k
           Previous >= 0 &&
397
11.1k
           Changes[Previous].Tok->getType() == TT_PointerOrReference;
398
11.0k
           
--Previous71
) {
399
71
        Changes[Previous + 1].Spaces -= Shift;
400
71
        Changes[Previous].Spaces += Shift;
401
71
      }
402
11.0k
    }
403
23.1k
  }
404
1.94k
}
WhitespaceManager.cpp:void clang::format::AlignTokenSequence<clang::format::WhitespaceManager::alignConsecutiveAssignments()::$_1&>(clang::format::FormatStyle const&, unsigned int, unsigned int, unsigned int, clang::format::WhitespaceManager::alignConsecutiveAssignments()::$_1&, llvm::SmallVector<clang::format::WhitespaceManager::Change, 16u>&)
Line
Count
Source
276
593
                   SmallVector<WhitespaceManager::Change, 16> &Changes) {
277
593
  bool FoundMatchOnLine = false;
278
593
  int Shift = 0;
279
280
  // ScopeStack keeps track of the current scope depth. It contains indices of
281
  // the first token on each scope.
282
  // We only run the "Matches" function on tokens from the outer-most scope.
283
  // However, we do need to pay special attention to one class of tokens
284
  // that are not in the outer-most scope, and that is function parameters
285
  // which are split across multiple lines, as illustrated by this example:
286
  //   double a(int x);
287
  //   int    b(int  y,
288
  //          double z);
289
  // In the above example, we need to take special care to ensure that
290
  // 'double z' is indented along with it's owning function 'b'.
291
  // The same holds for calling a function:
292
  //   double a = foo(x);
293
  //   int    b = bar(foo(y),
294
  //            foor(z));
295
  // Similar for broken string literals:
296
  //   double x = 3.14;
297
  //   auto s   = "Hello"
298
  //          "World";
299
  // Special handling is required for 'nested' ternary operators.
300
593
  SmallVector<unsigned, 16> ScopeStack;
301
302
8.33k
  for (unsigned i = Start; i != End; 
++i7.74k
) {
303
7.74k
    if (ScopeStack.size() != 0 &&
304
7.74k
        Changes[i].indentAndNestingLevel() <
305
3.05k
            Changes[ScopeStack.back()].indentAndNestingLevel())
306
561
      ScopeStack.pop_back();
307
308
    // Compare current token to previous non-comment token to ensure whether
309
    // it is in a deeper scope or not.
310
7.74k
    unsigned PreviousNonComment = i - 1;
311
7.90k
    while (PreviousNonComment > Start &&
312
7.90k
           
Changes[PreviousNonComment].Tok->is(tok::comment)6.71k
)
313
161
      --PreviousNonComment;
314
7.74k
    if (i != Start && Changes[i].indentAndNestingLevel() >
315
7.15k
                          Changes[PreviousNonComment].indentAndNestingLevel())
316
594
      ScopeStack.push_back(i);
317
318
7.74k
    bool InsideNestedScope = ScopeStack.size() != 0;
319
7.74k
    bool ContinuedStringLiteral = i > Start &&
320
7.74k
                                  
Changes[i].Tok->is(tok::string_literal)7.15k
&&
321
7.74k
                                  
Changes[i - 1].Tok->is(tok::string_literal)184
;
322
7.74k
    bool SkipMatchCheck = InsideNestedScope || 
ContinuedStringLiteral4.65k
;
323
324
7.74k
    if (Changes[i].NewlinesBefore > 0 && 
!SkipMatchCheck855
) {
325
552
      Shift = 0;
326
552
      FoundMatchOnLine = false;
327
552
    }
328
329
    // If this is the first matching token to be aligned, remember by how many
330
    // spaces it has to be shifted, so the rest of the changes on the line are
331
    // shifted by the same amount
332
7.74k
    if (!FoundMatchOnLine && 
!SkipMatchCheck2.38k
&&
Matches(Changes[i])2.17k
) {
333
987
      FoundMatchOnLine = true;
334
987
      Shift = Column - Changes[i].StartOfTokenColumn;
335
987
      Changes[i].Spaces += Shift;
336
987
    }
337
338
    // This is for function parameters that are split across multiple lines,
339
    // as mentioned in the ScopeStack comment.
340
7.74k
    if (InsideNestedScope && 
Changes[i].NewlinesBefore > 03.09k
) {
341
291
      unsigned ScopeStart = ScopeStack.back();
342
291
      auto ShouldShiftBeAdded = [&] {
343
        // Function declaration
344
291
        if (Changes[ScopeStart - 1].Tok->is(TT_FunctionDeclarationName))
345
291
          return true;
346
347
        // Continued function declaration
348
291
        if (ScopeStart > Start + 1 &&
349
291
            Changes[ScopeStart - 2].Tok->is(TT_FunctionDeclarationName))
350
291
          return true;
351
352
        // Continued function call
353
291
        if (ScopeStart > Start + 1 &&
354
291
            Changes[ScopeStart - 2].Tok->is(tok::identifier) &&
355
291
            Changes[ScopeStart - 1].Tok->is(tok::l_paren))
356
291
          return Style.BinPackArguments;
357
358
        // Ternary operator
359
291
        if (Changes[i].Tok->is(TT_ConditionalExpr))
360
291
          return true;
361
362
        // Period Initializer .XXX = 1.
363
291
        if (Changes[i].Tok->is(TT_DesignatedInitializerPeriod))
364
291
          return true;
365
366
        // Continued ternary operator
367
291
        if (Changes[i].Tok->Previous &&
368
291
            Changes[i].Tok->Previous->is(TT_ConditionalExpr))
369
291
          return true;
370
371
        // Continued braced list.
372
291
        if (ScopeStart > Start + 1 &&
373
291
            Changes[ScopeStart - 2].Tok->isNot(tok::identifier) &&
374
291
            Changes[ScopeStart - 1].Tok->is(tok::l_brace) &&
375
291
            Changes[i].Tok->isNot(tok::r_brace))
376
291
          return true;
377
378
291
        return false;
379
291
      };
380
381
291
      if (ShouldShiftBeAdded())
382
195
        Changes[i].Spaces += Shift;
383
291
    }
384
385
7.74k
    if (ContinuedStringLiteral)
386
16
      Changes[i].Spaces += Shift;
387
388
7.74k
    Changes[i].StartOfTokenColumn += Shift;
389
7.74k
    if (i + 1 != Changes.size())
390
7.53k
      Changes[i + 1].PreviousEndOfTokenColumn += Shift;
391
392
    // If PointerAlignment is PAS_Right, keep *s or &s next to the token
393
7.74k
    if (Style.PointerAlignment == FormatStyle::PAS_Right &&
394
7.74k
        
Changes[i].Spaces != 07.26k
) {
395
3.71k
      for (int Previous = i - 1;
396
3.71k
           Previous >= 0 &&
397
3.71k
           Changes[Previous].Tok->getType() == TT_PointerOrReference;
398
3.71k
           
--Previous0
) {
399
0
        Changes[Previous + 1].Spaces -= Shift;
400
0
        Changes[Previous].Spaces += Shift;
401
0
      }
402
3.71k
    }
403
7.74k
  }
404
593
}
WhitespaceManager.cpp:void clang::format::AlignTokenSequence<clang::format::WhitespaceManager::alignConsecutiveBitFields()::$_2&>(clang::format::FormatStyle const&, unsigned int, unsigned int, unsigned int, clang::format::WhitespaceManager::alignConsecutiveBitFields()::$_2&, llvm::SmallVector<clang::format::WhitespaceManager::Change, 16u>&)
Line
Count
Source
276
36
                   SmallVector<WhitespaceManager::Change, 16> &Changes) {
277
36
  bool FoundMatchOnLine = false;
278
36
  int Shift = 0;
279
280
  // ScopeStack keeps track of the current scope depth. It contains indices of
281
  // the first token on each scope.
282
  // We only run the "Matches" function on tokens from the outer-most scope.
283
  // However, we do need to pay special attention to one class of tokens
284
  // that are not in the outer-most scope, and that is function parameters
285
  // which are split across multiple lines, as illustrated by this example:
286
  //   double a(int x);
287
  //   int    b(int  y,
288
  //          double z);
289
  // In the above example, we need to take special care to ensure that
290
  // 'double z' is indented along with it's owning function 'b'.
291
  // The same holds for calling a function:
292
  //   double a = foo(x);
293
  //   int    b = bar(foo(y),
294
  //            foor(z));
295
  // Similar for broken string literals:
296
  //   double x = 3.14;
297
  //   auto s   = "Hello"
298
  //          "World";
299
  // Special handling is required for 'nested' ternary operators.
300
36
  SmallVector<unsigned, 16> ScopeStack;
301
302
439
  for (unsigned i = Start; i != End; 
++i403
) {
303
403
    if (ScopeStack.size() != 0 &&
304
403
        Changes[i].indentAndNestingLevel() <
305
6
            Changes[ScopeStack.back()].indentAndNestingLevel())
306
3
      ScopeStack.pop_back();
307
308
    // Compare current token to previous non-comment token to ensure whether
309
    // it is in a deeper scope or not.
310
403
    unsigned PreviousNonComment = i - 1;
311
413
    while (PreviousNonComment > Start &&
312
413
           
Changes[PreviousNonComment].Tok->is(tok::comment)341
)
313
10
      --PreviousNonComment;
314
403
    if (i != Start && Changes[i].indentAndNestingLevel() >
315
367
                          Changes[PreviousNonComment].indentAndNestingLevel())
316
3
      ScopeStack.push_back(i);
317
318
403
    bool InsideNestedScope = ScopeStack.size() != 0;
319
403
    bool ContinuedStringLiteral = i > Start &&
320
403
                                  
Changes[i].Tok->is(tok::string_literal)367
&&
321
403
                                  
Changes[i - 1].Tok->is(tok::string_literal)0
;
322
403
    bool SkipMatchCheck = InsideNestedScope || 
ContinuedStringLiteral397
;
323
324
403
    if (Changes[i].NewlinesBefore > 0 && 
!SkipMatchCheck43
) {
325
43
      Shift = 0;
326
43
      FoundMatchOnLine = false;
327
43
    }
328
329
    // If this is the first matching token to be aligned, remember by how many
330
    // spaces it has to be shifted, so the rest of the changes on the line are
331
    // shifted by the same amount
332
403
    if (!FoundMatchOnLine && 
!SkipMatchCheck157
&&
Matches(Changes[i])157
) {
333
75
      FoundMatchOnLine = true;
334
75
      Shift = Column - Changes[i].StartOfTokenColumn;
335
75
      Changes[i].Spaces += Shift;
336
75
    }
337
338
    // This is for function parameters that are split across multiple lines,
339
    // as mentioned in the ScopeStack comment.
340
403
    if (InsideNestedScope && 
Changes[i].NewlinesBefore > 06
) {
341
0
      unsigned ScopeStart = ScopeStack.back();
342
0
      auto ShouldShiftBeAdded = [&] {
343
        // Function declaration
344
0
        if (Changes[ScopeStart - 1].Tok->is(TT_FunctionDeclarationName))
345
0
          return true;
346
347
        // Continued function declaration
348
0
        if (ScopeStart > Start + 1 &&
349
0
            Changes[ScopeStart - 2].Tok->is(TT_FunctionDeclarationName))
350
0
          return true;
351
352
        // Continued function call
353
0
        if (ScopeStart > Start + 1 &&
354
0
            Changes[ScopeStart - 2].Tok->is(tok::identifier) &&
355
0
            Changes[ScopeStart - 1].Tok->is(tok::l_paren))
356
0
          return Style.BinPackArguments;
357
358
        // Ternary operator
359
0
        if (Changes[i].Tok->is(TT_ConditionalExpr))
360
0
          return true;
361
362
        // Period Initializer .XXX = 1.
363
0
        if (Changes[i].Tok->is(TT_DesignatedInitializerPeriod))
364
0
          return true;
365
366
        // Continued ternary operator
367
0
        if (Changes[i].Tok->Previous &&
368
0
            Changes[i].Tok->Previous->is(TT_ConditionalExpr))
369
0
          return true;
370
371
        // Continued braced list.
372
0
        if (ScopeStart > Start + 1 &&
373
0
            Changes[ScopeStart - 2].Tok->isNot(tok::identifier) &&
374
0
            Changes[ScopeStart - 1].Tok->is(tok::l_brace) &&
375
0
            Changes[i].Tok->isNot(tok::r_brace))
376
0
          return true;
377
378
0
        return false;
379
0
      };
380
381
0
      if (ShouldShiftBeAdded())
382
0
        Changes[i].Spaces += Shift;
383
0
    }
384
385
403
    if (ContinuedStringLiteral)
386
0
      Changes[i].Spaces += Shift;
387
388
403
    Changes[i].StartOfTokenColumn += Shift;
389
403
    if (i + 1 != Changes.size())
390
367
      Changes[i + 1].PreviousEndOfTokenColumn += Shift;
391
392
    // If PointerAlignment is PAS_Right, keep *s or &s next to the token
393
403
    if (Style.PointerAlignment == FormatStyle::PAS_Right &&
394
403
        Changes[i].Spaces != 0) {
395
225
      for (int Previous = i - 1;
396
225
           Previous >= 0 &&
397
225
           Changes[Previous].Tok->getType() == TT_PointerOrReference;
398
225
           
--Previous0
) {
399
0
        Changes[Previous + 1].Spaces -= Shift;
400
0
        Changes[Previous].Spaces += Shift;
401
0
      }
402
225
    }
403
403
  }
404
36
}
WhitespaceManager.cpp:void clang::format::AlignTokenSequence<clang::format::WhitespaceManager::alignConsecutiveDeclarations()::$_3&>(clang::format::FormatStyle const&, unsigned int, unsigned int, unsigned int, clang::format::WhitespaceManager::alignConsecutiveDeclarations()::$_3&, llvm::SmallVector<clang::format::WhitespaceManager::Change, 16u>&)
Line
Count
Source
276
738
                   SmallVector<WhitespaceManager::Change, 16> &Changes) {
277
738
  bool FoundMatchOnLine = false;
278
738
  int Shift = 0;
279
280
  // ScopeStack keeps track of the current scope depth. It contains indices of
281
  // the first token on each scope.
282
  // We only run the "Matches" function on tokens from the outer-most scope.
283
  // However, we do need to pay special attention to one class of tokens
284
  // that are not in the outer-most scope, and that is function parameters
285
  // which are split across multiple lines, as illustrated by this example:
286
  //   double a(int x);
287
  //   int    b(int  y,
288
  //          double z);
289
  // In the above example, we need to take special care to ensure that
290
  // 'double z' is indented along with it's owning function 'b'.
291
  // The same holds for calling a function:
292
  //   double a = foo(x);
293
  //   int    b = bar(foo(y),
294
  //            foor(z));
295
  // Similar for broken string literals:
296
  //   double x = 3.14;
297
  //   auto s   = "Hello"
298
  //          "World";
299
  // Special handling is required for 'nested' ternary operators.
300
738
  SmallVector<unsigned, 16> ScopeStack;
301
302
12.3k
  for (unsigned i = Start; i != End; 
++i11.6k
) {
303
11.6k
    if (ScopeStack.size() != 0 &&
304
11.6k
        Changes[i].indentAndNestingLevel() <
305
5.41k
            Changes[ScopeStack.back()].indentAndNestingLevel())
306
848
      ScopeStack.pop_back();
307
308
    // Compare current token to previous non-comment token to ensure whether
309
    // it is in a deeper scope or not.
310
11.6k
    unsigned PreviousNonComment = i - 1;
311
11.7k
    while (PreviousNonComment > Start &&
312
11.7k
           
Changes[PreviousNonComment].Tok->is(tok::comment)10.2k
)
313
94
      --PreviousNonComment;
314
11.6k
    if (i != Start && Changes[i].indentAndNestingLevel() >
315
10.8k
                          Changes[PreviousNonComment].indentAndNestingLevel())
316
929
      ScopeStack.push_back(i);
317
318
11.6k
    bool InsideNestedScope = ScopeStack.size() != 0;
319
11.6k
    bool ContinuedStringLiteral = i > Start &&
320
11.6k
                                  
Changes[i].Tok->is(tok::string_literal)10.8k
&&
321
11.6k
                                  
Changes[i - 1].Tok->is(tok::string_literal)211
;
322
11.6k
    bool SkipMatchCheck = InsideNestedScope || 
ContinuedStringLiteral6.14k
;
323
324
11.6k
    if (Changes[i].NewlinesBefore > 0 && 
!SkipMatchCheck1.15k
) {
325
638
      Shift = 0;
326
638
      FoundMatchOnLine = false;
327
638
    }
328
329
    // If this is the first matching token to be aligned, remember by how many
330
    // spaces it has to be shifted, so the rest of the changes on the line are
331
    // shifted by the same amount
332
11.6k
    if (!FoundMatchOnLine && 
!SkipMatchCheck2.61k
&&
Matches(Changes[i])2.52k
) {
333
1.27k
      FoundMatchOnLine = true;
334
1.27k
      Shift = Column - Changes[i].StartOfTokenColumn;
335
1.27k
      Changes[i].Spaces += Shift;
336
1.27k
    }
337
338
    // This is for function parameters that are split across multiple lines,
339
    // as mentioned in the ScopeStack comment.
340
11.6k
    if (InsideNestedScope && 
Changes[i].NewlinesBefore > 05.49k
) {
341
509
      unsigned ScopeStart = ScopeStack.back();
342
509
      auto ShouldShiftBeAdded = [&] {
343
        // Function declaration
344
509
        if (Changes[ScopeStart - 1].Tok->is(TT_FunctionDeclarationName))
345
509
          return true;
346
347
        // Continued function declaration
348
509
        if (ScopeStart > Start + 1 &&
349
509
            Changes[ScopeStart - 2].Tok->is(TT_FunctionDeclarationName))
350
509
          return true;
351
352
        // Continued function call
353
509
        if (ScopeStart > Start + 1 &&
354
509
            Changes[ScopeStart - 2].Tok->is(tok::identifier) &&
355
509
            Changes[ScopeStart - 1].Tok->is(tok::l_paren))
356
509
          return Style.BinPackArguments;
357
358
        // Ternary operator
359
509
        if (Changes[i].Tok->is(TT_ConditionalExpr))
360
509
          return true;
361
362
        // Period Initializer .XXX = 1.
363
509
        if (Changes[i].Tok->is(TT_DesignatedInitializerPeriod))
364
509
          return true;
365
366
        // Continued ternary operator
367
509
        if (Changes[i].Tok->Previous &&
368
509
            Changes[i].Tok->Previous->is(TT_ConditionalExpr))
369
509
          return true;
370
371
        // Continued braced list.
372
509
        if (ScopeStart > Start + 1 &&
373
509
            Changes[ScopeStart - 2].Tok->isNot(tok::identifier) &&
374
509
            Changes[ScopeStart - 1].Tok->is(tok::l_brace) &&
375
509
            Changes[i].Tok->isNot(tok::r_brace))
376
509
          return true;
377
378
509
        return false;
379
509
      };
380
381
509
      if (ShouldShiftBeAdded())
382
456
        Changes[i].Spaces += Shift;
383
509
    }
384
385
11.6k
    if (ContinuedStringLiteral)
386
28
      Changes[i].Spaces += Shift;
387
388
11.6k
    Changes[i].StartOfTokenColumn += Shift;
389
11.6k
    if (i + 1 != Changes.size())
390
11.3k
      Changes[i + 1].PreviousEndOfTokenColumn += Shift;
391
392
    // If PointerAlignment is PAS_Right, keep *s or &s next to the token
393
11.6k
    if (Style.PointerAlignment == FormatStyle::PAS_Right &&
394
11.6k
        
Changes[i].Spaces != 08.35k
) {
395
4.28k
      for (int Previous = i - 1;
396
4.35k
           Previous >= 0 &&
397
4.35k
           Changes[Previous].Tok->getType() == TT_PointerOrReference;
398
4.28k
           
--Previous71
) {
399
71
        Changes[Previous + 1].Spaces -= Shift;
400
71
        Changes[Previous].Spaces += Shift;
401
71
      }
402
4.28k
    }
403
11.6k
  }
404
738
}
WhitespaceManager.cpp:void clang::format::AlignTokenSequence<clang::format::WhitespaceManager::alignChainedConditionals()::$_4&>(clang::format::FormatStyle const&, unsigned int, unsigned int, unsigned int, clang::format::WhitespaceManager::alignChainedConditionals()::$_4&, llvm::SmallVector<clang::format::WhitespaceManager::Change, 16u>&)
Line
Count
Source
276
376
                   SmallVector<WhitespaceManager::Change, 16> &Changes) {
277
376
  bool FoundMatchOnLine = false;
278
376
  int Shift = 0;
279
280
  // ScopeStack keeps track of the current scope depth. It contains indices of
281
  // the first token on each scope.
282
  // We only run the "Matches" function on tokens from the outer-most scope.
283
  // However, we do need to pay special attention to one class of tokens
284
  // that are not in the outer-most scope, and that is function parameters
285
  // which are split across multiple lines, as illustrated by this example:
286
  //   double a(int x);
287
  //   int    b(int  y,
288
  //          double z);
289
  // In the above example, we need to take special care to ensure that
290
  // 'double z' is indented along with it's owning function 'b'.
291
  // The same holds for calling a function:
292
  //   double a = foo(x);
293
  //   int    b = bar(foo(y),
294
  //            foor(z));
295
  // Similar for broken string literals:
296
  //   double x = 3.14;
297
  //   auto s   = "Hello"
298
  //          "World";
299
  // Special handling is required for 'nested' ternary operators.
300
376
  SmallVector<unsigned, 16> ScopeStack;
301
302
2.37k
  for (unsigned i = Start; i != End; 
++i1.99k
) {
303
1.99k
    if (ScopeStack.size() != 0 &&
304
1.99k
        Changes[i].indentAndNestingLevel() <
305
427
            Changes[ScopeStack.back()].indentAndNestingLevel())
306
73
      ScopeStack.pop_back();
307
308
    // Compare current token to previous non-comment token to ensure whether
309
    // it is in a deeper scope or not.
310
1.99k
    unsigned PreviousNonComment = i - 1;
311
2.00k
    while (PreviousNonComment > Start &&
312
2.00k
           
Changes[PreviousNonComment].Tok->is(tok::comment)1.25k
)
313
9
      --PreviousNonComment;
314
1.99k
    if (i != Start && Changes[i].indentAndNestingLevel() >
315
1.62k
                          Changes[PreviousNonComment].indentAndNestingLevel())
316
94
      ScopeStack.push_back(i);
317
318
1.99k
    bool InsideNestedScope = ScopeStack.size() != 0;
319
1.99k
    bool ContinuedStringLiteral = i > Start &&
320
1.99k
                                  
Changes[i].Tok->is(tok::string_literal)1.62k
&&
321
1.99k
                                  
Changes[i - 1].Tok->is(tok::string_literal)20
;
322
1.99k
    bool SkipMatchCheck = InsideNestedScope || 
ContinuedStringLiteral1.55k
;
323
324
1.99k
    if (Changes[i].NewlinesBefore > 0 && 
!SkipMatchCheck394
) {
325
304
      Shift = 0;
326
304
      FoundMatchOnLine = false;
327
304
    }
328
329
    // If this is the first matching token to be aligned, remember by how many
330
    // spaces it has to be shifted, so the rest of the changes on the line are
331
    // shifted by the same amount
332
1.99k
    if (!FoundMatchOnLine && 
!SkipMatchCheck787
&&
Matches(Changes[i])787
) {
333
610
      FoundMatchOnLine = true;
334
610
      Shift = Column - Changes[i].StartOfTokenColumn;
335
610
      Changes[i].Spaces += Shift;
336
610
    }
337
338
    // This is for function parameters that are split across multiple lines,
339
    // as mentioned in the ScopeStack comment.
340
1.99k
    if (InsideNestedScope && 
Changes[i].NewlinesBefore > 0448
) {
341
87
      unsigned ScopeStart = ScopeStack.back();
342
87
      auto ShouldShiftBeAdded = [&] {
343
        // Function declaration
344
87
        if (Changes[ScopeStart - 1].Tok->is(TT_FunctionDeclarationName))
345
87
          return true;
346
347
        // Continued function declaration
348
87
        if (ScopeStart > Start + 1 &&
349
87
            Changes[ScopeStart - 2].Tok->is(TT_FunctionDeclarationName))
350
87
          return true;
351
352
        // Continued function call
353
87
        if (ScopeStart > Start + 1 &&
354
87
            Changes[ScopeStart - 2].Tok->is(tok::identifier) &&
355
87
            Changes[ScopeStart - 1].Tok->is(tok::l_paren))
356
87
          return Style.BinPackArguments;
357
358
        // Ternary operator
359
87
        if (Changes[i].Tok->is(TT_ConditionalExpr))
360
87
          return true;
361
362
        // Period Initializer .XXX = 1.
363
87
        if (Changes[i].Tok->is(TT_DesignatedInitializerPeriod))
364
87
          return true;
365
366
        // Continued ternary operator
367
87
        if (Changes[i].Tok->Previous &&
368
87
            Changes[i].Tok->Previous->is(TT_ConditionalExpr))
369
87
          return true;
370
371
        // Continued braced list.
372
87
        if (ScopeStart > Start + 1 &&
373
87
            Changes[ScopeStart - 2].Tok->isNot(tok::identifier) &&
374
87
            Changes[ScopeStart - 1].Tok->is(tok::l_brace) &&
375
87
            Changes[i].Tok->isNot(tok::r_brace))
376
87
          return true;
377
378
87
        return false;
379
87
      };
380
381
87
      if (ShouldShiftBeAdded())
382
84
        Changes[i].Spaces += Shift;
383
87
    }
384
385
1.99k
    if (ContinuedStringLiteral)
386
3
      Changes[i].Spaces += Shift;
387
388
1.99k
    Changes[i].StartOfTokenColumn += Shift;
389
1.99k
    if (i + 1 != Changes.size())
390
1.99k
      Changes[i + 1].PreviousEndOfTokenColumn += Shift;
391
392
    // If PointerAlignment is PAS_Right, keep *s or &s next to the token
393
1.99k
    if (Style.PointerAlignment == FormatStyle::PAS_Right &&
394
1.99k
        
Changes[i].Spaces != 01.91k
) {
395
1.70k
      for (int Previous = i - 1;
396
1.70k
           Previous >= 0 &&
397
1.70k
           Changes[Previous].Tok->getType() == TT_PointerOrReference;
398
1.70k
           
--Previous0
) {
399
0
        Changes[Previous + 1].Spaces -= Shift;
400
0
        Changes[Previous].Spaces += Shift;
401
0
      }
402
1.70k
    }
403
1.99k
  }
404
376
}
WhitespaceManager.cpp:void clang::format::AlignTokenSequence<clang::format::WhitespaceManager::alignChainedConditionals()::$_6&>(clang::format::FormatStyle const&, unsigned int, unsigned int, unsigned int, clang::format::WhitespaceManager::alignChainedConditionals()::$_6&, llvm::SmallVector<clang::format::WhitespaceManager::Change, 16u>&)
Line
Count
Source
276
203
                   SmallVector<WhitespaceManager::Change, 16> &Changes) {
277
203
  bool FoundMatchOnLine = false;
278
203
  int Shift = 0;
279
280
  // ScopeStack keeps track of the current scope depth. It contains indices of
281
  // the first token on each scope.
282
  // We only run the "Matches" function on tokens from the outer-most scope.
283
  // However, we do need to pay special attention to one class of tokens
284
  // that are not in the outer-most scope, and that is function parameters
285
  // which are split across multiple lines, as illustrated by this example:
286
  //   double a(int x);
287
  //   int    b(int  y,
288
  //          double z);
289
  // In the above example, we need to take special care to ensure that
290
  // 'double z' is indented along with it's owning function 'b'.
291
  // The same holds for calling a function:
292
  //   double a = foo(x);
293
  //   int    b = bar(foo(y),
294
  //            foor(z));
295
  // Similar for broken string literals:
296
  //   double x = 3.14;
297
  //   auto s   = "Hello"
298
  //          "World";
299
  // Special handling is required for 'nested' ternary operators.
300
203
  SmallVector<unsigned, 16> ScopeStack;
301
302
1.56k
  for (unsigned i = Start; i != End; 
++i1.36k
) {
303
1.36k
    if (ScopeStack.size() != 0 &&
304
1.36k
        Changes[i].indentAndNestingLevel() <
305
302
            Changes[ScopeStack.back()].indentAndNestingLevel())
306
50
      ScopeStack.pop_back();
307
308
    // Compare current token to previous non-comment token to ensure whether
309
    // it is in a deeper scope or not.
310
1.36k
    unsigned PreviousNonComment = i - 1;
311
1.36k
    while (PreviousNonComment > Start &&
312
1.36k
           
Changes[PreviousNonComment].Tok->is(tok::comment)990
)
313
3
      --PreviousNonComment;
314
1.36k
    if (i != Start && Changes[i].indentAndNestingLevel() >
315
1.16k
                          Changes[PreviousNonComment].indentAndNestingLevel())
316
65
      ScopeStack.push_back(i);
317
318
1.36k
    bool InsideNestedScope = ScopeStack.size() != 0;
319
1.36k
    bool ContinuedStringLiteral = i > Start &&
320
1.36k
                                  
Changes[i].Tok->is(tok::string_literal)1.16k
&&
321
1.36k
                                  
Changes[i - 1].Tok->is(tok::string_literal)8
;
322
1.36k
    bool SkipMatchCheck = InsideNestedScope || 
ContinuedStringLiteral1.04k
;
323
324
1.36k
    if (Changes[i].NewlinesBefore > 0 && 
!SkipMatchCheck316
) {
325
254
      Shift = 0;
326
254
      FoundMatchOnLine = false;
327
254
    }
328
329
    // If this is the first matching token to be aligned, remember by how many
330
    // spaces it has to be shifted, so the rest of the changes on the line are
331
    // shifted by the same amount
332
1.36k
    if (!FoundMatchOnLine && 
!SkipMatchCheck484
&&
Matches(Changes[i])484
) {
333
403
      FoundMatchOnLine = true;
334
403
      Shift = Column - Changes[i].StartOfTokenColumn;
335
403
      Changes[i].Spaces += Shift;
336
403
    }
337
338
    // This is for function parameters that are split across multiple lines,
339
    // as mentioned in the ScopeStack comment.
340
1.36k
    if (InsideNestedScope && 
Changes[i].NewlinesBefore > 0317
) {
341
62
      unsigned ScopeStart = ScopeStack.back();
342
62
      auto ShouldShiftBeAdded = [&] {
343
        // Function declaration
344
62
        if (Changes[ScopeStart - 1].Tok->is(TT_FunctionDeclarationName))
345
62
          return true;
346
347
        // Continued function declaration
348
62
        if (ScopeStart > Start + 1 &&
349
62
            Changes[ScopeStart - 2].Tok->is(TT_FunctionDeclarationName))
350
62
          return true;
351
352
        // Continued function call
353
62
        if (ScopeStart > Start + 1 &&
354
62
            Changes[ScopeStart - 2].Tok->is(tok::identifier) &&
355
62
            Changes[ScopeStart - 1].Tok->is(tok::l_paren))
356
62
          return Style.BinPackArguments;
357
358
        // Ternary operator
359
62
        if (Changes[i].Tok->is(TT_ConditionalExpr))
360
62
          return true;
361
362
        // Period Initializer .XXX = 1.
363
62
        if (Changes[i].Tok->is(TT_DesignatedInitializerPeriod))
364
62
          return true;
365
366
        // Continued ternary operator
367
62
        if (Changes[i].Tok->Previous &&
368
62
            Changes[i].Tok->Previous->is(TT_ConditionalExpr))
369
62
          return true;
370
371
        // Continued braced list.
372
62
        if (ScopeStart > Start + 1 &&
373
62
            Changes[ScopeStart - 2].Tok->isNot(tok::identifier) &&
374
62
            Changes[ScopeStart - 1].Tok->is(tok::l_brace) &&
375
62
            Changes[i].Tok->isNot(tok::r_brace))
376
62
          return true;
377
378
62
        return false;
379
62
      };
380
381
62
      if (ShouldShiftBeAdded())
382
59
        Changes[i].Spaces += Shift;
383
62
    }
384
385
1.36k
    if (ContinuedStringLiteral)
386
0
      Changes[i].Spaces += Shift;
387
388
1.36k
    Changes[i].StartOfTokenColumn += Shift;
389
1.36k
    if (i + 1 != Changes.size())
390
1.36k
      Changes[i + 1].PreviousEndOfTokenColumn += Shift;
391
392
    // If PointerAlignment is PAS_Right, keep *s or &s next to the token
393
1.36k
    if (Style.PointerAlignment == FormatStyle::PAS_Right &&
394
1.36k
        
Changes[i].Spaces != 01.23k
) {
395
1.12k
      for (int Previous = i - 1;
396
1.12k
           Previous >= 0 &&
397
1.12k
           Changes[Previous].Tok->getType() == TT_PointerOrReference;
398
1.12k
           
--Previous0
) {
399
0
        Changes[Previous + 1].Spaces -= Shift;
400
0
        Changes[Previous].Spaces += Shift;
401
0
      }
402
1.12k
    }
403
1.36k
  }
404
203
}
405
406
// Walk through a subset of the changes, starting at StartAt, and find
407
// sequences of matching tokens to align. To do so, keep track of the lines and
408
// whether or not a matching token was found on a line. If a matching token is
409
// found, extend the current sequence. If the current line cannot be part of a
410
// sequence, e.g. because there is an empty line before it or it contains only
411
// non-matching tokens, finalize the previous sequence.
412
// The value returned is the token on which we stopped, either because we
413
// exhausted all items inside Changes, or because we hit a scope level higher
414
// than our initial scope.
415
// This function is recursive. Each invocation processes only the scope level
416
// equal to the initial level, which is the level of Changes[StartAt].
417
// If we encounter a scope level greater than the initial level, then we call
418
// ourselves recursively, thereby avoiding the pollution of the current state
419
// with the alignment requirements of the nested sub-level. This recursive
420
// behavior is necessary for aligning function prototypes that have one or more
421
// arguments.
422
// If this function encounters a scope level less than the initial level,
423
// it returns the current position.
424
// There is a non-obvious subtlety in the recursive behavior: Even though we
425
// defer processing of nested levels to recursive invocations of this
426
// function, when it comes time to align a sequence of tokens, we run the
427
// alignment on the entire sequence, including the nested levels.
428
// When doing so, most of the nested tokens are skipped, because their
429
// alignment was already handled by the recursive invocations of this function.
430
// However, the special exception is that we do NOT skip function parameters
431
// that are split across multiple lines. See the test case in FormatTest.cpp
432
// that mentions "split function parameter alignment" for an example of this.
433
template <typename F>
434
static unsigned AlignTokens(
435
    const FormatStyle &Style, F &&Matches,
436
    SmallVector<WhitespaceManager::Change, 16> &Changes, unsigned StartAt,
437
76.7k
    const FormatStyle::AlignConsecutiveStyle &ACS = FormatStyle::ACS_None) {
438
76.7k
  unsigned MinColumn = 0;
439
76.7k
  unsigned MaxColumn = UINT_MAX;
440
441
  // Line number of the start and the end of the current token sequence.
442
76.7k
  unsigned StartOfSequence = 0;
443
76.7k
  unsigned EndOfSequence = 0;
444
445
  // Measure the scope level (i.e. depth of (), [], {}) of the first token, and
446
  // abort when we hit any token in a higher scope than the starting one.
447
76.7k
  auto IndentAndNestingLevel = StartAt < Changes.size()
448
76.7k
                                   ? Changes[StartAt].indentAndNestingLevel()
449
76.7k
                                   : 
std::tuple<unsigned, unsigned, unsigned>()0
;
450
451
  // Keep track of the number of commas before the matching tokens, we will only
452
  // align a sequence of matching tokens if they are preceded by the same number
453
  // of commas.
454
76.7k
  unsigned CommasBeforeLastMatch = 0;
455
76.7k
  unsigned CommasBeforeMatch = 0;
456
457
  // Whether a matching token has been found on the current line.
458
76.7k
  bool FoundMatchOnLine = false;
459
460
  // Whether the current line consists purely of comments.
461
76.7k
  bool LineIsComment = true;
462
463
  // Aligns a sequence of matching tokens, on the MinColumn column.
464
  //
465
  // Sequences start from the first matching token to align, and end at the
466
  // first token of the first line that doesn't need to be aligned.
467
  //
468
  // We need to adjust the StartOfTokenColumn of each Change that is on a line
469
  // containing any matching token to be aligned and located after such token.
470
137k
  auto AlignCurrentSequence = [&] {
471
137k
    if (StartOfSequence > 0 && 
StartOfSequence < EndOfSequence2.30k
)
472
1.94k
      AlignTokenSequence(Style, StartOfSequence, EndOfSequence, MinColumn,
473
1.94k
                         Matches, Changes);
474
137k
    MinColumn = 0;
475
137k
    MaxColumn = UINT_MAX;
476
137k
    StartOfSequence = 0;
477
137k
    EndOfSequence = 0;
478
137k
  };
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, clang::format::FormatStyle::AlignConsecutiveStyle const&)::'lambda'()::operator()() const
Line
Count
Source
470
1.18k
  auto AlignCurrentSequence = [&] {
471
1.18k
    if (StartOfSequence > 0 && 
StartOfSequence < EndOfSequence371
)
472
365
      AlignTokenSequence(Style, StartOfSequence, EndOfSequence, MinColumn,
473
365
                         Matches, Changes);
474
1.18k
    MinColumn = 0;
475
1.18k
    MaxColumn = UINT_MAX;
476
1.18k
    StartOfSequence = 0;
477
1.18k
    EndOfSequence = 0;
478
1.18k
  };
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, clang::format::FormatStyle::AlignConsecutiveStyle const&)::'lambda'()::operator()() const
Line
Count
Source
470
1.97k
  auto AlignCurrentSequence = [&] {
471
1.97k
    if (StartOfSequence > 0 && 
StartOfSequence < EndOfSequence252
)
472
228
      AlignTokenSequence(Style, StartOfSequence, EndOfSequence, MinColumn,
473
228
                         Matches, Changes);
474
1.97k
    MinColumn = 0;
475
1.97k
    MaxColumn = UINT_MAX;
476
1.97k
    StartOfSequence = 0;
477
1.97k
    EndOfSequence = 0;
478
1.97k
  };
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, clang::format::FormatStyle::AlignConsecutiveStyle const&)::'lambda'()::operator()() const
Line
Count
Source
470
36
  auto AlignCurrentSequence = [&] {
471
36
    if (StartOfSequence > 0 && StartOfSequence < EndOfSequence)
472
36
      AlignTokenSequence(Style, StartOfSequence, EndOfSequence, MinColumn,
473
36
                         Matches, Changes);
474
36
    MinColumn = 0;
475
36
    MaxColumn = UINT_MAX;
476
36
    StartOfSequence = 0;
477
36
    EndOfSequence = 0;
478
36
  };
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, clang::format::FormatStyle::AlignConsecutiveStyle const&)::'lambda'()::operator()() const
Line
Count
Source
470
3
  auto AlignCurrentSequence = [&] {
471
3
    if (StartOfSequence > 0 && 
StartOfSequence < EndOfSequence0
)
472
0
      AlignTokenSequence(Style, StartOfSequence, EndOfSequence, MinColumn,
473
0
                         Matches, Changes);
474
3
    MinColumn = 0;
475
3
    MaxColumn = UINT_MAX;
476
3
    StartOfSequence = 0;
477
3
    EndOfSequence = 0;
478
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, clang::format::FormatStyle::AlignConsecutiveStyle const&)::'lambda'()::operator()() const
Line
Count
Source
470
1.03k
  auto AlignCurrentSequence = [&] {
471
1.03k
    if (StartOfSequence > 0 && 
StartOfSequence < EndOfSequence408
)
472
408
      AlignTokenSequence(Style, StartOfSequence, EndOfSequence, MinColumn,
473
408
                         Matches, Changes);
474
1.03k
    MinColumn = 0;
475
1.03k
    MaxColumn = UINT_MAX;
476
1.03k
    StartOfSequence = 0;
477
1.03k
    EndOfSequence = 0;
478
1.03k
  };
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, clang::format::FormatStyle::AlignConsecutiveStyle const&)::'lambda'()::operator()() const
Line
Count
Source
470
2.20k
  auto AlignCurrentSequence = [&] {
471
2.20k
    if (StartOfSequence > 0 && 
StartOfSequence < EndOfSequence516
)
472
330
      AlignTokenSequence(Style, StartOfSequence, EndOfSequence, MinColumn,
473
330
                         Matches, Changes);
474
2.20k
    MinColumn = 0;
475
2.20k
    MaxColumn = UINT_MAX;
476
2.20k
    StartOfSequence = 0;
477
2.20k
    EndOfSequence = 0;
478
2.20k
  };
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, clang::format::FormatStyle::AlignConsecutiveStyle const&)::'lambda'()::operator()() const
Line
Count
Source
470
48.6k
  auto AlignCurrentSequence = [&] {
471
48.6k
    if (StartOfSequence > 0 && 
StartOfSequence < EndOfSequence23
)
472
16
      AlignTokenSequence(Style, StartOfSequence, EndOfSequence, MinColumn,
473
16
                         Matches, Changes);
474
48.6k
    MinColumn = 0;
475
48.6k
    MaxColumn = UINT_MAX;
476
48.6k
    StartOfSequence = 0;
477
48.6k
    EndOfSequence = 0;
478
48.6k
  };
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, clang::format::FormatStyle::AlignConsecutiveStyle const&)::'lambda'()::operator()() const
Line
Count
Source
470
74.1k
  auto AlignCurrentSequence = [&] {
471
74.1k
    if (StartOfSequence > 0 && 
StartOfSequence < EndOfSequence497
)
472
360
      AlignTokenSequence(Style, StartOfSequence, EndOfSequence, MinColumn,
473
360
                         Matches, Changes);
474
74.1k
    MinColumn = 0;
475
74.1k
    MaxColumn = UINT_MAX;
476
74.1k
    StartOfSequence = 0;
477
74.1k
    EndOfSequence = 0;
478
74.1k
  };
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, clang::format::FormatStyle::AlignConsecutiveStyle const&)::'lambda'()::operator()() const
Line
Count
Source
470
3.17k
  auto AlignCurrentSequence = [&] {
471
3.17k
    if (StartOfSequence > 0 && 
StartOfSequence < EndOfSequence11
)
472
11
      AlignTokenSequence(Style, StartOfSequence, EndOfSequence, MinColumn,
473
11
                         Matches, Changes);
474
3.17k
    MinColumn = 0;
475
3.17k
    MaxColumn = UINT_MAX;
476
3.17k
    StartOfSequence = 0;
477
3.17k
    EndOfSequence = 0;
478
3.17k
  };
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, clang::format::FormatStyle::AlignConsecutiveStyle const&)::'lambda'()::operator()() const
Line
Count
Source
470
4.90k
  auto AlignCurrentSequence = [&] {
471
4.90k
    if (StartOfSequence > 0 && 
StartOfSequence < EndOfSequence192
)
472
192
      AlignTokenSequence(Style, StartOfSequence, EndOfSequence, MinColumn,
473
192
                         Matches, Changes);
474
4.90k
    MinColumn = 0;
475
4.90k
    MaxColumn = UINT_MAX;
476
4.90k
    StartOfSequence = 0;
477
4.90k
    EndOfSequence = 0;
478
4.90k
  };
479
480
76.7k
  unsigned i = StartAt;
481
503k
  for (unsigned e = Changes.size(); i != e; 
++i426k
) {
482
481k
    if (Changes[i].indentAndNestingLevel() < IndentAndNestingLevel)
483
54.7k
      break;
484
485
426k
    if (Changes[i].NewlinesBefore != 0) {
486
62.1k
      CommasBeforeMatch = 0;
487
62.1k
      EndOfSequence = i;
488
489
      // Whether to break the alignment sequence because of an empty line.
490
62.1k
      bool EmptyLineBreak =
491
62.1k
          (Changes[i].NewlinesBefore > 1) &&
492
62.1k
          
(ACS != FormatStyle::ACS_AcrossEmptyLines)1.33k
&&
493
62.1k
          
(ACS != FormatStyle::ACS_AcrossEmptyLinesAndComments)1.33k
;
494
495
      // Whether to break the alignment sequence because of a line without a
496
      // match.
497
62.1k
      bool NoMatchBreak =
498
62.1k
          !FoundMatchOnLine &&
499
62.1k
          
!(60.1k
LineIsComment60.1k
&&
500
60.1k
            
(15.7k
(ACS == FormatStyle::ACS_AcrossComments)15.7k
||
501
15.7k
             
(ACS == FormatStyle::ACS_AcrossEmptyLinesAndComments)15.7k
));
502
503
62.1k
      if (EmptyLineBreak || 
NoMatchBreak60.8k
)
504
60.0k
        AlignCurrentSequence();
505
506
      // A new line starts, re-initialize line status tracking bools.
507
      // Keep the match state if a string literal is continued on this line.
508
62.1k
      if (i == 0 || 
!Changes[i].Tok->is(tok::string_literal)61.5k
||
509
62.1k
          
!Changes[i - 1].Tok->is(tok::string_literal)1.07k
)
510
61.7k
        FoundMatchOnLine = false;
511
62.1k
      LineIsComment = true;
512
62.1k
    }
513
514
426k
    if (!Changes[i].Tok->is(tok::comment)) {
515
418k
      LineIsComment = false;
516
418k
    }
517
518
426k
    if (Changes[i].Tok->is(tok::comma)) {
519
12.9k
      ++CommasBeforeMatch;
520
413k
    } else if (Changes[i].indentAndNestingLevel() > IndentAndNestingLevel) {
521
      // Call AlignTokens recursively, skipping over this scope block.
522
54.8k
      unsigned StoppedAt = AlignTokens(Style, Matches, Changes, i, ACS);
523
54.8k
      i = StoppedAt - 1;
524
54.8k
      continue;
525
54.8k
    }
526
527
371k
    if (!Matches(Changes[i]))
528
367k
      continue;
529
530
    // If there is more than one matching token per line, or if the number of
531
    // preceding commas, do not match anymore, end the sequence.
532
3.72k
    if (FoundMatchOnLine || 
CommasBeforeMatch != CommasBeforeLastMatch3.34k
)
533
433
      AlignCurrentSequence();
534
535
3.72k
    CommasBeforeLastMatch = CommasBeforeMatch;
536
3.72k
    FoundMatchOnLine = true;
537
538
3.72k
    if (StartOfSequence == 0)
539
2.29k
      StartOfSequence = i;
540
541
3.72k
    unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn;
542
3.72k
    int LineLengthAfter = Changes[i].TokenLength;
543
19.7k
    for (unsigned j = i + 1; j != e && 
Changes[j].NewlinesBefore == 018.5k
;
++j16.0k
) {
544
16.0k
      LineLengthAfter += Changes[j].Spaces;
545
      // Changes are generally 1:1 with the tokens, but a change could also be
546
      // inside of a token, in which case it's counted more than once: once for
547
      // the whitespace surrounding the token (!IsInsideToken) and once for
548
      // each whitespace change within it (IsInsideToken).
549
      // Therefore, changes inside of a token should only count the space.
550
16.0k
      if (!Changes[j].IsInsideToken)
551
16.0k
        LineLengthAfter += Changes[j].TokenLength;
552
16.0k
    }
553
3.72k
    unsigned ChangeMaxColumn = Style.ColumnLimit - LineLengthAfter;
554
555
    // If we are restricted by the maximum column width, end the sequence.
556
3.72k
    if (ChangeMinColumn > MaxColumn || 
ChangeMaxColumn < MinColumn3.71k
||
557
3.72k
        
CommasBeforeLastMatch != CommasBeforeMatch3.70k
) {
558
15
      AlignCurrentSequence();
559
15
      StartOfSequence = i;
560
15
    }
561
562
3.72k
    MinColumn = std::max(MinColumn, ChangeMinColumn);
563
3.72k
    MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
564
3.72k
  }
565
566
76.7k
  EndOfSequence = i;
567
76.7k
  AlignCurrentSequence();
568
76.7k
  return i;
569
76.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, clang::format::FormatStyle::AlignConsecutiveStyle const&)
Line
Count
Source
437
470
    const FormatStyle::AlignConsecutiveStyle &ACS = FormatStyle::ACS_None) {
438
470
  unsigned MinColumn = 0;
439
470
  unsigned MaxColumn = UINT_MAX;
440
441
  // Line number of the start and the end of the current token sequence.
442
470
  unsigned StartOfSequence = 0;
443
470
  unsigned EndOfSequence = 0;
444
445
  // Measure the scope level (i.e. depth of (), [], {}) of the first token, and
446
  // abort when we hit any token in a higher scope than the starting one.
447
470
  auto IndentAndNestingLevel = StartAt < Changes.size()
448
470
                                   ? Changes[StartAt].indentAndNestingLevel()
449
470
                                   : 
std::tuple<unsigned, unsigned, unsigned>()0
;
450
451
  // Keep track of the number of commas before the matching tokens, we will only
452
  // align a sequence of matching tokens if they are preceded by the same number
453
  // of commas.
454
470
  unsigned CommasBeforeLastMatch = 0;
455
470
  unsigned CommasBeforeMatch = 0;
456
457
  // Whether a matching token has been found on the current line.
458
470
  bool FoundMatchOnLine = false;
459
460
  // Whether the current line consists purely of comments.
461
470
  bool LineIsComment = true;
462
463
  // Aligns a sequence of matching tokens, on the MinColumn column.
464
  //
465
  // Sequences start from the first matching token to align, and end at the
466
  // first token of the first line that doesn't need to be aligned.
467
  //
468
  // We need to adjust the StartOfTokenColumn of each Change that is on a line
469
  // containing any matching token to be aligned and located after such token.
470
470
  auto AlignCurrentSequence = [&] {
471
470
    if (StartOfSequence > 0 && StartOfSequence < EndOfSequence)
472
470
      AlignTokenSequence(Style, StartOfSequence, EndOfSequence, MinColumn,
473
470
                         Matches, Changes);
474
470
    MinColumn = 0;
475
470
    MaxColumn = UINT_MAX;
476
470
    StartOfSequence = 0;
477
470
    EndOfSequence = 0;
478
470
  };
479
480
470
  unsigned i = StartAt;
481
6.49k
  for (unsigned e = Changes.size(); i != e; 
++i6.02k
) {
482
6.02k
    if (Changes[i].indentAndNestingLevel() < IndentAndNestingLevel)
483
0
      break;
484
485
6.02k
    if (Changes[i].NewlinesBefore != 0) {
486
1.09k
      CommasBeforeMatch = 0;
487
1.09k
      EndOfSequence = i;
488
489
      // Whether to break the alignment sequence because of an empty line.
490
1.09k
      bool EmptyLineBreak =
491
1.09k
          (Changes[i].NewlinesBefore > 1) &&
492
1.09k
          
(ACS != FormatStyle::ACS_AcrossEmptyLines)31
&&
493
1.09k
          
(ACS != FormatStyle::ACS_AcrossEmptyLinesAndComments)26
;
494
495
      // Whether to break the alignment sequence because of a line without a
496
      // match.
497
1.09k
      bool NoMatchBreak =
498
1.09k
          !FoundMatchOnLine &&
499
1.09k
          
!(673
LineIsComment673
&&
500
673
            
(52
(ACS == FormatStyle::ACS_AcrossComments)52
||
501
52
             
(ACS == FormatStyle::ACS_AcrossEmptyLinesAndComments)42
));
502
503
1.09k
      if (EmptyLineBreak || 
NoMatchBreak1.07k
)
504
658
        AlignCurrentSequence();
505
506
      // A new line starts, re-initialize line status tracking bools.
507
      // Keep the match state if a string literal is continued on this line.
508
1.09k
      if (i == 0 || !Changes[i].Tok->is(tok::string_literal) ||
509
1.09k
          
!Changes[i - 1].Tok->is(tok::string_literal)18
)
510
1.09k
        FoundMatchOnLine = false;
511
1.09k
      LineIsComment = true;
512
1.09k
    }
513
514
6.02k
    if (!Changes[i].Tok->is(tok::comment)) {
515
5.94k
      LineIsComment = false;
516
5.94k
    }
517
518
6.02k
    if (Changes[i].Tok->is(tok::comma)) {
519
30
      ++CommasBeforeMatch;
520
5.99k
    } else if (Changes[i].indentAndNestingLevel() > IndentAndNestingLevel) {
521
      // Call AlignTokens recursively, skipping over this scope block.
522
634
      unsigned StoppedAt = AlignTokens(Style, Matches, Changes, i, ACS);
523
634
      i = StoppedAt - 1;
524
634
      continue;
525
634
    }
526
527
5.39k
    if (!Matches(Changes[i]))
528
4.80k
      continue;
529
530
    // If there is more than one matching token per line, or if the number of
531
    // preceding commas, do not match anymore, end the sequence.
532
585
    if (FoundMatchOnLine || 
CommasBeforeMatch != CommasBeforeLastMatch564
)
533
54
      AlignCurrentSequence();
534
535
585
    CommasBeforeLastMatch = CommasBeforeMatch;
536
585
    FoundMatchOnLine = true;
537
538
585
    if (StartOfSequence == 0)
539
365
      StartOfSequence = i;
540
541
585
    unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn;
542
585
    int LineLengthAfter = Changes[i].TokenLength;
543
2.18k
    for (unsigned j = i + 1; j != e && 
Changes[j].NewlinesBefore == 02.03k
;
++j1.59k
) {
544
1.59k
      LineLengthAfter += Changes[j].Spaces;
545
      // Changes are generally 1:1 with the tokens, but a change could also be
546
      // inside of a token, in which case it's counted more than once: once for
547
      // the whitespace surrounding the token (!IsInsideToken) and once for
548
      // each whitespace change within it (IsInsideToken).
549
      // Therefore, changes inside of a token should only count the space.
550
1.59k
      if (!Changes[j].IsInsideToken)
551
1.59k
        LineLengthAfter += Changes[j].TokenLength;
552
1.59k
    }
553
585
    unsigned ChangeMaxColumn = Style.ColumnLimit - LineLengthAfter;
554
555
    // If we are restricted by the maximum column width, end the sequence.
556
585
    if (ChangeMinColumn > MaxColumn || 
ChangeMaxColumn < MinColumn582
||
557
585
        
CommasBeforeLastMatch != CommasBeforeMatch579
) {
558
6
      AlignCurrentSequence();
559
6
      StartOfSequence = i;
560
6
    }
561
562
585
    MinColumn = std::max(MinColumn, ChangeMinColumn);
563
585
    MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
564
585
  }
565
566
470
  EndOfSequence = i;
567
470
  AlignCurrentSequence();
568
470
  return i;
569
470
}
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, clang::format::FormatStyle::AlignConsecutiveStyle const&)
Line
Count
Source
437
1.31k
    const FormatStyle::AlignConsecutiveStyle &ACS = FormatStyle::ACS_None) {
438
1.31k
  unsigned MinColumn = 0;
439
1.31k
  unsigned MaxColumn = UINT_MAX;
440
441
  // Line number of the start and the end of the current token sequence.
442
1.31k
  unsigned StartOfSequence = 0;
443
1.31k
  unsigned EndOfSequence = 0;
444
445
  // Measure the scope level (i.e. depth of (), [], {}) of the first token, and
446
  // abort when we hit any token in a higher scope than the starting one.
447
1.31k
  auto IndentAndNestingLevel = StartAt < Changes.size()
448
1.31k
                                   ? Changes[StartAt].indentAndNestingLevel()
449
1.31k
                                   : 
std::tuple<unsigned, unsigned, unsigned>()0
;
450
451
  // Keep track of the number of commas before the matching tokens, we will only
452
  // align a sequence of matching tokens if they are preceded by the same number
453
  // of commas.
454
1.31k
  unsigned CommasBeforeLastMatch = 0;
455
1.31k
  unsigned CommasBeforeMatch = 0;
456
457
  // Whether a matching token has been found on the current line.
458
1.31k
  bool FoundMatchOnLine = false;
459
460
  // Whether the current line consists purely of comments.
461
1.31k
  bool LineIsComment = true;
462
463
  // Aligns a sequence of matching tokens, on the MinColumn column.
464
  //
465
  // Sequences start from the first matching token to align, and end at the
466
  // first token of the first line that doesn't need to be aligned.
467
  //
468
  // We need to adjust the StartOfTokenColumn of each Change that is on a line
469
  // containing any matching token to be aligned and located after such token.
470
1.31k
  auto AlignCurrentSequence = [&] {
471
1.31k
    if (StartOfSequence > 0 && StartOfSequence < EndOfSequence)
472
1.31k
      AlignTokenSequence(Style, StartOfSequence, EndOfSequence, MinColumn,
473
1.31k
                         Matches, Changes);
474
1.31k
    MinColumn = 0;
475
1.31k
    MaxColumn = UINT_MAX;
476
1.31k
    StartOfSequence = 0;
477
1.31k
    EndOfSequence = 0;
478
1.31k
  };
479
480
1.31k
  unsigned i = StartAt;
481
8.72k
  for (unsigned e = Changes.size(); i != e; 
++i7.41k
) {
482
8.72k
    if (Changes[i].indentAndNestingLevel() < IndentAndNestingLevel)
483
1.31k
      break;
484
485
7.41k
    if (Changes[i].NewlinesBefore != 0) {
486
949
      CommasBeforeMatch = 0;
487
949
      EndOfSequence = i;
488
489
      // Whether to break the alignment sequence because of an empty line.
490
949
      bool EmptyLineBreak =
491
949
          (Changes[i].NewlinesBefore > 1) &&
492
949
          
(ACS != FormatStyle::ACS_AcrossEmptyLines)6
&&
493
949
          
(ACS != FormatStyle::ACS_AcrossEmptyLinesAndComments)6
;
494
495
      // Whether to break the alignment sequence because of a line without a
496
      // match.
497
949
      bool NoMatchBreak =
498
949
          !FoundMatchOnLine &&
499
949
          
!(658
LineIsComment658
&&
500
658
            
(324
(ACS == FormatStyle::ACS_AcrossComments)324
||
501
324
             
(ACS == FormatStyle::ACS_AcrossEmptyLinesAndComments)317
));
502
503
949
      if (EmptyLineBreak || 
NoMatchBreak943
)
504
624
        AlignCurrentSequence();
505
506
      // A new line starts, re-initialize line status tracking bools.
507
      // Keep the match state if a string literal is continued on this line.
508
949
      if (i == 0 || !Changes[i].Tok->is(tok::string_literal) ||
509
949
          
!Changes[i - 1].Tok->is(tok::string_literal)81
)
510
902
        FoundMatchOnLine = false;
511
949
      LineIsComment = true;
512
949
    }
513
514
7.41k
    if (!Changes[i].Tok->is(tok::comment)) {
515
7.29k
      LineIsComment = false;
516
7.29k
    }
517
518
7.41k
    if (Changes[i].Tok->is(tok::comma)) {
519
780
      ++CommasBeforeMatch;
520
6.63k
    } else if (Changes[i].indentAndNestingLevel() > IndentAndNestingLevel) {
521
      // Call AlignTokens recursively, skipping over this scope block.
522
676
      unsigned StoppedAt = AlignTokens(Style, Matches, Changes, i, ACS);
523
676
      i = StoppedAt - 1;
524
676
      continue;
525
676
    }
526
527
6.73k
    if (!Matches(Changes[i]))
528
6.28k
      continue;
529
530
    // If there is more than one matching token per line, or if the number of
531
    // preceding commas, do not match anymore, end the sequence.
532
447
    if (FoundMatchOnLine || 
CommasBeforeMatch != CommasBeforeLastMatch423
)
533
42
      AlignCurrentSequence();
534
535
447
    CommasBeforeLastMatch = CommasBeforeMatch;
536
447
    FoundMatchOnLine = true;
537
538
447
    if (StartOfSequence == 0)
539
252
      StartOfSequence = i;
540
541
447
    unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn;
542
447
    int LineLengthAfter = Changes[i].TokenLength;
543
2.20k
    for (unsigned j = i + 1; j != e && 
Changes[j].NewlinesBefore == 02.19k
;
++j1.76k
) {
544
1.76k
      LineLengthAfter += Changes[j].Spaces;
545
      // Changes are generally 1:1 with the tokens, but a change could also be
546
      // inside of a token, in which case it's counted more than once: once for
547
      // the whitespace surrounding the token (!IsInsideToken) and once for
548
      // each whitespace change within it (IsInsideToken).
549
      // Therefore, changes inside of a token should only count the space.
550
1.76k
      if (!Changes[j].IsInsideToken)
551
1.76k
        LineLengthAfter += Changes[j].TokenLength;
552
1.76k
    }
553
447
    unsigned ChangeMaxColumn = Style.ColumnLimit - LineLengthAfter;
554
555
    // If we are restricted by the maximum column width, end the sequence.
556
447
    if (ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn ||
557
447
        CommasBeforeLastMatch != CommasBeforeMatch) {
558
0
      AlignCurrentSequence();
559
0
      StartOfSequence = i;
560
0
    }
561
562
447
    MinColumn = std::max(MinColumn, ChangeMinColumn);
563
447
    MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
564
447
  }
565
566
1.31k
  EndOfSequence = i;
567
1.31k
  AlignCurrentSequence();
568
1.31k
  return i;
569
1.31k
}
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, clang::format::FormatStyle::AlignConsecutiveStyle const&)
Line
Count
Source
437
36
    const FormatStyle::AlignConsecutiveStyle &ACS = FormatStyle::ACS_None) {
438
36
  unsigned MinColumn = 0;
439
36
  unsigned MaxColumn = UINT_MAX;
440
441
  // Line number of the start and the end of the current token sequence.
442
36
  unsigned StartOfSequence = 0;
443
36
  unsigned EndOfSequence = 0;
444
445
  // Measure the scope level (i.e. depth of (), [], {}) of the first token, and
446
  // abort when we hit any token in a higher scope than the starting one.
447
36
  auto IndentAndNestingLevel = StartAt < Changes.size()
448
36
                                   ? Changes[StartAt].indentAndNestingLevel()
449
36
                                   : 
std::tuple<unsigned, unsigned, unsigned>()0
;
450
451
  // Keep track of the number of commas before the matching tokens, we will only
452
  // align a sequence of matching tokens if they are preceded by the same number
453
  // of commas.
454
36
  unsigned CommasBeforeLastMatch = 0;
455
36
  unsigned CommasBeforeMatch = 0;
456
457
  // Whether a matching token has been found on the current line.
458
36
  bool FoundMatchOnLine = false;
459
460
  // Whether the current line consists purely of comments.
461
36
  bool LineIsComment = true;
462
463
  // Aligns a sequence of matching tokens, on the MinColumn column.
464
  //
465
  // Sequences start from the first matching token to align, and end at the
466
  // first token of the first line that doesn't need to be aligned.
467
  //
468
  // We need to adjust the StartOfTokenColumn of each Change that is on a line
469
  // containing any matching token to be aligned and located after such token.
470
36
  auto AlignCurrentSequence = [&] {
471
36
    if (StartOfSequence > 0 && StartOfSequence < EndOfSequence)
472
36
      AlignTokenSequence(Style, StartOfSequence, EndOfSequence, MinColumn,
473
36
                         Matches, Changes);
474
36
    MinColumn = 0;
475
36
    MaxColumn = UINT_MAX;
476
36
    StartOfSequence = 0;
477
36
    EndOfSequence = 0;
478
36
  };
479
480
36
  unsigned i = StartAt;
481
538
  for (unsigned e = Changes.size(); i != e; 
++i502
) {
482
502
    if (Changes[i].indentAndNestingLevel() < IndentAndNestingLevel)
483
0
      break;
484
485
502
    if (Changes[i].NewlinesBefore != 0) {
486
43
      CommasBeforeMatch = 0;
487
43
      EndOfSequence = i;
488
489
      // Whether to break the alignment sequence because of an empty line.
490
43
      bool EmptyLineBreak =
491
43
          (Changes[i].NewlinesBefore > 1) &&
492
43
          
(ACS != FormatStyle::ACS_AcrossEmptyLines)5
&&
493
43
          
(ACS != FormatStyle::ACS_AcrossEmptyLinesAndComments)5
;
494
495
      // Whether to break the alignment sequence because of a line without a
496
      // match.
497
43
      bool NoMatchBreak =
498
43
          !FoundMatchOnLine &&
499
43
          
!(4
LineIsComment4
&&
500
4
            ((ACS == FormatStyle::ACS_AcrossComments) ||
501
4
             (ACS == FormatStyle::ACS_AcrossEmptyLinesAndComments)));
502
503
43
      if (EmptyLineBreak || NoMatchBreak)
504
0
        AlignCurrentSequence();
505
506
      // A new line starts, re-initialize line status tracking bools.
507
      // Keep the match state if a string literal is continued on this line.
508
43
      if (i == 0 || !Changes[i].Tok->is(tok::string_literal) ||
509
43
          
!Changes[i - 1].Tok->is(tok::string_literal)0
)
510
43
        FoundMatchOnLine = false;
511
43
      LineIsComment = true;
512
43
    }
513
514
502
    if (!Changes[i].Tok->is(tok::comment)) {
515
492
      LineIsComment = false;
516
492
    }
517
518
502
    if (Changes[i].Tok->is(tok::comma)) {
519
0
      ++CommasBeforeMatch;
520
502
    } else if (Changes[i].indentAndNestingLevel() > IndentAndNestingLevel) {
521
      // Call AlignTokens recursively, skipping over this scope block.
522
3
      unsigned StoppedAt = AlignTokens(Style, Matches, Changes, i, ACS);
523
3
      i = StoppedAt - 1;
524
3
      continue;
525
3
    }
526
527
499
    if (!Matches(Changes[i]))
528
424
      continue;
529
530
    // If there is more than one matching token per line, or if the number of
531
    // preceding commas, do not match anymore, end the sequence.
532
75
    if (FoundMatchOnLine || CommasBeforeMatch != CommasBeforeLastMatch)
533
0
      AlignCurrentSequence();
534
535
75
    CommasBeforeLastMatch = CommasBeforeMatch;
536
75
    FoundMatchOnLine = true;
537
538
75
    if (StartOfSequence == 0)
539
36
      StartOfSequence = i;
540
541
75
    unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn;
542
75
    int LineLengthAfter = Changes[i].TokenLength;
543
321
    for (unsigned j = i + 1; j != e && 
Changes[j].NewlinesBefore == 0285
;
++j246
) {
544
246
      LineLengthAfter += Changes[j].Spaces;
545
      // Changes are generally 1:1 with the tokens, but a change could also be
546
      // inside of a token, in which case it's counted more than once: once for
547
      // the whitespace surrounding the token (!IsInsideToken) and once for
548
      // each whitespace change within it (IsInsideToken).
549
      // Therefore, changes inside of a token should only count the space.
550
246
      if (!Changes[j].IsInsideToken)
551
246
        LineLengthAfter += Changes[j].TokenLength;
552
246
    }
553
75
    unsigned ChangeMaxColumn = Style.ColumnLimit - LineLengthAfter;
554
555
    // If we are restricted by the maximum column width, end the sequence.
556
75
    if (ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn ||
557
75
        CommasBeforeLastMatch != CommasBeforeMatch) {
558
0
      AlignCurrentSequence();
559
0
      StartOfSequence = i;
560
0
    }
561
562
75
    MinColumn = std::max(MinColumn, ChangeMinColumn);
563
75
    MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
564
75
  }
565
566
36
  EndOfSequence = i;
567
36
  AlignCurrentSequence();
568
36
  return i;
569
36
}
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, clang::format::FormatStyle::AlignConsecutiveStyle const&)
Line
Count
Source
437
3
    const FormatStyle::AlignConsecutiveStyle &ACS = FormatStyle::ACS_None) {
438
3
  unsigned MinColumn = 0;
439
3
  unsigned MaxColumn = UINT_MAX;
440
441
  // Line number of the start and the end of the current token sequence.
442
3
  unsigned StartOfSequence = 0;
443
3
  unsigned EndOfSequence = 0;
444
445
  // Measure the scope level (i.e. depth of (), [], {}) of the first token, and
446
  // abort when we hit any token in a higher scope than the starting one.
447
3
  auto IndentAndNestingLevel = StartAt < Changes.size()
448
3
                                   ? Changes[StartAt].indentAndNestingLevel()
449
3
                                   : 
std::tuple<unsigned, unsigned, unsigned>()0
;
450
451
  // Keep track of the number of commas before the matching tokens, we will only
452
  // align a sequence of matching tokens if they are preceded by the same number
453
  // of commas.
454
3
  unsigned CommasBeforeLastMatch = 0;
455
3
  unsigned CommasBeforeMatch = 0;
456
457
  // Whether a matching token has been found on the current line.
458
3
  bool FoundMatchOnLine = false;
459
460
  // Whether the current line consists purely of comments.
461
3
  bool LineIsComment = true;
462
463
  // Aligns a sequence of matching tokens, on the MinColumn column.
464
  //
465
  // Sequences start from the first matching token to align, and end at the
466
  // first token of the first line that doesn't need to be aligned.
467
  //
468
  // We need to adjust the StartOfTokenColumn of each Change that is on a line
469
  // containing any matching token to be aligned and located after such token.
470
3
  auto AlignCurrentSequence = [&] {
471
3
    if (StartOfSequence > 0 && StartOfSequence < EndOfSequence)
472
3
      AlignTokenSequence(Style, StartOfSequence, EndOfSequence, MinColumn,
473
3
                         Matches, Changes);
474
3
    MinColumn = 0;
475
3
    MaxColumn = UINT_MAX;
476
3
    StartOfSequence = 0;
477
3
    EndOfSequence = 0;
478
3
  };
479
480
3
  unsigned i = StartAt;
481
9
  for (unsigned e = Changes.size(); i != e; 
++i6
) {
482
9
    if (Changes[i].indentAndNestingLevel() < IndentAndNestingLevel)
483
3
      break;
484
485
6
    if (Changes[i].NewlinesBefore != 0) {
486
0
      CommasBeforeMatch = 0;
487
0
      EndOfSequence = i;
488
489
      // Whether to break the alignment sequence because of an empty line.
490
0
      bool EmptyLineBreak =
491
0
          (Changes[i].NewlinesBefore > 1) &&
492
0
          (ACS != FormatStyle::ACS_AcrossEmptyLines) &&
493
0
          (ACS != FormatStyle::ACS_AcrossEmptyLinesAndComments);
494
495
      // Whether to break the alignment sequence because of a line without a
496
      // match.
497
0
      bool NoMatchBreak =
498
0
          !FoundMatchOnLine &&
499
0
          !(LineIsComment &&
500
0
            ((ACS == FormatStyle::ACS_AcrossComments) ||
501
0
             (ACS == FormatStyle::ACS_AcrossEmptyLinesAndComments)));
502
503
0
      if (EmptyLineBreak || NoMatchBreak)
504
0
        AlignCurrentSequence();
505
506
      // A new line starts, re-initialize line status tracking bools.
507
      // Keep the match state if a string literal is continued on this line.
508
0
      if (i == 0 || !Changes[i].Tok->is(tok::string_literal) ||
509
0
          !Changes[i - 1].Tok->is(tok::string_literal))
510
0
        FoundMatchOnLine = false;
511
0
      LineIsComment = true;
512
0
    }
513
514
6
    if (!Changes[i].Tok->is(tok::comment)) {
515
6
      LineIsComment = false;
516
6
    }
517
518
6
    if (Changes[i].Tok->is(tok::comma)) {
519
0
      ++CommasBeforeMatch;
520
6
    } else if (Changes[i].indentAndNestingLevel() > IndentAndNestingLevel) {
521
      // Call AlignTokens recursively, skipping over this scope block.
522
0
      unsigned StoppedAt = AlignTokens(Style, Matches, Changes, i, ACS);
523
0
      i = StoppedAt - 1;
524
0
      continue;
525
0
    }
526
527
6
    if (!Matches(Changes[i]))
528
6
      continue;
529
530
    // If there is more than one matching token per line, or if the number of
531
    // preceding commas, do not match anymore, end the sequence.
532
0
    if (FoundMatchOnLine || CommasBeforeMatch != CommasBeforeLastMatch)
533
0
      AlignCurrentSequence();
534
535
0
    CommasBeforeLastMatch = CommasBeforeMatch;
536
0
    FoundMatchOnLine = true;
537
538
0
    if (StartOfSequence == 0)
539
0
      StartOfSequence = i;
540
541
0
    unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn;
542
0
    int LineLengthAfter = Changes[i].TokenLength;
543
0
    for (unsigned j = i + 1; j != e && Changes[j].NewlinesBefore == 0; ++j) {
544
0
      LineLengthAfter += Changes[j].Spaces;
545
      // Changes are generally 1:1 with the tokens, but a change could also be
546
      // inside of a token, in which case it's counted more than once: once for
547
      // the whitespace surrounding the token (!IsInsideToken) and once for
548
      // each whitespace change within it (IsInsideToken).
549
      // Therefore, changes inside of a token should only count the space.
550
0
      if (!Changes[j].IsInsideToken)
551
0
        LineLengthAfter += Changes[j].TokenLength;
552
0
    }
553
0
    unsigned ChangeMaxColumn = Style.ColumnLimit - LineLengthAfter;
554
555
    // If we are restricted by the maximum column width, end the sequence.
556
0
    if (ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn ||
557
0
        CommasBeforeLastMatch != CommasBeforeMatch) {
558
0
      AlignCurrentSequence();
559
0
      StartOfSequence = i;
560
0
    }
561
562
0
    MinColumn = std::max(MinColumn, ChangeMinColumn);
563
0
    MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
564
0
  }
565
566
3
  EndOfSequence = i;
567
3
  AlignCurrentSequence();
568
3
  return i;
569
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, clang::format::FormatStyle::AlignConsecutiveStyle const&)
Line
Count
Source
437
602
    const FormatStyle::AlignConsecutiveStyle &ACS = FormatStyle::ACS_None) {
438
602
  unsigned MinColumn = 0;
439
602
  unsigned MaxColumn = UINT_MAX;
440
441
  // Line number of the start and the end of the current token sequence.
442
602
  unsigned StartOfSequence = 0;
443
602
  unsigned EndOfSequence = 0;
444
445
  // Measure the scope level (i.e. depth of (), [], {}) of the first token, and
446
  // abort when we hit any token in a higher scope than the starting one.
447
602
  auto IndentAndNestingLevel = StartAt < Changes.size()
448
602
                                   ? Changes[StartAt].indentAndNestingLevel()
449
602
                                   : 
std::tuple<unsigned, unsigned, unsigned>()0
;
450
451
  // Keep track of the number of commas before the matching tokens, we will only
452
  // align a sequence of matching tokens if they are preceded by the same number
453
  // of commas.
454
602
  unsigned CommasBeforeLastMatch = 0;
455
602
  unsigned CommasBeforeMatch = 0;
456
457
  // Whether a matching token has been found on the current line.
458
602
  bool FoundMatchOnLine = false;
459
460
  // Whether the current line consists purely of comments.
461
602
  bool LineIsComment = true;
462
463
  // Aligns a sequence of matching tokens, on the MinColumn column.
464
  //
465
  // Sequences start from the first matching token to align, and end at the
466
  // first token of the first line that doesn't need to be aligned.
467
  //
468
  // We need to adjust the StartOfTokenColumn of each Change that is on a line
469
  // containing any matching token to be aligned and located after such token.
470
602
  auto AlignCurrentSequence = [&] {
471
602
    if (StartOfSequence > 0 && StartOfSequence < EndOfSequence)
472
602
      AlignTokenSequence(Style, StartOfSequence, EndOfSequence, MinColumn,
473
602
                         Matches, Changes);
474
602
    MinColumn = 0;
475
602
    MaxColumn = UINT_MAX;
476
602
    StartOfSequence = 0;
477
602
    EndOfSequence = 0;
478
602
  };
479
480
602
  unsigned i = StartAt;
481
7.35k
  for (unsigned e = Changes.size(); i != e; 
++i6.75k
) {
482
6.75k
    if (Changes[i].indentAndNestingLevel() < IndentAndNestingLevel)
483
0
      break;
484
485
6.75k
    if (Changes[i].NewlinesBefore != 0) {
486
972
      CommasBeforeMatch = 0;
487
972
      EndOfSequence = i;
488
489
      // Whether to break the alignment sequence because of an empty line.
490
972
      bool EmptyLineBreak =
491
972
          (Changes[i].NewlinesBefore > 1) &&
492
972
          
(ACS != FormatStyle::ACS_AcrossEmptyLines)17
&&
493
972
          
(ACS != FormatStyle::ACS_AcrossEmptyLinesAndComments)17
;
494
495
      // Whether to break the alignment sequence because of a line without a
496
      // match.
497
972
      bool NoMatchBreak =
498
972
          !FoundMatchOnLine &&
499
972
          
!(429
LineIsComment429
&&
500
429
            
(30
(ACS == FormatStyle::ACS_AcrossComments)30
||
501
30
             (ACS == FormatStyle::ACS_AcrossEmptyLinesAndComments)));
502
503
972
      if (EmptyLineBreak || 
NoMatchBreak959
)
504
428
        AlignCurrentSequence();
505
506
      // A new line starts, re-initialize line status tracking bools.
507
      // Keep the match state if a string literal is continued on this line.
508
972
      if (i == 0 || !Changes[i].Tok->is(tok::string_literal) ||
509
972
          
!Changes[i - 1].Tok->is(tok::string_literal)18
)
510
972
        FoundMatchOnLine = false;
511
972
      LineIsComment = true;
512
972
    }
513
514
6.75k
    if (!Changes[i].Tok->is(tok::comment)) {
515
6.69k
      LineIsComment = false;
516
6.69k
    }
517
518
6.75k
    if (Changes[i].Tok->is(tok::comma)) {
519
18
      ++CommasBeforeMatch;
520
6.73k
    } else if (Changes[i].indentAndNestingLevel() > IndentAndNestingLevel) {
521
      // Call AlignTokens recursively, skipping over this scope block.
522
753
      unsigned StoppedAt = AlignTokens(Style, Matches, Changes, i, ACS);
523
753
      i = StoppedAt - 1;
524
753
      continue;
525
753
    }
526
527
6.00k
    if (!Matches(Changes[i]))
528
5.23k
      continue;
529
530
    // If there is more than one matching token per line, or if the number of
531
    // preceding commas, do not match anymore, end the sequence.
532
764
    if (FoundMatchOnLine || CommasBeforeMatch != CommasBeforeLastMatch)
533
0
      AlignCurrentSequence();
534
535
764
    CommasBeforeLastMatch = CommasBeforeMatch;
536
764
    FoundMatchOnLine = true;
537
538
764
    if (StartOfSequence == 0)
539
402
      StartOfSequence = i;
540
541
764
    unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn;
542
764
    int LineLengthAfter = Changes[i].TokenLength;
543
3.86k
    for (unsigned j = i + 1; j != e && 
Changes[j].NewlinesBefore == 03.64k
;
++j3.10k
) {
544
3.10k
      LineLengthAfter += Changes[j].Spaces;
545
      // Changes are generally 1:1 with the tokens, but a change could also be
546
      // inside of a token, in which case it's counted more than once: once for
547
      // the whitespace surrounding the token (!IsInsideToken) and once for
548
      // each whitespace change within it (IsInsideToken).
549
      // Therefore, changes inside of a token should only count the space.
550
3.10k
      if (!Changes[j].IsInsideToken)
551
3.10k
        LineLengthAfter += Changes[j].TokenLength;
552
3.10k
    }
553
764
    unsigned ChangeMaxColumn = Style.ColumnLimit - LineLengthAfter;
554
555
    // If we are restricted by the maximum column width, end the sequence.
556
764
    if (ChangeMinColumn > MaxColumn || 
ChangeMaxColumn < MinColumn761
||
557
764
        
CommasBeforeLastMatch != CommasBeforeMatch758
) {
558
6
      AlignCurrentSequence();
559
6
      StartOfSequence = i;
560
6
    }
561
562
764
    MinColumn = std::max(MinColumn, ChangeMinColumn);
563
764
    MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
564
764
  }
565
566
602
  EndOfSequence = i;
567
602
  AlignCurrentSequence();
568
602
  return i;
569
602
}
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, clang::format::FormatStyle::AlignConsecutiveStyle const&)
Line
Count
Source
437
1.44k
    const FormatStyle::AlignConsecutiveStyle &ACS = FormatStyle::ACS_None) {
438
1.44k
  unsigned MinColumn = 0;
439
1.44k
  unsigned MaxColumn = UINT_MAX;
440
441
  // Line number of the start and the end of the current token sequence.
442
1.44k
  unsigned StartOfSequence = 0;
443
1.44k
  unsigned EndOfSequence = 0;
444
445
  // Measure the scope level (i.e. depth of (), [], {}) of the first token, and
446
  // abort when we hit any token in a higher scope than the starting one.
447
1.44k
  auto IndentAndNestingLevel = StartAt < Changes.size()
448
1.44k
                                   ? Changes[StartAt].indentAndNestingLevel()
449
1.44k
                                   : 
std::tuple<unsigned, unsigned, unsigned>()0
;
450
451
  // Keep track of the number of commas before the matching tokens, we will only
452
  // align a sequence of matching tokens if they are preceded by the same number
453
  // of commas.
454
1.44k
  unsigned CommasBeforeLastMatch = 0;
455
1.44k
  unsigned CommasBeforeMatch = 0;
456
457
  // Whether a matching token has been found on the current line.
458
1.44k
  bool FoundMatchOnLine = false;
459
460
  // Whether the current line consists purely of comments.
461
1.44k
  bool LineIsComment = true;
462
463
  // Aligns a sequence of matching tokens, on the MinColumn column.
464
  //
465
  // Sequences start from the first matching token to align, and end at the
466
  // first token of the first line that doesn't need to be aligned.
467
  //
468
  // We need to adjust the StartOfTokenColumn of each Change that is on a line
469
  // containing any matching token to be aligned and located after such token.
470
1.44k
  auto AlignCurrentSequence = [&] {
471
1.44k
    if (StartOfSequence > 0 && StartOfSequence < EndOfSequence)
472
1.44k
      AlignTokenSequence(Style, StartOfSequence, EndOfSequence, MinColumn,
473
1.44k
                         Matches, Changes);
474
1.44k
    MinColumn = 0;
475
1.44k
    MaxColumn = UINT_MAX;
476
1.44k
    StartOfSequence = 0;
477
1.44k
    EndOfSequence = 0;
478
1.44k
  };
479
480
1.44k
  unsigned i = StartAt;
481
10.5k
  for (unsigned e = Changes.size(); i != e; 
++i9.06k
) {
482
10.5k
    if (Changes[i].indentAndNestingLevel() < IndentAndNestingLevel)
483
1.44k
      break;
484
485
9.06k
    if (Changes[i].NewlinesBefore != 0) {
486
829
      CommasBeforeMatch = 0;
487
829
      EndOfSequence = i;
488
489
      // Whether to break the alignment sequence because of an empty line.
490
829
      bool EmptyLineBreak =
491
829
          (Changes[i].NewlinesBefore > 1) &&
492
829
          
(ACS != FormatStyle::ACS_AcrossEmptyLines)6
&&
493
829
          
(ACS != FormatStyle::ACS_AcrossEmptyLinesAndComments)6
;
494
495
      // Whether to break the alignment sequence because of a line without a
496
      // match.
497
829
      bool NoMatchBreak =
498
829
          !FoundMatchOnLine &&
499
829
          
!(572
LineIsComment572
&&
500
572
            
(279
(ACS == FormatStyle::ACS_AcrossComments)279
||
501
279
             
(ACS == FormatStyle::ACS_AcrossEmptyLinesAndComments)272
));
502
503
829
      if (EmptyLineBreak || 
NoMatchBreak823
)
504
571
        AlignCurrentSequence();
505
506
      // A new line starts, re-initialize line status tracking bools.
507
      // Keep the match state if a string literal is continued on this line.
508
829
      if (i == 0 || !Changes[i].Tok->is(tok::string_literal) ||
509
829
          
!Changes[i - 1].Tok->is(tok::string_literal)81
)
510
782
        FoundMatchOnLine = false;
511
829
      LineIsComment = true;
512
829
    }
513
514
9.06k
    if (!Changes[i].Tok->is(tok::comment)) {
515
8.96k
      LineIsComment = false;
516
8.96k
    }
517
518
9.06k
    if (Changes[i].Tok->is(tok::comma)) {
519
1.11k
      ++CommasBeforeMatch;
520
7.95k
    } else if (Changes[i].indentAndNestingLevel() > IndentAndNestingLevel) {
521
      // Call AlignTokens recursively, skipping over this scope block.
522
688
      unsigned StoppedAt = AlignTokens(Style, Matches, Changes, i, ACS);
523
688
      i = StoppedAt - 1;
524
688
      continue;
525
688
    }
526
527
8.37k
    if (!Matches(Changes[i]))
528
7.68k
      continue;
529
530
    // If there is more than one matching token per line, or if the number of
531
    // preceding commas, do not match anymore, end the sequence.
532
693
    if (FoundMatchOnLine || 
CommasBeforeMatch != CommasBeforeLastMatch507
)
533
189
      AlignCurrentSequence();
534
535
693
    CommasBeforeLastMatch = CommasBeforeMatch;
536
693
    FoundMatchOnLine = true;
537
538
693
    if (StartOfSequence == 0)
539
516
      StartOfSequence = i;
540
541
693
    unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn;
542
693
    int LineLengthAfter = Changes[i].TokenLength;
543
6.11k
    for (unsigned j = i + 1; j != e && 
Changes[j].NewlinesBefore == 05.76k
;
++j5.41k
) {
544
5.41k
      LineLengthAfter += Changes[j].Spaces;
545
      // Changes are generally 1:1 with the tokens, but a change could also be
546
      // inside of a token, in which case it's counted more than once: once for
547
      // the whitespace surrounding the token (!IsInsideToken) and once for
548
      // each whitespace change within it (IsInsideToken).
549
      // Therefore, changes inside of a token should only count the space.
550
5.41k
      if (!Changes[j].IsInsideToken)
551
5.41k
        LineLengthAfter += Changes[j].TokenLength;
552
5.41k
    }
553
693
    unsigned ChangeMaxColumn = Style.ColumnLimit - LineLengthAfter;
554
555
    // If we are restricted by the maximum column width, end the sequence.
556
693
    if (ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn ||
557
693
        CommasBeforeLastMatch != CommasBeforeMatch) {
558
0
      AlignCurrentSequence();
559
0
      StartOfSequence = i;
560
0
    }
561
562
693
    MinColumn = std::max(MinColumn, ChangeMinColumn);
563
693
    MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
564
693
  }
565
566
1.44k
  EndOfSequence = i;
567
1.44k
  AlignCurrentSequence();
568
1.44k
  return i;
569
1.44k
}
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, clang::format::FormatStyle::AlignConsecutiveStyle const&)
Line
Count
Source
437
19.2k
    const FormatStyle::AlignConsecutiveStyle &ACS = FormatStyle::ACS_None) {
438
19.2k
  unsigned MinColumn = 0;
439
19.2k
  unsigned MaxColumn = UINT_MAX;
440
441
  // Line number of the start and the end of the current token sequence.
442
19.2k
  unsigned StartOfSequence = 0;
443
19.2k
  unsigned EndOfSequence = 0;
444
445
  // Measure the scope level (i.e. depth of (), [], {}) of the first token, and
446
  // abort when we hit any token in a higher scope than the starting one.
447
19.2k
  auto IndentAndNestingLevel = StartAt < Changes.size()
448
19.2k
                                   ? Changes[StartAt].indentAndNestingLevel()
449
19.2k
                                   : 
std::tuple<unsigned, unsigned, unsigned>()0
;
450
451
  // Keep track of the number of commas before the matching tokens, we will only
452
  // align a sequence of matching tokens if they are preceded by the same number
453
  // of commas.
454
19.2k
  unsigned CommasBeforeLastMatch = 0;
455
19.2k
  unsigned CommasBeforeMatch = 0;
456
457
  // Whether a matching token has been found on the current line.
458
19.2k
  bool FoundMatchOnLine = false;
459
460
  // Whether the current line consists purely of comments.
461
19.2k
  bool LineIsComment = true;
462
463
  // Aligns a sequence of matching tokens, on the MinColumn column.
464
  //
465
  // Sequences start from the first matching token to align, and end at the
466
  // first token of the first line that doesn't need to be aligned.
467
  //
468
  // We need to adjust the StartOfTokenColumn of each Change that is on a line
469
  // containing any matching token to be aligned and located after such token.
470
19.2k
  auto AlignCurrentSequence = [&] {
471
19.2k
    if (StartOfSequence > 0 && StartOfSequence < EndOfSequence)
472
19.2k
      AlignTokenSequence(Style, StartOfSequence, EndOfSequence, MinColumn,
473
19.2k
                         Matches, Changes);
474
19.2k
    MinColumn = 0;
475
19.2k
    MaxColumn = UINT_MAX;
476
19.2k
    StartOfSequence = 0;
477
19.2k
    EndOfSequence = 0;
478
19.2k
  };
479
480
19.2k
  unsigned i = StartAt;
481
205k
  for (unsigned e = Changes.size(); i != e; 
++i186k
) {
482
186k
    if (Changes[i].indentAndNestingLevel() < IndentAndNestingLevel)
483
24
      break;
484
485
186k
    if (Changes[i].NewlinesBefore != 0) {
486
29.3k
      CommasBeforeMatch = 0;
487
29.3k
      EndOfSequence = i;
488
489
      // Whether to break the alignment sequence because of an empty line.
490
29.3k
      bool EmptyLineBreak =
491
29.3k
          (Changes[i].NewlinesBefore > 1) &&
492
29.3k
          
(ACS != FormatStyle::ACS_AcrossEmptyLines)973
&&
493
29.3k
          
(ACS != FormatStyle::ACS_AcrossEmptyLinesAndComments)973
;
494
495
      // Whether to break the alignment sequence because of a line without a
496
      // match.
497
29.3k
      bool NoMatchBreak =
498
29.3k
          !FoundMatchOnLine &&
499
29.3k
          
!(29.3k
LineIsComment29.3k
&&
500
29.3k
            
(2.74k
(ACS == FormatStyle::ACS_AcrossComments)2.74k
||
501
2.74k
             (ACS == FormatStyle::ACS_AcrossEmptyLinesAndComments)));
502
503
29.3k
      if (EmptyLineBreak || 
NoMatchBreak28.3k
)
504
29.3k
        AlignCurrentSequence();
505
506
      // A new line starts, re-initialize line status tracking bools.
507
      // Keep the match state if a string literal is continued on this line.
508
29.3k
      if (i == 0 || 
!Changes[i].Tok->is(tok::string_literal)28.7k
||
509
29.3k
          
!Changes[i - 1].Tok->is(tok::string_literal)229
)
510
29.2k
        FoundMatchOnLine = false;
511
29.3k
      LineIsComment = true;
512
29.3k
    }
513
514
186k
    if (!Changes[i].Tok->is(tok::comment)) {
515
181k
      LineIsComment = false;
516
181k
    }
517
518
186k
    if (Changes[i].Tok->is(tok::comma)) {
519
958
      ++CommasBeforeMatch;
520
185k
    } else if (Changes[i].indentAndNestingLevel() > IndentAndNestingLevel) {
521
      // Call AlignTokens recursively, skipping over this scope block.
522
29.4k
      unsigned StoppedAt = AlignTokens(Style, Matches, Changes, i, ACS);
523
29.4k
      i = StoppedAt - 1;
524
29.4k
      continue;
525
29.4k
    }
526
527
157k
    if (!Matches(Changes[i]))
528
157k
      continue;
529
530
    // If there is more than one matching token per line, or if the number of
531
    // preceding commas, do not match anymore, end the sequence.
532
24
    if (FoundMatchOnLine || 
CommasBeforeMatch != CommasBeforeLastMatch17
)
533
7
      AlignCurrentSequence();
534
535
24
    CommasBeforeLastMatch = CommasBeforeMatch;
536
24
    FoundMatchOnLine = true;
537
538
24
    if (StartOfSequence == 0)
539
24
      StartOfSequence = i;
540
541
24
    unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn;
542
24
    int LineLengthAfter = Changes[i].TokenLength;
543
158
    for (unsigned j = i + 1; j != e && 
Changes[j].NewlinesBefore == 0137
;
++j134
) {
544
134
      LineLengthAfter += Changes[j].Spaces;
545
      // Changes are generally 1:1 with the tokens, but a change could also be
546
      // inside of a token, in which case it's counted more than once: once for
547
      // the whitespace surrounding the token (!IsInsideToken) and once for
548
      // each whitespace change within it (IsInsideToken).
549
      // Therefore, changes inside of a token should only count the space.
550
134
      if (!Changes[j].IsInsideToken)
551
134
        LineLengthAfter += Changes[j].TokenLength;
552
134
    }
553
24
    unsigned ChangeMaxColumn = Style.ColumnLimit - LineLengthAfter;
554
555
    // If we are restricted by the maximum column width, end the sequence.
556
24
    if (ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn ||
557
24
        CommasBeforeLastMatch != CommasBeforeMatch) {
558
0
      AlignCurrentSequence();
559
0
      StartOfSequence = i;
560
0
    }
561
562
24
    MinColumn = std::max(MinColumn, ChangeMinColumn);
563
24
    MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
564
24
  }
565
566
19.2k
  EndOfSequence = i;
567
19.2k
  AlignCurrentSequence();
568
19.2k
  return i;
569
19.2k
}
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, clang::format::FormatStyle::AlignConsecutiveStyle const&)
Line
Count
Source
437
49.0k
    const FormatStyle::AlignConsecutiveStyle &ACS = FormatStyle::ACS_None) {
438
49.0k
  unsigned MinColumn = 0;
439
49.0k
  unsigned MaxColumn = UINT_MAX;
440
441
  // Line number of the start and the end of the current token sequence.
442
49.0k
  unsigned StartOfSequence = 0;
443
49.0k
  unsigned EndOfSequence = 0;
444
445
  // Measure the scope level (i.e. depth of (), [], {}) of the first token, and
446
  // abort when we hit any token in a higher scope than the starting one.
447
49.0k
  auto IndentAndNestingLevel = StartAt < Changes.size()
448
49.0k
                                   ? Changes[StartAt].indentAndNestingLevel()
449
49.0k
                                   : 
std::tuple<unsigned, unsigned, unsigned>()0
;
450
451
  // Keep track of the number of commas before the matching tokens, we will only
452
  // align a sequence of matching tokens if they are preceded by the same number
453
  // of commas.
454
49.0k
  unsigned CommasBeforeLastMatch = 0;
455
49.0k
  unsigned CommasBeforeMatch = 0;
456
457
  // Whether a matching token has been found on the current line.
458
49.0k
  bool FoundMatchOnLine = false;
459
460
  // Whether the current line consists purely of comments.
461
49.0k
  bool LineIsComment = true;
462
463
  // Aligns a sequence of matching tokens, on the MinColumn column.
464
  //
465
  // Sequences start from the first matching token to align, and end at the
466
  // first token of the first line that doesn't need to be aligned.
467
  //
468
  // We need to adjust the StartOfTokenColumn of each Change that is on a line
469
  // containing any matching token to be aligned and located after such token.
470
49.0k
  auto AlignCurrentSequence = [&] {
471
49.0k
    if (StartOfSequence > 0 && StartOfSequence < EndOfSequence)
472
49.0k
      AlignTokenSequence(Style, StartOfSequence, EndOfSequence, MinColumn,
473
49.0k
                         Matches, Changes);
474
49.0k
    MinColumn = 0;
475
49.0k
    MaxColumn = UINT_MAX;
476
49.0k
    StartOfSequence = 0;
477
49.0k
    EndOfSequence = 0;
478
49.0k
  };
479
480
49.0k
  unsigned i = StartAt;
481
235k
  for (unsigned e = Changes.size(); i != e; 
++i186k
) {
482
235k
    if (Changes[i].indentAndNestingLevel() < IndentAndNestingLevel)
483
48.9k
      break;
484
485
186k
    if (Changes[i].NewlinesBefore != 0) {
486
25.2k
      CommasBeforeMatch = 0;
487
25.2k
      EndOfSequence = i;
488
489
      // Whether to break the alignment sequence because of an empty line.
490
25.2k
      bool EmptyLineBreak =
491
25.2k
          (Changes[i].NewlinesBefore > 1) &&
492
25.2k
          
(ACS != FormatStyle::ACS_AcrossEmptyLines)263
&&
493
25.2k
          
(ACS != FormatStyle::ACS_AcrossEmptyLinesAndComments)263
;
494
495
      // Whether to break the alignment sequence because of a line without a
496
      // match.
497
25.2k
      bool NoMatchBreak =
498
25.2k
          !FoundMatchOnLine &&
499
25.2k
          
!(24.9k
LineIsComment24.9k
&&
500
24.9k
            
(11.1k
(ACS == FormatStyle::ACS_AcrossComments)11.1k
||
501
11.1k
             (ACS == FormatStyle::ACS_AcrossEmptyLinesAndComments)));
502
503
25.2k
      if (EmptyLineBreak || 
NoMatchBreak24.9k
)
504
24.9k
        AlignCurrentSequence();
505
506
      // A new line starts, re-initialize line status tracking bools.
507
      // Keep the match state if a string literal is continued on this line.
508
25.2k
      if (i == 0 || !Changes[i].Tok->is(tok::string_literal) ||
509
25.2k
          
!Changes[i - 1].Tok->is(tok::string_literal)583
)
510
25.0k
        FoundMatchOnLine = false;
511
25.2k
      LineIsComment = true;
512
25.2k
    }
513
514
186k
    if (!Changes[i].Tok->is(tok::comment)) {
515
184k
      LineIsComment = false;
516
184k
    }
517
518
186k
    if (Changes[i].Tok->is(tok::comma)) {
519
9.46k
      ++CommasBeforeMatch;
520
177k
    } else if (Changes[i].indentAndNestingLevel() > IndentAndNestingLevel) {
521
      // Call AlignTokens recursively, skipping over this scope block.
522
19.5k
      unsigned StoppedAt = AlignTokens(Style, Matches, Changes, i, ACS);
523
19.5k
      i = StoppedAt - 1;
524
19.5k
      continue;
525
19.5k
    }
526
527
167k
    if (!Matches(Changes[i]))
528
166k
      continue;
529
530
    // If there is more than one matching token per line, or if the number of
531
    // preceding commas, do not match anymore, end the sequence.
532
731
    if (FoundMatchOnLine || 
CommasBeforeMatch != CommasBeforeLastMatch594
)
533
141
      AlignCurrentSequence();
534
535
731
    CommasBeforeLastMatch = CommasBeforeMatch;
536
731
    FoundMatchOnLine = true;
537
538
731
    if (StartOfSequence == 0)
539
497
      StartOfSequence = i;
540
541
731
    unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn;
542
731
    int LineLengthAfter = Changes[i].TokenLength;
543
3.38k
    for (unsigned j = i + 1; j != e && 
Changes[j].NewlinesBefore == 03.06k
;
++j2.65k
) {
544
2.65k
      LineLengthAfter += Changes[j].Spaces;
545
      // Changes are generally 1:1 with the tokens, but a change could also be
546
      // inside of a token, in which case it's counted more than once: once for
547
      // the whitespace surrounding the token (!IsInsideToken) and once for
548
      // each whitespace change within it (IsInsideToken).
549
      // Therefore, changes inside of a token should only count the space.
550
2.65k
      if (!Changes[j].IsInsideToken)
551
2.65k
        LineLengthAfter += Changes[j].TokenLength;
552
2.65k
    }
553
731
    unsigned ChangeMaxColumn = Style.ColumnLimit - LineLengthAfter;
554
555
    // If we are restricted by the maximum column width, end the sequence.
556
731
    if (ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn ||
557
731
        CommasBeforeLastMatch != CommasBeforeMatch) {
558
0
      AlignCurrentSequence();
559
0
      StartOfSequence = i;
560
0
    }
561
562
731
    MinColumn = std::max(MinColumn, ChangeMinColumn);
563
731
    MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
564
731
  }
565
566
49.0k
  EndOfSequence = i;
567
49.0k
  AlignCurrentSequence();
568
49.0k
  return i;
569
49.0k
}
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, clang::format::FormatStyle::AlignConsecutiveStyle const&)
Line
Count
Source
437
1.60k
    const FormatStyle::AlignConsecutiveStyle &ACS = FormatStyle::ACS_None) {
438
1.60k
  unsigned MinColumn = 0;
439
1.60k
  unsigned MaxColumn = UINT_MAX;
440
441
  // Line number of the start and the end of the current token sequence.
442
1.60k
  unsigned StartOfSequence = 0;
443
1.60k
  unsigned EndOfSequence = 0;
444
445
  // Measure the scope level (i.e. depth of (), [], {}) of the first token, and
446
  // abort when we hit any token in a higher scope than the starting one.
447
1.60k
  auto IndentAndNestingLevel = StartAt < Changes.size()
448
1.60k
                                   ? Changes[StartAt].indentAndNestingLevel()
449
1.60k
                                   : 
std::tuple<unsigned, unsigned, unsigned>()0
;
450
451
  // Keep track of the number of commas before the matching tokens, we will only
452
  // align a sequence of matching tokens if they are preceded by the same number
453
  // of commas.
454
1.60k
  unsigned CommasBeforeLastMatch = 0;
455
1.60k
  unsigned CommasBeforeMatch = 0;
456
457
  // Whether a matching token has been found on the current line.
458
1.60k
  bool FoundMatchOnLine = false;
459
460
  // Whether the current line consists purely of comments.
461
1.60k
  bool LineIsComment = true;
462
463
  // Aligns a sequence of matching tokens, on the MinColumn column.
464
  //
465
  // Sequences start from the first matching token to align, and end at the
466
  // first token of the first line that doesn't need to be aligned.
467
  //
468
  // We need to adjust the StartOfTokenColumn of each Change that is on a line
469
  // containing any matching token to be aligned and located after such token.
470
1.60k
  auto AlignCurrentSequence = [&] {
471
1.60k
    if (StartOfSequence > 0 && StartOfSequence < EndOfSequence)
472
1.60k
      AlignTokenSequence(Style, StartOfSequence, EndOfSequence, MinColumn,
473
1.60k
                         Matches, Changes);
474
1.60k
    MinColumn = 0;
475
1.60k
    MaxColumn = UINT_MAX;
476
1.60k
    StartOfSequence = 0;
477
1.60k
    EndOfSequence = 0;
478
1.60k
  };
479
480
1.60k
  unsigned i = StartAt;
481
13.8k
  for (unsigned e = Changes.size(); i != e; 
++i12.2k
) {
482
12.2k
    if (Changes[i].indentAndNestingLevel() < IndentAndNestingLevel)
483
11
      break;
484
485
12.2k
    if (Changes[i].NewlinesBefore != 0) {
486
1.57k
      CommasBeforeMatch = 0;
487
1.57k
      EndOfSequence = i;
488
489
      // Whether to break the alignment sequence because of an empty line.
490
1.57k
      bool EmptyLineBreak =
491
1.57k
          (Changes[i].NewlinesBefore > 1) &&
492
1.57k
          
(ACS != FormatStyle::ACS_AcrossEmptyLines)31
&&
493
1.57k
          
(ACS != FormatStyle::ACS_AcrossEmptyLinesAndComments)31
;
494
495
      // Whether to break the alignment sequence because of a line without a
496
      // match.
497
1.57k
      bool NoMatchBreak =
498
1.57k
          !FoundMatchOnLine &&
499
1.57k
          
!(1.57k
LineIsComment1.57k
&&
500
1.57k
            
(176
(ACS == FormatStyle::ACS_AcrossComments)176
||
501
176
             (ACS == FormatStyle::ACS_AcrossEmptyLinesAndComments)));
502
503
1.57k
      if (EmptyLineBreak || 
NoMatchBreak1.54k
)
504
1.57k
        AlignCurrentSequence();
505
506
      // A new line starts, re-initialize line status tracking bools.
507
      // Keep the match state if a string literal is continued on this line.
508
1.57k
      if (i == 0 || !Changes[i].Tok->is(tok::string_literal) ||
509
1.57k
          
!Changes[i - 1].Tok->is(tok::string_literal)38
)
510
1.57k
        FoundMatchOnLine = false;
511
1.57k
      LineIsComment = true;
512
1.57k
    }
513
514
12.2k
    if (!Changes[i].Tok->is(tok::comment)) {
515
11.9k
      LineIsComment = false;
516
11.9k
    }
517
518
12.2k
    if (Changes[i].Tok->is(tok::comma)) {
519
8
      ++CommasBeforeMatch;
520
12.1k
    } else if (Changes[i].indentAndNestingLevel() > IndentAndNestingLevel) {
521
      // Call AlignTokens recursively, skipping over this scope block.
522
1.74k
      unsigned StoppedAt = AlignTokens(Style, Matches, Changes, i, ACS);
523
1.74k
      i = StoppedAt - 1;
524
1.74k
      continue;
525
1.74k
    }
526
527
10.4k
    if (!Matches(Changes[i]))
528
10.4k
      continue;
529
530
    // If there is more than one matching token per line, or if the number of
531
    // preceding commas, do not match anymore, end the sequence.
532
11
    if (FoundMatchOnLine || CommasBeforeMatch != CommasBeforeLastMatch)
533
0
      AlignCurrentSequence();
534
535
11
    CommasBeforeLastMatch = CommasBeforeMatch;
536
11
    FoundMatchOnLine = true;
537
538
11
    if (StartOfSequence == 0)
539
11
      StartOfSequence = i;
540
541
11
    unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn;
542
11
    int LineLengthAfter = Changes[i].TokenLength;
543
36
    for (unsigned j = i + 1; j != e && 
Changes[j].NewlinesBefore == 028
;
++j25
) {
544
25
      LineLengthAfter += Changes[j].Spaces;
545
      // Changes are generally 1:1 with the tokens, but a change could also be
546
      // inside of a token, in which case it's counted more than once: once for
547
      // the whitespace surrounding the token (!IsInsideToken) and once for
548
      // each whitespace change within it (IsInsideToken).
549
      // Therefore, changes inside of a token should only count the space.
550
25
      if (!Changes[j].IsInsideToken)
551
25
        LineLengthAfter += Changes[j].TokenLength;
552
25
    }
553
11
    unsigned ChangeMaxColumn = Style.ColumnLimit - LineLengthAfter;
554
555
    // If we are restricted by the maximum column width, end the sequence.
556
11
    if (ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn ||
557
11
        CommasBeforeLastMatch != CommasBeforeMatch) {
558
0
      AlignCurrentSequence();
559
0
      StartOfSequence = i;
560
0
    }
561
562
11
    MinColumn = std::max(MinColumn, ChangeMinColumn);
563
11
    MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
564
11
  }
565
566
1.60k
  EndOfSequence = i;
567
1.60k
  AlignCurrentSequence();
568
1.60k
  return i;
569
1.60k
}
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, clang::format::FormatStyle::AlignConsecutiveStyle const&)
Line
Count
Source
437
3.01k
    const FormatStyle::AlignConsecutiveStyle &ACS = FormatStyle::ACS_None) {
438
3.01k
  unsigned MinColumn = 0;
439
3.01k
  unsigned MaxColumn = UINT_MAX;
440
441
  // Line number of the start and the end of the current token sequence.
442
3.01k
  unsigned StartOfSequence = 0;
443
3.01k
  unsigned EndOfSequence = 0;
444
445
  // Measure the scope level (i.e. depth of (), [], {}) of the first token, and
446
  // abort when we hit any token in a higher scope than the starting one.
447
3.01k
  auto IndentAndNestingLevel = StartAt < Changes.size()
448
3.01k
                                   ? Changes[StartAt].indentAndNestingLevel()
449
3.01k
                                   : 
std::tuple<unsigned, unsigned, unsigned>()0
;
450
451
  // Keep track of the number of commas before the matching tokens, we will only
452
  // align a sequence of matching tokens if they are preceded by the same number
453
  // of commas.
454
3.01k
  unsigned CommasBeforeLastMatch = 0;
455
3.01k
  unsigned CommasBeforeMatch = 0;
456
457
  // Whether a matching token has been found on the current line.
458
3.01k
  bool FoundMatchOnLine = false;
459
460
  // Whether the current line consists purely of comments.
461
3.01k
  bool LineIsComment = true;
462
463
  // Aligns a sequence of matching tokens, on the MinColumn column.
464
  //
465
  // Sequences start from the first matching token to align, and end at the
466
  // first token of the first line that doesn't need to be aligned.
467
  //
468
  // We need to adjust the StartOfTokenColumn of each Change that is on a line
469
  // containing any matching token to be aligned and located after such token.
470
3.01k
  auto AlignCurrentSequence = [&] {
471
3.01k
    if (StartOfSequence > 0 && StartOfSequence < EndOfSequence)
472
3.01k
      AlignTokenSequence(Style, StartOfSequence, EndOfSequence, MinColumn,
473
3.01k
                         Matches, Changes);
474
3.01k
    MinColumn = 0;
475
3.01k
    MaxColumn = UINT_MAX;
476
3.01k
    StartOfSequence = 0;
477
3.01k
    EndOfSequence = 0;
478
3.01k
  };
479
480
3.01k
  unsigned i = StartAt;
481
13.8k
  for (unsigned e = Changes.size(); i != e; 
++i10.8k
) {
482
13.8k
    if (Changes[i].indentAndNestingLevel() < IndentAndNestingLevel)
483
3.00k
      break;
484
485
10.8k
    if (Changes[i].NewlinesBefore != 0) {
486
2.10k
      CommasBeforeMatch = 0;
487
2.10k
      EndOfSequence = i;
488
489
      // Whether to break the alignment sequence because of an empty line.
490
2.10k
      bool EmptyLineBreak =
491
2.10k
          (Changes[i].NewlinesBefore > 1) &&
492
2.10k
          
(ACS != FormatStyle::ACS_AcrossEmptyLines)3
&&
493
2.10k
          
(ACS != FormatStyle::ACS_AcrossEmptyLinesAndComments)3
;
494
495
      // Whether to break the alignment sequence because of a line without a
496
      // match.
497
2.10k
      bool NoMatchBreak =
498
2.10k
          !FoundMatchOnLine &&
499
2.10k
          
!(1.88k
LineIsComment1.88k
&&
500
1.88k
            
(1.03k
(ACS == FormatStyle::ACS_AcrossComments)1.03k
||
501
1.03k
             (ACS == FormatStyle::ACS_AcrossEmptyLinesAndComments)));
502
503
2.10k
      if (EmptyLineBreak || 
NoMatchBreak2.09k
)
504
1.88k
        AlignCurrentSequence();
505
506
      // A new line starts, re-initialize line status tracking bools.
507
      // Keep the match state if a string literal is continued on this line.
508
2.10k
      if (i == 0 || !Changes[i].Tok->is(tok::string_literal) ||
509
2.10k
          
!Changes[i - 1].Tok->is(tok::string_literal)28
)
510
2.09k
        FoundMatchOnLine = false;
511
2.10k
      LineIsComment = true;
512
2.10k
    }
513
514
10.8k
    if (!Changes[i].Tok->is(tok::comment)) {
515
10.7k
      LineIsComment = false;
516
10.7k
    }
517
518
10.8k
    if (Changes[i].Tok->is(tok::comma)) {
519
613
      ++CommasBeforeMatch;
520
10.2k
    } else if (Changes[i].indentAndNestingLevel() > IndentAndNestingLevel) {
521
      // Call AlignTokens recursively, skipping over this scope block.
522
1.27k
      unsigned StoppedAt = AlignTokens(Style, Matches, Changes, i, ACS);
523
1.27k
      i = StoppedAt - 1;
524
1.27k
      continue;
525
1.27k
    }
526
527
9.58k
    if (!Matches(Changes[i]))
528
9.19k
      continue;
529
530
    // If there is more than one matching token per line, or if the number of
531
    // preceding commas, do not match anymore, end the sequence.
532
392
    if (FoundMatchOnLine || CommasBeforeMatch != CommasBeforeLastMatch)
533
0
      AlignCurrentSequence();
534
535
392
    CommasBeforeLastMatch = CommasBeforeMatch;
536
392
    FoundMatchOnLine = true;
537
538
392
    if (StartOfSequence == 0)
539
189
      StartOfSequence = i;
540
541
392
    unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn;
542
392
    int LineLengthAfter = Changes[i].TokenLength;
543
1.46k
    for (unsigned j = i + 1; j != e && 
Changes[j].NewlinesBefore == 01.35k
;
++j1.06k
) {
544
1.06k
      LineLengthAfter += Changes[j].Spaces;
545
      // Changes are generally 1:1 with the tokens, but a change could also be
546
      // inside of a token, in which case it's counted more than once: once for
547
      // the whitespace surrounding the token (!IsInsideToken) and once for
548
      // each whitespace change within it (IsInsideToken).
549
      // Therefore, changes inside of a token should only count the space.
550
1.06k
      if (!Changes[j].IsInsideToken)
551
1.06k
        LineLengthAfter += Changes[j].TokenLength;
552
1.06k
    }
553
392
    unsigned ChangeMaxColumn = Style.ColumnLimit - LineLengthAfter;
554
555
    // If we are restricted by the maximum column width, end the sequence.
556
392
    if (ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn ||
557
392
        
CommasBeforeLastMatch != CommasBeforeMatch389
) {
558
3
      AlignCurrentSequence();
559
3
      StartOfSequence = i;
560
3
    }
561
562
392
    MinColumn = std::max(MinColumn, ChangeMinColumn);
563
392
    MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
564
392
  }
565
566
3.01k
  EndOfSequence = i;
567
3.01k
  AlignCurrentSequence();
568
3.01k
  return i;
569
3.01k
}
570
571
// Aligns a sequence of matching tokens, on the MinColumn column.
572
//
573
// Sequences start from the first matching token to align, and end at the
574
// first token of the first line that doesn't need to be aligned.
575
//
576
// We need to adjust the StartOfTokenColumn of each Change that is on a line
577
// containing any matching token to be aligned and located after such token.
578
static void AlignMacroSequence(
579
    unsigned &StartOfSequence, unsigned &EndOfSequence, unsigned &MinColumn,
580
    unsigned &MaxColumn, bool &FoundMatchOnLine,
581
    std::function<bool(const WhitespaceManager::Change &C)> AlignMacrosMatches,
582
1.67k
    SmallVector<WhitespaceManager::Change, 16> &Changes) {
583
1.67k
  if (StartOfSequence > 0 && 
StartOfSequence < EndOfSequence30
) {
584
585
30
    FoundMatchOnLine = false;
586
30
    int Shift = 0;
587
588
636
    for (unsigned I = StartOfSequence; I != EndOfSequence; 
++I606
) {
589
606
      if (Changes[I].NewlinesBefore > 0) {
590
75
        Shift = 0;
591
75
        FoundMatchOnLine = false;
592
75
      }
593
594
      // If this is the first matching token to be aligned, remember by how many
595
      // spaces it has to be shifted, so the rest of the changes on the line are
596
      // shifted by the same amount
597
606
      if (!FoundMatchOnLine && 
AlignMacrosMatches(Changes[I])402
) {
598
90
        FoundMatchOnLine = true;
599
90
        Shift = MinColumn - Changes[I].StartOfTokenColumn;
600
90
        Changes[I].Spaces += Shift;
601
90
      }
602
603
606
      assert(Shift >= 0);
604
0
      Changes[I].StartOfTokenColumn += Shift;
605
606
      if (I + 1 != Changes.size())
606
581
        Changes[I + 1].PreviousEndOfTokenColumn += Shift;
607
606
    }
608
30
  }
609
610
1.67k
  MinColumn = 0;
611
1.67k
  MaxColumn = UINT_MAX;
612
1.67k
  StartOfSequence = 0;
613
1.67k
  EndOfSequence = 0;
614
1.67k
}
615
616
20.8k
void WhitespaceManager::alignConsecutiveMacros() {
617
20.8k
  if (Style.AlignConsecutiveMacros == FormatStyle::ACS_None)
618
20.4k
    return;
619
620
9.66k
  
auto AlignMacrosMatches = [](const Change &C) 388
{
621
9.66k
    const FormatToken *Current = C.Tok;
622
9.66k
    unsigned SpacesRequiredBefore = 1;
623
624
9.66k
    if (Current->SpacesRequiredBefore == 0 || 
!Current->Previous6.78k
)
625
4.83k
      return false;
626
627
4.83k
    Current = Current->Previous;
628
629
    // If token is a ")", skip over the parameter list, to the
630
    // token that precedes the "("
631
4.83k
    if (Current->is(tok::r_paren) && 
Current->MatchingParen150
) {
632
150
      Current = Current->MatchingParen->Previous;
633
150
      SpacesRequiredBefore = 0;
634
150
    }
635
636
4.83k
    if (!Current || !Current->is(tok::identifier))
637
3.17k
      return false;
638
639
1.66k
    if (!Current->Previous || 
!Current->Previous->is(tok::pp_define)1.48k
)
640
1.48k
      return false;
641
642
    // For a macro function, 0 spaces are required between the
643
    // identifier and the lparen that opens the parameter list.
644
    // For a simple macro, 1 space is required between the
645
    // identifier and the first token of the defined value.
646
180
    return Current->Next->SpacesRequiredBefore == SpacesRequiredBefore;
647
1.66k
  };
648
649
388
  unsigned MinColumn = 0;
650
388
  unsigned MaxColumn = UINT_MAX;
651
652
  // Start and end of the token sequence we're processing.
653
388
  unsigned StartOfSequence = 0;
654
388
  unsigned EndOfSequence = 0;
655
656
  // Whether a matching token has been found on the current line.
657
388
  bool FoundMatchOnLine = false;
658
659
  // Whether the current line consists only of comments
660
388
  bool LineIsComment = true;
661
662
388
  unsigned I = 0;
663
9.65k
  for (unsigned E = Changes.size(); I != E; 
++I9.26k
) {
664
9.26k
    if (Changes[I].NewlinesBefore != 0) {
665
1.36k
      EndOfSequence = I;
666
667
      // Whether to break the alignment sequence because of an empty line.
668
1.36k
      bool EmptyLineBreak =
669
1.36k
          (Changes[I].NewlinesBefore > 1) &&
670
1.36k
          
(Style.AlignConsecutiveMacros != FormatStyle::ACS_AcrossEmptyLines)40
&&
671
1.36k
          (Style.AlignConsecutiveMacros !=
672
38
           FormatStyle::ACS_AcrossEmptyLinesAndComments);
673
674
      // Whether to break the alignment sequence because of a line without a
675
      // match.
676
1.36k
      bool NoMatchBreak =
677
1.36k
          !FoundMatchOnLine &&
678
1.36k
          
!(1.29k
LineIsComment1.29k
&&
(39
(Style.AlignConsecutiveMacros ==
679
39
                               FormatStyle::ACS_AcrossComments) ||
680
39
                              (Style.AlignConsecutiveMacros ==
681
33
                               FormatStyle::ACS_AcrossEmptyLinesAndComments)));
682
683
1.36k
      if (EmptyLineBreak || 
NoMatchBreak1.33k
)
684
1.28k
        AlignMacroSequence(StartOfSequence, EndOfSequence, MinColumn, MaxColumn,
685
1.28k
                           FoundMatchOnLine, AlignMacrosMatches, Changes);
686
687
      // A new line starts, re-initialize line status tracking bools.
688
1.36k
      FoundMatchOnLine = false;
689
1.36k
      LineIsComment = true;
690
1.36k
    }
691
692
9.26k
    if (!Changes[I].Tok->is(tok::comment)) {
693
9.15k
      LineIsComment = false;
694
9.15k
    }
695
696
9.26k
    if (!AlignMacrosMatches(Changes[I]))
697
9.17k
      continue;
698
699
90
    FoundMatchOnLine = true;
700
701
90
    if (StartOfSequence == 0)
702
30
      StartOfSequence = I;
703
704
90
    unsigned ChangeMinColumn = Changes[I].StartOfTokenColumn;
705
90
    int LineLengthAfter = -Changes[I].Spaces;
706
384
    for (unsigned j = I; j != E && 
Changes[j].NewlinesBefore == 0359
;
++j294
)
707
294
      LineLengthAfter += Changes[j].Spaces + Changes[j].TokenLength;
708
90
    unsigned ChangeMaxColumn = Style.ColumnLimit - LineLengthAfter;
709
710
90
    MinColumn = std::max(MinColumn, ChangeMinColumn);
711
90
    MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
712
90
  }
713
714
388
  EndOfSequence = I;
715
388
  AlignMacroSequence(StartOfSequence, EndOfSequence, MinColumn, MaxColumn,
716
388
                     FoundMatchOnLine, AlignMacrosMatches, Changes);
717
388
}
718
719
20.8k
void WhitespaceManager::alignConsecutiveAssignments() {
720
20.8k
  if (Style.AlignConsecutiveAssignments == FormatStyle::ACS_None)
721
20.3k
    return;
722
723
470
  AlignTokens(
724
470
      Style,
725
14.3k
      [&](const Change &C) {
726
        // Do not align on equal signs that are first on a line.
727
14.3k
        if (C.NewlinesBefore > 0)
728
2.28k
          return false;
729
730
        // Do not align on equal signs that are last on a line.
731
12.0k
        if (&C != &Changes.back() && 
(&C + 1)->NewlinesBefore > 011.6k
)
732
1.64k
          return false;
733
734
10.3k
        return C.Tok->is(tok::equal);
735
12.0k
      },
736
470
      Changes, /*StartAt=*/0, Style.AlignConsecutiveAssignments);
737
470
}
738
739
20.8k
void WhitespaceManager::alignConsecutiveBitFields() {
740
20.8k
  if (Style.AlignConsecutiveBitFields == FormatStyle::ACS_None)
741
20.8k
    return;
742
743
36
  AlignTokens(
744
36
      Style,
745
662
      [&](Change const &C) {
746
        // Do not align on ':' that is first on a line.
747
662
        if (C.NewlinesBefore > 0)
748
86
          return false;
749
750
        // Do not align on ':' that is last on a line.
751
576
        if (&C != &Changes.back() && 
(&C + 1)->NewlinesBefore > 0540
)
752
39
          return false;
753
754
537
        return C.Tok->is(TT_BitFieldColon);
755
576
      },
756
36
      Changes, /*StartAt=*/0, Style.AlignConsecutiveBitFields);
757
36
}
758
759
20.8k
void WhitespaceManager::alignConsecutiveDeclarations() {
760
20.8k
  if (Style.AlignConsecutiveDeclarations == FormatStyle::ACS_None)
761
20.2k
    return;
762
763
602
  AlignTokens(
764
602
      Style,
765
16.9k
      [](Change const &C) {
766
        // tok::kw_operator is necessary for aligning operator overload
767
        // definitions.
768
16.9k
        if (C.Tok->isOneOf(TT_FunctionDeclarationName, tok::kw_operator))
769
316
          return true;
770
16.5k
        if (C.Tok->isNot(TT_StartOfName))
771
14.1k
          return false;
772
2.45k
        if (C.Tok->Previous &&
773
2.45k
            C.Tok->Previous->is(TT_StatementAttributeLikeMacro))
774
5
          return false;
775
        // Check if there is a subsequent name that starts the same declaration.
776
2.45k
        
for (FormatToken *Next = C.Tok->Next; 2.45k
Next;
Next = Next->Next2
) {
777
2.45k
          if (Next->is(tok::comment))
778
2
            continue;
779
2.45k
          if (Next->is(TT_PointerOrReference))
780
36
            return false;
781
2.41k
          if (!Next->Tok.getIdentifierInfo())
782
2.41k
            break;
783
2
          if (Next->isOneOf(TT_StartOfName, TT_FunctionDeclarationName,
784
2
                            tok::kw_operator))
785
2
            return false;
786
2
        }
787
2.41k
        return true;
788
2.45k
      },
789
602
      Changes, /*StartAt=*/0, Style.AlignConsecutiveDeclarations);
790
602
}
791
792
20.8k
void WhitespaceManager::alignChainedConditionals() {
793
20.8k
  if (Style.BreakBeforeTernaryOperators) {
794
19.2k
    AlignTokens(
795
19.2k
        Style,
796
325k
        [](Change const &C) {
797
          // Align question operators and last colon
798
325k
          return C.Tok->is(TT_ConditionalExpr) &&
799
325k
                 
(1.63k
(1.63k
C.Tok->is(tok::question)1.63k
&&
!C.NewlinesBefore729
) ||
800
1.63k
                  
(988
C.Tok->is(tok::colon)988
&&
C.Tok->Next906
&&
801
988
                   
(906
C.Tok->Next->FakeLParens.size() == 0906
||
802
906
                    
C.Tok->Next->FakeLParens.back() != prec::Conditional254
)));
803
325k
        },
804
19.2k
        Changes, /*StartAt=*/0);
805
19.2k
  } else {
806
40.1k
    static auto AlignWrappedOperand = [](Change const &C) {
807
40.1k
      FormatToken *Previous = C.Tok->getPreviousNonComment();
808
40.1k
      return C.NewlinesBefore && 
Previous5.63k
&&
Previous->is(TT_ConditionalExpr)2.95k
&&
809
40.1k
             
(889
Previous->is(tok::colon)889
&&
810
889
              
(771
C.Tok->FakeLParens.size() == 0771
||
811
771
               
C.Tok->FakeLParens.back() != prec::Conditional279
));
812
40.1k
    };
813
    // Ensure we keep alignment of wrapped operands with non-wrapped operands
814
    // Since we actually align the operators, the wrapped operands need the
815
    // extra offset to be properly aligned.
816
20.0k
    for (Change &C : Changes) {
817
20.0k
      if (AlignWrappedOperand(C))
818
173
        C.StartOfTokenColumn -= 2;
819
20.0k
    }
820
1.60k
    AlignTokens(
821
1.60k
        Style,
822
20.5k
        [this](Change const &C) {
823
          // Align question operators if next operand is not wrapped, as
824
          // well as wrapped operands after question operator or last
825
          // colon in conditional sequence
826
20.5k
          return (C.Tok->is(TT_ConditionalExpr) && 
C.Tok->is(tok::question)804
&&
827
20.5k
                  
&C != &Changes.back()517
&&
(&C + 1)->NewlinesBefore == 0517
&&
828
20.5k
                  
!(&C + 1)->IsTrailingComment472
) ||
829
20.5k
                 
AlignWrappedOperand(C)20.0k
;
830
20.5k
        },
831
1.60k
        Changes, /*StartAt=*/0);
832
1.60k
  }
833
20.8k
}
834
835
20.8k
void WhitespaceManager::alignTrailingComments() {
836
20.8k
  unsigned MinColumn = 0;
837
20.8k
  unsigned MaxColumn = UINT_MAX;
838
20.8k
  unsigned StartOfSequence = 0;
839
20.8k
  bool BreakBeforeNext = false;
840
20.8k
  unsigned Newlines = 0;
841
365k
  for (unsigned i = 0, e = Changes.size(); i != e; 
++i344k
) {
842
344k
    if (Changes[i].StartOfBlockComment)
843
666
      continue;
844
343k
    Newlines += Changes[i].NewlinesBefore;
845
343k
    if (!Changes[i].IsTrailingComment)
846
339k
      continue;
847
848
4.75k
    unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn;
849
4.75k
    unsigned ChangeMaxColumn;
850
851
4.75k
    if (Style.ColumnLimit == 0)
852
286
      ChangeMaxColumn = UINT_MAX;
853
4.46k
    else if (Style.ColumnLimit >= Changes[i].TokenLength)
854
4.41k
      ChangeMaxColumn = Style.ColumnLimit - Changes[i].TokenLength;
855
59
    else
856
59
      ChangeMaxColumn = ChangeMinColumn;
857
858
    // If we don't create a replacement for this change, we have to consider
859
    // it to be immovable.
860
4.75k
    if (!Changes[i].CreateReplacement)
861
469
      ChangeMaxColumn = ChangeMinColumn;
862
863
4.75k
    if (i + 1 != e && 
Changes[i + 1].ContinuesPPDirective4.74k
)
864
68
      ChangeMaxColumn -= 2;
865
    // If this comment follows an } in column 0, it probably documents the
866
    // closing of a namespace and we don't want to align it.
867
4.75k
    bool FollowsRBraceInColumn0 = i > 0 && 
Changes[i].NewlinesBefore == 04.27k
&&
868
4.75k
                                  
Changes[i - 1].Tok->is(tok::r_brace)2.22k
&&
869
4.75k
                                  
Changes[i - 1].StartOfTokenColumn == 0998
;
870
4.75k
    bool WasAlignedWithStartOfNextLine = false;
871
4.75k
    if (Changes[i].NewlinesBefore == 1) { // A comment on its own line.
872
1.85k
      unsigned CommentColumn = SourceMgr.getSpellingColumnNumber(
873
1.85k
          Changes[i].OriginalWhitespaceRange.getEnd());
874
6.84k
      for (unsigned j = i + 1; j != e; 
++j4.99k
) {
875
6.84k
        if (Changes[j].Tok->is(tok::comment))
876
4.99k
          continue;
877
878
1.84k
        unsigned NextColumn = SourceMgr.getSpellingColumnNumber(
879
1.84k
            Changes[j].OriginalWhitespaceRange.getEnd());
880
        // The start of the next token was previously aligned with the
881
        // start of this comment.
882
1.84k
        WasAlignedWithStartOfNextLine =
883
1.84k
            CommentColumn == NextColumn ||
884
1.84k
            
CommentColumn == NextColumn + Style.IndentWidth758
;
885
1.84k
        break;
886
6.84k
      }
887
1.85k
    }
888
4.75k
    if (!Style.AlignTrailingComments || 
FollowsRBraceInColumn04.66k
) {
889
532
      alignTrailingComments(StartOfSequence, i, MinColumn);
890
532
      MinColumn = ChangeMinColumn;
891
532
      MaxColumn = ChangeMinColumn;
892
532
      StartOfSequence = i;
893
4.22k
    } else if (BreakBeforeNext || 
Newlines > 13.03k
||
894
4.22k
               
(2.07k
ChangeMinColumn > MaxColumn2.07k
||
ChangeMaxColumn < MinColumn2.06k
) ||
895
               // Break the comment sequence if the previous line did not end
896
               // in a trailing comment.
897
4.22k
               
(2.05k
Changes[i].NewlinesBefore == 12.05k
&&
i > 0528
&&
898
2.05k
                
!Changes[i - 1].IsTrailingComment499
) ||
899
4.22k
               
WasAlignedWithStartOfNextLine1.67k
) {
900
2.62k
      alignTrailingComments(StartOfSequence, i, MinColumn);
901
2.62k
      MinColumn = ChangeMinColumn;
902
2.62k
      MaxColumn = ChangeMaxColumn;
903
2.62k
      StartOfSequence = i;
904
2.62k
    } else {
905
1.60k
      MinColumn = std::max(MinColumn, ChangeMinColumn);
906
1.60k
      MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
907
1.60k
    }
908
4.75k
    BreakBeforeNext = (i == 0) || 
(Changes[i].NewlinesBefore > 1)4.27k
||
909
                      // Never start a sequence with a comment at the beginning
910
                      // of the line.
911
4.75k
                      
(4.04k
Changes[i].NewlinesBefore == 14.04k
&&
StartOfSequence == i1.82k
);
912
4.75k
    Newlines = 0;
913
4.75k
  }
914
20.8k
  alignTrailingComments(StartOfSequence, Changes.size(), MinColumn);
915
20.8k
}
916
917
void WhitespaceManager::alignTrailingComments(unsigned Start, unsigned End,
918
24.0k
                                              unsigned Column) {
919
368k
  for (unsigned i = Start; i != End; 
++i344k
) {
920
344k
    int Shift = 0;
921
344k
    if (Changes[i].IsTrailingComment) {
922
5.36k
      Shift = Column - Changes[i].StartOfTokenColumn;
923
5.36k
    }
924
344k
    if (Changes[i].StartOfBlockComment) {
925
666
      Shift = Changes[i].IndentationOffset +
926
666
              Changes[i].StartOfBlockComment->StartOfTokenColumn -
927
666
              Changes[i].StartOfTokenColumn;
928
666
    }
929
344k
    if (Shift < 0)
930
0
      continue;
931
344k
    Changes[i].Spaces += Shift;
932
344k
    if (i + 1 != Changes.size())
933
323k
      Changes[i + 1].PreviousEndOfTokenColumn += Shift;
934
344k
    Changes[i].StartOfTokenColumn += Shift;
935
344k
  }
936
24.0k
}
937
938
20.8k
void WhitespaceManager::alignEscapedNewlines() {
939
20.8k
  if (Style.AlignEscapedNewlines == FormatStyle::ENAS_DontAlign)
940
10
    return;
941
942
20.8k
  bool AlignLeft = Style.AlignEscapedNewlines == FormatStyle::ENAS_Left;
943
20.8k
  unsigned MaxEndOfLine = AlignLeft ? 
04.08k
:
Style.ColumnLimit16.7k
;
944
20.8k
  unsigned StartOfMacro = 0;
945
344k
  for (unsigned i = 1, e = Changes.size(); i < e; 
++i323k
) {
946
323k
    Change &C = Changes[i];
947
323k
    if (C.NewlinesBefore > 0) {
948
46.3k
      if (C.ContinuesPPDirective) {
949
637
        MaxEndOfLine = std::max(C.PreviousEndOfTokenColumn + 2, MaxEndOfLine);
950
45.7k
      } else {
951
45.7k
        alignEscapedNewlines(StartOfMacro + 1, i, MaxEndOfLine);
952
45.7k
        MaxEndOfLine = AlignLeft ? 
08.88k
:
Style.ColumnLimit36.8k
;
953
45.7k
        StartOfMacro = i;
954
45.7k
      }
955
46.3k
    }
956
323k
  }
957
20.8k
  alignEscapedNewlines(StartOfMacro + 1, Changes.size(), MaxEndOfLine);
958
20.8k
}
959
960
void WhitespaceManager::alignEscapedNewlines(unsigned Start, unsigned End,
961
66.5k
                                             unsigned Column) {
962
344k
  for (unsigned i = Start; i < End; 
++i277k
) {
963
277k
    Change &C = Changes[i];
964
277k
    if (C.NewlinesBefore > 0) {
965
637
      assert(C.ContinuesPPDirective);
966
637
      if (C.PreviousEndOfTokenColumn + 1 > Column)
967
0
        C.EscapedNewlineColumn = 0;
968
637
      else
969
637
        C.EscapedNewlineColumn = Column;
970
637
    }
971
277k
  }
972
66.5k
}
973
974
20.8k
void WhitespaceManager::alignArrayInitializers() {
975
20.8k
  if (Style.AlignArrayOfStructures == FormatStyle::AIAS_None)
976
20.7k
    return;
977
978
106
  for (unsigned ChangeIndex = 1U, ChangeEnd = Changes.size();
979
886
       ChangeIndex < ChangeEnd; 
++ChangeIndex780
) {
980
780
    auto &C = Changes[ChangeIndex];
981
780
    if (C.Tok->IsArrayInitializer) {
982
112
      bool FoundComplete = false;
983
3.12k
      for (unsigned InsideIndex = ChangeIndex + 1; InsideIndex < ChangeEnd;
984
3.12k
           
++InsideIndex3.01k
) {
985
3.12k
        if (Changes[InsideIndex].Tok == C.Tok->MatchingParen) {
986
112
          alignArrayInitializers(ChangeIndex, InsideIndex + 1);
987
112
          ChangeIndex = InsideIndex + 1;
988
112
          FoundComplete = true;
989
112
          break;
990
112
        }
991
3.12k
      }
992
112
      if (!FoundComplete)
993
0
        ChangeIndex = ChangeEnd;
994
112
    }
995
780
  }
996
106
}
997
998
112
void WhitespaceManager::alignArrayInitializers(unsigned Start, unsigned End) {
999
1000
112
  if (Style.AlignArrayOfStructures == FormatStyle::AIAS_Right)
1001
61
    alignArrayInitializersRightJustified(getCells(Start, End));
1002
51
  else if (Style.AlignArrayOfStructures == FormatStyle::AIAS_Left)
1003
51
    alignArrayInitializersLeftJustified(getCells(Start, End));
1004
112
}
1005
1006
void WhitespaceManager::alignArrayInitializersRightJustified(
1007
61
    CellDescriptions &&CellDescs) {
1008
61
  auto &Cells = CellDescs.Cells;
1009
1010
  // Now go through and fixup the spaces.
1011
61
  auto *CellIter = Cells.begin();
1012
305
  for (auto i = 0U; i < CellDescs.CellCount; 
i++, ++CellIter244
) {
1013
244
    unsigned NetWidth = 0U;
1014
244
    if (isSplitCell(*CellIter))
1015
13
      NetWidth = getNetWidth(Cells.begin(), CellIter, CellDescs.InitialSpaces);
1016
244
    auto CellWidth = getMaximumCellWidth(CellIter, NetWidth);
1017
1018
244
    if (Changes[CellIter->Index].Tok->is(tok::r_brace)) {
1019
      // So in here we want to see if there is a brace that falls
1020
      // on a line that was split. If so on that line we make sure that
1021
      // the spaces in front of the brace are enough.
1022
61
      Changes[CellIter->Index].NewlinesBefore = 0;
1023
61
      Changes[CellIter->Index].Spaces = 0;
1024
174
      for (const auto *Next = CellIter->NextColumnElement; Next != nullptr;
1025
113
           Next = Next->NextColumnElement) {
1026
113
        Changes[Next->Index].Spaces = 0;
1027
113
        Changes[Next->Index].NewlinesBefore = 0;
1028
113
      }
1029
      // Unless the array is empty, we need the position of all the
1030
      // immediately adjacent cells
1031
61
      if (CellIter != Cells.begin()) {
1032
61
        auto ThisNetWidth =
1033
61
            getNetWidth(Cells.begin(), CellIter, CellDescs.InitialSpaces);
1034
61
        auto MaxNetWidth =
1035
61
            getMaximumNetWidth(Cells.begin(), CellIter, CellDescs.InitialSpaces,
1036
61
                               CellDescs.CellCount);
1037
61
        if (ThisNetWidth < MaxNetWidth)
1038
1
          Changes[CellIter->Index].Spaces = (MaxNetWidth - ThisNetWidth);
1039
61
        auto RowCount = 1U;
1040
61
        auto Offset = std::distance(Cells.begin(), CellIter);
1041
174
        for (const auto *Next = CellIter->NextColumnElement; Next != nullptr;
1042
113
             Next = Next->NextColumnElement) {
1043
113
          auto *Start = (Cells.begin() + RowCount * CellDescs.CellCount);
1044
113
          auto *End = Start + Offset;
1045
113
          ThisNetWidth = getNetWidth(Start, End, CellDescs.InitialSpaces);
1046
113
          if (ThisNetWidth < MaxNetWidth)
1047
1
            Changes[Next->Index].Spaces = (MaxNetWidth - ThisNetWidth);
1048
113
          ++RowCount;
1049
113
        }
1050
61
      }
1051
183
    } else {
1052
183
      auto ThisWidth =
1053
183
          calculateCellWidth(CellIter->Index, CellIter->EndIndex, true) +
1054
183
          NetWidth;
1055
183
      if (Changes[CellIter->Index].NewlinesBefore == 0) {
1056
170
        Changes[CellIter->Index].Spaces = (CellWidth - (ThisWidth + NetWidth));
1057
170
        Changes[CellIter->Index].Spaces += (i > 0) ? 
1110
:
060
;
1058
170
      }
1059
183
      alignToStartOfCell(CellIter->Index, CellIter->EndIndex);
1060
540
      for (const auto *Next = CellIter->NextColumnElement; Next != nullptr;
1061
357
           Next = Next->NextColumnElement) {
1062
357
        ThisWidth =
1063
357
            calculateCellWidth(Next->Index, Next->EndIndex, true) + NetWidth;
1064
357
        if (Changes[Next->Index].NewlinesBefore == 0) {
1065
355
          Changes[Next->Index].Spaces = (CellWidth - ThisWidth);
1066
355
          Changes[Next->Index].Spaces += (i > 0) ? 
1242
:
0113
;
1067
355
        }
1068
357
        alignToStartOfCell(Next->Index, Next->EndIndex);
1069
357
      }
1070
183
    }
1071
244
  }
1072
61
}
1073
1074
void WhitespaceManager::alignArrayInitializersLeftJustified(
1075
51
    CellDescriptions &&CellDescs) {
1076
51
  auto &Cells = CellDescs.Cells;
1077
1078
  // Now go through and fixup the spaces.
1079
51
  auto *CellIter = Cells.begin();
1080
  // The first cell needs to be against the left brace.
1081
51
  if (Changes[CellIter->Index].NewlinesBefore == 0)
1082
47
    Changes[CellIter->Index].Spaces = 0;
1083
4
  else
1084
4
    Changes[CellIter->Index].Spaces = CellDescs.InitialSpaces;
1085
51
  ++CellIter;
1086
198
  for (auto i = 1U; i < CellDescs.CellCount; 
i++, ++CellIter147
) {
1087
147
    auto MaxNetWidth = getMaximumNetWidth(
1088
147
        Cells.begin(), CellIter, CellDescs.InitialSpaces, CellDescs.CellCount);
1089
147
    auto ThisNetWidth =
1090
147
        getNetWidth(Cells.begin(), CellIter, CellDescs.InitialSpaces);
1091
147
    if (Changes[CellIter->Index].NewlinesBefore == 0) {
1092
139
      Changes[CellIter->Index].Spaces =
1093
139
          MaxNetWidth - ThisNetWidth +
1094
139
          (Changes[CellIter->Index].Tok->isNot(tok::r_brace) ? 
191
:
048
);
1095
139
    }
1096
147
    auto RowCount = 1U;
1097
147
    auto Offset = std::distance(Cells.begin(), CellIter);
1098
426
    for (const auto *Next = CellIter->NextColumnElement; Next != nullptr;
1099
279
         Next = Next->NextColumnElement) {
1100
279
      auto *Start = (Cells.begin() + RowCount * CellDescs.CellCount);
1101
279
      auto *End = Start + Offset;
1102
279
      auto ThisNetWidth = getNetWidth(Start, End, CellDescs.InitialSpaces);
1103
279
      if (Changes[Next->Index].NewlinesBefore == 0) {
1104
277
        Changes[Next->Index].Spaces =
1105
277
            MaxNetWidth - ThisNetWidth +
1106
277
            (Changes[Next->Index].Tok->isNot(tok::r_brace) ? 
1187
:
090
);
1107
277
      }
1108
279
      ++RowCount;
1109
279
    }
1110
147
  }
1111
51
}
1112
1113
244
bool WhitespaceManager::isSplitCell(const CellDescription &Cell) {
1114
244
  if (Cell.HasSplit)
1115
13
    return true;
1116
687
  
for (const auto *Next = Cell.NextColumnElement; 231
Next != nullptr;
1117
456
       Next = Next->NextColumnElement) {
1118
456
    if (Next->HasSplit)
1119
0
      return true;
1120
456
  }
1121
231
  return false;
1122
231
}
1123
1124
WhitespaceManager::CellDescriptions WhitespaceManager::getCells(unsigned Start,
1125
112
                                                                unsigned End) {
1126
1127
112
  unsigned Depth = 0;
1128
112
  unsigned Cell = 0;
1129
112
  unsigned CellCount = 0;
1130
112
  unsigned InitialSpaces = 0;
1131
112
  unsigned InitialTokenLength = 0;
1132
112
  unsigned EndSpaces = 0;
1133
112
  SmallVector<CellDescription> Cells;
1134
112
  const FormatToken *MatchingParen = nullptr;
1135
3.32k
  for (unsigned i = Start; i < End; 
++i3.21k
) {
1136
3.21k
    auto &C = Changes[i];
1137
3.21k
    if (C.Tok->is(tok::l_brace))
1138
487
      ++Depth;
1139
2.72k
    else if (C.Tok->is(tok::r_brace))
1140
487
      --Depth;
1141
3.21k
    if (Depth == 2) {
1142
2.07k
      if (C.Tok->is(tok::l_brace)) {
1143
315
        Cell = 0;
1144
315
        MatchingParen = C.Tok->MatchingParen;
1145
315
        if (InitialSpaces == 0) {
1146
112
          InitialSpaces = C.Spaces + C.TokenLength;
1147
112
          InitialTokenLength = C.TokenLength;
1148
112
          auto j = i - 1;
1149
112
          for (; Changes[j].NewlinesBefore == 0 && 
j > Start101
;
--j0
) {
1150
0
            InitialSpaces += Changes[j].Spaces + Changes[j].TokenLength;
1151
0
            InitialTokenLength += Changes[j].TokenLength;
1152
0
          }
1153
112
          if (C.NewlinesBefore == 0) {
1154
11
            InitialSpaces += Changes[j].Spaces + Changes[j].TokenLength;
1155
11
            InitialTokenLength += Changes[j].TokenLength;
1156
11
          }
1157
112
        }
1158
1.76k
      } else if (C.Tok->is(tok::comma)) {
1159
657
        if (!Cells.empty())
1160
657
          Cells.back().EndIndex = i;
1161
657
        if (C.Tok->getNextNonComment()->isNot(tok::r_brace)) // dangling comma
1162
654
          ++Cell;
1163
657
      }
1164
2.07k
    } else 
if (1.13k
Depth == 11.13k
) {
1165
763
      if (C.Tok == MatchingParen) {
1166
315
        if (!Cells.empty())
1167
315
          Cells.back().EndIndex = i;
1168
315
        Cells.push_back(CellDescription{i, ++Cell, i + 1, false, nullptr});
1169
315
        CellCount = C.Tok->Previous->isNot(tok::comma) ? 
Cell + 1312
:
Cell3
;
1170
        // Go to the next non-comment and ensure there is a break in front
1171
315
        const auto *NextNonComment = C.Tok->getNextNonComment();
1172
540
        while (NextNonComment->is(tok::comma))
1173
225
          NextNonComment = NextNonComment->getNextNonComment();
1174
315
        auto j = i;
1175
939
        while (Changes[j].Tok != NextNonComment && 
j < End624
)
1176
624
          ++j;
1177
315
        if (j < End && Changes[j].NewlinesBefore == 0 &&
1178
315
            
Changes[j].Tok->isNot(tok::r_brace)144
) {
1179
78
          Changes[j].NewlinesBefore = 1;
1180
          // Account for the added token lengths
1181
78
          Changes[j].Spaces = InitialSpaces - InitialTokenLength;
1182
78
        }
1183
448
      } else if (C.Tok->is(tok::comment)) {
1184
        // Trailing comments stay at a space past the last token
1185
54
        C.Spaces = Changes[i - 1].Tok->is(tok::comma) ? 
130
:
224
;
1186
394
      } else if (C.Tok->is(tok::l_brace)) {
1187
        // We need to make sure that the ending braces is aligned to the
1188
        // start of our initializer
1189
112
        auto j = i - 1;
1190
662
        for (; j > 0 && 
!Changes[j].Tok->ArrayInitializerLineStart556
;
--j550
)
1191
550
          ; // Nothing the loop does the work
1192
112
        EndSpaces = Changes[j].Spaces;
1193
112
      }
1194
763
    } else 
if (370
Depth == 0370
&&
C.Tok->is(tok::r_brace)112
) {
1195
112
      C.NewlinesBefore = 1;
1196
112
      C.Spaces = EndSpaces;
1197
112
    }
1198
3.21k
    if (C.Tok->StartsColumn) {
1199
      // This gets us past tokens that have been split over multiple
1200
      // lines
1201
972
      bool HasSplit = false;
1202
972
      if (Changes[i].NewlinesBefore > 0) {
1203
        // So if we split a line previously and the tail line + this token is
1204
        // less then the column limit we remove the split here and just put
1205
        // the column start at a space past the comma
1206
        //
1207
        // FIXME This if branch covers the cases where the column is not
1208
        // the first column. This leads to weird pathologies like the formatting
1209
        // auto foo = Items{
1210
        //     Section{
1211
        //             0, bar(),
1212
        //     }
1213
        // };
1214
        // Well if it doesn't lead to that it's indicative that the line
1215
        // breaking should be revisited. Unfortunately alot of other options
1216
        // interact with this
1217
36
        auto j = i - 1;
1218
36
        if ((j - 1) > Start && Changes[j].Tok->is(tok::comma) &&
1219
36
            
Changes[j - 1].NewlinesBefore > 031
) {
1220
4
          --j;
1221
4
          auto LineLimit = Changes[j].Spaces + Changes[j].TokenLength;
1222
4
          if (LineLimit < Style.ColumnLimit) {
1223
4
            Changes[i].NewlinesBefore = 0;
1224
4
            Changes[i].Spaces = 1;
1225
4
          }
1226
4
        }
1227
36
      }
1228
1.03k
      while (Changes[i].NewlinesBefore > 0 && 
Changes[i].Tok == C.Tok67
) {
1229
61
        Changes[i].Spaces = InitialSpaces;
1230
61
        ++i;
1231
61
        HasSplit = true;
1232
61
      }
1233
972
      if (Changes[i].Tok != C.Tok)
1234
32
        --i;
1235
972
      Cells.push_back(CellDescription{i, Cell, i, HasSplit, nullptr});
1236
972
    }
1237
3.21k
  }
1238
1239
112
  return linkCells({Cells, CellCount, InitialSpaces});
1240
112
}
1241
1242
unsigned WhitespaceManager::calculateCellWidth(unsigned Start, unsigned End,
1243
4.09k
                                               bool WithSpaces) const {
1244
4.09k
  unsigned CellWidth = 0;
1245
9.83k
  for (auto i = Start; i < End; 
i++5.74k
) {
1246
5.74k
    if (Changes[i].NewlinesBefore > 0)
1247
111
      CellWidth = 0;
1248
5.74k
    CellWidth += Changes[i].TokenLength;
1249
5.74k
    CellWidth += (WithSpaces ? Changes[i].Spaces : 
00
);
1250
5.74k
  }
1251
4.09k
  return CellWidth;
1252
4.09k
}
1253
1254
540
void WhitespaceManager::alignToStartOfCell(unsigned Start, unsigned End) {
1255
540
  if ((End - Start) <= 1)
1256
446
    return;
1257
  // If the line is broken anywhere in there make sure everything
1258
  // is aligned to the parent
1259
317
  
for (auto i = Start + 1; 94
i < End;
i++223
) {
1260
223
    if (Changes[i].NewlinesBefore > 0)
1261
3
      Changes[i].Spaces = Changes[Start].Spaces;
1262
223
  }
1263
94
}
1264
1265
WhitespaceManager::CellDescriptions
1266
112
WhitespaceManager::linkCells(CellDescriptions &&CellDesc) {
1267
112
  auto &Cells = CellDesc.Cells;
1268
1.39k
  for (auto *CellIter = Cells.begin(); CellIter != Cells.end(); 
++CellIter1.28k
) {
1269
1.28k
    if (CellIter->NextColumnElement == nullptr &&
1270
1.28k
        ((CellIter + 1) != Cells.end())) {
1271
4.50k
      for (auto *NextIter = CellIter + 1; NextIter != Cells.end(); 
++NextIter3.33k
) {
1272
4.17k
        if (NextIter->Cell == CellIter->Cell) {
1273
842
          CellIter->NextColumnElement = &(*NextIter);
1274
842
          break;
1275
842
        }
1276
4.17k
      }
1277
1.17k
    }
1278
1.28k
  }
1279
112
  return std::move(CellDesc);
1280
112
}
1281
1282
20.8k
void WhitespaceManager::generateChanges() {
1283
365k
  for (unsigned i = 0, e = Changes.size(); i != e; 
++i344k
) {
1284
344k
    const Change &C = Changes[i];
1285
344k
    if (i > 0 && Changes[i - 1].OriginalWhitespaceRange.getBegin() ==
1286
323k
                     C.OriginalWhitespaceRange.getBegin()) {
1287
      // Do not generate two replacements for the same location.
1288
0
      continue;
1289
0
    }
1290
344k
    if (C.CreateReplacement) {
1291
320k
      std::string ReplacementText = C.PreviousLinePostfix;
1292
320k
      if (C.ContinuesPPDirective)
1293
618
        appendEscapedNewlineText(ReplacementText, C.NewlinesBefore,
1294
618
                                 C.PreviousEndOfTokenColumn,
1295
618
                                 C.EscapedNewlineColumn);
1296
319k
      else
1297
319k
        appendNewlineText(ReplacementText, C.NewlinesBefore);
1298
      // FIXME: This assert should hold if we computed the column correctly.
1299
      // assert((int)C.StartOfTokenColumn >= C.Spaces);
1300
320k
      appendIndentText(
1301
320k
          ReplacementText, C.Tok->IndentLevel, std::max(0, C.Spaces),
1302
320k
          std::max((int)C.StartOfTokenColumn, C.Spaces) - std::max(0, C.Spaces),
1303
320k
          C.IsAligned);
1304
320k
      ReplacementText.append(C.CurrentLinePrefix);
1305
320k
      storeReplacement(C.OriginalWhitespaceRange, ReplacementText);
1306
320k
    }
1307
344k
  }
1308
20.8k
}
1309
1310
320k
void WhitespaceManager::storeReplacement(SourceRange Range, StringRef Text) {
1311
320k
  unsigned WhitespaceLength = SourceMgr.getFileOffset(Range.getEnd()) -
1312
320k
                              SourceMgr.getFileOffset(Range.getBegin());
1313
  // Don't create a replacement, if it does not change anything.
1314
320k
  if (StringRef(SourceMgr.getCharacterData(Range.getBegin()),
1315
320k
                WhitespaceLength) == Text)
1316
293k
    return;
1317
26.9k
  auto Err = Replaces.add(tooling::Replacement(
1318
26.9k
      SourceMgr, CharSourceRange::getCharRange(Range), Text));
1319
  // FIXME: better error handling. For now, just print an error message in the
1320
  // release version.
1321
26.9k
  if (Err) {
1322
0
    llvm::errs() << llvm::toString(std::move(Err)) << "\n";
1323
0
    assert(false);
1324
0
  }
1325
26.9k
}
1326
1327
void WhitespaceManager::appendNewlineText(std::string &Text,
1328
319k
                                          unsigned Newlines) {
1329
362k
  for (unsigned i = 0; i < Newlines; 
++i42.9k
)
1330
42.9k
    Text.append(UseCRLF ? 
"\r\n"189
:
"\n"42.7k
);
1331
319k
}
1332
1333
void WhitespaceManager::appendEscapedNewlineText(
1334
    std::string &Text, unsigned Newlines, unsigned PreviousEndOfTokenColumn,
1335
618
    unsigned EscapedNewlineColumn) {
1336
618
  if (Newlines > 0) {
1337
615
    unsigned Spaces =
1338
615
        std::max<int>(1, EscapedNewlineColumn - PreviousEndOfTokenColumn - 1);
1339
1.23k
    for (unsigned i = 0; i < Newlines; 
++i620
) {
1340
620
      Text.append(Spaces, ' ');
1341
620
      Text.append(UseCRLF ? 
"\\\r\n"7
:
"\\\n"613
);
1342
620
      Spaces = std::max<int>(0, EscapedNewlineColumn - 1);
1343
620
    }
1344
615
  }
1345
618
}
1346
1347
void WhitespaceManager::appendIndentText(std::string &Text,
1348
                                         unsigned IndentLevel, unsigned Spaces,
1349
                                         unsigned WhitespaceStartColumn,
1350
320k
                                         bool IsAligned) {
1351
320k
  switch (Style.UseTab) {
1352
317k
  case FormatStyle::UT_Never:
1353
317k
    Text.append(Spaces, ' ');
1354
317k
    break;
1355
481
  case FormatStyle::UT_Always: {
1356
481
    if (Style.TabWidth) {
1357
463
      unsigned FirstTabWidth =
1358
463
          Style.TabWidth - WhitespaceStartColumn % Style.TabWidth;
1359
1360
      // Insert only spaces when we want to end up before the next tab.
1361
463
      if (Spaces < FirstTabWidth || 
Spaces == 1116
) {
1362
368
        Text.append(Spaces, ' ');
1363
368
        break;
1364
368
      }
1365
      // Align to the next tab.
1366
95
      Spaces -= FirstTabWidth;
1367
95
      Text.append("\t");
1368
1369
95
      Text.append(Spaces / Style.TabWidth, '\t');
1370
95
      Text.append(Spaces % Style.TabWidth, ' ');
1371
95
    } else 
if (18
Spaces == 118
) {
1372
2
      Text.append(Spaces, ' ');
1373
2
    }
1374
113
    break;
1375
481
  }
1376
421
  case FormatStyle::UT_ForIndentation:
1377
421
    if (WhitespaceStartColumn == 0) {
1378
192
      unsigned Indentation = IndentLevel * Style.IndentWidth;
1379
192
      Spaces = appendTabIndent(Text, Spaces, Indentation);
1380
192
    }
1381
421
    Text.append(Spaces, ' ');
1382
421
    break;
1383
738
  case FormatStyle::UT_ForContinuationAndIndentation:
1384
738
    if (WhitespaceStartColumn == 0)
1385
319
      Spaces = appendTabIndent(Text, Spaces, Spaces);
1386
738
    Text.append(Spaces, ' ');
1387
738
    break;
1388
840
  case FormatStyle::UT_AlignWithSpaces:
1389
840
    if (WhitespaceStartColumn == 0) {
1390
358
      unsigned Indentation =
1391
358
          IsAligned ? 
IndentLevel * Style.IndentWidth57
:
Spaces301
;
1392
358
      Spaces = appendTabIndent(Text, Spaces, Indentation);
1393
358
    }
1394
840
    Text.append(Spaces, ' ');
1395
840
    break;
1396
320k
  }
1397
320k
}
1398
1399
unsigned WhitespaceManager::appendTabIndent(std::string &Text, unsigned Spaces,
1400
869
                                            unsigned Indentation) {
1401
  // This happens, e.g. when a line in a block comment is indented less than the
1402
  // first one.
1403
869
  if (Indentation > Spaces)
1404
4
    Indentation = Spaces;
1405
869
  if (Style.TabWidth) {
1406
845
    unsigned Tabs = Indentation / Style.TabWidth;
1407
845
    Text.append(Tabs, '\t');
1408
845
    Spaces -= Tabs * Style.TabWidth;
1409
845
  }
1410
869
  return Spaces;
1411
869
}
1412
1413
} // namespace format
1414
} // namespace clang