Eugene Zelenko | 4fcfc19 | 2017-06-30 23:06:03 +0000 | [diff] [blame^] | 1 | //===- Formatters.cpp -----------------------------------------------------===// |
Zachary Turner | 5ce0f4a | 2017-02-03 21:22:27 +0000 | [diff] [blame] | 2 | // |
| 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 Zelenko | 4fcfc19 | 2017-06-30 23:06:03 +0000 | [diff] [blame^] | 11 | #include "llvm/ADT/ArrayRef.h" |
| 12 | #include "llvm/Support/raw_ostream.h" |
| 13 | #include <algorithm> |
| 14 | #include <cassert> |
Zachary Turner | 5ce0f4a | 2017-02-03 21:22:27 +0000 | [diff] [blame] | 15 | |
| 16 | using namespace llvm; |
| 17 | using namespace llvm::codeview; |
| 18 | using namespace llvm::codeview::detail; |
| 19 | |
| 20 | GuidAdapter::GuidAdapter(StringRef Guid) |
| 21 | : FormatAdapter(makeArrayRef(Guid.bytes_begin(), Guid.bytes_end())) {} |
| 22 | |
| 23 | GuidAdapter::GuidAdapter(ArrayRef<uint8_t> Guid) |
| 24 | : FormatAdapter(std::move(Guid)) {} |
| 25 | |
Eugene Zelenko | 4fcfc19 | 2017-06-30 23:06:03 +0000 | [diff] [blame^] | 26 | void GuidAdapter::format(raw_ostream &Stream, StringRef Style) { |
Zachary Turner | 5ce0f4a | 2017-02-03 21:22:27 +0000 | [diff] [blame] | 27 | 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 | } |