Skip to content

Commit 9df7800

Browse files
committed
fixup: Use binary search to parse z args
1 parent b280a8f commit 9df7800

File tree

1 file changed

+42
-33
lines changed

1 file changed

+42
-33
lines changed

lld/ELF/Driver.cpp

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
#include "llvm/Support/TargetSelect.h"
6666
#include "llvm/Support/TimeProfiler.h"
6767
#include "llvm/Support/raw_ostream.h"
68+
#include <algorithm>
6869
#include <cstdlib>
6970
#include <tuple>
7071
#include <utility>
@@ -1700,51 +1701,59 @@ static void readConfigs(Ctx &ctx, opt::InputArgList &args) {
17001701
bool hasZicfilpFuncSigReportDynamic = false;
17011702
bool hasZicfissReportDynamic = false;
17021703
struct ReportOptDesc {
1703-
const char *const name;
1704+
const StringRef name;
17041705
ReportPolicy &policy;
17051706
bool *const seen;
17061707

1707-
ReportOptDesc(const char *const name, ReportPolicy &policy, bool *seen)
1708+
ReportOptDesc(const StringRef name, ReportPolicy &policy, bool *seen)
17081709
: name(name), policy(policy), seen(seen) {}
17091710
};
1710-
const ReportOptDesc reports[] = {
1711-
{"bti-report", ctx.arg.zBtiReport, nullptr},
1712-
{"cet-report", ctx.arg.zCetReport, nullptr},
1713-
{"execute-only-report", ctx.arg.zExecuteOnlyReport, nullptr},
1714-
{"gcs-report", ctx.arg.zGcsReport, nullptr},
1715-
{"gcs-report-dynamic", ctx.arg.zGcsReportDynamic, &hasGcsReportDynamic},
1716-
{"pauth-report", ctx.arg.zPauthReport, nullptr},
1717-
{"zicfilp-unlabeled-report", ctx.arg.zZicfilpUnlabeledReport, nullptr},
1718-
{"zicfilp-unlabeled-report-dynamic",
1711+
// Sort the descriptions by name according to StringRef::compare() so we can
1712+
// binary search it
1713+
ReportOptDesc reports[] = {
1714+
{{"bti-report"}, ctx.arg.zBtiReport, nullptr},
1715+
{{"cet-report"}, ctx.arg.zCetReport, nullptr},
1716+
{{"execute-only-report"}, ctx.arg.zExecuteOnlyReport, nullptr},
1717+
{{"gcs-report"}, ctx.arg.zGcsReport, nullptr},
1718+
{{"gcs-report-dynamic"}, ctx.arg.zGcsReportDynamic, &hasGcsReportDynamic},
1719+
{{"pauth-report"}, ctx.arg.zPauthReport, nullptr},
1720+
{{"zicfilp-func-sig-report"}, ctx.arg.zZicfilpFuncSigReport, nullptr},
1721+
{{"zicfilp-func-sig-report-dynamic"},
1722+
ctx.arg.zZicfilpFuncSigReportDynamic,
1723+
&hasZicfilpFuncSigReportDynamic},
1724+
{{"zicfilp-unlabeled-report"}, ctx.arg.zZicfilpUnlabeledReport, nullptr},
1725+
{{"zicfilp-unlabeled-report-dynamic"},
17191726
ctx.arg.zZicfilpUnlabeledReportDynamic,
17201727
&hasZicfilpUnlabeledReportDynamic},
1721-
{"zicfilp-func-sig-report", ctx.arg.zZicfilpFuncSigReport, nullptr},
1722-
{"zicfilp-func-sig-report-dynamic", ctx.arg.zZicfilpFuncSigReportDynamic,
1723-
&hasZicfilpFuncSigReportDynamic},
1724-
{"zicfiss-report", ctx.arg.zZicfissReport, nullptr},
1725-
{"zicfiss-report-dynamic", ctx.arg.zZicfissReportDynamic,
1728+
{{"zicfiss-report"}, ctx.arg.zZicfissReport, nullptr},
1729+
{{"zicfiss-report-dynamic"},
1730+
ctx.arg.zZicfissReportDynamic,
17261731
&hasZicfissReportDynamic}};
17271732
for (opt::Arg *arg : args.filtered(OPT_z)) {
17281733
std::pair<StringRef, StringRef> option =
17291734
StringRef(arg->getValue()).split('=');
1730-
for (const ReportOptDesc &desc : reports) {
1731-
if (option.first != desc.name)
1732-
continue;
1733-
arg->claim();
1734-
if (option.second == "none")
1735-
desc.policy = ReportPolicy::None;
1736-
else if (option.second == "warning")
1737-
desc.policy = ReportPolicy::Warning;
1738-
else if (option.second == "error")
1739-
desc.policy = ReportPolicy::Error;
1740-
else {
1741-
ErrAlways(ctx) << "unknown -z " << desc.name
1742-
<< "= value: " << option.second;
1743-
continue;
1744-
}
1745-
if (desc.seen)
1746-
*desc.seen = true;
1735+
ReportOptDesc *const desc =
1736+
std::lower_bound(std::begin(reports), std::end(reports), option.first,
1737+
[](const ReportOptDesc &d, const StringRef &s) {
1738+
return d.name < s;
1739+
});
1740+
if (desc == std::end(reports) || desc->name != option.first)
1741+
continue;
1742+
1743+
arg->claim();
1744+
if (option.second == "none")
1745+
desc->policy = ReportPolicy::None;
1746+
else if (option.second == "warning")
1747+
desc->policy = ReportPolicy::Warning;
1748+
else if (option.second == "error")
1749+
desc->policy = ReportPolicy::Error;
1750+
else {
1751+
ErrAlways(ctx) << "unknown -z " << desc->name
1752+
<< "= value: " << option.second;
1753+
continue;
17471754
}
1755+
if (desc->seen)
1756+
*desc->seen = true;
17481757
}
17491758

17501759
struct ReportDynamicOptDesc {

0 commit comments

Comments
 (0)