From: Andy Whitcroft <apw@shadowen.org>
To: Andrew Morton <akpm@osdl.org>
Cc: Randy Dunlap <rdunlap@xenotime.net>,
Joel Schopp <jschopp@austin.ibm.com>, Ingo Molnar <mingo@elte.hu>,
linux-kernel@vger.kernel.org, Andy Whitcroft <apw@shadowen.org>
Subject: [PATCH 12/17] checkpatch: report the real first line of all suspect indents
Date: Mon, 11 Aug 2008 21:13:07 +0100 [thread overview]
Message-ID: <1218485592-6679-13-git-send-email-apw@shadowen.org> (raw)
In-Reply-To: <1218485592-6679-1-git-send-email-apw@shadowen.org>
We are currently only reporting syspect indents if the conditional is
modified but the indent missmatch could be generated by the body changing,
make sure we catch both. Also only report the first line of the body,
and more importantly make sure we report the raw copy of the line. Finally
report the indent levels to make it easier to understand what is wrong.
Signed-off-by: Andy Whitcroft <apw@shadowen.org>
---
scripts/checkpatch.pl | 141 ++++++++++++++++++++++++++++++-------------------
1 files changed, 86 insertions(+), 55 deletions(-)
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index cc61cf7..e3ae79a 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -673,6 +673,22 @@ sub ctx_has_comment {
return ($cmt ne '');
}
+sub raw_line {
+ my ($linenr, $cnt) = @_;
+
+ my $offset = $linenr - 1;
+ $cnt++;
+
+ my $line;
+ while ($cnt) {
+ $line = $rawlines[$offset++];
+ next if (defined($line) && $line =~ /^-/);
+ $cnt--;
+ }
+
+ return $line;
+}
+
sub cat_vet {
my ($vet) = @_;
my ($res, $coded);
@@ -1392,6 +1408,76 @@ sub process {
}
}
+# Check relative indent for conditionals and blocks.
+ if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
+ my ($s, $c) = ($stat, $cond);
+
+ substr($s, 0, length($c), '');
+
+ # Make sure we remove the line prefixes as we have
+ # none on the first line, and are going to readd them
+ # where necessary.
+ $s =~ s/\n./\n/gs;
+
+ # Find out how long the conditional actually is.
+ my $cond_lines = 0 + $c =~ /\n/gs;
+
+ # We want to check the first line inside the block
+ # starting at the end of the conditional, so remove:
+ # 1) any blank line termination
+ # 2) any opening brace { on end of the line
+ # 3) any do (...) {
+ my $continuation = 0;
+ my $check = 0;
+ $s =~ s/^.*\bdo\b//;
+ $s =~ s/^\s*{//;
+ if ($s =~ s/^\s*\\//) {
+ $continuation = 1;
+ }
+ if ($s =~ s/^\s*\n//) {
+ $check = 1;
+ $cond_lines++;
+ }
+
+ # Also ignore a loop construct at the end of a
+ # preprocessor statement.
+ if (($prevline =~ /^.\s*#\s*define\s/ ||
+ $prevline =~ /\\\s*$/) && $continuation == 0) {
+ $check = 0;
+ }
+
+ # Ignore the current line if its is a preprocessor
+ # line.
+ if ($s =~ /^\s*#\s*/) {
+ $check = 0;
+ }
+
+ # Ignore the current line if it is label.
+ if ($s =~ /^\s*$Ident\s*:/) {
+ $check = 0;
+ }
+
+ my (undef, $sindent) = line_stats("+" . $s);
+ my $stat_real = raw_line($linenr, $cond_lines);
+
+ # Check if either of these lines are modified, else
+ # this is not this patch's fault.
+ if (!defined($stat_real) ||
+ $stat !~ /^\+/ && $stat_real !~ /^\+/) {
+ $check = 0;
+ }
+ if (defined($stat_real) && $cond_lines > 1) {
+ $stat_real = "[...]\n$stat_real";
+ }
+
+ ##print "line<$line> prevline<$prevline> indent<$indent> sindent<$sindent> check<$check> continuation<$continuation> s<$s> cond_lines<$cond_lines> stat_real<$stat_real> stat<$stat>\n";
+
+ if ($check && (($sindent % 8) != 0 ||
+ ($sindent <= $indent && $s ne ''))) {
+ WARN("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
+ }
+ }
+
# Track the 'values' across context and added lines.
my $opline = $line; $opline =~ s/^./ /;
my ($curr_values, $curr_vars) =
@@ -1869,61 +1955,6 @@ sub process {
}
}
-# Check relative indent for conditionals and blocks.
- if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
- my ($s, $c) = ($stat, $cond);
-
- substr($s, 0, length($c), '');
-
- # Make sure we remove the line prefixes as we have
- # none on the first line, and are going to readd them
- # where necessary.
- $s =~ s/\n./\n/gs;
-
- # We want to check the first line inside the block
- # starting at the end of the conditional, so remove:
- # 1) any blank line termination
- # 2) any opening brace { on end of the line
- # 3) any do (...) {
- my $continuation = 0;
- my $check = 0;
- $s =~ s/^.*\bdo\b//;
- $s =~ s/^\s*{//;
- if ($s =~ s/^\s*\\//) {
- $continuation = 1;
- }
- if ($s =~ s/^\s*\n//) {
- $check = 1;
- }
-
- # Also ignore a loop construct at the end of a
- # preprocessor statement.
- if (($prevline =~ /^.\s*#\s*define\s/ ||
- $prevline =~ /\\\s*$/) && $continuation == 0) {
- $check = 0;
- }
-
- # Ignore the current line if its is a preprocessor
- # line.
- if ($s =~ /^\s*#\s*/) {
- $check = 0;
- }
-
- # Ignore the current line if it is label.
- if ($s =~ /^\s*$Ident\s*:/) {
- $check = 0;
- }
-
- my (undef, $sindent) = line_stats("+" . $s);
-
- ##print "line<$line> prevline<$prevline> indent<$indent> sindent<$sindent> check<$check> continuation<$continuation> s<$s>\n";
-
- if ($check && (($sindent % 8) != 0 ||
- ($sindent <= $indent && $s ne ''))) {
- WARN("suspect code indent for conditional statements\n" . $herecurr);
- }
- }
-
# Check for bitwise tests written as boolean
if ($line =~ /
(?:
--
1.6.0.rc1.258.g80295
next prev parent reply other threads:[~2008-08-11 20:19 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-08-11 20:12 [PATCH 00/17] checkpatch: update to version 0.22 Andy Whitcroft
2008-08-11 20:12 ` [PATCH 01/17] checkpatch: square brackets -- exemption for array slices in braces Andy Whitcroft
2008-08-11 20:12 ` [PATCH 02/17] checkpatch: values: double ampersand may be unary Andy Whitcroft
2008-08-11 20:12 ` [PATCH 03/17] checkpatch: conditional indent -- labels have different indent rules Andy Whitcroft
2008-08-11 20:12 ` [PATCH 04/17] checkpatch: switch indent allow plain return Andy Whitcroft
2008-08-11 20:13 ` [PATCH 05/17] checkpatch: add tests for the attribute matcher Andy Whitcroft
2008-08-11 20:13 ` [PATCH 06/17] checkpatch: ____cacheline_aligned et al are modifiers Andy Whitcroft
2008-08-11 20:13 ` [PATCH 07/17] checkpatch: complex macros -- fix up extension handling Andy Whitcroft
2008-08-11 20:13 ` [PATCH 08/17] checkpatch: fix up comment checks search to scan the entire block Andy Whitcroft
2008-08-11 20:13 ` [PATCH 09/17] checkpatch: include/asm checks should be anchored Andy Whitcroft
2008-10-08 13:39 ` Geert Uytterhoeven
2008-08-11 20:13 ` [PATCH 10/17] checkpatch: reduce warnings for #include of asm/foo.h to check from arch/bar.c Andy Whitcroft
2008-08-11 20:13 ` [PATCH 11/17] checkpatch: report any absolute references to kernel source files Andy Whitcroft
2008-08-11 20:13 ` Andy Whitcroft [this message]
2008-08-11 20:13 ` [PATCH 13/17] checkpatch: suspect indent -- skip over preprocessor, label and blank lines Andy Whitcroft
2008-08-11 20:13 ` [PATCH 14/17] checkpatch: %Lx tests should hand %% as a literal Andy Whitcroft
2008-08-11 20:13 ` [PATCH 15/17] checkpatch: report the correct lines for single statement blocks Andy Whitcroft
2008-08-11 20:13 ` [PATCH 16/17] checkpatch: perform indent checks on perl Andy Whitcroft
2008-08-11 20:13 ` [PATCH 17/17] checkpatch: version: 0.22 Andy Whitcroft
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=1218485592-6679-13-git-send-email-apw@shadowen.org \
--to=apw@shadowen.org \
--cc=akpm@osdl.org \
--cc=jschopp@austin.ibm.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@elte.hu \
--cc=rdunlap@xenotime.net \
/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®