[lld-macho][nfc] Avoid using std::map for PlatformKinds
authorJez Ng <jezng@fb.com>
Sun, 11 Jul 2021 22:24:53 +0000 (18:24 -0400)
committerJez Ng <jezng@fb.com>
Sun, 11 Jul 2021 22:24:53 +0000 (18:24 -0400)
The mappings we were using had a small number of keys, so a vector is
probably better. This allows us to remove the last usage of std::map in
our codebase.

I also used `removeSimulator` to simplify the code a bit further.

Reviewed By: #lld-macho, thakis

Differential Revision: https://reviews.llvm.org/D105786

lld/MachO/Driver.cpp
lld/MachO/Driver.h
lld/MachO/InputFiles.cpp
lld/MachO/InputFiles.h
lld/MachO/Writer.cpp

index 86e9f4d..6737ad1 100644 (file)
@@ -849,17 +849,29 @@ static std::vector<SectionAlign> parseSectAlign(const opt::InputArgList &args) {
   return sectAligns;
 }
 
+PlatformKind macho::removeSimulator(PlatformKind platform) {
+  switch (platform) {
+  case PlatformKind::iOSSimulator:
+    return PlatformKind::iOS;
+  case PlatformKind::tvOSSimulator:
+    return PlatformKind::tvOS;
+  case PlatformKind::watchOSSimulator:
+    return PlatformKind::watchOS;
+  default:
+    return platform;
+  }
+}
+
 static bool dataConstDefault(const InputArgList &args) {
-  static const std::map<PlatformKind, VersionTuple> minVersion = {
+  static const std::vector<std::pair<PlatformKind, VersionTuple>> minVersion = {
       {PlatformKind::macOS, VersionTuple(10, 15)},
       {PlatformKind::iOS, VersionTuple(13, 0)},
-      {PlatformKind::iOSSimulator, VersionTuple(13, 0)},
       {PlatformKind::tvOS, VersionTuple(13, 0)},
-      {PlatformKind::tvOSSimulator, VersionTuple(13, 0)},
       {PlatformKind::watchOS, VersionTuple(6, 0)},
-      {PlatformKind::watchOSSimulator, VersionTuple(6, 0)},
       {PlatformKind::bridgeOS, VersionTuple(4, 0)}};
-  auto it = minVersion.find(config->platformInfo.target.Platform);
+  PlatformKind platform = removeSimulator(config->platformInfo.target.Platform);
+  auto it = llvm::find_if(minVersion,
+                          [&](const auto &p) { return p.first == platform; });
   if (it != minVersion.end())
     if (config->platformInfo.minimum < it->second)
       return false;
index 148c309..1006cc1 100644 (file)
@@ -22,6 +22,7 @@
 namespace llvm {
 namespace MachO {
 class InterfaceFile;
+enum class PlatformKind : unsigned;
 } // namespace MachO
 } // namespace llvm
 
@@ -75,6 +76,9 @@ uint32_t getModTime(llvm::StringRef path);
 
 void printArchiveMemberLoad(StringRef reason, const InputFile *);
 
+// Map simulator platforms to their underlying device platform.
+llvm::MachO::PlatformKind removeSimulator(llvm::MachO::PlatformKind platform);
+
 // Helper class to export dependency info.
 class DependencyTracker {
 public:
index 57773bf..523125c 100644 (file)
@@ -141,19 +141,6 @@ static std::vector<PlatformInfo> getPlatformInfos(const InputFile *input) {
   return platformInfos;
 }
 
-static PlatformKind removeSimulator(PlatformKind platform) {
-  // Mapping of platform to simulator and vice-versa.
-  static const std::map<PlatformKind, PlatformKind> platformMap = {
-      {PlatformKind::iOSSimulator, PlatformKind::iOS},
-      {PlatformKind::tvOSSimulator, PlatformKind::tvOS},
-      {PlatformKind::watchOSSimulator, PlatformKind::watchOS}};
-
-  auto iter = platformMap.find(platform);
-  if (iter == platformMap.end())
-    return platform;
-  return iter->second;
-}
-
 static bool checkCompatibility(const InputFile *input) {
   std::vector<PlatformInfo> platformInfos = getPlatformInfos(input);
   if (platformInfos.empty())
index fa34dbe..c57f24a 100644 (file)
@@ -22,7 +22,6 @@
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/TextAPI/TextAPIReader.h"
 
-#include <map>
 #include <vector>
 
 namespace llvm {
index 3bd0daf..a6e1b8b 100644 (file)
@@ -666,15 +666,17 @@ void Writer::scanSymbols() {
 
 // TODO: ld64 enforces the old load commands in a few other cases.
 static bool useLCBuildVersion(const PlatformInfo &platformInfo) {
-  static const std::map<PlatformKind, llvm::VersionTuple> minVersion = {
-      {PlatformKind::macOS, llvm::VersionTuple(10, 14)},
-      {PlatformKind::iOS, llvm::VersionTuple(12, 0)},
-      {PlatformKind::iOSSimulator, llvm::VersionTuple(13, 0)},
-      {PlatformKind::tvOS, llvm::VersionTuple(12, 0)},
-      {PlatformKind::tvOSSimulator, llvm::VersionTuple(13, 0)},
-      {PlatformKind::watchOS, llvm::VersionTuple(5, 0)},
-      {PlatformKind::watchOSSimulator, llvm::VersionTuple(6, 0)}};
-  auto it = minVersion.find(platformInfo.target.Platform);
+  static const std::vector<std::pair<PlatformKind, llvm::VersionTuple>>
+      minVersion = {{PlatformKind::macOS, llvm::VersionTuple(10, 14)},
+                    {PlatformKind::iOS, llvm::VersionTuple(12, 0)},
+                    {PlatformKind::iOSSimulator, llvm::VersionTuple(13, 0)},
+                    {PlatformKind::tvOS, llvm::VersionTuple(12, 0)},
+                    {PlatformKind::tvOSSimulator, llvm::VersionTuple(13, 0)},
+                    {PlatformKind::watchOS, llvm::VersionTuple(5, 0)},
+                    {PlatformKind::watchOSSimulator, llvm::VersionTuple(6, 0)}};
+  auto it = llvm::find_if(minVersion, [&](const auto &p) {
+    return p.first == platformInfo.target.Platform;
+  });
   return it == minVersion.end() ? true : platformInfo.minimum >= it->second;
 }