LLVM  6.0.0svn
TpiHashing.cpp
Go to the documentation of this file.
1 //===- 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 
11 
14 #include "llvm/Support/JamCRC.h"
15 
16 using namespace llvm;
17 using namespace llvm::codeview;
18 using namespace llvm::pdb;
19 
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");
24 }
25 
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);
34  bool IsAnon = HasUniqueName && isAnonymous(Rec.getName());
35 
36  if (!ForwardRef && !Scoped && !IsAnon)
37  return hashStringV1(Rec.getName());
38  if (!ForwardRef && HasUniqueName && !IsAnon)
39  return hashStringV1(Rec.getUniqueName());
40  return hashBufferV8(FullRecord);
41 }
42 
43 template <typename T>
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 
52 template <typename T>
54  T Deserialized;
55  if (auto E = TypeDeserializer::deserializeAs(const_cast<CVType &>(Rec),
56  Deserialized))
57  return std::move(E);
58  char Buf[4];
59  support::endian::write32le(Buf, Deserialized.getUDT().getIndex());
60  return hashStringV1(StringRef(Buf, 4));
61 }
62 
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);
73 
74  case LF_UDT_SRC_LINE:
75  return getSourceLineHash<UdtSourceLineRecord>(Rec);
76  case LF_UDT_MOD_SRC_LINE:
77  return getSourceLineHash<UdtModSourceLineRecord>(Rec);
78 
79  default:
80  break;
81  }
82 
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()),
86  Rec.data().size());
87  JC.update(Bytes);
88  return JC.getCRC();
89 }
uint32_t getCRC() const
Definition: JamCRC.h:42
uint32_t hashBufferV8(ArrayRef< uint8_t > Data)
Definition: Hash.cpp:81
Kind kind() const
Definition: CVRecord.h:37
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
void write32le(void *P, uint32_t V)
Definition: Endian.h:404
static bool isAnonymous(StringRef Name)
Definition: TpiHashing.cpp:21
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool endswith(StringRef Suffix) const
Check if this string ends with the given Suffix.
Definition: StringRef.h:279
StringRef getName() const
Definition: TypeRecord.h:426
static Expected< uint32_t > getSourceLineHash(const CVType &Rec)
Definition: TpiHashing.cpp:53
Tagged union holding either a T or a Error.
Definition: CachePruning.h:23
ClassOptions getOptions() const
Definition: TypeRecord.h:424
ArrayRef< uint8_t > data() const
Definition: CVRecord.h:38
Expected< uint32_t > hashTypeRecord(const llvm::codeview::CVType &Type)
Definition: TpiHashing.cpp:63
uint32_t hashStringV1(StringRef Str)
Definition: Hash.cpp:21
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:149
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
StringRef getUniqueName() const
Definition: TypeRecord.h:427
static Error deserializeAs(CVType &CVT, T &Record)
const T * data() const
Definition: ArrayRef.h:146
static uint32_t getHashForUdt(const TagRecord &Rec, ArrayRef< uint8_t > FullRecord)
Definition: TpiHashing.cpp:28
void update(ArrayRef< char > Data)
Definition: JamCRC.cpp:92
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49