LLVM 24.0.0git
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
17#include <cstdint>
18
19using namespace llvm;
20
22 const yaml::BinaryRef &Val, void *, raw_ostream &Out) {
23 Val.writeAsHex(Out);
24}
25
27 yaml::BinaryRef &Val) {
28 if (Scalar.size() % 2 != 0)
29 return "BinaryRef hex string must contain an even number of nybbles.";
30 // TODO: Can we improve YAMLIO to permit a more accurate diagnostic here?
31 // (e.g. a caret pointing to the offending character).
32 if (!llvm::all_of(Scalar, llvm::isHexDigit))
33 return "BinaryRef hex string must contain only hex digits.";
34 Val = yaml::BinaryRef(Scalar);
35 return {};
36}
37
38void yaml::BinaryRef::writeAsBinary(raw_ostream &OS, uint64_t N) const {
39 if (!DataIsHexString) {
40 OS.write((const char *)Data.data(), std::min<uint64_t>(N, Data.size()));
41 return;
42 }
43
44 for (uint64_t I = 0, E = std::min<uint64_t>(N, Data.size() / 2); I != E;
45 ++I) {
46 uint8_t Byte = llvm::hexDigitValue(Data[I * 2]);
47 Byte <<= 4;
48 Byte |= llvm::hexDigitValue(Data[I * 2 + 1]);
49 OS.write(Byte);
50 }
51}
52
54 if (binary_size() == 0)
55 return;
56 if (DataIsHexString) {
57 OS.write((const char *)Data.data(), Data.size());
58 return;
59 }
60 for (uint8_t Byte : Data)
61 OS << hexdigit(Byte >> 4) << hexdigit(Byte & 0xf);
62}
#define I(x, y, z)
Definition MD5.cpp:57
This file contains some functions that are useful when dealing with strings.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
raw_ostream & write(unsigned char C)
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.
Definition YAML.h:81
LLVM_ABI 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
LLVM_ABI 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:38
This is an optimization pass for GlobalISel generic memory operations.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1739
unsigned hexDigitValue(char C)
Interpret the given character C as a hexadecimal digit and return its value.
char hexdigit(unsigned X, bool LowerCase=false)
hexdigit - Return the hexadecimal character for the given number X (which should be less than 16).
bool isHexDigit(char C)
Checks if character C is a hexadecimal numeric character.
#define N
This class should be specialized by type that requires custom conversion to/from a yaml scalar.
Definition YAMLTraits.h:150