diff options
author | Pascal Stumpf <pascal@cvs.openbsd.org> | 2016-09-03 22:47:03 +0000 |
---|---|---|
committer | Pascal Stumpf <pascal@cvs.openbsd.org> | 2016-09-03 22:47:03 +0000 |
commit | c07c59953ad4d4f16e8c3a3e25692ad9657db3ea (patch) | |
tree | e5a516d9d25bf178ab50dad2aa60c32b8684d5ee /gnu/llvm/lib/Option | |
parent | 62bc401a55deb1281a3a42cd4f08325a6839a171 (diff) |
Use the space freed up by sparc and zaurus to import LLVM.
ok hackroom@
Diffstat (limited to 'gnu/llvm/lib/Option')
-rw-r--r-- | gnu/llvm/lib/Option/Arg.cpp | 120 | ||||
-rw-r--r-- | gnu/llvm/lib/Option/ArgList.cpp | 423 | ||||
-rw-r--r-- | gnu/llvm/lib/Option/CMakeLists.txt | 9 | ||||
-rw-r--r-- | gnu/llvm/lib/Option/LLVMBuild.txt | 22 | ||||
-rw-r--r-- | gnu/llvm/lib/Option/Makefile | 14 | ||||
-rw-r--r-- | gnu/llvm/lib/Option/OptTable.cpp | 433 | ||||
-rw-r--r-- | gnu/llvm/lib/Option/Option.cpp | 240 |
7 files changed, 1261 insertions, 0 deletions
diff --git a/gnu/llvm/lib/Option/Arg.cpp b/gnu/llvm/lib/Option/Arg.cpp new file mode 100644 index 00000000000..c3de2d1a496 --- /dev/null +++ b/gnu/llvm/lib/Option/Arg.cpp @@ -0,0 +1,120 @@ +//===--- Arg.cpp - Argument Implementations -------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "llvm/Option/Arg.h" +#include "llvm/ADT/SmallString.h" +#include "llvm/ADT/Twine.h" +#include "llvm/Option/ArgList.h" +#include "llvm/Option/Option.h" +#include "llvm/Support/raw_ostream.h" +#include "llvm/Support/Debug.h" + +using namespace llvm; +using namespace llvm::opt; + +Arg::Arg(const Option Opt, StringRef S, unsigned Index, const Arg *BaseArg) + : Opt(Opt), BaseArg(BaseArg), Spelling(S), Index(Index), Claimed(false), + OwnsValues(false) {} + +Arg::Arg(const Option Opt, StringRef S, unsigned Index, const char *Value0, + const Arg *BaseArg) + : Opt(Opt), BaseArg(BaseArg), Spelling(S), Index(Index), Claimed(false), + OwnsValues(false) { + Values.push_back(Value0); +} + +Arg::Arg(const Option Opt, StringRef S, unsigned Index, const char *Value0, + const char *Value1, const Arg *BaseArg) + : Opt(Opt), BaseArg(BaseArg), Spelling(S), Index(Index), Claimed(false), + OwnsValues(false) { + Values.push_back(Value0); + Values.push_back(Value1); +} + +Arg::~Arg() { + if (OwnsValues) { + for (unsigned i = 0, e = Values.size(); i != e; ++i) + delete[] Values[i]; + } +} + +void Arg::print(raw_ostream& O) const { + O << "<"; + + O << " Opt:"; + Opt.print(O); + + O << " Index:" << Index; + + O << " Values: ["; + for (unsigned i = 0, e = Values.size(); i != e; ++i) { + if (i) O << ", "; + O << "'" << Values[i] << "'"; + } + + O << "]>\n"; +} + +LLVM_DUMP_METHOD void Arg::dump() const { print(dbgs()); } + +std::string Arg::getAsString(const ArgList &Args) const { + SmallString<256> Res; + llvm::raw_svector_ostream OS(Res); + + ArgStringList ASL; + render(Args, ASL); + for (ArgStringList::iterator + it = ASL.begin(), ie = ASL.end(); it != ie; ++it) { + if (it != ASL.begin()) + OS << ' '; + OS << *it; + } + + return OS.str(); +} + +void Arg::renderAsInput(const ArgList &Args, ArgStringList &Output) const { + if (!getOption().hasNoOptAsInput()) { + render(Args, Output); + return; + } + + Output.append(Values.begin(), Values.end()); +} + +void Arg::render(const ArgList &Args, ArgStringList &Output) const { + switch (getOption().getRenderStyle()) { + case Option::RenderValuesStyle: + Output.append(Values.begin(), Values.end()); + break; + + case Option::RenderCommaJoinedStyle: { + SmallString<256> Res; + llvm::raw_svector_ostream OS(Res); + OS << getSpelling(); + for (unsigned i = 0, e = getNumValues(); i != e; ++i) { + if (i) OS << ','; + OS << getValue(i); + } + Output.push_back(Args.MakeArgString(OS.str())); + break; + } + + case Option::RenderJoinedStyle: + Output.push_back(Args.GetOrMakeJoinedArgString( + getIndex(), getSpelling(), getValue(0))); + Output.append(Values.begin() + 1, Values.end()); + break; + + case Option::RenderSeparateStyle: + Output.push_back(Args.MakeArgString(getSpelling())); + Output.append(Values.begin(), Values.end()); + break; + } +} diff --git a/gnu/llvm/lib/Option/ArgList.cpp b/gnu/llvm/lib/Option/ArgList.cpp new file mode 100644 index 00000000000..0826ef87319 --- /dev/null +++ b/gnu/llvm/lib/Option/ArgList.cpp @@ -0,0 +1,423 @@ +//===--- ArgList.cpp - Argument List Management ---------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "llvm/Option/ArgList.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/SmallString.h" +#include "llvm/ADT/Twine.h" +#include "llvm/Option/Arg.h" +#include "llvm/Option/Option.h" +#include "llvm/Support/Debug.h" +#include "llvm/Support/raw_ostream.h" + +using namespace llvm; +using namespace llvm::opt; + +void arg_iterator::SkipToNextArg() { + for (; Current != Args.end(); ++Current) { + // Done if there are no filters. + if (!Id0.isValid()) + break; + + // Otherwise require a match. + const Option &O = (*Current)->getOption(); + if (O.matches(Id0) || + (Id1.isValid() && O.matches(Id1)) || + (Id2.isValid() && O.matches(Id2))) + break; + } +} + +void ArgList::append(Arg *A) { + Args.push_back(A); +} + +void ArgList::eraseArg(OptSpecifier Id) { + Args.erase(std::remove_if(begin(), end(), + [=](Arg *A) { return A->getOption().matches(Id); }), + end()); +} + +Arg *ArgList::getLastArgNoClaim(OptSpecifier Id) const { + // FIXME: Make search efficient? + for (const_reverse_iterator it = rbegin(), ie = rend(); it != ie; ++it) + if ((*it)->getOption().matches(Id)) + return *it; + return nullptr; +} + +Arg *ArgList::getLastArgNoClaim(OptSpecifier Id0, OptSpecifier Id1) const { + // FIXME: Make search efficient? + for (const_reverse_iterator it = rbegin(), ie = rend(); it != ie; ++it) + if ((*it)->getOption().matches(Id0) || + (*it)->getOption().matches(Id1)) + return *it; + return nullptr; +} + +Arg *ArgList::getLastArgNoClaim(OptSpecifier Id0, OptSpecifier Id1, + OptSpecifier Id2) const { + // FIXME: Make search efficient? + for (const_reverse_iterator it = rbegin(), ie = rend(); it != ie; ++it) + if ((*it)->getOption().matches(Id0) || (*it)->getOption().matches(Id1) || + (*it)->getOption().matches(Id2)) + return *it; + return nullptr; +} + +Arg *ArgList::getLastArgNoClaim(OptSpecifier Id0, OptSpecifier Id1, + OptSpecifier Id2, OptSpecifier Id3) const { + // FIXME: Make search efficient? + for (const_reverse_iterator it = rbegin(), ie = rend(); it != ie; ++it) + if ((*it)->getOption().matches(Id0) || (*it)->getOption().matches(Id1) || + (*it)->getOption().matches(Id2) || (*it)->getOption().matches(Id3)) + return *it; + return nullptr; +} + +Arg *ArgList::getLastArg(OptSpecifier Id) const { + Arg *Res = nullptr; + for (const_iterator it = begin(), ie = end(); it != ie; ++it) { + if ((*it)->getOption().matches(Id)) { + Res = *it; + Res->claim(); + } + } + + return Res; +} + +Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1) const { + Arg *Res = nullptr; + for (const_iterator it = begin(), ie = end(); it != ie; ++it) { + if ((*it)->getOption().matches(Id0) || + (*it)->getOption().matches(Id1)) { + Res = *it; + Res->claim(); + + } + } + + return Res; +} + +Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1, + OptSpecifier Id2) const { + Arg *Res = nullptr; + for (const_iterator it = begin(), ie = end(); it != ie; ++it) { + if ((*it)->getOption().matches(Id0) || + (*it)->getOption().matches(Id1) || + (*it)->getOption().matches(Id2)) { + Res = *it; + Res->claim(); + } + } + + return Res; +} + +Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1, + OptSpecifier Id2, OptSpecifier Id3) const { + Arg *Res = nullptr; + for (const_iterator it = begin(), ie = end(); it != ie; ++it) { + if ((*it)->getOption().matches(Id0) || + (*it)->getOption().matches(Id1) || + (*it)->getOption().matches(Id2) || + (*it)->getOption().matches(Id3)) { + Res = *it; + Res->claim(); + } + } + + return Res; +} + +Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1, + OptSpecifier Id2, OptSpecifier Id3, + OptSpecifier Id4) const { + Arg *Res = nullptr; + for (const_iterator it = begin(), ie = end(); it != ie; ++it) { + if ((*it)->getOption().matches(Id0) || + (*it)->getOption().matches(Id1) || + (*it)->getOption().matches(Id2) || + (*it)->getOption().matches(Id3) || + (*it)->getOption().matches(Id4)) { + Res = *it; + Res->claim(); + } + } + + return Res; +} + +Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1, + OptSpecifier Id2, OptSpecifier Id3, + OptSpecifier Id4, OptSpecifier Id5) const { + Arg *Res = nullptr; + for (const_iterator it = begin(), ie = end(); it != ie; ++it) { + if ((*it)->getOption().matches(Id0) || + (*it)->getOption().matches(Id1) || + (*it)->getOption().matches(Id2) || + (*it)->getOption().matches(Id3) || + (*it)->getOption().matches(Id4) || + (*it)->getOption().matches(Id5)) { + Res = *it; + Res->claim(); + } + } + + return Res; +} + +Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1, + OptSpecifier Id2, OptSpecifier Id3, + OptSpecifier Id4, OptSpecifier Id5, + OptSpecifier Id6) const { + Arg *Res = nullptr; + for (const_iterator it = begin(), ie = end(); it != ie; ++it) { + if ((*it)->getOption().matches(Id0) || + (*it)->getOption().matches(Id1) || + (*it)->getOption().matches(Id2) || + (*it)->getOption().matches(Id3) || + (*it)->getOption().matches(Id4) || + (*it)->getOption().matches(Id5) || + (*it)->getOption().matches(Id6)) { + Res = *it; + Res->claim(); + } + } + + return Res; +} + +Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1, + OptSpecifier Id2, OptSpecifier Id3, + OptSpecifier Id4, OptSpecifier Id5, + OptSpecifier Id6, OptSpecifier Id7) const { + Arg *Res = nullptr; + for (const_iterator it = begin(), ie = end(); it != ie; ++it) { + if ((*it)->getOption().matches(Id0) || + (*it)->getOption().matches(Id1) || + (*it)->getOption().matches(Id2) || + (*it)->getOption().matches(Id3) || + (*it)->getOption().matches(Id4) || + (*it)->getOption().matches(Id5) || + (*it)->getOption().matches(Id6) || + (*it)->getOption().matches(Id7)) { + Res = *it; + Res->claim(); + } + } + + return Res; +} + +bool ArgList::hasFlag(OptSpecifier Pos, OptSpecifier Neg, bool Default) const { + if (Arg *A = getLastArg(Pos, Neg)) + return A->getOption().matches(Pos); + return Default; +} + +bool ArgList::hasFlag(OptSpecifier Pos, OptSpecifier PosAlias, OptSpecifier Neg, + bool Default) const { + if (Arg *A = getLastArg(Pos, PosAlias, Neg)) + return A->getOption().matches(Pos) || A->getOption().matches(PosAlias); + return Default; +} + +StringRef ArgList::getLastArgValue(OptSpecifier Id, + StringRef Default) const { + if (Arg *A = getLastArg(Id)) + return A->getValue(); + return Default; +} + +std::vector<std::string> ArgList::getAllArgValues(OptSpecifier Id) const { + SmallVector<const char *, 16> Values; + AddAllArgValues(Values, Id); + return std::vector<std::string>(Values.begin(), Values.end()); +} + +void ArgList::AddLastArg(ArgStringList &Output, OptSpecifier Id) const { + if (Arg *A = getLastArg(Id)) { + A->claim(); + A->render(*this, Output); + } +} + +void ArgList::AddLastArg(ArgStringList &Output, OptSpecifier Id0, + OptSpecifier Id1) const { + if (Arg *A = getLastArg(Id0, Id1)) { + A->claim(); + A->render(*this, Output); + } +} + +void ArgList::AddAllArgs(ArgStringList &Output, + ArrayRef<OptSpecifier> Ids) const { + for (const Arg *Arg : Args) { + for (OptSpecifier Id : Ids) { + if (Arg->getOption().matches(Id)) { + Arg->claim(); + Arg->render(*this, Output); + break; + } + } + } +} + +/// This 3-opt variant of AddAllArgs could be eliminated in favor of one +/// that accepts a single specifier, given the above which accepts any number. +void ArgList::AddAllArgs(ArgStringList &Output, OptSpecifier Id0, + OptSpecifier Id1, OptSpecifier Id2) const { + for (auto Arg: filtered(Id0, Id1, Id2)) { + Arg->claim(); + Arg->render(*this, Output); + } +} + +void ArgList::AddAllArgValues(ArgStringList &Output, OptSpecifier Id0, + OptSpecifier Id1, OptSpecifier Id2) const { + for (auto Arg : filtered(Id0, Id1, Id2)) { + Arg->claim(); + const auto &Values = Arg->getValues(); + Output.append(Values.begin(), Values.end()); + } +} + +void ArgList::AddAllArgsTranslated(ArgStringList &Output, OptSpecifier Id0, + const char *Translation, + bool Joined) const { + for (auto Arg: filtered(Id0)) { + Arg->claim(); + + if (Joined) { + Output.push_back(MakeArgString(StringRef(Translation) + + Arg->getValue(0))); + } else { + Output.push_back(Translation); + Output.push_back(Arg->getValue(0)); + } + } +} + +void ArgList::ClaimAllArgs(OptSpecifier Id0) const { + for (auto Arg : filtered(Id0)) + Arg->claim(); +} + +void ArgList::ClaimAllArgs() const { + for (const_iterator it = begin(), ie = end(); it != ie; ++it) + if (!(*it)->isClaimed()) + (*it)->claim(); +} + +const char *ArgList::GetOrMakeJoinedArgString(unsigned Index, + StringRef LHS, + StringRef RHS) const { + StringRef Cur = getArgString(Index); + if (Cur.size() == LHS.size() + RHS.size() && + Cur.startswith(LHS) && Cur.endswith(RHS)) + return Cur.data(); + + return MakeArgString(LHS + RHS); +} + +void ArgList::print(raw_ostream &O) const { + for (Arg *A : *this) { + O << "* "; + A->print(O); + } +} + +LLVM_DUMP_METHOD void ArgList::dump() const { print(dbgs()); } + +// + +void InputArgList::releaseMemory() { + // An InputArgList always owns its arguments. + for (Arg *A : *this) + delete A; +} + +InputArgList::InputArgList(const char* const *ArgBegin, + const char* const *ArgEnd) + : NumInputArgStrings(ArgEnd - ArgBegin) { + ArgStrings.append(ArgBegin, ArgEnd); +} + +unsigned InputArgList::MakeIndex(StringRef String0) const { + unsigned Index = ArgStrings.size(); + + // Tuck away so we have a reliable const char *. + SynthesizedStrings.push_back(String0); + ArgStrings.push_back(SynthesizedStrings.back().c_str()); + + return Index; +} + +unsigned InputArgList::MakeIndex(StringRef String0, + StringRef String1) const { + unsigned Index0 = MakeIndex(String0); + unsigned Index1 = MakeIndex(String1); + assert(Index0 + 1 == Index1 && "Unexpected non-consecutive indices!"); + (void) Index1; + return Index0; +} + +const char *InputArgList::MakeArgStringRef(StringRef Str) const { + return getArgString(MakeIndex(Str)); +} + +// + +DerivedArgList::DerivedArgList(const InputArgList &BaseArgs) + : BaseArgs(BaseArgs) {} + +const char *DerivedArgList::MakeArgStringRef(StringRef Str) const { + return BaseArgs.MakeArgString(Str); +} + +void DerivedArgList::AddSynthesizedArg(Arg *A) { + SynthesizedArgs.push_back(std::unique_ptr<Arg>(A)); +} + +Arg *DerivedArgList::MakeFlagArg(const Arg *BaseArg, const Option Opt) const { + SynthesizedArgs.push_back( + make_unique<Arg>(Opt, MakeArgString(Opt.getPrefix() + Opt.getName()), + BaseArgs.MakeIndex(Opt.getName()), BaseArg)); + return SynthesizedArgs.back().get(); +} + +Arg *DerivedArgList::MakePositionalArg(const Arg *BaseArg, const Option Opt, + StringRef Value) const { + unsigned Index = BaseArgs.MakeIndex(Value); + SynthesizedArgs.push_back( + make_unique<Arg>(Opt, MakeArgString(Opt.getPrefix() + Opt.getName()), + Index, BaseArgs.getArgString(Index), BaseArg)); + return SynthesizedArgs.back().get(); +} + +Arg *DerivedArgList::MakeSeparateArg(const Arg *BaseArg, const Option Opt, + StringRef Value) const { + unsigned Index = BaseArgs.MakeIndex(Opt.getName(), Value); + SynthesizedArgs.push_back( + make_unique<Arg>(Opt, MakeArgString(Opt.getPrefix() + Opt.getName()), + Index, BaseArgs.getArgString(Index + 1), BaseArg)); + return SynthesizedArgs.back().get(); +} + +Arg *DerivedArgList::MakeJoinedArg(const Arg *BaseArg, const Option Opt, + StringRef Value) const { + unsigned Index = BaseArgs.MakeIndex((Opt.getName() + Value).str()); + SynthesizedArgs.push_back(make_unique<Arg>( + Opt, MakeArgString(Opt.getPrefix() + Opt.getName()), Index, + BaseArgs.getArgString(Index) + Opt.getName().size(), BaseArg)); + return SynthesizedArgs.back().get(); +} diff --git a/gnu/llvm/lib/Option/CMakeLists.txt b/gnu/llvm/lib/Option/CMakeLists.txt new file mode 100644 index 00000000000..8eb086028da --- /dev/null +++ b/gnu/llvm/lib/Option/CMakeLists.txt @@ -0,0 +1,9 @@ +add_llvm_library(LLVMOption + Arg.cpp + ArgList.cpp + Option.cpp + OptTable.cpp + + ADDITIONAL_HEADER_DIRS + ${LLVM_MAIN_INCLUDE_DIR}/llvm/Option + ) diff --git a/gnu/llvm/lib/Option/LLVMBuild.txt b/gnu/llvm/lib/Option/LLVMBuild.txt new file mode 100644 index 00000000000..0b78cf20c05 --- /dev/null +++ b/gnu/llvm/lib/Option/LLVMBuild.txt @@ -0,0 +1,22 @@ +;===- ./lib/Option/LLVMBuild.txt -------------------------------*- Conf -*--===; +; +; The LLVM Compiler Infrastructure +; +; This file is distributed under the University of Illinois Open Source +; License. See LICENSE.TXT for details. +; +;===------------------------------------------------------------------------===; +; +; This is an LLVMBuild description file for the components in this subdirectory. +; +; For more information on the LLVMBuild system, please see: +; +; http://llvm.org/docs/LLVMBuild.html +; +;===------------------------------------------------------------------------===; + +[component_0] +type = Library +name = Option +parent = Libraries +required_libraries = Support diff --git a/gnu/llvm/lib/Option/Makefile b/gnu/llvm/lib/Option/Makefile new file mode 100644 index 00000000000..255d0796e23 --- /dev/null +++ b/gnu/llvm/lib/Option/Makefile @@ -0,0 +1,14 @@ +##===- lib/Option/Makefile ---------------------------------*- Makefile -*-===## +# +# The LLVM Compiler Infrastructure +# +# This file is distributed under the University of Illinois Open Source +# License. See LICENSE.TXT for details. +# +##===----------------------------------------------------------------------===## + +LEVEL = ../.. +LIBRARYNAME = LLVMOption +BUILD_ARCHIVE := 1 + +include $(LEVEL)/Makefile.common diff --git a/gnu/llvm/lib/Option/OptTable.cpp b/gnu/llvm/lib/Option/OptTable.cpp new file mode 100644 index 00000000000..09d4cebb83d --- /dev/null +++ b/gnu/llvm/lib/Option/OptTable.cpp @@ -0,0 +1,433 @@ +//===--- OptTable.cpp - Option Table Implementation -----------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "llvm/Option/OptTable.h" +#include "llvm/Option/Arg.h" +#include "llvm/Option/ArgList.h" +#include "llvm/Option/Option.h" +#include "llvm/Support/ErrorHandling.h" +#include "llvm/Support/raw_ostream.h" +#include <algorithm> +#include <cctype> +#include <map> + +using namespace llvm; +using namespace llvm::opt; + +namespace llvm { +namespace opt { + +// Ordering on Info. The ordering is *almost* case-insensitive lexicographic, +// with an exceptions. '\0' comes at the end of the alphabet instead of the +// beginning (thus options precede any other options which prefix them). +static int StrCmpOptionNameIgnoreCase(const char *A, const char *B) { + const char *X = A, *Y = B; + char a = tolower(*A), b = tolower(*B); + while (a == b) { + if (a == '\0') + return 0; + + a = tolower(*++X); + b = tolower(*++Y); + } + + if (a == '\0') // A is a prefix of B. + return 1; + if (b == '\0') // B is a prefix of A. + return -1; + + // Otherwise lexicographic. + return (a < b) ? -1 : 1; +} + +#ifndef NDEBUG +static int StrCmpOptionName(const char *A, const char *B) { + if (int N = StrCmpOptionNameIgnoreCase(A, B)) + return N; + return strcmp(A, B); +} + +static inline bool operator<(const OptTable::Info &A, const OptTable::Info &B) { + if (&A == &B) + return false; + + if (int N = StrCmpOptionName(A.Name, B.Name)) + return N < 0; + + for (const char * const *APre = A.Prefixes, + * const *BPre = B.Prefixes; + *APre != nullptr && *BPre != nullptr; ++APre, ++BPre){ + if (int N = StrCmpOptionName(*APre, *BPre)) + return N < 0; + } + + // Names are the same, check that classes are in order; exactly one + // should be joined, and it should succeed the other. + assert(((A.Kind == Option::JoinedClass) ^ (B.Kind == Option::JoinedClass)) && + "Unexpected classes for options with same name."); + return B.Kind == Option::JoinedClass; +} +#endif + +// Support lower_bound between info and an option name. +static inline bool operator<(const OptTable::Info &I, const char *Name) { + return StrCmpOptionNameIgnoreCase(I.Name, Name) < 0; +} +} +} + +OptSpecifier::OptSpecifier(const Option *Opt) : ID(Opt->getID()) {} + +OptTable::OptTable(ArrayRef<Info> OptionInfos, bool IgnoreCase) + : OptionInfos(OptionInfos), IgnoreCase(IgnoreCase), TheInputOptionID(0), + TheUnknownOptionID(0), FirstSearchableIndex(0) { + // Explicitly zero initialize the error to work around a bug in array + // value-initialization on MinGW with gcc 4.3.5. + + // Find start of normal options. + for (unsigned i = 0, e = getNumOptions(); i != e; ++i) { + unsigned Kind = getInfo(i + 1).Kind; + if (Kind == Option::InputClass) { + assert(!TheInputOptionID && "Cannot have multiple input options!"); + TheInputOptionID = getInfo(i + 1).ID; + } else if (Kind == Option::UnknownClass) { + assert(!TheUnknownOptionID && "Cannot have multiple unknown options!"); + TheUnknownOptionID = getInfo(i + 1).ID; + } else if (Kind != Option::GroupClass) { + FirstSearchableIndex = i; + break; + } + } + assert(FirstSearchableIndex != 0 && "No searchable options?"); + +#ifndef NDEBUG + // Check that everything after the first searchable option is a + // regular option class. + for (unsigned i = FirstSearchableIndex, e = getNumOptions(); i != e; ++i) { + Option::OptionClass Kind = (Option::OptionClass) getInfo(i + 1).Kind; + assert((Kind != Option::InputClass && Kind != Option::UnknownClass && + Kind != Option::GroupClass) && + "Special options should be defined first!"); + } + + // Check that options are in order. + for (unsigned i = FirstSearchableIndex + 1, e = getNumOptions(); i != e; ++i){ + if (!(getInfo(i) < getInfo(i + 1))) { + getOption(i).dump(); + getOption(i + 1).dump(); + llvm_unreachable("Options are not in order!"); + } + } +#endif + + // Build prefixes. + for (unsigned i = FirstSearchableIndex + 1, e = getNumOptions() + 1; + i != e; ++i) { + if (const char *const *P = getInfo(i).Prefixes) { + for (; *P != nullptr; ++P) { + PrefixesUnion.insert(*P); + } + } + } + + // Build prefix chars. + for (llvm::StringSet<>::const_iterator I = PrefixesUnion.begin(), + E = PrefixesUnion.end(); I != E; ++I) { + StringRef Prefix = I->getKey(); + for (StringRef::const_iterator C = Prefix.begin(), CE = Prefix.end(); + C != CE; ++C) + if (std::find(PrefixChars.begin(), PrefixChars.end(), *C) + == PrefixChars.end()) + PrefixChars.push_back(*C); + } +} + +OptTable::~OptTable() { +} + +const Option OptTable::getOption(OptSpecifier Opt) const { + unsigned id = Opt.getID(); + if (id == 0) + return Option(nullptr, nullptr); + assert((unsigned) (id - 1) < getNumOptions() && "Invalid ID."); + return Option(&getInfo(id), this); +} + +static bool isInput(const llvm::StringSet<> &Prefixes, StringRef Arg) { + if (Arg == "-") + return true; + for (llvm::StringSet<>::const_iterator I = Prefixes.begin(), + E = Prefixes.end(); I != E; ++I) + if (Arg.startswith(I->getKey())) + return false; + return true; +} + +/// \returns Matched size. 0 means no match. +static unsigned matchOption(const OptTable::Info *I, StringRef Str, + bool IgnoreCase) { + for (const char * const *Pre = I->Prefixes; *Pre != nullptr; ++Pre) { + StringRef Prefix(*Pre); + if (Str.startswith(Prefix)) { + StringRef Rest = Str.substr(Prefix.size()); + bool Matched = IgnoreCase + ? Rest.startswith_lower(I->Name) + : Rest.startswith(I->Name); + if (Matched) + return Prefix.size() + StringRef(I->Name).size(); + } + } + return 0; +} + +Arg *OptTable::ParseOneArg(const ArgList &Args, unsigned &Index, + unsigned FlagsToInclude, + unsigned FlagsToExclude) const { + unsigned Prev = Index; + const char *Str = Args.getArgString(Index); + + // Anything that doesn't start with PrefixesUnion is an input, as is '-' + // itself. + if (isInput(PrefixesUnion, Str)) + return new Arg(getOption(TheInputOptionID), Str, Index++, Str); + + const Info *Start = OptionInfos.begin() + FirstSearchableIndex; + const Info *End = OptionInfos.end(); + StringRef Name = StringRef(Str).ltrim(PrefixChars); + + // Search for the first next option which could be a prefix. + Start = std::lower_bound(Start, End, Name.data()); + + // Options are stored in sorted order, with '\0' at the end of the + // alphabet. Since the only options which can accept a string must + // prefix it, we iteratively search for the next option which could + // be a prefix. + // + // FIXME: This is searching much more than necessary, but I am + // blanking on the simplest way to make it fast. We can solve this + // problem when we move to TableGen. + for (; Start != End; ++Start) { + unsigned ArgSize = 0; + // Scan for first option which is a proper prefix. + for (; Start != End; ++Start) + if ((ArgSize = matchOption(Start, Str, IgnoreCase))) + break; + if (Start == End) + break; + + Option Opt(Start, this); + + if (FlagsToInclude && !Opt.hasFlag(FlagsToInclude)) + continue; + if (Opt.hasFlag(FlagsToExclude)) + continue; + + // See if this option matches. + if (Arg *A = Opt.accept(Args, Index, ArgSize)) + return A; + + // Otherwise, see if this argument was missing values. + if (Prev != Index) + return nullptr; + } + + // If we failed to find an option and this arg started with /, then it's + // probably an input path. + if (Str[0] == '/') + return new Arg(getOption(TheInputOptionID), Str, Index++, Str); + + return new Arg(getOption(TheUnknownOptionID), Str, Index++, Str); +} + +InputArgList OptTable::ParseArgs(ArrayRef<const char *> ArgArr, + unsigned &MissingArgIndex, + unsigned &MissingArgCount, + unsigned FlagsToInclude, + unsigned FlagsToExclude) const { + InputArgList Args(ArgArr.begin(), ArgArr.end()); + + // FIXME: Handle '@' args (or at least error on them). + + MissingArgIndex = MissingArgCount = 0; + unsigned Index = 0, End = ArgArr.size(); + while (Index < End) { + // Ingore nullptrs, they are response file's EOL markers + if (Args.getArgString(Index) == nullptr) { + ++Index; + continue; + } + // Ignore empty arguments (other things may still take them as arguments). + StringRef Str = Args.getArgString(Index); + if (Str == "") { + ++Index; + continue; + } + + unsigned Prev = Index; + Arg *A = ParseOneArg(Args, Index, FlagsToInclude, FlagsToExclude); + assert(Index > Prev && "Parser failed to consume argument."); + + // Check for missing argument error. + if (!A) { + assert(Index >= End && "Unexpected parser error."); + assert(Index - Prev - 1 && "No missing arguments!"); + MissingArgIndex = Prev; + MissingArgCount = Index - Prev - 1; + break; + } + + Args.append(A); + } + + return Args; +} + +static std::string getOptionHelpName(const OptTable &Opts, OptSpecifier Id) { + const Option O = Opts.getOption(Id); + std::string Name = O.getPrefixedName(); + + // Add metavar, if used. + switch (O.getKind()) { + case Option::GroupClass: case Option::InputClass: case Option::UnknownClass: + llvm_unreachable("Invalid option with help text."); + + case Option::MultiArgClass: + if (const char *MetaVarName = Opts.getOptionMetaVar(Id)) { + // For MultiArgs, metavar is full list of all argument names. + Name += ' '; + Name += MetaVarName; + } + else { + // For MultiArgs<N>, if metavar not supplied, print <value> N times. + for (unsigned i=0, e=O.getNumArgs(); i< e; ++i) { + Name += " <value>"; + } + } + break; + + case Option::FlagClass: + break; + + case Option::SeparateClass: case Option::JoinedOrSeparateClass: + case Option::RemainingArgsClass: + Name += ' '; + // FALLTHROUGH + case Option::JoinedClass: case Option::CommaJoinedClass: + case Option::JoinedAndSeparateClass: + if (const char *MetaVarName = Opts.getOptionMetaVar(Id)) + Name += MetaVarName; + else + Name += "<value>"; + break; + } + + return Name; +} + +static void PrintHelpOptionList(raw_ostream &OS, StringRef Title, + std::vector<std::pair<std::string, + const char*> > &OptionHelp) { + OS << Title << ":\n"; + + // Find the maximum option length. + unsigned OptionFieldWidth = 0; + for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) { + // Skip titles. + if (!OptionHelp[i].second) + continue; + + // Limit the amount of padding we are willing to give up for alignment. + unsigned Length = OptionHelp[i].first.size(); + if (Length <= 23) + OptionFieldWidth = std::max(OptionFieldWidth, Length); + } + + const unsigned InitialPad = 2; + for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) { + const std::string &Option = OptionHelp[i].first; + int Pad = OptionFieldWidth - int(Option.size()); + OS.indent(InitialPad) << Option; + + // Break on long option names. + if (Pad < 0) { + OS << "\n"; + Pad = OptionFieldWidth + InitialPad; + } + OS.indent(Pad + 1) << OptionHelp[i].second << '\n'; + } +} + +static const char *getOptionHelpGroup(const OptTable &Opts, OptSpecifier Id) { + unsigned GroupID = Opts.getOptionGroupID(Id); + + // If not in a group, return the default help group. + if (!GroupID) + return "OPTIONS"; + + // Abuse the help text of the option groups to store the "help group" + // name. + // + // FIXME: Split out option groups. + if (const char *GroupHelp = Opts.getOptionHelpText(GroupID)) + return GroupHelp; + + // Otherwise keep looking. + return getOptionHelpGroup(Opts, GroupID); +} + +void OptTable::PrintHelp(raw_ostream &OS, const char *Name, const char *Title, + bool ShowHidden) const { + PrintHelp(OS, Name, Title, /*Include*/ 0, /*Exclude*/ + (ShowHidden ? 0 : HelpHidden)); +} + + +void OptTable::PrintHelp(raw_ostream &OS, const char *Name, const char *Title, + unsigned FlagsToInclude, + unsigned FlagsToExclude) const { + OS << "OVERVIEW: " << Title << "\n"; + OS << '\n'; + OS << "USAGE: " << Name << " [options] <inputs>\n"; + OS << '\n'; + + // Render help text into a map of group-name to a list of (option, help) + // pairs. + typedef std::map<std::string, + std::vector<std::pair<std::string, const char*> > > helpmap_ty; + helpmap_ty GroupedOptionHelp; + + for (unsigned i = 0, e = getNumOptions(); i != e; ++i) { + unsigned Id = i + 1; + + // FIXME: Split out option groups. + if (getOptionKind(Id) == Option::GroupClass) + continue; + + unsigned Flags = getInfo(Id).Flags; + if (FlagsToInclude && !(Flags & FlagsToInclude)) + continue; + if (Flags & FlagsToExclude) + continue; + + if (const char *Text = getOptionHelpText(Id)) { + const char *HelpGroup = getOptionHelpGroup(*this, Id); + const std::string &OptName = getOptionHelpName(*this, Id); + GroupedOptionHelp[HelpGroup].push_back(std::make_pair(OptName, Text)); + } + } + + for (helpmap_ty::iterator it = GroupedOptionHelp .begin(), + ie = GroupedOptionHelp.end(); it != ie; ++it) { + if (it != GroupedOptionHelp .begin()) + OS << "\n"; + PrintHelpOptionList(OS, it->first, it->second); + } + + OS.flush(); +} diff --git a/gnu/llvm/lib/Option/Option.cpp b/gnu/llvm/lib/Option/Option.cpp new file mode 100644 index 00000000000..ebf05aab764 --- /dev/null +++ b/gnu/llvm/lib/Option/Option.cpp @@ -0,0 +1,240 @@ +//===--- Option.cpp - Abstract Driver Options -----------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "llvm/Option/Option.h" +#include "llvm/ADT/Twine.h" +#include "llvm/Option/Arg.h" +#include "llvm/Option/ArgList.h" +#include "llvm/Support/Debug.h" +#include "llvm/Support/ErrorHandling.h" +#include "llvm/Support/raw_ostream.h" +#include <algorithm> +#include <cassert> + +using namespace llvm; +using namespace llvm::opt; + +Option::Option(const OptTable::Info *info, const OptTable *owner) + : Info(info), Owner(owner) { + + // Multi-level aliases are not supported. This just simplifies option + // tracking, it is not an inherent limitation. + assert((!Info || !getAlias().isValid() || !getAlias().getAlias().isValid()) && + "Multi-level aliases are not supported."); + + 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."); + } +} + +void Option::print(raw_ostream &O) const { + O << "<"; + switch (getKind()) { +#define P(N) case N: O << #N; break + P(GroupClass); + P(InputClass); + P(UnknownClass); + P(FlagClass); + P(JoinedClass); + P(SeparateClass); + P(CommaJoinedClass); + P(MultiArgClass); + P(JoinedOrSeparateClass); + P(JoinedAndSeparateClass); + P(RemainingArgsClass); +#undef P + } + + if (Info->Prefixes) { + O << " Prefixes:["; + for (const char *const *Pre = Info->Prefixes; *Pre != nullptr; ++Pre) { + O << '"' << *Pre << (*(Pre + 1) == nullptr ? "\"" : "\", "); + } + O << ']'; + } + + O << " Name:\"" << getName() << '"'; + + const Option Group = getGroup(); + if (Group.isValid()) { + O << " Group:"; + Group.print(O); + } + + const Option Alias = getAlias(); + if (Alias.isValid()) { + O << " Alias:"; + Alias.print(O); + } + + if (getKind() == MultiArgClass) + O << " NumArgs:" << getNumArgs(); + + O << ">\n"; +} + +void Option::dump() const { print(dbgs()); } + +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; +} + +Arg *Option::accept(const ArgList &Args, + unsigned &Index, + unsigned ArgSize) const { + const Option &UnaliasedOption = getUnaliasedOption(); + StringRef Spelling; + // If the option was an alias, get the spelling from the unaliased one. + if (getID() == UnaliasedOption.getID()) { + Spelling = StringRef(Args.getArgString(Index), ArgSize); + } else { + Spelling = Args.MakeArgString(Twine(UnaliasedOption.getPrefix()) + + Twine(UnaliasedOption.getName())); + } + + switch (getKind()) { + case FlagClass: { + if (ArgSize != strlen(Args.getArgString(Index))) + return nullptr; + + Arg *A = new Arg(UnaliasedOption, Spelling, Index++); + if (getAliasArgs()) { + const char *Val = getAliasArgs(); + while (*Val != '\0') { + A->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. + A->getValues().push_back(""); + + return A; + } + case JoinedClass: { + const char *Value = Args.getArgString(Index) + ArgSize; + return new Arg(UnaliasedOption, Spelling, Index++, Value); + } + case CommaJoinedClass: { + // Always matches. + const char *Str = Args.getArgString(Index) + ArgSize; + Arg *A = new Arg(UnaliasedOption, Spelling, Index++); + + // 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))) + return nullptr; + + Index += 2; + if (Index > Args.getNumInputArgStrings() || + Args.getArgString(Index - 1) == nullptr) + return nullptr; + + return new Arg(UnaliasedOption, Spelling, + Index - 2, Args.getArgString(Index - 1)); + case MultiArgClass: { + // Matches iff this is an exact match. + // FIXME: Avoid strlen. + if (ArgSize != strlen(Args.getArgString(Index))) + return nullptr; + + Index += 1 + getNumArgs(); + if (Index > Args.getNumInputArgStrings()) + return nullptr; + + Arg *A = new Arg(UnaliasedOption, Spelling, Index - 1 - getNumArgs(), + Args.getArgString(Index - getNumArgs())); + 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; + return new Arg(*this, Spelling, Index++, Value); + } + + // Otherwise it must be separate. + Index += 2; + if (Index > Args.getNumInputArgStrings() || + Args.getArgString(Index - 1) == nullptr) + return nullptr; + + return new Arg(UnaliasedOption, Spelling, + Index - 2, Args.getArgString(Index - 1)); + } + case JoinedAndSeparateClass: + // Always matches. + Index += 2; + if (Index > Args.getNumInputArgStrings() || + Args.getArgString(Index - 1) == nullptr) + return nullptr; + + return new Arg(UnaliasedOption, Spelling, Index - 2, + Args.getArgString(Index - 2) + ArgSize, + Args.getArgString(Index - 1)); + case RemainingArgsClass: { + // Matches iff this is an exact match. + // FIXME: Avoid strlen. + if (ArgSize != strlen(Args.getArgString(Index))) + return nullptr; + Arg *A = new Arg(UnaliasedOption, Spelling, Index++); + while (Index < Args.getNumInputArgStrings() && + Args.getArgString(Index) != nullptr) + A->getValues().push_back(Args.getArgString(Index++)); + return A; + } + default: + llvm_unreachable("Invalid option kind!"); + } +} |