mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-16 11:58:12 +00:00
Lexer diagnostics used a single `const char*` as their source location, which only allowed pointing at a single character rather than a run of characters. This limited the underlining of unrecognized tokens, and also made escape sequence diagnostics particularly strange by stopping at whatever point the lexer discovered the error and pointing only at that location. For example: `"\w"` was reported at the `w`, and `"\x"` at the closing `"`. This change tries to always keep the primary location of a diagnostic the point at which typing (inserting or deleting) might fix the error. So for `"\x"`, `\x` is underlined, but the position is at the `"` as that's where you would type a hex code. The lexer's emitter now takes a special lexing source location that represents either a single position _between_ two characters, a single character, or a range of the source buffer. The lexer diagnostics are updated to construct these for the character range as appropriate. Parser diagnostics for a missing token were located at the _next_ token, which is particularly confusing for terminators like missing `;`s. Now they are located at a position after the previous token, with or without a space depending on the token, but typically on the same line. Diagnostics still sort by how far the parser has read. The parser diagnostics for unclosed groups also now attaches a label to the opening token when it is on a separate line. And the diagnostics for mismatched indent in a multi-line string attaches a label to the closing `'''` that establishes that indent. There are likely more location improvements we should do here, but this seemed like a reasonable incremental improvement and is what Claude found when doing an initial audit. One example where I think we should make further improvements is around suggesting raw identifiers when we find an unexpected `Cpp` or `Core` or `base`. We do this consistently now, but raw identifiers should be rare. A TODO is added for these cases. Assisted-by: Claude Code --------- Co-authored-by: Richard Smith <richard@metafoo.co.uk>
176 lines
7.2 KiB
C++
176 lines
7.2 KiB
C++
// 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
|
|
|
|
#ifndef CARBON_TOOLCHAIN_DIAGNOSTICS_MOCKS_H_
|
|
#define CARBON_TOOLCHAIN_DIAGNOSTICS_MOCKS_H_
|
|
|
|
#include <gmock/gmock.h>
|
|
|
|
#include "toolchain/diagnostics/emitter.h"
|
|
|
|
namespace Carbon::Testing {
|
|
|
|
class MockDiagnosticConsumer : public Diagnostics::Consumer {
|
|
public:
|
|
MOCK_METHOD(void, HandleDiagnostic, (Diagnostics::Diagnostic diagnostic),
|
|
(override));
|
|
};
|
|
|
|
// NOLINTNEXTLINE(modernize-use-trailing-return-type): From the macro.
|
|
MATCHER_P(IsDiagnosticsMessageString, matcher, "") {
|
|
const Diagnostics::Message& message = arg;
|
|
return testing::ExplainMatchResult(matcher, message.Format(),
|
|
result_listener);
|
|
}
|
|
|
|
// NOLINTNEXTLINE(modernize-use-trailing-return-type): From the macro.
|
|
MATCHER_P(IsDiagnosticsLabelString, matcher, "") {
|
|
const Diagnostics::Label& label = arg;
|
|
return testing::ExplainMatchResult(matcher, label.Format(), result_listener);
|
|
}
|
|
|
|
// NOLINTNEXTLINE(modernize-use-trailing-return-type): From the macro.
|
|
MATCHER_P(IsDiagnosticsContextString, matcher, "") {
|
|
const Diagnostics::Context& context = arg;
|
|
return testing::ExplainMatchResult(matcher, context.Format(),
|
|
result_listener);
|
|
}
|
|
|
|
// Matches the location fields both a message and a label carry.
|
|
template <typename T>
|
|
auto IsAtLoc(testing::Matcher<int> line_number,
|
|
testing::Matcher<int> column_number) -> testing::Matcher<T> {
|
|
using testing::AllOf;
|
|
using testing::Field;
|
|
return Field(
|
|
&T::loc,
|
|
AllOf(Field("line_number", &Diagnostics::Loc::line_number, line_number),
|
|
Field("column_number", &Diagnostics::Loc::column_number,
|
|
column_number)));
|
|
}
|
|
|
|
inline auto IsDiagnosticMessage(testing::Matcher<Diagnostics::Kind> kind,
|
|
testing::Matcher<Diagnostics::Level> level,
|
|
testing::Matcher<int> line_number,
|
|
testing::Matcher<int> column_number,
|
|
testing::Matcher<std::string> message)
|
|
-> testing::Matcher<Diagnostics::Message> {
|
|
using testing::AllOf;
|
|
using testing::Field;
|
|
return AllOf(Field("kind", &Diagnostics::Message::kind, kind),
|
|
Field("level", &Diagnostics::Message::level, level),
|
|
IsAtLoc<Diagnostics::Message>(line_number, column_number),
|
|
IsDiagnosticsMessageString(message));
|
|
}
|
|
|
|
inline auto IsDiagnosticLabel(
|
|
testing::Matcher<Diagnostics::LabelCategory> category,
|
|
testing::Matcher<int> line_number, testing::Matcher<int> column_number,
|
|
testing::Matcher<std::string> text)
|
|
-> testing::Matcher<Diagnostics::Label> {
|
|
using testing::AllOf;
|
|
using testing::Field;
|
|
return AllOf(Field("category", &Diagnostics::Label::category, category),
|
|
IsAtLoc<Diagnostics::Label>(line_number, column_number),
|
|
IsDiagnosticsLabelString(text));
|
|
}
|
|
|
|
inline auto IsDiagnosticContext(testing::Matcher<int> line_number,
|
|
testing::Matcher<int> column_number,
|
|
testing::Matcher<std::string> text)
|
|
-> testing::Matcher<Diagnostics::Context> {
|
|
return testing::AllOf(
|
|
IsAtLoc<Diagnostics::Context>(line_number, column_number),
|
|
IsDiagnosticsContextString(text));
|
|
}
|
|
|
|
inline auto IsDiagnostic(
|
|
testing::Matcher<Diagnostics::Level> level,
|
|
testing::Matcher<Diagnostics::Message> message,
|
|
testing::Matcher<llvm::SmallVector<Diagnostics::Context, 0>> contexts,
|
|
testing::Matcher<llvm::SmallVector<Diagnostics::Label, 0>> labels)
|
|
-> testing::Matcher<Diagnostics::Diagnostic> {
|
|
return testing::AllOf(
|
|
testing::Field("level", &Diagnostics::Diagnostic::level, level),
|
|
testing::Field("message", &Diagnostics::Diagnostic::message, message),
|
|
testing::Field("contexts", &Diagnostics::Diagnostic::contexts, contexts),
|
|
testing::Field("labels", &Diagnostics::Diagnostic::labels, labels));
|
|
}
|
|
|
|
// The start and length expected of a location, in `IsDiagnosticWithRange`.
|
|
struct ExpectedLoc {
|
|
testing::Matcher<int> line_number;
|
|
testing::Matcher<int> column_number;
|
|
testing::Matcher<int> length;
|
|
};
|
|
|
|
// Matches a diagnostic with one message and one range attached to it with no
|
|
// text, checking the lengths of the message's location and of the range as
|
|
// well as their starts.
|
|
inline auto IsDiagnosticWithRange(testing::Matcher<Diagnostics::Kind> kind,
|
|
testing::Matcher<Diagnostics::Level> level,
|
|
ExpectedLoc loc,
|
|
testing::Matcher<std::string> message,
|
|
ExpectedLoc range)
|
|
-> testing::Matcher<Diagnostics::Diagnostic> {
|
|
return IsDiagnostic(
|
|
level,
|
|
testing::AllOf(
|
|
IsDiagnosticMessage(kind, level, loc.line_number, loc.column_number,
|
|
message),
|
|
testing::Field(
|
|
"loc", &Diagnostics::Message::loc,
|
|
testing::Field("length", &Diagnostics::Loc::length, loc.length))),
|
|
testing::IsEmpty(),
|
|
testing::ElementsAre(testing::AllOf(
|
|
IsDiagnosticLabel(Diagnostics::LabelCategory::Primary,
|
|
range.line_number, range.column_number, ""),
|
|
testing::Field("loc", &Diagnostics::Label::loc,
|
|
testing::Field("length", &Diagnostics::Loc::length,
|
|
range.length)))));
|
|
}
|
|
|
|
inline auto IsSingleDiagnostic(testing::Matcher<Diagnostics::Kind> kind,
|
|
testing::Matcher<Diagnostics::Level> level,
|
|
testing::Matcher<int> line_number,
|
|
testing::Matcher<int> column_number,
|
|
testing::Matcher<std::string> message)
|
|
-> testing::Matcher<Diagnostics::Diagnostic> {
|
|
return IsDiagnostic(
|
|
level,
|
|
IsDiagnosticMessage(kind, level, line_number, column_number, message),
|
|
testing::IsEmpty(), testing::IsEmpty());
|
|
}
|
|
|
|
// Matches a diagnostic with one message, located at the point just before
|
|
// `column_number`, and nothing attached to it.
|
|
inline auto IsPointDiagnostic(testing::Matcher<Diagnostics::Kind> kind,
|
|
testing::Matcher<Diagnostics::Level> level,
|
|
testing::Matcher<int> line_number,
|
|
testing::Matcher<int> column_number,
|
|
testing::Matcher<std::string> message)
|
|
-> testing::Matcher<Diagnostics::Diagnostic> {
|
|
return IsDiagnostic(
|
|
level,
|
|
testing::AllOf(
|
|
IsDiagnosticMessage(kind, level, line_number, column_number, message),
|
|
testing::Field(
|
|
"loc", &Diagnostics::Message::loc,
|
|
testing::Field("length", &Diagnostics::Loc::length, 0))),
|
|
testing::IsEmpty(), testing::IsEmpty());
|
|
}
|
|
|
|
} // namespace Carbon::Testing
|
|
|
|
namespace Carbon::Diagnostics {
|
|
|
|
// Printing helpers for tests.
|
|
auto PrintTo(const Diagnostic& diagnostic, std::ostream* os) -> void;
|
|
auto PrintTo(Level level, std::ostream* os) -> void;
|
|
auto PrintTo(LabelCategory category, std::ostream* os) -> void;
|
|
|
|
} // namespace Carbon::Diagnostics
|
|
|
|
#endif // CARBON_TOOLCHAIN_DIAGNOSTICS_MOCKS_H_
|