blob: b8d89c76da3b6fbfb442e0fa658b78bb4f2878ea [file] [log] [blame]
Eugene Zelenko4fcfc192017-06-30 23:06:03 +00001//===- Formatters.cpp -----------------------------------------------------===//
Zachary Turner5ce0f4a2017-02-03 21:22:27 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/DebugInfo/CodeView/Formatters.h"
Eugene Zelenko4fcfc192017-06-30 23:06:03 +000011#include "llvm/ADT/ArrayRef.h"
Reid Kleckner67653ee2017-07-17 23:59:44 +000012#include "llvm/DebugInfo/CodeView/GUID.h"
Eugene Zelenko4fcfc192017-06-30 23:06:03 +000013#include "llvm/Support/raw_ostream.h"
14#include <algorithm>
15#include <cassert>
Zachary Turner5ce0f4a2017-02-03 21:22:27 +000016
17using namespace llvm;
18using namespace llvm::codeview;
19using namespace llvm::codeview::detail;
20
21GuidAdapter::GuidAdapter(StringRef Guid)
22 : FormatAdapter(makeArrayRef(Guid.bytes_begin(), Guid.bytes_end())) {}
23
24GuidAdapter::GuidAdapter(ArrayRef<uint8_t> Guid)
25 : FormatAdapter(std::move(Guid)) {}
26
Eugene Zelenko4fcfc192017-06-30 23:06:03 +000027void GuidAdapter::format(raw_ostream &Stream, StringRef Style) {
Zachary Turner5ce0f4a2017-02-03 21:22:27 +000028 static const char *Lookup = "0123456789ABCDEF";
29
30 assert(Item.size() == 16 && "Expected 16-byte GUID");
31 Stream << "{";
32 for (int i = 0; i < 16;) {
33 uint8_t Byte = Item[i];
34 uint8_t HighNibble = (Byte >> 4) & 0xF;
35 uint8_t LowNibble = Byte & 0xF;
36 Stream << Lookup[HighNibble] << Lookup[LowNibble];
37 ++i;
38 if (i >= 4 && i <= 10 && i % 2 == 0)
39 Stream << "-";
40 }
41 Stream << "}";
42}
Reid Kleckner67653ee2017-07-17 23:59:44 +000043
44raw_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}