All of lore.kernel.org
 help / color / mirror / Atom feed
From: Joe Perches <joe@perches.com>
To: Andrew Morton <akpm@linux-foundation.org>,
	Andy Whitcroft <apw@shadowen.org>
Cc: Takashi Iwai <tiwai@suse.de>,
	"alsa-devel@alsa-project.org" <alsa-devel@alsa-project.org>,
	Lars-Peter Clausen <lars@metafoo.de>,
	LKML <linux-kernel@vger.kernel.org>,
	"Harvey, Ryan" <Ryan.Harvey@cirrus.com>
Subject: [PATCH] checkpatch: Improve missing break for switch/case tests
Date: Sun, 10 Apr 2016 19:26:22 -0700	[thread overview]
Message-ID: <1460341582.1800.66.camel@perches.com> (raw)
In-Reply-To: <570928CC.6030404@metafoo.de>

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 <joe@perches.com>
Reported-by: Lars-Peter Clausen <lars@metafoo.de>
---
 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);
 			}
 		}
 

WARNING: multiple messages have this Message-ID (diff)
From: Joe Perches <joe@perches.com>
To: Andrew Morton <akpm@linux-foundation.org>,
	Andy Whitcroft <apw@shadowen.org>
Cc: "alsa-devel@alsa-project.org" <alsa-devel@alsa-project.org>,
	Lars-Peter Clausen <lars@metafoo.de>,
	Takashi Iwai <tiwai@suse.de>,
	"Harvey, Ryan" <Ryan.Harvey@cirrus.com>,
	LKML <linux-kernel@vger.kernel.org>
Subject: [PATCH] checkpatch: Improve missing break for switch/case tests
Date: Sun, 10 Apr 2016 19:26:22 -0700	[thread overview]
Message-ID: <1460341582.1800.66.camel@perches.com> (raw)
In-Reply-To: <570928CC.6030404@metafoo.de>

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 <joe@perches.com>
Reported-by: Lars-Peter Clausen <lars@metafoo.de>
---
 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);
 			}
 		}
 

  parent reply	other threads:[~2016-04-11  2:26 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-04-07 22:23 Checkpatch warnings on switch case fallthru Harvey, Ryan
2016-04-09  9:06 ` Takashi Iwai
2016-04-09 16:07   ` Lars-Peter Clausen
2016-04-09 19:45     ` Joe Perches
2016-04-11  2:26     ` Joe Perches [this message]
2016-04-11  2:26       ` [PATCH] checkpatch: Improve missing break for switch/case tests Joe Perches
2016-04-11  5:04       ` Joe Perches
2016-04-11  5:04         ` 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=1460341582.1800.66.camel@perches.com \
    --to=joe@perches.com \
    --cc=Ryan.Harvey@cirrus.com \
    --cc=akpm@linux-foundation.org \
    --cc=alsa-devel@alsa-project.org \
    --cc=apw@shadowen.org \
    --cc=lars@metafoo.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=tiwai@suse.de \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.