LLVM  10.0.0
YAML.cpp
Go to the documentation of this file.
1 //===- YAML.cpp - YAMLIO utilities for object files -----------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines utility classes for handling the YAML representation of
10 // object files.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/ObjectYAML/YAML.h"
15 #include "llvm/ADT/StringExtras.h"
17 #include <cctype>
18 #include <cstdint>
19 
20 using namespace llvm;
21 
22 void yaml::ScalarTraits<yaml::BinaryRef>::output(
23  const yaml::BinaryRef &Val, void *, raw_ostream &Out) {
24  Val.writeAsHex(Out);
25 }
26 
27 StringRef yaml::ScalarTraits<yaml::BinaryRef>::input(StringRef Scalar, void *,
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 (!llvm::isHexDigit(Scalar[I]))
35  return "BinaryRef hex string must contain only hex digits.";
36  Val = yaml::BinaryRef(Scalar);
37  return {};
38 }
39 
40 void yaml::BinaryRef::writeAsBinary(raw_ostream &OS, uint64_t N) const {
41  if (!DataIsHexString) {
42  OS.write((const char *)Data.data(), std::min<uint64_t>(N, Data.size()));
43  return;
44  }
45 
46  for (uint64_t I = 0, E = std::min<uint64_t>(N, Data.size() / 2); I != E;
47  ++I) {
48  uint8_t Byte = llvm::hexDigitValue(Data[I * 2]);
49  Byte <<= 4;
50  Byte |= llvm::hexDigitValue(Data[I * 2 + 1]);
51  OS.write(Byte);
52  }
53 }
54 
56  if (binary_size() == 0)
57  return;
58  if (DataIsHexString) {
59  OS.write((const char *)Data.data(), Data.size());
60  return;
61  }
62  for (uint8_t Byte : Data)
63  OS << hexdigit(Byte >> 4) << hexdigit(Byte & 0xf);
64 }
This class represents lattice values for constants.
Definition: AllocatorList.h:23
LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:151
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:36
void writeAsBinary(raw_ostream &OS, uint64_t N=UINT64_MAX) 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 array size.
Definition: ArrayRef.h:158
bool isHexDigit(char C)
Checks if character C is a hexadecimal numeric character.
Definition: StringExtras.h:79
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
const T * data() const
Definition: ArrayRef.h:155
raw_ostream & write(unsigned char C)
ArrayRef< uint8_t >::size_type binary_size() const
The number of bytes that are represented by this BinaryRef.
Definition: YAML.h:80
unsigned hexDigitValue(char C)
Interpret the given character C as a hexadecimal digit and return its value.
Definition: StringExtras.h:68
Specialized YAMLIO scalar type for representing a binary blob.
Definition: YAML.h:63
#define I(x, y, z)
Definition: MD5.cpp:58
#define N
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:54
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:55