LLVM API Documentation

YAML.cpp
Go to the documentation of this file.
00001 //===- YAML.cpp - YAMLIO utilities for object files -----------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines utility classes for handling the YAML representation of
00011 // object files.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "llvm/MC/YAML.h"
00016 #include "llvm/ADT/StringExtras.h"
00017 #include "llvm/Support/raw_ostream.h"
00018 #include <cctype>
00019 
00020 using namespace llvm;
00021 
00022 void yaml::ScalarTraits<yaml::BinaryRef>::output(
00023     const yaml::BinaryRef &Val, void *, llvm::raw_ostream &Out) {
00024   Val.writeAsHex(Out);
00025 }
00026 
00027 StringRef yaml::ScalarTraits<yaml::BinaryRef>::input(StringRef Scalar, void *,
00028                                                      yaml::BinaryRef &Val) {
00029   if (Scalar.size() % 2 != 0)
00030     return "BinaryRef hex string must contain an even number of nybbles.";
00031   // TODO: Can we improve YAMLIO to permit a more accurate diagnostic here?
00032   // (e.g. a caret pointing to the offending character).
00033   for (unsigned I = 0, N = Scalar.size(); I != N; ++I)
00034     if (!isxdigit(Scalar[I]))
00035       return "BinaryRef hex string must contain only hex digits.";
00036   Val = yaml::BinaryRef(Scalar);
00037   return StringRef();
00038 }
00039 
00040 void yaml::BinaryRef::writeAsBinary(raw_ostream &OS) const {
00041   if (!DataIsHexString) {
00042     OS.write((const char *)Data.data(), Data.size());
00043     return;
00044   }
00045   for (unsigned I = 0, N = Data.size(); I != N; I += 2) {
00046     uint8_t Byte;
00047     StringRef((const char *)&Data[I],  2).getAsInteger(16, Byte);
00048     OS.write(Byte);
00049   }
00050 }
00051 
00052 void yaml::BinaryRef::writeAsHex(raw_ostream &OS) const {
00053   if (binary_size() == 0)
00054     return;
00055   if (DataIsHexString) {
00056     OS.write((const char *)Data.data(), Data.size());
00057     return;
00058   }
00059   for (ArrayRef<uint8_t>::iterator I = Data.begin(), E = Data.end(); I != E;
00060        ++I) {
00061     uint8_t Byte = *I;
00062     OS << hexdigit(Byte >> 4);
00063     OS << hexdigit(Byte & 0xf);
00064   }
00065 }