source_buffer.cpp 3.2 KB

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