clang 20.0.0git
TokenBufferTokenManager.h
Go to the documentation of this file.
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
14
15namespace clang {
16namespace syntax {
17
18/// A TokenBuffer-powered token manager.
19/// It tracks the underlying token buffers, source manager, etc.
21public:
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
47private:
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.
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
#define SM(sm)
Definition: Cuda.cpp:83
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:476
This class handles loading and caching of source files into memory.
Exposes private syntax tree APIs required to implement node synthesis.
Definition: Synthesis.cpp:18
A TokenBuffer-powered token manager.
const syntax::Token * getToken(Key I) const
static bool classof(const TokenManager *N)
TokenBufferTokenManager(const TokenBuffer &Tokens, const LangOptions &LangOpts, SourceManager &SourceMgr)
llvm::StringLiteral kind() const override
Describes what the exact class kind of the TokenManager is.
llvm::StringRef getText(Key I) const override
const SourceManager & sourceManager() const
A list of tokens obtained by preprocessing a text buffer and operations to map between the expanded a...
Definition: Tokens.h:174
Defines interfaces for operating "Token" in the clang syntax-tree.
Definition: TokenManager.h:29
uintptr_t Key
A key to identify a specific token.
Definition: TokenManager.h:40
virtual llvm::StringLiteral kind() const =0
Describes what the exact class kind of the TokenManager is.
A token coming directly from a file or from a macro invocation.
Definition: Tokens.h:103
llvm::StringRef text(const SourceManager &SM) const
Get the substring covered by the token.
Definition: Tokens.cpp:154
tok::TokenKind kind() const
Definition: Tokens.h:109
The JSON file list parser is used to communicate input to InstallAPI.