1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-10-19 19:12:56 +02:00

[YAML] When outputting, provide the ability to write default values.

Previously, if you attempted to write a key/value pair and the
value was equal to the key's default value, we would not output
the value.  Sometimes it is useful to be able to see this value
in the output anyway.

llvm-svn: 297864
This commit is contained in:
Zachary Turner 2017-03-15 17:47:39 +00:00
parent 56f52ed341
commit 23a47b11a9
2 changed files with 17 additions and 15 deletions

View File

@ -689,11 +689,12 @@ private:
assert(DefaultValue.hasValue() == false &&
"Optional<T> shouldn't have a value!");
void *SaveInfo;
bool UseDefault;
bool UseDefault = true;
const bool sameAsDefault = outputting() && !Val.hasValue();
if (!outputting() && !Val.hasValue())
Val = T();
if (this->preflightKey(Key, Required, sameAsDefault, UseDefault,
if (Val.hasValue() &&
this->preflightKey(Key, Required, sameAsDefault, UseDefault,
SaveInfo)) {
yamlize(*this, Val.getValue(), Required, Ctx);
this->postflightKey(SaveInfo);
@ -1251,6 +1252,13 @@ public:
Output(llvm::raw_ostream &, void *Ctxt = nullptr, int WrapColumn = 70);
~Output() override;
/// \brief Set whether or not to output optional values which are equal
/// to the default value. By default, when outputting if you attempt
/// to write a value that is equal to the default, the value gets ignored.
/// Sometimes, it is useful to be able to see these in the resulting YAML
/// anyway.
void setWriteDefaultValues(bool Write) { WriteDefaultValues = Write; }
bool outputting() override;
bool mapTag(StringRef, bool) override;
void beginMapping() override;
@ -1314,6 +1322,7 @@ private:
bool NeedFlowSequenceComma;
bool EnumerationMatchFound;
bool NeedsNewLine;
bool WriteDefaultValues;
};
/// YAML I/O does conversion based on types. But often native data types

View File

@ -398,17 +398,10 @@ bool Input::canElideEmptySequence() {
//===----------------------------------------------------------------------===//
Output::Output(raw_ostream &yout, void *context, int WrapColumn)
: IO(context),
Out(yout),
WrapColumn(WrapColumn),
Column(0),
ColumnAtFlowStart(0),
ColumnAtMapFlowStart(0),
NeedBitValueComma(false),
NeedFlowSequenceComma(false),
EnumerationMatchFound(false),
NeedsNewLine(false) {
}
: IO(context), Out(yout), WrapColumn(WrapColumn), Column(0),
ColumnAtFlowStart(0), ColumnAtMapFlowStart(0), NeedBitValueComma(false),
NeedFlowSequenceComma(false), EnumerationMatchFound(false),
NeedsNewLine(false), WriteDefaultValues(false) {}
Output::~Output() {
}
@ -462,7 +455,7 @@ std::vector<StringRef> Output::keys() {
bool Output::preflightKey(const char *Key, bool Required, bool SameAsDefault,
bool &UseDefault, void *&) {
UseDefault = false;
if (Required || !SameAsDefault) {
if (Required || !SameAsDefault || WriteDefaultValues) {
auto State = StateStack.back();
if (State == inFlowMapFirstKey || State == inFlowMapOtherKey) {
flowKey(Key);