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 | 7547f74 | 2009-03-23 21:50:40 +0000 | [diff] [blame] | 14 | #include <algorithm> |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 15 | #include <cassert> |
| 16 | |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 17 | using namespace clang::driver; |
| 18 | using namespace clang::driver::options; |
| 19 | |
Daniel Dunbar | b355692 | 2009-03-23 18:41:45 +0000 | [diff] [blame] | 20 | // Ordering on Info. The ordering is *almost* lexicographic, with two |
| 21 | // exceptions. First, '\0' comes at the end of the alphabet instead of |
| 22 | // the beginning (thus options preceed any other options which prefix |
| 23 | // them). Second, for options with the same name, the less permissive |
| 24 | // version should come first; a Flag option should preceed a Joined |
| 25 | // option, for example. |
| 26 | |
| 27 | static int StrCmpOptionName(const char *A, const char *B) { |
| 28 | char a = *A, b = *B; |
| 29 | while (a == b) { |
| 30 | if (a == '\0') |
| 31 | return 0; |
| 32 | |
| 33 | a = *++A; |
| 34 | b = *++B; |
| 35 | } |
| 36 | |
| 37 | if (a == '\0') // A is a prefix of B. |
| 38 | return 1; |
| 39 | if (b == '\0') // B is a prefix of A. |
| 40 | return -1; |
| 41 | |
| 42 | // Otherwise lexicographic. |
| 43 | return (a < b) ? -1 : 1; |
| 44 | } |
| 45 | |
Daniel Dunbar | 1ce9cf0 | 2009-11-18 21:42:57 +0000 | [diff] [blame] | 46 | namespace clang { |
| 47 | namespace driver { |
Daniel Dunbar | a79a2b5 | 2009-11-18 20:19:36 +0000 | [diff] [blame] | 48 | static inline bool operator<(const OptTable::Info &A, const OptTable::Info &B) { |
Daniel Dunbar | b355692 | 2009-03-23 18:41:45 +0000 | [diff] [blame] | 49 | if (&A == &B) |
| 50 | return false; |
| 51 | |
| 52 | if (int N = StrCmpOptionName(A.Name, B.Name)) |
| 53 | return N == -1; |
| 54 | |
| 55 | // Names are the same, check that classes are in order; exactly one |
| 56 | // should be joined, and it should succeed the other. |
Daniel Dunbar | cf51ece | 2009-03-25 03:06:26 +0000 | [diff] [blame] | 57 | assert(((A.Kind == Option::JoinedClass) ^ (B.Kind == Option::JoinedClass)) && |
Daniel Dunbar | b355692 | 2009-03-23 18:41:45 +0000 | [diff] [blame] | 58 | "Unexpected classes for options with same name."); |
| 59 | return B.Kind == Option::JoinedClass; |
| 60 | } |
| 61 | |
Daniel Dunbar | 1ce9cf0 | 2009-11-18 21:42:57 +0000 | [diff] [blame] | 62 | // Support lower_bound between info and an option name. |
| 63 | static inline bool operator<(const OptTable::Info &I, const char *Name) { |
| 64 | return StrCmpOptionName(I.Name, Name) == -1; |
| 65 | } |
| 66 | static inline bool operator<(const char *Name, const OptTable::Info &I) { |
| 67 | return StrCmpOptionName(Name, I.Name) == -1; |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
Daniel Dunbar | b355692 | 2009-03-23 18:41:45 +0000 | [diff] [blame] | 72 | // |
| 73 | |
Daniel Dunbar | 9e1f982 | 2009-11-19 04:14:53 +0000 | [diff] [blame] | 74 | OptSpecifier::OptSpecifier(const Option *Opt) : ID(Opt->getID()) {} |
| 75 | |
| 76 | // |
| 77 | |
Daniel Dunbar | a79a2b5 | 2009-11-18 20:19:36 +0000 | [diff] [blame] | 78 | OptTable::OptTable(const Info *_OptionInfos, unsigned _NumOptionInfos) |
| 79 | : OptionInfos(_OptionInfos), NumOptionInfos(_NumOptionInfos), |
| 80 | Options(new Option*[NumOptionInfos]), |
| 81 | TheInputOption(0), TheUnknownOption(0), FirstSearchableIndex(0) |
| 82 | { |
Daniel Dunbar | 47393ba | 2009-07-13 21:50:47 +0000 | [diff] [blame] | 83 | // Explicitly zero initialize the error to work around a bug in array |
| 84 | // value-initialization on MinGW with gcc 4.3.5. |
Daniel Dunbar | a79a2b5 | 2009-11-18 20:19:36 +0000 | [diff] [blame] | 85 | memset(Options, 0, sizeof(*Options) * NumOptionInfos); |
Daniel Dunbar | 47393ba | 2009-07-13 21:50:47 +0000 | [diff] [blame] | 86 | |
Daniel Dunbar | b355692 | 2009-03-23 18:41:45 +0000 | [diff] [blame] | 87 | // Find start of normal options. |
Daniel Dunbar | a79a2b5 | 2009-11-18 20:19:36 +0000 | [diff] [blame] | 88 | for (unsigned i = 0, e = getNumOptions(); i != e; ++i) { |
| 89 | unsigned Kind = getInfo(i + 1).Kind; |
| 90 | if (Kind == Option::InputClass) { |
| 91 | assert(!TheInputOption && "Cannot have multiple input options!"); |
| 92 | TheInputOption = getOption(i + 1); |
| 93 | } else if (Kind == Option::UnknownClass) { |
| 94 | assert(!TheUnknownOption && "Cannot have multiple input options!"); |
| 95 | TheUnknownOption = getOption(i + 1); |
| 96 | } else if (Kind != Option::GroupClass) { |
| 97 | FirstSearchableIndex = i; |
Daniel Dunbar | b355692 | 2009-03-23 18:41:45 +0000 | [diff] [blame] | 98 | break; |
| 99 | } |
| 100 | } |
Daniel Dunbar | a79a2b5 | 2009-11-18 20:19:36 +0000 | [diff] [blame] | 101 | assert(FirstSearchableIndex != 0 && "No searchable options?"); |
Daniel Dunbar | b355692 | 2009-03-23 18:41:45 +0000 | [diff] [blame] | 102 | |
| 103 | #ifndef NDEBUG |
| 104 | // Check that everything after the first searchable option is a |
| 105 | // regular option class. |
Daniel Dunbar | a79a2b5 | 2009-11-18 20:19:36 +0000 | [diff] [blame] | 106 | for (unsigned i = FirstSearchableIndex, e = getNumOptions(); i != e; ++i) { |
| 107 | Option::OptionClass Kind = (Option::OptionClass) getInfo(i + 1).Kind; |
Daniel Dunbar | b355692 | 2009-03-23 18:41:45 +0000 | [diff] [blame] | 108 | assert((Kind != Option::InputClass && Kind != Option::UnknownClass && |
| 109 | Kind != Option::GroupClass) && |
| 110 | "Special options should be defined first!"); |
| 111 | } |
| 112 | |
| 113 | // Check that options are in order. |
Daniel Dunbar | a79a2b5 | 2009-11-18 20:19:36 +0000 | [diff] [blame] | 114 | for (unsigned i = FirstSearchableIndex+1, e = getNumOptions(); i != e; ++i) { |
| 115 | if (!(getInfo(i) < getInfo(i + 1))) { |
| 116 | getOption(i)->dump(); |
| 117 | getOption(i + 1)->dump(); |
Daniel Dunbar | b355692 | 2009-03-23 18:41:45 +0000 | [diff] [blame] | 118 | assert(0 && "Options are not in order!"); |
| 119 | } |
| 120 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 121 | #endif |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 124 | OptTable::~OptTable() { |
Daniel Dunbar | a79a2b5 | 2009-11-18 20:19:36 +0000 | [diff] [blame] | 125 | for (unsigned i = 0, e = getNumOptions(); i != e; ++i) |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 126 | delete Options[i]; |
| 127 | delete[] Options; |
| 128 | } |
| 129 | |
Daniel Dunbar | a79a2b5 | 2009-11-18 20:19:36 +0000 | [diff] [blame] | 130 | Option *OptTable::CreateOption(unsigned id) const { |
| 131 | const Info &info = getInfo(id); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 132 | const OptionGroup *Group = |
Daniel Dunbar | a79a2b5 | 2009-11-18 20:19:36 +0000 | [diff] [blame] | 133 | cast_or_null<OptionGroup>(getOption(info.GroupID)); |
| 134 | const Option *Alias = getOption(info.AliasID); |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 135 | |
| 136 | Option *Opt = 0; |
| 137 | switch (info.Kind) { |
Daniel Dunbar | 30b055f | 2009-03-04 21:53:04 +0000 | [diff] [blame] | 138 | case Option::InputClass: |
Daniel Dunbar | a0289fd | 2009-11-19 04:25:06 +0000 | [diff] [blame^] | 139 | Opt = new InputOption(id); break; |
Daniel Dunbar | 30b055f | 2009-03-04 21:53:04 +0000 | [diff] [blame] | 140 | case Option::UnknownClass: |
Daniel Dunbar | a0289fd | 2009-11-19 04:25:06 +0000 | [diff] [blame^] | 141 | Opt = new UnknownOption(id); break; |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 142 | case Option::GroupClass: |
Daniel Dunbar | 30b055f | 2009-03-04 21:53:04 +0000 | [diff] [blame] | 143 | Opt = new OptionGroup(id, info.Name, Group); break; |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 144 | case Option::FlagClass: |
Daniel Dunbar | 30b055f | 2009-03-04 21:53:04 +0000 | [diff] [blame] | 145 | Opt = new FlagOption(id, info.Name, Group, Alias); break; |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 146 | case Option::JoinedClass: |
Daniel Dunbar | 30b055f | 2009-03-04 21:53:04 +0000 | [diff] [blame] | 147 | Opt = new JoinedOption(id, info.Name, Group, Alias); break; |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 148 | case Option::SeparateClass: |
Daniel Dunbar | 30b055f | 2009-03-04 21:53:04 +0000 | [diff] [blame] | 149 | Opt = new SeparateOption(id, info.Name, Group, Alias); break; |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 150 | case Option::CommaJoinedClass: |
Daniel Dunbar | 30b055f | 2009-03-04 21:53:04 +0000 | [diff] [blame] | 151 | Opt = new CommaJoinedOption(id, info.Name, Group, Alias); break; |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 152 | case Option::MultiArgClass: |
Daniel Dunbar | 30b055f | 2009-03-04 21:53:04 +0000 | [diff] [blame] | 153 | Opt = new MultiArgOption(id, info.Name, Group, Alias, info.Param); break; |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 154 | case Option::JoinedOrSeparateClass: |
Daniel Dunbar | 30b055f | 2009-03-04 21:53:04 +0000 | [diff] [blame] | 155 | Opt = new JoinedOrSeparateOption(id, info.Name, Group, Alias); break; |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 156 | case Option::JoinedAndSeparateClass: |
Daniel Dunbar | 30b055f | 2009-03-04 21:53:04 +0000 | [diff] [blame] | 157 | Opt = new JoinedAndSeparateOption(id, info.Name, Group, Alias); break; |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Daniel Dunbar | a4f6480 | 2009-11-18 17:42:34 +0000 | [diff] [blame] | 160 | if (info.Flags & DriverOption) |
| 161 | Opt->setDriverOption(true); |
| 162 | if (info.Flags & LinkerInput) |
| 163 | Opt->setLinkerInput(true); |
| 164 | if (info.Flags & NoArgumentUnused) |
| 165 | Opt->setNoArgumentUnused(true); |
| 166 | if (info.Flags & RenderAsInput) |
| 167 | Opt->setNoOptAsInput(true); |
| 168 | if (info.Flags & RenderJoined) { |
| 169 | assert(info.Kind == Option::SeparateClass && "Invalid option."); |
| 170 | Opt->setForceJoinedRender(true); |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 171 | } |
Daniel Dunbar | a4f6480 | 2009-11-18 17:42:34 +0000 | [diff] [blame] | 172 | if (info.Flags & RenderSeparate) { |
| 173 | assert(info.Kind == Option::JoinedClass && "Invalid option."); |
| 174 | Opt->setForceSeparateRender(true); |
| 175 | } |
| 176 | if (info.Flags & Unsupported) |
| 177 | Opt->setUnsupported(true); |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 178 | |
| 179 | return Opt; |
| 180 | } |
Daniel Dunbar | 70a0dbb | 2009-03-04 22:41:37 +0000 | [diff] [blame] | 181 | |
Daniel Dunbar | f3cad36 | 2009-03-25 04:13:45 +0000 | [diff] [blame] | 182 | Arg *OptTable::ParseOneArg(const InputArgList &Args, unsigned &Index) const { |
Daniel Dunbar | b0c4df5 | 2009-03-22 23:26:43 +0000 | [diff] [blame] | 183 | unsigned Prev = Index; |
Daniel Dunbar | 70a0dbb | 2009-03-04 22:41:37 +0000 | [diff] [blame] | 184 | const char *Str = Args.getArgString(Index); |
| 185 | |
Daniel Dunbar | a948045 | 2009-03-12 08:44:47 +0000 | [diff] [blame] | 186 | // Anything that doesn't start with '-' is an input, as is '-' itself. |
| 187 | if (Str[0] != '-' || Str[1] == '\0') |
Daniel Dunbar | a79a2b5 | 2009-11-18 20:19:36 +0000 | [diff] [blame] | 188 | return new PositionalArg(TheInputOption, Index++); |
Daniel Dunbar | 70a0dbb | 2009-03-04 22:41:37 +0000 | [diff] [blame] | 189 | |
Daniel Dunbar | a79a2b5 | 2009-11-18 20:19:36 +0000 | [diff] [blame] | 190 | const Info *Start = OptionInfos + FirstSearchableIndex; |
Daniel Dunbar | a0289fd | 2009-11-19 04:25:06 +0000 | [diff] [blame^] | 191 | const Info *End = OptionInfos + getNumOptions(); |
Daniel Dunbar | b0c4df5 | 2009-03-22 23:26:43 +0000 | [diff] [blame] | 192 | |
Daniel Dunbar | 4ae24e7 | 2009-04-07 18:21:47 +0000 | [diff] [blame] | 193 | // Search for the first next option which could be a prefix. |
Daniel Dunbar | 7547f74 | 2009-03-23 21:50:40 +0000 | [diff] [blame] | 194 | Start = std::lower_bound(Start, End, Str); |
| 195 | |
Daniel Dunbar | 4ae24e7 | 2009-04-07 18:21:47 +0000 | [diff] [blame] | 196 | // Options are stored in sorted order, with '\0' at the end of the |
| 197 | // alphabet. Since the only options which can accept a string must |
| 198 | // prefix it, we iteratively search for the next option which could |
| 199 | // be a prefix. |
| 200 | // |
| 201 | // FIXME: This is searching much more than necessary, but I am |
| 202 | // blanking on the simplest way to make it fast. We can solve this |
| 203 | // problem when we move to TableGen. |
Daniel Dunbar | 7547f74 | 2009-03-23 21:50:40 +0000 | [diff] [blame] | 204 | for (; Start != End; ++Start) { |
Daniel Dunbar | 4ae24e7 | 2009-04-07 18:21:47 +0000 | [diff] [blame] | 205 | // Scan for first option which is a proper prefix. |
| 206 | for (; Start != End; ++Start) |
| 207 | if (memcmp(Str, Start->Name, strlen(Start->Name)) == 0) |
| 208 | break; |
| 209 | if (Start == End) |
Daniel Dunbar | 7547f74 | 2009-03-23 21:50:40 +0000 | [diff] [blame] | 210 | break; |
| 211 | |
Daniel Dunbar | 4ae24e7 | 2009-04-07 18:21:47 +0000 | [diff] [blame] | 212 | // See if this option matches. |
Daniel Dunbar | a0289fd | 2009-11-19 04:25:06 +0000 | [diff] [blame^] | 213 | if (Arg *A = getOption(Start - OptionInfos + 1)->accept(Args, Index)) |
Daniel Dunbar | 7547f74 | 2009-03-23 21:50:40 +0000 | [diff] [blame] | 214 | return A; |
| 215 | |
| 216 | // Otherwise, see if this argument was missing values. |
| 217 | if (Prev != Index) |
| 218 | return 0; |
Daniel Dunbar | 70a0dbb | 2009-03-04 22:41:37 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Daniel Dunbar | a79a2b5 | 2009-11-18 20:19:36 +0000 | [diff] [blame] | 221 | return new PositionalArg(TheUnknownOption, Index++); |
Daniel Dunbar | 70a0dbb | 2009-03-04 22:41:37 +0000 | [diff] [blame] | 222 | } |