aboutsummaryrefslogtreecommitdiff
path: root/include/cps/Diagnostics.h
diff options
context:
space:
mode:
author_ov4 <ov4@ov4.org>2026-10-21 14:06:54 +0000
committer_ov4 <ov4@ov4.org>2026-10-21 14:06:54 +0000
commit899c2af576124df2918fd2ae976434df0ebbae5b (patch)
treebb67a979eca30a6d447c2819576b81dc84741f17 /include/cps/Diagnostics.h
parentb4ffb2d9498560557821714e2d4651279dd6817c (diff)
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 <noreply@anthropic.com>
Diffstat (limited to 'include/cps/Diagnostics.h')
-rw-r--r--include/cps/Diagnostics.h14
1 files changed, 13 insertions, 1 deletions
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: "<locus>: <label>: "; an empty locus prints the label alone.
+// With colors, the bytes match gcc's default GCC_COLORS: bold locus
+// (including its colon), then the label in bold red/magenta/cyan.
inline std::string diagnosticPrefix(Severity S, const std::string &Locus = "cpsc") {
const char *Label = S == Severity::Error ? "error: "
: S == Severity::Warning ? "warning: "
: S == Severity::Note ? "note: "
: "fatal error: ";
- return (Locus.empty() ? "" : Locus + ": ") + Label;
+ if (!DiagnosticColors) return (Locus.empty() ? "" : Locus + ": ") + Label;
+ const char *Color = S == Severity::Warning ? "01;35"
+ : S == Severity::Note ? "01;36"
+ : "01;31";
+ auto Span = [](const char *SGR, const std::string &Text) {
+ return std::string("\33[") + SGR + "m\33[K" + Text + "\33[m\33[K";
+ };
+ return (Locus.empty() ? "" : Span("01", Locus + ":") + " ") + Span(Color, Label);
}
// Prints "<path>:<line>: <label>: <message>\n" (just "<path>:" when the line is