diff options
author | serge-sans-paille <sguelton@mozilla.com> | 2022-12-26 09:19:09 +0100 |
---|---|---|
committer | serge-sans-paille <sguelton@mozilla.com> | 2022-12-27 09:55:19 +0100 |
commit | d9ab3e82f30d646deff054230b0c742704a1cf26 (patch) | |
tree | bc1265dff45eb195c81b781bd11a1e3edd0e28bb /llvm | |
parent | d065472c9ebca5efea7361c229ead7d5cb6f81b5 (diff) | |
download | llvm-d9ab3e82f30d646deff054230b0c742704a1cf26.zip llvm-d9ab3e82f30d646deff054230b0c742704a1cf26.tar.gz llvm-d9ab3e82f30d646deff054230b0c742704a1cf26.tar.bz2 |
[clang] Use a StringRef instead of a raw char pointer to store builtin and call information
This avoids recomputing string length that is already known at compile time.
It has a slight impact on preprocessing / compile time, see
https://llvm-compile-time-tracker.com/compare.php?from=3f36d2d579d8b0e8824d9dd99bfa79f456858f88&to=e49640c507ddc6615b5e503144301c8e41f8f434&stat=instructions:u
This a recommit of e953ae5bbc313fd0cc980ce021d487e5b5199ea4 and the subsequent fixes caa713559bd38f337d7d35de35686775e8fb5175 and 06b90e2e9c991e211fecc97948e533320a825470.
The above patchset caused some version of GCC to take eons to compile clang/lib/Basic/Targets/AArch64.cpp, as spotted in aa171833ab0017d9732e82b8682c9848ab25ff9e.
The fix is to make BuiltinInfo tables a compilation unit static variable, instead of a private static variable.
Differential Revision: https://reviews.llvm.org/D139881
Diffstat (limited to 'llvm')
27 files changed, 204 insertions, 124 deletions
diff --git a/llvm/include/llvm/ADT/ArrayRef.h b/llvm/include/llvm/ADT/ArrayRef.h index 09c61884..6f23de9 100644 --- a/llvm/include/llvm/ADT/ArrayRef.h +++ b/llvm/include/llvm/ADT/ArrayRef.h @@ -74,12 +74,12 @@ namespace llvm { : Data(&OneElt), Length(1) {} /// Construct an ArrayRef from a pointer and length. - /*implicit*/ ArrayRef(const T *data, size_t length) - : Data(data), Length(length) {} + constexpr /*implicit*/ ArrayRef(const T *data, size_t length) + : Data(data), Length(length) {} /// Construct an ArrayRef from a range. - ArrayRef(const T *begin, const T *end) - : Data(begin), Length(end - begin) {} + constexpr ArrayRef(const T *begin, const T *end) + : Data(begin), Length(end - begin) {} /// Construct an ArrayRef from a SmallVector. This is templated in order to /// avoid instantiating SmallVectorTemplateCommon<T> whenever we @@ -111,9 +111,9 @@ namespace llvm { #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Winit-list-lifetime" #endif - /*implicit*/ ArrayRef(const std::initializer_list<T> &Vec) - : Data(Vec.begin() == Vec.end() ? (T*)nullptr : Vec.begin()), - Length(Vec.size()) {} + constexpr /*implicit*/ ArrayRef(const std::initializer_list<T> &Vec) + : Data(Vec.begin() == Vec.end() ? (T *)nullptr : Vec.begin()), + Length(Vec.size()) {} #if LLVM_GNUC_PREREQ(9, 0, 0) #pragma GCC diagnostic pop #endif diff --git a/llvm/include/llvm/Option/OptTable.h b/llvm/include/llvm/Option/OptTable.h index 10d67b1..52354eb 100644 --- a/llvm/include/llvm/Option/OptTable.h +++ b/llvm/include/llvm/Option/OptTable.h @@ -44,7 +44,7 @@ public: struct Info { /// A null terminated array of prefix strings to apply to name while /// matching. - const char *const *Prefixes; + ArrayRef<StringLiteral> Prefixes; StringRef Name; const char *HelpText; const char *MetaVar; diff --git a/llvm/include/llvm/Option/Option.h b/llvm/include/llvm/Option/Option.h index 106f686..9b35e81 100644 --- a/llvm/include/llvm/Option/Option.h +++ b/llvm/include/llvm/Option/Option.h @@ -124,8 +124,9 @@ public: /// Get the default prefix for this option. StringRef getPrefix() const { - const char *Prefix = *Info->Prefixes; - return Prefix ? Prefix : StringRef(); + return Info->Prefixes.empty() + ? StringRef() + : static_cast<const StringRef &>(Info->Prefixes[0]); } /// Get the name of this option with the default prefix. diff --git a/llvm/lib/ExecutionEngine/JITLink/COFFDirectiveParser.cpp b/llvm/lib/ExecutionEngine/JITLink/COFFDirectiveParser.cpp index 8c1c7e2..70c5a2c 100644 --- a/llvm/lib/ExecutionEngine/JITLink/COFFDirectiveParser.cpp +++ b/llvm/lib/ExecutionEngine/JITLink/COFFDirectiveParser.cpp @@ -12,13 +12,18 @@ #include "COFFDirectiveParser.h" +#include <array> + using namespace llvm; using namespace jitlink; #define DEBUG_TYPE "jitlink" // Create prefix string literals used in Options.td -#define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE; +#define PREFIX(NAME, VALUE) \ + static constexpr StringLiteral NAME##_init[] = VALUE; \ + static constexpr ArrayRef<StringLiteral> NAME(NAME##_init, \ + std::size(NAME##_init) - 1); #include "COFFOptions.inc" #undef PREFIX diff --git a/llvm/lib/Option/OptTable.cpp b/llvm/lib/Option/OptTable.cpp index 786760f..c21072b 100644 --- a/llvm/lib/Option/OptTable.cpp +++ b/llvm/lib/Option/OptTable.cpp @@ -62,12 +62,10 @@ static inline bool operator<(const OptTable::Info &A, const OptTable::Info &B) { if (int N = StrCmpOptionName(A.Name, B.Name)) return N < 0; - for (const char * const *APre = A.Prefixes, - * const *BPre = B.Prefixes; - *APre != nullptr && *BPre != nullptr; ++APre, ++BPre){ - if (int N = StrCmpOptionName(*APre, *BPre)) + for (size_t I = 0, K = std::min(A.Prefixes.size(), B.Prefixes.size()); I != K; + ++I) + if (int N = StrCmpOptionName(A.Prefixes[I], B.Prefixes[I])) return N < 0; - } // Names are the same, check that classes are in order; exactly one // should be joined, and it should succeed the other. @@ -131,11 +129,8 @@ OptTable::OptTable(ArrayRef<Info> OptionInfos, bool IgnoreCase) // Build prefixes. for (unsigned i = FirstSearchableIndex + 1, e = getNumOptions() + 1; i != e; ++i) { - if (const char *const *P = getInfo(i).Prefixes) { - for (; *P != nullptr; ++P) { - PrefixesUnion.insert(*P); - } - } + const auto &P = getInfo(i).Prefixes; + PrefixesUnion.insert(P.begin(), P.end()); } // Build prefix chars. @@ -168,8 +163,7 @@ static bool isInput(const StringSet<> &Prefixes, StringRef Arg) { /// \returns Matched size. 0 means no match. static unsigned matchOption(const OptTable::Info *I, StringRef Str, bool IgnoreCase) { - for (const char * const *Pre = I->Prefixes; *Pre != nullptr; ++Pre) { - StringRef Prefix(*Pre); + for (auto Prefix : I->Prefixes) { if (Str.startswith(Prefix)) { StringRef Rest = Str.substr(Prefix.size()); bool Matched = IgnoreCase ? Rest.startswith_insensitive(I->Name) @@ -183,13 +177,10 @@ static unsigned matchOption(const OptTable::Info *I, StringRef Str, // Returns true if one of the Prefixes + In.Names matches Option static bool optionMatches(const OptTable::Info &In, StringRef Option) { - if (In.Prefixes) { - StringRef InName(In.Name); - for (size_t I = 0; In.Prefixes[I]; I++) - if (Option.endswith(InName)) - if (Option.slice(0, Option.size() - InName.size()) == In.Prefixes[I]) - return true; - } + for (auto Prefix : In.Prefixes) + if (Option.endswith(In.Name)) + if (Option.slice(0, Option.size() - In.Name.size()) == Prefix) + return true; return false; } @@ -221,13 +212,13 @@ OptTable::findByPrefix(StringRef Cur, unsigned int DisableFlags) const { std::vector<std::string> Ret; for (size_t I = FirstSearchableIndex, E = OptionInfos.size(); I < E; I++) { const Info &In = OptionInfos[I]; - if (!In.Prefixes || (!In.HelpText && !In.GroupID)) + if (In.Prefixes.empty() || (!In.HelpText && !In.GroupID)) continue; if (In.Flags & DisableFlags) continue; - for (int I = 0; In.Prefixes[I]; I++) { - std::string S = std::string(In.Prefixes[I]) + std::string(In.Name) + "\t"; + for (auto Prefix : In.Prefixes) { + std::string S = (Prefix + In.Name + "\t").str(); if (In.HelpText) S += In.HelpText; if (StringRef(S).startswith(Cur) && S != std::string(Cur) + "\t") @@ -265,7 +256,7 @@ unsigned OptTable::findNearest(StringRef Option, std::string &NearestString, // * Ignore positional argument option candidates (which do not // have prefixes). - if (!CandidateInfo.Prefixes) + if (CandidateInfo.Prefixes.empty()) continue; // Now check if the candidate ends with a character commonly used when @@ -285,8 +276,7 @@ unsigned OptTable::findNearest(StringRef Option, std::string &NearestString, // Consider each possible prefix for each candidate to find the most // appropriate one. For example, if a user asks for "--helm", suggest // "--help" over "-help". - for (int P = 0; - const char *const CandidatePrefix = CandidateInfo.Prefixes[P]; P++) { + for (auto CandidatePrefix : CandidateInfo.Prefixes) { std::string Candidate = (CandidatePrefix + CandidateName).str(); StringRef CandidateRef = Candidate; unsigned Distance = diff --git a/llvm/lib/Option/Option.cpp b/llvm/lib/Option/Option.cpp index ebdba89..1f1eb93 100644 --- a/llvm/lib/Option/Option.cpp +++ b/llvm/lib/Option/Option.cpp @@ -58,11 +58,10 @@ void Option::print(raw_ostream &O) const { #undef P } - if (Info->Prefixes) { + if (!Info->Prefixes.empty()) { O << " Prefixes:["; - for (const char *const *Pre = Info->Prefixes; *Pre != nullptr; ++Pre) { - O << '"' << *Pre << (*(Pre + 1) == nullptr ? "\"" : "\", "); - } + for (size_t I = 0, N = Info->Prefixes.size(); I != N; ++I) + O << '"' << Info->Prefixes[I] << (I == N - 1 ? "\"" : "\", "); O << ']'; } diff --git a/llvm/lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp b/llvm/lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp index c8ef0d1..375c455 100644 --- a/llvm/lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp +++ b/llvm/lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp @@ -37,11 +37,14 @@ enum { #undef OPTION }; -#define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE; +#define PREFIX(NAME, VALUE) \ + static constexpr StringLiteral NAME##_init[] = VALUE; \ + static constexpr ArrayRef<StringLiteral> NAME(NAME##_init, \ + std::size(NAME##_init) - 1); #include "Options.inc" #undef PREFIX -static constexpr llvm::opt::OptTable::Info InfoTable[] = { +static constexpr opt::OptTable::Info InfoTable[] = { #define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X7, X8, X9, X10, X11, X12) \ {X1, X2, X10, X11, OPT_##ID, llvm::opt::Option::KIND##Class, \ X9, X8, OPT_##GROUP, OPT_##ALIAS, X7, X12}, diff --git a/llvm/lib/ToolDrivers/llvm-lib/LibDriver.cpp b/llvm/lib/ToolDrivers/llvm-lib/LibDriver.cpp index 7f06ed0..b0046e0 100644 --- a/llvm/lib/ToolDrivers/llvm-lib/LibDriver.cpp +++ b/llvm/lib/ToolDrivers/llvm-lib/LibDriver.cpp @@ -41,7 +41,10 @@ enum { #undef OPTION }; -#define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE; +#define PREFIX(NAME, VALUE) \ + static constexpr StringLiteral NAME##_init[] = VALUE; \ + static constexpr ArrayRef<StringLiteral> NAME(NAME##_init, \ + std::size(NAME##_init) - 1); #include "Options.inc" #undef PREFIX diff --git a/llvm/tools/dsymutil/dsymutil.cpp b/llvm/tools/dsymutil/dsymutil.cpp index 5ed3b2b..ba31f0a 100644 --- a/llvm/tools/dsymutil/dsymutil.cpp +++ b/llvm/tools/dsymutil/dsymutil.cpp @@ -63,7 +63,10 @@ enum ID { #undef OPTION }; -#define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE; +#define PREFIX(NAME, VALUE) \ + static constexpr StringLiteral NAME##_init[] = VALUE; \ + static constexpr ArrayRef<StringLiteral> NAME(NAME##_init, \ + std::size(NAME##_init) - 1); #include "Options.inc" #undef PREFIX diff --git a/llvm/tools/llvm-cvtres/llvm-cvtres.cpp b/llvm/tools/llvm-cvtres/llvm-cvtres.cpp index b2be58b..d799cc6 100644 --- a/llvm/tools/llvm-cvtres/llvm-cvtres.cpp +++ b/llvm/tools/llvm-cvtres/llvm-cvtres.cpp @@ -44,7 +44,10 @@ enum ID { #undef OPTION }; -#define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE; +#define PREFIX(NAME, VALUE) \ + static constexpr StringLiteral NAME##_init[] = VALUE; \ + static constexpr ArrayRef<StringLiteral> NAME(NAME##_init, \ + std::size(NAME##_init) - 1); #include "Opts.inc" #undef PREFIX diff --git a/llvm/tools/llvm-cxxfilt/llvm-cxxfilt.cpp b/llvm/tools/llvm-cxxfilt/llvm-cxxfilt.cpp index 52a75ee..c26049e 100644 --- a/llvm/tools/llvm-cxxfilt/llvm-cxxfilt.cpp +++ b/llvm/tools/llvm-cxxfilt/llvm-cxxfilt.cpp @@ -32,7 +32,10 @@ enum ID { #undef OPTION }; -#define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE; +#define PREFIX(NAME, VALUE) \ + static constexpr llvm::StringLiteral NAME##_init[] = VALUE; \ + static constexpr llvm::ArrayRef<llvm::StringLiteral> NAME( \ + NAME##_init, std::size(NAME##_init) - 1); #include "Opts.inc" #undef PREFIX diff --git a/llvm/tools/llvm-dwarfutil/llvm-dwarfutil.cpp b/llvm/tools/llvm-dwarfutil/llvm-dwarfutil.cpp index eeb92a7..be19b80 100644 --- a/llvm/tools/llvm-dwarfutil/llvm-dwarfutil.cpp +++ b/llvm/tools/llvm-dwarfutil/llvm-dwarfutil.cpp @@ -40,7 +40,10 @@ enum ID { #undef OPTION }; -#define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE; +#define PREFIX(NAME, VALUE) \ + static constexpr StringLiteral NAME##_init[] = VALUE; \ + static constexpr ArrayRef<StringLiteral> NAME(NAME##_init, \ + std::size(NAME##_init) - 1); #include "Options.inc" #undef PREFIX diff --git a/llvm/tools/llvm-ifs/llvm-ifs.cpp b/llvm/tools/llvm-ifs/llvm-ifs.cpp index bef2800..0c85a1f 100644 --- a/llvm/tools/llvm-ifs/llvm-ifs.cpp +++ b/llvm/tools/llvm-ifs/llvm-ifs.cpp @@ -60,7 +60,10 @@ enum ID { #undef OPTION }; -#define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE; +#define PREFIX(NAME, VALUE) \ + static constexpr StringLiteral NAME##_init[] = VALUE; \ + static constexpr ArrayRef<StringLiteral> NAME(NAME##_init, \ + std::size(NAME##_init) - 1); #include "Opts.inc" #undef PREFIX diff --git a/llvm/tools/llvm-lipo/llvm-lipo.cpp b/llvm/tools/llvm-lipo/llvm-lipo.cpp index b4a76e18..5efb9f5 100644 --- a/llvm/tools/llvm-lipo/llvm-lipo.cpp +++ b/llvm/tools/llvm-lipo/llvm-lipo.cpp @@ -73,26 +73,29 @@ enum LipoID { #undef OPTION }; -// LipoInfoTable below references LIPO_##PREFIX. OptionGroup has prefix nullptr. -constexpr const char *const *LIPO_nullptr = nullptr; -#define PREFIX(NAME, VALUE) const char *const LIPO_##NAME[] = VALUE; +namespace lipo { +#define PREFIX(NAME, VALUE) \ + static constexpr llvm::StringLiteral NAME##_init[] = VALUE; \ + static constexpr llvm::ArrayRef<llvm::StringLiteral> NAME( \ + NAME##_init, std::size(NAME##_init) - 1); #include "LipoOpts.inc" #undef PREFIX static constexpr opt::OptTable::Info LipoInfoTable[] = { #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \ HELPTEXT, METAVAR, VALUES) \ - {LIPO_##PREFIX, NAME, HELPTEXT, \ - METAVAR, LIPO_##ID, opt::Option::KIND##Class, \ - PARAM, FLAGS, LIPO_##GROUP, \ - LIPO_##ALIAS, ALIASARGS, VALUES}, + {PREFIX, NAME, HELPTEXT, \ + METAVAR, LIPO_##ID, opt::Option::KIND##Class, \ + PARAM, FLAGS, LIPO_##GROUP, \ + LIPO_##ALIAS, ALIASARGS, VALUES}, #include "LipoOpts.inc" #undef OPTION }; +} // namespace lipo class LipoOptTable : public opt::OptTable { public: - LipoOptTable() : OptTable(LipoInfoTable) {} + LipoOptTable() : OptTable(lipo::LipoInfoTable) {} }; enum class LipoAction { diff --git a/llvm/tools/llvm-ml/llvm-ml.cpp b/llvm/tools/llvm-ml/llvm-ml.cpp index 7626580..4cbe52e 100644 --- a/llvm/tools/llvm-ml/llvm-ml.cpp +++ b/llvm/tools/llvm-ml/llvm-ml.cpp @@ -60,7 +60,10 @@ enum ID { #undef OPTION }; -#define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE; +#define PREFIX(NAME, VALUE) \ + static constexpr StringLiteral NAME##_init[] = VALUE; \ + static constexpr ArrayRef<StringLiteral> NAME(NAME##_init, \ + std::size(NAME##_init) - 1); #include "Opts.inc" #undef PREFIX diff --git a/llvm/tools/llvm-mt/llvm-mt.cpp b/llvm/tools/llvm-mt/llvm-mt.cpp index 3f70c89..97a11c6 100644 --- a/llvm/tools/llvm-mt/llvm-mt.cpp +++ b/llvm/tools/llvm-mt/llvm-mt.cpp @@ -41,7 +41,10 @@ enum ID { #undef OPTION }; -#define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE; +#define PREFIX(NAME, VALUE) \ + static constexpr StringLiteral NAME##_init[] = VALUE; \ + static constexpr ArrayRef<StringLiteral> NAME(NAME##_init, \ + std::size(NAME##_init) - 1); #include "Opts.inc" #undef PREFIX diff --git a/llvm/tools/llvm-nm/llvm-nm.cpp b/llvm/tools/llvm-nm/llvm-nm.cpp index 47644f0..c62d210 100644 --- a/llvm/tools/llvm-nm/llvm-nm.cpp +++ b/llvm/tools/llvm-nm/llvm-nm.cpp @@ -64,7 +64,10 @@ enum ID { #undef OPTION }; -#define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE; +#define PREFIX(NAME, VALUE) \ + static constexpr StringLiteral NAME##_init[] = VALUE; \ + static constexpr ArrayRef<StringLiteral> NAME(NAME##_init, \ + std::size(NAME##_init) - 1); #include "Opts.inc" #undef PREFIX diff --git a/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp b/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp index bf39f8c..8cc18ad 100644 --- a/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp +++ b/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp @@ -36,32 +36,29 @@ enum ObjcopyID { #undef OPTION }; -#define PREFIX(NAME, VALUE) const char *const OBJCOPY_##NAME[] = VALUE; +namespace objcopy_opt { +#define PREFIX(NAME, VALUE) \ + static constexpr StringLiteral NAME##_init[] = VALUE; \ + static constexpr ArrayRef<StringLiteral> NAME(NAME##_init, \ + std::size(NAME##_init) - 1); #include "ObjcopyOpts.inc" #undef PREFIX static constexpr opt::OptTable::Info ObjcopyInfoTable[] = { #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \ HELPTEXT, METAVAR, VALUES) \ - {OBJCOPY_##PREFIX, \ - NAME, \ - HELPTEXT, \ - METAVAR, \ - OBJCOPY_##ID, \ - opt::Option::KIND##Class, \ - PARAM, \ - FLAGS, \ - OBJCOPY_##GROUP, \ - OBJCOPY_##ALIAS, \ - ALIASARGS, \ - VALUES}, + {PREFIX, NAME, HELPTEXT, \ + METAVAR, OBJCOPY_##ID, opt::Option::KIND##Class, \ + PARAM, FLAGS, OBJCOPY_##GROUP, \ + OBJCOPY_##ALIAS, ALIASARGS, VALUES}, #include "ObjcopyOpts.inc" #undef OPTION }; +} // namespace objcopy_opt class ObjcopyOptTable : public opt::OptTable { public: - ObjcopyOptTable() : OptTable(ObjcopyInfoTable) { + ObjcopyOptTable() : OptTable(objcopy_opt::ObjcopyInfoTable) { setGroupedShortOptions(true); } }; @@ -75,15 +72,19 @@ enum InstallNameToolID { #undef OPTION }; +namespace install_name_tool { + #define PREFIX(NAME, VALUE) \ - const char *const INSTALL_NAME_TOOL_##NAME[] = VALUE; + static constexpr StringLiteral NAME##_init[] = VALUE; \ + static constexpr ArrayRef<StringLiteral> NAME(NAME##_init, \ + std::size(NAME##_init) - 1); #include "InstallNameToolOpts.inc" #undef PREFIX static constexpr opt::OptTable::Info InstallNameToolInfoTable[] = { #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \ HELPTEXT, METAVAR, VALUES) \ - {INSTALL_NAME_TOOL_##PREFIX, \ + {PREFIX, \ NAME, \ HELPTEXT, \ METAVAR, \ @@ -98,10 +99,12 @@ static constexpr opt::OptTable::Info InstallNameToolInfoTable[] = { #include "InstallNameToolOpts.inc" #undef OPTION }; +} // namespace install_name_tool class InstallNameToolOptTable : public opt::OptTable { public: - InstallNameToolOptTable() : OptTable(InstallNameToolInfoTable) {} + InstallNameToolOptTable() + : OptTable(install_name_tool::InstallNameToolInfoTable) {} }; enum BitcodeStripID { @@ -113,14 +116,19 @@ enum BitcodeStripID { #undef OPTION }; -#define PREFIX(NAME, VALUE) const char *const BITCODE_STRIP_##NAME[] = VALUE; +namespace bitcode_strip { + +#define PREFIX(NAME, VALUE) \ + static constexpr StringLiteral NAME##_init[] = VALUE; \ + static constexpr ArrayRef<StringLiteral> NAME(NAME##_init, \ + std::size(NAME##_init) - 1); #include "BitcodeStripOpts.inc" #undef PREFIX static constexpr opt::OptTable::Info BitcodeStripInfoTable[] = { #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \ HELPTEXT, METAVAR, VALUES) \ - {BITCODE_STRIP_##PREFIX, \ + {PREFIX, \ NAME, \ HELPTEXT, \ METAVAR, \ @@ -135,10 +143,11 @@ static constexpr opt::OptTable::Info BitcodeStripInfoTable[] = { #include "BitcodeStripOpts.inc" #undef OPTION }; +} // namespace bitcode_strip class BitcodeStripOptTable : public opt::OptTable { public: - BitcodeStripOptTable() : OptTable(BitcodeStripInfoTable) {} + BitcodeStripOptTable() : OptTable(bitcode_strip::BitcodeStripInfoTable) {} }; enum StripID { @@ -150,24 +159,31 @@ enum StripID { #undef OPTION }; -#define PREFIX(NAME, VALUE) const char *const STRIP_##NAME[] = VALUE; +namespace strip { +#define PREFIX(NAME, VALUE) \ + static constexpr StringLiteral NAME##_init[] = VALUE; \ + static constexpr ArrayRef<StringLiteral> NAME(NAME##_init, \ + std::size(NAME##_init) - 1); #include "StripOpts.inc" #undef PREFIX static constexpr opt::OptTable::Info StripInfoTable[] = { #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \ HELPTEXT, METAVAR, VALUES) \ - {STRIP_##PREFIX, NAME, HELPTEXT, \ - METAVAR, STRIP_##ID, opt::Option::KIND##Class, \ - PARAM, FLAGS, STRIP_##GROUP, \ - STRIP_##ALIAS, ALIASARGS, VALUES}, + {PREFIX, NAME, HELPTEXT, \ + METAVAR, STRIP_##ID, opt::Option::KIND##Class, \ + PARAM, FLAGS, STRIP_##GROUP, \ + STRIP_##ALIAS, ALIASARGS, VALUES}, #include "StripOpts.inc" #undef OPTION }; +} // namespace strip class StripOptTable : public opt::OptTable { public: - StripOptTable() : OptTable(StripInfoTable) { setGroupedShortOptions(true); } + StripOptTable() : OptTable(strip::StripInfoTable) { + setGroupedShortOptions(true); + } }; } // namespace diff --git a/llvm/tools/llvm-objdump/llvm-objdump.cpp b/llvm/tools/llvm-objdump/llvm-objdump.cpp index bb02cd0..b30aa3a 100644 --- a/llvm/tools/llvm-objdump/llvm-objdump.cpp +++ b/llvm/tools/llvm-objdump/llvm-objdump.cpp @@ -119,28 +119,31 @@ private: }; // ObjdumpOptID is in ObjdumpOptID.h - -#define PREFIX(NAME, VALUE) const char *const OBJDUMP_##NAME[] = VALUE; +namespace objdump_opt { +#define PREFIX(NAME, VALUE) \ + static constexpr StringLiteral NAME##_init[] = VALUE; \ + static constexpr ArrayRef<StringLiteral> NAME(NAME##_init, \ + std::size(NAME##_init) - 1); #include "ObjdumpOpts.inc" #undef PREFIX static constexpr opt::OptTable::Info ObjdumpInfoTable[] = { -#define OBJDUMP_nullptr nullptr #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \ HELPTEXT, METAVAR, VALUES) \ - {OBJDUMP_##PREFIX, NAME, HELPTEXT, \ - METAVAR, OBJDUMP_##ID, opt::Option::KIND##Class, \ - PARAM, FLAGS, OBJDUMP_##GROUP, \ - OBJDUMP_##ALIAS, ALIASARGS, VALUES}, + {PREFIX, NAME, HELPTEXT, \ + METAVAR, OBJDUMP_##ID, opt::Option::KIND##Class, \ + PARAM, FLAGS, OBJDUMP_##GROUP, \ + OBJDUMP_##ALIAS, ALIASARGS, VALUES}, #include "ObjdumpOpts.inc" #undef OPTION -#undef OBJDUMP_nullptr }; +} // namespace objdump_opt class ObjdumpOptTable : public CommonOptTable { public: ObjdumpOptTable() - : CommonOptTable(ObjdumpInfoTable, " [options] <input object files>", + : CommonOptTable(objdump_opt::ObjdumpInfoTable, + " [options] <input object files>", "llvm object file dumper") {} }; @@ -153,27 +156,30 @@ enum OtoolOptID { #undef OPTION }; -#define PREFIX(NAME, VALUE) const char *const OTOOL_##NAME[] = VALUE; +namespace otool { +#define PREFIX(NAME, VALUE) \ + static constexpr StringLiteral NAME##_init[] = VALUE; \ + static constexpr ArrayRef<StringLiteral> NAME(NAME##_init, \ + std::size(NAME##_init) - 1); #include "OtoolOpts.inc" #undef PREFIX static constexpr opt::OptTable::Info OtoolInfoTable[] = { -#define OTOOL_nullptr nullptr #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \ HELPTEXT, METAVAR, VALUES) \ - {OTOOL_##PREFIX, NAME, HELPTEXT, \ - METAVAR, OTOOL_##ID, opt::Option::KIND##Class, \ - PARAM, FLAGS, OTOOL_##GROUP, \ - OTOOL_##ALIAS, ALIASARGS, VALUES}, + {PREFIX, NAME, HELPTEXT, \ + METAVAR, OTOOL_##ID, opt::Option::KIND##Class, \ + PARAM, FLAGS, OTOOL_##GROUP, \ + OTOOL_##ALIAS, ALIASARGS, VALUES}, #include "OtoolOpts.inc" #undef OPTION -#undef OTOOL_nullptr }; +} // namespace otool class OtoolOptTable : public CommonOptTable { public: OtoolOptTable() - : CommonOptTable(OtoolInfoTable, " [option...] [file...]", + : CommonOptTable(otool::OtoolInfoTable, " [option...] [file...]", "Mach-O object file displaying tool") {} }; diff --git a/llvm/tools/llvm-rc/llvm-rc.cpp b/llvm/tools/llvm-rc/llvm-rc.cpp index 00fb9c1..f7f9ce2 100644 --- a/llvm/tools/llvm-rc/llvm-rc.cpp +++ b/llvm/tools/llvm-rc/llvm-rc.cpp @@ -55,7 +55,11 @@ enum ID { #undef OPTION }; -#define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE; +namespace rc_opt { +#define PREFIX(NAME, VALUE) \ + static constexpr StringLiteral NAME##_init[] = VALUE; \ + static constexpr ArrayRef<StringLiteral> NAME(NAME##_init, \ + std::size(NAME##_init) - 1); #include "Opts.inc" #undef PREFIX @@ -70,10 +74,11 @@ static constexpr opt::OptTable::Info InfoTable[] = { #include "Opts.inc" #undef OPTION }; +} // namespace rc_opt class RcOptTable : public opt::OptTable { public: - RcOptTable() : OptTable(InfoTable, /* IgnoreCase = */ true) {} + RcOptTable() : OptTable(rc_opt::InfoTable, /* IgnoreCase = */ true) {} }; enum Windres_ID { @@ -85,25 +90,30 @@ enum Windres_ID { #undef OPTION }; -#define PREFIX(NAME, VALUE) const char *const WINDRES_##NAME[] = VALUE; +namespace windres_opt { +#define PREFIX(NAME, VALUE) \ + static constexpr StringLiteral NAME##_init[] = VALUE; \ + static constexpr ArrayRef<StringLiteral> NAME(NAME##_init, \ + std::size(NAME##_init) - 1); #include "WindresOpts.inc" #undef PREFIX -static constexpr opt::OptTable::Info WindresInfoTable[] = { +static constexpr opt::OptTable::Info InfoTable[] = { #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \ HELPTEXT, METAVAR, VALUES) \ - { \ - WINDRES_##PREFIX, NAME, HELPTEXT, \ - METAVAR, WINDRES_##ID, opt::Option::KIND##Class, \ - PARAM, FLAGS, WINDRES_##GROUP, \ - WINDRES_##ALIAS, ALIASARGS, VALUES}, + {PREFIX, NAME, HELPTEXT, \ + METAVAR, WINDRES_##ID, opt::Option::KIND##Class, \ + PARAM, FLAGS, WINDRES_##GROUP, \ + WINDRES_##ALIAS, ALIASARGS, VALUES}, #include "WindresOpts.inc" #undef OPTION }; +} // namespace windres_opt class WindresOptTable : public opt::OptTable { public: - WindresOptTable() : OptTable(WindresInfoTable, /* IgnoreCase = */ false) {} + WindresOptTable() + : OptTable(windres_opt::InfoTable, /* IgnoreCase = */ false) {} }; static ExitOnError ExitOnErr; diff --git a/llvm/tools/llvm-readobj/llvm-readobj.cpp b/llvm/tools/llvm-readobj/llvm-readobj.cpp index 7cb2036..89f6a89 100644 --- a/llvm/tools/llvm-readobj/llvm-readobj.cpp +++ b/llvm/tools/llvm-readobj/llvm-readobj.cpp @@ -61,7 +61,10 @@ enum ID { #undef OPTION }; -#define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE; +#define PREFIX(NAME, VALUE) \ + static constexpr StringLiteral NAME##_init[] = VALUE; \ + static constexpr ArrayRef<StringLiteral> NAME(NAME##_init, \ + std::size(NAME##_init) - 1); #include "Opts.inc" #undef PREFIX diff --git a/llvm/tools/llvm-size/llvm-size.cpp b/llvm/tools/llvm-size/llvm-size.cpp index de2e285..07fdab4 100644 --- a/llvm/tools/llvm-size/llvm-size.cpp +++ b/llvm/tools/llvm-size/llvm-size.cpp @@ -47,7 +47,10 @@ enum ID { #undef OPTION }; -#define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE; +#define PREFIX(NAME, VALUE) \ + static constexpr StringLiteral NAME##_init[] = VALUE; \ + static constexpr ArrayRef<StringLiteral> NAME(NAME##_init, \ + std::size(NAME##_init) - 1); #include "Opts.inc" #undef PREFIX diff --git a/llvm/tools/llvm-strings/llvm-strings.cpp b/llvm/tools/llvm-strings/llvm-strings.cpp index 918ca5f..69a3b12 100644 --- a/llvm/tools/llvm-strings/llvm-strings.cpp +++ b/llvm/tools/llvm-strings/llvm-strings.cpp @@ -39,7 +39,10 @@ enum ID { #undef OPTION }; -#define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE; +#define PREFIX(NAME, VALUE) \ + static constexpr StringLiteral NAME##_init[] = VALUE; \ + static constexpr ArrayRef<StringLiteral> NAME(NAME##_init, \ + std::size(NAME##_init) - 1); #include "Opts.inc" #undef PREFIX diff --git a/llvm/tools/llvm-symbolizer/llvm-symbolizer.cpp b/llvm/tools/llvm-symbolizer/llvm-symbolizer.cpp index 833506a..f38870c 100644 --- a/llvm/tools/llvm-symbolizer/llvm-symbolizer.cpp +++ b/llvm/tools/llvm-symbolizer/llvm-symbolizer.cpp @@ -56,7 +56,10 @@ enum ID { #undef OPTION }; -#define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE; +#define PREFIX(NAME, VALUE) \ + static constexpr StringLiteral NAME##_init[] = VALUE; \ + static constexpr ArrayRef<StringLiteral> NAME(NAME##_init, \ + std::size(NAME##_init) - 1); #include "Opts.inc" #undef PREFIX diff --git a/llvm/tools/llvm-tli-checker/llvm-tli-checker.cpp b/llvm/tools/llvm-tli-checker/llvm-tli-checker.cpp index 05fb5ff..1ac7189 100644 --- a/llvm/tools/llvm-tli-checker/llvm-tli-checker.cpp +++ b/llvm/tools/llvm-tli-checker/llvm-tli-checker.cpp @@ -35,7 +35,10 @@ enum ID { #undef OPTION }; -#define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE; +#define PREFIX(NAME, VALUE) \ + static constexpr StringLiteral NAME##_init[] = VALUE; \ + static constexpr ArrayRef<StringLiteral> NAME(NAME##_init, \ + std::size(NAME##_init) - 1); #include "Opts.inc" #undef PREFIX diff --git a/llvm/unittests/Option/OptionParsingTest.cpp b/llvm/unittests/Option/OptionParsingTest.cpp index 1857345..19f68c0 100644 --- a/llvm/unittests/Option/OptionParsingTest.cpp +++ b/llvm/unittests/Option/OptionParsingTest.cpp @@ -25,7 +25,10 @@ enum ID { #undef OPTION }; -#define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE; +#define PREFIX(NAME, VALUE) \ + static constexpr StringLiteral NAME##_init[] = VALUE; \ + static constexpr ArrayRef<StringLiteral> NAME(NAME##_init, \ + std::size(NAME##_init) - 1); #include "Opts.inc" #undef PREFIX diff --git a/llvm/utils/TableGen/OptParserEmitter.cpp b/llvm/utils/TableGen/OptParserEmitter.cpp index e8bc9da..8c57dda 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 << "llvm::StringRef(&"; - write_cstring(OS, StringRef(getOptionSpelling(R, PrefixLength))); - OS << "[" << PrefixLength << "], " << R.getValueAsString("Name").size() << ")"; + OS << "llvm::StringLiteral("; + write_cstring( + OS, StringRef(getOptionSpelling(R, PrefixLength)).substr(PrefixLength)); + OS << ")"; } class MarshallingInfo { @@ -251,8 +252,9 @@ void EmitOptParser(RecordKeeper &Records, raw_ostream &OS) { // Prefix values. OS << ", {"; for (const auto &PrefixKey : Prefix.first) - OS << "\"" << PrefixKey << "\" COMMA "; - OS << "nullptr})\n"; + OS << "llvm::StringLiteral(\"" << PrefixKey << "\") COMMA "; + // Append an empty element to avoid ending up with an empty array. + OS << "llvm::StringLiteral(\"\")})\n"; } OS << "#undef COMMA\n"; OS << "#endif // PREFIX\n\n"; @@ -265,7 +267,7 @@ void EmitOptParser(RecordKeeper &Records, raw_ostream &OS) { OS << "OPTION("; // The option prefix; - OS << "nullptr"; + OS << "llvm::ArrayRef<llvm::StringLiteral>()"; // The option string. OS << ", \"" << R.getValueAsString("Name") << '"'; |