[llvm] [llvm] Do not use Console API if the output isn't a console device (PR #90230)

Kacper Michajłow via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 26 09:34:08 PDT 2024


https://github.com/kasper93 created https://github.com/llvm/llvm-project/pull/90230

This fixes the `-fcolor-diagnostics` to properly enable color output when not outputting to console devices, such as when using Ninja.

This affects only Windows.

>From 85e1aa6738cbd821d904dd24ab42669fdf73769c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Kacper=20Michaj=C5=82ow?= <kasper93 at gmail.com>
Date: Thu, 2 Feb 2023 18:32:03 +0100
Subject: [PATCH] [llvm] Do not use Console API if the output isn't a console
 device
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This fixes the `-fcolor-diagnostics` to properly enable color output
when not outputting to console devices, such as when using Ninja.

This affects only Windows.

Signed-off-by: Kacper Michajłow <kasper93 at gmail.com>
---
 llvm/lib/Support/raw_ostream.cpp | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/llvm/lib/Support/raw_ostream.cpp b/llvm/lib/Support/raw_ostream.cpp
index 8cb7b5ac68ea7e..993d75536112a2 100644
--- a/llvm/lib/Support/raw_ostream.cpp
+++ b/llvm/lib/Support/raw_ostream.cpp
@@ -645,6 +645,11 @@ raw_fd_ostream::raw_fd_ostream(int fd, bool shouldClose, bool unbuffered,
   // Check if this is a console device. This is not equivalent to isatty.
   IsWindowsConsole =
       ::GetFileType((HANDLE)::_get_osfhandle(fd)) == FILE_TYPE_CHAR;
+
+  // If this isn't a console device, don't try to use the API to set the color.
+  // Switch to ANSI escape codes instead.
+  if (!IsWindowsConsole)
+    llvm::sys::Process::UseANSIEscapeCodes(true);
 #endif
 
   // Get the starting position.



More information about the llvm-commits mailing list