Skip to content

Commit ceaf58b

Browse files
committed
[llvm] Use XMACROS for MachO platforms.
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.
1 parent 5070c1e commit ceaf58b

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
@@ -3954,7 +3954,7 @@ static unsigned getBaseMachOPlatformID(const llvm::Triple &TT) {
39543954
case llvm::Triple::DriverKit:
39553955
return llvm::MachO::PLATFORM_DRIVERKIT;
39563956
default:
3957-
return /*Unknown platform*/ 0;
3957+
return llvm::MachO::PLATFORM_UNKNOWN;
39583958
}
39593959
}
39603960

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
@@ -497,17 +497,10 @@ enum { VM_PROT_READ = 0x1, VM_PROT_WRITE = 0x2, VM_PROT_EXECUTE = 0x4 };
497497

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

513506
// 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
@@ -789,16 +789,11 @@ class MachOObjectFile : public ObjectFile {
789789

790790
static std::string getBuildPlatform(uint32_t platform) {
791791
switch (platform) {
792-
case MachO::PLATFORM_MACOS: return "macos";
793-
case MachO::PLATFORM_IOS: return "ios";
794-
case MachO::PLATFORM_TVOS: return "tvos";
795-
case MachO::PLATFORM_WATCHOS: return "watchos";
796-
case MachO::PLATFORM_BRIDGEOS: return "bridgeos";
797-
case MachO::PLATFORM_MACCATALYST: return "macCatalyst";
798-
case MachO::PLATFORM_IOSSIMULATOR: return "iossimulator";
799-
case MachO::PLATFORM_TVOSSIMULATOR: return "tvossimulator";
800-
case MachO::PLATFORM_WATCHOSSIMULATOR: return "watchossimulator";
801-
case MachO::PLATFORM_DRIVERKIT: return "driverkit";
792+
#define PLATFORM(platform, id, name, build_name, target, tapi_target, \
793+
marketing) \
794+
case MachO::PLATFORM_##platform: \
795+
return #name;
796+
#include "llvm/BinaryFormat/MachO.def"
802797
default:
803798
std::string ret;
804799
raw_string_ostream ss(ret);

llvm/lib/MC/MCAsmStreamer.cpp

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

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

llvm/lib/MC/MCParser/DarwinAsmParser.cpp

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

11691169
unsigned Platform = StringSwitch<unsigned>(PlatformName)
1170-
.Case("macos", MachO::PLATFORM_MACOS)
1171-
.Case("ios", MachO::PLATFORM_IOS)
1172-
.Case("tvos", MachO::PLATFORM_TVOS)
1173-
.Case("watchos", MachO::PLATFORM_WATCHOS)
1174-
.Case("macCatalyst", MachO::PLATFORM_MACCATALYST)
1175-
.Case("driverkit", MachO::PLATFORM_DRIVERKIT)
1176-
.Default(0);
1177-
if (Platform == 0)
1170+
#define PLATFORM(platform, id, name, build_name, target, tapi_target, \
1171+
marketing) \
1172+
.Case(#build_name, MachO::PLATFORM_##platform)
1173+
#include "llvm/BinaryFormat/MachO.def"
1174+
.Default(MachO::PLATFORM_UNKNOWN);
1175+
1176+
if (Platform == MachO::PLATFORM_UNKNOWN)
11781177
return Error(PlatformLoc, "unknown platform name");
11791178

11801179
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
@@ -367,39 +367,12 @@ template <> struct ScalarTraits<Target> {
367367
static void output(const Target &Value, void *, raw_ostream &OS) {
368368
OS << Value.Arch << "-";
369369
switch (Value.Platform) {
370-
default:
371-
OS << "unknown";
372-
break;
373-
case PLATFORM_MACOS:
374-
OS << "macos";
375-
break;
376-
case PLATFORM_IOS:
377-
OS << "ios";
378-
break;
379-
case PLATFORM_TVOS:
380-
OS << "tvos";
381-
break;
382-
case PLATFORM_WATCHOS:
383-
OS << "watchos";
384-
break;
385-
case PLATFORM_BRIDGEOS:
386-
OS << "bridgeos";
387-
break;
388-
case PLATFORM_MACCATALYST:
389-
OS << "maccatalyst";
390-
break;
391-
case PLATFORM_IOSSIMULATOR:
392-
OS << "ios-simulator";
393-
break;
394-
case PLATFORM_TVOSSIMULATOR:
395-
OS << "tvos-simulator";
396-
break;
397-
case PLATFORM_WATCHOSSIMULATOR:
398-
OS << "watchos-simulator";
399-
break;
400-
case PLATFORM_DRIVERKIT:
401-
OS << "driverkit";
402-
break;
370+
#define PLATFORM(platform, id, name, build_name, target, tapi_target, \
371+
marketing) \
372+
case PLATFORM_##platform: \
373+
OS << #tapi_target; \
374+
break;
375+
#include "llvm/BinaryFormat/MachO.def"
403376
}
404377
}
405378

0 commit comments

Comments
 (0)