mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 19:10:14 +01:00
Switch SourceBuffer to diagnostics. (#3197)
This updates SourceBuffer to diagnostics. Some additional edits to diagnostics were necessary due to issues moving arguments around, which seems to stem from a compile error with clang 14 (fixed in later versions). --------- Co-authored-by: Richard Smith <richard@metafoo.co.uk>
This commit is contained in:
co-authored by
Richard Smith
parent
71e9fb7511
commit
bc63e6ae0a
@@ -12,6 +12,7 @@ cc_library(
|
||||
hdrs = ["source_buffer.h"],
|
||||
deps = [
|
||||
"//common:error",
|
||||
"//toolchain/diagnostics:diagnostic_emitter",
|
||||
"@llvm-project//llvm:Support",
|
||||
],
|
||||
)
|
||||
@@ -24,6 +25,7 @@ cc_test(
|
||||
":source_buffer",
|
||||
"//common:check",
|
||||
"//testing/base:gtest_main",
|
||||
"//toolchain/diagnostics:diagnostic_emitter",
|
||||
"@com_google_googletest//:gtest",
|
||||
"@llvm-project//llvm:Support",
|
||||
],
|
||||
|
||||
@@ -10,36 +10,52 @@
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
namespace {
|
||||
struct FilenameTranslator : DiagnosticLocationTranslator<llvm::StringRef> {
|
||||
auto GetLocation(llvm::StringRef filename) -> DiagnosticLocation override {
|
||||
return {.file_name = filename};
|
||||
}
|
||||
};
|
||||
} // namespace
|
||||
|
||||
auto SourceBuffer::CreateFromFile(llvm::vfs::FileSystem& fs,
|
||||
llvm::raw_ostream& error_stream,
|
||||
llvm::StringRef filename)
|
||||
llvm::StringRef filename,
|
||||
DiagnosticConsumer& consumer)
|
||||
-> std::optional<SourceBuffer> {
|
||||
FilenameTranslator translator;
|
||||
DiagnosticEmitter<llvm::StringRef> emitter(translator, consumer);
|
||||
|
||||
llvm::ErrorOr<std::unique_ptr<llvm::vfs::File>> file =
|
||||
fs.openFileForRead(filename);
|
||||
if (file.getError()) {
|
||||
error_stream << "Error opening `" << filename
|
||||
<< "`: " << file.getError().message();
|
||||
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()) {
|
||||
error_stream << "Error getting status for `" << filename
|
||||
<< "`: " << file.getError().message();
|
||||
CARBON_DIAGNOSTIC(ErrorStattingFile, Error, "Error statting file: {0}",
|
||||
std::string);
|
||||
emitter.Emit(filename, ErrorStattingFile, file.getError().message());
|
||||
return std::nullopt;
|
||||
}
|
||||
auto size = status->getSize();
|
||||
int64_t size = status->getSize();
|
||||
if (size >= std::numeric_limits<int32_t>::max()) {
|
||||
error_stream << "Cannot load `" << filename
|
||||
<< "`: file is over the 2GiB input limit.";
|
||||
CARBON_DIAGNOSTIC(FileTooLarge, Error,
|
||||
"File is over the 2GiB input limit; size is {0} bytes.",
|
||||
int64_t);
|
||||
emitter.Emit(filename, FileTooLarge, size);
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> buffer =
|
||||
(*file)->getBuffer(filename, size, /*RequiresNullTerminator=*/false);
|
||||
if (buffer.getError()) {
|
||||
error_stream << "Error reading `" << filename
|
||||
<< "`: " << file.getError().message();
|
||||
CARBON_DIAGNOSTIC(ErrorReadingFile, Error, "Error reading file: {0}",
|
||||
std::string);
|
||||
emitter.Emit(filename, ErrorReadingFile, file.getError().message());
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#include "llvm/ADT/StringRef.h"
|
||||
#include "llvm/Support/MemoryBuffer.h"
|
||||
#include "llvm/Support/VirtualFileSystem.h"
|
||||
#include "toolchain/diagnostics/diagnostic_emitter.h"
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
@@ -35,10 +36,9 @@ class SourceBuffer {
|
||||
public:
|
||||
// Opens the requested file. Returns a SourceBuffer on success. Prints an
|
||||
// error and returns nullopt on failure.
|
||||
// TODO: Switch to using diagnostics.
|
||||
static auto CreateFromFile(llvm::vfs::FileSystem& fs,
|
||||
llvm::raw_ostream& error_stream,
|
||||
llvm::StringRef filename)
|
||||
llvm::StringRef filename,
|
||||
DiagnosticConsumer& consumer)
|
||||
-> std::optional<SourceBuffer>;
|
||||
|
||||
// Use one of the factory functions above to create a source buffer.
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
|
||||
#include "common/check.h"
|
||||
#include "llvm/Support/VirtualFileSystem.h"
|
||||
#include "toolchain/diagnostics/diagnostic_emitter.h"
|
||||
|
||||
namespace Carbon::Testing {
|
||||
namespace {
|
||||
@@ -16,7 +17,8 @@ static constexpr llvm::StringLiteral TestFileName = "test.carbon";
|
||||
|
||||
TEST(SourceBufferTest, MissingFile) {
|
||||
llvm::vfs::InMemoryFileSystem fs;
|
||||
auto buffer = SourceBuffer::CreateFromFile(fs, llvm::errs(), TestFileName);
|
||||
auto buffer = SourceBuffer::CreateFromFile(fs, TestFileName,
|
||||
ConsoleDiagnosticConsumer());
|
||||
EXPECT_FALSE(buffer);
|
||||
}
|
||||
|
||||
@@ -25,7 +27,8 @@ TEST(SourceBufferTest, SimpleFile) {
|
||||
CARBON_CHECK(fs.addFile(TestFileName, /*ModificationTime=*/0,
|
||||
llvm::MemoryBuffer::getMemBuffer("Hello World")));
|
||||
|
||||
auto buffer = SourceBuffer::CreateFromFile(fs, llvm::errs(), TestFileName);
|
||||
auto buffer = SourceBuffer::CreateFromFile(fs, TestFileName,
|
||||
ConsoleDiagnosticConsumer());
|
||||
ASSERT_TRUE(buffer);
|
||||
|
||||
EXPECT_EQ(TestFileName, buffer->filename());
|
||||
@@ -41,7 +44,8 @@ TEST(SourceBufferTest, NoNull) {
|
||||
/*BufferName=*/"",
|
||||
/*RequiresNullTerminator=*/false)));
|
||||
|
||||
auto buffer = SourceBuffer::CreateFromFile(fs, llvm::errs(), TestFileName);
|
||||
auto buffer = SourceBuffer::CreateFromFile(fs, TestFileName,
|
||||
ConsoleDiagnosticConsumer());
|
||||
ASSERT_TRUE(buffer);
|
||||
|
||||
EXPECT_EQ(TestFileName, buffer->filename());
|
||||
@@ -53,7 +57,8 @@ TEST(SourceBufferTest, EmptyFile) {
|
||||
CARBON_CHECK(fs.addFile(TestFileName, /*ModificationTime=*/0,
|
||||
llvm::MemoryBuffer::getMemBuffer("")));
|
||||
|
||||
auto buffer = SourceBuffer::CreateFromFile(fs, llvm::errs(), TestFileName);
|
||||
auto buffer = SourceBuffer::CreateFromFile(fs, TestFileName,
|
||||
ConsoleDiagnosticConsumer());
|
||||
ASSERT_TRUE(buffer);
|
||||
|
||||
EXPECT_EQ(TestFileName, buffer->filename());
|
||||
|
||||
Reference in New Issue
Block a user