Skip to content

Commit b572035

Browse files
author
Alexander Shaposhnikov
committed
[lld][MachO] Refactor findCommand
Refactor findCommand to allow passing multiple types. NFC. Test plan: make check-lld-macho Differential revision: https://reviews.llvm.org/D100954
1 parent 55ee541 commit b572035

File tree

3 files changed

+26
-22
lines changed

3 files changed

+26
-22
lines changed

lld/MachO/InputFiles.cpp

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -109,29 +109,31 @@ static Optional<PlatformInfo> getPlatformInfo(const InputFile *input) {
109109

110110
using Header = typename LP::mach_header;
111111
auto *hdr = reinterpret_cast<const Header *>(input->mb.getBufferStart());
112+
112113
PlatformInfo platformInfo;
113114
if (const auto *cmd =
114115
findCommand<build_version_command>(hdr, LC_BUILD_VERSION)) {
115116
platformInfo.target.Platform = static_cast<PlatformKind>(cmd->platform);
116117
platformInfo.minimum = decodeVersion(cmd->minos);
117118
return platformInfo;
118-
} else if (const auto *cmd =
119-
findCommand<version_min_command>(hdr, LC_VERSION_MIN_MACOSX)) {
120-
platformInfo.target.Platform = PlatformKind::macOS;
121-
platformInfo.minimum = decodeVersion(cmd->version);
122-
return platformInfo;
123-
} else if (const auto *cmd = findCommand<version_min_command>(
124-
hdr, LC_VERSION_MIN_IPHONEOS)) {
125-
platformInfo.target.Platform = PlatformKind::iOS;
126-
platformInfo.minimum = decodeVersion(cmd->version);
127-
return platformInfo;
128-
} else if (const auto *cmd =
129-
findCommand<version_min_command>(hdr, LC_VERSION_MIN_TVOS)) {
130-
platformInfo.target.Platform = PlatformKind::tvOS;
131-
platformInfo.minimum = decodeVersion(cmd->version);
132-
} else if (const auto *cmd = findCommand<version_min_command>(
133-
hdr, LC_VERSION_MIN_WATCHOS)) {
134-
platformInfo.target.Platform = PlatformKind::watchOS;
119+
}
120+
if (const auto *cmd = findCommand<version_min_command>(
121+
hdr, LC_VERSION_MIN_MACOSX, LC_VERSION_MIN_IPHONEOS,
122+
LC_VERSION_MIN_TVOS, LC_VERSION_MIN_WATCHOS)) {
123+
switch (cmd->cmd) {
124+
case LC_VERSION_MIN_MACOSX:
125+
platformInfo.target.Platform = PlatformKind::macOS;
126+
break;
127+
case LC_VERSION_MIN_IPHONEOS:
128+
platformInfo.target.Platform = PlatformKind::iOS;
129+
break;
130+
case LC_VERSION_MIN_TVOS:
131+
platformInfo.target.Platform = PlatformKind::tvOS;
132+
break;
133+
case LC_VERSION_MIN_WATCHOS:
134+
platformInfo.target.Platform = PlatformKind::watchOS;
135+
break;
136+
}
135137
platformInfo.minimum = decodeVersion(cmd->version);
136138
return platformInfo;
137139
}

lld/MachO/InputFiles.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,13 +189,15 @@ extern llvm::SetVector<InputFile *> inputFiles;
189189

190190
llvm::Optional<MemoryBufferRef> readFile(StringRef path);
191191

192-
template <class CommandType = llvm::MachO::load_command, class Header>
193-
const CommandType *findCommand(const Header *hdr, uint32_t type) {
192+
template <class CommandType = llvm::MachO::load_command, class Header,
193+
class... Types>
194+
const CommandType *findCommand(const Header *hdr, Types... types) {
195+
std::initializer_list<uint32_t> typesList{types...};
194196
const uint8_t *p = reinterpret_cast<const uint8_t *>(hdr) + sizeof(Header);
195197

196198
for (uint32_t i = 0, n = hdr->ncmds; i < n; ++i) {
197199
auto *cmd = reinterpret_cast<const CommandType *>(p);
198-
if (cmd->cmd == type)
200+
if (llvm::is_contained(typesList, cmd->cmd))
199201
return cmd;
200202
p += cmd->cmdsize;
201203
}

lld/MachO/ObjC.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ template <class LP> static bool hasObjCSection(MemoryBufferRef mb) {
2323

2424
auto *hdr =
2525
reinterpret_cast<const typename LP::mach_header *>(mb.getBufferStart());
26-
if (const load_command *cmd = findCommand(hdr, LP::segmentLCType)) {
27-
auto *c = reinterpret_cast<const typename LP::segment_command *>(cmd);
26+
if (const auto *c =
27+
findCommand<typename LP::segment_command>(hdr, LP::segmentLCType)) {
2828
auto sectionHeaders =
2929
ArrayRef<Section>{reinterpret_cast<const Section *>(c + 1), c->nsects};
3030
for (const Section &sec : sectionHeaders) {

0 commit comments

Comments
 (0)