Index: llvm/include/llvm/Support/MemoryBuffer.h =================================================================== --- llvm/include/llvm/Support/MemoryBuffer.h +++ llvm/include/llvm/Support/MemoryBuffer.h @@ -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 #include #include namespace llvm { - -class MemoryBufferRef; - namespace sys { namespace fs { // Duplicated from FileSystem.h to avoid a dependency. @@ -260,26 +258,6 @@ 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) Index: llvm/include/llvm/Support/MemoryBufferRef.h =================================================================== --- /dev/null +++ llvm/include/llvm/Support/MemoryBufferRef.h @@ -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 Index: llvm/lib/Support/CMakeLists.txt =================================================================== --- llvm/lib/Support/CMakeLists.txt +++ llvm/lib/Support/CMakeLists.txt @@ -142,6 +142,7 @@ MathExtras.cpp MemAlloc.cpp MemoryBuffer.cpp + MemoryBufferRef.cpp MD5.cpp NativeFormatting.cpp OptimizedStructLayout.cpp Index: llvm/lib/Support/MemoryBufferRef.cpp =================================================================== --- /dev/null +++ llvm/lib/Support/MemoryBufferRef.cpp @@ -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()) {} Index: llvm/unittests/Support/CMakeLists.txt =================================================================== --- llvm/unittests/Support/CMakeLists.txt +++ llvm/unittests/Support/CMakeLists.txt @@ -50,6 +50,7 @@ MD5Test.cpp ManagedStatic.cpp MathExtrasTest.cpp + MemoryBufferRefTest.cpp MemoryBufferTest.cpp MemoryTest.cpp NativeFormatTests.cpp Index: llvm/unittests/Support/MemoryBufferRefTest.cpp =================================================================== --- /dev/null +++ llvm/unittests/Support/MemoryBufferRefTest.cpp @@ -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 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 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 Copy( + MemoryBuffer::getMemBufferCopy(Data, "id")); + MemoryBufferRef CopyRef(*Copy); + EXPECT_EQ(Ref.getBuffer(), CopyRef.getBuffer()); + EXPECT_NE(Ref, CopyRef); +} + +} // end namespace Index: llvm/unittests/Support/MemoryBufferTest.cpp =================================================================== --- llvm/unittests/Support/MemoryBufferTest.cpp +++ llvm/unittests/Support/MemoryBufferTest.cpp @@ -265,14 +265,6 @@ 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;