DIEHashTest.cpp revision 47f66d5a756d4eaeba9082ff4c82023a213daf45
1800a8761285a239bb82f7d1883a8398815cd2d8fEric Christopher//===- llvm/unittest/DebugInfo/DWARFFormValueTest.cpp ---------------------===//
2800a8761285a239bb82f7d1883a8398815cd2d8fEric Christopher//
3800a8761285a239bb82f7d1883a8398815cd2d8fEric Christopher//                     The LLVM Compiler Infrastructure
4800a8761285a239bb82f7d1883a8398815cd2d8fEric Christopher//
5800a8761285a239bb82f7d1883a8398815cd2d8fEric Christopher// This file is distributed under the University of Illinois Open Source
6800a8761285a239bb82f7d1883a8398815cd2d8fEric Christopher// License. See LICENSE.TXT for details.
7800a8761285a239bb82f7d1883a8398815cd2d8fEric Christopher//
8800a8761285a239bb82f7d1883a8398815cd2d8fEric Christopher//===----------------------------------------------------------------------===//
9800a8761285a239bb82f7d1883a8398815cd2d8fEric Christopher
10800a8761285a239bb82f7d1883a8398815cd2d8fEric Christopher#include "../lib/CodeGen/AsmPrinter/DIE.h"
11800a8761285a239bb82f7d1883a8398815cd2d8fEric Christopher#include "../lib/CodeGen/AsmPrinter/DIEHash.h"
12800a8761285a239bb82f7d1883a8398815cd2d8fEric Christopher#include "llvm/Support/Dwarf.h"
13800a8761285a239bb82f7d1883a8398815cd2d8fEric Christopher#include "llvm/Support/Debug.h"
14800a8761285a239bb82f7d1883a8398815cd2d8fEric Christopher#include "llvm/Support/Format.h"
15800a8761285a239bb82f7d1883a8398815cd2d8fEric Christopher#include "gtest/gtest.h"
16800a8761285a239bb82f7d1883a8398815cd2d8fEric Christopher
17800a8761285a239bb82f7d1883a8398815cd2d8fEric Christopherusing namespace llvm;
18e4df43e36dbdca444a6e812bdd2c9ea93360a2ecDavid Blaikie
19e4df43e36dbdca444a6e812bdd2c9ea93360a2ecDavid Blaikienamespace {
2088a68cbbb57aa9caef60dbd2f63d908983e77465David BlaikieTEST(Data1, DIEHash) {
21800a8761285a239bb82f7d1883a8398815cd2d8fEric Christopher  DIEHash Hash;
22449a88e9a6279cc016d795e86e73c026675ab929Benjamin Kramer  DIE Die(dwarf::DW_TAG_base_type);
23449a88e9a6279cc016d795e86e73c026675ab929Benjamin Kramer  DIEInteger Size(4);
24449a88e9a6279cc016d795e86e73c026675ab929Benjamin Kramer  Die.addValue(dwarf::DW_AT_byte_size, dwarf::DW_FORM_data1, &Size);
25449a88e9a6279cc016d795e86e73c026675ab929Benjamin Kramer  uint64_t MD5Res = Hash.computeTypeSignature(&Die);
26c0987082206d202c20ebe1d528c1b28a875160acDavid Blaikie  ASSERT_EQ(0x1AFE116E83701108ULL, MD5Res);
27c0987082206d202c20ebe1d528c1b28a875160acDavid Blaikie}
28c0987082206d202c20ebe1d528c1b28a875160acDavid Blaikie
2988a68cbbb57aa9caef60dbd2f63d908983e77465David BlaikieTEST(TrivialType, DIEHash) {
30c0987082206d202c20ebe1d528c1b28a875160acDavid Blaikie  // A complete, but simple, type containing no members and defined on the first
31c0987082206d202c20ebe1d528c1b28a875160acDavid Blaikie  // line of a file.
3288a68cbbb57aa9caef60dbd2f63d908983e77465David Blaikie  DIE Unnamed(dwarf::DW_TAG_structure_type);
33c0987082206d202c20ebe1d528c1b28a875160acDavid Blaikie  DIEInteger One(1);
3488a68cbbb57aa9caef60dbd2f63d908983e77465David Blaikie  Unnamed.addValue(dwarf::DW_AT_byte_size, dwarf::DW_FORM_data1, &One);
35c0987082206d202c20ebe1d528c1b28a875160acDavid Blaikie
36c0987082206d202c20ebe1d528c1b28a875160acDavid Blaikie  // Line and file number are ignored.
3788a68cbbb57aa9caef60dbd2f63d908983e77465David Blaikie  Unnamed.addValue(dwarf::DW_AT_decl_file, dwarf::DW_FORM_data1, &One);
3888a68cbbb57aa9caef60dbd2f63d908983e77465David Blaikie  Unnamed.addValue(dwarf::DW_AT_decl_line, dwarf::DW_FORM_data1, &One);
3988a68cbbb57aa9caef60dbd2f63d908983e77465David Blaikie  uint64_t MD5Res = DIEHash().computeTypeSignature(&Unnamed);
40c0987082206d202c20ebe1d528c1b28a875160acDavid Blaikie
41c0987082206d202c20ebe1d528c1b28a875160acDavid Blaikie  // The exact same hash GCC produces for this DIE.
42c0987082206d202c20ebe1d528c1b28a875160acDavid Blaikie  ASSERT_EQ(0x715305ce6cfd9ad1ULL, MD5Res);
43800a8761285a239bb82f7d1883a8398815cd2d8fEric Christopher}
4488a68cbbb57aa9caef60dbd2f63d908983e77465David Blaikie
4588a68cbbb57aa9caef60dbd2f63d908983e77465David BlaikieTEST(NamedType, DIEHash) {
4688a68cbbb57aa9caef60dbd2f63d908983e77465David Blaikie  // A complete named type containing no members and defined on the first line
4788a68cbbb57aa9caef60dbd2f63d908983e77465David Blaikie  // of a file.
4888a68cbbb57aa9caef60dbd2f63d908983e77465David Blaikie  DIE Foo(dwarf::DW_TAG_structure_type);
4988a68cbbb57aa9caef60dbd2f63d908983e77465David Blaikie  DIEInteger One(1);
5088a68cbbb57aa9caef60dbd2f63d908983e77465David Blaikie  DIEString FooStr(&One, "foo");
5188a68cbbb57aa9caef60dbd2f63d908983e77465David Blaikie  Foo.addValue(dwarf::DW_AT_name, dwarf::DW_FORM_strp, &FooStr);
5288a68cbbb57aa9caef60dbd2f63d908983e77465David Blaikie  Foo.addValue(dwarf::DW_AT_byte_size, dwarf::DW_FORM_data1, &One);
5388a68cbbb57aa9caef60dbd2f63d908983e77465David Blaikie
5488a68cbbb57aa9caef60dbd2f63d908983e77465David Blaikie  uint64_t MD5Res = DIEHash().computeTypeSignature(&Foo);
5588a68cbbb57aa9caef60dbd2f63d908983e77465David Blaikie
5688a68cbbb57aa9caef60dbd2f63d908983e77465David Blaikie  // The exact same hash GCC produces for this DIE.
5788a68cbbb57aa9caef60dbd2f63d908983e77465David Blaikie  ASSERT_EQ(0xd566dbd2ca5265ffULL, MD5Res);
5888a68cbbb57aa9caef60dbd2f63d908983e77465David Blaikie}
5988a68cbbb57aa9caef60dbd2f63d908983e77465David Blaikie
6088a68cbbb57aa9caef60dbd2f63d908983e77465David BlaikieTEST(NamespacedType, DIEHash) {
6188a68cbbb57aa9caef60dbd2f63d908983e77465David Blaikie  // A complete named type containing no members and defined on the first line
6288a68cbbb57aa9caef60dbd2f63d908983e77465David Blaikie  // of a file.
6388a68cbbb57aa9caef60dbd2f63d908983e77465David Blaikie  DIE CU(dwarf::DW_TAG_compile_unit);
6488a68cbbb57aa9caef60dbd2f63d908983e77465David Blaikie
6588a68cbbb57aa9caef60dbd2f63d908983e77465David Blaikie  DIE *Space = new DIE(dwarf::DW_TAG_namespace);
6688a68cbbb57aa9caef60dbd2f63d908983e77465David Blaikie  DIEInteger One(1);
6788a68cbbb57aa9caef60dbd2f63d908983e77465David Blaikie  DIEString SpaceStr(&One, "space");
6888a68cbbb57aa9caef60dbd2f63d908983e77465David Blaikie  Space->addValue(dwarf::DW_AT_name, dwarf::DW_FORM_strp, &SpaceStr);
6988a68cbbb57aa9caef60dbd2f63d908983e77465David Blaikie  // DW_AT_declaration is ignored.
7088a68cbbb57aa9caef60dbd2f63d908983e77465David Blaikie  Space->addValue(dwarf::DW_AT_declaration, dwarf::DW_FORM_flag_present, &One);
7188a68cbbb57aa9caef60dbd2f63d908983e77465David Blaikie  // sibling?
7288a68cbbb57aa9caef60dbd2f63d908983e77465David Blaikie
7388a68cbbb57aa9caef60dbd2f63d908983e77465David Blaikie  DIE *Foo = new DIE(dwarf::DW_TAG_structure_type);
7488a68cbbb57aa9caef60dbd2f63d908983e77465David Blaikie  DIEString FooStr(&One, "foo");
7588a68cbbb57aa9caef60dbd2f63d908983e77465David Blaikie  Foo->addValue(dwarf::DW_AT_name, dwarf::DW_FORM_strp, &FooStr);
7688a68cbbb57aa9caef60dbd2f63d908983e77465David Blaikie  Foo->addValue(dwarf::DW_AT_byte_size, dwarf::DW_FORM_data1, &One);
7788a68cbbb57aa9caef60dbd2f63d908983e77465David Blaikie
7888a68cbbb57aa9caef60dbd2f63d908983e77465David Blaikie  Space->addChild(Foo);
7988a68cbbb57aa9caef60dbd2f63d908983e77465David Blaikie  CU.addChild(Space);
8088a68cbbb57aa9caef60dbd2f63d908983e77465David Blaikie
8188a68cbbb57aa9caef60dbd2f63d908983e77465David Blaikie  uint64_t MD5Res = DIEHash().computeTypeSignature(Foo);
8288a68cbbb57aa9caef60dbd2f63d908983e77465David Blaikie
8388a68cbbb57aa9caef60dbd2f63d908983e77465David Blaikie  // The exact same hash GCC produces for this DIE.
8488a68cbbb57aa9caef60dbd2f63d908983e77465David Blaikie  ASSERT_EQ(0x7b80381fd17f1e33ULL, MD5Res);
8588a68cbbb57aa9caef60dbd2f63d908983e77465David Blaikie}
8647f66d5a756d4eaeba9082ff4c82023a213daf45David Blaikie
8747f66d5a756d4eaeba9082ff4c82023a213daf45David BlaikieTEST(TypeWithMember, DIEHash) {
8847f66d5a756d4eaeba9082ff4c82023a213daf45David Blaikie  DIE Unnamed(dwarf::DW_TAG_structure_type);
8947f66d5a756d4eaeba9082ff4c82023a213daf45David Blaikie  DIEInteger Four(4);
9047f66d5a756d4eaeba9082ff4c82023a213daf45David Blaikie  Unnamed.addValue(dwarf::DW_AT_byte_size, dwarf::DW_FORM_data1, &Four);
9147f66d5a756d4eaeba9082ff4c82023a213daf45David Blaikie
9247f66d5a756d4eaeba9082ff4c82023a213daf45David Blaikie  DIE *Member = new DIE(dwarf::DW_TAG_member);
9347f66d5a756d4eaeba9082ff4c82023a213daf45David Blaikie  DIEString MemberStr(&Four, "member");
9447f66d5a756d4eaeba9082ff4c82023a213daf45David Blaikie  Member->addValue(dwarf::DW_AT_name, dwarf::DW_FORM_strp, &MemberStr);
9547f66d5a756d4eaeba9082ff4c82023a213daf45David Blaikie  // type
9647f66d5a756d4eaeba9082ff4c82023a213daf45David Blaikie  DIEInteger Zero(0);
9747f66d5a756d4eaeba9082ff4c82023a213daf45David Blaikie  Member->addValue(dwarf::DW_AT_data_member_location, dwarf::DW_FORM_data1, &Zero);
9847f66d5a756d4eaeba9082ff4c82023a213daf45David Blaikie
9947f66d5a756d4eaeba9082ff4c82023a213daf45David Blaikie  Unnamed.addChild(Member);
10047f66d5a756d4eaeba9082ff4c82023a213daf45David Blaikie
10147f66d5a756d4eaeba9082ff4c82023a213daf45David Blaikie  DIE Int(dwarf::DW_TAG_base_type);
10247f66d5a756d4eaeba9082ff4c82023a213daf45David Blaikie  DIEString IntStr(&Four, "int");
10347f66d5a756d4eaeba9082ff4c82023a213daf45David Blaikie  Int.addValue(dwarf::DW_AT_name, dwarf::DW_FORM_strp, &IntStr);
10447f66d5a756d4eaeba9082ff4c82023a213daf45David Blaikie  Int.addValue(dwarf::DW_AT_byte_size, dwarf::DW_FORM_data1, &Four);
10547f66d5a756d4eaeba9082ff4c82023a213daf45David Blaikie  DIEInteger Five(5);
10647f66d5a756d4eaeba9082ff4c82023a213daf45David Blaikie  Int.addValue(dwarf::DW_AT_encoding, dwarf::DW_FORM_data1, &Five);
10747f66d5a756d4eaeba9082ff4c82023a213daf45David Blaikie
10847f66d5a756d4eaeba9082ff4c82023a213daf45David Blaikie  DIEEntry IntRef(&Int);
10947f66d5a756d4eaeba9082ff4c82023a213daf45David Blaikie  Member->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, &IntRef);
11047f66d5a756d4eaeba9082ff4c82023a213daf45David Blaikie
11147f66d5a756d4eaeba9082ff4c82023a213daf45David Blaikie  uint64_t MD5Res = DIEHash().computeTypeSignature(&Unnamed);
11247f66d5a756d4eaeba9082ff4c82023a213daf45David Blaikie
11347f66d5a756d4eaeba9082ff4c82023a213daf45David Blaikie  ASSERT_EQ(0x5646aa436b7e07c6ULL, MD5Res);
11447f66d5a756d4eaeba9082ff4c82023a213daf45David Blaikie}
115800a8761285a239bb82f7d1883a8398815cd2d8fEric Christopher}
116