Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 1 | //===--- Options.cpp - Option info table --------------------------------*-===// |
| 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 "clang/Driver/Options.h" |
| 11 | |
Daniel Dunbar | 70a0dbb | 2009-03-04 22:41:37 +0000 | [diff] [blame] | 12 | #include "clang/Driver/Arg.h" |
| 13 | #include "clang/Driver/ArgList.h" |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 14 | #include "clang/Driver/Option.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> |
| 17 | |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 18 | using namespace clang::driver; |
| 19 | using namespace clang::driver::options; |
| 20 | |
| 21 | struct Info { |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 22 | const char *Name; |
Daniel Dunbar | e1495ec | 2009-03-12 01:46:53 +0000 | [diff] [blame] | 23 | const char *Flags; |
Daniel Dunbar | c0d12e9 | 2009-03-31 20:12:05 +0000 | [diff] [blame] | 24 | const char *HelpText; |
| 25 | const char *MetaVar; |
Daniel Dunbar | e1495ec | 2009-03-12 01:46:53 +0000 | [diff] [blame] | 26 | |
| 27 | Option::OptionClass Kind; |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 28 | unsigned GroupID; |
| 29 | unsigned AliasID; |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 30 | unsigned Param; |
| 31 | }; |
| 32 | |
Daniel Dunbar | b355692 | 2009-03-23 18:41:45 +0000 | [diff] [blame] | 33 | // Ordering on Info. The ordering is *almost* lexicographic, with two |
| 34 | // exceptions. First, '\0' comes at the end of the alphabet instead of |
| 35 | // the beginning (thus options preceed any other options which prefix |
| 36 | // them). Second, for options with the same name, the less permissive |
| 37 | // version should come first; a Flag option should preceed a Joined |
| 38 | // option, for example. |
| 39 | |
| 40 | static int StrCmpOptionName(const char *A, const char *B) { |
| 41 | char a = *A, b = *B; |
| 42 | while (a == b) { |
| 43 | if (a == '\0') |
| 44 | return 0; |
| 45 | |
| 46 | a = *++A; |
| 47 | b = *++B; |
| 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; |
| 57 | } |
| 58 | |
| 59 | static inline bool operator<(const Info &A, const Info &B) { |
| 60 | if (&A == &B) |
| 61 | return false; |
| 62 | |
| 63 | if (int N = StrCmpOptionName(A.Name, B.Name)) |
| 64 | return N == -1; |
| 65 | |
| 66 | // Names are the same, check that classes are in order; exactly one |
| 67 | // should be joined, and it should succeed the other. |
Daniel Dunbar | cf51ece | 2009-03-25 03:06:26 +0000 | [diff] [blame] | 68 | assert(((A.Kind == Option::JoinedClass) ^ (B.Kind == Option::JoinedClass)) && |
Daniel Dunbar | b355692 | 2009-03-23 18:41:45 +0000 | [diff] [blame] | 69 | "Unexpected classes for options with same name."); |
| 70 | return B.Kind == Option::JoinedClass; |
| 71 | } |
| 72 | |
| 73 | // |
| 74 | |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 75 | static Info OptionInfos[] = { |
Daniel Dunbar | 30b055f | 2009-03-04 21:53:04 +0000 | [diff] [blame] | 76 | // The InputOption info |
Daniel Dunbar | c0d12e9 | 2009-03-31 20:12:05 +0000 | [diff] [blame] | 77 | { "<input>", "d", 0, 0, Option::InputClass, OPT_INVALID, OPT_INVALID, 0 }, |
Daniel Dunbar | 30b055f | 2009-03-04 21:53:04 +0000 | [diff] [blame] | 78 | // The UnknownOption info |
Daniel Dunbar | c0d12e9 | 2009-03-31 20:12:05 +0000 | [diff] [blame] | 79 | { "<unknown>", "", 0, 0, Option::UnknownClass, OPT_INVALID, OPT_INVALID, 0 }, |
Daniel Dunbar | 30b055f | 2009-03-04 21:53:04 +0000 | [diff] [blame] | 80 | |
Daniel Dunbar | c0d12e9 | 2009-03-31 20:12:05 +0000 | [diff] [blame] | 81 | #define OPTION(NAME, ID, KIND, GROUP, ALIAS, FLAGS, PARAM, \ |
| 82 | HELPTEXT, METAVAR) \ |
| 83 | { NAME, FLAGS, HELPTEXT, METAVAR, \ |
| 84 | Option::KIND##Class, OPT_##GROUP, OPT_##ALIAS, PARAM }, |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 85 | #include "clang/Driver/Options.def" |
| 86 | }; |
| 87 | static const unsigned numOptions = sizeof(OptionInfos) / sizeof(OptionInfos[0]); |
| 88 | |
| 89 | static Info &getInfo(unsigned id) { |
| 90 | assert(id > 0 && id - 1 < numOptions && "Invalid Option ID."); |
| 91 | return OptionInfos[id - 1]; |
| 92 | } |
| 93 | |
Daniel Dunbar | 47393ba | 2009-07-13 21:50:47 +0000 | [diff] [blame^] | 94 | OptTable::OptTable() : Options(new Option*[numOptions]) { |
| 95 | // Explicitly zero initialize the error to work around a bug in array |
| 96 | // value-initialization on MinGW with gcc 4.3.5. |
| 97 | memset(Options, 0, sizeof(*Options) * numOptions); |
| 98 | |
Daniel Dunbar | b355692 | 2009-03-23 18:41:45 +0000 | [diff] [blame] | 99 | // Find start of normal options. |
| 100 | FirstSearchableOption = 0; |
| 101 | for (unsigned i = OPT_UNKNOWN + 1; i < LastOption; ++i) { |
| 102 | if (getInfo(i).Kind != Option::GroupClass) { |
Daniel Dunbar | 8a7e66d | 2009-03-23 19:19:19 +0000 | [diff] [blame] | 103 | FirstSearchableOption = i; |
Daniel Dunbar | b355692 | 2009-03-23 18:41:45 +0000 | [diff] [blame] | 104 | break; |
| 105 | } |
| 106 | } |
| 107 | assert(FirstSearchableOption != 0 && "No searchable options?"); |
| 108 | |
| 109 | #ifndef NDEBUG |
| 110 | // Check that everything after the first searchable option is a |
| 111 | // regular option class. |
| 112 | for (unsigned i = FirstSearchableOption; i < LastOption; ++i) { |
| 113 | Option::OptionClass Kind = getInfo(i).Kind; |
| 114 | assert((Kind != Option::InputClass && Kind != Option::UnknownClass && |
| 115 | Kind != Option::GroupClass) && |
| 116 | "Special options should be defined first!"); |
| 117 | } |
| 118 | |
| 119 | // Check that options are in order. |
| 120 | for (unsigned i = FirstSearchableOption + 1; i < LastOption; ++i) { |
| 121 | if (!(getInfo(i - 1) < getInfo(i))) { |
| 122 | getOption((options::ID) (i - 1))->dump(); |
| 123 | getOption((options::ID) i)->dump(); |
| 124 | assert(0 && "Options are not in order!"); |
| 125 | } |
| 126 | } |
| 127 | #endif |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | OptTable::~OptTable() { |
Daniel Dunbar | b355692 | 2009-03-23 18:41:45 +0000 | [diff] [blame] | 131 | for (unsigned i = 0; i < numOptions; ++i) |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 132 | delete Options[i]; |
| 133 | delete[] Options; |
| 134 | } |
| 135 | |
| 136 | unsigned OptTable::getNumOptions() const { |
| 137 | return numOptions; |
| 138 | } |
| 139 | |
| 140 | const char *OptTable::getOptionName(options::ID id) const { |
| 141 | return getInfo(id).Name; |
| 142 | } |
| 143 | |
Daniel Dunbar | febdf7d | 2009-03-31 21:26:12 +0000 | [diff] [blame] | 144 | unsigned OptTable::getOptionKind(options::ID id) const { |
| 145 | return getInfo(id).Kind; |
| 146 | } |
| 147 | |
Daniel Dunbar | c0d12e9 | 2009-03-31 20:12:05 +0000 | [diff] [blame] | 148 | const char *OptTable::getOptionHelpText(options::ID id) const { |
| 149 | return getInfo(id).HelpText; |
| 150 | } |
| 151 | |
| 152 | const char *OptTable::getOptionMetaVar(options::ID id) const { |
Daniel Dunbar | febdf7d | 2009-03-31 21:26:12 +0000 | [diff] [blame] | 153 | return getInfo(id).MetaVar; |
Daniel Dunbar | c0d12e9 | 2009-03-31 20:12:05 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 156 | const Option *OptTable::getOption(options::ID id) const { |
Daniel Dunbar | b349fcc | 2009-03-12 03:42:54 +0000 | [diff] [blame] | 157 | if (id == OPT_INVALID) |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 158 | return 0; |
| 159 | |
| 160 | assert((unsigned) (id - 1) < numOptions && "Invalid ID."); |
| 161 | |
| 162 | Option *&Entry = Options[id - 1]; |
| 163 | if (!Entry) |
| 164 | Entry = constructOption(id); |
| 165 | |
| 166 | return Entry; |
| 167 | } |
| 168 | |
| 169 | Option *OptTable::constructOption(options::ID id) const { |
| 170 | Info &info = getInfo(id); |
| 171 | const OptionGroup *Group = |
| 172 | cast_or_null<OptionGroup>(getOption((options::ID) info.GroupID)); |
| 173 | const Option *Alias = getOption((options::ID) info.AliasID); |
| 174 | |
| 175 | Option *Opt = 0; |
| 176 | switch (info.Kind) { |
Daniel Dunbar | 30b055f | 2009-03-04 21:53:04 +0000 | [diff] [blame] | 177 | case Option::InputClass: |
| 178 | Opt = new InputOption(); break; |
| 179 | case Option::UnknownClass: |
| 180 | Opt = new UnknownOption(); break; |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 181 | case Option::GroupClass: |
Daniel Dunbar | 30b055f | 2009-03-04 21:53:04 +0000 | [diff] [blame] | 182 | Opt = new OptionGroup(id, info.Name, Group); break; |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 183 | case Option::FlagClass: |
Daniel Dunbar | 30b055f | 2009-03-04 21:53:04 +0000 | [diff] [blame] | 184 | Opt = new FlagOption(id, info.Name, Group, Alias); break; |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 185 | case Option::JoinedClass: |
Daniel Dunbar | 30b055f | 2009-03-04 21:53:04 +0000 | [diff] [blame] | 186 | Opt = new JoinedOption(id, info.Name, Group, Alias); break; |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 187 | case Option::SeparateClass: |
Daniel Dunbar | 30b055f | 2009-03-04 21:53:04 +0000 | [diff] [blame] | 188 | Opt = new SeparateOption(id, info.Name, Group, Alias); break; |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 189 | case Option::CommaJoinedClass: |
Daniel Dunbar | 30b055f | 2009-03-04 21:53:04 +0000 | [diff] [blame] | 190 | Opt = new CommaJoinedOption(id, info.Name, Group, Alias); break; |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 191 | case Option::MultiArgClass: |
Daniel Dunbar | 30b055f | 2009-03-04 21:53:04 +0000 | [diff] [blame] | 192 | Opt = new MultiArgOption(id, info.Name, Group, Alias, info.Param); break; |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 193 | case Option::JoinedOrSeparateClass: |
Daniel Dunbar | 30b055f | 2009-03-04 21:53:04 +0000 | [diff] [blame] | 194 | Opt = new JoinedOrSeparateOption(id, info.Name, Group, Alias); break; |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 195 | case Option::JoinedAndSeparateClass: |
Daniel Dunbar | 30b055f | 2009-03-04 21:53:04 +0000 | [diff] [blame] | 196 | Opt = new JoinedAndSeparateOption(id, info.Name, Group, Alias); break; |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 199 | for (const char *s = info.Flags; *s; ++s) { |
| 200 | switch (*s) { |
Daniel Dunbar | 0f9098e | 2009-03-04 21:05:23 +0000 | [diff] [blame] | 201 | default: assert(0 && "Invalid option flag."); |
Daniel Dunbar | 6d954d7 | 2009-03-18 08:01:15 +0000 | [diff] [blame] | 202 | case 'J': |
| 203 | assert(info.Kind == Option::SeparateClass && "Invalid option."); |
| 204 | Opt->setForceJoinedRender(true); break; |
| 205 | case 'S': |
| 206 | assert(info.Kind == Option::JoinedClass && "Invalid option."); |
| 207 | Opt->setForceSeparateRender(true); break; |
Daniel Dunbar | f6dd66b | 2009-03-25 06:08:46 +0000 | [diff] [blame] | 208 | case 'd': Opt->setDriverOption(true); break; |
Daniel Dunbar | 644eade | 2009-03-12 05:46:32 +0000 | [diff] [blame] | 209 | case 'i': Opt->setNoOptAsInput(true); break; |
| 210 | case 'l': Opt->setLinkerInput(true); break; |
Daniel Dunbar | 1e23f5f | 2009-04-07 19:04:18 +0000 | [diff] [blame] | 211 | case 'q': Opt->setNoArgumentUnused(true); break; |
Daniel Dunbar | 644eade | 2009-03-12 05:46:32 +0000 | [diff] [blame] | 212 | case 'u': Opt->setUnsupported(true); break; |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | |
| 216 | return Opt; |
| 217 | } |
Daniel Dunbar | 70a0dbb | 2009-03-04 22:41:37 +0000 | [diff] [blame] | 218 | |
Daniel Dunbar | 7547f74 | 2009-03-23 21:50:40 +0000 | [diff] [blame] | 219 | // Support lower_bound between info and an option name. |
| 220 | static inline bool operator<(struct Info &I, const char *Name) { |
| 221 | return StrCmpOptionName(I.Name, Name) == -1; |
| 222 | } |
| 223 | static inline bool operator<(const char *Name, struct Info &I) { |
| 224 | return StrCmpOptionName(Name, I.Name) == -1; |
| 225 | } |
| 226 | |
Daniel Dunbar | f3cad36 | 2009-03-25 04:13:45 +0000 | [diff] [blame] | 227 | Arg *OptTable::ParseOneArg(const InputArgList &Args, unsigned &Index) const { |
Daniel Dunbar | b0c4df5 | 2009-03-22 23:26:43 +0000 | [diff] [blame] | 228 | unsigned Prev = Index; |
Daniel Dunbar | 70a0dbb | 2009-03-04 22:41:37 +0000 | [diff] [blame] | 229 | const char *Str = Args.getArgString(Index); |
| 230 | |
Daniel Dunbar | a948045 | 2009-03-12 08:44:47 +0000 | [diff] [blame] | 231 | // Anything that doesn't start with '-' is an input, as is '-' itself. |
| 232 | if (Str[0] != '-' || Str[1] == '\0') |
Daniel Dunbar | b349fcc | 2009-03-12 03:42:54 +0000 | [diff] [blame] | 233 | return new PositionalArg(getOption(OPT_INPUT), Index++); |
Daniel Dunbar | 70a0dbb | 2009-03-04 22:41:37 +0000 | [diff] [blame] | 234 | |
Daniel Dunbar | 7547f74 | 2009-03-23 21:50:40 +0000 | [diff] [blame] | 235 | struct Info *Start = OptionInfos + FirstSearchableOption - 1; |
| 236 | struct Info *End = OptionInfos + LastOption - 1; |
Daniel Dunbar | b0c4df5 | 2009-03-22 23:26:43 +0000 | [diff] [blame] | 237 | |
Daniel Dunbar | 4ae24e7 | 2009-04-07 18:21:47 +0000 | [diff] [blame] | 238 | // Search for the first next option which could be a prefix. |
Daniel Dunbar | 7547f74 | 2009-03-23 21:50:40 +0000 | [diff] [blame] | 239 | Start = std::lower_bound(Start, End, Str); |
| 240 | |
Daniel Dunbar | 4ae24e7 | 2009-04-07 18:21:47 +0000 | [diff] [blame] | 241 | // Options are stored in sorted order, with '\0' at the end of the |
| 242 | // alphabet. Since the only options which can accept a string must |
| 243 | // prefix it, we iteratively search for the next option which could |
| 244 | // be a prefix. |
| 245 | // |
| 246 | // FIXME: This is searching much more than necessary, but I am |
| 247 | // blanking on the simplest way to make it fast. We can solve this |
| 248 | // problem when we move to TableGen. |
Daniel Dunbar | 7547f74 | 2009-03-23 21:50:40 +0000 | [diff] [blame] | 249 | for (; Start != End; ++Start) { |
Daniel Dunbar | 4ae24e7 | 2009-04-07 18:21:47 +0000 | [diff] [blame] | 250 | // Scan for first option which is a proper prefix. |
| 251 | for (; Start != End; ++Start) |
| 252 | if (memcmp(Str, Start->Name, strlen(Start->Name)) == 0) |
| 253 | break; |
| 254 | if (Start == End) |
Daniel Dunbar | 7547f74 | 2009-03-23 21:50:40 +0000 | [diff] [blame] | 255 | break; |
| 256 | |
Daniel Dunbar | 4ae24e7 | 2009-04-07 18:21:47 +0000 | [diff] [blame] | 257 | // See if this option matches. |
Daniel Dunbar | 7547f74 | 2009-03-23 21:50:40 +0000 | [diff] [blame] | 258 | options::ID id = (options::ID) (Start - OptionInfos + 1); |
| 259 | if (Arg *A = getOption(id)->accept(Args, Index)) |
| 260 | return A; |
| 261 | |
| 262 | // Otherwise, see if this argument was missing values. |
| 263 | if (Prev != Index) |
| 264 | return 0; |
Daniel Dunbar | 70a0dbb | 2009-03-04 22:41:37 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Daniel Dunbar | b349fcc | 2009-03-12 03:42:54 +0000 | [diff] [blame] | 267 | return new PositionalArg(getOption(OPT_UNKNOWN), Index++); |
Daniel Dunbar | 70a0dbb | 2009-03-04 22:41:37 +0000 | [diff] [blame] | 268 | } |
| 269 | |