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