checkpatch? (was: Re: [PATCH v3] coccinelle: misc: add uninitialized_var.cocci script)
diff mbox series

Message ID 28483d2663213666503e0109230ac3eb742c8a52.camel@perches.com
State In Next
Commit 1d6a814bcd3182076641e437d8ec77bbac31e3de
Headers show
Series
  • checkpatch? (was: Re: [PATCH v3] coccinelle: misc: add uninitialized_var.cocci script)
Related show

Commit Message

Joe Perches Sept. 1, 2020, 2:37 p.m. UTC
On Tue, 2020-09-01 at 12:48 +0300, Denis Efremov wrote:
> uninitialized_var() macro was removed from the sources [1] and
> other warning-silencing tricks were deprecated [2]. The purpose of this
> cocci script is to prevent new occurrences of uninitialized_var()
> open-coded variants.

> +(
> +* T var =@p var;
> +|
> +* T var =@p *(&(var));
> +|
> +* var =@p var
> +|
> +* var =@p *(&(var))
> +)

Adding a checkpatch test might be a good thing too.

---
 scripts/checkpatch.pl | 11 +++++++++++
 1 file changed, 11 insertions(+)

Comments

Denis Efremov Sept. 2, 2020, 5:17 a.m. UTC | #1
On 9/1/20 5:37 PM, Joe Perches wrote:
> On Tue, 2020-09-01 at 12:48 +0300, Denis Efremov wrote:
>> uninitialized_var() macro was removed from the sources [1] and
>> other warning-silencing tricks were deprecated [2]. The purpose of this
>> cocci script is to prevent new occurrences of uninitialized_var()
>> open-coded variants.
> 
>> +(
>> +* T var =@p var;
>> +|
>> +* T var =@p *(&(var));
>> +|
>> +* var =@p var
>> +|
>> +* var =@p *(&(var))
>> +)
> 
> Adding a checkpatch test might be a good thing too.
> 
> ---
>  scripts/checkpatch.pl | 11 +++++++++++
>  1 file changed, 11 insertions(+)
> 
> diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
> index 149518d2a6a7..300b2659aab3 100755
> --- a/scripts/checkpatch.pl
> +++ b/scripts/checkpatch.pl
> @@ -3901,6 +3901,17 @@ sub process {
>  #ignore lines not being added
>  		next if ($line =~ /^[^\+]/);
>  
> +# check for self assigments used to avoid compiler warnings
> +# e.g.:	int foo = foo, *bar = NULL;
> +#	struct foo bar = *(&(bar));
> +		if ($line =~ /^\+\s*(?:$Declare)?([A-Za-z_][A-Za-z\d_]*)\s*=/) {
> +			my $var = $1;
> +			if ($line =~ /^\+\s*(?:$Declare)?$var\s*=\s*(?:$var|\*\s*\(?\s*&\s*\(?\s*$var\s*\)?\s*\)?)\s*[;,]/) {
> +				WARN("SELF_ASSIGNMENT",
> +				     "Do not use self-assignments to avoid compiler warnings\n" . $herecurr);
> +			}
> +		}
> +
>  # check for dereferences that span multiple lines
>  		if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
>  		    $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {

Looks good. I also faced this kind of assignments after declarations.
https://lkml.org/lkml/2020/8/31/85

I'm not sure if they are used to suppress compiler warnings, through.

Denis

Patch
diff mbox series

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 149518d2a6a7..300b2659aab3 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -3901,6 +3901,17 @@  sub process {
 #ignore lines not being added
 		next if ($line =~ /^[^\+]/);
 
+# check for self assigments used to avoid compiler warnings
+# e.g.:	int foo = foo, *bar = NULL;
+#	struct foo bar = *(&(bar));
+		if ($line =~ /^\+\s*(?:$Declare)?([A-Za-z_][A-Za-z\d_]*)\s*=/) {
+			my $var = $1;
+			if ($line =~ /^\+\s*(?:$Declare)?$var\s*=\s*(?:$var|\*\s*\(?\s*&\s*\(?\s*$var\s*\)?\s*\)?)\s*[;,]/) {
+				WARN("SELF_ASSIGNMENT",
+				     "Do not use self-assignments to avoid compiler warnings\n" . $herecurr);
+			}
+		}
+
 # check for dereferences that span multiple lines
 		if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
 		    $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {