LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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/Object/YAML.h"
16 #include "llvm/ADT/StringExtras.h"
18 #include <cctype>
19 
20 using namespace llvm;
21 using namespace object::yaml;
22 
24  const object::yaml::BinaryRef &Val, void *, llvm::raw_ostream &Out) {
25  Val.writeAsHex(Out);
26 }
27 
30  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)
35  if (!isxdigit(Scalar[I]))
36  return "BinaryRef hex string must contain only hex digits.";
37  Val = object::yaml::BinaryRef(Scalar);
38  return StringRef();
39 }
40 
42  if (!DataIsHexString) {
43  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 }
52 
54  if (binary_size() == 0) {
55  OS << "\"\"";
56  return;
57  }
58  if (DataIsHexString) {
59  OS.write((const char *)Data.data(), Data.size());
60  return;
61  }
62  for (ArrayRef<uint8_t>::iterator I = Data.begin(), E = Data.end(); I != E;
63  ++I) {
64  uint8_t Byte = *I;
65  OS << hexdigit(Byte >> 4);
66  OS << hexdigit(Byte & 0xf);
67  }
68 }
size_t size() const
size - Get the string size.
Definition: StringRef.h:113
iterator end() const
Definition: ArrayRef.h:98
Specialized YAMLIO scalar type for representing a binary blob.
Definition: YAML.h:64
ArrayRef< uint8_t >::size_type binary_size() const
The number of bytes that are represented by this BinaryRef. This is the number of bytes that writeAsB...
Definition: YAML.h:80
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:41
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:109
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:53
iterator begin() const
Definition: ArrayRef.h:97
enable_if_c< std::numeric_limits< T >::is_signed, bool >::type getAsInteger(unsigned Radix, T &Result) const
Definition: StringRef.h:337
raw_ostream & write(unsigned char C)
static char hexdigit(unsigned X, bool LowerCase=false)
Definition: StringExtras.h:26
#define I(x, y, z)
Definition: MD5.cpp:54
#define N
const T * data() const
Definition: ArrayRef.h:106