diff options
Diffstat (limited to 'include')
| -rw-r--r-- | include/cps/Diagnostics.h | 14 |
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 |
