From: Joe Perches <joe@perches.com>
To: Andrew Morton <akpm@linux-foundation.org>,
Andy Whitcroft <apw@canonical.com>
Cc: Julia Lawall <julia.lawall@lip6.fr>,
Dan Carpenter <error27@gmail.com>,
linux-kernel@vger.kernel.org
Subject: [PATCH] checkpatch: Add a --strict test for macro argument reuse and precedence
Date: Sat, 3 Sep 2016 15:20:42 -0700 [thread overview]
Message-ID: <5ef3db05b4021f41d313e265f4c9ebd259c808ee.1472940911.git.joe@perches.com> (raw)
In-Reply-To: <20160903201847.GA4218@mwanda>
Add a test for reuse of macro arguments to highlight any possible
side-effects from this reuse.
Avoid this check on token name pasting and when the
argument is used in a typeof or a __builtin.
Add a test for macro arguents that have leading or trailing operators
where the argument isn't parenthesized to avoid possible precedence
issues.
These tests are noisy so make them --strict.
Signed-off-by: Joe Perches <joe@perches.com>
---
This is a slight expansion of a patch I sent a few years back.
https://lkml.org/lkml/2012/11/6/151
This one at least runs.
Maybe it's useful to find some of the possible macro misuses,
but it is _very_ noisy.
scripts/checkpatch.pl | 47 +++++++++++++++++++++++++++++++++++++++--------
1 file changed, 39 insertions(+), 8 deletions(-)
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 8946904..921155f 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -4714,7 +4714,17 @@ sub process {
$has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
$has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
- $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
+ $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
+ my $define_args = $1;
+ my $define_stmt = $dstat;
+ my @def_args = ();
+
+ if (defined $define_args && $define_args ne "") {
+ $define_args = substr($define_args, 1, length($define_args) - 2);
+ $define_args =~ s/\s*//g;
+ @def_args = split(",", $define_args);
+ }
+
$dstat =~ s/$;//g;
$dstat =~ s/\\\n.//g;
$dstat =~ s/^\s*//s;
@@ -4750,6 +4760,15 @@ sub process {
^\[
}x;
#print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
+
+ $ctx =~ s/\n*$//;
+ my $herectx = $here . "\n";
+ my $stmt_cnt = statement_rawlines($ctx);
+
+ for (my $n = 0; $n < $stmt_cnt; $n++) {
+ $herectx .= raw_line($linenr, $n) . "\n";
+ }
+
if ($dstat ne '' &&
$dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
$dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
@@ -4765,13 +4784,6 @@ sub process {
$dstat !~ /^\(\{/ && # ({...
$ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
{
- $ctx =~ s/\n*$//;
- my $herectx = $here . "\n";
- my $cnt = statement_rawlines($ctx);
-
- for (my $n = 0; $n < $cnt; $n++) {
- $herectx .= raw_line($linenr, $n) . "\n";
- }
if ($dstat =~ /;/) {
ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
@@ -4780,6 +4792,25 @@ sub process {
ERROR("COMPLEX_MACRO",
"Macros with complex values should be enclosed in parentheses\n" . "$herectx");
}
+
+ }
+# check if any macro arguments are reused or may have other precedence issues
+ foreach my $arg (@def_args) {
+ next if ($arg =~ /\.\.\./);
+ my $tmp = $define_stmt;
+ $tmp =~ s/\b(typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g;
+ $tmp =~ s/\#\#\s*$arg\b//g;
+ $tmp =~ s/\b$arg\s*\#\#//g;
+ my $use_cnt = $tmp =~ s/\b$arg\b//g;
+ if ($use_cnt > 1) {
+ CHK("MACRO_ARG_REUSE",
+ "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
+ }
+ if ($define_stmt =~ m/\b$arg\b\s*$Operators/m ||
+ $define_stmt =~ /$Operators\s*$arg\b/m) {
+ CHK("MACRO_ARG_PRECEDENCE",
+ "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
+ }
}
# check for macros with flow control, but without ## concatenation
--
2.10.0.rc2.1.g053435c
next prev parent reply other threads:[~2016-09-03 22:21 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-09-03 18:35 Possible code defects: macros " Joe Perches
2016-09-03 20:18 ` Dan Carpenter
2016-09-03 22:20 ` Joe Perches [this message]
2016-09-04 14:42 ` [PATCH] checkpatch: Add a --strict test for macro argument reuse " Joe Perches
2016-09-04 10:10 ` Possible code defects: macros " Julia Lawall
2016-09-04 15:06 ` Joe Perches
[not found] ` <alpine.DEB.2.10.1609172221110.3124@hadrien>
2016-09-17 21:27 ` Joe Perches
2016-09-18 5:09 ` Julia Lawall
2016-09-18 9:31 ` Joe Perches
2016-09-20 13:14 ` Julia Lawall
2016-09-20 17:07 ` Joe Perches
2016-09-20 18:03 ` Joe Perches
2016-09-20 23:47 ` Joe Perches
2016-09-21 5:04 ` Julia Lawall
2016-09-17 21:57 ` Joe Perches
2016-09-18 4:57 ` Julia Lawall
2016-09-18 9:27 ` Joe Perches
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=5ef3db05b4021f41d313e265f4c9ebd259c808ee.1472940911.git.joe@perches.com \
--to=joe@perches.com \
--cc=akpm@linux-foundation.org \
--cc=apw@canonical.com \
--cc=error27@gmail.com \
--cc=julia.lawall@lip6.fr \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®