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