Add an 'init' option property.

Makes possible to provide default values for options defined in plugins (same as
cl::init).

llvm-svn: 74905
This commit is contained in:
Mikhail Glushenkov 2009-07-07 16:08:41 +00:00
parent 1bf2cd5d62
commit a1d8f36bb7
2 changed files with 28 additions and 2 deletions

View File

@ -39,6 +39,7 @@ def prefix_list_option;
def extern;
def help;
def hidden;
def init;
def multi_val;
def one_or_more;
def really_hidden;
@ -51,6 +52,10 @@ def empty;
// The 'case' construct.
def case;
// Boolean constants.
def true;
def false;
// Boolean operators.
def and;
def or;

View File

@ -187,11 +187,12 @@ struct OptionDescription {
unsigned Flags;
std::string Help;
unsigned MultiVal;
Init* InitVal;
OptionDescription(OptionType::OptionType t = OptionType::Switch,
const std::string& n = "",
const std::string& h = DefaultHelpString)
: Type(t), Name(n), Flags(0x0), Help(h), MultiVal(1)
: Type(t), Name(n), Flags(0x0), Help(h), MultiVal(1), InitVal(0)
{}
/// GenTypeDeclaration - Returns the C++ variable type of this
@ -447,6 +448,7 @@ public:
AddHandler("extern", &CollectOptionProperties::onExtern);
AddHandler("help", &CollectOptionProperties::onHelp);
AddHandler("hidden", &CollectOptionProperties::onHidden);
AddHandler("init", &CollectOptionProperties::onInit);
AddHandler("multi_val", &CollectOptionProperties::onMultiVal);
AddHandler("one_or_more", &CollectOptionProperties::onOneOrMore);
AddHandler("really_hidden", &CollectOptionProperties::onReallyHidden);
@ -490,6 +492,20 @@ private:
optDesc_.setRequired();
}
void onInit (const DagInit* d) {
checkNumberOfArguments(d, 1);
Init* i = d->getArg(0);
const std::string& str = i->getAsString();
bool correct = optDesc_.isParameter() && dynamic_cast<StringInit*>(i);
correct |= (optDesc_.isSwitch() && (str == "true" || str == "false"));
if (!correct)
throw std::string("Incorrect usage of the 'init' option property!");
optDesc_.InitVal = i;
}
void onOneOrMore (const DagInit* d) {
checkNumberOfArguments(d, 0);
if (optDesc_.isRequired() || optDesc_.isZeroOrOne())
@ -1716,7 +1732,12 @@ void EmitOptionDefinitions (const OptionDescriptions& descs,
}
if (val.MultiVal > 1)
O << ", cl::multi_val(" << val.MultiVal << ")";
O << ", cl::multi_val(" << val.MultiVal << ')';
if (val.InitVal) {
const std::string& str = val.InitVal->getAsString();
O << ", cl::init(" << str << ')';
}
if (!val.Help.empty())
O << ", cl::desc(\"" << val.Help << "\")";