<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/119224>119224</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
clang-format's -fcolor-diagnostics is broken
</td>
</tr>
<tr>
<th>Labels</th>
<td>
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
bonzini
</td>
</tr>
</table>
<pre>
clang-format's `-fcolor-diagnostics` option simply does not work, and the working option `--color` is undocumented. The problem is that `-fcolor-diagnostics` is a complete no-operation; in fact it is a bool that is initialized to true. gdb shows:
(gdb) p ShowColors
$2 = {<llvm::cl::Option> = {
... <llvm::cl::opt_storage<bool, false, false>> = {Value = true, ... }, ...}
on entry to clang-format's main, meaning that specifying the option on the command line does nothing at all.
To see how clang-format determines whether to use colors you need to look at the beginning of `SMDiagnostic::print`, which simply does
ColorMode Mode = ShowColors ? ColorMode::Auto : ColorMode::Disable;
showing once more that in fact the option cannot force-on the colors (`-fno-color-diagnostics` instead works). Continuing in `SMDiagnostic::print`, this RAII constructor would write the escape sequence to the terminal:
WithColor S(OS, raw_ostream::SAVEDCOLOR, true, false, Mode);
It ends up in `WithColor::changeColor`, which does
if (colorsEnabled())
OS.changeColor(Color, Bold, BG);
Digging further down, `colorsEnabled()` is where the Mode member is consulted:
bool WithColor::colorsEnabled() {
switch (Mode) {
case ColorMode::Enable:
return true;
case ColorMode::Disable:
return false;
case ColorMode::Auto:
return AutoDetectFunction(OS);
}
llvm_unreachable("All cases handled above.");
}
and the `AutoDetectFunction` is
static bool DefaultAutoDetectFunction(const raw_ostream &OS) {
return *UseColor == cl::BOU_UNSET ? OS.has_colors()
: *UseColor == cl::BOU_TRUE;
}
`UseColor` is controlled by the `--color` option, so unless that option was set, you go to `OS.has_colors()` even in the presence of `-fcolor-diagnostics`, and terminal colors will *not* be force-enabled.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyEVttu27oS_Rr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