From 899c2af576124df2918fd2ae976434df0ebbae5b Mon Sep 17 00:00:00 2001 From: _ov4 Date: Wed, 21 Oct 2026 14:06:54 +0000 Subject: feat(driver): color diagnostics like gcc with -fdiagnostics-color Add -fdiagnostics-color[=auto|always|never] and -fno-diagnostics-color. The default auto mode colors only a terminal stderr (TERM set and not dumb on Unix; a console that accepts VT sequences on Windows), so test harnesses, the desktop bundle and other pipes stay plain. Every diagnostic label now comes from cps::diagnosticPrefix, which emits gcc's default GCC_COLORS bytes: bold locus, bold red error/fatal error, bold magenta warning, bold cyan note. compilation terminated., runtime messages of the compiled program and lld output are unchanged. Co-Authored-By: Claude Opus 5.5 --- AGENTS/ARCH/DRIVER.md | 10 +++- doc/cpsc.1.md | 5 +- include/cps/Diagnostics.h | 14 +++++- lib/CodeGen/CodeGen.cc | 2 +- plugin/AGENTS/ARCH.md | 3 +- tools/driver/EmbeddedRunner.cc | 43 +++++++++++------ tools/driver/NativeCompiler.cc | 9 ++-- tools/driver/StandardLibrary.cc | 9 ++-- tools/driver/main.cc | 101 ++++++++++++++++++++++++++++++++-------- 9 files changed, 151 insertions(+), 45 deletions(-) diff --git a/AGENTS/ARCH/DRIVER.md b/AGENTS/ARCH/DRIVER.md index d571509..1b37d01 100644 --- a/AGENTS/ARCH/DRIVER.md +++ b/AGENTS/ARCH/DRIVER.md @@ -4,7 +4,8 @@ Driver behavior (`tools/driver/main.cc`). CodeGen-side unit and entry-point inva [CONVENTIONS/EXCEPTIONS_AND_UNITS.md](../CONVENTIONS/EXCEPTIONS_AND_UNITS.md); diagnostic style is in [CONVENTIONS/SYMBOLS_AND_ERRORS.md](../CONVENTIONS/SYMBOLS_AND_ERRORS.md). -- One or more source files, with optional `-S`, `-o path`, `-r` and `-no-std`; options may appear among inputs. +- One or more source files, with optional `-S`, `-o path`, `-r`, `-no-std` and + `-fdiagnostics-color[=auto|always|never]` / `-fno-diagnostics-color`; options may appear among inputs. Each stream is parsed independently. INCLUDE paths are relative to the including file, matched against command-line inputs with fs::equivalent. The driver rejects repeated inputs and computes transitive symbol visibility; cycles do not copy statements. @@ -36,7 +37,12 @@ style is in [CONVENTIONS/SYMBOLS_AND_ERRORS.md](../CONVENTIONS/SYMBOLS_AND_ERROR `compilation terminated.` once on compilation failure, including argument errors. Lexer/parser/codegen diagnostics use the gcc layout `:: error: ` (Diagnostics.h), with the path as given on the command line; driver-level errors - use `cpsc: error:` / `cpsc: fatal error:`. + use `cpsc: error:` / `cpsc: fatal error:`. Every label comes from + `cps::diagnosticPrefix`, which emits gcc's default colors when + `cps::DiagnosticColors` is set. The driver scans the color options right after + `-h`, before any other diagnostic; the last valid one wins, and `auto` requires a + terminal stderr (Unix: `TERM` set and not `dumb`; Windows: a console that accepts + VT sequences). `compilation terminated.` and lld's own output are not colored. Execution-stage failures do not print that line. Check the exit code rather than grepping stderr. - Successful compilation overwrites the output, but paths aliasing any input source diff --git a/doc/cpsc.1.md b/doc/cpsc.1.md index b156d6f..6a81a8a 100644 --- a/doc/cpsc.1.md +++ b/doc/cpsc.1.md @@ -6,7 +6,7 @@ cpsc - compile and run CIE pseudocode # SYNOPSIS -**cpsc** [**-r**] [**-S**] [**-no-std**] [**-o** *output*] *source* [*source* ...] +**cpsc** [**-r**] [**-S**] [**-no-std**] [**-fdiagnostics-color**[=*when*]] [**-o** *output*] *source* [*source* ...] **cpsc** **-h** @@ -39,6 +39,9 @@ The embedded standard library is loaded and linked automatically. Its std.cps im **-no-std** : Disable automatic standard-library loading and linking. Explicit INCLUDE and INCLUDE_OBJ inputs still work. +**-fdiagnostics-color**[=*when*], **-fno-diagnostics-color** +: Color compiler diagnostics like gcc: the `file:line:` or `cpsc:` prefix in bold, `error:` and `fatal error:` in bold red, `warning:` in bold magenta and `note:` in bold cyan. *when* is `auto` (the default), `always` or `never`; **-fdiagnostics-color** alone means `always` and **-fno-diagnostics-color** means `never`. The last such option wins. `auto` colors only when stderr is a terminal: on Unix/macOS `TERM` must also be set and not `dumb`; on Windows the console must accept ANSI escape sequences. Runtime diagnostics of the compiled program are never colored. + # NATIVE COMPILATION Native compilation uses LLVM, the Clang driver and LLD embedded in `cpsc`. Required startup objects, runtime archives and system-library link inputs are embedded too and unpacked temporarily when linking. No external compiler, linker or development SDK is needed. Generate programs for the compiler's own operating system and CPU architecture; cross-compilation is not supported. diff --git a/include/cps/Diagnostics.h b/include/cps/Diagnostics.h index 3492f46..4bcb877 100644 --- a/include/cps/Diagnostics.h +++ b/include/cps/Diagnostics.h @@ -9,13 +9,25 @@ enum class Severity { Error, Warning, Note, Fatal }; // Path of the unit being diagnosed, as given on the command line. inline std::string DiagnosticSource; +// Set by the driver from -fdiagnostics-color. +inline bool DiagnosticColors = false; + // gcc layout: ":