1 //===- DIASourceFile.cpp - DIA implementation of IPDBSourceFile -*- 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/DIASourceFile.h"
11 #include "llvm/ADT/ArrayRef.h"
12 #include "llvm/DebugInfo/PDB/ConcreteSymbolEnumerator.h"
13 #include "llvm/DebugInfo/PDB/DIA/DIAEnumSymbols.h"
14 #include "llvm/DebugInfo/PDB/DIA/DIASession.h"
15 #include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
16 #include "llvm/Support/ConvertUTF.h"
17 
18 using namespace llvm;
19 
20 DIASourceFile::DIASourceFile(const DIASession &PDBSession,
21                              CComPtr<IDiaSourceFile> DiaSourceFile)
22     : Session(PDBSession), SourceFile(DiaSourceFile) {}
23 
24 std::string DIASourceFile::getFileName() const {
25   CComBSTR FileName16;
26   HRESULT Result = SourceFile->get_fileName(&FileName16);
27   if (S_OK != Result)
28     return std::string();
29 
30   std::string FileName8;
31   llvm::ArrayRef<char> FileNameBytes(reinterpret_cast<char *>(FileName16.m_str),
32                                      FileName16.ByteLength());
33   llvm::convertUTF16ToUTF8String(FileNameBytes, FileName8);
34   return FileName8;
35 }
36 
37 uint32_t DIASourceFile::getUniqueId() const {
38   DWORD Id;
39   return (S_OK == SourceFile->get_uniqueId(&Id)) ? Id : 0;
40 }
41 
42 std::string DIASourceFile::getChecksum() const {
43   DWORD ByteSize = 0;
44   HRESULT Result = SourceFile->get_checksum(0, &ByteSize, nullptr);
45   if (ByteSize == 0)
46     return std::string();
47   std::vector<BYTE> ChecksumBytes(ByteSize);
48   Result = SourceFile->get_checksum(ByteSize, &ByteSize, &ChecksumBytes[0]);
49   if (S_OK != Result)
50     return std::string();
51   return std::string(ChecksumBytes.begin(), ChecksumBytes.end());
52 }
53 
54 PDB_Checksum DIASourceFile::getChecksumType() const {
55   DWORD Type;
56   HRESULT Result = SourceFile->get_checksumType(&Type);
57   if (S_OK != Result)
58     return PDB_Checksum::None;
59   return static_cast<PDB_Checksum>(Type);
60 }
61 
62 std::unique_ptr<IPDBEnumChildren<PDBSymbolCompiland>>
63 DIASourceFile::getCompilands() const {
64   CComPtr<IDiaEnumSymbols> DiaEnumerator;
65   HRESULT Result = SourceFile->get_compilands(&DiaEnumerator);
66   if (S_OK != Result)
67     return nullptr;
68 
69   auto Enumerator = std::unique_ptr<IPDBEnumSymbols>(
70       new DIAEnumSymbols(Session, DiaEnumerator));
71   return std::unique_ptr<IPDBEnumChildren<PDBSymbolCompiland>>(
72       new ConcreteSymbolEnumerator<PDBSymbolCompiland>(std::move(Enumerator)));
73 }
74