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