xref: /netbsd-src/external/apache2/llvm/dist/llvm/lib/DebugInfo/CodeView/Formatters.cpp (revision 82d56013d7b633d116a93943de88e08335357a7c)
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 
format(raw_ostream & Stream,StringRef Style)26 void GuidAdapter::format(raw_ostream &Stream, StringRef Style) {
27   assert(Item.size() == 16 && "Expected 16-byte GUID");
28   struct MSGuid {
29     support::ulittle32_t Data1;
30     support::ulittle16_t Data2;
31     support::ulittle16_t Data3;
32     support::ubig64_t Data4;
33   };
34   const MSGuid *G = reinterpret_cast<const MSGuid *>(Item.data());
35   Stream
36       << '{' << format_hex_no_prefix(G->Data1, sizeof(G->Data1), /*Upper=*/true)
37       << '-' << format_hex_no_prefix(G->Data2, sizeof(G->Data2), /*Upper=*/true)
38       << '-' << format_hex_no_prefix(G->Data3, sizeof(G->Data3), /*Upper=*/true)
39       << '-' << format_hex_no_prefix(G->Data4 >> 48, 2, /*Upper=*/true) << '-'
40       << format_hex_no_prefix(G->Data4 & ((1ULL << 48) - 1), 6, /*Upper=*/true)
41       << '}';
42 }
43 
operator <<(raw_ostream & OS,const GUID & Guid)44 raw_ostream &llvm::codeview::operator<<(raw_ostream &OS, const GUID &Guid) {
45   codeview::detail::GuidAdapter A(Guid.Guid);
46   A.format(OS, "");
47   return OS;
48 }
49