Switch custom error stream output to diagnostic (#4846)

This switches most error printing to use diagnostics instead of direct
stream writes, even when not a specific file diagnostic. I'm allowing
empty filenames for this use-case.

This allows a little more specific testing to validate coverage of
output using the diagnostic coverage test. I'm adding a few tests to
cover things that weren't previously tested.

Separately, this also forces a little more standardization in format...
considering how changes like #4568 show effort being spent to _mirror_
diagnostic style, my thought is now to just use diagnostic code where
possible.

Note this also allows incrementally better testing of the language
server; I'm changing the crash fix from #4847 in favor of diagnostic
testing.

---------

Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
This commit is contained in:
Jon Ross-Perkins
2025-01-30 01:58:07 +00:00
committed by GitHub
co-authored by Chandler Carruth
parent 7d2958ad37
commit 7befe2ce9f
57 changed files with 379 additions and 174 deletions
+1
View File
@@ -13,6 +13,7 @@ cc_library(
deps = [
"//common:error",
"//toolchain/diagnostics:diagnostic_emitter",
"//toolchain/diagnostics:file_diagnostics",
"//toolchain/diagnostics:format_providers",
"@llvm-project//llvm:Support",
],
+3 -13
View File
@@ -7,18 +7,10 @@
#include <limits>
#include "llvm/Support/ErrorOr.h"
#include "toolchain/diagnostics/file_diagnostics.h"
namespace Carbon {
namespace {
struct FilenameConverter : DiagnosticConverter<llvm::StringRef> {
auto ConvertLoc(llvm::StringRef filename, ContextFnT /*context_fn*/) const
-> ConvertedDiagnosticLoc override {
return {.loc = {.filename = filename}, .last_byte_offset = -1};
}
};
} // namespace
auto SourceBuffer::MakeFromStdin(DiagnosticConsumer& consumer)
-> std::optional<SourceBuffer> {
return MakeFromMemoryBuffer(llvm::MemoryBuffer::getSTDIN(), "<stdin>",
@@ -29,8 +21,7 @@ auto SourceBuffer::MakeFromFile(llvm::vfs::FileSystem& fs,
llvm::StringRef filename,
DiagnosticConsumer& consumer)
-> std::optional<SourceBuffer> {
FilenameConverter converter;
DiagnosticEmitter<llvm::StringRef> emitter(converter, consumer);
FileDiagnosticEmitter emitter(&consumer);
llvm::ErrorOr<std::unique_ptr<llvm::vfs::File>> file =
fs.openFileForRead(filename);
@@ -64,8 +55,7 @@ auto SourceBuffer::MakeFromMemoryBuffer(
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> buffer,
llvm::StringRef filename, bool is_regular_file,
DiagnosticConsumer& consumer) -> std::optional<SourceBuffer> {
FilenameConverter converter;
DiagnosticEmitter<llvm::StringRef> emitter(converter, consumer);
FileDiagnosticEmitter emitter(&consumer);
if (buffer.getError()) {
CARBON_DIAGNOSTIC(ErrorReadingFile, Error, "error reading file: {0}",