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
156 lines
6.0 KiB
C++
156 lines
6.0 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/diagnostic_emitter.h"
|
|
|
|
#include <algorithm>
|
|
#include <optional>
|
|
#include <string>
|
|
|
|
#include "clang/AST/TypeBase.h"
|
|
#include "common/raw_string_ostream.h"
|
|
#include "toolchain/check/diagnostic_helpers.h"
|
|
#include "toolchain/sem_ir/absolute_node_ref.h"
|
|
#include "toolchain/sem_ir/diagnostic_loc_converter.h"
|
|
#include "toolchain/sem_ir/ids.h"
|
|
#include "toolchain/sem_ir/stringify.h"
|
|
|
|
namespace Carbon::Check {
|
|
|
|
auto DiagnosticEmitter::ConvertLoc(LocIdForDiagnostics loc_id,
|
|
ContextFnT context_fn) const
|
|
-> Diagnostics::ConvertedLoc {
|
|
auto [imports, converted] = loc_converter_.ConvertWithImports(
|
|
loc_id.loc_id(), loc_id.is_token_only());
|
|
for (const auto& import : imports) {
|
|
// These are rendered as `<text>: <location>`, hanging off the location
|
|
// they were reached from, so each reads as a step in a path.
|
|
CARBON_DIAGNOSTIC_LOCATION_INFO(InImport, "imported from");
|
|
CARBON_DIAGNOSTIC_LOCATION_INFO(InCppInclude, "included from");
|
|
CARBON_DIAGNOSTIC_LOCATION_INFO(InCppModule, "imported from module at");
|
|
CARBON_DIAGNOSTIC_LOCATION_INFO(InCppMacroExpansion,
|
|
"expanded from macro defined at");
|
|
switch (import.kind) {
|
|
case Carbon::SemIR::DiagnosticLocConverter::ImportLoc::Import:
|
|
// TODO: Include the library name in the label.
|
|
context_fn(import.loc, InImport);
|
|
break;
|
|
case Carbon::SemIR::DiagnosticLocConverter::ImportLoc::CppInclude:
|
|
// TODO: Include the file name in the label.
|
|
context_fn(import.loc, InCppInclude);
|
|
break;
|
|
case Carbon::SemIR::DiagnosticLocConverter::ImportLoc::CppModuleImport:
|
|
// TODO: Include the module name in the label.
|
|
context_fn(import.loc, InCppModule);
|
|
break;
|
|
case Carbon::SemIR::DiagnosticLocConverter::ImportLoc::CppMacroExpansion:
|
|
// TODO: Include the macro name in the label.
|
|
// TODO: Include the Clang-generated snippet here rather than with the
|
|
// main diagnostic.
|
|
context_fn(import.loc, InCppMacroExpansion);
|
|
break;
|
|
}
|
|
}
|
|
|
|
// Use the token when possible, but -1 is the default value.
|
|
auto last_offset = -1;
|
|
if (last_token_.has_value()) {
|
|
last_offset = sem_ir_->parse_tree().tokens().GetByteOffset(last_token_);
|
|
}
|
|
|
|
// When the diagnostic is in the same file, we use the last possible offset;
|
|
// otherwise, we ignore the offset because it's probably in that file.
|
|
if (converted.loc.filename == sem_ir_->filename()) {
|
|
converted.last_byte_offset =
|
|
std::max(converted.last_byte_offset, last_offset);
|
|
} else {
|
|
converted.last_byte_offset = last_offset;
|
|
}
|
|
|
|
return converted;
|
|
}
|
|
|
|
auto DiagnosticEmitter::ConvertArg(llvm::Any arg) const -> llvm::Any {
|
|
if (auto* library_name_id = llvm::any_cast<SemIR::LibraryNameId>(&arg)) {
|
|
std::string library_name;
|
|
if (*library_name_id == SemIR::LibraryNameId::Default) {
|
|
library_name = "default library";
|
|
} else if (!library_name_id->has_value()) {
|
|
library_name = "library <none>";
|
|
} else {
|
|
RawStringOstream stream;
|
|
stream << "library \""
|
|
<< sem_ir_->string_literal_values().Get(
|
|
library_name_id->AsStringLiteralValueId())
|
|
<< "\"";
|
|
library_name = stream.TakeStr();
|
|
}
|
|
return library_name;
|
|
}
|
|
if (auto* name_id = llvm::any_cast<SemIR::NameId>(&arg)) {
|
|
return sem_ir_->names().GetFormatted(*name_id).str();
|
|
}
|
|
if (auto* type_of_expr = llvm::any_cast<TypeOfInstId>(&arg)) {
|
|
if (!type_of_expr->inst_id.has_value()) {
|
|
return "<none>";
|
|
}
|
|
return "`" + StringifyTypeOfInst(*sem_ir_, type_of_expr->inst_id) + "`";
|
|
}
|
|
if (auto* expr = llvm::any_cast<InstIdAsConstant>(&arg)) {
|
|
return "`" + StringifyConstantInst(*sem_ir_, expr->inst_id) + "`";
|
|
}
|
|
if (auto* type_expr = llvm::any_cast<InstIdAsRawType>(&arg)) {
|
|
return StringifyConstantInst(*sem_ir_, type_expr->inst_id);
|
|
}
|
|
if (auto* type = llvm::any_cast<TypeIdAsRawType>(&arg)) {
|
|
return StringifyConstantInst(*sem_ir_,
|
|
sem_ir_->types().GetTypeInstId(type->type_id));
|
|
}
|
|
if (auto* type_id = llvm::any_cast<SemIR::TypeId>(&arg)) {
|
|
return "`" +
|
|
StringifyConstantInst(*sem_ir_,
|
|
sem_ir_->types().GetTypeInstId(*type_id)) +
|
|
"`";
|
|
}
|
|
if (auto* declared_facet_type_id =
|
|
llvm::any_cast<SemIR::DeclaredFacetTypeId>(&arg)) {
|
|
return "`" + StringifyDeclaredFacetType(*sem_ir_, *declared_facet_type_id) +
|
|
"`";
|
|
}
|
|
if (auto* specific_id = llvm::any_cast<SemIR::SpecificId>(&arg)) {
|
|
return "`" + StringifySpecific(*sem_ir_, *specific_id) + "`";
|
|
}
|
|
if (auto* typed_int = llvm::any_cast<TypedInt>(&arg)) {
|
|
return llvm::APSInt(typed_int->value,
|
|
!sem_ir_->types().IsSignedInt(typed_int->type));
|
|
}
|
|
if (auto* real_id = llvm::any_cast<RealId>(&arg)) {
|
|
RawStringOstream out;
|
|
sem_ir_->reals().Get(*real_id).Print(out);
|
|
return out.TakeStr();
|
|
}
|
|
if (auto* specific_interface =
|
|
llvm::any_cast<SemIR::SpecificInterface>(&arg)) {
|
|
return StringifySpecificInterface(*sem_ir_, *specific_interface);
|
|
}
|
|
if (auto* specific_interface_id =
|
|
llvm::any_cast<SemIR::SpecificInterfaceId>(&arg)) {
|
|
auto specific_interface =
|
|
sem_ir_->specific_interfaces().Get(*specific_interface_id);
|
|
return "`" + StringifySpecificInterface(*sem_ir_, specific_interface) + "`";
|
|
}
|
|
if (auto* specific_interface_raw =
|
|
llvm::any_cast<SpecificInterfaceIdAsRawType>(&arg)) {
|
|
auto specific_interface = sem_ir_->specific_interfaces().Get(
|
|
specific_interface_raw->specific_interface_id);
|
|
return StringifySpecificInterface(*sem_ir_, specific_interface);
|
|
}
|
|
if (auto* clang_type = llvm::any_cast<CppType>(&arg)) {
|
|
return clang_type->type.getAsString();
|
|
}
|
|
return DiagnosticEmitterBase::ConvertArg(arg);
|
|
}
|
|
|
|
} // namespace Carbon::Check
|