[WebAssembly] Refactor config setting and checking. NFC.

This matches the way the ELF backend does it.

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

git-svn-id: https://llvm.org/svn/llvm-project/lld/trunk@346972 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/ELF/Driver.cpp b/ELF/Driver.cpp
index 17f15d8..956f0b3 100644
--- a/ELF/Driver.cpp
+++ b/ELF/Driver.cpp
@@ -275,7 +275,7 @@
 
 // Some command line options or some combinations of them are not allowed.
 // This function checks for such errors.
-static void checkOptions(opt::InputArgList &Args) {
+static void checkOptions() {
   // The MIPS ABI as of 2016 does not support the GNU-style symbol lookup
   // table which is a relatively new feature.
   if (Config->EMachine == EM_MIPS && Config->GnuHash)
@@ -429,7 +429,7 @@
 
   inferMachineType();
   setConfigs(Args);
-  checkOptions(Args);
+  checkOptions();
   if (errorCount())
     return;
 
diff --git a/wasm/Driver.cpp b/wasm/Driver.cpp
index bda2efb..398205c 100644
--- a/wasm/Driver.cpp
+++ b/wasm/Driver.cpp
@@ -325,49 +325,11 @@
   }
 }
 
-// Force Sym to be entered in the output. Used for -u or equivalent.
-static Symbol *handleUndefined(StringRef Name) {
-  Symbol *Sym = Symtab->find(Name);
-  if (!Sym)
-    return nullptr;
-
-  // Since symbol S may not be used inside the program, LTO may
-  // eliminate it. Mark the symbol as "used" to prevent it.
-  Sym->IsUsedInRegularObj = true;
-
-  if (auto *LazySym = dyn_cast<LazySymbol>(Sym))
-    LazySym->fetch();
-
-  return Sym;
-}
-
-void LinkerDriver::link(ArrayRef<const char *> ArgsArr) {
-  WasmOptTable Parser;
-  opt::InputArgList Args = Parser.parse(ArgsArr.slice(1));
-
-  // Handle --help
-  if (Args.hasArg(OPT_help)) {
-    Parser.PrintHelp(outs(),
-                     (std::string(ArgsArr[0]) + " [options] file...").c_str(),
-                     "LLVM Linker", false);
-    return;
-  }
-
-  // Handle --version
-  if (Args.hasArg(OPT_version) || Args.hasArg(OPT_v)) {
-    outs() << getLLDVersion() << "\n";
-    return;
-  }
-
-  // Parse and evaluate -mllvm options.
-  std::vector<const char *> V;
-  V.push_back("wasm-ld (LLVM option parsing)");
-  for (auto *Arg : Args.filtered(OPT_mllvm))
-    V.push_back(Arg->getValue());
-  cl::ParseCommandLineOptions(V.size(), V.data());
-
-  errorHandler().ErrorLimit = args::getInteger(Args, OPT_error_limit, 20);
-
+// Some Config members do not directly correspond to any particular
+// command line options, but computed based on other Config values.
+// This function initialize such members. See Config.h for the details
+// of these values.
+static void setConfigs(opt::InputArgList &Args) {
   Config->AllowUndefined = Args.hasArg(OPT_allow_undefined);
   Config->CompressRelocations = Args.hasArg(OPT_compress_relocations);
   Config->Demangle = Args.hasFlag(OPT_demangle, OPT_no_demangle, true);
@@ -414,7 +376,11 @@
   Config->MaxMemory = args::getInteger(Args, OPT_max_memory, 0);
   Config->ZStackSize =
       args::getZOptionValue(Args, OPT_z, "stack-size", WasmPageSize);
+}
 
+// Some command line options or some combinations of them are not allowed.
+// This function checks for such errors.
+static void checkOptions(opt::InputArgList &Args) {
   if (!Config->StripDebug && !Config->StripAll && Config->CompressRelocations)
     error("--compress-relocations is incompatible with output debug"
           " information. Please pass --strip-debug or --strip-all");
@@ -426,14 +392,6 @@
   if (Config->ThinLTOJobs == 0)
     error("--thinlto-jobs: number of threads must be > 0");
 
-  if (auto *Arg = Args.getLastArg(OPT_allow_undefined_file))
-    readImportFile(Arg->getValue());
-
-  if (!Args.hasArg(OPT_INPUT)) {
-    error("no input files");
-    return;
-  }
-
   if (Config->Pie && Config->Shared)
     error("-shared and -pie may not be used together");
 
@@ -455,6 +413,61 @@
     if (Config->Pie)
       error("-r and -pie may not be used together");
   }
+}
+
+// Force Sym to be entered in the output. Used for -u or equivalent.
+static Symbol *handleUndefined(StringRef Name) {
+  Symbol *Sym = Symtab->find(Name);
+  if (!Sym)
+    return nullptr;
+
+  // Since symbol S may not be used inside the program, LTO may
+  // eliminate it. Mark the symbol as "used" to prevent it.
+  Sym->IsUsedInRegularObj = true;
+
+  if (auto *LazySym = dyn_cast<LazySymbol>(Sym))
+    LazySym->fetch();
+
+  return Sym;
+}
+
+void LinkerDriver::link(ArrayRef<const char *> ArgsArr) {
+  WasmOptTable Parser;
+  opt::InputArgList Args = Parser.parse(ArgsArr.slice(1));
+
+  // Handle --help
+  if (Args.hasArg(OPT_help)) {
+    Parser.PrintHelp(outs(),
+                     (std::string(ArgsArr[0]) + " [options] file...").c_str(),
+                     "LLVM Linker", false);
+    return;
+  }
+
+  // Handle --version
+  if (Args.hasArg(OPT_version) || Args.hasArg(OPT_v)) {
+    outs() << getLLDVersion() << "\n";
+    return;
+  }
+
+  // Parse and evaluate -mllvm options.
+  std::vector<const char *> V;
+  V.push_back("wasm-ld (LLVM option parsing)");
+  for (auto *Arg : Args.filtered(OPT_mllvm))
+    V.push_back(Arg->getValue());
+  cl::ParseCommandLineOptions(V.size(), V.data());
+
+  errorHandler().ErrorLimit = args::getInteger(Args, OPT_error_limit, 20);
+
+  setConfigs(Args);
+  checkOptions(Args);
+
+  if (auto *Arg = Args.getLastArg(OPT_allow_undefined_file))
+    readImportFile(Arg->getValue());
+
+  if (!Args.hasArg(OPT_INPUT)) {
+    error("no input files");
+    return;
+  }
 
   Config->Pic = Config->Pie || Config->Shared;