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