From: Joe Perches <joe@perches.com>
To: Sasha Levin <sashal@kernel.org>,
dwaipayanray1@gmail.com, lukas.bulwahn@gmail.com
Cc: mricon@kernel.org, corbet@lwn.net, skhan@linuxfoundation.org,
apw@canonical.com, workflows@vger.kernel.org,
linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2] checkpatch: add --json output mode
Date: Wed, 08 Apr 2026 11:16:40 -0700 [thread overview]
Message-ID: <bbd86dd5a7e457d368f5875589cc78287ece4193.camel@perches.com> (raw)
In-Reply-To: <20260408172435.1268067-1-sashal@kernel.org>
On Wed, 2026-04-08 at 13:24 -0400, Sasha Levin wrote:
Adding --json seems sensible but some of the
added checkpatch code seems odd to me.
> diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
> @@ -2395,6 +2400,18 @@ sub report {
>
> push(our @report, $output);
>
> + if ($json) {
> + our ($realfile, $realline);
Seems an odd way to check if $realfile/$readline is set
> + my %issue = (
> + level => $level,
> + type => $type,
> + message => $msg,
> + );
> + $issue{file} = $realfile if (defined $realfile && $realfile ne '');
> + $issue{line} = $realline + 0 if (defined $realline && $realline);
All the uses of + 0 seem unnecessary, but I gather it's for
string/decimal conversions.
> +sub json_print_result {
> + my ($filename, $total_errors, $total_warnings, $total_checks,
> + $total_lines, $issues, $used_types, $ignored_types) = @_;
> + my %result = (
> + filename => $filename,
> + total_errors => $total_errors + 0,
> + total_warnings => $total_warnings + 0,
> + total_checks => $total_checks + 0,
> + total_lines => $total_lines + 0,
> + issues => $issues,
> + );
> + $result{used_types} = $used_types if (defined $used_types);
> + $result{ignored_types} = $ignored_types if (defined $ignored_types);
> + my $json_encoder = JSON::PP->new->canonical->utf8;
Maybe add JSON pretty too?
> + print $json_encoder->encode(\%result) . "\n";
Still missing parentheses around print args.
I do know that not all existing print uses have parentheses.
I just prefer them to be more like C readable.
> +}
> +
> sub fixup_current_range {
> my ($lineRef, $offset, $length) = @_;
>
> @@ -2690,14 +2724,15 @@ sub process {
> my $last_coalesced_string_linenr = -1;
>
> our @report = ();
> + our @json_issues = ();
> our $cnt_lines = 0;
> our $cnt_error = 0;
> our $cnt_warn = 0;
> our $cnt_chk = 0;
>
> # Trace the real file/line as we go.
> - my $realfile = '';
> - my $realline = 0;
> + our $realfile = '';
> + our $realline = 0;
?
> @@ -7791,18 +7826,27 @@ sub process {
> # If we have no input at all, then there is nothing to report on
> # so just keep quiet.
> if ($#rawlines == -1) {
> + if ($json) {
> + json_print_result($filename, 0, 0, 0, 0, []);
> + }
> exit(0);
> }
>
> # In mailback mode only produce a report in the negative, for
> # things that appear to be patches.
> if ($mailback && ($clean == 1 || !$is_patch)) {
> + if ($json) {
> + json_print_result($filename, 0, 0, 0, 0, []);
> + }
> exit(0);
> }
>
> # This is not a patch, and we are in 'no-patch' mode so
> # just keep quiet.
> if (!$chk_patch && !$is_patch) {
> + if ($json) {
> + json_print_result($filename, 0, 0, 0, 0, []);
> + }
> exit(0);
> }
Duplicated code, maybe use a function or consolidate the code?
Something like:
if (($#rawlines == -1) ||
# If we have no input, there's nothing to report
($mailback && ($clean == 1 || !$is_patch)) ||
# In mailback mode only produce a report for what seems to be a patch
(!$chk_patch && !$is_patch)) {
# This is not a patch, and we are in 'no-patch' mode.
json_print_result($filename, 0, 0, 0, 0, []) if ($json);
exit(0);
}
>
> @@ -7850,6 +7894,13 @@ sub process {
> }
> }
>
> + if ($json) {
> + my @used = sort keys %use_type;
> + my @ignored = sort keys %ignore_type;
> + json_print_result($filename, $cnt_error, $cnt_warn,
> + $cnt_chk, $cnt_lines, \@json_issues,
> + \@used, \@ignored);
> + } else {
> print report_dump();
> if ($summary && !($clean == 1 && $quiet == 1)) {
> print "$filename " if ($summary_file);
> @@ -7878,8 +7929,9 @@ NOTE: Whitespace errors detected.
> EOM
> }
> }
> + } # end !$json
I quite dislike misleading indentation.
Perhaps it's unnecessary here and simpler to use an
exit in the new block at line 7850
>
> - if ($clean == 0 && $fix &&
> + if (!$json && $clean == 0 && $fix &&
> ("@rawlines" ne "@fixed" ||
> $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
> my $newfile = $filename;
> @@ -7918,7 +7970,7 @@ EOM
> }
> }
>
> - if ($quiet == 0) {
> + if (!$json && $quiet == 0) {
> print "\n";
> if ($clean == 1) {
> print "$vname has no obvious style problems and is ready for submission.\n";
>
prev parent reply other threads:[~2026-04-08 18:16 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-06 17:00 [PATCH] " Sasha Levin
2026-04-06 19:00 ` Konstantin Ryabitsev
2026-04-06 19:13 ` Sasha Levin
2026-04-06 19:22 ` Konstantin Ryabitsev
2026-04-06 20:34 ` Joe Perches
2026-04-06 20:41 ` Joe Perches
2026-04-08 17:24 ` [PATCH v2] " Sasha Levin
2026-04-08 18:16 ` Joe Perches [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=bbd86dd5a7e457d368f5875589cc78287ece4193.camel@perches.com \
--to=joe@perches.com \
--cc=apw@canonical.com \
--cc=corbet@lwn.net \
--cc=dwaipayanray1@gmail.com \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=lukas.bulwahn@gmail.com \
--cc=mricon@kernel.org \
--cc=sashal@kernel.org \
--cc=skhan@linuxfoundation.org \
--cc=workflows@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox