| 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 | |
| 16 | #ifndef LLVM_CLANG_LIB_FORMAT_TOKENANALYZER_H |
| 17 | #define LLVM_CLANG_LIB_FORMAT_TOKENANALYZER_H |
| 18 | |
| 19 | #include "AffectedRangeManager.h" |
| 20 | #include "FormatTokenLexer.h" |
| 21 | #include "TokenAnnotator.h" |
| 22 | |
| 23 | namespace clang { |
| 24 | namespace format { |
| 25 | |
| 26 | class Environment { |
| 27 | public: |
| 28 | // This sets up an virtual file system with file \p FileName containing the |
| 29 | // fragment \p Code. Assumes that \p Code starts at \p FirstStartColumn, |
| 30 | // that the next lines of \p Code should start at \p NextStartColumn, and |
| 31 | // that \p Code should end at \p LastStartColumn if it ends in newline. |
| 32 | // See also the documentation of clang::format::internal::reformat. |
| 33 | Environment(StringRef Code, StringRef FileName, unsigned FirstStartColumn = 0, |
| 34 | unsigned NextStartColumn = 0, unsigned LastStartColumn = 0); |
| 35 | |
| 36 | FileID getFileID() const { return ID; } |
| 37 | |
| 38 | SourceManager &getSourceManager() const { return SM; } |
| 39 | |
| 40 | ArrayRef<CharSourceRange> getCharRanges() const { return CharRanges; } |
| 41 | |
| 42 | // Returns the column at which the fragment of code managed by this |
| 43 | // environment starts. |
| 44 | unsigned getFirstStartColumn() const { return FirstStartColumn; } |
| 45 | |
| 46 | // Returns the column at which subsequent lines of the fragment of code |
| 47 | // managed by this environment should start. |
| 48 | unsigned getNextStartColumn() const { return NextStartColumn; } |
| 49 | |
| 50 | // Returns the column at which the fragment of code managed by this |
| 51 | // environment should end if it ends in a newline. |
| 52 | unsigned getLastStartColumn() const { return LastStartColumn; } |
| 53 | |
| 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 | |
| 62 | private: |
| 63 | // This is only set if constructed from string. |
| 64 | std::unique_ptr<SourceManagerForFile> VirtualSM; |
| 65 | |
| 66 | // This refers to either a SourceManager provided by users or VirtualSM |
| 67 | // created for a single file. |
| 68 | SourceManager &SM; |
| 69 | FileID ID; |
| 70 | |
| 71 | SmallVector<CharSourceRange, 8> CharRanges; |
| 72 | unsigned FirstStartColumn; |
| 73 | unsigned NextStartColumn; |
| 74 | unsigned LastStartColumn; |
| 75 | }; |
| 76 | |
| 77 | class TokenAnalyzer : public UnwrappedLineConsumer { |
| 78 | public: |
| 79 | TokenAnalyzer(const Environment &Env, const FormatStyle &Style); |
| 80 | |
| 81 | std::pair<tooling::Replacements, unsigned> |
| 82 | process(bool SkipAnnotation = false); |
| 83 | |
| 84 | protected: |
| 85 | virtual std::pair<tooling::Replacements, unsigned> |
| 86 | analyze(TokenAnnotator &Annotator, |
| 87 | SmallVectorImpl<AnnotatedLine *> &AnnotatedLines, |
| 88 | FormatTokenLexer &Tokens) = 0; |
| 89 | |
| 90 | void consumeUnwrappedLine(const UnwrappedLine &TheLine) override; |
| 91 | |
| 92 | void finishRun() override; |
| 93 | |
| 94 | FormatStyle Style; |
| 95 | LangOptions LangOpts; |
| 96 | // Stores Style, FileID and SourceManager etc. |
| 97 | const Environment &Env; |
| 98 | // AffectedRangeMgr stores ranges to be fixed. |
| 99 | AffectedRangeManager AffectedRangeMgr; |
| 100 | SmallVector<SmallVector<UnwrappedLine, 16>, 2> UnwrappedLines; |
| 101 | encoding::Encoding Encoding; |
| 102 | }; |
| 103 | |
| 104 | } // end namespace format |
| 105 | } // end namespace clang |
| 106 | |
| 107 | #endif |
| 108 | |