Files
carbon-lang/source/source_buffer.cpp
T
Chandler Carruth d4a2d435b8 Enable most relevant clang-tidy checks and fix uncovered issues. (#220)
Most of these were fixed automatically (including things like adding
`[[nodiscard]]` and such). A number of others required manual edits.
I think all of them were pretty nice improvements.

There were a few places where the issues really stem from external
constraints and I've disabled the checks: GoogleTest macros or the
specific LibFuzzer entry points.

The only other places I disabled are the implicit conversions to
a private `enum` in the classes wrapping those `enum`s. These implicit
conversions are necessarily implicit to serve their only purpose:
enabling their use in `switch` statements and `case` labels. When these
were highlighted, it showed that one of these was actually converting to
an *`int`*. I've switched that to use the private `enum` instead as
doing so is important to enable warnings on non-covering `switch`
statements over than `enum`. And indeed, there is a `switch` that was
was implicitly relying on falling through in this way, so I've added the
explicit documentation of the intentional pattern to address that
warning.

Sorry this is so large, all of this somewhat fell out of enabling the
`clang-tidy` checks. If it is too difficult to review as lump, I can
work on breaking it apart as needed. Just let me know.
2020-12-08 14:43:37 -08:00

95 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 <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#include <cerrno>
#include <cstdint>
#include <system_error>
#include "llvm/ADT/ScopeExit.h"
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*>(static_cast<const void*>(text_.data())),
text_.size());
(void)result;
assert(result != -1 && "Unmapping text failed!");
}
}
} // namespace Carbon