diff options
Diffstat (limited to 'contrib/llvm/lib/Support/YAMLTraits.cpp')
-rw-r--r-- | contrib/llvm/lib/Support/YAMLTraits.cpp | 124 |
1 files changed, 108 insertions, 16 deletions
diff --git a/contrib/llvm/lib/Support/YAMLTraits.cpp b/contrib/llvm/lib/Support/YAMLTraits.cpp index c87790e..6b59a16 100644 --- a/contrib/llvm/lib/Support/YAMLTraits.cpp +++ b/contrib/llvm/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> @@ -95,6 +97,10 @@ bool Input::nextDocument() { return ++DocIterator != Strm->end(); } +const Node *Input::getCurrentNode() const { + return CurrentNode ? CurrentNode->_node : nullptr; +} + bool Input::mapTag(StringRef Tag, bool Default) { std::string foundTag = CurrentNode->_node->getVerbatimTag(); if (foundTag.empty()) { @@ -167,10 +173,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 +210,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 +246,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 +314,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 +338,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) { @@ -377,11 +404,13 @@ bool Input::canElideEmptySequence() { // Output //===----------------------------------------------------------------------===// -Output::Output(raw_ostream &yout, void *context) +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), @@ -416,8 +445,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 +461,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("---"); } @@ -481,7 +530,7 @@ void Output::endFlowSequence() { bool Output::preflightFlowElement(unsigned, void *&) { if (NeedFlowSequenceComma) output(", "); - if (Column > 70) { + if (WrapColumn && Column > WrapColumn) { output("\n"); for (int i = 0; i < ColumnAtFlowStart; ++i) output(" "); @@ -508,6 +557,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 +622,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 +663,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 +691,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 +718,20 @@ void Output::paddedKey(StringRef key) { output(" "); } +void Output::flowKey(StringRef Key) { + if (StateStack.back() == inFlowMapOtherKey) + output(", "); + if (WrapColumn && Column > WrapColumn) { + output("\n"); + for (int I = 0; I < ColumnAtMapFlowStart; ++I) + output(" "); + Column = ColumnAtMapFlowStart; + output(" "); + } + output(Key); + output(": "); +} + //===----------------------------------------------------------------------===// // traits for built-in types //===----------------------------------------------------------------------===// @@ -669,7 +761,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; |