1a580b014SDimitry Andric //===- Formatters.cpp -----------------------------------------------------===//
27a7e6055SDimitry Andric //
37a7e6055SDimitry Andric //                     The LLVM Compiler Infrastructure
47a7e6055SDimitry Andric //
57a7e6055SDimitry Andric // This file is distributed under the University of Illinois Open Source
67a7e6055SDimitry Andric // License. See LICENSE.TXT for details.
77a7e6055SDimitry Andric //
87a7e6055SDimitry Andric //===----------------------------------------------------------------------===//
97a7e6055SDimitry Andric 
107a7e6055SDimitry Andric #include "llvm/DebugInfo/CodeView/Formatters.h"
11a580b014SDimitry Andric #include "llvm/ADT/ArrayRef.h"
12*b40b48b8SDimitry Andric #include "llvm/DebugInfo/CodeView/GUID.h"
13a580b014SDimitry Andric #include "llvm/Support/raw_ostream.h"
14a580b014SDimitry Andric #include <algorithm>
15a580b014SDimitry Andric #include <cassert>
167a7e6055SDimitry Andric 
177a7e6055SDimitry Andric using namespace llvm;
187a7e6055SDimitry Andric using namespace llvm::codeview;
197a7e6055SDimitry Andric using namespace llvm::codeview::detail;
207a7e6055SDimitry Andric 
GuidAdapter(StringRef Guid)217a7e6055SDimitry Andric GuidAdapter::GuidAdapter(StringRef Guid)
227a7e6055SDimitry Andric     : FormatAdapter(makeArrayRef(Guid.bytes_begin(), Guid.bytes_end())) {}
237a7e6055SDimitry Andric 
GuidAdapter(ArrayRef<uint8_t> Guid)247a7e6055SDimitry Andric GuidAdapter::GuidAdapter(ArrayRef<uint8_t> Guid)
257a7e6055SDimitry Andric     : FormatAdapter(std::move(Guid)) {}
267a7e6055SDimitry Andric 
format(raw_ostream & Stream,StringRef Style)27a580b014SDimitry Andric void GuidAdapter::format(raw_ostream &Stream, StringRef Style) {
287a7e6055SDimitry Andric   static const char *Lookup = "0123456789ABCDEF";
297a7e6055SDimitry Andric 
307a7e6055SDimitry Andric   assert(Item.size() == 16 && "Expected 16-byte GUID");
317a7e6055SDimitry Andric   Stream << "{";
327a7e6055SDimitry Andric   for (int i = 0; i < 16;) {
337a7e6055SDimitry Andric     uint8_t Byte = Item[i];
347a7e6055SDimitry Andric     uint8_t HighNibble = (Byte >> 4) & 0xF;
357a7e6055SDimitry Andric     uint8_t LowNibble = Byte & 0xF;
367a7e6055SDimitry Andric     Stream << Lookup[HighNibble] << Lookup[LowNibble];
377a7e6055SDimitry Andric     ++i;
387a7e6055SDimitry Andric     if (i >= 4 && i <= 10 && i % 2 == 0)
397a7e6055SDimitry Andric       Stream << "-";
407a7e6055SDimitry Andric   }
417a7e6055SDimitry Andric   Stream << "}";
427a7e6055SDimitry Andric }
43*b40b48b8SDimitry Andric 
operator <<(raw_ostream & OS,const GUID & Guid)44*b40b48b8SDimitry Andric raw_ostream &llvm::codeview::operator<<(raw_ostream &OS, const GUID &Guid) {
45*b40b48b8SDimitry Andric   codeview::detail::GuidAdapter A(Guid.Guid);
46*b40b48b8SDimitry Andric   A.format(OS, "");
47*b40b48b8SDimitry Andric   return OS;
48*b40b48b8SDimitry Andric }
49