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
+31 -28
View File
@@ -1028,8 +1028,8 @@ TEST_F(LexerTest, TypeLiteralTooManyDigits) {
// We increase the number of digits until the first one that is to large.
Testing::MockDiagnosticConsumer consumer;
EXPECT_CALL(consumer, HandleDiagnostic(IsSingleDiagnostic(
DiagnosticKind::TooManyTypeBitWidthDigits,
DiagnosticLevel::Error, 1, 2, _)));
Diagnostics::Kind::TooManyTypeBitWidthDigits,
Diagnostics::Level::Error, 1, 2, _)));
std::string code = "i";
// A 128-bit APInt should be plenty large, but if needed in the future it can
// be widened without issue.
@@ -1061,8 +1061,8 @@ TEST_F(LexerTest, TypeLiteralTooManyDigits) {
// crashing or hanging, and show the correct number.
constexpr int Count = 10000;
EXPECT_CALL(consumer, HandleDiagnostic(IsSingleDiagnostic(
DiagnosticKind::TooManyTypeBitWidthDigits,
DiagnosticLevel::Error, 1, 2,
Diagnostics::Kind::TooManyTypeBitWidthDigits,
Diagnostics::Level::Error, 1, 2,
HasSubstr(llvm::formatv(" {0} ", Count)))));
code = "i";
code.append(Count, '9');
@@ -1083,72 +1083,75 @@ TEST_F(LexerTest, DiagnosticTrailingComment) {
Testing::MockDiagnosticConsumer consumer;
EXPECT_CALL(consumer, HandleDiagnostic(IsSingleDiagnostic(
DiagnosticKind::TrailingComment,
DiagnosticLevel::Error, 3, 19, _)));
Diagnostics::Kind::TrailingComment,
Diagnostics::Level::Error, 3, 19, _)));
compile_helper_.GetTokenizedBuffer(testcase, &consumer);
}
TEST_F(LexerTest, DiagnosticWhitespace) {
Testing::MockDiagnosticConsumer consumer;
EXPECT_CALL(consumer, HandleDiagnostic(IsSingleDiagnostic(
DiagnosticKind::NoWhitespaceAfterCommentIntroducer,
DiagnosticLevel::Error, 1, 3, _)));
EXPECT_CALL(consumer,
HandleDiagnostic(IsSingleDiagnostic(
Diagnostics::Kind::NoWhitespaceAfterCommentIntroducer,
Diagnostics::Level::Error, 1, 3, _)));
compile_helper_.GetTokenizedBuffer("//no space after comment", &consumer);
}
TEST_F(LexerTest, DiagnosticUnrecognizedEscape) {
Testing::MockDiagnosticConsumer consumer;
EXPECT_CALL(consumer, HandleDiagnostic(IsSingleDiagnostic(
DiagnosticKind::UnknownEscapeSequence,
DiagnosticLevel::Error, 1, 8, HasSubstr("`b`"))));
EXPECT_CALL(consumer,
HandleDiagnostic(IsSingleDiagnostic(
Diagnostics::Kind::UnknownEscapeSequence,
Diagnostics::Level::Error, 1, 8, HasSubstr("`b`"))));
compile_helper_.GetTokenizedBuffer(R"("hello\bworld")", &consumer);
}
TEST_F(LexerTest, DiagnosticBadHex) {
Testing::MockDiagnosticConsumer consumer;
EXPECT_CALL(consumer, HandleDiagnostic(IsSingleDiagnostic(
DiagnosticKind::HexadecimalEscapeMissingDigits,
DiagnosticLevel::Error, 1, 9, _)));
Diagnostics::Kind::HexadecimalEscapeMissingDigits,
Diagnostics::Level::Error, 1, 9, _)));
compile_helper_.GetTokenizedBuffer(R"("hello\xabworld")", &consumer);
}
TEST_F(LexerTest, DiagnosticInvalidDigit) {
Testing::MockDiagnosticConsumer consumer;
EXPECT_CALL(consumer, HandleDiagnostic(IsSingleDiagnostic(
DiagnosticKind::InvalidDigit,
DiagnosticLevel::Error, 1, 6, HasSubstr("'a'"))));
EXPECT_CALL(consumer,
HandleDiagnostic(IsSingleDiagnostic(
Diagnostics::Kind::InvalidDigit, Diagnostics::Level::Error, 1,
6, HasSubstr("'a'"))));
compile_helper_.GetTokenizedBuffer("0x123abc", &consumer);
}
TEST_F(LexerTest, DiagnosticCR) {
Testing::MockDiagnosticConsumer consumer;
EXPECT_CALL(consumer, HandleDiagnostic(IsSingleDiagnostic(
DiagnosticKind::UnsupportedCrLineEnding,
DiagnosticLevel::Error, 1, 1, _)));
Diagnostics::Kind::UnsupportedCrLineEnding,
Diagnostics::Level::Error, 1, 1, _)));
compile_helper_.GetTokenizedBuffer("\r", &consumer);
}
TEST_F(LexerTest, DiagnosticLfCr) {
Testing::MockDiagnosticConsumer consumer;
EXPECT_CALL(consumer, HandleDiagnostic(IsSingleDiagnostic(
DiagnosticKind::UnsupportedLfCrLineEnding,
DiagnosticLevel::Error, 2, 1, _)));
Diagnostics::Kind::UnsupportedLfCrLineEnding,
Diagnostics::Level::Error, 2, 1, _)));
compile_helper_.GetTokenizedBuffer("\n\r", &consumer);
}
TEST_F(LexerTest, DiagnosticMissingTerminator) {
Testing::MockDiagnosticConsumer consumer;
EXPECT_CALL(consumer, HandleDiagnostic(IsSingleDiagnostic(
DiagnosticKind::UnterminatedString,
DiagnosticLevel::Error, 1, 1, _)));
Diagnostics::Kind::UnterminatedString,
Diagnostics::Level::Error, 1, 1, _)));
compile_helper_.GetTokenizedBuffer(R"(#" ")", &consumer);
}
TEST_F(LexerTest, DiagnosticUnrecognizedChar) {
Testing::MockDiagnosticConsumer consumer;
EXPECT_CALL(consumer, HandleDiagnostic(IsSingleDiagnostic(
DiagnosticKind::UnrecognizedCharacters,
DiagnosticLevel::Error, 1, 1, _)));
Diagnostics::Kind::UnrecognizedCharacters,
Diagnostics::Level::Error, 1, 1, _)));
compile_helper_.GetTokenizedBuffer("\b", &consumer);
}
@@ -1161,9 +1164,9 @@ TEST_F(LexerTest, DiagnosticFileTooLarge) {
input += "{}\n";
}
EXPECT_CALL(consumer,
HandleDiagnostic(IsSingleDiagnostic(DiagnosticKind::TooManyTokens,
DiagnosticLevel::Error,
TokenIndex::Max / 2, 1, _)));
HandleDiagnostic(IsSingleDiagnostic(
Diagnostics::Kind::TooManyTokens, Diagnostics::Level::Error,
TokenIndex::Max / 2, 1, _)));
compile_helper_.GetTokenizedBuffer(input, &consumer);
}