aboutsummaryrefslogtreecommitdiffstats
path: root/unittests/CodeGen/DIEHashTest.cpp
blob: 834a16a2ce11dcb2397e11754f764cb2e4f52a6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//===- llvm/unittest/DebugInfo/DWARFFormValueTest.cpp ---------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "../lib/CodeGen/AsmPrinter/DIE.h"
#include "../lib/CodeGen/AsmPrinter/DIEHash.h"
#include "llvm/Support/Dwarf.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/Format.h"
#include "gtest/gtest.h"

using namespace llvm;

namespace {
TEST(Data1, DIEHash) {
  DIEHash Hash;
  DIE Die(dwarf::DW_TAG_base_type);
  DIEInteger Size(4);
  Die.addValue(dwarf::DW_AT_byte_size, dwarf::DW_FORM_data1, &Size);
  uint64_t MD5Res = Hash.computeTypeSignature(&Die);
  ASSERT_EQ(0x1AFE116E83701108ULL, MD5Res);
}

TEST(TrivialType, DIEHash) {
  // A complete, but simple, type containing no members and defined on the first
  // line of a file.
  DIE Unnamed(dwarf::DW_TAG_structure_type);
  DIEInteger One(1);
  Unnamed.addValue(dwarf::DW_AT_byte_size, dwarf::DW_FORM_data1, &One);

  // Line and file number are ignored.
  Unnamed.addValue(dwarf::DW_AT_decl_file, dwarf::DW_FORM_data1, &One);
  Unnamed.addValue(dwarf::DW_AT_decl_line, dwarf::DW_FORM_data1, &One);
  uint64_t MD5Res = DIEHash().computeTypeSignature(&Unnamed);

  // The exact same hash GCC produces for this DIE.
  ASSERT_EQ(0x715305ce6cfd9ad1ULL, MD5Res);
}

TEST(NamedType, DIEHash) {
  // A complete named type containing no members and defined on the first line
  // of a file.
  DIE Foo(dwarf::DW_TAG_structure_type);
  DIEInteger One(1);
  DIEString FooStr(&One, "foo");
  Foo.addValue(dwarf::DW_AT_name, dwarf::DW_FORM_strp, &FooStr);
  Foo.addValue(dwarf::DW_AT_byte_size, dwarf::DW_FORM_data1, &One);

  uint64_t MD5Res = DIEHash().computeTypeSignature(&Foo);

  // The exact same hash GCC produces for this DIE.
  ASSERT_EQ(0xd566dbd2ca5265ffULL, MD5Res);
}

TEST(NamespacedType, DIEHash) {
  // A complete named type containing no members and defined on the first line
  // of a file.
  DIE CU(dwarf::DW_TAG_compile_unit);

  DIE *Space = new DIE(dwarf::DW_TAG_namespace);
  DIEInteger One(1);
  DIEString SpaceStr(&One, "space");
  Space->addValue(dwarf::DW_AT_name, dwarf::DW_FORM_strp, &SpaceStr);
  // DW_AT_declaration is ignored.
  Space->addValue(dwarf::DW_AT_declaration, dwarf::DW_FORM_flag_present, &One);
  // sibling?

  DIE *Foo = new DIE(dwarf::DW_TAG_structure_type);
  DIEString FooStr(&One, "foo");
  Foo->addValue(dwarf::DW_AT_name, dwarf::DW_FORM_strp, &FooStr);
  Foo->addValue(dwarf::DW_AT_byte_size, dwarf::DW_FORM_data1, &One);

  Space->addChild(Foo);
  CU.addChild(Space);

  uint64_t MD5Res = DIEHash().computeTypeSignature(Foo);

  // The exact same hash GCC produces for this DIE.
  ASSERT_EQ(0x7b80381fd17f1e33ULL, MD5Res);
}

TEST(TypeWithMember, DIEHash) {
  DIE Unnamed(dwarf::DW_TAG_structure_type);
  DIEInteger Four(4);
  Unnamed.addValue(dwarf::DW_AT_byte_size, dwarf::DW_FORM_data1, &Four);

  DIE *Member = new DIE(dwarf::DW_TAG_member);
  DIEString MemberStr(&Four, "member");
  Member->addValue(dwarf::DW_AT_name, dwarf::DW_FORM_strp, &MemberStr);
  // type
  DIEInteger Zero(0);
  Member->addValue(dwarf::DW_AT_data_member_location, dwarf::DW_FORM_data1, &Zero);

  Unnamed.addChild(Member);

  DIE Int(dwarf::DW_TAG_base_type);
  DIEString IntStr(&Four, "int");
  Int.addValue(dwarf::DW_AT_name, dwarf::DW_FORM_strp, &IntStr);
  Int.addValue(dwarf::DW_AT_byte_size, dwarf::DW_FORM_data1, &Four);
  DIEInteger Five(5);
  Int.addValue(dwarf::DW_AT_encoding, dwarf::DW_FORM_data1, &Five);

  DIEEntry IntRef(&Int);
  Member->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, &IntRef);

  uint64_t MD5Res = DIEHash().computeTypeSignature(&Unnamed);

  ASSERT_EQ(0x5646aa436b7e07c6ULL, MD5Res);
}
}