This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Provide fine control of color in run-clang-tidy
ClosedPublic

Authored by kesyog on Feb 11 2022, 10:11 AM.

Details

Summary

D90110 modified the behavior of run-clang-tidy to always pass the
--use-color option to clang-tidy, which enabled colored diagnostics
output regardless of TTY status or .clang-tidy settings. This left the
user with no option to disable the colored output.

This presents an issue when trying to parse the output of run-clang-tidy
programmaticall, as the output is polluted with ANSI escape characters.

This PR fixes this issue in two ways:

  1. It restores the default behavior of run-clang-tidy to let clang-tidy decide whether to color output. This allows the user to configure color via the UseColor option in a .clang-tidy file.
  2. It adds mutually exclusive, optional -use-color and -no-use-color argument flags that let the user explicitly set the color option via the invocation.

After this change the default behavior of run-clang-tidy when no
.clang-tidy file is available is now to show no color, presumably
because clang-tidy detects that the output is being piped and defaults
to not showing colored output. This seems like an acceptable tradeoff
to respect .clang-tidy configurations, as users can still use the
-use-color option to explicitly enable color.

Fixes #49441 (50097 in Bugzilla)

Diff Detail

Event Timeline

kesyog created this revision.Feb 11 2022, 10:11 AM
kesyog requested review of this revision.Feb 11 2022, 10:11 AM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 11 2022, 10:11 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
Eugene.Zelenko added inline comments.
clang-tools-extra/clang-tidy/tool/run-clang-tidy.py
111

Shouldn't it be just else:?

kesyog added inline comments.Feb 11 2022, 10:35 AM
clang-tools-extra/clang-tidy/tool/run-clang-tidy.py
111

There are three cases:

Argumentuse_colorBehavior
-use-colorTrue--use-color is passed to clang-tidy, force enabling color
-no-use-colorFalse--use-color=false is passed to clang-tidy, force disabling color
(none provided)NoneNothing passed to clang-tidy. clang-tidy follows its default coloring behavior

The case on the highlighted line is the second row of the table, and we have to check that use_color is not None to exclude the case of the third row.

I was trying to avoid the extra nesting of something like below, but maybe the intent would be clearer?

if use_color is not None:
  if use_color:
    start.append('--use-color')
  else:
    start.append('--use-color=false')
clang-tools-extra/clang-tidy/tool/run-clang-tidy.py
111

I think you implementation is incorrect. You should check for not None first and than set proper value for --use-color.

kesyog updated this revision to Diff 407950.Feb 11 2022, 10:48 AM

Refactor tri-state logic for readability

kesyog marked an inline comment as done.Feb 11 2022, 10:49 AM
kesyog added inline comments.
clang-tools-extra/clang-tidy/tool/run-clang-tidy.py
111

fixed

kesyog marked an inline comment as done.Feb 17 2022, 11:45 AM

ping

njames93 added inline comments.Feb 19 2022, 5:19 PM
clang-tools-extra/clang-tidy/tool/run-clang-tidy.py
255–264

All the arguments to this script mirror clang-tidy arguments, therefore I'm against introducing gnc style command line flags for controlling colour output. I'd suggest something like this which mirrors clang-tidys behaviour much more closely,
This requires an import

from distutils.util import strtobool

Tbh it may be better writing a strtobool that is in sync with llvms command line bool parser

{ "true", "TRUE", "True", "1"` -> true }
{ "false", "FALSE", "False", "0" -> false }
{ ... -> Error }

Why have you removed execute perms form the scripts, can you add them back please
$ chmod +x run-clang-tidy.py'
Unless you are using windows in which case you will have to manually edit the diff

kesyog updated this revision to Diff 410132.Feb 19 2022, 10:27 PM

Use argument style more consistent with clang-tidy & LLVM

kesyog marked an inline comment as done.Feb 19 2022, 10:35 PM

Thanks for the suggestions. I removed -no-use-color and modified -use-color to follow the behavior of LLVM's CLI parser. I also fixed the (very unintentional) file mode change.

njames93 retitled this revision from Provide fine control of color in run-clang-tidy to [clang-tidy] Provide fine control of color in run-clang-tidy.Feb 20 2022, 1:34 AM
njames93 accepted this revision.Feb 20 2022, 2:56 AM

LGTM

clang-tools-extra/clang-tidy/tool/run-clang-tidy.py
69

Didn't even realise an empty string was acceptable in LLVM CLI. Learn something new everyday.

This revision is now accepted and ready to land.Feb 20 2022, 2:56 AM

I don't have commit privileges, so I'll need help committing this to main. My name is "Kesavan Yogeswaran" and my email is hikes [at] google [dot] com

This revision was automatically updated to reflect the committed changes.