Restructure Diagnostic objects to allow late formatting (#1131)

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
This commit is contained in:
Jon Meow
2022-04-04 10:59:09 -07:00
committed by GitHub
co-authored by Richard Smith
parent 014c3847b3
commit aaca540a05
22 changed files with 661 additions and 765 deletions
+37 -44
View File
@@ -24,6 +24,7 @@
namespace Carbon::Testing {
namespace {
using ::testing::_;
using ::testing::ElementsAre;
using ::testing::Eq;
using ::testing::HasSubstr;
@@ -934,25 +935,26 @@ TEST_F(LexerTest, TypeLiterals) {
TEST_F(LexerTest, TypeLiteralTooManyDigits) {
std::string code = "i";
code.append(10000, '9');
constexpr int Count = 10000;
code.append(Count, '9');
Testing::MockDiagnosticConsumer consumer;
EXPECT_CALL(
consumer,
HandleDiagnostic(AllOf(
DiagnosticAt(1, 2),
DiagnosticMessage(HasSubstr("Found a sequence of 10000 digits")))));
EXPECT_CALL(consumer,
HandleDiagnostic(IsDiagnostic(
DiagnosticKind::TooManyDigits, DiagnosticLevel::Error, 1, 2,
HasSubstr(llvm::formatv(" {0} ", Count)))));
auto buffer = Lex(code, consumer);
EXPECT_TRUE(buffer.has_errors());
ASSERT_THAT(buffer,
HasTokens(llvm::ArrayRef<ExpectedToken>{
{.kind = TokenKind::Error(),
.line = 1,
.column = 1,
.indent_column = 1,
.text = {code}},
{.kind = TokenKind::EndOfFile(), .line = 1, .column = 10002},
}));
ASSERT_THAT(
buffer,
HasTokens(llvm::ArrayRef<ExpectedToken>{
{.kind = TokenKind::Error(),
.line = 1,
.column = 1,
.indent_column = 1,
.text = {code}},
{.kind = TokenKind::EndOfFile(), .line = 1, .column = Count + 2},
}));
}
TEST_F(LexerTest, DiagnosticTrailingComment) {
@@ -962,66 +964,57 @@ TEST_F(LexerTest, DiagnosticTrailingComment) {
)";
Testing::MockDiagnosticConsumer consumer;
EXPECT_CALL(consumer, HandleDiagnostic(AllOf(
DiagnosticAt(3, 19),
DiagnosticMessage(HasSubstr("Trailing comment")))));
EXPECT_CALL(consumer,
HandleDiagnostic(IsDiagnostic(DiagnosticKind::TrailingComment,
DiagnosticLevel::Error, 3, 19, _)));
Lex(testcase, consumer);
}
TEST_F(LexerTest, DiagnosticWhitespace) {
Testing::MockDiagnosticConsumer consumer;
EXPECT_CALL(consumer,
HandleDiagnostic(AllOf(
DiagnosticAt(1, 3),
DiagnosticMessage(HasSubstr("Whitespace is required")))));
EXPECT_CALL(consumer, HandleDiagnostic(IsDiagnostic(
DiagnosticKind::NoWhitespaceAfterCommentIntroducer,
DiagnosticLevel::Error, 1, 3, _)));
Lex("//no space after comment", consumer);
}
TEST_F(LexerTest, DiagnosticUnrecognizedEscape) {
Testing::MockDiagnosticConsumer consumer;
EXPECT_CALL(
consumer,
HandleDiagnostic(AllOf(
DiagnosticAt(1, 8),
DiagnosticMessage(HasSubstr("Unrecognized escape sequence `b`")))));
EXPECT_CALL(consumer, HandleDiagnostic(IsDiagnostic(
DiagnosticKind::UnknownEscapeSequence,
DiagnosticLevel::Error, 1, 8, HasSubstr("`b`"))));
Lex(R"("hello\bworld")", consumer);
}
TEST_F(LexerTest, DiagnosticBadHex) {
Testing::MockDiagnosticConsumer consumer;
EXPECT_CALL(
consumer,
HandleDiagnostic(AllOf(
DiagnosticAt(1, 9),
DiagnosticMessage(HasSubstr("two uppercase hexadecimal digits")))));
EXPECT_CALL(consumer, HandleDiagnostic(IsDiagnostic(
DiagnosticKind::HexadecimalEscapeMissingDigits,
DiagnosticLevel::Error, 1, 9, _)));
Lex(R"("hello\xabworld")", consumer);
}
TEST_F(LexerTest, DiagnosticInvalidDigit) {
Testing::MockDiagnosticConsumer consumer;
EXPECT_CALL(
consumer,
HandleDiagnostic(AllOf(
DiagnosticAt(1, 6),
DiagnosticMessage(HasSubstr("Invalid digit 'a' in hexadecimal")))));
EXPECT_CALL(consumer, HandleDiagnostic(IsDiagnostic(
DiagnosticKind::InvalidDigit,
DiagnosticLevel::Error, 1, 6, HasSubstr("'a'"))));
Lex("0x123abc", consumer);
}
TEST_F(LexerTest, DiagnosticMissingTerminator) {
Testing::MockDiagnosticConsumer consumer;
EXPECT_CALL(consumer,
HandleDiagnostic(
AllOf(DiagnosticAt(1, 1),
DiagnosticMessage(HasSubstr("missing a terminator")))));
HandleDiagnostic(IsDiagnostic(DiagnosticKind::UnterminatedString,
DiagnosticLevel::Error, 1, 1, _)));
Lex(R"(#" ")", consumer);
}
TEST_F(LexerTest, DiagnosticUnrecognizedChar) {
Testing::MockDiagnosticConsumer consumer;
EXPECT_CALL(consumer,
HandleDiagnostic(AllOf(
DiagnosticAt(1, 1),
DiagnosticMessage(HasSubstr("unrecognized character")))));
EXPECT_CALL(consumer, HandleDiagnostic(
IsDiagnostic(DiagnosticKind::UnrecognizedCharacters,
DiagnosticLevel::Error, 1, 1, _)));
Lex("\b", consumer);
}