1 //===- TpiHashing.cpp -----------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/DebugInfo/PDB/Native/TpiHashing.h"
11 #include "llvm/DebugInfo/CodeView/TypeDeserializer.h"
12 #include "llvm/DebugInfo/PDB/Native/Hash.h"
13 #include "llvm/Support/JamCRC.h"
16 using namespace llvm::codeview;
17 using namespace llvm::pdb;
19 // Corresponds to `fUDTAnon`.
20 static bool isAnonymous(StringRef Name) {
21 return Name == "<unnamed-tag>" || Name == "__unnamed" ||
22 Name.endswith("::<unnamed-tag>") || Name.endswith("::__unnamed");
25 // Computes the hash for a user-defined type record. This could be a struct,
26 // class, union, or enum.
27 static 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(Rec.getName());
35 if (!ForwardRef && !Scoped && !IsAnon)
36 return hashStringV1(Rec.getName());
37 if (!ForwardRef && HasUniqueName && !IsAnon)
38 return hashStringV1(Rec.getUniqueName());
39 return hashBufferV8(FullRecord);
43 static Expected<uint32_t> getHashForUdt(const CVType &Rec) {
45 if (auto E = TypeDeserializer::deserializeAs(const_cast<CVType &>(Rec),
48 return getHashForUdt(Deserialized, Rec.data());
52 static Expected<TagRecordHash> getTagRecordHashForUdt(const CVType &Rec) {
54 if (auto E = TypeDeserializer::deserializeAs(const_cast<CVType &>(Rec),
58 ClassOptions Opts = Deserialized.getOptions();
60 bool ForwardRef = bool(Opts & ClassOptions::ForwardReference);
62 uint32_t ThisRecordHash = getHashForUdt(Deserialized, Rec.data());
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.
67 return TagRecordHash{std::move(Deserialized), ThisRecordHash, 0};
69 bool Scoped = bool(Opts & ClassOptions::Scoped);
71 StringRef NameToHash =
72 Scoped ? Deserialized.getUniqueName() : Deserialized.getName();
73 uint32_t FullHash = hashStringV1(NameToHash);
74 return TagRecordHash{std::move(Deserialized), FullHash, ThisRecordHash};
78 static Expected<uint32_t> getSourceLineHash(const CVType &Rec) {
80 if (auto E = TypeDeserializer::deserializeAs(const_cast<CVType &>(Rec),
84 support::endian::write32le(Buf, Deserialized.getUDT().getIndex());
85 return hashStringV1(StringRef(Buf, 4));
88 Expected<TagRecordHash> llvm::pdb::hashTagRecord(const codeview::CVType &Type) {
89 switch (Type.kind()) {
93 return getTagRecordHashForUdt<ClassRecord>(Type);
95 return getTagRecordHashForUdt<UnionRecord>(Type);
97 return getTagRecordHashForUdt<EnumRecord>(Type);
99 assert(false && "Type is not a tag record!");
101 return make_error<StringError>("Invalid record type",
102 inconvertibleErrorCode());
105 Expected<uint32_t> llvm::pdb::hashTypeRecord(const CVType &Rec) {
106 switch (Rec.kind()) {
110 return getHashForUdt<ClassRecord>(Rec);
112 return getHashForUdt<UnionRecord>(Rec);
114 return getHashForUdt<EnumRecord>(Rec);
116 case LF_UDT_SRC_LINE:
117 return getSourceLineHash<UdtSourceLineRecord>(Rec);
118 case LF_UDT_MOD_SRC_LINE:
119 return getSourceLineHash<UdtModSourceLineRecord>(Rec);
125 // Run CRC32 over the bytes. This corresponds to `hashBufv8`.
126 JamCRC JC(/*Init=*/0U);
127 ArrayRef<char> Bytes(reinterpret_cast<const char *>(Rec.data().data()),