[llvm] 39285a0 - Add streaming/equality operators to DWARFAddressRange/DWARFLocationExpression
Pavel Labath via llvm-commits
llvm-commits at lists.llvm.org
Tue Nov 19 01:39:21 PST 2019
Author: Pavel Labath
Date: 2019-11-19T10:34:30+01:00
New Revision: 39285a0f02c7bc5e4bad8a684a900123b097511d
URL: https://github.com/llvm/llvm-project/commit/39285a0f02c7bc5e4bad8a684a900123b097511d
DIFF: https://github.com/llvm/llvm-project/commit/39285a0f02c7bc5e4bad8a684a900123b097511d.diff
LOG: 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.
Added:
llvm/lib/DebugInfo/DWARF/DWARFLocationExpression.cpp
llvm/unittests/DebugInfo/DWARF/DWARFLocationExpressionTest.cpp
Modified:
llvm/include/llvm/DebugInfo/DWARF/DWARFAddressRange.h
llvm/include/llvm/DebugInfo/DWARF/DWARFLocationExpression.h
llvm/lib/DebugInfo/DWARF/CMakeLists.txt
llvm/unittests/DebugInfo/DWARF/CMakeLists.txt
Removed:
################################################################################
diff --git a/llvm/include/llvm/DebugInfo/DWARF/DWARFAddressRange.h b/llvm/include/llvm/DebugInfo/DWARF/DWARFAddressRange.h
index e171477310a6..7a728c2508cc 100644
--- a/llvm/include/llvm/DebugInfo/DWARF/DWARFAddressRange.h
+++ b/llvm/include/llvm/DebugInfo/DWARF/DWARFAddressRange.h
@@ -49,11 +49,16 @@ struct DWARFAddressRange {
const DWARFObject *Obj = nullptr) const;
};
-static inline bool operator<(const DWARFAddressRange &LHS,
- const DWARFAddressRange &RHS) {
+inline bool operator<(const DWARFAddressRange &LHS,
+ const DWARFAddressRange &RHS) {
return std::tie(LHS.LowPC, LHS.HighPC) < std::tie(RHS.LowPC, RHS.HighPC);
}
+inline bool operator==(const DWARFAddressRange &LHS,
+ const DWARFAddressRange &RHS) {
+ return std::tie(LHS.LowPC, LHS.HighPC) == std::tie(RHS.LowPC, RHS.HighPC);
+}
+
raw_ostream &operator<<(raw_ostream &OS, const DWARFAddressRange &R);
/// DWARFAddressRangesVector - represents a set of absolute address ranges.
diff --git a/llvm/include/llvm/DebugInfo/DWARF/DWARFLocationExpression.h b/llvm/include/llvm/DebugInfo/DWARF/DWARFLocationExpression.h
index 7bde33d394fd..1522658ead75 100644
--- a/llvm/include/llvm/DebugInfo/DWARF/DWARFLocationExpression.h
+++ b/llvm/include/llvm/DebugInfo/DWARF/DWARFLocationExpression.h
@@ -14,6 +14,8 @@
namespace llvm {
+class raw_ostream;
+
/// Represents a single DWARF expression, whose value is location-dependent.
/// Typically used in DW_AT_location attributes to describe the location of
/// objects.
@@ -27,6 +29,18 @@ struct DWARFLocationExpression {
SmallVector<uint8_t, 4> Expr;
};
+inline bool operator==(const DWARFLocationExpression &L,
+ const DWARFLocationExpression &R) {
+ return L.Range == R.Range && L.Expr == R.Expr;
+}
+
+inline bool operator!=(const DWARFLocationExpression &L,
+ const DWARFLocationExpression &R) {
+ return !(L == R);
+}
+
+raw_ostream &operator<<(raw_ostream &OS, const DWARFLocationExpression &Loc);
+
} // end namespace llvm
#endif // LLVM_DEBUGINFO_DWARF_DWARFLOCATIONEXPRESSION_H
diff --git a/llvm/lib/DebugInfo/DWARF/CMakeLists.txt b/llvm/lib/DebugInfo/DWARF/CMakeLists.txt
index b4770e561f71..fcfb00e8e5f3 100644
--- a/llvm/lib/DebugInfo/DWARF/CMakeLists.txt
+++ b/llvm/lib/DebugInfo/DWARF/CMakeLists.txt
@@ -22,6 +22,7 @@ add_llvm_library(LLVMDebugInfoDWARF
DWARFFormValue.cpp
DWARFGdbIndex.cpp
DWARFListTable.cpp
+ DWARFLocationExpression.cpp
DWARFTypeUnit.cpp
DWARFUnitIndex.cpp
DWARFUnit.cpp
diff --git a/llvm/lib/DebugInfo/DWARF/DWARFLocationExpression.cpp b/llvm/lib/DebugInfo/DWARF/DWARFLocationExpression.cpp
new file mode 100644
index 000000000000..1cf73a666778
--- /dev/null
+++ b/llvm/lib/DebugInfo/DWARF/DWARFLocationExpression.cpp
@@ -0,0 +1,19 @@
+//===- DWARFLocationExpression.cpp ----------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/DebugInfo/DWARF/DWARFLocationExpression.h"
+#include "llvm/ADT/iterator_range.h"
+#include "llvm/Support/FormatVariadic.h"
+
+using namespace llvm;
+
+raw_ostream &llvm::operator<<(raw_ostream &OS,
+ const DWARFLocationExpression &Loc) {
+ return OS << Loc.Range << ": "
+ << formatv("{0}", make_range(Loc.Expr.begin(), Loc.Expr.end()));
+}
diff --git a/llvm/unittests/DebugInfo/DWARF/CMakeLists.txt b/llvm/unittests/DebugInfo/DWARF/CMakeLists.txt
index d97e573ea38f..2181e0543d04 100644
--- a/llvm/unittests/DebugInfo/DWARF/CMakeLists.txt
+++ b/llvm/unittests/DebugInfo/DWARF/CMakeLists.txt
@@ -14,6 +14,7 @@ add_llvm_unittest(DebugInfoDWARFTests
DWARFDebugInfoTest.cpp
DWARFDebugLineTest.cpp
DWARFFormValueTest.cpp
+ DWARFLocationExpressionTest.cpp
)
target_link_libraries(DebugInfoDWARFTests PRIVATE LLVMTestingSupport)
diff --git a/llvm/unittests/DebugInfo/DWARF/DWARFLocationExpressionTest.cpp b/llvm/unittests/DebugInfo/DWARF/DWARFLocationExpressionTest.cpp
new file mode 100644
index 000000000000..fb1dc3bd280a
--- /dev/null
+++ b/llvm/unittests/DebugInfo/DWARF/DWARFLocationExpressionTest.cpp
@@ -0,0 +1,30 @@
+//===- llvm/unittest/DebugInfo/DWARFLocationExpressionTest.cpp ------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/DebugInfo/DWARF/DWARFLocationExpression.h"
+#include "llvm/Support/ScopedPrinter.h"
+#include "gtest/gtest.h"
+
+using namespace llvm;
+using object::SectionedAddress;
+
+TEST(DWARFLocationExpression, Equality) {
+ EXPECT_EQ((DWARFLocationExpression{None, {}}),
+ (DWARFLocationExpression{None, {}}));
+ EXPECT_NE((DWARFLocationExpression{DWARFAddressRange{1, 47}, {}}),
+ (DWARFLocationExpression{DWARFAddressRange{1, 48}, {}}));
+ EXPECT_NE((DWARFLocationExpression{DWARFAddressRange{1, 47}, {}}),
+ (DWARFLocationExpression{DWARFAddressRange{1, 47}, {42}}));
+}
+
+TEST(DWARFLocationExpression, StreamingOperator) {
+ EXPECT_EQ("None: 1, 2", to_string(DWARFLocationExpression{None, {1, 2}}));
+ EXPECT_EQ(
+ "[0x0000000000000042, 0x0000000000000047): 1",
+ to_string(DWARFLocationExpression{DWARFAddressRange{0x42, 0x47}, {1}}));
+}
More information about the llvm-commits
mailing list