1//===- TpiHashing.cpp -----------------------------------------------------===//
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#include "llvm/DebugInfo/PDB/Native/TpiHashing.h"
10
11#include "llvm/DebugInfo/CodeView/TypeDeserializer.h"
12#include "llvm/DebugInfo/PDB/Native/Hash.h"
13#include "llvm/Support/CRC.h"
14
15using namespace llvm;
16using namespace llvm::codeview;
17using namespace llvm::pdb;
18
19// Corresponds to `fUDTAnon`.
20static bool isAnonymous(StringRef Name) {
21 return Name == "<unnamed-tag>" || Name == "__unnamed" ||
22 Name.ends_with(Suffix: "::<unnamed-tag>") || Name.ends_with(Suffix: "::__unnamed");
23}
24
25// Computes the hash for a user-defined type record. This could be a struct,
26// class, union, or enum.
27static uint32_t getHashForUdt(const TagRecord &Rec,
28 ArrayRef<uint8_t> FullRecord) {
29 ClassOptions Opts = Rec.getOptions();
30 bool ForwardRef = bool(Opts & ClassOptions::ForwardReference);
31 bool Scoped = bool(Opts & ClassOptions::Scoped);
32 bool HasUniqueName = bool(Opts & ClassOptions::HasUniqueName);
33 bool IsAnon = HasUniqueName && isAnonymous(Name: Rec.getName());
34
35 if (!ForwardRef && !Scoped && !IsAnon)
36 return hashStringV1(Str: Rec.getName());
37 if (!ForwardRef && HasUniqueName && !IsAnon)
38 return hashStringV1(Str: Rec.getUniqueName());
39 return hashBufferV8(Data: FullRecord);
40}
41
42template <typename T>
43static Expected<uint32_t> getHashForUdt(const CVType &Rec) {
44 T Deserialized;
45 if (auto E = TypeDeserializer::deserializeAs(const_cast<CVType &>(Rec),
46 Deserialized))
47 return std::move(E);
48 return getHashForUdt(Deserialized, Rec.data());
49}
50
51template <typename T>
52static Expected<TagRecordHash> getTagRecordHashForUdt(const CVType &Rec) {
53 T Deserialized;
54 if (auto E = TypeDeserializer::deserializeAs(const_cast<CVType &>(Rec),
55 Deserialized))
56 return std::move(E);
57
58 ClassOptions Opts = Deserialized.getOptions();
59
60 bool ForwardRef = bool(Opts & ClassOptions::ForwardReference);
61
62 uint32_t ThisRecordHash = getHashForUdt(Deserialized, Rec.data());
63
64 // If we don't have a forward ref we can't compute the hash of it from the
65 // full record because it requires hashing the entire buffer.
66 if (!ForwardRef)
67 return TagRecordHash{std::move(Deserialized), ThisRecordHash, 0};
68
69 bool Scoped = bool(Opts & ClassOptions::Scoped);
70
71 StringRef NameToHash =
72 Scoped ? Deserialized.getUniqueName() : Deserialized.getName();
73 uint32_t FullHash = hashStringV1(Str: NameToHash);
74 return TagRecordHash{std::move(Deserialized), FullHash, ThisRecordHash};
75}
76
77template <typename T>
78static Expected<uint32_t> getSourceLineHash(const CVType &Rec) {
79 T Deserialized;
80 if (auto E = TypeDeserializer::deserializeAs(const_cast<CVType &>(Rec),
81 Deserialized))
82 return std::move(E);
83 char Buf[4];
84 support::endian::write32le(P: Buf, V: Deserialized.getUDT().getIndex());
85 return hashStringV1(Str: StringRef(Buf, 4));
86}
87
88Expected<TagRecordHash> llvm::pdb::hashTagRecord(const codeview::CVType &Type) {
89 switch (Type.kind()) {
90 case LF_CLASS:
91 case LF_CLASS2:
92 case LF_STRUCTURE:
93 case LF_STRUCTURE2:
94 case LF_INTERFACE:
95 case LF_INTERFACE2:
96 return getTagRecordHashForUdt<ClassRecord>(Rec: Type);
97 case LF_UNION:
98 case LF_UNION2:
99 return getTagRecordHashForUdt<UnionRecord>(Rec: Type);
100 case LF_ENUM:
101 return getTagRecordHashForUdt<EnumRecord>(Rec: Type);
102 default:
103 assert(false && "Type is not a tag record!");
104 }
105 return make_error<StringError>(Args: "Invalid record type",
106 Args: inconvertibleErrorCode());
107}
108
109Expected<uint32_t> llvm::pdb::hashTypeRecord(const CVType &Rec) {
110 switch (Rec.kind()) {
111 case LF_CLASS:
112 case LF_CLASS2:
113 case LF_STRUCTURE:
114 case LF_STRUCTURE2:
115 case LF_INTERFACE:
116 case LF_INTERFACE2:
117 return getHashForUdt<ClassRecord>(Rec);
118 case LF_UNION:
119 case LF_UNION2:
120 return getHashForUdt<UnionRecord>(Rec);
121 case LF_ENUM:
122 return getHashForUdt<EnumRecord>(Rec);
123
124 case LF_UDT_SRC_LINE:
125 return getSourceLineHash<UdtSourceLineRecord>(Rec);
126 case LF_UDT_MOD_SRC_LINE:
127 return getSourceLineHash<UdtModSourceLineRecord>(Rec);
128
129 default:
130 break;
131 }
132
133 // Run CRC32 over the bytes. This corresponds to `hashBufv8`.
134 JamCRC JC(/*Init=*/0U);
135 JC.update(Data: Rec.data());
136 return JC.getCRC();
137}
138