Skip to content

Commit 64c3e8e

Browse files
ributzkacyndyishida
authored andcommitted
[llvm] Use XMACROS for MachO platforms. (llvm#69262)
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)
1 parent 36f818b commit 64c3e8e

File tree

9 files changed

+57
-117
lines changed

9 files changed

+57
-117
lines changed

clang/lib/CodeGen/CGObjC.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3970,7 +3970,7 @@ static unsigned getBaseMachOPlatformID(const llvm::Triple &TT) {
39703970
case llvm::Triple::DriverKit:
39713971
return llvm::MachO::PLATFORM_DRIVERKIT;
39723972
default:
3973-
return /*Unknown platform*/ 0;
3973+
return llvm::MachO::PLATFORM_UNKNOWN;
39743974
}
39753975
}
39763976

llvm/include/llvm/BinaryFormat/MachO.def

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,5 +120,21 @@ LOAD_COMMAND_STRUCT(fileset_entry_command)
120120

121121
#endif
122122

123+
#ifdef PLATFORM
124+
// PLATFORM(platform, id, name, build_name, target, tapi_target, marketing)
125+
PLATFORM(UNKNOWN, 0, unknown, unknown, unknown, unknown, unknown)
126+
PLATFORM(MACOS, 1, macos, macos, macos, macos, macOS)
127+
PLATFORM(IOS, 2, ios, ios, ios, ios, iOS)
128+
PLATFORM(TVOS, 3, tvos, tvos, tvos, tvos, tvOS)
129+
PLATFORM(WATCHOS, 4, watchos, watchos, watchos, watchos, watchOS)
130+
PLATFORM(BRIDGEOS, 5, bridgeos, bridgeos, bridgeos, bridgeos, bridgeOS)
131+
PLATFORM(MACCATALYST, 6, macCatalyst, macCatalyst, ios-macabi, maccatalyst, macCatalyst)
132+
PLATFORM(IOSSIMULATOR, 7, iossimulator, iossimulator, ios-simulator, ios-simulator, iOS Simulator)
133+
PLATFORM(TVOSSIMULATOR, 8, tvossimulator, tvossimulator, tvos-simulator, tvos-simulator, tvOS Simulator)
134+
PLATFORM(WATCHOSSIMULATOR, 9, watchossimulator, watchossimulator, watchos-simulator, watchos-simulator, watchOS Simulator)
135+
PLATFORM(DRIVERKIT, 10, driverkit, driverkit, driverkit, driverkit, DriverKit)
136+
#endif
137+
123138
#undef HANDLE_LOAD_COMMAND
124139
#undef LOAD_COMMAND_STRUCT
140+
#undef PLATFORM

llvm/include/llvm/BinaryFormat/MachO.h

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -498,17 +498,10 @@ enum { VM_PROT_READ = 0x1, VM_PROT_WRITE = 0x2, VM_PROT_EXECUTE = 0x4 };
498498

499499
// Values for platform field in build_version_command.
500500
enum PlatformType {
501-
PLATFORM_UNKNOWN = 0,
502-
PLATFORM_MACOS = 1,
503-
PLATFORM_IOS = 2,
504-
PLATFORM_TVOS = 3,
505-
PLATFORM_WATCHOS = 4,
506-
PLATFORM_BRIDGEOS = 5,
507-
PLATFORM_MACCATALYST = 6,
508-
PLATFORM_IOSSIMULATOR = 7,
509-
PLATFORM_TVOSSIMULATOR = 8,
510-
PLATFORM_WATCHOSSIMULATOR = 9,
511-
PLATFORM_DRIVERKIT = 10,
501+
#define PLATFORM(platform, id, name, build_name, target, tapi_target, \
502+
marketing) \
503+
PLATFORM_##platform = id,
504+
#include "MachO.def"
512505
};
513506

514507
// Values for tools enum in build_tool_version.

llvm/include/llvm/Object/MachO.h

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -784,16 +784,11 @@ class MachOObjectFile : public ObjectFile {
784784

785785
static std::string getBuildPlatform(uint32_t platform) {
786786
switch (platform) {
787-
case MachO::PLATFORM_MACOS: return "macos";
788-
case MachO::PLATFORM_IOS: return "ios";
789-
case MachO::PLATFORM_TVOS: return "tvos";
790-
case MachO::PLATFORM_WATCHOS: return "watchos";
791-
case MachO::PLATFORM_BRIDGEOS: return "bridgeos";
792-
case MachO::PLATFORM_MACCATALYST: return "macCatalyst";
793-
case MachO::PLATFORM_IOSSIMULATOR: return "iossimulator";
794-
case MachO::PLATFORM_TVOSSIMULATOR: return "tvossimulator";
795-
case MachO::PLATFORM_WATCHOSSIMULATOR: return "watchossimulator";
796-
case MachO::PLATFORM_DRIVERKIT: return "driverkit";
787+
#define PLATFORM(platform, id, name, build_name, target, tapi_target, \
788+
marketing) \
789+
case MachO::PLATFORM_##platform: \
790+
return #name;
791+
#include "llvm/BinaryFormat/MachO.def"
797792
default:
798793
std::string ret;
799794
raw_string_ostream ss(ret);

llvm/lib/MC/MCAsmStreamer.cpp

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -631,18 +631,11 @@ void MCAsmStreamer::emitVersionMin(MCVersionMinType Type, unsigned Major,
631631

632632
static const char *getPlatformName(MachO::PlatformType Type) {
633633
switch (Type) {
634-
case MachO::PLATFORM_UNKNOWN: /* silence warning*/
635-
break;
636-
case MachO::PLATFORM_MACOS: return "macos";
637-
case MachO::PLATFORM_IOS: return "ios";
638-
case MachO::PLATFORM_TVOS: return "tvos";
639-
case MachO::PLATFORM_WATCHOS: return "watchos";
640-
case MachO::PLATFORM_BRIDGEOS: return "bridgeos";
641-
case MachO::PLATFORM_MACCATALYST: return "macCatalyst";
642-
case MachO::PLATFORM_IOSSIMULATOR: return "iossimulator";
643-
case MachO::PLATFORM_TVOSSIMULATOR: return "tvossimulator";
644-
case MachO::PLATFORM_WATCHOSSIMULATOR: return "watchossimulator";
645-
case MachO::PLATFORM_DRIVERKIT: return "driverkit";
634+
#define PLATFORM(platform, id, name, build_name, target, tapi_target, \
635+
marketing) \
636+
case MachO::PLATFORM_##platform: \
637+
return #build_name;
638+
#include "llvm/BinaryFormat/MachO.def"
646639
}
647640
llvm_unreachable("Invalid Mach-O platform type");
648641
}

llvm/lib/MC/MCParser/DarwinAsmParser.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,14 +1174,13 @@ bool DarwinAsmParser::parseBuildVersion(StringRef Directive, SMLoc Loc) {
11741174
return TokError("platform name expected");
11751175

11761176
unsigned Platform = StringSwitch<unsigned>(PlatformName)
1177-
.Case("macos", MachO::PLATFORM_MACOS)
1178-
.Case("ios", MachO::PLATFORM_IOS)
1179-
.Case("tvos", MachO::PLATFORM_TVOS)
1180-
.Case("watchos", MachO::PLATFORM_WATCHOS)
1181-
.Case("macCatalyst", MachO::PLATFORM_MACCATALYST)
1182-
.Case("driverkit", MachO::PLATFORM_DRIVERKIT)
1183-
.Default(0);
1184-
if (Platform == 0)
1177+
#define PLATFORM(platform, id, name, build_name, target, tapi_target, \
1178+
marketing) \
1179+
.Case(#build_name, MachO::PLATFORM_##platform)
1180+
#include "llvm/BinaryFormat/MachO.def"
1181+
.Default(MachO::PLATFORM_UNKNOWN);
1182+
1183+
if (Platform == MachO::PLATFORM_UNKNOWN)
11851184
return Error(PlatformLoc, "unknown platform name");
11861185

11871186
if (getLexer().isNot(AsmToken::Comma))

llvm/lib/TextAPI/Platform.cpp

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -62,45 +62,22 @@ PlatformSet mapToPlatformSet(ArrayRef<Triple> Targets) {
6262

6363
StringRef getPlatformName(PlatformType Platform) {
6464
switch (Platform) {
65-
case PLATFORM_UNKNOWN:
66-
return "unknown";
67-
case PLATFORM_MACOS:
68-
return "macOS";
69-
case PLATFORM_IOS:
70-
return "iOS";
71-
case PLATFORM_TVOS:
72-
return "tvOS";
73-
case PLATFORM_WATCHOS:
74-
return "watchOS";
75-
case PLATFORM_BRIDGEOS:
76-
return "bridgeOS";
77-
case PLATFORM_MACCATALYST:
78-
return "macCatalyst";
79-
case PLATFORM_IOSSIMULATOR:
80-
return "iOS Simulator";
81-
case PLATFORM_TVOSSIMULATOR:
82-
return "tvOS Simulator";
83-
case PLATFORM_WATCHOSSIMULATOR:
84-
return "watchOS Simulator";
85-
case PLATFORM_DRIVERKIT:
86-
return "DriverKit";
65+
#define PLATFORM(platform, id, name, build_name, target, tapi_target, \
66+
marketing) \
67+
case PLATFORM_##platform: \
68+
return #marketing;
69+
#include "llvm/BinaryFormat/MachO.def"
8770
}
8871
llvm_unreachable("Unknown llvm::MachO::PlatformType enum");
8972
}
9073

9174
PlatformType getPlatformFromName(StringRef Name) {
9275
return StringSwitch<PlatformType>(Name)
9376
.Case("osx", PLATFORM_MACOS)
94-
.Case("macos", PLATFORM_MACOS)
95-
.Case("ios", PLATFORM_IOS)
96-
.Case("tvos", PLATFORM_TVOS)
97-
.Case("watchos", PLATFORM_WATCHOS)
98-
.Case("bridgeos", PLATFORM_BRIDGEOS)
99-
.Case("ios-macabi", PLATFORM_MACCATALYST)
100-
.Case("ios-simulator", PLATFORM_IOSSIMULATOR)
101-
.Case("tvos-simulator", PLATFORM_TVOSSIMULATOR)
102-
.Case("watchos-simulator", PLATFORM_WATCHOSSIMULATOR)
103-
.Case("driverkit", PLATFORM_DRIVERKIT)
77+
#define PLATFORM(platform, id, name, build_name, target, tapi_target, \
78+
marketing) \
79+
.Case(#tapi_target, PLATFORM_##platform)
80+
#include "llvm/BinaryFormat/MachO.def"
10481
.Default(PLATFORM_UNKNOWN);
10582
}
10683

llvm/lib/TextAPI/Target.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,10 @@ Expected<Target> Target::create(StringRef TargetValue) {
2121
auto PlatformStr = Result.second;
2222
PlatformType Platform;
2323
Platform = StringSwitch<PlatformType>(PlatformStr)
24-
.Case("macos", PLATFORM_MACOS)
25-
.Case("ios", PLATFORM_IOS)
26-
.Case("tvos", PLATFORM_TVOS)
27-
.Case("watchos", PLATFORM_WATCHOS)
28-
.Case("bridgeos", PLATFORM_BRIDGEOS)
29-
.Case("maccatalyst", PLATFORM_MACCATALYST)
30-
.Case("ios-simulator", PLATFORM_IOSSIMULATOR)
31-
.Case("tvos-simulator", PLATFORM_TVOSSIMULATOR)
32-
.Case("watchos-simulator", PLATFORM_WATCHOSSIMULATOR)
33-
.Case("driverkit", PLATFORM_DRIVERKIT)
24+
#define PLATFORM(platform, id, name, build_name, target, tapi_target, \
25+
marketing) \
26+
.Case(#tapi_target, PLATFORM_##platform)
27+
#include "llvm/BinaryFormat/MachO.def"
3428
.Default(PLATFORM_UNKNOWN);
3529

3630
if (Platform == PLATFORM_UNKNOWN) {

llvm/lib/TextAPI/TextStub.cpp

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -369,39 +369,12 @@ template <> struct ScalarTraits<Target> {
369369
static void output(const Target &Value, void *, raw_ostream &OS) {
370370
OS << Value.Arch << "-";
371371
switch (Value.Platform) {
372-
default:
373-
OS << "unknown";
374-
break;
375-
case PLATFORM_MACOS:
376-
OS << "macos";
377-
break;
378-
case PLATFORM_IOS:
379-
OS << "ios";
380-
break;
381-
case PLATFORM_TVOS:
382-
OS << "tvos";
383-
break;
384-
case PLATFORM_WATCHOS:
385-
OS << "watchos";
386-
break;
387-
case PLATFORM_BRIDGEOS:
388-
OS << "bridgeos";
389-
break;
390-
case PLATFORM_MACCATALYST:
391-
OS << "maccatalyst";
392-
break;
393-
case PLATFORM_IOSSIMULATOR:
394-
OS << "ios-simulator";
395-
break;
396-
case PLATFORM_TVOSSIMULATOR:
397-
OS << "tvos-simulator";
398-
break;
399-
case PLATFORM_WATCHOSSIMULATOR:
400-
OS << "watchos-simulator";
401-
break;
402-
case PLATFORM_DRIVERKIT:
403-
OS << "driverkit";
404-
break;
372+
#define PLATFORM(platform, id, name, build_name, target, tapi_target, \
373+
marketing) \
374+
case PLATFORM_##platform: \
375+
OS << #tapi_target; \
376+
break;
377+
#include "llvm/BinaryFormat/MachO.def"
405378
}
406379
}
407380

0 commit comments

Comments
 (0)