2017-06-16 08:43:26 +08:00
|
|
|
//===- Option.cpp - Abstract Driver Options -------------------------------===//
|
2012-12-05 08:29:32 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2012-12-05 08:29:32 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-06-16 08:43:26 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2012-12-05 08:29:32 +08:00
|
|
|
#include "llvm/ADT/Twine.h"
|
2018-04-30 22:59:11 +08:00
|
|
|
#include "llvm/Config/llvm-config.h"
|
2012-12-05 08:29:32 +08:00
|
|
|
#include "llvm/Option/Arg.h"
|
|
|
|
#include "llvm/Option/ArgList.h"
|
2017-06-16 08:43:26 +08:00
|
|
|
#include "llvm/Option/Option.h"
|
|
|
|
#include "llvm/Option/OptTable.h"
|
|
|
|
#include "llvm/Support/Compiler.h"
|
2015-12-19 02:55:26 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2012-12-05 08:29:32 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2013-01-02 18:22:59 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2012-12-05 08:29:32 +08:00
|
|
|
#include <cassert>
|
2017-06-16 08:43:26 +08:00
|
|
|
#include <cstring>
|
2012-12-05 08:29:32 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::opt;
|
|
|
|
|
|
|
|
Option::Option(const OptTable::Info *info, const OptTable *owner)
|
|
|
|
: Info(info), Owner(owner) {
|
2013-07-23 00:18:13 +08:00
|
|
|
// Multi-level aliases are not supported. This just simplifies option
|
|
|
|
// tracking, it is not an inherent limitation.
|
2013-07-23 05:29:28 +08:00
|
|
|
assert((!Info || !getAlias().isValid() || !getAlias().getAlias().isValid()) &&
|
2013-07-23 00:18:13 +08:00
|
|
|
"Multi-level aliases are not supported.");
|
2013-08-01 06:44:41 +08:00
|
|
|
|
|
|
|
if (Info && getAliasArgs()) {
|
|
|
|
assert(getAlias().isValid() && "Only alias options can have alias args.");
|
|
|
|
assert(getKind() == FlagClass && "Only Flag aliases can have alias args.");
|
|
|
|
assert(getAlias().getKind() != FlagClass &&
|
|
|
|
"Cannot provide alias args to a flag option.");
|
|
|
|
}
|
2012-12-05 08:29:32 +08:00
|
|
|
}
|
|
|
|
|
2015-12-19 02:55:26 +08:00
|
|
|
void Option::print(raw_ostream &O) const {
|
|
|
|
O << "<";
|
2012-12-05 08:29:32 +08:00
|
|
|
switch (getKind()) {
|
2015-12-19 02:55:26 +08:00
|
|
|
#define P(N) case N: O << #N; break
|
2012-12-05 08:29:32 +08:00
|
|
|
P(GroupClass);
|
|
|
|
P(InputClass);
|
|
|
|
P(UnknownClass);
|
|
|
|
P(FlagClass);
|
|
|
|
P(JoinedClass);
|
2017-06-21 00:31:31 +08:00
|
|
|
P(ValuesClass);
|
2012-12-05 08:29:32 +08:00
|
|
|
P(SeparateClass);
|
|
|
|
P(CommaJoinedClass);
|
|
|
|
P(MultiArgClass);
|
|
|
|
P(JoinedOrSeparateClass);
|
|
|
|
P(JoinedAndSeparateClass);
|
2013-08-14 05:09:50 +08:00
|
|
|
P(RemainingArgsClass);
|
2016-04-15 08:23:30 +08:00
|
|
|
P(RemainingArgsJoinedClass);
|
2012-12-05 08:29:32 +08:00
|
|
|
#undef P
|
|
|
|
}
|
|
|
|
|
2013-06-27 06:43:37 +08:00
|
|
|
if (Info->Prefixes) {
|
2015-12-19 02:55:26 +08:00
|
|
|
O << " Prefixes:[";
|
|
|
|
for (const char *const *Pre = Info->Prefixes; *Pre != nullptr; ++Pre) {
|
|
|
|
O << '"' << *Pre << (*(Pre + 1) == nullptr ? "\"" : "\", ");
|
2013-06-27 06:43:37 +08:00
|
|
|
}
|
2015-12-19 02:55:26 +08:00
|
|
|
O << ']';
|
2012-12-05 08:29:32 +08:00
|
|
|
}
|
|
|
|
|
2015-12-19 02:55:26 +08:00
|
|
|
O << " Name:\"" << getName() << '"';
|
2012-12-05 08:29:32 +08:00
|
|
|
|
|
|
|
const Option Group = getGroup();
|
|
|
|
if (Group.isValid()) {
|
2015-12-19 02:55:26 +08:00
|
|
|
O << " Group:";
|
|
|
|
Group.print(O);
|
2012-12-05 08:29:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const Option Alias = getAlias();
|
|
|
|
if (Alias.isValid()) {
|
2015-12-19 02:55:26 +08:00
|
|
|
O << " Alias:";
|
|
|
|
Alias.print(O);
|
2012-12-05 08:29:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (getKind() == MultiArgClass)
|
2015-12-19 02:55:26 +08:00
|
|
|
O << " NumArgs:" << getNumArgs();
|
2012-12-05 08:29:32 +08:00
|
|
|
|
2015-12-19 02:55:26 +08:00
|
|
|
O << ">\n";
|
2012-12-05 08:29:32 +08:00
|
|
|
}
|
|
|
|
|
2017-10-15 22:32:27 +08:00
|
|
|
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
|
2016-01-30 04:50:44 +08:00
|
|
|
LLVM_DUMP_METHOD void Option::dump() const { print(dbgs()); }
|
2017-01-28 10:02:38 +08:00
|
|
|
#endif
|
2015-12-19 02:55:26 +08:00
|
|
|
|
2012-12-05 08:29:32 +08:00
|
|
|
bool Option::matches(OptSpecifier Opt) const {
|
|
|
|
// Aliases are never considered in matching, look through them.
|
|
|
|
const Option Alias = getAlias();
|
|
|
|
if (Alias.isValid())
|
|
|
|
return Alias.matches(Opt);
|
|
|
|
|
|
|
|
// Check exact match.
|
|
|
|
if (getID() == Opt.getID())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
const Option Group = getGroup();
|
|
|
|
if (Group.isValid())
|
|
|
|
return Group.matches(Opt);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
Let unaliased Args track which Alias they were created from, and use that in Arg::getAsString() for diagnostics
With this, `clang-cl /source-charset:utf-16 test.cc` now prints `invalid
value 'utf-16' in '/source-charset:utf-16'` instead of `invalid value
'utf-16' in '-finput-charset=utf-16'` before, and several other clang-cl
flags produce much less confusing output as well.
Fixes PR29106.
Since an arg and its alias can have different arg types (joined vs not)
and different values (because of AliasArgs<>), I chose to give the Alias
its own Arg object. For convenience, I just store the alias directly in
the unaliased arg – there aren't many arg objects at runtime, so that
seems ok.
Finally, I changed Arg::getAsString() to use the alias's representation
if it's present – that function was already documented as being the
suitable function for diagnostics, and most callers already used it for
diagnostics.
Implementation-wise, Arg::accept() previously used to parse things as
the unaliased option. The core of that switch is now extracted into a
new function acceptInternal() which parses as the _aliased_ option, and
the previously-intermingled unaliasing is now done as an explicit step
afterwards.
(This also changes one place in lld that didn't use getAsString() for
diagnostics, so that that one place now also prints the flag as the user
wrote it, not as it looks after it went through unaliasing.)
Differential Revision: https://reviews.llvm.org/D64253
llvm-svn: 365413
2019-07-09 08:34:08 +08:00
|
|
|
Arg *Option::acceptInternal(const ArgList &Args, unsigned &Index,
|
|
|
|
unsigned ArgSize) const {
|
|
|
|
StringRef Spelling = StringRef(Args.getArgString(Index), ArgSize);
|
2012-12-05 08:29:32 +08:00
|
|
|
switch (getKind()) {
|
2013-08-01 06:44:41 +08:00
|
|
|
case FlagClass: {
|
2012-12-05 08:29:32 +08:00
|
|
|
if (ArgSize != strlen(Args.getArgString(Index)))
|
2014-04-15 14:32:26 +08:00
|
|
|
return nullptr;
|
Let unaliased Args track which Alias they were created from, and use that in Arg::getAsString() for diagnostics
With this, `clang-cl /source-charset:utf-16 test.cc` now prints `invalid
value 'utf-16' in '/source-charset:utf-16'` instead of `invalid value
'utf-16' in '-finput-charset=utf-16'` before, and several other clang-cl
flags produce much less confusing output as well.
Fixes PR29106.
Since an arg and its alias can have different arg types (joined vs not)
and different values (because of AliasArgs<>), I chose to give the Alias
its own Arg object. For convenience, I just store the alias directly in
the unaliased arg – there aren't many arg objects at runtime, so that
seems ok.
Finally, I changed Arg::getAsString() to use the alias's representation
if it's present – that function was already documented as being the
suitable function for diagnostics, and most callers already used it for
diagnostics.
Implementation-wise, Arg::accept() previously used to parse things as
the unaliased option. The core of that switch is now extracted into a
new function acceptInternal() which parses as the _aliased_ option, and
the previously-intermingled unaliasing is now done as an explicit step
afterwards.
(This also changes one place in lld that didn't use getAsString() for
diagnostics, so that that one place now also prints the flag as the user
wrote it, not as it looks after it went through unaliasing.)
Differential Revision: https://reviews.llvm.org/D64253
llvm-svn: 365413
2019-07-09 08:34:08 +08:00
|
|
|
return new Arg(*this, Spelling, Index++);
|
2013-08-01 06:44:41 +08:00
|
|
|
}
|
2012-12-05 08:29:32 +08:00
|
|
|
case JoinedClass: {
|
|
|
|
const char *Value = Args.getArgString(Index) + ArgSize;
|
Let unaliased Args track which Alias they were created from, and use that in Arg::getAsString() for diagnostics
With this, `clang-cl /source-charset:utf-16 test.cc` now prints `invalid
value 'utf-16' in '/source-charset:utf-16'` instead of `invalid value
'utf-16' in '-finput-charset=utf-16'` before, and several other clang-cl
flags produce much less confusing output as well.
Fixes PR29106.
Since an arg and its alias can have different arg types (joined vs not)
and different values (because of AliasArgs<>), I chose to give the Alias
its own Arg object. For convenience, I just store the alias directly in
the unaliased arg – there aren't many arg objects at runtime, so that
seems ok.
Finally, I changed Arg::getAsString() to use the alias's representation
if it's present – that function was already documented as being the
suitable function for diagnostics, and most callers already used it for
diagnostics.
Implementation-wise, Arg::accept() previously used to parse things as
the unaliased option. The core of that switch is now extracted into a
new function acceptInternal() which parses as the _aliased_ option, and
the previously-intermingled unaliasing is now done as an explicit step
afterwards.
(This also changes one place in lld that didn't use getAsString() for
diagnostics, so that that one place now also prints the flag as the user
wrote it, not as it looks after it went through unaliasing.)
Differential Revision: https://reviews.llvm.org/D64253
llvm-svn: 365413
2019-07-09 08:34:08 +08:00
|
|
|
return new Arg(*this, Spelling, Index++, Value);
|
2012-12-05 08:29:32 +08:00
|
|
|
}
|
|
|
|
case CommaJoinedClass: {
|
|
|
|
// Always matches.
|
|
|
|
const char *Str = Args.getArgString(Index) + ArgSize;
|
Let unaliased Args track which Alias they were created from, and use that in Arg::getAsString() for diagnostics
With this, `clang-cl /source-charset:utf-16 test.cc` now prints `invalid
value 'utf-16' in '/source-charset:utf-16'` instead of `invalid value
'utf-16' in '-finput-charset=utf-16'` before, and several other clang-cl
flags produce much less confusing output as well.
Fixes PR29106.
Since an arg and its alias can have different arg types (joined vs not)
and different values (because of AliasArgs<>), I chose to give the Alias
its own Arg object. For convenience, I just store the alias directly in
the unaliased arg – there aren't many arg objects at runtime, so that
seems ok.
Finally, I changed Arg::getAsString() to use the alias's representation
if it's present – that function was already documented as being the
suitable function for diagnostics, and most callers already used it for
diagnostics.
Implementation-wise, Arg::accept() previously used to parse things as
the unaliased option. The core of that switch is now extracted into a
new function acceptInternal() which parses as the _aliased_ option, and
the previously-intermingled unaliasing is now done as an explicit step
afterwards.
(This also changes one place in lld that didn't use getAsString() for
diagnostics, so that that one place now also prints the flag as the user
wrote it, not as it looks after it went through unaliasing.)
Differential Revision: https://reviews.llvm.org/D64253
llvm-svn: 365413
2019-07-09 08:34:08 +08:00
|
|
|
Arg *A = new Arg(*this, Spelling, Index++);
|
2012-12-05 08:29:32 +08:00
|
|
|
|
|
|
|
// Parse out the comma separated values.
|
|
|
|
const char *Prev = Str;
|
|
|
|
for (;; ++Str) {
|
|
|
|
char c = *Str;
|
|
|
|
|
|
|
|
if (!c || c == ',') {
|
|
|
|
if (Prev != Str) {
|
|
|
|
char *Value = new char[Str - Prev + 1];
|
|
|
|
memcpy(Value, Prev, Str - Prev);
|
|
|
|
Value[Str - Prev] = '\0';
|
|
|
|
A->getValues().push_back(Value);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!c)
|
|
|
|
break;
|
|
|
|
|
|
|
|
Prev = Str + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
A->setOwnsValues(true);
|
|
|
|
|
|
|
|
return A;
|
|
|
|
}
|
|
|
|
case SeparateClass:
|
|
|
|
// Matches iff this is an exact match.
|
|
|
|
// FIXME: Avoid strlen.
|
|
|
|
if (ArgSize != strlen(Args.getArgString(Index)))
|
2014-04-15 14:32:26 +08:00
|
|
|
return nullptr;
|
2012-12-05 08:29:32 +08:00
|
|
|
|
|
|
|
Index += 2;
|
2014-08-23 03:29:17 +08:00
|
|
|
if (Index > Args.getNumInputArgStrings() ||
|
|
|
|
Args.getArgString(Index - 1) == nullptr)
|
2014-04-15 14:32:26 +08:00
|
|
|
return nullptr;
|
2012-12-05 08:29:32 +08:00
|
|
|
|
Let unaliased Args track which Alias they were created from, and use that in Arg::getAsString() for diagnostics
With this, `clang-cl /source-charset:utf-16 test.cc` now prints `invalid
value 'utf-16' in '/source-charset:utf-16'` instead of `invalid value
'utf-16' in '-finput-charset=utf-16'` before, and several other clang-cl
flags produce much less confusing output as well.
Fixes PR29106.
Since an arg and its alias can have different arg types (joined vs not)
and different values (because of AliasArgs<>), I chose to give the Alias
its own Arg object. For convenience, I just store the alias directly in
the unaliased arg – there aren't many arg objects at runtime, so that
seems ok.
Finally, I changed Arg::getAsString() to use the alias's representation
if it's present – that function was already documented as being the
suitable function for diagnostics, and most callers already used it for
diagnostics.
Implementation-wise, Arg::accept() previously used to parse things as
the unaliased option. The core of that switch is now extracted into a
new function acceptInternal() which parses as the _aliased_ option, and
the previously-intermingled unaliasing is now done as an explicit step
afterwards.
(This also changes one place in lld that didn't use getAsString() for
diagnostics, so that that one place now also prints the flag as the user
wrote it, not as it looks after it went through unaliasing.)
Differential Revision: https://reviews.llvm.org/D64253
llvm-svn: 365413
2019-07-09 08:34:08 +08:00
|
|
|
return new Arg(*this, Spelling, Index - 2, Args.getArgString(Index - 1));
|
2012-12-05 08:29:32 +08:00
|
|
|
case MultiArgClass: {
|
|
|
|
// Matches iff this is an exact match.
|
|
|
|
// FIXME: Avoid strlen.
|
|
|
|
if (ArgSize != strlen(Args.getArgString(Index)))
|
2014-04-15 14:32:26 +08:00
|
|
|
return nullptr;
|
2012-12-05 08:29:32 +08:00
|
|
|
|
|
|
|
Index += 1 + getNumArgs();
|
|
|
|
if (Index > Args.getNumInputArgStrings())
|
2014-04-15 14:32:26 +08:00
|
|
|
return nullptr;
|
2012-12-05 08:29:32 +08:00
|
|
|
|
Let unaliased Args track which Alias they were created from, and use that in Arg::getAsString() for diagnostics
With this, `clang-cl /source-charset:utf-16 test.cc` now prints `invalid
value 'utf-16' in '/source-charset:utf-16'` instead of `invalid value
'utf-16' in '-finput-charset=utf-16'` before, and several other clang-cl
flags produce much less confusing output as well.
Fixes PR29106.
Since an arg and its alias can have different arg types (joined vs not)
and different values (because of AliasArgs<>), I chose to give the Alias
its own Arg object. For convenience, I just store the alias directly in
the unaliased arg – there aren't many arg objects at runtime, so that
seems ok.
Finally, I changed Arg::getAsString() to use the alias's representation
if it's present – that function was already documented as being the
suitable function for diagnostics, and most callers already used it for
diagnostics.
Implementation-wise, Arg::accept() previously used to parse things as
the unaliased option. The core of that switch is now extracted into a
new function acceptInternal() which parses as the _aliased_ option, and
the previously-intermingled unaliasing is now done as an explicit step
afterwards.
(This also changes one place in lld that didn't use getAsString() for
diagnostics, so that that one place now also prints the flag as the user
wrote it, not as it looks after it went through unaliasing.)
Differential Revision: https://reviews.llvm.org/D64253
llvm-svn: 365413
2019-07-09 08:34:08 +08:00
|
|
|
Arg *A = new Arg(*this, Spelling, Index - 1 - getNumArgs(),
|
|
|
|
Args.getArgString(Index - getNumArgs()));
|
2012-12-05 08:29:32 +08:00
|
|
|
for (unsigned i = 1; i != getNumArgs(); ++i)
|
|
|
|
A->getValues().push_back(Args.getArgString(Index - getNumArgs() + i));
|
|
|
|
return A;
|
|
|
|
}
|
|
|
|
case JoinedOrSeparateClass: {
|
|
|
|
// If this is not an exact match, it is a joined arg.
|
|
|
|
// FIXME: Avoid strlen.
|
|
|
|
if (ArgSize != strlen(Args.getArgString(Index))) {
|
|
|
|
const char *Value = Args.getArgString(Index) + ArgSize;
|
Let unaliased Args track which Alias they were created from, and use that in Arg::getAsString() for diagnostics
With this, `clang-cl /source-charset:utf-16 test.cc` now prints `invalid
value 'utf-16' in '/source-charset:utf-16'` instead of `invalid value
'utf-16' in '-finput-charset=utf-16'` before, and several other clang-cl
flags produce much less confusing output as well.
Fixes PR29106.
Since an arg and its alias can have different arg types (joined vs not)
and different values (because of AliasArgs<>), I chose to give the Alias
its own Arg object. For convenience, I just store the alias directly in
the unaliased arg – there aren't many arg objects at runtime, so that
seems ok.
Finally, I changed Arg::getAsString() to use the alias's representation
if it's present – that function was already documented as being the
suitable function for diagnostics, and most callers already used it for
diagnostics.
Implementation-wise, Arg::accept() previously used to parse things as
the unaliased option. The core of that switch is now extracted into a
new function acceptInternal() which parses as the _aliased_ option, and
the previously-intermingled unaliasing is now done as an explicit step
afterwards.
(This also changes one place in lld that didn't use getAsString() for
diagnostics, so that that one place now also prints the flag as the user
wrote it, not as it looks after it went through unaliasing.)
Differential Revision: https://reviews.llvm.org/D64253
llvm-svn: 365413
2019-07-09 08:34:08 +08:00
|
|
|
return new Arg(*this, Spelling, Index++, Value);
|
2012-12-05 08:29:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise it must be separate.
|
|
|
|
Index += 2;
|
2014-08-23 03:29:17 +08:00
|
|
|
if (Index > Args.getNumInputArgStrings() ||
|
|
|
|
Args.getArgString(Index - 1) == nullptr)
|
2014-04-15 14:32:26 +08:00
|
|
|
return nullptr;
|
2012-12-05 08:29:32 +08:00
|
|
|
|
Let unaliased Args track which Alias they were created from, and use that in Arg::getAsString() for diagnostics
With this, `clang-cl /source-charset:utf-16 test.cc` now prints `invalid
value 'utf-16' in '/source-charset:utf-16'` instead of `invalid value
'utf-16' in '-finput-charset=utf-16'` before, and several other clang-cl
flags produce much less confusing output as well.
Fixes PR29106.
Since an arg and its alias can have different arg types (joined vs not)
and different values (because of AliasArgs<>), I chose to give the Alias
its own Arg object. For convenience, I just store the alias directly in
the unaliased arg – there aren't many arg objects at runtime, so that
seems ok.
Finally, I changed Arg::getAsString() to use the alias's representation
if it's present – that function was already documented as being the
suitable function for diagnostics, and most callers already used it for
diagnostics.
Implementation-wise, Arg::accept() previously used to parse things as
the unaliased option. The core of that switch is now extracted into a
new function acceptInternal() which parses as the _aliased_ option, and
the previously-intermingled unaliasing is now done as an explicit step
afterwards.
(This also changes one place in lld that didn't use getAsString() for
diagnostics, so that that one place now also prints the flag as the user
wrote it, not as it looks after it went through unaliasing.)
Differential Revision: https://reviews.llvm.org/D64253
llvm-svn: 365413
2019-07-09 08:34:08 +08:00
|
|
|
return new Arg(*this, Spelling, Index - 2, Args.getArgString(Index - 1));
|
2012-12-05 08:29:32 +08:00
|
|
|
}
|
|
|
|
case JoinedAndSeparateClass:
|
|
|
|
// Always matches.
|
|
|
|
Index += 2;
|
2014-08-23 03:29:17 +08:00
|
|
|
if (Index > Args.getNumInputArgStrings() ||
|
|
|
|
Args.getArgString(Index - 1) == nullptr)
|
2014-04-15 14:32:26 +08:00
|
|
|
return nullptr;
|
2012-12-05 08:29:32 +08:00
|
|
|
|
Let unaliased Args track which Alias they were created from, and use that in Arg::getAsString() for diagnostics
With this, `clang-cl /source-charset:utf-16 test.cc` now prints `invalid
value 'utf-16' in '/source-charset:utf-16'` instead of `invalid value
'utf-16' in '-finput-charset=utf-16'` before, and several other clang-cl
flags produce much less confusing output as well.
Fixes PR29106.
Since an arg and its alias can have different arg types (joined vs not)
and different values (because of AliasArgs<>), I chose to give the Alias
its own Arg object. For convenience, I just store the alias directly in
the unaliased arg – there aren't many arg objects at runtime, so that
seems ok.
Finally, I changed Arg::getAsString() to use the alias's representation
if it's present – that function was already documented as being the
suitable function for diagnostics, and most callers already used it for
diagnostics.
Implementation-wise, Arg::accept() previously used to parse things as
the unaliased option. The core of that switch is now extracted into a
new function acceptInternal() which parses as the _aliased_ option, and
the previously-intermingled unaliasing is now done as an explicit step
afterwards.
(This also changes one place in lld that didn't use getAsString() for
diagnostics, so that that one place now also prints the flag as the user
wrote it, not as it looks after it went through unaliasing.)
Differential Revision: https://reviews.llvm.org/D64253
llvm-svn: 365413
2019-07-09 08:34:08 +08:00
|
|
|
return new Arg(*this, Spelling, Index - 2,
|
2012-12-05 08:29:32 +08:00
|
|
|
Args.getArgString(Index - 2) + ArgSize,
|
|
|
|
Args.getArgString(Index - 1));
|
2013-08-14 05:09:50 +08:00
|
|
|
case RemainingArgsClass: {
|
|
|
|
// Matches iff this is an exact match.
|
|
|
|
// FIXME: Avoid strlen.
|
|
|
|
if (ArgSize != strlen(Args.getArgString(Index)))
|
2014-04-15 14:32:26 +08:00
|
|
|
return nullptr;
|
Let unaliased Args track which Alias they were created from, and use that in Arg::getAsString() for diagnostics
With this, `clang-cl /source-charset:utf-16 test.cc` now prints `invalid
value 'utf-16' in '/source-charset:utf-16'` instead of `invalid value
'utf-16' in '-finput-charset=utf-16'` before, and several other clang-cl
flags produce much less confusing output as well.
Fixes PR29106.
Since an arg and its alias can have different arg types (joined vs not)
and different values (because of AliasArgs<>), I chose to give the Alias
its own Arg object. For convenience, I just store the alias directly in
the unaliased arg – there aren't many arg objects at runtime, so that
seems ok.
Finally, I changed Arg::getAsString() to use the alias's representation
if it's present – that function was already documented as being the
suitable function for diagnostics, and most callers already used it for
diagnostics.
Implementation-wise, Arg::accept() previously used to parse things as
the unaliased option. The core of that switch is now extracted into a
new function acceptInternal() which parses as the _aliased_ option, and
the previously-intermingled unaliasing is now done as an explicit step
afterwards.
(This also changes one place in lld that didn't use getAsString() for
diagnostics, so that that one place now also prints the flag as the user
wrote it, not as it looks after it went through unaliasing.)
Differential Revision: https://reviews.llvm.org/D64253
llvm-svn: 365413
2019-07-09 08:34:08 +08:00
|
|
|
Arg *A = new Arg(*this, Spelling, Index++);
|
2014-08-23 03:29:17 +08:00
|
|
|
while (Index < Args.getNumInputArgStrings() &&
|
|
|
|
Args.getArgString(Index) != nullptr)
|
2013-08-14 05:09:50 +08:00
|
|
|
A->getValues().push_back(Args.getArgString(Index++));
|
|
|
|
return A;
|
|
|
|
}
|
2016-04-15 08:23:30 +08:00
|
|
|
case RemainingArgsJoinedClass: {
|
Let unaliased Args track which Alias they were created from, and use that in Arg::getAsString() for diagnostics
With this, `clang-cl /source-charset:utf-16 test.cc` now prints `invalid
value 'utf-16' in '/source-charset:utf-16'` instead of `invalid value
'utf-16' in '-finput-charset=utf-16'` before, and several other clang-cl
flags produce much less confusing output as well.
Fixes PR29106.
Since an arg and its alias can have different arg types (joined vs not)
and different values (because of AliasArgs<>), I chose to give the Alias
its own Arg object. For convenience, I just store the alias directly in
the unaliased arg – there aren't many arg objects at runtime, so that
seems ok.
Finally, I changed Arg::getAsString() to use the alias's representation
if it's present – that function was already documented as being the
suitable function for diagnostics, and most callers already used it for
diagnostics.
Implementation-wise, Arg::accept() previously used to parse things as
the unaliased option. The core of that switch is now extracted into a
new function acceptInternal() which parses as the _aliased_ option, and
the previously-intermingled unaliasing is now done as an explicit step
afterwards.
(This also changes one place in lld that didn't use getAsString() for
diagnostics, so that that one place now also prints the flag as the user
wrote it, not as it looks after it went through unaliasing.)
Differential Revision: https://reviews.llvm.org/D64253
llvm-svn: 365413
2019-07-09 08:34:08 +08:00
|
|
|
Arg *A = new Arg(*this, Spelling, Index);
|
2016-04-15 08:23:30 +08:00
|
|
|
if (ArgSize != strlen(Args.getArgString(Index))) {
|
|
|
|
// An inexact match means there is a joined arg.
|
|
|
|
A->getValues().push_back(Args.getArgString(Index) + ArgSize);
|
|
|
|
}
|
|
|
|
Index++;
|
|
|
|
while (Index < Args.getNumInputArgStrings() &&
|
|
|
|
Args.getArgString(Index) != nullptr)
|
|
|
|
A->getValues().push_back(Args.getArgString(Index++));
|
|
|
|
return A;
|
|
|
|
}
|
|
|
|
|
2012-12-05 08:29:32 +08:00
|
|
|
default:
|
|
|
|
llvm_unreachable("Invalid option kind!");
|
|
|
|
}
|
|
|
|
}
|
Let unaliased Args track which Alias they were created from, and use that in Arg::getAsString() for diagnostics
With this, `clang-cl /source-charset:utf-16 test.cc` now prints `invalid
value 'utf-16' in '/source-charset:utf-16'` instead of `invalid value
'utf-16' in '-finput-charset=utf-16'` before, and several other clang-cl
flags produce much less confusing output as well.
Fixes PR29106.
Since an arg and its alias can have different arg types (joined vs not)
and different values (because of AliasArgs<>), I chose to give the Alias
its own Arg object. For convenience, I just store the alias directly in
the unaliased arg – there aren't many arg objects at runtime, so that
seems ok.
Finally, I changed Arg::getAsString() to use the alias's representation
if it's present – that function was already documented as being the
suitable function for diagnostics, and most callers already used it for
diagnostics.
Implementation-wise, Arg::accept() previously used to parse things as
the unaliased option. The core of that switch is now extracted into a
new function acceptInternal() which parses as the _aliased_ option, and
the previously-intermingled unaliasing is now done as an explicit step
afterwards.
(This also changes one place in lld that didn't use getAsString() for
diagnostics, so that that one place now also prints the flag as the user
wrote it, not as it looks after it went through unaliasing.)
Differential Revision: https://reviews.llvm.org/D64253
llvm-svn: 365413
2019-07-09 08:34:08 +08:00
|
|
|
|
|
|
|
Arg *Option::accept(const ArgList &Args,
|
|
|
|
unsigned &Index,
|
|
|
|
unsigned ArgSize) const {
|
|
|
|
std::unique_ptr<Arg> A(acceptInternal(Args, Index, ArgSize));
|
|
|
|
if (!A)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
const Option &UnaliasedOption = getUnaliasedOption();
|
|
|
|
if (getID() == UnaliasedOption.getID())
|
|
|
|
return A.release();
|
|
|
|
|
|
|
|
// "A" is an alias for a different flag. For most clients it's more convenient
|
|
|
|
// if this function returns unaliased Args, so create an unaliased arg for
|
|
|
|
// returning.
|
|
|
|
|
|
|
|
// This creates a completely new Arg object for the unaliased Arg because
|
|
|
|
// the alias and the unaliased arg can have different Kinds and different
|
|
|
|
// Values (due to AliasArgs<>).
|
|
|
|
|
|
|
|
// Get the spelling from the unaliased option.
|
|
|
|
StringRef UnaliasedSpelling = Args.MakeArgString(
|
|
|
|
Twine(UnaliasedOption.getPrefix()) + Twine(UnaliasedOption.getName()));
|
|
|
|
|
|
|
|
// It's a bit weird that aliased and unaliased arg share one index, but
|
|
|
|
// the index is mostly use as a memory optimization in render().
|
|
|
|
// Due to this, ArgList::getArgString(A->getIndex()) will return the spelling
|
|
|
|
// of the aliased arg always, while A->getSpelling() returns either the
|
|
|
|
// unaliased or the aliased arg, depending on which Arg object it's called on.
|
|
|
|
Arg *UnaliasedA = new Arg(UnaliasedOption, UnaliasedSpelling, A->getIndex());
|
|
|
|
Arg *RawA = A.get();
|
|
|
|
UnaliasedA->setAlias(std::move(A));
|
|
|
|
|
|
|
|
if (getKind() != FlagClass) {
|
|
|
|
// Values are usually owned by the ArgList. The exception are
|
|
|
|
// CommaJoined flags, where the Arg owns the values. For aliased flags,
|
|
|
|
// make the unaliased Arg the owner of the values.
|
|
|
|
// FIXME: There aren't many uses of CommaJoined -- try removing
|
|
|
|
// CommaJoined in favor of just calling StringRef::split(',') instead.
|
|
|
|
UnaliasedA->getValues() = RawA->getValues();
|
|
|
|
UnaliasedA->setOwnsValues(RawA->getOwnsValues());
|
|
|
|
RawA->setOwnsValues(false);
|
|
|
|
return UnaliasedA;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FlagClass aliases can have AliasArgs<>; add those to the unaliased arg.
|
|
|
|
if (const char *Val = getAliasArgs()) {
|
|
|
|
while (*Val != '\0') {
|
|
|
|
UnaliasedA->getValues().push_back(Val);
|
|
|
|
|
|
|
|
// Move past the '\0' to the next argument.
|
|
|
|
Val += strlen(Val) + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (UnaliasedOption.getKind() == JoinedClass && !getAliasArgs())
|
|
|
|
// A Flag alias for a Joined option must provide an argument.
|
|
|
|
UnaliasedA->getValues().push_back("");
|
|
|
|
return UnaliasedA;
|
|
|
|
}
|