QualifierAlignmentFixer.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //===--- LeftRightQualifierAlignmentFixer.h ------------------------------*- C++
  2. //-*-===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. ///
  10. /// \file
  11. /// This file declares LeftRightQualifierAlignmentFixer, a TokenAnalyzer that
  12. /// enforces either east or west const depending on the style.
  13. ///
  14. //===----------------------------------------------------------------------===//
  15. #ifndef LLVM_CLANG_LIB_FORMAT_QUALIFIERALIGNMENTFIXER_H
  16. #define LLVM_CLANG_LIB_FORMAT_QUALIFIERALIGNMENTFIXER_H
  17. #include "TokenAnalyzer.h"
  18. namespace clang {
  19. namespace format {
  20. typedef std::function<std::pair<tooling::Replacements, unsigned>(
  21. const Environment &)>
  22. AnalyzerPass;
  23. class QualifierAlignmentFixer : public TokenAnalyzer {
  24. // Left to Right ordering requires multiple passes
  25. SmallVector<AnalyzerPass, 8> Passes;
  26. StringRef &Code;
  27. ArrayRef<tooling::Range> Ranges;
  28. unsigned FirstStartColumn;
  29. unsigned NextStartColumn;
  30. unsigned LastStartColumn;
  31. StringRef FileName;
  32. public:
  33. QualifierAlignmentFixer(const Environment &Env, const FormatStyle &Style,
  34. StringRef &Code, ArrayRef<tooling::Range> Ranges,
  35. unsigned FirstStartColumn, unsigned NextStartColumn,
  36. unsigned LastStartColumn, StringRef FileName);
  37. std::pair<tooling::Replacements, unsigned>
  38. analyze(TokenAnnotator &Annotator,
  39. SmallVectorImpl<AnnotatedLine *> &AnnotatedLines,
  40. FormatTokenLexer &Tokens) override;
  41. static void PrepareLeftRightOrdering(const std::vector<std::string> &Order,
  42. std::vector<std::string> &LeftOrder,
  43. std::vector<std::string> &RightOrder,
  44. std::vector<tok::TokenKind> &Qualifiers);
  45. };
  46. class LeftRightQualifierAlignmentFixer : public TokenAnalyzer {
  47. std::string Qualifier;
  48. bool RightAlign;
  49. SmallVector<tok::TokenKind, 8> QualifierTokens;
  50. std::vector<tok::TokenKind> ConfiguredQualifierTokens;
  51. public:
  52. LeftRightQualifierAlignmentFixer(
  53. const Environment &Env, const FormatStyle &Style,
  54. const std::string &Qualifier,
  55. const std::vector<tok::TokenKind> &ConfiguredQualifierTokens,
  56. bool RightAlign);
  57. std::pair<tooling::Replacements, unsigned>
  58. analyze(TokenAnnotator &Annotator,
  59. SmallVectorImpl<AnnotatedLine *> &AnnotatedLines,
  60. FormatTokenLexer &Tokens) override;
  61. static tok::TokenKind getTokenFromQualifier(const std::string &Qualifier);
  62. const FormatToken *analyzeRight(const SourceManager &SourceMgr,
  63. const AdditionalKeywords &Keywords,
  64. tooling::Replacements &Fixes,
  65. const FormatToken *Tok,
  66. const std::string &Qualifier,
  67. tok::TokenKind QualifierType);
  68. const FormatToken *analyzeLeft(const SourceManager &SourceMgr,
  69. const AdditionalKeywords &Keywords,
  70. tooling::Replacements &Fixes,
  71. const FormatToken *Tok,
  72. const std::string &Qualifier,
  73. tok::TokenKind QualifierType);
  74. // is the Token a simple or qualifier type
  75. static bool isQualifierOrType(const FormatToken *Tok,
  76. const std::vector<tok::TokenKind> &Qualifiers);
  77. // is the Token likely a Macro
  78. static bool isPossibleMacro(const FormatToken *Tok);
  79. };
  80. } // end namespace format
  81. } // end namespace clang
  82. #endif