-// Test for the OptionPreprocessor and any*.
+// Test for the OptionPreprocessor and related functionality.
// RUN: tblgen -I %p/../../include --gen-llvmc %s -o %t
// RUN: FileCheck -input-file %t %s
// RUN: %compile_cxx -fexceptions -x c++ %t
(unset_option "foo_p"), (unset_option "foo_l")],
// CHECK: W2
// CHECK: foo = true;
+ // CHECK: bar = true;
+ // CHECK: baz = false;
// CHECK: foo_p = "asdf";
// CHECK: foo_l.clear();
// CHECK: foo_l.push_back("qwert");
// CHECK: foo_l.push_back("yuiop");
// CHECK: foo_l.push_back("asdf");
(and (switch_on ["foo", "bar"]), (any_empty ["foo_p", "bar_p"])),
- [(warning "W2"), (set_option "foo"), (set_option "foo_p", "asdf"),
+ [(warning "W2"), (set_option "foo"),
+ (set_option "bar", true),
+ (set_option "baz", false),
+ (set_option "foo_p", "asdf"),
(set_option "foo_l", ["qwert", "yuiop", "asdf"])],
// CHECK: W3
// CHECK: foo = true;
``OptionPreprocessor`` is basically a single big ``case`` expression, which is
evaluated only once right after the plugin is loaded. The only allowed actions
-in ``OptionPreprocessor`` are ``error``, ``warning`` and two special actions:
+in ``OptionPreprocessor`` are ``error``, ``warning``, and two special actions:
``unset_option`` and ``set_option``. As their names suggest, they can be used to
-set or unset a given option. To set a parameter option with ``set_option``, use
-the two-argument form: ``(set_option "parameter", "value")``. For convenience,
-``set_option`` and ``unset_option`` also work on lists (that is, instead of
-``[(unset_option "A"), (unset_option "B")]`` you can use ``(unset_option ["A",
-"B"])``).
+set or unset a given option. To set an option with ``set_option``, use the
+two-argument form: ``(set_option "parameter", VALUE)``. Here, ``VALUE`` can be
+either a string, a string list, or a boolean constant.
+
+For convenience, ``set_option`` and ``unset_option`` also work on lists. That
+is, instead of ``[(unset_option "A"), (unset_option "B")]`` you can use
+``(unset_option ["A", "B"])``. Obviously, ``(set_option ["A", "B"])`` is valid
+only if both ``A`` and ``B`` are switches.
More advanced topics
#include "llvm/ADT/IntrusiveRefCntPtr.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringSet.h"
+
#include <algorithm>
#include <cassert>
#include <functional>
return D.getOperator()->getAsString();
}
+/// CheckBooleanConstant - Check that the provided value is a boolean constant.
+void CheckBooleanConstant(const Init* I) {
+ const DefInit& val = dynamic_cast<const DefInit&>(*I);
+ const std::string& str = val.getAsString();
+
+ if (str != "true" && str != "false") {
+ throw "Incorrect boolean value: '" + str +
+ "': must be either 'true' or 'false'";
+ }
+}
+
// checkNumberOfArguments - Ensure that the number of args in d is
// greater than or equal to min_arguments, otherwise throw an exception.
void checkNumberOfArguments (const DagInit& d, unsigned minArgs) {
const OptionDescriptions& OptDescs_;
- void onListOrDag(HandlerImpl h,
- const DagInit& d, unsigned IndentLevel, raw_ostream& O) const
+ void onListOrDag(const DagInit& d, HandlerImpl h,
+ unsigned IndentLevel, raw_ostream& O) const
{
checkNumberOfArguments(d, 1);
const Init* I = d.getArg(0);
void onUnsetOption(const DagInit& d,
unsigned IndentLevel, raw_ostream& O) const
{
- this->onListOrDag(&EmitPreprocessOptionsCallback::onUnsetOptionImpl,
- d, IndentLevel, O);
+ this->onListOrDag(d, &EmitPreprocessOptionsCallback::onUnsetOptionImpl,
+ IndentLevel, O);
}
- void onSetListOrParameter(const DagInit& d,
- unsigned IndentLevel, raw_ostream& O) const {
+ void onSetOptionImpl(const DagInit& d,
+ unsigned IndentLevel, raw_ostream& O) const {
checkNumberOfArguments(d, 2);
const std::string& OptName = InitPtrToString(d.getArg(0));
const Init* Value = d.getArg(1);
<< InitPtrToString(*B) << "\");\n";
}
}
+ else if (OptDesc.isSwitch()) {
+ CheckBooleanConstant(Value);
+ O.indent(IndentLevel) << OptDesc.GenVariableName()
+ << " = " << Value->getAsString() << ";\n";
+ }
else if (OptDesc.isParameter()) {
const std::string& Str = InitPtrToString(Value);
O.indent(IndentLevel) << OptDesc.GenVariableName()
<< " = \"" << Str << "\";\n";
}
else {
- throw "set_option: -" + OptName + ": is not a list or parameter option!";
+ throw "Can't apply 'set_option' to alias option -" + OptName + " !";
}
}
{
checkNumberOfArguments(d, 1);
- // Two arguments: (set_option "parameter", VALUE), where VALUE is either a
- // string or a string list.
+ // Two arguments: (set_option "parameter", VALUE), where VALUE can be a
+ // boolean, a string or a string list.
if (d.getNumArgs() > 1)
- this->onSetListOrParameter(d, IndentLevel, O);
+ this->onSetOptionImpl(d, IndentLevel, O);
// One argument: (set_option "switch")
// or (set_option ["switch1", "switch2", ...])
else
- this->onListOrDag(&EmitPreprocessOptionsCallback::onSetSwitch,
- d, IndentLevel, O);
+ this->onListOrDag(d, &EmitPreprocessOptionsCallback::onSetSwitch,
+ IndentLevel, O);
}
public: