source_buffer.cpp 3.2 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 "toolchain/source/source_buffer.h"
  5. #include <limits>
  6. #include "llvm/Support/ErrorOr.h"
  7. namespace Carbon {
  8. namespace {
  9. struct FilenameConverter : DiagnosticConverter<llvm::StringRef> {
  10. auto ConvertLoc(llvm::StringRef filename, ContextFnT /*context_fn*/) const
  11. -> DiagnosticLoc override {
  12. return {.filename = filename};
  13. }
  14. };
  15. } // namespace
  16. auto SourceBuffer::MakeFromStdin(DiagnosticConsumer& consumer)
  17. -> std::optional<SourceBuffer> {
  18. return MakeFromMemoryBuffer(llvm::MemoryBuffer::getSTDIN(), "<stdin>",
  19. /*is_regular_file=*/false, consumer);
  20. }
  21. auto SourceBuffer::MakeFromFile(llvm::vfs::FileSystem& fs,
  22. llvm::StringRef filename,
  23. DiagnosticConsumer& consumer)
  24. -> std::optional<SourceBuffer> {
  25. FilenameConverter converter;
  26. DiagnosticEmitter<llvm::StringRef> emitter(converter, consumer);
  27. llvm::ErrorOr<std::unique_ptr<llvm::vfs::File>> file =
  28. fs.openFileForRead(filename);
  29. if (file.getError()) {
  30. CARBON_DIAGNOSTIC(ErrorOpeningFile, Error,
  31. "Error opening file for read: {0}", std::string);
  32. emitter.Emit(filename, ErrorOpeningFile, file.getError().message());
  33. return std::nullopt;
  34. }
  35. llvm::ErrorOr<llvm::vfs::Status> status = (*file)->status();
  36. if (status.getError()) {
  37. CARBON_DIAGNOSTIC(ErrorStattingFile, Error, "Error statting file: {0}",
  38. std::string);
  39. emitter.Emit(filename, ErrorStattingFile, file.getError().message());
  40. return std::nullopt;
  41. }
  42. // `stat` on a file without a known size gives a size of 0, which causes
  43. // `llvm::vfs::File::getBuffer` to produce an empty buffer. Use a size of -1
  44. // in this case so we get the complete file contents.
  45. bool is_regular_file = status->isRegularFile();
  46. int64_t size = is_regular_file ? status->getSize() : -1;
  47. return MakeFromMemoryBuffer(
  48. (*file)->getBuffer(filename, size, /*RequiresNullTerminator=*/false),
  49. filename, is_regular_file, consumer);
  50. }
  51. auto SourceBuffer::MakeFromMemoryBuffer(
  52. llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> buffer,
  53. llvm::StringRef filename, bool is_regular_file,
  54. DiagnosticConsumer& consumer) -> std::optional<SourceBuffer> {
  55. FilenameConverter converter;
  56. DiagnosticEmitter<llvm::StringRef> emitter(converter, consumer);
  57. if (buffer.getError()) {
  58. CARBON_DIAGNOSTIC(ErrorReadingFile, Error, "Error reading file: {0}",
  59. std::string);
  60. emitter.Emit(filename, ErrorReadingFile, buffer.getError().message());
  61. return std::nullopt;
  62. }
  63. if (buffer.get()->getBufferSize() >= std::numeric_limits<int32_t>::max()) {
  64. CARBON_DIAGNOSTIC(FileTooLarge, Error,
  65. "File is over the 2GiB input limit; size is {0} bytes.",
  66. int64_t);
  67. emitter.Emit(filename, FileTooLarge, buffer.get()->getBufferSize());
  68. return std::nullopt;
  69. }
  70. return SourceBuffer(filename.str(), std::move(buffer.get()), is_regular_file);
  71. }
  72. } // namespace Carbon