mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-24 15:02:44 +00:00
This is the final step in landing the _structural_ updates to diagnostic rendering. The key idea here is that we want the primary diagnostic to have a message clearly explaining the problem and attached to the code that has that problem. This should be the starting point for readers working to address the issue, but is rendered attached to the code snippet in question so errors are always "in context" of the code with the error. Everything else in a diagnostic is an additional label attached to code to explain or help the reader understand the problem in the primary message. Previously, `ContextScope` resulted in some confusion here, as diagnostic consumers turned messages into labels and made the message in the context scope the message. In essence, this isn't a scope with context _for_ a diagnostic (with two exceptions that should probably have been an `AnnotationScope`), but a context within `check` where we have a preferred message for a diagnostic emitted by general purpose code we call into. For example, "class was forward declared here" starts as an error, and then consumers render it as a label to make the message from the context scope the error. We also grew an `AnnotationScope` to handle the contextual information we want to show for a diagnostic (what I would have assumed `ContextScope` actually did). Now we have a `Diagnoser`: code in `check` that can better explain the problem than shared logic it is calling into passes a callback that builds a caller-specific diagnostic. The callee then can attach relevant labels and emit it. Diagnostics no longer store `contexts` and consumers no longer need the logic to restructure things. We move all (but two) of the old contexts to be normal messages emitted in this way, and we move some diagnostics to become labels. Each of the new normal messages corresponds to a new diagnostic kind as usual. Common code following this pattern now _requires_ the `Diagnoser` to be passed (or emit nothing), and this uncovered three places where an empty context was used that made the rendered form miss key information. All three are fixed with new diagnostics, one already covered by tests, and two with TODOs to add test coverage. We also completely remove the old `ContextScope` and `CARBON_DIAGNOSTIC_CONTEXT` and rename `AnnotationScope` to `ContextScope`. It is only used to attach information about when/where a diagnostic occurred. Labels also no longer have categories. What were `Primary` are now `ExtraRange`s on the diagnostic rather than labels. Labels are now always informational. `DeclaredName` now narrows the range of a label when the range is a declaration of a named entity to only be the range of that name. For entities that don't have a declared name, the entire declaration remains the range. The code building and emitting these diagnostics can skip them if the declared location isn't distinct from the diagnostic location. Wording is largely unchanged here so we can just review the _structural_ change. #7991 iterates using this to improve locations and adds missing labels. A subsequent PR will iterate on the wording to increasingly fit within this structural model. This is separated out as a way to review incrementally, and so that subsequently we don't have to review wording improvements that only make sense in a new structure that you cannot see yet. It seems easier to look at the structure here even with awkward wording. Assisted-by: Claude Code
165 lines
6.2 KiB
C++
165 lines
6.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
|
|
|
|
// Fuzzes the diagnostic renderer directly, on `Diagnostic` values a compiler
|
|
// would never build: degenerate locations, spans past the end of a line,
|
|
// overlapping ranges, control bytes and invalid UTF-8 in every text field, and
|
|
// every combination of capabilities. The renderer's contract is that none of
|
|
// this is ever the reason a compiler dies while reporting a problem, so what is
|
|
// checked is that every form of every shape renders without a crash. Whether
|
|
// what it draws fits the width is not checked here: an unbreakable token or a
|
|
// path legitimately overhangs, and only the line art must fit, which the
|
|
// terminal buffer CHECKs on its own.
|
|
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <string>
|
|
#include <utility>
|
|
|
|
#include "common/check.h"
|
|
#include "common/terminal/capabilities.h"
|
|
#include "common/terminal/output_buffer_ref.h"
|
|
#include "llvm/ADT/SmallString.h"
|
|
#include "llvm/ADT/SmallVector.h"
|
|
#include "llvm/ADT/StringRef.h"
|
|
#include "testing/fuzzing/libfuzzer.h"
|
|
#include "toolchain/diagnostics/diagnostic.h"
|
|
#include "toolchain/diagnostics/renderer.h"
|
|
|
|
namespace Carbon::Diagnostics {
|
|
namespace {
|
|
|
|
// A cursor over the fuzzer input that hands out bounded values, returning zero
|
|
// once the bytes run out so that decoding always terminates.
|
|
class Bytes {
|
|
public:
|
|
Bytes(const unsigned char* data, size_t size) : data_(data), size_(size) {}
|
|
|
|
// Returns the next byte, or 0 when the input is exhausted.
|
|
auto Next() -> uint8_t { return at_ < size_ ? data_[at_++] : 0; }
|
|
|
|
// Returns the next value in `[0, bound)`, or 0 when `bound` is 0.
|
|
auto Below(int bound) -> int { return bound > 0 ? Next() % bound : 0; }
|
|
|
|
// Returns a signed value spanning a range that includes the degenerate
|
|
// location values (-1) the renderer must tolerate.
|
|
auto Small() -> int32_t { return static_cast<int32_t>(Next()) - 2; }
|
|
|
|
// Returns a run of the raw input bytes, so text fields carry whatever the
|
|
// fuzzer chose -- control bytes, invalid UTF-8, and embedded escapes
|
|
// included. The reference is into the input and lives as long as it does.
|
|
auto Text() -> llvm::StringRef {
|
|
size_t len = Next();
|
|
if (at_ + len > size_) {
|
|
len = size_ - at_;
|
|
}
|
|
llvm::StringRef result(reinterpret_cast<const char*>(data_ + at_), len);
|
|
at_ += len;
|
|
return result;
|
|
}
|
|
|
|
private:
|
|
const unsigned char* data_;
|
|
size_t size_;
|
|
size_t at_ = 0;
|
|
};
|
|
|
|
// Builds a `Loc` from the fuzzer, sharing one buffer for `line`/`file_text` so
|
|
// the renderer's pointer-containment paths are exercised rather than always
|
|
// failing their guards.
|
|
static auto MakeLoc(Bytes& bytes, llvm::StringRef file_text) -> Loc {
|
|
llvm::StringRef line = file_text;
|
|
// Sometimes point `line` into `file_text` (a real slice), sometimes not.
|
|
if (!file_text.empty() && (bytes.Next() & 1)) {
|
|
size_t start = bytes.Next() % file_text.size();
|
|
line = file_text.substr(start, bytes.Next());
|
|
}
|
|
return {.filename = bytes.Text(),
|
|
.line = line,
|
|
.file_text = file_text,
|
|
.line_number = bytes.Small(),
|
|
.column_number = bytes.Small(),
|
|
.length = bytes.Small()};
|
|
}
|
|
|
|
// A format function that ignores the format and returns fuzzer-chosen text, so
|
|
// a part's rendered words are whatever the input holds.
|
|
static auto SaysText(std::string text) -> FormatFn {
|
|
return [text = std::move(text)](llvm::StringLiteral /*format*/,
|
|
llvm::ArrayRef<llvm::Any> /*args*/) {
|
|
return text;
|
|
};
|
|
}
|
|
|
|
} // namespace
|
|
|
|
// NOLINTNEXTLINE: Match the documented fuzzer entry point declaration style.
|
|
extern "C" int LLVMFuzzerTestOneInput(const unsigned char* data, size_t size) {
|
|
Bytes bytes(data, size);
|
|
|
|
Terminal::Capabilities capabilities;
|
|
capabilities.color_mode = static_cast<Terminal::ColorMode>(bytes.Below(4));
|
|
capabilities.charset = static_cast<Terminal::Charset>(bytes.Below(2));
|
|
capabilities.background = static_cast<Terminal::Background>(bytes.Below(2));
|
|
capabilities.is_terminal = bytes.Next() & 1;
|
|
// Sometimes a width, always positive as the field promises. The range spans
|
|
// the compact-form threshold and small values a grid could divide badly.
|
|
if (bytes.Next() & 1) {
|
|
capabilities.columns = 1 + bytes.Below(200);
|
|
}
|
|
|
|
// The source both the message and the labels point into. One shared buffer
|
|
// makes elision between two spans reachable.
|
|
llvm::StringRef file_text = bytes.Text();
|
|
|
|
bool include_kind = bytes.Next() & 1;
|
|
auto level = static_cast<Level>(bytes.Below(2));
|
|
|
|
Message message = {.kind = Kind::TestDiagnostic,
|
|
.level = level,
|
|
.loc = MakeLoc(bytes, file_text),
|
|
.format = "",
|
|
.format_fn = SaysText(bytes.Text().str())};
|
|
|
|
int range_count = bytes.Below(3);
|
|
for (int i = 0; i < range_count; ++i) {
|
|
message.ranges.push_back(MakeLoc(bytes, file_text));
|
|
}
|
|
|
|
llvm::SmallVector<Label, 0> labels;
|
|
int label_count = bytes.Below(8);
|
|
for (int i = 0; i < label_count; ++i) {
|
|
llvm::StringRef words = bytes.Text();
|
|
labels.push_back(
|
|
{.loc = MakeLoc(bytes, file_text),
|
|
.name = "TestLabel",
|
|
.format = "",
|
|
.format_fn = words.empty() ? FormatFn() : SaysText(words.str())});
|
|
}
|
|
|
|
Diagnostic diagnostic = {.level = level,
|
|
.message = std::move(message),
|
|
.labels = std::move(labels)};
|
|
|
|
Renderer renderer(capabilities);
|
|
renderer.set_include_kind(include_kind);
|
|
|
|
// The contract is that no shape of diagnostic, however malformed, is ever the
|
|
// reason a compiler dies while reporting a problem. Every form has to hold to
|
|
// it, so all of them are drawn; a crash in any is a crash in the error path.
|
|
for (bool snippets : {true, false}) {
|
|
for (LineNumbers line_numbers : {LineNumbers::All, LineNumbers::First}) {
|
|
renderer.set_snippets(snippets);
|
|
renderer.set_line_numbers(line_numbers);
|
|
llvm::SmallString<256> rendered;
|
|
Terminal::OutputBufferRef out(rendered);
|
|
renderer.Render(out, diagnostic);
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
} // namespace Carbon::Diagnostics
|