public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* Re: checkpatch: CHECK: No space is necessary after a cast
       [not found] <874mvq1w9v.fsf@kamboji.qca.qualcomm.com>
@ 2014-09-29 18:02 ` Joe Perches
  2014-09-30  5:35   ` Kalle Valo
  0 siblings, 1 reply; 6+ messages in thread
From: Joe Perches @ 2014-09-29 18:02 UTC (permalink / raw)
  To: Kalle Valo; +Cc: ath10k, Andrew Morton, LKML, Dan Carpenter

On Mon, 2014-09-29 at 14:49 +0300, Kalle Valo wrote:
> Hi Joe,
> 
> I have a problem with checkpatch. On ath10k we have this function:
> 
> static inline struct ath10k_skb_cb *ATH10K_SKB_CB(struct sk_buff *skb)
> {
> 	BUILD_BUG_ON(sizeof(struct ath10k_skb_cb) >
> 		     IEEE80211_TX_INFO_DRIVER_DATA_SIZE);
> 	return (struct ath10k_skb_cb *)&IEEE80211_SKB_CB(skb)->driver_data;
> }
> 
> And the BUILD_BUG_ON triggers this warning:
> 
> drivers/net/wireless/ath/ath10k/core.h:85: CHECK: No space is necessary after a cast
> 
> Any advice how to handle that?

It's a checkpatch false positive that could be fixed.

It needs something like another test to look for
sizeof(type) as not a cast and more arithmetic/comparison
uses.

Maybe you can test this?
---
 scripts/checkpatch.pl | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 52a223e..6132329 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -2515,7 +2515,8 @@ sub process {
 			}
 		}
 
-		if ($line =~ /^\+.*\(\s*$Type\s*\)[ \t]+(?!$Assignment|$Arithmetic|{)/) {
+		if ($line =~ /^\+.*(\w+\s*)?\(\s*$Type\s*\)[ \t]+(?!$Assignment|$Arithmetic|[,;\({\[\<\>])/ &&
+		    (!defined($1) || $1 !~ /sizeof\s*/)) {
 			if (CHK("SPACING",
 				"No space is necessary after a cast\n" . $herecurr) &&
 			    $fix) {



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: checkpatch: CHECK: No space is necessary after a cast
  2014-09-29 18:02 ` checkpatch: CHECK: No space is necessary after a cast Joe Perches
@ 2014-09-30  5:35   ` Kalle Valo
  0 siblings, 0 replies; 6+ messages in thread
From: Kalle Valo @ 2014-09-30  5:35 UTC (permalink / raw)
  To: Joe Perches; +Cc: ath10k, Andrew Morton, LKML, Dan Carpenter

Joe Perches <joe@perches.com> writes:

> On Mon, 2014-09-29 at 14:49 +0300, Kalle Valo wrote:
>> Hi Joe,
>> 
>> I have a problem with checkpatch. On ath10k we have this function:
>> 
>> static inline struct ath10k_skb_cb *ATH10K_SKB_CB(struct sk_buff *skb)
>> {
>> 	BUILD_BUG_ON(sizeof(struct ath10k_skb_cb) >
>> 		     IEEE80211_TX_INFO_DRIVER_DATA_SIZE);
>> 	return (struct ath10k_skb_cb *)&IEEE80211_SKB_CB(skb)->driver_data;
>> }
>> 
>> And the BUILD_BUG_ON triggers this warning:
>> 
>> drivers/net/wireless/ath/ath10k/core.h:85: CHECK: No space is necessary after a cast
>> 
>> Any advice how to handle that?
>
> It's a checkpatch false positive that could be fixed.
>
> It needs something like another test to look for
> sizeof(type) as not a cast and more arithmetic/comparison
> uses.
>
> Maybe you can test this?

It works, thank you!

Tested-by: Kalle Valo <kvalo@qca.qualcomm.com>

-- 
Kalle Valo

^ permalink raw reply	[flat|nested] 6+ messages in thread

* checkpatch: CHECK: No space is necessary after a cast
@ 2015-02-19  5:35 Marek Lindner
  2015-02-19 19:55 ` Joe Perches
  0 siblings, 1 reply; 6+ messages in thread
From: Marek Lindner @ 2015-02-19  5:35 UTC (permalink / raw)
  To: Joe Perches, b.a.t.m.a.n, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 1099 bytes --]


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

[-- Attachment #2: This is a digitally signed message part. --]
[-- Type: application/pgp-signature, Size: 473 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: checkpatch: CHECK: No space is necessary after a cast
  2015-02-19  5:35 checkpatch: CHECK: No space is necessary after a cast Marek Lindner
@ 2015-02-19 19:55 ` Joe Perches
  2015-02-20 11:56   ` [B.A.T.M.A.N.] " Marek Lindner
  0 siblings, 1 reply; 6+ messages in thread
From: Joe Perches @ 2015-02-19 19:55 UTC (permalink / raw)
  To: Marek Lindner; +Cc: b.a.t.m.a.n, linux-kernel, Dan Carpenter

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] =~



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [B.A.T.M.A.N.] checkpatch: CHECK: No space is necessary after a cast
  2015-02-19 19:55 ` Joe Perches
@ 2015-02-20 11:56   ` Marek Lindner
  2015-02-20 19:52     ` [PATCH] checkpatch: Improve "no space is necessary after a cast" test Joe Perches
  0 siblings, 1 reply; 6+ messages in thread
From: Marek Lindner @ 2015-02-20 11:56 UTC (permalink / raw)
  To: b.a.t.m.a.n; +Cc: Joe Perches, linux-kernel, Dan Carpenter

[-- Attachment #1: Type: text/plain, Size: 467 bytes --]

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

[-- Attachment #2: This is a digitally signed message part. --]
[-- Type: application/pgp-signature, Size: 473 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH] checkpatch: Improve "no space is necessary after a cast" test
  2015-02-20 11:56   ` [B.A.T.M.A.N.] " Marek Lindner
@ 2015-02-20 19:52     ` Joe Perches
  0 siblings, 0 replies; 6+ messages in thread
From: Joe Perches @ 2015-02-20 19:52 UTC (permalink / raw)
  To: Andrew Morton; +Cc: b.a.t.m.a.n, linux-kernel, Dan Carpenter, Marek Lindner

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 <joe@perches.com>
Reported-by: Marek Lindner <mareklindner@neomailbox.ch>
---
 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] =~



^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2015-02-20 19:52 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-02-19  5:35 checkpatch: CHECK: No space is necessary after a cast Marek Lindner
2015-02-19 19:55 ` Joe Perches
2015-02-20 11:56   ` [B.A.T.M.A.N.] " Marek Lindner
2015-02-20 19:52     ` [PATCH] checkpatch: Improve "no space is necessary after a cast" test Joe Perches
     [not found] <874mvq1w9v.fsf@kamboji.qca.qualcomm.com>
2014-09-29 18:02 ` checkpatch: CHECK: No space is necessary after a cast Joe Perches
2014-09-30  5:35   ` Kalle Valo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox