Coverage Report

Created: 2018-11-16 02:38

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/include/llvm/DebugInfo/CodeView/CVRecord.h
Line
Count
Source (jump to first uncovered line)
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"
15
#include "llvm/DebugInfo/CodeView/CodeViewError.h"
16
#include "llvm/DebugInfo/CodeView/RecordSerialization.h"
17
#include "llvm/DebugInfo/CodeView/TypeIndex.h"
18
#include "llvm/Support/BinaryStreamReader.h"
19
#include "llvm/Support/BinaryStreamRef.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
100k
  CVRecord() : Type(static_cast<Kind>(0)) {}
31
32
29.4k
  CVRecord(Kind K, ArrayRef<uint8_t> Data) : Type(K), RecordData(Data) {}
llvm::codeview::CVRecord<llvm::codeview::TypeLeafKind>::CVRecord(llvm::codeview::TypeLeafKind, llvm::ArrayRef<unsigned char>)
Line
Count
Source
32
23.2k
  CVRecord(Kind K, ArrayRef<uint8_t> Data) : Type(K), RecordData(Data) {}
llvm::codeview::CVRecord<llvm::codeview::SymbolKind>::CVRecord(llvm::codeview::SymbolKind, llvm::ArrayRef<unsigned char>)
Line
Count
Source
32
6.13k
  CVRecord(Kind K, ArrayRef<uint8_t> Data) : Type(K), RecordData(Data) {}
33
34
  bool valid() const { return Type != static_cast<Kind>(0); }
35
36
19.2k
  uint32_t length() const { return RecordData.size(); }
37
43.8k
  Kind kind() const { return Type; }
llvm::codeview::CVRecord<llvm::codeview::SymbolKind>::kind() const
Line
Count
Source
37
13.6k
  Kind kind() const { return Type; }
llvm::codeview::CVRecord<llvm::codeview::TypeLeafKind>::kind() const
Line
Count
Source
37
30.2k
  Kind kind() const { return Type; }
38
  ArrayRef<uint8_t> data() const { return RecordData; }
39
1.73k
  StringRef str_data() const {
40
1.73k
    return StringRef(reinterpret_cast<const char *>(RecordData.data()),
41
1.73k
                     RecordData.size());
42
1.73k
  }
43
44
20.9k
  ArrayRef<uint8_t> content() const {
45
20.9k
    return RecordData.drop_front(sizeof(RecordPrefix));
46
20.9k
  }
llvm::codeview::CVRecord<llvm::codeview::TypeLeafKind>::content() const
Line
Count
Source
44
16.5k
  ArrayRef<uint8_t> content() const {
45
16.5k
    return RecordData.drop_front(sizeof(RecordPrefix));
46
16.5k
  }
llvm::codeview::CVRecord<llvm::codeview::SymbolKind>::content() const
Line
Count
Source
44
4.43k
  ArrayRef<uint8_t> content() const {
45
4.43k
    return RecordData.drop_front(sizeof(RecordPrefix));
46
4.43k
  }
47
48
  Optional<uint32_t> hash() const { return Hash; }
49
50
  void setHash(uint32_t Value) { Hash = Value; }
51
52
  Kind Type;
53
  ArrayRef<uint8_t> RecordData;
54
  Optional<uint32_t> Hash;
55
};
56
57
template <typename Kind> struct RemappedRecord {
58
  explicit RemappedRecord(const CVRecord<Kind> &R) : OriginalRecord(R) {}
59
60
  CVRecord<Kind> OriginalRecord;
61
  SmallVector<std::pair<uint32_t, TypeIndex>, 8> Mappings;
62
};
63
64
template <typename Record, typename Func>
65
Error forEachCodeViewRecord(ArrayRef<uint8_t> StreamBuffer, Func F) {
66
  while (!StreamBuffer.empty()) {
67
    if (StreamBuffer.size() < sizeof(RecordPrefix))
68
      return make_error<CodeViewError>(cv_error_code::corrupt_record);
69
70
    const RecordPrefix *Prefix =
71
        reinterpret_cast<const RecordPrefix *>(StreamBuffer.data());
72
73
    size_t RealLen = Prefix->RecordLen + 2;
74
    if (StreamBuffer.size() < RealLen)
75
      return make_error<CodeViewError>(cv_error_code::corrupt_record);
76
77
    ArrayRef<uint8_t> Data = StreamBuffer.take_front(RealLen);
78
    StreamBuffer = StreamBuffer.drop_front(RealLen);
79
80
    Record R(static_cast<decltype(Record::Type)>((uint16_t)Prefix->RecordKind),
81
             Data);
82
    if (auto EC = F(R))
83
      return EC;
84
  }
85
  return Error::success();
86
}
87
88
/// Read a complete record from a stream at a random offset.
89
template <typename Kind>
90
inline Expected<CVRecord<Kind>> readCVRecordFromStream(BinaryStreamRef Stream,
91
17.2k
                                                       uint32_t Offset) {
92
17.2k
  const RecordPrefix *Prefix = nullptr;
93
17.2k
  BinaryStreamReader Reader(Stream);
94
17.2k
  Reader.setOffset(Offset);
95
17.2k
96
17.2k
  if (auto EC = Reader.readObject(Prefix))
97
1
    return std::move(EC);
98
17.2k
  if (Prefix->RecordLen < 2)
99
0
    return make_error<CodeViewError>(cv_error_code::corrupt_record);
100
17.2k
  Kind K = static_cast<Kind>(uint16_t(Prefix->RecordKind));
101
17.2k
102
17.2k
  Reader.setOffset(Offset);
103
17.2k
  ArrayRef<uint8_t> RawData;
104
17.2k
  if (auto EC = Reader.readBytes(RawData, Prefix->RecordLen + sizeof(uint16_t)))
105
0
    return std::move(EC);
106
17.2k
  return codeview::CVRecord<Kind>(K, RawData);
107
17.2k
}
llvm::Expected<llvm::codeview::CVRecord<llvm::codeview::TypeLeafKind> > llvm::codeview::readCVRecordFromStream<llvm::codeview::TypeLeafKind>(llvm::BinaryStreamRef, unsigned int)
Line
Count
Source
91
14.4k
                                                       uint32_t Offset) {
92
14.4k
  const RecordPrefix *Prefix = nullptr;
93
14.4k
  BinaryStreamReader Reader(Stream);
94
14.4k
  Reader.setOffset(Offset);
95
14.4k
96
14.4k
  if (auto EC = Reader.readObject(Prefix))
97
0
    return std::move(EC);
98
14.4k
  if (Prefix->RecordLen < 2)
99
0
    return make_error<CodeViewError>(cv_error_code::corrupt_record);
100
14.4k
  Kind K = static_cast<Kind>(uint16_t(Prefix->RecordKind));
101
14.4k
102
14.4k
  Reader.setOffset(Offset);
103
14.4k
  ArrayRef<uint8_t> RawData;
104
14.4k
  if (auto EC = Reader.readBytes(RawData, Prefix->RecordLen + sizeof(uint16_t)))
105
0
    return std::move(EC);
106
14.4k
  return codeview::CVRecord<Kind>(K, RawData);
107
14.4k
}
llvm::Expected<llvm::codeview::CVRecord<llvm::codeview::SymbolKind> > llvm::codeview::readCVRecordFromStream<llvm::codeview::SymbolKind>(llvm::BinaryStreamRef, unsigned int)
Line
Count
Source
91
2.76k
                                                       uint32_t Offset) {
92
2.76k
  const RecordPrefix *Prefix = nullptr;
93
2.76k
  BinaryStreamReader Reader(Stream);
94
2.76k
  Reader.setOffset(Offset);
95
2.76k
96
2.76k
  if (auto EC = Reader.readObject(Prefix))
97
1
    return std::move(EC);
98
2.76k
  if (Prefix->RecordLen < 2)
99
0
    return make_error<CodeViewError>(cv_error_code::corrupt_record);
100
2.76k
  Kind K = static_cast<Kind>(uint16_t(Prefix->RecordKind));
101
2.76k
102
2.76k
  Reader.setOffset(Offset);
103
2.76k
  ArrayRef<uint8_t> RawData;
104
2.76k
  if (auto EC = Reader.readBytes(RawData, Prefix->RecordLen + sizeof(uint16_t)))
105
0
    return std::move(EC);
106
2.76k
  return codeview::CVRecord<Kind>(K, RawData);
107
2.76k
}
108
109
} // end namespace codeview
110
111
template <typename Kind>
112
struct VarStreamArrayExtractor<codeview::CVRecord<Kind>> {
113
  Error operator()(BinaryStreamRef Stream, uint32_t &Len,
114
14.4k
                   codeview::CVRecord<Kind> &Item) {
115
14.4k
    auto ExpectedRec = codeview::readCVRecordFromStream<Kind>(Stream, 0);
116
14.4k
    if (!ExpectedRec)
117
0
      return ExpectedRec.takeError();
118
14.4k
    Item = *ExpectedRec;
119
14.4k
    Len = ExpectedRec->length();
120
14.4k
    return Error::success();
121
14.4k
  }
122
};
123
124
} // end namespace llvm
125
126
#endif // LLVM_DEBUGINFO_CODEVIEW_RECORDITERATOR_H