diff options
| author | Joe Perches <joe@perches.com> | 2014-01-23 18:54:49 -0500 |
|---|---|---|
| committer | Linus Torvalds <torvalds@linux-foundation.org> | 2014-01-23 19:36:58 -0500 |
| commit | 31070b5d4490c6c876e0d3b093e5d5b05e4027fa (patch) | |
| tree | 7d6a71fcb471bdc4f82e6fc101d8043e966f1d15 /scripts | |
| parent | 3e2232f2d03ffa531e31662c447496ec2552d85b (diff) | |
checkpatch: add tests for function pointer style misuses
Kernel style uses function pointers in this form:
"type (*funcptr)(args...)"
Emit warnings when this function pointer form isn't used.
Signed-off-by: Joe Perches <joe@perches.com>
Cc: Andy Whitcroft <apw@shadowen.org>
Cc: Derek Perrin <d.roc16@gmail.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'scripts')
| -rwxr-xr-x | scripts/checkpatch.pl | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl index 82fd120df0c2..19e8e862c1b9 100755 --- a/scripts/checkpatch.pl +++ b/scripts/checkpatch.pl | |||
| @@ -2811,6 +2811,65 @@ sub process { | |||
| 2811 | } | 2811 | } |
| 2812 | } | 2812 | } |
| 2813 | 2813 | ||
| 2814 | # Function pointer declarations | ||
| 2815 | # check spacing between type, funcptr, and args | ||
| 2816 | # canonical declaration is "type (*funcptr)(args...)" | ||
| 2817 | # | ||
| 2818 | # the $Declare variable will capture all spaces after the type | ||
| 2819 | # so check it for trailing missing spaces or multiple spaces | ||
| 2820 | if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)$Ident(\s*)\)(\s*)\(/) { | ||
| 2821 | my $declare = $1; | ||
| 2822 | my $pre_pointer_space = $2; | ||
| 2823 | my $post_pointer_space = $3; | ||
| 2824 | my $funcname = $4; | ||
| 2825 | my $post_funcname_space = $5; | ||
| 2826 | my $pre_args_space = $6; | ||
| 2827 | |||
| 2828 | if ($declare !~ /\s$/) { | ||
| 2829 | WARN("SPACING", | ||
| 2830 | "missing space after return type\n" . $herecurr); | ||
| 2831 | } | ||
| 2832 | |||
| 2833 | # unnecessary space "type (*funcptr)(args...)" | ||
| 2834 | elsif ($declare =~ /\s{2,}$/) { | ||
| 2835 | WARN("SPACING", | ||
| 2836 | "Multiple spaces after return type\n" . $herecurr); | ||
| 2837 | } | ||
| 2838 | |||
| 2839 | # unnecessary space "type ( *funcptr)(args...)" | ||
| 2840 | if (defined $pre_pointer_space && | ||
| 2841 | $pre_pointer_space =~ /^\s/) { | ||
| 2842 | WARN("SPACING", | ||
| 2843 | "Unnecessary space after function pointer open parenthesis\n" . $herecurr); | ||
| 2844 | } | ||
| 2845 | |||
| 2846 | # unnecessary space "type (* funcptr)(args...)" | ||
| 2847 | if (defined $post_pointer_space && | ||
| 2848 | $post_pointer_space =~ /^\s/) { | ||
| 2849 | WARN("SPACING", | ||
| 2850 | "Unnecessary space before function pointer name\n" . $herecurr); | ||
| 2851 | } | ||
| 2852 | |||
| 2853 | # unnecessary space "type (*funcptr )(args...)" | ||
| 2854 | if (defined $post_funcname_space && | ||
| 2855 | $post_funcname_space =~ /^\s/) { | ||
| 2856 | WARN("SPACING", | ||
| 2857 | "Unnecessary space after function pointer name\n" . $herecurr); | ||
| 2858 | } | ||
| 2859 | |||
| 2860 | # unnecessary space "type (*funcptr) (args...)" | ||
| 2861 | if (defined $pre_args_space && | ||
| 2862 | $pre_args_space =~ /^\s/) { | ||
| 2863 | WARN("SPACING", | ||
| 2864 | "Unnecessary space before function pointer arguments\n" . $herecurr); | ||
| 2865 | } | ||
| 2866 | |||
| 2867 | if (show_type("SPACING") && $fix) { | ||
| 2868 | $fixed[$linenr - 1] =~ | ||
| 2869 | s/^(.\s*$Declare)\(\s*\*\s*($Ident)\s*\)\s*\(/rtrim($1) . " " . "\(\*$2\)\("/ex; | ||
| 2870 | } | ||
| 2871 | } | ||
| 2872 | |||
| 2814 | # check for spacing round square brackets; allowed: | 2873 | # check for spacing round square brackets; allowed: |
| 2815 | # 1. with a type on the left -- int [] a; | 2874 | # 1. with a type on the left -- int [] a; |
| 2816 | # 2. at the beginning of a line for slice initialisers -- [0...10] = 5, | 2875 | # 2. at the beginning of a line for slice initialisers -- [0...10] = 5, |
