LLVM  6.0.0svn
CVRecord.h
Go to the documentation of this file.
1 //===- RecordIterator.h -----------------------------------------*- 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 #ifndef LLVM_DEBUGINFO_CODEVIEW_RECORDITERATOR_H
11 #define LLVM_DEBUGINFO_CODEVIEW_RECORDITERATOR_H
12 
13 #include "llvm/ADT/ArrayRef.h"
14 #include "llvm/ADT/Optional.h"
20 #include "llvm/Support/Endian.h"
21 #include "llvm/Support/Error.h"
22 #include <cstdint>
23 
24 namespace llvm {
25 
26 namespace codeview {
27 
28 template <typename Kind> class CVRecord {
29 public:
30  CVRecord() : Type(static_cast<Kind>(0)) {}
31 
32  CVRecord(Kind K, ArrayRef<uint8_t> Data) : Type(K), RecordData(Data) {}
33 
34  bool valid() const { return Type != static_cast<Kind>(0); }
35 
36  uint32_t length() const { return RecordData.size(); }
37  Kind kind() const { return Type; }
38  ArrayRef<uint8_t> data() const { return RecordData; }
39  StringRef str_data() const {
40  return StringRef(reinterpret_cast<const char *>(RecordData.data()),
41  RecordData.size());
42  }
43 
45  return RecordData.drop_front(sizeof(RecordPrefix));
46  }
47 
48  Optional<uint32_t> hash() const { return Hash; }
49 
50  void setHash(uint32_t Value) { Hash = Value; }
51 
55 };
56 
57 template <typename Kind> struct RemappedRecord {
58  explicit RemappedRecord(const CVRecord<Kind> &R) : OriginalRecord(R) {}
59 
62 };
63 
64 /// Read a complete record from a stream at a random offset.
65 template <typename Kind>
67  uint32_t Offset) {
68  const RecordPrefix *Prefix = nullptr;
69  BinaryStreamReader Reader(Stream);
70  Reader.setOffset(Offset);
71 
72  if (auto EC = Reader.readObject(Prefix))
73  return std::move(EC);
74  if (Prefix->RecordLen < 2)
75  return make_error<CodeViewError>(cv_error_code::corrupt_record);
76  Kind K = static_cast<Kind>(uint16_t(Prefix->RecordKind));
77 
78  Reader.setOffset(Offset);
79  ArrayRef<uint8_t> RawData;
80  if (auto EC = Reader.readBytes(RawData, Prefix->RecordLen + sizeof(uint16_t)))
81  return std::move(EC);
82  return codeview::CVRecord<Kind>(K, RawData);
83 }
84 
85 } // end namespace codeview
86 
87 template <typename Kind>
88 struct VarStreamArrayExtractor<codeview::CVRecord<Kind>> {
91  auto ExpectedRec = codeview::readCVRecordFromStream<Kind>(Stream, 0);
92  if (!ExpectedRec)
93  return ExpectedRec.takeError();
94  Item = *ExpectedRec;
95  Len = ExpectedRec->length();
96  return Error::success();
97  }
98 };
99 
100 } // end namespace llvm
101 
102 #endif // LLVM_DEBUGINFO_CODEVIEW_RECORDITERATOR_H
Kind kind() const
Definition: CVRecord.h:37
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
Error readObject(const T *&Dest)
Get a pointer to an object of type T from the underlying stream, as if by memcpy, and store the resul...
Optional< uint32_t > hash() const
Definition: CVRecord.h:48
VarStreamArrayExtractor is intended to be specialized to provide customized extraction logic...
SmallVector< std::pair< uint32_t, TypeIndex >, 8 > Mappings
Definition: CVRecord.h:61
CVRecord< Kind > OriginalRecord
Definition: CVRecord.h:60
ArrayRef< uint8_t > content() const
Definition: CVRecord.h:44
Tagged union holding either a T or a Error.
Definition: CachePruning.h:23
void setHash(uint32_t Value)
Definition: CVRecord.h:50
Error operator()(BinaryStreamRef Stream, uint32_t &Len, codeview::CVRecord< Kind > &Item)
Definition: CVRecord.h:89
uint32_t length() const
Definition: CVRecord.h:36
ArrayRef< uint8_t > data() const
Definition: CVRecord.h:38
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
StringRef str_data() const
Definition: CVRecord.h:39
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:149
Optional< uint32_t > Hash
Definition: CVRecord.h:54
Expected< CVRecord< Kind > > readCVRecordFromStream(BinaryStreamRef Stream, uint32_t Offset)
Read a complete record from a stream at a random offset.
Definition: CVRecord.h:66
const T * data() const
Definition: ArrayRef.h:146
void setOffset(uint32_t Off)
static ErrorSuccess success()
Create a success value.
Definition: Error.h:313
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:864
BinaryStreamRef is to BinaryStream what ArrayRef is to an Array.
CVRecord(Kind K, ArrayRef< uint8_t > Data)
Definition: CVRecord.h:32
ArrayRef< uint8_t > RecordData
Definition: CVRecord.h:53
Error readBytes(ArrayRef< uint8_t > &Buffer, uint32_t Size)
Read Size bytes from the underlying stream at the current offset and and set Buffer to the resulting ...
ArrayRef< T > drop_front(size_t N=1) const
Drop the first N elements of the array.
Definition: ArrayRef.h:188
const unsigned Kind
LLVM Value Representation.
Definition: Value.h:73
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
RemappedRecord(const CVRecord< Kind > &R)
Definition: CVRecord.h:58
Provides read only access to a subclass of BinaryStream.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
bool valid() const
Definition: CVRecord.h:34