1 //===--- QualifierAlignmentFixer.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 QualifierAlignmentFixer, a TokenAnalyzer that 11 /// enforces either left or right const depending on the style. 12 /// 13 //===----------------------------------------------------------------------===// 14 15 #include "QualifierAlignmentFixer.h" 16 17 #define DEBUG_TYPE "format-qualifier-alignment-fixer" 18 19 namespace clang { 20 namespace format { 21 22 void addQualifierAlignmentFixerPasses(const FormatStyle &Style, 23 SmallVectorImpl<AnalyzerPass> &Passes) { 24 std::vector<std::string> LeftOrder; 25 std::vector<std::string> RightOrder; 26 std::vector<tok::TokenKind> ConfiguredQualifierTokens; 27 prepareLeftRightOrderingForQualifierAlignmentFixer( 28 Style.QualifierOrder, LeftOrder, RightOrder, ConfiguredQualifierTokens); 29 30 // Handle the left and right alignment separately. 31 for (const auto &Qualifier : LeftOrder) { 32 Passes.emplace_back( 33 [&, Qualifier, ConfiguredQualifierTokens](const Environment &Env) { 34 return LeftRightQualifierAlignmentFixer(Env, Style, Qualifier, 35 ConfiguredQualifierTokens, 36 /*RightAlign=*/false) 37 .process(); 38 }); 39 } 40 for (const auto &Qualifier : RightOrder) { 41 Passes.emplace_back( 42 [&, Qualifier, ConfiguredQualifierTokens](const Environment &Env) { 43 return LeftRightQualifierAlignmentFixer(Env, Style, Qualifier, 44 ConfiguredQualifierTokens, 45 /*RightAlign=*/true) 46 .process(); 47 }); 48 } 49 } 50 51 static void replaceToken(const SourceManager &SourceMgr, 52 tooling::Replacements &Fixes, 53 const CharSourceRange &Range, std::string NewText) { 54 auto Replacement = tooling::Replacement(SourceMgr, Range, NewText); 55 auto Err = Fixes.add(Replacement); 56 57 if (Err) { 58 llvm::errs() << "Error while rearranging Qualifier : " 59 << llvm::toString(std::move(Err)) << "\n"; 60 } 61 } 62 63 static void removeToken(const SourceManager &SourceMgr, 64 tooling::Replacements &Fixes, 65 const FormatToken *First) { 66 auto Range = CharSourceRange::getCharRange(First->getStartOfNonWhitespace(), 67 First->Tok.getEndLoc()); 68 replaceToken(SourceMgr, Fixes, Range, ""); 69 } 70 71 static void insertQualifierAfter(const SourceManager &SourceMgr, 72 tooling::Replacements &Fixes, 73 const FormatToken *First, 74 const std::string &Qualifier) { 75 auto Range = CharSourceRange::getCharRange(First->Tok.getLocation(), 76 First->Tok.getEndLoc()); 77 78 std::string NewText{}; 79 NewText += First->TokenText; 80 NewText += " " + Qualifier; 81 replaceToken(SourceMgr, Fixes, Range, NewText); 82 } 83 84 static void insertQualifierBefore(const SourceManager &SourceMgr, 85 tooling::Replacements &Fixes, 86 const FormatToken *First, 87 const std::string &Qualifier) { 88 auto Range = CharSourceRange::getCharRange(First->getStartOfNonWhitespace(), 89 First->Tok.getEndLoc()); 90 91 std::string NewText = " " + Qualifier + " "; 92 NewText += First->TokenText; 93 94 replaceToken(SourceMgr, Fixes, Range, NewText); 95 } 96 97 static bool endsWithSpace(const std::string &s) { 98 if (s.empty()) 99 return false; 100 return isspace(s.back()); 101 } 102 103 static bool startsWithSpace(const std::string &s) { 104 if (s.empty()) 105 return false; 106 return isspace(s.front()); 107 } 108 109 static void rotateTokens(const SourceManager &SourceMgr, 110 tooling::Replacements &Fixes, const FormatToken *First, 111 const FormatToken *Last, bool Left) { 112 auto *End = Last; 113 auto *Begin = First; 114 if (!Left) { 115 End = Last->Next; 116 Begin = First->Next; 117 } 118 119 std::string NewText; 120 // If we are rotating to the left we move the Last token to the front. 121 if (Left) { 122 NewText += Last->TokenText; 123 NewText += " "; 124 } 125 126 // Then move through the other tokens. 127 auto *Tok = Begin; 128 while (Tok != End) { 129 if (!NewText.empty() && !endsWithSpace(NewText)) 130 NewText += " "; 131 132 NewText += Tok->TokenText; 133 Tok = Tok->Next; 134 } 135 136 // If we are rotating to the right we move the first token to the back. 137 if (!Left) { 138 if (!NewText.empty() && !startsWithSpace(NewText)) 139 NewText += " "; 140 NewText += First->TokenText; 141 } 142 143 auto Range = CharSourceRange::getCharRange(First->getStartOfNonWhitespace(), 144 Last->Tok.getEndLoc()); 145 146 replaceToken(SourceMgr, Fixes, Range, NewText); 147 } 148 149 static bool 150 isConfiguredQualifier(const FormatToken *const Tok, 151 const std::vector<tok::TokenKind> &Qualifiers) { 152 return Tok && llvm::is_contained(Qualifiers, Tok->Tok.getKind()); 153 } 154 155 static bool isQualifier(const FormatToken *const Tok) { 156 if (!Tok) 157 return false; 158 159 switch (Tok->Tok.getKind()) { 160 case tok::kw_const: 161 case tok::kw_volatile: 162 case tok::kw_static: 163 case tok::kw_inline: 164 case tok::kw_constexpr: 165 case tok::kw_restrict: 166 case tok::kw_friend: 167 return true; 168 default: 169 return false; 170 } 171 } 172 173 const FormatToken *LeftRightQualifierAlignmentFixer::analyzeRight( 174 const SourceManager &SourceMgr, const AdditionalKeywords &Keywords, 175 tooling::Replacements &Fixes, const FormatToken *const Tok, 176 const std::string &Qualifier, tok::TokenKind QualifierType) { 177 // We only need to think about streams that begin with a qualifier. 178 if (Tok->isNot(QualifierType)) 179 return Tok; 180 // Don't concern yourself if nothing follows the qualifier. 181 if (!Tok->Next) 182 return Tok; 183 184 // Skip qualifiers to the left to find what preceeds the qualifiers. 185 // Use isQualifier rather than isConfiguredQualifier to cover all qualifiers. 186 const FormatToken *PreviousCheck = Tok->getPreviousNonComment(); 187 while (isQualifier(PreviousCheck)) 188 PreviousCheck = PreviousCheck->getPreviousNonComment(); 189 190 // Examples given in order of ['type', 'const', 'volatile'] 191 const bool IsRightQualifier = PreviousCheck && [PreviousCheck]() { 192 // The cases: 193 // `Foo() const` -> `Foo() const` 194 // `Foo() const final` -> `Foo() const final` 195 // `Foo() const override` -> `Foo() const final` 196 // `Foo() const volatile override` -> `Foo() const volatile override` 197 // `Foo() volatile const final` -> `Foo() const volatile final` 198 if (PreviousCheck->is(tok::r_paren)) 199 return true; 200 201 // The cases: 202 // `struct {} volatile const a;` -> `struct {} const volatile a;` 203 // `class {} volatile const a;` -> `class {} const volatile a;` 204 if (PreviousCheck->is(tok::r_brace)) 205 return true; 206 207 // The case: 208 // `template <class T> const Bar Foo()` -> 209 // `template <class T> Bar const Foo()` 210 // The cases: 211 // `Foo<int> const foo` -> `Foo<int> const foo` 212 // `Foo<int> volatile const` -> `Foo<int> const volatile` 213 // The case: 214 // ``` 215 // template <class T> 216 // requires Concept1<T> && requires Concept2<T> 217 // const Foo f(); 218 // ``` 219 // -> 220 // ``` 221 // template <class T> 222 // requires Concept1<T> && requires Concept2<T> 223 // Foo const f(); 224 // ``` 225 if (PreviousCheck->is(TT_TemplateCloser)) { 226 // If the token closes a template<> or requires clause, then it is a left 227 // qualifier and should be moved to the right. 228 return !(PreviousCheck->ClosesTemplateDeclaration || 229 PreviousCheck->ClosesRequiresClause); 230 } 231 232 // The case `Foo* const` -> `Foo* const` 233 // The case `Foo* volatile const` -> `Foo* const volatile` 234 // The case `int32_t const` -> `int32_t const` 235 // The case `auto volatile const` -> `auto const volatile` 236 if (PreviousCheck->isOneOf(TT_PointerOrReference, tok::identifier, 237 tok::kw_auto)) { 238 return true; 239 } 240 241 return false; 242 }(); 243 244 // Find the last qualifier to the right. 245 const FormatToken *LastQual = Tok; 246 while (isQualifier(LastQual->getNextNonComment())) 247 LastQual = LastQual->getNextNonComment(); 248 249 // If this qualifier is to the right of a type or pointer do a partial sort 250 // and return. 251 if (IsRightQualifier) { 252 if (LastQual != Tok) 253 rotateTokens(SourceMgr, Fixes, Tok, LastQual, /*Left=*/false); 254 return Tok; 255 } 256 257 const FormatToken *TypeToken = LastQual->getNextNonComment(); 258 if (!TypeToken) 259 return Tok; 260 261 // Stay safe and don't move past macros, also don't bother with sorting. 262 if (isPossibleMacro(TypeToken)) 263 return Tok; 264 265 const bool IsCpp = Style.isCpp(); 266 267 // The case `const long long int volatile` -> `long long int const volatile` 268 // The case `long const long int volatile` -> `long long int const volatile` 269 // The case `long long volatile int const` -> `long long int const volatile` 270 // The case `const long long volatile int` -> `long long int const volatile` 271 if (TypeToken->isTypeName(IsCpp)) { 272 // The case `const decltype(foo)` -> `const decltype(foo)` 273 // The case `const typeof(foo)` -> `const typeof(foo)` 274 // The case `const _Atomic(foo)` -> `const _Atomic(foo)` 275 if (TypeToken->isOneOf(tok::kw_decltype, tok::kw_typeof, tok::kw__Atomic)) 276 return Tok; 277 278 const FormatToken *LastSimpleTypeSpecifier = TypeToken; 279 while (isQualifierOrType(LastSimpleTypeSpecifier->getNextNonComment(), 280 IsCpp)) { 281 LastSimpleTypeSpecifier = LastSimpleTypeSpecifier->getNextNonComment(); 282 } 283 284 rotateTokens(SourceMgr, Fixes, Tok, LastSimpleTypeSpecifier, 285 /*Left=*/false); 286 return LastSimpleTypeSpecifier; 287 } 288 289 // The case `unsigned short const` -> `unsigned short const` 290 // The case: 291 // `unsigned short volatile const` -> `unsigned short const volatile` 292 if (PreviousCheck && PreviousCheck->isTypeName(IsCpp)) { 293 if (LastQual != Tok) 294 rotateTokens(SourceMgr, Fixes, Tok, LastQual, /*Left=*/false); 295 return Tok; 296 } 297 298 // Skip the typename keyword. 299 // The case `const typename C::type` -> `typename C::type const` 300 if (TypeToken->is(tok::kw_typename)) 301 TypeToken = TypeToken->getNextNonComment(); 302 303 // Skip the initial :: of a global-namespace type. 304 // The case `const ::...` -> `::... const` 305 if (TypeToken->is(tok::coloncolon)) { 306 // The case `const ::template Foo...` -> `::template Foo... const` 307 TypeToken = TypeToken->getNextNonComment(); 308 if (TypeToken && TypeToken->is(tok::kw_template)) 309 TypeToken = TypeToken->getNextNonComment(); 310 } 311 312 // Don't change declarations such as 313 // `foo(const struct Foo a);` -> `foo(const struct Foo a);` 314 // as they would currently change code such as 315 // `const struct my_struct_t {} my_struct;` -> `struct my_struct_t const {} 316 // my_struct;` 317 if (TypeToken->isOneOf(tok::kw_struct, tok::kw_class)) 318 return Tok; 319 320 if (TypeToken->isOneOf(tok::kw_auto, tok::identifier)) { 321 // The case `const auto` -> `auto const` 322 // The case `const Foo` -> `Foo const` 323 // The case `const ::Foo` -> `::Foo const` 324 // The case `const Foo *` -> `Foo const *` 325 // The case `const Foo &` -> `Foo const &` 326 // The case `const Foo &&` -> `Foo const &&` 327 // The case `const std::Foo &&` -> `std::Foo const &&` 328 // The case `const std::Foo<T> &&` -> `std::Foo<T> const &&` 329 // The case `const ::template Foo` -> `::template Foo const` 330 // The case `const T::template Foo` -> `T::template Foo const` 331 const FormatToken *Next = nullptr; 332 while ((Next = TypeToken->getNextNonComment()) && 333 (Next->is(TT_TemplateOpener) || 334 Next->startsSequence(tok::coloncolon, tok::identifier) || 335 Next->startsSequence(tok::coloncolon, tok::kw_template, 336 tok::identifier))) { 337 if (Next->is(TT_TemplateOpener)) { 338 assert(Next->MatchingParen && "Missing template closer"); 339 TypeToken = Next->MatchingParen; 340 } else if (Next->startsSequence(tok::coloncolon, tok::identifier)) { 341 TypeToken = Next->getNextNonComment(); 342 } else { 343 TypeToken = Next->getNextNonComment()->getNextNonComment(); 344 } 345 } 346 347 if (Next->is(tok::kw_auto)) 348 TypeToken = Next; 349 350 // Place the Qualifier at the end of the list of qualifiers. 351 while (isQualifier(TypeToken->getNextNonComment())) { 352 // The case `volatile Foo::iter const` -> `Foo::iter const volatile` 353 TypeToken = TypeToken->getNextNonComment(); 354 } 355 356 insertQualifierAfter(SourceMgr, Fixes, TypeToken, Qualifier); 357 // Remove token and following whitespace. 358 auto Range = CharSourceRange::getCharRange( 359 Tok->getStartOfNonWhitespace(), Tok->Next->getStartOfNonWhitespace()); 360 replaceToken(SourceMgr, Fixes, Range, ""); 361 } 362 363 return Tok; 364 } 365 366 const FormatToken *LeftRightQualifierAlignmentFixer::analyzeLeft( 367 const SourceManager &SourceMgr, const AdditionalKeywords &Keywords, 368 tooling::Replacements &Fixes, const FormatToken *const Tok, 369 const std::string &Qualifier, tok::TokenKind QualifierType) { 370 // We only need to think about streams that begin with a qualifier. 371 if (Tok->isNot(QualifierType)) 372 return Tok; 373 // Don't concern yourself if nothing preceeds the qualifier. 374 if (!Tok->getPreviousNonComment()) 375 return Tok; 376 377 // Skip qualifiers to the left to find what preceeds the qualifiers. 378 const FormatToken *TypeToken = Tok->getPreviousNonComment(); 379 while (isQualifier(TypeToken)) 380 TypeToken = TypeToken->getPreviousNonComment(); 381 382 // For left qualifiers preceeded by nothing, a template declaration, or *,&,&& 383 // we only perform sorting. 384 if (!TypeToken || TypeToken->isPointerOrReference() || 385 TypeToken->ClosesRequiresClause || TypeToken->ClosesTemplateDeclaration) { 386 387 // Don't sort past a non-configured qualifier token. 388 const FormatToken *FirstQual = Tok; 389 while (isConfiguredQualifier(FirstQual->getPreviousNonComment(), 390 ConfiguredQualifierTokens)) { 391 FirstQual = FirstQual->getPreviousNonComment(); 392 } 393 394 if (FirstQual != Tok) 395 rotateTokens(SourceMgr, Fixes, FirstQual, Tok, /*Left=*/true); 396 return Tok; 397 } 398 399 // Stay safe and don't move past macros, also don't bother with sorting. 400 if (isPossibleMacro(TypeToken)) 401 return Tok; 402 403 // Examples given in order of ['const', 'volatile', 'type'] 404 405 // The case `volatile long long int const` -> `const volatile long long int` 406 // The case `volatile long long const int` -> `const volatile long long int` 407 // The case `const long long volatile int` -> `const volatile long long int` 408 // The case `long volatile long int const` -> `const volatile long long int` 409 if (const bool IsCpp = Style.isCpp(); TypeToken->isTypeName(IsCpp)) { 410 const FormatToken *LastSimpleTypeSpecifier = TypeToken; 411 while (isConfiguredQualifierOrType( 412 LastSimpleTypeSpecifier->getPreviousNonComment(), 413 ConfiguredQualifierTokens, IsCpp)) { 414 LastSimpleTypeSpecifier = 415 LastSimpleTypeSpecifier->getPreviousNonComment(); 416 } 417 418 rotateTokens(SourceMgr, Fixes, LastSimpleTypeSpecifier, Tok, 419 /*Left=*/true); 420 return Tok; 421 } 422 423 if (TypeToken->isOneOf(tok::kw_auto, tok::identifier, TT_TemplateCloser)) { 424 const auto IsStartOfType = [](const FormatToken *const Tok) -> bool { 425 if (!Tok) 426 return true; 427 428 // A template closer is not the start of a type. 429 // The case `?<> const` -> `const ?<>` 430 if (Tok->is(TT_TemplateCloser)) 431 return false; 432 433 const FormatToken *const Previous = Tok->getPreviousNonComment(); 434 if (!Previous) 435 return true; 436 437 // An identifier preceeded by :: is not the start of a type. 438 // The case `?::Foo const` -> `const ?::Foo` 439 if (Tok->is(tok::identifier) && Previous->is(tok::coloncolon)) 440 return false; 441 442 const FormatToken *const PrePrevious = Previous->getPreviousNonComment(); 443 // An identifier preceeded by ::template is not the start of a type. 444 // The case `?::template Foo const` -> `const ?::template Foo` 445 if (Tok->is(tok::identifier) && Previous->is(tok::kw_template) && 446 PrePrevious && PrePrevious->is(tok::coloncolon)) { 447 return false; 448 } 449 450 if (Tok->endsSequence(tok::kw_auto, tok::identifier)) 451 return false; 452 453 return true; 454 }; 455 456 while (!IsStartOfType(TypeToken)) { 457 // The case `?<>` 458 if (TypeToken->is(TT_TemplateCloser)) { 459 assert(TypeToken->MatchingParen && "Missing template opener"); 460 TypeToken = TypeToken->MatchingParen->getPreviousNonComment(); 461 } else { 462 // The cases 463 // `::Foo` 464 // `?>::Foo` 465 // `?Bar::Foo` 466 // `::template Foo` 467 // `?>::template Foo` 468 // `?Bar::template Foo` 469 if (TypeToken->getPreviousNonComment()->is(tok::kw_template)) 470 TypeToken = TypeToken->getPreviousNonComment(); 471 472 const FormatToken *const ColonColon = 473 TypeToken->getPreviousNonComment(); 474 const FormatToken *const PreColonColon = 475 ColonColon->getPreviousNonComment(); 476 if (PreColonColon && 477 PreColonColon->isOneOf(TT_TemplateCloser, tok::identifier)) { 478 TypeToken = PreColonColon; 479 } else { 480 TypeToken = ColonColon; 481 } 482 } 483 } 484 485 assert(TypeToken && "Should be auto or identifier"); 486 487 // Place the Qualifier at the start of the list of qualifiers. 488 const FormatToken *Previous = nullptr; 489 while ((Previous = TypeToken->getPreviousNonComment()) && 490 (isConfiguredQualifier(Previous, ConfiguredQualifierTokens) || 491 Previous->is(tok::kw_typename))) { 492 // The case `volatile Foo::iter const` -> `const volatile Foo::iter` 493 // The case `typename C::type const` -> `const typename C::type` 494 TypeToken = Previous; 495 } 496 497 // Don't change declarations such as 498 // `foo(struct Foo const a);` -> `foo(struct Foo const a);` 499 if (!Previous || !Previous->isOneOf(tok::kw_struct, tok::kw_class)) { 500 insertQualifierBefore(SourceMgr, Fixes, TypeToken, Qualifier); 501 removeToken(SourceMgr, Fixes, Tok); 502 } 503 } 504 505 return Tok; 506 } 507 508 tok::TokenKind LeftRightQualifierAlignmentFixer::getTokenFromQualifier( 509 const std::string &Qualifier) { 510 // Don't let 'type' be an identifier, but steal typeof token. 511 return llvm::StringSwitch<tok::TokenKind>(Qualifier) 512 .Case("type", tok::kw_typeof) 513 .Case("const", tok::kw_const) 514 .Case("volatile", tok::kw_volatile) 515 .Case("static", tok::kw_static) 516 .Case("inline", tok::kw_inline) 517 .Case("constexpr", tok::kw_constexpr) 518 .Case("restrict", tok::kw_restrict) 519 .Case("friend", tok::kw_friend) 520 .Default(tok::identifier); 521 } 522 523 LeftRightQualifierAlignmentFixer::LeftRightQualifierAlignmentFixer( 524 const Environment &Env, const FormatStyle &Style, 525 const std::string &Qualifier, 526 const std::vector<tok::TokenKind> &QualifierTokens, bool RightAlign) 527 : TokenAnalyzer(Env, Style), Qualifier(Qualifier), RightAlign(RightAlign), 528 ConfiguredQualifierTokens(QualifierTokens) {} 529 530 std::pair<tooling::Replacements, unsigned> 531 LeftRightQualifierAlignmentFixer::analyze( 532 TokenAnnotator & /*Annotator*/, 533 SmallVectorImpl<AnnotatedLine *> &AnnotatedLines, 534 FormatTokenLexer &Tokens) { 535 tooling::Replacements Fixes; 536 AffectedRangeMgr.computeAffectedLines(AnnotatedLines); 537 fixQualifierAlignment(AnnotatedLines, Tokens, Fixes); 538 return {Fixes, 0}; 539 } 540 541 void LeftRightQualifierAlignmentFixer::fixQualifierAlignment( 542 SmallVectorImpl<AnnotatedLine *> &AnnotatedLines, FormatTokenLexer &Tokens, 543 tooling::Replacements &Fixes) { 544 const AdditionalKeywords &Keywords = Tokens.getKeywords(); 545 const SourceManager &SourceMgr = Env.getSourceManager(); 546 tok::TokenKind QualifierToken = getTokenFromQualifier(Qualifier); 547 assert(QualifierToken != tok::identifier && "Unrecognised Qualifier"); 548 549 for (AnnotatedLine *Line : AnnotatedLines) { 550 fixQualifierAlignment(Line->Children, Tokens, Fixes); 551 if (!Line->Affected || Line->InPPDirective) 552 continue; 553 FormatToken *First = Line->First; 554 assert(First); 555 if (First->Finalized) 556 continue; 557 558 const auto *Last = Line->Last; 559 560 for (const auto *Tok = First; Tok && Tok != Last && Tok->Next; 561 Tok = Tok->Next) { 562 if (Tok->MustBreakBefore) 563 break; 564 if (Tok->is(tok::comment)) 565 continue; 566 if (RightAlign) { 567 Tok = analyzeRight(SourceMgr, Keywords, Fixes, Tok, Qualifier, 568 QualifierToken); 569 } else { 570 Tok = analyzeLeft(SourceMgr, Keywords, Fixes, Tok, Qualifier, 571 QualifierToken); 572 } 573 } 574 } 575 } 576 577 void prepareLeftRightOrderingForQualifierAlignmentFixer( 578 const std::vector<std::string> &Order, std::vector<std::string> &LeftOrder, 579 std::vector<std::string> &RightOrder, 580 std::vector<tok::TokenKind> &Qualifiers) { 581 582 // Depending on the position of type in the order you need 583 // To iterate forward or backward through the order list as qualifier 584 // can push through each other. 585 // The Order list must define the position of "type" to signify 586 assert(llvm::is_contained(Order, "type") && 587 "QualifierOrder must contain type"); 588 // Split the Order list by type and reverse the left side. 589 590 bool left = true; 591 for (const auto &s : Order) { 592 if (s == "type") { 593 left = false; 594 continue; 595 } 596 597 tok::TokenKind QualifierToken = 598 LeftRightQualifierAlignmentFixer::getTokenFromQualifier(s); 599 if (QualifierToken != tok::kw_typeof && QualifierToken != tok::identifier) 600 Qualifiers.push_back(QualifierToken); 601 602 if (left) { 603 // Reverse the order for left aligned items. 604 LeftOrder.insert(LeftOrder.begin(), s); 605 } else { 606 RightOrder.push_back(s); 607 } 608 } 609 } 610 611 bool LeftRightQualifierAlignmentFixer::isQualifierOrType(const FormatToken *Tok, 612 bool IsCpp) { 613 return Tok && 614 (Tok->isTypeName(IsCpp) || Tok->is(tok::kw_auto) || isQualifier(Tok)); 615 } 616 617 bool LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType( 618 const FormatToken *Tok, const std::vector<tok::TokenKind> &Qualifiers, 619 bool IsCpp) { 620 return Tok && (Tok->isTypeName(IsCpp) || Tok->is(tok::kw_auto) || 621 isConfiguredQualifier(Tok, Qualifiers)); 622 } 623 624 // If a token is an identifier and it's upper case, it could 625 // be a macro and hence we need to be able to ignore it. 626 bool LeftRightQualifierAlignmentFixer::isPossibleMacro(const FormatToken *Tok) { 627 if (!Tok) 628 return false; 629 if (Tok->isNot(tok::identifier)) 630 return false; 631 if (Tok->TokenText.upper() == Tok->TokenText.str()) { 632 // T,K,U,V likely could be template arguments 633 return Tok->TokenText.size() != 1; 634 } 635 return false; 636 } 637 638 } // namespace format 639 } // namespace clang 640