TokenAnalyzer.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //===--- TokenAnalyzer.h - Analyze Token Streams ----------------*- 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 declares an abstract TokenAnalyzer, and associated helper
  11. /// classes. TokenAnalyzer can be extended to generate replacements based on
  12. /// an annotated and pre-processed token stream.
  13. ///
  14. //===----------------------------------------------------------------------===//
  15. #ifndef LLVM_CLANG_LIB_FORMAT_TOKENANALYZER_H
  16. #define LLVM_CLANG_LIB_FORMAT_TOKENANALYZER_H
  17. #include "AffectedRangeManager.h"
  18. #include "Encoding.h"
  19. #include "FormatToken.h"
  20. #include "FormatTokenLexer.h"
  21. #include "TokenAnnotator.h"
  22. #include "UnwrappedLineParser.h"
  23. #include "clang/Basic/Diagnostic.h"
  24. #include "clang/Basic/DiagnosticOptions.h"
  25. #include "clang/Basic/FileManager.h"
  26. #include "clang/Basic/SourceManager.h"
  27. #include "clang/Format/Format.h"
  28. #include "llvm/ADT/STLExtras.h"
  29. #include "llvm/Support/Debug.h"
  30. namespace clang {
  31. namespace format {
  32. class Environment {
  33. public:
  34. // This sets up an virtual file system with file \p FileName containing the
  35. // fragment \p Code. Assumes that \p Code starts at \p FirstStartColumn,
  36. // that the next lines of \p Code should start at \p NextStartColumn, and
  37. // that \p Code should end at \p LastStartColumn if it ends in newline.
  38. // See also the documentation of clang::format::internal::reformat.
  39. Environment(StringRef Code, StringRef FileName,
  40. ArrayRef<tooling::Range> Ranges, unsigned FirstStartColumn = 0,
  41. unsigned NextStartColumn = 0, unsigned LastStartColumn = 0);
  42. FileID getFileID() const { return ID; }
  43. const SourceManager &getSourceManager() const { return SM; }
  44. ArrayRef<CharSourceRange> getCharRanges() const { return CharRanges; }
  45. // Returns the column at which the fragment of code managed by this
  46. // environment starts.
  47. unsigned getFirstStartColumn() const { return FirstStartColumn; }
  48. // Returns the column at which subsequent lines of the fragment of code
  49. // managed by this environment should start.
  50. unsigned getNextStartColumn() const { return NextStartColumn; }
  51. // Returns the column at which the fragment of code managed by this
  52. // environment should end if it ends in a newline.
  53. unsigned getLastStartColumn() const { return LastStartColumn; }
  54. private:
  55. // This is only set if constructed from string.
  56. std::unique_ptr<SourceManagerForFile> VirtualSM;
  57. // This refers to either a SourceManager provided by users or VirtualSM
  58. // created for a single file.
  59. SourceManager &SM;
  60. FileID ID;
  61. SmallVector<CharSourceRange, 8> CharRanges;
  62. unsigned FirstStartColumn;
  63. unsigned NextStartColumn;
  64. unsigned LastStartColumn;
  65. };
  66. class TokenAnalyzer : public UnwrappedLineConsumer {
  67. public:
  68. TokenAnalyzer(const Environment &Env, const FormatStyle &Style);
  69. std::pair<tooling::Replacements, unsigned> process();
  70. protected:
  71. virtual std::pair<tooling::Replacements, unsigned>
  72. analyze(TokenAnnotator &Annotator,
  73. SmallVectorImpl<AnnotatedLine *> &AnnotatedLines,
  74. FormatTokenLexer &Tokens) = 0;
  75. void consumeUnwrappedLine(const UnwrappedLine &TheLine) override;
  76. void finishRun() override;
  77. FormatStyle Style;
  78. // Stores Style, FileID and SourceManager etc.
  79. const Environment &Env;
  80. // AffectedRangeMgr stores ranges to be fixed.
  81. AffectedRangeManager AffectedRangeMgr;
  82. SmallVector<SmallVector<UnwrappedLine, 16>, 2> UnwrappedLines;
  83. encoding::Encoding Encoding;
  84. };
  85. } // end namespace format
  86. } // end namespace clang
  87. #endif