Skip to contentSkip to conversation

scripts/checkpatch: Add code spelling check

RFC In review Zhao Liu 2 participants 6 messages Dec 4, 2023 – Dec 5, 2023 Checkpatch

lore
Share & tools
lore

Zhao Liu

author
#0 lore raw

cover scripts/checkpatch: Add code spelling check

From: Zhao Liu <[email protected]>

Inspired by Linux's spelling check, QEMU could also add the similar support in checkpatch.pl to help ease the burden on trivial's maintainers ;-).

QEMU's checkpatch.pl is mainly based on an older version of Linux's checkpatch.pl, so this RFC ports Linux's codespell-related functionality to QEMU.

This RFC contains mainly the following work:
* Added default typo dictionary "selling.text". When using checkpatch.pl, it will be based on selling.txt for typo checking.
* Added "--codespell" and "--codespellfile" options for enhanced spell checking. The former will use either the system dictionary or a python dictionary for spell checking, while the latter allows the user to use a customized dictionary.
* Based on the QEMU typo statistics from v7.0.0 to v8.2.0-rc2, updated the default dictionary "selling.txt" to be more in line with QEMU developers' "typo habits".

Thanks and Best Regards,
Zhao
---
Zhao Liu (3):
scripts/checkpatch: Check common spelling be default scripts/checkpatch: Add --codespell and --codespellfile options scripts/spelling: Add common spelling mistakes in default spelling.txt

 scripts/checkpatch.pl |  110 ++-
 scripts/spelling.txt  | 1729 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 1838 insertions(+), 1 deletion(-)
 create mode 100644 scripts/spelling.txt
Signature · 1 line
2.34.1
author
#1 lore raw

RFC 1/3 scripts/checkpatch: Check common spelling be default

From: Zhao Liu <[email protected]>

Add the check for common spelling mistakes for QEMU, which stole selling.txt from Linux kernel and referenced the Linux kernel's implementation in checkpatch.pl.

This check covers common spelling mistakes, and can be updated/ extended as per QEMU's realities.

---
 scripts/checkpatch.pl |   44 ++
 scripts/spelling.txt  | 1713 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 1757 insertions(+)
 create mode 100644 scripts/spelling.txt
scripts/checkpatch.pl +44 -0
index 6e4100d2a41c..755a1f683866 100755
@@ -7,10 +7,13 @@
 
 use strict;
 use warnings;
+use File::Basename;
+use Cwd 'abs_path';
 use Term::ANSIColor qw(:constants);
 
 my $P = $0;
 $P =~ s@.*/@@g;
+my $D = dirname(abs_path($0));
 
 our $SrcFile    = qr{\.(?:(h|c)(\.inc)?|cpp|s|S|pl|py|sh)$};
 
@@ -35,6 +38,7 @@ my $summary_file = 0;
 my $root;
 my %debug;
 my $help = 0;
+my $spelling_file = "$D/spelling.txt";
 
 sub help {
 	my ($exitcode) = @_;
@@ -337,6 +341,31 @@ our @typeList = (
 	qr{guintptr},
 );
 
+# Load common spelling mistakes and build regular expression list.
+my $misspellings;
+my %spelling_fix;
+
+if (open(my $spelling, '<', $spelling_file)) {
+	while (<$spelling>) {
+		my $line = $_;
+
+		$line =~ s/\s*\n?$//g;
+		$line =~ s/^\s*//g;
+
+		next if ($line =~ m/^\s*#/);
+		next if ($line =~ m/^\s*$/);
+
+		my ($suspect, $fix) = split(/\|\|/, $line);
+
+		$spelling_fix{$suspect} = $fix;
+	}
+	close($spelling);
+} else {
+	warn "No typos will be found - file '$spelling_file': $!\n";
+}
+
+$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
+
 # This can be modified by sub possible.  Since it can be empty, be careful
 # about regexes that always match, because they can cause infinite loops.
 our @modifierList = (
@@ -1585,6 +1614,21 @@ sub process {
 			WARN("8-bit UTF-8 used in possible commit log\n" . $herecurr);
 		}
 
+# Check for various typo / spelling mistakes
+		if (defined($misspellings) &&
+		    ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
+			while ($rawline =~ /(?:^|[^\w\-'`])($misspellings)(?:[^\w\-'`]|$)/gi) {
+				my $typo = $1;
+				my $blank = copy_spacing($rawline);
+				my $ptr = substr($blank, 0, $-[1]) . "^" x length($typo);
+				my $hereptr = "$hereline$ptr\n";
+				my $typo_fix = $spelling_fix{lc($typo)};
+				$typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
+				$typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
+				WARN("'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $hereptr);
+			}
+		}
+
 # ignore non-hunk lines and lines being removed
 		next if (!$hunk_line || $line =~ /^-/);
 
scripts/spelling.txt new file +1713 -0

1,713 lines not on this page · raw message

Signature · 1 line
2.34.1
#2 lore raw

Re: [RFC 1/3] scripts/checkpatch: Check common spelling be default

Quoted 4 lines · Zhao Liu

From: Zhao Liu <[email protected]>

Add the check for common spelling mistakes for QEMU, which stole selling.txt from Linux kernel and referenced the Linux kernel's

You need to sellcheck^Wspellcheck the above line ;-)

Quoted 11 lines

implementation in checkpatch.pl.

This check covers common spelling mistakes, and can be updated/ extended as per QEMU's realities.

---
  scripts/checkpatch.pl |   44 ++
  scripts/spelling.txt  | 1713 +++++++++++++++++++++++++++++++++++++++++
  2 files changed, 1757 insertions(+)

create mode 100644 scripts/spelling.txt

I like the idea of having spellchecking in checkpatch.pl ... not sure though if we need both, this patch and support for codespell. If you ask me, I'd just go with the next codespell patch and avoid adding a full spelling.txt file here ... but if others like this patch here, too, I'm also fine with it.

Thomas

author
#3 lore raw

Re: [RFC 1/3] scripts/checkpatch: Check common spelling be default

Hi Thomas,

Quoted 11 lines · Thomas Huth

Date: Mon, 4 Dec 2023 10:07:12 +0100
From: Thomas Huth <[email protected]>
Subject: Re: [RFC 1/3] scripts/checkpatch: Check common spelling be default

Quoted 4 lines · Zhao Liu

From: Zhao Liu <[email protected]>

Add the check for common spelling mistakes for QEMU, which stole selling.txt from Linux kernel and referenced the Linux kernel's

You need to sellcheck^Wspellcheck the above line ;-)

Oh no, I didn't expect this place to be wrong...--codespelling isn't infallible!

Quoted 6 lines
Quoted 6 lines

implementation in checkpatch.pl.

This check covers common spelling mistakes, and can be updated/ extended as per QEMU's realities.

Quoted 8 lines
Quoted 6 lines
---
  scripts/checkpatch.pl |   44 ++
  scripts/spelling.txt  | 1713 +++++++++++++++++++++++++++++++++++++++++
  2 files changed, 1757 insertions(+)

create mode 100644 scripts/spelling.txt

I like the idea of having spellchecking in checkpatch.pl

Thanks!

Quoted 5 lines

... not sure though
if we need both, this patch and support for codespell. If you ask me, I'd just go with the next codespell patch and avoid adding a full spelling.txt file here ... but if others like this patch here, too, I'm also fine with it.

OK, I'll wait a few days to see if anyone else has any comments, if not I'll refresh a V2 and just keep the next patch.

Regards,
Zhao

author
#4 lore raw

RFC 2/3 scripts/checkpatch: Add --codespell and --codespellfile options

In reply to Zhao Liu

From: Zhao Liu <[email protected]>

Add two spelling check options (--codespell and --codespellfile) to enhance spelling check through dictionary, which copied the Linux kernel's implementation in checkpatch.pl.

---
 scripts/checkpatch.pl | 66 ++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 65 insertions(+), 1 deletion(-)
scripts/checkpatch.pl +65 -1
index 755a1f683866..20ee07f016ca 100755
@@ -38,7 +38,10 @@ my $summary_file = 0;
 my $root;
 my %debug;
 my $help = 0;
+my $codespell = 0;
 my $spelling_file = "$D/spelling.txt";
+my $codespellfile = "/usr/share/codespell/dictionary.txt";
+my $user_codespellfile = "";
 
 sub help {
 	my ($exitcode) = @_;
@@ -70,6 +73,9 @@ Options:
                              is all off)
   --test-only=WORD           report only warnings/errors containing WORD
                              literally
+  --codespell                Use the codespell dictionary for spelling/typos
+                             (default:$codespellfile)
+  --codespellfile            Use this codespell dictionary
   --color[=WHEN]             Use colors 'always', 'never', or only when output
                              is a terminal ('auto'). Default is 'auto'.
   -h, --help, --version      display this help and exit
@@ -102,15 +108,37 @@ GetOptions(
 	'summary!'	=> \$summary,
 	'mailback!'	=> \$mailback,
 	'summary-file!'	=> \$summary_file,
-
 	'debug=s'	=> \%debug,
 	'test-only=s'	=> \$tst_only,
+	'codespell!'	=> \$codespell,
+	'codespellfile=s'	=> \$user_codespellfile,
 	'color=s'       => \$color,
 	'no-color'      => sub { $color = 'never'; },
 	'h|help'	=> \$help,
 	'version'	=> \$help
 ) or help(1);
 
+if ($user_codespellfile) {
+	# Use the user provided codespell file unconditionally
+	$codespellfile = $user_codespellfile;
+} elsif (!(-f $codespellfile)) {
+	# If /usr/share/codespell/dictionary.txt is not present, try to find it
+	# under codespell's install directory: <codespell_root>/data/dictionary.txt
+	if (($codespell || $help) && which("python3") ne "") {
+		my $python_codespell_dict = << "EOF";
+
+import os.path as op
+import codespell_lib
+codespell_dir = op.dirname(codespell_lib.__file__)
+codespell_file = op.join(codespell_dir, 'data', 'dictionary.txt')
+print(codespell_file, end='')
+EOF
+
+		my $codespell_dict = `python3 -c "$python_codespell_dict" 2> /dev/null`;
+		$codespellfile = $codespell_dict if (-f $codespell_dict);
+	}
+}
+
 help(0) if ($help);
 
 my $exit = 0;
@@ -364,6 +392,30 @@ if (open(my $spelling, '<', $spelling_file)) {
 	warn "No typos will be found - file '$spelling_file': $!\n";
 }
 
+if ($codespell) {
+	if (open(my $spelling, '<', $codespellfile)) {
+		while (<$spelling>) {
+			my $line = $_;
+
+			$line =~ s/\s*\n?$//g;
+			$line =~ s/^\s*//g;
+
+			next if ($line =~ m/^\s*#/);
+			next if ($line =~ m/^\s*$/);
+			next if ($line =~ m/, disabled/i);
+
+			$line =~ s/,.*$//;
+
+			my ($suspect, $fix) = split(/->/, $line);
+
+			$spelling_fix{$suspect} = $fix;
+		}
+		close($spelling);
+	} else {
+		warn "No codespell typos will be found - file '$codespellfile': $!\n";
+	}
+}
+
 $misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
 
 # This can be modified by sub possible.  Since it can be empty, be careful
@@ -506,6 +558,18 @@ sub top_of_kernel_tree {
 	return 1;
 }
 
+sub which {
+	my ($bin) = @_;
+
+	foreach my $path (split(/:/, $ENV{PATH})) {
+		if (-e "$path/$bin") {
+			return "$path/$bin";
+		}
+	}
+
+	return "";
+}
+
 sub expand_tabs {
 	my ($str) = @_;
 
Signature · 1 line
2.34.1
author
#5 lore raw

RFC 3/3 scripts/spelling: Add common spelling mistakes in default spelling.txt

In reply to Zhao Liu

From: Zhao Liu <[email protected]>

Select the typos in commits from 7.0.0 to 8.2.0-rc2 that were typed more than three times to add to the default dictionary selling.txt.

The typos were counted by (Referenced Kees' command in Linux kernel's commit 66b47b4a9dad00):

$ git log --format='%H' v7.0.0..v8.2.0-rc2 | \
   while read commit ; do \
     echo "commit $commit" ; \
     git log --format=email --stat -p -1 $commit | \
     ./scripts/checkpatch.pl --codespell - ; \
   done | \
   tee spell_v7.0.0..v8.2.0-rc2.txt | \
   grep "may be misspelled" | awk '{print $2}' | \
   tr A-Z a-z | sort | uniq -c | sort -rn

The typos were listed as follows (In addition to variable naming and misreporting, select typos greater than three times):

1289 'fpr'

    222 'ot'
     45 'hace'
     25 'te'
     24 'parm'
     21 'sie'
     20 'nd'
     20 'clen'
     15 'sring'
     14 'hax'
     13 'hda'
     12 'endianess'
     10 'tabl'
     10 'stip'
     10 'datas'
      9 'reenable'
      8 'unitialized'
      8 'invers'
      7 'seh'
      7 'priviledge'
      7 'indentification'
      7 'existance'
      6 'varience'
      6 'unuseful'
      6 'tranfer'
      6 'tne'
      6 'octects'
      6 'informations'
      6 'indicies'
      6 'extenstion'
      6 'betwen'
      6 'ammends'
      5 'writting'
      5 'wont'
      5 'som'
      5 'responsability'
      5 'padd'
      5 'offsetp'
      5 'negotation'
      5 'necesary'
      5 'nam'
      5 'maintainance'
      5 'extnesion'
      5 'convertion'
      5 'configuraiton'
      5 'comparision'
      5 'arbitrer'
      5 'an
      5 'algorithim'
      5 'addreses'
      5 'achived'
      5 'accomodate'
      4 'undocummented'
      4 'ue'
      4 'truely'
      4 'tre'
      4 'suppoted'
      4 'separatly'
      4 'reenabled'
      4 'occured'
      4 'myu'
      4 'mone'
      4 'ment'
      4 'limitaions'
      4 'instread'
      4 'infomation'
      4 'implment'
      4 'hammmer'
      4 'desciptor'
      4 'defintions'
      4 'crypted'
      4 'constext'
      4 'acces'
      3 'wronly'
      3 'wether'
      3 'vill'
      3 'unsupport'
      3 'unneded'
      3 'transfered'
      3 'sxl'
      3 'suppor'
      3 'superceded'
      3 'succesfully'
      3 'slighly'
      3 'setted'
      3 'respectivelly'
      3 'reigster'
      3 'recive'
      3 'priviledged'
      3 'potentialy'
      3 'phyiscal'
      3 'pathes'
      3 'othe'
      3 'ontaining'
      3 'nunber'
      3 'missmatch'
      3 'minumum'
      3 'mesage'
      3 'legnth'
      3 'kenel'
      3 'intialized'
      3 'inbetween'
      3 'implemetation'
      3 'garanteed'
      3 'explictly'
      3 'enhacements'
      3 'ealier'
      3 'doen't'
      3 'diferent'
      3 'debuging'
      3 'daa'
      3 'convergance'
      3 'compatiblity'
      3 'caf'
      3 'buid'
      3 'becase'
      3 'bandwith'
      3 'atleast'
      3 'asume'
      3 'analoguous'
      3 'alse'
      3 'addtionally'
      3 'addtional'
      3 'addresss'
      3 'acknowledgement'
      3 'accross'
      3 'acceses'
---
 scripts/spelling.txt | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
scripts/spelling.txt +16 -0
index 549ffd0f2d78..177dee2bb28d 100644
@@ -28,6 +28,7 @@ accelaration||acceleration
 accelearion||acceleration
 acceleratoin||acceleration
 accelleration||acceleration
+acces||access
 accesing||accessing
 accesnt||accent
 accessable||accessible
@@ -50,6 +51,7 @@ acessable||accessible
 acess||access
 acessing||accessing
 achitecture||architecture
+achived||achieved
 acient||ancient
 acitions||actions
 acitve||active
@@ -95,6 +97,7 @@ albumns||albums
 alegorical||allegorical
 algined||aligned
 algorith||algorithm
+algorithim||algorithm
 algorithmical||algorithmically
 algoritm||algorithm
 algoritms||algorithms
@@ -124,6 +127,7 @@ altough||although
 alue||value
 ambigious||ambiguous
 ambigous||ambiguous
+ammend||amend
 amoung||among
 amount of times||number of times
 amout||amount
@@ -248,6 +252,7 @@ beeing||being
 befor||before
 begining||beginning
 beter||better
+betwen||between
 betweeen||between
 bianries||binaries
 bitmast||bitmask
@@ -350,6 +355,7 @@ comsuming||consuming
 comaptible||compatible
 compability||compatibility
 compaibility||compatibility
+comparision||comparison
 comparsion||comparison
 compatability||compatibility
 compatable||compatible
@@ -385,6 +391,7 @@ configred||configured
 configuartion||configuration
 configuation||configuration
 configued||configured
+configuraiton||configuration
 configuratoin||configuration
 configuraton||configuration
 configuretion||configuration
@@ -658,6 +665,7 @@ exteneded||extended
 extensability||extensibility
 extention||extension
 extenstion||extension
+extnesion||extension
 extracter||extractor
 faied||failed
 faield||failed
@@ -796,6 +804,7 @@ implementd||implemented
 implemetation||implementation
 implemntation||implementation
 implentation||implementation
+implment||implement
 implmentation||implementation
 implmenting||implementing
 incative||inactive
@@ -812,11 +821,13 @@ incuding||including
 inculde||include
 indendation||indentation
 indended||intended
+indentification||identification
 independant||independent
 independantly||independently
 independed||independent
 indiate||indicate
 indicat||indicate
+indicies||indices
 inexpect||inexpected
 inferface||interface
 infinit||infinite
@@ -850,6 +861,7 @@ instace||instance
 instal||install
 instanciate||instantiate
 instanciated||instantiated
+instread||instead
 instuments||instruments
 insufficent||insufficient
 inteface||interface
@@ -934,6 +946,7 @@ libary||library
 librairies||libraries
 libraris||libraries
 licenceing||licencing
+limitaion||limitation
 limted||limited
 logaritmic||logarithmic
 loggging||logging
@@ -1331,6 +1344,7 @@ resouce||resource
 resouces||resources
 resoures||resources
 responce||response
+responsability||responsibility
 resrouce||resource
 ressizes||resizes
 ressource||resource
@@ -1554,6 +1568,7 @@ throught||through
 tansition||transition
 trackling||tracking
 troughput||throughput
+truely||truly
 trys||tries
 thses||these
 tiggers||triggers
@@ -1623,6 +1638,7 @@ unknouwn||unknown
 unknow||unknown
 unkown||unknown
 unamed||unnamed
+undocummented||undocumented
 uneeded||unneeded
 unneded||unneeded
 unneccecary||unnecessary
Signature · 1 line
2.34.1