Files
carbon-lang/toolchain/driver/driver.h
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

87 lines
3.5 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_DRIVER_DRIVER_H_
#define CARBON_TOOLCHAIN_DRIVER_DRIVER_H_
#include "common/command_line.h"
#include "common/filesystem.h"
#include "common/terminal/capabilities.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/StringRef.h"
#include "toolchain/driver/driver_env.h"
#include "toolchain/driver/driver_subcommand.h"
namespace Carbon {
// Command line interface driver.
//
// Provides simple API to parse and run command lines for Carbon. It is
// generally expected to be used to implement command line tools for working
// with the language.
class Driver {
public:
// Constructs a driver with the provided environment. `input_stream` is
// optional; other parameters are required.
//
// `error_file` is the file `error_stream` writes to, when there is one.
// Diagnostic rendering is detected from it, and stays plain when it is
// invalid, which is what an in-memory stream gets.
explicit Driver(llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs,
const InstallPaths* installation, FILE* input_stream,
llvm::raw_pwrite_stream* output_stream,
llvm::raw_pwrite_stream* error_stream,
Filesystem::WriteFileRef error_file = {},
bool fuzzing = false, bool enable_leaking = false)
: fs_(std::move(fs)),
installation_(installation),
input_stream_(input_stream),
output_stream_(output_stream),
error_stream_(error_stream),
error_file_(error_file),
fuzzing_(fuzzing),
enable_leaking_(enable_leaking) {}
// Parses the given arguments into both a subcommand to select the operation
// to perform and any arguments to that subcommand.
//
// Returns true if the operation succeeds. If the operation fails, returns
// false and any information about the failure is printed to the registered
// error stream (stderr by default).
auto RunCommand(llvm::ArrayRef<llvm::StringRef> args) -> DriverResult;
// Sets a memory-usage sink for subsequent commands. When set, a compile
// command collects each compiled file's memory usage into `mem_usage` (the
// same data `--dump-mem-usage` reports), so it can be queried
// programmatically. The labels of files compiled together are accumulated
// into the one `mem_usage`; consumers typically sum entries by label. Pass
// null to disable.
auto set_mem_usage(MemUsage* mem_usage) -> void { mem_usage_ = mem_usage; }
private:
// Returns what to render diagnostics for, honoring `preferences`.
auto ErrorCapabilities(Terminal::Preferences preferences) const
-> Terminal::Capabilities;
// We store the initial values in the `DriverEnv` that will be used for each
// subcommand invocation here. These are used as the _starting_ values of the
// environment, but individual `RunCommand` invocations may customize the
// `DriverEnv` instance changing these values.
//
// For details on each of these fields, see the documentation in `DriverEnv`.
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs_;
const InstallPaths* installation_;
FILE* input_stream_;
llvm::raw_pwrite_stream* output_stream_;
llvm::raw_pwrite_stream* error_stream_;
Filesystem::WriteFileRef error_file_;
bool fuzzing_;
bool enable_leaking_;
MemUsage* mem_usage_ = nullptr;
};
} // namespace Carbon
#endif // CARBON_TOOLCHAIN_DRIVER_DRIVER_H_