From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1750906AbcDKC02 (ORCPT ); Sun, 10 Apr 2016 22:26:28 -0400 Received: from smtprelay0158.hostedemail.com ([216.40.44.158]:56650 "EHLO smtprelay.hostedemail.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1750751AbcDKC01 (ORCPT ); Sun, 10 Apr 2016 22:26:27 -0400 X-Session-Marker: 6A6F6540706572636865732E636F6D X-Spam-Summary: 2,0,0,,d41d8cd98f00b204,joe@perches.com,:::::::::::::,RULES_HIT:41:355:379:541:800:960:973:982:988:989:1260:1277:1311:1313:1314:1345:1359:1373:1437:1515:1516:1518:1534:1542:1593:1594:1711:1730:1747:1777:1792:2198:2199:2393:2559:2562:2828:2898:3138:3139:3140:3141:3142:3354:3653:3865:3868:3871:3874:4321:5007:7903:8957:10004:10400:10848:11658:11783:11914:12043:12346:12517:12519:12555:12663:13161:13221:13229:13439:13894:14181:14394:14659:14721:21080:30054:30070,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: face62_550d6f14ac35d X-Filterd-Recvd-Size: 3933 Message-ID: <1460341582.1800.66.camel@perches.com> Subject: [PATCH] checkpatch: Improve missing break for switch/case tests From: Joe Perches To: Andrew Morton , Andy Whitcroft Cc: "alsa-devel@alsa-project.org" , Lars-Peter Clausen , Takashi Iwai , "Harvey, Ryan" , LKML Date: Sun, 10 Apr 2016 19:26:22 -0700 In-Reply-To: <570928CC.6030404@metafoo.de> References: <570928CC.6030404@metafoo.de> Content-Type: text/plain; charset="ISO-8859-1" X-Mailer: Evolution 3.18.5.2-0ubuntu1 Mime-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The current switch/case test doesn't handle ... case labels like: switch (foo) { case bar ... baz: etc... } Add a specific regex for that form and the default label. Use the regex where a case label is tested. Improve the missing break/fall-through test by only reporting the first case label missing the break not every case label where there isn't a preceding break/fall-through. Show the line above the case label when reporting the possible defect. Signed-off-by: Joe Perches Reported-by: Lars-Peter Clausen ---  scripts/checkpatch.pl | 18 ++++++++++++------  1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl index e3d9c34..216e4a1 100755 --- a/scripts/checkpatch.pl +++ b/scripts/checkpatch.pl @@ -331,6 +331,11 @@ our $Operators = qr{     }x;    our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x; +our $case_label = qr{ + (?:case\s+(?:$Ident|$Constant) +     (?:\s*\.\.\.\s*\s*(?:$Ident|$Constant))? | +  default)\s*: +   }x;    our $BasicType;  our $NonptrType; @@ -4417,7 +4422,7 @@ sub process {   $herecurr);   }  # case and default should not have general statements after them - if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g && + if ($line =~ /^.\s*$case_label/g &&       $line !~ /\G(?:   (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|   \s*return\s+ @@ -5648,27 +5653,28 @@ sub process {   }    # check for case / default statements not preceded by break/fallthrough/switch - if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) { + if ($line =~ /^.\s*$case_label/ && +     $prevline !~ /^.\s*$case_label/) {   my $has_break = 0;   my $has_statement = 0;   my $count = 0;   my $prevline = $linenr; - while ($prevline > 1 && ($file || $count < 3) && !$has_break) { + while ($prevline > 1 && ($file || $count < 3) && !$has_break && !$has_statement) {   $prevline--;   my $rline = $rawlines[$prevline - 1];   my $fline = $lines[$prevline - 1];   last if ($fline =~ /^\@\@/);   next if ($fline =~ /^\-/); - next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/); + next if ($fline =~ /^.(?:\s*${case_label}[\s$;]*)*$/);   $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);   next if ($fline =~ /^.[\s$;]*$/); - $has_statement = 1;   $count++;   $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/); + $has_statement = 1;   }   if (!$has_break && $has_statement) {   WARN("MISSING_BREAK", -      "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr); +      "Possible switch case/default not preceeded by break or fallthrough comment\n" . $hereprev);   }   }