Coverage Report

Created: 2018-07-18 22:01

/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
72.9k
  CVRecord() : Type(static_cast<Kind>(0)) {}
31
32
16.1k
  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
12.7k
  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
3.34k
  CVRecord(Kind K, ArrayRef<uint8_t> Data) : Type(K), RecordData(Data) {}
33
34
  bool valid() const { return Type != static_cast<Kind>(0); }
35
36
10.7k
  uint32_t length() const { return RecordData.size(); }
37
16.8k
  Kind kind() const { return Type; }
llvm::codeview::CVRecord<llvm::codeview::SymbolKind>::kind() const
Line
Count
Source
37
6.67k
  Kind kind() const { return Type; }
llvm::codeview::CVRecord<llvm::codeview::TypeLeafKind>::kind() const
Line
Count
Source
37
10.1k
  Kind kind() const { return Type; }
38
  ArrayRef<uint8_t> data() const { return RecordData; }
39
1.16k
  StringRef str_data() const {
40
1.16k
    return StringRef(reinterpret_cast<const char *>(RecordData.data()),
41
1.16k
                     RecordData.size());
42
1.16k
  }
43
44
13.2k
  ArrayRef<uint8_t> content() const {
45
13.2k
    return RecordData.drop_front(sizeof(RecordPrefix));
46
13.2k
  }
llvm::codeview::CVRecord<llvm::codeview::TypeLeafKind>::content() const
Line
Count
Source
44
10.0k
  ArrayRef<uint8_t> content() const {
45
10.0k
    return RecordData.drop_front(sizeof(RecordPrefix));
46
10.0k
  }
llvm::codeview::CVRecord<llvm::codeview::SymbolKind>::content() const
Line
Count
Source
44
3.25k
  ArrayRef<uint8_t> content() const {
45
3.25k
    return RecordData.drop_front(sizeof(RecordPrefix));
46
3.25k
  }
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
10.3k
                                                       uint32_t Offset) {
92
10.3k
  const RecordPrefix *Prefix = nullptr;
93
10.3k
  BinaryStreamReader Reader(Stream);
94
10.3k
  Reader.setOffset(Offset);
95
10.3k
96
10.3k
  if (auto EC = Reader.readObject(Prefix))
97
1
    return std::move(EC);
98
10.3k
  if (Prefix->RecordLen < 2)
99
0
    return make_error<CodeViewError>(cv_error_code::corrupt_record);
100
10.3k
  Kind K = static_cast<Kind>(uint16_t(Prefix->RecordKind));
101
10.3k
102
10.3k
  Reader.setOffset(Offset);
103
10.3k
  ArrayRef<uint8_t> RawData;
104
10.3k
  if (auto EC = Reader.readBytes(RawData, Prefix->RecordLen + sizeof(uint16_t)))
105
0
    return std::move(EC);
106
10.3k
  return codeview::CVRecord<Kind>(K, RawData);
107
10.3k
}
llvm::Expected<llvm::codeview::CVRecord<llvm::codeview::TypeLeafKind> > llvm::codeview::readCVRecordFromStream<llvm::codeview::TypeLeafKind>(llvm::BinaryStreamRef, unsigned int)
Line
Count
Source
91
8.05k
                                                       uint32_t Offset) {
92
8.05k
  const RecordPrefix *Prefix = nullptr;
93
8.05k
  BinaryStreamReader Reader(Stream);
94
8.05k
  Reader.setOffset(Offset);
95
8.05k
96
8.05k
  if (auto EC = Reader.readObject(Prefix))
97
0
    return std::move(EC);
98
8.05k
  if (Prefix->RecordLen < 2)
99
0
    return make_error<CodeViewError>(cv_error_code::corrupt_record);
100
8.05k
  Kind K = static_cast<Kind>(uint16_t(Prefix->RecordKind));
101
8.05k
102
8.05k
  Reader.setOffset(Offset);
103
8.05k
  ArrayRef<uint8_t> RawData;
104
8.05k
  if (auto EC = Reader.readBytes(RawData, Prefix->RecordLen + sizeof(uint16_t)))
105
0
    return std::move(EC);
106
8.05k
  return codeview::CVRecord<Kind>(K, RawData);
107
8.05k
}
llvm::Expected<llvm::codeview::CVRecord<llvm::codeview::SymbolKind> > llvm::codeview::readCVRecordFromStream<llvm::codeview::SymbolKind>(llvm::BinaryStreamRef, unsigned int)
Line
Count
Source
91
2.28k
                                                       uint32_t Offset) {
92
2.28k
  const RecordPrefix *Prefix = nullptr;
93
2.28k
  BinaryStreamReader Reader(Stream);
94
2.28k
  Reader.setOffset(Offset);
95
2.28k
96
2.28k
  if (auto EC = Reader.readObject(Prefix))
97
1
    return std::move(EC);
98
2.28k
  if (Prefix->RecordLen < 2)
99
0
    return make_error<CodeViewError>(cv_error_code::corrupt_record);
100
2.28k
  Kind K = static_cast<Kind>(uint16_t(Prefix->RecordKind));
101
2.28k
102
2.28k
  Reader.setOffset(Offset);
103
2.28k
  ArrayRef<uint8_t> RawData;
104
2.28k
  if (auto EC = Reader.readBytes(RawData, Prefix->RecordLen + sizeof(uint16_t)))
105
0
    return std::move(EC);
106
2.28k
  return codeview::CVRecord<Kind>(K, RawData);
107
2.28k
}
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
8.05k
                   codeview::CVRecord<Kind> &Item) {
115
8.05k
    auto ExpectedRec = codeview::readCVRecordFromStream<Kind>(Stream, 0);
116
8.05k
    if (!ExpectedRec)
117
0
      return ExpectedRec.takeError();
118
8.05k
    Item = *ExpectedRec;
119
8.05k
    Len = ExpectedRec->length();
120
8.05k
    return Error::success();
121
8.05k
  }
122
};
123
124
} // end namespace llvm
125
126
#endif // LLVM_DEBUGINFO_CODEVIEW_RECORDITERATOR_H