[PATCH] D39693: Move factory methods out of their classes.
Rafael Avila de Espindola via llvm-commits
llvm-commits at lists.llvm.org
Mon Nov 6 12:58:32 PST 2017
LGTM, thanks!
Rui Ueyama via Phabricator <reviews at reviews.llvm.org> writes:
> ruiu created this revision.
> Herald added a subscriber: hiraditya.
>
> InMemoryBuffer and OnDiskBuffer classes have both factory methods and
> public constructors, and that looks a bit odd. This patch makes factory
> methods non-member function to fix it.
>
>
> https://reviews.llvm.org/D39693
>
> Files:
> llvm/lib/Support/FileOutputBuffer.cpp
>
>
> Index: llvm/lib/Support/FileOutputBuffer.cpp
> ===================================================================
> --- llvm/lib/Support/FileOutputBuffer.cpp
> +++ llvm/lib/Support/FileOutputBuffer.cpp
> @@ -38,9 +38,6 @@
> std::unique_ptr<fs::mapped_file_region> Buf)
> : FileOutputBuffer(Path), Buffer(std::move(Buf)), TempPath(TempPath) {}
>
> - static ErrorOr<std::unique_ptr<OnDiskBuffer>>
> - create(StringRef Path, size_t Size, unsigned Mode);
> -
> uint8_t *getBufferStart() const override { return (uint8_t *)Buffer->data(); }
>
> uint8_t *getBufferEnd() const override {
> @@ -78,16 +75,6 @@
> InMemoryBuffer(StringRef Path, MemoryBlock Buf, unsigned Mode)
> : FileOutputBuffer(Path), Buffer(Buf), Mode(Mode) {}
>
> - static ErrorOr<std::unique_ptr<InMemoryBuffer>>
> - create(StringRef Path, size_t Size, unsigned Mode) {
> - std::error_code EC;
> - MemoryBlock MB = Memory::allocateMappedMemory(
> - Size, nullptr, sys::Memory::MF_READ | sys::Memory::MF_WRITE, EC);
> - if (EC)
> - return EC;
> - return llvm::make_unique<InMemoryBuffer>(Path, MB, Mode);
> - }
> -
> uint8_t *getBufferStart() const override { return (uint8_t *)Buffer.base(); }
>
> uint8_t *getBufferEnd() const override {
> @@ -111,8 +98,8 @@
> unsigned Mode;
> };
>
> -ErrorOr<std::unique_ptr<OnDiskBuffer>>
> -OnDiskBuffer::create(StringRef Path, size_t Size, unsigned Mode) {
> +static ErrorOr<std::unique_ptr<OnDiskBuffer>>
> +createOnDiskBuffer(StringRef Path, size_t Size, unsigned Mode) {
> // Create new file in same directory but with random name.
> SmallString<128> TempPath;
> int FD;
> @@ -141,6 +128,16 @@
> return llvm::make_unique<OnDiskBuffer>(Path, TempPath, std::move(MappedFile));
> }
>
> +static ErrorOr<std::unique_ptr<InMemoryBuffer>>
> +createInMemoryBuffer(StringRef Path, size_t Size, unsigned Mode) {
> + std::error_code EC;
> + MemoryBlock MB = Memory::allocateMappedMemory(
> + Size, nullptr, sys::Memory::MF_READ | sys::Memory::MF_WRITE, EC);
> + if (EC)
> + return EC;
> + return llvm::make_unique<InMemoryBuffer>(Path, MB, Mode);
> +}
> +
> // Create an instance of FileOutputBuffer.
> ErrorOr<std::unique_ptr<FileOutputBuffer>>
> FileOutputBuffer::create(StringRef Path, size_t Size, unsigned Flags) {
> @@ -165,8 +162,8 @@
> case fs::file_type::regular_file:
> case fs::file_type::file_not_found:
> case fs::file_type::status_error:
> - return OnDiskBuffer::create(Path, Size, Mode);
> + return createOnDiskBuffer(Path, Size, Mode);
> default:
> - return InMemoryBuffer::create(Path, Size, Mode);
> + return createInMemoryBuffer(Path, Size, Mode);
> }
> }
>
>
> Index: llvm/lib/Support/FileOutputBuffer.cpp
> ===================================================================
> --- llvm/lib/Support/FileOutputBuffer.cpp
> +++ llvm/lib/Support/FileOutputBuffer.cpp
> @@ -38,9 +38,6 @@
> std::unique_ptr<fs::mapped_file_region> Buf)
> : FileOutputBuffer(Path), Buffer(std::move(Buf)), TempPath(TempPath) {}
>
> - static ErrorOr<std::unique_ptr<OnDiskBuffer>>
> - create(StringRef Path, size_t Size, unsigned Mode);
> -
> uint8_t *getBufferStart() const override { return (uint8_t *)Buffer->data(); }
>
> uint8_t *getBufferEnd() const override {
> @@ -78,16 +75,6 @@
> InMemoryBuffer(StringRef Path, MemoryBlock Buf, unsigned Mode)
> : FileOutputBuffer(Path), Buffer(Buf), Mode(Mode) {}
>
> - static ErrorOr<std::unique_ptr<InMemoryBuffer>>
> - create(StringRef Path, size_t Size, unsigned Mode) {
> - std::error_code EC;
> - MemoryBlock MB = Memory::allocateMappedMemory(
> - Size, nullptr, sys::Memory::MF_READ | sys::Memory::MF_WRITE, EC);
> - if (EC)
> - return EC;
> - return llvm::make_unique<InMemoryBuffer>(Path, MB, Mode);
> - }
> -
> uint8_t *getBufferStart() const override { return (uint8_t *)Buffer.base(); }
>
> uint8_t *getBufferEnd() const override {
> @@ -111,8 +98,8 @@
> unsigned Mode;
> };
>
> -ErrorOr<std::unique_ptr<OnDiskBuffer>>
> -OnDiskBuffer::create(StringRef Path, size_t Size, unsigned Mode) {
> +static ErrorOr<std::unique_ptr<OnDiskBuffer>>
> +createOnDiskBuffer(StringRef Path, size_t Size, unsigned Mode) {
> // Create new file in same directory but with random name.
> SmallString<128> TempPath;
> int FD;
> @@ -141,6 +128,16 @@
> return llvm::make_unique<OnDiskBuffer>(Path, TempPath, std::move(MappedFile));
> }
>
> +static ErrorOr<std::unique_ptr<InMemoryBuffer>>
> +createInMemoryBuffer(StringRef Path, size_t Size, unsigned Mode) {
> + std::error_code EC;
> + MemoryBlock MB = Memory::allocateMappedMemory(
> + Size, nullptr, sys::Memory::MF_READ | sys::Memory::MF_WRITE, EC);
> + if (EC)
> + return EC;
> + return llvm::make_unique<InMemoryBuffer>(Path, MB, Mode);
> +}
> +
> // Create an instance of FileOutputBuffer.
> ErrorOr<std::unique_ptr<FileOutputBuffer>>
> FileOutputBuffer::create(StringRef Path, size_t Size, unsigned Flags) {
> @@ -165,8 +162,8 @@
> case fs::file_type::regular_file:
> case fs::file_type::file_not_found:
> case fs::file_type::status_error:
> - return OnDiskBuffer::create(Path, Size, Mode);
> + return createOnDiskBuffer(Path, Size, Mode);
> default:
> - return InMemoryBuffer::create(Path, Size, Mode);
> + return createInMemoryBuffer(Path, Size, Mode);
> }
> }
More information about the llvm-commits
mailing list