#include "llvm/Support/type_traits.h"
#include "llvm/Support/DataTypes.h"
#include "llvm/Support/Compiler.h"
+#include "llvm/ADT/SmallVector.h"
#include <string>
#include <vector>
#include <utility>
/// CommandLine utilities to print their own version string.
void SetVersionPrinter(void (*func)());
+
+// MarkOptionsChanged - Internal helper function.
+void MarkOptionsChanged();
+
//===----------------------------------------------------------------------===//
// Flags permitted to be passed to command line arguments
//
virtual bool handleOccurrence(unsigned pos, const char *ArgName,
const std::string &Arg) = 0;
- virtual enum NumOccurrences getNumOccurrencesFlagDefault() const {
- return Optional;
- }
virtual enum ValueExpected getValueExpectedFlagDefault() const {
return ValueOptional;
}
- virtual enum OptionHidden getOptionHiddenFlagDefault() const {
- return NotHidden;
- }
- virtual enum FormattingFlags getFormattingFlagDefault() const {
- return NormalFormatting;
- }
-
+
// Out of line virtual function to provide home for the class.
virtual void anchor();
- int NumOccurrences; // The number of times specified
- int Flags; // Flags for the argument
- unsigned Position; // Position of last occurrence of the option
+ int NumOccurrences; // The number of times specified
+ int Flags; // Flags for the argument
+ unsigned Position; // Position of last occurrence of the option
+ Option *NextRegistered; // Singly linked list of registered options.
public:
- const char *ArgStr; // The argument string itself (ex: "help", "o")
- const char *HelpStr; // The descriptive text message for --help
- const char *ValueStr; // String describing what the value of this option is
+ const char *ArgStr; // The argument string itself (ex: "help", "o")
+ const char *HelpStr; // The descriptive text message for --help
+ const char *ValueStr; // String describing what the value of this option is
inline enum NumOccurrences getNumOccurrencesFlag() const {
- int NO = Flags & OccurrencesMask;
- return NO ? static_cast<enum NumOccurrences>(NO)
- : getNumOccurrencesFlagDefault();
+ return static_cast<enum NumOccurrences>(Flags & OccurrencesMask);
}
inline enum ValueExpected getValueExpectedFlag() const {
int VE = Flags & ValueMask;
: getValueExpectedFlagDefault();
}
inline enum OptionHidden getOptionHiddenFlag() const {
- int OH = Flags & HiddenMask;
- return OH ? static_cast<enum OptionHidden>(OH)
- : getOptionHiddenFlagDefault();
+ return static_cast<enum OptionHidden>(Flags & HiddenMask);
}
inline enum FormattingFlags getFormattingFlag() const {
- int OH = Flags & FormattingMask;
- return OH ? static_cast<enum FormattingFlags>(OH)
- : getFormattingFlagDefault();
+ return static_cast<enum FormattingFlags>(Flags & FormattingMask);
}
inline unsigned getMiscFlags() const {
return Flags & MiscMask;
void setMiscFlag(enum MiscFlags M) { setFlag(M, M); }
void setPosition(unsigned pos) { Position = pos; }
protected:
- Option() : NumOccurrences(0), Flags(0), Position(0),
- ArgStr(""), HelpStr(""), ValueStr("") {}
+ explicit Option(unsigned DefaultFlags)
+ : NumOccurrences(0), Flags(DefaultFlags | NormalFormatting), Position(0),
+ NextRegistered(0), ArgStr(""), HelpStr(""), ValueStr("") {
+ assert(getNumOccurrencesFlag() != 0 &&
+ getOptionHiddenFlag() != 0 && "Not all default flags specified!");
+ }
public:
- // addArgument - Tell the system that this Option subclass will handle all
- // occurrences of -ArgStr on the command line.
+ // addArgument - Register this argument with the commandline system.
//
- void addArgument(const char *ArgStr);
+ void addArgument();
+
+ Option *getNextRegisteredOption() const { return NextRegistered; }
// Return the width of the option tag for printing...
virtual unsigned getOptionWidth() const = 0;
//
virtual void printOptionInfo(unsigned GlobalWidth) const = 0;
+ virtual void getExtraOptionNames(std::vector<const char*> &OptionNames) {}
+
// addOccurrence - Wrapper around handleOccurrence that enforces Flags
//
bool addOccurrence(unsigned pos, const char *ArgName,
// Use a vector instead of a map, because the lists should be short,
// the overhead is less, and most importantly, it keeps them in the order
// inserted so we can print our option out nicely.
- std::vector<std::pair<const char *, std::pair<int, const char *> > > Values;
+ SmallVector<std::pair<const char *, std::pair<int, const char *> >,4> Values;
void processValues(va_list Vals);
public:
ValuesClass(const char *EnumName, DataType Val, const char *Desc,
// argstr field should be stable, copy it down now.
//
hasArgStr = O.hasArgStr();
-
+ }
+
+ void getExtraOptionNames(std::vector<const char*> &OptionNames) {
// If there has been no argstr specified, that means that we need to add an
// argument for every possible option. This ensures that our options are
// vectored to us.
- //
if (!hasArgStr)
for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
- O.addArgument(getOption(i));
+ OptionNames.push_back(getOption(i));
}
+
enum ValueExpected getValueExpectedFlagDefault() const {
// If there is an ArgStr specified, then we are of the form:
//
template <class DataType>
class parser : public generic_parser_base {
protected:
- std::vector<std::pair<const char *,
- std::pair<DataType, const char *> > > Values;
+ SmallVector<std::pair<const char *,
+ std::pair<DataType, const char *> >, 8> Values;
public:
typedef DataType parser_data_type;
return O.error(": Cannot find option named '" + ArgVal + "'!");
}
- // addLiteralOption - Add an entry to the mapping table...
+ /// addLiteralOption - Add an entry to the mapping table.
+ ///
template <class DT>
void addLiteralOption(const char *Name, const DT &V, const char *HelpStr) {
assert(findOption(Name) == Values.size() && "Option already exists!");
Values.push_back(std::make_pair(Name,
std::make_pair(static_cast<DataType>(V),HelpStr)));
+ MarkOptionsChanged();
}
- // removeLiteralOption - Remove the specified option.
- //
+ /// removeLiteralOption - Remove the specified option.
+ ///
void removeLiteralOption(const char *Name) {
unsigned N = findOption(Name);
assert(N != Values.size() && "Option not found!");
return ValueRequired;
}
+ void getExtraOptionNames(std::vector<const char*> &OptionNames) {}
+
void initialize(Option &O) {}
// Return the width of the option tag for printing...
EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<bool>);
+//--------------------------------------------------
+// parser<boolOrDefault>
+enum boolOrDefault { BOU_UNSET, BOU_TRUE, BOU_FALSE };
+template<>
+class parser<boolOrDefault> : public basic_parser<boolOrDefault> {
+public:
+ // parse - Return true on error.
+ bool parse(Option &O, const char *ArgName, const std::string &Arg,
+ boolOrDefault &Val);
+
+ enum ValueExpected getValueExpectedFlagDefault() const {
+ return ValueOptional;
+ }
+
+ // getValueName - Do not print =<value> at all.
+ virtual const char *getValueName() const { return 0; }
+
+ // An out-of-line virtual method to provide a 'home' for this class.
+ virtual void anchor();
+};
+
+EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<boolOrDefault>);
//--------------------------------------------------
// parser<int>
virtual enum ValueExpected getValueExpectedFlagDefault() const {
return Parser.getValueExpectedFlagDefault();
}
+ virtual void getExtraOptionNames(std::vector<const char*> &OptionNames) {
+ return Parser.getExtraOptionNames(OptionNames);
+ }
// Forward printing stuff to the parser...
virtual unsigned getOptionWidth() const {return Parser.getOptionWidth(*this);}
}
void done() {
- addArgument(ArgStr);
+ addArgument();
Parser.initialize(*this);
}
public:
// One option...
template<class M0t>
- opt(const M0t &M0) {
+ opt(const M0t &M0) : Option(Optional | NotHidden) {
apply(M0, this);
done();
}
// Two options...
template<class M0t, class M1t>
- opt(const M0t &M0, const M1t &M1) {
+ opt(const M0t &M0, const M1t &M1) : Option(Optional | NotHidden) {
apply(M0, this); apply(M1, this);
done();
}
// Three options...
template<class M0t, class M1t, class M2t>
- opt(const M0t &M0, const M1t &M1, const M2t &M2) {
+ opt(const M0t &M0, const M1t &M1,
+ const M2t &M2) : Option(Optional | NotHidden) {
apply(M0, this); apply(M1, this); apply(M2, this);
done();
}
// Four options...
template<class M0t, class M1t, class M2t, class M3t>
- opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) {
+ opt(const M0t &M0, const M1t &M1, const M2t &M2,
+ const M3t &M3) : Option(Optional | NotHidden) {
apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
done();
}
// Five options...
template<class M0t, class M1t, class M2t, class M3t, class M4t>
opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
- const M4t &M4) {
+ const M4t &M4) : Option(Optional | NotHidden) {
apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
apply(M4, this);
done();
template<class M0t, class M1t, class M2t, class M3t,
class M4t, class M5t>
opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
- const M4t &M4, const M5t &M5) {
+ const M4t &M4, const M5t &M5) : Option(Optional | NotHidden) {
apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
apply(M4, this); apply(M5, this);
done();
template<class M0t, class M1t, class M2t, class M3t,
class M4t, class M5t, class M6t>
opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
- const M4t &M4, const M5t &M5, const M6t &M6) {
+ const M4t &M4, const M5t &M5,
+ const M6t &M6) : Option(Optional | NotHidden) {
apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
apply(M4, this); apply(M5, this); apply(M6, this);
done();
template<class M0t, class M1t, class M2t, class M3t,
class M4t, class M5t, class M6t, class M7t>
opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
- const M4t &M4, const M5t &M5, const M6t &M6, const M7t &M7) {
+ const M4t &M4, const M5t &M5, const M6t &M6,
+ const M7t &M7) : Option(Optional | NotHidden) {
apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
done();
std::vector<unsigned> Positions;
ParserClass Parser;
- virtual enum NumOccurrences getNumOccurrencesFlagDefault() const {
- return ZeroOrMore;
- }
virtual enum ValueExpected getValueExpectedFlagDefault() const {
return Parser.getValueExpectedFlagDefault();
}
-
+ virtual void getExtraOptionNames(std::vector<const char*> &OptionNames) {
+ return Parser.getExtraOptionNames(OptionNames);
+ }
+
virtual bool handleOccurrence(unsigned pos, const char *ArgName,
const std::string &Arg) {
typename ParserClass::parser_data_type Val =
}
void done() {
- addArgument(ArgStr);
+ addArgument();
Parser.initialize(*this);
}
public:
// One option...
template<class M0t>
- list(const M0t &M0) {
+ list(const M0t &M0) : Option(ZeroOrMore | NotHidden) {
apply(M0, this);
done();
}
// Two options...
template<class M0t, class M1t>
- list(const M0t &M0, const M1t &M1) {
+ list(const M0t &M0, const M1t &M1) : Option(ZeroOrMore | NotHidden) {
apply(M0, this); apply(M1, this);
done();
}
// Three options...
template<class M0t, class M1t, class M2t>
- list(const M0t &M0, const M1t &M1, const M2t &M2) {
+ list(const M0t &M0, const M1t &M1, const M2t &M2)
+ : Option(ZeroOrMore | NotHidden) {
apply(M0, this); apply(M1, this); apply(M2, this);
done();
}
// Four options...
template<class M0t, class M1t, class M2t, class M3t>
- list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) {
+ list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3)
+ : Option(ZeroOrMore | NotHidden) {
apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
done();
}
// Five options...
template<class M0t, class M1t, class M2t, class M3t, class M4t>
list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
- const M4t &M4) {
+ const M4t &M4) : Option(ZeroOrMore | NotHidden) {
apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
apply(M4, this);
done();
template<class M0t, class M1t, class M2t, class M3t,
class M4t, class M5t>
list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
- const M4t &M4, const M5t &M5) {
+ const M4t &M4, const M5t &M5) : Option(ZeroOrMore | NotHidden) {
apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
apply(M4, this); apply(M5, this);
done();
template<class M0t, class M1t, class M2t, class M3t,
class M4t, class M5t, class M6t>
list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
- const M4t &M4, const M5t &M5, const M6t &M6) {
+ const M4t &M4, const M5t &M5, const M6t &M6)
+ : Option(ZeroOrMore | NotHidden) {
apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
apply(M4, this); apply(M5, this); apply(M6, this);
done();
template<class M0t, class M1t, class M2t, class M3t,
class M4t, class M5t, class M6t, class M7t>
list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
- const M4t &M4, const M5t &M5, const M6t &M6, const M7t &M7) {
+ const M4t &M4, const M5t &M5, const M6t &M6,
+ const M7t &M7) : Option(ZeroOrMore | NotHidden) {
apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
done();
std::vector<unsigned> Positions;
ParserClass Parser;
- virtual enum NumOccurrences getNumOccurrencesFlagDefault() const {
- return ZeroOrMore;
- }
virtual enum ValueExpected getValueExpectedFlagDefault() const {
return Parser.getValueExpectedFlagDefault();
}
-
+ virtual void getExtraOptionNames(std::vector<const char*> &OptionNames) {
+ return Parser.getExtraOptionNames(OptionNames);
+ }
+
virtual bool handleOccurrence(unsigned pos, const char *ArgName,
const std::string &Arg) {
typename ParserClass::parser_data_type Val =
}
void done() {
- addArgument(ArgStr);
+ addArgument();
Parser.initialize(*this);
}
public:
// One option...
template<class M0t>
- bits(const M0t &M0) {
+ bits(const M0t &M0) : Option(ZeroOrMore | NotHidden) {
apply(M0, this);
done();
}
// Two options...
template<class M0t, class M1t>
- bits(const M0t &M0, const M1t &M1) {
+ bits(const M0t &M0, const M1t &M1) : Option(ZeroOrMore | NotHidden) {
apply(M0, this); apply(M1, this);
done();
}
// Three options...
template<class M0t, class M1t, class M2t>
- bits(const M0t &M0, const M1t &M1, const M2t &M2) {
+ bits(const M0t &M0, const M1t &M1, const M2t &M2)
+ : Option(ZeroOrMore | NotHidden) {
apply(M0, this); apply(M1, this); apply(M2, this);
done();
}
// Four options...
template<class M0t, class M1t, class M2t, class M3t>
- bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) {
+ bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3)
+ : Option(ZeroOrMore | NotHidden) {
apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
done();
}
// Five options...
template<class M0t, class M1t, class M2t, class M3t, class M4t>
bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
- const M4t &M4) {
+ const M4t &M4) : Option(ZeroOrMore | NotHidden) {
apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
apply(M4, this);
done();
template<class M0t, class M1t, class M2t, class M3t,
class M4t, class M5t>
bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
- const M4t &M4, const M5t &M5) {
+ const M4t &M4, const M5t &M5) : Option(ZeroOrMore | NotHidden) {
apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
apply(M4, this); apply(M5, this);
done();
template<class M0t, class M1t, class M2t, class M3t,
class M4t, class M5t, class M6t>
bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
- const M4t &M4, const M5t &M5, const M6t &M6) {
+ const M4t &M4, const M5t &M5, const M6t &M6)
+ : Option(ZeroOrMore | NotHidden) {
apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
apply(M4, this); apply(M5, this); apply(M6, this);
done();
template<class M0t, class M1t, class M2t, class M3t,
class M4t, class M5t, class M6t, class M7t>
bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
- const M4t &M4, const M5t &M5, const M6t &M6, const M7t &M7) {
+ const M4t &M4, const M5t &M5, const M6t &M6,
+ const M7t &M7) : Option(ZeroOrMore | NotHidden) {
apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
done();
const std::string &Arg) {
return AliasFor->handleOccurrence(pos, AliasFor->ArgStr, Arg);
}
- // Aliases default to be hidden...
- virtual enum OptionHidden getOptionHiddenFlagDefault() const {return Hidden;}
-
// Handle printing stuff...
virtual unsigned getOptionWidth() const;
virtual void printOptionInfo(unsigned GlobalWidth) const;
error(": cl::alias must have argument name specified!");
if (AliasFor == 0)
error(": cl::alias must have an cl::aliasopt(option) specified!");
- addArgument(ArgStr);
+ addArgument();
}
public:
void setAliasFor(Option &O) {
// One option...
template<class M0t>
- alias(const M0t &M0) : AliasFor(0) {
+ alias(const M0t &M0) : Option(Optional | Hidden), AliasFor(0) {
apply(M0, this);
done();
}
// Two options...
template<class M0t, class M1t>
- alias(const M0t &M0, const M1t &M1) : AliasFor(0) {
+ alias(const M0t &M0, const M1t &M1) : Option(Optional | Hidden), AliasFor(0) {
apply(M0, this); apply(M1, this);
done();
}
// Three options...
template<class M0t, class M1t, class M2t>
- alias(const M0t &M0, const M1t &M1, const M2t &M2) : AliasFor(0) {
+ alias(const M0t &M0, const M1t &M1, const M2t &M2)
+ : Option(Optional | Hidden), AliasFor(0) {
apply(M0, this); apply(M1, this); apply(M2, this);
done();
}
// Four options...
template<class M0t, class M1t, class M2t, class M3t>
alias(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3)
- : AliasFor(0) {
+ : Option(Optional | Hidden), AliasFor(0) {
apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
done();
}