diff options
author | Joe Perches <joe@perches.com> | 2016-10-11 16:52:08 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2016-10-11 18:06:30 -0400 |
commit | f59b64bffe163431b4ec61f3b00c1aeb846948f3 (patch) | |
tree | c0eb4d7984ee48484270be672d8adbeb22097ab1 /scripts | |
parent | af207524a49c3d70af0a9196072ab3b6d690fcf7 (diff) |
checkpatch: add --strict test for macro argument reuse
If a macro argument is used multiple times in the macro definition, the
macro argument may have an unexpected side-effect.
Add a test (MACRO_ARG_REUSE) for that condition which is only
emitted with command-line option --strict.
Link: http://lkml.kernel.org/r/b6d67a87cafcafd15499e91780dc63b15dec0aa0.1473744906.git.joe@perches.com
Signed-off-by: Joe Perches <joe@perches.com>
Cc: Andy Whitcroft <apw@canonical.com>
Cc: Julia Lawall <julia.lawall@lip6.fr>
Cc: Dan Carpenter <dan.carpenter@oracle.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 | 43 |
1 files changed, 35 insertions, 8 deletions
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl index 3fa2b2e973eb..f135f8e4d069 100755 --- a/scripts/checkpatch.pl +++ b/scripts/checkpatch.pl | |||
@@ -4753,7 +4753,17 @@ sub process { | |||
4753 | $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/); | 4753 | $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/); |
4754 | $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/); | 4754 | $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/); |
4755 | 4755 | ||
4756 | $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//; | 4756 | $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//; |
4757 | my $define_args = $1; | ||
4758 | my $define_stmt = $dstat; | ||
4759 | my @def_args = (); | ||
4760 | |||
4761 | if (defined $define_args && $define_args ne "") { | ||
4762 | $define_args = substr($define_args, 1, length($define_args) - 2); | ||
4763 | $define_args =~ s/\s*//g; | ||
4764 | @def_args = split(",", $define_args); | ||
4765 | } | ||
4766 | |||
4757 | $dstat =~ s/$;//g; | 4767 | $dstat =~ s/$;//g; |
4758 | $dstat =~ s/\\\n.//g; | 4768 | $dstat =~ s/\\\n.//g; |
4759 | $dstat =~ s/^\s*//s; | 4769 | $dstat =~ s/^\s*//s; |
@@ -4789,6 +4799,15 @@ sub process { | |||
4789 | ^\[ | 4799 | ^\[ |
4790 | }x; | 4800 | }x; |
4791 | #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n"; | 4801 | #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n"; |
4802 | |||
4803 | $ctx =~ s/\n*$//; | ||
4804 | my $herectx = $here . "\n"; | ||
4805 | my $stmt_cnt = statement_rawlines($ctx); | ||
4806 | |||
4807 | for (my $n = 0; $n < $stmt_cnt; $n++) { | ||
4808 | $herectx .= raw_line($linenr, $n) . "\n"; | ||
4809 | } | ||
4810 | |||
4792 | if ($dstat ne '' && | 4811 | if ($dstat ne '' && |
4793 | $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(), | 4812 | $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(), |
4794 | $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo(); | 4813 | $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo(); |
@@ -4804,13 +4823,6 @@ sub process { | |||
4804 | $dstat !~ /^\(\{/ && # ({... | 4823 | $dstat !~ /^\(\{/ && # ({... |
4805 | $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/) | 4824 | $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/) |
4806 | { | 4825 | { |
4807 | $ctx =~ s/\n*$//; | ||
4808 | my $herectx = $here . "\n"; | ||
4809 | my $cnt = statement_rawlines($ctx); | ||
4810 | |||
4811 | for (my $n = 0; $n < $cnt; $n++) { | ||
4812 | $herectx .= raw_line($linenr, $n) . "\n"; | ||
4813 | } | ||
4814 | 4826 | ||
4815 | if ($dstat =~ /;/) { | 4827 | if ($dstat =~ /;/) { |
4816 | ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE", | 4828 | ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE", |
@@ -4819,6 +4831,21 @@ sub process { | |||
4819 | ERROR("COMPLEX_MACRO", | 4831 | ERROR("COMPLEX_MACRO", |
4820 | "Macros with complex values should be enclosed in parentheses\n" . "$herectx"); | 4832 | "Macros with complex values should be enclosed in parentheses\n" . "$herectx"); |
4821 | } | 4833 | } |
4834 | |||
4835 | } | ||
4836 | # check if any macro arguments are reused (ignore '...' and 'type') | ||
4837 | foreach my $arg (@def_args) { | ||
4838 | next if ($arg =~ /\.\.\./); | ||
4839 | next if ($arg =~ /^type$/); | ||
4840 | my $tmp = $define_stmt; | ||
4841 | $tmp =~ s/\b(typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g; | ||
4842 | $tmp =~ s/\#\s*$arg\b//g; | ||
4843 | $tmp =~ s/\b$arg\s*\#\#//g; | ||
4844 | my $use_cnt = $tmp =~ s/\b$arg\b//g; | ||
4845 | if ($use_cnt > 1) { | ||
4846 | CHK("MACRO_ARG_REUSE", | ||
4847 | "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx"); | ||
4848 | } | ||
4822 | } | 4849 | } |
4823 | 4850 | ||
4824 | # check for macros with flow control, but without ## concatenation | 4851 | # check for macros with flow control, but without ## concatenation |