From ed145620461280eb7d79de88c343b9f6843f8f1c Mon Sep 17 00:00:00 2001 From: Zachary Turner Date: Wed, 15 Mar 2017 17:47:39 +0000 Subject: [PATCH] [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. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@297864 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Support/YAMLTraits.h | 15 ++++++++++++--- lib/Support/YAMLTraits.cpp | 17 +++++------------ 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/include/llvm/Support/YAMLTraits.h b/include/llvm/Support/YAMLTraits.h index cbba9c08275..6d02e4aba48 100644 --- a/include/llvm/Support/YAMLTraits.h +++ b/include/llvm/Support/YAMLTraits.h @@ -689,11 +689,12 @@ private: assert(DefaultValue.hasValue() == false && "Optional 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); @@ -731,7 +732,7 @@ private: } private: - void *Ctxt; + void *Ctxt; }; namespace detail { @@ -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 diff --git a/lib/Support/YAMLTraits.cpp b/lib/Support/YAMLTraits.cpp index 9849b3aa1ce..c410b1d5608 100644 --- a/lib/Support/YAMLTraits.cpp +++ b/lib/Support/YAMLTraits.cpp @@ -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 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); -- 2.50.1