1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-10-27 05:53:07 +01:00
llvm-mirror/include/llvm/Object/YAML.h
Sean Silva e61870890f Remove error-prone methods of BinaryRef.
A user shouldn't care about the internal state, and these methods by
their very nature require asserting a predicate on the internal state.
As such, they cannot be used safely without introducing hidden
long-distance dependencies on the manner of construction of the
BinaryRef.

Use writeAsBinary(raw_ostream &) and writeAsHex(raw_ostream &) if you
need to access the data in a binary or hex format.

llvm-svn: 183353
2013-06-05 23:55:26 +00:00

77 lines
2.5 KiB
C++

//===- YAML.h - YAMLIO utilities for object files ---------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file declares utility classes for handling the YAML representation of
// object files.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_OBJECT_YAML_H
#define LLVM_OBJECT_YAML_H
#include "llvm/Support/YAMLTraits.h"
namespace llvm {
namespace object {
namespace yaml {
/// In an object file this is just a binary blob. In an yaml file it is an hex
/// string. Using this avoid having to allocate temporary strings.
class BinaryRef {
/// \brief Either raw binary data, or a string of hex bytes (must always
/// be an even number of characters).
ArrayRef<uint8_t> Data;
/// \brief Discriminator between the two states of the `Data` member.
bool DataIsHexString;
public:
BinaryRef(ArrayRef<uint8_t> Data) : Data(Data), DataIsHexString(false) {}
BinaryRef(StringRef Data)
: Data(reinterpret_cast<const uint8_t *>(Data.data()), Data.size()),
DataIsHexString(true) {}
BinaryRef() : DataIsHexString(true) {}
/// \brief The number of bytes that are represented by this BinaryRef.
/// This is the number of bytes that writeAsBinary() will write.
ArrayRef<uint8_t>::size_type binary_size() const {
if (DataIsHexString)
return Data.size() / 2;
return Data.size();
}
bool operator==(const BinaryRef &Ref) {
// Special case for default constructed BinaryRef.
if (Ref.Data.empty() && Data.empty())
return true;
return Ref.DataIsHexString == DataIsHexString && Ref.Data == Data;
}
/// \brief Write the contents (regardless of whether it is binary or a
/// hex string) as binary to the given raw_ostream.
void writeAsBinary(raw_ostream &OS) const;
/// \brief Write the contents (regardless of whether it is binary or a
/// hex string) as hex to the given raw_ostream.
///
/// For example, a possible output could be `DEADBEEFCAFEBABE`.
void writeAsHex(raw_ostream &OS) const;
};
}
}
namespace yaml {
template <> struct ScalarTraits<object::yaml::BinaryRef> {
static void output(const object::yaml::BinaryRef &, void *,
llvm::raw_ostream &);
static StringRef input(StringRef, void *, object::yaml::BinaryRef &);
};
}
}
#endif