blob: a7a8c7ff82bf317ba3b12cb878c37497179f45ed [file] [log] [blame]
Eugene Zelenko4fcfc192017-06-30 23:06:03 +00001//===- Formatters.cpp -----------------------------------------------------===//
Zachary Turner5ce0f4a2017-02-03 21:22:27 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Zachary Turner5ce0f4a2017-02-03 21:22:27 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/DebugInfo/CodeView/Formatters.h"
Eugene Zelenko4fcfc192017-06-30 23:06:03 +000010#include "llvm/ADT/ArrayRef.h"
Reid Kleckner67653ee2017-07-17 23:59:44 +000011#include "llvm/DebugInfo/CodeView/GUID.h"
Eugene Zelenko4fcfc192017-06-30 23:06:03 +000012#include "llvm/Support/raw_ostream.h"
13#include <algorithm>
14#include <cassert>
Zachary Turner5ce0f4a2017-02-03 21:22:27 +000015
16using namespace llvm;
17using namespace llvm::codeview;
18using namespace llvm::codeview::detail;
19
20GuidAdapter::GuidAdapter(StringRef Guid)
21 : FormatAdapter(makeArrayRef(Guid.bytes_begin(), Guid.bytes_end())) {}
22
23GuidAdapter::GuidAdapter(ArrayRef<uint8_t> Guid)
24 : FormatAdapter(std::move(Guid)) {}
25
Eugene Zelenko4fcfc192017-06-30 23:06:03 +000026void GuidAdapter::format(raw_ostream &Stream, StringRef Style) {
Zachary Turner5ce0f4a2017-02-03 21:22:27 +000027 static const char *Lookup = "0123456789ABCDEF";
28
29 assert(Item.size() == 16 && "Expected 16-byte GUID");
30 Stream << "{";
31 for (int i = 0; i < 16;) {
32 uint8_t Byte = Item[i];
33 uint8_t HighNibble = (Byte >> 4) & 0xF;
34 uint8_t LowNibble = Byte & 0xF;
35 Stream << Lookup[HighNibble] << Lookup[LowNibble];
36 ++i;
37 if (i >= 4 && i <= 10 && i % 2 == 0)
38 Stream << "-";
39 }
40 Stream << "}";
41}
Reid Kleckner67653ee2017-07-17 23:59:44 +000042
43raw_ostream &llvm::codeview::operator<<(raw_ostream &OS, const GUID &Guid) {
44 codeview::detail::GuidAdapter A(Guid.Guid);
45 A.format(OS, "");
46 return OS;
47}