Martin Probst | c4a0dd4 | 2016-05-20 11:24:24 +0000 | [diff] [blame] | 1 | //===--- TokenAnalyzer.h - Analyze Token Streams ----------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | /// |
| 10 | /// \file |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 11 | /// This file declares an abstract TokenAnalyzer, and associated helper |
Martin Probst | c4a0dd4 | 2016-05-20 11:24:24 +0000 | [diff] [blame] | 12 | /// classes. TokenAnalyzer can be extended to generate replacements based on |
| 13 | /// an annotated and pre-processed token stream. |
| 14 | /// |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | #ifndef LLVM_CLANG_LIB_FORMAT_TOKENANALYZER_H |
| 18 | #define LLVM_CLANG_LIB_FORMAT_TOKENANALYZER_H |
| 19 | |
| 20 | #include "AffectedRangeManager.h" |
| 21 | #include "Encoding.h" |
| 22 | #include "FormatToken.h" |
| 23 | #include "FormatTokenLexer.h" |
| 24 | #include "TokenAnnotator.h" |
| 25 | #include "UnwrappedLineParser.h" |
| 26 | #include "clang/Basic/Diagnostic.h" |
| 27 | #include "clang/Basic/DiagnosticOptions.h" |
| 28 | #include "clang/Basic/FileManager.h" |
| 29 | #include "clang/Basic/SourceManager.h" |
| 30 | #include "clang/Format/Format.h" |
| 31 | #include "llvm/ADT/STLExtras.h" |
| 32 | #include "llvm/Support/Debug.h" |
| 33 | |
Martin Probst | c4a0dd4 | 2016-05-20 11:24:24 +0000 | [diff] [blame] | 34 | namespace clang { |
| 35 | namespace format { |
| 36 | |
| 37 | class Environment { |
| 38 | public: |
| 39 | Environment(SourceManager &SM, FileID ID, ArrayRef<CharSourceRange> Ranges) |
Eric Liu | 2e53808 | 2018-05-09 21:35:52 +0000 | [diff] [blame] | 40 | : SM(SM), ID(ID), CharRanges(Ranges.begin(), Ranges.end()), |
| 41 | FirstStartColumn(0), NextStartColumn(0), LastStartColumn(0) {} |
Martin Probst | c4a0dd4 | 2016-05-20 11:24:24 +0000 | [diff] [blame] | 42 | |
Krasimir Georgiev | 9ad83fe | 2017-10-30 14:01:50 +0000 | [diff] [blame] | 43 | // This sets up an virtual file system with file \p FileName containing the |
| 44 | // fragment \p Code. Assumes that \p Code starts at \p FirstStartColumn, |
| 45 | // that the next lines of \p Code should start at \p NextStartColumn, and |
| 46 | // that \p Code should end at \p LastStartColumn if it ends in newline. |
| 47 | // See also the documentation of clang::format::internal::reformat. |
Eric Liu | 2e53808 | 2018-05-09 21:35:52 +0000 | [diff] [blame] | 48 | Environment(StringRef Code, StringRef FileName, |
| 49 | ArrayRef<tooling::Range> Ranges, unsigned FirstStartColumn = 0, |
| 50 | unsigned NextStartColumn = 0, unsigned LastStartColumn = 0); |
Martin Probst | c4a0dd4 | 2016-05-20 11:24:24 +0000 | [diff] [blame] | 51 | |
| 52 | FileID getFileID() const { return ID; } |
| 53 | |
Martin Probst | c4a0dd4 | 2016-05-20 11:24:24 +0000 | [diff] [blame] | 54 | const SourceManager &getSourceManager() const { return SM; } |
| 55 | |
Eric Liu | 2e53808 | 2018-05-09 21:35:52 +0000 | [diff] [blame] | 56 | ArrayRef<CharSourceRange> getCharRanges() const { return CharRanges; } |
| 57 | |
Krasimir Georgiev | 9ad83fe | 2017-10-30 14:01:50 +0000 | [diff] [blame] | 58 | // Returns the column at which the fragment of code managed by this |
| 59 | // environment starts. |
| 60 | unsigned getFirstStartColumn() const { return FirstStartColumn; } |
| 61 | |
| 62 | // Returns the column at which subsequent lines of the fragment of code |
| 63 | // managed by this environment should start. |
| 64 | unsigned getNextStartColumn() const { return NextStartColumn; } |
| 65 | |
| 66 | // Returns the column at which the fragment of code managed by this |
| 67 | // environment should end if it ends in a newline. |
| 68 | unsigned getLastStartColumn() const { return LastStartColumn; } |
| 69 | |
Martin Probst | c4a0dd4 | 2016-05-20 11:24:24 +0000 | [diff] [blame] | 70 | private: |
Eric Liu | 2e53808 | 2018-05-09 21:35:52 +0000 | [diff] [blame] | 71 | // This is only set if constructed from string. |
| 72 | std::unique_ptr<SourceManagerForFile> VirtualSM; |
| 73 | |
| 74 | // This refers to either a SourceManager provided by users or VirtualSM |
| 75 | // created for a single file. |
Martin Probst | c4a0dd4 | 2016-05-20 11:24:24 +0000 | [diff] [blame] | 76 | SourceManager &SM; |
Eric Liu | 2e53808 | 2018-05-09 21:35:52 +0000 | [diff] [blame] | 77 | FileID ID; |
| 78 | |
| 79 | SmallVector<CharSourceRange, 8> CharRanges; |
Krasimir Georgiev | 9ad83fe | 2017-10-30 14:01:50 +0000 | [diff] [blame] | 80 | unsigned FirstStartColumn; |
| 81 | unsigned NextStartColumn; |
| 82 | unsigned LastStartColumn; |
Martin Probst | c4a0dd4 | 2016-05-20 11:24:24 +0000 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | class TokenAnalyzer : public UnwrappedLineConsumer { |
| 86 | public: |
| 87 | TokenAnalyzer(const Environment &Env, const FormatStyle &Style); |
| 88 | |
Krasimir Georgiev | 9ad83fe | 2017-10-30 14:01:50 +0000 | [diff] [blame] | 89 | std::pair<tooling::Replacements, unsigned> process(); |
Martin Probst | c4a0dd4 | 2016-05-20 11:24:24 +0000 | [diff] [blame] | 90 | |
| 91 | protected: |
Krasimir Georgiev | 9ad83fe | 2017-10-30 14:01:50 +0000 | [diff] [blame] | 92 | virtual std::pair<tooling::Replacements, unsigned> |
Martin Probst | c4a0dd4 | 2016-05-20 11:24:24 +0000 | [diff] [blame] | 93 | analyze(TokenAnnotator &Annotator, |
| 94 | SmallVectorImpl<AnnotatedLine *> &AnnotatedLines, |
Martin Probst | a9855af | 2016-09-02 14:29:48 +0000 | [diff] [blame] | 95 | FormatTokenLexer &Tokens) = 0; |
Martin Probst | c4a0dd4 | 2016-05-20 11:24:24 +0000 | [diff] [blame] | 96 | |
| 97 | void consumeUnwrappedLine(const UnwrappedLine &TheLine) override; |
| 98 | |
| 99 | void finishRun() override; |
| 100 | |
| 101 | FormatStyle Style; |
| 102 | // Stores Style, FileID and SourceManager etc. |
| 103 | const Environment &Env; |
| 104 | // AffectedRangeMgr stores ranges to be fixed. |
| 105 | AffectedRangeManager AffectedRangeMgr; |
| 106 | SmallVector<SmallVector<UnwrappedLine, 16>, 2> UnwrappedLines; |
| 107 | encoding::Encoding Encoding; |
| 108 | }; |
| 109 | |
| 110 | } // end namespace format |
| 111 | } // end namespace clang |
| 112 | |
| 113 | #endif |