Skip to content

Commit 1168736

Browse files
committed
[lld-macho][nfc] Parse more options using getLastArg{Value}
The option-iterating loop should be reserved for options whose command-line order is important. I think LLD-ELF follows a similar design. Reviewed By: #lld-macho, smeenai Differential Revision: https://reviews.llvm.org/D97797
1 parent 58d531f commit 1168736

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

lld/MachO/Driver.cpp

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -560,13 +560,18 @@ static std::string lowerDash(StringRef s) {
560560
map_iterator(s.end(), toLowerDash));
561561
}
562562

563-
static void handlePlatformVersion(const opt::Arg *arg) {
563+
static PlatformInfo getPlatformVersion(const opt::ArgList &args) {
564+
const opt::Arg *arg = args.getLastArg(OPT_platform_version);
565+
PlatformInfo platform;
566+
if (!arg)
567+
return platform;
568+
564569
StringRef platformStr = arg->getValue(0);
565570
StringRef minVersionStr = arg->getValue(1);
566571
StringRef sdkVersionStr = arg->getValue(2);
567572

568573
// TODO(compnerd) see if we can generate this case list via XMACROS
569-
config->platform.kind =
574+
platform.kind =
570575
StringSwitch<PlatformKind>(lowerDash(platformStr))
571576
.Cases("macos", "1", PlatformKind::macOS)
572577
.Cases("ios", "2", PlatformKind::iOS)
@@ -579,23 +584,25 @@ static void handlePlatformVersion(const opt::Arg *arg) {
579584
.Cases("watchos-simulator", "9", PlatformKind::watchOSSimulator)
580585
.Cases("driverkit", "10", PlatformKind::driverKit)
581586
.Default(PlatformKind::unknown);
582-
if (config->platform.kind == PlatformKind::unknown)
587+
if (platform.kind == PlatformKind::unknown)
583588
error(Twine("malformed platform: ") + platformStr);
584589
// TODO: check validity of version strings, which varies by platform
585590
// NOTE: ld64 accepts version strings with 5 components
586591
// llvm::VersionTuple accepts no more than 4 components
587592
// Has Apple ever published version strings with 5 components?
588-
if (config->platform.minimum.tryParse(minVersionStr))
593+
if (platform.minimum.tryParse(minVersionStr))
589594
error(Twine("malformed minimum version: ") + minVersionStr);
590-
if (config->platform.sdk.tryParse(sdkVersionStr))
595+
if (platform.sdk.tryParse(sdkVersionStr))
591596
error(Twine("malformed sdk version: ") + sdkVersionStr);
597+
return platform;
592598
}
593599

594-
static void handleUndefined(const opt::Arg *arg) {
595-
StringRef treatmentStr = arg->getValue(0);
600+
static UndefinedSymbolTreatment
601+
getUndefinedSymbolTreatment(const opt::ArgList &args) {
602+
StringRef treatmentStr = args.getLastArgValue(OPT_undefined);
596603
auto treatment =
597604
StringSwitch<UndefinedSymbolTreatment>(treatmentStr)
598-
.Case("error", UndefinedSymbolTreatment::error)
605+
.Cases("error", "", UndefinedSymbolTreatment::error)
599606
.Case("warning", UndefinedSymbolTreatment::warning)
600607
.Case("suppress", UndefinedSymbolTreatment::suppress)
601608
.Case("dynamic_lookup", UndefinedSymbolTreatment::dynamic_lookup)
@@ -613,7 +620,7 @@ static void handleUndefined(const opt::Arg *arg) {
613620
error("'-undefined suppress' only valid with '-flat_namespace'");
614621
treatment = UndefinedSymbolTreatment::error;
615622
}
616-
config->undefinedSymbolTreatment = treatment;
623+
return treatment;
617624
}
618625

619626
static void warnIfDeprecatedOption(const opt::Option &opt) {
@@ -752,6 +759,7 @@ bool macho::link(ArrayRef<const char *> argsArr, bool canExitEarly,
752759
config = make<Configuration>();
753760
symtab = make<SymbolTable>();
754761
target = createTargetInfo(args);
762+
config->platform = getPlatformVersion(args);
755763

756764
config->entry = symtab->addUndefined(args.getLastArgValue(OPT_e, "_main"),
757765
/*file=*/nullptr,
@@ -792,11 +800,12 @@ bool macho::link(ArrayRef<const char *> argsArr, bool canExitEarly,
792800
config->staticLink = (arg->getOption().getID() == OPT_static);
793801

794802
if (const opt::Arg *arg =
795-
args.getLastArg(OPT_flat_namespace, OPT_twolevel_namespace)) {
803+
args.getLastArg(OPT_flat_namespace, OPT_twolevel_namespace))
796804
config->namespaceKind = arg->getOption().getID() == OPT_twolevel_namespace
797805
? NamespaceKind::twolevel
798806
: NamespaceKind::flat;
799-
}
807+
808+
config->undefinedSymbolTreatment = getUndefinedSymbolTreatment(args);
800809

801810
config->systemLibraryRoots = getSystemLibraryRoots(args);
802811
config->librarySearchPaths =
@@ -846,12 +855,13 @@ bool macho::link(ArrayRef<const char *> argsArr, bool canExitEarly,
846855

847856
initLLVM(); // must be run before any call to addFile()
848857

858+
// This loop should be reserved for options whose exact ordering matters.
859+
// Other options should be handled via filtered() and/or getLastArg().
849860
for (const auto &arg : args) {
850861
const auto &opt = arg->getOption();
851862
warnIfDeprecatedOption(opt);
852863
warnIfUnimplementedOption(opt);
853864

854-
// TODO: are any of these better handled via filtered() or getLastArg()?
855865
switch (opt.getID()) {
856866
case OPT_INPUT:
857867
addFile(arg->getValue(), false);
@@ -875,12 +885,6 @@ bool macho::link(ArrayRef<const char *> argsArr, bool canExitEarly,
875885
case OPT_weak_framework:
876886
addFramework(arg->getValue(), opt.getID() == OPT_weak_framework);
877887
break;
878-
case OPT_platform_version:
879-
handlePlatformVersion(arg);
880-
break;
881-
case OPT_undefined:
882-
handleUndefined(arg);
883-
break;
884888
default:
885889
break;
886890
}

0 commit comments

Comments
 (0)