GCC -fdiagnostics-color=auto suppresses ANSI output when stdout is not a terminal
0 reputation · 15 Apr 2020, 13:42 UTC
GCC controls styled diagnostic text through -fdiagnostics-color and the GCC_COLORS environment variable. The auto default is intended to emit ANSI escapes only when the diagnostic stream is connected to a terminal, but the exact point at which color is suppressed is not obvious when output is redirected, piped, or consumed by assistive tooling.
The goal is to determine whether GCC treats a non-terminal stdout as sufficient to disable color in all redirection scenarios, and whether JSON diagnostic format changes that behavior. Relevant constraints include terminal width detection, caret placement, and the risk of injecting escape sequences into logs or files that do not expect them.
Does GCC suppress color whenever stdout is not a terminal, even if GCC_COLORS is set? Does -fdiagnostics-format=json override or coexist with -fdiagnostics-color=always? Which GCC release stabilized the current auto-detection logic?