LLVM  5.0.0
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 } // end namespace codeview
65 
66 template <typename Kind>
67 struct VarStreamArrayExtractor<codeview::CVRecord<Kind>> {
70  using namespace codeview;
71  const RecordPrefix *Prefix = nullptr;
72  BinaryStreamReader Reader(Stream);
73  uint32_t Offset = Reader.getOffset();
74 
75  if (auto EC = Reader.readObject(Prefix))
76  return EC;
77  if (Prefix->RecordLen < 2)
78  return make_error<CodeViewError>(cv_error_code::corrupt_record);
79  Kind K = static_cast<Kind>(uint16_t(Prefix->RecordKind));
80 
81  Reader.setOffset(Offset);
82  ArrayRef<uint8_t> RawData;
83  if (auto EC =
84  Reader.readBytes(RawData, Prefix->RecordLen + sizeof(uint16_t)))
85  return EC;
86  Item = codeview::CVRecord<Kind>(K, RawData);
87  Len = Item.length();
88  return Error::success();
89  }
90 };
91 
92 } // end namespace llvm
93 
94 #endif // LLVM_DEBUGINFO_CODEVIEW_RECORDITERATOR_H
ArrayRef< uint8_t > data() const
Definition: CVRecord.h:38
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...
VarStreamArrayExtractor is intended to be specialized to provide customized extraction logic...
bool valid() const
Definition: CVRecord.h:34
Optional< uint32_t > hash() const
Definition: CVRecord.h:48
StringRef str_data() const
Definition: CVRecord.h:39
SmallVector< std::pair< uint32_t, TypeIndex >, 8 > Mappings
Definition: CVRecord.h:61
CVRecord< Kind > OriginalRecord
Definition: CVRecord.h:60
void setHash(uint32_t Value)
Definition: CVRecord.h:50
Error operator()(BinaryStreamRef Stream, uint32_t &Len, codeview::CVRecord< Kind > &Item)
Definition: CVRecord.h:68
Kind kind() const
Definition: CVRecord.h:37
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:149
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
Optional< uint32_t > Hash
Definition: CVRecord.h:54
void setOffset(uint32_t Off)
static ErrorSuccess success()
Create a success value.
Definition: Error.h:311
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
ArrayRef< uint8_t > content() const
Definition: CVRecord.h:44
BinaryStreamRef is to BinaryStream what ArrayRef is to an Array.
uint32_t length() const
Definition: CVRecord.h:36
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 ...
const unsigned Kind
ArrayRef< T > drop_front(size_t N=1) const
Drop the first N elements of the array.
Definition: ArrayRef.h:188
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
const T * data() const
Definition: ArrayRef.h:146