blob: e63bd5df27ccabd030b84ec495544b1af78cc491 [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
15#include "llvm/Object/YAML.h"
Sean Silva639adc52013-06-05 22:59:00 +000016#include "llvm/Support/raw_ostream.h"
Rafael Espindola5fd5fe02013-06-05 02:32:26 +000017
18using namespace llvm;
Sean Silva639adc52013-06-05 22:59:00 +000019using namespace object::yaml;
Rafael Espindola5fd5fe02013-06-05 02:32:26 +000020
21void yaml::ScalarTraits<object::yaml::BinaryRef>::output(
22 const object::yaml::BinaryRef &Val, void *, llvm::raw_ostream &Out) {
Sean Silva4370ddb2013-06-05 23:47:23 +000023 Val.writeAsHex(Out);
Rafael Espindola5fd5fe02013-06-05 02:32:26 +000024}
25
Sean Silva639adc52013-06-05 22:59:00 +000026// Can't find this anywhere else in the codebase (clang has one, but it has
27// some baggage). Deduplicate as required.
28static bool isHexDigit(uint8_t C) {
29 return ('0' <= C && C <= '9') ||
30 ('A' <= C && C <= 'F') ||
31 ('a' <= C && C <= 'f');
32}
33
Rafael Espindola5fd5fe02013-06-05 02:32:26 +000034StringRef yaml::ScalarTraits<object::yaml::BinaryRef>::input(
35 StringRef Scalar, void *, object::yaml::BinaryRef &Val) {
Sean Silva639adc52013-06-05 22:59:00 +000036 if (Scalar.size() % 2 != 0)
37 return "BinaryRef hex string must contain an even number of nybbles.";
38 // TODO: Can we improve YAMLIO to permit a more accurate diagnostic here?
39 // (e.g. a caret pointing to the offending character).
40 for (unsigned I = 0, N = Scalar.size(); I != N; ++I)
41 if (!isHexDigit(Scalar[I]))
42 return "BinaryRef hex string must contain only hex digits.";
Rafael Espindola5fd5fe02013-06-05 02:32:26 +000043 Val = object::yaml::BinaryRef(Scalar);
44 return StringRef();
45}
Sean Silva639adc52013-06-05 22:59:00 +000046
47void BinaryRef::writeAsBinary(raw_ostream &OS) const {
Sean Silva6acc9822013-06-05 23:32:31 +000048 if (!DataIsHexString) {
Sean Silva639adc52013-06-05 22:59:00 +000049 OS.write((const char *)Data.data(), Data.size());
50 return;
51 }
52 for (unsigned I = 0, N = Data.size(); I != N; I += 2) {
53 uint8_t Byte;
54 StringRef((const char *)&Data[I], 2).getAsInteger(16, Byte);
55 OS.write(Byte);
56 }
57}
Sean Silva4370ddb2013-06-05 23:47:23 +000058
59void BinaryRef::writeAsHex(raw_ostream &OS) const {
60 if (DataIsHexString) {
61 OS.write((const char *)Data.data(), Data.size());
62 return;
63 }
64 for (ArrayRef<uint8_t>::iterator I = Data.begin(), E = Data.end(); I != E;
65 ++I) {
66 uint8_t Byte = *I;
67 OS << hexdigit(Byte >> 4);
68 OS << hexdigit(Byte & 0xf);
69 }
70}