From 4faf00006cf989f3ae212912994022c0486a2dc4 Mon Sep 17 00:00:00 2001 From: serge-sans-paille Date: Thu, 8 Dec 2022 22:04:39 +0100 Subject: [PATCH] Recommit of 8ae18303f97d5dcfaecc90b4d87effb2011ed82e - part 2 Differential Revision: https://reviews.llvm.org/D139274 --- clang/lib/Driver/ToolChains/Gnu.cpp | 4 +-- .../Plugins/Platform/MacOSX/PlatformDarwin.cpp | 2 +- llvm/include/llvm/Option/OptTable.h | 4 +-- llvm/lib/Option/OptTable.cpp | 35 ++++++++++------------ llvm/unittests/Option/OptionMarshallingTest.cpp | 11 +++---- llvm/utils/TableGen/OptParserEmitter.cpp | 5 ++-- 6 files changed, 29 insertions(+), 32 deletions(-) diff --git a/clang/lib/Driver/ToolChains/Gnu.cpp b/clang/lib/Driver/ToolChains/Gnu.cpp index 4621850..60d62e2b 100644 --- a/clang/lib/Driver/ToolChains/Gnu.cpp +++ b/clang/lib/Driver/ToolChains/Gnu.cpp @@ -331,8 +331,8 @@ static bool getStaticPIE(const ArgList &Args, const ToolChain &TC) { if (HasStaticPIE && Args.hasArg(options::OPT_nopie)) { const Driver &D = TC.getDriver(); const llvm::opt::OptTable &Opts = D.getOpts(); - const char *StaticPIEName = Opts.getOptionName(options::OPT_static_pie); - const char *NoPIEName = Opts.getOptionName(options::OPT_nopie); + StringRef StaticPIEName = Opts.getOptionName(options::OPT_static_pie); + StringRef NoPIEName = Opts.getOptionName(options::OPT_nopie); D.Diag(diag::err_drv_cannot_mix_options) << StaticPIEName << NoPIEName; } return HasStaticPIE; diff --git a/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp b/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp index 9d89148..fde0988 100644 --- a/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp +++ b/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp @@ -1055,7 +1055,7 @@ void PlatformDarwin::AddClangModuleCompilationOptionsForSDKType( // Only add the version-min options if we got a version from somewhere if (!version.empty() && sdk_type != XcodeSDK::Type::Linux) { #define OPTION(PREFIX, NAME, VAR, ...) \ - const char *opt_##VAR = NAME; \ + llvm::StringRef opt_##VAR = NAME; \ (void)opt_##VAR; #include "clang/Driver/Options.inc" #undef OPTION diff --git a/llvm/include/llvm/Option/OptTable.h b/llvm/include/llvm/Option/OptTable.h index 07d9870..390462d 100644 --- a/llvm/include/llvm/Option/OptTable.h +++ b/llvm/include/llvm/Option/OptTable.h @@ -102,9 +102,7 @@ public: const Option getOption(OptSpecifier Opt) const; /// Lookup the name of the given option. - const char *getOptionName(OptSpecifier id) const { - return getInfo(id).Name; - } + StringRef getOptionName(OptSpecifier id) const { return getInfo(id).Name; } /// Get the kind of the given option. unsigned getOptionKind(OptSpecifier id) const { diff --git a/llvm/lib/Option/OptTable.cpp b/llvm/lib/Option/OptTable.cpp index ef4873e..f4426bc 100644 --- a/llvm/lib/Option/OptTable.cpp +++ b/llvm/lib/Option/OptTable.cpp @@ -36,16 +36,10 @@ namespace opt { // Ordering on Info. The ordering is *almost* case-insensitive lexicographic, // with an exception. '\0' comes at the end of the alphabet instead of the // beginning (thus options precede any other options which prefix them). -static int StrCmpOptionNameIgnoreCase(const char *A, const char *B) { - const char *X = A, *Y = B; - char a = tolower(*A), b = tolower(*B); - while (a == b) { - if (a == '\0') - return 0; - - a = tolower(*++X); - b = tolower(*++Y); - } +static int StrCmpOptionNameIgnoreCase(StringRef A, StringRef B) { + size_t MinSize = std::min(A.size(), B.size()); + if (int Res = A.substr(0, MinSize).compare_insensitive(B.substr(0, MinSize))) + return Res; if (a == '\0') // A is a prefix of B. return 1; @@ -60,7 +54,7 @@ static int StrCmpOptionNameIgnoreCase(const char *A, const char *B) { static int StrCmpOptionName(const char *A, const char *B) { if (int N = StrCmpOptionNameIgnoreCase(A, B)) return N; - return strcmp(A, B); + return A.compare(B); } static inline bool operator<(const OptTable::Info &A, const OptTable::Info &B) { @@ -186,7 +180,7 @@ static unsigned matchOption(const OptTable::Info *I, StringRef Str, bool Matched = IgnoreCase ? Rest.startswith_insensitive(I->Name) : Rest.startswith(I->Name); if (Matched) - return Prefix.size() + StringRef(I->Name).size(); + return Prefix.size() + I->Name.size(); } } return 0; @@ -347,8 +341,8 @@ std::unique_ptr OptTable::parseOneArgGrouped(InputArgList &Args, const Info *End = OptionInfos.data() + OptionInfos.size(); StringRef Name = Str.ltrim(PrefixChars); - const Info *Start = std::lower_bound( - OptionInfos.data() + FirstSearchableIndex, End, Name.data()); + const Info *Start = + std::lower_bound(OptionInfos.data() + FirstSearchableIndex, End, Name); const Info *Fallback = nullptr; unsigned Prev = Index; @@ -403,19 +397,20 @@ std::unique_ptr OptTable::ParseOneArg(const ArgList &Args, unsigned &Index, unsigned FlagsToInclude, unsigned FlagsToExclude) const { unsigned Prev = Index; - const char *Str = Args.getArgString(Index); + StringRef Str = Args.getArgString(Index); // Anything that doesn't start with PrefixesUnion is an input, as is '-' // itself. if (isInput(PrefixesUnion, Str)) - return std::make_unique(getOption(InputOptionID), Str, Index++, Str); + return std::make_unique(getOption(InputOptionID), Str, Index++, + Str.data()); const Info *Start = OptionInfos.data() + FirstSearchableIndex; const Info *End = OptionInfos.data() + OptionInfos.size(); StringRef Name = StringRef(Str).ltrim(PrefixChars); // Search for the first next option which could be a prefix. - Start = std::lower_bound(Start, End, Name.data()); + Start = std::lower_bound(Start, End, Name); // Options are stored in sorted order, with '\0' at the end of the // alphabet. Since the only options which can accept a string must @@ -455,9 +450,11 @@ std::unique_ptr OptTable::ParseOneArg(const ArgList &Args, unsigned &Index, // If we failed to find an option and this arg started with /, then it's // probably an input path. if (Str[0] == '/') - return std::make_unique(getOption(InputOptionID), Str, Index++, Str); + return std::make_unique(getOption(InputOptionID), Str, Index++, + Str.data()); - return std::make_unique(getOption(UnknownOptionID), Str, Index++, Str); + return std::make_unique(getOption(UnknownOptionID), Str, Index++, + Str.data()); } InputArgList OptTable::ParseArgs(ArrayRef ArgArr, diff --git a/llvm/unittests/Option/OptionMarshallingTest.cpp b/llvm/unittests/Option/OptionMarshallingTest.cpp index 85f0d86..c4d48ac 100644 --- a/llvm/unittests/Option/OptionMarshallingTest.cpp +++ b/llvm/unittests/Option/OptionMarshallingTest.cpp @@ -6,10 +6,11 @@ // //===----------------------------------------------------------------------===// +#include "llvm/ADT/StringRef.h" #include "gtest/gtest.h" struct OptionWithMarshallingInfo { - const char *Name; + llvm::StringRef Name; const char *KeyPath; const char *ImpliedCheck; const char *ImpliedValue; @@ -27,10 +28,10 @@ static const OptionWithMarshallingInfo MarshallingTable[] = { }; TEST(OptionMarshalling, EmittedOrderSameAsDefinitionOrder) { - ASSERT_STREQ(MarshallingTable[0].Name, "marshalled-flag-d"); - ASSERT_STREQ(MarshallingTable[1].Name, "marshalled-flag-c"); - ASSERT_STREQ(MarshallingTable[2].Name, "marshalled-flag-b"); - ASSERT_STREQ(MarshallingTable[3].Name, "marshalled-flag-a"); + ASSERT_STREQ(MarshallingTable[0].Name.data(), "marshalled-flag-d"); + ASSERT_STREQ(MarshallingTable[1].Name.data(), "marshalled-flag-c"); + ASSERT_STREQ(MarshallingTable[2].Name.data(), "marshalled-flag-b"); + ASSERT_STREQ(MarshallingTable[3].Name.data(), "marshalled-flag-a"); } TEST(OptionMarshalling, EmittedSpecifiedKeyPath) { diff --git a/llvm/utils/TableGen/OptParserEmitter.cpp b/llvm/utils/TableGen/OptParserEmitter.cpp index 9fff65e..375d7ed 100644 --- a/llvm/utils/TableGen/OptParserEmitter.cpp +++ b/llvm/utils/TableGen/OptParserEmitter.cpp @@ -54,9 +54,10 @@ static std::string getOptionSpelling(const Record &R) { static void emitNameUsingSpelling(raw_ostream &OS, const Record &R) { size_t PrefixLength; - OS << "&"; + OS << "llvm::StringRef(&"; write_cstring(OS, StringRef(getOptionSpelling(R, PrefixLength))); - OS << "[" << PrefixLength << "]"; + OS << "[" << PrefixLength << "], " << R.getValueAsString("Name").size() + << ")"; } class MarshallingInfo { -- 2.7.4