Path: blob/main/contrib/llvm-project/llvm/lib/DebugInfo/PDB/Native/NativeEnumInjectedSources.cpp
35293 views
//==- NativeEnumInjectedSources.cpp - Native Injected Source Enumerator --*-==//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "llvm/DebugInfo/PDB/Native/NativeEnumInjectedSources.h"910#include "llvm/ADT/StringExtras.h"11#include "llvm/DebugInfo/MSF/MappedBlockStream.h"12#include "llvm/DebugInfo/PDB/Native/HashTable.h"13#include "llvm/DebugInfo/PDB/Native/PDBFile.h"14#include "llvm/DebugInfo/PDB/Native/PDBStringTable.h"15#include "llvm/DebugInfo/PDB/Native/RawTypes.h"1617namespace llvm {18namespace pdb {1920namespace {2122Expected<std::string> readStreamData(BinaryStream &Stream, uint64_t Limit) {23uint64_t Offset = 0, DataLength = std::min(Limit, Stream.getLength());24std::string Result;25Result.reserve(DataLength);26while (Offset < DataLength) {27ArrayRef<uint8_t> Data;28if (auto E = Stream.readLongestContiguousChunk(Offset, Data))29return std::move(E);30Data = Data.take_front(DataLength - Offset);31Offset += Data.size();32Result += toStringRef(Data);33}34return Result;35}3637class NativeInjectedSource final : public IPDBInjectedSource {38const SrcHeaderBlockEntry &Entry;39const PDBStringTable &Strings;40PDBFile &File;4142public:43NativeInjectedSource(const SrcHeaderBlockEntry &Entry,44PDBFile &File, const PDBStringTable &Strings)45: Entry(Entry), Strings(Strings), File(File) {}4647uint32_t getCrc32() const override { return Entry.CRC; }48uint64_t getCodeByteSize() const override { return Entry.FileSize; }4950std::string getFileName() const override {51StringRef Ret = cantFail(Strings.getStringForID(Entry.FileNI),52"InjectedSourceStream should have rejected this");53return std::string(Ret);54}5556std::string getObjectFileName() const override {57StringRef Ret = cantFail(Strings.getStringForID(Entry.ObjNI),58"InjectedSourceStream should have rejected this");59return std::string(Ret);60}6162std::string getVirtualFileName() const override {63StringRef Ret = cantFail(Strings.getStringForID(Entry.VFileNI),64"InjectedSourceStream should have rejected this");65return std::string(Ret);66}6768uint32_t getCompression() const override { return Entry.Compression; }6970std::string getCode() const override {71// Get name of stream storing the data.72StringRef VName =73cantFail(Strings.getStringForID(Entry.VFileNI),74"InjectedSourceStream should have rejected this");75std::string StreamName = ("/src/files/" + VName).str();7677// Find stream with that name and read its data.78// FIXME: Consider validating (or even loading) all this in79// InjectedSourceStream so that no error can happen here.80auto ExpectedFileStream = File.safelyCreateNamedStream(StreamName);81if (!ExpectedFileStream) {82consumeError(ExpectedFileStream.takeError());83return "(failed to open data stream)";84}8586auto Data = readStreamData(**ExpectedFileStream, Entry.FileSize);87if (!Data) {88consumeError(Data.takeError());89return "(failed to read data)";90}91return *Data;92}93};9495} // namespace9697NativeEnumInjectedSources::NativeEnumInjectedSources(98PDBFile &File, const InjectedSourceStream &IJS,99const PDBStringTable &Strings)100: File(File), Stream(IJS), Strings(Strings), Cur(Stream.begin()) {}101102uint32_t NativeEnumInjectedSources::getChildCount() const {103return static_cast<uint32_t>(Stream.size());104}105106std::unique_ptr<IPDBInjectedSource>107NativeEnumInjectedSources::getChildAtIndex(uint32_t N) const {108if (N >= getChildCount())109return nullptr;110return std::make_unique<NativeInjectedSource>(std::next(Stream.begin(), N)->second,111File, Strings);112}113114std::unique_ptr<IPDBInjectedSource> NativeEnumInjectedSources::getNext() {115if (Cur == Stream.end())116return nullptr;117return std::make_unique<NativeInjectedSource>((Cur++)->second, File, Strings);118}119120void NativeEnumInjectedSources::reset() { Cur = Stream.begin(); }121122}123}124125126