Nick Lewycky | 3fdcc6f | 2010-12-31 17:31:54 +0000 | [diff] [blame] | 1 | //===--- OptTable.cpp - Option Table Implementation -----------------------===// |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +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 | |
Daniel Dunbar | 27e738d | 2009-11-19 00:15:11 +0000 | [diff] [blame] | 10 | #include "clang/Driver/OptTable.h" |
Daniel Dunbar | 70a0dbb | 2009-03-04 22:41:37 +0000 | [diff] [blame] | 11 | #include "clang/Driver/Arg.h" |
| 12 | #include "clang/Driver/ArgList.h" |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 13 | #include "clang/Driver/Option.h" |
Michael J. Spencer | e4151c5 | 2012-10-19 22:36:40 +0000 | [diff] [blame] | 14 | #include "clang/Driver/Options.h" |
Daniel Dunbar | 60e107f | 2009-12-03 07:01:38 +0000 | [diff] [blame] | 15 | #include "llvm/Support/raw_ostream.h" |
David Blaikie | 548f6c8 | 2011-09-23 05:57:42 +0000 | [diff] [blame] | 16 | #include "llvm/Support/ErrorHandling.h" |
Daniel Dunbar | 7547f74 | 2009-03-23 21:50:40 +0000 | [diff] [blame] | 17 | #include <algorithm> |
Daniel Dunbar | 2658f05 | 2009-12-04 21:08:40 +0000 | [diff] [blame] | 18 | #include <map> |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 19 | using namespace clang::driver; |
| 20 | using namespace clang::driver::options; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 21 | using namespace clang; |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 22 | |
Daniel Dunbar | b355692 | 2009-03-23 18:41:45 +0000 | [diff] [blame] | 23 | // Ordering on Info. The ordering is *almost* lexicographic, with two |
| 24 | // exceptions. First, '\0' comes at the end of the alphabet instead of |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 25 | // the beginning (thus options precede any other options which prefix |
Daniel Dunbar | b355692 | 2009-03-23 18:41:45 +0000 | [diff] [blame] | 26 | // them). Second, for options with the same name, the less permissive |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 27 | // version should come first; a Flag option should precede a Joined |
Daniel Dunbar | b355692 | 2009-03-23 18:41:45 +0000 | [diff] [blame] | 28 | // option, for example. |
| 29 | |
| 30 | static int StrCmpOptionName(const char *A, const char *B) { |
| 31 | char a = *A, b = *B; |
| 32 | while (a == b) { |
| 33 | if (a == '\0') |
| 34 | return 0; |
| 35 | |
| 36 | a = *++A; |
| 37 | b = *++B; |
| 38 | } |
| 39 | |
| 40 | if (a == '\0') // A is a prefix of B. |
| 41 | return 1; |
| 42 | if (b == '\0') // B is a prefix of A. |
| 43 | return -1; |
| 44 | |
| 45 | // Otherwise lexicographic. |
| 46 | return (a < b) ? -1 : 1; |
| 47 | } |
| 48 | |
Daniel Dunbar | 1ce9cf0 | 2009-11-18 21:42:57 +0000 | [diff] [blame] | 49 | namespace clang { |
| 50 | namespace driver { |
Daniel Dunbar | a79a2b5 | 2009-11-18 20:19:36 +0000 | [diff] [blame] | 51 | static inline bool operator<(const OptTable::Info &A, const OptTable::Info &B) { |
Daniel Dunbar | b355692 | 2009-03-23 18:41:45 +0000 | [diff] [blame] | 52 | if (&A == &B) |
| 53 | return false; |
| 54 | |
| 55 | if (int N = StrCmpOptionName(A.Name, B.Name)) |
| 56 | return N == -1; |
| 57 | |
Michael J. Spencer | c635710 | 2012-10-22 22:13:48 +0000 | [diff] [blame] | 58 | for (const char * const *APre = A.Prefixes, |
| 59 | * const *BPre = B.Prefixes; |
| 60 | *APre != 0 && *BPre != 0; ++APre, ++BPre) { |
| 61 | if (int N = StrCmpOptionName(*APre, *BPre)) |
| 62 | return N == -1; |
| 63 | } |
| 64 | |
Daniel Dunbar | b355692 | 2009-03-23 18:41:45 +0000 | [diff] [blame] | 65 | // Names are the same, check that classes are in order; exactly one |
| 66 | // should be joined, and it should succeed the other. |
Daniel Dunbar | cf51ece | 2009-03-25 03:06:26 +0000 | [diff] [blame] | 67 | assert(((A.Kind == Option::JoinedClass) ^ (B.Kind == Option::JoinedClass)) && |
Daniel Dunbar | b355692 | 2009-03-23 18:41:45 +0000 | [diff] [blame] | 68 | "Unexpected classes for options with same name."); |
| 69 | return B.Kind == Option::JoinedClass; |
| 70 | } |
| 71 | |
Daniel Dunbar | 1ce9cf0 | 2009-11-18 21:42:57 +0000 | [diff] [blame] | 72 | // Support lower_bound between info and an option name. |
| 73 | static inline bool operator<(const OptTable::Info &I, const char *Name) { |
| 74 | return StrCmpOptionName(I.Name, Name) == -1; |
| 75 | } |
| 76 | static inline bool operator<(const char *Name, const OptTable::Info &I) { |
| 77 | return StrCmpOptionName(Name, I.Name) == -1; |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
Daniel Dunbar | b355692 | 2009-03-23 18:41:45 +0000 | [diff] [blame] | 82 | // |
| 83 | |
Daniel Dunbar | 9e1f982 | 2009-11-19 04:14:53 +0000 | [diff] [blame] | 84 | OptSpecifier::OptSpecifier(const Option *Opt) : ID(Opt->getID()) {} |
| 85 | |
| 86 | // |
| 87 | |
Daniel Dunbar | a79a2b5 | 2009-11-18 20:19:36 +0000 | [diff] [blame] | 88 | OptTable::OptTable(const Info *_OptionInfos, unsigned _NumOptionInfos) |
Michael J. Spencer | e4151c5 | 2012-10-19 22:36:40 +0000 | [diff] [blame] | 89 | : OptionInfos(_OptionInfos), |
| 90 | NumOptionInfos(_NumOptionInfos), |
| 91 | TheInputOptionID(0), |
| 92 | TheUnknownOptionID(0), |
| 93 | FirstSearchableIndex(0) |
Daniel Dunbar | a79a2b5 | 2009-11-18 20:19:36 +0000 | [diff] [blame] | 94 | { |
Daniel Dunbar | 47393ba | 2009-07-13 21:50:47 +0000 | [diff] [blame] | 95 | // Explicitly zero initialize the error to work around a bug in array |
| 96 | // value-initialization on MinGW with gcc 4.3.5. |
Daniel Dunbar | 47393ba | 2009-07-13 21:50:47 +0000 | [diff] [blame] | 97 | |
Daniel Dunbar | b355692 | 2009-03-23 18:41:45 +0000 | [diff] [blame] | 98 | // Find start of normal options. |
Daniel Dunbar | a79a2b5 | 2009-11-18 20:19:36 +0000 | [diff] [blame] | 99 | for (unsigned i = 0, e = getNumOptions(); i != e; ++i) { |
| 100 | unsigned Kind = getInfo(i + 1).Kind; |
| 101 | if (Kind == Option::InputClass) { |
Michael J. Spencer | e4151c5 | 2012-10-19 22:36:40 +0000 | [diff] [blame] | 102 | assert(!TheInputOptionID && "Cannot have multiple input options!"); |
| 103 | TheInputOptionID = getInfo(i + 1).ID; |
Daniel Dunbar | a79a2b5 | 2009-11-18 20:19:36 +0000 | [diff] [blame] | 104 | } else if (Kind == Option::UnknownClass) { |
Michael J. Spencer | e4151c5 | 2012-10-19 22:36:40 +0000 | [diff] [blame] | 105 | assert(!TheUnknownOptionID && "Cannot have multiple unknown options!"); |
| 106 | TheUnknownOptionID = getInfo(i + 1).ID; |
Daniel Dunbar | a79a2b5 | 2009-11-18 20:19:36 +0000 | [diff] [blame] | 107 | } else if (Kind != Option::GroupClass) { |
| 108 | FirstSearchableIndex = i; |
Daniel Dunbar | b355692 | 2009-03-23 18:41:45 +0000 | [diff] [blame] | 109 | break; |
| 110 | } |
| 111 | } |
Daniel Dunbar | a79a2b5 | 2009-11-18 20:19:36 +0000 | [diff] [blame] | 112 | assert(FirstSearchableIndex != 0 && "No searchable options?"); |
Daniel Dunbar | b355692 | 2009-03-23 18:41:45 +0000 | [diff] [blame] | 113 | |
| 114 | #ifndef NDEBUG |
| 115 | // Check that everything after the first searchable option is a |
| 116 | // regular option class. |
Daniel Dunbar | a79a2b5 | 2009-11-18 20:19:36 +0000 | [diff] [blame] | 117 | for (unsigned i = FirstSearchableIndex, e = getNumOptions(); i != e; ++i) { |
| 118 | Option::OptionClass Kind = (Option::OptionClass) getInfo(i + 1).Kind; |
Daniel Dunbar | b355692 | 2009-03-23 18:41:45 +0000 | [diff] [blame] | 119 | assert((Kind != Option::InputClass && Kind != Option::UnknownClass && |
| 120 | Kind != Option::GroupClass) && |
| 121 | "Special options should be defined first!"); |
| 122 | } |
| 123 | |
| 124 | // Check that options are in order. |
Daniel Dunbar | a79a2b5 | 2009-11-18 20:19:36 +0000 | [diff] [blame] | 125 | for (unsigned i = FirstSearchableIndex+1, e = getNumOptions(); i != e; ++i) { |
| 126 | if (!(getInfo(i) < getInfo(i + 1))) { |
Michael J. Spencer | e4151c5 | 2012-10-19 22:36:40 +0000 | [diff] [blame] | 127 | getOption(i).dump(); |
| 128 | getOption(i + 1).dump(); |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 129 | llvm_unreachable("Options are not in order!"); |
Daniel Dunbar | b355692 | 2009-03-23 18:41:45 +0000 | [diff] [blame] | 130 | } |
| 131 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 132 | #endif |
Michael J. Spencer | c635710 | 2012-10-22 22:13:48 +0000 | [diff] [blame] | 133 | |
| 134 | // Build prefixes. |
| 135 | for (unsigned i = FirstSearchableIndex+1, e = getNumOptions(); i != e; ++i) { |
| 136 | if (const char *const *P = getInfo(i).Prefixes) { |
| 137 | for (; *P != 0; ++P) { |
| 138 | PrefixesUnion.insert(*P); |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | // Build prefix chars. |
| 144 | for (llvm::StringSet<>::const_iterator I = PrefixesUnion.begin(), |
| 145 | E = PrefixesUnion.end(); I != E; ++I) { |
| 146 | StringRef Prefix = I->getKey(); |
| 147 | for (StringRef::const_iterator C = Prefix.begin(), CE = Prefix.end(); |
| 148 | C != CE; ++C) |
| 149 | if (std::find(PrefixChars.begin(), PrefixChars.end(), *C) |
| 150 | == PrefixChars.end()) |
| 151 | PrefixChars.push_back(*C); |
| 152 | } |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 155 | OptTable::~OptTable() { |
Michael J. Spencer | e4151c5 | 2012-10-19 22:36:40 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | const Option OptTable::getOption(OptSpecifier Opt) const { |
| 159 | unsigned id = Opt.getID(); |
| 160 | if (id == 0) |
| 161 | return Option(0, 0); |
| 162 | assert((unsigned) (id - 1) < getNumOptions() && "Invalid ID."); |
| 163 | return Option(&getInfo(id), this); |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Michael J. Spencer | c635710 | 2012-10-22 22:13:48 +0000 | [diff] [blame] | 166 | static bool isInput(const llvm::StringSet<> &Prefixes, StringRef Arg) { |
| 167 | if (Arg == "-") |
| 168 | return true; |
| 169 | for (llvm::StringSet<>::const_iterator I = Prefixes.begin(), |
| 170 | E = Prefixes.end(); I != E; ++I) |
| 171 | if (Arg.startswith(I->getKey())) |
| 172 | return false; |
| 173 | return true; |
| 174 | } |
| 175 | |
| 176 | /// \returns Matched size. 0 means no match. |
| 177 | static unsigned matchOption(const OptTable::Info *I, StringRef Str) { |
| 178 | for (const char * const *Pre = I->Prefixes; *Pre != 0; ++Pre) { |
| 179 | StringRef Prefix(*Pre); |
| 180 | if (Str.startswith(Prefix) && Str.substr(Prefix.size()).startswith(I->Name)) |
| 181 | return Prefix.size() + StringRef(I->Name).size(); |
| 182 | } |
| 183 | return 0; |
| 184 | } |
| 185 | |
Daniel Dunbar | 785e796 | 2010-06-11 22:00:17 +0000 | [diff] [blame] | 186 | Arg *OptTable::ParseOneArg(const ArgList &Args, unsigned &Index) const { |
Daniel Dunbar | b0c4df5 | 2009-03-22 23:26:43 +0000 | [diff] [blame] | 187 | unsigned Prev = Index; |
Daniel Dunbar | 70a0dbb | 2009-03-04 22:41:37 +0000 | [diff] [blame] | 188 | const char *Str = Args.getArgString(Index); |
| 189 | |
Michael J. Spencer | c635710 | 2012-10-22 22:13:48 +0000 | [diff] [blame] | 190 | // Anything that doesn't start with PrefixesUnion is an input, as is '-' |
| 191 | // itself. |
| 192 | if (isInput(PrefixesUnion, Str)) |
| 193 | return new Arg(getOption(TheInputOptionID), Str, Index++, Str); |
Daniel Dunbar | 70a0dbb | 2009-03-04 22:41:37 +0000 | [diff] [blame] | 194 | |
Daniel Dunbar | a79a2b5 | 2009-11-18 20:19:36 +0000 | [diff] [blame] | 195 | const Info *Start = OptionInfos + FirstSearchableIndex; |
Daniel Dunbar | a0289fd | 2009-11-19 04:25:06 +0000 | [diff] [blame] | 196 | const Info *End = OptionInfos + getNumOptions(); |
Michael J. Spencer | c635710 | 2012-10-22 22:13:48 +0000 | [diff] [blame] | 197 | StringRef Name = StringRef(Str).ltrim(PrefixChars); |
Daniel Dunbar | b0c4df5 | 2009-03-22 23:26:43 +0000 | [diff] [blame] | 198 | |
Daniel Dunbar | 4ae24e7 | 2009-04-07 18:21:47 +0000 | [diff] [blame] | 199 | // Search for the first next option which could be a prefix. |
Michael J. Spencer | c635710 | 2012-10-22 22:13:48 +0000 | [diff] [blame] | 200 | Start = std::lower_bound(Start, End, Name.data()); |
Daniel Dunbar | 7547f74 | 2009-03-23 21:50:40 +0000 | [diff] [blame] | 201 | |
Daniel Dunbar | 4ae24e7 | 2009-04-07 18:21:47 +0000 | [diff] [blame] | 202 | // Options are stored in sorted order, with '\0' at the end of the |
| 203 | // alphabet. Since the only options which can accept a string must |
| 204 | // prefix it, we iteratively search for the next option which could |
| 205 | // be a prefix. |
| 206 | // |
| 207 | // FIXME: This is searching much more than necessary, but I am |
| 208 | // blanking on the simplest way to make it fast. We can solve this |
| 209 | // problem when we move to TableGen. |
Daniel Dunbar | 7547f74 | 2009-03-23 21:50:40 +0000 | [diff] [blame] | 210 | for (; Start != End; ++Start) { |
Michael J. Spencer | c635710 | 2012-10-22 22:13:48 +0000 | [diff] [blame] | 211 | unsigned ArgSize = 0; |
Daniel Dunbar | 4ae24e7 | 2009-04-07 18:21:47 +0000 | [diff] [blame] | 212 | // Scan for first option which is a proper prefix. |
| 213 | for (; Start != End; ++Start) |
Michael J. Spencer | c635710 | 2012-10-22 22:13:48 +0000 | [diff] [blame] | 214 | if ((ArgSize = matchOption(Start, Str))) |
Daniel Dunbar | 4ae24e7 | 2009-04-07 18:21:47 +0000 | [diff] [blame] | 215 | break; |
| 216 | if (Start == End) |
Daniel Dunbar | 7547f74 | 2009-03-23 21:50:40 +0000 | [diff] [blame] | 217 | break; |
| 218 | |
Daniel Dunbar | 4ae24e7 | 2009-04-07 18:21:47 +0000 | [diff] [blame] | 219 | // See if this option matches. |
Michael J. Spencer | c635710 | 2012-10-22 22:13:48 +0000 | [diff] [blame] | 220 | if (Arg *A = Option(Start, this).accept(Args, Index, ArgSize)) |
Daniel Dunbar | 7547f74 | 2009-03-23 21:50:40 +0000 | [diff] [blame] | 221 | return A; |
| 222 | |
| 223 | // Otherwise, see if this argument was missing values. |
| 224 | if (Prev != Index) |
| 225 | return 0; |
Daniel Dunbar | 70a0dbb | 2009-03-04 22:41:37 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Michael J. Spencer | c635710 | 2012-10-22 22:13:48 +0000 | [diff] [blame] | 228 | return new Arg(getOption(TheUnknownOptionID), Str, Index++, Str); |
Daniel Dunbar | 70a0dbb | 2009-03-04 22:41:37 +0000 | [diff] [blame] | 229 | } |
Daniel Dunbar | 847abaa | 2009-11-19 06:35:06 +0000 | [diff] [blame] | 230 | |
Axel Naumann | 9d520c5 | 2010-10-11 09:18:43 +0000 | [diff] [blame] | 231 | InputArgList *OptTable::ParseArgs(const char* const *ArgBegin, |
| 232 | const char* const *ArgEnd, |
Daniel Dunbar | 847abaa | 2009-11-19 06:35:06 +0000 | [diff] [blame] | 233 | unsigned &MissingArgIndex, |
| 234 | unsigned &MissingArgCount) const { |
| 235 | InputArgList *Args = new InputArgList(ArgBegin, ArgEnd); |
| 236 | |
| 237 | // FIXME: Handle '@' args (or at least error on them). |
| 238 | |
| 239 | MissingArgIndex = MissingArgCount = 0; |
| 240 | unsigned Index = 0, End = ArgEnd - ArgBegin; |
| 241 | while (Index < End) { |
| 242 | // Ignore empty arguments (other things may still take them as arguments). |
| 243 | if (Args->getArgString(Index)[0] == '\0') { |
| 244 | ++Index; |
| 245 | continue; |
| 246 | } |
| 247 | |
| 248 | unsigned Prev = Index; |
| 249 | Arg *A = ParseOneArg(*Args, Index); |
| 250 | assert(Index > Prev && "Parser failed to consume argument."); |
| 251 | |
| 252 | // Check for missing argument error. |
| 253 | if (!A) { |
| 254 | assert(Index >= End && "Unexpected parser error."); |
| 255 | assert(Index - Prev - 1 && "No missing arguments!"); |
| 256 | MissingArgIndex = Prev; |
| 257 | MissingArgCount = Index - Prev - 1; |
| 258 | break; |
| 259 | } |
| 260 | |
| 261 | Args->append(A); |
| 262 | } |
| 263 | |
| 264 | return Args; |
| 265 | } |
Daniel Dunbar | 60e107f | 2009-12-03 07:01:38 +0000 | [diff] [blame] | 266 | |
| 267 | static std::string getOptionHelpName(const OptTable &Opts, OptSpecifier Id) { |
Michael J. Spencer | c635710 | 2012-10-22 22:13:48 +0000 | [diff] [blame] | 268 | const Option O = Opts.getOption(Id); |
| 269 | std::string Name = O.getPrefixedName(); |
Daniel Dunbar | 60e107f | 2009-12-03 07:01:38 +0000 | [diff] [blame] | 270 | |
| 271 | // Add metavar, if used. |
Michael J. Spencer | c635710 | 2012-10-22 22:13:48 +0000 | [diff] [blame] | 272 | switch (O.getKind()) { |
Daniel Dunbar | 60e107f | 2009-12-03 07:01:38 +0000 | [diff] [blame] | 273 | case Option::GroupClass: case Option::InputClass: case Option::UnknownClass: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 274 | llvm_unreachable("Invalid option with help text."); |
Daniel Dunbar | 60e107f | 2009-12-03 07:01:38 +0000 | [diff] [blame] | 275 | |
Daniel Dunbar | 3177aae | 2010-06-16 16:59:23 +0000 | [diff] [blame] | 276 | case Option::MultiArgClass: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 277 | llvm_unreachable("Cannot print metavar for this kind of option."); |
Daniel Dunbar | 60e107f | 2009-12-03 07:01:38 +0000 | [diff] [blame] | 278 | |
| 279 | case Option::FlagClass: |
| 280 | break; |
| 281 | |
| 282 | case Option::SeparateClass: case Option::JoinedOrSeparateClass: |
| 283 | Name += ' '; |
| 284 | // FALLTHROUGH |
| 285 | case Option::JoinedClass: case Option::CommaJoinedClass: |
Daniel Dunbar | 3177aae | 2010-06-16 16:59:23 +0000 | [diff] [blame] | 286 | case Option::JoinedAndSeparateClass: |
Daniel Dunbar | 60e107f | 2009-12-03 07:01:38 +0000 | [diff] [blame] | 287 | if (const char *MetaVarName = Opts.getOptionMetaVar(Id)) |
| 288 | Name += MetaVarName; |
| 289 | else |
| 290 | Name += "<value>"; |
| 291 | break; |
| 292 | } |
| 293 | |
| 294 | return Name; |
| 295 | } |
| 296 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 297 | static void PrintHelpOptionList(raw_ostream &OS, StringRef Title, |
Daniel Dunbar | 2658f05 | 2009-12-04 21:08:40 +0000 | [diff] [blame] | 298 | std::vector<std::pair<std::string, |
| 299 | const char*> > &OptionHelp) { |
| 300 | OS << Title << ":\n"; |
Daniel Dunbar | 60e107f | 2009-12-03 07:01:38 +0000 | [diff] [blame] | 301 | |
| 302 | // Find the maximum option length. |
| 303 | unsigned OptionFieldWidth = 0; |
| 304 | for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) { |
| 305 | // Skip titles. |
| 306 | if (!OptionHelp[i].second) |
| 307 | continue; |
| 308 | |
| 309 | // Limit the amount of padding we are willing to give up for alignment. |
| 310 | unsigned Length = OptionHelp[i].first.size(); |
| 311 | if (Length <= 23) |
| 312 | OptionFieldWidth = std::max(OptionFieldWidth, Length); |
| 313 | } |
| 314 | |
| 315 | const unsigned InitialPad = 2; |
| 316 | for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) { |
| 317 | const std::string &Option = OptionHelp[i].first; |
| 318 | int Pad = OptionFieldWidth - int(Option.size()); |
| 319 | OS.indent(InitialPad) << Option; |
| 320 | |
| 321 | // Break on long option names. |
| 322 | if (Pad < 0) { |
| 323 | OS << "\n"; |
| 324 | Pad = OptionFieldWidth + InitialPad; |
| 325 | } |
| 326 | OS.indent(Pad + 1) << OptionHelp[i].second << '\n'; |
| 327 | } |
Daniel Dunbar | 2658f05 | 2009-12-04 21:08:40 +0000 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | static const char *getOptionHelpGroup(const OptTable &Opts, OptSpecifier Id) { |
| 331 | unsigned GroupID = Opts.getOptionGroupID(Id); |
| 332 | |
| 333 | // If not in a group, return the default help group. |
| 334 | if (!GroupID) |
| 335 | return "OPTIONS"; |
| 336 | |
| 337 | // Abuse the help text of the option groups to store the "help group" |
| 338 | // name. |
| 339 | // |
| 340 | // FIXME: Split out option groups. |
| 341 | if (const char *GroupHelp = Opts.getOptionHelpText(GroupID)) |
| 342 | return GroupHelp; |
| 343 | |
| 344 | // Otherwise keep looking. |
| 345 | return getOptionHelpGroup(Opts, GroupID); |
| 346 | } |
| 347 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 348 | void OptTable::PrintHelp(raw_ostream &OS, const char *Name, |
Richard Smith | f1eaab1 | 2012-11-09 22:36:44 +0000 | [diff] [blame^] | 349 | const char *Title, unsigned short FlagsToInclude, |
| 350 | unsigned short FlagsToExclude) const { |
Daniel Dunbar | 2658f05 | 2009-12-04 21:08:40 +0000 | [diff] [blame] | 351 | OS << "OVERVIEW: " << Title << "\n"; |
| 352 | OS << '\n'; |
| 353 | OS << "USAGE: " << Name << " [options] <inputs>\n"; |
| 354 | OS << '\n'; |
| 355 | |
| 356 | // Render help text into a map of group-name to a list of (option, help) |
| 357 | // pairs. |
| 358 | typedef std::map<std::string, |
| 359 | std::vector<std::pair<std::string, const char*> > > helpmap_ty; |
| 360 | helpmap_ty GroupedOptionHelp; |
| 361 | |
| 362 | for (unsigned i = 0, e = getNumOptions(); i != e; ++i) { |
| 363 | unsigned Id = i + 1; |
| 364 | |
| 365 | // FIXME: Split out option groups. |
| 366 | if (getOptionKind(Id) == Option::GroupClass) |
| 367 | continue; |
| 368 | |
Richard Smith | f1eaab1 | 2012-11-09 22:36:44 +0000 | [diff] [blame^] | 369 | if ((FlagsToInclude && !(getInfo(Id).Flags & FlagsToInclude)) || |
| 370 | getInfo(Id).Flags & FlagsToExclude) |
Daniel Dunbar | 2658f05 | 2009-12-04 21:08:40 +0000 | [diff] [blame] | 371 | continue; |
| 372 | |
| 373 | if (const char *Text = getOptionHelpText(Id)) { |
| 374 | const char *HelpGroup = getOptionHelpGroup(*this, Id); |
| 375 | const std::string &OptName = getOptionHelpName(*this, Id); |
| 376 | GroupedOptionHelp[HelpGroup].push_back(std::make_pair(OptName, Text)); |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | for (helpmap_ty::iterator it = GroupedOptionHelp .begin(), |
| 381 | ie = GroupedOptionHelp.end(); it != ie; ++it) { |
| 382 | if (it != GroupedOptionHelp .begin()) |
| 383 | OS << "\n"; |
| 384 | PrintHelpOptionList(OS, it->first, it->second); |
| 385 | } |
Daniel Dunbar | 60e107f | 2009-12-03 07:01:38 +0000 | [diff] [blame] | 386 | |
| 387 | OS.flush(); |
| 388 | } |