[PATCH] Replace nested switch statements.

Rui Ueyama ruiu at google.com
Thu Jan 2 20:10:06 PST 2014


Hi joerg,

This is for readability. No functionality change.

http://llvm-reviews.chandlerc.com/D2501

Files:
  lib/Driver/GnuLdDriver.cpp

Index: lib/Driver/GnuLdDriver.cpp
===================================================================
--- lib/Driver/GnuLdDriver.cpp
+++ lib/Driver/GnuLdDriver.cpp
@@ -17,6 +17,7 @@
 #include "lld/Driver/GnuLdInputGraph.h"
 
 #include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/Optional.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/ADT/Triple.h"
@@ -123,38 +124,37 @@
   return link(*options, diagnostics);
 }
 
+static llvm::Optional<llvm::Triple::ArchType>
+getArchType(const llvm::Triple &triple, const std::string &value) {
+  if (triple.getOS() != llvm::Triple::NetBSD)
+    return llvm::None;
+  switch (triple.getArch()) {
+  case llvm::Triple::x86:
+  case llvm::Triple::x86_64:
+    if (value == "elf_i386")
+      return llvm::Triple::x86;
+    if (value == "elf_x86_64")
+      return llvm::Triple::x86_64;
+    return llvm::None;
+  default:
+    return llvm::None;
+  }
+}
+
 bool GnuLdDriver::applyEmulation(llvm::Triple &triple,
                                  llvm::opt::InputArgList &args,
                                  raw_ostream &diagnostics) {
   llvm::opt::Arg *arg = args.getLastArg(OPT_m);
   if (!arg)
     return true;
   std::string value(arg->getValue());
-
-  switch (triple.getOS()) {
-  case llvm::Triple::NetBSD:
-    switch (triple.getArch()) {
-    case llvm::Triple::x86:
-    case llvm::Triple::x86_64:
-      if (value == "elf_i386") {
-        triple.setArch(llvm::Triple::x86);
-        return true;
-      }
-      if (value == "elf_x86_64") {
-        triple.setArch(llvm::Triple::x86_64);
-        return true;
-      }
-      break;
-    default:
-      break;
-    }
-    break;
-  default:
-    break;
+  llvm::Optional<llvm::Triple::ArchType> arch = getArchType(triple, value);
+  if (!arch) {
+    diagnostics << "error: unsupported emulation '" << value << "'.\n";
+    return false;
   }
-
-  diagnostics << "error: unsupported emulation '" << value << "'.\n";
-  return false;
+  triple.setArch(*arch);
+  return true;
 }
 
 void GnuLdDriver::addPlatformSearchDirs(ELFLinkingContext &ctx,
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D2501.1.patch
Type: text/x-patch
Size: 2087 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20140102/c00e3aad/attachment.bin>


More information about the llvm-commits mailing list