mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-17 16:00:33 +00:00
Diagnostic rendering was part of the data model: `Loc::FormatLocation` and `Loc::FormatSnippet` wrote plain text to a stream, so no capability, color, or terminal width could reach them. A diagnostic is now one message with labels, contexts, and location information attached, and `Diagnostics::Renderer` draws it into a `Terminal::Buffer`. The design is in `toolchain/docs/diagnostics_rendering.md` (#7668), authoring in `toolchain/docs/diagnostics.md`. Before: ``` example.carbon:6:3: error: 1 argument passed to function expecting 0 arguments Run0(1); ^~~~~~~ example.carbon:3:1: note: calling function declared here fn Run0() {} ^~~~~~~~~~~ ``` After: ``` error: 1 argument passed to function expecting 0 arguments ╭─┤ example.carbon:6:3 ┆ 3 │ fn Run0() {} · ─────┬───── · ╰──┤ calling function declared here ┆ -> 6 │ Run0(1); · ─────── ╰──────────── ``` `--no-diagnostic-snippets` gives a compact form, `example.carbon:6:3-9`. File tests use it; the rendering tests under `toolchain/diagnostics/testdata` ask for snippets. C++ interop diagnostics are data now rather than text Clang drew: `SemIR::ClangSourceLocId` names a `clang::SourceRange`, and a Clang location is marked as the point it names. `StreamConsumer` moves to a `:renderer` target that `:emitter` does not depend on, keeping `common/terminal` out of `consumer.h`. Capabilities come from the file behind the error stream, so tests get plain ASCII. `renderer_fuzzer` draws diagnostics built from fuzzer bytes with every capability combination, since a shape the renderer doesn't expect must never be why the compiler dies while reporting a problem. TODOs mark the follow-ups: line weights and crossing lines, wrapping that keeps a quoted type whole, and ordering diagnostics that share a byte offset. Also: `IncorrectExtension`'s hint moves into its message, since its note had no location of its own, and `strip_noise` in `check_diagnostics.py` blanks multi-line `#define`s instead of deleting them. Assisted-by: Claude Code
125 lines
4.9 KiB
C++
125 lines
4.9 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));
|
|
}
|
|
|
|
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());
|
|
}
|
|
|
|
} // 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_
|