Daniel Dunbar | 1eb4e64 | 2009-03-03 05:55:11 +0000 | [diff] [blame] | 1 | //===--- Option.cpp - Abstract Driver Options ---------------------------*-===// |
| 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/Option.h" |
Daniel Dunbar | bbf842b | 2009-03-04 23:22:02 +0000 | [diff] [blame] | 11 | |
| 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 "llvm/Support/raw_ostream.h" |
Daniel Dunbar | 1eb4e64 | 2009-03-03 05:55:11 +0000 | [diff] [blame] | 15 | #include <cassert> |
Daniel Dunbar | bbf842b | 2009-03-04 23:22:02 +0000 | [diff] [blame] | 16 | #include <algorithm> |
Daniel Dunbar | 1eb4e64 | 2009-03-03 05:55:11 +0000 | [diff] [blame] | 17 | using namespace clang::driver; |
| 18 | |
Daniel Dunbar | 9e1f982 | 2009-11-19 04:14:53 +0000 | [diff] [blame] | 19 | Option::Option(OptionClass _Kind, OptSpecifier _ID, const char *_Name, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 20 | const OptionGroup *_Group, const Option *_Alias) |
Daniel Dunbar | 9e1f982 | 2009-11-19 04:14:53 +0000 | [diff] [blame] | 21 | : Kind(_Kind), ID(_ID.getID()), Name(_Name), Group(_Group), Alias(_Alias), |
Daniel Dunbar | 0f9098e | 2009-03-04 21:05:23 +0000 | [diff] [blame] | 22 | Unsupported(false), LinkerInput(false), NoOptAsInput(false), |
Daniel Dunbar | 6d954d7 | 2009-03-18 08:01:15 +0000 | [diff] [blame] | 23 | ForceSeparateRender(false), ForceJoinedRender(false), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 24 | DriverOption(false), NoArgumentUnused(false) { |
Daniel Dunbar | 1eb4e64 | 2009-03-03 05:55:11 +0000 | [diff] [blame] | 25 | |
| 26 | // Multi-level aliases are not supported, and alias options cannot |
| 27 | // have groups. This just simplifies option tracking, it is not an |
| 28 | // inherent limitation. |
| 29 | assert((!Alias || (!Alias->Alias && !Group)) && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 30 | "Multi-level aliases and aliases with groups are unsupported."); |
Daniel Dunbar | 1eb4e64 | 2009-03-03 05:55:11 +0000 | [diff] [blame] | 31 | } |
| 32 | |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 33 | Option::~Option() { |
| 34 | } |
| 35 | |
| 36 | void Option::dump() const { |
| 37 | llvm::errs() << "<"; |
| 38 | switch (Kind) { |
| 39 | default: |
| 40 | assert(0 && "Invalid kind"); |
| 41 | #define P(N) case N: llvm::errs() << #N; break |
| 42 | P(GroupClass); |
| 43 | P(InputClass); |
| 44 | P(UnknownClass); |
| 45 | P(FlagClass); |
| 46 | P(JoinedClass); |
| 47 | P(SeparateClass); |
| 48 | P(CommaJoinedClass); |
| 49 | P(MultiArgClass); |
| 50 | P(JoinedOrSeparateClass); |
| 51 | P(JoinedAndSeparateClass); |
| 52 | #undef P |
| 53 | } |
| 54 | |
| 55 | llvm::errs() << " Name:\"" << Name << '"'; |
| 56 | |
| 57 | if (Group) { |
| 58 | llvm::errs() << " Group:"; |
| 59 | Group->dump(); |
| 60 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 61 | |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 62 | if (Alias) { |
| 63 | llvm::errs() << " Alias:"; |
| 64 | Alias->dump(); |
| 65 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 66 | |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 67 | if (const MultiArgOption *MOA = dyn_cast<MultiArgOption>(this)) |
| 68 | llvm::errs() << " NumArgs:" << MOA->getNumArgs(); |
| 69 | |
| 70 | llvm::errs() << ">\n"; |
| 71 | } |
| 72 | |
Daniel Dunbar | 9e1f982 | 2009-11-19 04:14:53 +0000 | [diff] [blame] | 73 | bool Option::matches(OptSpecifier Opt) const { |
Daniel Dunbar | b32aa51 | 2009-11-19 03:26:50 +0000 | [diff] [blame] | 74 | // Aliases are never considered in matching, look through them. |
Daniel Dunbar | 1eb4e64 | 2009-03-03 05:55:11 +0000 | [diff] [blame] | 75 | if (Alias) |
| 76 | return Alias->matches(Opt); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 77 | |
Daniel Dunbar | 9e1f982 | 2009-11-19 04:14:53 +0000 | [diff] [blame] | 78 | // Check exact match. |
| 79 | if (ID == Opt) |
Daniel Dunbar | 1eb4e64 | 2009-03-03 05:55:11 +0000 | [diff] [blame] | 80 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 81 | |
Daniel Dunbar | 1eb4e64 | 2009-03-03 05:55:11 +0000 | [diff] [blame] | 82 | if (Group) |
| 83 | return Group->matches(Opt); |
| 84 | return false; |
| 85 | } |
| 86 | |
Daniel Dunbar | 9e1f982 | 2009-11-19 04:14:53 +0000 | [diff] [blame] | 87 | OptionGroup::OptionGroup(OptSpecifier ID, const char *Name, |
Daniel Dunbar | 30b055f | 2009-03-04 21:53:04 +0000 | [diff] [blame] | 88 | const OptionGroup *Group) |
| 89 | : Option(Option::GroupClass, ID, Name, Group, 0) { |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Daniel Dunbar | f3cad36 | 2009-03-25 04:13:45 +0000 | [diff] [blame] | 92 | Arg *OptionGroup::accept(const InputArgList &Args, unsigned &Index) const { |
Daniel Dunbar | b0c4df5 | 2009-03-22 23:26:43 +0000 | [diff] [blame] | 93 | assert(0 && "accept() should never be called on an OptionGroup"); |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 94 | return 0; |
Daniel Dunbar | 1eb4e64 | 2009-03-03 05:55:11 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Daniel Dunbar | a0289fd | 2009-11-19 04:25:06 +0000 | [diff] [blame] | 97 | InputOption::InputOption(OptSpecifier ID) |
| 98 | : Option(Option::InputClass, ID, "<input>", 0, 0) { |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Daniel Dunbar | f3cad36 | 2009-03-25 04:13:45 +0000 | [diff] [blame] | 101 | Arg *InputOption::accept(const InputArgList &Args, unsigned &Index) const { |
Daniel Dunbar | b0c4df5 | 2009-03-22 23:26:43 +0000 | [diff] [blame] | 102 | assert(0 && "accept() should never be called on an InputOption"); |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 103 | return 0; |
Daniel Dunbar | 1eb4e64 | 2009-03-03 05:55:11 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Daniel Dunbar | a0289fd | 2009-11-19 04:25:06 +0000 | [diff] [blame] | 106 | UnknownOption::UnknownOption(OptSpecifier ID) |
| 107 | : Option(Option::UnknownClass, ID, "<unknown>", 0, 0) { |
Daniel Dunbar | 1eb4e64 | 2009-03-03 05:55:11 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Daniel Dunbar | f3cad36 | 2009-03-25 04:13:45 +0000 | [diff] [blame] | 110 | Arg *UnknownOption::accept(const InputArgList &Args, unsigned &Index) const { |
Daniel Dunbar | b0c4df5 | 2009-03-22 23:26:43 +0000 | [diff] [blame] | 111 | assert(0 && "accept() should never be called on an UnknownOption"); |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 112 | return 0; |
Daniel Dunbar | 1eb4e64 | 2009-03-03 05:55:11 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Daniel Dunbar | 9e1f982 | 2009-11-19 04:14:53 +0000 | [diff] [blame] | 115 | FlagOption::FlagOption(OptSpecifier ID, const char *Name, |
Daniel Dunbar | 30b055f | 2009-03-04 21:53:04 +0000 | [diff] [blame] | 116 | const OptionGroup *Group, const Option *Alias) |
| 117 | : Option(Option::FlagClass, ID, Name, Group, Alias) { |
Daniel Dunbar | 1eb4e64 | 2009-03-03 05:55:11 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Daniel Dunbar | f3cad36 | 2009-03-25 04:13:45 +0000 | [diff] [blame] | 120 | Arg *FlagOption::accept(const InputArgList &Args, unsigned &Index) const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 121 | // Matches iff this is an exact match. |
Daniel Dunbar | bbf842b | 2009-03-04 23:22:02 +0000 | [diff] [blame] | 122 | // FIXME: Avoid strlen. |
| 123 | if (strlen(getName()) != strlen(Args.getArgString(Index))) |
| 124 | return 0; |
| 125 | |
Daniel Dunbar | 0648262 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 126 | return new FlagArg(this, Index++); |
Daniel Dunbar | 1eb4e64 | 2009-03-03 05:55:11 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Daniel Dunbar | 9e1f982 | 2009-11-19 04:14:53 +0000 | [diff] [blame] | 129 | JoinedOption::JoinedOption(OptSpecifier ID, const char *Name, |
Daniel Dunbar | 30b055f | 2009-03-04 21:53:04 +0000 | [diff] [blame] | 130 | const OptionGroup *Group, const Option *Alias) |
| 131 | : Option(Option::JoinedClass, ID, Name, Group, Alias) { |
Daniel Dunbar | 1eb4e64 | 2009-03-03 05:55:11 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Daniel Dunbar | f3cad36 | 2009-03-25 04:13:45 +0000 | [diff] [blame] | 134 | Arg *JoinedOption::accept(const InputArgList &Args, unsigned &Index) const { |
Daniel Dunbar | bbf842b | 2009-03-04 23:22:02 +0000 | [diff] [blame] | 135 | // Always matches. |
| 136 | return new JoinedArg(this, Index++); |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Daniel Dunbar | 9e1f982 | 2009-11-19 04:14:53 +0000 | [diff] [blame] | 139 | CommaJoinedOption::CommaJoinedOption(OptSpecifier ID, const char *Name, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 140 | const OptionGroup *Group, |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 141 | const Option *Alias) |
Daniel Dunbar | 30b055f | 2009-03-04 21:53:04 +0000 | [diff] [blame] | 142 | : Option(Option::CommaJoinedClass, ID, Name, Group, Alias) { |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 145 | Arg *CommaJoinedOption::accept(const InputArgList &Args, |
Daniel Dunbar | f3cad36 | 2009-03-25 04:13:45 +0000 | [diff] [blame] | 146 | unsigned &Index) const { |
Daniel Dunbar | bbf842b | 2009-03-04 23:22:02 +0000 | [diff] [blame] | 147 | // Always matches. We count the commas now so we can answer |
| 148 | // getNumValues easily. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 149 | |
Daniel Dunbar | bbf842b | 2009-03-04 23:22:02 +0000 | [diff] [blame] | 150 | // Get the suffix string. |
| 151 | // FIXME: Avoid strlen, and move to helper method? |
| 152 | const char *Suffix = Args.getArgString(Index) + strlen(getName()); |
Daniel Dunbar | 0648262 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 153 | return new CommaJoinedArg(this, Index++, Suffix); |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Daniel Dunbar | 9e1f982 | 2009-11-19 04:14:53 +0000 | [diff] [blame] | 156 | SeparateOption::SeparateOption(OptSpecifier ID, const char *Name, |
Daniel Dunbar | 30b055f | 2009-03-04 21:53:04 +0000 | [diff] [blame] | 157 | const OptionGroup *Group, const Option *Alias) |
| 158 | : Option(Option::SeparateClass, ID, Name, Group, Alias) { |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Daniel Dunbar | f3cad36 | 2009-03-25 04:13:45 +0000 | [diff] [blame] | 161 | Arg *SeparateOption::accept(const InputArgList &Args, unsigned &Index) const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 162 | // Matches iff this is an exact match. |
Daniel Dunbar | bbf842b | 2009-03-04 23:22:02 +0000 | [diff] [blame] | 163 | // FIXME: Avoid strlen. |
| 164 | if (strlen(getName()) != strlen(Args.getArgString(Index))) |
| 165 | return 0; |
| 166 | |
Daniel Dunbar | bbf842b | 2009-03-04 23:22:02 +0000 | [diff] [blame] | 167 | Index += 2; |
Daniel Dunbar | b0c4df5 | 2009-03-22 23:26:43 +0000 | [diff] [blame] | 168 | if (Index > Args.getNumInputArgStrings()) |
| 169 | return 0; |
| 170 | |
Daniel Dunbar | bbf842b | 2009-03-04 23:22:02 +0000 | [diff] [blame] | 171 | return new SeparateArg(this, Index - 2, 1); |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Daniel Dunbar | 9e1f982 | 2009-11-19 04:14:53 +0000 | [diff] [blame] | 174 | MultiArgOption::MultiArgOption(OptSpecifier ID, const char *Name, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 175 | const OptionGroup *Group, const Option *Alias, |
Daniel Dunbar | 30b055f | 2009-03-04 21:53:04 +0000 | [diff] [blame] | 176 | unsigned _NumArgs) |
| 177 | : Option(Option::MultiArgClass, ID, Name, Group, Alias), NumArgs(_NumArgs) { |
Daniel Dunbar | 644eade | 2009-03-12 05:46:32 +0000 | [diff] [blame] | 178 | assert(NumArgs > 1 && "Invalid MultiArgOption!"); |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Daniel Dunbar | f3cad36 | 2009-03-25 04:13:45 +0000 | [diff] [blame] | 181 | Arg *MultiArgOption::accept(const InputArgList &Args, unsigned &Index) const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 182 | // Matches iff this is an exact match. |
Daniel Dunbar | bbf842b | 2009-03-04 23:22:02 +0000 | [diff] [blame] | 183 | // FIXME: Avoid strlen. |
| 184 | if (strlen(getName()) != strlen(Args.getArgString(Index))) |
| 185 | return 0; |
| 186 | |
Daniel Dunbar | bbf842b | 2009-03-04 23:22:02 +0000 | [diff] [blame] | 187 | Index += 1 + NumArgs; |
Daniel Dunbar | b0c4df5 | 2009-03-22 23:26:43 +0000 | [diff] [blame] | 188 | if (Index > Args.getNumInputArgStrings()) |
| 189 | return 0; |
| 190 | |
Daniel Dunbar | bbf842b | 2009-03-04 23:22:02 +0000 | [diff] [blame] | 191 | return new SeparateArg(this, Index - 1 - NumArgs, NumArgs); |
Daniel Dunbar | 1eb4e64 | 2009-03-03 05:55:11 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Daniel Dunbar | 9e1f982 | 2009-11-19 04:14:53 +0000 | [diff] [blame] | 194 | JoinedOrSeparateOption::JoinedOrSeparateOption(OptSpecifier ID, const char *Name, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 195 | const OptionGroup *Group, |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 196 | const Option *Alias) |
Daniel Dunbar | 30b055f | 2009-03-04 21:53:04 +0000 | [diff] [blame] | 197 | : Option(Option::JoinedOrSeparateClass, ID, Name, Group, Alias) { |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 200 | Arg *JoinedOrSeparateOption::accept(const InputArgList &Args, |
Daniel Dunbar | b0c4df5 | 2009-03-22 23:26:43 +0000 | [diff] [blame] | 201 | unsigned &Index) const { |
Daniel Dunbar | bbf842b | 2009-03-04 23:22:02 +0000 | [diff] [blame] | 202 | // If this is not an exact match, it is a joined arg. |
| 203 | // FIXME: Avoid strlen. |
| 204 | if (strlen(getName()) != strlen(Args.getArgString(Index))) |
| 205 | return new JoinedArg(this, Index++); |
| 206 | |
| 207 | // Otherwise it must be separate. |
Daniel Dunbar | bbf842b | 2009-03-04 23:22:02 +0000 | [diff] [blame] | 208 | Index += 2; |
Daniel Dunbar | b0c4df5 | 2009-03-22 23:26:43 +0000 | [diff] [blame] | 209 | if (Index > Args.getNumInputArgStrings()) |
| 210 | return 0; |
| 211 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 212 | return new SeparateArg(this, Index - 2, 1); |
Daniel Dunbar | 1eb4e64 | 2009-03-03 05:55:11 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Daniel Dunbar | 9e1f982 | 2009-11-19 04:14:53 +0000 | [diff] [blame] | 215 | JoinedAndSeparateOption::JoinedAndSeparateOption(OptSpecifier ID, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 216 | const char *Name, |
| 217 | const OptionGroup *Group, |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 218 | const Option *Alias) |
Daniel Dunbar | 30b055f | 2009-03-04 21:53:04 +0000 | [diff] [blame] | 219 | : Option(Option::JoinedAndSeparateClass, ID, Name, Group, Alias) { |
Daniel Dunbar | 1eb4e64 | 2009-03-03 05:55:11 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 222 | Arg *JoinedAndSeparateOption::accept(const InputArgList &Args, |
Daniel Dunbar | b0c4df5 | 2009-03-22 23:26:43 +0000 | [diff] [blame] | 223 | unsigned &Index) const { |
Daniel Dunbar | bbf842b | 2009-03-04 23:22:02 +0000 | [diff] [blame] | 224 | // Always matches. |
| 225 | |
Daniel Dunbar | bbf842b | 2009-03-04 23:22:02 +0000 | [diff] [blame] | 226 | Index += 2; |
Daniel Dunbar | b0c4df5 | 2009-03-22 23:26:43 +0000 | [diff] [blame] | 227 | if (Index > Args.getNumInputArgStrings()) |
| 228 | return 0; |
| 229 | |
Daniel Dunbar | bbf842b | 2009-03-04 23:22:02 +0000 | [diff] [blame] | 230 | return new JoinedAndSeparateArg(this, Index - 2); |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 231 | } |
Daniel Dunbar | 1eb4e64 | 2009-03-03 05:55:11 +0000 | [diff] [blame] | 232 | |