mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-25 18:51:11 +00:00
Historically, we disabled the rendered form in tests outside of `toolchain/diagnostics`. The idea was to keep tests shorter and avoid very long chains of autoupdate before stabilizing due to all the line numbers and other artifacts in the rendered form. This PR adds `--diagnostic-line-numbers=[all|first]` and defaults to `first` in tests. This keeps the rendered form so that we can easily code review that form and the visual cues it does or doesn't provide. But it removes all but the first location's line numbers to provide significantly more stability in tests. By default, tests get the full unicode line-drawing form of diagnostics so we can ensure all of the artifacts there make sense with the specific case being diagnosed, but we do retain some focused ASCII-rendering tests to ensure that fallback remains usable. There are some issues here that will be fixed in subsequent PRs. Notably, we try to keep the frame together in the check lines, and so force some parts to use the first location. This largely works, but fails in some edge cases, causing the entire frame to be located incorrectly. There is a TODO about this that we need to fix, and it only occurs 6 times across the test suite, so it didn't seem worth front-loading a fix for here. Assisted-by: Claude Code
139 lines
5.8 KiB
C++
139 lines
5.8 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_RENDERER_H_
|
|
#define CARBON_TOOLCHAIN_DIAGNOSTICS_RENDERER_H_
|
|
|
|
#include <cstdint>
|
|
#include <string>
|
|
|
|
#include "common/ostream.h"
|
|
#include "common/terminal/capabilities.h"
|
|
#include "common/terminal/output_buffer_ref.h"
|
|
#include "llvm/ADT/ArrayRef.h"
|
|
#include "toolchain/diagnostics/diagnostic.h"
|
|
|
|
namespace Carbon::Diagnostics {
|
|
|
|
namespace Internal {
|
|
struct PreparedPart;
|
|
} // namespace Internal
|
|
|
|
// Which line numbers the framed form shows.
|
|
enum class LineNumbers : int8_t {
|
|
// Every line of source is numbered, and every location has its line and
|
|
// column.
|
|
All,
|
|
// Only the first location row that has a location keeps its line and
|
|
// column. The gutter has no numbers, and every other location shows only its
|
|
// file name.
|
|
First,
|
|
};
|
|
|
|
// Draws diagnostics for a terminal.
|
|
//
|
|
// A diagnostic renders as one frame holding a snippet of each file it points
|
|
// into. The message's text is placed under the range that is wrong, which has a
|
|
// heavy underline, and begins with the level word. Every label is placed under
|
|
// the range it is attached to.
|
|
//
|
|
// ╭─┤ foo.carbon:19:3
|
|
// 1 │ fn Run0() {}
|
|
// · ──────┬─────
|
|
// · ╰──┤ calling function declared here
|
|
// ┆
|
|
// -> 19 │ Run0(1);
|
|
// · ━━━┳━━━
|
|
// · ┗━━┫ error: invalid arguments in function call
|
|
// ╰────
|
|
//
|
|
// With snippets off, each part of the diagnostic is one line, led by its
|
|
// location and the extent of its range:
|
|
//
|
|
// foo.carbon:19:3-9: error: invalid arguments in function call
|
|
// foo.carbon:1:1-12: note: calling function declared here
|
|
//
|
|
// See /toolchain/docs/diagnostics_rendering.md for the full form, the palette,
|
|
// and why it looks like this rather than like something else.
|
|
//
|
|
// The rendering is decided entirely by the capabilities passed in, so the same
|
|
// diagnostic renders the same way every time for a given terminal, and the
|
|
// default capabilities render plain ASCII. What a stream's capabilities
|
|
// actually are is `Terminal::Capabilities::Detect`'s to answer: notably it
|
|
// takes the character set from the locale whether or not a terminal is
|
|
// attached, since the locale is what says how bytes will be decoded.
|
|
class Renderer {
|
|
public:
|
|
// Renders for a terminal with `capabilities`. The default describes a stream
|
|
// with no terminal behind it: no color, no line drawing, and no width.
|
|
// Layout still fits one -- the width code is formatted to, plus the gutter
|
|
// -- so the rendering is decided by the diagnostic rather than by the
|
|
// environment the stream ends up in.
|
|
explicit Renderer(const Terminal::Capabilities& capabilities = {})
|
|
: capabilities_(capabilities) {}
|
|
|
|
// Renders `diagnostic`, appending the bytes that draw it to `out`. Each row
|
|
// ends in a newline, and no style is left set at the end.
|
|
auto Render(Terminal::OutputBufferRef out, const Diagnostic& diagnostic) const
|
|
-> void;
|
|
|
|
// Sets whether each message names the diagnostic kind that produced it. This
|
|
// is for tests, which match against the kind to prove they cover it.
|
|
auto set_include_kind(bool value) -> void { include_kind_ = value; }
|
|
|
|
// Sets whether diagnostics show snippets of the source they point at. With
|
|
// them off, every diagnostic takes the compact form: one line per part, led
|
|
// by its location and the extent of its range, positioned against nothing,
|
|
// which suits a build log.
|
|
auto set_snippets(bool value) -> void { snippets_ = value; }
|
|
|
|
// Sets which line numbers the framed form shows. The layout is the same
|
|
// either way.
|
|
//
|
|
// `LineNumbers::First` is for golden files. Adding a `CHECK` line changes the
|
|
// number of every source line below it, so a golden can only hold a line
|
|
// number as a `[[@LINE...]]` offset from its `CHECK` line. Autoupdate writes
|
|
// that offset for the first location row only: it doesn't rewrite gutter
|
|
// numbers, and a frame's `CHECK` lines are all in that row's file, so a
|
|
// location in another file has no offset from them.
|
|
auto set_line_numbers(LineNumbers value) -> void { line_numbers_ = value; }
|
|
|
|
auto set_capabilities(const Terminal::Capabilities& capabilities) -> void {
|
|
capabilities_ = capabilities;
|
|
}
|
|
|
|
private:
|
|
// Renders one line per part of the diagnostic, led by its location.
|
|
auto RenderCompact(Terminal::OutputBufferRef out,
|
|
llvm::ArrayRef<Internal::PreparedPart> parts) const
|
|
-> void;
|
|
|
|
// Renders the framed form into the geometry `Render` chose.
|
|
auto RenderFramed(Terminal::OutputBufferRef out,
|
|
llvm::MutableArrayRef<Internal::PreparedPart> parts,
|
|
int leading, Level level, int frame_x, int content_x,
|
|
int source_columns, int columns) const -> void;
|
|
|
|
Terminal::Capabilities capabilities_;
|
|
bool include_kind_ = false;
|
|
bool snippets_ = true;
|
|
LineNumbers line_numbers_ = LineNumbers::All;
|
|
};
|
|
|
|
// Returns `loc` as `<filename>:<line>:<column>`, with the parts that are
|
|
// unknown dropped from the right, and empty when even the filename is unknown.
|
|
auto FormatLocation(const Loc& loc) -> std::string;
|
|
|
|
// Writes the source snippet for `loc` to `out`, indented by `indent` columns
|
|
// and with no styling.
|
|
//
|
|
// This and `FormatLocation` are for crash handlers, which interleave a location
|
|
// and its source with text of their own rather than emitting a diagnostic. They
|
|
// share their layout with `Renderer` so that there is one implementation of it.
|
|
auto PrintSnippet(llvm::raw_ostream& out, const Loc& loc, int indent) -> void;
|
|
|
|
} // namespace Carbon::Diagnostics
|
|
|
|
#endif // CARBON_TOOLCHAIN_DIAGNOSTICS_RENDERER_H_
|