-
Notifications
You must be signed in to change notification settings - Fork 11k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[llvm] Do not use Console API if the output isn't a console device #90230
base: main
Are you sure you want to change the base?
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write If you have received no comments on your PR for a week, you can request a review If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-clang-driver @llvm/pr-subscribers-llvm-support Author: Kacper Michajłow (kasper93) ChangesThis fixes the This affects only Windows. Full diff: https://github.com/llvm/llvm-project/pull/90230.diff 1 Files Affected:
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.
|
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@gmail.com>
Any news about this one? |
I am not opposed to the idea of making ANSI escape codes the default on Windows when the output is redirected, and also for console output if supported by the system. But currently this patch only does the former. Besides, in the case of Clang, it has the option |
So, I'm not very well acquainted with these APIs, so I'll try to summarize my understanding of the situation:
I'm not entirely following @alvinhochun's comment here - I presume this is about when we're outputting directly to a terminal, whether we should prefer ANSI codes or console APIs for setting color? And that it's possible to query this with All in all, this sounds mostly reasonable to me, but I'll see if @aganea or @tru or @zmodem have anything to say. |
Correct.
Correct.
To clarify. This patch makes us output ANSI codes instead of using console APIs for color, only if we are not outputting to Windows Console. So the current default behavior of using Console API is preserved. This patch changes the default behavior to output ANSI codes, if forced by Note that this overwrite
Yes, this is another topic, that we briefly discussed on IRC. Generally speaking I've made this patch the most basic to make it convenient from the user point of view. Possible extension of this changes would be:
I realize the proposed patch is only small change and we may want to more thorough changes, but I wanted to start off discussion about it. I think this patch alone is fine and even further changes can be made in another PR. |
I think this is a good idea. But it would need a release note. Does it need a test of some kind? To make sure the fallback to console api works maybe? |
Some sort of automated test would certainly be good, but I'm not really sure how doable that is in our test infrastructure. (I would expect that all test executions are towards something that isn't directly a console, etc.) So I think it's fine without one... But bonus points for a sequence of easy instructions for verifying that it works, that anyone can repeat both now and when looking into related things in the future. |
Just thinking out loud, the conhost properties has an option to "use legacy console" (which should disable VT), and it is backed by the registry key |
That's correct, but for checking support I would like to also note that |
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.