Coverage Report

Created: 2022-01-22 13:19

/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/include/lldb/Utility/UUID.h
Line
Count
Source (jump to first uncovered line)
1
//===-- UUID.h --------------------------------------------------*- C++ -*-===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#ifndef LLDB_UTILITY_UUID_H
10
#define LLDB_UTILITY_UUID_H
11
12
#include "llvm/ADT/ArrayRef.h"
13
#include "llvm/ADT/StringRef.h"
14
#include "llvm/Support/Endian.h"
15
#include <cstddef>
16
#include <cstdint>
17
#include <string>
18
19
namespace lldb_private {
20
21
  class Stream;
22
23
class UUID {
24
public:
25
3.39M
  UUID() = default;
26
27
  // Reference:
28
  // https://crashpad.chromium.org/doxygen/structcrashpad_1_1CodeViewRecordPDB70.html
29
  struct CvRecordPdb70 {
30
    struct {
31
      llvm::support::ulittle32_t Data1;
32
      llvm::support::ulittle16_t Data2;
33
      llvm::support::ulittle16_t Data3;
34
      uint8_t Data4[8];
35
    } Uuid;
36
    llvm::support::ulittle32_t Age;
37
    // char PDBFileName[];
38
  };
39
40
  /// Create a UUID from CvRecordPdb70.
41
  static UUID fromCvRecord(CvRecordPdb70 debug_info);
42
43
  /// Creates a UUID from the data pointed to by the bytes argument. No special
44
  /// significance is attached to any of the values.
45
1.47M
  static UUID fromData(const void *bytes, uint32_t num_bytes) {
46
1.47M
    if (bytes)
47
1.47M
      return fromData({reinterpret_cast<const uint8_t *>(bytes), num_bytes});
48
0
    return UUID();
49
1.47M
  }
50
51
  /// Creates a uuid from the data pointed to by the bytes argument. No special
52
  /// significance is attached to any of the values.
53
1.61M
  static UUID fromData(llvm::ArrayRef<uint8_t> bytes) { return UUID(bytes); }
54
55
  /// Creates a UUID from the data pointed to by the bytes argument. Data
56
  /// consisting purely of zero bytes is treated as an invalid UUID.
57
1.19M
  static UUID fromOptionalData(const void *bytes, uint32_t num_bytes) {
58
1.19M
    if (bytes)
59
1.19M
      return fromOptionalData(
60
1.19M
          {reinterpret_cast<const uint8_t *>(bytes), num_bytes});
61
0
    return UUID();
62
1.19M
  }
63
64
  /// Creates a UUID from the data pointed to by the bytes argument. Data
65
  /// consisting purely of zero bytes is treated as an invalid UUID.
66
1.19M
  static UUID fromOptionalData(llvm::ArrayRef<uint8_t> bytes) {
67
1.19M
    if (
llvm::all_of(bytes, [](uint8_t b) 1.19M
{ return b == 0; }))
68
6
      return UUID();
69
1.19M
    return UUID(bytes);
70
1.19M
  }
71
72
136k
  void Clear() { m_bytes.clear(); }
73
74
  void Dump(Stream *s) const;
75
76
137k
  llvm::ArrayRef<uint8_t> GetBytes() const { return m_bytes; }
77
78
534k
  explicit operator bool() const { return IsValid(); }
79
35.7M
  bool IsValid() const { return !m_bytes.empty(); }
80
81
  std::string GetAsString(llvm::StringRef separator = "-") const;
82
83
  bool SetFromStringRef(llvm::StringRef str);
84
85
  // Same as SetFromStringRef, but if the resultant UUID is all 0 bytes, set the
86
  // UUID to invalid.
87
  bool SetFromOptionalStringRef(llvm::StringRef str);
88
89
  /// Decode as many UUID bytes as possible from the C string \a cstr.
90
  ///
91
  /// \param[in] str
92
  ///     An llvm::StringRef that points at a UUID string value (no leading
93
  ///     spaces). The string must contain only hex characters and optionally
94
  ///     can contain the '-' sepearators.
95
  ///
96
  /// \param[in] uuid_bytes
97
  ///     A buffer of bytes that will contain a full or partially decoded UUID.
98
  ///
99
  /// \return
100
  ///     The original string, with all decoded bytes removed.
101
  static llvm::StringRef
102
  DecodeUUIDBytesFromString(llvm::StringRef str,
103
                            llvm::SmallVectorImpl<uint8_t> &uuid_bytes);
104
105
private:
106
2.80M
  UUID(llvm::ArrayRef<uint8_t> bytes) : m_bytes(bytes.begin(), bytes.end()) {}
107
108
  // GNU ld generates 20-byte build-ids. Size chosen to avoid heap allocations
109
  // for this case.
110
  llvm::SmallVector<uint8_t, 20> m_bytes;
111
112
23.4M
  friend bool operator==(const UUID &LHS, const UUID &RHS) {
113
23.4M
    return LHS.m_bytes == RHS.m_bytes;
114
23.4M
  }
115
255k
  friend bool operator!=(const UUID &LHS, const UUID &RHS) {
116
255k
    return !(LHS == RHS);
117
255k
  }
118
4
  friend bool operator<(const UUID &LHS, const UUID &RHS) {
119
4
    return LHS.m_bytes < RHS.m_bytes;
120
4
  }
121
0
  friend bool operator<=(const UUID &LHS, const UUID &RHS) {
122
0
    return !(RHS < LHS);
123
0
  }
124
  friend bool operator>(const UUID &LHS, const UUID &RHS) { return RHS < LHS; }
125
0
  friend bool operator>=(const UUID &LHS, const UUID &RHS) {
126
0
    return !(LHS < RHS);
127
0
  }
128
};
129
} // namespace lldb_private
130
131
#endif // LLDB_UTILITY_UUID_H