Eugene Zelenko | 1d475d8 | 2017-06-16 00:43:26 +0000 | [diff] [blame] | 1 | //===- OptTable.cpp - Option Table Implementation -------------------------===// |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
David Majnemer | 975248e | 2016-08-11 22:21:41 +0000 | [diff] [blame] | 10 | #include "llvm/ADT/STLExtras.h" |
Eugene Zelenko | 1d475d8 | 2017-06-16 00:43:26 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/StringRef.h" |
| 12 | #include "llvm/ADT/StringSet.h" |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 13 | #include "llvm/Option/Arg.h" |
| 14 | #include "llvm/Option/ArgList.h" |
| 15 | #include "llvm/Option/Option.h" |
Eugene Zelenko | 1d475d8 | 2017-06-16 00:43:26 +0000 | [diff] [blame] | 16 | #include "llvm/Option/OptSpecifier.h" |
| 17 | #include "llvm/Option/OptTable.h" |
| 18 | #include "llvm/Support/Compiler.h" |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 19 | #include "llvm/Support/ErrorHandling.h" |
Chandler Carruth | 58a2cbe | 2013-01-02 10:22:59 +0000 | [diff] [blame] | 20 | #include "llvm/Support/raw_ostream.h" |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 21 | #include <algorithm> |
Eugene Zelenko | 1d475d8 | 2017-06-16 00:43:26 +0000 | [diff] [blame] | 22 | #include <cassert> |
Rui Ueyama | 2957273 | 2013-08-28 20:04:31 +0000 | [diff] [blame] | 23 | #include <cctype> |
Eugene Zelenko | 1d475d8 | 2017-06-16 00:43:26 +0000 | [diff] [blame] | 24 | #include <cstring> |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 25 | #include <map> |
Eugene Zelenko | 1d475d8 | 2017-06-16 00:43:26 +0000 | [diff] [blame] | 26 | #include <string> |
| 27 | #include <utility> |
| 28 | #include <vector> |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 29 | |
| 30 | using namespace llvm; |
| 31 | using namespace llvm::opt; |
| 32 | |
Rui Ueyama | 2957273 | 2013-08-28 20:04:31 +0000 | [diff] [blame] | 33 | namespace llvm { |
| 34 | namespace opt { |
Rui Ueyama | 055f4e9 | 2013-08-27 23:47:01 +0000 | [diff] [blame] | 35 | |
Rui Ueyama | 2957273 | 2013-08-28 20:04:31 +0000 | [diff] [blame] | 36 | // Ordering on Info. The ordering is *almost* case-insensitive lexicographic, |
Nathan Hawes | 71f4afc | 2017-08-24 21:20:41 +0000 | [diff] [blame] | 37 | // with an exception. '\0' comes at the end of the alphabet instead of the |
Rui Ueyama | 2957273 | 2013-08-28 20:04:31 +0000 | [diff] [blame] | 38 | // beginning (thus options precede any other options which prefix them). |
| 39 | static int StrCmpOptionNameIgnoreCase(const char *A, const char *B) { |
| 40 | const char *X = A, *Y = B; |
| 41 | char a = tolower(*A), b = tolower(*B); |
Rui Ueyama | 1997734 | 2013-08-28 00:02:06 +0000 | [diff] [blame] | 42 | while (a == b) { |
| 43 | if (a == '\0') |
| 44 | return 0; |
| 45 | |
Rui Ueyama | 2957273 | 2013-08-28 20:04:31 +0000 | [diff] [blame] | 46 | a = tolower(*++X); |
| 47 | b = tolower(*++Y); |
Rui Ueyama | 1997734 | 2013-08-28 00:02:06 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | if (a == '\0') // A is a prefix of B. |
| 51 | return 1; |
| 52 | if (b == '\0') // B is a prefix of A. |
| 53 | return -1; |
| 54 | |
| 55 | // Otherwise lexicographic. |
| 56 | return (a < b) ? -1 : 1; |
Rui Ueyama | 055f4e9 | 2013-08-27 23:47:01 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Eli Friedman | add560e | 2013-09-10 23:22:56 +0000 | [diff] [blame] | 59 | #ifndef NDEBUG |
| 60 | static int StrCmpOptionName(const char *A, const char *B) { |
| 61 | if (int N = StrCmpOptionNameIgnoreCase(A, B)) |
| 62 | return N; |
| 63 | return strcmp(A, B); |
| 64 | } |
| 65 | |
| 66 | static inline bool operator<(const OptTable::Info &A, const OptTable::Info &B) { |
| 67 | if (&A == &B) |
| 68 | return false; |
| 69 | |
| 70 | if (int N = StrCmpOptionName(A.Name, B.Name)) |
| 71 | return N < 0; |
| 72 | |
| 73 | for (const char * const *APre = A.Prefixes, |
| 74 | * const *BPre = B.Prefixes; |
Craig Topper | 0b6cb71 | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 75 | *APre != nullptr && *BPre != nullptr; ++APre, ++BPre){ |
Eli Friedman | add560e | 2013-09-10 23:22:56 +0000 | [diff] [blame] | 76 | if (int N = StrCmpOptionName(*APre, *BPre)) |
| 77 | return N < 0; |
| 78 | } |
| 79 | |
| 80 | // Names are the same, check that classes are in order; exactly one |
| 81 | // should be joined, and it should succeed the other. |
| 82 | assert(((A.Kind == Option::JoinedClass) ^ (B.Kind == Option::JoinedClass)) && |
| 83 | "Unexpected classes for options with same name."); |
| 84 | return B.Kind == Option::JoinedClass; |
| 85 | } |
| 86 | #endif |
| 87 | |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 88 | // Support lower_bound between info and an option name. |
| 89 | static inline bool operator<(const OptTable::Info &I, const char *Name) { |
Rui Ueyama | 2957273 | 2013-08-28 20:04:31 +0000 | [diff] [blame] | 90 | return StrCmpOptionNameIgnoreCase(I.Name, Name) < 0; |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 91 | } |
Eugene Zelenko | 1d475d8 | 2017-06-16 00:43:26 +0000 | [diff] [blame] | 92 | |
| 93 | } // end namespace opt |
| 94 | } // end namespace llvm |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 95 | |
| 96 | OptSpecifier::OptSpecifier(const Option *Opt) : ID(Opt->getID()) {} |
| 97 | |
Craig Topper | 94f8b07 | 2015-10-21 16:30:42 +0000 | [diff] [blame] | 98 | OptTable::OptTable(ArrayRef<Info> OptionInfos, bool IgnoreCase) |
Eugene Zelenko | 1d475d8 | 2017-06-16 00:43:26 +0000 | [diff] [blame] | 99 | : OptionInfos(OptionInfos), IgnoreCase(IgnoreCase) { |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 100 | // Explicitly zero initialize the error to work around a bug in array |
| 101 | // value-initialization on MinGW with gcc 4.3.5. |
| 102 | |
| 103 | // Find start of normal options. |
| 104 | for (unsigned i = 0, e = getNumOptions(); i != e; ++i) { |
| 105 | unsigned Kind = getInfo(i + 1).Kind; |
| 106 | if (Kind == Option::InputClass) { |
| 107 | assert(!TheInputOptionID && "Cannot have multiple input options!"); |
| 108 | TheInputOptionID = getInfo(i + 1).ID; |
| 109 | } else if (Kind == Option::UnknownClass) { |
| 110 | assert(!TheUnknownOptionID && "Cannot have multiple unknown options!"); |
| 111 | TheUnknownOptionID = getInfo(i + 1).ID; |
| 112 | } else if (Kind != Option::GroupClass) { |
| 113 | FirstSearchableIndex = i; |
| 114 | break; |
| 115 | } |
| 116 | } |
| 117 | assert(FirstSearchableIndex != 0 && "No searchable options?"); |
| 118 | |
| 119 | #ifndef NDEBUG |
| 120 | // Check that everything after the first searchable option is a |
| 121 | // regular option class. |
| 122 | for (unsigned i = FirstSearchableIndex, e = getNumOptions(); i != e; ++i) { |
| 123 | Option::OptionClass Kind = (Option::OptionClass) getInfo(i + 1).Kind; |
| 124 | assert((Kind != Option::InputClass && Kind != Option::UnknownClass && |
| 125 | Kind != Option::GroupClass) && |
| 126 | "Special options should be defined first!"); |
| 127 | } |
| 128 | |
| 129 | // Check that options are in order. |
| 130 | for (unsigned i = FirstSearchableIndex + 1, e = getNumOptions(); i != e; ++i){ |
| 131 | if (!(getInfo(i) < getInfo(i + 1))) { |
| 132 | getOption(i).dump(); |
| 133 | getOption(i + 1).dump(); |
| 134 | llvm_unreachable("Options are not in order!"); |
| 135 | } |
| 136 | } |
| 137 | #endif |
| 138 | |
| 139 | // Build prefixes. |
| 140 | for (unsigned i = FirstSearchableIndex + 1, e = getNumOptions() + 1; |
| 141 | i != e; ++i) { |
| 142 | if (const char *const *P = getInfo(i).Prefixes) { |
Craig Topper | 0b6cb71 | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 143 | for (; *P != nullptr; ++P) { |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 144 | PrefixesUnion.insert(*P); |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | // Build prefix chars. |
Eugene Zelenko | 1d475d8 | 2017-06-16 00:43:26 +0000 | [diff] [blame] | 150 | for (StringSet<>::const_iterator I = PrefixesUnion.begin(), |
| 151 | E = PrefixesUnion.end(); I != E; ++I) { |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 152 | StringRef Prefix = I->getKey(); |
| 153 | for (StringRef::const_iterator C = Prefix.begin(), CE = Prefix.end(); |
| 154 | C != CE; ++C) |
David Majnemer | 975248e | 2016-08-11 22:21:41 +0000 | [diff] [blame] | 155 | if (!is_contained(PrefixChars, *C)) |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 156 | PrefixChars.push_back(*C); |
| 157 | } |
| 158 | } |
| 159 | |
Eugene Zelenko | 1d475d8 | 2017-06-16 00:43:26 +0000 | [diff] [blame] | 160 | OptTable::~OptTable() = default; |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 161 | |
| 162 | const Option OptTable::getOption(OptSpecifier Opt) const { |
| 163 | unsigned id = Opt.getID(); |
| 164 | if (id == 0) |
Craig Topper | 0b6cb71 | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 165 | return Option(nullptr, nullptr); |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 166 | assert((unsigned) (id - 1) < getNumOptions() && "Invalid ID."); |
| 167 | return Option(&getInfo(id), this); |
| 168 | } |
| 169 | |
Eugene Zelenko | 1d475d8 | 2017-06-16 00:43:26 +0000 | [diff] [blame] | 170 | static bool isInput(const StringSet<> &Prefixes, StringRef Arg) { |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 171 | if (Arg == "-") |
| 172 | return true; |
Eugene Zelenko | 1d475d8 | 2017-06-16 00:43:26 +0000 | [diff] [blame] | 173 | for (StringSet<>::const_iterator I = Prefixes.begin(), |
| 174 | E = Prefixes.end(); I != E; ++I) |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 175 | if (Arg.startswith(I->getKey())) |
| 176 | return false; |
| 177 | return true; |
| 178 | } |
| 179 | |
| 180 | /// \returns Matched size. 0 means no match. |
Rui Ueyama | 2957273 | 2013-08-28 20:04:31 +0000 | [diff] [blame] | 181 | static unsigned matchOption(const OptTable::Info *I, StringRef Str, |
| 182 | bool IgnoreCase) { |
Craig Topper | 0b6cb71 | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 183 | for (const char * const *Pre = I->Prefixes; *Pre != nullptr; ++Pre) { |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 184 | StringRef Prefix(*Pre); |
Rui Ueyama | 2957273 | 2013-08-28 20:04:31 +0000 | [diff] [blame] | 185 | if (Str.startswith(Prefix)) { |
| 186 | StringRef Rest = Str.substr(Prefix.size()); |
| 187 | bool Matched = IgnoreCase |
Jakub Staszak | 42f2a6b | 2013-11-04 19:22:50 +0000 | [diff] [blame] | 188 | ? Rest.startswith_lower(I->Name) |
Rui Ueyama | 2957273 | 2013-08-28 20:04:31 +0000 | [diff] [blame] | 189 | : Rest.startswith(I->Name); |
| 190 | if (Matched) |
| 191 | return Prefix.size() + StringRef(I->Name).size(); |
| 192 | } |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 193 | } |
| 194 | return 0; |
| 195 | } |
| 196 | |
Yuka Takahashi | bc5df29 | 2017-06-20 16:31:31 +0000 | [diff] [blame] | 197 | // Returns true if one of the Prefixes + In.Names matches Option |
| 198 | static bool optionMatches(const OptTable::Info &In, StringRef Option) { |
Yuka Takahashi | f9f2c95 | 2017-08-29 00:09:31 +0000 | [diff] [blame] | 199 | if (In.Prefixes) |
Yuka Takahashi | bc5df29 | 2017-06-20 16:31:31 +0000 | [diff] [blame] | 200 | for (size_t I = 0; In.Prefixes[I]; I++) |
| 201 | if (Option == std::string(In.Prefixes[I]) + In.Name) |
| 202 | return true; |
| 203 | return false; |
| 204 | } |
| 205 | |
| 206 | // This function is for flag value completion. |
| 207 | // Eg. When "-stdlib=" and "l" was passed to this function, it will return |
| 208 | // appropiriate values for stdlib, which starts with l. |
| 209 | std::vector<std::string> |
| 210 | OptTable::suggestValueCompletions(StringRef Option, StringRef Arg) const { |
| 211 | // Search all options and return possible values. |
Yuka Takahashi | f9f2c95 | 2017-08-29 00:09:31 +0000 | [diff] [blame] | 212 | for (size_t I = FirstSearchableIndex, E = OptionInfos.size(); I < E; I++) { |
| 213 | const Info &In = OptionInfos[I]; |
| 214 | if (!In.Values || !optionMatches(In, Option)) |
Yuka Takahashi | bc5df29 | 2017-06-20 16:31:31 +0000 | [diff] [blame] | 215 | continue; |
| 216 | |
| 217 | SmallVector<StringRef, 8> Candidates; |
| 218 | StringRef(In.Values).split(Candidates, ",", -1, false); |
| 219 | |
| 220 | std::vector<std::string> Result; |
| 221 | for (StringRef Val : Candidates) |
Yuka Takahashi | fce2d38 | 2018-03-05 08:54:20 +0000 | [diff] [blame] | 222 | if (Val.startswith(Arg) && Arg.compare(Val)) |
Yuka Takahashi | bc5df29 | 2017-06-20 16:31:31 +0000 | [diff] [blame] | 223 | Result.push_back(Val); |
| 224 | return Result; |
| 225 | } |
| 226 | return {}; |
| 227 | } |
| 228 | |
Yuka Takahashi | fb5cc88 | 2017-07-08 17:48:59 +0000 | [diff] [blame] | 229 | std::vector<std::string> |
| 230 | OptTable::findByPrefix(StringRef Cur, unsigned short DisableFlags) const { |
Yuka Takahashi | 476b551 | 2017-05-23 18:39:08 +0000 | [diff] [blame] | 231 | std::vector<std::string> Ret; |
Yuka Takahashi | f9f2c95 | 2017-08-29 00:09:31 +0000 | [diff] [blame] | 232 | for (size_t I = FirstSearchableIndex, E = OptionInfos.size(); I < E; I++) { |
| 233 | const Info &In = OptionInfos[I]; |
Yuka Takahashi | 7a41a9b | 2017-07-05 02:36:32 +0000 | [diff] [blame] | 234 | if (!In.Prefixes || (!In.HelpText && !In.GroupID)) |
Yuka Takahashi | 476b551 | 2017-05-23 18:39:08 +0000 | [diff] [blame] | 235 | continue; |
Yuka Takahashi | fb5cc88 | 2017-07-08 17:48:59 +0000 | [diff] [blame] | 236 | if (In.Flags & DisableFlags) |
| 237 | continue; |
| 238 | |
Yuka Takahashi | 476b551 | 2017-05-23 18:39:08 +0000 | [diff] [blame] | 239 | for (int I = 0; In.Prefixes[I]; I++) { |
Yuka Takahashi | 6f6a181 | 2017-07-26 13:36:58 +0000 | [diff] [blame] | 240 | std::string S = std::string(In.Prefixes[I]) + std::string(In.Name) + "\t"; |
| 241 | if (In.HelpText) |
| 242 | S += In.HelpText; |
Yuka Takahashi | fce2d38 | 2018-03-05 08:54:20 +0000 | [diff] [blame] | 243 | if (StringRef(S).startswith(Cur) && S.compare(std::string(Cur) + "\t")) |
Yuka Takahashi | 476b551 | 2017-05-23 18:39:08 +0000 | [diff] [blame] | 244 | Ret.push_back(S); |
| 245 | } |
| 246 | } |
| 247 | return Ret; |
| 248 | } |
| 249 | |
Brian Gesiak | 1fed623 | 2018-01-05 17:10:39 +0000 | [diff] [blame] | 250 | unsigned OptTable::findNearest(StringRef Option, std::string &NearestString, |
| 251 | unsigned FlagsToInclude, unsigned FlagsToExclude, |
| 252 | unsigned MinimumLength) const { |
| 253 | assert(!Option.empty()); |
| 254 | |
Brian Gesiak | 2ea8666 | 2018-09-03 18:13:46 +0000 | [diff] [blame] | 255 | // Consider each option as a candidate, finding the closest match. |
Brian Gesiak | 1fed623 | 2018-01-05 17:10:39 +0000 | [diff] [blame] | 256 | unsigned BestDistance = UINT_MAX; |
| 257 | for (const Info &CandidateInfo : |
| 258 | ArrayRef<Info>(OptionInfos).drop_front(FirstSearchableIndex)) { |
| 259 | StringRef CandidateName = CandidateInfo.Name; |
| 260 | |
Brian Gesiak | 2ea8666 | 2018-09-03 18:13:46 +0000 | [diff] [blame] | 261 | // Ignore option candidates with empty names, such as "--", or names |
| 262 | // that do not meet the minimum length. |
Brian Gesiak | 1fed623 | 2018-01-05 17:10:39 +0000 | [diff] [blame] | 263 | if (CandidateName.empty() || CandidateName.size() < MinimumLength) |
| 264 | continue; |
| 265 | |
Brian Gesiak | 2ea8666 | 2018-09-03 18:13:46 +0000 | [diff] [blame] | 266 | // If FlagsToInclude were specified, ignore options that don't include |
| 267 | // those flags. |
Brian Gesiak | 1fed623 | 2018-01-05 17:10:39 +0000 | [diff] [blame] | 268 | if (FlagsToInclude && !(CandidateInfo.Flags & FlagsToInclude)) |
| 269 | continue; |
Brian Gesiak | 2ea8666 | 2018-09-03 18:13:46 +0000 | [diff] [blame] | 270 | // Ignore options that contain the FlagsToExclude. |
Brian Gesiak | 1fed623 | 2018-01-05 17:10:39 +0000 | [diff] [blame] | 271 | if (CandidateInfo.Flags & FlagsToExclude) |
| 272 | continue; |
| 273 | |
Brian Gesiak | 2ea8666 | 2018-09-03 18:13:46 +0000 | [diff] [blame] | 274 | // Ignore positional argument option candidates (which do not |
| 275 | // have prefixes). |
Brian Gesiak | 1fed623 | 2018-01-05 17:10:39 +0000 | [diff] [blame] | 276 | if (!CandidateInfo.Prefixes) |
| 277 | continue; |
Brian Gesiak | 2ea8666 | 2018-09-03 18:13:46 +0000 | [diff] [blame] | 278 | // Find the most appropriate prefix. For example, if a user asks for |
| 279 | // "--helm", suggest "--help" over "-help". |
| 280 | StringRef Prefix = CandidateInfo.Prefixes[0]; |
| 281 | for (int P = 1; CandidateInfo.Prefixes[P]; P++) { |
| 282 | if (Option.startswith(CandidateInfo.Prefixes[P])) |
| 283 | Prefix = CandidateInfo.Prefixes[P]; |
| 284 | } |
Brian Gesiak | 1fed623 | 2018-01-05 17:10:39 +0000 | [diff] [blame] | 285 | |
Brian Gesiak | 2ea8666 | 2018-09-03 18:13:46 +0000 | [diff] [blame] | 286 | // Check if the candidate ends with a character commonly used when |
Brian Gesiak | 1fed623 | 2018-01-05 17:10:39 +0000 | [diff] [blame] | 287 | // delimiting an option from its value, such as '=' or ':'. If it does, |
| 288 | // attempt to split the given option based on that delimiter. |
| 289 | std::string Delimiter = ""; |
| 290 | char Last = CandidateName.back(); |
| 291 | if (Last == '=' || Last == ':') |
| 292 | Delimiter = std::string(1, Last); |
| 293 | |
| 294 | StringRef LHS, RHS; |
| 295 | if (Delimiter.empty()) |
| 296 | LHS = Option; |
| 297 | else |
| 298 | std::tie(LHS, RHS) = Option.split(Last); |
| 299 | |
Brian Gesiak | 2ea8666 | 2018-09-03 18:13:46 +0000 | [diff] [blame] | 300 | std::string NormalizedName = |
| 301 | (LHS.drop_front(Prefix.size()) + Delimiter).str(); |
| 302 | unsigned Distance = |
| 303 | CandidateName.edit_distance(NormalizedName, /*AllowReplacements=*/true, |
| 304 | /*MaxEditDistance=*/BestDistance); |
| 305 | if (Distance < BestDistance) { |
| 306 | BestDistance = Distance; |
| 307 | NearestString = (Prefix + CandidateName + RHS).str(); |
Brian Gesiak | 1fed623 | 2018-01-05 17:10:39 +0000 | [diff] [blame] | 308 | } |
| 309 | } |
| 310 | return BestDistance; |
| 311 | } |
| 312 | |
Yuka Takahashi | f9f2c95 | 2017-08-29 00:09:31 +0000 | [diff] [blame] | 313 | bool OptTable::addValues(const char *Option, const char *Values) { |
| 314 | for (size_t I = FirstSearchableIndex, E = OptionInfos.size(); I < E; I++) { |
| 315 | Info &In = OptionInfos[I]; |
| 316 | if (optionMatches(In, Option)) { |
| 317 | In.Values = Values; |
| 318 | return true; |
| 319 | } |
| 320 | } |
| 321 | return false; |
| 322 | } |
| 323 | |
Reid Kleckner | a2549d3 | 2013-07-19 18:04:57 +0000 | [diff] [blame] | 324 | Arg *OptTable::ParseOneArg(const ArgList &Args, unsigned &Index, |
| 325 | unsigned FlagsToInclude, |
| 326 | unsigned FlagsToExclude) const { |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 327 | unsigned Prev = Index; |
| 328 | const char *Str = Args.getArgString(Index); |
| 329 | |
| 330 | // Anything that doesn't start with PrefixesUnion is an input, as is '-' |
| 331 | // itself. |
| 332 | if (isInput(PrefixesUnion, Str)) |
| 333 | return new Arg(getOption(TheInputOptionID), Str, Index++, Str); |
| 334 | |
Yuka Takahashi | f9f2c95 | 2017-08-29 00:09:31 +0000 | [diff] [blame] | 335 | const Info *Start = OptionInfos.data() + FirstSearchableIndex; |
| 336 | const Info *End = OptionInfos.data() + OptionInfos.size(); |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 337 | StringRef Name = StringRef(Str).ltrim(PrefixChars); |
| 338 | |
| 339 | // Search for the first next option which could be a prefix. |
| 340 | Start = std::lower_bound(Start, End, Name.data()); |
| 341 | |
| 342 | // Options are stored in sorted order, with '\0' at the end of the |
| 343 | // alphabet. Since the only options which can accept a string must |
| 344 | // prefix it, we iteratively search for the next option which could |
| 345 | // be a prefix. |
| 346 | // |
| 347 | // FIXME: This is searching much more than necessary, but I am |
| 348 | // blanking on the simplest way to make it fast. We can solve this |
| 349 | // problem when we move to TableGen. |
| 350 | for (; Start != End; ++Start) { |
| 351 | unsigned ArgSize = 0; |
| 352 | // Scan for first option which is a proper prefix. |
| 353 | for (; Start != End; ++Start) |
Rui Ueyama | 2957273 | 2013-08-28 20:04:31 +0000 | [diff] [blame] | 354 | if ((ArgSize = matchOption(Start, Str, IgnoreCase))) |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 355 | break; |
| 356 | if (Start == End) |
| 357 | break; |
| 358 | |
Reid Kleckner | a2549d3 | 2013-07-19 18:04:57 +0000 | [diff] [blame] | 359 | Option Opt(Start, this); |
| 360 | |
| 361 | if (FlagsToInclude && !Opt.hasFlag(FlagsToInclude)) |
| 362 | continue; |
| 363 | if (Opt.hasFlag(FlagsToExclude)) |
| 364 | continue; |
| 365 | |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 366 | // See if this option matches. |
Reid Kleckner | a2549d3 | 2013-07-19 18:04:57 +0000 | [diff] [blame] | 367 | if (Arg *A = Opt.accept(Args, Index, ArgSize)) |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 368 | return A; |
| 369 | |
| 370 | // Otherwise, see if this argument was missing values. |
| 371 | if (Prev != Index) |
Craig Topper | 0b6cb71 | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 372 | return nullptr; |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 373 | } |
| 374 | |
Reid Kleckner | a2549d3 | 2013-07-19 18:04:57 +0000 | [diff] [blame] | 375 | // If we failed to find an option and this arg started with /, then it's |
| 376 | // probably an input path. |
| 377 | if (Str[0] == '/') |
| 378 | return new Arg(getOption(TheInputOptionID), Str, Index++, Str); |
| 379 | |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 380 | return new Arg(getOption(TheUnknownOptionID), Str, Index++, Str); |
| 381 | } |
| 382 | |
David Blaikie | 0c17f95 | 2015-06-22 22:06:37 +0000 | [diff] [blame] | 383 | InputArgList OptTable::ParseArgs(ArrayRef<const char *> ArgArr, |
| 384 | unsigned &MissingArgIndex, |
| 385 | unsigned &MissingArgCount, |
| 386 | unsigned FlagsToInclude, |
| 387 | unsigned FlagsToExclude) const { |
| 388 | InputArgList Args(ArgArr.begin(), ArgArr.end()); |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 389 | |
| 390 | // FIXME: Handle '@' args (or at least error on them). |
| 391 | |
| 392 | MissingArgIndex = MissingArgCount = 0; |
David Blaikie | 006bce7 | 2015-06-21 06:31:53 +0000 | [diff] [blame] | 393 | unsigned Index = 0, End = ArgArr.size(); |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 394 | while (Index < End) { |
Reid Kleckner | 2e1bf78 | 2014-08-22 19:29:17 +0000 | [diff] [blame] | 395 | // Ingore nullptrs, they are response file's EOL markers |
David Blaikie | 0c17f95 | 2015-06-22 22:06:37 +0000 | [diff] [blame] | 396 | if (Args.getArgString(Index) == nullptr) { |
Reid Kleckner | 2e1bf78 | 2014-08-22 19:29:17 +0000 | [diff] [blame] | 397 | ++Index; |
| 398 | continue; |
| 399 | } |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 400 | // Ignore empty arguments (other things may still take them as arguments). |
David Blaikie | 0c17f95 | 2015-06-22 22:06:37 +0000 | [diff] [blame] | 401 | StringRef Str = Args.getArgString(Index); |
Hans Wennborg | 6bf104b | 2013-08-02 21:20:27 +0000 | [diff] [blame] | 402 | if (Str == "") { |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 403 | ++Index; |
| 404 | continue; |
| 405 | } |
| 406 | |
| 407 | unsigned Prev = Index; |
David Blaikie | 0c17f95 | 2015-06-22 22:06:37 +0000 | [diff] [blame] | 408 | Arg *A = ParseOneArg(Args, Index, FlagsToInclude, FlagsToExclude); |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 409 | assert(Index > Prev && "Parser failed to consume argument."); |
| 410 | |
| 411 | // Check for missing argument error. |
| 412 | if (!A) { |
| 413 | assert(Index >= End && "Unexpected parser error."); |
| 414 | assert(Index - Prev - 1 && "No missing arguments!"); |
| 415 | MissingArgIndex = Prev; |
| 416 | MissingArgCount = Index - Prev - 1; |
| 417 | break; |
| 418 | } |
| 419 | |
David Blaikie | 0c17f95 | 2015-06-22 22:06:37 +0000 | [diff] [blame] | 420 | Args.append(A); |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 421 | } |
| 422 | |
Logan Chien | 3755f9f | 2015-06-22 23:16:02 +0000 | [diff] [blame] | 423 | return Args; |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | static std::string getOptionHelpName(const OptTable &Opts, OptSpecifier Id) { |
| 427 | const Option O = Opts.getOption(Id); |
| 428 | std::string Name = O.getPrefixedName(); |
| 429 | |
| 430 | // Add metavar, if used. |
| 431 | switch (O.getKind()) { |
| 432 | case Option::GroupClass: case Option::InputClass: case Option::UnknownClass: |
| 433 | llvm_unreachable("Invalid option with help text."); |
| 434 | |
| 435 | case Option::MultiArgClass: |
Nick Kledzik | 4a8a0f2 | 2014-08-15 21:35:07 +0000 | [diff] [blame] | 436 | if (const char *MetaVarName = Opts.getOptionMetaVar(Id)) { |
| 437 | // For MultiArgs, metavar is full list of all argument names. |
| 438 | Name += ' '; |
| 439 | Name += MetaVarName; |
| 440 | } |
| 441 | else { |
| 442 | // For MultiArgs<N>, if metavar not supplied, print <value> N times. |
| 443 | for (unsigned i=0, e=O.getNumArgs(); i< e; ++i) { |
| 444 | Name += " <value>"; |
| 445 | } |
| 446 | } |
| 447 | break; |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 448 | |
| 449 | case Option::FlagClass: |
| 450 | break; |
| 451 | |
Yuka Takahashi | bc5df29 | 2017-06-20 16:31:31 +0000 | [diff] [blame] | 452 | case Option::ValuesClass: |
| 453 | break; |
| 454 | |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 455 | case Option::SeparateClass: case Option::JoinedOrSeparateClass: |
Hans Wennborg | 9f34fd5 | 2016-04-15 00:23:30 +0000 | [diff] [blame] | 456 | case Option::RemainingArgsClass: case Option::RemainingArgsJoinedClass: |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 457 | Name += ' '; |
Justin Bogner | 6673ea8 | 2016-08-17 05:10:15 +0000 | [diff] [blame] | 458 | LLVM_FALLTHROUGH; |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 459 | case Option::JoinedClass: case Option::CommaJoinedClass: |
| 460 | case Option::JoinedAndSeparateClass: |
| 461 | if (const char *MetaVarName = Opts.getOptionMetaVar(Id)) |
| 462 | Name += MetaVarName; |
| 463 | else |
| 464 | Name += "<value>"; |
| 465 | break; |
| 466 | } |
| 467 | |
| 468 | return Name; |
| 469 | } |
| 470 | |
George Rimar | cd31858 | 2017-07-18 10:59:30 +0000 | [diff] [blame] | 471 | namespace { |
| 472 | struct OptionInfo { |
| 473 | std::string Name; |
| 474 | StringRef HelpText; |
| 475 | }; |
| 476 | } // namespace |
| 477 | |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 478 | static void PrintHelpOptionList(raw_ostream &OS, StringRef Title, |
George Rimar | cd31858 | 2017-07-18 10:59:30 +0000 | [diff] [blame] | 479 | std::vector<OptionInfo> &OptionHelp) { |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 480 | OS << Title << ":\n"; |
| 481 | |
| 482 | // Find the maximum option length. |
| 483 | unsigned OptionFieldWidth = 0; |
| 484 | for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) { |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 485 | // Limit the amount of padding we are willing to give up for alignment. |
George Rimar | cd31858 | 2017-07-18 10:59:30 +0000 | [diff] [blame] | 486 | unsigned Length = OptionHelp[i].Name.size(); |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 487 | if (Length <= 23) |
| 488 | OptionFieldWidth = std::max(OptionFieldWidth, Length); |
| 489 | } |
| 490 | |
| 491 | const unsigned InitialPad = 2; |
| 492 | for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) { |
George Rimar | cd31858 | 2017-07-18 10:59:30 +0000 | [diff] [blame] | 493 | const std::string &Option = OptionHelp[i].Name; |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 494 | int Pad = OptionFieldWidth - int(Option.size()); |
| 495 | OS.indent(InitialPad) << Option; |
| 496 | |
| 497 | // Break on long option names. |
| 498 | if (Pad < 0) { |
| 499 | OS << "\n"; |
| 500 | Pad = OptionFieldWidth + InitialPad; |
| 501 | } |
George Rimar | cd31858 | 2017-07-18 10:59:30 +0000 | [diff] [blame] | 502 | OS.indent(Pad + 1) << OptionHelp[i].HelpText << '\n'; |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | |
| 506 | static const char *getOptionHelpGroup(const OptTable &Opts, OptSpecifier Id) { |
| 507 | unsigned GroupID = Opts.getOptionGroupID(Id); |
| 508 | |
| 509 | // If not in a group, return the default help group. |
| 510 | if (!GroupID) |
| 511 | return "OPTIONS"; |
| 512 | |
| 513 | // Abuse the help text of the option groups to store the "help group" |
| 514 | // name. |
| 515 | // |
| 516 | // FIXME: Split out option groups. |
| 517 | if (const char *GroupHelp = Opts.getOptionHelpText(GroupID)) |
| 518 | return GroupHelp; |
| 519 | |
| 520 | // Otherwise keep looking. |
| 521 | return getOptionHelpGroup(Opts, GroupID); |
| 522 | } |
| 523 | |
Fangrui Song | 42f63b6 | 2018-10-10 00:15:31 +0000 | [diff] [blame] | 524 | void OptTable::PrintHelp(raw_ostream &OS, const char *Usage, const char *Title, |
George Rimar | 2e3a5b7 | 2017-07-26 09:09:56 +0000 | [diff] [blame] | 525 | bool ShowHidden, bool ShowAllAliases) const { |
Fangrui Song | 42f63b6 | 2018-10-10 00:15:31 +0000 | [diff] [blame] | 526 | PrintHelp(OS, Usage, Title, /*Include*/ 0, /*Exclude*/ |
George Rimar | 2e3a5b7 | 2017-07-26 09:09:56 +0000 | [diff] [blame] | 527 | (ShowHidden ? 0 : HelpHidden), ShowAllAliases); |
Reid Kleckner | 1ee21dc | 2013-06-13 18:12:12 +0000 | [diff] [blame] | 528 | } |
| 529 | |
Fangrui Song | 42f63b6 | 2018-10-10 00:15:31 +0000 | [diff] [blame] | 530 | void OptTable::PrintHelp(raw_ostream &OS, const char *Usage, const char *Title, |
George Rimar | 2e3a5b7 | 2017-07-26 09:09:56 +0000 | [diff] [blame] | 531 | unsigned FlagsToInclude, unsigned FlagsToExclude, |
| 532 | bool ShowAllAliases) const { |
Fangrui Song | 42f63b6 | 2018-10-10 00:15:31 +0000 | [diff] [blame] | 533 | OS << "OVERVIEW: " << Title << "\n\n"; |
| 534 | OS << "USAGE: " << Usage << "\n\n"; |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 535 | |
| 536 | // Render help text into a map of group-name to a list of (option, help) |
| 537 | // pairs. |
Jan Korous | 9b2441e | 2018-03-12 18:31:07 +0000 | [diff] [blame] | 538 | std::map<std::string, std::vector<OptionInfo>> GroupedOptionHelp; |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 539 | |
Jan Korous | 584d88c | 2018-03-12 18:30:47 +0000 | [diff] [blame] | 540 | for (unsigned Id = 1, e = getNumOptions() + 1; Id != e; ++Id) { |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 541 | // FIXME: Split out option groups. |
| 542 | if (getOptionKind(Id) == Option::GroupClass) |
| 543 | continue; |
| 544 | |
Reid Kleckner | 1ee21dc | 2013-06-13 18:12:12 +0000 | [diff] [blame] | 545 | unsigned Flags = getInfo(Id).Flags; |
| 546 | if (FlagsToInclude && !(Flags & FlagsToInclude)) |
| 547 | continue; |
| 548 | if (Flags & FlagsToExclude) |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 549 | continue; |
| 550 | |
George Rimar | 2e3a5b7 | 2017-07-26 09:09:56 +0000 | [diff] [blame] | 551 | // If an alias doesn't have a help text, show a help text for the aliased |
| 552 | // option instead. |
| 553 | const char *HelpText = getOptionHelpText(Id); |
| 554 | if (!HelpText && ShowAllAliases) { |
| 555 | const Option Alias = getOption(Id).getAlias(); |
| 556 | if (Alias.isValid()) |
| 557 | HelpText = getOptionHelpText(Alias.getID()); |
| 558 | } |
| 559 | |
| 560 | if (HelpText) { |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 561 | const char *HelpGroup = getOptionHelpGroup(*this, Id); |
| 562 | const std::string &OptName = getOptionHelpName(*this, Id); |
George Rimar | 2e3a5b7 | 2017-07-26 09:09:56 +0000 | [diff] [blame] | 563 | GroupedOptionHelp[HelpGroup].push_back({OptName, HelpText}); |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 564 | } |
| 565 | } |
| 566 | |
Jan Korous | 9b2441e | 2018-03-12 18:31:07 +0000 | [diff] [blame] | 567 | for (auto& OptionGroup : GroupedOptionHelp) { |
| 568 | if (OptionGroup.first != GroupedOptionHelp.begin()->first) |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 569 | OS << "\n"; |
Jan Korous | 9b2441e | 2018-03-12 18:31:07 +0000 | [diff] [blame] | 570 | PrintHelpOptionList(OS, OptionGroup.first, OptionGroup.second); |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | OS.flush(); |
| 574 | } |