1 //===- DIADataStream.cpp - DIA implementation of IPDBDataStream -*- C++ -*-===//
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/PDB/DIA/DIADataStream.h"
11 #include "llvm/Support/ConvertUTF.h"
12 
13 using namespace llvm;
14 
DIADataStream(CComPtr<IDiaEnumDebugStreamData> DiaStreamData)15 DIADataStream::DIADataStream(CComPtr<IDiaEnumDebugStreamData> DiaStreamData)
16     : StreamData(DiaStreamData) {}
17 
getRecordCount() const18 uint32_t DIADataStream::getRecordCount() const {
19   LONG Count = 0;
20   return (S_OK == StreamData->get_Count(&Count)) ? Count : 0;
21 }
22 
getName() const23 std::string DIADataStream::getName() const {
24   CComBSTR Name16;
25   if (S_OK != StreamData->get_name(&Name16))
26     return std::string();
27 
28   std::string Name8;
29   llvm::ArrayRef<char> Name16Bytes(reinterpret_cast<char *>(Name16.m_str),
30                                    Name16.ByteLength());
31   if (!llvm::convertUTF16ToUTF8String(Name16Bytes, Name8))
32     return std::string();
33   return Name8;
34 }
35 
36 llvm::Optional<DIADataStream::RecordType>
getItemAtIndex(uint32_t Index) const37 DIADataStream::getItemAtIndex(uint32_t Index) const {
38   RecordType Record;
39   DWORD RecordSize = 0;
40   StreamData->Item(Index, 0, &RecordSize, nullptr);
41   if (RecordSize == 0)
42     return llvm::Optional<RecordType>();
43 
44   Record.resize(RecordSize);
45   if (S_OK != StreamData->Item(Index, RecordSize, &RecordSize, &Record[0]))
46     return llvm::Optional<RecordType>();
47   return Record;
48 }
49 
getNext(RecordType & Record)50 bool DIADataStream::getNext(RecordType &Record) {
51   Record.clear();
52   DWORD RecordSize = 0;
53   ULONG CountFetched = 0;
54   StreamData->Next(1, 0, &RecordSize, nullptr, &CountFetched);
55   if (RecordSize == 0)
56     return false;
57 
58   Record.resize(RecordSize);
59   if (S_OK ==
60       StreamData->Next(1, RecordSize, &RecordSize, &Record[0], &CountFetched))
61     return false;
62   return true;
63 }
64 
reset()65 void DIADataStream::reset() { StreamData->Reset(); }
66 
clone() const67 DIADataStream *DIADataStream::clone() const {
68   CComPtr<IDiaEnumDebugStreamData> EnumeratorClone;
69   if (S_OK != StreamData->Clone(&EnumeratorClone))
70     return nullptr;
71 
72   return new DIADataStream(EnumeratorClone);
73 }
74