From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754087AbcICWVW (ORCPT ); Sat, 3 Sep 2016 18:21:22 -0400 Received: from smtprelay0071.hostedemail.com ([216.40.44.71]:45419 "EHLO smtprelay.hostedemail.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1753565AbcICWVU (ORCPT ); Sat, 3 Sep 2016 18:21:20 -0400 X-Session-Marker: 6A6F6540706572636865732E636F6D X-Spam-Summary: 50,0,0,,d41d8cd98f00b204,joe@perches.com,:::::::::,RULES_HIT:41:355:379:541:800:960:967:973:982:988:989:1260:1345:1359:1437:1534:1543:1711:1730:1747:1777:1792:2197:2199:2393:2525:2553:2560:2563:2682:2685:2859:2933:2937:2939:2942:2945:2947:2951:2954:3022:3138:3139:3140:3141:3142:3353:3653:3865:3866:3867:3868:3870:3871:3872:3874:3934:3936:3938:3941:3944:3947:3950:3953:3956:3959:4321:4605:5007:6119:6261:6691:7875:7903:8985:9025:9592:10004:10848:11026:11232:11473:11658:11914:12043:12291:12438:12555:12683:13161:13229:14181:14394:14721:21080:21221:21324:21451:30029:30054:30070:30090,0,RBL:none,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fn,MSBL:0,DNSBL:none,Custom_rules:0:0:0,LFtime:2,LUA_SUMMARY:none X-HE-Tag: uncle76_2388a21de8928 X-Filterd-Recvd-Size: 4453 From: Joe Perches To: Andrew Morton , Andy Whitcroft Cc: Julia Lawall , Dan Carpenter , 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 Message-Id: <5ef3db05b4021f41d313e265f4c9ebd259c808ee.1472940911.git.joe@perches.com> X-Mailer: git-send-email 2.10.0.rc2.1.g053435c In-Reply-To: <20160903201847.GA4218@mwanda> References: <20160903201847.GA4218@mwanda> Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- 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