Files
carbon-lang/toolchain/parse/BUILD
Chandler Carruth 2c9739799b 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
2026-10-15 06:08:57 +00:00

259 lines
6.1 KiB
Python

# 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
load("//bazel/cc_rules:defs.bzl", "cc_library", "cc_test")
load("//bazel/manifest:defs.bzl", "manifest")
load("//testing/fuzzing:rules.bzl", "cc_fuzz_test")
package(default_visibility = ["//visibility:public"])
filegroup(
name = "testdata",
srcs = glob(["testdata/**/*.carbon"]),
)
manifest(
name = "testdata.txt",
srcs = [":testdata"],
)
cc_library(
name = "node_category",
srcs = ["node_category.cpp"],
hdrs = ["node_category.h"],
deps = [
"//common:enum_mask_base",
"//common:ostream",
"@llvm-project//llvm:Support",
],
)
cc_library(
name = "node_kind",
srcs = ["node_kind.cpp"],
hdrs = [
"node_ids.h",
"node_kind.h",
"typed_nodes.h",
],
textual_hdrs = ["node_kind.def"],
deps = [
":node_category",
"//common:check",
"//common:enum_base",
"//common:ostream",
"//toolchain/base:index_base",
"//toolchain/lex:token_index",
"//toolchain/lex:token_kind",
"@llvm-project//llvm:Support",
],
)
cc_test(
name = "typed_nodes_test",
size = "small",
srcs = ["typed_nodes_test.cpp"],
deps = [
":node_kind",
":parse",
":tree",
"//testing/base:gtest_main",
"//toolchain/diagnostics:emitter",
"//toolchain/diagnostics:mocks",
"//toolchain/lex",
"//toolchain/lex:tokenized_buffer",
"//toolchain/testing:compile_helper",
"@googletest//:gtest",
],
)
cc_library(
name = "context",
srcs = ["context.cpp"],
hdrs = ["context.h"],
deps = [
":node_kind",
":precedence",
":state",
":tree",
"//common:check",
"//common:ostream",
"//common:vlog",
"//toolchain/diagnostics:emitter",
"//toolchain/diagnostics:format_providers",
"//toolchain/lex:token_kind",
"//toolchain/lex:tokenized_buffer",
"@llvm-project//llvm:Support",
],
)
cc_library(
name = "parse",
srcs = ["parse.cpp"] +
# Glob handler files to avoid missing any.
glob([
"handle_*.cpp",
]),
hdrs = [
"handle.h",
"parse.h",
],
deps = [
":context",
":dump",
":node_kind",
":precedence",
":state",
":tree",
"//common:check",
"//common:ostream",
"//common:pretty_stack_trace_function",
"//toolchain/base:shared_value_stores",
"//toolchain/diagnostics:emitter",
"//toolchain/diagnostics:format_providers",
"//toolchain/diagnostics:renderer",
"//toolchain/lex:token_index",
"//toolchain/lex:token_kind",
"//toolchain/lex:tokenized_buffer",
],
)
cc_library(
name = "dump",
srcs = ["dump.cpp"],
hdrs = ["dump.h"],
deps = [
":context",
":node_kind",
":tree",
"//common:raw_string_ostream",
"//toolchain/lex:dump",
"//toolchain/lex:token_index",
],
# Always link dump methods.
alwayslink = 1,
)
cc_library(
name = "state",
srcs = ["state.cpp"],
hdrs = ["state.h"],
textual_hdrs = ["state.def"],
deps = ["//common:enum_base"],
)
cc_library(
name = "tree",
srcs = [
"extract.cpp",
"extract_declarations.cpp",
"extract_expressions.cpp",
"extract_patterns.cpp",
"extract_statements.cpp",
"tree.cpp",
"tree_and_subtrees.cpp",
],
hdrs = [
"extract.h",
"tree.h",
"tree_and_subtrees.h",
],
deps = [
":node_category",
":node_kind",
"//common:check",
"//common:error",
"//common:find",
"//common:ostream",
"//common:struct_reflection",
"//toolchain/base:fixed_size_value_store",
"//toolchain/base:value_store",
"//toolchain/lex:token_index",
"//toolchain/lex:token_kind",
"//toolchain/lex:tokenized_buffer",
"@llvm-project//llvm:Support",
],
)
cc_test(
name = "tree_test",
size = "small",
srcs = ["tree_test.cpp"],
deps = [
":node_kind",
":parse",
":tree",
"//common:ostream",
"//common:raw_string_ostream",
"//testing/base:gtest_main",
"//toolchain/base:shared_value_stores",
"//toolchain/diagnostics:emitter",
"//toolchain/diagnostics:mocks",
"//toolchain/lex",
"//toolchain/lex:tokenized_buffer",
"//toolchain/testing:compile_helper",
"//toolchain/testing:yaml_test_helpers",
"@googletest//:gtest",
"@llvm-project//llvm:Support",
],
)
cc_fuzz_test(
name = "parse_fuzzer",
size = "small",
srcs = ["parse_fuzzer.cpp"],
corpus = glob(["fuzzer_corpus/*"]),
deps = [
":parse",
"//common:check",
"//testing/fuzzing:libfuzzer_header",
"//toolchain/base:shared_value_stores",
"//toolchain/diagnostics:emitter",
"//toolchain/diagnostics:null_diagnostics",
"//toolchain/lex",
"@llvm-project//llvm:Support",
],
)
cc_library(
name = "precedence",
srcs = ["precedence.cpp"],
hdrs = ["precedence.h"],
deps = [
"//common:check",
"//toolchain/lex:token_kind",
"@llvm-project//llvm:Support",
],
)
cc_test(
name = "precedence_test",
size = "small",
srcs = ["precedence_test.cpp"],
deps = [
":precedence",
"//testing/base:gtest_main",
"//toolchain/lex:token_kind",
"@googletest//:gtest",
],
)
cc_test(
name = "coverage_test",
size = "small",
srcs = ["coverage_test.cpp"],
args = ["--testdata_manifest=$(location :testdata.txt)"],
data = [
":testdata",
":testdata.txt",
],
deps = [
":node_kind",
"//testing/base:gtest_main",
"//toolchain/testing:coverage_helper",
"@abseil-cpp//absl/flags:flag",
"@googletest//:gtest",
],
)