Skip to content

Commit 39285a0

Browse files
committed
Add streaming/equality operators to DWARFAddressRange/DWARFLocationExpression
The main motivation for this is being able to write simpler assertions and get better error messages in unit tests. Split off from D70394.
1 parent c0fc29c commit 39285a0

File tree

6 files changed

+72
-2
lines changed

6 files changed

+72
-2
lines changed

llvm/include/llvm/DebugInfo/DWARF/DWARFAddressRange.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,16 @@ struct DWARFAddressRange {
4949
const DWARFObject *Obj = nullptr) const;
5050
};
5151

52-
static inline bool operator<(const DWARFAddressRange &LHS,
53-
const DWARFAddressRange &RHS) {
52+
inline bool operator<(const DWARFAddressRange &LHS,
53+
const DWARFAddressRange &RHS) {
5454
return std::tie(LHS.LowPC, LHS.HighPC) < std::tie(RHS.LowPC, RHS.HighPC);
5555
}
5656

57+
inline bool operator==(const DWARFAddressRange &LHS,
58+
const DWARFAddressRange &RHS) {
59+
return std::tie(LHS.LowPC, LHS.HighPC) == std::tie(RHS.LowPC, RHS.HighPC);
60+
}
61+
5762
raw_ostream &operator<<(raw_ostream &OS, const DWARFAddressRange &R);
5863

5964
/// DWARFAddressRangesVector - represents a set of absolute address ranges.

llvm/include/llvm/DebugInfo/DWARF/DWARFLocationExpression.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
namespace llvm {
1616

17+
class raw_ostream;
18+
1719
/// Represents a single DWARF expression, whose value is location-dependent.
1820
/// Typically used in DW_AT_location attributes to describe the location of
1921
/// objects.
@@ -27,6 +29,18 @@ struct DWARFLocationExpression {
2729
SmallVector<uint8_t, 4> Expr;
2830
};
2931

32+
inline bool operator==(const DWARFLocationExpression &L,
33+
const DWARFLocationExpression &R) {
34+
return L.Range == R.Range && L.Expr == R.Expr;
35+
}
36+
37+
inline bool operator!=(const DWARFLocationExpression &L,
38+
const DWARFLocationExpression &R) {
39+
return !(L == R);
40+
}
41+
42+
raw_ostream &operator<<(raw_ostream &OS, const DWARFLocationExpression &Loc);
43+
3044
} // end namespace llvm
3145

3246
#endif // LLVM_DEBUGINFO_DWARF_DWARFLOCATIONEXPRESSION_H

llvm/lib/DebugInfo/DWARF/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ add_llvm_library(LLVMDebugInfoDWARF
2222
DWARFFormValue.cpp
2323
DWARFGdbIndex.cpp
2424
DWARFListTable.cpp
25+
DWARFLocationExpression.cpp
2526
DWARFTypeUnit.cpp
2627
DWARFUnitIndex.cpp
2728
DWARFUnit.cpp
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//===- DWARFLocationExpression.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/DWARFLocationExpression.h"
10+
#include "llvm/ADT/iterator_range.h"
11+
#include "llvm/Support/FormatVariadic.h"
12+
13+
using namespace llvm;
14+
15+
raw_ostream &llvm::operator<<(raw_ostream &OS,
16+
const DWARFLocationExpression &Loc) {
17+
return OS << Loc.Range << ": "
18+
<< formatv("{0}", make_range(Loc.Expr.begin(), Loc.Expr.end()));
19+
}

llvm/unittests/DebugInfo/DWARF/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ add_llvm_unittest(DebugInfoDWARFTests
1414
DWARFDebugInfoTest.cpp
1515
DWARFDebugLineTest.cpp
1616
DWARFFormValueTest.cpp
17+
DWARFLocationExpressionTest.cpp
1718
)
1819

1920
target_link_libraries(DebugInfoDWARFTests PRIVATE LLVMTestingSupport)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//===- llvm/unittest/DebugInfo/DWARFLocationExpressionTest.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/DWARFLocationExpression.h"
10+
#include "llvm/Support/ScopedPrinter.h"
11+
#include "gtest/gtest.h"
12+
13+
using namespace llvm;
14+
using object::SectionedAddress;
15+
16+
TEST(DWARFLocationExpression, Equality) {
17+
EXPECT_EQ((DWARFLocationExpression{None, {}}),
18+
(DWARFLocationExpression{None, {}}));
19+
EXPECT_NE((DWARFLocationExpression{DWARFAddressRange{1, 47}, {}}),
20+
(DWARFLocationExpression{DWARFAddressRange{1, 48}, {}}));
21+
EXPECT_NE((DWARFLocationExpression{DWARFAddressRange{1, 47}, {}}),
22+
(DWARFLocationExpression{DWARFAddressRange{1, 47}, {42}}));
23+
}
24+
25+
TEST(DWARFLocationExpression, StreamingOperator) {
26+
EXPECT_EQ("None: 1, 2", to_string(DWARFLocationExpression{None, {1, 2}}));
27+
EXPECT_EQ(
28+
"[0x0000000000000042, 0x0000000000000047): 1",
29+
to_string(DWARFLocationExpression{DWARFAddressRange{0x42, 0x47}, {1}}));
30+
}

0 commit comments

Comments
 (0)