Thanks to visit codestin.com
Credit goes to llvm.org

LLVM 22.0.0git
YAML.h
Go to the documentation of this file.
1//===- YAML.h ---------------------------------------------------*- C++ -*-===//
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#ifndef LLVM_OBJECTYAML_YAML_H
10#define LLVM_OBJECTYAML_YAML_H
11
12#include "llvm/ADT/ArrayRef.h"
13#include "llvm/ADT/StringRef.h"
16#include <cstdint>
17
18namespace llvm {
19
20class raw_ostream;
21
22namespace yaml {
23
24/// Specialized YAMLIO scalar type for representing a binary blob.
25///
26/// A typical use case would be to represent the content of a section in a
27/// binary file.
28/// This class has custom YAMLIO traits for convenient reading and writing.
29/// It renders as a string of hex digits in a YAML file.
30/// For example, it might render as `DEADBEEFCAFEBABE` (YAML does not
31/// require the quotation marks, so for simplicity when outputting they are
32/// omitted).
33/// When reading, any string whose content is an even number of hex digits
34/// will be accepted.
35/// For example, all of the following are acceptable:
36/// `DEADBEEF`, `"DeADbEeF"`, `"\x44EADBEEF"` (Note: '\x44' == 'D')
37///
38/// A significant advantage of using this class is that it never allocates
39/// temporary strings or buffers for any of its functionality.
40///
41/// Example:
42///
43/// The YAML mapping:
44/// \code
45/// Foo: DEADBEEFCAFEBABE
46/// \endcode
47///
48/// Could be modeled in YAMLIO by the struct:
49/// \code
50/// struct FooHolder {
51/// BinaryRef Foo;
52/// };
53/// namespace llvm {
54/// namespace yaml {
55/// template <>
56/// struct MappingTraits<FooHolder> {
57/// static void mapping(IO &IO, FooHolder &FH) {
58/// IO.mapRequired("Foo", FH.Foo);
59/// }
60/// };
61/// } // end namespace yaml
62/// } // end namespace llvm
63/// \endcode
64class BinaryRef {
65 friend bool operator==(const BinaryRef &LHS, const BinaryRef &RHS);
66
67 /// Either raw binary data, or a string of hex bytes (must always
68 /// be an even number of characters).
70
71 /// Discriminator between the two states of the `Data` member.
72 bool DataIsHexString = true;
73
74public:
75 BinaryRef() = default;
76 BinaryRef(ArrayRef<uint8_t> Data) : Data(Data), DataIsHexString(false) {}
78
79 /// The number of bytes that are represented by this BinaryRef.
80 /// This is the number of bytes that writeAsBinary() will write.
82 if (DataIsHexString)
83 return Data.size() / 2;
84 return Data.size();
85 }
86
87 /// Write the contents (regardless of whether it is binary or a
88 /// hex string) as binary to the given raw_ostream.
89 /// N can be used to specify the maximum number of bytes.
91
92 /// Write the contents (regardless of whether it is binary or a
93 /// hex string) as hex to the given raw_ostream.
94 ///
95 /// For example, a possible output could be `DEADBEEFCAFEBABE`.
96 LLVM_ABI void writeAsHex(raw_ostream &OS) const;
97};
98
99inline bool operator==(const BinaryRef &LHS, const BinaryRef &RHS) {
100 // Special case for default constructed BinaryRef.
101 if (LHS.Data.empty() && RHS.Data.empty())
102 return true;
103
104 return LHS.DataIsHexString == RHS.DataIsHexString && LHS.Data == RHS.Data;
105}
106
107template <> struct ScalarTraits<BinaryRef> {
108 LLVM_ABI static void output(const BinaryRef &, void *, raw_ostream &);
111};
112
113} // end namespace yaml
114
115} // end namespace llvm
116
117#endif // LLVM_OBJECTYAML_YAML_H
#define LLVM_ABI
Definition Compiler.h:213
Value * RHS
Value * LHS
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:41
size_t size_type
Definition ArrayRef.h:52
size_t size() const
size - Get the array size.
Definition ArrayRef.h:147
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
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
BinaryRef(StringRef Data)
Definition YAML.h:77
friend bool operator==(const BinaryRef &LHS, const BinaryRef &RHS)
Definition YAML.h:99
BinaryRef(ArrayRef< uint8_t > Data)
Definition YAML.h:76
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:54
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:39
#define UINT64_MAX
Definition DataTypes.h:77
bool operator==(const BinaryRef &LHS, const BinaryRef &RHS)
Definition YAML.h:99
QuotingType
Describe which type of quotes should be used when quoting is necessary.
Definition YAMLTraits.h:131
QuotingType needsQuotes(StringRef S, bool ForcePreserveAsString=true)
Definition YAMLTraits.h:589
This is an optimization pass for GlobalISel generic memory operations.
ArrayRef< CharT > arrayRefFromStringRef(StringRef Input)
Construct a string ref from an array ref of unsigned chars.
#define N
static LLVM_ABI void output(const BinaryRef &, void *, raw_ostream &)
static LLVM_ABI StringRef input(StringRef, void *, BinaryRef &)
static QuotingType mustQuote(StringRef S)
Definition YAML.h:110
This class should be specialized by type that requires custom conversion to/from a yaml scalar.
Definition YAMLTraits.h:149