Commit 938224b5 authored by Joe Perches's avatar Joe Perches Committed by Linus Torvalds

checkpatch: warn when casting constants to c90 int or longer types

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
Signed-off-by: default avatarJoe Perches <joe@perches.com>
Suggested-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent f5948701
...@@ -433,6 +433,28 @@ our @typeList = ( ...@@ -433,6 +433,28 @@ our @typeList = (
qr{${Ident}_handler_fn}, qr{${Ident}_handler_fn},
@typeListMisordered, @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 @typeListFile = ();
our @typeListWithAttr = ( our @typeListWithAttr = (
@typeList, @typeList,
...@@ -5272,6 +5294,26 @@ sub process { ...@@ -5272,6 +5294,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(&) # check for sizeof(&)
if ($line =~ /\bsizeof\s*\(\s*\&/) { if ($line =~ /\bsizeof\s*\(\s*\&/) {
WARN("SIZEOF_ADDRESS", WARN("SIZEOF_ADDRESS",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment