This patch adds --use-color command line option and UseColor option to clang-tidy to control colors in diagnostics. With these options, users can force colorful output. This is useful when using clang-tidy with parallelization command line tools (like ninja and GNU parallel), as they often pipe clang-tidy's standard output and make the colors disappear.
Details
Diff Detail
- Repository
- rG LLVM Github Monorepo
Event Timeline
mostly good, just nits.
clang-tools-extra/clang-tidy/ClangTidyOptions.h | ||
---|---|---|
130 | nit: ... wil be used if the terminal connected to standard output supports colors, or even simple if missing, it will be auto detect. | |
clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp | ||
233 | nit: just use-color | |
237 | nit: set cl::init(false) |
Address the comments above.
I also removed the incorrect DefaultOptions.ColorDiagnostics = ColorDiagnostics in ClangTidyMain.cpp. That produces incorrect UseColor: false with -dump-config.
Rename clang-tools-extra/test/clang-tidy/infrastructure/color-diagnostics.cpp to use-color.cpp.
clang-tools-extra/clang-tidy/ClangTidyOptions.cpp | ||
---|---|---|
99 | I'm not entirely sure this option belongs in the config file. However if it will get read in here, maybe update the help string for the command line option to say it overrides the UseColor option from the .clang-tidy configuration file. |
Say the --use-color command line option overrides the UseColor option in .clang-tidy file in its help text.
clang-tools-extra/clang-tidy/ClangTidyOptions.cpp | ||
---|---|---|
99 |
It will be useful in the config file if the user invokes clang-tidy via another tool that does not allow the user to set clang-tidy's command line options.
I've updated it. |
clang-tools-extra/clang-tidy/ClangTidyOptions.h | ||
---|---|---|
130 | detect -> detected |
Could this not use the -fcolor-diagnostics and fno-color-diagnostics command line flags that clang uses for its diagnostics to keep everything the same?
None of the clang-tidy command line options are prefixed with -f.
This command line option used to be --color-diagnostics, but I've followed @hokein's advice to change it to --use-color.
If you mean to use the -fcolor-diagnostics option from compile_commands.json, clang-tidy and clang are two separate tools. The users may not want them to share the same setting, because they may invoke them in different environments.
Fair point, will this option also control the color of the diagnostics emitted by clang or just clang tidy specific diagnostics?
Fair point, will this option also control the color of the diagnostics emitted by clang or just clang tidy specific diagnostics?
This option sets DiagOpts->ShowColors to true. As I known, it controls all diagnostics reported by the clang-tidy program (by clang::tidy::(anonymous namespace)::ErrorReporter), including clang-diagnostic-* and other clang-tidy checks.
Would a test case be needed?
clang-tools-extra/test/clang-tidy/infrastructure/use-color.cpp | ||
---|---|---|
11 | Not a fan of this test case as it only demonstrates the color behaviour of the process running the check not the actual option itself |
Would a test case be needed?
clang::tidy::clangTidyMain() shows that clang-tidy reports all diagnostics by clang::tidy::handleErrors(), which constructs a clang::tidy::(anonymous namespace)::ErrorReporter, no matter where they come from, so I think a test case is not required.
Not a fan of this test case as it only demonstrates the color behaviour of the process running the check not the actual option itself
What does "option itself" mean?
What I mean to say is, if the behaviour of the testing environment changes to pipe the result to a terminal that supports color, it could cause this test case to also fail.
The standard output is always piped to FileCheck's standard input. It will never become "a terminal that supports color".
Standard error is not redirected explicitly, but it is not used to detect color support.
I don't have commit access. @njames93 Could you please commit it for me (hyd-dev <yd-huang@outlook.com>)? Thanks!
nit: ... wil be used if the terminal connected to standard output supports colors, or even simple if missing, it will be auto detect.