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.
This commit is contained in:
Jon Ross-Perkins
2025-03-26 19:12:10 +00:00
committed by GitHub
parent d317c56916
commit acbe6530c3
83 changed files with 560 additions and 526 deletions
+7 -7
View File
@@ -22,7 +22,7 @@ namespace Carbon::LanguageServer {
// A consumer for turning diagnostics into a `textDocument/publishDiagnostics`
// notification.
// https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_publishDiagnostics
class PublishDiagnosticConsumer : public DiagnosticConsumer {
class PublishDiagnosticConsumer : public Diagnostics::Consumer {
public:
// Initializes params with the target file information.
explicit PublishDiagnosticConsumer(Context* context,
@@ -31,14 +31,14 @@ class PublishDiagnosticConsumer : public DiagnosticConsumer {
: context_(context), params_{.uri = uri, .version = version} {}
// Turns a diagnostic into an LSP diagnostic.
auto HandleDiagnostic(Diagnostic diagnostic) -> void override {
auto HandleDiagnostic(Diagnostics::Diagnostic diagnostic) -> void override {
const auto& message = diagnostic.messages[0];
if (message.loc.filename != params_.uri.file()) {
// `pushDiagnostic` requires diagnostics to be associated with a location
// in the current file. Suppress diagnostics rooted in other files.
// TODO: Consider if there's a better way to handle this.
RawStringOstream stream;
StreamDiagnosticConsumer consumer(&stream);
Diagnostics::StreamConsumer consumer(&stream);
consumer.HandleDiagnostic(diagnostic);
CARBON_DIAGNOSTIC(LanguageServerDiagnosticInWrongFile, Warning,
@@ -68,7 +68,7 @@ class PublishDiagnosticConsumer : public DiagnosticConsumer {
private:
// Returns the LSP range for a diagnostic. Note that Carbon uses 1-based
// numbers while LSP uses 0-based.
auto GetRange(const DiagnosticLoc& loc) -> clang::clangd::Range {
auto GetRange(const Diagnostics::Loc& loc) -> clang::clangd::Range {
return {.start = {.line = loc.line_number - 1,
.character = loc.column_number - 1},
.end = {.line = loc.line_number,
@@ -76,7 +76,7 @@ class PublishDiagnosticConsumer : public DiagnosticConsumer {
}
// Converts a diagnostic level to an LSP severity.
auto GetSeverity(DiagnosticLevel level) -> int {
auto GetSeverity(Diagnostics::Level level) -> int {
// https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#diagnosticSeverity
enum class DiagnosticSeverity {
Error = 1,
@@ -86,9 +86,9 @@ class PublishDiagnosticConsumer : public DiagnosticConsumer {
};
switch (level) {
case DiagnosticLevel::Error:
case Diagnostics::Level::Error:
return static_cast<int>(DiagnosticSeverity::Error);
case DiagnosticLevel::Warning:
case Diagnostics::Level::Warning:
return static_cast<int>(DiagnosticSeverity::Warning);
default:
CARBON_FATAL("Unexpected diagnostic level: {0}", level);