[PATCH] D89713: [llvm-objcopy][NFC] extract arg parsing logic to function

Keith Smiley via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 21 09:22:03 PDT 2020


keith updated this revision to Diff 299720.
keith marked an inline comment as done.
keith added a comment.

Make function static


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D89713

Files:
  llvm/tools/llvm-objcopy/llvm-objcopy.cpp


Index: llvm/tools/llvm-objcopy/llvm-objcopy.cpp
===================================================================
--- llvm/tools/llvm-objcopy/llvm-objcopy.cpp
+++ llvm/tools/llvm-objcopy/llvm-objcopy.cpp
@@ -63,6 +63,30 @@
   return Error::success();
 }
 
+static Expected<DriverConfig> getDriverConfig(ArrayRef<const char *> Args) {
+  StringRef Stem = sys::path::stem(ToolName);
+  auto Is = [=](StringRef Tool) {
+    // We need to recognize the following filenames:
+    //
+    // llvm-objcopy -> objcopy
+    // strip-10.exe -> strip
+    // powerpc64-unknown-freebsd13-objcopy -> objcopy
+    // llvm-install-name-tool -> install-name-tool
+    auto I = Stem.rfind_lower(Tool);
+    return I != StringRef::npos &&
+           (I + Tool.size() == Stem.size() || !isAlnum(Stem[I + Tool.size()]));
+  };
+
+  if (Is("bitcode-strip") || Is("bitcode_strip"))
+    return parseBitcodeStripOptions(Args);
+  else if (Is("strip"))
+    return parseStripOptions(Args, reportWarning);
+  else if (Is("install-name-tool") || Is("install_name_tool"))
+    return parseInstallNameToolOptions(Args);
+  else
+    return parseObjcopyOptions(Args, reportWarning);
+}
+
 } // end namespace objcopy
 } // end namespace llvm
 
@@ -309,34 +333,12 @@
 
 namespace {
 
-enum class ToolType { Objcopy, Strip, InstallNameTool, BitcodeStrip };
-
 } // anonymous namespace
 
 int main(int argc, char **argv) {
   InitLLVM X(argc, argv);
   ToolName = argv[0];
 
-  StringRef Stem = sys::path::stem(ToolName);
-  auto Is = [=](StringRef Tool) {
-    // We need to recognize the following filenames:
-    //
-    // llvm-objcopy -> objcopy
-    // strip-10.exe -> strip
-    // powerpc64-unknown-freebsd13-objcopy -> objcopy
-    // llvm-install-name-tool -> install-name-tool
-    auto I = Stem.rfind_lower(Tool);
-    return I != StringRef::npos &&
-           (I + Tool.size() == Stem.size() || !isAlnum(Stem[I + Tool.size()]));
-  };
-  ToolType Tool = ToolType::Objcopy;
-  if (Is("bitcode-strip") || Is("bitcode_strip"))
-    Tool = ToolType::BitcodeStrip;
-  else if (Is("strip"))
-    Tool = ToolType::Strip;
-  else if (Is("install-name-tool") || Is("install_name_tool"))
-    Tool = ToolType::InstallNameTool;
-
   // Expand response files.
   // TODO: Move these lines, which are copied from lib/Support/CommandLine.cpp,
   // into a separate function in the CommandLine library and call that function
@@ -351,14 +353,8 @@
                           NewArgv);
 
   auto Args = makeArrayRef(NewArgv).drop_front();
-  Expected<DriverConfig> DriverConfig =
-      (Tool == ToolType::Strip)
-          ? parseStripOptions(Args, reportWarning)
-          : ((Tool == ToolType::InstallNameTool)
-                 ? parseInstallNameToolOptions(Args)
-                 : ((Tool == ToolType::BitcodeStrip)
-                        ? parseBitcodeStripOptions(Args)
-                        : parseObjcopyOptions(Args, reportWarning)));
+  Expected<DriverConfig> DriverConfig = getDriverConfig(Args);
+
   if (!DriverConfig) {
     logAllUnhandledErrors(DriverConfig.takeError(),
                           WithColor::error(errs(), ToolName));


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D89713.299720.patch
Type: text/x-patch
Size: 3125 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20201021/e608ffa3/attachment.bin>


More information about the llvm-commits mailing list