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 11 /// \brief This file declares an abstract TokenAnalyzer, and associated helper 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 34 #define DEBUG_TYPE "format-formatter" 35 36 namespace clang { 37 namespace format { 38 39 class Environment { 40 public: Environment(SourceManager & SM,FileID ID,ArrayRef<CharSourceRange> Ranges)41 Environment(SourceManager &SM, FileID ID, ArrayRef<CharSourceRange> Ranges) 42 : ID(ID), CharRanges(Ranges.begin(), Ranges.end()), SM(SM) {} 43 Environment(FileID ID,std::unique_ptr<FileManager> FileMgr,std::unique_ptr<SourceManager> VirtualSM,std::unique_ptr<DiagnosticsEngine> Diagnostics,const std::vector<CharSourceRange> & CharRanges)44 Environment(FileID ID, std::unique_ptr<FileManager> FileMgr, 45 std::unique_ptr<SourceManager> VirtualSM, 46 std::unique_ptr<DiagnosticsEngine> Diagnostics, 47 const std::vector<CharSourceRange> &CharRanges) 48 : ID(ID), CharRanges(CharRanges.begin(), CharRanges.end()), 49 SM(*VirtualSM), FileMgr(std::move(FileMgr)), 50 VirtualSM(std::move(VirtualSM)), Diagnostics(std::move(Diagnostics)) {} 51 52 // This sets up an virtual file system with file \p FileName containing \p 53 // Code. 54 static std::unique_ptr<Environment> 55 CreateVirtualEnvironment(StringRef Code, StringRef FileName, 56 ArrayRef<tooling::Range> Ranges); 57 getFileID()58 FileID getFileID() const { return ID; } 59 getFileName()60 StringRef getFileName() const { return FileName; } 61 getCharRanges()62 ArrayRef<CharSourceRange> getCharRanges() const { return CharRanges; } 63 getSourceManager()64 const SourceManager &getSourceManager() const { return SM; } 65 66 private: 67 FileID ID; 68 StringRef FileName; 69 SmallVector<CharSourceRange, 8> CharRanges; 70 SourceManager &SM; 71 72 // The order of these fields are important - they should be in the same order 73 // as they are created in `CreateVirtualEnvironment` so that they can be 74 // deleted in the reverse order as they are created. 75 std::unique_ptr<FileManager> FileMgr; 76 std::unique_ptr<SourceManager> VirtualSM; 77 std::unique_ptr<DiagnosticsEngine> Diagnostics; 78 }; 79 80 class TokenAnalyzer : public UnwrappedLineConsumer { 81 public: 82 TokenAnalyzer(const Environment &Env, const FormatStyle &Style); 83 84 tooling::Replacements process(); 85 86 protected: 87 virtual tooling::Replacements 88 analyze(TokenAnnotator &Annotator, 89 SmallVectorImpl<AnnotatedLine *> &AnnotatedLines, 90 FormatTokenLexer &Tokens, tooling::Replacements &Result) = 0; 91 92 void consumeUnwrappedLine(const UnwrappedLine &TheLine) override; 93 94 void finishRun() override; 95 96 FormatStyle Style; 97 // Stores Style, FileID and SourceManager etc. 98 const Environment &Env; 99 // AffectedRangeMgr stores ranges to be fixed. 100 AffectedRangeManager AffectedRangeMgr; 101 SmallVector<SmallVector<UnwrappedLine, 16>, 2> UnwrappedLines; 102 encoding::Encoding Encoding; 103 }; 104 105 } // end namespace format 106 } // end namespace clang 107 108 #endif 109