xref: /freebsd-src/contrib/llvm-project/llvm/lib/DebugInfo/CodeView/Formatters.cpp (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
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"
1281ad6265SDimitry Andric #include "llvm/Support/Endian.h"
1381ad6265SDimitry Andric #include "llvm/Support/ErrorHandling.h"
1481ad6265SDimitry Andric #include "llvm/Support/Format.h"
150b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
160b57cec5SDimitry Andric #include <cassert>
170b57cec5SDimitry Andric 
180b57cec5SDimitry Andric using namespace llvm;
190b57cec5SDimitry Andric using namespace llvm::codeview;
200b57cec5SDimitry Andric using namespace llvm::codeview::detail;
210b57cec5SDimitry Andric 
GuidAdapter(StringRef Guid)220b57cec5SDimitry Andric GuidAdapter::GuidAdapter(StringRef Guid)
23*bdd1243dSDimitry Andric     : FormatAdapter(ArrayRef(Guid.bytes_begin(), Guid.bytes_end())) {}
240b57cec5SDimitry Andric 
GuidAdapter(ArrayRef<uint8_t> Guid)250b57cec5SDimitry Andric GuidAdapter::GuidAdapter(ArrayRef<uint8_t> Guid)
260b57cec5SDimitry Andric     : FormatAdapter(std::move(Guid)) {}
270b57cec5SDimitry Andric 
28fe6060f1SDimitry Andric // From https://docs.microsoft.com/en-us/windows/win32/msi/guid documentation:
29fe6060f1SDimitry Andric // The GUID data type is a text string representing a Class identifier (ID).
30fe6060f1SDimitry Andric // All GUIDs must be authored in uppercase.
31fe6060f1SDimitry Andric // The valid format for a GUID is {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX} where
32fe6060f1SDimitry Andric // X is a hex digit (0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F).
33fe6060f1SDimitry Andric //
34fe6060f1SDimitry Andric // The individual string components must be padded to comply with the specific
35fe6060f1SDimitry Andric // lengths of {8-4-4-4-12} characters.
36fe6060f1SDimitry Andric // The llvm-yaml2obj tool checks that a GUID follow that format:
37fe6060f1SDimitry Andric // - the total length to be 38 (including the curly braces.
38fe6060f1SDimitry Andric // - there is a dash at the positions: 8, 13, 18 and 23.
format(raw_ostream & Stream,StringRef Style)390b57cec5SDimitry Andric void GuidAdapter::format(raw_ostream &Stream, StringRef Style) {
400b57cec5SDimitry Andric   assert(Item.size() == 16 && "Expected 16-byte GUID");
41fe6060f1SDimitry Andric   struct MSGuid {
42fe6060f1SDimitry Andric     support::ulittle32_t Data1;
43fe6060f1SDimitry Andric     support::ulittle16_t Data2;
44fe6060f1SDimitry Andric     support::ulittle16_t Data3;
45fe6060f1SDimitry Andric     support::ubig64_t Data4;
46fe6060f1SDimitry Andric   };
47fe6060f1SDimitry Andric   const MSGuid *G = reinterpret_cast<const MSGuid *>(Item.data());
48fe6060f1SDimitry Andric   Stream
49fe6060f1SDimitry Andric       << '{' << format_hex_no_prefix(G->Data1, 8, /*Upper=*/true)
50fe6060f1SDimitry Andric       << '-' << format_hex_no_prefix(G->Data2, 4, /*Upper=*/true)
51fe6060f1SDimitry Andric       << '-' << format_hex_no_prefix(G->Data3, 4, /*Upper=*/true)
52fe6060f1SDimitry Andric       << '-' << format_hex_no_prefix(G->Data4 >> 48, 4, /*Upper=*/true) << '-'
53fe6060f1SDimitry Andric       << format_hex_no_prefix(G->Data4 & ((1ULL << 48) - 1), 12, /*Upper=*/true)
54fe6060f1SDimitry Andric       << '}';
550b57cec5SDimitry Andric }
560b57cec5SDimitry Andric 
operator <<(raw_ostream & OS,const GUID & Guid)570b57cec5SDimitry Andric raw_ostream &llvm::codeview::operator<<(raw_ostream &OS, const GUID &Guid) {
580b57cec5SDimitry Andric   codeview::detail::GuidAdapter A(Guid.Guid);
590b57cec5SDimitry Andric   A.format(OS, "");
600b57cec5SDimitry Andric   return OS;
610b57cec5SDimitry Andric }
62