aboutsummaryrefslogtreecommitdiff
path: root/contrib/llvm-project/clang/include/clang/Tooling/Syntax/TokenBufferTokenManager.h
blob: 6522af584e9abba561d0224149863b92056fd212 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//===- TokenBufferTokenManager.h  -----------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_TOOLING_SYNTAX_TOKEN_BUFFER_TOKEN_MANAGER_H
#define LLVM_CLANG_TOOLING_SYNTAX_TOKEN_BUFFER_TOKEN_MANAGER_H

#include "clang/Tooling/Syntax/TokenManager.h"
#include "clang/Tooling/Syntax/Tokens.h"

namespace clang {
namespace syntax {

/// A TokenBuffer-powered token manager.
/// It tracks the underlying token buffers, source manager, etc.
class TokenBufferTokenManager : public TokenManager {
public:
  TokenBufferTokenManager(const TokenBuffer &Tokens,
                          const LangOptions &LangOpts, SourceManager &SourceMgr)
      : Tokens(Tokens), LangOpts(LangOpts), SM(SourceMgr) {}

  static bool classof(const TokenManager *N) { return N->kind() == Kind; }
  llvm::StringLiteral kind() const override { return Kind; }

  llvm::StringRef getText(Key I) const override {
    const auto *Token = getToken(I);
    assert(Token);
    // Handle 'eof' separately, calling text() on it produces an empty string.
    // FIXME: this special logic is for syntax::Leaf dump, move it when we
    // have a direct way to retrive token kind in the syntax::Leaf.
    if (Token->kind() == tok::eof)
      return "<eof>";
    return Token->text(SM);
  }

  const syntax::Token *getToken(Key I) const {
    return reinterpret_cast<const syntax::Token *>(I);
  }
  SourceManager &sourceManager() { return SM; }
  const SourceManager &sourceManager() const { return SM; }
  const TokenBuffer &tokenBuffer() const { return Tokens; }

private:
  // This manager is powered by the TokenBuffer.
  static constexpr llvm::StringLiteral Kind = "TokenBuffer";

  /// Add \p Buffer to the underlying source manager, tokenize it and store the
  /// resulting tokens. Used exclusively in `FactoryImpl` to materialize tokens
  /// that were not written in user code.
  std::pair<FileID, ArrayRef<Token>>
  lexBuffer(std::unique_ptr<llvm::MemoryBuffer> Buffer);
  friend class FactoryImpl;

  const TokenBuffer &Tokens;
  const LangOptions &LangOpts;

  /// The underlying source manager for the ExtraTokens.
  SourceManager &SM;
  /// IDs and storage for additional tokenized files.
  llvm::DenseMap<FileID, std::vector<Token>> ExtraTokens;
};

} // namespace syntax
} // namespace clang

#endif // LLVM_CLANG_TOOLING_SYNTAX_TOKEN_BUFFER_TOKEN_MANAGER_H