/home/arjun/llvm-project/llvm/include/llvm/ADT/StringSwitch.h
Line | Count | Source (jump to first uncovered line) |
1 | | //===--- StringSwitch.h - Switch-on-literal-string Construct --------------===/ |
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 | | // This file implements the StringSwitch template, which mimics a switch() |
9 | | // statement whose cases are string literals. |
10 | | // |
11 | | //===----------------------------------------------------------------------===/ |
12 | | #ifndef LLVM_ADT_STRINGSWITCH_H |
13 | | #define LLVM_ADT_STRINGSWITCH_H |
14 | | |
15 | | #include "llvm/ADT/StringRef.h" |
16 | | #include "llvm/Support/Compiler.h" |
17 | | #include <cassert> |
18 | | #include <cstring> |
19 | | |
20 | | namespace llvm { |
21 | | |
22 | | /// A switch()-like statement whose cases are string literals. |
23 | | /// |
24 | | /// The StringSwitch class is a simple form of a switch() statement that |
25 | | /// determines whether the given string matches one of the given string |
26 | | /// literals. The template type parameter \p T is the type of the value that |
27 | | /// will be returned from the string-switch expression. For example, |
28 | | /// the following code switches on the name of a color in \c argv[i]: |
29 | | /// |
30 | | /// \code |
31 | | /// Color color = StringSwitch<Color>(argv[i]) |
32 | | /// .Case("red", Red) |
33 | | /// .Case("orange", Orange) |
34 | | /// .Case("yellow", Yellow) |
35 | | /// .Case("green", Green) |
36 | | /// .Case("blue", Blue) |
37 | | /// .Case("indigo", Indigo) |
38 | | /// .Cases("violet", "purple", Violet) |
39 | | /// .Default(UnknownColor); |
40 | | /// \endcode |
41 | | template<typename T, typename R = T> |
42 | | class StringSwitch { |
43 | | /// The string we are matching. |
44 | | const StringRef Str; |
45 | | |
46 | | /// The pointer to the result of this switch statement, once known, |
47 | | /// null before that. |
48 | | Optional<T> Result; |
49 | | |
50 | | public: |
51 | | explicit StringSwitch(StringRef S) |
52 | 46 | : Str(S), Result() { } Unexecuted instantiation: _ZN4llvm12StringSwitchINS_6Triple11SubArchTypeES2_EC2ENS_9StringRefE _ZN4llvm12StringSwitchINS_6Triple10VendorTypeES2_EC2ENS_9StringRefE Line | Count | Source | 52 | 10 | : Str(S), Result() { } |
_ZN4llvm12StringSwitchINS_6Triple6OSTypeES2_EC2ENS_9StringRefE Line | Count | Source | 52 | 8 | : Str(S), Result() { } |
_ZN4llvm12StringSwitchINS_6Triple16ObjectFormatTypeES2_EC2ENS_9StringRefE Line | Count | Source | 52 | 6 | : Str(S), Result() { } |
_ZN4llvm12StringSwitchINS_6Triple8ArchTypeES2_EC2ENS_9StringRefE Line | Count | Source | 52 | 8 | : Str(S), Result() { } |
_ZN4llvm12StringSwitchINS_6Triple15EnvironmentTypeES2_EC2ENS_9StringRefE Line | Count | Source | 52 | 8 | : Str(S), Result() { } |
_ZN4llvm12StringSwitchINS_9StringRefES1_EC2ES1_ Line | Count | Source | 52 | 6 | : Str(S), Result() { } |
Unexecuted instantiation: _ZN4llvm12StringSwitchINS_3ARM7ISAKindES2_EC2ENS_9StringRefE Unexecuted instantiation: _ZN4llvm12StringSwitchIjjEC2ENS_9StringRefE Unexecuted instantiation: _ZN4llvm12StringSwitchImmEC2ENS_9StringRefE Unexecuted instantiation: _ZN4llvm12StringSwitchIPKcS2_EC2ENS_9StringRefE Unexecuted instantiation: _ZN4llvm12StringSwitchINS_8OptionalIN4mlir13AtomicRMWKindEEES4_EC2ENS_9StringRefE Unexecuted instantiation: _ZN4llvm12StringSwitchINS_8OptionalIN4mlir13CmpFPredicateEEES4_EC2ENS_9StringRefE Unexecuted instantiation: _ZN4llvm12StringSwitchINS_8OptionalIN4mlir13CmpIPredicateEEES4_EC2ENS_9StringRefE Unexecuted instantiation: _ZN4llvm12StringSwitchIN4mlir11SymbolTable10VisibilityES3_EC2ENS_9StringRefE |
53 | | |
54 | | // StringSwitch is not copyable. |
55 | | StringSwitch(const StringSwitch &) = delete; |
56 | | |
57 | | // StringSwitch is not assignable due to 'Str' being 'const'. |
58 | | void operator=(const StringSwitch &) = delete; |
59 | | void operator=(StringSwitch &&other) = delete; |
60 | | |
61 | | StringSwitch(StringSwitch &&other) |
62 | | : Str(other.Str), Result(std::move(other.Result)) { } |
63 | | |
64 | | ~StringSwitch() = default; |
65 | | |
66 | | // Case-sensitive case matchers |
67 | 1.01k | StringSwitch &Case(StringLiteral S, T Value) { |
68 | 1.01k | if (!Result && Str == S) { |
69 | 8 | Result = std::move(Value); |
70 | 8 | } |
71 | 1.01k | return *this; |
72 | 1.01k | } Unexecuted instantiation: _ZN4llvm12StringSwitchINS_12DenormalMode16DenormalModeKindES2_E4CaseENS_13StringLiteralES2_ Unexecuted instantiation: _ZN4llvm12StringSwitchIPKcS2_E4CaseENS_13StringLiteralES2_ _ZN4llvm12StringSwitchINS_6Triple10VendorTypeES2_E4CaseENS_13StringLiteralES2_ Line | Count | Source | 67 | 160 | StringSwitch &Case(StringLiteral S, T Value) { | 68 | 160 | if (!Result && Str == S) { | 69 | 0 | Result = std::move(Value); | 70 | 0 | } | 71 | 160 | return *this; | 72 | 160 | } |
_ZN4llvm12StringSwitchINS_6Triple8ArchTypeES2_E4CaseENS_13StringLiteralES2_ Line | Count | Source | 67 | 672 | StringSwitch &Case(StringLiteral S, T Value) { | 68 | 672 | if (!Result && Str == S) { | 69 | 8 | Result = std::move(Value); | 70 | 8 | } | 71 | 672 | return *this; | 72 | 672 | } |
Unexecuted instantiation: _ZN4llvm12StringSwitchINS_6Triple15EnvironmentTypeES2_E4CaseENS_13StringLiteralES2_ _ZN4llvm12StringSwitchINS_9StringRefES1_E4CaseENS_13StringLiteralES1_ Line | Count | Source | 67 | 186 | StringSwitch &Case(StringLiteral S, T Value) { | 68 | 186 | if (!Result && Str == S) { | 69 | 0 | Result = std::move(Value); | 70 | 0 | } | 71 | 186 | return *this; | 72 | 186 | } |
Unexecuted instantiation: _ZN4llvm12StringSwitchIjjE4CaseENS_13StringLiteralEj Unexecuted instantiation: _ZN4llvm12StringSwitchImmE4CaseENS_13StringLiteralEm Unexecuted instantiation: _ZN4llvm12StringSwitchINS_8OptionalIN4mlir13AtomicRMWKindEEES4_E4CaseENS_13StringLiteralES4_ Unexecuted instantiation: _ZN4llvm12StringSwitchINS_8OptionalIN4mlir13CmpFPredicateEEES4_E4CaseENS_13StringLiteralES4_ Unexecuted instantiation: _ZN4llvm12StringSwitchINS_8OptionalIN4mlir13CmpIPredicateEEES4_E4CaseENS_13StringLiteralES4_ Unexecuted instantiation: _ZN4llvm12StringSwitchIN4mlir11SymbolTable10VisibilityES3_E4CaseENS_13StringLiteralES3_ |
73 | | |
74 | 30 | StringSwitch& EndsWith(StringLiteral S, T Value) { |
75 | 30 | if (!Result && Str.endswith(S)) { |
76 | 0 | Result = std::move(Value); |
77 | 0 | } |
78 | 30 | return *this; |
79 | 30 | } Unexecuted instantiation: _ZN4llvm12StringSwitchINS_6Triple11SubArchTypeES2_E8EndsWithENS_13StringLiteralES2_ _ZN4llvm12StringSwitchINS_6Triple16ObjectFormatTypeES2_E8EndsWithENS_13StringLiteralES2_ Line | Count | Source | 74 | 30 | StringSwitch& EndsWith(StringLiteral S, T Value) { | 75 | 30 | if (!Result && Str.endswith(S)) { | 76 | 0 | Result = std::move(Value); | 77 | 0 | } | 78 | 30 | return *this; | 79 | 30 | } |
|
80 | | |
81 | 448 | StringSwitch& StartsWith(StringLiteral S, T Value) { |
82 | 448 | if (!Result && Str.startswith(S)) { |
83 | 16 | Result = std::move(Value); |
84 | 16 | } |
85 | 448 | return *this; |
86 | 448 | } _ZN4llvm12StringSwitchINS_6Triple6OSTypeES2_E10StartsWithENS_13StringLiteralES2_ Line | Count | Source | 81 | 288 | StringSwitch& StartsWith(StringLiteral S, T Value) { | 82 | 288 | if (!Result && Str.startswith(S)) { | 83 | 8 | Result = std::move(Value); | 84 | 8 | } | 85 | 288 | return *this; | 86 | 288 | } |
_ZN4llvm12StringSwitchINS_6Triple8ArchTypeES2_E10StartsWithENS_13StringLiteralES2_ Line | Count | Source | 81 | 8 | StringSwitch& StartsWith(StringLiteral S, T Value) { | 82 | 8 | if (!Result && Str.startswith(S)) { | 83 | 0 | Result = std::move(Value); | 84 | 0 | } | 85 | 8 | return *this; | 86 | 8 | } |
_ZN4llvm12StringSwitchINS_6Triple15EnvironmentTypeES2_E10StartsWithENS_13StringLiteralES2_ Line | Count | Source | 81 | 152 | StringSwitch& StartsWith(StringLiteral S, T Value) { | 82 | 152 | if (!Result && Str.startswith(S)) { | 83 | 8 | Result = std::move(Value); | 84 | 8 | } | 85 | 152 | return *this; | 86 | 152 | } |
Unexecuted instantiation: _ZN4llvm12StringSwitchINS_3ARM7ISAKindES2_E10StartsWithENS_13StringLiteralES2_ |
87 | | |
88 | 120 | StringSwitch &Cases(StringLiteral S0, StringLiteral S1, T Value) { |
89 | 120 | return Case(S0, Value).Case(S1, Value); |
90 | 120 | } Unexecuted instantiation: _ZN4llvm12StringSwitchINS_12DenormalMode16DenormalModeKindES2_E5CasesENS_13StringLiteralES4_S2_ Unexecuted instantiation: _ZN4llvm12StringSwitchIPKcS2_E5CasesENS_13StringLiteralES4_S2_ _ZN4llvm12StringSwitchINS_6Triple8ArchTypeES2_E5CasesENS_13StringLiteralES4_S2_ Line | Count | Source | 88 | 96 | StringSwitch &Cases(StringLiteral S0, StringLiteral S1, T Value) { | 89 | 96 | return Case(S0, Value).Case(S1, Value); | 90 | 96 | } |
Unexecuted instantiation: _ZN4llvm12StringSwitchINS_6Triple15EnvironmentTypeES2_E5CasesENS_13StringLiteralES4_S2_ _ZN4llvm12StringSwitchINS_9StringRefES1_E5CasesENS_13StringLiteralES3_S1_ Line | Count | Source | 88 | 24 | StringSwitch &Cases(StringLiteral S0, StringLiteral S1, T Value) { | 89 | 24 | return Case(S0, Value).Case(S1, Value); | 90 | 24 | } |
|
91 | | |
92 | | StringSwitch &Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2, |
93 | 90 | T Value) { |
94 | 90 | return Case(S0, Value).Cases(S1, S2, Value); |
95 | 90 | } _ZN4llvm12StringSwitchINS_6Triple8ArchTypeES2_E5CasesENS_13StringLiteralES4_S4_S2_ Line | Count | Source | 93 | 72 | T Value) { | 94 | 72 | return Case(S0, Value).Cases(S1, S2, Value); | 95 | 72 | } |
Unexecuted instantiation: _ZN4llvm12StringSwitchINS_6Triple15EnvironmentTypeES2_E5CasesENS_13StringLiteralES4_S4_S2_ _ZN4llvm12StringSwitchINS_9StringRefES1_E5CasesENS_13StringLiteralES3_S3_S1_ Line | Count | Source | 93 | 18 | T Value) { | 94 | 18 | return Case(S0, Value).Cases(S1, S2, Value); | 95 | 18 | } |
|
96 | | |
97 | | StringSwitch &Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2, |
98 | 60 | StringLiteral S3, T Value) { |
99 | 60 | return Case(S0, Value).Cases(S1, S2, S3, Value); |
100 | 60 | } _ZN4llvm12StringSwitchINS_6Triple8ArchTypeES2_E5CasesENS_13StringLiteralES4_S4_S4_S2_ Line | Count | Source | 98 | 48 | StringLiteral S3, T Value) { | 99 | 48 | return Case(S0, Value).Cases(S1, S2, S3, Value); | 100 | 48 | } |
Unexecuted instantiation: _ZN4llvm12StringSwitchINS_6Triple15EnvironmentTypeES2_E5CasesENS_13StringLiteralES4_S4_S4_S2_ _ZN4llvm12StringSwitchINS_9StringRefES1_E5CasesENS_13StringLiteralES3_S3_S3_S1_ Line | Count | Source | 98 | 12 | StringLiteral S3, T Value) { | 99 | 12 | return Case(S0, Value).Cases(S1, S2, S3, Value); | 100 | 12 | } |
|
101 | | |
102 | | StringSwitch &Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2, |
103 | 30 | StringLiteral S3, StringLiteral S4, T Value) { |
104 | 30 | return Case(S0, Value).Cases(S1, S2, S3, S4, Value); |
105 | 30 | } _ZN4llvm12StringSwitchINS_6Triple8ArchTypeES2_E5CasesENS_13StringLiteralES4_S4_S4_S4_S2_ Line | Count | Source | 103 | 24 | StringLiteral S3, StringLiteral S4, T Value) { | 104 | 24 | return Case(S0, Value).Cases(S1, S2, S3, S4, Value); | 105 | 24 | } |
_ZN4llvm12StringSwitchINS_9StringRefES1_E5CasesENS_13StringLiteralES3_S3_S3_S3_S1_ Line | Count | Source | 103 | 6 | StringLiteral S3, StringLiteral S4, T Value) { | 104 | 6 | return Case(S0, Value).Cases(S1, S2, S3, S4, Value); | 105 | 6 | } |
|
106 | | |
107 | | StringSwitch &Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2, |
108 | | StringLiteral S3, StringLiteral S4, StringLiteral S5, |
109 | 8 | T Value) { |
110 | 8 | return Case(S0, Value).Cases(S1, S2, S3, S4, S5, Value); |
111 | 8 | } |
112 | | |
113 | | StringSwitch &Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2, |
114 | | StringLiteral S3, StringLiteral S4, StringLiteral S5, |
115 | | StringLiteral S6, T Value) { |
116 | | return Case(S0, Value).Cases(S1, S2, S3, S4, S5, S6, Value); |
117 | | } |
118 | | |
119 | | StringSwitch &Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2, |
120 | | StringLiteral S3, StringLiteral S4, StringLiteral S5, |
121 | | StringLiteral S6, StringLiteral S7, T Value) { |
122 | | return Case(S0, Value).Cases(S1, S2, S3, S4, S5, S6, S7, Value); |
123 | | } |
124 | | |
125 | | StringSwitch &Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2, |
126 | | StringLiteral S3, StringLiteral S4, StringLiteral S5, |
127 | | StringLiteral S6, StringLiteral S7, StringLiteral S8, |
128 | | T Value) { |
129 | | return Case(S0, Value).Cases(S1, S2, S3, S4, S5, S6, S7, S8, Value); |
130 | | } |
131 | | |
132 | | StringSwitch &Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2, |
133 | | StringLiteral S3, StringLiteral S4, StringLiteral S5, |
134 | | StringLiteral S6, StringLiteral S7, StringLiteral S8, |
135 | | StringLiteral S9, T Value) { |
136 | | return Case(S0, Value).Cases(S1, S2, S3, S4, S5, S6, S7, S8, S9, Value); |
137 | | } |
138 | | |
139 | | // Case-insensitive case matchers. |
140 | 0 | StringSwitch &CaseLower(StringLiteral S, T Value) { |
141 | 0 | if (!Result && Str.equals_lower(S)) |
142 | 0 | Result = std::move(Value); |
143 | 0 |
|
144 | 0 | return *this; |
145 | 0 | } |
146 | | |
147 | | StringSwitch &EndsWithLower(StringLiteral S, T Value) { |
148 | | if (!Result && Str.endswith_lower(S)) |
149 | | Result = Value; |
150 | | |
151 | | return *this; |
152 | | } |
153 | | |
154 | | StringSwitch &StartsWithLower(StringLiteral S, T Value) { |
155 | | if (!Result && Str.startswith_lower(S)) |
156 | | Result = std::move(Value); |
157 | | |
158 | | return *this; |
159 | | } |
160 | | |
161 | | StringSwitch &CasesLower(StringLiteral S0, StringLiteral S1, T Value) { |
162 | | return CaseLower(S0, Value).CaseLower(S1, Value); |
163 | | } |
164 | | |
165 | | StringSwitch &CasesLower(StringLiteral S0, StringLiteral S1, StringLiteral S2, |
166 | | T Value) { |
167 | | return CaseLower(S0, Value).CasesLower(S1, S2, Value); |
168 | | } |
169 | | |
170 | | StringSwitch &CasesLower(StringLiteral S0, StringLiteral S1, StringLiteral S2, |
171 | | StringLiteral S3, T Value) { |
172 | | return CaseLower(S0, Value).CasesLower(S1, S2, S3, Value); |
173 | | } |
174 | | |
175 | | StringSwitch &CasesLower(StringLiteral S0, StringLiteral S1, StringLiteral S2, |
176 | | StringLiteral S3, StringLiteral S4, T Value) { |
177 | | return CaseLower(S0, Value).CasesLower(S1, S2, S3, S4, Value); |
178 | | } |
179 | | |
180 | | LLVM_NODISCARD |
181 | 46 | R Default(T Value) { |
182 | 46 | if (Result) |
183 | 24 | return std::move(*Result); |
184 | 22 | return Value; |
185 | 22 | } Unexecuted instantiation: _ZN4llvm12StringSwitchINS_12DenormalMode16DenormalModeKindES2_E7DefaultES2_ Unexecuted instantiation: _ZN4llvm12StringSwitchIPKcS2_E7DefaultES2_ Unexecuted instantiation: _ZN4llvm12StringSwitchINS_6Triple11SubArchTypeES2_E7DefaultES2_ _ZN4llvm12StringSwitchINS_6Triple10VendorTypeES2_E7DefaultES2_ Line | Count | Source | 181 | 10 | R Default(T Value) { | 182 | 10 | if (Result) | 183 | 0 | return std::move(*Result); | 184 | 10 | return Value; | 185 | 10 | } |
_ZN4llvm12StringSwitchINS_6Triple6OSTypeES2_E7DefaultES2_ Line | Count | Source | 181 | 8 | R Default(T Value) { | 182 | 8 | if (Result) | 183 | 8 | return std::move(*Result); | 184 | 0 | return Value; | 185 | 0 | } |
_ZN4llvm12StringSwitchINS_6Triple16ObjectFormatTypeES2_E7DefaultES2_ Line | Count | Source | 181 | 6 | R Default(T Value) { | 182 | 6 | if (Result) | 183 | 0 | return std::move(*Result); | 184 | 6 | return Value; | 185 | 6 | } |
_ZN4llvm12StringSwitchINS_6Triple8ArchTypeES2_E7DefaultES2_ Line | Count | Source | 181 | 8 | R Default(T Value) { | 182 | 8 | if (Result) | 183 | 8 | return std::move(*Result); | 184 | 0 | return Value; | 185 | 0 | } |
_ZN4llvm12StringSwitchINS_6Triple15EnvironmentTypeES2_E7DefaultES2_ Line | Count | Source | 181 | 8 | R Default(T Value) { | 182 | 8 | if (Result) | 183 | 8 | return std::move(*Result); | 184 | 0 | return Value; | 185 | 0 | } |
_ZN4llvm12StringSwitchINS_9StringRefES1_E7DefaultES1_ Line | Count | Source | 181 | 6 | R Default(T Value) { | 182 | 6 | if (Result) | 183 | 0 | return std::move(*Result); | 184 | 6 | return Value; | 185 | 6 | } |
Unexecuted instantiation: _ZN4llvm12StringSwitchINS_3ARM7ISAKindES2_E7DefaultES2_ Unexecuted instantiation: _ZN4llvm12StringSwitchIjjE7DefaultEj Unexecuted instantiation: _ZN4llvm12StringSwitchImmE7DefaultEm Unexecuted instantiation: _ZN4llvm12StringSwitchINS_8OptionalIN4mlir13AtomicRMWKindEEES4_E7DefaultES4_ Unexecuted instantiation: _ZN4llvm12StringSwitchINS_8OptionalIN4mlir13CmpFPredicateEEES4_E7DefaultES4_ Unexecuted instantiation: _ZN4llvm12StringSwitchINS_8OptionalIN4mlir13CmpIPredicateEEES4_E7DefaultES4_ |
186 | | |
187 | | LLVM_NODISCARD |
188 | 0 | operator R() { |
189 | 0 | assert(Result && "Fell off the end of a string-switch"); |
190 | 0 | return std::move(*Result); |
191 | 0 | } |
192 | | }; |
193 | | |
194 | | } // end namespace llvm |
195 | | |
196 | | #endif // LLVM_ADT_STRINGSWITCH_H |