YAML.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. //===- YAML.cpp - YAMLIO utilities for object files -----------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file defines utility classes for handling the YAML representation of
  11. // object files.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/MC/YAML.h"
  15. #include "llvm/ADT/StringExtras.h"
  16. #include "llvm/Support/raw_ostream.h"
  17. #include <cctype>
  18. using namespace llvm;
  19. void yaml::ScalarTraits<yaml::BinaryRef>::output(
  20. const yaml::BinaryRef &Val, void *, llvm::raw_ostream &Out) {
  21. Val.writeAsHex(Out);
  22. }
  23. StringRef yaml::ScalarTraits<yaml::BinaryRef>::input(StringRef Scalar, void *,
  24. yaml::BinaryRef &Val) {
  25. if (Scalar.size() % 2 != 0)
  26. return "BinaryRef hex string must contain an even number of nybbles.";
  27. // TODO: Can we improve YAMLIO to permit a more accurate diagnostic here?
  28. // (e.g. a caret pointing to the offending character).
  29. for (unsigned I = 0, N = Scalar.size(); I != N; ++I)
  30. if (!isxdigit(Scalar[I]))
  31. return "BinaryRef hex string must contain only hex digits.";
  32. Val = yaml::BinaryRef(Scalar);
  33. return StringRef();
  34. }
  35. void yaml::BinaryRef::writeAsBinary(raw_ostream &OS) const {
  36. if (!DataIsHexString) {
  37. OS.write((const char *)Data.data(), Data.size());
  38. return;
  39. }
  40. for (unsigned I = 0, N = Data.size(); I != N; I += 2) {
  41. uint8_t Byte;
  42. StringRef((const char *)&Data[I], 2).getAsInteger(16, Byte);
  43. OS.write(Byte);
  44. }
  45. }
  46. void yaml::BinaryRef::writeAsHex(raw_ostream &OS) const {
  47. if (binary_size() == 0)
  48. return;
  49. if (DataIsHexString) {
  50. OS.write((const char *)Data.data(), Data.size());
  51. return;
  52. }
  53. for (ArrayRef<uint8_t>::iterator I = Data.begin(), E = Data.end(); I != E;
  54. ++I) {
  55. uint8_t Byte = *I;
  56. OS << hexdigit(Byte >> 4);
  57. OS << hexdigit(Byte & 0xf);
  58. }
  59. }