Coverage Report

Created: 2022-07-16 07:03

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Format/QualifierAlignmentFixer.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- LeftRightQualifierAlignmentFixer.cpp -------------------*- C++--*-===//
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 LeftRightQualifierAlignmentFixer, a TokenAnalyzer that
11
/// enforces either left or right const depending on the style.
12
///
13
//===----------------------------------------------------------------------===//
14
15
#include "QualifierAlignmentFixer.h"
16
#include "FormatToken.h"
17
#include "llvm/Support/Debug.h"
18
#include "llvm/Support/Regex.h"
19
20
#include <algorithm>
21
22
#define DEBUG_TYPE "format-qualifier-alignment-fixer"
23
24
namespace clang {
25
namespace format {
26
27
QualifierAlignmentFixer::QualifierAlignmentFixer(
28
    const Environment &Env, const FormatStyle &Style, StringRef &Code,
29
    ArrayRef<tooling::Range> Ranges, unsigned FirstStartColumn,
30
    unsigned NextStartColumn, unsigned LastStartColumn, StringRef FileName)
31
    : TokenAnalyzer(Env, Style), Code(Code), Ranges(Ranges),
32
      FirstStartColumn(FirstStartColumn), NextStartColumn(NextStartColumn),
33
816
      LastStartColumn(LastStartColumn), FileName(FileName) {
34
816
  std::vector<std::string> LeftOrder;
35
816
  std::vector<std::string> RightOrder;
36
816
  std::vector<tok::TokenKind> ConfiguredQualifierTokens;
37
816
  PrepareLeftRightOrdering(Style.QualifierOrder, LeftOrder, RightOrder,
38
816
                           ConfiguredQualifierTokens);
39
40
  // Handle the left and right alignment separately.
41
1.52k
  for (const auto &Qualifier : LeftOrder) {
42
1.52k
    Passes.emplace_back(
43
1.56k
        [&, Qualifier, ConfiguredQualifierTokens](const Environment &Env) {
44
1.56k
          return LeftRightQualifierAlignmentFixer(Env, Style, Qualifier,
45
1.56k
                                                  ConfiguredQualifierTokens,
46
1.56k
                                                  /*RightAlign=*/false)
47
1.56k
              .process();
48
1.56k
        });
49
1.52k
  }
50
816
  for (const auto &Qualifier : RightOrder) {
51
729
    Passes.emplace_back(
52
759
        [&, Qualifier, ConfiguredQualifierTokens](const Environment &Env) {
53
759
          return LeftRightQualifierAlignmentFixer(Env, Style, Qualifier,
54
759
                                                  ConfiguredQualifierTokens,
55
759
                                                  /*RightAlign=*/true)
56
759
              .process();
57
759
        });
58
729
  }
59
816
}
60
61
std::pair<tooling::Replacements, unsigned> QualifierAlignmentFixer::analyze(
62
    TokenAnnotator & /*Annotator*/,
63
    SmallVectorImpl<AnnotatedLine *> & /*AnnotatedLines*/,
64
840
    FormatTokenLexer & /*Tokens*/) {
65
840
  auto Env = Environment::make(Code, FileName, Ranges, FirstStartColumn,
66
840
                               NextStartColumn, LastStartColumn);
67
840
  if (!Env)
68
0
    return {};
69
840
  llvm::Optional<std::string> CurrentCode = None;
70
840
  tooling::Replacements Fixes;
71
3.15k
  for (size_t I = 0, E = Passes.size(); I < E; 
++I2.31k
) {
72
2.31k
    std::pair<tooling::Replacements, unsigned> PassFixes = Passes[I](*Env);
73
2.31k
    auto NewCode = applyAllReplacements(
74
2.31k
        CurrentCode ? 
StringRef(*CurrentCode)1.47k
:
Code840
, PassFixes.first);
75
2.31k
    if (NewCode) {
76
2.31k
      Fixes = Fixes.merge(PassFixes.first);
77
2.31k
      if (I + 1 < E) {
78
1.47k
        CurrentCode = std::move(*NewCode);
79
1.47k
        Env = Environment::make(
80
1.47k
            *CurrentCode, FileName,
81
1.47k
            tooling::calculateRangesAfterReplacements(Fixes, Ranges),
82
1.47k
            FirstStartColumn, NextStartColumn, LastStartColumn);
83
1.47k
        if (!Env)
84
0
          return {};
85
1.47k
      }
86
2.31k
    }
87
2.31k
  }
88
89
  // Don't make replacements that replace nothing.
90
840
  tooling::Replacements NonNoOpFixes;
91
92
840
  for (const tooling::Replacement &Fix : Fixes) {
93
535
    StringRef OriginalCode = Code.substr(Fix.getOffset(), Fix.getLength());
94
95
535
    if (!OriginalCode.equals(Fix.getReplacementText())) {
96
434
      auto Err = NonNoOpFixes.add(Fix);
97
434
      if (Err) {
98
0
        llvm::errs() << "Error adding replacements : "
99
0
                     << llvm::toString(std::move(Err)) << "\n";
100
0
      }
101
434
    }
102
535
  }
103
840
  return {NonNoOpFixes, 0};
104
840
}
105
106
static void replaceToken(const SourceManager &SourceMgr,
107
                         tooling::Replacements &Fixes,
108
841
                         const CharSourceRange &Range, std::string NewText) {
109
841
  auto Replacement = tooling::Replacement(SourceMgr, Range, NewText);
110
841
  auto Err = Fixes.add(Replacement);
111
112
841
  if (Err) {
113
0
    llvm::errs() << "Error while rearranging Qualifier : "
114
0
                 << llvm::toString(std::move(Err)) << "\n";
115
0
  }
116
841
}
117
118
static void removeToken(const SourceManager &SourceMgr,
119
                        tooling::Replacements &Fixes,
120
114
                        const FormatToken *First) {
121
114
  auto Range = CharSourceRange::getCharRange(First->getStartOfNonWhitespace(),
122
114
                                             First->Tok.getEndLoc());
123
114
  replaceToken(SourceMgr, Fixes, Range, "");
124
114
}
125
126
static void insertQualifierAfter(const SourceManager &SourceMgr,
127
                                 tooling::Replacements &Fixes,
128
                                 const FormatToken *First,
129
76
                                 const std::string &Qualifier) {
130
76
  FormatToken *Next = First->Next;
131
76
  if (!Next)
132
0
    return;
133
76
  auto Range = CharSourceRange::getCharRange(Next->getStartOfNonWhitespace(),
134
76
                                             Next->Tok.getEndLoc());
135
136
76
  std::string NewText = " " + Qualifier + " ";
137
76
  NewText += Next->TokenText;
138
76
  replaceToken(SourceMgr, Fixes, Range, NewText);
139
76
}
140
141
static void insertQualifierBefore(const SourceManager &SourceMgr,
142
                                  tooling::Replacements &Fixes,
143
                                  const FormatToken *First,
144
38
                                  const std::string &Qualifier) {
145
38
  auto Range = CharSourceRange::getCharRange(First->getStartOfNonWhitespace(),
146
38
                                             First->Tok.getEndLoc());
147
148
38
  std::string NewText = " " + Qualifier + " ";
149
38
  NewText += First->TokenText;
150
151
38
  replaceToken(SourceMgr, Fixes, Range, NewText);
152
38
}
153
154
683
static bool endsWithSpace(const std::string &s) {
155
683
  if (s.empty())
156
0
    return false;
157
683
  return isspace(s.back());
158
683
}
159
160
222
static bool startsWithSpace(const std::string &s) {
161
222
  if (s.empty())
162
0
    return false;
163
222
  return isspace(s.front());
164
222
}
165
166
static void rotateTokens(const SourceManager &SourceMgr,
167
                         tooling::Replacements &Fixes, const FormatToken *First,
168
613
                         const FormatToken *Last, bool Left) {
169
613
  auto *End = Last;
170
613
  auto *Begin = First;
171
613
  if (!Left) {
172
222
    End = Last->Next;
173
222
    Begin = First->Next;
174
222
  }
175
176
613
  std::string NewText;
177
  // If we are rotating to the left we move the Last token to the front.
178
613
  if (Left) {
179
391
    NewText += Last->TokenText;
180
391
    NewText += " ";
181
391
  }
182
183
  // Then move through the other tokens.
184
613
  auto *Tok = Begin;
185
1.51k
  while (Tok != End) {
186
905
    if (!NewText.empty() && 
!endsWithSpace(NewText)683
)
187
292
      NewText += " ";
188
189
905
    NewText += Tok->TokenText;
190
905
    Tok = Tok->Next;
191
905
  }
192
193
  // If we are rotating to the right we move the first token to the back.
194
613
  if (!Left) {
195
222
    if (!NewText.empty() && !startsWithSpace(NewText))
196
222
      NewText += " ";
197
222
    NewText += First->TokenText;
198
222
  }
199
200
613
  auto Range = CharSourceRange::getCharRange(First->getStartOfNonWhitespace(),
201
613
                                             Last->Tok.getEndLoc());
202
203
613
  replaceToken(SourceMgr, Fixes, Range, NewText);
204
613
}
205
206
const FormatToken *LeftRightQualifierAlignmentFixer::analyzeRight(
207
    const SourceManager &SourceMgr, const AdditionalKeywords &Keywords,
208
    tooling::Replacements &Fixes, const FormatToken *Tok,
209
5.77k
    const std::string &Qualifier, tok::TokenKind QualifierType) {
210
  // We only need to think about streams that begin with a qualifier.
211
5.77k
  if (!Tok->is(QualifierType))
212
5.24k
    return Tok;
213
  // Don't concern yourself if nothing follows the qualifier.
214
528
  if (!Tok->Next)
215
0
    return Tok;
216
528
  if (LeftRightQualifierAlignmentFixer::isPossibleMacro(Tok->Next))
217
6
    return Tok;
218
219
522
  auto AnalyzeTemplate =
220
522
      [&](const FormatToken *Tok,
221
522
          const FormatToken *StartTemplate) -> const FormatToken * {
222
    // Read from the TemplateOpener to TemplateCloser.
223
55
    FormatToken *EndTemplate = StartTemplate->MatchingParen;
224
55
    if (EndTemplate) {
225
      // Move to the end of any template class members e.g.
226
      // `Foo<int>::iterator`.
227
55
      if (EndTemplate->startsSequence(TT_TemplateCloser, tok::coloncolon,
228
55
                                      tok::identifier)) {
229
11
        EndTemplate = EndTemplate->Next->Next;
230
11
      }
231
55
    }
232
55
    if (EndTemplate && EndTemplate->Next &&
233
55
        !EndTemplate->Next->isOneOf(tok::equal, tok::l_paren)) {
234
48
      insertQualifierAfter(SourceMgr, Fixes, EndTemplate, Qualifier);
235
      // Remove the qualifier.
236
48
      removeToken(SourceMgr, Fixes, Tok);
237
48
      return Tok;
238
48
    }
239
7
    return nullptr;
240
55
  };
241
242
522
  FormatToken *Qual = Tok->Next;
243
522
  FormatToken *LastQual = Qual;
244
798
  while (Qual && isQualifierOrType(Qual, ConfiguredQualifierTokens)) {
245
276
    LastQual = Qual;
246
276
    Qual = Qual->Next;
247
276
  }
248
522
  if (LastQual && Qual != LastQual) {
249
216
    rotateTokens(SourceMgr, Fixes, Tok, LastQual, /*Left=*/false);
250
216
    Tok = LastQual;
251
306
  } else if (Tok->startsSequence(QualifierType, tok::identifier,
252
306
                                 TT_TemplateCloser)) {
253
6
    FormatToken *Closer = Tok->Next->Next;
254
6
    rotateTokens(SourceMgr, Fixes, Tok, Tok->Next, /*Left=*/false);
255
6
    Tok = Closer;
256
6
    return Tok;
257
300
  } else if (Tok->startsSequence(QualifierType, tok::identifier,
258
300
                                 TT_TemplateOpener)) {
259
    // `const ArrayRef<int> a;`
260
    // `const ArrayRef<int> &a;`
261
53
    const FormatToken *NewTok = AnalyzeTemplate(Tok, Tok->Next->Next);
262
53
    if (NewTok)
263
46
      return NewTok;
264
247
  } else if (Tok->startsSequence(QualifierType, tok::coloncolon,
265
247
                                 tok::identifier, TT_TemplateOpener)) {
266
    // `const ::ArrayRef<int> a;`
267
    // `const ::ArrayRef<int> &a;`
268
2
    const FormatToken *NewTok = AnalyzeTemplate(Tok, Tok->Next->Next->Next);
269
2
    if (NewTok)
270
2
      return NewTok;
271
245
  } else if (Tok->startsSequence(QualifierType, tok::identifier) ||
272
245
             Tok->startsSequence(QualifierType, tok::coloncolon,
273
149
                                 tok::identifier)) {
274
98
    FormatToken *Next = Tok->Next;
275
    // The case  `const Foo` -> `Foo const`
276
    // The case  `const ::Foo` -> `::Foo const`
277
    // The case  `const Foo *` -> `Foo const *`
278
    // The case  `const Foo &` -> `Foo const &`
279
    // The case  `const Foo &&` -> `Foo const &&`
280
    // The case  `const std::Foo &&` -> `std::Foo const &&`
281
    // The case  `const std::Foo<T> &&` -> `std::Foo<T> const &&`
282
255
    while (Next && 
Next->isOneOf(tok::identifier, tok::coloncolon)252
)
283
157
      Next = Next->Next;
284
98
    if (Next && 
Next->is(TT_TemplateOpener)95
) {
285
8
      Next = Next->MatchingParen;
286
      // Move to the end of any template class members e.g.
287
      // `Foo<int>::iterator`.
288
8
      if (Next && Next->startsSequence(TT_TemplateCloser, tok::coloncolon,
289
8
                                       tok::identifier)) {
290
0
        return Tok;
291
0
      }
292
8
      assert(Next && "Missing template opener");
293
0
      Next = Next->Next;
294
8
    }
295
98
    if (Next && 
Next->isOneOf(tok::star, tok::amp, tok::ampamp)95
&&
296
98
        
!Tok->Next->isOneOf(Keywords.kw_override, Keywords.kw_final)28
) {
297
28
      if (Next->Previous && !Next->Previous->is(QualifierType)) {
298
28
        insertQualifierAfter(SourceMgr, Fixes, Next->Previous, Qualifier);
299
28
        removeToken(SourceMgr, Fixes, Tok);
300
28
      }
301
28
      return Next;
302
28
    }
303
98
  }
304
305
440
  return Tok;
306
522
}
307
308
const FormatToken *LeftRightQualifierAlignmentFixer::analyzeLeft(
309
    const SourceManager &SourceMgr, const AdditionalKeywords &Keywords,
310
    tooling::Replacements &Fixes, const FormatToken *Tok,
311
9.60k
    const std::string &Qualifier, tok::TokenKind QualifierType) {
312
  // if Tok is an identifier and possibly a macro then don't convert.
313
9.60k
  if (LeftRightQualifierAlignmentFixer::isPossibleMacro(Tok))
314
83
    return Tok;
315
316
9.52k
  const FormatToken *Qual = Tok;
317
9.52k
  const FormatToken *LastQual = Qual;
318
13.6k
  while (Qual && isQualifierOrType(Qual, ConfiguredQualifierTokens)) {
319
4.54k
    LastQual = Qual;
320
4.54k
    Qual = Qual->Next;
321
4.54k
    if (Qual && Qual->is(QualifierType))
322
367
      break;
323
4.54k
  }
324
325
9.52k
  if (!Qual)
326
0
    return Tok;
327
328
9.52k
  if (LastQual && Qual != LastQual && 
Qual->is(QualifierType)2.96k
) {
329
367
    rotateTokens(SourceMgr, Fixes, Tok, Qual, /*Left=*/true);
330
367
    if (!Qual->Next)
331
6
      return Tok;
332
361
    Tok = Qual->Next;
333
9.15k
  } else if (Tok->startsSequence(tok::identifier, QualifierType)) {
334
25
    if (Tok->Next->Next && Tok->Next->Next->isOneOf(tok::identifier, tok::star,
335
25
                                                    tok::amp, tok::ampamp)) {
336
      // Don't swap `::iterator const` to `::const iterator`.
337
2
      if (!Tok->Previous ||
338
2
          (Tok->Previous && !Tok->Previous->is(tok::coloncolon))) {
339
2
        rotateTokens(SourceMgr, Fixes, Tok, Tok->Next, /*Left=*/true);
340
2
        Tok = Tok->Next;
341
2
      }
342
23
    } else if (Tok->startsSequence(tok::identifier, QualifierType,
343
23
                                   TT_TemplateCloser)) {
344
8
      FormatToken *Closer = Tok->Next->Next;
345
8
      rotateTokens(SourceMgr, Fixes, Tok, Tok->Next, /*Left=*/true);
346
8
      Tok = Closer;
347
8
    }
348
25
  }
349
9.51k
  if (Tok->is(TT_TemplateOpener) && 
Tok->Next690
&&
350
9.51k
      
(690
Tok->Next->is(tok::identifier)690
||
Tok->Next->isSimpleTypeSpecifier()376
) &&
351
9.51k
      
Tok->Next->Next504
&&
Tok->Next->Next->is(QualifierType)504
) {
352
14
    rotateTokens(SourceMgr, Fixes, Tok->Next, Tok->Next->Next, /*Left=*/true);
353
14
  }
354
9.51k
  if ((Tok->startsSequence(tok::coloncolon, tok::identifier) ||
355
9.51k
       
Tok->is(tok::identifier)9.25k
) &&
356
9.51k
      
Tok->Next3.25k
) {
357
3.25k
    if (Tok->Previous &&
358
3.25k
        
Tok->Previous->isOneOf(tok::star, tok::ampamp, tok::amp)3.09k
) {
359
513
      return Tok;
360
513
    }
361
2.74k
    const FormatToken *Next = Tok->Next;
362
    // The case  `std::Foo<T> const` -> `const std::Foo<T> &&`
363
3.59k
    while (Next && 
Next->isOneOf(tok::identifier, tok::coloncolon)3.56k
)
364
846
      Next = Next->Next;
365
2.74k
    if (Next && 
Next->Previous2.72k
&&
366
2.74k
        
Next->Previous->startsSequence(tok::identifier, TT_TemplateOpener)2.72k
) {
367
      // Read from to the end of the TemplateOpener to
368
      // TemplateCloser const ArrayRef<int> a; const ArrayRef<int> &a;
369
807
      if (Next->is(tok::comment) && 
Next->getNextNonComment()36
)
370
36
        Next = Next->getNextNonComment();
371
807
      assert(Next->MatchingParen && "Missing template closer");
372
0
      Next = Next->MatchingParen;
373
374
      // If the template closer is closing the requires clause,
375
      // then stop and go back to the TemplateOpener and do whatever is
376
      // inside the <>.
377
807
      if (Next->ClosesRequiresClause)
378
6
        return Next->MatchingParen;
379
801
      Next = Next->Next;
380
381
      // Move to the end of any template class members e.g.
382
      // `Foo<int>::iterator`.
383
801
      if (Next && 
Next->startsSequence(tok::coloncolon, tok::identifier)729
)
384
94
        Next = Next->Next->Next;
385
801
      if (Next && 
Next->is(QualifierType)729
) {
386
        // Move the qualifier.
387
32
        insertQualifierBefore(SourceMgr, Fixes, Tok, Qualifier);
388
32
        removeToken(SourceMgr, Fixes, Next);
389
32
        return Next;
390
32
      }
391
801
    }
392
2.70k
    if (Next && 
Next->Next2.61k
&&
393
2.70k
        
Next->Next->isOneOf(tok::amp, tok::ampamp, tok::star)1.74k
) {
394
193
      if (Next->is(QualifierType)) {
395
        // Move the qualifier.
396
6
        insertQualifierBefore(SourceMgr, Fixes, Tok, Qualifier);
397
6
        removeToken(SourceMgr, Fixes, Next);
398
6
        return Next;
399
6
      }
400
193
    }
401
2.70k
  }
402
8.95k
  return Tok;
403
9.51k
}
404
405
tok::TokenKind LeftRightQualifierAlignmentFixer::getTokenFromQualifier(
406
4.83k
    const std::string &Qualifier) {
407
  // Don't let 'type' be an identifier, but steal typeof token.
408
4.83k
  return llvm::StringSwitch<tok::TokenKind>(Qualifier)
409
4.83k
      .Case("type", tok::kw_typeof)
410
4.83k
      .Case("const", tok::kw_const)
411
4.83k
      .Case("volatile", tok::kw_volatile)
412
4.83k
      .Case("static", tok::kw_static)
413
4.83k
      .Case("inline", tok::kw_inline)
414
4.83k
      .Case("constexpr", tok::kw_constexpr)
415
4.83k
      .Case("restrict", tok::kw_restrict)
416
4.83k
      .Default(tok::identifier);
417
4.83k
}
418
419
LeftRightQualifierAlignmentFixer::LeftRightQualifierAlignmentFixer(
420
    const Environment &Env, const FormatStyle &Style,
421
    const std::string &Qualifier,
422
    const std::vector<tok::TokenKind> &QualifierTokens, bool RightAlign)
423
    : TokenAnalyzer(Env, Style), Qualifier(Qualifier), RightAlign(RightAlign),
424
2.31k
      ConfiguredQualifierTokens(QualifierTokens) {}
425
426
std::pair<tooling::Replacements, unsigned>
427
LeftRightQualifierAlignmentFixer::analyze(
428
    TokenAnnotator & /*Annotator*/,
429
    SmallVectorImpl<AnnotatedLine *> &AnnotatedLines,
430
2.45k
    FormatTokenLexer &Tokens) {
431
2.45k
  tooling::Replacements Fixes;
432
2.45k
  const AdditionalKeywords &Keywords = Tokens.getKeywords();
433
2.45k
  const SourceManager &SourceMgr = Env.getSourceManager();
434
2.45k
  AffectedRangeMgr.computeAffectedLines(AnnotatedLines);
435
436
2.45k
  tok::TokenKind QualifierToken = getTokenFromQualifier(Qualifier);
437
2.45k
  assert(QualifierToken != tok::identifier && "Unrecognised Qualifier");
438
439
5.74k
  for (AnnotatedLine *Line : AnnotatedLines) {
440
5.74k
    if (Line->InPPDirective)
441
804
      continue;
442
4.94k
    FormatToken *First = Line->First;
443
4.94k
    assert(First);
444
4.94k
    if (First->Finalized)
445
18
      continue;
446
447
4.92k
    const auto *Last = Line->Last;
448
449
20.4k
    for (const auto *Tok = First; Tok && 
Tok != Last20.4k
&&
Tok->Next15.4k
;
450
15.4k
         Tok = Tok->Next) {
451
15.4k
      if (Tok->is(tok::comment))
452
114
        continue;
453
15.3k
      if (RightAlign) {
454
5.77k
        Tok = analyzeRight(SourceMgr, Keywords, Fixes, Tok, Qualifier,
455
5.77k
                           QualifierToken);
456
9.60k
      } else {
457
9.60k
        Tok = analyzeLeft(SourceMgr, Keywords, Fixes, Tok, Qualifier,
458
9.60k
                          QualifierToken);
459
9.60k
      }
460
15.3k
    }
461
4.92k
  }
462
2.45k
  return {Fixes, 0};
463
2.45k
}
464
465
void QualifierAlignmentFixer::PrepareLeftRightOrdering(
466
    const std::vector<std::string> &Order, std::vector<std::string> &LeftOrder,
467
    std::vector<std::string> &RightOrder,
468
817
    std::vector<tok::TokenKind> &Qualifiers) {
469
470
  // Depending on the position of type in the order you need
471
  // To iterate forward or backward through the order list as qualifier
472
  // can push through each other.
473
  // The Order list must define the position of "type" to signify
474
817
  assert(llvm::is_contained(Order, "type") &&
475
817
         "QualifierOrder must contain type");
476
  // Split the Order list by type and reverse the left side.
477
478
0
  bool left = true;
479
3.07k
  for (const auto &s : Order) {
480
3.07k
    if (s == "type") {
481
817
      left = false;
482
817
      continue;
483
817
    }
484
485
2.25k
    tok::TokenKind QualifierToken =
486
2.25k
        LeftRightQualifierAlignmentFixer::getTokenFromQualifier(s);
487
2.25k
    if (QualifierToken != tok::kw_typeof && QualifierToken != tok::identifier)
488
2.25k
      Qualifiers.push_back(QualifierToken);
489
490
2.25k
    if (left) {
491
      // Reverse the order for left aligned items.
492
1.52k
      LeftOrder.insert(LeftOrder.begin(), s);
493
1.52k
    } else {
494
731
      RightOrder.push_back(s);
495
731
    }
496
2.25k
  }
497
817
}
498
499
bool LeftRightQualifierAlignmentFixer::isQualifierOrType(
500
14.5k
    const FormatToken *Tok, const std::vector<tok::TokenKind> &specifiedTypes) {
501
14.5k
  return Tok && (Tok->isSimpleTypeSpecifier() || 
Tok->is(tok::kw_auto)12.1k
||
502
14.5k
                 
llvm::is_contained(specifiedTypes, Tok->Tok.getKind())12.0k
);
503
14.5k
}
504
505
// If a token is an identifier and it's upper case, it could
506
// be a macro and hence we need to be able to ignore it.
507
10.1k
bool LeftRightQualifierAlignmentFixer::isPossibleMacro(const FormatToken *Tok) {
508
10.1k
  if (!Tok)
509
0
    return false;
510
10.1k
  if (!Tok->is(tok::identifier))
511
6.97k
    return false;
512
3.15k
  if (Tok->TokenText.upper() == Tok->TokenText.str()) {
513
    // T,K,U,V likely could be template arguments
514
593
    return (Tok->TokenText.size() != 1);
515
593
  }
516
2.56k
  return false;
517
3.15k
}
518
519
} // namespace format
520
} // namespace clang