blob: c82876b8058428d643815f03e735b3c3284072aa [file] [log] [blame]
Justin Lebar59b5dbc2016-07-13 18:38:20 +00001//===- llvm/unittest/CodeGen/DIEHashTest.cpp ------------------------------===//
Eric Christopher800a8762013-09-03 21:57:57 +00002//
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
Eric Christopher800a8762013-09-03 21:57:57 +000010#include "../lib/CodeGen/AsmPrinter/DIEHash.h"
Chandler Carruth1b279142015-01-14 11:23:27 +000011#include "llvm/ADT/STLExtras.h"
Zachary Turner19ca2b02017-06-07 03:48:56 +000012#include "llvm/BinaryFormat/Dwarf.h"
Chandler Carruth3c0d6072017-06-06 11:06:56 +000013#include "llvm/CodeGen/DIE.h"
Duncan P. N. Exon Smith24ad5c92015-05-24 16:40:47 +000014#include "llvm/CodeGen/DwarfStringPoolEntry.h"
Eric Christopher800a8762013-09-03 21:57:57 +000015#include "llvm/Support/Debug.h"
16#include "llvm/Support/Format.h"
17#include "gtest/gtest.h"
18
Eric Christopher800a8762013-09-03 21:57:57 +000019using namespace llvm;
David Blaikiee4df43e2013-10-15 23:00:17 +000020
21namespace {
Duncan P. N. Exon Smith24ad5c92015-05-24 16:40:47 +000022
23// Test fixture
24class DIEHashTest : public testing::Test {
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +000025public:
26 BumpPtrAllocator Alloc;
27
28private:
Duncan P. N. Exon Smith24ad5c92015-05-24 16:40:47 +000029 StringMap<DwarfStringPoolEntry> Pool;
30
31public:
32 DIEString getString(StringRef S) {
33 DwarfStringPoolEntry Entry = {nullptr, 1, 1};
Pavel Labath23332c52018-08-07 09:54:52 +000034 return DIEString(DwarfStringPoolEntryRef(
35 *Pool.insert(std::make_pair(S, Entry)).first, Entry.isIndexed()));
Duncan P. N. Exon Smith24ad5c92015-05-24 16:40:47 +000036 }
37};
38
39TEST_F(DIEHashTest, Data1) {
Eric Christopher800a8762013-09-03 21:57:57 +000040 DIEHash Hash;
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +000041 DIE &Die = *DIE::get(Alloc, dwarf::DW_TAG_base_type);
Benjamin Kramer449a88e2013-09-29 11:29:20 +000042 DIEInteger Size(4);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +000043 Die.addValue(Alloc, dwarf::DW_AT_byte_size, dwarf::DW_FORM_data1, Size);
David Blaikie377e8322013-10-24 18:29:03 +000044 uint64_t MD5Res = Hash.computeTypeSignature(Die);
David Blaikiec0987082013-10-16 23:36:20 +000045 ASSERT_EQ(0x1AFE116E83701108ULL, MD5Res);
46}
47
David Blaikief1545a22013-10-21 22:36:50 +000048// struct {};
Duncan P. N. Exon Smith24ad5c92015-05-24 16:40:47 +000049TEST_F(DIEHashTest, TrivialType) {
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +000050 DIE &Unnamed = *DIE::get(Alloc, dwarf::DW_TAG_structure_type);
David Blaikiec0987082013-10-16 23:36:20 +000051 DIEInteger One(1);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +000052 Unnamed.addValue(Alloc, dwarf::DW_AT_byte_size, dwarf::DW_FORM_data1, One);
David Blaikiec0987082013-10-16 23:36:20 +000053
54 // Line and file number are ignored.
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +000055 Unnamed.addValue(Alloc, dwarf::DW_AT_decl_file, dwarf::DW_FORM_data1, One);
56 Unnamed.addValue(Alloc, dwarf::DW_AT_decl_line, dwarf::DW_FORM_data1, One);
David Blaikie377e8322013-10-24 18:29:03 +000057 uint64_t MD5Res = DIEHash().computeTypeSignature(Unnamed);
David Blaikiec0987082013-10-16 23:36:20 +000058
59 // The exact same hash GCC produces for this DIE.
60 ASSERT_EQ(0x715305ce6cfd9ad1ULL, MD5Res);
Eric Christopher800a8762013-09-03 21:57:57 +000061}
David Blaikie88a68cb2013-10-17 00:10:34 +000062
David Blaikief1545a22013-10-21 22:36:50 +000063// struct foo { };
Duncan P. N. Exon Smith24ad5c92015-05-24 16:40:47 +000064TEST_F(DIEHashTest, NamedType) {
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +000065 DIE &Foo = *DIE::get(Alloc, dwarf::DW_TAG_structure_type);
David Blaikie88a68cb2013-10-17 00:10:34 +000066 DIEInteger One(1);
Duncan P. N. Exon Smith24ad5c92015-05-24 16:40:47 +000067 DIEString FooStr = getString("foo");
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +000068 Foo.addValue(Alloc, dwarf::DW_AT_name, dwarf::DW_FORM_strp, FooStr);
69 Foo.addValue(Alloc, dwarf::DW_AT_byte_size, dwarf::DW_FORM_data1, One);
David Blaikie88a68cb2013-10-17 00:10:34 +000070
David Blaikie377e8322013-10-24 18:29:03 +000071 uint64_t MD5Res = DIEHash().computeTypeSignature(Foo);
David Blaikie88a68cb2013-10-17 00:10:34 +000072
73 // The exact same hash GCC produces for this DIE.
74 ASSERT_EQ(0xd566dbd2ca5265ffULL, MD5Res);
75}
76
David Blaikief1545a22013-10-21 22:36:50 +000077// namespace space { struct foo { }; }
Duncan P. N. Exon Smith24ad5c92015-05-24 16:40:47 +000078TEST_F(DIEHashTest, NamespacedType) {
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +000079 DIE &CU = *DIE::get(Alloc, dwarf::DW_TAG_compile_unit);
David Blaikie88a68cb2013-10-17 00:10:34 +000080
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +000081 auto Space = DIE::get(Alloc, dwarf::DW_TAG_namespace);
David Blaikie88a68cb2013-10-17 00:10:34 +000082 DIEInteger One(1);
Duncan P. N. Exon Smith24ad5c92015-05-24 16:40:47 +000083 DIEString SpaceStr = getString("space");
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +000084 Space->addValue(Alloc, dwarf::DW_AT_name, dwarf::DW_FORM_strp, SpaceStr);
David Blaikie88a68cb2013-10-17 00:10:34 +000085 // DW_AT_declaration is ignored.
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +000086 Space->addValue(Alloc, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag_present,
87 One);
David Blaikie88a68cb2013-10-17 00:10:34 +000088 // sibling?
89
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +000090 auto Foo = DIE::get(Alloc, dwarf::DW_TAG_structure_type);
Duncan P. N. Exon Smith24ad5c92015-05-24 16:40:47 +000091 DIEString FooStr = getString("foo");
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +000092 Foo->addValue(Alloc, dwarf::DW_AT_name, dwarf::DW_FORM_strp, FooStr);
93 Foo->addValue(Alloc, dwarf::DW_AT_byte_size, dwarf::DW_FORM_data1, One);
David Blaikie88a68cb2013-10-17 00:10:34 +000094
David Blaikie12d52242014-04-25 20:00:34 +000095 DIE &N = *Foo;
96 Space->addChild(std::move(Foo));
97 CU.addChild(std::move(Space));
David Blaikie88a68cb2013-10-17 00:10:34 +000098
David Blaikie12d52242014-04-25 20:00:34 +000099 uint64_t MD5Res = DIEHash().computeTypeSignature(N);
David Blaikie88a68cb2013-10-17 00:10:34 +0000100
101 // The exact same hash GCC produces for this DIE.
102 ASSERT_EQ(0x7b80381fd17f1e33ULL, MD5Res);
103}
David Blaikie47f66d52013-10-17 22:07:09 +0000104
David Blaikief1545a22013-10-21 22:36:50 +0000105// struct { int member; };
Duncan P. N. Exon Smith24ad5c92015-05-24 16:40:47 +0000106TEST_F(DIEHashTest, TypeWithMember) {
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +0000107 DIE &Unnamed = *DIE::get(Alloc, dwarf::DW_TAG_structure_type);
David Blaikie47f66d52013-10-17 22:07:09 +0000108 DIEInteger Four(4);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000109 Unnamed.addValue(Alloc, dwarf::DW_AT_byte_size, dwarf::DW_FORM_data1, Four);
David Blaikie47f66d52013-10-17 22:07:09 +0000110
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +0000111 DIE &Int = *DIE::get(Alloc, dwarf::DW_TAG_base_type);
Duncan P. N. Exon Smith24ad5c92015-05-24 16:40:47 +0000112 DIEString IntStr = getString("int");
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000113 Int.addValue(Alloc, dwarf::DW_AT_name, dwarf::DW_FORM_strp, IntStr);
114 Int.addValue(Alloc, dwarf::DW_AT_byte_size, dwarf::DW_FORM_data1, Four);
David Blaikie47f66d52013-10-17 22:07:09 +0000115 DIEInteger Five(5);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000116 Int.addValue(Alloc, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1, Five);
David Blaikie47f66d52013-10-17 22:07:09 +0000117
David Blaikie172515f2014-04-25 19:33:43 +0000118 DIEEntry IntRef(Int);
David Blaikie12d52242014-04-25 20:00:34 +0000119
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +0000120 auto Member = DIE::get(Alloc, dwarf::DW_TAG_member);
Duncan P. N. Exon Smith24ad5c92015-05-24 16:40:47 +0000121 DIEString MemberStr = getString("member");
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000122 Member->addValue(Alloc, dwarf::DW_AT_name, dwarf::DW_FORM_strp, MemberStr);
David Blaikie12d52242014-04-25 20:00:34 +0000123 DIEInteger Zero(0);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000124 Member->addValue(Alloc, dwarf::DW_AT_data_member_location,
125 dwarf::DW_FORM_data1, Zero);
126 Member->addValue(Alloc, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IntRef);
David Blaikie47f66d52013-10-17 22:07:09 +0000127
David Blaikie12d52242014-04-25 20:00:34 +0000128 Unnamed.addChild(std::move(Member));
129
David Blaikie377e8322013-10-24 18:29:03 +0000130 uint64_t MD5Res = DIEHash().computeTypeSignature(Unnamed);
David Blaikie47f66d52013-10-17 22:07:09 +0000131
132 ASSERT_EQ(0x5646aa436b7e07c6ULL, MD5Res);
133}
David Blaikie3baa3c32013-10-21 18:59:40 +0000134
David Blaikief1545a22013-10-21 22:36:50 +0000135// struct foo { int mem1, mem2; };
Duncan P. N. Exon Smith24ad5c92015-05-24 16:40:47 +0000136TEST_F(DIEHashTest, ReusedType) {
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +0000137 DIE &Unnamed = *DIE::get(Alloc, dwarf::DW_TAG_structure_type);
David Blaikie3baa3c32013-10-21 18:59:40 +0000138 DIEInteger Eight(8);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000139 Unnamed.addValue(Alloc, dwarf::DW_AT_byte_size, dwarf::DW_FORM_data1, Eight);
David Blaikie3baa3c32013-10-21 18:59:40 +0000140
David Blaikie3baa3c32013-10-21 18:59:40 +0000141 DIEInteger Four(4);
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +0000142 DIE &Int = *DIE::get(Alloc, dwarf::DW_TAG_base_type);
Duncan P. N. Exon Smith24ad5c92015-05-24 16:40:47 +0000143 DIEString IntStr = getString("int");
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000144 Int.addValue(Alloc, dwarf::DW_AT_name, dwarf::DW_FORM_strp, IntStr);
145 Int.addValue(Alloc, dwarf::DW_AT_byte_size, dwarf::DW_FORM_data1, Four);
David Blaikie3baa3c32013-10-21 18:59:40 +0000146 DIEInteger Five(5);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000147 Int.addValue(Alloc, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1, Five);
David Blaikie3baa3c32013-10-21 18:59:40 +0000148
David Blaikie172515f2014-04-25 19:33:43 +0000149 DIEEntry IntRef(Int);
David Blaikie12d52242014-04-25 20:00:34 +0000150
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +0000151 auto Mem1 = DIE::get(Alloc, dwarf::DW_TAG_member);
Duncan P. N. Exon Smith24ad5c92015-05-24 16:40:47 +0000152 DIEString Mem1Str = getString("mem1");
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000153 Mem1->addValue(Alloc, dwarf::DW_AT_name, dwarf::DW_FORM_strp, Mem1Str);
David Blaikie12d52242014-04-25 20:00:34 +0000154 DIEInteger Zero(0);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000155 Mem1->addValue(Alloc, dwarf::DW_AT_data_member_location, dwarf::DW_FORM_data1,
Duncan P. N. Exon Smith09fe4bf2015-05-27 22:14:58 +0000156 Zero);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000157 Mem1->addValue(Alloc, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IntRef);
David Blaikie12d52242014-04-25 20:00:34 +0000158
159 Unnamed.addChild(std::move(Mem1));
160
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +0000161 auto Mem2 = DIE::get(Alloc, dwarf::DW_TAG_member);
Duncan P. N. Exon Smith24ad5c92015-05-24 16:40:47 +0000162 DIEString Mem2Str = getString("mem2");
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000163 Mem2->addValue(Alloc, dwarf::DW_AT_name, dwarf::DW_FORM_strp, Mem2Str);
164 Mem2->addValue(Alloc, dwarf::DW_AT_data_member_location, dwarf::DW_FORM_data1,
Duncan P. N. Exon Smith09fe4bf2015-05-27 22:14:58 +0000165 Four);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000166 Mem2->addValue(Alloc, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IntRef);
David Blaikie3baa3c32013-10-21 18:59:40 +0000167
David Blaikie12d52242014-04-25 20:00:34 +0000168 Unnamed.addChild(std::move(Mem2));
169
David Blaikie377e8322013-10-24 18:29:03 +0000170 uint64_t MD5Res = DIEHash().computeTypeSignature(Unnamed);
David Blaikie3baa3c32013-10-21 18:59:40 +0000171
172 ASSERT_EQ(0x3a7dc3ed7b76b2f8ULL, MD5Res);
173}
174
David Blaikief1545a22013-10-21 22:36:50 +0000175// struct foo { static foo f; };
Duncan P. N. Exon Smith24ad5c92015-05-24 16:40:47 +0000176TEST_F(DIEHashTest, RecursiveType) {
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +0000177 DIE &Foo = *DIE::get(Alloc, dwarf::DW_TAG_structure_type);
David Blaikie3baa3c32013-10-21 18:59:40 +0000178 DIEInteger One(1);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000179 Foo.addValue(Alloc, dwarf::DW_AT_byte_size, dwarf::DW_FORM_data1, One);
Duncan P. N. Exon Smith24ad5c92015-05-24 16:40:47 +0000180 DIEString FooStr = getString("foo");
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000181 Foo.addValue(Alloc, dwarf::DW_AT_name, dwarf::DW_FORM_strp, FooStr);
David Blaikie3baa3c32013-10-21 18:59:40 +0000182
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +0000183 auto Mem = DIE::get(Alloc, dwarf::DW_TAG_member);
Duncan P. N. Exon Smith24ad5c92015-05-24 16:40:47 +0000184 DIEString MemStr = getString("mem");
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000185 Mem->addValue(Alloc, dwarf::DW_AT_name, dwarf::DW_FORM_strp, MemStr);
David Blaikie172515f2014-04-25 19:33:43 +0000186 DIEEntry FooRef(Foo);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000187 Mem->addValue(Alloc, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, FooRef);
David Blaikie3baa3c32013-10-21 18:59:40 +0000188 // DW_AT_external and DW_AT_declaration are ignored anyway, so skip them.
189
David Blaikie12d52242014-04-25 20:00:34 +0000190 Foo.addChild(std::move(Mem));
David Blaikie3baa3c32013-10-21 18:59:40 +0000191
David Blaikie377e8322013-10-24 18:29:03 +0000192 uint64_t MD5Res = DIEHash().computeTypeSignature(Foo);
David Blaikie3baa3c32013-10-21 18:59:40 +0000193
194 ASSERT_EQ(0x73d8b25aef227b06ULL, MD5Res);
195}
David Blaikief1545a22013-10-21 22:36:50 +0000196
197// struct foo { foo *mem; };
Duncan P. N. Exon Smith24ad5c92015-05-24 16:40:47 +0000198TEST_F(DIEHashTest, Pointer) {
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +0000199 DIE &Foo = *DIE::get(Alloc, dwarf::DW_TAG_structure_type);
David Blaikief1545a22013-10-21 22:36:50 +0000200 DIEInteger Eight(8);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000201 Foo.addValue(Alloc, dwarf::DW_AT_byte_size, dwarf::DW_FORM_data1, Eight);
Duncan P. N. Exon Smith24ad5c92015-05-24 16:40:47 +0000202 DIEString FooStr = getString("foo");
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000203 Foo.addValue(Alloc, dwarf::DW_AT_name, dwarf::DW_FORM_strp, FooStr);
David Blaikief1545a22013-10-21 22:36:50 +0000204
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +0000205 auto Mem = DIE::get(Alloc, dwarf::DW_TAG_member);
Duncan P. N. Exon Smith24ad5c92015-05-24 16:40:47 +0000206 DIEString MemStr = getString("mem");
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000207 Mem->addValue(Alloc, dwarf::DW_AT_name, dwarf::DW_FORM_strp, MemStr);
David Blaikief1545a22013-10-21 22:36:50 +0000208 DIEInteger Zero(0);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000209 Mem->addValue(Alloc, dwarf::DW_AT_data_member_location, dwarf::DW_FORM_data1,
210 Zero);
David Blaikief1545a22013-10-21 22:36:50 +0000211
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +0000212 DIE &FooPtr = *DIE::get(Alloc, dwarf::DW_TAG_pointer_type);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000213 FooPtr.addValue(Alloc, dwarf::DW_AT_byte_size, dwarf::DW_FORM_data1, Eight);
David Blaikie172515f2014-04-25 19:33:43 +0000214 DIEEntry FooRef(Foo);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000215 FooPtr.addValue(Alloc, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, FooRef);
David Blaikief1545a22013-10-21 22:36:50 +0000216
David Blaikie172515f2014-04-25 19:33:43 +0000217 DIEEntry FooPtrRef(FooPtr);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000218 Mem->addValue(Alloc, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, FooPtrRef);
David Blaikief1545a22013-10-21 22:36:50 +0000219
David Blaikie12d52242014-04-25 20:00:34 +0000220 Foo.addChild(std::move(Mem));
David Blaikief1545a22013-10-21 22:36:50 +0000221
David Blaikie377e8322013-10-24 18:29:03 +0000222 uint64_t MD5Res = DIEHash().computeTypeSignature(Foo);
David Blaikief1545a22013-10-21 22:36:50 +0000223
224 ASSERT_EQ(0x74ea73862e8708d2ULL, MD5Res);
225}
David Blaikie449f0362013-10-21 23:06:19 +0000226
227// struct foo { foo &mem; };
Duncan P. N. Exon Smith24ad5c92015-05-24 16:40:47 +0000228TEST_F(DIEHashTest, Reference) {
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +0000229 DIE &Foo = *DIE::get(Alloc, dwarf::DW_TAG_structure_type);
David Blaikie449f0362013-10-21 23:06:19 +0000230 DIEInteger Eight(8);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000231 Foo.addValue(Alloc, dwarf::DW_AT_byte_size, dwarf::DW_FORM_data1, Eight);
Duncan P. N. Exon Smith24ad5c92015-05-24 16:40:47 +0000232 DIEString FooStr = getString("foo");
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000233 Foo.addValue(Alloc, dwarf::DW_AT_name, dwarf::DW_FORM_strp, FooStr);
David Blaikie449f0362013-10-21 23:06:19 +0000234
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +0000235 auto Mem = DIE::get(Alloc, dwarf::DW_TAG_member);
Duncan P. N. Exon Smith24ad5c92015-05-24 16:40:47 +0000236 DIEString MemStr = getString("mem");
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000237 Mem->addValue(Alloc, dwarf::DW_AT_name, dwarf::DW_FORM_strp, MemStr);
David Blaikie449f0362013-10-21 23:06:19 +0000238 DIEInteger Zero(0);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000239 Mem->addValue(Alloc, dwarf::DW_AT_data_member_location, dwarf::DW_FORM_data1,
240 Zero);
David Blaikie449f0362013-10-21 23:06:19 +0000241
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +0000242 DIE &FooRef = *DIE::get(Alloc, dwarf::DW_TAG_reference_type);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000243 FooRef.addValue(Alloc, dwarf::DW_AT_byte_size, dwarf::DW_FORM_data1, Eight);
David Blaikie172515f2014-04-25 19:33:43 +0000244 DIEEntry FooEntry(Foo);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000245 FooRef.addValue(Alloc, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, FooEntry);
David Blaikie449f0362013-10-21 23:06:19 +0000246
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +0000247 DIE &FooRefConst = *DIE::get(Alloc, dwarf::DW_TAG_const_type);
David Blaikie172515f2014-04-25 19:33:43 +0000248 DIEEntry FooRefRef(FooRef);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000249 FooRefConst.addValue(Alloc, dwarf::DW_AT_type, dwarf::DW_FORM_ref4,
250 FooRefRef);
David Blaikie449f0362013-10-21 23:06:19 +0000251
David Blaikie172515f2014-04-25 19:33:43 +0000252 DIEEntry FooRefConstRef(FooRefConst);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000253 Mem->addValue(Alloc, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, FooRefConstRef);
David Blaikie449f0362013-10-21 23:06:19 +0000254
David Blaikie12d52242014-04-25 20:00:34 +0000255 Foo.addChild(std::move(Mem));
David Blaikie449f0362013-10-21 23:06:19 +0000256
David Blaikie377e8322013-10-24 18:29:03 +0000257 uint64_t MD5Res = DIEHash().computeTypeSignature(Foo);
David Blaikie449f0362013-10-21 23:06:19 +0000258
259 ASSERT_EQ(0xa0b15f467ad4525bULL, MD5Res);
260}
261
262// struct foo { foo &&mem; };
Duncan P. N. Exon Smith24ad5c92015-05-24 16:40:47 +0000263TEST_F(DIEHashTest, RValueReference) {
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +0000264 DIE &Foo = *DIE::get(Alloc, dwarf::DW_TAG_structure_type);
David Blaikie449f0362013-10-21 23:06:19 +0000265 DIEInteger Eight(8);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000266 Foo.addValue(Alloc, dwarf::DW_AT_byte_size, dwarf::DW_FORM_data1, Eight);
Duncan P. N. Exon Smith24ad5c92015-05-24 16:40:47 +0000267 DIEString FooStr = getString("foo");
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000268 Foo.addValue(Alloc, dwarf::DW_AT_name, dwarf::DW_FORM_strp, FooStr);
David Blaikie449f0362013-10-21 23:06:19 +0000269
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +0000270 auto Mem = DIE::get(Alloc, dwarf::DW_TAG_member);
Duncan P. N. Exon Smith24ad5c92015-05-24 16:40:47 +0000271 DIEString MemStr = getString("mem");
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000272 Mem->addValue(Alloc, dwarf::DW_AT_name, dwarf::DW_FORM_strp, MemStr);
David Blaikie449f0362013-10-21 23:06:19 +0000273 DIEInteger Zero(0);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000274 Mem->addValue(Alloc, dwarf::DW_AT_data_member_location, dwarf::DW_FORM_data1,
275 Zero);
David Blaikie449f0362013-10-21 23:06:19 +0000276
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +0000277 DIE &FooRef = *DIE::get(Alloc, dwarf::DW_TAG_rvalue_reference_type);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000278 FooRef.addValue(Alloc, dwarf::DW_AT_byte_size, dwarf::DW_FORM_data1, Eight);
David Blaikie172515f2014-04-25 19:33:43 +0000279 DIEEntry FooEntry(Foo);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000280 FooRef.addValue(Alloc, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, FooEntry);
David Blaikie449f0362013-10-21 23:06:19 +0000281
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +0000282 DIE &FooRefConst = *DIE::get(Alloc, dwarf::DW_TAG_const_type);
David Blaikie172515f2014-04-25 19:33:43 +0000283 DIEEntry FooRefRef(FooRef);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000284 FooRefConst.addValue(Alloc, dwarf::DW_AT_type, dwarf::DW_FORM_ref4,
285 FooRefRef);
David Blaikie449f0362013-10-21 23:06:19 +0000286
David Blaikie172515f2014-04-25 19:33:43 +0000287 DIEEntry FooRefConstRef(FooRefConst);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000288 Mem->addValue(Alloc, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, FooRefConstRef);
David Blaikie449f0362013-10-21 23:06:19 +0000289
David Blaikie12d52242014-04-25 20:00:34 +0000290 Foo.addChild(std::move(Mem));
David Blaikie449f0362013-10-21 23:06:19 +0000291
David Blaikie377e8322013-10-24 18:29:03 +0000292 uint64_t MD5Res = DIEHash().computeTypeSignature(Foo);
David Blaikie449f0362013-10-21 23:06:19 +0000293
294 ASSERT_EQ(0xad211c8c3b31e57ULL, MD5Res);
295}
David Blaikief1962082013-10-22 18:14:41 +0000296
297// struct foo { foo foo::*mem; };
Duncan P. N. Exon Smith24ad5c92015-05-24 16:40:47 +0000298TEST_F(DIEHashTest, PtrToMember) {
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +0000299 DIE &Foo = *DIE::get(Alloc, dwarf::DW_TAG_structure_type);
David Blaikief1962082013-10-22 18:14:41 +0000300 DIEInteger Eight(8);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000301 Foo.addValue(Alloc, dwarf::DW_AT_byte_size, dwarf::DW_FORM_data1, Eight);
Duncan P. N. Exon Smith24ad5c92015-05-24 16:40:47 +0000302 DIEString FooStr = getString("foo");
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000303 Foo.addValue(Alloc, dwarf::DW_AT_name, dwarf::DW_FORM_strp, FooStr);
David Blaikief1962082013-10-22 18:14:41 +0000304
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +0000305 auto Mem = DIE::get(Alloc, dwarf::DW_TAG_member);
Duncan P. N. Exon Smith24ad5c92015-05-24 16:40:47 +0000306 DIEString MemStr = getString("mem");
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000307 Mem->addValue(Alloc, dwarf::DW_AT_name, dwarf::DW_FORM_strp, MemStr);
David Blaikief1962082013-10-22 18:14:41 +0000308 DIEInteger Zero(0);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000309 Mem->addValue(Alloc, dwarf::DW_AT_data_member_location, dwarf::DW_FORM_data1,
310 Zero);
David Blaikief1962082013-10-22 18:14:41 +0000311
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +0000312 DIE &PtrToFooMem = *DIE::get(Alloc, dwarf::DW_TAG_ptr_to_member_type);
David Blaikie172515f2014-04-25 19:33:43 +0000313 DIEEntry FooEntry(Foo);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000314 PtrToFooMem.addValue(Alloc, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, FooEntry);
315 PtrToFooMem.addValue(Alloc, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4,
Duncan P. N. Exon Smith09fe4bf2015-05-27 22:14:58 +0000316 FooEntry);
David Blaikief1962082013-10-22 18:14:41 +0000317
David Blaikie172515f2014-04-25 19:33:43 +0000318 DIEEntry PtrToFooMemRef(PtrToFooMem);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000319 Mem->addValue(Alloc, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, PtrToFooMemRef);
David Blaikief1962082013-10-22 18:14:41 +0000320
David Blaikie12d52242014-04-25 20:00:34 +0000321 Foo.addChild(std::move(Mem));
David Blaikief1962082013-10-22 18:14:41 +0000322
David Blaikie377e8322013-10-24 18:29:03 +0000323 uint64_t MD5Res = DIEHash().computeTypeSignature(Foo);
David Blaikief1962082013-10-22 18:14:41 +0000324
325 ASSERT_EQ(0x852e0c9ff7c04ebULL, MD5Res);
326}
327
328// Check that the hash for a pointer-to-member matches regardless of whether the
329// pointed-to type is a declaration or a definition.
330//
331// struct bar; // { };
332// struct foo { bar foo::*mem; };
Duncan P. N. Exon Smith24ad5c92015-05-24 16:40:47 +0000333TEST_F(DIEHashTest, PtrToMemberDeclDefMatch) {
David Blaikief1962082013-10-22 18:14:41 +0000334 DIEInteger Zero(0);
335 DIEInteger One(1);
336 DIEInteger Eight(8);
Duncan P. N. Exon Smith24ad5c92015-05-24 16:40:47 +0000337 DIEString FooStr = getString("foo");
338 DIEString BarStr = getString("bar");
339 DIEString MemStr = getString("mem");
David Blaikief1962082013-10-22 18:14:41 +0000340 uint64_t MD5ResDecl;
341 {
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +0000342 DIE &Bar = *DIE::get(Alloc, dwarf::DW_TAG_structure_type);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000343 Bar.addValue(Alloc, dwarf::DW_AT_name, dwarf::DW_FORM_strp, BarStr);
344 Bar.addValue(Alloc, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag_present,
345 One);
David Blaikief1962082013-10-22 18:14:41 +0000346
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +0000347 DIE &Foo = *DIE::get(Alloc, dwarf::DW_TAG_structure_type);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000348 Foo.addValue(Alloc, dwarf::DW_AT_byte_size, dwarf::DW_FORM_data1, Eight);
349 Foo.addValue(Alloc, dwarf::DW_AT_name, dwarf::DW_FORM_strp, FooStr);
David Blaikief1962082013-10-22 18:14:41 +0000350
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +0000351 auto Mem = DIE::get(Alloc, dwarf::DW_TAG_member);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000352 Mem->addValue(Alloc, dwarf::DW_AT_name, dwarf::DW_FORM_strp, MemStr);
353 Mem->addValue(Alloc, dwarf::DW_AT_data_member_location,
354 dwarf::DW_FORM_data1, Zero);
David Blaikief1962082013-10-22 18:14:41 +0000355
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +0000356 DIE &PtrToFooMem = *DIE::get(Alloc, dwarf::DW_TAG_ptr_to_member_type);
David Blaikie172515f2014-04-25 19:33:43 +0000357 DIEEntry BarEntry(Bar);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000358 PtrToFooMem.addValue(Alloc, dwarf::DW_AT_type, dwarf::DW_FORM_ref4,
359 BarEntry);
David Blaikie172515f2014-04-25 19:33:43 +0000360 DIEEntry FooEntry(Foo);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000361 PtrToFooMem.addValue(Alloc, dwarf::DW_AT_containing_type,
362 dwarf::DW_FORM_ref4, FooEntry);
David Blaikief1962082013-10-22 18:14:41 +0000363
David Blaikie172515f2014-04-25 19:33:43 +0000364 DIEEntry PtrToFooMemRef(PtrToFooMem);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000365 Mem->addValue(Alloc, dwarf::DW_AT_type, dwarf::DW_FORM_ref4,
366 PtrToFooMemRef);
David Blaikief1962082013-10-22 18:14:41 +0000367
David Blaikie12d52242014-04-25 20:00:34 +0000368 Foo.addChild(std::move(Mem));
David Blaikief1962082013-10-22 18:14:41 +0000369
David Blaikie377e8322013-10-24 18:29:03 +0000370 MD5ResDecl = DIEHash().computeTypeSignature(Foo);
David Blaikief1962082013-10-22 18:14:41 +0000371 }
372 uint64_t MD5ResDef;
373 {
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +0000374 DIE &Bar = *DIE::get(Alloc, dwarf::DW_TAG_structure_type);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000375 Bar.addValue(Alloc, dwarf::DW_AT_name, dwarf::DW_FORM_strp, BarStr);
376 Bar.addValue(Alloc, dwarf::DW_AT_byte_size, dwarf::DW_FORM_data1, One);
David Blaikief1962082013-10-22 18:14:41 +0000377
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +0000378 DIE &Foo = *DIE::get(Alloc, dwarf::DW_TAG_structure_type);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000379 Foo.addValue(Alloc, dwarf::DW_AT_byte_size, dwarf::DW_FORM_data1, Eight);
380 Foo.addValue(Alloc, dwarf::DW_AT_name, dwarf::DW_FORM_strp, FooStr);
David Blaikief1962082013-10-22 18:14:41 +0000381
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +0000382 auto Mem = DIE::get(Alloc, dwarf::DW_TAG_member);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000383 Mem->addValue(Alloc, dwarf::DW_AT_name, dwarf::DW_FORM_strp, MemStr);
384 Mem->addValue(Alloc, dwarf::DW_AT_data_member_location,
385 dwarf::DW_FORM_data1, Zero);
David Blaikief1962082013-10-22 18:14:41 +0000386
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +0000387 DIE &PtrToFooMem = *DIE::get(Alloc, dwarf::DW_TAG_ptr_to_member_type);
David Blaikie172515f2014-04-25 19:33:43 +0000388 DIEEntry BarEntry(Bar);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000389 PtrToFooMem.addValue(Alloc, dwarf::DW_AT_type, dwarf::DW_FORM_ref4,
390 BarEntry);
David Blaikie172515f2014-04-25 19:33:43 +0000391 DIEEntry FooEntry(Foo);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000392 PtrToFooMem.addValue(Alloc, dwarf::DW_AT_containing_type,
393 dwarf::DW_FORM_ref4, FooEntry);
David Blaikief1962082013-10-22 18:14:41 +0000394
David Blaikie172515f2014-04-25 19:33:43 +0000395 DIEEntry PtrToFooMemRef(PtrToFooMem);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000396 Mem->addValue(Alloc, dwarf::DW_AT_type, dwarf::DW_FORM_ref4,
397 PtrToFooMemRef);
David Blaikief1962082013-10-22 18:14:41 +0000398
David Blaikie12d52242014-04-25 20:00:34 +0000399 Foo.addChild(std::move(Mem));
David Blaikief1962082013-10-22 18:14:41 +0000400
David Blaikie377e8322013-10-24 18:29:03 +0000401 MD5ResDef = DIEHash().computeTypeSignature(Foo);
David Blaikief1962082013-10-22 18:14:41 +0000402 }
403 ASSERT_EQ(MD5ResDef, MD5ResDecl);
404}
405
406// Check that the hash for a pointer-to-member matches regardless of whether the
407// pointed-to type is a declaration or a definition.
408//
409// struct bar; // { };
410// struct foo { bar bar::*mem; };
Duncan P. N. Exon Smith24ad5c92015-05-24 16:40:47 +0000411TEST_F(DIEHashTest, PtrToMemberDeclDefMisMatch) {
David Blaikief1962082013-10-22 18:14:41 +0000412 DIEInteger Zero(0);
413 DIEInteger One(1);
414 DIEInteger Eight(8);
Duncan P. N. Exon Smith24ad5c92015-05-24 16:40:47 +0000415 DIEString FooStr = getString("foo");
416 DIEString BarStr = getString("bar");
417 DIEString MemStr = getString("mem");
David Blaikief1962082013-10-22 18:14:41 +0000418 uint64_t MD5ResDecl;
419 {
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +0000420 DIE &Bar = *DIE::get(Alloc, dwarf::DW_TAG_structure_type);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000421 Bar.addValue(Alloc, dwarf::DW_AT_name, dwarf::DW_FORM_strp, BarStr);
422 Bar.addValue(Alloc, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag_present,
423 One);
David Blaikief1962082013-10-22 18:14:41 +0000424
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +0000425 DIE &Foo = *DIE::get(Alloc, dwarf::DW_TAG_structure_type);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000426 Foo.addValue(Alloc, dwarf::DW_AT_byte_size, dwarf::DW_FORM_data1, Eight);
427 Foo.addValue(Alloc, dwarf::DW_AT_name, dwarf::DW_FORM_strp, FooStr);
David Blaikief1962082013-10-22 18:14:41 +0000428
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +0000429 auto Mem = DIE::get(Alloc, dwarf::DW_TAG_member);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000430 Mem->addValue(Alloc, dwarf::DW_AT_name, dwarf::DW_FORM_strp, MemStr);
431 Mem->addValue(Alloc, dwarf::DW_AT_data_member_location,
432 dwarf::DW_FORM_data1, Zero);
David Blaikief1962082013-10-22 18:14:41 +0000433
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +0000434 DIE &PtrToFooMem = *DIE::get(Alloc, dwarf::DW_TAG_ptr_to_member_type);
David Blaikie172515f2014-04-25 19:33:43 +0000435 DIEEntry BarEntry(Bar);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000436 PtrToFooMem.addValue(Alloc, dwarf::DW_AT_type, dwarf::DW_FORM_ref4,
Duncan P. N. Exon Smith09fe4bf2015-05-27 22:14:58 +0000437 BarEntry);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000438 PtrToFooMem.addValue(Alloc, dwarf::DW_AT_containing_type,
439 dwarf::DW_FORM_ref4, BarEntry);
David Blaikief1962082013-10-22 18:14:41 +0000440
David Blaikie172515f2014-04-25 19:33:43 +0000441 DIEEntry PtrToFooMemRef(PtrToFooMem);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000442 Mem->addValue(Alloc, dwarf::DW_AT_type, dwarf::DW_FORM_ref4,
443 PtrToFooMemRef);
David Blaikief1962082013-10-22 18:14:41 +0000444
David Blaikie12d52242014-04-25 20:00:34 +0000445 Foo.addChild(std::move(Mem));
David Blaikief1962082013-10-22 18:14:41 +0000446
David Blaikie377e8322013-10-24 18:29:03 +0000447 MD5ResDecl = DIEHash().computeTypeSignature(Foo);
David Blaikief1962082013-10-22 18:14:41 +0000448 }
449 uint64_t MD5ResDef;
450 {
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +0000451 DIE &Bar = *DIE::get(Alloc, dwarf::DW_TAG_structure_type);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000452 Bar.addValue(Alloc, dwarf::DW_AT_name, dwarf::DW_FORM_strp, BarStr);
453 Bar.addValue(Alloc, dwarf::DW_AT_byte_size, dwarf::DW_FORM_data1, One);
David Blaikief1962082013-10-22 18:14:41 +0000454
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +0000455 DIE &Foo = *DIE::get(Alloc, dwarf::DW_TAG_structure_type);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000456 Foo.addValue(Alloc, dwarf::DW_AT_byte_size, dwarf::DW_FORM_data1, Eight);
457 Foo.addValue(Alloc, dwarf::DW_AT_name, dwarf::DW_FORM_strp, FooStr);
David Blaikief1962082013-10-22 18:14:41 +0000458
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +0000459 auto Mem = DIE::get(Alloc, dwarf::DW_TAG_member);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000460 Mem->addValue(Alloc, dwarf::DW_AT_name, dwarf::DW_FORM_strp, MemStr);
461 Mem->addValue(Alloc, dwarf::DW_AT_data_member_location,
462 dwarf::DW_FORM_data1, Zero);
David Blaikief1962082013-10-22 18:14:41 +0000463
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +0000464 DIE &PtrToFooMem = *DIE::get(Alloc, dwarf::DW_TAG_ptr_to_member_type);
David Blaikie172515f2014-04-25 19:33:43 +0000465 DIEEntry BarEntry(Bar);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000466 PtrToFooMem.addValue(Alloc, dwarf::DW_AT_type, dwarf::DW_FORM_ref4,
Duncan P. N. Exon Smith09fe4bf2015-05-27 22:14:58 +0000467 BarEntry);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000468 PtrToFooMem.addValue(Alloc, dwarf::DW_AT_containing_type,
469 dwarf::DW_FORM_ref4, BarEntry);
David Blaikief1962082013-10-22 18:14:41 +0000470
David Blaikie172515f2014-04-25 19:33:43 +0000471 DIEEntry PtrToFooMemRef(PtrToFooMem);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000472 Mem->addValue(Alloc, dwarf::DW_AT_type, dwarf::DW_FORM_ref4,
473 PtrToFooMemRef);
David Blaikief1962082013-10-22 18:14:41 +0000474
David Blaikie12d52242014-04-25 20:00:34 +0000475 Foo.addChild(std::move(Mem));
David Blaikief1962082013-10-22 18:14:41 +0000476
David Blaikie377e8322013-10-24 18:29:03 +0000477 MD5ResDef = DIEHash().computeTypeSignature(Foo);
David Blaikief1962082013-10-22 18:14:41 +0000478 }
479 // FIXME: This seems to be a bug in the DWARF type hashing specification that
480 // only uses the brief name hashing for types referenced via DW_AT_type. In
481 // this case the type is referenced via DW_AT_containing_type and full hashing
482 // causes a hash to differ when the containing type is a declaration in one TU
483 // and a definition in another.
484 ASSERT_NE(MD5ResDef, MD5ResDecl);
485}
David Blaikie1d7d8da2013-10-24 17:53:58 +0000486
487// struct { } a;
488// struct foo { decltype(a) mem; };
Duncan P. N. Exon Smith24ad5c92015-05-24 16:40:47 +0000489TEST_F(DIEHashTest, RefUnnamedType) {
David Blaikie1d7d8da2013-10-24 17:53:58 +0000490 DIEInteger Zero(0);
491 DIEInteger One(1);
492 DIEInteger Eight(8);
Duncan P. N. Exon Smith24ad5c92015-05-24 16:40:47 +0000493 DIEString FooStr = getString("foo");
494 DIEString MemStr = getString("mem");
David Blaikie1d7d8da2013-10-24 17:53:58 +0000495
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +0000496 DIE &Unnamed = *DIE::get(Alloc, dwarf::DW_TAG_structure_type);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000497 Unnamed.addValue(Alloc, dwarf::DW_AT_byte_size, dwarf::DW_FORM_data1, One);
David Blaikie1d7d8da2013-10-24 17:53:58 +0000498
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +0000499 DIE &Foo = *DIE::get(Alloc, dwarf::DW_TAG_structure_type);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000500 Foo.addValue(Alloc, dwarf::DW_AT_byte_size, dwarf::DW_FORM_data1, Eight);
501 Foo.addValue(Alloc, dwarf::DW_AT_name, dwarf::DW_FORM_strp, FooStr);
David Blaikie1d7d8da2013-10-24 17:53:58 +0000502
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +0000503 auto Mem = DIE::get(Alloc, dwarf::DW_TAG_member);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000504 Mem->addValue(Alloc, dwarf::DW_AT_name, dwarf::DW_FORM_strp, MemStr);
505 Mem->addValue(Alloc, dwarf::DW_AT_data_member_location, dwarf::DW_FORM_data1,
506 Zero);
David Blaikie1d7d8da2013-10-24 17:53:58 +0000507
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +0000508 DIE &UnnamedPtr = *DIE::get(Alloc, dwarf::DW_TAG_pointer_type);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000509 UnnamedPtr.addValue(Alloc, dwarf::DW_AT_byte_size, dwarf::DW_FORM_data1,
510 Eight);
David Blaikie172515f2014-04-25 19:33:43 +0000511 DIEEntry UnnamedRef(Unnamed);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000512 UnnamedPtr.addValue(Alloc, dwarf::DW_AT_type, dwarf::DW_FORM_ref4,
513 UnnamedRef);
David Blaikie1d7d8da2013-10-24 17:53:58 +0000514
David Blaikie172515f2014-04-25 19:33:43 +0000515 DIEEntry UnnamedPtrRef(UnnamedPtr);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000516 Mem->addValue(Alloc, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, UnnamedPtrRef);
David Blaikie1d7d8da2013-10-24 17:53:58 +0000517
David Blaikie12d52242014-04-25 20:00:34 +0000518 Foo.addChild(std::move(Mem));
David Blaikie1d7d8da2013-10-24 17:53:58 +0000519
David Blaikie377e8322013-10-24 18:29:03 +0000520 uint64_t MD5Res = DIEHash().computeTypeSignature(Foo);
David Blaikie1d7d8da2013-10-24 17:53:58 +0000521
522 ASSERT_EQ(0x954e026f01c02529ULL, MD5Res);
523}
David Blaikiea9546182013-10-25 18:38:43 +0000524
Eric Christopher5ce9d652014-01-31 20:02:55 +0000525// struct { struct foo { }; };
Duncan P. N. Exon Smith24ad5c92015-05-24 16:40:47 +0000526TEST_F(DIEHashTest, NestedType) {
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +0000527 DIE &Unnamed = *DIE::get(Alloc, dwarf::DW_TAG_structure_type);
David Blaikiea9546182013-10-25 18:38:43 +0000528 DIEInteger One(1);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000529 Unnamed.addValue(Alloc, dwarf::DW_AT_byte_size, dwarf::DW_FORM_data1, One);
David Blaikiea9546182013-10-25 18:38:43 +0000530
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +0000531 auto Foo = DIE::get(Alloc, dwarf::DW_TAG_structure_type);
Duncan P. N. Exon Smith24ad5c92015-05-24 16:40:47 +0000532 DIEString FooStr = getString("foo");
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000533 Foo->addValue(Alloc, dwarf::DW_AT_name, dwarf::DW_FORM_strp, FooStr);
534 Foo->addValue(Alloc, dwarf::DW_AT_byte_size, dwarf::DW_FORM_data1, One);
David Blaikiea9546182013-10-25 18:38:43 +0000535
David Blaikie12d52242014-04-25 20:00:34 +0000536 Unnamed.addChild(std::move(Foo));
David Blaikiea9546182013-10-25 18:38:43 +0000537
538 uint64_t MD5Res = DIEHash().computeTypeSignature(Unnamed);
539
540 // The exact same hash GCC produces for this DIE.
541 ASSERT_EQ(0xde8a3b7b43807f4aULL, MD5Res);
542}
David Blaikie7c0c2e52013-10-25 20:04:25 +0000543
544// struct { static void func(); };
Duncan P. N. Exon Smith24ad5c92015-05-24 16:40:47 +0000545TEST_F(DIEHashTest, MemberFunc) {
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +0000546 DIE &Unnamed = *DIE::get(Alloc, dwarf::DW_TAG_structure_type);
David Blaikie7c0c2e52013-10-25 20:04:25 +0000547 DIEInteger One(1);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000548 Unnamed.addValue(Alloc, dwarf::DW_AT_byte_size, dwarf::DW_FORM_data1, One);
David Blaikie7c0c2e52013-10-25 20:04:25 +0000549
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +0000550 auto Func = DIE::get(Alloc, dwarf::DW_TAG_subprogram);
Duncan P. N. Exon Smith24ad5c92015-05-24 16:40:47 +0000551 DIEString FuncStr = getString("func");
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000552 Func->addValue(Alloc, dwarf::DW_AT_name, dwarf::DW_FORM_strp, FuncStr);
David Blaikie7c0c2e52013-10-25 20:04:25 +0000553
David Blaikie12d52242014-04-25 20:00:34 +0000554 Unnamed.addChild(std::move(Func));
David Blaikie7c0c2e52013-10-25 20:04:25 +0000555
556 uint64_t MD5Res = DIEHash().computeTypeSignature(Unnamed);
557
558 // The exact same hash GCC produces for this DIE.
559 ASSERT_EQ(0xd36a1b6dfb604ba0ULL, MD5Res);
560}
Eric Christopher5ff4b202014-01-31 20:02:58 +0000561
562// struct A {
563// static void func();
564// };
Duncan P. N. Exon Smith24ad5c92015-05-24 16:40:47 +0000565TEST_F(DIEHashTest, MemberFuncFlag) {
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +0000566 DIE &A = *DIE::get(Alloc, dwarf::DW_TAG_structure_type);
Eric Christopher5ff4b202014-01-31 20:02:58 +0000567 DIEInteger One(1);
Duncan P. N. Exon Smith24ad5c92015-05-24 16:40:47 +0000568 DIEString AStr = getString("A");
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000569 A.addValue(Alloc, dwarf::DW_AT_name, dwarf::DW_FORM_strp, AStr);
570 A.addValue(Alloc, dwarf::DW_AT_byte_size, dwarf::DW_FORM_data1, One);
571 A.addValue(Alloc, dwarf::DW_AT_decl_file, dwarf::DW_FORM_data1, One);
572 A.addValue(Alloc, dwarf::DW_AT_decl_line, dwarf::DW_FORM_data1, One);
Eric Christopher5ff4b202014-01-31 20:02:58 +0000573
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +0000574 auto Func = DIE::get(Alloc, dwarf::DW_TAG_subprogram);
Duncan P. N. Exon Smith24ad5c92015-05-24 16:40:47 +0000575 DIEString FuncStr = getString("func");
576 DIEString FuncLinkage = getString("_ZN1A4funcEv");
Eric Christopher5ff4b202014-01-31 20:02:58 +0000577 DIEInteger Two(2);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000578 Func->addValue(Alloc, dwarf::DW_AT_external, dwarf::DW_FORM_flag_present,
579 One);
580 Func->addValue(Alloc, dwarf::DW_AT_name, dwarf::DW_FORM_strp, FuncStr);
581 Func->addValue(Alloc, dwarf::DW_AT_decl_file, dwarf::DW_FORM_data1, One);
582 Func->addValue(Alloc, dwarf::DW_AT_decl_line, dwarf::DW_FORM_data1, Two);
583 Func->addValue(Alloc, dwarf::DW_AT_linkage_name, dwarf::DW_FORM_strp,
584 FuncLinkage);
585 Func->addValue(Alloc, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag_present,
586 One);
Eric Christopher5ff4b202014-01-31 20:02:58 +0000587
David Blaikie12d52242014-04-25 20:00:34 +0000588 A.addChild(std::move(Func));
Eric Christopher5ff4b202014-01-31 20:02:58 +0000589
590 uint64_t MD5Res = DIEHash().computeTypeSignature(A);
591
592 // The exact same hash GCC produces for this DIE.
593 ASSERT_EQ(0x8f78211ddce3df10ULL, MD5Res);
594}
Eric Christopherf5334f22014-02-20 00:54:40 +0000595
596// Derived from:
597// struct A {
Eric Christopherd613f182014-02-20 00:59:17 +0000598// const static int PI = -3;
Eric Christopherf5334f22014-02-20 00:54:40 +0000599// };
600// A a;
Duncan P. N. Exon Smith24ad5c92015-05-24 16:40:47 +0000601TEST_F(DIEHashTest, MemberSdata) {
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +0000602 DIE &A = *DIE::get(Alloc, dwarf::DW_TAG_structure_type);
Eric Christopherf5334f22014-02-20 00:54:40 +0000603 DIEInteger One(1);
Duncan P. N. Exon Smith24ad5c92015-05-24 16:40:47 +0000604 DIEString AStr = getString("A");
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000605 A.addValue(Alloc, dwarf::DW_AT_name, dwarf::DW_FORM_strp, AStr);
606 A.addValue(Alloc, dwarf::DW_AT_byte_size, dwarf::DW_FORM_data1, One);
607 A.addValue(Alloc, dwarf::DW_AT_decl_file, dwarf::DW_FORM_data1, One);
608 A.addValue(Alloc, dwarf::DW_AT_decl_line, dwarf::DW_FORM_data1, One);
Eric Christopherf5334f22014-02-20 00:54:40 +0000609
610 DIEInteger Four(4);
611 DIEInteger Five(5);
Duncan P. N. Exon Smith24ad5c92015-05-24 16:40:47 +0000612 DIEString FStr = getString("int");
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +0000613 DIE &IntTyDIE = *DIE::get(Alloc, dwarf::DW_TAG_base_type);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000614 IntTyDIE.addValue(Alloc, dwarf::DW_AT_byte_size, dwarf::DW_FORM_data1, Four);
615 IntTyDIE.addValue(Alloc, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1, Five);
616 IntTyDIE.addValue(Alloc, dwarf::DW_AT_name, dwarf::DW_FORM_strp, FStr);
Eric Christopherf5334f22014-02-20 00:54:40 +0000617
David Blaikie172515f2014-04-25 19:33:43 +0000618 DIEEntry IntTy(IntTyDIE);
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +0000619 auto PITyDIE = DIE::get(Alloc, dwarf::DW_TAG_const_type);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000620 PITyDIE->addValue(Alloc, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IntTy);
Eric Christopherf5334f22014-02-20 00:54:40 +0000621
David Blaikie172515f2014-04-25 19:33:43 +0000622 DIEEntry PITy(*PITyDIE);
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +0000623 auto PI = DIE::get(Alloc, dwarf::DW_TAG_member);
Duncan P. N. Exon Smith24ad5c92015-05-24 16:40:47 +0000624 DIEString PIStr = getString("PI");
Eric Christopherf5334f22014-02-20 00:54:40 +0000625 DIEInteger Two(2);
626 DIEInteger NegThree(-3);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000627 PI->addValue(Alloc, dwarf::DW_AT_name, dwarf::DW_FORM_strp, PIStr);
628 PI->addValue(Alloc, dwarf::DW_AT_decl_file, dwarf::DW_FORM_data1, One);
629 PI->addValue(Alloc, dwarf::DW_AT_decl_line, dwarf::DW_FORM_data1, Two);
630 PI->addValue(Alloc, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, PITy);
631 PI->addValue(Alloc, dwarf::DW_AT_external, dwarf::DW_FORM_flag_present, One);
632 PI->addValue(Alloc, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag_present,
633 One);
634 PI->addValue(Alloc, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, NegThree);
Eric Christopherf5334f22014-02-20 00:54:40 +0000635
David Blaikie12d52242014-04-25 20:00:34 +0000636 A.addChild(std::move(PI));
Eric Christopherf5334f22014-02-20 00:54:40 +0000637
Eric Christopherf5334f22014-02-20 00:54:40 +0000638 uint64_t MD5Res = DIEHash().computeTypeSignature(A);
639 ASSERT_EQ(0x9a216000dd3788a7ULL, MD5Res);
640}
Eric Christopher31dd38e2014-02-20 02:50:45 +0000641
642// Derived from:
643// struct A {
644// const static float PI = 3.14;
645// };
646// A a;
Duncan P. N. Exon Smith24ad5c92015-05-24 16:40:47 +0000647TEST_F(DIEHashTest, MemberBlock) {
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +0000648 DIE &A = *DIE::get(Alloc, dwarf::DW_TAG_structure_type);
Eric Christopher31dd38e2014-02-20 02:50:45 +0000649 DIEInteger One(1);
Duncan P. N. Exon Smith24ad5c92015-05-24 16:40:47 +0000650 DIEString AStr = getString("A");
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000651 A.addValue(Alloc, dwarf::DW_AT_name, dwarf::DW_FORM_strp, AStr);
652 A.addValue(Alloc, dwarf::DW_AT_byte_size, dwarf::DW_FORM_data1, One);
653 A.addValue(Alloc, dwarf::DW_AT_decl_file, dwarf::DW_FORM_data1, One);
654 A.addValue(Alloc, dwarf::DW_AT_decl_line, dwarf::DW_FORM_data1, One);
Eric Christopher31dd38e2014-02-20 02:50:45 +0000655
656 DIEInteger Four(4);
Duncan P. N. Exon Smith24ad5c92015-05-24 16:40:47 +0000657 DIEString FStr = getString("float");
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +0000658 auto FloatTyDIE = DIE::get(Alloc, dwarf::DW_TAG_base_type);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000659 FloatTyDIE->addValue(Alloc, dwarf::DW_AT_byte_size, dwarf::DW_FORM_data1,
660 Four);
661 FloatTyDIE->addValue(Alloc, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
662 Four);
663 FloatTyDIE->addValue(Alloc, dwarf::DW_AT_name, dwarf::DW_FORM_strp, FStr);
David Blaikie172515f2014-04-25 19:33:43 +0000664 DIEEntry FloatTy(*FloatTyDIE);
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +0000665 auto PITyDIE = DIE::get(Alloc, dwarf::DW_TAG_const_type);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000666 PITyDIE->addValue(Alloc, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, FloatTy);
Eric Christopher31dd38e2014-02-20 02:50:45 +0000667
David Blaikie172515f2014-04-25 19:33:43 +0000668 DIEEntry PITy(*PITyDIE);
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +0000669 auto PI = DIE::get(Alloc, dwarf::DW_TAG_member);
Duncan P. N. Exon Smith24ad5c92015-05-24 16:40:47 +0000670 DIEString PIStr = getString("PI");
Eric Christopher31dd38e2014-02-20 02:50:45 +0000671 DIEInteger Two(2);
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000672 PI->addValue(Alloc, dwarf::DW_AT_name, dwarf::DW_FORM_strp, PIStr);
673 PI->addValue(Alloc, dwarf::DW_AT_decl_file, dwarf::DW_FORM_data1, One);
674 PI->addValue(Alloc, dwarf::DW_AT_decl_line, dwarf::DW_FORM_data1, Two);
675 PI->addValue(Alloc, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, PITy);
676 PI->addValue(Alloc, dwarf::DW_AT_external, dwarf::DW_FORM_flag_present, One);
677 PI->addValue(Alloc, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag_present,
678 One);
Eric Christopher31dd38e2014-02-20 02:50:45 +0000679
David Blaikie5d71d872014-04-25 17:07:55 +0000680 DIEBlock PIBlock;
Eric Christopher31dd38e2014-02-20 02:50:45 +0000681 DIEInteger Blk1(0xc3);
682 DIEInteger Blk2(0xf5);
683 DIEInteger Blk3(0x48);
684 DIEInteger Blk4(0x40);
685
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000686 PIBlock.addValue(Alloc, (dwarf::Attribute)0, dwarf::DW_FORM_data1, Blk1);
687 PIBlock.addValue(Alloc, (dwarf::Attribute)0, dwarf::DW_FORM_data1, Blk2);
688 PIBlock.addValue(Alloc, (dwarf::Attribute)0, dwarf::DW_FORM_data1, Blk3);
689 PIBlock.addValue(Alloc, (dwarf::Attribute)0, dwarf::DW_FORM_data1, Blk4);
Eric Christopher31dd38e2014-02-20 02:50:45 +0000690
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000691 PI->addValue(Alloc, dwarf::DW_AT_const_value, dwarf::DW_FORM_block1,
692 &PIBlock);
Eric Christopher31dd38e2014-02-20 02:50:45 +0000693
David Blaikie12d52242014-04-25 20:00:34 +0000694 A.addChild(std::move(PI));
Eric Christopher31dd38e2014-02-20 02:50:45 +0000695
696 uint64_t MD5Res = DIEHash().computeTypeSignature(A);
697 ASSERT_EQ(0x493af53ad3d3f651ULL, MD5Res);
698}
Eric Christopher800a8762013-09-03 21:57:57 +0000699}