mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-10 12:28:25 +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
109 lines
4.3 KiB
C++
109 lines
4.3 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
|
|
|
|
#include "toolchain/check/context.h"
|
|
#include "toolchain/check/decl_name_stack.h"
|
|
#include "toolchain/check/generic.h"
|
|
#include "toolchain/check/handle.h"
|
|
#include "toolchain/check/inst.h"
|
|
#include "toolchain/check/modifiers.h"
|
|
#include "toolchain/check/name_component.h"
|
|
#include "toolchain/check/name_lookup.h"
|
|
#include "toolchain/parse/typed_nodes.h"
|
|
#include "toolchain/sem_ir/ids.h"
|
|
#include "toolchain/sem_ir/typed_insts.h"
|
|
|
|
namespace Carbon::Check {
|
|
|
|
auto HandleParseNode(Context& context, Parse::ExportIntroducerId /*node_id*/)
|
|
-> bool {
|
|
// Export declarations can't be generic, but we might have parsed a generic
|
|
// parameter in their name, so enter a generic scope just in case.
|
|
StartGenericDecl(context);
|
|
context.decl_introducer_state_stack().Push<Lex::TokenKind::Export>();
|
|
// TODO: Probably need to update DeclNameStack to restrict to only namespaces.
|
|
context.decl_name_stack().PushScopeAndStartName();
|
|
return true;
|
|
}
|
|
|
|
auto HandleParseNode(Context& context, Parse::ExportDeclId node_id) -> bool {
|
|
auto name_context = context.decl_name_stack().FinishName(
|
|
PopNameComponentWithoutParams(context, Lex::TokenKind::Export));
|
|
DiscardGenericDecl(context);
|
|
context.decl_name_stack().PopScope();
|
|
|
|
auto introducer =
|
|
context.decl_introducer_state_stack().Pop<Lex::TokenKind::Export>();
|
|
LimitModifiersOnDecl(context, introducer, KeywordModifierSet::None);
|
|
|
|
if (name_context.state == DeclNameStack::NameContext::State::Error) {
|
|
// Should already be diagnosed.
|
|
return true;
|
|
}
|
|
|
|
// Exporting uses the decl name primarily for lookup, so treat poisoning the
|
|
// same as "not found".
|
|
auto inst_id =
|
|
name_context.state == DeclNameStack::NameContext::State::Poisoned
|
|
? SemIR::InstId::None
|
|
: name_context.prev_inst_id();
|
|
if (!inst_id.has_value()) {
|
|
DiagnoseNameNotFound(context, node_id, name_context.name_id_for_new_inst());
|
|
return true;
|
|
}
|
|
|
|
auto inst = context.insts().Get(inst_id);
|
|
|
|
if (inst.Is<SemIR::ExportDecl>()) {
|
|
CARBON_DIAGNOSTIC(ExportRedundant, Warning,
|
|
"`export` matches previous `export`");
|
|
CARBON_DIAGNOSTIC_LABEL(ExportPrevious, Info, "previous `export` here");
|
|
context.emitter()
|
|
.Build(node_id, ExportRedundant)
|
|
// Use the location of the export itself, not the exported instruction.
|
|
//
|
|
// TODO: This construction of a LocId that does not just contain the
|
|
// InstId prevents GetAbsoluteNodeIdImpl() from seeing the `ExportDecl`
|
|
// instruction, which prevents it from chasing through it to the entity
|
|
// being exported. It might be nice to make this more explicit.
|
|
.Attach(context.insts().GetCanonicalLocId(inst_id), ExportPrevious)
|
|
.Emit();
|
|
return true;
|
|
}
|
|
|
|
auto import_ref = context.insts().TryGetAs<SemIR::ImportRefLoaded>(inst_id);
|
|
if (!import_ref) {
|
|
CARBON_DIAGNOSTIC(ExportNotImportedEntity, Error,
|
|
"only imported entities are valid for `export`");
|
|
CARBON_DIAGNOSTIC_LABEL(ExportNotImportedEntitySource, Info,
|
|
"name is declared here");
|
|
context.emitter()
|
|
.Build(node_id, ExportNotImportedEntity)
|
|
.Attach(inst_id, ExportNotImportedEntitySource)
|
|
.Emit();
|
|
return true;
|
|
}
|
|
|
|
auto export_id =
|
|
AddInst<SemIR::ExportDecl>(context, node_id,
|
|
{.type_id = import_ref->type_id,
|
|
.entity_name_id = import_ref->entity_name_id,
|
|
.value_id = inst_id});
|
|
context.exports().push_back(export_id);
|
|
|
|
// Replace the ImportRef in name lookup, both for the above duplicate
|
|
// diagnostic and so that cross-package imports can find it easily.
|
|
auto entity_name = context.entity_names().Get(import_ref->entity_name_id);
|
|
auto& parent_scope = context.name_scopes().Get(entity_name.parent_scope_id);
|
|
auto& scope_result =
|
|
parent_scope.GetEntry(*parent_scope.Lookup(entity_name.name_id)).result;
|
|
CARBON_CHECK(scope_result.target_inst_id() == inst_id);
|
|
scope_result = SemIR::ScopeLookupResult::MakeFound(
|
|
export_id, scope_result.access_kind());
|
|
|
|
return true;
|
|
}
|
|
|
|
} // namespace Carbon::Check
|