blob: b71b2b15814419b0b592edd8eda6de7ac981882f [file] [log] [blame]
Zachary Turner620961d2016-09-14 23:00:02 +00001//===- TpiHashing.cpp -----------------------------------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Zachary Turner620961d2016-09-14 23:00:02 +00006//
7//===----------------------------------------------------------------------===//
8
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +00009#include "llvm/DebugInfo/PDB/Native/TpiHashing.h"
Zachary Turner620961d2016-09-14 23:00:02 +000010
Zachary Turnerf8a2e042017-06-15 23:04:42 +000011#include "llvm/DebugInfo/CodeView/TypeDeserializer.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000012#include "llvm/DebugInfo/PDB/Native/Hash.h"
Hans Wennborg1e1e3ba2019-10-09 09:06:30 +000013#include "llvm/Support/CRC.h"
Zachary Turner620961d2016-09-14 23:00:02 +000014
15using namespace llvm;
16using namespace llvm::codeview;
17using namespace llvm::pdb;
18
19// Corresponds to `fUDTAnon`.
Reid Klecknerc50349d2017-07-18 00:33:45 +000020static bool isAnonymous(StringRef Name) {
Zachary Turner620961d2016-09-14 23:00:02 +000021 return Name == "<unnamed-tag>" || Name == "__unnamed" ||
22 Name.endswith("::<unnamed-tag>") || Name.endswith("::__unnamed");
23}
24
Reid Klecknerc50349d2017-07-18 00:33:45 +000025// 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(Rec.getName());
Zachary Turner620961d2016-09-14 23:00:02 +000034
35 if (!ForwardRef && !Scoped && !IsAnon)
36 return hashStringV1(Rec.getName());
Reid Klecknerc50349d2017-07-18 00:33:45 +000037 if (!ForwardRef && HasUniqueName && !IsAnon)
Zachary Turner620961d2016-09-14 23:00:02 +000038 return hashStringV1(Rec.getUniqueName());
39 return hashBufferV8(FullRecord);
40}
41
Reid Klecknerc50349d2017-07-18 00:33:45 +000042template <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>
Zachary Turnercfa1d492018-09-20 15:50:13 +000052static 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(NameToHash);
74 return TagRecordHash{std::move(Deserialized), FullHash, ThisRecordHash};
75}
76
77template <typename T>
Reid Klecknerc50349d2017-07-18 00:33:45 +000078static 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);
Zachary Turner620961d2016-09-14 23:00:02 +000083 char Buf[4];
Reid Klecknerc50349d2017-07-18 00:33:45 +000084 support::endian::write32le(Buf, Deserialized.getUDT().getIndex());
Zachary Turner620961d2016-09-14 23:00:02 +000085 return hashStringV1(StringRef(Buf, 4));
86}
87
Zachary Turnercfa1d492018-09-20 15:50:13 +000088Expected<TagRecordHash> llvm::pdb::hashTagRecord(const codeview::CVType &Type) {
89 switch (Type.kind()) {
90 case LF_CLASS:
91 case LF_STRUCTURE:
92 case LF_INTERFACE:
93 return getTagRecordHashForUdt<ClassRecord>(Type);
94 case LF_UNION:
95 return getTagRecordHashForUdt<UnionRecord>(Type);
96 case LF_ENUM:
97 return getTagRecordHashForUdt<EnumRecord>(Type);
98 default:
99 assert(false && "Type is not a tag record!");
100 }
101 return make_error<StringError>("Invalid record type",
102 inconvertibleErrorCode());
103}
104
Reid Klecknerc50349d2017-07-18 00:33:45 +0000105Expected<uint32_t> llvm::pdb::hashTypeRecord(const CVType &Rec) {
106 switch (Rec.kind()) {
107 case LF_CLASS:
108 case LF_STRUCTURE:
109 case LF_INTERFACE:
110 return getHashForUdt<ClassRecord>(Rec);
111 case LF_UNION:
112 return getHashForUdt<UnionRecord>(Rec);
113 case LF_ENUM:
114 return getHashForUdt<EnumRecord>(Rec);
Zachary Turner620961d2016-09-14 23:00:02 +0000115
Reid Klecknerc50349d2017-07-18 00:33:45 +0000116 case LF_UDT_SRC_LINE:
117 return getSourceLineHash<UdtSourceLineRecord>(Rec);
118 case LF_UDT_MOD_SRC_LINE:
119 return getSourceLineHash<UdtModSourceLineRecord>(Rec);
Zachary Turner620961d2016-09-14 23:00:02 +0000120
Reid Klecknerc50349d2017-07-18 00:33:45 +0000121 default:
122 break;
123 }
Zachary Turner620961d2016-09-14 23:00:02 +0000124
Reid Klecknerc50349d2017-07-18 00:33:45 +0000125 // Run CRC32 over the bytes. This corresponds to `hashBufv8`.
126 JamCRC JC(/*Init=*/0U);
Hans Wennborg1e1e3ba2019-10-09 09:06:30 +0000127 JC.update(Rec.data());
Reid Klecknerc50349d2017-07-18 00:33:45 +0000128 return JC.getCRC();
Zachary Turner620961d2016-09-14 23:00:02 +0000129}