-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[llvm] Use XMACROS for MachO platforms. #69262
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@llvm/pr-subscribers-mc @llvm/pr-subscribers-clang-codegen Author: Juergen Ributzka (ributzka) ChangesThis change adds the PLATFORM XMACRO to simplify the addition of new MachO Full diff: https://github.com/llvm/llvm-project/pull/69262.diff 9 Files Affected:
diff --git a/clang/lib/CodeGen/CGObjC.cpp b/clang/lib/CodeGen/CGObjC.cpp
index 6c594b5db4bca1f..aa3a0ff57003d7c 100644
--- a/clang/lib/CodeGen/CGObjC.cpp
+++ b/clang/lib/CodeGen/CGObjC.cpp
@@ -3954,7 +3954,7 @@ static unsigned getBaseMachOPlatformID(const llvm::Triple &TT) {
case llvm::Triple::DriverKit:
return llvm::MachO::PLATFORM_DRIVERKIT;
default:
- return /*Unknown platform*/ 0;
+ return llvm::MachO::PLATFORM_UNKNOWN;
}
}
diff --git a/llvm/include/llvm/BinaryFormat/MachO.def b/llvm/include/llvm/BinaryFormat/MachO.def
index d841b42ee808b2e..70966952ca7efe1 100644
--- a/llvm/include/llvm/BinaryFormat/MachO.def
+++ b/llvm/include/llvm/BinaryFormat/MachO.def
@@ -120,5 +120,21 @@ LOAD_COMMAND_STRUCT(fileset_entry_command)
#endif
+#ifdef PLATFORM
+// PLATFORM(platform, id, name, target, tapi_target, marketing)
+PLATFORM(UNKNOWN, 0, unknown, unknown, unknown, unknown)
+PLATFORM(MACOS, 1, macos, macos, macos, macOS)
+PLATFORM(IOS, 2, ios, ios, ios, iOS)
+PLATFORM(TVOS, 3, tvos, tvos, tvos, tvOS)
+PLATFORM(WATCHOS, 4, watchos, watchos, watchos, watchOS)
+PLATFORM(BRIDGEOS, 5, bridgeos, bridgeos, bridgeos, bridgeOS)
+PLATFORM(MACCATALYST, 6, macCatalyst, ios-macabi, maccatalyst, macCatalyst)
+PLATFORM(IOSSIMULATOR, 7, iossimulator, ios-simulator, ios-simulator, iOS Simulator)
+PLATFORM(TVOSSIMULATOR, 8, tvossimulator, tvos-simulator, tvos-simulator, tvOS Simulator)
+PLATFORM(WATCHOSSIMULATOR, 9, watchossimulator, watchos-simulator, watchos-simulator, watchOS Simulator)
+PLATFORM(DRIVERKIT, 10, driverkit, driverkit, driverkit, DriverKit)
+#endif
+
#undef HANDLE_LOAD_COMMAND
#undef LOAD_COMMAND_STRUCT
+#undef PLATFORM
diff --git a/llvm/include/llvm/BinaryFormat/MachO.h b/llvm/include/llvm/BinaryFormat/MachO.h
index f59cd14c1b5c055..6dfc115c7e905fc 100644
--- a/llvm/include/llvm/BinaryFormat/MachO.h
+++ b/llvm/include/llvm/BinaryFormat/MachO.h
@@ -497,17 +497,9 @@ enum { VM_PROT_READ = 0x1, VM_PROT_WRITE = 0x2, VM_PROT_EXECUTE = 0x4 };
// Values for platform field in build_version_command.
enum PlatformType {
- PLATFORM_UNKNOWN = 0,
- PLATFORM_MACOS = 1,
- PLATFORM_IOS = 2,
- PLATFORM_TVOS = 3,
- PLATFORM_WATCHOS = 4,
- PLATFORM_BRIDGEOS = 5,
- PLATFORM_MACCATALYST = 6,
- PLATFORM_IOSSIMULATOR = 7,
- PLATFORM_TVOSSIMULATOR = 8,
- PLATFORM_WATCHOSSIMULATOR = 9,
- PLATFORM_DRIVERKIT = 10,
+#define PLATFORM(platform, id, name, target, tapi_target, marketing) \
+ PLATFORM_##platform = id,
+#include "MachO.def"
};
// Values for tools enum in build_tool_version.
diff --git a/llvm/include/llvm/Object/MachO.h b/llvm/include/llvm/Object/MachO.h
index 894252db538f9e7..7bb2be76ff5b865 100644
--- a/llvm/include/llvm/Object/MachO.h
+++ b/llvm/include/llvm/Object/MachO.h
@@ -789,16 +789,10 @@ class MachOObjectFile : public ObjectFile {
static std::string getBuildPlatform(uint32_t platform) {
switch (platform) {
- case MachO::PLATFORM_MACOS: return "macos";
- case MachO::PLATFORM_IOS: return "ios";
- case MachO::PLATFORM_TVOS: return "tvos";
- case MachO::PLATFORM_WATCHOS: return "watchos";
- case MachO::PLATFORM_BRIDGEOS: return "bridgeos";
- case MachO::PLATFORM_MACCATALYST: return "macCatalyst";
- case MachO::PLATFORM_IOSSIMULATOR: return "iossimulator";
- case MachO::PLATFORM_TVOSSIMULATOR: return "tvossimulator";
- case MachO::PLATFORM_WATCHOSSIMULATOR: return "watchossimulator";
- case MachO::PLATFORM_DRIVERKIT: return "driverkit";
+#define PLATFORM(platform, id, name, target, tapi_target, marketing) \
+ case MachO::PLATFORM_##platform: \
+ return #name;
+#include "llvm/BinaryFormat/MachO.def"
default:
std::string ret;
raw_string_ostream ss(ret);
diff --git a/llvm/lib/MC/MCAsmStreamer.cpp b/llvm/lib/MC/MCAsmStreamer.cpp
index 06de70ad2f395a2..01fc90513daca39 100644
--- a/llvm/lib/MC/MCAsmStreamer.cpp
+++ b/llvm/lib/MC/MCAsmStreamer.cpp
@@ -629,18 +629,10 @@ void MCAsmStreamer::emitVersionMin(MCVersionMinType Type, unsigned Major,
static const char *getPlatformName(MachO::PlatformType Type) {
switch (Type) {
- case MachO::PLATFORM_UNKNOWN: /* silence warning*/
- break;
- case MachO::PLATFORM_MACOS: return "macos";
- case MachO::PLATFORM_IOS: return "ios";
- case MachO::PLATFORM_TVOS: return "tvos";
- case MachO::PLATFORM_WATCHOS: return "watchos";
- case MachO::PLATFORM_BRIDGEOS: return "bridgeos";
- case MachO::PLATFORM_MACCATALYST: return "macCatalyst";
- case MachO::PLATFORM_IOSSIMULATOR: return "iossimulator";
- case MachO::PLATFORM_TVOSSIMULATOR: return "tvossimulator";
- case MachO::PLATFORM_WATCHOSSIMULATOR: return "watchossimulator";
- case MachO::PLATFORM_DRIVERKIT: return "driverkit";
+#define PLATFORM(platform, id, name, target, tapi_target, marketing) \
+ case MachO::PLATFORM_##platform: \
+ return #name;
+#include "llvm/BinaryFormat/MachO.def"
}
llvm_unreachable("Invalid Mach-O platform type");
}
diff --git a/llvm/lib/MC/MCParser/DarwinAsmParser.cpp b/llvm/lib/MC/MCParser/DarwinAsmParser.cpp
index 7c390041b3698a4..287af822311cb63 100644
--- a/llvm/lib/MC/MCParser/DarwinAsmParser.cpp
+++ b/llvm/lib/MC/MCParser/DarwinAsmParser.cpp
@@ -1167,14 +1167,12 @@ bool DarwinAsmParser::parseBuildVersion(StringRef Directive, SMLoc Loc) {
return TokError("platform name expected");
unsigned Platform = StringSwitch<unsigned>(PlatformName)
- .Case("macos", MachO::PLATFORM_MACOS)
- .Case("ios", MachO::PLATFORM_IOS)
- .Case("tvos", MachO::PLATFORM_TVOS)
- .Case("watchos", MachO::PLATFORM_WATCHOS)
- .Case("macCatalyst", MachO::PLATFORM_MACCATALYST)
- .Case("driverkit", MachO::PLATFORM_DRIVERKIT)
- .Default(0);
- if (Platform == 0)
+ #define PLATFORM(platform, id, name, target, tapi_target, marketing) \
+ .Case(#name, MachO::PLATFORM_##platform)
+ #include "llvm/BinaryFormat/MachO.def"
+ .Default(MachO::PLATFORM_UNKNOWN);
+
+ if (Platform == MachO::PLATFORM_UNKNOWN)
return Error(PlatformLoc, "unknown platform name");
if (getLexer().isNot(AsmToken::Comma))
diff --git a/llvm/lib/TextAPI/Platform.cpp b/llvm/lib/TextAPI/Platform.cpp
index d0575847a876a75..963acd2fe9f381c 100644
--- a/llvm/lib/TextAPI/Platform.cpp
+++ b/llvm/lib/TextAPI/Platform.cpp
@@ -62,28 +62,10 @@ PlatformSet mapToPlatformSet(ArrayRef<Triple> Targets) {
StringRef getPlatformName(PlatformType Platform) {
switch (Platform) {
- case PLATFORM_UNKNOWN:
- return "unknown";
- case PLATFORM_MACOS:
- return "macOS";
- case PLATFORM_IOS:
- return "iOS";
- case PLATFORM_TVOS:
- return "tvOS";
- case PLATFORM_WATCHOS:
- return "watchOS";
- case PLATFORM_BRIDGEOS:
- return "bridgeOS";
- case PLATFORM_MACCATALYST:
- return "macCatalyst";
- case PLATFORM_IOSSIMULATOR:
- return "iOS Simulator";
- case PLATFORM_TVOSSIMULATOR:
- return "tvOS Simulator";
- case PLATFORM_WATCHOSSIMULATOR:
- return "watchOS Simulator";
- case PLATFORM_DRIVERKIT:
- return "DriverKit";
+#define PLATFORM(platform, id, name, target, tapi_target, marketing) \
+ case PLATFORM_##platform: \
+ return #marketing;
+#include "llvm/BinaryFormat/MachO.def"
}
llvm_unreachable("Unknown llvm::MachO::PlatformType enum");
}
@@ -91,16 +73,9 @@ StringRef getPlatformName(PlatformType Platform) {
PlatformType getPlatformFromName(StringRef Name) {
return StringSwitch<PlatformType>(Name)
.Case("osx", PLATFORM_MACOS)
- .Case("macos", PLATFORM_MACOS)
- .Case("ios", PLATFORM_IOS)
- .Case("tvos", PLATFORM_TVOS)
- .Case("watchos", PLATFORM_WATCHOS)
- .Case("bridgeos", PLATFORM_BRIDGEOS)
- .Case("ios-macabi", PLATFORM_MACCATALYST)
- .Case("ios-simulator", PLATFORM_IOSSIMULATOR)
- .Case("tvos-simulator", PLATFORM_TVOSSIMULATOR)
- .Case("watchos-simulator", PLATFORM_WATCHOSSIMULATOR)
- .Case("driverkit", PLATFORM_DRIVERKIT)
+#define PLATFORM(platform, id, name, target, tapi_target, marketing) \
+ .Case(#tapi_target, PLATFORM_##platform)
+#include "llvm/BinaryFormat/MachO.def"
.Default(PLATFORM_UNKNOWN);
}
diff --git a/llvm/lib/TextAPI/Target.cpp b/llvm/lib/TextAPI/Target.cpp
index e20842498331490..4ac442ea99d3cd1 100644
--- a/llvm/lib/TextAPI/Target.cpp
+++ b/llvm/lib/TextAPI/Target.cpp
@@ -21,16 +21,9 @@ Expected<Target> Target::create(StringRef TargetValue) {
auto PlatformStr = Result.second;
PlatformType Platform;
Platform = StringSwitch<PlatformType>(PlatformStr)
- .Case("macos", PLATFORM_MACOS)
- .Case("ios", PLATFORM_IOS)
- .Case("tvos", PLATFORM_TVOS)
- .Case("watchos", PLATFORM_WATCHOS)
- .Case("bridgeos", PLATFORM_BRIDGEOS)
- .Case("maccatalyst", PLATFORM_MACCATALYST)
- .Case("ios-simulator", PLATFORM_IOSSIMULATOR)
- .Case("tvos-simulator", PLATFORM_TVOSSIMULATOR)
- .Case("watchos-simulator", PLATFORM_WATCHOSSIMULATOR)
- .Case("driverkit", PLATFORM_DRIVERKIT)
+#define PLATFORM(platform, id, name, target, tapi_target, marketing) \
+ .Case(#tapi_target, PLATFORM_##platform)
+#include "llvm/BinaryFormat/MachO.def"
.Default(PLATFORM_UNKNOWN);
if (Platform == PLATFORM_UNKNOWN) {
diff --git a/llvm/lib/TextAPI/TextStub.cpp b/llvm/lib/TextAPI/TextStub.cpp
index 3b94f084b538c54..2eb67b8fb56078b 100644
--- a/llvm/lib/TextAPI/TextStub.cpp
+++ b/llvm/lib/TextAPI/TextStub.cpp
@@ -367,39 +367,11 @@ template <> struct ScalarTraits<Target> {
static void output(const Target &Value, void *, raw_ostream &OS) {
OS << Value.Arch << "-";
switch (Value.Platform) {
- default:
- OS << "unknown";
- break;
- case PLATFORM_MACOS:
- OS << "macos";
- break;
- case PLATFORM_IOS:
- OS << "ios";
- break;
- case PLATFORM_TVOS:
- OS << "tvos";
- break;
- case PLATFORM_WATCHOS:
- OS << "watchos";
- break;
- case PLATFORM_BRIDGEOS:
- OS << "bridgeos";
- break;
- case PLATFORM_MACCATALYST:
- OS << "maccatalyst";
- break;
- case PLATFORM_IOSSIMULATOR:
- OS << "ios-simulator";
- break;
- case PLATFORM_TVOSSIMULATOR:
- OS << "tvos-simulator";
- break;
- case PLATFORM_WATCHOSSIMULATOR:
- OS << "watchos-simulator";
- break;
- case PLATFORM_DRIVERKIT:
- OS << "driverkit";
- break;
+#define PLATFORM(platform, id, name, target, tapi_target, marketing) \
+ case PLATFORM_##platform: \
+ OS << #tapi_target; \
+ break;
+#include "llvm/BinaryFormat/MachO.def"
}
}
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
✅ With the latest revision this PR passed the C/C++ code formatter. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like how this centralizes everything in a single place and the def file format seems like a natural fit for the platforms.
Unrelated build issue on windows: |
This change adds the PLATFORM XMACRO to simplify the addition of new MachO platforms and reduce the number of required changes. Many of the changes needed for adding a new platform are mechanical, such as adding new cases to a switch statement. This will help streamline the process by consolidating much of the necessary information into the MachO.def file.
036206a
to
ceaf58b
Compare
Thanks for the reviews. Checks are good now too after rebase. |
This change adds the PLATFORM XMACRO to simplify the addition of new MachO platforms and reduce the number of required changes. Many of the changes needed for adding a new platform are mechanical, such as adding new cases to a switch statement. This will help streamline the process by consolidating much of the necessary information into the MachO.def file.
This change adds the PLATFORM XMACRO to simplify the addition of new MachO platforms and reduce the number of required changes. Many of the changes needed for adding a new platform are mechanical, such as adding new cases to a switch statement. This will help streamline the process by consolidating much of the necessary information into the MachO.def file. (cherry picked from commit bde2e69)
This change adds the PLATFORM XMACRO to simplify the addition of new MachO platforms and reduce the number of required changes. Many of the changes needed for adding a new platform are mechanical, such as adding new cases to a switch statement. This will help streamline the process by consolidating much of the necessary information into the MachO.def file. (cherry picked from commit bde2e69)
This change adds the PLATFORM XMACRO to simplify the addition of new MachO
platforms and reduce the number of required changes. Many of the changes needed
for adding a new platform are mechanical, such as adding new cases to a switch
statement. This will help streamline the process by consolidating much of the
necessary information into the MachO.def file.