check-format.pl: further fixes for whitespace reporting within 'for (...)'

Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Richard Levitte <levitte@openssl.org>
Reviewed-by: David von Oheimb <david.von.oheimb@siemens.com>
(Merged from https://github.com/openssl/openssl/pull/18812)
This commit is contained in:
Dr. David von Oheimb 2022-07-25 18:14:33 +02:00 committed by Dr. David von Oheimb
parent 521f07eb08
commit 0a8a9f8f63
3 changed files with 40 additions and 27 deletions

View File

@ -117,17 +117,24 @@ int g(void)
/* leading comment has same indentation as normal code */ stmt;
/* entire-line comment may have same indent as normal code */
}
for (;;)
;
for (i = 0;;)
;
for (i = 0; i < 1;)
;
for (;;)
for (i = 0; i < n; i++)
for (; i < n; i++)
for (;; p++)
;
for (i = 0; ; i++)
for (i = 0;; i++)
for (i = 0; i < n; )
for (i = 0; i < n;)
;
for (i = 0; ; )
for (i = 0; ;)
for (i = 0;; )
for (i = 0;;)
for (; i < n; )
for (; j < n;)
for (; ; i++)
for (;; i++)
;
for (;;) /* the only variant allowed in case of "empty" for (...) */
;
for (;;) ; /* should not trigger: space before ';' */
lab: ; /* should not trigger: space before ';' */

View File

@ -29,8 +29,8 @@
/*@ whitespace at EOL: */
// /*@ end-of-line comment style not allowed (for C90 compatibility) */
/*@0 intra-line comment indent off by 1, reported unless sloppy-cmt */
/*X */ /*@2 no space nor '*' after comment start, reported unless sloppy-spc */
/* X*/ /*@ no space before comment end , reported unless sloppy-spc */
/*X */ /*@2 missing spc or '*' after comment start reported unless sloppy-spc */
/* X*/ /*@ missing space before comment end , reported unless sloppy-spc */
/*@ comment starting delimiter: /* inside intra-line comment */
/*@0
*@ above multi-line comment start indent off by 1, reported unless sloppy-cmt; this comment line is too long
@ -67,7 +67,9 @@ typedef struct { /*@0 extra space in code, reported unless sloppy-spc */
} s_type; /*@ statement/type declaration indent off by 4 */
int* somefunc(); /*@ no space before '*' in type decl, r unless sloppy-spc */
void main(int n) { /*@ opening brace at end of function definition header */
for (;;n++) { /*@ no space after ';', reported unless sloppy-spc */
for (; ; ) ; /*@ space before ')', reported unless sloppy-spc */
for ( ; x; y) ; /*@2 space after '(' and before ';', unless sloppy-spc */
for (;;n++) { /*@ missing space after ';', reported unless sloppy-spc */
return; /*@0 (1-line) single statement in braces */
}} /*@2 code after '}' outside expr */
} /*@ unexpected closing brace (too many '}') outside expr */
@ -102,13 +104,13 @@ int f (int a, /*@ space after fn before '(', reported unless sloppy-spc */
# define MAC(A) (A) /*@ nesting indent of preprocessor directive off by 1 */
? 1 /*@ hanging expr indent off by 1 */
: 2); /*@ hanging expr indent off by 2, or 1 for leading ':' */
if(a /*@ no space after 'if', reported unless sloppy-spc */
if(a /*@ missing space after 'if', reported unless sloppy-spc */
/*@0 intra-line comment indent off by -1 (not: by 3 due to '&&') */
&& ! 0 /*@2 space after '!', reported unless sloppy-spc */
|| b == /*@ hanging expr indent off by 2, or -2 for leading '||' */
(xx+= 2) + /*@ no space before '+=', reported unless sloppy-spc */
(a^ 1) + /*@ no space before '^', reported unless sloppy-spc */
a %2 / /*@ no space after '%', reported unless sloppy-spc */
(xx+= 2) + /*@ missing space before '+=', reported unless sloppy-spc */
(a^ 1) + /*@ missing space before '^', reported unless sloppy-spc */
a %2 / /*@ missing space after '%', reported unless sloppy-spc */
1 +/* */ /*@ no space before comment, reported unless sloppy-spc */
/* */+ /*@ no space after comment, reported unless sloppy-spc */
s. e_member) /*@ space after '.', reported unless sloppy-spc */
@ -117,7 +119,7 @@ int f (int a, /*@ space after fn before '(', reported unless sloppy-spc */
if (a ++) /*@ space before postfix '++', reported unless sloppy-spc */
{ /*@ {' not on same line as preceding 'if' */
c; /*@0 single stmt in braces, reported on 1-stmt */
} else /*@ no '{' on same line after '} else' */
} else /*@ missing '{' on same line after '} else' */
{ /*@ statement indent off by 2 */
d; /*@0 single stmt in braces, reported on 1-stmt */
} /*@ statement indent off by 6 */
@ -127,18 +129,18 @@ int f (int a, /*@ space after fn before '(', reported unless sloppy-spc */
while ( 2); /*@ space after '(', reported unless sloppy-spc */
b; c; /*@ more than one statement per line */
outer: /*@ outer label special indent off by 1 */
do{ /*@ no space before '{', reported unless sloppy-spc */
do{ /*@ missing space before '{', reported unless sloppy-spc */
inner: /*@ inner label normal indent off by 1 */
f (3, /*@ space after fn before '(', reported unless sloppy-spc */
4); /*@0 false negative: should report single stmt in braces */
} /*@0 'while' not on same line as preceding '}' */
while (a+ 0); /*@2 no space before '+', reported unless sloppy-spc */
while (a+ 0); /*@2 missing space before '+', reported unless sloppy-spc */
switch (b ) { /*@ space before ')', reported unless sloppy-spc */
case 1: /*@ 'case' special statement indent off by -1 */
case(2): /*@ no space after 'case', reported unless sloppy-spc */
case(2): /*@ missing space after 'case', reported unless sloppy-spc */
default: ; /*@ code after 'default:' */
} /*@ statement indent off by -4 */
return( /*@ no space after 'return', reported unless sloppy-spc */
return( /*@ missing space after 'return', reported unless sloppy-spc */
x); } /*@ code before block-level '}' */
/* Here the tool should stop complaining apart from the below issues at EOF */

View File

@ -452,7 +452,7 @@ sub update_nested_indents { # may reset $in_paren_expr and in this case also res
my ($head, $tail) = (substr($str, 0, $i).$1, $3);
$i += length($1) + length($2) - 1;
# stop at terminator outside 'for(..;..;..)', assuming that 'for' is followed by '('
# stop at terminator outside 'for (..;..;..)', assuming that 'for' is followed by '('
return $i if $c eq ";" && (!$in_paren_expr || @nested_indents == 0);
my $in_stmt = $in_expr || @nested_symbols != 0; # not: || $in_typedecl != 0
@ -784,9 +784,12 @@ while (<>) { # loop over all lines of all input files
# treat remaining blinded comments and string literal contents as (single) space during matching below
$intra_line =~ s/@+/ /g; # note that extra SPC has already been handled above
$intra_line =~ s/\s+$//; # strip any (resulting) space at EOL
$intra_line =~ s/(for\s*\([^;]*);;(\))/"$1$2"/eg; # strip trailing ';;' in for (;;)
$intra_line =~ s/(for\s*\([^;]+;[^;]+);(\))/"$1$2"/eg; # strip trailing ';' in for (;;)
$intra_line =~ s/(for\s*\();(;)/"$1$2"/eg; # replace leading ';;' in for (;;) by ';'
# replace ';;' or '; ;' by ';' in "for(;;)" and in "for (...)" unless "..." contains just SPC and ';' characters:
$intra_line =~ s/((^|\W)for\s*\()([^;]*?)(\s*)(;\s?);(\s*)([^;]*)(\))/
"$1$3$4".("$3$4$5$6$7" eq ";" || $3 ne "" || $7 ne "" ? "" : $5).";$6$7$8"/eg;
# strip trailing ';' or '; ' in "for (...)" except in "for (;;)" or "for (;; )":
$intra_line =~ s/((^|\W)for\s*\()([^;]*(;[^;]*)?)(;\s?)(\))/
"$1$3".($3 eq ";" ? $5 : "")."$6"/eg;
$intra_line =~ s/(=\s*)\{ /"$1@ "/eg; # do not report {SPC in initializers such as ' = { 0, };'
$intra_line =~ s/, \};/, @;/g; # do not report SPC} in initializers such as ' = { 0, };'
report("space before '$1'") if $intra_line =~ m/[\w)\]]\s+(\+\+|--)/; # postfix ++/-- with preceding space
@ -795,7 +798,8 @@ while (<>) { # loop over all lines of all input files
report("space before '$1'") if $intra_line =~ m/\s(\.|->)/; # '.' or '->' with preceding space
report("space after '$1'") if $intra_line =~ m/(\.|->)\s/; # '.' or '->' with following space
$intra_line =~ s/\-\>|\+\+|\-\-/@/g; # blind '->,', '++', and '--'
report("space before '$1'") if $intra_line =~ m/[^:)]\s+(;)/; # space before ';' but not after ':' or ')'
report("space before '$1'") if $intra_line =~ m/[^:)]\s+(;)/; # space before ';' but not after ':' or ')' # note that
# exceptions for "for (;; )" are handled above
report("space before '$1'") if $intra_line =~ m/\s([,)\]])/; # space before ,)]
report("space after '$1'") if $intra_line =~ m/([(\[~!])\s/; # space after ([~!
report("space after '$1'") if $intra_line =~ m/(defined)\s/; # space after 'defined'