Re: [PATCH v2] checkpatch: add --json output mode

Joe Perches
2026-04-08T18:16:53Z

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";
>