Skip to content

Commit

Permalink
Add operator<< for object::SectionedAddress
Browse files Browse the repository at this point in the history
The main motivation for this is better failure messages in unit tests.

Split off from D70394.
  • Loading branch information
labath committed Nov 19, 2019
1 parent 7db1230 commit c0fc29c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions llvm/include/llvm/Object/ObjectFile.h
Expand Up @@ -155,6 +155,8 @@ inline bool operator==(const SectionedAddress &LHS,
std::tie(RHS.SectionIndex, RHS.Address);
}

raw_ostream &operator<<(raw_ostream &OS, const SectionedAddress &Addr);

/// This is a value type class that represents a single symbol in the list of
/// symbols in the object file.
class SymbolRef : public BasicSymbolRef {
Expand Down
7 changes: 7 additions & 0 deletions llvm/lib/Object/ObjectFile.cpp
Expand Up @@ -32,6 +32,13 @@
using namespace llvm;
using namespace object;

raw_ostream &object::operator<<(raw_ostream &OS, const SectionedAddress &Addr) {
OS << "SectionedAddress{" << format_hex(Addr.Address, 10);
if (Addr.SectionIndex != SectionedAddress::UndefSection)
OS << ", " << Addr.SectionIndex;
return OS << "}";
}

void ObjectFile::anchor() {}

ObjectFile::ObjectFile(unsigned int Type, MemoryBufferRef Source)
Expand Down
1 change: 1 addition & 0 deletions llvm/unittests/Object/CMakeLists.txt
Expand Up @@ -5,6 +5,7 @@ set(LLVM_LINK_COMPONENTS

add_llvm_unittest(ObjectTests
MinidumpTest.cpp
ObjectFileTest.cpp
SymbolSizeTest.cpp
SymbolicFileTest.cpp
)
Expand Down
20 changes: 20 additions & 0 deletions llvm/unittests/Object/ObjectFileTest.cpp
@@ -0,0 +1,20 @@
//===- ObjectFileTest.cpp - Tests for ObjectFile.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/Object/ObjectFile.h"
#include "llvm/Support/ScopedPrinter.h"
#include "gtest/gtest.h"

using namespace llvm;
using namespace llvm::object;

TEST(SectionedAddress, StreamingOperator) {
EXPECT_EQ("SectionedAddress{0x00000047}", to_string(SectionedAddress{0x47}));
EXPECT_EQ("SectionedAddress{0x00000047, 42}",
to_string(SectionedAddress{0x47, 42}));
}

0 comments on commit c0fc29c

Please sign in to comment.