1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-22 10:42:39 +01:00

Support: Add operator== for MemoryBufferRef and split out MemoryBufferRef.h

As preparation for changing `LineIterator` to work with `MemoryBufferRef`:

- Add an `operator==` that uses buffer pointer identity to ensure two buffers
  are equivalent.
- Split out `MemoryBufferRef.h`, to avoid polluting `LineIterator.h` includers
  with everything from `MemoryBuffer.h`. This also means moving the
  `MemoryBuffer` constructor to a source file.

Differential Revision: https://reviews.llvm.org/D89279
This commit is contained in:
Duncan P. N. Exon Smith 2020-10-12 18:41:15 -04:00
parent fa159c46b5
commit b7c96281d2
7 changed files with 129 additions and 31 deletions

View File

@ -19,14 +19,12 @@
#include "llvm/ADT/Twine.h"
#include "llvm/Support/CBindingWrapping.h"
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/MemoryBufferRef.h"
#include <cstddef>
#include <cstdint>
#include <memory>
namespace llvm {
class MemoryBufferRef;
namespace sys {
namespace fs {
// Duplicated from FileSystem.h to avoid a dependency.
@ -260,26 +258,6 @@ private:
using MemoryBuffer::getSTDIN;
};
class MemoryBufferRef {
StringRef Buffer;
StringRef Identifier;
public:
MemoryBufferRef() = default;
MemoryBufferRef(const MemoryBuffer& Buffer)
: Buffer(Buffer.getBuffer()), Identifier(Buffer.getBufferIdentifier()) {}
MemoryBufferRef(StringRef Buffer, StringRef Identifier)
: Buffer(Buffer), Identifier(Identifier) {}
StringRef getBuffer() const { return Buffer; }
StringRef getBufferIdentifier() const { return Identifier; }
const char *getBufferStart() const { return Buffer.begin(); }
const char *getBufferEnd() const { return Buffer.end(); }
size_t getBufferSize() const { return Buffer.size(); }
};
// Create wrappers for C Binding types (see CBindingWrapping.h).
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(MemoryBuffer, LLVMMemoryBufferRef)

View File

@ -0,0 +1,56 @@
//===- MemoryBufferRef.h - Memory Buffer Reference --------------*- C++ -*-===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// This file defines the MemoryBuffer interface.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_SUPPORT_MEMORYBUFFERREF_H
#define LLVM_SUPPORT_MEMORYBUFFERREF_H
#include "llvm/ADT/StringRef.h"
namespace llvm {
class MemoryBuffer;
class MemoryBufferRef {
StringRef Buffer;
StringRef Identifier;
public:
MemoryBufferRef() = default;
MemoryBufferRef(const MemoryBuffer &Buffer);
MemoryBufferRef(StringRef Buffer, StringRef Identifier)
: Buffer(Buffer), Identifier(Identifier) {}
StringRef getBuffer() const { return Buffer; }
StringRef getBufferIdentifier() const { return Identifier; }
const char *getBufferStart() const { return Buffer.begin(); }
const char *getBufferEnd() const { return Buffer.end(); }
size_t getBufferSize() const { return Buffer.size(); }
/// Check pointer identity (not value) of identifier and data.
friend bool operator==(const MemoryBufferRef &LHS,
const MemoryBufferRef &RHS) {
return LHS.Buffer.begin() == RHS.Buffer.begin() &&
LHS.Buffer.end() == RHS.Buffer.end() &&
LHS.Identifier.begin() == RHS.Identifier.begin() &&
LHS.Identifier.end() == RHS.Identifier.end();
}
friend bool operator!=(const MemoryBufferRef &LHS,
const MemoryBufferRef &RHS) {
return !(LHS == RHS);
}
};
} // namespace llvm
#endif // LLVM_SUPPORT_MEMORYBUFFERREF_H

View File

@ -142,6 +142,7 @@ add_llvm_component_library(LLVMSupport
MathExtras.cpp
MemAlloc.cpp
MemoryBuffer.cpp
MemoryBufferRef.cpp
MD5.cpp
NativeFormatting.cpp
OptimizedStructLayout.cpp

View File

@ -0,0 +1,19 @@
//===- MemoryBufferRef.cpp - Memory Buffer Reference ----------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// This file implements the MemoryBufferRef interface.
//
//===----------------------------------------------------------------------===//
#include "llvm/Support/MemoryBufferRef.h"
#include "llvm/Support/MemoryBuffer.h"
using namespace llvm;
MemoryBufferRef::MemoryBufferRef(const MemoryBuffer &Buffer)
: Buffer(Buffer.getBuffer()), Identifier(Buffer.getBufferIdentifier()) {}

View File

@ -50,6 +50,7 @@ add_llvm_unittest(SupportTests
MD5Test.cpp
ManagedStatic.cpp
MathExtrasTest.cpp
MemoryBufferRefTest.cpp
MemoryBufferTest.cpp
MemoryTest.cpp
NativeFormatTests.cpp

View File

@ -0,0 +1,51 @@
//===- MemoryBufferRefTest.cpp - MemoryBufferRef tests --------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// This file implements unit tests for the MemoryBufferRef support class.
//
//===----------------------------------------------------------------------===//
#include "llvm/Support/MemoryBufferRef.h"
#include "llvm/Support/MemoryBuffer.h"
#include "gtest/gtest.h"
using namespace llvm;
namespace {
TEST(MemoryBufferRefTest, construct) {
std::unique_ptr<MemoryBuffer> MB(MemoryBuffer::getMemBuffer("data", "id"));
MemoryBufferRef MBR(*MB);
EXPECT_EQ(MB->getBufferStart(), MBR.getBufferStart());
EXPECT_EQ(MB->getBufferIdentifier(), MBR.getBufferIdentifier());
}
TEST(MemoryBufferRefTest, compareEquals) {
std::string Data = "data";
std::unique_ptr<MemoryBuffer> MB(MemoryBuffer::getMemBuffer(Data, "id"));
MemoryBufferRef Ref(*MB);
MemoryBufferRef Empty;
MemoryBufferRef NoIdentifier(MB->getBuffer(), "");
MemoryBufferRef NoData("", MB->getBufferIdentifier());
MemoryBufferRef Same(MB->getBuffer(), MB->getBufferIdentifier());
EXPECT_NE(Empty, Ref);
EXPECT_NE(NoIdentifier, Ref);
EXPECT_NE(NoData, Ref);
EXPECT_EQ(Same, Ref);
// Confirm NE when content matches but pointer identity does not.
std::unique_ptr<MemoryBuffer> Copy(
MemoryBuffer::getMemBufferCopy(Data, "id"));
MemoryBufferRef CopyRef(*Copy);
EXPECT_EQ(Ref.getBuffer(), CopyRef.getBuffer());
EXPECT_NE(Ref, CopyRef);
}
} // end namespace

View File

@ -265,14 +265,6 @@ TEST_F(MemoryBufferTest, getOpenFileReopened) {
testGetOpenFileSlice(true);
}
TEST_F(MemoryBufferTest, reference) {
OwningBuffer MB(MemoryBuffer::getMemBuffer(data));
MemoryBufferRef MBR(*MB);
EXPECT_EQ(MB->getBufferStart(), MBR.getBufferStart());
EXPECT_EQ(MB->getBufferIdentifier(), MBR.getBufferIdentifier());
}
TEST_F(MemoryBufferTest, slice) {
// Create a file that is six pages long with different data on each page.
int FD;