Coverage Report

Created: 2025-06-24 06:43

/src/hermes/external/llvh/include/llvh/ADT/StringSwitch.h
Line
Count
Source (jump to first uncovered line)
1
//===--- StringSwitch.h - Switch-on-literal-string Construct --------------===/
2
//
3
//                     The LLVM Compiler Infrastructure
4
//
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
7
//===----------------------------------------------------------------------===/
8
//
9
//  This file implements the StringSwitch template, which mimics a switch()
10
//  statement whose cases are string literals.
11
//
12
//===----------------------------------------------------------------------===/
13
#ifndef LLVM_ADT_STRINGSWITCH_H
14
#define LLVM_ADT_STRINGSWITCH_H
15
16
#include "llvh/ADT/StringRef.h"
17
#include "llvh/Support/Compiler.h"
18
#include <cassert>
19
#include <cstring>
20
21
namespace llvh {
22
23
/// A switch()-like statement whose cases are string literals.
24
///
25
/// The StringSwitch class is a simple form of a switch() statement that
26
/// determines whether the given string matches one of the given string
27
/// literals. The template type parameter \p T is the type of the value that
28
/// will be returned from the string-switch expression. For example,
29
/// the following code switches on the name of a color in \c argv[i]:
30
///
31
/// \code
32
/// Color color = StringSwitch<Color>(argv[i])
33
///   .Case("red", Red)
34
///   .Case("orange", Orange)
35
///   .Case("yellow", Yellow)
36
///   .Case("green", Green)
37
///   .Case("blue", Blue)
38
///   .Case("indigo", Indigo)
39
///   .Cases("violet", "purple", Violet)
40
///   .Default(UnknownColor);
41
/// \endcode
42
template<typename T, typename R = T>
43
class StringSwitch {
44
  /// The string we are matching.
45
  const StringRef Str;
46
47
  /// The pointer to the result of this switch statement, once known,
48
  /// null before that.
49
  Optional<T> Result;
50
51
public:
52
  LLVM_ATTRIBUTE_ALWAYS_INLINE
53
  explicit StringSwitch(StringRef S)
54
1.53M
  : Str(S), Result() { }
llvh::StringSwitch<hermes::parser::TokenKind, hermes::parser::TokenKind>::StringSwitch(llvh::StringRef)
Line
Count
Source
54
1.53M
  : Str(S), Result() { }
Unexecuted instantiation: llvh::StringSwitch<bool, bool>::StringSwitch(llvh::StringRef)
Unexecuted instantiation: llvh::StringSwitch<char const*, char const*>::StringSwitch(llvh::StringRef)
Unexecuted instantiation: llvh::StringSwitch<llvh::Triple::SubArchType, llvh::Triple::SubArchType>::StringSwitch(llvh::StringRef)
Unexecuted instantiation: llvh::StringSwitch<llvh::Triple::VendorType, llvh::Triple::VendorType>::StringSwitch(llvh::StringRef)
Unexecuted instantiation: llvh::StringSwitch<llvh::Triple::OSType, llvh::Triple::OSType>::StringSwitch(llvh::StringRef)
Unexecuted instantiation: llvh::StringSwitch<llvh::Triple::ObjectFormatType, llvh::Triple::ObjectFormatType>::StringSwitch(llvh::StringRef)
Unexecuted instantiation: llvh::StringSwitch<llvh::Triple::ArchType, llvh::Triple::ArchType>::StringSwitch(llvh::StringRef)
Unexecuted instantiation: llvh::StringSwitch<llvh::Triple::EnvironmentType, llvh::Triple::EnvironmentType>::StringSwitch(llvh::StringRef)
Unexecuted instantiation: llvh::StringSwitch<llvh::StringRef, llvh::StringRef>::StringSwitch(llvh::StringRef)
Unexecuted instantiation: llvh::StringSwitch<unsigned int, unsigned int>::StringSwitch(llvh::StringRef)
Unexecuted instantiation: llvh::StringSwitch<llvh::AArch64::ArchKind, llvh::AArch64::ArchKind>::StringSwitch(llvh::StringRef)
Unexecuted instantiation: llvh::StringSwitch<llvh::ARM::ISAKind, llvh::ARM::ISAKind>::StringSwitch(llvh::StringRef)
55
56
  // StringSwitch is not copyable.
57
  StringSwitch(const StringSwitch &) = delete;
58
59
  // StringSwitch is not assignable due to 'Str' being 'const'.
60
  void operator=(const StringSwitch &) = delete;
61
  void operator=(StringSwitch &&other) = delete;
62
63
  StringSwitch(StringSwitch &&other)
64
    : Str(other.Str), Result(std::move(other.Result)) { }
65
66
  ~StringSwitch() = default;
67
68
  // Case-sensitive case matchers
69
  LLVM_ATTRIBUTE_ALWAYS_INLINE
70
67.6M
  StringSwitch &Case(StringLiteral S, T Value) {
71
67.6M
    if (!Result && Str == S) {
72
4.09k
      Result = std::move(Value);
73
4.09k
    }
74
67.6M
    return *this;
75
67.6M
  }
Unexecuted instantiation: llvh::StringSwitch<char const*, char const*>::Case(llvh::StringLiteral, char const*)
llvh::StringSwitch<hermes::parser::TokenKind, hermes::parser::TokenKind>::Case(llvh::StringLiteral, hermes::parser::TokenKind)
Line
Count
Source
70
67.6M
  StringSwitch &Case(StringLiteral S, T Value) {
71
67.6M
    if (!Result && Str == S) {
72
4.09k
      Result = std::move(Value);
73
4.09k
    }
74
67.6M
    return *this;
75
67.6M
  }
Unexecuted instantiation: llvh::StringSwitch<bool, bool>::Case(llvh::StringLiteral, bool)
Unexecuted instantiation: llvh::StringSwitch<llvh::Triple::VendorType, llvh::Triple::VendorType>::Case(llvh::StringLiteral, llvh::Triple::VendorType)
Unexecuted instantiation: llvh::StringSwitch<llvh::Triple::ArchType, llvh::Triple::ArchType>::Case(llvh::StringLiteral, llvh::Triple::ArchType)
Unexecuted instantiation: llvh::StringSwitch<llvh::Triple::EnvironmentType, llvh::Triple::EnvironmentType>::Case(llvh::StringLiteral, llvh::Triple::EnvironmentType)
Unexecuted instantiation: llvh::StringSwitch<llvh::StringRef, llvh::StringRef>::Case(llvh::StringLiteral, llvh::StringRef)
Unexecuted instantiation: llvh::StringSwitch<unsigned int, unsigned int>::Case(llvh::StringLiteral, unsigned int)
Unexecuted instantiation: llvh::StringSwitch<llvh::AArch64::ArchKind, llvh::AArch64::ArchKind>::Case(llvh::StringLiteral, llvh::AArch64::ArchKind)
76
77
  LLVM_ATTRIBUTE_ALWAYS_INLINE
78
0
  StringSwitch& EndsWith(StringLiteral S, T Value) {
79
0
    if (!Result && Str.endswith(S)) {
80
0
      Result = std::move(Value);
81
0
    }
82
0
    return *this;
83
0
  }
Unexecuted instantiation: llvh::StringSwitch<bool, bool>::EndsWith(llvh::StringLiteral, bool)
Unexecuted instantiation: llvh::StringSwitch<llvh::Triple::SubArchType, llvh::Triple::SubArchType>::EndsWith(llvh::StringLiteral, llvh::Triple::SubArchType)
Unexecuted instantiation: llvh::StringSwitch<llvh::Triple::ObjectFormatType, llvh::Triple::ObjectFormatType>::EndsWith(llvh::StringLiteral, llvh::Triple::ObjectFormatType)
84
85
  LLVM_ATTRIBUTE_ALWAYS_INLINE
86
0
  StringSwitch& StartsWith(StringLiteral S, T Value) {
87
0
    if (!Result && Str.startswith(S)) {
88
0
      Result = std::move(Value);
89
0
    }
90
0
    return *this;
91
0
  }
Unexecuted instantiation: llvh::StringSwitch<bool, bool>::StartsWith(llvh::StringLiteral, bool)
Unexecuted instantiation: llvh::StringSwitch<llvh::Triple::OSType, llvh::Triple::OSType>::StartsWith(llvh::StringLiteral, llvh::Triple::OSType)
Unexecuted instantiation: llvh::StringSwitch<llvh::Triple::ArchType, llvh::Triple::ArchType>::StartsWith(llvh::StringLiteral, llvh::Triple::ArchType)
Unexecuted instantiation: llvh::StringSwitch<llvh::Triple::EnvironmentType, llvh::Triple::EnvironmentType>::StartsWith(llvh::StringLiteral, llvh::Triple::EnvironmentType)
Unexecuted instantiation: llvh::StringSwitch<llvh::ARM::ISAKind, llvh::ARM::ISAKind>::StartsWith(llvh::StringLiteral, llvh::ARM::ISAKind)
92
93
  LLVM_ATTRIBUTE_ALWAYS_INLINE
94
0
  StringSwitch &Cases(StringLiteral S0, StringLiteral S1, T Value) {
95
0
    return Case(S0, Value).Case(S1, Value);
96
0
  }
Unexecuted instantiation: llvh::StringSwitch<char const*, char const*>::Cases(llvh::StringLiteral, llvh::StringLiteral, char const*)
Unexecuted instantiation: llvh::StringSwitch<llvh::Triple::ArchType, llvh::Triple::ArchType>::Cases(llvh::StringLiteral, llvh::StringLiteral, llvh::Triple::ArchType)
Unexecuted instantiation: llvh::StringSwitch<llvh::Triple::EnvironmentType, llvh::Triple::EnvironmentType>::Cases(llvh::StringLiteral, llvh::StringLiteral, llvh::Triple::EnvironmentType)
Unexecuted instantiation: llvh::StringSwitch<llvh::StringRef, llvh::StringRef>::Cases(llvh::StringLiteral, llvh::StringLiteral, llvh::StringRef)
97
98
  LLVM_ATTRIBUTE_ALWAYS_INLINE
99
  StringSwitch &Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2,
100
0
                      T Value) {
101
0
    return Case(S0, Value).Cases(S1, S2, Value);
102
0
  }
Unexecuted instantiation: llvh::StringSwitch<llvh::Triple::ArchType, llvh::Triple::ArchType>::Cases(llvh::StringLiteral, llvh::StringLiteral, llvh::StringLiteral, llvh::Triple::ArchType)
Unexecuted instantiation: llvh::StringSwitch<llvh::Triple::EnvironmentType, llvh::Triple::EnvironmentType>::Cases(llvh::StringLiteral, llvh::StringLiteral, llvh::StringLiteral, llvh::Triple::EnvironmentType)
Unexecuted instantiation: llvh::StringSwitch<llvh::StringRef, llvh::StringRef>::Cases(llvh::StringLiteral, llvh::StringLiteral, llvh::StringLiteral, llvh::StringRef)
103
104
  LLVM_ATTRIBUTE_ALWAYS_INLINE
105
  StringSwitch &Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2,
106
0
                      StringLiteral S3, T Value) {
107
0
    return Case(S0, Value).Cases(S1, S2, S3, Value);
108
0
  }
Unexecuted instantiation: llvh::StringSwitch<llvh::Triple::ArchType, llvh::Triple::ArchType>::Cases(llvh::StringLiteral, llvh::StringLiteral, llvh::StringLiteral, llvh::StringLiteral, llvh::Triple::ArchType)
Unexecuted instantiation: llvh::StringSwitch<llvh::Triple::EnvironmentType, llvh::Triple::EnvironmentType>::Cases(llvh::StringLiteral, llvh::StringLiteral, llvh::StringLiteral, llvh::StringLiteral, llvh::Triple::EnvironmentType)
Unexecuted instantiation: llvh::StringSwitch<llvh::StringRef, llvh::StringRef>::Cases(llvh::StringLiteral, llvh::StringLiteral, llvh::StringLiteral, llvh::StringLiteral, llvh::StringRef)
109
110
  LLVM_ATTRIBUTE_ALWAYS_INLINE
111
  StringSwitch &Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2,
112
0
                      StringLiteral S3, StringLiteral S4, T Value) {
113
0
    return Case(S0, Value).Cases(S1, S2, S3, S4, Value);
114
0
  }
Unexecuted instantiation: llvh::StringSwitch<llvh::Triple::ArchType, llvh::Triple::ArchType>::Cases(llvh::StringLiteral, llvh::StringLiteral, llvh::StringLiteral, llvh::StringLiteral, llvh::StringLiteral, llvh::Triple::ArchType)
Unexecuted instantiation: llvh::StringSwitch<llvh::StringRef, llvh::StringRef>::Cases(llvh::StringLiteral, llvh::StringLiteral, llvh::StringLiteral, llvh::StringLiteral, llvh::StringLiteral, llvh::StringRef)
115
116
  LLVM_ATTRIBUTE_ALWAYS_INLINE
117
  StringSwitch &Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2,
118
                      StringLiteral S3, StringLiteral S4, StringLiteral S5,
119
0
                      T Value) {
120
0
    return Case(S0, Value).Cases(S1, S2, S3, S4, S5, Value);
121
0
  }
122
123
  LLVM_ATTRIBUTE_ALWAYS_INLINE
124
  StringSwitch &Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2,
125
                      StringLiteral S3, StringLiteral S4, StringLiteral S5,
126
                      StringLiteral S6, T Value) {
127
    return Case(S0, Value).Cases(S1, S2, S3, S4, S5, S6, Value);
128
  }
129
130
  LLVM_ATTRIBUTE_ALWAYS_INLINE
131
  StringSwitch &Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2,
132
                      StringLiteral S3, StringLiteral S4, StringLiteral S5,
133
                      StringLiteral S6, StringLiteral S7, T Value) {
134
    return Case(S0, Value).Cases(S1, S2, S3, S4, S5, S6, S7, Value);
135
  }
136
137
  LLVM_ATTRIBUTE_ALWAYS_INLINE
138
  StringSwitch &Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2,
139
                      StringLiteral S3, StringLiteral S4, StringLiteral S5,
140
                      StringLiteral S6, StringLiteral S7, StringLiteral S8,
141
                      T Value) {
142
    return Case(S0, Value).Cases(S1, S2, S3, S4, S5, S6, S7, S8, Value);
143
  }
144
145
  LLVM_ATTRIBUTE_ALWAYS_INLINE
146
  StringSwitch &Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2,
147
                      StringLiteral S3, StringLiteral S4, StringLiteral S5,
148
                      StringLiteral S6, StringLiteral S7, StringLiteral S8,
149
                      StringLiteral S9, T Value) {
150
    return Case(S0, Value).Cases(S1, S2, S3, S4, S5, S6, S7, S8, S9, Value);
151
  }
152
153
  // Case-insensitive case matchers.
154
  LLVM_ATTRIBUTE_ALWAYS_INLINE
155
0
  StringSwitch &CaseLower(StringLiteral S, T Value) {
156
0
    if (!Result && Str.equals_lower(S))
157
0
      Result = std::move(Value);
158
0
159
0
    return *this;
160
0
  }
161
162
  LLVM_ATTRIBUTE_ALWAYS_INLINE
163
  StringSwitch &EndsWithLower(StringLiteral S, T Value) {
164
    if (!Result && Str.endswith_lower(S))
165
      Result = Value;
166
167
    return *this;
168
  }
169
170
  LLVM_ATTRIBUTE_ALWAYS_INLINE
171
  StringSwitch &StartsWithLower(StringLiteral S, T Value) {
172
    if (!Result && Str.startswith_lower(S))
173
      Result = std::move(Value);
174
175
    return *this;
176
  }
177
178
  LLVM_ATTRIBUTE_ALWAYS_INLINE
179
  StringSwitch &CasesLower(StringLiteral S0, StringLiteral S1, T Value) {
180
    return CaseLower(S0, Value).CaseLower(S1, Value);
181
  }
182
183
  LLVM_ATTRIBUTE_ALWAYS_INLINE
184
  StringSwitch &CasesLower(StringLiteral S0, StringLiteral S1, StringLiteral S2,
185
                           T Value) {
186
    return CaseLower(S0, Value).CasesLower(S1, S2, Value);
187
  }
188
189
  LLVM_ATTRIBUTE_ALWAYS_INLINE
190
  StringSwitch &CasesLower(StringLiteral S0, StringLiteral S1, StringLiteral S2,
191
                           StringLiteral S3, T Value) {
192
    return CaseLower(S0, Value).CasesLower(S1, S2, S3, Value);
193
  }
194
195
  LLVM_ATTRIBUTE_ALWAYS_INLINE
196
  StringSwitch &CasesLower(StringLiteral S0, StringLiteral S1, StringLiteral S2,
197
                           StringLiteral S3, StringLiteral S4, T Value) {
198
    return CaseLower(S0, Value).CasesLower(S1, S2, S3, S4, Value);
199
  }
200
201
  LLVM_NODISCARD
202
  LLVM_ATTRIBUTE_ALWAYS_INLINE
203
1.53M
  R Default(T Value) {
204
1.53M
    if (Result)
205
4.09k
      return std::move(*Result);
206
1.53M
    return Value;
207
1.53M
  }
Unexecuted instantiation: llvh::StringSwitch<char const*, char const*>::Default(char const*)
llvh::StringSwitch<hermes::parser::TokenKind, hermes::parser::TokenKind>::Default(hermes::parser::TokenKind)
Line
Count
Source
203
1.53M
  R Default(T Value) {
204
1.53M
    if (Result)
205
4.09k
      return std::move(*Result);
206
1.53M
    return Value;
207
1.53M
  }
Unexecuted instantiation: llvh::StringSwitch<bool, bool>::Default(bool)
Unexecuted instantiation: llvh::StringSwitch<llvh::Triple::SubArchType, llvh::Triple::SubArchType>::Default(llvh::Triple::SubArchType)
Unexecuted instantiation: llvh::StringSwitch<llvh::Triple::VendorType, llvh::Triple::VendorType>::Default(llvh::Triple::VendorType)
Unexecuted instantiation: llvh::StringSwitch<llvh::Triple::OSType, llvh::Triple::OSType>::Default(llvh::Triple::OSType)
Unexecuted instantiation: llvh::StringSwitch<llvh::Triple::ObjectFormatType, llvh::Triple::ObjectFormatType>::Default(llvh::Triple::ObjectFormatType)
Unexecuted instantiation: llvh::StringSwitch<llvh::Triple::ArchType, llvh::Triple::ArchType>::Default(llvh::Triple::ArchType)
Unexecuted instantiation: llvh::StringSwitch<llvh::Triple::EnvironmentType, llvh::Triple::EnvironmentType>::Default(llvh::Triple::EnvironmentType)
Unexecuted instantiation: llvh::StringSwitch<llvh::StringRef, llvh::StringRef>::Default(llvh::StringRef)
Unexecuted instantiation: llvh::StringSwitch<unsigned int, unsigned int>::Default(unsigned int)
Unexecuted instantiation: llvh::StringSwitch<llvh::AArch64::ArchKind, llvh::AArch64::ArchKind>::Default(llvh::AArch64::ArchKind)
Unexecuted instantiation: llvh::StringSwitch<llvh::ARM::ISAKind, llvh::ARM::ISAKind>::Default(llvh::ARM::ISAKind)
208
209
  LLVM_NODISCARD
210
  LLVM_ATTRIBUTE_ALWAYS_INLINE
211
  operator R() {
212
    assert(Result && "Fell off the end of a string-switch");
213
    return std::move(*Result);
214
  }
215
};
216
217
} // end namespace llvh
218
219
#endif // LLVM_ADT_STRINGSWITCH_H