source_buffer.cpp 4.2 KB

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