aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Frontend/CompilerInvocation.cpp
diff options
context:
space:
mode:
authorAaron Ballman <aaron@aaronballman.com>2023-06-07 11:53:08 -0400
committerAaron Ballman <aaron@aaronballman.com>2023-06-07 11:55:06 -0400
commitda0a7d5cfb0c65bcb54a1ba5ed6c93bb906648e4 (patch)
tree8b7c0d55ab3e9003f7db6fcfb8644c2b326dac3f /clang/lib/Frontend/CompilerInvocation.cpp
parent774b33632998591a6714948da62fcf39126b9ee4 (diff)
downloadllvm-da0a7d5cfb0c65bcb54a1ba5ed6c93bb906648e4.zip
llvm-da0a7d5cfb0c65bcb54a1ba5ed6c93bb906648e4.tar.gz
llvm-da0a7d5cfb0c65bcb54a1ba5ed6c93bb906648e4.tar.bz2
Add support for the NO_COLOR environment variable
Clang currently supports disabling color diagnostic output via -fno-color-diagnostics. However, there is a somewhat long-standing push to support use of an environment variable to override color output so that users can set up their terminal such that most color output is disabled (largely for accessibility reasons). There are two competing de facto standards to accomplish this: NO_COLOR (https://no-color.org/) and CLICOLOR/CLICOLOR_FORCE (http://bixense.com/clicolors/). This patch adds support for NO_COLOR as that appears to be the more commonly supported feature, at least when comparing issues and pull requests: https://github.com/search?q=NO_COLOR&type=issues (2.2k issues, 35k pull requests) https://github.com/search?q=CLICOLOR&type=issues (1k issues, 3k pull requests) It's also the more straightforward and thoroughly-specified of the two options. If NO_COLOR is present as an environment variable (regardless of value), color output is suppressed unless the command line specifies use of color output (command line takes precedence over the environment variable). Differential Revision: https://reviews.llvm.org/D152285
Diffstat (limited to 'clang/lib/Frontend/CompilerInvocation.cpp')
-rw-r--r--clang/lib/Frontend/CompilerInvocation.cpp12
1 files changed, 11 insertions, 1 deletions
diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp
index f74bca3..0ce26fd 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -2341,10 +2341,20 @@ clang::CreateAndPopulateDiagOpts(ArrayRef<const char *> Argv) {
unsigned MissingArgIndex, MissingArgCount;
InputArgList Args = getDriverOptTable().ParseArgs(
Argv.slice(1), MissingArgIndex, MissingArgCount);
+
+ bool ShowColors = true;
+ if (std::optional<std::string> NoColor =
+ llvm::sys::Process::GetEnv("NO_COLOR");
+ NoColor && !NoColor->empty()) {
+ // If the user set the NO_COLOR environment variable, we'll honor that
+ // unless the command line overrides it.
+ ShowColors = false;
+ }
+
// We ignore MissingArgCount and the return value of ParseDiagnosticArgs.
// Any errors that would be diagnosed here will also be diagnosed later,
// when the DiagnosticsEngine actually exists.
- (void)ParseDiagnosticArgs(*DiagOpts, Args);
+ (void)ParseDiagnosticArgs(*DiagOpts, Args, /*Diags=*/nullptr, ShowColors);
return DiagOpts;
}