blob: b101f90f756ed413761b4d0e6fbc292687988dfd [file] [log] [blame]
Zachary Turner2f09b502016-04-29 17:28:47 +00001//===- NameMap.cpp - PDB Name Map -------------------------------*- C++ -*-===//
Zachary Turnerf34e0162016-04-26 16:20:00 +00002//
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
Zachary Turner11036a92017-01-19 23:31:24 +000010#include "llvm/DebugInfo/PDB/Raw/NameMap.h"
11
David Majnemer36b7b082016-06-04 22:47:39 +000012#include "llvm/ADT/SparseBitVector.h"
Eugene Zelenko570e39a2016-11-23 23:16:32 +000013#include "llvm/ADT/StringMap.h"
14#include "llvm/ADT/StringRef.h"
15#include "llvm/ADT/iterator_range.h"
Zachary Turnera3225b02016-07-29 20:56:36 +000016#include "llvm/DebugInfo/MSF/StreamReader.h"
Zachary Turner11036a92017-01-19 23:31:24 +000017#include "llvm/DebugInfo/PDB/Raw/HashTable.h"
Zachary Turner819e77d2016-05-06 20:51:57 +000018#include "llvm/DebugInfo/PDB/Raw/RawError.h"
Eugene Zelenko570e39a2016-11-23 23:16:32 +000019#include "llvm/Support/Error.h"
20#include <algorithm>
21#include <cstdint>
Zachary Turnerf34e0162016-04-26 16:20:00 +000022
23using namespace llvm;
Zachary Turnerbac69d32016-07-22 19:56:05 +000024using namespace llvm::msf;
Zachary Turner2f09b502016-04-29 17:28:47 +000025using namespace llvm::pdb;
Zachary Turnerf34e0162016-04-26 16:20:00 +000026
Eugene Zelenko570e39a2016-11-23 23:16:32 +000027NameMap::NameMap() = default;
Zachary Turnerf34e0162016-04-26 16:20:00 +000028
Zachary Turnerbac69d32016-07-22 19:56:05 +000029Error NameMap::load(StreamReader &Stream) {
Zachary Turner11036a92017-01-19 23:31:24 +000030 uint32_t StringBufferSize;
31 if (auto EC = Stream.readInteger(StringBufferSize))
David Majnemer836937e2016-05-27 16:16:56 +000032 return joinErrors(std::move(EC),
33 make_error<RawError>(raw_error_code::corrupt_file,
Zachary Turner11036a92017-01-19 23:31:24 +000034 "Expected string buffer size"));
Zachary Turnerf34e0162016-04-26 16:20:00 +000035
Zachary Turner11036a92017-01-19 23:31:24 +000036 msf::ReadableStreamRef StringsBuffer;
37 if (auto EC = Stream.readStreamRef(StringsBuffer, StringBufferSize))
38 return EC;
Zachary Turnerf34e0162016-04-26 16:20:00 +000039
Zachary Turner11036a92017-01-19 23:31:24 +000040 HashTable OffsetIndexMap;
41 if (auto EC = OffsetIndexMap.load(Stream))
42 return EC;
Zachary Turnerf34e0162016-04-26 16:20:00 +000043
Zachary Turner11036a92017-01-19 23:31:24 +000044 uint32_t NameOffset;
45 uint32_t NameIndex;
46 for (const auto &Entry : OffsetIndexMap) {
47 std::tie(NameOffset, NameIndex) = Entry;
Zachary Turnerf34e0162016-04-26 16:20:00 +000048
49 // Compute the offset of the start of the string relative to the stream.
Zachary Turner11036a92017-01-19 23:31:24 +000050 msf::StreamReader NameReader(StringsBuffer);
51 NameReader.setOffset(NameOffset);
Zachary Turnerf34e0162016-04-26 16:20:00 +000052 // Pump out our c-string from the stream.
Zachary Turner8dbe3622016-05-27 01:54:44 +000053 StringRef Str;
Zachary Turner11036a92017-01-19 23:31:24 +000054 if (auto EC = NameReader.readZeroString(Str))
David Majnemer836937e2016-05-27 16:16:56 +000055 return joinErrors(std::move(EC),
56 make_error<RawError>(raw_error_code::corrupt_file,
57 "Expected name map name"));
Zachary Turnerf34e0162016-04-26 16:20:00 +000058
Zachary Turnerf34e0162016-04-26 16:20:00 +000059 // Add this to a string-map from name to stream number.
60 Mapping.insert({Str, NameIndex});
61 }
62
Zachary Turner819e77d2016-05-06 20:51:57 +000063 return Error::success();
Zachary Turnerf34e0162016-04-26 16:20:00 +000064}
65
Zachary Turner85ed80b2016-05-25 03:43:17 +000066iterator_range<StringMapConstIterator<uint32_t>> NameMap::entries() const {
Eugene Zelenko570e39a2016-11-23 23:16:32 +000067 return make_range<StringMapConstIterator<uint32_t>>(Mapping.begin(),
68 Mapping.end());
Zachary Turner85ed80b2016-05-25 03:43:17 +000069}
70
Zachary Turner2f09b502016-04-29 17:28:47 +000071bool NameMap::tryGetValue(StringRef Name, uint32_t &Value) const {
Zachary Turnerf34e0162016-04-26 16:20:00 +000072 auto Iter = Mapping.find(Name);
73 if (Iter == Mapping.end())
74 return false;
75 Value = Iter->second;
76 return true;
77}