Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1659695 > unrolled thread

[PATCH] checkpatch: Improve tests for multiple line function definitions

Started byJoe Perches <joe@perches.com>
First post2017-06-07 13:40 +0200
Last post2017-06-07 17:50 +0200
Articles 2 — 2 participants

Back to article view | Back to linux.kernel


Contents

  [PATCH] checkpatch: Improve tests for multiple line function definitions Joe Perches <joe@perches.com> - 2017-06-07 13:40 +0200
    RE: [PATCH] checkpatch: Improve tests for multiple line function  definitions "Kershner, David A" <David.Kershner@unisys.com> - 2017-06-07 17:50 +0200

#1659695 — [PATCH] checkpatch: Improve tests for multiple line function definitions

FromJoe Perches <joe@perches.com>
Date2017-06-07 13:40 +0200
Subject[PATCH] checkpatch: Improve tests for multiple line function definitions
Message-ID<tPHtD-61j-9@gated-at.bofh.it>
Add a block that identifies multiple line function definitions.

Save the function name into $context_function to improve the embedded
function name test.

Look for misplaced open brace on the function definition.
Emit an OPEN_BRACE error when the function definition is similar to

     void foo(int arg1,
              int arg2) {

Miscellanea:

o Remove the $realfile test in function declaration w/o named arguments test
o Comment the function declaration w/o named arguments test

Signed-off-by: Joe Perches <joe@perches.com>
---
 scripts/checkpatch.pl | 26 +++++++++++++++++++++++++-
 1 file changed, 25 insertions(+), 1 deletion(-)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 7fcaf5ca997b..3643d390eb1a 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -5897,7 +5897,8 @@ sub process {
 			     "externs should be avoided in .c files\n" .  $herecurr);
 		}
 
-		if ($realfile =~ /\.[ch]$/ && defined $stat &&
+# check for function declarations that have arguments without identifier names
+		if (defined $stat &&
 		    $stat =~ /^.\s*(?:extern\s+)?$Type\s*$Ident\s*\(\s*([^{]+)\s*\)\s*;/s &&
 		    $1 ne "void") {
 			my $args = trim($1);
@@ -5910,6 +5911,29 @@ sub process {
 			}
 		}
 
+# check for function definitions
+		if ($^V && $^V ge 5.10.0 &&
+		    defined $stat &&
+		    $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
+			$context_function = $1;
+
+# check for multiline function definition with misplaced open brace
+			my $ok = 0;
+			my $cnt = statement_rawlines($stat);
+			my $herectx = $here . "\n";
+			for (my $n = 0; $n < $cnt; $n++) {
+				my $rl = raw_line($linenr, $n);
+				$herectx .=  $rl . "\n";
+				$ok = 1 if ($rl =~ /^[ \+]\{/);
+				$ok = 1 if ($rl =~ /\{/ && $n == 0);
+				last if $rl =~ /^[ \+].*\{/;
+			}
+			if (!$ok) {
+				ERROR("OPEN_BRACE",
+				      "open brace '{' following function definitions go on the next line\n" . $herectx);
+			}
+		}
+
 # checks for new __setup's
 		if ($rawline =~ /\b__setup\("([^"]*)"/) {
 			my $name = $1;
-- 
2.10.0.rc2.1.g053435c

[toc] | [next] | [standalone]


#1659938 — RE: [PATCH] checkpatch: Improve tests for multiple line function definitions

From"Kershner, David A" <David.Kershner@unisys.com>
Date2017-06-07 17:50 +0200
SubjectRE: [PATCH] checkpatch: Improve tests for multiple line function definitions
Message-ID<tPLnz-8w9-3@gated-at.bofh.it>
In reply to#1659695

> -----Original Message-----
> From: Joe Perches [mailto:joe@perches.com]
> Sent: Wednesday, June 7, 2017 7:36 AM
> To: Andrew Morton <akpm@linux-foundation.org>; Andy Whitcroft
> <apw@canonical.com>
> Cc: Greg KH <gregkh@linuxfoundation.org>; Kershner, David A
> <David.Kershner@unisys.com>; linux-kernel@vger.kernel.org
> Subject: [PATCH] checkpatch: Improve tests for multiple line function
> definitions
> 
> Add a block that identifies multiple line function definitions.
> 
> Save the function name into $context_function to improve the embedded
> function name test.
> 
> Look for misplaced open brace on the function definition.
> Emit an OPEN_BRACE error when the function definition is similar to
> 
>      void foo(int arg1,
>               int arg2) {
> 
> Miscellanea:
> 
> o Remove the $realfile test in function declaration w/o named arguments
> test
> o Comment the function declaration w/o named arguments test
> 
> Signed-off-by: Joe Perches <joe@perches.com>

Thanks for the quick turnaround! I tested it on our drivers and it showed the
errors.

Tested-by: David Kershner <david.kershner@unisys.com>

David Kershner


> ---
>  scripts/checkpatch.pl | 26 +++++++++++++++++++++++++-
>  1 file changed, 25 insertions(+), 1 deletion(-)
> 
> diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
> index 7fcaf5ca997b..3643d390eb1a 100755
> --- a/scripts/checkpatch.pl
> +++ b/scripts/checkpatch.pl
> @@ -5897,7 +5897,8 @@ sub process {
>  			     "externs should be avoided in .c files\n" .
> $herecurr);
>  		}
> 
> -		if ($realfile =~ /\.[ch]$/ && defined $stat &&
> +# check for function declarations that have arguments without identifier
> names
> +		if (defined $stat &&
>  		    $stat =~
> /^.\s*(?:extern\s+)?$Type\s*$Ident\s*\(\s*([^{]+)\s*\)\s*;/s &&
>  		    $1 ne "void") {
>  			my $args = trim($1);
> @@ -5910,6 +5911,29 @@ sub process {
>  			}
>  		}
> 
> +# check for function definitions
> +		if ($^V && $^V ge 5.10.0 &&
> +		    defined $stat &&
> +		    $stat =~
> /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
> +			$context_function = $1;
> +
> +# check for multiline function definition with misplaced open brace
> +			my $ok = 0;
> +			my $cnt = statement_rawlines($stat);
> +			my $herectx = $here . "\n";
> +			for (my $n = 0; $n < $cnt; $n++) {
> +				my $rl = raw_line($linenr, $n);
> +				$herectx .=  $rl . "\n";
> +				$ok = 1 if ($rl =~ /^[ \+]\{/);
> +				$ok = 1 if ($rl =~ /\{/ && $n == 0);
> +				last if $rl =~ /^[ \+].*\{/;
> +			}
> +			if (!$ok) {
> +				ERROR("OPEN_BRACE",
> +				      "open brace '{' following function
> definitions go on the next line\n" . $herectx);
> +			}
> +		}
> +
>  # checks for new __setup's
>  		if ($rawline =~ /\b__setup\("([^"]*)"/) {
>  			my $name = $1;
> --
> 2.10.0.rc2.1.g053435c

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web