Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions include/swift/Demangling/TypeDecoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -539,8 +539,8 @@ void decodeRequirement(
.Case("D", LayoutConstraintKind::NativeClass)
.Case("T", LayoutConstraintKind::Trivial)
.Case("B", LayoutConstraintKind::BridgeObject)
.Cases("E", "e", LayoutConstraintKind::TrivialOfExactSize)
.Cases("M", "m", LayoutConstraintKind::TrivialOfAtMostSize)
.Cases({"E", "e"}, LayoutConstraintKind::TrivialOfExactSize)
.Cases({"M", "m"}, LayoutConstraintKind::TrivialOfAtMostSize)
.Case("S", LayoutConstraintKind::TrivialStride)
.Default(std::nullopt);

Expand Down
41 changes: 21 additions & 20 deletions lib/Basic/Platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,18 +367,18 @@ getArchForAppleTargetSpecificModuleTriple(const llvm::Triple &triple) {
auto tripleArchName = triple.getArchName();

return llvm::StringSwitch<StringRef>(tripleArchName)
.Cases("arm64", "aarch64", "arm64")
.Cases("arm64_32", "aarch64_32", "arm64_32")
.Cases("x86_64", "amd64", "x86_64")
.Cases("i386", "i486", "i586", "i686", "i786", "i886", "i986",
"i386")
.Cases("unknown", "", "unknown")
// These values are also supported, but are handled by the default case below:
// .Case ("armv7s", "armv7s")
// .Case ("armv7k", "armv7k")
// .Case ("armv7", "armv7")
// .Case ("arm64e", "arm64e")
.Default(tripleArchName);
.Cases({"arm64", "aarch64"}, "arm64")
.Cases({"arm64_32", "aarch64_32"}, "arm64_32")
.Cases({"x86_64", "amd64"}, "x86_64")
.Cases({"i386", "i486", "i586", "i686", "i786", "i886", "i986"}, "i386")
.Cases({"unknown", ""}, "unknown")
// These values are also supported, but are handled by the default case
// below:
// .Case ("armv7s", "armv7s")
// .Case ("armv7k", "armv7k")
// .Case ("armv7", "armv7")
// .Case ("arm64e", "arm64e")
.Default(tripleArchName);
}

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

return llvm::StringSwitch<StringRef>(tripleOSNameNoVersion)
.Cases("macos", "macosx", "darwin", "macos")
.Cases("unknown", "", "unknown")
// These values are also supported, but are handled by the default case below:
// .Case ("ios", "ios")
// .Case ("tvos", "tvos")
// .Case ("watchos", "watchos")
.Default(tripleOSNameNoVersion);
.Cases({"macos", "macosx", "darwin"}, "macos")
.Cases({"unknown", ""}, "unknown")
// These values are also supported, but are handled by the default case
// below:
// .Case ("ios", "ios")
// .Case ("tvos", "tvos")
// .Case ("watchos", "watchos")
.Default(tripleOSNameNoVersion);
}

static std::optional<StringRef>
getEnvironmentForAppleTargetSpecificModuleTriple(const llvm::Triple &triple) {
auto tripleEnvironment = triple.getEnvironmentName();
return llvm::StringSwitch<std::optional<StringRef>>(tripleEnvironment)
.Cases("unknown", "", std::nullopt)
.Cases({"unknown", ""}, std::nullopt)
// These values are also supported, but are handled by the default case
// below:
// .Case ("simulator", StringRef("simulator"))
Expand Down
14 changes: 7 additions & 7 deletions lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3452,19 +3452,19 @@ class DarwinLegacyFilterDeclConsumer : public swift::VisibleDeclConsumer {
if (!VD->hasName() || VD->getBaseName().isSpecial())
return true;
return llvm::StringSwitch<bool>(VD->getBaseName().userFacingName())
.Cases("OSErr", "OSStatus", "OptionBits", false)
.Cases("FourCharCode", "OSType", false)
.Cases({"OSErr", "OSStatus", "OptionBits"}, false)
.Cases({"FourCharCode", "OSType"}, false)
.Case("Boolean", false)
.Case("kUnknownType", false)
.Cases("UTF32Char", "UniChar", "UTF16Char", "UTF8Char", false)
.Cases({"UTF32Char", "UniChar", "UTF16Char", "UTF8Char"}, false)
.Case("ProcessSerialNumber", false)
.Default(true);
}

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

Expand Down
8 changes: 4 additions & 4 deletions lib/Driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1487,13 +1487,13 @@ void Driver::buildOutputInfo(const ToolChain &TC, const DerivedArgList &Args,
OI.RuntimeVariant =
llvm::StringSwitch<std::optional<OutputInfo::MSVCRuntime>>(
A->getValue())
.Cases("MD", "MultiThreadedDLL", "shared-ucrt",
.Cases({"MD", "MultiThreadedDLL", "shared-ucrt"},
OutputInfo::MSVCRuntime::MultiThreadedDLL)
.Cases("MDd", "MultiThreadedDebugDLL", "shared-debug-ucrt",
.Cases({"MDd", "MultiThreadedDebugDLL", "shared-debug-ucrt"},
OutputInfo::MSVCRuntime::MultiThreadedDebugDLL)
.Cases("MT", "MultiThreaded", "static-ucrt",
.Cases({"MT", "MultiThreaded", "static-ucrt"},
OutputInfo::MSVCRuntime::MultiThreaded)
.Cases("MTd", "MultiThreadedDebug", "static-debug-ucrt",
.Cases({"MTd", "MultiThreadedDebug", "static-debug-ucrt"},
OutputInfo::MSVCRuntime::MultiThreadedDebug)
.Default(std::nullopt);
if (!OI.RuntimeVariant)
Expand Down
4 changes: 2 additions & 2 deletions lib/Driver/WindowsToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ toolchains::Windows::constructInvocation(const DynamicLinkJobAction &job,
auto requiresLLD = [&]{
if (const Arg *A = context.Args.getLastArg(options::OPT_use_ld)) {
return llvm::StringSwitch<bool>(A->getValue())
.Cases("lld", "lld.exe", "lld-link", "lld-link.exe", true)
.Default(false);
.Cases({"lld", "lld.exe", "lld-link", "lld-link.exe"}, true)
.Default(false);
}
// Force to use lld for LTO on Windows because we don't support link LTO or
// something else except for lld LTO at this time.
Expand Down
7 changes: 4 additions & 3 deletions lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -568,9 +568,10 @@ static void ParseModuleInterfaceArgs(ModuleInterfaceOptions &Opts,
OPT_disable_module_selectors_in_module_interface,
false);
} else if (auto envValue = ::getenv("SWIFT_MODULE_SELECTORS_IN_INTERFACES")) {
Opts.UseModuleSelectors = llvm::StringSwitch<bool>(envValue)
.CasesLower("false", "no", "off", "0", false)
.Default(true);
Opts.UseModuleSelectors =
llvm::StringSwitch<bool>(envValue)
.CasesLower({"false", "no", "off", "0"}, false)
.Default(true);
} else {
// Any heuristics we might add would go here.
Opts.UseModuleSelectors = false;
Expand Down
13 changes: 7 additions & 6 deletions lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1444,12 +1444,13 @@ bool Parser::parseDifferentiableAttributeArguments(
// Parse optional differentiability parameters.
// Parse differentiability kind (optional).
if (Tok.is(tok::identifier)) {
diffKind = llvm::StringSwitch<DifferentiabilityKind>(Tok.getText())
.Case("reverse", DifferentiabilityKind::Reverse)
.Cases("wrt", "withRespectTo", DifferentiabilityKind::Normal)
.Case("_linear", DifferentiabilityKind::Linear)
.Case("_forward", DifferentiabilityKind::Forward)
.Default(DifferentiabilityKind::NonDifferentiable);
diffKind =
llvm::StringSwitch<DifferentiabilityKind>(Tok.getText())
.Case("reverse", DifferentiabilityKind::Reverse)
.Cases({"wrt", "withRespectTo"}, DifferentiabilityKind::Normal)
.Case("_linear", DifferentiabilityKind::Linear)
.Case("_forward", DifferentiabilityKind::Forward)
.Default(DifferentiabilityKind::NonDifferentiable);

switch (diffKind) {
// Reject unsupported differentiability kinds.
Expand Down
2 changes: 1 addition & 1 deletion lib/PrintAsClang/PrintAsClang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ static void collectClangModuleHeaderIncludes(
dir != end && !errorCode; dir.increment(errorCode)) {

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

// Compute path to the header relative to the root of the module
Expand Down
121 changes: 47 additions & 74 deletions stdlib/include/llvm/ADT/StringSwitch.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "llvm/Support/Compiler.h"
#include <cassert>
#include <cstring>
#include <initializer_list>

inline namespace __swift { inline namespace __runtime {
namespace llvm {
Expand Down Expand Up @@ -66,9 +67,7 @@ class StringSwitch {

// Case-sensitive case matchers
StringSwitch &Case(StringLiteral S, T Value) {
if (!Result && Str == S) {
Result = std::move(Value);
}
CaseImpl(Value, S);
return *this;
}

Expand All @@ -86,62 +85,14 @@ class StringSwitch {
return *this;
}

StringSwitch &Cases(StringLiteral S0, StringLiteral S1, T Value) {
return Case(S0, Value).Case(S1, Value);
}

StringSwitch &Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2,
T Value) {
return Case(S0, Value).Cases(S1, S2, Value);
}

StringSwitch &Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2,
StringLiteral S3, T Value) {
return Case(S0, Value).Cases(S1, S2, S3, Value);
}

StringSwitch &Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2,
StringLiteral S3, StringLiteral S4, T Value) {
return Case(S0, Value).Cases(S1, S2, S3, S4, Value);
}

StringSwitch &Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2,
StringLiteral S3, StringLiteral S4, StringLiteral S5,
StringSwitch &Cases(std::initializer_list<StringLiteral> CaseStrings,
T Value) {
return Case(S0, Value).Cases(S1, S2, S3, S4, S5, Value);
}

StringSwitch &Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2,
StringLiteral S3, StringLiteral S4, StringLiteral S5,
StringLiteral S6, T Value) {
return Case(S0, Value).Cases(S1, S2, S3, S4, S5, S6, Value);
}

StringSwitch &Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2,
StringLiteral S3, StringLiteral S4, StringLiteral S5,
StringLiteral S6, StringLiteral S7, T Value) {
return Case(S0, Value).Cases(S1, S2, S3, S4, S5, S6, S7, Value);
}

StringSwitch &Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2,
StringLiteral S3, StringLiteral S4, StringLiteral S5,
StringLiteral S6, StringLiteral S7, StringLiteral S8,
T Value) {
return Case(S0, Value).Cases(S1, S2, S3, S4, S5, S6, S7, S8, Value);
}

StringSwitch &Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2,
StringLiteral S3, StringLiteral S4, StringLiteral S5,
StringLiteral S6, StringLiteral S7, StringLiteral S8,
StringLiteral S9, T Value) {
return Case(S0, Value).Cases(S1, S2, S3, S4, S5, S6, S7, S8, S9, Value);
return CasesImpl(Value, CaseStrings);
}

// Case-insensitive case matchers.
StringSwitch &CaseLower(StringLiteral S, T Value) {
if (!Result && Str.equals_insensitive(S))
Result = std::move(Value);

CaseLowerImpl(Value, S);
return *this;
}

Expand All @@ -159,37 +110,59 @@ class StringSwitch {
return *this;
}

StringSwitch &CasesLower(StringLiteral S0, StringLiteral S1, T Value) {
return CaseLower(S0, Value).CaseLower(S1, Value);
}

StringSwitch &CasesLower(StringLiteral S0, StringLiteral S1, StringLiteral S2,
StringSwitch &CasesLower(std::initializer_list<StringLiteral> CaseStrings,
T Value) {
return CaseLower(S0, Value).CasesLower(S1, S2, Value);
return CasesLowerImpl(Value, CaseStrings);
}

StringSwitch &CasesLower(StringLiteral S0, StringLiteral S1, StringLiteral S2,
StringLiteral S3, T Value) {
return CaseLower(S0, Value).CasesLower(S1, S2, S3, Value);
}

StringSwitch &CasesLower(StringLiteral S0, StringLiteral S1, StringLiteral S2,
StringLiteral S3, StringLiteral S4, T Value) {
return CaseLower(S0, Value).CasesLower(S1, S2, S3, S4, Value);
}

[[nodiscard]]
R Default(T Value) {
[[nodiscard]] R Default(T Value) {
if (Result)
return std::move(*Result);
return Value;
}

[[nodiscard]]
operator R() {
[[nodiscard]] operator R() {
assert(Result && "Fell off the end of a string-switch");
return std::move(*Result);
}

private:
// Returns true when `Str` matches the `S` argument, and stores the result.
bool CaseImpl(T &Value, StringLiteral S) {
if (!Result && Str == S) {
Result = std::move(Value);
return true;
}
return false;
}

// Returns true when `Str` matches the `S` argument (case-insensitive), and
// stores the result.
bool CaseLowerImpl(T &Value, StringLiteral S) {
if (!Result && Str.equals_insensitive(S)) {
Result = std::move(Value);
return true;
}
return false;
}

StringSwitch &CasesImpl(T &Value,
std::initializer_list<StringLiteral> Cases) {
// Stop matching after the string is found.
for (StringLiteral S : Cases)
if (CaseImpl(Value, S))
break;
return *this;
}

StringSwitch &CasesLowerImpl(T &Value,
std::initializer_list<StringLiteral> Cases) {
// Stop matching after the string is found.
for (StringLiteral S : Cases)
if (CaseLowerImpl(Value, S))
break;
return *this;
}
};

} // end namespace llvm
Expand Down