Eugene Zelenko | 1d475d8 | 2017-06-16 00:43:26 +0000 | [diff] [blame] | 1 | //===- Option.cpp - Abstract Driver Options -------------------------------===// |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +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 | |
Eugene Zelenko | 1d475d8 | 2017-06-16 00:43:26 +0000 | [diff] [blame] | 10 | #include "llvm/ADT/StringRef.h" |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/Twine.h" |
Nico Weber | 0f38c60 | 2018-04-30 14:59:11 +0000 | [diff] [blame] | 12 | #include "llvm/Config/llvm-config.h" |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 13 | #include "llvm/Option/Arg.h" |
| 14 | #include "llvm/Option/ArgList.h" |
Eugene Zelenko | 1d475d8 | 2017-06-16 00:43:26 +0000 | [diff] [blame] | 15 | #include "llvm/Option/Option.h" |
| 16 | #include "llvm/Option/OptTable.h" |
| 17 | #include "llvm/Support/Compiler.h" |
Eric Christopher | 96e80fc | 2015-12-18 18:55:26 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Debug.h" |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 19 | #include "llvm/Support/ErrorHandling.h" |
Chandler Carruth | 58a2cbe | 2013-01-02 10:22:59 +0000 | [diff] [blame] | 20 | #include "llvm/Support/raw_ostream.h" |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 21 | #include <cassert> |
Eugene Zelenko | 1d475d8 | 2017-06-16 00:43:26 +0000 | [diff] [blame] | 22 | #include <cstring> |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 23 | |
| 24 | using namespace llvm; |
| 25 | using namespace llvm::opt; |
| 26 | |
| 27 | Option::Option(const OptTable::Info *info, const OptTable *owner) |
| 28 | : Info(info), Owner(owner) { |
Hans Wennborg | 6c7e787 | 2013-07-22 16:18:13 +0000 | [diff] [blame] | 29 | // Multi-level aliases are not supported. This just simplifies option |
| 30 | // tracking, it is not an inherent limitation. |
Richard Trieu | 3f60fb8 | 2013-07-22 21:29:28 +0000 | [diff] [blame] | 31 | assert((!Info || !getAlias().isValid() || !getAlias().getAlias().isValid()) && |
Hans Wennborg | 6c7e787 | 2013-07-22 16:18:13 +0000 | [diff] [blame] | 32 | "Multi-level aliases are not supported."); |
Hans Wennborg | 9dd8c0c | 2013-07-31 22:44:41 +0000 | [diff] [blame] | 33 | |
| 34 | if (Info && getAliasArgs()) { |
| 35 | assert(getAlias().isValid() && "Only alias options can have alias args."); |
| 36 | assert(getKind() == FlagClass && "Only Flag aliases can have alias args."); |
| 37 | assert(getAlias().getKind() != FlagClass && |
| 38 | "Cannot provide alias args to a flag option."); |
| 39 | } |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Eric Christopher | 96e80fc | 2015-12-18 18:55:26 +0000 | [diff] [blame] | 42 | void Option::print(raw_ostream &O) const { |
| 43 | O << "<"; |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 44 | switch (getKind()) { |
Eric Christopher | 96e80fc | 2015-12-18 18:55:26 +0000 | [diff] [blame] | 45 | #define P(N) case N: O << #N; break |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 46 | P(GroupClass); |
| 47 | P(InputClass); |
| 48 | P(UnknownClass); |
| 49 | P(FlagClass); |
| 50 | P(JoinedClass); |
Yuka Takahashi | bc5df29 | 2017-06-20 16:31:31 +0000 | [diff] [blame] | 51 | P(ValuesClass); |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 52 | P(SeparateClass); |
| 53 | P(CommaJoinedClass); |
| 54 | P(MultiArgClass); |
| 55 | P(JoinedOrSeparateClass); |
| 56 | P(JoinedAndSeparateClass); |
Hans Wennborg | af9e355 | 2013-08-13 21:09:50 +0000 | [diff] [blame] | 57 | P(RemainingArgsClass); |
Hans Wennborg | 9f34fd5 | 2016-04-15 00:23:30 +0000 | [diff] [blame] | 58 | P(RemainingArgsJoinedClass); |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 59 | #undef P |
| 60 | } |
| 61 | |
Reid Kleckner | b0a9ffa | 2013-06-26 22:43:37 +0000 | [diff] [blame] | 62 | if (Info->Prefixes) { |
Eric Christopher | 96e80fc | 2015-12-18 18:55:26 +0000 | [diff] [blame] | 63 | O << " Prefixes:["; |
| 64 | for (const char *const *Pre = Info->Prefixes; *Pre != nullptr; ++Pre) { |
| 65 | O << '"' << *Pre << (*(Pre + 1) == nullptr ? "\"" : "\", "); |
Reid Kleckner | b0a9ffa | 2013-06-26 22:43:37 +0000 | [diff] [blame] | 66 | } |
Eric Christopher | 96e80fc | 2015-12-18 18:55:26 +0000 | [diff] [blame] | 67 | O << ']'; |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 68 | } |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 69 | |
Eric Christopher | 96e80fc | 2015-12-18 18:55:26 +0000 | [diff] [blame] | 70 | O << " Name:\"" << getName() << '"'; |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 71 | |
| 72 | const Option Group = getGroup(); |
| 73 | if (Group.isValid()) { |
Eric Christopher | 96e80fc | 2015-12-18 18:55:26 +0000 | [diff] [blame] | 74 | O << " Group:"; |
| 75 | Group.print(O); |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | const Option Alias = getAlias(); |
| 79 | if (Alias.isValid()) { |
Eric Christopher | 96e80fc | 2015-12-18 18:55:26 +0000 | [diff] [blame] | 80 | O << " Alias:"; |
| 81 | Alias.print(O); |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | if (getKind() == MultiArgClass) |
Eric Christopher | 96e80fc | 2015-12-18 18:55:26 +0000 | [diff] [blame] | 85 | O << " NumArgs:" << getNumArgs(); |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 86 | |
Eric Christopher | 96e80fc | 2015-12-18 18:55:26 +0000 | [diff] [blame] | 87 | O << ">\n"; |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Aaron Ballman | 1d03d38 | 2017-10-15 14:32:27 +0000 | [diff] [blame] | 90 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Yaron Keren | 5530798 | 2016-01-29 20:50:44 +0000 | [diff] [blame] | 91 | LLVM_DUMP_METHOD void Option::dump() const { print(dbgs()); } |
Matthias Braun | 88d2075 | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 92 | #endif |
Eric Christopher | 96e80fc | 2015-12-18 18:55:26 +0000 | [diff] [blame] | 93 | |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 94 | bool Option::matches(OptSpecifier Opt) const { |
| 95 | // Aliases are never considered in matching, look through them. |
| 96 | const Option Alias = getAlias(); |
| 97 | if (Alias.isValid()) |
| 98 | return Alias.matches(Opt); |
| 99 | |
| 100 | // Check exact match. |
| 101 | if (getID() == Opt.getID()) |
| 102 | return true; |
| 103 | |
| 104 | const Option Group = getGroup(); |
| 105 | if (Group.isValid()) |
| 106 | return Group.matches(Opt); |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | Arg *Option::accept(const ArgList &Args, |
| 111 | unsigned &Index, |
| 112 | unsigned ArgSize) const { |
| 113 | const Option &UnaliasedOption = getUnaliasedOption(); |
| 114 | StringRef Spelling; |
| 115 | // If the option was an alias, get the spelling from the unaliased one. |
| 116 | if (getID() == UnaliasedOption.getID()) { |
| 117 | Spelling = StringRef(Args.getArgString(Index), ArgSize); |
| 118 | } else { |
| 119 | Spelling = Args.MakeArgString(Twine(UnaliasedOption.getPrefix()) + |
| 120 | Twine(UnaliasedOption.getName())); |
| 121 | } |
| 122 | |
| 123 | switch (getKind()) { |
Hans Wennborg | 9dd8c0c | 2013-07-31 22:44:41 +0000 | [diff] [blame] | 124 | case FlagClass: { |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 125 | if (ArgSize != strlen(Args.getArgString(Index))) |
Craig Topper | 0b6cb71 | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 126 | return nullptr; |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 127 | |
Hans Wennborg | 9dd8c0c | 2013-07-31 22:44:41 +0000 | [diff] [blame] | 128 | Arg *A = new Arg(UnaliasedOption, Spelling, Index++); |
| 129 | if (getAliasArgs()) { |
| 130 | const char *Val = getAliasArgs(); |
| 131 | while (*Val != '\0') { |
| 132 | A->getValues().push_back(Val); |
| 133 | |
| 134 | // Move past the '\0' to the next argument. |
| 135 | Val += strlen(Val) + 1; |
| 136 | } |
| 137 | } |
Hans Wennborg | 375079a | 2015-05-04 18:00:13 +0000 | [diff] [blame] | 138 | |
| 139 | if (UnaliasedOption.getKind() == JoinedClass && !getAliasArgs()) |
| 140 | // A Flag alias for a Joined option must provide an argument. |
| 141 | A->getValues().push_back(""); |
| 142 | |
Hans Wennborg | 9dd8c0c | 2013-07-31 22:44:41 +0000 | [diff] [blame] | 143 | return A; |
| 144 | } |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 145 | case JoinedClass: { |
| 146 | const char *Value = Args.getArgString(Index) + ArgSize; |
| 147 | return new Arg(UnaliasedOption, Spelling, Index++, Value); |
| 148 | } |
| 149 | case CommaJoinedClass: { |
| 150 | // Always matches. |
| 151 | const char *Str = Args.getArgString(Index) + ArgSize; |
| 152 | Arg *A = new Arg(UnaliasedOption, Spelling, Index++); |
| 153 | |
| 154 | // Parse out the comma separated values. |
| 155 | const char *Prev = Str; |
| 156 | for (;; ++Str) { |
| 157 | char c = *Str; |
| 158 | |
| 159 | if (!c || c == ',') { |
| 160 | if (Prev != Str) { |
| 161 | char *Value = new char[Str - Prev + 1]; |
| 162 | memcpy(Value, Prev, Str - Prev); |
| 163 | Value[Str - Prev] = '\0'; |
| 164 | A->getValues().push_back(Value); |
| 165 | } |
| 166 | |
| 167 | if (!c) |
| 168 | break; |
| 169 | |
| 170 | Prev = Str + 1; |
| 171 | } |
| 172 | } |
| 173 | A->setOwnsValues(true); |
| 174 | |
| 175 | return A; |
| 176 | } |
| 177 | case SeparateClass: |
| 178 | // Matches iff this is an exact match. |
| 179 | // FIXME: Avoid strlen. |
| 180 | if (ArgSize != strlen(Args.getArgString(Index))) |
Craig Topper | 0b6cb71 | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 181 | return nullptr; |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 182 | |
| 183 | Index += 2; |
Reid Kleckner | 2e1bf78 | 2014-08-22 19:29:17 +0000 | [diff] [blame] | 184 | if (Index > Args.getNumInputArgStrings() || |
| 185 | Args.getArgString(Index - 1) == nullptr) |
Craig Topper | 0b6cb71 | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 186 | return nullptr; |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 187 | |
| 188 | return new Arg(UnaliasedOption, Spelling, |
| 189 | Index - 2, Args.getArgString(Index - 1)); |
| 190 | case MultiArgClass: { |
| 191 | // Matches iff this is an exact match. |
| 192 | // FIXME: Avoid strlen. |
| 193 | if (ArgSize != strlen(Args.getArgString(Index))) |
Craig Topper | 0b6cb71 | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 194 | return nullptr; |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 195 | |
| 196 | Index += 1 + getNumArgs(); |
| 197 | if (Index > Args.getNumInputArgStrings()) |
Craig Topper | 0b6cb71 | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 198 | return nullptr; |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 199 | |
| 200 | Arg *A = new Arg(UnaliasedOption, Spelling, Index - 1 - getNumArgs(), |
| 201 | Args.getArgString(Index - getNumArgs())); |
| 202 | for (unsigned i = 1; i != getNumArgs(); ++i) |
| 203 | A->getValues().push_back(Args.getArgString(Index - getNumArgs() + i)); |
| 204 | return A; |
| 205 | } |
| 206 | case JoinedOrSeparateClass: { |
| 207 | // If this is not an exact match, it is a joined arg. |
| 208 | // FIXME: Avoid strlen. |
| 209 | if (ArgSize != strlen(Args.getArgString(Index))) { |
| 210 | const char *Value = Args.getArgString(Index) + ArgSize; |
| 211 | return new Arg(*this, Spelling, Index++, Value); |
| 212 | } |
| 213 | |
| 214 | // Otherwise it must be separate. |
| 215 | Index += 2; |
Reid Kleckner | 2e1bf78 | 2014-08-22 19:29:17 +0000 | [diff] [blame] | 216 | if (Index > Args.getNumInputArgStrings() || |
| 217 | Args.getArgString(Index - 1) == nullptr) |
Craig Topper | 0b6cb71 | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 218 | return nullptr; |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 219 | |
| 220 | return new Arg(UnaliasedOption, Spelling, |
| 221 | Index - 2, Args.getArgString(Index - 1)); |
| 222 | } |
| 223 | case JoinedAndSeparateClass: |
| 224 | // Always matches. |
| 225 | Index += 2; |
Reid Kleckner | 2e1bf78 | 2014-08-22 19:29:17 +0000 | [diff] [blame] | 226 | if (Index > Args.getNumInputArgStrings() || |
| 227 | Args.getArgString(Index - 1) == nullptr) |
Craig Topper | 0b6cb71 | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 228 | return nullptr; |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 229 | |
| 230 | return new Arg(UnaliasedOption, Spelling, Index - 2, |
| 231 | Args.getArgString(Index - 2) + ArgSize, |
| 232 | Args.getArgString(Index - 1)); |
Hans Wennborg | af9e355 | 2013-08-13 21:09:50 +0000 | [diff] [blame] | 233 | case RemainingArgsClass: { |
| 234 | // Matches iff this is an exact match. |
| 235 | // FIXME: Avoid strlen. |
| 236 | if (ArgSize != strlen(Args.getArgString(Index))) |
Craig Topper | 0b6cb71 | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 237 | return nullptr; |
Hans Wennborg | af9e355 | 2013-08-13 21:09:50 +0000 | [diff] [blame] | 238 | Arg *A = new Arg(UnaliasedOption, Spelling, Index++); |
Reid Kleckner | 2e1bf78 | 2014-08-22 19:29:17 +0000 | [diff] [blame] | 239 | while (Index < Args.getNumInputArgStrings() && |
| 240 | Args.getArgString(Index) != nullptr) |
Hans Wennborg | af9e355 | 2013-08-13 21:09:50 +0000 | [diff] [blame] | 241 | A->getValues().push_back(Args.getArgString(Index++)); |
| 242 | return A; |
| 243 | } |
Hans Wennborg | 9f34fd5 | 2016-04-15 00:23:30 +0000 | [diff] [blame] | 244 | case RemainingArgsJoinedClass: { |
| 245 | Arg *A = new Arg(UnaliasedOption, Spelling, Index); |
| 246 | if (ArgSize != strlen(Args.getArgString(Index))) { |
| 247 | // An inexact match means there is a joined arg. |
| 248 | A->getValues().push_back(Args.getArgString(Index) + ArgSize); |
| 249 | } |
| 250 | Index++; |
| 251 | while (Index < Args.getNumInputArgStrings() && |
| 252 | Args.getArgString(Index) != nullptr) |
| 253 | A->getValues().push_back(Args.getArgString(Index++)); |
| 254 | return A; |
| 255 | } |
| 256 | |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 257 | default: |
| 258 | llvm_unreachable("Invalid option kind!"); |
| 259 | } |
| 260 | } |