[TextAPI] Switch back to a custom Platform enum.

Moving to PlatformType from BinaryFormat had some UB fallout when handing
unknown platforms or malformed input files.

This should fix the sanitizer bots.

llvm-svn: 347836
This commit is contained in:
Juergen Ributzka 2018-11-29 05:56:03 +00:00
parent c2540995ed
commit b7013d690f
6 changed files with 54 additions and 43 deletions

View File

@ -34,6 +34,16 @@
namespace llvm {
namespace MachO {
/// Defines the list of MachO platforms.
enum class PlatformKind : unsigned {
unknown,
macOS = MachO::PLATFORM_MACOS,
iOS = MachO::PLATFORM_IOS,
tvOS = MachO::PLATFORM_TVOS,
watchOS = MachO::PLATFORM_WATCHOS,
bridgeOS = MachO::PLATFORM_BRIDGEOS,
};
/// Defines a list of Objective-C constraints.
enum class ObjCConstraintType : unsigned {
/// No constraint.
@ -164,10 +174,10 @@ public:
FileType getFileType() const { return FileKind; }
/// Set the platform.
void setPlatform(PlatformType Platform_) { Platform = Platform_; }
void setPlatform(PlatformKind Platform_) { Platform = Platform_; }
/// Get the platform.
PlatformType getPlatform() const { return Platform; }
PlatformKind getPlatform() const { return Platform; }
/// Specify the set of supported architectures by this file.
void setArchitectures(ArchitectureSet Architectures_) {
@ -406,7 +416,7 @@ private:
std::string Path;
FileType FileKind;
PlatformType Platform;
PlatformKind Platform;
ArchitectureSet Architectures;
std::string InstallName;
PackedVersion CurrentVersion;

View File

@ -515,11 +515,11 @@ template <> struct MappingTraits<const InterfaceFile *> {
std::vector<Architecture> Architectures;
std::vector<UUID> UUIDs;
PlatformType Platform;
PlatformKind Platform{PlatformKind::unknown};
StringRef InstallName;
PackedVersion CurrentVersion;
PackedVersion CompatibilityVersion;
SwiftVersion SwiftABIVersion;
SwiftVersion SwiftABIVersion{0};
ObjCConstraintType ObjCConstraint{ObjCConstraintType::None};
TBDFlags Flags{TBDFlags::None};
StringRef ParentUmbrella;

View File

@ -43,43 +43,44 @@ void ScalarEnumerationTraits<ObjCConstraintType>::enumeration(
IO.enumCase(Constraint, "gc", ObjCConstraintType::GC);
}
void ScalarTraits<PlatformType>::output(const PlatformType &Value, void *,
void ScalarTraits<PlatformKind>::output(const PlatformKind &Value, void *,
raw_ostream &OS) {
switch (Value) {
case PLATFORM_MACOS:
default:
llvm_unreachable("unexpected platform");
break;
case PlatformKind::macOS:
OS << "macosx";
break;
case PLATFORM_IOS:
case PlatformKind::iOS:
OS << "ios";
break;
case PLATFORM_WATCHOS:
case PlatformKind::watchOS:
OS << "watchos";
break;
case PLATFORM_TVOS:
case PlatformKind::tvOS:
OS << "tvos";
break;
case PLATFORM_BRIDGEOS:
case PlatformKind::bridgeOS:
OS << "bridgeos";
break;
}
}
StringRef ScalarTraits<PlatformType>::input(StringRef Scalar, void *,
PlatformType &Value) {
int Result = StringSwitch<unsigned>(Scalar)
.Case("macosx", PLATFORM_MACOS)
.Case("ios", PLATFORM_IOS)
.Case("watchos", PLATFORM_WATCHOS)
.Case("tvos", PLATFORM_TVOS)
.Case("bridgeos", PLATFORM_BRIDGEOS)
.Default(0);
StringRef ScalarTraits<PlatformKind>::input(StringRef Scalar, void *,
PlatformKind &Value) {
Value = StringSwitch<PlatformKind>(Scalar)
.Case("macosx", PlatformKind::macOS)
.Case("ios", PlatformKind::iOS)
.Case("watchos", PlatformKind::watchOS)
.Case("tvos", PlatformKind::tvOS)
.Case("bridgeos", PlatformKind::bridgeOS)
.Default(PlatformKind::unknown);
if (!Result)
if (Value == PlatformKind::unknown)
return "unknown platform";
Value = static_cast<PlatformType>(Result);
return {};
}
QuotingType ScalarTraits<PlatformType>::mustQuote(StringRef) {
QuotingType ScalarTraits<PlatformKind>::mustQuote(StringRef) {
return QuotingType::None;
}

View File

@ -43,9 +43,9 @@ template <> struct ScalarEnumerationTraits<MachO::ObjCConstraintType> {
static void enumeration(IO &, MachO::ObjCConstraintType &);
};
template <> struct ScalarTraits<MachO::PlatformType> {
static void output(const MachO::PlatformType &, void *, raw_ostream &);
static StringRef input(StringRef, void *, MachO::PlatformType &);
template <> struct ScalarTraits<MachO::PlatformKind> {
static void output(const MachO::PlatformKind &, void *, raw_ostream &);
static StringRef input(StringRef, void *, MachO::PlatformKind &);
static QuotingType mustQuote(StringRef);
};

View File

@ -90,7 +90,7 @@ TEST(TBDv1, ReadFile) {
auto Archs = Architecture::armv7 | Architecture::armv7s |
Architecture::armv7k | Architecture::arm64;
EXPECT_EQ(Archs, File->getArchitectures());
EXPECT_EQ(PLATFORM_IOS, File->getPlatform());
EXPECT_EQ(PlatformKind::iOS, File->getPlatform());
EXPECT_EQ(std::string("Test.dylib"), File->getInstallName());
EXPECT_EQ(PackedVersion(2, 3, 4), File->getCurrentVersion());
EXPECT_EQ(PackedVersion(1, 0, 0), File->getCompatibilityVersion());
@ -136,7 +136,7 @@ TEST(TBDv1, ReadFile2) {
auto Archs = Architecture::armv7 | Architecture::armv7s |
Architecture::armv7k | Architecture::arm64;
EXPECT_EQ(Archs, File->getArchitectures());
EXPECT_EQ(PLATFORM_IOS, File->getPlatform());
EXPECT_EQ(PlatformKind::iOS, File->getPlatform());
EXPECT_EQ(std::string("Test.dylib"), File->getInstallName());
EXPECT_EQ(PackedVersion(1, 0, 0), File->getCurrentVersion());
EXPECT_EQ(PackedVersion(1, 0, 0), File->getCompatibilityVersion());
@ -177,7 +177,7 @@ TEST(TBDv1, WriteFile) {
File.setInstallName("/usr/lib/libfoo.dylib");
File.setFileType(FileType::TBD_V1);
File.setArchitectures(Architecture::i386 | Architecture::x86_64);
File.setPlatform(PLATFORM_MACOS);
File.setPlatform(PlatformKind::macOS);
File.setCurrentVersion(PackedVersion(1, 2, 3));
File.setSwiftABIVersion(5);
File.setObjCConstraint(ObjCConstraintType::Retain_Release);
@ -213,7 +213,7 @@ TEST(TBDv1, Platform_macOS) {
EXPECT_TRUE(!!Result);
auto File = std::move(Result.get());
EXPECT_EQ(FileType::TBD_V1, File->getFileType());
EXPECT_EQ(PLATFORM_MACOS, File->getPlatform());
EXPECT_EQ(PlatformKind::macOS, File->getPlatform());
}
TEST(TBDv1, Platform_iOS) {
@ -228,7 +228,7 @@ TEST(TBDv1, Platform_iOS) {
EXPECT_TRUE(!!Result);
auto File = std::move(Result.get());
EXPECT_EQ(FileType::TBD_V1, File->getFileType());
EXPECT_EQ(PLATFORM_IOS, File->getPlatform());
EXPECT_EQ(PlatformKind::iOS, File->getPlatform());
}
TEST(TBDv1, Platform_watchOS) {
@ -243,7 +243,7 @@ TEST(TBDv1, Platform_watchOS) {
EXPECT_TRUE(!!Result);
auto File = std::move(Result.get());
EXPECT_EQ(FileType::TBD_V1, File->getFileType());
EXPECT_EQ(PLATFORM_WATCHOS, File->getPlatform());
EXPECT_EQ(PlatformKind::watchOS, File->getPlatform());
}
TEST(TBDv1, Platform_tvOS) {
@ -258,7 +258,7 @@ TEST(TBDv1, Platform_tvOS) {
EXPECT_TRUE(!!Result);
auto File = std::move(Result.get());
EXPECT_EQ(FileType::TBD_V1, File->getFileType());
EXPECT_EQ(PLATFORM_TVOS, File->getPlatform());
EXPECT_EQ(PlatformKind::tvOS, File->getPlatform());
}
TEST(TBDv1, Platform_bridgeOS) {
@ -274,7 +274,7 @@ TEST(TBDv1, Platform_bridgeOS) {
EXPECT_TRUE(!!Result);
auto File = std::move(Result.get());
EXPECT_EQ(FileType::TBD_V1, File->getFileType());
EXPECT_EQ(PLATFORM_BRIDGEOS, File->getPlatform());
EXPECT_EQ(PlatformKind::bridgeOS, File->getPlatform());
}
TEST(TBDv1, Swift_1_0) {

View File

@ -92,7 +92,7 @@ TEST(TBDv2, ReadFile) {
auto Archs = Architecture::armv7 | Architecture::armv7s |
Architecture::armv7k | Architecture::arm64;
EXPECT_EQ(Archs, File->getArchitectures());
EXPECT_EQ(PLATFORM_IOS, File->getPlatform());
EXPECT_EQ(PlatformKind::iOS, File->getPlatform());
EXPECT_EQ(std::string("Test.dylib"), File->getInstallName());
EXPECT_EQ(PackedVersion(2, 3, 4), File->getCurrentVersion());
EXPECT_EQ(PackedVersion(1, 0, 0), File->getCompatibilityVersion());
@ -160,7 +160,7 @@ TEST(TBDv2, ReadFile2) {
auto Archs = Architecture::armv7 | Architecture::armv7s |
Architecture::armv7k | Architecture::arm64;
EXPECT_EQ(Archs, File->getArchitectures());
EXPECT_EQ(PLATFORM_IOS, File->getPlatform());
EXPECT_EQ(PlatformKind::iOS, File->getPlatform());
EXPECT_EQ(std::string("Test.dylib"), File->getInstallName());
EXPECT_EQ(PackedVersion(1, 0, 0), File->getCurrentVersion());
EXPECT_EQ(PackedVersion(1, 0, 0), File->getCompatibilityVersion());
@ -200,7 +200,7 @@ TEST(TBDv2, WriteFile) {
File.setInstallName("/usr/lib/libfoo.dylib");
File.setFileType(FileType::TBD_V2);
File.setArchitectures(Architecture::i386 | Architecture::x86_64);
File.setPlatform(PLATFORM_MACOS);
File.setPlatform(PlatformKind::macOS);
File.setCurrentVersion(PackedVersion(1, 2, 3));
File.setTwoLevelNamespace();
File.setApplicationExtensionSafe();
@ -238,7 +238,7 @@ TEST(TBDv2, Platform_macOS) {
EXPECT_TRUE(!!Result);
auto File = std::move(Result.get());
EXPECT_EQ(FileType::TBD_V2, File->getFileType());
EXPECT_EQ(PLATFORM_MACOS, File->getPlatform());
EXPECT_EQ(PlatformKind::macOS, File->getPlatform());
}
TEST(TBDv2, Platform_iOS) {
@ -253,7 +253,7 @@ TEST(TBDv2, Platform_iOS) {
EXPECT_TRUE(!!Result);
auto File = std::move(Result.get());
EXPECT_EQ(FileType::TBD_V2, File->getFileType());
EXPECT_EQ(PLATFORM_IOS, File->getPlatform());
EXPECT_EQ(PlatformKind::iOS, File->getPlatform());
}
TEST(TBDv2, Platform_watchOS) {
@ -268,7 +268,7 @@ TEST(TBDv2, Platform_watchOS) {
EXPECT_TRUE(!!Result);
auto File = std::move(Result.get());
EXPECT_EQ(FileType::TBD_V2, File->getFileType());
EXPECT_EQ(PLATFORM_WATCHOS, File->getPlatform());
EXPECT_EQ(PlatformKind::watchOS, File->getPlatform());
}
TEST(TBDv2, Platform_tvOS) {
@ -283,7 +283,7 @@ TEST(TBDv2, Platform_tvOS) {
EXPECT_TRUE(!!Result);
auto File = std::move(Result.get());
EXPECT_EQ(FileType::TBD_V2, File->getFileType());
EXPECT_EQ(PLATFORM_TVOS, File->getPlatform());
EXPECT_EQ(PlatformKind::tvOS, File->getPlatform());
}
TEST(TBDv2, Platform_bridgeOS) {
@ -299,7 +299,7 @@ TEST(TBDv2, Platform_bridgeOS) {
EXPECT_TRUE(!!Result);
auto File = std::move(Result.get());
EXPECT_EQ(FileType::TBD_V2, File->getFileType());
EXPECT_EQ(PLATFORM_BRIDGEOS, File->getPlatform());
EXPECT_EQ(PlatformKind::bridgeOS, File->getPlatform());
}
TEST(TBDv2, Swift_1_0) {