//===- FileOutputBuffer.cpp - File Output Buffer ----------------*- 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 // //===----------------------------------------------------------------------===// // // Utility for creating a in-memory buffer that will be written to a file. // //===----------------------------------------------------------------------===// #include "llvm/Support/FileOutputBuffer.h" #include "llvm/Support/Errc.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/Memory.h" #include "llvm/Support/TimeProfiler.h" #include <system_error> #if !defined(_MSC_VER) && !defined(__MINGW32__) #include <unistd.h> #else #include <io.h> #endif usingnamespacellvm; usingnamespacellvm::sys; namespace { // A FileOutputBuffer which creates a temporary file in the same directory // as the final output file. The final output file is atomically replaced // with the temporary file on commit(). class OnDiskBuffer : public FileOutputBuffer { … }; // A FileOutputBuffer which keeps data in memory and writes to the final // output file on commit(). This is used only when we cannot use OnDiskBuffer. class InMemoryBuffer : public FileOutputBuffer { … }; } // namespace static Expected<std::unique_ptr<InMemoryBuffer>> createInMemoryBuffer(StringRef Path, size_t Size, unsigned Mode) { … } static Expected<std::unique_ptr<FileOutputBuffer>> createOnDiskBuffer(StringRef Path, size_t Size, unsigned Mode) { … } // Create an instance of FileOutputBuffer. Expected<std::unique_ptr<FileOutputBuffer>> FileOutputBuffer::create(StringRef Path, size_t Size, unsigned Flags) { … }