Skip to content
This repository was archived by the owner on Apr 23, 2020. It is now read-only.

Commit 1f1f02a

Browse files
committed
Slightly simplify MappedBlockStream::createIndexedStream() calls
All callers had a PDBFile object at hand, so call Pdb.createIndexedStream() instead, which pre-populates all the arguments (and returns nullptr for kInvalidStreamIndex). Also change safelyCreateIndexedStream() to only take the string index, and update callers. Make the method public and call it in two places that manually did the bounds checking before. No intended behavior change. Differential Revision: https://reviews.llvm.org/D64633 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@365936 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent d4b9425 commit 1f1f02a

File tree

8 files changed

+31
-48
lines changed

8 files changed

+31
-48
lines changed

include/llvm/DebugInfo/PDB/Native/PDBFile.h

+4-6
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,10 @@ class PDBFile : public msf::IMSFFile {
8383

8484
ArrayRef<support::ulittle32_t> getDirectoryBlockArray() const;
8585

86-
std::unique_ptr<msf::MappedBlockStream> createIndexedStream(uint16_t SN);
86+
std::unique_ptr<msf::MappedBlockStream>
87+
createIndexedStream(uint16_t SN) const;
88+
Expected<std::unique_ptr<msf::MappedBlockStream>>
89+
safelyCreateIndexedStream(uint32_t StreamIndex) const;
8790

8891
msf::MSFStreamLayout getStreamLayout(uint32_t StreamIdx) const;
8992
msf::MSFStreamLayout getFpmStreamLayout() const;
@@ -114,11 +117,6 @@ class PDBFile : public msf::IMSFFile {
114117
uint32_t getPointerSize();
115118

116119
private:
117-
Expected<std::unique_ptr<msf::MappedBlockStream>>
118-
safelyCreateIndexedStream(const msf::MSFLayout &Layout,
119-
BinaryStreamRef MsfData,
120-
uint32_t StreamIndex) const;
121-
122120
std::string FilePath;
123121
BumpPtrAllocator &Allocator;
124122

lib/DebugInfo/PDB/Native/DbiStream.cpp

+2-6
Original file line numberDiff line numberDiff line change
@@ -333,15 +333,11 @@ DbiStream::createIndexedStreamForHeaderType(PDBFile *Pdb,
333333

334334
uint32_t StreamNum = getDebugStreamIndex(Type);
335335

336-
// This means there is no such stream
336+
// This means there is no such stream.
337337
if (StreamNum == kInvalidStreamIndex)
338338
return nullptr;
339339

340-
if (StreamNum >= Pdb->getNumStreams())
341-
return make_error<RawError>(raw_error_code::no_stream);
342-
343-
return MappedBlockStream::createIndexedStream(
344-
Pdb->getMsfLayout(), Pdb->getMsfBuffer(), StreamNum, Pdb->getAllocator());
340+
return Pdb->safelyCreateIndexedStream(StreamNum);
345341
}
346342

347343
BinarySubstreamRef DbiStream::getSectionContributionData() const {

lib/DebugInfo/PDB/Native/PDBFile.cpp

+15-18
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,8 @@ ArrayRef<support::ulittle32_t> PDBFile::getDirectoryBlockArray() const {
233233
return ContainerLayout.DirectoryBlocks;
234234
}
235235

236-
std::unique_ptr<MappedBlockStream> PDBFile::createIndexedStream(uint16_t SN) {
236+
std::unique_ptr<MappedBlockStream>
237+
PDBFile::createIndexedStream(uint16_t SN) const {
237238
if (SN == kInvalidStreamIndex)
238239
return nullptr;
239240
return MappedBlockStream::createIndexedStream(ContainerLayout, *Buffer, SN,
@@ -258,8 +259,8 @@ Expected<GlobalsStream &> PDBFile::getPDBGlobalsStream() {
258259
if (!DbiS)
259260
return DbiS.takeError();
260261

261-
auto GlobalS = safelyCreateIndexedStream(
262-
ContainerLayout, *Buffer, DbiS->getGlobalSymbolStreamIndex());
262+
auto GlobalS =
263+
safelyCreateIndexedStream(DbiS->getGlobalSymbolStreamIndex());
263264
if (!GlobalS)
264265
return GlobalS.takeError();
265266
auto TempGlobals = llvm::make_unique<GlobalsStream>(std::move(*GlobalS));
@@ -272,7 +273,7 @@ Expected<GlobalsStream &> PDBFile::getPDBGlobalsStream() {
272273

273274
Expected<InfoStream &> PDBFile::getPDBInfoStream() {
274275
if (!Info) {
275-
auto InfoS = safelyCreateIndexedStream(ContainerLayout, *Buffer, StreamPDB);
276+
auto InfoS = safelyCreateIndexedStream(StreamPDB);
276277
if (!InfoS)
277278
return InfoS.takeError();
278279
auto TempInfo = llvm::make_unique<InfoStream>(std::move(*InfoS));
@@ -285,7 +286,7 @@ Expected<InfoStream &> PDBFile::getPDBInfoStream() {
285286

286287
Expected<DbiStream &> PDBFile::getPDBDbiStream() {
287288
if (!Dbi) {
288-
auto DbiS = safelyCreateIndexedStream(ContainerLayout, *Buffer, StreamDBI);
289+
auto DbiS = safelyCreateIndexedStream(StreamDBI);
289290
if (!DbiS)
290291
return DbiS.takeError();
291292
auto TempDbi = llvm::make_unique<DbiStream>(std::move(*DbiS));
@@ -298,7 +299,7 @@ Expected<DbiStream &> PDBFile::getPDBDbiStream() {
298299

299300
Expected<TpiStream &> PDBFile::getPDBTpiStream() {
300301
if (!Tpi) {
301-
auto TpiS = safelyCreateIndexedStream(ContainerLayout, *Buffer, StreamTPI);
302+
auto TpiS = safelyCreateIndexedStream(StreamTPI);
302303
if (!TpiS)
303304
return TpiS.takeError();
304305
auto TempTpi = llvm::make_unique<TpiStream>(*this, std::move(*TpiS));
@@ -314,7 +315,7 @@ Expected<TpiStream &> PDBFile::getPDBIpiStream() {
314315
if (!hasPDBIpiStream())
315316
return make_error<RawError>(raw_error_code::no_stream);
316317

317-
auto IpiS = safelyCreateIndexedStream(ContainerLayout, *Buffer, StreamIPI);
318+
auto IpiS = safelyCreateIndexedStream(StreamIPI);
318319
if (!IpiS)
319320
return IpiS.takeError();
320321
auto TempIpi = llvm::make_unique<TpiStream>(*this, std::move(*IpiS));
@@ -331,8 +332,8 @@ Expected<PublicsStream &> PDBFile::getPDBPublicsStream() {
331332
if (!DbiS)
332333
return DbiS.takeError();
333334

334-
auto PublicS = safelyCreateIndexedStream(
335-
ContainerLayout, *Buffer, DbiS->getPublicSymbolStreamIndex());
335+
auto PublicS =
336+
safelyCreateIndexedStream(DbiS->getPublicSymbolStreamIndex());
336337
if (!PublicS)
337338
return PublicS.takeError();
338339
auto TempPublics = llvm::make_unique<PublicsStream>(std::move(*PublicS));
@@ -350,8 +351,7 @@ Expected<SymbolStream &> PDBFile::getPDBSymbolStream() {
350351
return DbiS.takeError();
351352

352353
uint32_t SymbolStreamNum = DbiS->getSymRecordStreamIndex();
353-
auto SymbolS =
354-
safelyCreateIndexedStream(ContainerLayout, *Buffer, SymbolStreamNum);
354+
auto SymbolS = safelyCreateIndexedStream(SymbolStreamNum);
355355
if (!SymbolS)
356356
return SymbolS.takeError();
357357

@@ -374,8 +374,7 @@ Expected<PDBStringTable &> PDBFile::getStringTable() {
374374
return ExpectedNSI.takeError();
375375
uint32_t NameStreamIndex = *ExpectedNSI;
376376

377-
auto NS =
378-
safelyCreateIndexedStream(ContainerLayout, *Buffer, NameStreamIndex);
377+
auto NS = safelyCreateIndexedStream(NameStreamIndex);
379378
if (!NS)
380379
return NS.takeError();
381380

@@ -463,11 +462,9 @@ bool PDBFile::hasPDBStringTable() {
463462
/// will have an MSFError with code msf_error_code::no_stream. Else, the return
464463
/// value will contain the stream returned by createIndexedStream().
465464
Expected<std::unique_ptr<MappedBlockStream>>
466-
PDBFile::safelyCreateIndexedStream(const MSFLayout &Layout,
467-
BinaryStreamRef MsfData,
468-
uint32_t StreamIndex) const {
465+
PDBFile::safelyCreateIndexedStream(uint32_t StreamIndex) const {
469466
if (StreamIndex >= getNumStreams())
467+
// This rejects kInvalidStreamIndex with an error as well.
470468
return make_error<RawError>(raw_error_code::no_stream);
471-
return MappedBlockStream::createIndexedStream(Layout, MsfData, StreamIndex,
472-
Allocator);
469+
return createIndexedStream(StreamIndex);
473470
}

lib/DebugInfo/PDB/Native/TpiStream.cpp

+6-7
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,13 @@ Error TpiStream::reload() {
7878

7979
// Hash indices, hash values, etc come from the hash stream.
8080
if (Header->HashStreamIndex != kInvalidStreamIndex) {
81-
if (Header->HashStreamIndex >= Pdb.getNumStreams())
81+
auto HS = Pdb.safelyCreateIndexedStream(Header->HashStreamIndex);
82+
if (!HS) {
83+
consumeError(HS.takeError());
8284
return make_error<RawError>(raw_error_code::corrupt_file,
8385
"Invalid TPI hash stream index.");
84-
85-
auto HS = MappedBlockStream::createIndexedStream(
86-
Pdb.getMsfLayout(), Pdb.getMsfBuffer(), Header->HashStreamIndex,
87-
Pdb.getAllocator());
88-
BinaryStreamReader HSR(*HS);
86+
}
87+
BinaryStreamReader HSR(**HS);
8988

9089
// There should be a hash value for every type record, or no hashes at all.
9190
uint32_t NumHashValues =
@@ -110,7 +109,7 @@ Error TpiStream::reload() {
110109
return EC;
111110
}
112111

113-
HashStream = std::move(HS);
112+
HashStream = std::move(*HS);
114113
}
115114

116115
Types = llvm::make_unique<LazyRandomTypeCollection>(

tools/llvm-pdbutil/BytesOutputStyle.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -340,9 +340,7 @@ static void iterateOneModule(PDBFile &File, LinePrinter &P,
340340
if (ModiStream == kInvalidStreamIndex)
341341
return;
342342

343-
auto ModStreamData = MappedBlockStream::createIndexedStream(
344-
File.getMsfLayout(), File.getMsfBuffer(), ModiStream,
345-
File.getAllocator());
343+
auto ModStreamData = File.createIndexedStream(ModiStream);
346344
ModuleDebugStreamRef ModStream(Modi, std::move(ModStreamData));
347345
if (auto EC = ModStream.reload()) {
348346
P.formatLine("Could not parse debug information.");

tools/llvm-pdbutil/LinePrinter.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,7 @@ void LinePrinter::formatMsfStreamData(StringRef Label, PDBFile &File,
186186
return;
187187
}
188188

189-
auto S = MappedBlockStream::createIndexedStream(
190-
File.getMsfLayout(), File.getMsfBuffer(), StreamIdx, File.getAllocator());
189+
auto S = File.createIndexedStream(StreamIdx);
191190
if (!S) {
192191
NewLine();
193192
formatLine("Stream {0}: Not present", StreamIdx);

tools/llvm-pdbutil/YAMLOutputStyle.cpp

+1-4
Original file line numberDiff line numberDiff line change
@@ -231,10 +231,7 @@ Error YAMLOutputStyle::dumpDbiStream() {
231231
if (ModiStream == kInvalidStreamIndex)
232232
continue;
233233

234-
auto ModStreamData = msf::MappedBlockStream::createIndexedStream(
235-
File.getMsfLayout(), File.getMsfBuffer(), ModiStream,
236-
File.getAllocator());
237-
234+
auto ModStreamData = File.createIndexedStream(ModiStream);
238235
pdb::ModuleDebugStreamRef ModS(MI, std::move(ModStreamData));
239236
if (auto EC = ModS.reload())
240237
return EC;

tools/llvm-pdbutil/llvm-pdbutil.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -1384,8 +1384,7 @@ static void exportStream() {
13841384
<< "' (index " << Index << ") to file " << OutFileName << ".\n";
13851385
}
13861386

1387-
SourceStream = MappedBlockStream::createIndexedStream(
1388-
File.getMsfLayout(), File.getMsfBuffer(), Index, File.getAllocator());
1387+
SourceStream = File.createIndexedStream(Index);
13891388
auto OutFile = ExitOnErr(
13901389
FileOutputBuffer::create(OutFileName, SourceStream->getLength()));
13911390
FileBufferByteStream DestStream(std::move(OutFile), llvm::support::little);

0 commit comments

Comments
 (0)