YAML.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. #ifndef LLVM_OBJECTYAML_YAML_H
  9. #define LLVM_OBJECTYAML_YAML_H
  10. #include "llvm/ADT/ArrayRef.h"
  11. #include "llvm/ADT/StringRef.h"
  12. #include "llvm/Support/YAMLTraits.h"
  13. #include <cstdint>
  14. namespace llvm {
  15. class raw_ostream;
  16. namespace yaml {
  17. /// Specialized YAMLIO scalar type for representing a binary blob.
  18. ///
  19. /// A typical use case would be to represent the content of a section in a
  20. /// binary file.
  21. /// This class has custom YAMLIO traits for convenient reading and writing.
  22. /// It renders as a string of hex digits in a YAML file.
  23. /// For example, it might render as `DEADBEEFCAFEBABE` (YAML does not
  24. /// require the quotation marks, so for simplicity when outputting they are
  25. /// omitted).
  26. /// When reading, any string whose content is an even number of hex digits
  27. /// will be accepted.
  28. /// For example, all of the following are acceptable:
  29. /// `DEADBEEF`, `"DeADbEeF"`, `"\x44EADBEEF"` (Note: '\x44' == 'D')
  30. ///
  31. /// A significant advantage of using this class is that it never allocates
  32. /// temporary strings or buffers for any of its functionality.
  33. ///
  34. /// Example:
  35. ///
  36. /// The YAML mapping:
  37. /// \code
  38. /// Foo: DEADBEEFCAFEBABE
  39. /// \endcode
  40. ///
  41. /// Could be modeled in YAMLIO by the struct:
  42. /// \code
  43. /// struct FooHolder {
  44. /// BinaryRef Foo;
  45. /// };
  46. /// namespace llvm {
  47. /// namespace yaml {
  48. /// template <>
  49. /// struct MappingTraits<FooHolder> {
  50. /// static void mapping(IO &IO, FooHolder &FH) {
  51. /// IO.mapRequired("Foo", FH.Foo);
  52. /// }
  53. /// };
  54. /// } // end namespace yaml
  55. /// } // end namespace llvm
  56. /// \endcode
  57. class BinaryRef {
  58. friend bool operator==(const BinaryRef &LHS, const BinaryRef &RHS);
  59. /// Either raw binary data, or a string of hex bytes (must always
  60. /// be an even number of characters).
  61. ArrayRef<uint8_t> Data;
  62. /// Discriminator between the two states of the `Data` member.
  63. bool DataIsHexString = true;
  64. public:
  65. BinaryRef() = default;
  66. BinaryRef(ArrayRef<uint8_t> Data) : Data(Data), DataIsHexString(false) {}
  67. BinaryRef(StringRef Data) : Data(arrayRefFromStringRef(Data)) {}
  68. /// The number of bytes that are represented by this BinaryRef.
  69. /// This is the number of bytes that writeAsBinary() will write.
  70. ArrayRef<uint8_t>::size_type binary_size() const {
  71. if (DataIsHexString)
  72. return Data.size() / 2;
  73. return Data.size();
  74. }
  75. /// Write the contents (regardless of whether it is binary or a
  76. /// hex string) as binary to the given raw_ostream.
  77. /// N can be used to specify the maximum number of bytes.
  78. void writeAsBinary(raw_ostream &OS, uint64_t N = UINT64_MAX) const;
  79. /// Write the contents (regardless of whether it is binary or a
  80. /// hex string) as hex to the given raw_ostream.
  81. ///
  82. /// For example, a possible output could be `DEADBEEFCAFEBABE`.
  83. void writeAsHex(raw_ostream &OS) const;
  84. };
  85. inline bool operator==(const BinaryRef &LHS, const BinaryRef &RHS) {
  86. // Special case for default constructed BinaryRef.
  87. if (LHS.Data.empty() && RHS.Data.empty())
  88. return true;
  89. return LHS.DataIsHexString == RHS.DataIsHexString && LHS.Data == RHS.Data;
  90. }
  91. template <> struct ScalarTraits<BinaryRef> {
  92. static void output(const BinaryRef &, void *, raw_ostream &);
  93. static StringRef input(StringRef, void *, BinaryRef &);
  94. static QuotingType mustQuote(StringRef S) { return needsQuotes(S); }
  95. };
  96. } // end namespace yaml
  97. } // end namespace llvm
  98. #endif // LLVM_OBJECTYAML_YAML_H