1 //===- unittest/BinaryFormat/DwarfTest.cpp - Dwarf support tests ----------===//
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 "llvm/BinaryFormat/Dwarf.h"
11 #include "llvm/ADT/StringRef.h"
12 #include "llvm/Support/FormatVariadic.h"
13 #include "gtest/gtest.h"
14
15 using namespace llvm;
16 using namespace llvm::dwarf;
17
18 namespace {
19
TEST(DwarfTest,TagStringOnInvalid)20 TEST(DwarfTest, TagStringOnInvalid) {
21 // This is invalid, so it shouldn't be stringified.
22 EXPECT_EQ(StringRef(), TagString(DW_TAG_invalid));
23
24 // These aren't really tags: they describe ranges within tags. They
25 // shouldn't be stringified either.
26 EXPECT_EQ(StringRef(), TagString(DW_TAG_lo_user));
27 EXPECT_EQ(StringRef(), TagString(DW_TAG_hi_user));
28 EXPECT_EQ(StringRef(), TagString(DW_TAG_user_base));
29 }
30
TEST(DwarfTest,getTag)31 TEST(DwarfTest, getTag) {
32 // A couple of valid tags.
33 EXPECT_EQ(DW_TAG_array_type, getTag("DW_TAG_array_type"));
34 EXPECT_EQ(DW_TAG_module, getTag("DW_TAG_module"));
35
36 // Invalid tags.
37 EXPECT_EQ(DW_TAG_invalid, getTag("DW_TAG_invalid"));
38 EXPECT_EQ(DW_TAG_invalid, getTag("DW_TAG_madeuptag"));
39 EXPECT_EQ(DW_TAG_invalid, getTag("something else"));
40
41 // Tag range markers should not be recognized.
42 EXPECT_EQ(DW_TAG_invalid, getTag("DW_TAG_lo_user"));
43 EXPECT_EQ(DW_TAG_invalid, getTag("DW_TAG_hi_user"));
44 EXPECT_EQ(DW_TAG_invalid, getTag("DW_TAG_user_base"));
45 }
46
TEST(DwarfTest,getOperationEncoding)47 TEST(DwarfTest, getOperationEncoding) {
48 // Some valid ops.
49 EXPECT_EQ(DW_OP_deref, getOperationEncoding("DW_OP_deref"));
50 EXPECT_EQ(DW_OP_bit_piece, getOperationEncoding("DW_OP_bit_piece"));
51
52 // Invalid ops.
53 EXPECT_EQ(0u, getOperationEncoding("DW_OP_otherthings"));
54 EXPECT_EQ(0u, getOperationEncoding("other"));
55
56 // Markers shouldn't be recognized.
57 EXPECT_EQ(0u, getOperationEncoding("DW_OP_lo_user"));
58 EXPECT_EQ(0u, getOperationEncoding("DW_OP_hi_user"));
59 }
60
TEST(DwarfTest,LanguageStringOnInvalid)61 TEST(DwarfTest, LanguageStringOnInvalid) {
62 // This is invalid, so it shouldn't be stringified.
63 EXPECT_EQ(StringRef(), LanguageString(0));
64
65 // These aren't really tags: they describe ranges within tags. They
66 // shouldn't be stringified either.
67 EXPECT_EQ(StringRef(), LanguageString(DW_LANG_lo_user));
68 EXPECT_EQ(StringRef(), LanguageString(DW_LANG_hi_user));
69 }
70
TEST(DwarfTest,getLanguage)71 TEST(DwarfTest, getLanguage) {
72 // A couple of valid languages.
73 EXPECT_EQ(DW_LANG_C89, getLanguage("DW_LANG_C89"));
74 EXPECT_EQ(DW_LANG_C_plus_plus_11, getLanguage("DW_LANG_C_plus_plus_11"));
75 EXPECT_EQ(DW_LANG_OCaml, getLanguage("DW_LANG_OCaml"));
76 EXPECT_EQ(DW_LANG_Mips_Assembler, getLanguage("DW_LANG_Mips_Assembler"));
77
78 // Invalid languages.
79 EXPECT_EQ(0u, getLanguage("DW_LANG_invalid"));
80 EXPECT_EQ(0u, getLanguage("DW_TAG_array_type"));
81 EXPECT_EQ(0u, getLanguage("something else"));
82
83 // Language range markers should not be recognized.
84 EXPECT_EQ(0u, getLanguage("DW_LANG_lo_user"));
85 EXPECT_EQ(0u, getLanguage("DW_LANG_hi_user"));
86 }
87
TEST(DwarfTest,AttributeEncodingStringOnInvalid)88 TEST(DwarfTest, AttributeEncodingStringOnInvalid) {
89 // This is invalid, so it shouldn't be stringified.
90 EXPECT_EQ(StringRef(), AttributeEncodingString(0));
91
92 // These aren't really tags: they describe ranges within tags. They
93 // shouldn't be stringified either.
94 EXPECT_EQ(StringRef(), AttributeEncodingString(DW_ATE_lo_user));
95 EXPECT_EQ(StringRef(), AttributeEncodingString(DW_ATE_hi_user));
96 }
97
TEST(DwarfTest,getAttributeEncoding)98 TEST(DwarfTest, getAttributeEncoding) {
99 // A couple of valid languages.
100 EXPECT_EQ(DW_ATE_boolean, getAttributeEncoding("DW_ATE_boolean"));
101 EXPECT_EQ(DW_ATE_imaginary_float,
102 getAttributeEncoding("DW_ATE_imaginary_float"));
103
104 // Invalid languages.
105 EXPECT_EQ(0u, getAttributeEncoding("DW_ATE_invalid"));
106 EXPECT_EQ(0u, getAttributeEncoding("DW_TAG_array_type"));
107 EXPECT_EQ(0u, getAttributeEncoding("something else"));
108
109 // AttributeEncoding range markers should not be recognized.
110 EXPECT_EQ(0u, getAttributeEncoding("DW_ATE_lo_user"));
111 EXPECT_EQ(0u, getAttributeEncoding("DW_ATE_hi_user"));
112 }
113
TEST(DwarfTest,VirtualityString)114 TEST(DwarfTest, VirtualityString) {
115 EXPECT_EQ(StringRef("DW_VIRTUALITY_none"),
116 VirtualityString(DW_VIRTUALITY_none));
117 EXPECT_EQ(StringRef("DW_VIRTUALITY_virtual"),
118 VirtualityString(DW_VIRTUALITY_virtual));
119 EXPECT_EQ(StringRef("DW_VIRTUALITY_pure_virtual"),
120 VirtualityString(DW_VIRTUALITY_pure_virtual));
121
122 // DW_VIRTUALITY_max should be pure virtual.
123 EXPECT_EQ(StringRef("DW_VIRTUALITY_pure_virtual"),
124 VirtualityString(DW_VIRTUALITY_max));
125
126 // Invalid numbers shouldn't be stringified.
127 EXPECT_EQ(StringRef(), VirtualityString(DW_VIRTUALITY_max + 1));
128 EXPECT_EQ(StringRef(), VirtualityString(DW_VIRTUALITY_max + 77));
129 }
130
TEST(DwarfTest,getVirtuality)131 TEST(DwarfTest, getVirtuality) {
132 EXPECT_EQ(DW_VIRTUALITY_none, getVirtuality("DW_VIRTUALITY_none"));
133 EXPECT_EQ(DW_VIRTUALITY_virtual, getVirtuality("DW_VIRTUALITY_virtual"));
134 EXPECT_EQ(DW_VIRTUALITY_pure_virtual,
135 getVirtuality("DW_VIRTUALITY_pure_virtual"));
136
137 // Invalid strings.
138 EXPECT_EQ(DW_VIRTUALITY_invalid, getVirtuality("DW_VIRTUALITY_invalid"));
139 EXPECT_EQ(DW_VIRTUALITY_invalid, getVirtuality("DW_VIRTUALITY_max"));
140 EXPECT_EQ(DW_VIRTUALITY_invalid, getVirtuality("something else"));
141 }
142
TEST(DwarfTest,FixedFormSizes)143 TEST(DwarfTest, FixedFormSizes) {
144 Optional<uint8_t> RefSize;
145 Optional<uint8_t> AddrSize;
146
147 // Test 32 bit DWARF version 2 with 4 byte addresses.
148 FormParams Params_2_4_32 = {2, 4, DWARF32};
149 RefSize = getFixedFormByteSize(DW_FORM_ref_addr, Params_2_4_32);
150 AddrSize = getFixedFormByteSize(DW_FORM_ref_addr, Params_2_4_32);
151 EXPECT_TRUE(RefSize.hasValue());
152 EXPECT_TRUE(AddrSize.hasValue());
153 EXPECT_EQ(*RefSize, *AddrSize);
154
155 // Test 32 bit DWARF version 2 with 8 byte addresses.
156 FormParams Params_2_8_32 = {2, 8, DWARF32};
157 RefSize = getFixedFormByteSize(DW_FORM_ref_addr, Params_2_8_32);
158 AddrSize = getFixedFormByteSize(DW_FORM_ref_addr, Params_2_8_32);
159 EXPECT_TRUE(RefSize.hasValue());
160 EXPECT_TRUE(AddrSize.hasValue());
161 EXPECT_EQ(*RefSize, *AddrSize);
162
163 // DW_FORM_ref_addr is 4 bytes in DWARF 32 in DWARF version 3 and beyond.
164 FormParams Params_3_4_32 = {3, 4, DWARF32};
165 RefSize = getFixedFormByteSize(DW_FORM_ref_addr, Params_3_4_32);
166 EXPECT_TRUE(RefSize.hasValue());
167 EXPECT_EQ(*RefSize, 4);
168
169 FormParams Params_4_4_32 = {4, 4, DWARF32};
170 RefSize = getFixedFormByteSize(DW_FORM_ref_addr, Params_4_4_32);
171 EXPECT_TRUE(RefSize.hasValue());
172 EXPECT_EQ(*RefSize, 4);
173
174 FormParams Params_5_4_32 = {5, 4, DWARF32};
175 RefSize = getFixedFormByteSize(DW_FORM_ref_addr, Params_5_4_32);
176 EXPECT_TRUE(RefSize.hasValue());
177 EXPECT_EQ(*RefSize, 4);
178
179 // DW_FORM_ref_addr is 8 bytes in DWARF 64 in DWARF version 3 and beyond.
180 FormParams Params_3_8_64 = {3, 8, DWARF64};
181 RefSize = getFixedFormByteSize(DW_FORM_ref_addr, Params_3_8_64);
182 EXPECT_TRUE(RefSize.hasValue());
183 EXPECT_EQ(*RefSize, 8);
184
185 FormParams Params_4_8_64 = {4, 8, DWARF64};
186 RefSize = getFixedFormByteSize(DW_FORM_ref_addr, Params_4_8_64);
187 EXPECT_TRUE(RefSize.hasValue());
188 EXPECT_EQ(*RefSize, 8);
189
190 FormParams Params_5_8_64 = {5, 8, DWARF64};
191 RefSize = getFixedFormByteSize(DW_FORM_ref_addr, Params_5_8_64);
192 EXPECT_TRUE(RefSize.hasValue());
193 EXPECT_EQ(*RefSize, 8);
194 }
195
TEST(DwarfTest,format_provider)196 TEST(DwarfTest, format_provider) {
197 EXPECT_EQ("DW_AT_name", formatv("{0}", DW_AT_name).str());
198 EXPECT_EQ("DW_AT_unknown_3fff", formatv("{0}", DW_AT_hi_user).str());
199 EXPECT_EQ("DW_FORM_addr", formatv("{0}", DW_FORM_addr).str());
200 EXPECT_EQ("DW_FORM_unknown_1f00", formatv("{0}", DW_FORM_lo_user).str());
201 EXPECT_EQ("DW_IDX_compile_unit", formatv("{0}", DW_IDX_compile_unit).str());
202 EXPECT_EQ("DW_IDX_unknown_3fff", formatv("{0}", DW_IDX_hi_user).str());
203 EXPECT_EQ("DW_TAG_compile_unit", formatv("{0}", DW_TAG_compile_unit).str());
204 EXPECT_EQ("DW_TAG_unknown_ffff", formatv("{0}", DW_TAG_hi_user).str());
205 }
206 } // end namespace
207