source_buffer.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "source/source_buffer.h"
  5. #include <fcntl.h>
  6. #include <sys/mman.h>
  7. #include <sys/stat.h>
  8. #include <unistd.h>
  9. #include <cerrno>
  10. #include <cstdint>
  11. #include <system_error>
  12. #include "llvm/ADT/ScopeExit.h"
  13. namespace Carbon {
  14. auto SourceBuffer::CreateFromText(llvm::Twine text, llvm::StringRef filename)
  15. -> SourceBuffer {
  16. return SourceBuffer(filename, text.str());
  17. }
  18. static auto ErrnoToError(int errno_value) -> llvm::Error {
  19. return llvm::errorCodeToError(
  20. std::error_code(errno_value, std::generic_category()));
  21. }
  22. auto SourceBuffer::CreateFromFile(llvm::StringRef filename)
  23. -> llvm::Expected<SourceBuffer> {
  24. SourceBuffer buffer(filename);
  25. errno = 0;
  26. int file_descriptor = open(buffer.filename_.c_str(), O_RDONLY);
  27. if (file_descriptor == -1) {
  28. return ErrnoToError(errno);
  29. }
  30. // Now that we have an open file, we need to close it on any error.
  31. auto closer =
  32. llvm::make_scope_exit([file_descriptor] { close(file_descriptor); });
  33. struct stat stat_buffer = {};
  34. errno = 0;
  35. if (fstat(file_descriptor, &stat_buffer) == -1) {
  36. return ErrnoToError(errno);
  37. }
  38. int64_t size = stat_buffer.st_size;
  39. if (size == 0) {
  40. // Nothing to do for an empty file.
  41. return {std::move(buffer)};
  42. }
  43. errno = 0;
  44. void* mapped_text = mmap(nullptr, size, PROT_READ, MAP_PRIVATE | MAP_POPULATE,
  45. file_descriptor, /*offset=*/0);
  46. if (mapped_text == MAP_FAILED) {
  47. return ErrnoToError(errno);
  48. }
  49. errno = 0;
  50. closer.release();
  51. if (close(file_descriptor) == -1) {
  52. // Try to unmap the text. No errer handling as this is just best-effort
  53. // cleanup.
  54. munmap(mapped_text, size);
  55. return ErrnoToError(errno);
  56. }
  57. buffer.text_ = llvm::StringRef(static_cast<const char*>(mapped_text), size);
  58. assert(!buffer.text_.empty() &&
  59. "Must not have an empty text when we have mapped data from a file!");
  60. return {std::move(buffer)};
  61. }
  62. SourceBuffer::~SourceBuffer() {
  63. if (is_string_rep_) {
  64. string_storage_.~decltype(string_storage_)();
  65. return;
  66. }
  67. if (!text_.empty()) {
  68. errno = 0;
  69. int result =
  70. munmap(const_cast<void*>(static_cast<const void*>(text_.data())),
  71. text_.size());
  72. (void)result;
  73. assert(result != -1 && "Unmapping text failed!");
  74. }
  75. }
  76. } // namespace Carbon