source_buffer.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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,
  45. #ifdef __APPLE__
  46. MAP_PRIVATE,
  47. #else
  48. MAP_PRIVATE | MAP_POPULATE,
  49. #endif
  50. file_descriptor, /*offset=*/0);
  51. if (mapped_text == MAP_FAILED) {
  52. return ErrnoToError(errno);
  53. }
  54. errno = 0;
  55. closer.release();
  56. if (close(file_descriptor) == -1) {
  57. // Try to unmap the text. No errer handling as this is just best-effort
  58. // cleanup.
  59. munmap(mapped_text, size);
  60. return ErrnoToError(errno);
  61. }
  62. buffer.text_ = llvm::StringRef(static_cast<const char*>(mapped_text), size);
  63. assert(!buffer.text_.empty() &&
  64. "Must not have an empty text when we have mapped data from a file!");
  65. return {std::move(buffer)};
  66. }
  67. SourceBuffer::~SourceBuffer() {
  68. if (is_string_rep_) {
  69. string_storage_.~decltype(string_storage_)();
  70. return;
  71. }
  72. if (!text_.empty()) {
  73. errno = 0;
  74. int result =
  75. munmap(const_cast<void*>(static_cast<const void*>(text_.data())),
  76. text_.size());
  77. (void)result;
  78. assert(result != -1 && "Unmapping text failed!");
  79. }
  80. }
  81. } // namespace Carbon