2015-02-19 05:52:49

by Marek Lindner

[permalink] [raw]
Subject: checkpatch: CHECK: No space is necessary after a cast


Hi Joe,

we have come across a checkpatch false-positive:

>>>>
static void batadv_recv_handler_init(void)
{
int i;

for (i = 0; i < ARRAY_SIZE(batadv_rx_handler); i++)
batadv_rx_handler[i] = batadv_recv_unhandled_packet;

for (i = BATADV_UNICAST_MIN; i <= BATADV_UNICAST_MAX; i++)
batadv_rx_handler[i] = batadv_recv_unhandled_unicast_packet;

/* compile time checks for sizes */
BUILD_BUG_ON(sizeof(struct batadv_bla_claim_dst) != 6);
BUILD_BUG_ON(sizeof(struct batadv_ogm_packet) != 24);
BUILD_BUG_ON(sizeof(struct batadv_icmp_header) != 20);
..
}
<<<<<

The following warnings are triggered:

CHECK: No space is necessary after a cast
#440: FILE: main.c:440:
+ BUILD_BUG_ON(sizeof(struct batadv_bla_claim_dst) != 6);

CHECK: No space is necessary after a cast
#441: FILE: main.c:441:
+ BUILD_BUG_ON(sizeof(struct batadv_ogm_packet) != 24);

CHECK: No space is necessary after a cast
#442: FILE: main.c:442:
+ BUILD_BUG_ON(sizeof(struct batadv_icmp_header) != 20);

Can you make a suggestion / patch for checkpatch to better handle this case ?

Thanks,
Marek


Attachments:
signature.asc (473.00 B)
This is a digitally signed message part.

2015-02-19 19:55:47

by Joe Perches

[permalink] [raw]
Subject: Re: checkpatch: CHECK: No space is necessary after a cast

On Thu, 2015-02-19 at 13:35 +0800, Marek Lindner wrote:
> Hi Joe,

Hi Marek

> we have come across a checkpatch false-positive:
[]
> BUILD_BUG_ON(sizeof(struct batadv_bla_claim_dst) != 6);
> CHECK: No space is necessary after a cast
> #440: FILE: main.c:440:
> + BUILD_BUG_ON(sizeof(struct batadv_bla_claim_dst) != 6);
[]
> Can you make a suggestion / patch for checkpatch to better handle this case ?

The "sizeof" test in the current script doesn't work.

I believe the patch below works with no false positives
but <shrug> it's perl regexes against odd coding styles
and weird macros, who knows for sure...

I did run it against drivers/, net/ and include/

Give this a try:
---
scripts/checkpatch.pl | 12 +++++++++---
1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index d124359..2f5bb27 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -2552,9 +2552,15 @@ sub process {
}
}

- if ($line =~ /^\+.*(\w+\s*)?\(\s*$Type\s*\)[ \t]+(?!$Assignment|$Arithmetic|[,;:\?\(\{\}\[\<\>]|&&|\|\||\\$)/ &&
- (!defined($1) || $1 !~ /sizeof\s*/)) {
- if (CHK("SPACING",
+# check for space after cast like "(int) foo" or "(struct foo) bar"
+# avoid checking a few false positives:
+# "sizeof(<type>)" or "__alignof__(<type>)"
+# function pointer declarations like "(*foo)(int) = bar;"
+# structure definitions like "(struct foo) { 0 };"
+# multiline macros that define functions
+ if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$))/ &&
+ (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
+ if (CHK("SPACING",
"No space is necessary after a cast\n" . $herecurr) &&
$fix) {
$fixed[$fixlinenr] =~

2015-02-20 11:56:24

by Marek Lindner

[permalink] [raw]
Subject: Re: [B.A.T.M.A.N.] checkpatch: CHECK: No space is necessary after a cast

On Thursday, February 19, 2015 11:55:41 Joe Perches wrote:
> > Can you make a suggestion / patch for checkpatch to better handle this
> > case ?
> The "sizeof" test in the current script doesn't work.
>
> I believe the patch below works with no false positives
> but <shrug> it's perl regexes against odd coding styles
> and weird macros, who knows for sure...
>
> I did run it against drivers/, net/ and include/

Your perl-regex-fu does the trick!

Thanks,
Marek


Attachments:
signature.asc (473.00 B)
This is a digitally signed message part.

2015-02-20 19:52:40

by Joe Perches

[permalink] [raw]
Subject: [PATCH] checkpatch: Improve "no space is necessary after a cast" test

The "no space is necessary after a cast" sizeof exclusion
doesn't work properly.

The test reports a false positive for code like:

BUILD_BUG_ON(sizeof(struct batadv_bla_claim_dst) != 6);

Make it work, simplify the exclusions, and add some comments.

Signed-off-by: Joe Perches <[email protected]>
Reported-by: Marek Lindner <[email protected]>
---
scripts/checkpatch.pl | 13 ++++++++++---
1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index d124359..2898e49 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -2552,9 +2552,16 @@ sub process {
}
}

- if ($line =~ /^\+.*(\w+\s*)?\(\s*$Type\s*\)[ \t]+(?!$Assignment|$Arithmetic|[,;:\?\(\{\}\[\<\>]|&&|\|\||\\$)/ &&
- (!defined($1) || $1 !~ /sizeof\s*/)) {
- if (CHK("SPACING",
+# check for space after cast like "(int) foo" or "(struct foo) bar"
+# avoid checking a few false positives:
+# "sizeof(<type>)" or "__alignof__(<type>)"
+# function pointer declarations like "(*foo)(int) = bar;"
+# structure definitions like "(struct foo) { 0 };"
+# multiline macros that define functions
+# known attributes or the __attribute__ keyword
+ if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
+ (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
+ if (CHK("SPACING",
"No space is necessary after a cast\n" . $herecurr) &&
$fix) {
$fixed[$fixlinenr] =~