source_buffer.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 <limits>
  12. #include <optional>
  13. #include <system_error>
  14. #include <utility>
  15. #include <variant>
  16. #include "common/check.h"
  17. #include "llvm/ADT/ScopeExit.h"
  18. #include "llvm/Support/Error.h"
  19. namespace Carbon {
  20. // Verifies that the content size is within limits.
  21. static auto CheckContentSize(int64_t size) -> llvm::Error {
  22. if (size < std::numeric_limits<int32_t>::max()) {
  23. return llvm::Error::success();
  24. }
  25. return llvm::createStringError(llvm::inconvertibleErrorCode(),
  26. "Input too large!");
  27. }
  28. auto SourceBuffer::CreateFromText(llvm::Twine text, llvm::StringRef filename)
  29. -> llvm::Expected<SourceBuffer> {
  30. std::string buffer = text.str();
  31. auto size_check = CheckContentSize(buffer.size());
  32. if (size_check) {
  33. return std::move(size_check);
  34. }
  35. return SourceBuffer(filename.str(), std::move(buffer));
  36. }
  37. static auto ErrnoToError(int errno_value) -> llvm::Error {
  38. return llvm::errorCodeToError(
  39. std::error_code(errno_value, std::generic_category()));
  40. }
  41. auto SourceBuffer::CreateFromFile(llvm::StringRef filename)
  42. -> llvm::Expected<SourceBuffer> {
  43. // Add storage to ensure there's a nul-terminator for open().
  44. std::string filename_str = filename.str();
  45. errno = 0;
  46. int file_descriptor = open(filename_str.c_str(), O_RDONLY);
  47. if (file_descriptor == -1) {
  48. return ErrnoToError(errno);
  49. }
  50. // Now that we have an open file, we need to close it on any error.
  51. auto closer =
  52. llvm::make_scope_exit([file_descriptor] { close(file_descriptor); });
  53. struct stat stat_buffer = {};
  54. errno = 0;
  55. if (fstat(file_descriptor, &stat_buffer) == -1) {
  56. return ErrnoToError(errno);
  57. }
  58. int64_t size = stat_buffer.st_size;
  59. if (size == 0) {
  60. // Rather than opening an empty file, create an empty buffer.
  61. return SourceBuffer(std::move(filename_str), std::string());
  62. }
  63. auto size_check = CheckContentSize(size);
  64. if (size_check) {
  65. return std::move(size_check);
  66. }
  67. errno = 0;
  68. void* mapped_text = mmap(nullptr, size, PROT_READ,
  69. #if defined(__linux__)
  70. MAP_PRIVATE | MAP_POPULATE,
  71. #else
  72. MAP_PRIVATE,
  73. #endif
  74. file_descriptor, /*offset=*/0);
  75. // The `MAP_FAILED` macro may expand to a cast to pointer that `clang-tidy`
  76. // complains about.
  77. // NOLINTNEXTLINE(performance-no-int-to-ptr)
  78. if (mapped_text == MAP_FAILED) {
  79. return ErrnoToError(errno);
  80. }
  81. errno = 0;
  82. closer.release();
  83. if (close(file_descriptor) == -1) {
  84. // Try to unmap the text. No error handling as this is just best-effort
  85. // cleanup.
  86. munmap(mapped_text, size);
  87. return ErrnoToError(errno);
  88. }
  89. return SourceBuffer(
  90. std::move(filename_str),
  91. llvm::StringRef(static_cast<const char*>(mapped_text), size));
  92. }
  93. SourceBuffer::SourceBuffer(SourceBuffer&& arg) noexcept
  94. // Sets Uninitialized to ensure the input doesn't release mmapped data.
  95. : content_mode_(
  96. std::exchange(arg.content_mode_, ContentMode::Uninitialized)),
  97. filename_(std::move(arg.filename_)),
  98. text_storage_(std::move(arg.text_storage_)),
  99. text_(content_mode_ == ContentMode::Owned ? text_storage_ : arg.text_) {}
  100. SourceBuffer::SourceBuffer(std::string filename, std::string text)
  101. : content_mode_(ContentMode::Owned),
  102. filename_(std::move(filename)),
  103. text_storage_(std::move(text)),
  104. text_(text_storage_) {}
  105. SourceBuffer::SourceBuffer(std::string filename, llvm::StringRef text)
  106. : content_mode_(ContentMode::MMapped),
  107. filename_(std::move(filename)),
  108. text_(text) {
  109. CARBON_CHECK(!text.empty())
  110. << "Must not have an empty text when we have mapped data from a file!";
  111. }
  112. SourceBuffer::~SourceBuffer() {
  113. if (content_mode_ == ContentMode::MMapped) {
  114. errno = 0;
  115. int result =
  116. munmap(const_cast<void*>(static_cast<const void*>(text_.data())),
  117. text_.size());
  118. CARBON_CHECK(result != -1) << "Unmapping text failed!";
  119. }
  120. }
  121. } // namespace Carbon