[lld] r293409 - Return early from getUnresolvedSymbolOption() to simplify.

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Sat Jan 28 17:59:11 PST 2017


Author: ruiu
Date: Sat Jan 28 19:59:11 2017
New Revision: 293409

URL: http://llvm.org/viewvc/llvm-project?rev=293409&view=rev
Log:
Return early from getUnresolvedSymbolOption() to simplify.

Modified:
    lld/trunk/ELF/Driver.cpp

Modified: lld/trunk/ELF/Driver.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Driver.cpp?rev=293409&r1=293408&r2=293409&view=diff
==============================================================================
--- lld/trunk/ELF/Driver.cpp (original)
+++ lld/trunk/ELF/Driver.cpp Sat Jan 28 19:59:11 2017
@@ -349,40 +349,53 @@ void LinkerDriver::main(ArrayRef<const c
   }
 }
 
-static UnresolvedPolicy getUnresolvedSymbolOption(opt::InputArgList &Args) {
-  // Find the last of --unresolved-symbols, --no-undefined and -z defs.
-  bool UnresolvedSymbolIsIgnoreAll = false;
-  bool ZDefs = false;
-  for (auto *Arg : Args) {
-    unsigned ID = Arg->getOption().getID();
-    if (ID == OPT_unresolved_symbols) {
-      StringRef S = Arg->getValue();
-      if (S == "ignore-all" || S == "ignore-in-object-files") {
-        ZDefs = false;
-        UnresolvedSymbolIsIgnoreAll = true;
-      } else if (S != "ignore-in-shared-libs" && S != "report-all") {
-        error("unknown --unresolved-symbols value: " + S);
-      }
-    } else if (ID == OPT_no_undefined ||
-               (ID == OPT_z && Arg->getValue() == StringRef("defs"))) {
-      ZDefs = true;
-      UnresolvedSymbolIsIgnoreAll = false;
-    }
-  }
+static bool getArg(opt::InputArgList &Args, unsigned K1, unsigned K2,
+                   bool Default) {
+  if (auto *Arg = Args.getLastArg(K1, K2))
+    return Arg->getOption().getID() == K1;
+  return Default;
+}
 
+// Determines what we should do if there are remaining unresolved
+// symbols after the name resolution.
+static UnresolvedPolicy getUnresolvedSymbolPolicy(opt::InputArgList &Args) {
+  // -noinhibit-exec or -r imply some default values.
   if (Args.hasArg(OPT_noinhibit_exec))
     return UnresolvedPolicy::WarnAll;
   if (Config->Relocatable)
     return UnresolvedPolicy::IgnoreAll;
 
-  if (ZDefs || (!Config->Shared && !UnresolvedSymbolIsIgnoreAll)) {
-    if (auto *Arg = Args.getLastArg(OPT_warn_undef, OPT_error_undef))
-      if (Arg->getOption().getID() == OPT_warn_undef)
-        return UnresolvedPolicy::Warn;
-    return UnresolvedPolicy::ReportError;
+  UnresolvedPolicy ErrorOrWarn =
+      getArg(Args, OPT_error_undef, OPT_warn_undef, true)
+          ? UnresolvedPolicy::ReportError
+          : UnresolvedPolicy::Warn;
+
+  // Process the last of -unresolved-symbols, -no-undefined or -z defs.
+  for (auto *Arg : llvm::reverse(Args)) {
+    switch (Arg->getOption().getID()) {
+    case OPT_unresolved_symbols: {
+      StringRef S = Arg->getValue();
+      if (S == "ignore-all" || S == "ignore-in-object-files")
+        return UnresolvedPolicy::Ignore;
+      if (S == "ignore-in-shared-libs" || S == "report-all")
+        return ErrorOrWarn;
+      error("unknown --unresolved-symbols value: " + S);
+      continue;
+    }
+    case OPT_no_undefined:
+      return ErrorOrWarn;
+    case OPT_z:
+      if (StringRef(Arg->getValue()) == "defs")
+        return ErrorOrWarn;
+      continue;
+    }
   }
 
-  return UnresolvedPolicy::Ignore;
+  // -shared implies -unresolved-symbols=ignore-all because missing
+  // symbols are likely to be resolved at runtime using other DSOs.
+  if (Config->Shared)
+    return UnresolvedPolicy::Ignore;
+  return ErrorOrWarn;
 }
 
 static Target2Policy getTarget2Option(opt::InputArgList &Args) {
@@ -409,13 +422,6 @@ static bool isOutputFormatBinary(opt::In
   return false;
 }
 
-static bool getArg(opt::InputArgList &Args, unsigned K1, unsigned K2,
-                   bool Default) {
-  if (auto *Arg = Args.getLastArg(K1, K2))
-    return Arg->getOption().getID() == K1;
-  return Default;
-}
-
 static DiscardPolicy getDiscardOption(opt::InputArgList &Args) {
   if (Config->Relocatable)
     return DiscardPolicy::None;
@@ -578,7 +584,7 @@ void LinkerDriver::readConfigs(opt::Inpu
   Config->SectionStartMap = getSectionStartMap(Args);
   Config->SortSection = getSortKind(Args);
   Config->Target2 = getTarget2Option(Args);
-  Config->UnresolvedSymbols = getUnresolvedSymbolOption(Args);
+  Config->UnresolvedSymbols = getUnresolvedSymbolPolicy(Args);
 
   if (Args.hasArg(OPT_print_map))
     Config->MapFile = "-";




More information about the llvm-commits mailing list