Make BinaryRef output correctly in case of empty data.
authorSean Silva <silvas@purdue.edu>
Tue, 9 Jul 2013 00:54:46 +0000 (00:54 +0000)
committerSean Silva <silvas@purdue.edu>
Tue, 9 Jul 2013 00:54:46 +0000 (00:54 +0000)
Previously, it would simply output nothing, but it should output an
empty string `""`.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@185894 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Object/YAML.cpp
unittests/CMakeLists.txt
unittests/Makefile
unittests/Object/CMakeLists.txt [new file with mode: 0644]
unittests/Object/Makefile [new file with mode: 0644]
unittests/Object/YAMLTest.cpp [new file with mode: 0644]

index 4e7f0890989bcc7dff1da07ac712babe1d8fbfdd..5b665032bb3449680070e0ac5f6f583255216dea 100644 (file)
@@ -49,6 +49,10 @@ void BinaryRef::writeAsBinary(raw_ostream &OS) const {
 }
 
 void BinaryRef::writeAsHex(raw_ostream &OS) const {
+  if (binary_size() == 0) {
+    OS << "\"\"";
+    return;
+  }
   if (DataIsHexString) {
     OS.write((const char *)Data.data(), Data.size());
     return;
index 4b7e418cd180b4fcf81ecd7cd7d18e8d45769a87..53b7e9c1e8305e33fa48ec95908f7eaf786efc3e 100644 (file)
@@ -14,3 +14,4 @@ add_subdirectory(Support)
 add_subdirectory(Transforms)
 add_subdirectory(IR)
 add_subdirectory(DebugInfo)
+add_subdirectory(Object)
index 61d60611be9ca9017a30197761d1b9b9a5840b7a..d8bf1f8fffd3deebb777df05bcbf3851a4ac508f 100644 (file)
@@ -10,7 +10,7 @@
 LEVEL = ..
 
 PARALLEL_DIRS = ADT ExecutionEngine Support Transforms IR Analysis Bitcode \
-                                                               DebugInfo
+               DebugInfo Object
 
 include $(LEVEL)/Makefile.common
 
diff --git a/unittests/Object/CMakeLists.txt b/unittests/Object/CMakeLists.txt
new file mode 100644 (file)
index 0000000..b491dd7
--- /dev/null
@@ -0,0 +1,7 @@
+set(LLVM_LINK_COMPONENTS
+  object
+  )
+
+add_llvm_unittest(ObjectTests
+  YAMLTest.cpp
+  )
diff --git a/unittests/Object/Makefile b/unittests/Object/Makefile
new file mode 100644 (file)
index 0000000..0788a62
--- /dev/null
@@ -0,0 +1,15 @@
+##===- unittests/IR/Makefile -------------------------------*- Makefile -*-===##
+#
+#                     The LLVM Compiler Infrastructure
+#
+# This file is distributed under the University of Illinois Open Source
+# License. See LICENSE.TXT for details.
+#
+##===----------------------------------------------------------------------===##
+
+LEVEL = ../..
+TESTNAME = Object
+LINK_COMPONENTS := object
+
+include $(LEVEL)/Makefile.config
+include $(LLVM_SRC_ROOT)/unittests/Makefile.unittest
diff --git a/unittests/Object/YAMLTest.cpp b/unittests/Object/YAMLTest.cpp
new file mode 100644 (file)
index 0000000..3428e94
--- /dev/null
@@ -0,0 +1,40 @@
+//===- llvm/unittest/Object/YAMLTest.cpp - Tests for Object YAML ----------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Object/YAML.h"
+#include "llvm/Support/YAMLTraits.h"
+#include "gtest/gtest.h"
+
+using namespace llvm;
+
+namespace {
+struct BinaryHolder {
+  object::yaml::BinaryRef Binary;
+};
+} // end anonymous namespace
+
+namespace llvm {
+namespace yaml {
+template <>
+struct MappingTraits<BinaryHolder> {
+  static void mapping(IO &IO, BinaryHolder &BH) {
+    IO.mapRequired("Binary", BH.Binary);
+  }
+};
+} // end namespace yaml
+} // end namespace llvm
+
+TEST(ObjectYAML, BinaryRef) {
+  BinaryHolder BH;
+  SmallVector<char, 32> Buf;
+  llvm::raw_svector_ostream OS(Buf);
+  yaml::Output YOut(OS);
+  YOut << BH;
+  EXPECT_NE(OS.str().find("\"\""), StringRef::npos);
+}