Michael J. Spencer | 41ee041 | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 1 | //===--- OptTable.cpp - Option Table Implementation -----------------------===// |
| 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 | |
| 10 | #include "llvm/Option/OptTable.h" |
David Majnemer | 0d955d0 | 2016-08-11 22:21:41 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/STLExtras.h" |
Michael J. Spencer | 41ee041 | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 12 | #include "llvm/Option/Arg.h" |
| 13 | #include "llvm/Option/ArgList.h" |
| 14 | #include "llvm/Option/Option.h" |
Michael J. Spencer | 41ee041 | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 15 | #include "llvm/Support/ErrorHandling.h" |
Chandler Carruth | be81023 | 2013-01-02 10:22:59 +0000 | [diff] [blame] | 16 | #include "llvm/Support/raw_ostream.h" |
Michael J. Spencer | 41ee041 | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 17 | #include <algorithm> |
Rui Ueyama | 8fb5a91 | 2013-08-28 20:04:31 +0000 | [diff] [blame] | 18 | #include <cctype> |
Michael J. Spencer | 41ee041 | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 19 | #include <map> |
| 20 | |
| 21 | using namespace llvm; |
| 22 | using namespace llvm::opt; |
| 23 | |
Rui Ueyama | 8fb5a91 | 2013-08-28 20:04:31 +0000 | [diff] [blame] | 24 | namespace llvm { |
| 25 | namespace opt { |
Rui Ueyama | 7159bd9 | 2013-08-27 23:47:01 +0000 | [diff] [blame] | 26 | |
Rui Ueyama | 8fb5a91 | 2013-08-28 20:04:31 +0000 | [diff] [blame] | 27 | // Ordering on Info. The ordering is *almost* case-insensitive lexicographic, |
| 28 | // with an exceptions. '\0' comes at the end of the alphabet instead of the |
| 29 | // beginning (thus options precede any other options which prefix them). |
| 30 | static int StrCmpOptionNameIgnoreCase(const char *A, const char *B) { |
| 31 | const char *X = A, *Y = B; |
| 32 | char a = tolower(*A), b = tolower(*B); |
Rui Ueyama | c3779ff | 2013-08-28 00:02:06 +0000 | [diff] [blame] | 33 | while (a == b) { |
| 34 | if (a == '\0') |
| 35 | return 0; |
| 36 | |
Rui Ueyama | 8fb5a91 | 2013-08-28 20:04:31 +0000 | [diff] [blame] | 37 | a = tolower(*++X); |
| 38 | b = tolower(*++Y); |
Rui Ueyama | c3779ff | 2013-08-28 00:02:06 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | if (a == '\0') // A is a prefix of B. |
| 42 | return 1; |
| 43 | if (b == '\0') // B is a prefix of A. |
| 44 | return -1; |
| 45 | |
| 46 | // Otherwise lexicographic. |
| 47 | return (a < b) ? -1 : 1; |
Rui Ueyama | 7159bd9 | 2013-08-27 23:47:01 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Eli Friedman | 3e7dca6 | 2013-09-10 23:22:56 +0000 | [diff] [blame] | 50 | #ifndef NDEBUG |
| 51 | static int StrCmpOptionName(const char *A, const char *B) { |
| 52 | if (int N = StrCmpOptionNameIgnoreCase(A, B)) |
| 53 | return N; |
| 54 | return strcmp(A, B); |
| 55 | } |
| 56 | |
| 57 | static inline bool operator<(const OptTable::Info &A, const OptTable::Info &B) { |
| 58 | if (&A == &B) |
| 59 | return false; |
| 60 | |
| 61 | if (int N = StrCmpOptionName(A.Name, B.Name)) |
| 62 | return N < 0; |
| 63 | |
| 64 | for (const char * const *APre = A.Prefixes, |
| 65 | * const *BPre = B.Prefixes; |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 66 | *APre != nullptr && *BPre != nullptr; ++APre, ++BPre){ |
Eli Friedman | 3e7dca6 | 2013-09-10 23:22:56 +0000 | [diff] [blame] | 67 | if (int N = StrCmpOptionName(*APre, *BPre)) |
| 68 | return N < 0; |
| 69 | } |
| 70 | |
| 71 | // Names are the same, check that classes are in order; exactly one |
| 72 | // should be joined, and it should succeed the other. |
| 73 | assert(((A.Kind == Option::JoinedClass) ^ (B.Kind == Option::JoinedClass)) && |
| 74 | "Unexpected classes for options with same name."); |
| 75 | return B.Kind == Option::JoinedClass; |
| 76 | } |
| 77 | #endif |
| 78 | |
Michael J. Spencer | 41ee041 | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 79 | // Support lower_bound between info and an option name. |
| 80 | static inline bool operator<(const OptTable::Info &I, const char *Name) { |
Rui Ueyama | 8fb5a91 | 2013-08-28 20:04:31 +0000 | [diff] [blame] | 81 | return StrCmpOptionNameIgnoreCase(I.Name, Name) < 0; |
Michael J. Spencer | 41ee041 | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 82 | } |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 83 | } |
| 84 | } |
Michael J. Spencer | 41ee041 | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 85 | |
| 86 | OptSpecifier::OptSpecifier(const Option *Opt) : ID(Opt->getID()) {} |
| 87 | |
Craig Topper | 8ea2390 | 2015-10-21 16:30:42 +0000 | [diff] [blame] | 88 | OptTable::OptTable(ArrayRef<Info> OptionInfos, bool IgnoreCase) |
| 89 | : OptionInfos(OptionInfos), IgnoreCase(IgnoreCase), TheInputOptionID(0), |
| 90 | TheUnknownOptionID(0), FirstSearchableIndex(0) { |
Michael J. Spencer | 41ee041 | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 91 | // Explicitly zero initialize the error to work around a bug in array |
| 92 | // value-initialization on MinGW with gcc 4.3.5. |
| 93 | |
| 94 | // Find start of normal options. |
| 95 | for (unsigned i = 0, e = getNumOptions(); i != e; ++i) { |
| 96 | unsigned Kind = getInfo(i + 1).Kind; |
| 97 | if (Kind == Option::InputClass) { |
| 98 | assert(!TheInputOptionID && "Cannot have multiple input options!"); |
| 99 | TheInputOptionID = getInfo(i + 1).ID; |
| 100 | } else if (Kind == Option::UnknownClass) { |
| 101 | assert(!TheUnknownOptionID && "Cannot have multiple unknown options!"); |
| 102 | TheUnknownOptionID = getInfo(i + 1).ID; |
| 103 | } else if (Kind != Option::GroupClass) { |
| 104 | FirstSearchableIndex = i; |
| 105 | break; |
| 106 | } |
| 107 | } |
| 108 | assert(FirstSearchableIndex != 0 && "No searchable options?"); |
| 109 | |
| 110 | #ifndef NDEBUG |
| 111 | // Check that everything after the first searchable option is a |
| 112 | // regular option class. |
| 113 | for (unsigned i = FirstSearchableIndex, e = getNumOptions(); i != e; ++i) { |
| 114 | Option::OptionClass Kind = (Option::OptionClass) getInfo(i + 1).Kind; |
| 115 | assert((Kind != Option::InputClass && Kind != Option::UnknownClass && |
| 116 | Kind != Option::GroupClass) && |
| 117 | "Special options should be defined first!"); |
| 118 | } |
| 119 | |
| 120 | // Check that options are in order. |
| 121 | for (unsigned i = FirstSearchableIndex + 1, e = getNumOptions(); i != e; ++i){ |
| 122 | if (!(getInfo(i) < getInfo(i + 1))) { |
| 123 | getOption(i).dump(); |
| 124 | getOption(i + 1).dump(); |
| 125 | llvm_unreachable("Options are not in order!"); |
| 126 | } |
| 127 | } |
| 128 | #endif |
| 129 | |
| 130 | // Build prefixes. |
| 131 | for (unsigned i = FirstSearchableIndex + 1, e = getNumOptions() + 1; |
| 132 | i != e; ++i) { |
| 133 | if (const char *const *P = getInfo(i).Prefixes) { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 134 | for (; *P != nullptr; ++P) { |
Michael J. Spencer | 41ee041 | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 135 | PrefixesUnion.insert(*P); |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | // Build prefix chars. |
| 141 | for (llvm::StringSet<>::const_iterator I = PrefixesUnion.begin(), |
| 142 | E = PrefixesUnion.end(); I != E; ++I) { |
| 143 | StringRef Prefix = I->getKey(); |
| 144 | for (StringRef::const_iterator C = Prefix.begin(), CE = Prefix.end(); |
| 145 | C != CE; ++C) |
David Majnemer | 0d955d0 | 2016-08-11 22:21:41 +0000 | [diff] [blame] | 146 | if (!is_contained(PrefixChars, *C)) |
Michael J. Spencer | 41ee041 | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 147 | PrefixChars.push_back(*C); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | OptTable::~OptTable() { |
| 152 | } |
| 153 | |
| 154 | const Option OptTable::getOption(OptSpecifier Opt) const { |
| 155 | unsigned id = Opt.getID(); |
| 156 | if (id == 0) |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 157 | return Option(nullptr, nullptr); |
Michael J. Spencer | 41ee041 | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 158 | assert((unsigned) (id - 1) < getNumOptions() && "Invalid ID."); |
| 159 | return Option(&getInfo(id), this); |
| 160 | } |
| 161 | |
Michael J. Spencer | 41ee041 | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 162 | static bool isInput(const llvm::StringSet<> &Prefixes, StringRef Arg) { |
| 163 | if (Arg == "-") |
| 164 | return true; |
| 165 | for (llvm::StringSet<>::const_iterator I = Prefixes.begin(), |
| 166 | E = Prefixes.end(); I != E; ++I) |
| 167 | if (Arg.startswith(I->getKey())) |
| 168 | return false; |
| 169 | return true; |
| 170 | } |
| 171 | |
| 172 | /// \returns Matched size. 0 means no match. |
Rui Ueyama | 8fb5a91 | 2013-08-28 20:04:31 +0000 | [diff] [blame] | 173 | static unsigned matchOption(const OptTable::Info *I, StringRef Str, |
| 174 | bool IgnoreCase) { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 175 | for (const char * const *Pre = I->Prefixes; *Pre != nullptr; ++Pre) { |
Michael J. Spencer | 41ee041 | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 176 | StringRef Prefix(*Pre); |
Rui Ueyama | 8fb5a91 | 2013-08-28 20:04:31 +0000 | [diff] [blame] | 177 | if (Str.startswith(Prefix)) { |
| 178 | StringRef Rest = Str.substr(Prefix.size()); |
| 179 | bool Matched = IgnoreCase |
Jakub Staszak | cfcfee0 | 2013-11-04 19:22:50 +0000 | [diff] [blame] | 180 | ? Rest.startswith_lower(I->Name) |
Rui Ueyama | 8fb5a91 | 2013-08-28 20:04:31 +0000 | [diff] [blame] | 181 | : Rest.startswith(I->Name); |
| 182 | if (Matched) |
| 183 | return Prefix.size() + StringRef(I->Name).size(); |
| 184 | } |
Michael J. Spencer | 41ee041 | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 185 | } |
| 186 | return 0; |
| 187 | } |
| 188 | |
Reid Kleckner | eadb765 | 2013-07-19 18:04:57 +0000 | [diff] [blame] | 189 | Arg *OptTable::ParseOneArg(const ArgList &Args, unsigned &Index, |
| 190 | unsigned FlagsToInclude, |
| 191 | unsigned FlagsToExclude) const { |
Michael J. Spencer | 41ee041 | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 192 | unsigned Prev = Index; |
| 193 | const char *Str = Args.getArgString(Index); |
| 194 | |
| 195 | // Anything that doesn't start with PrefixesUnion is an input, as is '-' |
| 196 | // itself. |
| 197 | if (isInput(PrefixesUnion, Str)) |
| 198 | return new Arg(getOption(TheInputOptionID), Str, Index++, Str); |
| 199 | |
Craig Topper | 8ea2390 | 2015-10-21 16:30:42 +0000 | [diff] [blame] | 200 | const Info *Start = OptionInfos.begin() + FirstSearchableIndex; |
| 201 | const Info *End = OptionInfos.end(); |
Michael J. Spencer | 41ee041 | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 202 | StringRef Name = StringRef(Str).ltrim(PrefixChars); |
| 203 | |
| 204 | // Search for the first next option which could be a prefix. |
| 205 | Start = std::lower_bound(Start, End, Name.data()); |
| 206 | |
| 207 | // Options are stored in sorted order, with '\0' at the end of the |
| 208 | // alphabet. Since the only options which can accept a string must |
| 209 | // prefix it, we iteratively search for the next option which could |
| 210 | // be a prefix. |
| 211 | // |
| 212 | // FIXME: This is searching much more than necessary, but I am |
| 213 | // blanking on the simplest way to make it fast. We can solve this |
| 214 | // problem when we move to TableGen. |
| 215 | for (; Start != End; ++Start) { |
| 216 | unsigned ArgSize = 0; |
| 217 | // Scan for first option which is a proper prefix. |
| 218 | for (; Start != End; ++Start) |
Rui Ueyama | 8fb5a91 | 2013-08-28 20:04:31 +0000 | [diff] [blame] | 219 | if ((ArgSize = matchOption(Start, Str, IgnoreCase))) |
Michael J. Spencer | 41ee041 | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 220 | break; |
| 221 | if (Start == End) |
| 222 | break; |
| 223 | |
Reid Kleckner | eadb765 | 2013-07-19 18:04:57 +0000 | [diff] [blame] | 224 | Option Opt(Start, this); |
| 225 | |
| 226 | if (FlagsToInclude && !Opt.hasFlag(FlagsToInclude)) |
| 227 | continue; |
| 228 | if (Opt.hasFlag(FlagsToExclude)) |
| 229 | continue; |
| 230 | |
Michael J. Spencer | 41ee041 | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 231 | // See if this option matches. |
Reid Kleckner | eadb765 | 2013-07-19 18:04:57 +0000 | [diff] [blame] | 232 | if (Arg *A = Opt.accept(Args, Index, ArgSize)) |
Michael J. Spencer | 41ee041 | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 233 | return A; |
| 234 | |
| 235 | // Otherwise, see if this argument was missing values. |
| 236 | if (Prev != Index) |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 237 | return nullptr; |
Michael J. Spencer | 41ee041 | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Reid Kleckner | eadb765 | 2013-07-19 18:04:57 +0000 | [diff] [blame] | 240 | // If we failed to find an option and this arg started with /, then it's |
| 241 | // probably an input path. |
| 242 | if (Str[0] == '/') |
| 243 | return new Arg(getOption(TheInputOptionID), Str, Index++, Str); |
| 244 | |
Michael J. Spencer | 41ee041 | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 245 | return new Arg(getOption(TheUnknownOptionID), Str, Index++, Str); |
| 246 | } |
| 247 | |
David Blaikie | db3d31d | 2015-06-22 22:06:37 +0000 | [diff] [blame] | 248 | InputArgList OptTable::ParseArgs(ArrayRef<const char *> ArgArr, |
| 249 | unsigned &MissingArgIndex, |
| 250 | unsigned &MissingArgCount, |
| 251 | unsigned FlagsToInclude, |
| 252 | unsigned FlagsToExclude) const { |
| 253 | InputArgList Args(ArgArr.begin(), ArgArr.end()); |
Michael J. Spencer | 41ee041 | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 254 | |
| 255 | // FIXME: Handle '@' args (or at least error on them). |
| 256 | |
| 257 | MissingArgIndex = MissingArgCount = 0; |
David Blaikie | 259f61d | 2015-06-21 06:31:53 +0000 | [diff] [blame] | 258 | unsigned Index = 0, End = ArgArr.size(); |
Michael J. Spencer | 41ee041 | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 259 | while (Index < End) { |
Reid Kleckner | e3f146d | 2014-08-22 19:29:17 +0000 | [diff] [blame] | 260 | // Ingore nullptrs, they are response file's EOL markers |
David Blaikie | db3d31d | 2015-06-22 22:06:37 +0000 | [diff] [blame] | 261 | if (Args.getArgString(Index) == nullptr) { |
Reid Kleckner | e3f146d | 2014-08-22 19:29:17 +0000 | [diff] [blame] | 262 | ++Index; |
| 263 | continue; |
| 264 | } |
Michael J. Spencer | 41ee041 | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 265 | // Ignore empty arguments (other things may still take them as arguments). |
David Blaikie | db3d31d | 2015-06-22 22:06:37 +0000 | [diff] [blame] | 266 | StringRef Str = Args.getArgString(Index); |
Hans Wennborg | b8f3420 | 2013-08-02 21:20:27 +0000 | [diff] [blame] | 267 | if (Str == "") { |
Michael J. Spencer | 41ee041 | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 268 | ++Index; |
| 269 | continue; |
| 270 | } |
| 271 | |
| 272 | unsigned Prev = Index; |
David Blaikie | db3d31d | 2015-06-22 22:06:37 +0000 | [diff] [blame] | 273 | Arg *A = ParseOneArg(Args, Index, FlagsToInclude, FlagsToExclude); |
Michael J. Spencer | 41ee041 | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 274 | assert(Index > Prev && "Parser failed to consume argument."); |
| 275 | |
| 276 | // Check for missing argument error. |
| 277 | if (!A) { |
| 278 | assert(Index >= End && "Unexpected parser error."); |
| 279 | assert(Index - Prev - 1 && "No missing arguments!"); |
| 280 | MissingArgIndex = Prev; |
| 281 | MissingArgCount = Index - Prev - 1; |
| 282 | break; |
| 283 | } |
| 284 | |
David Blaikie | db3d31d | 2015-06-22 22:06:37 +0000 | [diff] [blame] | 285 | Args.append(A); |
Michael J. Spencer | 41ee041 | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 286 | } |
| 287 | |
Logan Chien | 9d5891f | 2015-06-22 23:16:02 +0000 | [diff] [blame] | 288 | return Args; |
Michael J. Spencer | 41ee041 | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | static std::string getOptionHelpName(const OptTable &Opts, OptSpecifier Id) { |
| 292 | const Option O = Opts.getOption(Id); |
| 293 | std::string Name = O.getPrefixedName(); |
| 294 | |
| 295 | // Add metavar, if used. |
| 296 | switch (O.getKind()) { |
| 297 | case Option::GroupClass: case Option::InputClass: case Option::UnknownClass: |
| 298 | llvm_unreachable("Invalid option with help text."); |
| 299 | |
| 300 | case Option::MultiArgClass: |
Nick Kledzik | bcd6e2a | 2014-08-15 21:35:07 +0000 | [diff] [blame] | 301 | if (const char *MetaVarName = Opts.getOptionMetaVar(Id)) { |
| 302 | // For MultiArgs, metavar is full list of all argument names. |
| 303 | Name += ' '; |
| 304 | Name += MetaVarName; |
| 305 | } |
| 306 | else { |
| 307 | // For MultiArgs<N>, if metavar not supplied, print <value> N times. |
| 308 | for (unsigned i=0, e=O.getNumArgs(); i< e; ++i) { |
| 309 | Name += " <value>"; |
| 310 | } |
| 311 | } |
| 312 | break; |
Michael J. Spencer | 41ee041 | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 313 | |
| 314 | case Option::FlagClass: |
| 315 | break; |
| 316 | |
| 317 | case Option::SeparateClass: case Option::JoinedOrSeparateClass: |
Hans Wennborg | 40cfde3 | 2016-04-15 00:23:30 +0000 | [diff] [blame] | 318 | case Option::RemainingArgsClass: case Option::RemainingArgsJoinedClass: |
Michael J. Spencer | 41ee041 | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 319 | Name += ' '; |
Justin Bogner | b03fd12 | 2016-08-17 05:10:15 +0000 | [diff] [blame] | 320 | LLVM_FALLTHROUGH; |
Michael J. Spencer | 41ee041 | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 321 | case Option::JoinedClass: case Option::CommaJoinedClass: |
| 322 | case Option::JoinedAndSeparateClass: |
| 323 | if (const char *MetaVarName = Opts.getOptionMetaVar(Id)) |
| 324 | Name += MetaVarName; |
| 325 | else |
| 326 | Name += "<value>"; |
| 327 | break; |
| 328 | } |
| 329 | |
| 330 | return Name; |
| 331 | } |
| 332 | |
| 333 | static void PrintHelpOptionList(raw_ostream &OS, StringRef Title, |
| 334 | std::vector<std::pair<std::string, |
| 335 | const char*> > &OptionHelp) { |
| 336 | OS << Title << ":\n"; |
| 337 | |
| 338 | // Find the maximum option length. |
| 339 | unsigned OptionFieldWidth = 0; |
| 340 | for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) { |
| 341 | // Skip titles. |
| 342 | if (!OptionHelp[i].second) |
| 343 | continue; |
| 344 | |
| 345 | // Limit the amount of padding we are willing to give up for alignment. |
| 346 | unsigned Length = OptionHelp[i].first.size(); |
| 347 | if (Length <= 23) |
| 348 | OptionFieldWidth = std::max(OptionFieldWidth, Length); |
| 349 | } |
| 350 | |
| 351 | const unsigned InitialPad = 2; |
| 352 | for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) { |
| 353 | const std::string &Option = OptionHelp[i].first; |
| 354 | int Pad = OptionFieldWidth - int(Option.size()); |
| 355 | OS.indent(InitialPad) << Option; |
| 356 | |
| 357 | // Break on long option names. |
| 358 | if (Pad < 0) { |
| 359 | OS << "\n"; |
| 360 | Pad = OptionFieldWidth + InitialPad; |
| 361 | } |
| 362 | OS.indent(Pad + 1) << OptionHelp[i].second << '\n'; |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | static const char *getOptionHelpGroup(const OptTable &Opts, OptSpecifier Id) { |
| 367 | unsigned GroupID = Opts.getOptionGroupID(Id); |
| 368 | |
| 369 | // If not in a group, return the default help group. |
| 370 | if (!GroupID) |
| 371 | return "OPTIONS"; |
| 372 | |
| 373 | // Abuse the help text of the option groups to store the "help group" |
| 374 | // name. |
| 375 | // |
| 376 | // FIXME: Split out option groups. |
| 377 | if (const char *GroupHelp = Opts.getOptionHelpText(GroupID)) |
| 378 | return GroupHelp; |
| 379 | |
| 380 | // Otherwise keep looking. |
| 381 | return getOptionHelpGroup(Opts, GroupID); |
| 382 | } |
| 383 | |
Reid Kleckner | 12e0332 | 2013-06-13 18:12:12 +0000 | [diff] [blame] | 384 | void OptTable::PrintHelp(raw_ostream &OS, const char *Name, const char *Title, |
| 385 | bool ShowHidden) const { |
| 386 | PrintHelp(OS, Name, Title, /*Include*/ 0, /*Exclude*/ |
| 387 | (ShowHidden ? 0 : HelpHidden)); |
| 388 | } |
| 389 | |
| 390 | |
| 391 | void OptTable::PrintHelp(raw_ostream &OS, const char *Name, const char *Title, |
| 392 | unsigned FlagsToInclude, |
| 393 | unsigned FlagsToExclude) const { |
Michael J. Spencer | 41ee041 | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 394 | OS << "OVERVIEW: " << Title << "\n"; |
| 395 | OS << '\n'; |
| 396 | OS << "USAGE: " << Name << " [options] <inputs>\n"; |
| 397 | OS << '\n'; |
| 398 | |
| 399 | // Render help text into a map of group-name to a list of (option, help) |
| 400 | // pairs. |
| 401 | typedef std::map<std::string, |
| 402 | std::vector<std::pair<std::string, const char*> > > helpmap_ty; |
| 403 | helpmap_ty GroupedOptionHelp; |
| 404 | |
| 405 | for (unsigned i = 0, e = getNumOptions(); i != e; ++i) { |
| 406 | unsigned Id = i + 1; |
| 407 | |
| 408 | // FIXME: Split out option groups. |
| 409 | if (getOptionKind(Id) == Option::GroupClass) |
| 410 | continue; |
| 411 | |
Reid Kleckner | 12e0332 | 2013-06-13 18:12:12 +0000 | [diff] [blame] | 412 | unsigned Flags = getInfo(Id).Flags; |
| 413 | if (FlagsToInclude && !(Flags & FlagsToInclude)) |
| 414 | continue; |
| 415 | if (Flags & FlagsToExclude) |
Michael J. Spencer | 41ee041 | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 416 | continue; |
| 417 | |
| 418 | if (const char *Text = getOptionHelpText(Id)) { |
| 419 | const char *HelpGroup = getOptionHelpGroup(*this, Id); |
| 420 | const std::string &OptName = getOptionHelpName(*this, Id); |
| 421 | GroupedOptionHelp[HelpGroup].push_back(std::make_pair(OptName, Text)); |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | for (helpmap_ty::iterator it = GroupedOptionHelp .begin(), |
| 426 | ie = GroupedOptionHelp.end(); it != ie; ++it) { |
| 427 | if (it != GroupedOptionHelp .begin()) |
| 428 | OS << "\n"; |
| 429 | PrintHelpOptionList(OS, it->first, it->second); |
| 430 | } |
| 431 | |
| 432 | OS.flush(); |
| 433 | } |