blob: 75cf1fbccc800fb79afe3105888d975a29ed235a [file] [log] [blame]
Rafael Espindola8e713012013-06-05 02:32:26 +00001//===- YAML.cpp - YAMLIO utilities for object files -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines utility classes for handling the YAML representation of
11// object files.
12//
13//===----------------------------------------------------------------------===//
14
Rafael Espindolaebd91932016-03-01 19:15:06 +000015#include "llvm/ObjectYAML/YAML.h"
Dmitri Gribenkodf73c302013-08-07 05:51:27 +000016#include "llvm/ADT/StringExtras.h"
Sean Silva23f3b822013-06-05 22:59:00 +000017#include "llvm/Support/raw_ostream.h"
Will Dietz981af002013-10-12 00:55:57 +000018#include <cctype>
Rafael Espindola8e713012013-06-05 02:32:26 +000019
20using namespace llvm;
21
Rafael Espindola97de4742014-07-03 02:01:39 +000022void yaml::ScalarTraits<yaml::BinaryRef>::output(
23 const yaml::BinaryRef &Val, void *, llvm::raw_ostream &Out) {
Sean Silva42fa38f2013-06-05 23:47:23 +000024 Val.writeAsHex(Out);
Rafael Espindola8e713012013-06-05 02:32:26 +000025}
26
Rafael Espindola97de4742014-07-03 02:01:39 +000027StringRef yaml::ScalarTraits<yaml::BinaryRef>::input(StringRef Scalar, void *,
28 yaml::BinaryRef &Val) {
Sean Silva23f3b822013-06-05 22:59:00 +000029 if (Scalar.size() % 2 != 0)
30 return "BinaryRef hex string must contain an even number of nybbles.";
31 // TODO: Can we improve YAMLIO to permit a more accurate diagnostic here?
32 // (e.g. a caret pointing to the offending character).
33 for (unsigned I = 0, N = Scalar.size(); I != N; ++I)
Rafael Espindola8d477dc2013-06-07 18:05:03 +000034 if (!isxdigit(Scalar[I]))
Sean Silva23f3b822013-06-05 22:59:00 +000035 return "BinaryRef hex string must contain only hex digits.";
Rafael Espindola97de4742014-07-03 02:01:39 +000036 Val = yaml::BinaryRef(Scalar);
Rafael Espindola8e713012013-06-05 02:32:26 +000037 return StringRef();
38}
Sean Silva23f3b822013-06-05 22:59:00 +000039
Rafael Espindola97de4742014-07-03 02:01:39 +000040void yaml::BinaryRef::writeAsBinary(raw_ostream &OS) const {
Sean Silvab7e82ad2013-06-05 23:32:31 +000041 if (!DataIsHexString) {
Sean Silva23f3b822013-06-05 22:59:00 +000042 OS.write((const char *)Data.data(), Data.size());
43 return;
44 }
45 for (unsigned I = 0, N = Data.size(); I != N; I += 2) {
46 uint8_t Byte;
47 StringRef((const char *)&Data[I], 2).getAsInteger(16, Byte);
48 OS.write(Byte);
49 }
50}
Sean Silva42fa38f2013-06-05 23:47:23 +000051
Rafael Espindola97de4742014-07-03 02:01:39 +000052void yaml::BinaryRef::writeAsHex(raw_ostream &OS) const {
David Majnemerd9eb2d12014-03-20 06:28:52 +000053 if (binary_size() == 0)
Sean Silva2f672d62013-07-09 00:54:46 +000054 return;
Sean Silva42fa38f2013-06-05 23:47:23 +000055 if (DataIsHexString) {
56 OS.write((const char *)Data.data(), Data.size());
57 return;
58 }
Simon Atanasyan255689c2016-03-01 10:11:27 +000059 for (uint8_t Byte : Data)
60 OS << hexdigit(Byte >> 4) << hexdigit(Byte & 0xf);
Sean Silva42fa38f2013-06-05 23:47:23 +000061}