LLVM  3.7.0
YAML.cpp
Go to the documentation of this file.
1 //===- 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/MC/YAML.h"
16 #include "llvm/ADT/StringExtras.h"
18 #include <cctype>
19 
20 using namespace llvm;
21 
23  const yaml::BinaryRef &Val, void *, llvm::raw_ostream &Out) {
24  Val.writeAsHex(Out);
25 }
26 
28  yaml::BinaryRef &Val) {
29  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)
34  if (!isxdigit(Scalar[I]))
35  return "BinaryRef hex string must contain only hex digits.";
36  Val = yaml::BinaryRef(Scalar);
37  return StringRef();
38 }
39 
41  if (!DataIsHexString) {
42  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 }
51 
53  if (binary_size() == 0)
54  return;
55  if (DataIsHexString) {
56  OS.write((const char *)Data.data(), Data.size());
57  return;
58  }
59  for (ArrayRef<uint8_t>::iterator I = Data.begin(), E = Data.end(); I != E;
60  ++I) {
61  uint8_t Byte = *I;
62  OS << hexdigit(Byte >> 4);
63  OS << hexdigit(Byte & 0xf);
64  }
65 }
std::enable_if< std::numeric_limits< T >::is_signed, bool >::type getAsInteger(unsigned Radix, T &Result) const
Parse the current string as an integer of the specified radix.
Definition: StringRef.h:347
void writeAsBinary(raw_ostream &OS) const
Write the contents (regardless of whether it is binary or a hex string) as binary to the given raw_os...
Definition: YAML.cpp:40
size_t size() const
size - Get the string size.
Definition: StringRef.h:113
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: ArrayRef.h:31
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:134
raw_ostream & write(unsigned char C)
This class should be specialized by type that requires custom conversion to/from a yaml scalar...
Definition: YAMLTraits.h:109
static char hexdigit(unsigned X, bool LowerCase=false)
hexdigit - Return the hexadecimal character for the given number X (which should be less than 16)...
Definition: StringExtras.h:26
Specialized YAMLIO scalar type for representing a binary blob.
Definition: YAML.h:48
#define I(x, y, z)
Definition: MD5.cpp:54
#define N
void writeAsHex(raw_ostream &OS) const
Write the contents (regardless of whether it is binary or a hex string) as hex to the given raw_ostre...
Definition: YAML.cpp:52
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:38
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
const T * data() const
Definition: ArrayRef.h:131