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 -1
View File
@@ -19,9 +19,9 @@ cc_library(
":context",
":incoming_messages",
":outgoing_messages",
"//common:error",
"//common:ostream",
"//common:raw_string_ostream",
"//toolchain/diagnostics:diagnostic_emitter",
"@llvm-project//clang-tools-extra/clangd:ClangDaemon",
],
)
@@ -8,6 +8,7 @@
#include "clang-tools-extra/clangd/Transport.h"
#include "clang-tools-extra/clangd/support/Logger.h"
#include "common/raw_string_ostream.h"
#include "toolchain/diagnostics/diagnostic_emitter.h"
#include "toolchain/language_server/context.h"
#include "toolchain/language_server/incoming_messages.h"
#include "toolchain/language_server/outgoing_messages.h"
@@ -15,7 +16,8 @@
namespace Carbon::LanguageServer {
auto Run(FILE* input_stream, llvm::raw_ostream& output_stream,
llvm::raw_ostream& error_stream) -> ErrorOr<Success> {
llvm::raw_ostream& error_stream, DiagnosticConsumer& consumer)
-> bool {
// TODO: Consider implementing a custom logger that splits vlog to
// vlog_stream when provided. For now, this disables verbose logging.
clang::clangd::StreamLogger logger(error_stream, clang::clangd::Logger::Info);
@@ -36,10 +38,12 @@ auto Run(FILE* input_stream, llvm::raw_ostream& output_stream,
if (err) {
RawStringOstream out;
out << err;
return Error(out.TakeStr());
} else {
return Success();
CARBON_DIAGNOSTIC(LanguageServerTransportError, Error, "{0}", std::string);
NoLocDiagnosticEmitter emitter(&consumer);
emitter.Emit(LanguageServerTransportError, out.TakeStr());
return false;
}
return true;
}
} // namespace Carbon::LanguageServer
+4 -3
View File
@@ -5,15 +5,16 @@
#ifndef CARBON_TOOLCHAIN_LANGUAGE_SERVER_LANGUAGE_SERVER_H_
#define CARBON_TOOLCHAIN_LANGUAGE_SERVER_LANGUAGE_SERVER_H_
#include "common/error.h"
#include "common/ostream.h"
#include "toolchain/diagnostics/diagnostic_consumer.h"
namespace Carbon::LanguageServer {
// Start the language server. input_stream and output_stream are used by LSP;
// error_stream is primarily for errors that don't fit into LSP.
// error_stream is primarily for errors that don't fit into LSP. Returns true if
// the server cleanly exits.
auto Run(FILE* input_stream, llvm::raw_ostream& output_stream,
llvm::raw_ostream& error_stream) -> ErrorOr<Success>;
llvm::raw_ostream& error_stream, DiagnosticConsumer& consumer) -> bool;
} // namespace Carbon::LanguageServer
+1 -1
View File
@@ -11,6 +11,6 @@
// --- STDIN
// --- AUTOUPDATE-SPLIT
// CHECK:STDERR: error: Input/output error
// CHECK:STDERR: error: Input/output error [LanguageServerTransportError]
// CHECK:STDERR:
// CHECK:STDOUT:
+15
View File
@@ -0,0 +1,15 @@
// 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
//
// AUTOUPDATE
// TIP: To test this file alone, run:
// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/language_server/testdata/fail_no_stdin.carbon
// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/language_server/testdata/fail_no_stdin.carbon
// --- AUTOUPDATE-SPLIT
// CHECK:STDERR: error: language-server requires input_stream [LanguageServerMissingInputStream]
// CHECK:STDERR:
// CHECK:STDOUT: