blob: 7d8b024e40af9c435b41a8bc471e2b1289195bb9 [file] [log] [blame]
Eric Christopher800a8762013-09-03 21:57:57 +00001//===- llvm/unittest/DebugInfo/DWARFFormValueTest.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
10#include "../lib/CodeGen/AsmPrinter/DIE.h"
11#include "../lib/CodeGen/AsmPrinter/DIEHash.h"
12#include "llvm/Support/Dwarf.h"
13#include "llvm/Support/Debug.h"
14#include "llvm/Support/Format.h"
15#include "gtest/gtest.h"
16
Eric Christopher800a8762013-09-03 21:57:57 +000017using namespace llvm;
David Blaikiee4df43e2013-10-15 23:00:17 +000018
19namespace {
David Blaikiee46be782013-10-21 20:28:30 +000020TEST(DIEHashTest, Data1) {
Eric Christopher800a8762013-09-03 21:57:57 +000021 DIEHash Hash;
Benjamin Kramer449a88e2013-09-29 11:29:20 +000022 DIE Die(dwarf::DW_TAG_base_type);
23 DIEInteger Size(4);
24 Die.addValue(dwarf::DW_AT_byte_size, dwarf::DW_FORM_data1, &Size);
25 uint64_t MD5Res = Hash.computeTypeSignature(&Die);
David Blaikiec0987082013-10-16 23:36:20 +000026 ASSERT_EQ(0x1AFE116E83701108ULL, MD5Res);
27}
28
David Blaikief1545a22013-10-21 22:36:50 +000029// struct {};
David Blaikiee46be782013-10-21 20:28:30 +000030TEST(DIEHashTest, TrivialType) {
David Blaikie88a68cb2013-10-17 00:10:34 +000031 DIE Unnamed(dwarf::DW_TAG_structure_type);
David Blaikiec0987082013-10-16 23:36:20 +000032 DIEInteger One(1);
David Blaikie88a68cb2013-10-17 00:10:34 +000033 Unnamed.addValue(dwarf::DW_AT_byte_size, dwarf::DW_FORM_data1, &One);
David Blaikiec0987082013-10-16 23:36:20 +000034
35 // Line and file number are ignored.
David Blaikie88a68cb2013-10-17 00:10:34 +000036 Unnamed.addValue(dwarf::DW_AT_decl_file, dwarf::DW_FORM_data1, &One);
37 Unnamed.addValue(dwarf::DW_AT_decl_line, dwarf::DW_FORM_data1, &One);
38 uint64_t MD5Res = DIEHash().computeTypeSignature(&Unnamed);
David Blaikiec0987082013-10-16 23:36:20 +000039
40 // The exact same hash GCC produces for this DIE.
41 ASSERT_EQ(0x715305ce6cfd9ad1ULL, MD5Res);
Eric Christopher800a8762013-09-03 21:57:57 +000042}
David Blaikie88a68cb2013-10-17 00:10:34 +000043
David Blaikief1545a22013-10-21 22:36:50 +000044// struct foo { };
David Blaikiee46be782013-10-21 20:28:30 +000045TEST(DIEHashTest, NamedType) {
David Blaikie88a68cb2013-10-17 00:10:34 +000046 DIE Foo(dwarf::DW_TAG_structure_type);
47 DIEInteger One(1);
48 DIEString FooStr(&One, "foo");
49 Foo.addValue(dwarf::DW_AT_name, dwarf::DW_FORM_strp, &FooStr);
50 Foo.addValue(dwarf::DW_AT_byte_size, dwarf::DW_FORM_data1, &One);
51
David Blaikie88a68cb2013-10-17 00:10:34 +000052 uint64_t MD5Res = DIEHash().computeTypeSignature(&Foo);
53
54 // The exact same hash GCC produces for this DIE.
55 ASSERT_EQ(0xd566dbd2ca5265ffULL, MD5Res);
56}
57
David Blaikief1545a22013-10-21 22:36:50 +000058// namespace space { struct foo { }; }
David Blaikiee46be782013-10-21 20:28:30 +000059TEST(DIEHashTest, NamespacedType) {
David Blaikie88a68cb2013-10-17 00:10:34 +000060 DIE CU(dwarf::DW_TAG_compile_unit);
61
62 DIE *Space = new DIE(dwarf::DW_TAG_namespace);
63 DIEInteger One(1);
64 DIEString SpaceStr(&One, "space");
65 Space->addValue(dwarf::DW_AT_name, dwarf::DW_FORM_strp, &SpaceStr);
66 // DW_AT_declaration is ignored.
67 Space->addValue(dwarf::DW_AT_declaration, dwarf::DW_FORM_flag_present, &One);
68 // sibling?
69
70 DIE *Foo = new DIE(dwarf::DW_TAG_structure_type);
71 DIEString FooStr(&One, "foo");
72 Foo->addValue(dwarf::DW_AT_name, dwarf::DW_FORM_strp, &FooStr);
73 Foo->addValue(dwarf::DW_AT_byte_size, dwarf::DW_FORM_data1, &One);
74
David Blaikie88a68cb2013-10-17 00:10:34 +000075 Space->addChild(Foo);
76 CU.addChild(Space);
77
78 uint64_t MD5Res = DIEHash().computeTypeSignature(Foo);
79
80 // The exact same hash GCC produces for this DIE.
81 ASSERT_EQ(0x7b80381fd17f1e33ULL, MD5Res);
82}
David Blaikie47f66d52013-10-17 22:07:09 +000083
David Blaikief1545a22013-10-21 22:36:50 +000084// struct { int member; };
David Blaikiee46be782013-10-21 20:28:30 +000085TEST(DIEHashTest, TypeWithMember) {
David Blaikie47f66d52013-10-17 22:07:09 +000086 DIE Unnamed(dwarf::DW_TAG_structure_type);
87 DIEInteger Four(4);
88 Unnamed.addValue(dwarf::DW_AT_byte_size, dwarf::DW_FORM_data1, &Four);
89
90 DIE *Member = new DIE(dwarf::DW_TAG_member);
91 DIEString MemberStr(&Four, "member");
92 Member->addValue(dwarf::DW_AT_name, dwarf::DW_FORM_strp, &MemberStr);
David Blaikie47f66d52013-10-17 22:07:09 +000093 DIEInteger Zero(0);
94 Member->addValue(dwarf::DW_AT_data_member_location, dwarf::DW_FORM_data1, &Zero);
95
96 Unnamed.addChild(Member);
97
98 DIE Int(dwarf::DW_TAG_base_type);
99 DIEString IntStr(&Four, "int");
100 Int.addValue(dwarf::DW_AT_name, dwarf::DW_FORM_strp, &IntStr);
101 Int.addValue(dwarf::DW_AT_byte_size, dwarf::DW_FORM_data1, &Four);
102 DIEInteger Five(5);
103 Int.addValue(dwarf::DW_AT_encoding, dwarf::DW_FORM_data1, &Five);
104
105 DIEEntry IntRef(&Int);
106 Member->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, &IntRef);
107
108 uint64_t MD5Res = DIEHash().computeTypeSignature(&Unnamed);
109
110 ASSERT_EQ(0x5646aa436b7e07c6ULL, MD5Res);
111}
David Blaikie3baa3c32013-10-21 18:59:40 +0000112
David Blaikief1545a22013-10-21 22:36:50 +0000113// struct foo { int mem1, mem2; };
David Blaikiee46be782013-10-21 20:28:30 +0000114TEST(DIEHashTest, ReusedType) {
David Blaikie3baa3c32013-10-21 18:59:40 +0000115 DIE Unnamed(dwarf::DW_TAG_structure_type);
116 DIEInteger Eight(8);
117 Unnamed.addValue(dwarf::DW_AT_byte_size, dwarf::DW_FORM_data1, &Eight);
118
119 DIE *Mem1 = new DIE(dwarf::DW_TAG_member);
120 DIEInteger Four(4);
121 DIEString Mem1Str(&Four, "mem1");
122 Mem1->addValue(dwarf::DW_AT_name, dwarf::DW_FORM_strp, &Mem1Str);
123 DIEInteger Zero(0);
124 Mem1->addValue(dwarf::DW_AT_data_member_location, dwarf::DW_FORM_data1, &Zero);
125
126 Unnamed.addChild(Mem1);
127
128 DIE *Mem2 = new DIE(dwarf::DW_TAG_member);
129 DIEString Mem2Str(&Four, "mem2");
130 Mem2->addValue(dwarf::DW_AT_name, dwarf::DW_FORM_strp, &Mem2Str);
131 Mem2->addValue(dwarf::DW_AT_data_member_location, dwarf::DW_FORM_data1, &Four);
132
133 Unnamed.addChild(Mem2);
134
135 DIE Int(dwarf::DW_TAG_base_type);
136 DIEString IntStr(&Four, "int");
137 Int.addValue(dwarf::DW_AT_name, dwarf::DW_FORM_strp, &IntStr);
138 Int.addValue(dwarf::DW_AT_byte_size, dwarf::DW_FORM_data1, &Four);
139 DIEInteger Five(5);
140 Int.addValue(dwarf::DW_AT_encoding, dwarf::DW_FORM_data1, &Five);
141
142 DIEEntry IntRef(&Int);
143 Mem1->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, &IntRef);
144 Mem2->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, &IntRef);
145
146 uint64_t MD5Res = DIEHash().computeTypeSignature(&Unnamed);
147
148 ASSERT_EQ(0x3a7dc3ed7b76b2f8ULL, MD5Res);
149}
150
David Blaikief1545a22013-10-21 22:36:50 +0000151// struct foo { static foo f; };
David Blaikiee46be782013-10-21 20:28:30 +0000152TEST(DIEHashTest, RecursiveType) {
David Blaikie3baa3c32013-10-21 18:59:40 +0000153 DIE Foo(dwarf::DW_TAG_structure_type);
154 DIEInteger One(1);
155 Foo.addValue(dwarf::DW_AT_byte_size, dwarf::DW_FORM_data1, &One);
156 DIEString FooStr(&One, "foo");
157 Foo.addValue(dwarf::DW_AT_name, dwarf::DW_FORM_strp, &FooStr);
158
159 DIE *Mem = new DIE(dwarf::DW_TAG_member);
160 DIEString MemStr(&One, "mem");
161 Mem->addValue(dwarf::DW_AT_name, dwarf::DW_FORM_strp, &MemStr);
162 DIEEntry FooRef(&Foo);
163 Mem->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, &FooRef);
164 // DW_AT_external and DW_AT_declaration are ignored anyway, so skip them.
165
166 Foo.addChild(Mem);
167
168 uint64_t MD5Res = DIEHash().computeTypeSignature(&Foo);
169
170 ASSERT_EQ(0x73d8b25aef227b06ULL, MD5Res);
171}
David Blaikief1545a22013-10-21 22:36:50 +0000172
173// struct foo { foo *mem; };
174TEST(DIEHashTest, Pointer) {
175 DIE Foo(dwarf::DW_TAG_structure_type);
176 DIEInteger Eight(8);
177 Foo.addValue(dwarf::DW_AT_byte_size, dwarf::DW_FORM_data1, &Eight);
178 DIEString FooStr(&Eight, "foo");
179 Foo.addValue(dwarf::DW_AT_name, dwarf::DW_FORM_strp, &FooStr);
180
181 DIE *Mem = new DIE(dwarf::DW_TAG_member);
182 DIEString MemStr(&Eight, "mem");
183 Mem->addValue(dwarf::DW_AT_name, dwarf::DW_FORM_strp, &MemStr);
184 DIEInteger Zero(0);
185 Mem->addValue(dwarf::DW_AT_data_member_location, dwarf::DW_FORM_data1, &Zero);
186
187 DIE FooPtr(dwarf::DW_TAG_pointer_type);
188 FooPtr.addValue(dwarf::DW_AT_byte_size, dwarf::DW_FORM_data1, &Eight);
189 DIEEntry FooRef(&Foo);
190 FooPtr.addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, &FooRef);
191
192 DIEEntry FooPtrRef(&FooPtr);
193 Mem->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, &FooPtrRef);
194
195 Foo.addChild(Mem);
196
197 uint64_t MD5Res = DIEHash().computeTypeSignature(&Foo);
198
199 ASSERT_EQ(0x74ea73862e8708d2ULL, MD5Res);
200}
Eric Christopher800a8762013-09-03 21:57:57 +0000201}