Path: blob/main/contrib/llvm-project/llvm/lib/ObjectYAML/YAML.cpp
35234 views
//===- YAML.cpp - YAMLIO utilities for object files -----------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file defines utility classes for handling the YAML representation of9// object files.10//11//===----------------------------------------------------------------------===//1213#include "llvm/ObjectYAML/YAML.h"14#include "llvm/ADT/StringExtras.h"15#include "llvm/Support/raw_ostream.h"16#include <cctype>17#include <cstdint>1819using namespace llvm;2021void yaml::ScalarTraits<yaml::BinaryRef>::output(22const yaml::BinaryRef &Val, void *, raw_ostream &Out) {23Val.writeAsHex(Out);24}2526StringRef yaml::ScalarTraits<yaml::BinaryRef>::input(StringRef Scalar, void *,27yaml::BinaryRef &Val) {28if (Scalar.size() % 2 != 0)29return "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).32if (!llvm::all_of(Scalar, llvm::isHexDigit))33return "BinaryRef hex string must contain only hex digits.";34Val = yaml::BinaryRef(Scalar);35return {};36}3738void yaml::BinaryRef::writeAsBinary(raw_ostream &OS, uint64_t N) const {39if (!DataIsHexString) {40OS.write((const char *)Data.data(), std::min<uint64_t>(N, Data.size()));41return;42}4344for (uint64_t I = 0, E = std::min<uint64_t>(N, Data.size() / 2); I != E;45++I) {46uint8_t Byte = llvm::hexDigitValue(Data[I * 2]);47Byte <<= 4;48Byte |= llvm::hexDigitValue(Data[I * 2 + 1]);49OS.write(Byte);50}51}5253void yaml::BinaryRef::writeAsHex(raw_ostream &OS) const {54if (binary_size() == 0)55return;56if (DataIsHexString) {57OS.write((const char *)Data.data(), Data.size());58return;59}60for (uint8_t Byte : Data)61OS << hexdigit(Byte >> 4) << hexdigit(Byte & 0xf);62}636465