Formatters.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. //===- Formatters.cpp -----------------------------------------------------===//
  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. #include "llvm/DebugInfo/CodeView/Formatters.h"
  9. #include "llvm/ADT/ArrayRef.h"
  10. #include "llvm/DebugInfo/CodeView/GUID.h"
  11. #include "llvm/Support/Endian.h"
  12. #include "llvm/Support/ErrorHandling.h"
  13. #include "llvm/Support/Format.h"
  14. #include "llvm/Support/raw_ostream.h"
  15. #include <cassert>
  16. using namespace llvm;
  17. using namespace llvm::codeview;
  18. using namespace llvm::codeview::detail;
  19. GuidAdapter::GuidAdapter(StringRef Guid)
  20. : FormatAdapter(ArrayRef(Guid.bytes_begin(), Guid.bytes_end())) {}
  21. GuidAdapter::GuidAdapter(ArrayRef<uint8_t> Guid)
  22. : FormatAdapter(std::move(Guid)) {}
  23. // From https://docs.microsoft.com/en-us/windows/win32/msi/guid documentation:
  24. // The GUID data type is a text string representing a Class identifier (ID).
  25. // All GUIDs must be authored in uppercase.
  26. // The valid format for a GUID is {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX} where
  27. // X is a hex digit (0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F).
  28. //
  29. // The individual string components must be padded to comply with the specific
  30. // lengths of {8-4-4-4-12} characters.
  31. // The llvm-yaml2obj tool checks that a GUID follow that format:
  32. // - the total length to be 38 (including the curly braces.
  33. // - there is a dash at the positions: 8, 13, 18 and 23.
  34. void GuidAdapter::format(raw_ostream &Stream, StringRef Style) {
  35. assert(Item.size() == 16 && "Expected 16-byte GUID");
  36. struct MSGuid {
  37. support::ulittle32_t Data1;
  38. support::ulittle16_t Data2;
  39. support::ulittle16_t Data3;
  40. support::ubig64_t Data4;
  41. };
  42. const MSGuid *G = reinterpret_cast<const MSGuid *>(Item.data());
  43. Stream
  44. << '{' << format_hex_no_prefix(G->Data1, 8, /*Upper=*/true)
  45. << '-' << format_hex_no_prefix(G->Data2, 4, /*Upper=*/true)
  46. << '-' << format_hex_no_prefix(G->Data3, 4, /*Upper=*/true)
  47. << '-' << format_hex_no_prefix(G->Data4 >> 48, 4, /*Upper=*/true) << '-'
  48. << format_hex_no_prefix(G->Data4 & ((1ULL << 48) - 1), 12, /*Upper=*/true)
  49. << '}';
  50. }
  51. raw_ostream &llvm::codeview::operator<<(raw_ostream &OS, const GUID &Guid) {
  52. codeview::detail::GuidAdapter A(Guid.Guid);
  53. A.format(OS, "");
  54. return OS;
  55. }