blob: 67b5764eadaa42ba6ce460b72da978a2b3f4fa17 [file] [log] [blame]
Rafael Espindola5fd5fe02013-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 Espindola18903ff2016-03-01 19:15:06 +000015#include "llvm/ObjectYAML/YAML.h"
Dmitri Gribenko9e8eafa2013-08-07 05:51:27 +000016#include "llvm/ADT/StringExtras.h"
Sean Silva639adc52013-06-05 22:59:00 +000017#include "llvm/Support/raw_ostream.h"
Will Dietze3ba15c2013-10-12 00:55:57 +000018#include <cctype>
Eugene Zelenkocd903442017-07-01 01:35:55 +000019#include <cstdint>
Rafael Espindola5fd5fe02013-06-05 02:32:26 +000020
21using namespace llvm;
22
Rafael Espindola7413fef2014-07-03 02:01:39 +000023void yaml::ScalarTraits<yaml::BinaryRef>::output(
Eugene Zelenkocd903442017-07-01 01:35:55 +000024 const yaml::BinaryRef &Val, void *, raw_ostream &Out) {
Sean Silva4370ddb2013-06-05 23:47:23 +000025 Val.writeAsHex(Out);
Rafael Espindola5fd5fe02013-06-05 02:32:26 +000026}
27
Rafael Espindola7413fef2014-07-03 02:01:39 +000028StringRef yaml::ScalarTraits<yaml::BinaryRef>::input(StringRef Scalar, void *,
29 yaml::BinaryRef &Val) {
Sean Silva639adc52013-06-05 22:59:00 +000030 if (Scalar.size() % 2 != 0)
31 return "BinaryRef hex string must contain an even number of nybbles.";
32 // TODO: Can we improve YAMLIO to permit a more accurate diagnostic here?
33 // (e.g. a caret pointing to the offending character).
34 for (unsigned I = 0, N = Scalar.size(); I != N; ++I)
Rafael Espindola149d1a12013-06-07 18:05:03 +000035 if (!isxdigit(Scalar[I]))
Sean Silva639adc52013-06-05 22:59:00 +000036 return "BinaryRef hex string must contain only hex digits.";
Rafael Espindola7413fef2014-07-03 02:01:39 +000037 Val = yaml::BinaryRef(Scalar);
Eugene Zelenkocd903442017-07-01 01:35:55 +000038 return {};
Rafael Espindola5fd5fe02013-06-05 02:32:26 +000039}
Sean Silva639adc52013-06-05 22:59:00 +000040
Rafael Espindola7413fef2014-07-03 02:01:39 +000041void yaml::BinaryRef::writeAsBinary(raw_ostream &OS) const {
Sean Silva6acc9822013-06-05 23:32:31 +000042 if (!DataIsHexString) {
Sean Silva639adc52013-06-05 22:59:00 +000043 OS.write((const char *)Data.data(), Data.size());
44 return;
45 }
46 for (unsigned I = 0, N = Data.size(); I != N; I += 2) {
47 uint8_t Byte;
48 StringRef((const char *)&Data[I], 2).getAsInteger(16, Byte);
49 OS.write(Byte);
50 }
51}
Sean Silva4370ddb2013-06-05 23:47:23 +000052
Rafael Espindola7413fef2014-07-03 02:01:39 +000053void yaml::BinaryRef::writeAsHex(raw_ostream &OS) const {
David Majnemer8bd27992014-03-20 06:28:52 +000054 if (binary_size() == 0)
Sean Silva845e1962013-07-09 00:54:46 +000055 return;
Sean Silva4370ddb2013-06-05 23:47:23 +000056 if (DataIsHexString) {
57 OS.write((const char *)Data.data(), Data.size());
58 return;
59 }
Simon Atanasyan940260b2016-03-01 10:11:27 +000060 for (uint8_t Byte : Data)
61 OS << hexdigit(Byte >> 4) << hexdigit(Byte & 0xf);
Sean Silva4370ddb2013-06-05 23:47:23 +000062}