Coverage Report

Created: 2022-07-16 07:03

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/include/clang/Tooling/Syntax/TokenBufferTokenManager.h
Line
Count
Source (jump to first uncovered line)
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
2.16k
      : Tokens(Tokens), LangOpts(LangOpts), SM(SourceMgr) {}
25
26
0
  static bool classof(const TokenManager *N) { return N->kind() == Kind; }
27
0
  llvm::StringLiteral kind() const override { return Kind; }
28
29
19.6k
  llvm::StringRef getText(Key I) const override {
30
19.6k
    const auto *Token = getToken(I);
31
19.6k
    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
19.6k
    if (Token->kind() == tok::eof)
36
0
      return "<eof>";
37
19.6k
    return Token->text(SM);
38
19.6k
  }
39
40
125k
  const syntax::Token *getToken(Key I) const {
41
125k
    return reinterpret_cast<const syntax::Token *>(I);
42
125k
  }
43
22.8k
  SourceManager &sourceManager() { return SM; }
44
84
  const SourceManager &sourceManager() const { return SM; }
45
37.3k
  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