Skip to content

Commit ca1b994

Browse files
committed
[DebugInfo][NFCI] Add unittest for DWARFAbbreviationDeclarationSet
The classes relevant to DWARFDebugAbbrev do not have any unittests verifying their behavior. Seeing as there is not much error handling around these classes right now, I want to add some testing as I plan on making changes to these classes in the near future. Differential Revision: https://reviews.llvm.org/D151001
1 parent 872e46a commit ca1b994

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed

llvm/include/llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ class DWARFAbbreviationDeclarationSet {
4949

5050
std::string getCodeRange() const;
5151

52+
uint32_t getFirstAbbrCode() const { return FirstAbbrCode; }
53+
5254
private:
5355
void clear();
5456
};

llvm/unittests/DebugInfo/DWARF/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ add_llvm_unittest(DebugInfoDWARFTests
1616
DwarfUtils.cpp
1717
DWARFAcceleratorTableTest.cpp
1818
DWARFDataExtractorTest.cpp
19+
DWARFDebugAbbrevTest.cpp
1920
DWARFDebugArangeSetTest.cpp
2021
DWARFDebugFrameTest.cpp
2122
DWARFDebugInfoTest.cpp
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
//===- llvm/unittest/DebugInfo/DWARFDebugAbbrevTest.cpp -------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h"
10+
#include "llvm/BinaryFormat/Dwarf.h"
11+
#include "llvm/DebugInfo/DWARF/DWARFAbbreviationDeclaration.h"
12+
#include "llvm/Support/DataExtractor.h"
13+
#include "llvm/Support/LEB128.h"
14+
#include "llvm/Support/MemoryBuffer.h"
15+
#include "llvm/Testing/Support/Error.h"
16+
#include "gtest/gtest.h"
17+
18+
using namespace llvm;
19+
using namespace dwarf;
20+
21+
enum Order : bool { InOrder, OutOfOrder };
22+
23+
void WriteAbbreviationDeclarations(raw_ostream &OS, uint32_t FirstCode,
24+
Order Ord) {
25+
encodeULEB128(FirstCode, OS);
26+
encodeULEB128(DW_TAG_compile_unit, OS);
27+
OS << static_cast<uint8_t>(DW_CHILDREN_yes);
28+
encodeULEB128(DW_AT_name, OS);
29+
encodeULEB128(DW_FORM_strp, OS);
30+
encodeULEB128(0, OS);
31+
encodeULEB128(0, OS);
32+
33+
uint32_t SecondCode = Ord == Order::InOrder ? FirstCode + 1 : FirstCode - 1;
34+
35+
encodeULEB128(SecondCode, OS);
36+
encodeULEB128(DW_TAG_subprogram, OS);
37+
OS << static_cast<uint8_t>(DW_CHILDREN_no);
38+
encodeULEB128(DW_AT_name, OS);
39+
encodeULEB128(DW_FORM_strp, OS);
40+
encodeULEB128(0, OS);
41+
encodeULEB128(0, OS);
42+
}
43+
44+
TEST(DWARFDebugAbbrevTest, DWARFAbbrevDeclSetExtractSuccess) {
45+
SmallString<64> RawData;
46+
raw_svector_ostream OS(RawData);
47+
uint32_t FirstCode = 5;
48+
49+
WriteAbbreviationDeclarations(OS, FirstCode, InOrder);
50+
encodeULEB128(0, OS);
51+
52+
uint64_t Offset = 0;
53+
DataExtractor Data(RawData, sys::IsLittleEndianHost, sizeof(uint64_t));
54+
DWARFAbbreviationDeclarationSet AbbrevSet;
55+
const bool DataWasExtracted = AbbrevSet.extract(Data, &Offset);
56+
EXPECT_TRUE(DataWasExtracted);
57+
// The Abbreviation Declarations are in order and contiguous, so we want to
58+
// make sure that FirstAbbrCode was correctly set
59+
EXPECT_EQ(AbbrevSet.getFirstAbbrCode(), FirstCode);
60+
61+
const DWARFAbbreviationDeclaration *Abbrev5 =
62+
AbbrevSet.getAbbreviationDeclaration(FirstCode);
63+
EXPECT_TRUE(Abbrev5);
64+
EXPECT_EQ(Abbrev5->getTag(), DW_TAG_compile_unit);
65+
EXPECT_TRUE(Abbrev5->hasChildren());
66+
EXPECT_EQ(Abbrev5->getNumAttributes(), 1u);
67+
68+
const DWARFAbbreviationDeclaration *Abbrev6 =
69+
AbbrevSet.getAbbreviationDeclaration(FirstCode + 1);
70+
EXPECT_TRUE(Abbrev6);
71+
EXPECT_EQ(Abbrev6->getTag(), DW_TAG_subprogram);
72+
EXPECT_FALSE(Abbrev6->hasChildren());
73+
EXPECT_EQ(Abbrev6->getNumAttributes(), 1u);
74+
}
75+
76+
TEST(DWARFDebugAbbrevTest, DWARFAbbrevDeclSetExtractSuccessOutOfOrder) {
77+
SmallString<64> RawData;
78+
raw_svector_ostream OS(RawData);
79+
uint32_t FirstCode = 2;
80+
81+
WriteAbbreviationDeclarations(OS, FirstCode, OutOfOrder);
82+
encodeULEB128(0, OS);
83+
84+
uint64_t Offset = 0;
85+
DataExtractor Data(RawData, sys::IsLittleEndianHost, sizeof(uint64_t));
86+
DWARFAbbreviationDeclarationSet AbbrevSet;
87+
const bool DataWasExtracted = AbbrevSet.extract(Data, &Offset);
88+
EXPECT_TRUE(DataWasExtracted);
89+
// The declarations are out of order, ensure that FirstAbbrCode is UINT32_MAX
90+
EXPECT_EQ(AbbrevSet.getFirstAbbrCode(), UINT32_MAX);
91+
92+
const DWARFAbbreviationDeclaration *Abbrev2 =
93+
AbbrevSet.getAbbreviationDeclaration(FirstCode);
94+
EXPECT_TRUE(Abbrev2);
95+
EXPECT_EQ(Abbrev2->getTag(), DW_TAG_compile_unit);
96+
EXPECT_TRUE(Abbrev2->hasChildren());
97+
EXPECT_EQ(Abbrev2->getNumAttributes(), 1u);
98+
99+
const DWARFAbbreviationDeclaration *Abbrev1 =
100+
AbbrevSet.getAbbreviationDeclaration(FirstCode - 1);
101+
EXPECT_TRUE(Abbrev1);
102+
EXPECT_EQ(Abbrev1->getTag(), DW_TAG_subprogram);
103+
EXPECT_FALSE(Abbrev1->hasChildren());
104+
EXPECT_EQ(Abbrev1->getNumAttributes(), 1u);
105+
}

0 commit comments

Comments
 (0)