LLVM  15.0.0git
NativeEnumInjectedSources.cpp
Go to the documentation of this file.
1 //==- NativeEnumInjectedSources.cpp - Native Injected Source Enumerator --*-==//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
10 
16 
17 namespace llvm {
18 namespace pdb {
19 
20 namespace {
21 
22 Expected<std::string> readStreamData(BinaryStream &Stream, uint64_t Limit) {
23  uint64_t Offset = 0, DataLength = std::min(Limit, Stream.getLength());
24  std::string Result;
25  Result.reserve(DataLength);
26  while (Offset < DataLength) {
27  ArrayRef<uint8_t> Data;
28  if (auto E = Stream.readLongestContiguousChunk(Offset, Data))
29  return std::move(E);
30  Data = Data.take_front(DataLength - Offset);
31  Offset += Data.size();
32  Result += toStringRef(Data);
33  }
34  return Result;
35 }
36 
37 class NativeInjectedSource final : public IPDBInjectedSource {
38  const SrcHeaderBlockEntry &Entry;
39  const PDBStringTable &Strings;
40  PDBFile &File;
41 
42 public:
43  NativeInjectedSource(const SrcHeaderBlockEntry &Entry,
44  PDBFile &File, const PDBStringTable &Strings)
45  : Entry(Entry), Strings(Strings), File(File) {}
46 
47  uint32_t getCrc32() const override { return Entry.CRC; }
48  uint64_t getCodeByteSize() const override { return Entry.FileSize; }
49 
50  std::string getFileName() const override {
51  StringRef Ret = cantFail(Strings.getStringForID(Entry.FileNI),
52  "InjectedSourceStream should have rejected this");
53  return std::string(Ret);
54  }
55 
56  std::string getObjectFileName() const override {
57  StringRef Ret = cantFail(Strings.getStringForID(Entry.ObjNI),
58  "InjectedSourceStream should have rejected this");
59  return std::string(Ret);
60  }
61 
62  std::string getVirtualFileName() const override {
63  StringRef Ret = cantFail(Strings.getStringForID(Entry.VFileNI),
64  "InjectedSourceStream should have rejected this");
65  return std::string(Ret);
66  }
67 
68  uint32_t getCompression() const override { return Entry.Compression; }
69 
70  std::string getCode() const override {
71  // Get name of stream storing the data.
72  StringRef VName =
73  cantFail(Strings.getStringForID(Entry.VFileNI),
74  "InjectedSourceStream should have rejected this");
75  std::string StreamName = ("/src/files/" + VName).str();
76 
77  // Find stream with that name and read its data.
78  // FIXME: Consider validating (or even loading) all this in
79  // InjectedSourceStream so that no error can happen here.
80  auto ExpectedFileStream = File.safelyCreateNamedStream(StreamName);
81  if (!ExpectedFileStream) {
82  consumeError(ExpectedFileStream.takeError());
83  return "(failed to open data stream)";
84  }
85 
86  auto Data = readStreamData(**ExpectedFileStream, Entry.FileSize);
87  if (!Data) {
88  consumeError(Data.takeError());
89  return "(failed to read data)";
90  }
91  return *Data;
92  }
93 };
94 
95 } // namespace
96 
98  PDBFile &File, const InjectedSourceStream &IJS,
99  const PDBStringTable &Strings)
100  : File(File), Stream(IJS), Strings(Strings), Cur(Stream.begin()) {}
101 
103  return static_cast<uint32_t>(Stream.size());
104 }
105 
106 std::unique_ptr<IPDBInjectedSource>
108  if (N >= getChildCount())
109  return nullptr;
110  return std::make_unique<NativeInjectedSource>(std::next(Stream.begin(), N)->second,
111  File, Strings);
112 }
113 
114 std::unique_ptr<IPDBInjectedSource> NativeEnumInjectedSources::getNext() {
115  if (Cur == Stream.end())
116  return nullptr;
117  return std::make_unique<NativeInjectedSource>((Cur++)->second, File, Strings);
118 }
119 
120 void NativeEnumInjectedSources::reset() { Cur = Stream.begin(); }
121 
122 }
123 }
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::pdb::NativeEnumInjectedSources::getNext
std::unique_ptr< IPDBInjectedSource > getNext() override
Definition: NativeEnumInjectedSources.cpp:114
RawTypes.h
llvm::pdb::NativeEnumInjectedSources::reset
void reset() override
Definition: NativeEnumInjectedSources.cpp:120
llvm::sys::path::begin
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
Definition: Path.cpp:226
NativeEnumInjectedSources.h
HashTable.h
llvm::MipsISD::Ret
@ Ret
Definition: MipsISelLowering.h:119
llvm::consumeError
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1043
llvm::Data
@ Data
Definition: SIMachineScheduler.h:55
llvm::pdb::NativeEnumInjectedSources::getChildAtIndex
std::unique_ptr< IPDBInjectedSource > getChildAtIndex(uint32_t Index) const override
Definition: NativeEnumInjectedSources.cpp:107
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::ms_demangle::QualifierMangleMode::Result
@ Result
llvm::pdb::NativeEnumInjectedSources::getChildCount
uint32_t getChildCount() const override
Definition: NativeEnumInjectedSources.cpp:102
getFileName
static Expected< StringRef > getFileName(const DebugStringTableSubsectionRef &Strings, const DebugChecksumsSubsectionRef &Checksums, uint32_t FileID)
Definition: CodeViewYAMLDebugSections.cpp:555
llvm::dwarf::toStringRef
StringRef toStringRef(const Optional< DWARFFormValue > &V, StringRef Default={})
Take an optional DWARFFormValue and try to extract a string value from it.
Definition: DWARFFormValue.h:193
llvm::pdb::InjectedSourceStream
Definition: InjectedSourceStream.h:22
llvm::pdb::NativeEnumInjectedSources::NativeEnumInjectedSources
NativeEnumInjectedSources(PDBFile &File, const InjectedSourceStream &IJS, const PDBStringTable &Strings)
Definition: NativeEnumInjectedSources.cpp:97
llvm::AMDGPU::Hwreg::Offset
Offset
Definition: SIDefines.h:409
uint64_t
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
MappedBlockStream.h
llvm::SPIRV::Decoration::Stream
@ Stream
llvm::pdb::PDBStringTable
Definition: PDBStringTable.h:26
llvm::min
Expected< ExpressionValue > min(const ExpressionValue &Lhs, const ExpressionValue &Rhs)
Definition: FileCheck.cpp:357
llvm::cantFail
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition: Error.h:745
uint32_t
PDBStringTable.h
llvm::pdb::PDBFile
Definition: PDBFile.h:40
PDBFile.h
N
#define N
llvm::ARMBuildAttrs::File
@ File
Definition: ARMBuildAttributes.h:36