Formatters.cpp 2.3 KB

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