xref: /src/contrib/llvm-project/llvm/lib/DebugInfo/CodeView/Formatters.cpp (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
19df3605dSDimitry Andric //===- Formatters.cpp -----------------------------------------------------===//
271d5a254SDimitry Andric //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
671d5a254SDimitry Andric //
771d5a254SDimitry Andric //===----------------------------------------------------------------------===//
871d5a254SDimitry Andric 
971d5a254SDimitry Andric #include "llvm/DebugInfo/CodeView/Formatters.h"
109df3605dSDimitry Andric #include "llvm/ADT/ArrayRef.h"
1193c91e39SDimitry Andric #include "llvm/DebugInfo/CodeView/GUID.h"
12145449b1SDimitry Andric #include "llvm/Support/Endian.h"
13145449b1SDimitry Andric #include "llvm/Support/ErrorHandling.h"
14145449b1SDimitry Andric #include "llvm/Support/Format.h"
159df3605dSDimitry Andric #include "llvm/Support/raw_ostream.h"
169df3605dSDimitry Andric #include <cassert>
1771d5a254SDimitry Andric 
1871d5a254SDimitry Andric using namespace llvm;
1971d5a254SDimitry Andric using namespace llvm::codeview;
2071d5a254SDimitry Andric using namespace llvm::codeview::detail;
2171d5a254SDimitry Andric 
GuidAdapter(StringRef Guid)2271d5a254SDimitry Andric GuidAdapter::GuidAdapter(StringRef Guid)
23e3b55780SDimitry Andric     : FormatAdapter(ArrayRef(Guid.bytes_begin(), Guid.bytes_end())) {}
2471d5a254SDimitry Andric 
GuidAdapter(ArrayRef<uint8_t> Guid)2571d5a254SDimitry Andric GuidAdapter::GuidAdapter(ArrayRef<uint8_t> Guid)
2671d5a254SDimitry Andric     : FormatAdapter(std::move(Guid)) {}
2771d5a254SDimitry Andric 
28344a3780SDimitry Andric // From https://docs.microsoft.com/en-us/windows/win32/msi/guid documentation:
29344a3780SDimitry Andric // The GUID data type is a text string representing a Class identifier (ID).
30344a3780SDimitry Andric // All GUIDs must be authored in uppercase.
31344a3780SDimitry Andric // The valid format for a GUID is {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX} where
32344a3780SDimitry Andric // X is a hex digit (0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F).
33344a3780SDimitry Andric //
34344a3780SDimitry Andric // The individual string components must be padded to comply with the specific
35344a3780SDimitry Andric // lengths of {8-4-4-4-12} characters.
36344a3780SDimitry Andric // The llvm-yaml2obj tool checks that a GUID follow that format:
37344a3780SDimitry Andric // - the total length to be 38 (including the curly braces.
38344a3780SDimitry Andric // - there is a dash at the positions: 8, 13, 18 and 23.
format(raw_ostream & Stream,StringRef Style)399df3605dSDimitry Andric void GuidAdapter::format(raw_ostream &Stream, StringRef Style) {
4071d5a254SDimitry Andric   assert(Item.size() == 16 && "Expected 16-byte GUID");
41344a3780SDimitry Andric   struct MSGuid {
42344a3780SDimitry Andric     support::ulittle32_t Data1;
43344a3780SDimitry Andric     support::ulittle16_t Data2;
44344a3780SDimitry Andric     support::ulittle16_t Data3;
45344a3780SDimitry Andric     support::ubig64_t Data4;
46344a3780SDimitry Andric   };
47344a3780SDimitry Andric   const MSGuid *G = reinterpret_cast<const MSGuid *>(Item.data());
48344a3780SDimitry Andric   Stream
49344a3780SDimitry Andric       << '{' << format_hex_no_prefix(G->Data1, 8, /*Upper=*/true)
50344a3780SDimitry Andric       << '-' << format_hex_no_prefix(G->Data2, 4, /*Upper=*/true)
51344a3780SDimitry Andric       << '-' << format_hex_no_prefix(G->Data3, 4, /*Upper=*/true)
52344a3780SDimitry Andric       << '-' << format_hex_no_prefix(G->Data4 >> 48, 4, /*Upper=*/true) << '-'
53344a3780SDimitry Andric       << format_hex_no_prefix(G->Data4 & ((1ULL << 48) - 1), 12, /*Upper=*/true)
54344a3780SDimitry Andric       << '}';
5571d5a254SDimitry Andric }
5693c91e39SDimitry Andric 
operator <<(raw_ostream & OS,const GUID & Guid)5793c91e39SDimitry Andric raw_ostream &llvm::codeview::operator<<(raw_ostream &OS, const GUID &Guid) {
5893c91e39SDimitry Andric   codeview::detail::GuidAdapter A(Guid.Guid);
5993c91e39SDimitry Andric   A.format(OS, "");
6093c91e39SDimitry Andric   return OS;
6193c91e39SDimitry Andric }
62