source_buffer.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 "llvm/ADT/ScopeExit.h"
  6. #include <errno.h>
  7. #include <fcntl.h>
  8. #include <stdint.h>
  9. #include <sys/mman.h>
  10. #include <sys/stat.h>
  11. #include <unistd.h>
  12. #include <system_error>
  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. // Now that we have an open file, we need to close it on any error.
  30. auto closer = llvm::make_scope_exit([file_descriptor] { close(file_descriptor); });
  31. struct stat stat_buffer = {};
  32. errno = 0;
  33. if (fstat(file_descriptor, &stat_buffer) == -1)
  34. return ErrnoToError(errno);
  35. int64_t size = stat_buffer.st_size;
  36. if (size == 0) {
  37. // Nothing to do for an empty file.
  38. return {std::move(buffer)};
  39. }
  40. errno = 0;
  41. void* mapped_text = mmap(nullptr, size, PROT_READ, MAP_PRIVATE | MAP_POPULATE,
  42. file_descriptor, /*offset=*/0);
  43. if (mapped_text == MAP_FAILED)
  44. return ErrnoToError(errno);
  45. errno = 0;
  46. closer.release();
  47. if (close(file_descriptor) == -1) {
  48. // Try to unmap the text. No errer handling as this is just best-effort
  49. // cleanup.
  50. munmap(mapped_text, size);
  51. return ErrnoToError(errno);
  52. }
  53. buffer.text_ = llvm::StringRef(static_cast<const char*>(mapped_text), size);
  54. assert(!buffer.text_.empty() &&
  55. "Must not have an empty text when we have mapped data from a file!");
  56. return {std::move(buffer)};
  57. }
  58. SourceBuffer::~SourceBuffer() {
  59. if (is_string_rep_) {
  60. string_storage_.~decltype(string_storage_)();
  61. return;
  62. }
  63. if (!text_.empty()) {
  64. errno = 0;
  65. int result =
  66. munmap(const_cast<void*>((const void*)text_.data()), text_.size());
  67. (void)result;
  68. assert(result != -1 && "Unmapping text failed!");
  69. }
  70. }
  71. } // namespace Carbon