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