aboutsummaryrefslogtreecommitdiffstats
path: root/scripts
diff options
context:
space:
mode:
authorJoe Perches <joe@perches.com>2016-01-20 17:59:15 -0500
committerLinus Torvalds <torvalds@linux-foundation.org>2016-01-20 20:09:18 -0500
commit938224b5e596c1c30d968ffd927a578ea7c4f45b (patch)
tree8d2ae9d42c9bc887f0dc984979d2e7c01ecf990d /scripts
parentf5948701891322770ad6ede317da5fc9cf33d2f0 (diff)
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: Joe Perches <joe@perches.com> Suggested-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'scripts')
-rwxr-xr-xscripts/checkpatch.pl42
1 files changed, 42 insertions, 0 deletions
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index c7bf1aa2eeb3..86457062db29 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -433,6 +433,28 @@ our @typeList = (
433 qr{${Ident}_handler_fn}, 433 qr{${Ident}_handler_fn},
434 @typeListMisordered, 434 @typeListMisordered,
435); 435);
436
437our $C90_int_types = qr{(?x:
438 long\s+long\s+int\s+(?:un)?signed|
439 long\s+long\s+(?:un)?signed\s+int|
440 long\s+long\s+(?:un)?signed|
441 (?:(?:un)?signed\s+)?long\s+long\s+int|
442 (?:(?:un)?signed\s+)?long\s+long|
443 int\s+long\s+long\s+(?:un)?signed|
444 int\s+(?:(?:un)?signed\s+)?long\s+long|
445
446 long\s+int\s+(?:un)?signed|
447 long\s+(?:un)?signed\s+int|
448 long\s+(?:un)?signed|
449 (?:(?:un)?signed\s+)?long\s+int|
450 (?:(?:un)?signed\s+)?long|
451 int\s+long\s+(?:un)?signed|
452 int\s+(?:(?:un)?signed\s+)?long|
453
454 int\s+(?:un)?signed|
455 (?:(?:un)?signed\s+)?int
456)};
457
436our @typeListFile = (); 458our @typeListFile = ();
437our @typeListWithAttr = ( 459our @typeListWithAttr = (
438 @typeList, 460 @typeList,
@@ -5272,6 +5294,26 @@ sub process {
5272 } 5294 }
5273 } 5295 }
5274 5296
5297# check for cast of C90 native int or longer types constants
5298 if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
5299 my $cast = $1;
5300 my $const = $2;
5301 if (WARN("TYPECAST_INT_CONSTANT",
5302 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
5303 $fix) {
5304 my $suffix = "";
5305 my $newconst = $const;
5306 $newconst =~ s/${Int_type}$//;
5307 $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
5308 if ($cast =~ /\blong\s+long\b/) {
5309 $suffix .= 'LL';
5310 } elsif ($cast =~ /\blong\b/) {
5311 $suffix .= 'L';
5312 }
5313 $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
5314 }
5315 }
5316
5275# check for sizeof(&) 5317# check for sizeof(&)
5276 if ($line =~ /\bsizeof\s*\(\s*\&/) { 5318 if ($line =~ /\bsizeof\s*\(\s*\&/) {
5277 WARN("SIZEOF_ADDRESS", 5319 WARN("SIZEOF_ADDRESS",