diff options
Diffstat (limited to 'lib/Support/YAMLTraits.cpp')
| -rw-r--r-- | lib/Support/YAMLTraits.cpp | 115 | 
1 files changed, 101 insertions, 14 deletions
diff --git a/lib/Support/YAMLTraits.cpp b/lib/Support/YAMLTraits.cpp index c87790efb8c83..90f34f6e232d5 100644 --- a/lib/Support/YAMLTraits.cpp +++ b/lib/Support/YAMLTraits.cpp @@ -7,13 +7,15 @@  //  //===----------------------------------------------------------------------===// -#include "llvm/Support/Errc.h" +#include "llvm/Support/YAMLTraits.h" +#include "llvm/ADT/SmallString.h"  #include "llvm/ADT/Twine.h"  #include "llvm/Support/Casting.h" +#include "llvm/Support/Errc.h"  #include "llvm/Support/ErrorHandling.h"  #include "llvm/Support/Format.h" +#include "llvm/Support/LineIterator.h"  #include "llvm/Support/YAMLParser.h" -#include "llvm/Support/YAMLTraits.h"  #include "llvm/Support/raw_ostream.h"  #include <cctype>  #include <cstring> @@ -167,10 +169,22 @@ void Input::endMapping() {    }  } +void Input::beginFlowMapping() { beginMapping(); } + +void Input::endFlowMapping() { endMapping(); } +  unsigned Input::beginSequence() { -  if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) { +  if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode))      return SQ->Entries.size(); +  if (isa<EmptyHNode>(CurrentNode)) +    return 0; +  // Treat case where there's a scalar "null" value as an empty sequence. +  if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) { +    if (isNull(SN->value())) +      return 0;    } +  // Any other type of HNode is an error. +  setError(CurrentNode, "not a sequence");    return 0;  } @@ -192,12 +206,7 @@ void Input::postflightElement(void *SaveInfo) {    CurrentNode = reinterpret_cast<HNode *>(SaveInfo);  } -unsigned Input::beginFlowSequence() { -  if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) { -    return SQ->Entries.size(); -  } -  return 0; -} +unsigned Input::beginFlowSequence() { return beginSequence(); }  bool Input::preflightFlowElement(unsigned index, void *&SaveInfo) {    if (EC) @@ -233,6 +242,13 @@ bool Input::matchEnumScalar(const char *Str, bool) {    return false;  } +bool Input::matchEnumFallback() { +  if (ScalarMatchFound) +    return false; +  ScalarMatchFound = true; +  return true; +} +  void Input::endEnumScalar() {    if (!ScalarMatchFound) {      setError(CurrentNode, "unknown enumerated scalar"); @@ -294,6 +310,8 @@ void Input::scalarString(StringRef &S, bool) {    }  } +void Input::blockScalarString(StringRef &S) { scalarString(S, false); } +  void Input::setError(HNode *hnode, const Twine &message) {    assert(hnode && "HNode must not be NULL");    this->setError(hnode->_node, message); @@ -316,6 +334,11 @@ std::unique_ptr<Input::HNode> Input::createHNodes(Node *N) {        KeyStr = StringRef(Buf, Len);      }      return llvm::make_unique<ScalarHNode>(N, KeyStr); +  } else if (BlockScalarNode *BSN = dyn_cast<BlockScalarNode>(N)) { +    StringRef Value = BSN->getValue(); +    char *Buf = StringAllocator.Allocate<char>(Value.size()); +    memcpy(Buf, Value.data(), Value.size()); +    return llvm::make_unique<ScalarHNode>(N, StringRef(Buf, Value.size()));    } else if (SequenceNode *SQ = dyn_cast<SequenceNode>(N)) {      auto SQHNode = llvm::make_unique<SequenceHNode>(N);      for (Node &SN : *SQ) { @@ -382,6 +405,7 @@ Output::Output(raw_ostream &yout, void *context)        Out(yout),        Column(0),        ColumnAtFlowStart(0), +      ColumnAtMapFlowStart(0),        NeedBitValueComma(false),        NeedFlowSequenceComma(false),        EnumerationMatchFound(false), @@ -416,8 +440,13 @@ bool Output::preflightKey(const char *Key, bool Required, bool SameAsDefault,                            bool &UseDefault, void *&) {    UseDefault = false;    if (Required || !SameAsDefault) { -    this->newLineCheck(); -    this->paddedKey(Key); +    auto State = StateStack.back(); +    if (State == inFlowMapFirstKey || State == inFlowMapOtherKey) { +      flowKey(Key); +    } else { +      this->newLineCheck(); +      this->paddedKey(Key); +    }      return true;    }    return false; @@ -427,9 +456,24 @@ void Output::postflightKey(void *) {    if (StateStack.back() == inMapFirstKey) {      StateStack.pop_back();      StateStack.push_back(inMapOtherKey); +  } else if (StateStack.back() == inFlowMapFirstKey) { +    StateStack.pop_back(); +    StateStack.push_back(inFlowMapOtherKey);    }  } +void Output::beginFlowMapping() { +  StateStack.push_back(inFlowMapFirstKey); +  this->newLineCheck(); +  ColumnAtMapFlowStart = Column; +  output("{ "); +} + +void Output::endFlowMapping() { +  StateStack.pop_back(); +  this->outputUpToEndOfLine(" }"); +} +  void Output::beginDocuments() {    this->outputUpToEndOfLine("---");  } @@ -508,6 +552,13 @@ bool Output::matchEnumScalar(const char *Str, bool Match) {    return false;  } +bool Output::matchEnumFallback() { +  if (EnumerationMatchFound) +    return false; +  EnumerationMatchFound = true; +  return true; +} +  void Output::endEnumScalar() {    if (!EnumerationMatchFound)      llvm_unreachable("bad runtime enum value"); @@ -566,6 +617,24 @@ void Output::scalarString(StringRef &S, bool MustQuote) {    this->outputUpToEndOfLine("'"); // Ending single quote.  } +void Output::blockScalarString(StringRef &S) { +  if (!StateStack.empty()) +    newLineCheck(); +  output(" |"); +  outputNewLine(); + +  unsigned Indent = StateStack.empty() ? 1 : StateStack.size(); + +  auto Buffer = MemoryBuffer::getMemBuffer(S, "", false); +  for (line_iterator Lines(*Buffer, false); !Lines.is_at_end(); ++Lines) { +    for (unsigned I = 0; I < Indent; ++I) { +      output("  "); +    } +    output(*Lines); +    outputNewLine(); +  } +} +  void Output::setError(const Twine &message) {  } @@ -589,7 +658,9 @@ void Output::output(StringRef s) {  void Output::outputUpToEndOfLine(StringRef s) {    this->output(s); -  if (StateStack.empty() || StateStack.back() != inFlowSeq) +  if (StateStack.empty() || (StateStack.back() != inFlowSeq && +                             StateStack.back() != inFlowMapFirstKey && +                             StateStack.back() != inFlowMapOtherKey))      NeedsNewLine = true;  } @@ -615,7 +686,9 @@ void Output::newLineCheck() {    if (StateStack.back() == inSeq) {      OutputDash = true; -  } else if ((StateStack.size() > 1) && (StateStack.back() == inMapFirstKey) && +  } else if ((StateStack.size() > 1) && ((StateStack.back() == inMapFirstKey) || +             (StateStack.back() == inFlowSeq) || +             (StateStack.back() == inFlowMapFirstKey)) &&               (StateStack[StateStack.size() - 2] == inSeq)) {      --Indent;      OutputDash = true; @@ -640,6 +713,20 @@ void Output::paddedKey(StringRef key) {      output(" ");  } +void Output::flowKey(StringRef Key) { +  if (StateStack.back() == inFlowMapOtherKey) +    output(", "); +  if (Column > 70) { +    output("\n"); +    for (int I = 0; I < ColumnAtMapFlowStart; ++I) +      output(" "); +    Column = ColumnAtMapFlowStart; +    output("  "); +  } +  output(Key); +  output(": "); +} +  //===----------------------------------------------------------------------===//  //  traits for built-in types  //===----------------------------------------------------------------------===// @@ -669,7 +756,7 @@ StringRef ScalarTraits<StringRef>::input(StringRef Scalar, void *,    Val = Scalar;    return StringRef();  } -  +  void ScalarTraits<std::string>::output(const std::string &Val, void *,                                       raw_ostream &Out) {    Out << Val;  | 
