LLVM  8.0.0svn
DebugStringTableSubsection.cpp
Go to the documentation of this file.
1 //===- DebugStringTableSubsection.cpp - CodeView String Table -------------===//
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/StringRef.h"
15 #include "llvm/Support/Error.h"
16 #include <algorithm>
17 #include <cassert>
18 #include <cstdint>
19 
20 using namespace llvm;
21 using namespace llvm::codeview;
22 
25 
27  Stream = Contents;
28  return Error::success();
29 }
30 
32  return Reader.readStreamRef(Stream);
33 }
34 
37  BinaryStreamReader Reader(Stream);
38  Reader.setOffset(Offset);
40  if (auto EC = Reader.readCString(Result))
41  return std::move(EC);
42  return Result;
43 }
44 
47 
49  auto P = StringToId.insert({S, StringSize});
50 
51  // If a given string didn't exist in the string table, we want to increment
52  // the string table size and insert it into the reverse lookup.
53  if (P.second) {
54  IdToString.insert({P.first->getValue(), P.first->getKey()});
55  StringSize += S.size() + 1; // +1 for '\0'
56  }
57 
58  return P.first->second;
59 }
60 
62  return StringSize;
63 }
64 
66  uint32_t Begin = Writer.getOffset();
67  uint32_t End = Begin + StringSize;
68 
69  // Write a null string at the beginning.
70  if (auto EC = Writer.writeCString(StringRef()))
71  return EC;
72 
73  for (auto &Pair : StringToId) {
74  StringRef S = Pair.getKey();
75  uint32_t Offset = Begin + Pair.getValue();
76  Writer.setOffset(Offset);
77  if (auto EC = Writer.writeCString(S))
78  return EC;
79  assert(Writer.getOffset() <= End);
80  }
81 
82  Writer.setOffset(End);
83  assert((End - Begin) == StringSize);
84  return Error::success();
85 }
86 
87 uint32_t DebugStringTableSubsection::size() const { return StringToId.size(); }
88 
89 std::vector<uint32_t> DebugStringTableSubsection::sortedIds() const {
90  std::vector<uint32_t> Result;
91  Result.reserve(IdToString.size());
92  for (const auto &Entry : IdToString)
93  Result.push_back(Entry.first);
94  llvm::sort(Result);
95  return Result;
96 }
97 
99  auto Iter = StringToId.find(S);
100  assert(Iter != StringToId.end());
101  return Iter->second;
102 }
103 
105  auto Iter = IdToString.find(Id);
106  assert(Iter != IdToString.end());
107  return Iter->second;
108 }
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
Definition: StringRef.h:138
iterator find(StringRef Key)
Definition: StringMap.h:333
Error commit(BinaryStreamWriter &Writer) const override
Expected< StringRef > getString(uint32_t Offset) const
unsigned size() const
Definition: StringMap.h:112
Tagged union holding either a T or a Error.
Definition: CachePruning.h:23
Error readCString(StringRef &Dest)
Read a null terminated string from Dest.
#define P(N)
Provides write only access to a subclass of WritableBinaryStream.
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:972
Error writeCString(StringRef Str)
Write the string Str to the underlying stream followed by a null terminator.
void setOffset(uint32_t Off)
static ErrorSuccess success()
Create a success value.
Definition: Error.h:327
BinaryStreamRef is to BinaryStream what ArrayRef is to an Array.
bool insert(MapEntryTy *KeyValue)
insert - Insert the specified key/value pair into the map.
Definition: StringMap.h:366
void setOffset(uint32_t Off)
Error readStreamRef(BinaryStreamRef &Ref)
Read the entire remainder of the underlying stream into Ref.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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
iterator end()
Definition: StringMap.h:318