freebsd-dev/contrib/llvm/lib/Support/FileOutputBuffer.cpp
Dimitry Andric f785676f2a Upgrade our copy of llvm/clang to 3.4 release. This version supports
all of the features in the current working draft of the upcoming C++
standard, provisionally named C++1y.

The code generator's performance is greatly increased, and the loop
auto-vectorizer is now enabled at -Os and -O2 in addition to -O3.  The
PowerPC backend has made several major improvements to code generation
quality and compile time, and the X86, SPARC, ARM32, Aarch64 and SystemZ
backends have all seen major feature work.

Release notes for llvm and clang can be found here:
<http://llvm.org/releases/3.4/docs/ReleaseNotes.html>
<http://llvm.org/releases/3.4/tools/clang/docs/ReleaseNotes.html>

MFC after:	1 month
2014-02-16 19:44:07 +00:00

105 lines
3.2 KiB
C++

//===- FileOutputBuffer.cpp - File Output Buffer ----------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Utility for creating a in-memory buffer that will be written to a file.
//
//===----------------------------------------------------------------------===//
#include "llvm/Support/FileOutputBuffer.h"
#include "llvm/ADT/OwningPtr.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/system_error.h"
using llvm::sys::fs::mapped_file_region;
namespace llvm {
FileOutputBuffer::FileOutputBuffer(mapped_file_region * R,
StringRef Path, StringRef TmpPath)
: Region(R)
, FinalPath(Path)
, TempPath(TmpPath) {
}
FileOutputBuffer::~FileOutputBuffer() {
bool Existed;
sys::fs::remove(Twine(TempPath), Existed);
}
error_code FileOutputBuffer::create(StringRef FilePath,
size_t Size,
OwningPtr<FileOutputBuffer> &Result,
unsigned Flags) {
// If file already exists, it must be a regular file (to be mappable).
sys::fs::file_status Stat;
error_code EC = sys::fs::status(FilePath, Stat);
switch (Stat.type()) {
case sys::fs::file_type::file_not_found:
// If file does not exist, we'll create one.
break;
case sys::fs::file_type::regular_file: {
// If file is not currently writable, error out.
// FIXME: There is no sys::fs:: api for checking this.
// FIXME: In posix, you use the access() call to check this.
}
break;
default:
if (EC)
return EC;
else
return make_error_code(errc::operation_not_permitted);
}
// Delete target file.
bool Existed;
EC = sys::fs::remove(FilePath, Existed);
if (EC)
return EC;
unsigned Mode = sys::fs::all_read | sys::fs::all_write;
// If requested, make the output file executable.
if (Flags & F_executable)
Mode |= sys::fs::all_exe;
// Create new file in same directory but with random name.
SmallString<128> TempFilePath;
int FD;
EC = sys::fs::createUniqueFile(Twine(FilePath) + ".tmp%%%%%%%", FD,
TempFilePath, Mode);
if (EC)
return EC;
OwningPtr<mapped_file_region> MappedFile(new mapped_file_region(
FD, true, mapped_file_region::readwrite, Size, 0, EC));
if (EC)
return EC;
Result.reset(new FileOutputBuffer(MappedFile.get(), FilePath, TempFilePath));
if (Result)
MappedFile.take();
return error_code::success();
}
error_code FileOutputBuffer::commit(int64_t NewSmallerSize) {
// Unmap buffer, letting OS flush dirty pages to file on disk.
Region.reset(0);
// If requested, resize file as part of commit.
if ( NewSmallerSize != -1 ) {
error_code EC = sys::fs::resize_file(Twine(TempPath), NewSmallerSize);
if (EC)
return EC;
}
// Rename file to final name.
return sys::fs::rename(Twine(TempPath), Twine(FinalPath));
}
} // namespace