blob: 77a2d57a83698a84d237da01a88006ccbf47f3f7 [file] [log] [blame]
Zachary Turner620961d2016-09-14 23:00:02 +00001//===- TpiHashing.cpp -----------------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000010#include "llvm/DebugInfo/PDB/Native/TpiHashing.h"
Zachary Turner620961d2016-09-14 23:00:02 +000011
Zachary Turnerf8a2e042017-06-15 23:04:42 +000012#include "llvm/DebugInfo/CodeView/TypeDeserializer.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000013#include "llvm/DebugInfo/PDB/Native/Hash.h"
Reid Klecknerc50349d2017-07-18 00:33:45 +000014#include "llvm/Support/JamCRC.h"
Zachary Turner620961d2016-09-14 23:00:02 +000015
16using namespace llvm;
17using namespace llvm::codeview;
18using namespace llvm::pdb;
19
20// Corresponds to `fUDTAnon`.
Reid Klecknerc50349d2017-07-18 00:33:45 +000021static bool isAnonymous(StringRef Name) {
Zachary Turner620961d2016-09-14 23:00:02 +000022 return Name == "<unnamed-tag>" || Name == "__unnamed" ||
23 Name.endswith("::<unnamed-tag>") || Name.endswith("::__unnamed");
24}
25
Reid Klecknerc50349d2017-07-18 00:33:45 +000026// Computes the hash for a user-defined type record. This could be a struct,
27// class, union, or enum.
28static 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());
Zachary Turner620961d2016-09-14 23:00:02 +000035
36 if (!ForwardRef && !Scoped && !IsAnon)
37 return hashStringV1(Rec.getName());
Reid Klecknerc50349d2017-07-18 00:33:45 +000038 if (!ForwardRef && HasUniqueName && !IsAnon)
Zachary Turner620961d2016-09-14 23:00:02 +000039 return hashStringV1(Rec.getUniqueName());
40 return hashBufferV8(FullRecord);
41}
42
Reid Klecknerc50349d2017-07-18 00:33:45 +000043template <typename T>
44static Expected<uint32_t> getHashForUdt(const CVType &Rec) {
45 T Deserialized;
46 if (auto E = TypeDeserializer::deserializeAs(const_cast<CVType &>(Rec),
47 Deserialized))
48 return std::move(E);
49 return getHashForUdt(Deserialized, Rec.data());
50}
51
52template <typename T>
53static Expected<uint32_t> getSourceLineHash(const CVType &Rec) {
54 T Deserialized;
55 if (auto E = TypeDeserializer::deserializeAs(const_cast<CVType &>(Rec),
56 Deserialized))
57 return std::move(E);
Zachary Turner620961d2016-09-14 23:00:02 +000058 char Buf[4];
Reid Klecknerc50349d2017-07-18 00:33:45 +000059 support::endian::write32le(Buf, Deserialized.getUDT().getIndex());
Zachary Turner620961d2016-09-14 23:00:02 +000060 return hashStringV1(StringRef(Buf, 4));
61}
62
Reid Klecknerc50349d2017-07-18 00:33:45 +000063Expected<uint32_t> llvm::pdb::hashTypeRecord(const CVType &Rec) {
64 switch (Rec.kind()) {
65 case LF_CLASS:
66 case LF_STRUCTURE:
67 case LF_INTERFACE:
68 return getHashForUdt<ClassRecord>(Rec);
69 case LF_UNION:
70 return getHashForUdt<UnionRecord>(Rec);
71 case LF_ENUM:
72 return getHashForUdt<EnumRecord>(Rec);
Zachary Turner620961d2016-09-14 23:00:02 +000073
Reid Klecknerc50349d2017-07-18 00:33:45 +000074 case LF_UDT_SRC_LINE:
75 return getSourceLineHash<UdtSourceLineRecord>(Rec);
76 case LF_UDT_MOD_SRC_LINE:
77 return getSourceLineHash<UdtModSourceLineRecord>(Rec);
Zachary Turner620961d2016-09-14 23:00:02 +000078
Reid Klecknerc50349d2017-07-18 00:33:45 +000079 default:
80 break;
81 }
Zachary Turner620961d2016-09-14 23:00:02 +000082
Reid Klecknerc50349d2017-07-18 00:33:45 +000083 // 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()),
86 Rec.data().size());
87 JC.update(Bytes);
88 return JC.getCRC();
Zachary Turner620961d2016-09-14 23:00:02 +000089}