Skip to content

Commit c0fc29c

Browse files
committed
Add operator<< for object::SectionedAddress
The main motivation for this is better failure messages in unit tests. Split off from D70394.
1 parent 7db1230 commit c0fc29c

File tree

4 files changed

+30
-0
lines changed

4 files changed

+30
-0
lines changed

llvm/include/llvm/Object/ObjectFile.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ inline bool operator==(const SectionedAddress &LHS,
155155
std::tie(RHS.SectionIndex, RHS.Address);
156156
}
157157

158+
raw_ostream &operator<<(raw_ostream &OS, const SectionedAddress &Addr);
159+
158160
/// This is a value type class that represents a single symbol in the list of
159161
/// symbols in the object file.
160162
class SymbolRef : public BasicSymbolRef {

llvm/lib/Object/ObjectFile.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@
3232
using namespace llvm;
3333
using namespace object;
3434

35+
raw_ostream &object::operator<<(raw_ostream &OS, const SectionedAddress &Addr) {
36+
OS << "SectionedAddress{" << format_hex(Addr.Address, 10);
37+
if (Addr.SectionIndex != SectionedAddress::UndefSection)
38+
OS << ", " << Addr.SectionIndex;
39+
return OS << "}";
40+
}
41+
3542
void ObjectFile::anchor() {}
3643

3744
ObjectFile::ObjectFile(unsigned int Type, MemoryBufferRef Source)

llvm/unittests/Object/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ set(LLVM_LINK_COMPONENTS
55

66
add_llvm_unittest(ObjectTests
77
MinidumpTest.cpp
8+
ObjectFileTest.cpp
89
SymbolSizeTest.cpp
910
SymbolicFileTest.cpp
1011
)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===- ObjectFileTest.cpp - Tests for ObjectFile.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/Object/ObjectFile.h"
10+
#include "llvm/Support/ScopedPrinter.h"
11+
#include "gtest/gtest.h"
12+
13+
using namespace llvm;
14+
using namespace llvm::object;
15+
16+
TEST(SectionedAddress, StreamingOperator) {
17+
EXPECT_EQ("SectionedAddress{0x00000047}", to_string(SectionedAddress{0x47}));
18+
EXPECT_EQ("SectionedAddress{0x00000047, 42}",
19+
to_string(SectionedAddress{0x47, 42}));
20+
}

0 commit comments

Comments
 (0)