[PATCH] D100954: [lld][MachO] Refactor findCommand

Alexander Shaposhnikov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 21 08:38:42 PDT 2021


This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rGb5720354efb6: [lld][MachO] Refactor findCommand (authored by alexshap).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D100954/new/

https://reviews.llvm.org/D100954

Files:
  lld/MachO/InputFiles.cpp
  lld/MachO/InputFiles.h
  lld/MachO/ObjC.cpp


Index: lld/MachO/ObjC.cpp
===================================================================
--- lld/MachO/ObjC.cpp
+++ lld/MachO/ObjC.cpp
@@ -23,8 +23,8 @@
 
   auto *hdr =
       reinterpret_cast<const typename LP::mach_header *>(mb.getBufferStart());
-  if (const load_command *cmd = findCommand(hdr, LP::segmentLCType)) {
-    auto *c = reinterpret_cast<const typename LP::segment_command *>(cmd);
+  if (const auto *c =
+          findCommand<typename LP::segment_command>(hdr, LP::segmentLCType)) {
     auto sectionHeaders =
         ArrayRef<Section>{reinterpret_cast<const Section *>(c + 1), c->nsects};
     for (const Section &sec : sectionHeaders) {
Index: lld/MachO/InputFiles.h
===================================================================
--- lld/MachO/InputFiles.h
+++ lld/MachO/InputFiles.h
@@ -189,13 +189,15 @@
 
 llvm::Optional<MemoryBufferRef> readFile(StringRef path);
 
-template <class CommandType = llvm::MachO::load_command, class Header>
-const CommandType *findCommand(const Header *hdr, uint32_t type) {
+template <class CommandType = llvm::MachO::load_command, class Header,
+          class... Types>
+const CommandType *findCommand(const Header *hdr, Types... types) {
+  std::initializer_list<uint32_t> typesList{types...};
   const uint8_t *p = reinterpret_cast<const uint8_t *>(hdr) + sizeof(Header);
 
   for (uint32_t i = 0, n = hdr->ncmds; i < n; ++i) {
     auto *cmd = reinterpret_cast<const CommandType *>(p);
-    if (cmd->cmd == type)
+    if (llvm::is_contained(typesList, cmd->cmd))
       return cmd;
     p += cmd->cmdsize;
   }
Index: lld/MachO/InputFiles.cpp
===================================================================
--- lld/MachO/InputFiles.cpp
+++ lld/MachO/InputFiles.cpp
@@ -109,29 +109,31 @@
 
   using Header = typename LP::mach_header;
   auto *hdr = reinterpret_cast<const Header *>(input->mb.getBufferStart());
+
   PlatformInfo platformInfo;
   if (const auto *cmd =
           findCommand<build_version_command>(hdr, LC_BUILD_VERSION)) {
     platformInfo.target.Platform = static_cast<PlatformKind>(cmd->platform);
     platformInfo.minimum = decodeVersion(cmd->minos);
     return platformInfo;
-  } else if (const auto *cmd =
-                 findCommand<version_min_command>(hdr, LC_VERSION_MIN_MACOSX)) {
-    platformInfo.target.Platform = PlatformKind::macOS;
-    platformInfo.minimum = decodeVersion(cmd->version);
-    return platformInfo;
-  } else if (const auto *cmd = findCommand<version_min_command>(
-                 hdr, LC_VERSION_MIN_IPHONEOS)) {
-    platformInfo.target.Platform = PlatformKind::iOS;
-    platformInfo.minimum = decodeVersion(cmd->version);
-    return platformInfo;
-  } else if (const auto *cmd =
-                 findCommand<version_min_command>(hdr, LC_VERSION_MIN_TVOS)) {
-    platformInfo.target.Platform = PlatformKind::tvOS;
-    platformInfo.minimum = decodeVersion(cmd->version);
-  } else if (const auto *cmd = findCommand<version_min_command>(
-                 hdr, LC_VERSION_MIN_WATCHOS)) {
-    platformInfo.target.Platform = PlatformKind::watchOS;
+  }
+  if (const auto *cmd = findCommand<version_min_command>(
+          hdr, LC_VERSION_MIN_MACOSX, LC_VERSION_MIN_IPHONEOS,
+          LC_VERSION_MIN_TVOS, LC_VERSION_MIN_WATCHOS)) {
+    switch (cmd->cmd) {
+    case LC_VERSION_MIN_MACOSX:
+      platformInfo.target.Platform = PlatformKind::macOS;
+      break;
+    case LC_VERSION_MIN_IPHONEOS:
+      platformInfo.target.Platform = PlatformKind::iOS;
+      break;
+    case LC_VERSION_MIN_TVOS:
+      platformInfo.target.Platform = PlatformKind::tvOS;
+      break;
+    case LC_VERSION_MIN_WATCHOS:
+      platformInfo.target.Platform = PlatformKind::watchOS;
+      break;
+    }
     platformInfo.minimum = decodeVersion(cmd->version);
     return platformInfo;
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D100954.339254.patch
Type: text/x-patch
Size: 3814 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210421/901f5635/attachment-0001.bin>


More information about the llvm-commits mailing list