blob: cf6e616241fc644a044033d76b2950b619b8a0bc [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) {
23 ArrayRef<uint8_t> Data = Val.getBinary();
24 for (ArrayRef<uint8_t>::iterator I = Data.begin(), E = Data.end(); I != E;
25 ++I) {
26 uint8_t Byte = *I;
27 Out << hexdigit(Byte >> 4);
28 Out << hexdigit(Byte & 0xf);
29 }
30}
31
Sean Silva639adc52013-06-05 22:59:00 +000032// Can't find this anywhere else in the codebase (clang has one, but it has
33// some baggage). Deduplicate as required.
34static bool isHexDigit(uint8_t C) {
35 return ('0' <= C && C <= '9') ||
36 ('A' <= C && C <= 'F') ||
37 ('a' <= C && C <= 'f');
38}
39
Rafael Espindola5fd5fe02013-06-05 02:32:26 +000040StringRef yaml::ScalarTraits<object::yaml::BinaryRef>::input(
41 StringRef Scalar, void *, object::yaml::BinaryRef &Val) {
Sean Silva639adc52013-06-05 22:59:00 +000042 if (Scalar.size() % 2 != 0)
43 return "BinaryRef hex string must contain an even number of nybbles.";
44 // TODO: Can we improve YAMLIO to permit a more accurate diagnostic here?
45 // (e.g. a caret pointing to the offending character).
46 for (unsigned I = 0, N = Scalar.size(); I != N; ++I)
47 if (!isHexDigit(Scalar[I]))
48 return "BinaryRef hex string must contain only hex digits.";
Rafael Espindola5fd5fe02013-06-05 02:32:26 +000049 Val = object::yaml::BinaryRef(Scalar);
50 return StringRef();
51}
Sean Silva639adc52013-06-05 22:59:00 +000052
53void BinaryRef::writeAsBinary(raw_ostream &OS) const {
Sean Silva6acc9822013-06-05 23:32:31 +000054 if (!DataIsHexString) {
Sean Silva639adc52013-06-05 22:59:00 +000055 OS.write((const char *)Data.data(), Data.size());
56 return;
57 }
58 for (unsigned I = 0, N = Data.size(); I != N; I += 2) {
59 uint8_t Byte;
60 StringRef((const char *)&Data[I], 2).getAsInteger(16, Byte);
61 OS.write(Byte);
62 }
63}