X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=lib%2FSupport%2FYAMLTraits.cpp;h=526667fc59e7615bbb6bd7f54878a1942a658a40;hb=12af22e8cc217827cf4f118b0f5e4ebbda9925ae;hp=8fcef40d243f4499364034eb38632cf288b79b8b;hpb=2b45dd58b22da40bb08e82d1564ccecca4a700f5;p=oota-llvm.git diff --git a/lib/Support/YAMLTraits.cpp b/lib/Support/YAMLTraits.cpp index 8fcef40d243..526667fc59e 100644 --- a/lib/Support/YAMLTraits.cpp +++ b/lib/Support/YAMLTraits.cpp @@ -7,6 +7,7 @@ // //===----------------------------------------------------------------------===// +#include "llvm/Support/Errc.h" #include "llvm/Support/YAMLTraits.h" #include "llvm/ADT/Twine.h" #include "llvm/Support/Casting.h" @@ -14,6 +15,7 @@ #include "llvm/Support/Format.h" #include "llvm/Support/YAMLParser.h" #include "llvm/Support/raw_ostream.h" +#include #include using namespace llvm; using namespace yaml; @@ -40,18 +42,27 @@ void IO::setContext(void *Context) { // Input //===----------------------------------------------------------------------===// -Input::Input(StringRef InputContent, void *Ctxt) : IO(Ctxt), CurrentNode(NULL) { - Strm = new Stream(InputContent, SrcMgr); +Input::Input(StringRef InputContent, + void *Ctxt, + SourceMgr::DiagHandlerTy DiagHandler, + void *DiagHandlerCtxt) + : IO(Ctxt), + Strm(new Stream(InputContent, SrcMgr)), + CurrentNode(nullptr) { + if (DiagHandler) + SrcMgr.setDiagHandler(DiagHandler, DiagHandlerCtxt); DocIterator = Strm->begin(); } -error_code Input::error() { - return EC; +Input::~Input() { } -void Input::setDiagHandler(SourceMgr::DiagHandlerTy Handler, void *Ctxt) { - SrcMgr.setDiagHandler(Handler, Ctxt); -} +std::error_code Input::error() { return EC; } + +// Pin the vtables to this file. +void Input::HNode::anchor() {} +void Input::EmptyHNode::anchor() {} +void Input::ScalarHNode::anchor() {} bool Input::outputting() { return false; @@ -60,25 +71,43 @@ bool Input::outputting() { bool Input::setCurrentDocument() { if (DocIterator != Strm->end()) { Node *N = DocIterator->getRoot(); + if (!N) { + assert(Strm->failed() && "Root is NULL iff parsing failed"); + EC = make_error_code(errc::invalid_argument); + return false; + } + if (isa(N)) { // Empty files are allowed and ignored ++DocIterator; return setCurrentDocument(); } - CurrentNode = this->createHNodes(N); + TopNode.reset(this->createHNodes(N)); + CurrentNode = TopNode.get(); return true; } return false; } -void Input::nextDocument() { - ++DocIterator; +bool Input::nextDocument() { + return ++DocIterator != Strm->end(); +} + +bool Input::mapTag(StringRef Tag, bool Default) { + std::string foundTag = CurrentNode->_node->getVerbatimTag(); + if (foundTag.empty()) { + // If no tag found and 'Tag' is the default, say it was found. + return Default; + } + // Return true iff found tag matches supplied tag. + return Tag.equals(foundTag); } void Input::beginMapping() { if (EC) return; - MapHNode *MN = dyn_cast(CurrentNode); + // CurrentNode can be null if the document is empty. + MapHNode *MN = dyn_cast_or_null(CurrentNode); if (MN) { MN->ValidKeys.clear(); } @@ -89,6 +118,15 @@ bool Input::preflightKey(const char *Key, bool Required, bool, bool &UseDefault, UseDefault = false; if (EC) return false; + + // CurrentNode is null for empty documents, which is an error in case required + // nodes are present. + if (!CurrentNode) { + if (Required) + EC = make_error_code(errc::invalid_argument); + return false; + } + MapHNode *MN = dyn_cast(CurrentNode); if (!MN) { setError(CurrentNode, "not a mapping"); @@ -115,13 +153,13 @@ void Input::postflightKey(void *saveInfo) { void Input::endMapping() { if (EC) return; - MapHNode *MN = dyn_cast(CurrentNode); + // CurrentNode can be null if the document is empty. + MapHNode *MN = dyn_cast_or_null(CurrentNode); if (!MN) return; - for (MapHNode::NameToNode::iterator i = MN->Mapping.begin(), - End = MN->Mapping.end(); i != End; ++i) { - if (!MN->isValidKey(i->first)) { - setError(i->second, Twine("unknown key '") + i->first + "'"); + for (const auto &NN : MN->Mapping) { + if (!MN->isValidKey(NN.first())) { + setError(NN.second, Twine("unknown key '") + NN.first() + "'"); break; } } @@ -215,9 +253,8 @@ bool Input::bitSetMatch(const char *Str, bool) { return false; if (SequenceHNode *SQ = dyn_cast(CurrentNode)) { unsigned Index = 0; - for (std::vector::iterator i = SQ->Entries.begin(), - End = SQ->Entries.end(); i != End; ++i) { - if (ScalarHNode *SN = dyn_cast(*i)) { + for (HNode *N : SQ->Entries) { + if (ScalarHNode *SN = dyn_cast(N)) { if (SN->value().equals(Str)) { BitValuesUsed[Index] = true; return true; @@ -247,7 +284,7 @@ void Input::endBitSetScalar() { } } -void Input::scalarString(StringRef &S) { +void Input::scalarString(StringRef &S, bool) { if (ScalarHNode *SN = dyn_cast(CurrentNode)) { S = SN->value(); } else { @@ -256,6 +293,7 @@ void Input::scalarString(StringRef &S) { } void Input::setError(HNode *hnode, const Twine &message) { + assert(hnode && "HNode must not be NULL"); this->setError(hnode->_node, message); } @@ -271,53 +309,55 @@ Input::HNode *Input::createHNodes(Node *N) { if (!StringStorage.empty()) { // Copy string to permanent storage unsigned Len = StringStorage.size(); - char *Buf = Allocator.Allocate(Len); + char *Buf = StringAllocator.Allocate(Len); memcpy(Buf, &StringStorage[0], Len); KeyStr = StringRef(Buf, Len); } - return new (Allocator) ScalarHNode(N, KeyStr); + return new ScalarHNode(N, KeyStr); } else if (SequenceNode *SQ = dyn_cast(N)) { - SequenceHNode *SQHNode = new (Allocator) SequenceHNode(N); - for (SequenceNode::iterator i = SQ->begin(), End = SQ->end(); i != End; - ++i) { - HNode *Entry = this->createHNodes(i); + SequenceHNode *SQHNode = new SequenceHNode(N); + for (Node &SN : *SQ) { + HNode *Entry = this->createHNodes(&SN); if (EC) break; SQHNode->Entries.push_back(Entry); } return SQHNode; } else if (MappingNode *Map = dyn_cast(N)) { - MapHNode *mapHNode = new (Allocator) MapHNode(N); - for (MappingNode::iterator i = Map->begin(), End = Map->end(); i != End; - ++i) { - ScalarNode *KeyScalar = dyn_cast(i->getKey()); + MapHNode *mapHNode = new MapHNode(N); + for (KeyValueNode &KVN : *Map) { + Node *KeyNode = KVN.getKey(); + ScalarNode *KeyScalar = dyn_cast(KeyNode); + if (!KeyScalar) { + setError(KeyNode, "Map key must be a scalar"); + break; + } StringStorage.clear(); StringRef KeyStr = KeyScalar->getValue(StringStorage); if (!StringStorage.empty()) { // Copy string to permanent storage unsigned Len = StringStorage.size(); - char *Buf = Allocator.Allocate(Len); + char *Buf = StringAllocator.Allocate(Len); memcpy(Buf, &StringStorage[0], Len); KeyStr = StringRef(Buf, Len); } - HNode *ValueHNode = this->createHNodes(i->getValue()); + HNode *ValueHNode = this->createHNodes(KVN.getValue()); if (EC) break; mapHNode->Mapping[KeyStr] = ValueHNode; } return mapHNode; } else if (isa(N)) { - return new (Allocator) EmptyHNode(N); + return new EmptyHNode(N); } else { setError(N, "unknown node kind"); - return NULL; + return nullptr; } } bool Input::MapHNode::isValidKey(StringRef Key) { - for (SmallVector::iterator i = ValidKeys.begin(), - End = ValidKeys.end(); i != End; ++i) { - if (Key.equals(*i)) + for (const char *K : ValidKeys) { + if (Key.equals(K)) return true; } return false; @@ -327,6 +367,22 @@ void Input::setError(const Twine &Message) { this->setError(CurrentNode, Message); } +bool Input::canElideEmptySequence() { + return false; +} + +Input::MapHNode::~MapHNode() { + for (auto &N : Mapping) + delete N.second; +} + +Input::SequenceHNode::~SequenceHNode() { + for (HNode *N : Entries) + delete N; +} + + + //===----------------------------------------------------------------------===// // Output //===----------------------------------------------------------------------===// @@ -354,6 +410,14 @@ void Output::beginMapping() { NeedsNewLine = true; } +bool Output::mapTag(StringRef Tag, bool Use) { + if (Use) { + this->output(" "); + this->output(Tag); + } + return Use; +} + void Output::endMapping() { StateStack.pop_back(); } @@ -411,8 +475,8 @@ void Output::postflightElement(void *) { } unsigned Output::beginFlowSequence() { - this->newLineCheck(); StateStack.push_back(inFlowSeq); + this->newLineCheck(); ColumnAtFlowStart = Column; output("[ "); NeedFlowSequenceComma = false; @@ -481,10 +545,16 @@ void Output::endBitSetScalar() { this->outputUpToEndOfLine(" ]"); } -void Output::scalarString(StringRef &S) { +void Output::scalarString(StringRef &S, bool MustQuote) { this->newLineCheck(); - if (S.find('\n') == StringRef::npos) { - // No embedded new-line chars, just print string. + if (S.empty()) { + // Print '' for the empty string because leaving the field empty is not + // allowed. + this->outputUpToEndOfLine("''"); + return; + } + if (!MustQuote) { + // Only quote if we must. this->outputUpToEndOfLine(S); return; } @@ -509,6 +579,19 @@ void Output::scalarString(StringRef &S) { void Output::setError(const Twine &message) { } +bool Output::canElideEmptySequence() { + // Normally, with an optional key/value where the value is an empty sequence, + // the whole key/value can be not written. But, that produces wrong yaml + // if the key/value is the only thing in the map and the map is used in + // a sequence. This detects if the this sequence is the first key/value + // in map that itself is embedded in a sequnce. + if (StateStack.size() < 2) + return true; + if (StateStack.back() != inMapFirstKey) + return true; + return (StateStack[StateStack.size()-2] != inSeq); +} + void Output::output(StringRef s) { Column += s.size(); Out << s; @@ -596,6 +679,17 @@ StringRef ScalarTraits::input(StringRef Scalar, void *, Val = Scalar; return StringRef(); } + +void ScalarTraits::output(const std::string &Val, void *, + raw_ostream &Out) { + Out << Val; +} + +StringRef ScalarTraits::input(StringRef Scalar, void *, + std::string &Val) { + Val = Scalar.str(); + return StringRef(); +} void ScalarTraits::output(const uint8_t &Val, void *, raw_ostream &Out) {