<div dir="ltr"><div dir="ltr"><div dir="ltr">Hello Tim,<br><br>Some of you recent commits broke tests on one of our win builders:<br><a href="http://lab.llvm.org:8011/builders/llvm-clang-x86_64-expensive-checks-win/builds/16366">http://lab.llvm.org:8011/builders/llvm-clang-x86_64-expensive-checks-win/builds/16366</a><br><br>Please have a look ASAP?<br><br>Thanks<br><br>Galina<br><br>. . .<br>Failing Tests (18):<br>    LLVM-Unit :: BinaryFormat/./BinaryFormatTests.exe/MsgPackDocument.TestOutputYAMLArray<br>    LLVM-Unit :: BinaryFormat/./BinaryFormatTests.exe/MsgPackDocument.TestOutputYAMLMap<br>    LLVM-Unit :: BinaryFormat/./BinaryFormatTests.exe/MsgPackDocument.TestOutputYAMLMapHex<br>. . .<br>    LLVM :: CodeGen/AMDGPU/attr-amdgpu-flat-work-group-size-v3.ll<br>    LLVM :: CodeGen/AMDGPU/hsa-metadata-deduce-ro-arg-v3.ll<br>    LLVM :: CodeGen/AMDGPU/hsa-metadata-enqueu-kernel-v3.ll<br>    LLVM :: CodeGen/AMDGPU/hsa-metadata-from-llvm-ir-full-v3.ll<br>    LLVM :: CodeGen/AMDGPU/hsa-metadata-hidden-args-v3.ll<br>    LLVM :: CodeGen/AMDGPU/hsa-metadata-images-v3.ll<br>    LLVM :: CodeGen/AMDGPU/hsa-metadata-invalid-ocl-version-1-v3.ll<br>    LLVM :: CodeGen/AMDGPU/hsa-metadata-invalid-ocl-version-2-v3.ll<br>    LLVM :: CodeGen/AMDGPU/hsa-metadata-invalid-ocl-version-3-v3.ll<br>    LLVM :: CodeGen/AMDGPU/hsa-metadata-kernel-code-props-v3.ll<br>    LLVM :: MC/AMDGPU/hsa-metadata-kernel-args-v3.s<br>    LLVM :: MC/AMDGPU/hsa-metadata-kernel-attrs-v3.s<br>    LLVM :: MC/AMDGPU/hsa-metadata-kernel-code-props-v3.s<br>    LLVM :: MC/AMDGPU/hsa-v3.s<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Mar 13, 2019 at 11:53 AM Tim Renouf via llvm-commits <<a href="mailto:llvm-commits@lists.llvm.org">llvm-commits@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Author: tpr<br>
Date: Wed Mar 13 11:54:47 2019<br>
New Revision: 356080<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=356080&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=356080&view=rev</a><br>
Log:<br>
[MsgPack] New MsgPackDocument class<br>
<br>
Summary:<br>
A class that exposes a simple in-memory representation of a document of<br>
MsgPack objects, that can be read from and written to MsgPack, read from<br>
and written to YAML, and inspected and modified in memory. This is<br>
intended to be a lighter-weight (in terms of memory allocations)<br>
replacement for MsgPackTypes.<br>
<br>
Two subsequent changes will:<br>
1. switch AMDGPU HSA metadata to using MsgPackDocument instead of<br>
   MsgPackTypes;<br>
2. add MsgPack AMDGPU PAL metadata via MsgPackDocument.<br>
<br>
Differential Revision: <a href="https://reviews.llvm.org/D57023" rel="noreferrer" target="_blank">https://reviews.llvm.org/D57023</a><br>
<br>
Change-Id: Ie15a054831d5a6467c5867c064c8f8f6b80270e1<br>
<br>
Added:<br>
    llvm/trunk/include/llvm/BinaryFormat/MsgPackDocument.h<br>
    llvm/trunk/lib/BinaryFormat/MsgPackDocument.cpp<br>
    llvm/trunk/lib/BinaryFormat/MsgPackDocumentYAML.cpp<br>
    llvm/trunk/unittests/BinaryFormat/MsgPackDocumentTest.cpp<br>
Modified:<br>
    llvm/trunk/lib/BinaryFormat/CMakeLists.txt<br>
    llvm/trunk/unittests/BinaryFormat/CMakeLists.txt<br>
<br>
Added: llvm/trunk/include/llvm/BinaryFormat/MsgPackDocument.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/BinaryFormat/MsgPackDocument.h?rev=356080&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/BinaryFormat/MsgPackDocument.h?rev=356080&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/BinaryFormat/MsgPackDocument.h (added)<br>
+++ llvm/trunk/include/llvm/BinaryFormat/MsgPackDocument.h Wed Mar 13 11:54:47 2019<br>
@@ -0,0 +1,385 @@<br>
+//===-- MsgPackDocument.h - MsgPack Document --------------------*- C++ -*-===//<br>
+//<br>
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.<br>
+// See <a href="https://llvm.org/LICENSE.txt" rel="noreferrer" target="_blank">https://llvm.org/LICENSE.txt</a> for license information.<br>
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception<br>
+//<br>
+//===----------------------------------------------------------------------===//<br>
+///<br>
+/// This file declares a class that exposes a simple in-memory representation<br>
+/// of a document of MsgPack objects, that can be read from MsgPack, written to<br>
+/// MsgPack, and inspected and modified in memory. This is intended to be a<br>
+/// lighter-weight (in terms of memory allocations) replacement for<br>
+/// MsgPackTypes.<br>
+///<br>
+//===----------------------------------------------------------------------===//<br>
+<br>
+#ifndef LLVM_BINARYFORMAT_MSGPACKDOCUMENT_H<br>
+#define LLVM_BINARYFORMAT_MSGPACKDOCUMENT_H<br>
+<br>
+#include "llvm/BinaryFormat/MsgPackReader.h"<br>
+#include <map><br>
+<br>
+namespace llvm {<br>
+namespace msgpack {<br>
+<br>
+class ArrayDocNode;<br>
+class Document;<br>
+class MapDocNode;<br>
+<br>
+/// The kind of a DocNode and its owning Document.<br>
+struct KindAndDocument {<br>
+  Document *Doc;<br>
+  Type Kind;<br>
+};<br>
+<br>
+/// A node in a MsgPack Document. This is a simple copyable and<br>
+/// passable-by-value type that does not own any memory.<br>
+class DocNode {<br>
+  friend Document;<br>
+<br>
+public:<br>
+  typedef std::map<DocNode, DocNode> MapTy;<br>
+  typedef std::vector<DocNode> ArrayTy;<br>
+<br>
+private:<br>
+  // Using KindAndDocument allows us to squeeze Kind and a pointer to the<br>
+  // owning Document into the same word. Having a pointer to the owning<br>
+  // Document makes the API of DocNode more convenient, and allows its use in<br>
+  // YAMLIO.<br>
+  const KindAndDocument *KindAndDoc;<br>
+<br>
+protected:<br>
+  // The union of different values.<br>
+  union {<br>
+    int64_t Int;<br>
+    uint64_t UInt;<br>
+    bool Bool;<br>
+    double Float;<br>
+    StringRef Raw;<br>
+    ArrayTy *Array;<br>
+    MapTy *Map;<br>
+  };<br>
+<br>
+public:<br>
+  DocNode() : KindAndDoc(nullptr) {}<br>
+<br>
+  // Type methods<br>
+  bool isMap() const { return getKind() == Type::Map; }<br>
+  bool isArray() const { return getKind() == Type::Array; }<br>
+  bool isScalar() const { return !isMap() && !isArray(); }<br>
+  bool isString() const { return getKind() == Type::String; }<br>
+<br>
+  // Accessors<br>
+  bool isEmpty() const { return !KindAndDoc; }<br>
+  Type getKind() const { return KindAndDoc->Kind; }<br>
+  Document *getDocument() const { return KindAndDoc->Doc; }<br>
+<br>
+  int64_t &getInt() {<br>
+    assert(getKind() == Type::Int);<br>
+    return Int;<br>
+  }<br>
+<br>
+  uint64_t &getUInt() {<br>
+    assert(getKind() == Type::UInt);<br>
+    return UInt;<br>
+  }<br>
+<br>
+  bool &getBool() {<br>
+    assert(getKind() == Type::Boolean);<br>
+    return Bool;<br>
+  }<br>
+<br>
+  double &getFloat() {<br>
+    assert(getKind() == Type::Float);<br>
+    return Float;<br>
+  }<br>
+<br>
+  int64_t getInt() const {<br>
+    assert(getKind() == Type::Int);<br>
+    return Int;<br>
+  }<br>
+<br>
+  uint64_t getUInt() const {<br>
+    assert(getKind() == Type::UInt);<br>
+    return UInt;<br>
+  }<br>
+<br>
+  bool getBool() const {<br>
+    assert(getKind() == Type::Boolean);<br>
+    return Bool;<br>
+  }<br>
+<br>
+  double getFloat() const {<br>
+    assert(getKind() == Type::Float);<br>
+    return Float;<br>
+  }<br>
+<br>
+  StringRef getString() const {<br>
+    assert(getKind() == Type::String);<br>
+    return Raw;<br>
+  }<br>
+<br>
+  /// Get an ArrayDocNode for an array node. If Convert, convert the node to an<br>
+  /// array node if necessary.<br>
+  ArrayDocNode &getArray(bool Convert = false) {<br>
+    if (getKind() != Type::Array) {<br>
+      assert(Convert);<br>
+      convertToArray();<br>
+    }<br>
+    // This could be a static_cast, except ArrayDocNode is a forward reference.<br>
+    return *reinterpret_cast<ArrayDocNode *>(this);<br>
+  }<br>
+<br>
+  /// Get a MapDocNode for a map node. If Convert, convert the node to a map<br>
+  /// node if necessary.<br>
+  MapDocNode &getMap(bool Convert = false) {<br>
+    if (getKind() != Type::Map) {<br>
+      assert(Convert);<br>
+      convertToMap();<br>
+    }<br>
+    // This could be a static_cast, except MapDocNode is a forward reference.<br>
+    return *reinterpret_cast<MapDocNode *>(this);<br>
+  }<br>
+<br>
+  /// Comparison operator, used for map keys.<br>
+  friend bool operator<(const DocNode &Lhs, const DocNode &Rhs) {<br>
+    // This has to cope with one or both of the nodes being default-constructed,<br>
+    // such that KindAndDoc is not set.<br>
+    if (Lhs.KindAndDoc != Rhs.KindAndDoc) {<br>
+      if (!Rhs.KindAndDoc)<br>
+        return false;<br>
+      if (!Lhs.KindAndDoc)<br>
+        return true;<br>
+      return (unsigned)Lhs.getKind() < (unsigned)Rhs.getKind();<br>
+    }<br>
+    switch (Lhs.getKind()) {<br>
+    case Type::Int:<br>
+      return Lhs.Int < Rhs.Int;<br>
+    case Type::UInt:<br>
+      return Lhs.UInt < Rhs.UInt;<br>
+    case Type::Nil:<br>
+      return false;<br>
+    case Type::Boolean:<br>
+      return Lhs.Bool < Rhs.Bool;<br>
+    case Type::Float:<br>
+      return Lhs.Float < Rhs.Float;<br>
+    case Type::String:<br>
+    case Type::Binary:<br>
+      return Lhs.Raw < Rhs.Raw;<br>
+    default:<br>
+      llvm_unreachable("bad map key type");<br>
+    }<br>
+  }<br>
+<br>
+  /// Equality operator<br>
+  friend bool operator==(const DocNode &Lhs, const DocNode &Rhs) {<br>
+    return !(Lhs < Rhs) && !(Rhs < Lhs);<br>
+  }<br>
+<br>
+  /// Convert this node to a string, assuming it is scalar.<br>
+  std::string toString() const;<br>
+<br>
+  /// Convert the StringRef and use it to set this DocNode (assuming scalar). If<br>
+  /// it is a string, copy the string into the Document's strings list so we do<br>
+  /// not rely on S having a lifetime beyond this call. Tag is "" or a YAML tag.<br>
+  StringRef fromString(StringRef S, StringRef Tag = "");<br>
+<br>
+private:<br>
+  // Private constructor setting KindAndDoc, used by methods in Document.<br>
+  DocNode(const KindAndDocument *KindAndDoc) : KindAndDoc(KindAndDoc) {}<br>
+<br>
+  void convertToArray();<br>
+  void convertToMap();<br>
+};<br>
+<br>
+/// A DocNode that is a map.<br>
+class MapDocNode : public DocNode {<br>
+public:<br>
+  MapDocNode() {}<br>
+  MapDocNode(DocNode &N) : DocNode(N) { assert(getKind() == Type::Map); }<br>
+<br>
+  // Map access methods.<br>
+  size_t size() const { return Map->size(); }<br>
+  bool empty() const { return !size(); }<br>
+  MapTy::iterator begin() { return Map->begin(); }<br>
+  MapTy::iterator end() { return Map->end(); }<br>
+  MapTy::iterator find(DocNode Key) { return Map->find(Key); }<br>
+  MapTy::iterator find(StringRef Key);<br>
+  /// Member access. The string data must remain valid for the lifetime of the<br>
+  /// Document.<br>
+  DocNode &operator[](StringRef S);<br>
+  /// Member access.<br>
+  DocNode &operator[](DocNode Key);<br>
+};<br>
+<br>
+/// A DocNode that is an array.<br>
+class ArrayDocNode : public DocNode {<br>
+public:<br>
+  ArrayDocNode() {}<br>
+  ArrayDocNode(DocNode &N) : DocNode(N) { assert(getKind() == Type::Array); }<br>
+<br>
+  // Array access methods.<br>
+  size_t size() const { return Array->size(); }<br>
+  bool empty() const { return !size(); }<br>
+  ArrayTy::iterator begin() { return Array->begin(); }<br>
+  ArrayTy::iterator end() { return Array->end(); }<br>
+  void push_back(DocNode N) {<br>
+    assert(N.getDocument() == getDocument());<br>
+    Array->push_back(N);<br>
+  }<br>
+<br>
+  /// Element access. This extends the array if necessary.<br>
+  DocNode &operator[](size_t Index);<br>
+};<br>
+<br>
+/// Simple in-memory representation of a document of msgpack objects with<br>
+/// ability to find and create array and map elements.  Does not currently cope<br>
+/// with any extension types.<br>
+class Document {<br>
+  // Maps, arrays and strings used by nodes in the document. No attempt is made<br>
+  // to free unused ones.<br>
+  std::vector<std::unique_ptr<DocNode::MapTy>> Maps;<br>
+  std::vector<std::unique_ptr<DocNode::ArrayTy>> Arrays;<br>
+  std::vector<std::unique_ptr<char[]>> Strings;<br>
+<br>
+  // The root node of the document.<br>
+  DocNode Root;<br>
+<br>
+  // The KindAndDocument structs pointed to by nodes in the document.<br>
+  KindAndDocument KindAndDocs[size_t(Type::Extension) + 1];<br>
+<br>
+  // Whether YAML output uses hex for UInt.<br>
+  bool HexMode = false;<br>
+<br>
+public:<br>
+  Document() {<br>
+    clear();<br>
+    for (unsigned T = 0; T != size_t(Type::Extension) + 1; ++T)<br>
+      KindAndDocs[T] = {this, Type(T)};<br>
+  }<br>
+<br>
+  /// Get ref to the document's root element.<br>
+  DocNode &getRoot() { return Root; }<br>
+<br>
+  /// Restore the Document to an empty state.<br>
+  void clear() { getRoot() = getNode(); }<br>
+<br>
+  /// Create a nil node associated with this Document.<br>
+  DocNode getNode() {<br>
+    auto N = DocNode(&KindAndDocs[size_t(Type::Nil)]);<br>
+    return N;<br>
+  }<br>
+<br>
+  /// Create an Int node associated with this Document.<br>
+  DocNode getNode(int64_t V) {<br>
+    auto N = DocNode(&KindAndDocs[size_t(Type::Int)]);<br>
+    N.Int = V;<br>
+    return N;<br>
+  }<br>
+<br>
+  /// Create an Int node associated with this Document.<br>
+  DocNode getNode(int V) {<br>
+    auto N = DocNode(&KindAndDocs[size_t(Type::Int)]);<br>
+    N.Int = V;<br>
+    return N;<br>
+  }<br>
+<br>
+  /// Create a UInt node associated with this Document.<br>
+  DocNode getNode(uint64_t V) {<br>
+    auto N = DocNode(&KindAndDocs[size_t(Type::UInt)]);<br>
+    N.UInt = V;<br>
+    return N;<br>
+  }<br>
+<br>
+  /// Create a UInt node associated with this Document.<br>
+  DocNode getNode(unsigned V) {<br>
+    auto N = DocNode(&KindAndDocs[size_t(Type::UInt)]);<br>
+    N.UInt = V;<br>
+    return N;<br>
+  }<br>
+<br>
+  /// Create a Boolean node associated with this Document.<br>
+  DocNode getNode(bool V) {<br>
+    auto N = DocNode(&KindAndDocs[size_t(Type::Boolean)]);<br>
+    N.Bool = V;<br>
+    return N;<br>
+  }<br>
+<br>
+  /// Create a Float node associated with this Document.<br>
+  DocNode getNode(double V) {<br>
+    auto N = DocNode(&KindAndDocs[size_t(Type::Float)]);<br>
+    N.Float = V;<br>
+    return N;<br>
+  }<br>
+<br>
+  /// Create a String node associated with this Document. If !Copy, the passed<br>
+  /// string must remain valid for the lifetime of the Document.<br>
+  DocNode getNode(StringRef V, bool Copy = false) {<br>
+    if (Copy)<br>
+      V = addString(V);<br>
+    auto N = DocNode(&KindAndDocs[size_t(Type::String)]);<br>
+    N.Raw = V;<br>
+    return N;<br>
+  }<br>
+<br>
+  /// Create a String node associated with this Document. If !Copy, the passed<br>
+  /// string must remain valid for the lifetime of the Document.<br>
+  DocNode getNode(const char *V, bool Copy = false) {<br>
+    return getNode(StringRef(V), Copy);<br>
+  }<br>
+<br>
+  /// Create an empty Map node associated with this Document.<br>
+  MapDocNode getMapNode() {<br>
+    auto N = DocNode(&KindAndDocs[size_t(Type::Map)]);<br>
+    Maps.push_back(std::unique_ptr<DocNode::MapTy>(new DocNode::MapTy));<br>
+    N.Map = Maps.back().get();<br>
+    return N.getMap();<br>
+  }<br>
+<br>
+  /// Create an empty Array node associated with this Document.<br>
+  ArrayDocNode getArrayNode() {<br>
+    auto N = DocNode(&KindAndDocs[size_t(Type::Array)]);<br>
+    Arrays.push_back(std::unique_ptr<DocNode::ArrayTy>(new DocNode::ArrayTy));<br>
+    N.Array = Arrays.back().get();<br>
+    return N.getArray();<br>
+  }<br>
+<br>
+  /// Read a MsgPack document from a binary MsgPack blob.<br>
+  /// The blob data must remain valid for the lifetime of this Document (because<br>
+  /// a string object in the document contains a StringRef into the original<br>
+  /// blob).<br>
+  /// If Multi, then this sets root to an array and adds top-level objects to<br>
+  /// it. If !Multi, then it only reads a single top-level object, even if there<br>
+  /// are more, and sets root to that.<br>
+  /// Returns false if failed due to illegal format.<br>
+  bool readFromBlob(StringRef Blob, bool Multi);<br>
+<br>
+  /// Write a MsgPack document to a binary MsgPack blob.<br>
+  void writeToBlob(std::string &Blob);<br>
+<br>
+  /// Copy a string into the Document's strings list, and return the copy that<br>
+  /// is owned by the Document.<br>
+  StringRef addString(StringRef S) {<br>
+    Strings.push_back(std::unique_ptr<char[]>(new char[S.size()]));<br>
+    memcpy(&Strings.back()[0], S.data(), S.size());<br>
+    return StringRef(&Strings.back()[0], S.size());<br>
+  }<br>
+<br>
+  /// Set whether YAML output uses hex for UInt. Default off.<br>
+  void setHexMode(bool Val = true) { HexMode = Val; }<br>
+<br>
+  /// Get Hexmode flag.<br>
+  bool getHexMode() const { return HexMode; }<br>
+<br>
+  /// Convert MsgPack Document to YAML text.<br>
+  void toYAML(raw_ostream &OS);<br>
+<br>
+  /// Read YAML text into the MsgPack document. Returns false on failure.<br>
+  bool fromYAML(StringRef S);<br>
+};<br>
+<br>
+} // namespace msgpack<br>
+} // namespace llvm<br>
+<br>
+#endif // LLVM_BINARYFORMAT_MSGPACKDOCUMENT_H<br>
<br>
Modified: llvm/trunk/lib/BinaryFormat/CMakeLists.txt<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/BinaryFormat/CMakeLists.txt?rev=356080&r1=356079&r2=356080&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/BinaryFormat/CMakeLists.txt?rev=356080&r1=356079&r2=356080&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/BinaryFormat/CMakeLists.txt (original)<br>
+++ llvm/trunk/lib/BinaryFormat/CMakeLists.txt Wed Mar 13 11:54:47 2019<br>
@@ -2,6 +2,8 @@ add_llvm_library(LLVMBinaryFormat<br>
   AMDGPUMetadataVerifier.cpp<br>
   Dwarf.cpp<br>
   Magic.cpp<br>
+  MsgPackDocument.cpp<br>
+  MsgPackDocumentYAML.cpp<br>
   MsgPackReader.cpp<br>
   MsgPackTypes.cpp<br>
   MsgPackWriter.cpp<br>
<br>
Added: llvm/trunk/lib/BinaryFormat/MsgPackDocument.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/BinaryFormat/MsgPackDocument.cpp?rev=356080&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/BinaryFormat/MsgPackDocument.cpp?rev=356080&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/lib/BinaryFormat/MsgPackDocument.cpp (added)<br>
+++ llvm/trunk/lib/BinaryFormat/MsgPackDocument.cpp Wed Mar 13 11:54:47 2019<br>
@@ -0,0 +1,245 @@<br>
+//===-- MsgPackDocument.cpp - MsgPack Document --------------------------*-===//<br>
+//<br>
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.<br>
+// See <a href="https://llvm.org/LICENSE.txt" rel="noreferrer" target="_blank">https://llvm.org/LICENSE.txt</a> for license information.<br>
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception<br>
+//<br>
+//===----------------------------------------------------------------------===//<br>
+///<br>
+/// This file implements a class that exposes a simple in-memory representation<br>
+/// of a document of MsgPack objects, that can be read from MsgPack, written to<br>
+/// MsgPack, and inspected and modified in memory. This is intended to be a<br>
+/// lighter-weight (in terms of memory allocations) replacement for<br>
+/// MsgPackTypes.<br>
+///<br>
+//===----------------------------------------------------------------------===//<br>
+<br>
+#include "llvm/BinaryFormat/MsgPackDocument.h"<br>
+#include "llvm/BinaryFormat/MsgPackWriter.h"<br>
+<br>
+using namespace llvm;<br>
+using namespace msgpack;<br>
+<br>
+// Convert this DocNode into an empty array.<br>
+void DocNode::convertToArray() { *this = getDocument()->getArrayNode(); }<br>
+<br>
+// Convert this DocNode into an empty map.<br>
+void DocNode::convertToMap() { *this = getDocument()->getMapNode(); }<br>
+<br>
+/// Find the key in the MapDocNode.<br>
+DocNode::MapTy::iterator MapDocNode::find(StringRef S) {<br>
+  return find(getDocument()->getNode(S));<br>
+}<br>
+<br>
+/// Member access for MapDocNode. The string data must remain valid for the<br>
+/// lifetime of the Document.<br>
+DocNode &MapDocNode::operator[](StringRef S) {<br>
+  return (*this)[getDocument()->getNode(S)];<br>
+}<br>
+<br>
+/// Member access for MapDocNode.<br>
+DocNode &MapDocNode::operator[](DocNode Key) {<br>
+  assert(!Key.isEmpty());<br>
+  MapTy::value_type Entry(Key, DocNode());<br>
+  auto ItAndInserted = Map->insert(Entry);<br>
+  if (ItAndInserted.second) {<br>
+    // Ensure a new element has its KindAndDoc initialized.<br>
+    ItAndInserted.first->second = getDocument()->getNode();<br>
+  }<br>
+  return ItAndInserted.first->second;<br>
+}<br>
+<br>
+/// Array element access. This extends the array if necessary.<br>
+DocNode &ArrayDocNode::operator[](size_t Index) {<br>
+  if (size() <= Index) {<br>
+    // Ensure new elements have their KindAndDoc initialized.<br>
+    Array->resize(Index + 1, getDocument()->getNode());<br>
+  }<br>
+  return (*Array)[Index];<br>
+}<br>
+<br>
+// A level in the document reading stack.<br>
+struct StackLevel {<br>
+  DocNode Node;<br>
+  size_t Length;<br>
+  // Points to map entry when we have just processed a map key.<br>
+  DocNode *MapEntry;<br>
+};<br>
+<br>
+// Read a document from a binary msgpack blob.<br>
+// The blob data must remain valid for the lifetime of this Document (because a<br>
+// string object in the document contains a StringRef into the original blob).<br>
+// If Multi, then this sets root to an array and adds top-level objects to it.<br>
+// If !Multi, then it only reads a single top-level object, even if there are<br>
+// more, and sets root to that.<br>
+// Returns false if failed due to illegal format.<br>
+bool Document::readFromBlob(StringRef Blob, bool Multi) {<br>
+  msgpack::Reader MPReader(Blob);<br>
+  SmallVector<StackLevel, 4> Stack;<br>
+  if (Multi) {<br>
+    // Create the array for multiple top-level objects.<br>
+    Root = getArrayNode();<br>
+    Stack.push_back(StackLevel({Root, (size_t)-1, nullptr}));<br>
+  }<br>
+  do {<br>
+    // On to next element (or key if doing a map key next).<br>
+    // Read the value.<br>
+    Object Obj;<br>
+    if (!MPReader.read(Obj)) {<br>
+      if (Multi && Stack.size() == 1) {<br>
+        // OK to finish here as we've just done a top-level element with Multi<br>
+        break;<br>
+      }<br>
+      return false; // Finished too early<br>
+    }<br>
+    // Convert it into a DocNode.<br>
+    DocNode Node;<br>
+    switch (Obj.Kind) {<br>
+    case Type::Nil:<br>
+      Node = getNode();<br>
+      break;<br>
+    case Type::Int:<br>
+      Node = getNode(Obj.Int);<br>
+      break;<br>
+    case Type::UInt:<br>
+      Node = getNode(Obj.UInt);<br>
+      break;<br>
+    case Type::Boolean:<br>
+      Node = getNode(Obj.Bool);<br>
+      break;<br>
+    case Type::Float:<br>
+      Node = getNode(Obj.Float);<br>
+      break;<br>
+    case Type::String:<br>
+      Node = getNode(Obj.Raw);<br>
+      break;<br>
+    case Type::Map:<br>
+      Node = getMapNode();<br>
+      break;<br>
+    case Type::Array:<br>
+      Node = getArrayNode();<br>
+      break;<br>
+    default:<br>
+      return false; // Raw and Extension not supported<br>
+    }<br>
+<br>
+    // Store it.<br>
+    if (Stack.empty())<br>
+      Root = Node;<br>
+    else if (Stack.back().Node.getKind() == Type::Array) {<br>
+      // Reading an array entry.<br>
+      auto &Array = Stack.back().Node.getArray();<br>
+      Array.push_back(Node);<br>
+    } else {<br>
+      auto &Map = Stack.back().Node.getMap();<br>
+      if (!Stack.back().MapEntry) {<br>
+        // Reading a map key.<br>
+        Stack.back().MapEntry = &Map[Node];<br>
+      } else {<br>
+        // Reading the value for the map key read in the last iteration.<br>
+        *Stack.back().MapEntry = Node;<br>
+        Stack.back().MapEntry = nullptr;<br>
+      }<br>
+    }<br>
+<br>
+    // See if we're starting a new array or map.<br>
+    switch (Node.getKind()) {<br>
+    case msgpack::Type::Array:<br>
+    case msgpack::Type::Map:<br>
+      Stack.push_back(StackLevel({Node, Obj.Length, nullptr}));<br>
+      break;<br>
+    default:<br>
+      break;<br>
+    }<br>
+<br>
+    // Pop finished stack levels.<br>
+    while (!Stack.empty()) {<br>
+      if (Stack.back().Node.getKind() == msgpack::Type::Array) {<br>
+        if (Stack.back().Node.getArray().size() != Stack.back().Length)<br>
+          break;<br>
+      } else {<br>
+        if (Stack.back().MapEntry ||<br>
+            Stack.back().Node.getMap().size() != Stack.back().Length)<br>
+          break;<br>
+      }<br>
+      Stack.pop_back();<br>
+    }<br>
+  } while (!Stack.empty());<br>
+  return true;<br>
+}<br>
+<br>
+struct WriterStackLevel {<br>
+  DocNode Node;<br>
+  DocNode::MapTy::iterator MapIt;<br>
+  DocNode::ArrayTy::iterator ArrayIt;<br>
+  bool OnKey;<br>
+};<br>
+<br>
+/// Write a MsgPack document to a binary MsgPack blob.<br>
+void Document::writeToBlob(std::string &Blob) {<br>
+  Blob.clear();<br>
+  raw_string_ostream OS(Blob);<br>
+  msgpack::Writer MPWriter(OS);<br>
+  SmallVector<WriterStackLevel, 4> Stack;<br>
+  DocNode Node = getRoot();<br>
+  for (;;) {<br>
+    switch (Node.getKind()) {<br>
+    case Type::Array:<br>
+      MPWriter.writeArraySize(Node.getArray().size());<br>
+      Stack.push_back(<br>
+          {Node, DocNode::MapTy::iterator(), Node.getArray().begin(), false});<br>
+      break;<br>
+    case Type::Map:<br>
+      MPWriter.writeMapSize(Node.getMap().size());<br>
+      Stack.push_back(<br>
+          {Node, Node.getMap().begin(), DocNode::ArrayTy::iterator(), true});<br>
+      break;<br>
+    case Type::Nil:<br>
+      MPWriter.writeNil();<br>
+      break;<br>
+    case Type::Boolean:<br>
+      MPWriter.write(Node.getBool());<br>
+      break;<br>
+    case Type::Int:<br>
+      MPWriter.write(Node.getInt());<br>
+      break;<br>
+    case Type::UInt:<br>
+      MPWriter.write(Node.getUInt());<br>
+      break;<br>
+    case Type::String:<br>
+      MPWriter.write(Node.getString());<br>
+      break;<br>
+    default:<br>
+      llvm_unreachable("unhandled msgpack object kind");<br>
+    }<br>
+    // Pop finished stack levels.<br>
+    while (!Stack.empty()) {<br>
+      if (Stack.back().Node.getKind() == Type::Map) {<br>
+        if (Stack.back().MapIt != Stack.back().Node.getMap().end())<br>
+          break;<br>
+      } else {<br>
+        if (Stack.back().ArrayIt != Stack.back().Node.getArray().end())<br>
+          break;<br>
+      }<br>
+      Stack.pop_back();<br>
+    }<br>
+    if (Stack.empty())<br>
+      break;<br>
+    // Get the next value.<br>
+    if (Stack.back().Node.getKind() == Type::Map) {<br>
+      if (Stack.back().OnKey) {<br>
+        // Do the key of a key,value pair in a map.<br>
+        Node = Stack.back().MapIt->first;<br>
+        Stack.back().OnKey = false;<br>
+      } else {<br>
+        Node = Stack.back().MapIt->second;<br>
+        ++Stack.back().MapIt;<br>
+        Stack.back().OnKey = true;<br>
+      }<br>
+    } else {<br>
+      Node = *Stack.back().ArrayIt;<br>
+      ++Stack.back().ArrayIt;<br>
+    }<br>
+  }<br>
+}<br>
+<br>
<br>
Added: llvm/trunk/lib/BinaryFormat/MsgPackDocumentYAML.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/BinaryFormat/MsgPackDocumentYAML.cpp?rev=356080&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/BinaryFormat/MsgPackDocumentYAML.cpp?rev=356080&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/lib/BinaryFormat/MsgPackDocumentYAML.cpp (added)<br>
+++ llvm/trunk/lib/BinaryFormat/MsgPackDocumentYAML.cpp Wed Mar 13 11:54:47 2019<br>
@@ -0,0 +1,249 @@<br>
+//===-- MsgPackDocumentYAML.cpp - MsgPack Document YAML interface -------*-===//<br>
+//<br>
+//                     The LLVM Compiler Infrastructure<br>
+//<br>
+// This file is distributed under the University of Illinois Open Source<br>
+// License. See LICENSE.TXT for details.<br>
+//<br>
+//===----------------------------------------------------------------------===//<br>
+//<br>
+/// This file implements YAMLIO on a msgpack::Document.<br>
+//<br>
+//===----------------------------------------------------------------------===//<br>
+<br>
+#include "llvm/BinaryFormat/MsgPackDocument.h"<br>
+#include "llvm/Support/YAMLTraits.h"<br>
+<br>
+using namespace llvm;<br>
+using namespace msgpack;<br>
+<br>
+namespace {<br>
+<br>
+// Struct used to represent scalar node. (MapDocNode and ArrayDocNode already<br>
+// exist in MsgPackDocument.h.)<br>
+struct ScalarDocNode : DocNode {<br>
+  ScalarDocNode(DocNode N) : DocNode(N) {}<br>
+<br>
+  /// Get the YAML tag for this ScalarDocNode. This normally returns ""; it only<br>
+  /// returns something else if the result of toString would be ambiguous, e.g.<br>
+  /// a string that parses as a number or boolean.<br>
+  StringRef getYAMLTag() const;<br>
+};<br>
+<br>
+} // namespace<br>
+<br>
+/// Convert this DocNode to a string, assuming it is scalar.<br>
+std::string DocNode::toString() const {<br>
+  std::string S;<br>
+  raw_string_ostream OS(S);<br>
+  switch (getKind()) {<br>
+  case msgpack::Type::String:<br>
+    OS << Raw;<br>
+    break;<br>
+  case msgpack::Type::Nil:<br>
+    break;<br>
+  case msgpack::Type::Boolean:<br>
+    OS << (Bool ? "true" : "false");<br>
+    break;<br>
+  case msgpack::Type::Int:<br>
+    OS << Int;<br>
+    break;<br>
+  case msgpack::Type::UInt:<br>
+    if (getDocument()->getHexMode())<br>
+      OS << format("%#llx", (unsigned long long)UInt);<br>
+    else<br>
+      OS << UInt;<br>
+    break;<br>
+  case msgpack::Type::Float:<br>
+    OS << Float;<br>
+    break;<br>
+  default:<br>
+    llvm_unreachable("not scalar");<br>
+    break;<br>
+  }<br>
+  return S;<br>
+}<br>
+<br>
+/// Convert the StringRef and use it to set this DocNode (assuming scalar). If<br>
+/// it is a string, copy the string into the Document's strings list so we do<br>
+/// not rely on S having a lifetime beyond this call. Tag is "" or a YAML tag.<br>
+StringRef DocNode::fromString(StringRef S, StringRef Tag) {<br>
+  if (Tag == "tag:<a href="http://yaml.org" rel="noreferrer" target="_blank">yaml.org</a>,2002:str")<br>
+    Tag = "";<br>
+  if (Tag == "!int" || Tag == "") {<br>
+    // Try unsigned int then signed int.<br>
+    *this = getDocument()->getNode(uint64_t(0));<br>
+    StringRef Err = yaml::ScalarTraits<uint64_t>::input(S, nullptr, getUInt());<br>
+    if (Err != "") {<br>
+      *this = getDocument()->getNode(int64_t(0));<br>
+      Err = yaml::ScalarTraits<int64_t>::input(S, nullptr, getInt());<br>
+    }<br>
+    if (Err == "" || Tag != "")<br>
+      return Err;<br>
+  }<br>
+  if (Tag == "!nil") {<br>
+    *this = getDocument()->getNode();<br>
+    return "";<br>
+  }<br>
+  if (Tag == "!bool" || Tag == "") {<br>
+    *this = getDocument()->getNode(false);<br>
+    StringRef Err = yaml::ScalarTraits<bool>::input(S, nullptr, getBool());<br>
+    if (Err == "" || Tag != "")<br>
+      return Err;<br>
+  }<br>
+  if (Tag == "!float" || Tag == "") {<br>
+    *this = getDocument()->getNode(0.0);<br>
+    StringRef Err = yaml::ScalarTraits<double>::input(S, nullptr, getFloat());<br>
+    if (Err == "" || Tag != "")<br>
+      return Err;<br>
+  }<br>
+  assert((Tag == "!str" || Tag == "") && "unsupported tag");<br>
+  std::string V;<br>
+  StringRef Err = yaml::ScalarTraits<std::string>::input(S, nullptr, V);<br>
+  if (Err == "")<br>
+    *this = getDocument()->getNode(V, /*Copy=*/true);<br>
+  return Err;<br>
+}<br>
+<br>
+/// Get the YAML tag for this ScalarDocNode. This normally returns ""; it only<br>
+/// returns something else if the result of toString would be ambiguous, e.g.<br>
+/// a string that parses as a number or boolean.<br>
+StringRef ScalarDocNode::getYAMLTag() const {<br>
+  if (getKind() == msgpack::Type::Nil)<br>
+    return "!nil";<br>
+  // Try converting both ways and see if we get the same kind. If not, we need<br>
+  // a tag.<br>
+  ScalarDocNode N = getDocument()->getNode();<br>
+  N.fromString(toString(), "");<br>
+  if (N.getKind() == getKind())<br>
+    return "";<br>
+  // Tolerate signedness of int changing, as tags do not differentiate between<br>
+  // them anyway.<br>
+  if (N.getKind() == msgpack::Type::UInt && getKind() == msgpack::Type::Int)<br>
+    return "";<br>
+  if (N.getKind() == msgpack::Type::Int && getKind() == msgpack::Type::UInt)<br>
+    return "";<br>
+  // We do need a tag.<br>
+  switch (getKind()) {<br>
+  case msgpack::Type::String:<br>
+    return "!str";<br>
+  case msgpack::Type::Int:<br>
+    return "!int";<br>
+  case msgpack::Type::UInt:<br>
+    return "!int";<br>
+  case msgpack::Type::Boolean:<br>
+    return "!bool";<br>
+  case msgpack::Type::Float:<br>
+    return "!float";<br>
+  default:<br>
+    llvm_unreachable("unrecognized kind");<br>
+  }<br>
+}<br>
+<br>
+namespace llvm {<br>
+namespace yaml {<br>
+<br>
+/// YAMLIO for DocNode<br>
+template <> struct PolymorphicTraits<DocNode> {<br>
+<br>
+  static NodeKind getKind(const DocNode &N) {<br>
+    switch (N.getKind()) {<br>
+    case msgpack::Type::Map:<br>
+      return NodeKind::Map;<br>
+    case msgpack::Type::Array:<br>
+      return NodeKind::Sequence;<br>
+    default:<br>
+      return NodeKind::Scalar;<br>
+    }<br>
+  }<br>
+<br>
+  static MapDocNode &getAsMap(DocNode &N) { return N.getMap(/*Convert=*/true); }<br>
+<br>
+  static ArrayDocNode &getAsSequence(DocNode &N) {<br>
+    N.getArray(/*Convert=*/true);<br>
+    return *static_cast<ArrayDocNode *>(&N);<br>
+  }<br>
+<br>
+  static ScalarDocNode &getAsScalar(DocNode &N) {<br>
+    return *static_cast<ScalarDocNode *>(&N);<br>
+  }<br>
+};<br>
+<br>
+/// YAMLIO for ScalarDocNode<br>
+template <> struct TaggedScalarTraits<ScalarDocNode> {<br>
+<br>
+  static void output(const ScalarDocNode &S, void *Ctxt, raw_ostream &OS,<br>
+                     raw_ostream &TagOS) {<br>
+    TagOS << S.getYAMLTag();<br>
+    OS << S.toString();<br>
+  }<br>
+<br>
+  static StringRef input(StringRef Str, StringRef Tag, void *Ctxt,<br>
+                         ScalarDocNode &S) {<br>
+    return S.fromString(Str, Tag);<br>
+  }<br>
+<br>
+  static QuotingType mustQuote(const ScalarDocNode &S, StringRef ScalarStr) {<br>
+    switch (S.getKind()) {<br>
+    case Type::Int:<br>
+      return ScalarTraits<int64_t>::mustQuote(ScalarStr);<br>
+    case Type::UInt:<br>
+      return ScalarTraits<uint64_t>::mustQuote(ScalarStr);<br>
+    case Type::Nil:<br>
+      return ScalarTraits<StringRef>::mustQuote(ScalarStr);<br>
+    case Type::Boolean:<br>
+      return ScalarTraits<bool>::mustQuote(ScalarStr);<br>
+    case Type::Float:<br>
+      return ScalarTraits<double>::mustQuote(ScalarStr);<br>
+    case Type::Binary:<br>
+    case Type::String:<br>
+      return ScalarTraits<std::string>::mustQuote(ScalarStr);<br>
+    default:<br>
+      llvm_unreachable("unrecognized ScalarKind");<br>
+    }<br>
+  }<br>
+};<br>
+<br>
+/// YAMLIO for MapDocNode<br>
+template <> struct CustomMappingTraits<MapDocNode> {<br>
+<br>
+  static void inputOne(IO &IO, StringRef Key, MapDocNode &M) {<br>
+    ScalarDocNode KeyObj = M.getDocument()->getNode();<br>
+    KeyObj.fromString(Key, "");<br>
+    IO.mapRequired(Key.str().c_str(), M.getMap()[KeyObj]);<br>
+  }<br>
+<br>
+  static void output(IO &IO, MapDocNode &M) {<br>
+    for (auto I : M.getMap()) {<br>
+      IO.mapRequired(I.first.toString().c_str(), I.second);<br>
+    }<br>
+  }<br>
+};<br>
+<br>
+/// YAMLIO for ArrayNode<br>
+template <> struct SequenceTraits<ArrayDocNode> {<br>
+<br>
+  static size_t size(IO &IO, ArrayDocNode &A) { return A.size(); }<br>
+<br>
+  static DocNode &element(IO &IO, ArrayDocNode &A, size_t Index) {<br>
+    return A[Index];<br>
+  }<br>
+};<br>
+<br>
+} // namespace yaml<br>
+} // namespace llvm<br>
+<br>
+/// Convert MsgPack Document to YAML text.<br>
+void msgpack::Document::toYAML(raw_ostream &OS) {<br>
+  yaml::Output Yout(OS);<br>
+  Yout << getRoot();<br>
+}<br>
+<br>
+/// Read YAML text into the MsgPack document. Returns false on failure.<br>
+bool msgpack::Document::fromYAML(StringRef S) {<br>
+  clear();<br>
+  yaml::Input Yin(S);<br>
+  Yin >> getRoot();<br>
+  return !Yin.error();<br>
+}<br>
+<br>
<br>
Modified: llvm/trunk/unittests/BinaryFormat/CMakeLists.txt<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/BinaryFormat/CMakeLists.txt?rev=356080&r1=356079&r2=356080&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/BinaryFormat/CMakeLists.txt?rev=356080&r1=356079&r2=356080&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/unittests/BinaryFormat/CMakeLists.txt (original)<br>
+++ llvm/trunk/unittests/BinaryFormat/CMakeLists.txt Wed Mar 13 11:54:47 2019<br>
@@ -5,6 +5,7 @@ set(LLVM_LINK_COMPONENTS<br>
 add_llvm_unittest(BinaryFormatTests<br>
   DwarfTest.cpp<br>
   MachOTest.cpp<br>
+  MsgPackDocumentTest.cpp<br>
   MsgPackReaderTest.cpp<br>
   MsgPackTypesTest.cpp<br>
   MsgPackWriterTest.cpp<br>
<br>
Added: llvm/trunk/unittests/BinaryFormat/MsgPackDocumentTest.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/BinaryFormat/MsgPackDocumentTest.cpp?rev=356080&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/BinaryFormat/MsgPackDocumentTest.cpp?rev=356080&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/unittests/BinaryFormat/MsgPackDocumentTest.cpp (added)<br>
+++ llvm/trunk/unittests/BinaryFormat/MsgPackDocumentTest.cpp Wed Mar 13 11:54:47 2019<br>
@@ -0,0 +1,168 @@<br>
+//===- MsgPackDocumentTest.cpp --------------------------------------------===//<br>
+//<br>
+//                     The LLVM Compiler Infrastructure<br>
+//<br>
+// This file is distributed under the University of Illinois Open Source<br>
+// License. See LICENSE.TXT for details.<br>
+//<br>
+//===----------------------------------------------------------------------===//<br>
+<br>
+#include "llvm/BinaryFormat/MsgPackDocument.h"<br>
+#include "gtest/gtest.h"<br>
+<br>
+using namespace llvm;<br>
+using namespace msgpack;<br>
+<br>
+TEST(MsgPackDocument, TestReadInt) {<br>
+  Document Doc;<br>
+  bool Ok = Doc.readFromBlob(StringRef("\xd0\x00", 2), /*Multi=*/false);<br>
+  ASSERT_TRUE(Ok);<br>
+  ASSERT_EQ(Doc.getRoot().getKind(), Type::Int);<br>
+  ASSERT_EQ(Doc.getRoot().getInt(), 0);<br>
+}<br>
+<br>
+TEST(MsgPackDocument, TestReadArray) {<br>
+  Document Doc;<br>
+  bool Ok = Doc.readFromBlob(StringRef("\x92\xd0\x01\xc0"), /*Multi=*/false);<br>
+  ASSERT_TRUE(Ok);<br>
+  ASSERT_EQ(Doc.getRoot().getKind(), Type::Array);<br>
+  auto A = Doc.getRoot().getArray();<br>
+  ASSERT_EQ(A.size(), 2u);<br>
+  auto SI = A[0];<br>
+  ASSERT_EQ(SI.getKind(), Type::Int);<br>
+  ASSERT_EQ(SI.getInt(), 1);<br>
+  auto SN = A[1];<br>
+  ASSERT_EQ(SN.getKind(), Type::Nil);<br>
+}<br>
+<br>
+TEST(MsgPackDocument, TestReadMap) {<br>
+  Document Doc;<br>
+  bool Ok = Doc.readFromBlob(StringRef("\x82\xa3"<br>
+                                       "foo"<br>
+                                       "\xd0\x01\xa3"<br>
+                                       "bar"<br>
+                                       "\xd0\x02"),<br>
+                             /*Multi=*/false);<br>
+  ASSERT_TRUE(Ok);<br>
+  ASSERT_EQ(Doc.getRoot().getKind(), Type::Map);<br>
+  auto M = Doc.getRoot().getMap();<br>
+  ASSERT_EQ(M.size(), 2u);<br>
+  auto FooS = M["foo"];<br>
+  ASSERT_EQ(FooS.getKind(), Type::Int);<br>
+  ASSERT_EQ(FooS.getInt(), 1);<br>
+  auto BarS = M["bar"];<br>
+  ASSERT_EQ(BarS.getKind(), Type::Int);<br>
+  ASSERT_EQ(BarS.getInt(), 2);<br>
+}<br>
+<br>
+TEST(MsgPackDocument, TestWriteInt) {<br>
+  Document Doc;<br>
+  Doc.getRoot() = Doc.getNode(int64_t(1));<br>
+  std::string Buffer;<br>
+  Doc.writeToBlob(Buffer);<br>
+  ASSERT_EQ(Buffer, "\x01");<br>
+}<br>
+<br>
+TEST(MsgPackDocument, TestWriteArray) {<br>
+  Document Doc;<br>
+  auto A = Doc.getRoot().getArray(/*Convert=*/true);<br>
+  A.push_back(Doc.getNode(int64_t(1)));<br>
+  A.push_back(Doc.getNode());<br>
+  std::string Buffer;<br>
+  Doc.writeToBlob(Buffer);<br>
+  ASSERT_EQ(Buffer, "\x92\x01\xc0");<br>
+}<br>
+<br>
+TEST(MsgPackDocument, TestWriteMap) {<br>
+  Document Doc;<br>
+  auto M = Doc.getRoot().getMap(/*Convert=*/true);<br>
+  M["foo"] = Doc.getNode(int64_t(1));<br>
+  M["bar"] = Doc.getNode(int64_t(2));<br>
+  std::string Buffer;<br>
+  Doc.writeToBlob(Buffer);<br>
+  ASSERT_EQ(Buffer, "\x82\xa3"<br>
+                    "bar"<br>
+                    "\x02\xa3"<br>
+                    "foo"<br>
+                    "\x01");<br>
+}<br>
+<br>
+TEST(MsgPackDocument, TestOutputYAMLArray) {<br>
+  Document Doc;<br>
+  auto A = Doc.getRoot().getArray(/*Convert=*/true);<br>
+  A.push_back(Doc.getNode(int64_t(1)));<br>
+  A.push_back(Doc.getNode(int64_t(2)));<br>
+  std::string Buffer;<br>
+  raw_string_ostream OStream(Buffer);<br>
+  Doc.toYAML(OStream);<br>
+  ASSERT_EQ(OStream.str(), "---\n- 1\n- 2\n...\n");<br>
+}<br>
+<br>
+TEST(MsgPackDocument, TestInputYAMLArray) {<br>
+  Document Doc;<br>
+  bool Ok = Doc.fromYAML("---\n- !int 0x1\n- !str 2\n...\n");<br>
+  ASSERT_TRUE(Ok);<br>
+  ASSERT_EQ(Doc.getRoot().getKind(), Type::Array);<br>
+  auto A = Doc.getRoot().getArray();<br>
+  ASSERT_EQ(A.size(), 2u);<br>
+  auto SI = A[0];<br>
+  ASSERT_EQ(SI.getKind(), Type::UInt);<br>
+  ASSERT_EQ(SI.getUInt(), 1u);<br>
+  auto SS = A[1];<br>
+  ASSERT_EQ(SS.getKind(), Type::String);<br>
+  ASSERT_EQ(SS.getString(), "2");<br>
+}<br>
+<br>
+TEST(MsgPackDocument, TestOutputYAMLMap) {<br>
+  Document Doc;<br>
+  auto M = Doc.getRoot().getMap(/*Convert=*/true);<br>
+  M["foo"] = Doc.getNode(int64_t(1));<br>
+  M["bar"] = Doc.getNode(uint64_t(2));<br>
+  auto N = Doc.getMapNode();<br>
+  M["qux"] = N;<br>
+  N["baz"] = Doc.getNode(true);<br>
+  std::string Buffer;<br>
+  raw_string_ostream OStream(Buffer);<br>
+  Doc.toYAML(OStream);<br>
+  ASSERT_EQ(OStream.str(), "---\n"<br>
+                           "bar:             2\n"<br>
+                           "foo:             1\n"<br>
+                           "qux:             \n"<br>
+                           "  baz:             true\n"<br>
+                           "...\n");<br>
+}<br>
+<br>
+TEST(MsgPackDocument, TestOutputYAMLMapHex) {<br>
+  Document Doc;<br>
+  Doc.setHexMode();<br>
+  auto M = Doc.getRoot().getMap(/*Convert=*/true);<br>
+  M["foo"] = Doc.getNode(int64_t(1));<br>
+  M["bar"] = Doc.getNode(uint64_t(2));<br>
+  auto N = Doc.getMapNode();<br>
+  M["qux"] = N;<br>
+  N["baz"] = Doc.getNode(true);<br>
+  std::string Buffer;<br>
+  raw_string_ostream OStream(Buffer);<br>
+  Doc.toYAML(OStream);<br>
+  ASSERT_EQ(OStream.str(), "---\n"<br>
+                           "bar:             0x2\n"<br>
+                           "foo:             1\n"<br>
+                           "qux:             \n"<br>
+                           "  baz:             true\n"<br>
+                           "...\n");<br>
+}<br>
+<br>
+TEST(MsgPackDocument, TestInputYAMLMap) {<br>
+  Document Doc;<br>
+  bool Ok = Doc.fromYAML("---\nfoo: !int 0x1\nbaz: !str 2\n...\n");<br>
+  ASSERT_TRUE(Ok);<br>
+  ASSERT_EQ(Doc.getRoot().getKind(), Type::Map);<br>
+  auto M = Doc.getRoot().getMap();<br>
+  ASSERT_EQ(M.size(), 2u);<br>
+  auto SI = M["foo"];<br>
+  ASSERT_EQ(SI.getKind(), Type::UInt);<br>
+  ASSERT_EQ(SI.getUInt(), 1u);<br>
+  auto SS = M["baz"];<br>
+  ASSERT_EQ(SS.getKind(), Type::String);<br>
+  ASSERT_EQ(SS.getString(), "2");<br>
+}<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits</a><br>
</blockquote></div></div>