1 //===- TpiHashing.cpp -----------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/DebugInfo/PDB/Native/TpiHashing.h"
12 #include "llvm/DebugInfo/CodeView/TypeDeserializer.h"
13 #include "llvm/DebugInfo/PDB/Native/Hash.h"
14 #include "llvm/Support/JamCRC.h"
17 using namespace llvm::codeview;
18 using namespace llvm::pdb;
20 // Corresponds to `fUDTAnon`.
21 static bool isAnonymous(StringRef Name) {
22 return Name == "<unnamed-tag>" || Name == "__unnamed" ||
23 Name.endswith("::<unnamed-tag>") || Name.endswith("::__unnamed");
26 // Computes the hash for a user-defined type record. This could be a struct,
27 // class, union, or enum.
28 static uint32_t getHashForUdt(const TagRecord &Rec,
29 ArrayRef<uint8_t> FullRecord) {
30 ClassOptions Opts = Rec.getOptions();
31 bool ForwardRef = bool(Opts & ClassOptions::ForwardReference);
32 bool Scoped = bool(Opts & ClassOptions::Scoped);
33 bool HasUniqueName = bool(Opts & ClassOptions::HasUniqueName);
34 bool IsAnon = HasUniqueName && isAnonymous(Rec.getName());
36 if (!ForwardRef && !Scoped && !IsAnon)
37 return hashStringV1(Rec.getName());
38 if (!ForwardRef && HasUniqueName && !IsAnon)
39 return hashStringV1(Rec.getUniqueName());
40 return hashBufferV8(FullRecord);
44 static Expected<uint32_t> getHashForUdt(const CVType &Rec) {
46 if (auto E = TypeDeserializer::deserializeAs(const_cast<CVType &>(Rec),
49 return getHashForUdt(Deserialized, Rec.data());
53 static Expected<uint32_t> getSourceLineHash(const CVType &Rec) {
55 if (auto E = TypeDeserializer::deserializeAs(const_cast<CVType &>(Rec),
59 support::endian::write32le(Buf, Deserialized.getUDT().getIndex());
60 return hashStringV1(StringRef(Buf, 4));
63 Expected<uint32_t> llvm::pdb::hashTypeRecord(const CVType &Rec) {
68 return getHashForUdt<ClassRecord>(Rec);
70 return getHashForUdt<UnionRecord>(Rec);
72 return getHashForUdt<EnumRecord>(Rec);
75 return getSourceLineHash<UdtSourceLineRecord>(Rec);
76 case LF_UDT_MOD_SRC_LINE:
77 return getSourceLineHash<UdtModSourceLineRecord>(Rec);
83 // Run CRC32 over the bytes. This corresponds to `hashBufv8`.
84 JamCRC JC(/*Init=*/0U);
85 ArrayRef<char> Bytes(reinterpret_cast<const char *>(Rec.data().data()),