[llvm] f087597 - Support: Add operator== for MemoryBufferRef and split out MemoryBufferRef.h
Duncan P. N. Exon Smith via llvm-commits
llvm-commits at lists.llvm.org
Tue Oct 13 13:42:58 PDT 2020
Author: Duncan P. N. Exon Smith
Date: 2020-10-13T16:42:24-04:00
New Revision: f0875971249b59b5a4c07cedc9633f0359cf6b73
URL: https://github.com/llvm/llvm-project/commit/f0875971249b59b5a4c07cedc9633f0359cf6b73
DIFF: https://github.com/llvm/llvm-project/commit/f0875971249b59b5a4c07cedc9633f0359cf6b73.diff
LOG: 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
Added:
llvm/include/llvm/Support/MemoryBufferRef.h
llvm/lib/Support/MemoryBufferRef.cpp
llvm/unittests/Support/MemoryBufferRefTest.cpp
Modified:
llvm/include/llvm/Support/MemoryBuffer.h
llvm/lib/Support/CMakeLists.txt
llvm/unittests/Support/CMakeLists.txt
llvm/unittests/Support/MemoryBufferTest.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/Support/MemoryBuffer.h b/llvm/include/llvm/Support/MemoryBuffer.h
index f47a8d2d334b..9e6ee2536c5e 100644
--- a/llvm/include/llvm/Support/MemoryBuffer.h
+++ b/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 <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 @@ class WriteThroughMemoryBuffer : public MemoryBuffer {
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)
diff --git a/llvm/include/llvm/Support/MemoryBufferRef.h b/llvm/include/llvm/Support/MemoryBufferRef.h
new file mode 100644
index 000000000000..b38a1f3b6565
--- /dev/null
+++ b/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
diff --git a/llvm/lib/Support/CMakeLists.txt b/llvm/lib/Support/CMakeLists.txt
index 45fe23c5b5a6..24b0fc715cd9 100644
--- a/llvm/lib/Support/CMakeLists.txt
+++ b/llvm/lib/Support/CMakeLists.txt
@@ -142,6 +142,7 @@ add_llvm_component_library(LLVMSupport
MathExtras.cpp
MemAlloc.cpp
MemoryBuffer.cpp
+ MemoryBufferRef.cpp
MD5.cpp
NativeFormatting.cpp
OptimizedStructLayout.cpp
diff --git a/llvm/lib/Support/MemoryBufferRef.cpp b/llvm/lib/Support/MemoryBufferRef.cpp
new file mode 100644
index 000000000000..a93853c0acb1
--- /dev/null
+++ b/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()) {}
diff --git a/llvm/unittests/Support/CMakeLists.txt b/llvm/unittests/Support/CMakeLists.txt
index 90545bf056a3..86603e0fc5f4 100644
--- a/llvm/unittests/Support/CMakeLists.txt
+++ b/llvm/unittests/Support/CMakeLists.txt
@@ -50,6 +50,7 @@ add_llvm_unittest(SupportTests
MD5Test.cpp
ManagedStatic.cpp
MathExtrasTest.cpp
+ MemoryBufferRefTest.cpp
MemoryBufferTest.cpp
MemoryTest.cpp
NativeFormatTests.cpp
diff --git a/llvm/unittests/Support/MemoryBufferRefTest.cpp b/llvm/unittests/Support/MemoryBufferRefTest.cpp
new file mode 100644
index 000000000000..760e583b450b
--- /dev/null
+++ b/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<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
diff --git a/llvm/unittests/Support/MemoryBufferTest.cpp b/llvm/unittests/Support/MemoryBufferTest.cpp
index e0f51db1d79f..97bb62df393b 100644
--- a/llvm/unittests/Support/MemoryBufferTest.cpp
+++ b/llvm/unittests/Support/MemoryBufferTest.cpp
@@ -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
diff erent data on each page.
int FD;
More information about the llvm-commits
mailing list