LLVM  4.0.0
NameMapBuilder.cpp
Go to the documentation of this file.
1 //===- NameMapBuilder.cpp - PDB Name Map Builder ----------------*- C++ -*-===//
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 
10 #include "llvm/ADT/STLExtras.h"
14 #include "llvm/Support/Endian.h"
15 #include "llvm/Support/Error.h"
16 #include <algorithm>
17 #include <cstdint>
18 
19 using namespace llvm;
20 using namespace llvm::pdb;
21 
23 
25  StringDataBytes += Name.size() + 1;
26  Map.insert({Name, Mapping});
27 }
28 
30  auto Result = llvm::make_unique<NameMap>();
31  Result->Mapping = Map;
32  return std::move(Result);
33 }
34 
36  uint32_t TotalLength = 0;
37 
38  TotalLength += sizeof(support::ulittle32_t); // StringDataBytes value
39  TotalLength += StringDataBytes; // actual string data
40 
41  TotalLength += sizeof(support::ulittle32_t); // Hash Size
42  TotalLength += sizeof(support::ulittle32_t); // Max Number of Strings
43  TotalLength += sizeof(support::ulittle32_t); // Num Present Words
44  // One bitmask word for each present entry
45  TotalLength += Map.size() * sizeof(support::ulittle32_t);
46  TotalLength += sizeof(support::ulittle32_t); // Num Deleted Words
47 
48  // For each present word, which we are treating as equivalent to the number of
49  // entries in the table, we have a pair of integers. An offset into the
50  // string data, and a corresponding stream number.
51  TotalLength += Map.size() * 2 * sizeof(support::ulittle32_t);
52 
53  return TotalLength;
54 }
55 
57  // The first field is the number of bytes of string data. So add
58  // up the length of all strings plus a null terminator for each
59  // one.
60  uint32_t NumBytes = 0;
61  for (auto B = Map.begin(), E = Map.end(); B != E; ++B) {
62  NumBytes += B->getKeyLength() + 1;
63  }
64 
65  if (auto EC = Writer.writeInteger(NumBytes)) // Number of bytes of string data
66  return EC;
67  // Now all of the string data itself.
68  for (auto B = Map.begin(), E = Map.end(); B != E; ++B) {
69  if (auto EC = Writer.writeZeroString(B->getKey()))
70  return EC;
71  }
72 
73  if (auto EC = Writer.writeInteger(Map.size())) // Hash Size
74  return EC;
75 
76  if (auto EC = Writer.writeInteger(Map.size())) // Max Number of Strings
77  return EC;
78 
79  if (auto EC = Writer.writeInteger(Map.size())) // Num Present Words
80  return EC;
81 
82  // For each entry in the mapping, write a bit mask which represents a bucket
83  // to store it in. We don't use this, so the value we write isn't important
84  // to us, it just has to be there.
85  for (auto B = Map.begin(), E = Map.end(); B != E; ++B) {
86  if (auto EC = Writer.writeInteger(1U))
87  return EC;
88  }
89 
90  if (auto EC = Writer.writeInteger(0U)) // Num Deleted Words
91  return EC;
92 
93  // Mappings of each word.
94  uint32_t OffsetSoFar = 0;
95  for (auto B = Map.begin(), E = Map.end(); B != E; ++B) {
96  // This is a list of key value pairs where the key is the offset into the
97  // strings buffer, and the value is a stream number. Write each pair.
98  if (auto EC = Writer.writeInteger(OffsetSoFar))
99  return EC;
100 
101  if (auto EC = Writer.writeInteger(B->second))
102  return EC;
103 
104  OffsetSoFar += B->getKeyLength() + 1;
105  }
106 
107  return Error::success();
108 }
Error writeZeroString(StringRef Str)
Tagged union holding either a T or a Error.
static GCRegistry::Add< OcamlGC > B("ocaml","ocaml 3.10-compatible GC")
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
Definition: StringRef.h:135
static GCRegistry::Add< CoreCLRGC > E("coreclr","CoreCLR-compatible GC")
unsigned size() const
Definition: StringMap.h:114
Error writeInteger(uint8_t Int)
static ErrorSuccess success()
Create a success value.
Expected< std::unique_ptr< NameMap > > build()
bool insert(MapEntryTy *KeyValue)
insert - Insert the specified key/value pair into the map.
Definition: StringMap.h:348
iterator begin()
Definition: StringMap.h:302
detail::packed_endian_specific_integral< uint32_t, little, unaligned > ulittle32_t
Definition: Endian.h:235
Error commit(msf::StreamWriter &Writer) const
Lightweight error class with error context and mandatory checking.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47
void addMapping(StringRef Name, uint32_t Mapping)
uint32_t calculateSerializedLength() const
iterator end()
Definition: StringMap.h:305