Nick Lewycky | 3fdcc6f | 2010-12-31 17:31:54 +0000 | [diff] [blame] | 1 | //===--- Option.cpp - Abstract Driver Options -----------------------------===// |
Daniel Dunbar | 1eb4e64 | 2009-03-03 05:55:11 +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 | |
| 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" |
David Blaikie | 548f6c8 | 2011-09-23 05:57:42 +0000 | [diff] [blame] | 15 | #include "llvm/Support/ErrorHandling.h" |
Daniel Dunbar | 1eb4e64 | 2009-03-03 05:55:11 +0000 | [diff] [blame] | 16 | #include <cassert> |
Daniel Dunbar | bbf842b | 2009-03-04 23:22:02 +0000 | [diff] [blame] | 17 | #include <algorithm> |
Daniel Dunbar | 1eb4e64 | 2009-03-03 05:55:11 +0000 | [diff] [blame] | 18 | using namespace clang::driver; |
| 19 | |
Michael J. Spencer | 9b7dcdb | 2012-10-03 19:58:10 +0000 | [diff] [blame] | 20 | Option::Option(const OptTable::Info *info, const OptTable *owner) |
| 21 | : Info(info), Owner(owner) { |
Daniel Dunbar | 1eb4e64 | 2009-03-03 05:55:11 +0000 | [diff] [blame] | 22 | |
| 23 | // Multi-level aliases are not supported, and alias options cannot |
| 24 | // have groups. This just simplifies option tracking, it is not an |
| 25 | // inherent limitation. |
Michael J. Spencer | 0464fd5 | 2012-10-10 21:48:26 +0000 | [diff] [blame^] | 26 | assert((!getAlias().isValid() || (!getAlias().getAlias().isValid() && |
| 27 | !getGroup().isValid())) && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 28 | "Multi-level aliases and aliases with groups are unsupported."); |
Daniel Dunbar | 1eb4e64 | 2009-03-03 05:55:11 +0000 | [diff] [blame] | 29 | } |
| 30 | |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 31 | Option::~Option() { |
| 32 | } |
| 33 | |
| 34 | void Option::dump() const { |
| 35 | llvm::errs() << "<"; |
Michael J. Spencer | 04a4279 | 2012-08-21 18:51:17 +0000 | [diff] [blame] | 36 | switch (getKind()) { |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 37 | #define P(N) case N: llvm::errs() << #N; break |
| 38 | P(GroupClass); |
| 39 | P(InputClass); |
| 40 | P(UnknownClass); |
| 41 | P(FlagClass); |
| 42 | P(JoinedClass); |
| 43 | P(SeparateClass); |
| 44 | P(CommaJoinedClass); |
| 45 | P(MultiArgClass); |
| 46 | P(JoinedOrSeparateClass); |
| 47 | P(JoinedAndSeparateClass); |
| 48 | #undef P |
| 49 | } |
| 50 | |
Michael J. Spencer | 04a4279 | 2012-08-21 18:51:17 +0000 | [diff] [blame] | 51 | llvm::errs() << " Name:\"" << getName() << '"'; |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 52 | |
Michael J. Spencer | 0464fd5 | 2012-10-10 21:48:26 +0000 | [diff] [blame^] | 53 | const Option Group = getGroup(); |
| 54 | if (Group.isValid()) { |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 55 | llvm::errs() << " Group:"; |
Michael J. Spencer | 0464fd5 | 2012-10-10 21:48:26 +0000 | [diff] [blame^] | 56 | Group.dump(); |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 57 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 58 | |
Michael J. Spencer | 0464fd5 | 2012-10-10 21:48:26 +0000 | [diff] [blame^] | 59 | const Option Alias = getAlias(); |
| 60 | if (Alias.isValid()) { |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 61 | llvm::errs() << " Alias:"; |
Michael J. Spencer | 0464fd5 | 2012-10-10 21:48:26 +0000 | [diff] [blame^] | 62 | Alias.dump(); |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 63 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 64 | |
Michael J. Spencer | 04a4279 | 2012-08-21 18:51:17 +0000 | [diff] [blame] | 65 | if (getKind() == MultiArgClass) |
| 66 | llvm::errs() << " NumArgs:" << getNumArgs(); |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 67 | |
| 68 | llvm::errs() << ">\n"; |
| 69 | } |
| 70 | |
Daniel Dunbar | 9e1f982 | 2009-11-19 04:14:53 +0000 | [diff] [blame] | 71 | bool Option::matches(OptSpecifier Opt) const { |
Daniel Dunbar | b32aa51 | 2009-11-19 03:26:50 +0000 | [diff] [blame] | 72 | // Aliases are never considered in matching, look through them. |
Michael J. Spencer | 0464fd5 | 2012-10-10 21:48:26 +0000 | [diff] [blame^] | 73 | const Option Alias = getAlias(); |
| 74 | if (Alias.isValid()) |
| 75 | return Alias.matches(Opt); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 76 | |
Daniel Dunbar | 9e1f982 | 2009-11-19 04:14:53 +0000 | [diff] [blame] | 77 | // Check exact match. |
Michael J. Spencer | 663117a | 2012-09-25 23:12:48 +0000 | [diff] [blame] | 78 | if (getID() == Opt.getID()) |
Daniel Dunbar | 1eb4e64 | 2009-03-03 05:55:11 +0000 | [diff] [blame] | 79 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 80 | |
Michael J. Spencer | 0464fd5 | 2012-10-10 21:48:26 +0000 | [diff] [blame^] | 81 | const Option Group = getGroup(); |
| 82 | if (Group.isValid()) |
| 83 | return Group.matches(Opt); |
Daniel Dunbar | 1eb4e64 | 2009-03-03 05:55:11 +0000 | [diff] [blame] | 84 | return false; |
| 85 | } |
| 86 | |
Michael J. Spencer | 4327557 | 2012-08-20 21:41:17 +0000 | [diff] [blame] | 87 | Arg *Option::accept(const ArgList &Args, unsigned &Index) const { |
Michael J. Spencer | 04a4279 | 2012-08-21 18:51:17 +0000 | [diff] [blame] | 88 | switch (getKind()) { |
Michael J. Spencer | 4327557 | 2012-08-20 21:41:17 +0000 | [diff] [blame] | 89 | case FlagClass: |
| 90 | if (getName().size() != strlen(Args.getArgString(Index))) |
| 91 | return 0; |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 92 | |
Michael J. Spencer | 4327557 | 2012-08-20 21:41:17 +0000 | [diff] [blame] | 93 | return new Arg(getUnaliasedOption(), Index++); |
| 94 | case JoinedClass: { |
Douglas Gregor | bcf6a80 | 2011-07-05 16:56:25 +0000 | [diff] [blame] | 95 | const char *Value = Args.getArgString(Index) + getName().size(); |
Michael J. Spencer | 4327557 | 2012-08-20 21:41:17 +0000 | [diff] [blame] | 96 | return new Arg(getUnaliasedOption(), Index++, Value); |
Daniel Dunbar | 4465a77 | 2010-06-09 22:31:00 +0000 | [diff] [blame] | 97 | } |
Michael J. Spencer | 4327557 | 2012-08-20 21:41:17 +0000 | [diff] [blame] | 98 | case CommaJoinedClass: { |
| 99 | // Always matches. |
| 100 | const char *Str = Args.getArgString(Index) + getName().size(); |
| 101 | Arg *A = new Arg(getUnaliasedOption(), Index++); |
Daniel Dunbar | bbf842b | 2009-03-04 23:22:02 +0000 | [diff] [blame] | 102 | |
Michael J. Spencer | 4327557 | 2012-08-20 21:41:17 +0000 | [diff] [blame] | 103 | // Parse out the comma separated values. |
| 104 | const char *Prev = Str; |
| 105 | for (;; ++Str) { |
| 106 | char c = *Str; |
Daniel Dunbar | b0c4df5 | 2009-03-22 23:26:43 +0000 | [diff] [blame] | 107 | |
Michael J. Spencer | 4327557 | 2012-08-20 21:41:17 +0000 | [diff] [blame] | 108 | if (!c || c == ',') { |
| 109 | if (Prev != Str) { |
| 110 | char *Value = new char[Str - Prev + 1]; |
| 111 | memcpy(Value, Prev, Str - Prev); |
| 112 | Value[Str - Prev] = '\0'; |
| 113 | A->getValues().push_back(Value); |
| 114 | } |
Daniel Dunbar | 1eb4e64 | 2009-03-03 05:55:11 +0000 | [diff] [blame] | 115 | |
Michael J. Spencer | 4327557 | 2012-08-20 21:41:17 +0000 | [diff] [blame] | 116 | if (!c) |
| 117 | break; |
Daniel Dunbar | 1eb4e64 | 2009-03-03 05:55:11 +0000 | [diff] [blame] | 118 | |
Michael J. Spencer | 4327557 | 2012-08-20 21:41:17 +0000 | [diff] [blame] | 119 | Prev = Str + 1; |
| 120 | } |
| 121 | } |
| 122 | A->setOwnsValues(true); |
Daniel Dunbar | bbf842b | 2009-03-04 23:22:02 +0000 | [diff] [blame] | 123 | |
Michael J. Spencer | 4327557 | 2012-08-20 21:41:17 +0000 | [diff] [blame] | 124 | return A; |
| 125 | } |
| 126 | case SeparateClass: |
Sylvestre Ledru | f3477c1 | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 127 | // Matches iff this is an exact match. |
Michael J. Spencer | 4327557 | 2012-08-20 21:41:17 +0000 | [diff] [blame] | 128 | // FIXME: Avoid strlen. |
| 129 | if (getName().size() != strlen(Args.getArgString(Index))) |
| 130 | return 0; |
Daniel Dunbar | b0c4df5 | 2009-03-22 23:26:43 +0000 | [diff] [blame] | 131 | |
Michael J. Spencer | 4327557 | 2012-08-20 21:41:17 +0000 | [diff] [blame] | 132 | Index += 2; |
| 133 | if (Index > Args.getNumInputArgStrings()) |
| 134 | return 0; |
| 135 | |
| 136 | return new Arg(getUnaliasedOption(), |
| 137 | Index - 2, Args.getArgString(Index - 1)); |
| 138 | case MultiArgClass: { |
Sylvestre Ledru | f3477c1 | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 139 | // Matches iff this is an exact match. |
Michael J. Spencer | 4327557 | 2012-08-20 21:41:17 +0000 | [diff] [blame] | 140 | // FIXME: Avoid strlen. |
| 141 | if (getName().size() != strlen(Args.getArgString(Index))) |
| 142 | return 0; |
| 143 | |
Michael J. Spencer | 04a4279 | 2012-08-21 18:51:17 +0000 | [diff] [blame] | 144 | Index += 1 + getNumArgs(); |
Michael J. Spencer | 4327557 | 2012-08-20 21:41:17 +0000 | [diff] [blame] | 145 | if (Index > Args.getNumInputArgStrings()) |
| 146 | return 0; |
| 147 | |
Michael J. Spencer | 04a4279 | 2012-08-21 18:51:17 +0000 | [diff] [blame] | 148 | Arg *A = new Arg(getUnaliasedOption(), Index - 1 - getNumArgs(), |
| 149 | Args.getArgString(Index - getNumArgs())); |
| 150 | for (unsigned i = 1; i != getNumArgs(); ++i) |
| 151 | A->getValues().push_back(Args.getArgString(Index - getNumArgs() + i)); |
Michael J. Spencer | 4327557 | 2012-08-20 21:41:17 +0000 | [diff] [blame] | 152 | return A; |
| 153 | } |
| 154 | case JoinedOrSeparateClass: { |
| 155 | // If this is not an exact match, it is a joined arg. |
| 156 | // FIXME: Avoid strlen. |
| 157 | if (getName().size() != strlen(Args.getArgString(Index))) { |
| 158 | const char *Value = Args.getArgString(Index) + getName().size(); |
Michael J. Spencer | 0464fd5 | 2012-10-10 21:48:26 +0000 | [diff] [blame^] | 159 | return new Arg(*this, Index++, Value); |
Michael J. Spencer | 4327557 | 2012-08-20 21:41:17 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | // Otherwise it must be separate. |
| 163 | Index += 2; |
| 164 | if (Index > Args.getNumInputArgStrings()) |
| 165 | return 0; |
| 166 | |
| 167 | return new Arg(getUnaliasedOption(), |
| 168 | Index - 2, Args.getArgString(Index - 1)); |
| 169 | } |
| 170 | case JoinedAndSeparateClass: |
| 171 | // Always matches. |
| 172 | Index += 2; |
| 173 | if (Index > Args.getNumInputArgStrings()) |
| 174 | return 0; |
| 175 | |
| 176 | return new Arg(getUnaliasedOption(), Index - 2, |
| 177 | Args.getArgString(Index-2)+getName().size(), |
| 178 | Args.getArgString(Index-1)); |
David Blaikie | 279e0be | 2012-08-20 22:22:51 +0000 | [diff] [blame] | 179 | default: |
| 180 | llvm_unreachable("Invalid option kind!"); |
Michael J. Spencer | 4327557 | 2012-08-20 21:41:17 +0000 | [diff] [blame] | 181 | } |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 182 | } |