blob: be78f116ce459ab0a79f7a4ee79ecf8884a51419 [file] [log] [blame]
Alexey Samsonov045c6c52013-04-17 08:29:02 +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
Zachary Turner82af9432015-01-30 18:07:45 +000010#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
Frederic Riss77f850e2015-03-04 22:07:41 +000011#include "llvm/ADT/SmallString.h"
Alexey Samsonov045c6c52013-04-17 08:29:02 +000012#include "llvm/Support/Dwarf.h"
Frederic Riss77f850e2015-03-04 22:07:41 +000013#include "llvm/Support/LEB128.h"
14#include "llvm/Support/Host.h"
Alexey Samsonov045c6c52013-04-17 08:29:02 +000015#include "gtest/gtest.h"
16using namespace llvm;
17using namespace dwarf;
18
19namespace {
20
21TEST(DWARFFormValue, FixedFormSizes) {
22 // Size of DW_FORM_addr and DW_FORM_ref_addr are equal in DWARF2,
23 // DW_FORM_ref_addr is always 4 bytes in DWARF32 starting from DWARF3.
Alexey Samsonova56bbf02013-10-28 23:41:49 +000024 ArrayRef<uint8_t> sizes = DWARFFormValue::getFixedFormSizes(4, 2);
Alexey Samsonov045c6c52013-04-17 08:29:02 +000025 EXPECT_EQ(sizes[DW_FORM_addr], sizes[DW_FORM_ref_addr]);
26 sizes = DWARFFormValue::getFixedFormSizes(8, 2);
27 EXPECT_EQ(sizes[DW_FORM_addr], sizes[DW_FORM_ref_addr]);
28 sizes = DWARFFormValue::getFixedFormSizes(8, 3);
29 EXPECT_EQ(4, sizes[DW_FORM_ref_addr]);
30 // Check that we don't have fixed form sizes for weird address sizes.
Alexey Samsonova56bbf02013-10-28 23:41:49 +000031 EXPECT_EQ(0U, DWARFFormValue::getFixedFormSizes(16, 2).size());
Alexey Samsonov045c6c52013-04-17 08:29:02 +000032}
33
Alexey Samsonov48cbda52013-10-28 23:01:48 +000034bool isFormClass(uint16_t Form, DWARFFormValue::FormClass FC) {
35 return DWARFFormValue(Form).isFormClass(FC);
36}
37
38TEST(DWARFFormValue, FormClass) {
39 EXPECT_TRUE(isFormClass(DW_FORM_addr, DWARFFormValue::FC_Address));
40 EXPECT_FALSE(isFormClass(DW_FORM_data8, DWARFFormValue::FC_Address));
41 EXPECT_TRUE(isFormClass(DW_FORM_data8, DWARFFormValue::FC_Constant));
42 EXPECT_TRUE(isFormClass(DW_FORM_data8, DWARFFormValue::FC_SectionOffset));
43 EXPECT_TRUE(
44 isFormClass(DW_FORM_sec_offset, DWARFFormValue::FC_SectionOffset));
45 EXPECT_TRUE(isFormClass(DW_FORM_GNU_str_index, DWARFFormValue::FC_String));
46 EXPECT_TRUE(isFormClass(DW_FORM_GNU_addr_index, DWARFFormValue::FC_Address));
47 EXPECT_FALSE(isFormClass(DW_FORM_ref_addr, DWARFFormValue::FC_Address));
48 EXPECT_TRUE(isFormClass(DW_FORM_ref_addr, DWARFFormValue::FC_Reference));
Alexey Samsonovcbd806a2013-10-29 16:32:19 +000049 EXPECT_TRUE(isFormClass(DW_FORM_ref_sig8, DWARFFormValue::FC_Reference));
Alexey Samsonov48cbda52013-10-28 23:01:48 +000050}
51
Frederic Riss77f850e2015-03-04 22:07:41 +000052template<typename RawTypeT>
53DWARFFormValue createDataXFormValue(uint16_t Form, RawTypeT Value) {
54 char Raw[sizeof(RawTypeT)];
55 memcpy(Raw, &Value, sizeof(RawTypeT));
56 uint32_t Offset = 0;
57 DWARFFormValue Result(Form);
58 DataExtractor Data(StringRef(Raw, sizeof(RawTypeT)),
59 sys::IsLittleEndianHost, sizeof(void*));
60 Result.extractValue(Data, &Offset, nullptr);
61 return Result;
62}
63
64DWARFFormValue createULEBFormValue(uint64_t Value) {
65 SmallString<10> RawData;
66 raw_svector_ostream OS(RawData);
67 encodeULEB128(Value, OS);
68 uint32_t Offset = 0;
69 DWARFFormValue Result(DW_FORM_udata);
70 DataExtractor Data(OS.str(), sys::IsLittleEndianHost, sizeof(void*));
71 Result.extractValue(Data, &Offset, nullptr);
72 return Result;
73}
74
75DWARFFormValue createSLEBFormValue(int64_t Value) {
76 SmallString<10> RawData;
77 raw_svector_ostream OS(RawData);
78 encodeSLEB128(Value, OS);
79 uint32_t Offset = 0;
80 DWARFFormValue Result(DW_FORM_sdata);
81 DataExtractor Data(OS.str(), sys::IsLittleEndianHost, sizeof(void*));
82 Result.extractValue(Data, &Offset, nullptr);
83 return Result;
84}
85
86TEST(DWARFFormValue, SignedConstantForms) {
87 // Check that we correctly sign extend fixed size forms.
88 auto Sign1 = createDataXFormValue<uint8_t>(DW_FORM_data1, -123);
89 auto Sign2 = createDataXFormValue<uint16_t>(DW_FORM_data2, -12345);
90 auto Sign4 = createDataXFormValue<uint32_t>(DW_FORM_data4, -123456789);
91 auto Sign8 = createDataXFormValue<uint64_t>(DW_FORM_data8, -1);
92 EXPECT_EQ(Sign1.getAsSignedConstant().getValue(), -123);
93 EXPECT_EQ(Sign2.getAsSignedConstant().getValue(), -12345);
94 EXPECT_EQ(Sign4.getAsSignedConstant().getValue(), -123456789);
95 EXPECT_EQ(Sign8.getAsSignedConstant().getValue(), -1);
96
97 // Check that we can handle big positive values, but that we return
98 // an error just over the limit.
99 auto UMax = createULEBFormValue(LLONG_MAX);
100 auto TooBig = createULEBFormValue(LLONG_MAX + 1);
101 EXPECT_EQ(UMax.getAsSignedConstant().getValue(), LLONG_MAX);
102 EXPECT_EQ(TooBig.getAsSignedConstant().hasValue(), false);
103
104 // Sanity check some other forms.
105 auto Data1 = createDataXFormValue<uint8_t>(DW_FORM_data1, 120);
106 auto Data2 = createDataXFormValue<uint16_t>(DW_FORM_data2, 32000);
107 auto Data4 = createDataXFormValue<uint32_t>(DW_FORM_data4, 2000000000);
108 auto Data8 = createDataXFormValue<uint64_t>(DW_FORM_data8, 0x1234567812345678LL);
109 auto LEBMin = createSLEBFormValue(LLONG_MIN);
110 auto LEBMax = createSLEBFormValue(LLONG_MAX);
111 auto LEB1 = createSLEBFormValue(-42);
112 auto LEB2 = createSLEBFormValue(42);
113 EXPECT_EQ(Data1.getAsSignedConstant().getValue(), 120);
114 EXPECT_EQ(Data2.getAsSignedConstant().getValue(), 32000);
115 EXPECT_EQ(Data4.getAsSignedConstant().getValue(), 2000000000);
116 EXPECT_EQ(Data8.getAsSignedConstant().getValue(), 0x1234567812345678LL);
117 EXPECT_EQ(LEBMin.getAsSignedConstant().getValue(), LLONG_MIN);
118 EXPECT_EQ(LEBMax.getAsSignedConstant().getValue(), LLONG_MAX);
119 EXPECT_EQ(LEB1.getAsSignedConstant().getValue(), -42);
120 EXPECT_EQ(LEB2.getAsSignedConstant().getValue(), 42);
121}
122
Alexey Samsonov045c6c52013-04-17 08:29:02 +0000123} // end anonymous namespace