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

via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 26 09:34:59 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-support

Author: Kacper Michajłow (kasper93)

<details>
<summary>Changes</summary>

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.

---
Full diff: https://github.com/llvm/llvm-project/pull/90230.diff


1 Files Affected:

- (modified) llvm/lib/Support/raw_ostream.cpp (+5) 


``````````diff
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.

``````````

</details>


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


More information about the llvm-commits mailing list