From 995798d2d58499f598ba125810e8e18799d900b1 Mon Sep 17 00:00:00 2001 From: Shoaib Meenai Date: Thu, 4 Jul 2019 00:17:02 +0000 Subject: [PATCH] [MachO] Add valid architecture function Added array of valid architectures and function returning array. Modified llvm-lipo to include list of valid architectures in error message for invalid arch. Patch by Anusha Basana Differential Revision: https://reviews.llvm.org/D63735 llvm-svn: 365099 --- llvm/include/llvm/Object/MachO.h | 1 + llvm/lib/Object/MachOObjectFile.cpp | 29 ++++++++-------------- .../tools/llvm-lipo/thin-universal-binary.test | 4 +-- llvm/tools/llvm-lipo/llvm-lipo.cpp | 11 ++++++-- 4 files changed, 22 insertions(+), 23 deletions(-) diff --git a/llvm/include/llvm/Object/MachO.h b/llvm/include/llvm/Object/MachO.h index 8442df1..ca9512f 100644 --- a/llvm/include/llvm/Object/MachO.h +++ b/llvm/include/llvm/Object/MachO.h @@ -571,6 +571,7 @@ public: const char **McpuDefault = nullptr, const char **ArchFlag = nullptr); static bool isValidArch(StringRef ArchFlag); + static ArrayRef getValidArchs(); static Triple getHostArch(); bool isRelocatableObject() const override; diff --git a/llvm/lib/Object/MachOObjectFile.cpp b/llvm/lib/Object/MachOObjectFile.cpp index 1b7392e..5aec844 100644 --- a/llvm/lib/Object/MachOObjectFile.cpp +++ b/llvm/lib/Object/MachOObjectFile.cpp @@ -57,6 +57,12 @@ namespace { } // end anonymous namespace +static const std::array validArchs = { + "i386", "x86_64", "x86_64h", "armv4t", "arm", "armv5e", + "armv6", "armv6m", "armv7", "armv7em", "armv7k", "armv7m", + "armv7s", "arm64", "arm64_32", "ppc", "ppc64", +}; + static Error malformedError(const Twine &Msg) { return make_error("truncated or malformed object (" + Msg + ")", @@ -2718,27 +2724,12 @@ Triple MachOObjectFile::getHostArch() { } bool MachOObjectFile::isValidArch(StringRef ArchFlag) { - return StringSwitch(ArchFlag) - .Case("i386", true) - .Case("x86_64", true) - .Case("x86_64h", true) - .Case("armv4t", true) - .Case("arm", true) - .Case("armv5e", true) - .Case("armv6", true) - .Case("armv6m", true) - .Case("armv7", true) - .Case("armv7em", true) - .Case("armv7k", true) - .Case("armv7m", true) - .Case("armv7s", true) - .Case("arm64", true) - .Case("arm64_32", true) - .Case("ppc", true) - .Case("ppc64", true) - .Default(false); + return std::find(validArchs.cbegin(), validArchs.cend(), ArchFlag) != + validArchs.cend(); } +ArrayRef MachOObjectFile::getValidArchs() { return validArchs; } + Triple::ArchType MachOObjectFile::getArch() const { return getArch(getCPUType(*this)); } diff --git a/llvm/test/tools/llvm-lipo/thin-universal-binary.test b/llvm/test/tools/llvm-lipo/thin-universal-binary.test index 0a9a6b4..8edb686 100644 --- a/llvm/test/tools/llvm-lipo/thin-universal-binary.test +++ b/llvm/test/tools/llvm-lipo/thin-universal-binary.test @@ -1,7 +1,7 @@ # RUN: yaml2obj %s > %t -# RUN: not llvm-lipo %t -thin arc -output %t.out 2>&1 | FileCheck --check-prefix=ARCH_NOT_IN_FILE %s -# ARCH_NOT_IN_FILE: does not contain the specified architecture arc to thin it to +# RUN: not llvm-lipo %t -thin arm64_32 -output %t.out 2>&1 | FileCheck --check-prefix=ARCH_NOT_IN_FILE %s +# ARCH_NOT_IN_FILE: does not contain the specified architecture arm64_32 to thin it to # RUN: not llvm-lipo %t -thin aarch101 -output %t.out 2>&1 | FileCheck --check-prefix=INVALID_ARCH %s # INVALID_ARCH: Invalid architecture: aarch101 diff --git a/llvm/tools/llvm-lipo/llvm-lipo.cpp b/llvm/tools/llvm-lipo/llvm-lipo.cpp index 902cd81..ea0d427 100644 --- a/llvm/tools/llvm-lipo/llvm-lipo.cpp +++ b/llvm/tools/llvm-lipo/llvm-lipo.cpp @@ -93,8 +93,15 @@ struct Config { } // end namespace static void validateArchitectureName(StringRef ArchitectureName) { - if (Triple(ArchitectureName).getArch() == Triple::ArchType::UnknownArch) - reportError("Invalid architecture: " + ArchitectureName); + if (!MachOObjectFile::isValidArch(ArchitectureName)) { + std::string Buf; + raw_string_ostream OS(Buf); + OS << "Invalid architecture: " << ArchitectureName + << "\nValid architecture names are:"; + for (auto arch : MachOObjectFile::getValidArchs()) + OS << " " << arch; + reportError(OS.str()); + } } static Config parseLipoOptions(ArrayRef ArgsArr) { -- 2.7.4