Coverage Report

Created: 2026-06-15 06:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/wabt/src/token.cc
Line
Count
Source
1
/*
2
 * Copyright 2017 WebAssembly Community Group participants
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
17
#include "wabt/token.h"
18
19
namespace wabt {
20
21
3.92M
const char* GetTokenTypeName(TokenType token_type) {
22
3.92M
  static const char* s_names[] = {
23
576M
#define WABT_TOKEN(name, string) string,
24
3.92M
#define WABT_TOKEN_FIRST(name, string)
25
3.92M
#define WABT_TOKEN_LAST(name, string)
26
3.92M
#include "wabt/token.def"
27
3.92M
#undef WABT_TOKEN
28
3.92M
#undef WABT_TOKEN_FIRST
29
3.92M
#undef WABT_TOKEN_LAST
30
3.92M
  };
31
32
3.92M
  static_assert(
33
3.92M
      WABT_ARRAY_SIZE(s_names) == WABT_ENUM_COUNT(TokenType),
34
3.92M
      "Expected TokenType names list length to match number of TokenTypes.");
35
36
3.92M
  int x = static_cast<int>(token_type);
37
3.92M
  if (x < WABT_ENUM_COUNT(TokenType)) {
38
3.92M
    return s_names[x];
39
3.92M
  }
40
41
0
  return "Invalid";
42
3.92M
}
43
44
Token::Token(Location loc, TokenType token_type)
45
15.7M
    : loc(loc), token_type_(token_type) {
46
15.7M
  assert(IsTokenTypeBare(token_type_));
47
15.7M
}
48
49
Token::Token(Location loc, TokenType token_type, Type type)
50
3.28M
    : loc(loc), token_type_(token_type) {
51
3.28M
  assert(HasType());
52
3.28M
  Construct(type_, type);
53
3.28M
}
54
55
Token::Token(Location loc, TokenType token_type, std::string_view text)
56
8.33M
    : loc(loc), token_type_(token_type) {
57
8.33M
  assert(HasText());
58
8.33M
  Construct(text_, text);
59
8.33M
}
60
61
Token::Token(Location loc, TokenType token_type, Opcode opcode)
62
1.36M
    : loc(loc), token_type_(token_type) {
63
1.36M
  assert(HasOpcode());
64
1.36M
  Construct(opcode_, opcode);
65
1.36M
}
66
67
Token::Token(Location loc, TokenType token_type, const Literal& literal)
68
1.13M
    : loc(loc), token_type_(token_type) {
69
1.13M
  assert(HasLiteral());
70
1.13M
  Construct(literal_, literal);
71
1.13M
}
72
73
5.55M
std::string Token::to_string() const {
74
5.55M
  if (IsTokenTypeBare(token_type_)) {
75
1.15M
    return GetTokenTypeName(token_type_);
76
4.40M
  } else if (HasLiteral()) {
77
129k
    return std::string(literal_.text);
78
4.27M
  } else if (HasOpcode()) {
79
8.43k
    return opcode_.GetName();
80
4.26M
  } else if (HasText()) {
81
4.14M
    return std::string(text_);
82
4.14M
  } else if (IsTokenTypeRefKind(token_type_)) {
83
114k
    return type_.GetRefKindName();
84
114k
  } else {
85
11.9k
    assert(HasType());
86
11.9k
    return type_.GetName();
87
11.9k
  }
88
5.55M
}
89
90
4.71M
std::string Token::to_string_clamp(size_t max_length) const {
91
4.71M
  std::string s = to_string();
92
4.71M
  if (s.length() > max_length) {
93
65.1k
    return s.substr(0, max_length - 3) + "...";
94
4.65M
  } else {
95
4.65M
    return s;
96
4.65M
  }
97
4.71M
}
98
99
}  // namespace wabt