From patchwork Mon Apr 11 02:26:22 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Joe Perches X-Patchwork-Id: 8795991 Return-Path: X-Original-To: patchwork-alsa-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id BA7149F39A for ; Mon, 11 Apr 2016 02:26:46 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id D933220256 for ; Mon, 11 Apr 2016 02:26:45 +0000 (UTC) Received: from alsa0.perex.cz (alsa0.perex.cz [77.48.224.243]) by mail.kernel.org (Postfix) with ESMTP id 9363820254 for ; Mon, 11 Apr 2016 02:26:44 +0000 (UTC) Received: by alsa0.perex.cz (Postfix, from userid 1000) id 427192619D7; Mon, 11 Apr 2016 04:26:43 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,NO_DNS_FOR_FROM, RCVD_IN_DNSWL_NONE,UNPARSEABLE_RELAY autolearn=no version=3.3.1 Received: from alsa0.perex.cz (localhost [127.0.0.1]) by alsa0.perex.cz (Postfix) with ESMTP id C68562614A9; Mon, 11 Apr 2016 04:26:35 +0200 (CEST) X-Original-To: alsa-devel@alsa-project.org Delivered-To: alsa-devel@alsa-project.org Received: by alsa0.perex.cz (Postfix, from userid 1000) id C330C26153A; Mon, 11 Apr 2016 04:26:33 +0200 (CEST) Received: from smtprelay.hostedemail.com (smtprelay0239.hostedemail.com [216.40.44.239]) by alsa0.perex.cz (Postfix) with ESMTP id 45713261469 for ; Mon, 11 Apr 2016 04:26:27 +0200 (CEST) Received: from filter.hostedemail.com (unknown [216.40.38.60]) by smtprelay08.hostedemail.com (Postfix) with ESMTP id EE55429DD78; Mon, 11 Apr 2016 02:26:25 +0000 (UTC) X-Session-Marker: 6A6F6540706572636865732E636F6D X-HE-Tag: face62_550d6f14ac35d X-Filterd-Recvd-Size: 3933 Received: from XPS-9350 (14.sub-70-197-74.myvzw.com [70.197.74.14]) (Authenticated sender: joe@perches.com) by omf10.hostedemail.com (Postfix) with ESMTPA; Mon, 11 Apr 2016 02:26:24 +0000 (UTC) Message-ID: <1460341582.1800.66.camel@perches.com> From: Joe Perches To: Andrew Morton , Andy Whitcroft Date: Sun, 10 Apr 2016 19:26:22 -0700 In-Reply-To: <570928CC.6030404@metafoo.de> References: <570928CC.6030404@metafoo.de> X-Mailer: Evolution 3.18.5.2-0ubuntu1 Mime-Version: 1.0 Cc: Takashi Iwai , "alsa-devel@alsa-project.org" , Lars-Peter Clausen , LKML , "Harvey, Ryan" Subject: [alsa-devel] [PATCH] checkpatch: Improve missing break for switch/case tests X-BeenThere: alsa-devel@alsa-project.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Alsa-devel mailing list for ALSA developers - http://www.alsa-project.org" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: alsa-devel-bounces@alsa-project.org Sender: alsa-devel-bounces@alsa-project.org X-Virus-Scanned: ClamAV using ClamSMTP 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);   }   }