TokenAnalyzer.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. #include <memory>
  31. namespace clang {
  32. namespace format {
  33. class Environment {
  34. public:
  35. // This sets up an virtual file system with file \p FileName containing the
  36. // fragment \p Code. Assumes that \p Code starts at \p FirstStartColumn,
  37. // that the next lines of \p Code should start at \p NextStartColumn, and
  38. // that \p Code should end at \p LastStartColumn if it ends in newline.
  39. // See also the documentation of clang::format::internal::reformat.
  40. Environment(StringRef Code, StringRef FileName, 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. // Returns nullptr and prints a diagnostic to stderr if the environment
  55. // can't be created.
  56. static std::unique_ptr<Environment> make(StringRef Code, StringRef FileName,
  57. ArrayRef<tooling::Range> Ranges,
  58. unsigned FirstStartColumn = 0,
  59. unsigned NextStartColumn = 0,
  60. unsigned LastStartColumn = 0);
  61. private:
  62. // This is only set if constructed from string.
  63. std::unique_ptr<SourceManagerForFile> VirtualSM;
  64. // This refers to either a SourceManager provided by users or VirtualSM
  65. // created for a single file.
  66. SourceManager &SM;
  67. FileID ID;
  68. SmallVector<CharSourceRange, 8> CharRanges;
  69. unsigned FirstStartColumn;
  70. unsigned NextStartColumn;
  71. unsigned LastStartColumn;
  72. };
  73. class TokenAnalyzer : public UnwrappedLineConsumer {
  74. public:
  75. TokenAnalyzer(const Environment &Env, const FormatStyle &Style);
  76. std::pair<tooling::Replacements, unsigned>
  77. process(bool SkipAnnotation = false);
  78. protected:
  79. virtual std::pair<tooling::Replacements, unsigned>
  80. analyze(TokenAnnotator &Annotator,
  81. SmallVectorImpl<AnnotatedLine *> &AnnotatedLines,
  82. FormatTokenLexer &Tokens) = 0;
  83. void consumeUnwrappedLine(const UnwrappedLine &TheLine) override;
  84. void finishRun() override;
  85. FormatStyle Style;
  86. // Stores Style, FileID and SourceManager etc.
  87. const Environment &Env;
  88. // AffectedRangeMgr stores ranges to be fixed.
  89. AffectedRangeManager AffectedRangeMgr;
  90. SmallVector<SmallVector<UnwrappedLine, 16>, 2> UnwrappedLines;
  91. encoding::Encoding Encoding;
  92. };
  93. } // end namespace format
  94. } // end namespace clang
  95. #endif