1 //===- TokenBufferTokenManager.h -----------------------------------------===// 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 #ifndef LLVM_CLANG_TOOLING_SYNTAX_TOKEN_BUFFER_TOKEN_MANAGER_H 10 #define LLVM_CLANG_TOOLING_SYNTAX_TOKEN_BUFFER_TOKEN_MANAGER_H 11 12 #include "clang/Tooling/Syntax/TokenManager.h" 13 #include "clang/Tooling/Syntax/Tokens.h" 14 15 namespace clang { 16 namespace syntax { 17 18 /// A TokenBuffer-powered token manager. 19 /// It tracks the underlying token buffers, source manager, etc. 20 class TokenBufferTokenManager : public TokenManager { 21 public: 22 TokenBufferTokenManager(const TokenBuffer &Tokens, 23 const LangOptions &LangOpts, SourceManager &SourceMgr) 24 : Tokens(Tokens), LangOpts(LangOpts), SM(SourceMgr) {} 25 26 static bool classof(const TokenManager *N) { return N->kind() == Kind; } 27 llvm::StringLiteral kind() const override { return Kind; } 28 29 llvm::StringRef getText(Key I) const override { 30 const auto *Token = getToken(I); 31 assert(Token); 32 // Handle 'eof' separately, calling text() on it produces an empty string. 33 // FIXME: this special logic is for syntax::Leaf dump, move it when we 34 // have a direct way to retrive token kind in the syntax::Leaf. 35 if (Token->kind() == tok::eof) 36 return "<eof>"; 37 return Token->text(SM); 38 } 39 40 const syntax::Token *getToken(Key I) const { 41 return reinterpret_cast<const syntax::Token *>(I); 42 } 43 SourceManager &sourceManager() { return SM; } 44 const SourceManager &sourceManager() const { return SM; } 45 const TokenBuffer &tokenBuffer() const { return Tokens; } 46 47 private: 48 // This manager is powered by the TokenBuffer. 49 static constexpr llvm::StringLiteral Kind = "TokenBuffer"; 50 51 /// Add \p Buffer to the underlying source manager, tokenize it and store the 52 /// resulting tokens. Used exclusively in `FactoryImpl` to materialize tokens 53 /// that were not written in user code. 54 std::pair<FileID, ArrayRef<Token>> 55 lexBuffer(std::unique_ptr<llvm::MemoryBuffer> Buffer); 56 friend class FactoryImpl; 57 58 const TokenBuffer &Tokens; 59 const LangOptions &LangOpts; 60 61 /// The underlying source manager for the ExtraTokens. 62 SourceManager &SM; 63 /// IDs and storage for additional tokenized files. 64 llvm::DenseMap<FileID, std::vector<Token>> ExtraTokens; 65 }; 66 67 } // namespace syntax 68 } // namespace clang 69 70 #endif // LLVM_CLANG_TOOLING_SYNTAX_TOKEN_BUFFER_TOKEN_MANAGER_H 71