LLVM  8.0.1
DebugCrossImpSubsection.cpp
Go to the documentation of this file.
1 //===- DebugCrossImpSubsection.cpp ----------------------------------------===//
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 
11 #include "llvm/ADT/ArrayRef.h"
16 #include "llvm/Support/Endian.h"
17 #include "llvm/Support/Error.h"
18 #include <algorithm>
19 #include <cstdint>
20 #include <utility>
21 #include <vector>
22 
23 using namespace llvm;
24 using namespace llvm::codeview;
25 
29  BinaryStreamReader Reader(Stream);
30  if (Reader.bytesRemaining() < sizeof(CrossModuleImport))
31  return make_error<CodeViewError>(
33  "Not enough bytes for a Cross Module Import Header!");
34  if (auto EC = Reader.readObject(Item.Header))
35  return EC;
36  if (Reader.bytesRemaining() < Item.Header->Count * sizeof(uint32_t))
37  return make_error<CodeViewError>(
39  "Not enough to read specified number of Cross Module References!");
40  if (auto EC = Reader.readArray(Item.Imports, Item.Header->Count))
41  return EC;
42  return Error::success();
43 }
44 
46  BinaryStreamReader Reader) {
47  return Reader.readArray(References, Reader.bytesRemaining());
48 }
49 
51  BinaryStreamReader Reader(Stream);
52  return initialize(Reader);
53 }
54 
56  uint32_t ImportId) {
57  Strings.insert(Module);
58  std::vector<support::ulittle32_t> Targets = {support::ulittle32_t(ImportId)};
59  auto Result = Mappings.insert(std::make_pair(Module, Targets));
60  if (!Result.second)
61  Result.first->getValue().push_back(Targets[0]);
62 }
63 
65  uint32_t Size = 0;
66  for (const auto &Item : Mappings) {
67  Size += sizeof(CrossModuleImport);
68  Size += sizeof(support::ulittle32_t) * Item.second.size();
69  }
70  return Size;
71 }
72 
74  BinaryStreamWriter &Writer) const {
75  using T = decltype(&*Mappings.begin());
76  std::vector<T> Ids;
77  Ids.reserve(Mappings.size());
78 
79  for (const auto &M : Mappings)
80  Ids.push_back(&M);
81 
82  llvm::sort(Ids, [this](const T &L1, const T &L2) {
83  return Strings.getIdForString(L1->getKey()) <
84  Strings.getIdForString(L2->getKey());
85  });
86 
87  for (const auto &Item : Ids) {
89  Imp.ModuleNameOffset = Strings.getIdForString(Item->getKey());
90  Imp.Count = Item->getValue().size();
91  if (auto EC = Writer.writeObject(Imp))
92  return EC;
93  if (auto EC = Writer.writeArray(makeArrayRef(Item->getValue())))
94  return EC;
95  }
96  return Error::success();
97 }
Error writeObject(const T &Obj)
Writes the object Obj to the underlying stream, as if by using memcpy.
This class represents lattice values for constants.
Definition: AllocatorList.h:24
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
support::ulittle32_t Count
Definition: CodeView.h:583
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:451
Error writeArray(ArrayRef< T > Array)
Writes an array of objects of type T to the underlying stream, as if by using memcpy.
detail::packed_endian_specific_integral< uint32_t, little, unaligned > ulittle32_t
Definition: Endian.h:271
Provides write only access to a subclass of WritableBinaryStream.
support::ulittle32_t ModuleNameOffset
Definition: CodeView.h:582
void addImport(StringRef Module, uint32_t ImportId)
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1116
static ErrorSuccess success()
Create a success value.
Definition: Error.h:327
Error commit(BinaryStreamWriter &Writer) const override
FixedStreamArray< support::ulittle32_t > Imports
BinaryStreamRef is to BinaryStream what ArrayRef is to an Array.
uint32_t Size
Definition: Profile.cpp:47
uint32_t bytesRemaining() const
Error operator()(BinaryStreamRef Stream, uint32_t &Len, T &Item) const =delete
Lightweight error class with error context and mandatory checking.
Definition: Error.h:158
Provides read only access to a subclass of BinaryStream.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
Error readArray(ArrayRef< T > &Array, uint32_t NumElements)
Get a reference to a NumElements element array of objects of type T from the underlying stream as if ...