D62056 makes the output color if clang auto-detects a tty, but if it
does not, there is no way to force it to use colors anyway.
This patch adjusts the command-lines given to ClangTool which will
force color on or off if --use-color is specified.
Differential D94624
PATCH] [clang-query] Add a --use-color option to clang-query to allow forcing the behavior tomrittervg on Jan 13 2021, 11:31 AM. Authored by
Details
D62056 makes the output color if clang auto-detects a tty, but if it This patch adjusts the command-lines given to ClangTool which will
Diff Detail
Event TimelineComment Actions Thank you for working on this! I think this change should have some test coverage in clang-tools-extra/test/clang-query where a test file that emits some output is run with and without the --use-color option enabled, if possible. IIRC, there are some clang tests that you can probably use to find out the right command line arguments to pass to get the ANSI escape codes to print out. Comment Actions Can we make using color the default too? We already use colors for Decls I think, so this just adds colors for other Node types. Comment Actions I thought we already did? At least, for me on Windows, the effects of the patch are that I can pass --use-colors=0 to disable colors, but if I don't pass --use-colors or if I pass --use-colors=1, I get colors. Comment Actions I also don't think we need to add unit tests for this, just because we can. The tests would be more complex than the code and wouldn't add much value. https://softwareengineering.stackexchange.com/a/147342 There are lots of resources about this.
Comment Actions Actually, I think I need to be smarter than changing the default. We want to let clang auto-detect the tty and behave that way by default if the option isn't specified. Otherwise you'd get ASNI color codes when you pipe to a file. Comment Actions +1 to this, but also, you need to thread the option through to the diagnostics engine as well. For reference, here are some screenshots of what I'm seeing on Windows with your original patch applied when running clang-query and clang-tidy. Note the difference in diagnostic text colorization where clang-tidy controls it via use-color and clang-query currently does not. Comment Actions Thanks for this, I think it's looking more promising! I'd still like to see some test coverage for the changes so long as it doesn't turn into a slog. Tests like clang\test\AST\ast-dump-color.cpp show roughly how it's done in the frontend. If it turns out that the tests are too much of a pain to write for clang-query, it won't be a blocker.
Comment Actions I started trying to work on a patch, but I'm still unpacking from a move and don't have all my machines set up - trying to enable and build tests filled up my hard drive (even after I removed the easy-to-remove stuff), so I don't think I'm going to be able to create a test for this in the short-term.
Comment Actions Okay, I think this LGTM even without the test coverage. Thank you for the patch, do you need someone to commit it on your behalf? Comment Actions I'm not certain why, but the patch is not applying to a fresh pull for me. F:\llvm-project>git apply "F:\Aaron Ballman\Desktop\D94624.diff" error: patch failed: clang-tools-extra/clang-query/tool/ClangQuery.cpp:109 error: clang-tools-extra/clang-query/tool/ClangQuery.cpp: patch does not apply As usual, git's not being particularly helpful in stating what went wrong. Perhaps rebasing your patch on ToT and re-uploading it will cause the issue to go away? Comment Actions Thanks, that was sufficient for me to apply it. I've commit on your behalf in d6d36baa33e76ace11ac20c03de1097d48bd9246, thank you for the patch! |
Semi-idle curiosity, does the source manager have a different diagnostics object than the AST? I guess I'm a bit surprised this change is needed (or is the change just a minor simplification to the code?).