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/DebugInfo/PDB/Native/RawError.h"
17 using namespace llvm::codeview;
18 using namespace llvm::pdb;
20 // Corresponds to `fUDTAnon`.
21 template <typename T> static bool isAnonymous(T &Rec) {
22 StringRef Name = Rec.getName();
23 return Name == "<unnamed-tag>" || Name == "__unnamed" ||
24 Name.endswith("::<unnamed-tag>") || Name.endswith("::__unnamed");
27 // Computes a hash for a given TPI record.
29 static uint32_t getTpiHash(T &Rec, ArrayRef<uint8_t> FullRecord) {
30 auto Opts = static_cast<uint16_t>(Rec.getOptions());
33 Opts & static_cast<uint16_t>(ClassOptions::ForwardReference);
34 bool Scoped = Opts & static_cast<uint16_t>(ClassOptions::Scoped);
35 bool UniqueName = Opts & static_cast<uint16_t>(ClassOptions::HasUniqueName);
36 bool IsAnon = UniqueName && isAnonymous(Rec);
38 if (!ForwardRef && !Scoped && !IsAnon)
39 return hashStringV1(Rec.getName());
40 if (!ForwardRef && UniqueName && !IsAnon)
41 return hashStringV1(Rec.getUniqueName());
42 return hashBufferV8(FullRecord);
45 template <typename T> static uint32_t getSourceLineHash(T &Rec) {
47 support::endian::write32le(Buf, Rec.getUDT().getIndex());
48 return hashStringV1(StringRef(Buf, 4));
51 void TpiHashUpdater::visitKnownRecordImpl(CVType &CVR,
52 UdtSourceLineRecord &Rec) {
53 CVR.Hash = getSourceLineHash(Rec);
56 void TpiHashUpdater::visitKnownRecordImpl(CVType &CVR,
57 UdtModSourceLineRecord &Rec) {
58 CVR.Hash = getSourceLineHash(Rec);
61 void TpiHashUpdater::visitKnownRecordImpl(CVType &CVR, ClassRecord &Rec) {
62 CVR.Hash = getTpiHash(Rec, CVR.data());
65 void TpiHashUpdater::visitKnownRecordImpl(CVType &CVR, EnumRecord &Rec) {
66 CVR.Hash = getTpiHash(Rec, CVR.data());
69 void TpiHashUpdater::visitKnownRecordImpl(CVType &CVR, UnionRecord &Rec) {
70 CVR.Hash = getTpiHash(Rec, CVR.data());
73 Error TpiHashVerifier::visitKnownRecord(CVType &CVR, UdtSourceLineRecord &Rec) {
74 return verifySourceLine(Rec.getUDT());
77 Error TpiHashVerifier::visitKnownRecord(CVType &CVR,
78 UdtModSourceLineRecord &Rec) {
79 return verifySourceLine(Rec.getUDT());
82 Error TpiHashVerifier::visitKnownRecord(CVType &CVR, ClassRecord &Rec) {
83 if (getTpiHash(Rec, CVR.data()) % NumHashBuckets != HashValues[Index])
84 return errorInvalidHash();
85 return Error::success();
87 Error TpiHashVerifier::visitKnownRecord(CVType &CVR, EnumRecord &Rec) {
88 if (getTpiHash(Rec, CVR.data()) % NumHashBuckets != HashValues[Index])
89 return errorInvalidHash();
90 return Error::success();
92 Error TpiHashVerifier::visitKnownRecord(CVType &CVR, UnionRecord &Rec) {
93 if (getTpiHash(Rec, CVR.data()) % NumHashBuckets != HashValues[Index])
94 return errorInvalidHash();
95 return Error::success();
98 Error TpiHashVerifier::verifySourceLine(codeview::TypeIndex TI) {
100 support::endian::write32le(Buf, TI.getIndex());
101 uint32_t Hash = hashStringV1(StringRef(Buf, 4));
102 if (Hash % NumHashBuckets != HashValues[Index])
103 return errorInvalidHash();
104 return Error::success();
107 Error TpiHashVerifier::visitTypeBegin(CVType &Rec) {
110 return Error::success();