source_buffer.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 "toolchain/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 "common/check.h"
  13. #include "llvm/ADT/ScopeExit.h"
  14. namespace Carbon {
  15. auto SourceBuffer::CreateFromText(llvm::Twine text, llvm::StringRef filename)
  16. -> SourceBuffer {
  17. return SourceBuffer(filename, text.str());
  18. }
  19. static auto ErrnoToError(int errno_value) -> llvm::Error {
  20. return llvm::errorCodeToError(
  21. std::error_code(errno_value, std::generic_category()));
  22. }
  23. auto SourceBuffer::CreateFromFile(llvm::StringRef filename)
  24. -> llvm::Expected<SourceBuffer> {
  25. SourceBuffer buffer(filename);
  26. errno = 0;
  27. int file_descriptor = open(buffer.filename_.c_str(), O_RDONLY);
  28. if (file_descriptor == -1) {
  29. return ErrnoToError(errno);
  30. }
  31. // Now that we have an open file, we need to close it on any error.
  32. auto closer =
  33. llvm::make_scope_exit([file_descriptor] { close(file_descriptor); });
  34. struct stat stat_buffer = {};
  35. errno = 0;
  36. if (fstat(file_descriptor, &stat_buffer) == -1) {
  37. return ErrnoToError(errno);
  38. }
  39. int64_t size = stat_buffer.st_size;
  40. if (size == 0) {
  41. // Nothing to do for an empty file.
  42. return {std::move(buffer)};
  43. }
  44. errno = 0;
  45. void* mapped_text = mmap(nullptr, size, PROT_READ,
  46. #ifdef __APPLE__
  47. MAP_PRIVATE,
  48. #else
  49. MAP_PRIVATE | MAP_POPULATE,
  50. #endif
  51. file_descriptor, /*offset=*/0);
  52. // The `MAP_FAILED` macro may expand to a cast to pointer that `clang-tidy`
  53. // complains about.
  54. // NOLINTNEXTLINE(performance-no-int-to-ptr)
  55. if (mapped_text == MAP_FAILED) {
  56. return ErrnoToError(errno);
  57. }
  58. errno = 0;
  59. closer.release();
  60. if (close(file_descriptor) == -1) {
  61. // Try to unmap the text. No errer handling as this is just best-effort
  62. // cleanup.
  63. munmap(mapped_text, size);
  64. return ErrnoToError(errno);
  65. }
  66. buffer.text_ = llvm::StringRef(static_cast<const char*>(mapped_text), size);
  67. CHECK(!buffer.text_.empty())
  68. << "Must not have an empty text when we have mapped data from a file!";
  69. return {std::move(buffer)};
  70. }
  71. SourceBuffer::~SourceBuffer() {
  72. if (is_string_rep_) {
  73. string_storage_.~decltype(string_storage_)();
  74. return;
  75. }
  76. if (!text_.empty()) {
  77. errno = 0;
  78. int result =
  79. munmap(const_cast<void*>(static_cast<const void*>(text_.data())),
  80. text_.size());
  81. (void)result;
  82. CHECK(result != -1) << "Unmapping text failed!";
  83. }
  84. }
  85. } // namespace Carbon