Files
carbon-lang/source/source_buffer.cpp
T
Chandler Carruth 2ddf0936fd Merge the source library from the toolchain repository. (#210)
This library manages buffers of source code, either in-memory or mapped
from the filesystem. At the moment it is a bit simplistic and assumes
`mmap` is available in its implementation details. We should make this
more portable in the future, but this is currently just a direct copy
from the toolchain repository.
2020-12-04 22:39:11 -08:00

89 lines
2.4 KiB
C++

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "source/source_buffer.h"
#include "llvm/ADT/ScopeExit.h"
#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#include <system_error>
namespace Carbon {
auto SourceBuffer::CreateFromText(llvm::Twine text, llvm::StringRef filename)
-> SourceBuffer {
return SourceBuffer(filename, text.str());
}
static auto ErrnoToError(int errno_value) -> llvm::Error {
return llvm::errorCodeToError(
std::error_code(errno_value, std::generic_category()));
}
auto SourceBuffer::CreateFromFile(llvm::StringRef filename)
-> llvm::Expected<SourceBuffer> {
SourceBuffer buffer(filename);
errno = 0;
int file_descriptor = open(buffer.filename_.c_str(), O_RDONLY);
if (file_descriptor == -1)
return ErrnoToError(errno);
// Now that we have an open file, we need to close it on any error.
auto closer = llvm::make_scope_exit([file_descriptor] { close(file_descriptor); });
struct stat stat_buffer = {};
errno = 0;
if (fstat(file_descriptor, &stat_buffer) == -1)
return ErrnoToError(errno);
int64_t size = stat_buffer.st_size;
if (size == 0) {
// Nothing to do for an empty file.
return {std::move(buffer)};
}
errno = 0;
void* mapped_text = mmap(nullptr, size, PROT_READ, MAP_PRIVATE | MAP_POPULATE,
file_descriptor, /*offset=*/0);
if (mapped_text == MAP_FAILED)
return ErrnoToError(errno);
errno = 0;
closer.release();
if (close(file_descriptor) == -1) {
// Try to unmap the text. No errer handling as this is just best-effort
// cleanup.
munmap(mapped_text, size);
return ErrnoToError(errno);
}
buffer.text_ = llvm::StringRef(static_cast<const char*>(mapped_text), size);
assert(!buffer.text_.empty() &&
"Must not have an empty text when we have mapped data from a file!");
return {std::move(buffer)};
}
SourceBuffer::~SourceBuffer() {
if (is_string_rep_) {
string_storage_.~decltype(string_storage_)();
return;
}
if (!text_.empty()) {
errno = 0;
int result =
munmap(const_cast<void*>((const void*)text_.data()), text_.size());
(void)result;
assert(result != -1 && "Unmapping text failed!");
}
}
} // namespace Carbon