10b57cec5SDimitry Andric //===- Formatters.cpp -----------------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric
90b57cec5SDimitry Andric #include "llvm/DebugInfo/CodeView/Formatters.h"
100b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h"
110b57cec5SDimitry Andric #include "llvm/DebugInfo/CodeView/GUID.h"
120b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
130b57cec5SDimitry Andric #include <algorithm>
140b57cec5SDimitry Andric #include <cassert>
150b57cec5SDimitry Andric
160b57cec5SDimitry Andric using namespace llvm;
170b57cec5SDimitry Andric using namespace llvm::codeview;
180b57cec5SDimitry Andric using namespace llvm::codeview::detail;
190b57cec5SDimitry Andric
GuidAdapter(StringRef Guid)200b57cec5SDimitry Andric GuidAdapter::GuidAdapter(StringRef Guid)
210b57cec5SDimitry Andric : FormatAdapter(makeArrayRef(Guid.bytes_begin(), Guid.bytes_end())) {}
220b57cec5SDimitry Andric
GuidAdapter(ArrayRef<uint8_t> Guid)230b57cec5SDimitry Andric GuidAdapter::GuidAdapter(ArrayRef<uint8_t> Guid)
240b57cec5SDimitry Andric : FormatAdapter(std::move(Guid)) {}
250b57cec5SDimitry Andric
26*5f7ddb14SDimitry Andric // From https://docs.microsoft.com/en-us/windows/win32/msi/guid documentation:
27*5f7ddb14SDimitry Andric // The GUID data type is a text string representing a Class identifier (ID).
28*5f7ddb14SDimitry Andric // All GUIDs must be authored in uppercase.
29*5f7ddb14SDimitry Andric // The valid format for a GUID is {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX} where
30*5f7ddb14SDimitry Andric // X is a hex digit (0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F).
31*5f7ddb14SDimitry Andric //
32*5f7ddb14SDimitry Andric // The individual string components must be padded to comply with the specific
33*5f7ddb14SDimitry Andric // lengths of {8-4-4-4-12} characters.
34*5f7ddb14SDimitry Andric // The llvm-yaml2obj tool checks that a GUID follow that format:
35*5f7ddb14SDimitry Andric // - the total length to be 38 (including the curly braces.
36*5f7ddb14SDimitry Andric // - there is a dash at the positions: 8, 13, 18 and 23.
format(raw_ostream & Stream,StringRef Style)370b57cec5SDimitry Andric void GuidAdapter::format(raw_ostream &Stream, StringRef Style) {
380b57cec5SDimitry Andric assert(Item.size() == 16 && "Expected 16-byte GUID");
39*5f7ddb14SDimitry Andric struct MSGuid {
40*5f7ddb14SDimitry Andric support::ulittle32_t Data1;
41*5f7ddb14SDimitry Andric support::ulittle16_t Data2;
42*5f7ddb14SDimitry Andric support::ulittle16_t Data3;
43*5f7ddb14SDimitry Andric support::ubig64_t Data4;
44*5f7ddb14SDimitry Andric };
45*5f7ddb14SDimitry Andric const MSGuid *G = reinterpret_cast<const MSGuid *>(Item.data());
46*5f7ddb14SDimitry Andric Stream
47*5f7ddb14SDimitry Andric << '{' << format_hex_no_prefix(G->Data1, 8, /*Upper=*/true)
48*5f7ddb14SDimitry Andric << '-' << format_hex_no_prefix(G->Data2, 4, /*Upper=*/true)
49*5f7ddb14SDimitry Andric << '-' << format_hex_no_prefix(G->Data3, 4, /*Upper=*/true)
50*5f7ddb14SDimitry Andric << '-' << format_hex_no_prefix(G->Data4 >> 48, 4, /*Upper=*/true) << '-'
51*5f7ddb14SDimitry Andric << format_hex_no_prefix(G->Data4 & ((1ULL << 48) - 1), 12, /*Upper=*/true)
52*5f7ddb14SDimitry Andric << '}';
530b57cec5SDimitry Andric }
540b57cec5SDimitry Andric
operator <<(raw_ostream & OS,const GUID & Guid)550b57cec5SDimitry Andric raw_ostream &llvm::codeview::operator<<(raw_ostream &OS, const GUID &Guid) {
560b57cec5SDimitry Andric codeview::detail::GuidAdapter A(Guid.Guid);
570b57cec5SDimitry Andric A.format(OS, "");
580b57cec5SDimitry Andric return OS;
590b57cec5SDimitry Andric }
60