source_buffer.cpp 3.2 KB

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