checkpatch: add --json output mode
Subsystems (4)
PATCH 1/1 checkpatch: add --json output mode
Add a --json flag to checkpatch.pl that emits structured JSON output, making results machine-parseable for CI systems, IDE integrations, and AI-assisted code review tools.
The JSON output includes per-file totals (errors, warnings, checks, lines) and an array of individual issues with structured fields for level, type, message, file path, and line number.
The --json flag is mutually exclusive with --terse and --emacs. Normal text output behavior is completely unchanged when --json is not specified.
--- Documentation/dev-tools/checkpatch.rst | 7 +++ scripts/checkpatch.pl | 86 ++++++++++++++++++++++++-- 2 files changed, 87 insertions(+), 6 deletions(-)
Documentation/dev-tools/checkpatch.rst +7 -0
@@ -64,6 +64,13 @@ Available options: Output only one line per report. + - --json + + Output results as a JSON object. The object includes total error, warning, + and check counts, plus an array of individual issues with structured fields + for level, type, message, file, and line number. Cannot be used with + --terse or --emacs. + - --showfile Show the diffed file position instead of the input file position.
scripts/checkpatch.pl +80 -6
183 lines not on this page · raw message
Signature · 1 line
2.53.0
Quoted 11 lines · Sasha Levin
Add a --json flag to checkpatch.pl that emits structured JSON output, making results machine-parseable for CI systems, IDE integrations, and AI-assisted code review tools.
The JSON output includes per-file totals (errors, warnings, checks, lines) and an array of individual issues with structured fields for level, type, message, file path, and line number.
The --json flag is mutually exclusive with --terse and --emacs. Normal text output behavior is completely unchanged when --json is not specified.
I see that it's writing json out manually, implementing its own escaping. While there are upsides to not requiring a perl json library, I think it's fair to expect that people who would want to get json output can probably make sure that JSON::XS is installed.
Not a strong object, but seems cleaner that way.
-K
Quoted 13 lines · Konstantin Ryabitsev
Quoted 11 lines · Sasha Levin
Add a --json flag to checkpatch.pl that emits structured JSON output, making results machine-parseable for CI systems, IDE integrations, and AI-assisted code review tools.
The JSON output includes per-file totals (errors, warnings, checks, lines) and an array of individual issues with structured fields for level, type, message, file path, and line number.
The --json flag is mutually exclusive with --terse and --emacs. Normal text output behavior is completely unchanged when --json is not specified.
Quoted 6 lines
I see that it's writing json out manually, implementing its own escaping. While there are upsides to not requiring a perl json library, I think it's fair to expect that people who would want to get json output can probably make sure that
JSON::XSis installed.Not a strong object, but seems cleaner that way.
No objection here, but from what I saw the checkpatch code only uses core perl packages so I wanted to keep it that way.
Signature · 2 lines
Thanks, Sasha
Quoted 9 lines · Sasha Levin
Quoted 6 lines
I see that it's writing json out manually, implementing its own escaping. While there are upsides to not requiring a perl json library, I think it's fair to expect that people who would want to get json output can probably make sure that
JSON::XSis installed.Not a strong object, but seems cleaner that way.
No objection here, but from what I saw the checkpatch code only uses core perl packages so I wanted to keep it that way.
I saw that, too, but I think that stems from the expectation that we need to make it easy to run checkpatch by any random person submitting patches, which is why, by default, we'll output human-readable results.
JSON output, on the other hand, is mostly useful for specific setups that have a lot more control over their environment and we don't have to stick to the "pure perl only" guideline here.
Generating correct json is an exercise in corner cases, which is why I'd rather this is done with a library that has addressed most of them already.
Regards,
Signature · 1 line
KR
Joe Perches
maintainerQuoted 7 lines · Konstantin Ryabitsev
On Mon, Apr 06, 2026 at 03:13:52PM -0400, Sasha Levin wrote:
I see that it's writing json out manually, implementing its own escaping.Quoted 5 lines
Quoted 5 lines
While there are upsides to not requiring a perl json library, I think it's fair to expect that people who would want to get json output can probably make sure that
JSON::XSis installed.Not a strong object, but seems cleaner that way.
To me too.
JSON:PP is standard since 5.14, and that's 15 years old.
I'd rather just require 5.14 as a minimum and remove a bunch of other checks too.
Joe Perches
maintainerQuoted 3 lines · Sasha Levin
Add a --json flag to checkpatch.pl that emits structured JSON output, making results machine-parseable for CI systems, IDE integrations, and AI-assisted code review tools.
Seems a reasonable idea but perhaps can be improved
scripts/checkpatch.pl for my $filename (@ARGV) { · line 1381
Quoted 8 lines
scripts/checkpatch.pl +1
-1@@ -1372,7 +1376,7 @@ for my $filename (@ARGV) { $file = $oldfile if ($is_git_file); } -if (!$quiet) { +if (!$quiet && !$json) { hash_show_words(\%use_type, "Used"); hash_show_words(\%ignore_type, "Ignored");
Maybe keep but update?
scripts/checkpatch.pl sub process { · line 7841
Quoted 7 lines
scripts/checkpatch.pl +3
-0@@ -7791,18 +7836,33 @@ 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) { + print '{"filename":"' . json_escape($filename) . + '","total_errors":0,"total_warnings":0,' .
poor formatting for that trailing ". please separate by content blocks.
Review sub process { · line 7843
Quoted 2 lines
(quoted diff) +2
-0+ '"total_checks":0,"total_lines":0,"issues":[]}' . "\n"; + }
I'd prefer to keep the print() style used elsewhere and perhaps the JSON:PP module should be used here.
PATCH v2 1/1 checkpatch: add --json output mode
In reply to Sasha LevinAdd a --json flag to checkpatch.pl that emits structured JSON output, making results machine-parseable for CI systems, IDE integrations, and AI-assisted code review tools.
The JSON output includes per-file totals (errors, warnings, checks, lines) and an array of individual issues with structured fields for level, type, message, file path, and line number.
The --json flag is mutually exclusive with --terse and --emacs. Normal text output behavior is completely unchanged when --json is not specified.
---
Changes since v1:
- Replace hand-rolled json_escape()/json_encode_issue() with JSON::PP module (core since perl 5.14), as suggested by Konstantin and Joe
- Factor duplicated empty-result JSON blocks into json_print_result() helper
- Include used_types and ignored_types arrays in JSON output instead of suppressing hash_show_words, per Joe's suggestion
--- Documentation/dev-tools/checkpatch.rst | 7 +++ scripts/checkpatch.pl | 64 +++++++++++++++++++++++--- 2 files changed, 65 insertions(+), 6 deletions(-)
Documentation/dev-tools/checkpatch.rst +7 -0
@@ -64,6 +64,13 @@ Available options: Output only one line per report. + - --json + + Output results as a JSON object. The object includes total error, warning, + and check counts, plus an array of individual issues with structured fields + for level, type, message, file, and line number. Cannot be used with + --terse or --emacs. + - --showfile Show the diffed file position instead of the input file position.
scripts/checkpatch.pl +58 -6
167 lines not on this page · raw message
Signature · 1 line
2.53.0
Joe Perches
maintainerOn 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.
scripts/checkpatch.pl sub report { · line 2404
Quoted 7 lines
scripts/checkpatch.pl +2
-0@@ -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
scripts/checkpatch.pl sub report { · line 2411
Quoted 7 lines
scripts/checkpatch.pl +7
-0+ 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.
Quoted 4 lines
scripts/checkpatch.pl +4
-0+sub json_print_result { + my ($filename, $total_errors, $total_warnings, $total_checks, + $total_lines, $issues, $used_types, $ignored_types) = @_; + my %result = (
scripts/checkpatch.pl sub report_dump { · line 2435
Quoted 10 lines
scripts/checkpatch.pl +10
-0+ 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?
scripts/checkpatch.pl sub report_dump { · line 2436
Quoted 1 line
scripts/checkpatch.pl +1
-0+ 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.
Quoted 10 lines
scripts/checkpatch.pl +3
-0+} + 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 = ();
scripts/checkpatch.pl sub process { · line 2735
Quoted 10 lines
scripts/checkpatch.pl +2
-2our $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;
?
Quoted 17 lines
scripts/checkpatch.pl +6
-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);
scripts/checkpatch.pl sub process { · line 7851
Quoted 10 lines
scripts/checkpatch.pl +3
-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);
}
Quoted 16 lines
scripts/checkpatch.pl +7
-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);
scripts/checkpatch.pl NOTE: Whitespace errors detected. · line 7932
Quoted 4 lines
scripts/checkpatch.pl +1
-0@@ -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
Quoted 16 lines
scripts/checkpatch.pl +2
-2- 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";
PATCH v3 1/1 checkpatch: add --json output mode
In reply to Sasha LevinAdd a --json flag to checkpatch.pl that emits structured JSON output, making results machine-parseable for CI systems, IDE integrations, and AI-assisted code review tools.
The JSON output includes per-file totals (errors, warnings, checks, lines) and an array of individual issues with structured fields for level, type, message, file path, and line number.
A separate --json-pretty flag emits the same JSON in a pretty-printed (indented, multi-line) form for human reading.
The --json (and --json-pretty) flags are mutually exclusive with --terse and --emacs. Normal text output behaviour is completely unchanged when --json is not specified.
---
v3:
- Report the same line numbers text mode prints (matches --emacs and --showfile). Fixes wrong locations on commit-message issues.
- Drop redundant defined() checks and + 0 coercions in the JSON code; the values are already numeric and always defined.
- Add --json-pretty for indented output.
- Add parens around print() arguments.
- Consolidate the three empty-result early exits into one block.
- Return at the JSON branch instead of wrapping the rest of process() in an else, so multi-file runs emit one document per file and the indentation is no longer misleading.
v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/
Documentation/dev-tools/checkpatch.rst | 13 ++++ scripts/checkpatch.pl | 86 +++++++++++++++++++------- 2 files changed, 78 insertions(+), 21 deletions(-)
Documentation/dev-tools/checkpatch.rst +13 -0
@@ -64,6 +64,19 @@ Available options: Output only one line per report. + - --json + + Output results as a JSON object. The object includes total error, + warning, and check counts, plus an array of individual issues with + structured fields for level, type, message, file, and line number. + Output is one compact JSON document per input file, suitable for CI + and scripted post-processing. Cannot be used with --terse or --emacs. + + - --json-pretty + + Like --json, but emit pretty-printed (indented, multi-line) JSON for + human reading. + - --showfile Show the diffed file position instead of the input file position.
scripts/checkpatch.pl +65 -21
170 lines not on this page · raw message
Signature · 1 line
2.53.0
Joe Perches
maintainerQuoted 3 lines · Sasha Levin
Add a --json flag to checkpatch.pl that emits structured JSON output, making results machine-parseable for CI systems, IDE integrations, and AI-assisted code review tools.
[]
Quoted 2 lines
A separate --json-pretty flag emits the same JSON in a pretty-printed (indented, multi-line) form for human reading
Why not just always use pretty?
Would a script care?
Quoted 10 lines · Joe Perches
Quoted 3 lines · Sasha Levin
Add a --json flag to checkpatch.pl that emits structured JSON output, making results machine-parseable for CI systems, IDE integrations, and AI-assisted code review tools.
[]
Quoted 2 lines
A separate --json-pretty flag emits the same JSON in a pretty-printed (indented, multi-line) form for human reading
Why not just always use pretty?
Would a script care?
Who's the intended consumer for the --json-pretty?
I my mind, --json is there to make it easier for tooling to process the output.
A user can already achieve the same result by piping the json output through jq or other similar tools:
$ ./scripts/checkpatch.pl --json 0001-checkpatch-add-json-output-mode.patch {"filename":"0001-checkpatch-add-json-output-mode.patch","ignored_types":[],"issues":[],"total_checks":0,"total_errors":0,"total_lines":189,"total_warnings":0,"used_types":[]}
$ ./scripts/checkpatch.pl --json 0001-checkpatch-add-json-output-mode.patch | jq
{
"filename": "0001-checkpatch-add-json-output-mode.patch",
"ignored_types": [],
"issues": [],
"total_checks": 0,
"total_errors": 0,
"total_lines": 189,
"total_warnings": 0,
"used_types": []
}Signature · 2 lines
Thanks, Sasha
Joe Perches
maintainerQuoted 6 lines · Sasha Levin
On Sat, Apr 25, 2026 at 02:52:35PM -0700, Joe Perches wrote:
On 2026-04-25 13:04, Sasha Levin wrote:
Add a --json flag to checkpatch.pl that emits structured JSON output,Quoted 3 lines
Quoted 2 lines
making results machine-parseable for CI systems, IDE integrations, and AI-assisted code review tools.
[]
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
A separate --json-pretty flag emits the same JSON in a pretty-printed (indented, multi-line) form for human reading
Why not just always use pretty?
Would a script care?Who's the intended consumer for the --json-pretty?
I my mind, --json is there to make it easier for tooling to process the output.
Agree, but does the pretty output make it harder for tooling?
Quoted 9 lines · Joe Perches
Quoted 8 lines · Sasha Levin
On Sat, Apr 25, 2026 at 02:52:35PM -0700, Joe Perches wrote:
On 2026-04-25 13:04, Sasha Levin wrote:
Add a --json flag to checkpatch.pl that emits structured JSON output,Quoted 5 lines
Quoted 2 lines
making results machine-parseable for CI systems, IDE integrations, and AI-assisted code review tools.
[]
Quoted 2 lines
A separate --json-pretty flag emits the same JSON in a pretty-printed (indented, multi-line) form for human reading
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
Why not just always use pretty?
Would a script care?Who's the intended consumer for the --json-pretty?
I my mind, --json is there to make it easier for tooling to process the output.
Agree, but does the pretty output make it harder for tooling?
For real JSON parsers, no - they handle either form fine.
The one issue is multi-file invocations: --json emits one compact document per file per line (NDJSON), which lets plain-shell consumers do while read line; do ... or pipe through grep/awk/head. Pretty mode loses that property because each document spans multiple lines, so consumers need a
streaming JSON parser.
Signature · 2 lines
Thanks, Sasha
Joe Perches
maintainerscripts/checkpatch.pl
Quoted 1 line · Sasha Levin
scripts/checkpatch.pl +0
-0
[]
Quoted 8 lines
scripts/checkpatch.pl +4
-0@@ -2403,6 +2426,24 @@ sub report_dump { our @report; } +sub json_print_result { + my ($filename, $total_errors, $total_warnings, $total_checks, + $total_lines, $issues, $used_types, $ignored_types) = @_; + my %result = (
scripts/checkpatch.pl sub report_dump { · line 2442
Quoted 10 lines
scripts/checkpatch.pl +10
-0+ filename => $filename, + total_errors => $total_errors, + total_warnings => $total_warnings, + total_checks => $total_checks, + total_lines => $total_lines, + 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 canonical isn't great as it outputs keys in alphabetic order. The output may be more sensible in the defined order.