• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/ADT/ArrayRef.h"
12 #include "llvm/Support/ConvertUTF.h"
13 
14 using namespace llvm;
15 using namespace llvm::pdb;
16 
DIADataStream(CComPtr<IDiaEnumDebugStreamData> DiaStreamData)17 DIADataStream::DIADataStream(CComPtr<IDiaEnumDebugStreamData> DiaStreamData)
18     : StreamData(DiaStreamData) {}
19 
getRecordCount() const20 uint32_t DIADataStream::getRecordCount() const {
21   LONG Count = 0;
22   return (S_OK == StreamData->get_Count(&Count)) ? Count : 0;
23 }
24 
getName() const25 std::string DIADataStream::getName() const {
26   CComBSTR Name16;
27   if (S_OK != StreamData->get_name(&Name16))
28     return std::string();
29 
30   std::string Name8;
31   llvm::ArrayRef<char> Name16Bytes(reinterpret_cast<char *>(Name16.m_str),
32                                    Name16.ByteLength());
33   if (!llvm::convertUTF16ToUTF8String(Name16Bytes, Name8))
34     return std::string();
35   return Name8;
36 }
37 
38 llvm::Optional<DIADataStream::RecordType>
getItemAtIndex(uint32_t Index) const39 DIADataStream::getItemAtIndex(uint32_t Index) const {
40   RecordType Record;
41   DWORD RecordSize = 0;
42   StreamData->Item(Index, 0, &RecordSize, nullptr);
43   if (RecordSize == 0)
44     return llvm::Optional<RecordType>();
45 
46   Record.resize(RecordSize);
47   if (S_OK != StreamData->Item(Index, RecordSize, &RecordSize, &Record[0]))
48     return llvm::Optional<RecordType>();
49   return Record;
50 }
51 
getNext(RecordType & Record)52 bool DIADataStream::getNext(RecordType &Record) {
53   Record.clear();
54   DWORD RecordSize = 0;
55   ULONG CountFetched = 0;
56   StreamData->Next(1, 0, &RecordSize, nullptr, &CountFetched);
57   if (RecordSize == 0)
58     return false;
59 
60   Record.resize(RecordSize);
61   if (S_OK ==
62       StreamData->Next(1, RecordSize, &RecordSize, &Record[0], &CountFetched))
63     return false;
64   return true;
65 }
66 
reset()67 void DIADataStream::reset() { StreamData->Reset(); }
68 
clone() const69 DIADataStream *DIADataStream::clone() const {
70   CComPtr<IDiaEnumDebugStreamData> EnumeratorClone;
71   if (S_OK != StreamData->Clone(&EnumeratorClone))
72     return nullptr;
73 
74   return new DIADataStream(EnumeratorClone);
75 }
76