History

checkpatch: improve the unnecessary initialisers tests

Global and static variables don't need to be initialized to 0. There is already a test for this but the output message doesn't mention booleans initialized to false. Improve the output message and the test by adding various forms with possible specific integer types and possible multiple zeros. Miscellanea: o Use a variable to hold the possible 0 test

Joe Perches committed 11 years ago

Commit 6d32f7a1 parent 369c8dd1 file · +8 −7Browse files

Showing the changes to scripts/checkpatch.pl (1 of 1 file). Show all

Unified · Split

scripts/checkpatch.pl+8−7
@@ -370,6 +370,8 @@ our $typeTypedefs = qr{(?x: · 369 unmodified lines
370 $typeKernelTypedefs\b370 $typeKernelTypedefs\b
371)};371)};
372 372
373our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
374
373our $logFunctions = qr{(?x:375our $logFunctions = qr{(?x:
374 printk(?:_ratelimited|_once|)|376 printk(?:_ratelimited|_once|)|
375 (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|377 (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
@@ -3334,21 +3336,20 @@ sub process { · 2958 unmodified lines
3334 }3336 }
3335 3337
3336# check for global initialisers.3338# check for global initialisers.
3337 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*(?:0|NULL|false)\s*;/) {3339 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
3338 if (ERROR("GLOBAL_INITIALISERS",3340 if (ERROR("GLOBAL_INITIALISERS",
3339 "do not initialise globals to 0 or NULL\n" .3341 "do not initialise globals to $1\n" . $herecurr) &&
3340 $herecurr) &&
3341 $fix) {3342 $fix) {
3342 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*(0|NULL|false)\s*;/$1;/;3343 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
3343 }3344 }
3344 }3345 }
3345# check for static initialisers.3346# check for static initialisers.
3346 if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) {3347 if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
3347 if (ERROR("INITIALISED_STATIC",3348 if (ERROR("INITIALISED_STATIC",
3348 "do not initialise statics to 0 or NULL\n" .3349 "do not initialise statics to $1\n" .
3349 $herecurr) &&3350 $herecurr) &&
3350 $fix) {3351 $fix) {
3351 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/;3352 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
3352 }3353 }
3353 }3354 }
3354 3355