mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-23 01:32:39 +00:00
Render diagnostics through the terminal library (#7683)
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
This commit is contained in:
1 parent
e1c8c1f855
commit
2c9739799b
1205 files changed
+20291
-24988
No files matched your search
+114
-2
@@ -12,6 +12,7 @@
|
||||
#include "common/command_line.h"
|
||||
#include "common/pretty_stack_trace_function.h"
|
||||
#include "common/version.h"
|
||||
#include "toolchain/diagnostics/stream_consumer.h"
|
||||
#include "toolchain/driver/build_runtimes_subcommand.h"
|
||||
#include "toolchain/driver/build_subcommand.h"
|
||||
#include "toolchain/driver/clang_subcommand.h"
|
||||
@@ -34,9 +35,12 @@ struct Options {
|
||||
bool verbose = false;
|
||||
bool fuzzing = false;
|
||||
bool include_diagnostic_kind = false;
|
||||
bool diagnostic_snippets = true;
|
||||
bool threads = true;
|
||||
bool build_runtimes_on_demand = false;
|
||||
|
||||
Terminal::Preferences terminal;
|
||||
|
||||
llvm::StringRef runtimes_cache_path;
|
||||
llvm::StringRef prebuilt_runtimes_path;
|
||||
|
||||
@@ -134,6 +138,96 @@ the installation tree in the default searched locations.
|
||||
arg_b.Set(&build_runtimes_on_demand);
|
||||
});
|
||||
|
||||
b.AddFlag(
|
||||
{
|
||||
.name = "diagnostic-snippets",
|
||||
.help = R"""(
|
||||
Whether diagnostics show snippets of the source they point at.
|
||||
|
||||
By default a diagnostic shows one view of each file it points into, with the
|
||||
lines between its ranges elided, so a message and a label near it read as one
|
||||
piece of code. `--no-diagnostic-snippets` drops that: each part of a diagnostic
|
||||
is one line, led by its location and the extent of its range, positioned
|
||||
against nothing, which is what a build log and a golden file want.
|
||||
)""",
|
||||
},
|
||||
[&](auto& arg_b) {
|
||||
arg_b.Default(true);
|
||||
arg_b.Set(&diagnostic_snippets);
|
||||
});
|
||||
|
||||
b.AddOneOfOption(
|
||||
{
|
||||
.name = "terminal-background",
|
||||
.value_name = "(auto|dark|light)",
|
||||
.help = R"""(
|
||||
What the terminal draws its text on, which decides the colors a diagnostic is
|
||||
drawn in.
|
||||
|
||||
By default this is read from `COLORFGBG`, which some terminals set, and is
|
||||
otherwise taken to be dark: guessing dark when the terminal is light costs
|
||||
contrast, while guessing light when it is dark puts pale text on a pale
|
||||
background.
|
||||
)""",
|
||||
},
|
||||
[&](auto& arg_b) {
|
||||
arg_b.SetOneOf(
|
||||
{
|
||||
arg_b.OneOfValue("auto", Terminal::BackgroundPreference::Auto)
|
||||
.Default(true),
|
||||
arg_b.OneOfValue("dark", Terminal::BackgroundPreference::Dark),
|
||||
arg_b.OneOfValue("light",
|
||||
Terminal::BackgroundPreference::Light),
|
||||
},
|
||||
&terminal.background);
|
||||
});
|
||||
|
||||
b.AddOneOfOption(
|
||||
{
|
||||
.name = "color",
|
||||
.value_name = "(auto|always|never)",
|
||||
.help = R"""(
|
||||
Whether to color diagnostics.
|
||||
|
||||
By default, color is used when the error stream is a terminal that is known to
|
||||
render it. `NO_COLOR` in the environment disables it, and `CLICOLOR_FORCE` or
|
||||
`FORCE_COLOR` enables it even when the stream isn't a terminal.
|
||||
)""",
|
||||
},
|
||||
[&](auto& arg_b) {
|
||||
arg_b.SetOneOf(
|
||||
{
|
||||
arg_b.OneOfValue("auto", Terminal::Preference::Auto)
|
||||
.Default(true),
|
||||
arg_b.OneOfValue("always", Terminal::Preference::Always),
|
||||
arg_b.OneOfValue("never", Terminal::Preference::Never),
|
||||
},
|
||||
&terminal.color);
|
||||
});
|
||||
|
||||
b.AddOneOfOption(
|
||||
{
|
||||
.name = "terminal-unicode",
|
||||
.value_name = "(auto|always|never)",
|
||||
.help = R"""(
|
||||
Whether to draw output with Unicode box drawing characters.
|
||||
|
||||
By default, they are used when the locale says the terminal decodes UTF-8.
|
||||
Drawing them for a terminal that doesn't misaligns every line that isn't plain
|
||||
ASCII, so this is deliberately separate from `--color`.
|
||||
)""",
|
||||
},
|
||||
[&](auto& arg_b) {
|
||||
arg_b.SetOneOf(
|
||||
{
|
||||
arg_b.OneOfValue("auto", Terminal::Preference::Auto)
|
||||
.Default(true),
|
||||
arg_b.OneOfValue("always", Terminal::Preference::Always),
|
||||
arg_b.OneOfValue("never", Terminal::Preference::Never),
|
||||
},
|
||||
&terminal.utf8);
|
||||
});
|
||||
|
||||
b.AddFlag(
|
||||
{
|
||||
.name = "fuzzing",
|
||||
@@ -146,7 +240,7 @@ the installation tree in the default searched locations.
|
||||
.name = "include-diagnostic-kind",
|
||||
.help = R"""(
|
||||
When printing diagnostics, include the diagnostic kind as part of output. This
|
||||
applies to each message that forms a diagnostic, not just the primary message.
|
||||
applies to each label as well as to the message.
|
||||
)""",
|
||||
},
|
||||
[&](auto& arg_b) { arg_b.Set(&include_diagnostic_kind); });
|
||||
@@ -229,6 +323,22 @@ static auto HandleRuntimesOptions(const Options& options, DriverEnv& driver_env)
|
||||
return true;
|
||||
}
|
||||
|
||||
auto Driver::ErrorCapabilities(Terminal::Preferences preferences) const
|
||||
-> Terminal::Capabilities {
|
||||
if (error_file_.is_valid()) {
|
||||
return Terminal::Capabilities::Detect(error_file_, preferences);
|
||||
}
|
||||
|
||||
// With no file behind the error stream, as in tests and in-process uses,
|
||||
// nothing is detected: detection would make output depend on the environment
|
||||
// a test happens to run in. Only an explicit preference has any effect.
|
||||
return {.color_mode = Terminal::ChooseColorMode(preferences.color, {},
|
||||
/*is_terminal=*/false),
|
||||
.charset = Terminal::ChooseCharset(preferences.utf8, /*locale=*/""),
|
||||
.background = Terminal::ChooseBackground(preferences.background,
|
||||
/*colorfgbg=*/"")};
|
||||
}
|
||||
|
||||
auto Driver::RunCommand(llvm::ArrayRef<llvm::StringRef> args) -> DriverResult {
|
||||
PrettyStackTraceFunction trace_version([&](llvm::raw_ostream& out) {
|
||||
out << "Carbon version: " << Version::String << "\n";
|
||||
@@ -245,8 +355,10 @@ auto Driver::RunCommand(llvm::ArrayRef<llvm::StringRef> args) -> DriverResult {
|
||||
[&](CommandLine::CommandBuilder& b) { options.Build(b); });
|
||||
|
||||
// Regardless of whether the parse succeeded, try to use the diagnostic kind
|
||||
// flag.
|
||||
// flag and render for whatever is behind the error stream.
|
||||
consumer.set_include_diagnostic_kind(options.include_diagnostic_kind);
|
||||
consumer.set_diagnostic_snippets(options.diagnostic_snippets);
|
||||
consumer.set_capabilities(ErrorCapabilities(options.terminal));
|
||||
|
||||
if (env.installation->error()) {
|
||||
CARBON_DIAGNOSTIC(DriverInstallInvalid, Error, "{0}", std::string);
|
||||
|
||||
Reference in new issue
Block a user