checkpatch does not handle full files well when they are passed on
stdin, because it does not know how to treat the text, and whether it is
a C file, or a DTS file, or something else, and so it assumes that when
it works with stdin it should be a unified diff. For full files it
expects to have a file name as an argument and read the contents from
disk. Unfortunately this does not well when trying to use checkpatch as
an online linter and feed it contents of an editor buffer that have not
made it to the disk yet.

To solve the above introduce a new optional argument --stdin-filename=FILE
that allows tell checkpatch the kind of file it is dealing with and
apply appropriate set of checks and rules to it.

Signed-off-by: Dmitry Torokhov <[email protected]>
---
 Documentation/dev-tools/checkpatch.rst |  4 ++++
 scripts/checkpatch.pl                  | 11 +++++++++++
 2 files changed, 15 insertions(+)

diff --git a/Documentation/dev-tools/checkpatch.rst 
b/Documentation/dev-tools/checkpatch.rst
index dccede68698c..b521e3ca6ebf 100644
--- a/Documentation/dev-tools/checkpatch.rst
+++ b/Documentation/dev-tools/checkpatch.rst
@@ -68,6 +68,10 @@ Available options:
 
    Show the diffed file position instead of the input file position.
 
+ - --stdin-filename
+
+   When using stdin, identify the file as FILE.
+
  - -g,  --git
 
    Treat FILE as a single commit or a git revision range.
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index e56374662ff7..e26951ceb36b 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -57,6 +57,7 @@ my %ignore_type = ();
 my @ignore = ();
 my $help = 0;
 my $configuration_file = ".checkpatch.conf";
+my $stdin_filename;
 my $max_line_length = 100;
 my $ignore_perl_version = 0;
 my $minimum_perl_version = 5.10.0;
@@ -94,6 +95,7 @@ Options:
   --emacs                    emacs compile window format
   --terse                    one line per report
   --showfile                 emit diffed file position, not input file position
+  --stdin-filename=FILE      when using stdin, identify the file as FILE
   -g, --git                  treat FILE as a single commit or git revision 
range
                              single git commit with:
                                <rev>
@@ -323,6 +325,7 @@ GetOptions(
        'showfile!'     => \$showfile,
        'f|file!'       => \$file,
        'g|git!'        => \$git,
+       'stdin-filename=s' => \$stdin_filename,
        'subjective!'   => \$check,
        'strict!'       => \$check,
        'ignore=s'      => \@ignore,
@@ -2652,6 +2655,10 @@ sub is_userspace {
 sub process {
        my $filename = shift;
 
+       if ($filename eq '-' && defined($stdin_filename)) {
+               $filename = $stdin_filename;
+       }
+
        my $linenr=0;
        my $prevline="";
        my $prevrawline="";
@@ -2891,6 +2898,10 @@ sub process {
                        $realfile =~ s@^([^/]*)/@@ if (!$file);
                        $in_commit_log = 0;
 
+                       if ($realfile eq "-" && defined($stdin_filename)) {
+                               $realfile = $stdin_filename;
+                       }
+
                        $p1_prefix = $1;
                        if (!$file && $tree && $p1_prefix ne '' &&
                            -e "$root/$p1_prefix") {
-- 
2.53.0.1018.g2bb0e51243-goog


-- 
Dmitry

Reply via email to