Files
carbon-lang/toolchain/source/source_buffer.cpp
T
Jon Ross-Perkins acbe6530c3 Move diagnostics into a namespace (#5173)
What this really does is avoids shadowing names, so that we can
comfortable have things like `Check::DiagnosticEmitter` or
`Check::DiagnosticLoc` without shadowing being a concern.

Note, down this path I'm also thinking about:

- Renaming misc DiagnosticConsumer/DiagnosticEmitter classes, possibly
just to DiagnosticConsumer/DiagnosticEmitter (so
`Check::DiagnosticEmitter` instead of `SemIRLocDiagnosticEmitter`).
- Dropping `Diagnostic` from `Emitter::DiagnosticBuilder`.
- But not for `Check::DiagnosticBuilder`, because `Check::Builder` would
be ambiguous.
- Renaming diagnostics/diagnostic_* to drop "diagnostic".

[Discussion about SemIRLoc ->
DiagnosticLoc](https://discord.com/channels/655572317891461132/655578254970716160/1353771570463768698)
reminded me of this (in particular the older [Check::DiagnosticBuilder
discussion](https://discord.com/channels/655572317891461132/655578254970716160/1344363562608627763)),
but I'd only do that rename if there's matching consensus about a path
forward where we keep SemIRLoc, and in a way that it's only ever used
for diagnostics (the divergence from which is at the root of current
LocId discussion).

I'm trying to keep that separate from a namespace addition for clarity.
2025-03-26 19:12:10 +00:00

88 lines
3.3 KiB
C++

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "toolchain/source/source_buffer.h"
#include <limits>
#include "llvm/Support/ErrorOr.h"
#include "toolchain/diagnostics/file_diagnostics.h"
namespace Carbon {
auto SourceBuffer::MakeFromStdin(Diagnostics::Consumer& consumer)
-> std::optional<SourceBuffer> {
return MakeFromMemoryBuffer(llvm::MemoryBuffer::getSTDIN(), "<stdin>",
/*is_regular_file=*/false, consumer);
}
auto SourceBuffer::MakeFromFile(llvm::vfs::FileSystem& fs,
llvm::StringRef filename,
Diagnostics::Consumer& consumer)
-> std::optional<SourceBuffer> {
Diagnostics::FileEmitter emitter(&consumer);
llvm::ErrorOr<std::unique_ptr<llvm::vfs::File>> file =
fs.openFileForRead(filename);
if (file.getError()) {
CARBON_DIAGNOSTIC(ErrorOpeningFile, Error,
"error opening file for read: {0}", std::string);
emitter.Emit(filename, ErrorOpeningFile, file.getError().message());
return std::nullopt;
}
llvm::ErrorOr<llvm::vfs::Status> status = (*file)->status();
if (status.getError()) {
CARBON_DIAGNOSTIC(ErrorStattingFile, Error, "error statting file: {0}",
std::string);
emitter.Emit(filename, ErrorStattingFile, file.getError().message());
return std::nullopt;
}
// `stat` on a file without a known size gives a size of 0, which causes
// `llvm::vfs::File::getBuffer` to produce an empty buffer. Use a size of -1
// in this case so we get the complete file contents.
bool is_regular_file = status->isRegularFile();
int64_t size = is_regular_file ? status->getSize() : -1;
return MakeFromMemoryBuffer(
(*file)->getBuffer(filename, size, /*RequiresNullTerminator=*/false),
filename, is_regular_file, consumer);
}
auto SourceBuffer::MakeFromStringCopy(llvm::StringRef filename,
llvm::StringRef text,
Diagnostics::Consumer& consumer)
-> std::optional<SourceBuffer> {
return MakeFromMemoryBuffer(
llvm::MemoryBuffer::getMemBufferCopy(text, filename), filename,
/*is_regular_file=*/true, consumer);
}
auto SourceBuffer::MakeFromMemoryBuffer(
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> buffer,
llvm::StringRef filename, bool is_regular_file,
Diagnostics::Consumer& consumer) -> std::optional<SourceBuffer> {
Diagnostics::FileEmitter emitter(&consumer);
if (buffer.getError()) {
CARBON_DIAGNOSTIC(ErrorReadingFile, Error, "error reading file: {0}",
std::string);
emitter.Emit(filename, ErrorReadingFile, buffer.getError().message());
return std::nullopt;
}
if (buffer.get()->getBufferSize() >= std::numeric_limits<int32_t>::max()) {
CARBON_DIAGNOSTIC(FileTooLarge, Error,
"file is over the 2GiB input limit; size is {0} bytes",
int64_t);
emitter.Emit(filename, FileTooLarge, buffer.get()->getBufferSize());
return std::nullopt;
}
return SourceBuffer(filename.str(), std::move(buffer.get()), is_regular_file);
}
} // namespace Carbon