Index: lib/Support/YAMLTraits.cpp =================================================================== --- lib/Support/YAMLTraits.cpp +++ lib/Support/YAMLTraits.cpp @@ -349,54 +349,57 @@ EC = make_error_code(errc::invalid_argument); } -std::unique_ptr Input::createHNodes(Node *N) { +static StringRef getScalarNodeValue(ScalarNode *S, BumpPtrAllocator &Alloc) { SmallString<128> StringStorage; - if (ScalarNode *SN = dyn_cast(N)) { - StringRef KeyStr = SN->getValue(StringStorage); - if (!StringStorage.empty()) { - // Copy string to permanent storage - KeyStr = StringStorage.str().copy(StringAllocator); - } - return llvm::make_unique(N, KeyStr); - } else if (BlockScalarNode *BSN = dyn_cast(N)) { - StringRef ValueCopy = BSN->getValue().copy(StringAllocator); - return llvm::make_unique(N, ValueCopy); - } else if (SequenceNode *SQ = dyn_cast(N)) { + StringRef KeyStr = S->getValue(StringStorage); + // Copy string to permanent storage + if (!StringStorage.empty()) + return StringStorage.str().copy(Alloc); + return KeyStr; +} + +std::unique_ptr Input::createHNodes(Node *N) { + if (ScalarNode *SN = dyn_cast(N)) + return llvm::make_unique( + SN, getScalarNodeValue(SN, StringAllocator)); + + if (BlockScalarNode *BSN = dyn_cast(N)) + return llvm::make_unique( + N, BSN->getValue().copy(StringAllocator)); + + if (SequenceNode *SQ = dyn_cast(N)) { auto SQHNode = llvm::make_unique(N); for (Node &SN : *SQ) { auto Entry = this->createHNodes(&SN); - if (EC) - break; + if (!Entry) + return nullptr; SQHNode->Entries.push_back(std::move(Entry)); } return std::move(SQHNode); - } else if (MappingNode *Map = dyn_cast(N)) { - auto mapHNode = llvm::make_unique(N); + } + + if (MappingNode *Map = dyn_cast(N)) { + auto MapNode = llvm::make_unique(N); for (KeyValueNode &KVN : *Map) { - Node *KeyNode = KVN.getKey(); - ScalarNode *KeyScalar = dyn_cast(KeyNode); + ScalarNode *KeyScalar = dyn_cast(KVN.getKey()); 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 - KeyStr = StringStorage.str().copy(StringAllocator); + setError(KVN.getKey(), "Map key must be a scalar"); + return nullptr; } auto ValueHNode = this->createHNodes(KVN.getValue()); - if (EC) - break; - mapHNode->Mapping[KeyStr] = std::move(ValueHNode); + if (!ValueHNode) + return nullptr; + StringRef Key = getScalarNodeValue(KeyScalar, StringAllocator); + MapNode->Mapping[Key] = std::move(ValueHNode); } - return std::move(mapHNode); - } else if (isa(N)) { - return llvm::make_unique(N); - } else { - setError(N, "unknown node kind"); - return nullptr; + return std::move(MapNode); } + + if (isa(N)) + return llvm::make_unique(N); + + setError(N, "unknown node kind"); + return nullptr; } void Input::setError(const Twine &Message) {