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 | |
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 | a79a2b5 | 2009-11-18 20:19:36 +0000 | [diff] [blame] | 75 | OptTable::OptTable(const Info *_OptionInfos, unsigned _NumOptionInfos) |
| 76 | : OptionInfos(_OptionInfos), NumOptionInfos(_NumOptionInfos), |
| 77 | Options(new Option*[NumOptionInfos]), |
| 78 | TheInputOption(0), TheUnknownOption(0), FirstSearchableIndex(0) |
| 79 | { |
Daniel Dunbar | 47393ba | 2009-07-13 21:50:47 +0000 | [diff] [blame] | 80 | // Explicitly zero initialize the error to work around a bug in array |
| 81 | // value-initialization on MinGW with gcc 4.3.5. |
Daniel Dunbar | a79a2b5 | 2009-11-18 20:19:36 +0000 | [diff] [blame] | 82 | memset(Options, 0, sizeof(*Options) * NumOptionInfos); |
Daniel Dunbar | 47393ba | 2009-07-13 21:50:47 +0000 | [diff] [blame] | 83 | |
Daniel Dunbar | b355692 | 2009-03-23 18:41:45 +0000 | [diff] [blame] | 84 | // Find start of normal options. |
Daniel Dunbar | a79a2b5 | 2009-11-18 20:19:36 +0000 | [diff] [blame] | 85 | for (unsigned i = 0, e = getNumOptions(); i != e; ++i) { |
| 86 | unsigned Kind = getInfo(i + 1).Kind; |
| 87 | if (Kind == Option::InputClass) { |
| 88 | assert(!TheInputOption && "Cannot have multiple input options!"); |
| 89 | TheInputOption = getOption(i + 1); |
| 90 | } else if (Kind == Option::UnknownClass) { |
| 91 | assert(!TheUnknownOption && "Cannot have multiple input options!"); |
| 92 | TheUnknownOption = getOption(i + 1); |
| 93 | } else if (Kind != Option::GroupClass) { |
| 94 | FirstSearchableIndex = i; |
Daniel Dunbar | b355692 | 2009-03-23 18:41:45 +0000 | [diff] [blame] | 95 | break; |
| 96 | } |
| 97 | } |
Daniel Dunbar | a79a2b5 | 2009-11-18 20:19:36 +0000 | [diff] [blame] | 98 | assert(FirstSearchableIndex != 0 && "No searchable options?"); |
Daniel Dunbar | b355692 | 2009-03-23 18:41:45 +0000 | [diff] [blame] | 99 | |
| 100 | #ifndef NDEBUG |
| 101 | // Check that everything after the first searchable option is a |
| 102 | // regular option class. |
Daniel Dunbar | a79a2b5 | 2009-11-18 20:19:36 +0000 | [diff] [blame] | 103 | for (unsigned i = FirstSearchableIndex, e = getNumOptions(); i != e; ++i) { |
| 104 | Option::OptionClass Kind = (Option::OptionClass) getInfo(i + 1).Kind; |
Daniel Dunbar | b355692 | 2009-03-23 18:41:45 +0000 | [diff] [blame] | 105 | assert((Kind != Option::InputClass && Kind != Option::UnknownClass && |
| 106 | Kind != Option::GroupClass) && |
| 107 | "Special options should be defined first!"); |
| 108 | } |
| 109 | |
| 110 | // Check that options are in order. |
Daniel Dunbar | a79a2b5 | 2009-11-18 20:19:36 +0000 | [diff] [blame] | 111 | for (unsigned i = FirstSearchableIndex+1, e = getNumOptions(); i != e; ++i) { |
| 112 | if (!(getInfo(i) < getInfo(i + 1))) { |
| 113 | getOption(i)->dump(); |
| 114 | getOption(i + 1)->dump(); |
Daniel Dunbar | b355692 | 2009-03-23 18:41:45 +0000 | [diff] [blame] | 115 | assert(0 && "Options are not in order!"); |
| 116 | } |
| 117 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 118 | #endif |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 121 | OptTable::~OptTable() { |
Daniel Dunbar | a79a2b5 | 2009-11-18 20:19:36 +0000 | [diff] [blame] | 122 | for (unsigned i = 0, e = getNumOptions(); i != e; ++i) |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 123 | delete Options[i]; |
| 124 | delete[] Options; |
| 125 | } |
| 126 | |
Daniel Dunbar | a79a2b5 | 2009-11-18 20:19:36 +0000 | [diff] [blame] | 127 | Option *OptTable::CreateOption(unsigned id) const { |
| 128 | const Info &info = getInfo(id); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 129 | const OptionGroup *Group = |
Daniel Dunbar | a79a2b5 | 2009-11-18 20:19:36 +0000 | [diff] [blame] | 130 | cast_or_null<OptionGroup>(getOption(info.GroupID)); |
| 131 | const Option *Alias = getOption(info.AliasID); |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 132 | |
| 133 | Option *Opt = 0; |
| 134 | switch (info.Kind) { |
Daniel Dunbar | 30b055f | 2009-03-04 21:53:04 +0000 | [diff] [blame] | 135 | case Option::InputClass: |
| 136 | Opt = new InputOption(); break; |
| 137 | case Option::UnknownClass: |
| 138 | Opt = new UnknownOption(); break; |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 139 | case Option::GroupClass: |
Daniel Dunbar | 30b055f | 2009-03-04 21:53:04 +0000 | [diff] [blame] | 140 | Opt = new OptionGroup(id, info.Name, Group); break; |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 141 | case Option::FlagClass: |
Daniel Dunbar | 30b055f | 2009-03-04 21:53:04 +0000 | [diff] [blame] | 142 | Opt = new FlagOption(id, info.Name, Group, Alias); break; |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 143 | case Option::JoinedClass: |
Daniel Dunbar | 30b055f | 2009-03-04 21:53:04 +0000 | [diff] [blame] | 144 | Opt = new JoinedOption(id, info.Name, Group, Alias); break; |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 145 | case Option::SeparateClass: |
Daniel Dunbar | 30b055f | 2009-03-04 21:53:04 +0000 | [diff] [blame] | 146 | Opt = new SeparateOption(id, info.Name, Group, Alias); break; |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 147 | case Option::CommaJoinedClass: |
Daniel Dunbar | 30b055f | 2009-03-04 21:53:04 +0000 | [diff] [blame] | 148 | Opt = new CommaJoinedOption(id, info.Name, Group, Alias); break; |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 149 | case Option::MultiArgClass: |
Daniel Dunbar | 30b055f | 2009-03-04 21:53:04 +0000 | [diff] [blame] | 150 | Opt = new MultiArgOption(id, info.Name, Group, Alias, info.Param); break; |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 151 | case Option::JoinedOrSeparateClass: |
Daniel Dunbar | 30b055f | 2009-03-04 21:53:04 +0000 | [diff] [blame] | 152 | Opt = new JoinedOrSeparateOption(id, info.Name, Group, Alias); break; |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 153 | case Option::JoinedAndSeparateClass: |
Daniel Dunbar | 30b055f | 2009-03-04 21:53:04 +0000 | [diff] [blame] | 154 | Opt = new JoinedAndSeparateOption(id, info.Name, Group, Alias); break; |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Daniel Dunbar | a4f6480 | 2009-11-18 17:42:34 +0000 | [diff] [blame] | 157 | if (info.Flags & DriverOption) |
| 158 | Opt->setDriverOption(true); |
| 159 | if (info.Flags & LinkerInput) |
| 160 | Opt->setLinkerInput(true); |
| 161 | if (info.Flags & NoArgumentUnused) |
| 162 | Opt->setNoArgumentUnused(true); |
| 163 | if (info.Flags & RenderAsInput) |
| 164 | Opt->setNoOptAsInput(true); |
| 165 | if (info.Flags & RenderJoined) { |
| 166 | assert(info.Kind == Option::SeparateClass && "Invalid option."); |
| 167 | Opt->setForceJoinedRender(true); |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 168 | } |
Daniel Dunbar | a4f6480 | 2009-11-18 17:42:34 +0000 | [diff] [blame] | 169 | if (info.Flags & RenderSeparate) { |
| 170 | assert(info.Kind == Option::JoinedClass && "Invalid option."); |
| 171 | Opt->setForceSeparateRender(true); |
| 172 | } |
| 173 | if (info.Flags & Unsupported) |
| 174 | Opt->setUnsupported(true); |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 175 | |
| 176 | return Opt; |
| 177 | } |
Daniel Dunbar | 70a0dbb | 2009-03-04 22:41:37 +0000 | [diff] [blame] | 178 | |
Daniel Dunbar | f3cad36 | 2009-03-25 04:13:45 +0000 | [diff] [blame] | 179 | Arg *OptTable::ParseOneArg(const InputArgList &Args, unsigned &Index) const { |
Daniel Dunbar | b0c4df5 | 2009-03-22 23:26:43 +0000 | [diff] [blame] | 180 | unsigned Prev = Index; |
Daniel Dunbar | 70a0dbb | 2009-03-04 22:41:37 +0000 | [diff] [blame] | 181 | const char *Str = Args.getArgString(Index); |
| 182 | |
Daniel Dunbar | a948045 | 2009-03-12 08:44:47 +0000 | [diff] [blame] | 183 | // Anything that doesn't start with '-' is an input, as is '-' itself. |
| 184 | if (Str[0] != '-' || Str[1] == '\0') |
Daniel Dunbar | a79a2b5 | 2009-11-18 20:19:36 +0000 | [diff] [blame] | 185 | return new PositionalArg(TheInputOption, Index++); |
Daniel Dunbar | 70a0dbb | 2009-03-04 22:41:37 +0000 | [diff] [blame] | 186 | |
Daniel Dunbar | a79a2b5 | 2009-11-18 20:19:36 +0000 | [diff] [blame] | 187 | const Info *Start = OptionInfos + FirstSearchableIndex; |
| 188 | const Info *End = OptionInfos + LastOption - 1; |
Daniel Dunbar | b0c4df5 | 2009-03-22 23:26:43 +0000 | [diff] [blame] | 189 | |
Daniel Dunbar | 4ae24e7 | 2009-04-07 18:21:47 +0000 | [diff] [blame] | 190 | // Search for the first next option which could be a prefix. |
Daniel Dunbar | 7547f74 | 2009-03-23 21:50:40 +0000 | [diff] [blame] | 191 | Start = std::lower_bound(Start, End, Str); |
| 192 | |
Daniel Dunbar | 4ae24e7 | 2009-04-07 18:21:47 +0000 | [diff] [blame] | 193 | // Options are stored in sorted order, with '\0' at the end of the |
| 194 | // alphabet. Since the only options which can accept a string must |
| 195 | // prefix it, we iteratively search for the next option which could |
| 196 | // be a prefix. |
| 197 | // |
| 198 | // FIXME: This is searching much more than necessary, but I am |
| 199 | // blanking on the simplest way to make it fast. We can solve this |
| 200 | // problem when we move to TableGen. |
Daniel Dunbar | 7547f74 | 2009-03-23 21:50:40 +0000 | [diff] [blame] | 201 | for (; Start != End; ++Start) { |
Daniel Dunbar | 4ae24e7 | 2009-04-07 18:21:47 +0000 | [diff] [blame] | 202 | // Scan for first option which is a proper prefix. |
| 203 | for (; Start != End; ++Start) |
| 204 | if (memcmp(Str, Start->Name, strlen(Start->Name)) == 0) |
| 205 | break; |
| 206 | if (Start == End) |
Daniel Dunbar | 7547f74 | 2009-03-23 21:50:40 +0000 | [diff] [blame] | 207 | break; |
| 208 | |
Daniel Dunbar | 4ae24e7 | 2009-04-07 18:21:47 +0000 | [diff] [blame] | 209 | // See if this option matches. |
Daniel Dunbar | 7547f74 | 2009-03-23 21:50:40 +0000 | [diff] [blame] | 210 | options::ID id = (options::ID) (Start - OptionInfos + 1); |
| 211 | if (Arg *A = getOption(id)->accept(Args, Index)) |
| 212 | return A; |
| 213 | |
| 214 | // Otherwise, see if this argument was missing values. |
| 215 | if (Prev != Index) |
| 216 | return 0; |
Daniel Dunbar | 70a0dbb | 2009-03-04 22:41:37 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Daniel Dunbar | a79a2b5 | 2009-11-18 20:19:36 +0000 | [diff] [blame] | 219 | return new PositionalArg(TheUnknownOption, Index++); |
Daniel Dunbar | 70a0dbb | 2009-03-04 22:41:37 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Daniel Dunbar | a79a2b5 | 2009-11-18 20:19:36 +0000 | [diff] [blame] | 222 | // |
| 223 | |
| 224 | static OptTable::Info InfoTable[] = { |
| 225 | // The InputOption info |
| 226 | { "<input>", 0, 0, Option::InputClass, DriverOption, 0, OPT_INVALID, OPT_INVALID }, |
| 227 | // The UnknownOption info |
| 228 | { "<unknown>", 0, 0, Option::UnknownClass, 0, 0, OPT_INVALID, OPT_INVALID }, |
| 229 | |
| 230 | #define OPTION(NAME, ID, KIND, GROUP, ALIAS, FLAGS, PARAM, \ |
| 231 | HELPTEXT, METAVAR) \ |
| 232 | { NAME, HELPTEXT, METAVAR, Option::KIND##Class, FLAGS, PARAM, \ |
| 233 | OPT_##GROUP, OPT_##ALIAS }, |
| 234 | #include "clang/Driver/Options.def" |
| 235 | }; |
| 236 | |
| 237 | namespace { |
| 238 | |
| 239 | class DriverOptTable : public OptTable { |
| 240 | public: |
| 241 | DriverOptTable() |
| 242 | : OptTable(InfoTable, sizeof(InfoTable) / sizeof(InfoTable[0])) {} |
| 243 | }; |
| 244 | |
| 245 | } |
| 246 | |
| 247 | OptTable *clang::driver::createDriverOptTable() { |
| 248 | return new DriverOptTable(); |
| 249 | } |