Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1264215

[PATCH] checkpatch: Warn when casting constants to c90 int or longer types

From Joe Perches <joe@perches.com>
Newsgroups linux.kernel
Subject [PATCH] checkpatch: Warn when casting constants to c90 int or longer types
Date 2015-11-06 19:40 +0100
Message-ID <qrU5A-875-21@gated-at.bofh.it> (permalink)
References <qrAzU-3Gs-17@gated-at.bofh.it> <qrDo6-5L5-13@gated-at.bofh.it> <qrDxM-5P4-3@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


Linus Torvalds wrote:

> I can't but help to react that this:
>  #define IOMMU_ERROR_CODE       (~(unsigned long) 0)
> Not that this *matters*, but it's a bit odd to have to cast constants
> to perfectly regular C types.

So add a test that looks for constants that are cast to
standard C90 int or longer types and suggest using C90
"6.4.4.1 Integer constants" integer-suffixes instead.

Miscellanea:

o Add a --fix option too

Suggested-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Joe Perches <joe@perches.com>
---
 scripts/checkpatch.pl | 42 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 2d88cbf9..24cdeb0 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -431,6 +431,28 @@ our @typeList = (
 	qr{${Ident}_handler_fn},
 	@typeListMisordered,
 );
+
+our $C90_int_types = qr{(?x:
+	long\s+long\s+int\s+(?:un)?signed|
+	long\s+long\s+(?:un)?signed\s+int|
+	long\s+long\s+(?:un)?signed|
+	(?:(?:un)?signed\s+)?long\s+long\s+int|
+	(?:(?:un)?signed\s+)?long\s+long|
+	int\s+long\s+long\s+(?:un)?signed|
+	int\s+(?:(?:un)?signed\s+)?long\s+long|
+
+	long\s+int\s+(?:un)?signed|
+	long\s+(?:un)?signed\s+int|
+	long\s+(?:un)?signed|
+	(?:(?:un)?signed\s+)?long\s+int|
+	(?:(?:un)?signed\s+)?long|
+	int\s+long\s+(?:un)?signed|
+	int\s+(?:(?:un)?signed\s+)?long|
+
+	int\s+(?:un)?signed|
+	(?:(?:un)?signed\s+)?int
+)};
+
 our @typeListFile = ();
 our @typeListWithAttr = (
 	@typeList,
@@ -5240,6 +5262,26 @@ sub process {
 			}
 		}
 
+# check for cast of C90 native int or longer types constants
+		if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
+			my $cast = $1;
+			my $const = $2;
+			if (WARN("TYPECAST_INT_CONSTANT",
+				 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
+			    $fix) {
+				my $suffix = "";
+				my $newconst = $const;
+				$newconst =~ s/${Int_type}$//;
+				$suffix .= 'U' if ($cast =~ /\bunsigned\b/);
+				if ($cast =~ /\blong\s+long\b/) {
+					$suffix .= 'LL';
+				} elsif ($cast =~ /\blong\b/) {
+					$suffix .= 'L';
+				}
+				$fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
+			}
+		}
+
 # check for sizeof(&)
 		if ($line =~ /\bsizeof\s*\(\s*\&/) {
 			WARN("SIZEOF_ADDRESS",
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

Back to linux.kernel | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

[GIT] Sparc David Miller <davem@davemloft.net> - 2015-11-05 22:50 +0100
  Re: [GIT] Sparc Linus Torvalds <torvalds@linux-foundation.org> - 2015-11-06 01:50 +0100
    Re: [GIT] Sparc Linus Torvalds <torvalds@linux-foundation.org> - 2015-11-06 02:00 +0100
      Re: [GIT] Sparc Julian Calaby <julian.calaby@gmail.com> - 2015-11-06 02:10 +0100
        Re: [GIT] Sparc Julia Lawall <julia.lawall@lip6.fr> - 2015-11-06 07:50 +0100
          Re: [GIT] Sparc Julian Calaby <julian.calaby@gmail.com> - 2015-11-07 04:40 +0100
            Re: [GIT] Sparc Julia Lawall <julia.lawall@lip6.fr> - 2015-11-07 07:10 +0100
      [PATCH] checkpatch: Warn when casting constants to c90 int or  longer types Joe Perches <joe@perches.com> - 2015-11-06 19:40 +0100
    Re: [GIT] Sparc David Miller <davem@davemloft.net> - 2015-11-06 06:20 +0100

csiph-web