Skip to content

Commit 8572b7e

Browse files
committed
Address llvm::StringSwitch deprecations in advance
There's a whole bunch of these, e.g. - llvm/llvm-project#163405 - llvm/llvm-project#164276 - llvm/llvm-project#165119 - llvm/llvm-project#166016 - llvm/llvm-project#166066
1 parent ce082bc commit 8572b7e

File tree

9 files changed

+95
-119
lines changed

9 files changed

+95
-119
lines changed

include/swift/Demangling/TypeDecoder.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -539,8 +539,8 @@ void decodeRequirement(
539539
.Case("D", LayoutConstraintKind::NativeClass)
540540
.Case("T", LayoutConstraintKind::Trivial)
541541
.Case("B", LayoutConstraintKind::BridgeObject)
542-
.Cases("E", "e", LayoutConstraintKind::TrivialOfExactSize)
543-
.Cases("M", "m", LayoutConstraintKind::TrivialOfAtMostSize)
542+
.Cases({"E", "e"}, LayoutConstraintKind::TrivialOfExactSize)
543+
.Cases({"M", "m"}, LayoutConstraintKind::TrivialOfAtMostSize)
544544
.Case("S", LayoutConstraintKind::TrivialStride)
545545
.Default(std::nullopt);
546546

lib/Basic/Platform.cpp

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -367,18 +367,18 @@ getArchForAppleTargetSpecificModuleTriple(const llvm::Triple &triple) {
367367
auto tripleArchName = triple.getArchName();
368368

369369
return llvm::StringSwitch<StringRef>(tripleArchName)
370-
.Cases("arm64", "aarch64", "arm64")
371-
.Cases("arm64_32", "aarch64_32", "arm64_32")
372-
.Cases("x86_64", "amd64", "x86_64")
373-
.Cases("i386", "i486", "i586", "i686", "i786", "i886", "i986",
374-
"i386")
375-
.Cases("unknown", "", "unknown")
376-
// These values are also supported, but are handled by the default case below:
377-
// .Case ("armv7s", "armv7s")
378-
// .Case ("armv7k", "armv7k")
379-
// .Case ("armv7", "armv7")
380-
// .Case ("arm64e", "arm64e")
381-
.Default(tripleArchName);
370+
.Cases({"arm64", "aarch64"}, "arm64")
371+
.Cases({"arm64_32", "aarch64_32"}, "arm64_32")
372+
.Cases({"x86_64", "amd64"}, "x86_64")
373+
.Cases({"i386", "i486", "i586", "i686", "i786", "i886", "i986"}, "i386")
374+
.Cases({"unknown", ""}, "unknown")
375+
// These values are also supported, but are handled by the default case
376+
// below:
377+
// .Case ("armv7s", "armv7s")
378+
// .Case ("armv7k", "armv7k")
379+
// .Case ("armv7", "armv7")
380+
// .Case ("arm64e", "arm64e")
381+
.Default(tripleArchName);
382382
}
383383

384384
static StringRef
@@ -405,20 +405,21 @@ getOSForAppleTargetSpecificModuleTriple(const llvm::Triple &triple) {
405405
auto tripleOSNameNoVersion = tripleOSName.take_until(llvm::isDigit);
406406

407407
return llvm::StringSwitch<StringRef>(tripleOSNameNoVersion)
408-
.Cases("macos", "macosx", "darwin", "macos")
409-
.Cases("unknown", "", "unknown")
410-
// These values are also supported, but are handled by the default case below:
411-
// .Case ("ios", "ios")
412-
// .Case ("tvos", "tvos")
413-
// .Case ("watchos", "watchos")
414-
.Default(tripleOSNameNoVersion);
408+
.Cases({"macos", "macosx", "darwin"}, "macos")
409+
.Cases({"unknown", ""}, "unknown")
410+
// These values are also supported, but are handled by the default case
411+
// below:
412+
// .Case ("ios", "ios")
413+
// .Case ("tvos", "tvos")
414+
// .Case ("watchos", "watchos")
415+
.Default(tripleOSNameNoVersion);
415416
}
416417

417418
static std::optional<StringRef>
418419
getEnvironmentForAppleTargetSpecificModuleTriple(const llvm::Triple &triple) {
419420
auto tripleEnvironment = triple.getEnvironmentName();
420421
return llvm::StringSwitch<std::optional<StringRef>>(tripleEnvironment)
421-
.Cases("unknown", "", std::nullopt)
422+
.Cases({"unknown", ""}, std::nullopt)
422423
// These values are also supported, but are handled by the default case
423424
// below:
424425
// .Case ("simulator", StringRef("simulator"))

lib/ClangImporter/ClangImporter.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3452,19 +3452,19 @@ class DarwinLegacyFilterDeclConsumer : public swift::VisibleDeclConsumer {
34523452
if (!VD->hasName() || VD->getBaseName().isSpecial())
34533453
return true;
34543454
return llvm::StringSwitch<bool>(VD->getBaseName().userFacingName())
3455-
.Cases("OSErr", "OSStatus", "OptionBits", false)
3456-
.Cases("FourCharCode", "OSType", false)
3455+
.Cases({"OSErr", "OSStatus", "OptionBits"}, false)
3456+
.Cases({"FourCharCode", "OSType"}, false)
34573457
.Case("Boolean", false)
34583458
.Case("kUnknownType", false)
3459-
.Cases("UTF32Char", "UniChar", "UTF16Char", "UTF8Char", false)
3459+
.Cases({"UTF32Char", "UniChar", "UTF16Char", "UTF8Char"}, false)
34603460
.Case("ProcessSerialNumber", false)
34613461
.Default(true);
34623462
}
34633463

34643464
if (clangModule->Parent &&
34653465
clangModule->Parent->Name == "CarbonCore") {
34663466
return llvm::StringSwitch<bool>(clangModule->Name)
3467-
.Cases("BackupCore", "DiskSpaceRecovery", "MacErrors", false)
3467+
.Cases({"BackupCore", "DiskSpaceRecovery", "MacErrors"}, false)
34683468
.Case("UnicodeUtilities", false)
34693469
.Default(true);
34703470
}
@@ -3474,9 +3474,9 @@ class DarwinLegacyFilterDeclConsumer : public swift::VisibleDeclConsumer {
34743474
// Note that this is a list of things to /drop/ rather than to /keep/.
34753475
// We're more likely to see new, modern headers added to OSServices.
34763476
return llvm::StringSwitch<bool>(clangModule->Name)
3477-
.Cases("IconStorage", "KeychainCore", "Power", true)
3478-
.Cases("SecurityCore", "SystemSound", true)
3479-
.Cases("WSMethodInvocation", "WSProtocolHandler", "WSTypes", true)
3477+
.Cases({"IconStorage", "KeychainCore", "Power"}, true)
3478+
.Cases({"SecurityCore", "SystemSound"}, true)
3479+
.Cases({"WSMethodInvocation", "WSProtocolHandler", "WSTypes"}, true)
34803480
.Default(false);
34813481
}
34823482

lib/Driver/Driver.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1487,13 +1487,13 @@ void Driver::buildOutputInfo(const ToolChain &TC, const DerivedArgList &Args,
14871487
OI.RuntimeVariant =
14881488
llvm::StringSwitch<std::optional<OutputInfo::MSVCRuntime>>(
14891489
A->getValue())
1490-
.Cases("MD", "MultiThreadedDLL", "shared-ucrt",
1490+
.Cases({"MD", "MultiThreadedDLL", "shared-ucrt"},
14911491
OutputInfo::MSVCRuntime::MultiThreadedDLL)
1492-
.Cases("MDd", "MultiThreadedDebugDLL", "shared-debug-ucrt",
1492+
.Cases({"MDd", "MultiThreadedDebugDLL", "shared-debug-ucrt"},
14931493
OutputInfo::MSVCRuntime::MultiThreadedDebugDLL)
1494-
.Cases("MT", "MultiThreaded", "static-ucrt",
1494+
.Cases({"MT", "MultiThreaded", "static-ucrt"},
14951495
OutputInfo::MSVCRuntime::MultiThreaded)
1496-
.Cases("MTd", "MultiThreadedDebug", "static-debug-ucrt",
1496+
.Cases({"MTd", "MultiThreadedDebug", "static-debug-ucrt"},
14971497
OutputInfo::MSVCRuntime::MultiThreadedDebug)
14981498
.Default(std::nullopt);
14991499
if (!OI.RuntimeVariant)

lib/Driver/WindowsToolChains.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ toolchains::Windows::constructInvocation(const DynamicLinkJobAction &job,
7676
auto requiresLLD = [&]{
7777
if (const Arg *A = context.Args.getLastArg(options::OPT_use_ld)) {
7878
return llvm::StringSwitch<bool>(A->getValue())
79-
.Cases("lld", "lld.exe", "lld-link", "lld-link.exe", true)
80-
.Default(false);
79+
.Cases({"lld", "lld.exe", "lld-link", "lld-link.exe"}, true)
80+
.Default(false);
8181
}
8282
// Force to use lld for LTO on Windows because we don't support link LTO or
8383
// something else except for lld LTO at this time.

lib/Frontend/CompilerInvocation.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -568,9 +568,10 @@ static void ParseModuleInterfaceArgs(ModuleInterfaceOptions &Opts,
568568
OPT_disable_module_selectors_in_module_interface,
569569
false);
570570
} else if (auto envValue = ::getenv("SWIFT_MODULE_SELECTORS_IN_INTERFACES")) {
571-
Opts.UseModuleSelectors = llvm::StringSwitch<bool>(envValue)
572-
.CasesLower("false", "no", "off", "0", false)
573-
.Default(true);
571+
Opts.UseModuleSelectors =
572+
llvm::StringSwitch<bool>(envValue)
573+
.CasesLower({"false", "no", "off", "0"}, false)
574+
.Default(true);
574575
} else {
575576
// Any heuristics we might add would go here.
576577
Opts.UseModuleSelectors = false;

lib/Parse/ParseDecl.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1444,12 +1444,13 @@ bool Parser::parseDifferentiableAttributeArguments(
14441444
// Parse optional differentiability parameters.
14451445
// Parse differentiability kind (optional).
14461446
if (Tok.is(tok::identifier)) {
1447-
diffKind = llvm::StringSwitch<DifferentiabilityKind>(Tok.getText())
1448-
.Case("reverse", DifferentiabilityKind::Reverse)
1449-
.Cases("wrt", "withRespectTo", DifferentiabilityKind::Normal)
1450-
.Case("_linear", DifferentiabilityKind::Linear)
1451-
.Case("_forward", DifferentiabilityKind::Forward)
1452-
.Default(DifferentiabilityKind::NonDifferentiable);
1447+
diffKind =
1448+
llvm::StringSwitch<DifferentiabilityKind>(Tok.getText())
1449+
.Case("reverse", DifferentiabilityKind::Reverse)
1450+
.Cases({"wrt", "withRespectTo"}, DifferentiabilityKind::Normal)
1451+
.Case("_linear", DifferentiabilityKind::Linear)
1452+
.Case("_forward", DifferentiabilityKind::Forward)
1453+
.Default(DifferentiabilityKind::NonDifferentiable);
14531454

14541455
switch (diffKind) {
14551456
// Reject unsupported differentiability kinds.

lib/PrintAsClang/PrintAsClang.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ static void collectClangModuleHeaderIncludes(
358358
dir != end && !errorCode; dir.increment(errorCode)) {
359359

360360
if (llvm::StringSwitch<bool>(llvm::sys::path::extension(dir->path()))
361-
.Cases(".h", ".H", ".hh", ".hpp", true)
361+
.Cases({".h", ".H", ".hh", ".hpp"}, true)
362362
.Default(false)) {
363363

364364
// Compute path to the header relative to the root of the module

stdlib/include/llvm/ADT/StringSwitch.h

Lines changed: 47 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "llvm/Support/Compiler.h"
1717
#include <cassert>
1818
#include <cstring>
19+
#include <initializer_list>
1920

2021
inline namespace __swift { inline namespace __runtime {
2122
namespace llvm {
@@ -66,9 +67,7 @@ class StringSwitch {
6667

6768
// Case-sensitive case matchers
6869
StringSwitch &Case(StringLiteral S, T Value) {
69-
if (!Result && Str == S) {
70-
Result = std::move(Value);
71-
}
70+
CaseImpl(Value, S);
7271
return *this;
7372
}
7473

@@ -86,62 +85,14 @@ class StringSwitch {
8685
return *this;
8786
}
8887

89-
StringSwitch &Cases(StringLiteral S0, StringLiteral S1, T Value) {
90-
return Case(S0, Value).Case(S1, Value);
91-
}
92-
93-
StringSwitch &Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2,
94-
T Value) {
95-
return Case(S0, Value).Cases(S1, S2, Value);
96-
}
97-
98-
StringSwitch &Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2,
99-
StringLiteral S3, T Value) {
100-
return Case(S0, Value).Cases(S1, S2, S3, Value);
101-
}
102-
103-
StringSwitch &Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2,
104-
StringLiteral S3, StringLiteral S4, T Value) {
105-
return Case(S0, Value).Cases(S1, S2, S3, S4, Value);
106-
}
107-
108-
StringSwitch &Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2,
109-
StringLiteral S3, StringLiteral S4, StringLiteral S5,
88+
StringSwitch &Cases(std::initializer_list<StringLiteral> CaseStrings,
11089
T Value) {
111-
return Case(S0, Value).Cases(S1, S2, S3, S4, S5, Value);
112-
}
113-
114-
StringSwitch &Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2,
115-
StringLiteral S3, StringLiteral S4, StringLiteral S5,
116-
StringLiteral S6, T Value) {
117-
return Case(S0, Value).Cases(S1, S2, S3, S4, S5, S6, Value);
118-
}
119-
120-
StringSwitch &Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2,
121-
StringLiteral S3, StringLiteral S4, StringLiteral S5,
122-
StringLiteral S6, StringLiteral S7, T Value) {
123-
return Case(S0, Value).Cases(S1, S2, S3, S4, S5, S6, S7, Value);
124-
}
125-
126-
StringSwitch &Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2,
127-
StringLiteral S3, StringLiteral S4, StringLiteral S5,
128-
StringLiteral S6, StringLiteral S7, StringLiteral S8,
129-
T Value) {
130-
return Case(S0, Value).Cases(S1, S2, S3, S4, S5, S6, S7, S8, Value);
131-
}
132-
133-
StringSwitch &Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2,
134-
StringLiteral S3, StringLiteral S4, StringLiteral S5,
135-
StringLiteral S6, StringLiteral S7, StringLiteral S8,
136-
StringLiteral S9, T Value) {
137-
return Case(S0, Value).Cases(S1, S2, S3, S4, S5, S6, S7, S8, S9, Value);
90+
return CasesImpl(Value, CaseStrings);
13891
}
13992

14093
// Case-insensitive case matchers.
14194
StringSwitch &CaseLower(StringLiteral S, T Value) {
142-
if (!Result && Str.equals_insensitive(S))
143-
Result = std::move(Value);
144-
95+
CaseLowerImpl(Value, S);
14596
return *this;
14697
}
14798

@@ -159,37 +110,59 @@ class StringSwitch {
159110
return *this;
160111
}
161112

162-
StringSwitch &CasesLower(StringLiteral S0, StringLiteral S1, T Value) {
163-
return CaseLower(S0, Value).CaseLower(S1, Value);
164-
}
165-
166-
StringSwitch &CasesLower(StringLiteral S0, StringLiteral S1, StringLiteral S2,
113+
StringSwitch &CasesLower(std::initializer_list<StringLiteral> CaseStrings,
167114
T Value) {
168-
return CaseLower(S0, Value).CasesLower(S1, S2, Value);
115+
return CasesLowerImpl(Value, CaseStrings);
169116
}
170117

171-
StringSwitch &CasesLower(StringLiteral S0, StringLiteral S1, StringLiteral S2,
172-
StringLiteral S3, T Value) {
173-
return CaseLower(S0, Value).CasesLower(S1, S2, S3, Value);
174-
}
175-
176-
StringSwitch &CasesLower(StringLiteral S0, StringLiteral S1, StringLiteral S2,
177-
StringLiteral S3, StringLiteral S4, T Value) {
178-
return CaseLower(S0, Value).CasesLower(S1, S2, S3, S4, Value);
179-
}
180-
181-
[[nodiscard]]
182-
R Default(T Value) {
118+
[[nodiscard]] R Default(T Value) {
183119
if (Result)
184120
return std::move(*Result);
185121
return Value;
186122
}
187123

188-
[[nodiscard]]
189-
operator R() {
124+
[[nodiscard]] operator R() {
190125
assert(Result && "Fell off the end of a string-switch");
191126
return std::move(*Result);
192127
}
128+
129+
private:
130+
// Returns true when `Str` matches the `S` argument, and stores the result.
131+
bool CaseImpl(T &Value, StringLiteral S) {
132+
if (!Result && Str == S) {
133+
Result = std::move(Value);
134+
return true;
135+
}
136+
return false;
137+
}
138+
139+
// Returns true when `Str` matches the `S` argument (case-insensitive), and
140+
// stores the result.
141+
bool CaseLowerImpl(T &Value, StringLiteral S) {
142+
if (!Result && Str.equals_insensitive(S)) {
143+
Result = std::move(Value);
144+
return true;
145+
}
146+
return false;
147+
}
148+
149+
StringSwitch &CasesImpl(T &Value,
150+
std::initializer_list<StringLiteral> Cases) {
151+
// Stop matching after the string is found.
152+
for (StringLiteral S : Cases)
153+
if (CaseImpl(Value, S))
154+
break;
155+
return *this;
156+
}
157+
158+
StringSwitch &CasesLowerImpl(T &Value,
159+
std::initializer_list<StringLiteral> Cases) {
160+
// Stop matching after the string is found.
161+
for (StringLiteral S : Cases)
162+
if (CaseLowerImpl(Value, S))
163+
break;
164+
return *this;
165+
}
193166
};
194167

195168
} // end namespace llvm

0 commit comments

Comments
 (0)