]>
Commit | Line | Data |
---|---|---|
473664aa | 1 | #! /usr/bin/env perl |
99a16e04 | 2 | # |
d8662f2f DDO |
3 | # Copyright 2020-2022 The OpenSSL Project Authors. All Rights Reserved. |
4 | # Copyright Siemens AG 2019-2022 | |
99a16e04 DDO |
5 | # |
6 | # Licensed under the Apache License 2.0 (the "License"). | |
7 | # You may not use this file except in compliance with the License. | |
8 | # You can obtain a copy in the file LICENSE in the source distribution | |
9 | # or at https://www.openssl.org/source/license.html | |
10 | # | |
11 | # check-format.pl | |
12 | # - check formatting of C source according to OpenSSL coding style | |
13 | # | |
14 | # usage: | |
15 | # check-format.pl [-l|--sloppy-len] [-l|--sloppy-bodylen] | |
41d331b6 | 16 | # [-s|--sloppy-space] [-c|--sloppy-comment] |
435e659a | 17 | # [-m|--sloppy-macro] [-h|--sloppy-hang] |
41d331b6 | 18 | # [-e|--eol-comment] [-1|--1-stmt] |
99a16e04 DDO |
19 | # <files> |
20 | # | |
df4ec392 DDO |
21 | # run self-tests: |
22 | # util/check-format.pl util/check-format-test-positives.c | |
23 | # util/check-format.pl util/check-format-test-negatives.c | |
24 | # | |
99a16e04 DDO |
25 | # checks adherence to the formatting rules of the OpenSSL coding guidelines |
26 | # assuming that the input files contain syntactically correct C code. | |
27 | # This pragmatic tool is incomplete and yields some false positives. | |
28 | # Still it should be useful for detecting most typical glitches. | |
29 | # | |
30 | # options: | |
41d331b6 | 31 | # -l | --sloppy-len increase accepted max line length from 80 to 84 |
99a16e04 | 32 | # -l | --sloppy-bodylen do not report function body length > 200 |
41d331b6 DDO |
33 | # -s | --sloppy-space do not report whitespace nits |
34 | # -c | --sloppy-comment do not report indentation of comments | |
35 | # Otherwise for each multi-line comment the indentation of | |
36 | # its lines is checked for consistency. For each comment | |
37 | # that does not begin to the right of normal code its | |
38 | # indentation must be as for normal code, while in case it | |
39 | # also has no normal code to its right it is considered to | |
40 | # refer to the following line and may be indented equally. | |
41 | # -m | --sloppy-macro allow missing extra indentation of macro bodies | |
42 | # -h | --sloppy-hang when checking hanging indentation, do not report | |
43 | # * same indentation as on line before | |
44 | # * same indentation as non-hanging indent level | |
45 | # * indentation moved left (not beyond non-hanging indent) | |
46 | # just to fit contents within the line length limit | |
47 | # -e | --eol-comment report needless intermediate multiple consecutive spaces also before end-of-line comments | |
48 | # -1 | --1-stmt do more aggressive checks for { 1 stmt } - see below | |
99a16e04 DDO |
49 | # |
50 | # There are non-trivial false positives and negatives such as the following. | |
51 | # | |
52 | # * When a line contains several issues of the same kind only one is reported. | |
53 | # | |
54 | # * When a line contains more than one statement this is (correctly) reported | |
55 | # but in some situations the indentation checks for subsequent lines go wrong. | |
56 | # | |
57 | # * There is the special OpenSSL rule not to unnecessarily use braces around | |
58 | # single statements: | |
59 | # { | |
60 | # stmt; | |
61 | # } | |
62 | # except within if ... else constructs where some branch contains more than one | |
63 | # statement. Since the exception is hard to recognize when such branches occur | |
64 | # after the current position (such that false positives would be reported) | |
e304aa87 | 65 | # the tool by checks for this rule by default only for do/while/for bodies. |
99a16e04 | 66 | # Yet with the --1-stmt option false positives are preferred over negatives. |
d8662f2f | 67 | # False negatives occur if the braces are more than two non-blank lines apart. |
99a16e04 | 68 | # |
eb9b5320 | 69 | # * The presence of multiple consecutive spaces is regarded a coding style nit |
41d331b6 | 70 | # except when this is before end-of-line comments (unless the --eol-comment is given) and |
435e659a | 71 | # except when done in order to align certain columns over multiple lines, e.g.: |
99a16e04 DDO |
72 | # # define AB 1 |
73 | # # define CDE 22 | |
74 | # # define F 3333 | |
435e659a | 75 | # This pattern is recognized - and consequently extra space not reported - |
d8662f2f | 76 | # for a given line if in the non-blank line before or after (if existing) |
99a16e04 DDO |
77 | # for each occurrence of " \S" (where \S means non-space) in the given line |
78 | # there is " \S" in the other line in the respective column position. | |
79 | # This may lead to both false negatives (in case of coincidental " \S") | |
80 | # and false positives (in case of more complex multi-column alignment). | |
81 | # | |
82 | # * When just part of control structures depend on #if(n)(def), which can be | |
83 | # considered bad programming style, indentation false positives occur, e.g.: | |
84 | # #if X | |
85 | # if (1) /* bad style */ | |
86 | # #else | |
87 | # if (2) /* bad style resulting in false positive */ | |
88 | # #endif | |
89 | # c; /* resulting further false positive */ | |
90 | ||
91 | use strict; | |
92 | # use List::Util qw[min max]; | |
93 | use POSIX; | |
94 | ||
95 | use constant INDENT_LEVEL => 4; | |
96 | use constant MAX_LINE_LENGTH => 80; | |
97 | use constant MAX_BODY_LENGTH => 200; | |
98 | ||
99 | # global variables @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | |
100 | ||
101 | # command-line options | |
102 | my $max_length = MAX_LINE_LENGTH; | |
103 | my $sloppy_bodylen = 0; | |
104 | my $sloppy_SPC = 0; | |
105 | my $sloppy_hang = 0; | |
106 | my $sloppy_cmt = 0; | |
107 | my $sloppy_macro = 0; | |
eb9b5320 | 108 | my $eol_cmt = 0; |
99a16e04 DDO |
109 | my $extended_1_stmt = 0; |
110 | ||
111 | while ($ARGV[0] =~ m/^-(\w|-[\w\-]+)$/) { | |
112 | my $arg = $1; shift; | |
113 | if ($arg =~ m/^(l|-sloppy-len)$/) { | |
114 | $max_length += INDENT_LEVEL; | |
115 | } elsif ($arg =~ m/^(b|-sloppy-bodylen)$/) { | |
116 | $sloppy_bodylen = 1; | |
f35a9b6a DDO |
117 | } elsif ($arg =~ m/^(s|-sloppy-space)$/) { |
118 | $sloppy_SPC= 1; | |
41d331b6 | 119 | } elsif ($arg =~ m/^(c|-sloppy-comment)$/) { |
99a16e04 DDO |
120 | $sloppy_cmt = 1; |
121 | } elsif ($arg =~ m/^(m|-sloppy-macro)$/) { | |
122 | $sloppy_macro = 1; | |
123 | } elsif ($arg =~ m/^(h|-sloppy-hang)$/) { | |
124 | $sloppy_hang = 1; | |
41d331b6 | 125 | } elsif ($arg =~ m/^(e|-eol-comment)$/) { |
eb9b5320 | 126 | $eol_cmt = 1; |
99a16e04 DDO |
127 | } elsif ($arg =~ m/^(1|-1-stmt)$/) { |
128 | $extended_1_stmt = 1; | |
129 | } else { | |
130 | die("unknown option: -$arg"); | |
131 | } | |
132 | } | |
133 | ||
134 | # status variables | |
135 | my $self_test; # whether the current input file is regarded to contain (positive/negative) self-tests | |
136 | my $line; # current line number | |
d8662f2f DDO |
137 | my $line_before; # number of previous not essentially blank line (containing at most whitespace and '\') |
138 | my $line_before2; # number of not essentially blank line before previous not essentially blank line | |
eb9b5320 DDO |
139 | my $contents; # contents of current line (without blinding) |
140 | # $_ # current line, where comments etc. get blinded | |
d8662f2f | 141 | my $code_contents_before; # contents of previous non-comment non-directive line (without blinding), initially "" |
eb9b5320 | 142 | my $contents_before; # contents of $line_before (without blinding), if $line_before > 0 |
99a16e04 | 143 | my $contents_before_; # contents of $line_before after blinding comments etc., if $line_before > 0 |
eb9b5320 | 144 | my $contents_before2; # contents of $line_before2 (without blinding), if $line_before2 > 0 |
99a16e04 DDO |
145 | my $contents_before_2; # contents of $line_before2 after blinding comments etc., if $line_before2 > 0 |
146 | my $in_multiline_string; # line starts within multi-line string literal | |
147 | my $count; # -1 or number of leading whitespace characters (except newline) in current line, | |
148 | # which should be $block_indent + $hanging_offset + $local_offset or $expr_indent | |
149 | my $count_before; # number of leading whitespace characters (except line ending chars) in $contents_before | |
150 | my $has_label; # current line contains label | |
151 | my $local_offset; # current extra indent due to label, switch case/default, or leading closing brace(s) | |
152 | my $line_body_start; # number of line where last function body started, or 0 | |
153 | my $line_function_start; # number of line where last function definition started, used if $line_body_start != 0 | |
154 | my $last_function_header; # header containing name of last function defined, used if $line_function_start != 0 | |
155 | my $line_opening_brace; # number of previous line with opening brace after do/while/for, optionally for if/else | |
156 | ||
157 | my $keyword_opening_brace; # name of previous keyword, used if $line_opening_brace != 0 | |
158 | my $ifdef__cplusplus; # line before contained '#ifdef __cplusplus' (used in header files) | |
159 | my $block_indent; # currently required normal indentation at block/statement level | |
160 | my $hanging_offset; # extra indent, which may be nested, for just one hanging statement or expr or typedef | |
161 | my @in_do_hanging_offsets; # stack of hanging offsets for nested 'do' ... 'while' | |
162 | my @in_if_hanging_offsets; # stack of hanging offsets for nested 'if' (but not its potential 'else' branch) | |
163 | my $if_maybe_terminated; # 'if' ends and $hanging_offset should be reset unless the next line starts with 'else' | |
164 | my @nested_block_indents; # stack of indentations at block/statement level, needed due to hanging statements | |
165 | my @nested_hanging_offsets;# stack of nested $hanging_offset values, in parallel to @nested_block_indents | |
166 | my @nested_in_typedecl; # stack of nested $in_typedecl values, partly in parallel to @nested_block_indents | |
167 | my @nested_indents; # stack of hanging indents due to parentheses, braces, brackets, or conditionals | |
168 | my @nested_symbols; # stack of hanging symbols '(', '{', '[', or '?', in parallel to @nested_indents | |
169 | my @nested_conds_indents; # stack of hanging indents due to conditionals ('?' ... ':') | |
170 | my $expr_indent; # resulting hanging indent within (multi-line) expressions including type exprs, else 0 | |
171 | my $hanging_symbol; # character ('(', '{', '[', not: '?') responsible for $expr_indent, if $expr_indent != 0 | |
d8662f2f | 172 | my $in_block_decls; # number of local declaration lines after block opening before normal statements |
99a16e04 DDO |
173 | my $in_expr; # in expression after if/while/for/switch/return/enum/LHS of assignment |
174 | my $in_paren_expr; # in parenthesized if/while/for condition and switch expression, if $expr_indent != 0 | |
175 | my $in_typedecl; # nesting level of typedef/struct/union/enum | |
176 | my $in_directive; # number of lines so far within preprocessor directive, e.g., macro definition | |
177 | my $directive_nesting; # currently required indentation of preprocessor directive according to #if(n)(def) | |
178 | my $directive_offset; # indent offset within multi-line preprocessor directive, if $in_directive > 0 | |
179 | my $in_macro_header; # number of open parentheses + 1 in (multi-line) header of #define, if $in_directive > 0 | |
180 | my $in_comment; # number of lines so far within multi-line comment, or < 0 when end is on current line | |
181 | my $leading_comment; # multi-line comment has no code before its beginning delimiter | |
182 | my $formatted_comment; # multi-line comment beginning with "/*-", which indicates/allows special formatting | |
183 | my $comment_indent; # comment indent, if $in_comment != 0 | |
184 | my $num_reports_line = 0; # number of issues found on current line | |
185 | my $num_reports = 0; # total number of issues found | |
186 | my $num_indent_reports = 0;# total number of indentation issues found | |
187 | my $num_nesting_issues = 0;# total number of directive nesting issues found | |
188 | my $num_syntax_issues = 0; # total number of syntax issues found during sanity checks | |
189 | my $num_SPC_reports = 0; # total number of whitespace issues found | |
190 | my $num_length_reports = 0;# total number of line length issues found | |
191 | ||
192 | sub reset_file_state { | |
193 | $line = 0; | |
194 | $line_before = 0; | |
195 | $line_before2 = 0; | |
d8662f2f | 196 | $code_contents_before = ""; |
99a16e04 DDO |
197 | @nested_block_indents = (); |
198 | @nested_hanging_offsets = (); | |
199 | @nested_in_typedecl = (); | |
200 | @nested_symbols = (); | |
201 | @nested_indents = (); | |
202 | @nested_conds_indents = (); | |
203 | $expr_indent = 0; | |
d8662f2f | 204 | $in_block_decls = -1; |
99a16e04 | 205 | $in_expr = 0; |
d8662f2f | 206 | $in_paren_expr = 0; |
99a16e04 DDO |
207 | $hanging_offset = 0; |
208 | @in_do_hanging_offsets = (); | |
209 | @in_if_hanging_offsets = (); | |
210 | $if_maybe_terminated = 0; | |
211 | $block_indent = 0; | |
212 | $ifdef__cplusplus = 0; | |
213 | $in_multiline_string = 0; | |
214 | $line_body_start = 0; | |
215 | $line_opening_brace = 0; | |
216 | $in_typedecl = 0; | |
217 | $in_directive = 0; | |
218 | $directive_nesting = 0; | |
219 | $in_comment = 0; | |
220 | } | |
221 | ||
222 | # auxiliary submodules @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | |
223 | ||
224 | sub report_flexibly { | |
225 | my $line = shift; | |
226 | my $msg = shift; | |
227 | my $contents = shift; | |
f35a9b6a | 228 | my $report_SPC = $msg =~ /space/; |
99a16e04 DDO |
229 | return if $report_SPC && $sloppy_SPC; |
230 | ||
231 | print "$ARGV:$line:$msg:$contents" unless $self_test; | |
232 | $num_reports_line++; | |
233 | $num_reports++; | |
234 | $num_indent_reports++ if $msg =~ m/indent/; | |
235 | $num_nesting_issues++ if $msg =~ m/directive nesting/; | |
236 | $num_syntax_issues++ if $msg =~ m/unclosed|unexpected/; | |
237 | $num_SPC_reports++ if $report_SPC; | |
238 | $num_length_reports++ if $msg =~ m/length/; | |
239 | } | |
240 | ||
241 | sub report { | |
242 | my $msg = shift; | |
243 | report_flexibly($line, $msg, $contents); | |
244 | } | |
245 | ||
246 | sub parens_balance { # count balance of opening parentheses - closing parentheses | |
247 | my $str = shift; | |
248 | return $str =~ tr/\(// - $str =~ tr/\)//; | |
249 | } | |
250 | ||
251 | sub blind_nonspace { # blind non-space text of comment as @, preserving length and spaces | |
252 | # the @ character is used because it cannot occur in normal program code so there is no confusion | |
435e659a | 253 | # comment text is not blinded to whitespace in order to be able to check extra SPC also in comments |
99a16e04 | 254 | my $comment_text = shift; |
435e659a | 255 | $comment_text =~ s/([\.\?\!])\s\s/$1. /g; # in extra SPC checks allow one extra SPC after period '.', '?', or '!' in comments |
99a16e04 DDO |
256 | return $comment_text =~ tr/ /@/cr; |
257 | } | |
258 | ||
259 | # submodule for indentation checking/reporting @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | |
260 | ||
261 | sub check_indent { # used for lines outside multi-line string literals | |
262 | my $stmt_indent = $block_indent + $hanging_offset + $local_offset; | |
263 | $stmt_indent = 0 if $stmt_indent < 0; # TODO maybe give warning/error | |
264 | my $stmt_desc = $contents =~ | |
265 | m/^\s*\/\*/ ? "intra-line comment" : | |
266 | $has_label ? "label" : | |
267 | ($hanging_offset != 0 ? "hanging " : ""). | |
268 | ($hanging_offset != 0 ? "stmt/expr" : "stmt/decl"); # $in_typedecl is not fully to the point here | |
269 | my ($ref_desc, $ref_indent) = $expr_indent == 0 ? ($stmt_desc, $stmt_indent) | |
270 | : ("hanging '$hanging_symbol'", $expr_indent); | |
271 | my ($alt_desc, $alt_indent) = ("", $ref_indent); | |
272 | ||
273 | # allow indent 1 for labels - this cannot happen for leading ':' | |
274 | ($alt_desc, $alt_indent) = ("outermost position", 1) if $expr_indent == 0 && $has_label; | |
275 | ||
276 | if (@nested_conds_indents != 0 && substr($_, $count, 1) eq ":") { | |
a77571c3 | 277 | # leading ':' within stmt/expr/decl - this cannot happen for labels, leading '&&', or leading '||' |
99a16e04 DDO |
278 | # allow special indent at level of corresponding "?" |
279 | ($alt_desc, $alt_indent) = ("leading ':'", @nested_conds_indents[-1]); | |
280 | } | |
281 | # allow extra indent offset leading '&&' or '||' - this cannot happen for leading ":" | |
282 | ($alt_desc, $alt_indent) = ("leading '$1'", $ref_indent + INDENT_LEVEL) if $contents =~ m/^[\s@]*(\&\&|\|\|)/; | |
283 | ||
284 | if ($expr_indent < 0) { # implies @nested_symbols != 0 && @nested_symbols[0] eq "{" && @nested_indents[-1] < 0 | |
285 | # allow normal stmt indentation level for hanging initializer/enum expressions after trailing '{' | |
286 | # this cannot happen for labels and overrides special treatment of ':', '&&' and '||' for this line | |
287 | ($alt_desc, $alt_indent) = ("lines after '{'", $stmt_indent); | |
288 | # decide depending on current actual indentation, preventing forth and back | |
289 | @nested_indents[-1] = $count == $stmt_indent ? $stmt_indent : -@nested_indents[-1]; # allow $stmt_indent | |
290 | $ref_indent = $expr_indent = @nested_indents[-1]; | |
291 | } | |
292 | ||
293 | # check consistency of indentation within multi-line comment (i.e., between its first, inner, and last lines) | |
294 | if ($in_comment != 0 && $in_comment != 1) { # in multi-line comment but not on its first line | |
295 | if (!$sloppy_cmt) { | |
296 | if ($in_comment > 0) { # not at its end | |
297 | report("indent = $count != $comment_indent within multi-line comment") | |
298 | if $count != $comment_indent; | |
299 | } else { | |
300 | my $tweak = $in_comment == -2 ? 1 : 0; | |
301 | report("indent = ".($count + $tweak)." != $comment_indent at end of multi-line comment") | |
302 | if $count + $tweak != $comment_indent; | |
303 | } | |
304 | } | |
305 | # do not check indentation of last line of non-leading multi-line comment | |
306 | if ($in_comment < 0 && !$leading_comment) { | |
307 | s/^(\s*)@/$1*/; # blind first '@' as '*' to prevent below delayed check for the line before | |
308 | return; | |
309 | } | |
310 | return if $in_comment > 0; # not on its last line | |
311 | # $comment_indent will be checked by the below checks for end of multi-line comment | |
312 | } | |
313 | ||
314 | # else check indentation of entire-line comment or entire-line end of multi-line comment | |
315 | # ... w.r.t. indent of the following line by delayed check for the line before | |
316 | if (($in_comment == 0 || $in_comment == 1) # no comment, intra-line comment, or begin of multi-line comment | |
317 | && $line_before > 0 # there is a line before | |
318 | && $contents_before_ =~ m/^(\s*)@[\s@]*$/) { # line before begins with '@', no code follows (except '\') | |
319 | report_flexibly($line_before, "entire-line comment indent = $count_before != $count (of following line)", | |
320 | $contents_before) if !$sloppy_cmt && $count_before != $count; | |
321 | } | |
322 | # ... but allow normal indentation for the current line, else above check will be done for the line before | |
e304aa87 | 323 | if (($in_comment == 0 || $in_comment < 0) # (no comment,) intra-line comment or end of multi-line comment |
99a16e04 DDO |
324 | && m/^(\s*)@[\s@]*$/) { # line begins with '@', no code follows (except '\') |
325 | if ($count == $ref_indent) { # indentation is like for (normal) code in this line | |
326 | s/^(\s*)@/$1*/; # blind first '@' as '*' to prevent above delayed check for the line before | |
327 | return; | |
328 | } | |
329 | return if !eof; # defer check of entire-line comment to next line | |
330 | } | |
331 | ||
332 | # else check indentation of leading intra-line comment or end of multi-line comment | |
333 | if (m/^(\s*)@/) { # line begins with '@', i.e., any (remaining type of) comment | |
334 | if (!$sloppy_cmt && $count != $ref_indent) { | |
335 | report("intra-line comment indent = $count != $ref_indent") if $in_comment == 0; | |
336 | report("multi-line comment indent = $count != $ref_indent") if $in_comment < 0; | |
337 | } | |
338 | return; | |
339 | } | |
340 | ||
341 | if ($sloppy_hang && ($hanging_offset != 0 || $expr_indent != 0)) { | |
342 | # do not report same indentation as on the line before (potentially due to same violations) | |
343 | return if $line_before > 0 && $count == $count_before; | |
344 | ||
345 | # do not report indentation at normal indentation level while hanging expression indent would be required | |
346 | return if $expr_indent != 0 && $count == $stmt_indent; | |
347 | ||
348 | # do not report if contents have been shifted left of nested expr indent (but not as far as stmt indent) | |
349 | # apparently aligned to the right in order to fit within line length limit | |
350 | return if $stmt_indent < $count && $count < $expr_indent && | |
351 | length($contents) == MAX_LINE_LENGTH + length("\n"); | |
352 | } | |
353 | ||
354 | report("indent = $count != $ref_indent for $ref_desc". | |
355 | ($alt_desc eq "" | |
356 | || $alt_indent == $ref_indent # prevent showing alternative that happens to have equal value | |
357 | ? "" : " or $alt_indent for $alt_desc")) | |
358 | if $count != $ref_indent && $count != $alt_indent; | |
359 | } | |
360 | ||
361 | # submodules handling indentation within expressions @@@@@@@@@@@@@@@@@@@@@@@@@@@ | |
362 | ||
363 | sub update_nested_indents { # may reset $in_paren_expr and in this case also resets $in_expr | |
364 | my $str = shift; | |
365 | my $start = shift; # defaults to 0 | |
366 | my $terminator_position = -1; | |
367 | for (my $i = $start; $i < length($str); $i++) { | |
368 | my $c; | |
369 | my $curr = substr($str, $i); | |
370 | if ($curr =~ m/^(.*?)([{}()?:;\[\]])(.*)$/) { # match from position $i the first {}()?:;[] | |
371 | $c = $2; | |
372 | } else { | |
373 | last; | |
374 | } | |
375 | my ($head, $tail) = (substr($str, 0, $i).$1, $3); | |
376 | $i += length($1) + length($2) - 1; | |
377 | ||
378 | # stop at terminator outside 'for(..;..;..)', assuming that 'for' is followed by '(' | |
379 | return $i if $c eq ";" && (!$in_paren_expr || @nested_indents == 0); | |
380 | ||
381 | my $in_stmt = $in_expr || @nested_symbols != 0; # not: || $in_typedecl != 0 | |
382 | if ($c =~ m/[{([?]/) { # $c is '{', '(', '[', or '?' | |
383 | if ($c eq "{") { # '{' in any context | |
d8662f2f | 384 | $in_block_decls = 0 if !$in_expr && $in_typedecl == 0; |
99a16e04 DDO |
385 | # cancel newly hanging_offset if opening brace '{' is after non-whitespace non-comment: |
386 | $hanging_offset -= INDENT_LEVEL if $hanging_offset > 0 && $head =~ m/[^\s\@]/; | |
387 | push @nested_block_indents, $block_indent; | |
388 | push @nested_hanging_offsets, $in_expr ? $hanging_offset : 0; | |
389 | push @nested_in_typedecl, $in_typedecl if $in_typedecl != 0; | |
390 | $block_indent += INDENT_LEVEL + $hanging_offset; | |
391 | $hanging_offset = 0; | |
392 | } | |
393 | if ($c ne "{" || $in_stmt) { # for '{' inside stmt/expr (not: decl), for '(', '[', or '?' anywhere | |
394 | $tail =~ m/^([\s@]*)([^\s\@])/; | |
395 | push @nested_indents, defined $2 | |
396 | ? $i + 1 + length($1) # actual indentation of following non-space non-comment | |
397 | : $c ne "{" ? +($i + 1) # just after '(' or '[' if only whitespace thereafter | |
398 | : -($i + 1); # allow also $stmt_indent if '{' with only whitespace thereafter | |
399 | push @nested_symbols, $c; # done also for '?' to be able to check correct nesting | |
400 | push @nested_conds_indents, $i if $c eq "?"; # remember special alternative indent for ':' | |
401 | } | |
402 | } elsif ($c =~ m/[})\]:]/) { # $c is '}', ')', ']', or ':' | |
403 | my $opening_c = ($c =~ tr/})]:/{([/r); | |
404 | if (($c ne ":" || $in_stmt # ignore ':' outside stmt/expr/decl | |
405 | # in the presence of ':', one could add this sanity check: | |
406 | # && !(# ':' after initial label/case/default | |
407 | # $head =~ m/^([\s@]*)(case\W.*$|\w+$)/ || # this matching would not work for | |
408 | # # multi-line expr after 'case' | |
409 | # # bitfield length within unsigned type decl | |
410 | # $tail =~ m/^[\s@]*\d+/ # this matching would need improvement | |
411 | # ) | |
412 | )) { | |
413 | if ($c ne "}" || $in_stmt) { # for '}' inside stmt/expr/decl, ')', ']', or ':' | |
414 | if (@nested_symbols != 0 && | |
415 | @nested_symbols[-1] == $opening_c) { # for $c there was a corresponding $opening_c | |
416 | pop @nested_indents; | |
417 | pop @nested_symbols; | |
418 | pop @nested_conds_indents if $opening_c eq "?"; | |
419 | } else { | |
420 | report("unexpected '$c' @ ".($in_paren_expr ? "(expr)" : "expr")); | |
421 | next; | |
422 | } | |
423 | } | |
424 | if ($c eq "}") { # '}' at block level but also inside stmt/expr/decl | |
425 | if (@nested_block_indents == 0) { | |
426 | report("unexpected '}'"); | |
427 | } else { | |
428 | $block_indent = pop @nested_block_indents; | |
429 | $hanging_offset = pop @nested_hanging_offsets; | |
430 | $in_typedecl = pop @nested_in_typedecl if @nested_in_typedecl != 0; | |
431 | } | |
432 | } | |
433 | if ($in_paren_expr && !grep(/\(/, @nested_symbols)) { # end of (expr) | |
434 | check_nested_nonblock_indents("(expr)"); | |
435 | $in_paren_expr = $in_expr = 0; | |
436 | report("code after (expr)") | |
437 | if $tail =~ m/^([^{]*)/ && $1 =~ m/[^\s\@;]/; # non-space non-';' before any '{' | |
438 | } | |
439 | } | |
440 | } | |
441 | } | |
442 | return -1; | |
443 | } | |
444 | ||
445 | sub check_nested_nonblock_indents { | |
446 | my $position = shift; | |
447 | while (@nested_symbols != 0) { | |
448 | my $symbol = pop @nested_symbols; | |
449 | report("unclosed '$symbol' in $position"); | |
450 | if ($symbol eq "{") { # repair stack of blocks | |
451 | $block_indent = pop @nested_block_indents; | |
452 | $hanging_offset = pop @nested_hanging_offsets; | |
453 | $in_typedecl = pop @nested_in_typedecl if @nested_in_typedecl != 0; | |
454 | } | |
455 | } | |
456 | @nested_indents = (); | |
457 | @nested_conds_indents = (); | |
458 | } | |
459 | ||
460 | # start of main program @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | |
461 | ||
462 | reset_file_state(); | |
463 | ||
464 | while (<>) { # loop over all lines of all input files | |
465 | $self_test = $ARGV =~ m/check-format-test/; | |
d8662f2f | 466 | $_ = "" if $self_test && m/ blank line within local decls /; |
99a16e04 DDO |
467 | $line++; |
468 | s/\r$//; # strip any trailing CR '\r' (which are typical on Windows systems) | |
469 | $contents = $_; | |
470 | ||
471 | # check for illegal characters | |
472 | if (m/(.*?)([\x00-\x09\x0B-\x1F\x7F-\xFF])/) { | |
473 | my $col = length($1); | |
474 | report(($2 eq "\x09" ? "TAB" : $2 eq "\x0D" ? "CR " : $2 =~ m/[\x00-\x1F]/ ? "non-printable" | |
475 | : "non-7bit char") . " at column $col") ; | |
476 | } | |
477 | ||
478 | # check for whitespace at EOL | |
479 | report("trailing whitespace at EOL") if m/\s\n$/; | |
480 | ||
481 | # assign to $count the actual indentation level of the current line | |
482 | chomp; # remove trailing NL '\n' | |
483 | m/^(\s*)/; | |
484 | $count = length($1); # actual indentation | |
485 | $has_label = 0; | |
486 | $local_offset = 0; | |
487 | ||
488 | # character/string literals @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | |
489 | ||
490 | s/\\["']/@@/g; # blind all '"' and "'" escaped by '\' (typically within character literals or string literals) | |
491 | ||
492 | # handle multi-line string literals to avoid confusion on starting/ending '"' and trailing '\' | |
493 | if ($in_multiline_string) { | |
494 | if (s#^([^"]*)"#($1 =~ tr/"/@/cr).'@'#e) { # string literal terminated by '"' | |
495 | # string contents and its terminating '"' have been blinded as '@' | |
496 | $count = -1; # do not check indentation | |
497 | } else { | |
498 | report("multi-line string literal not terminated by '\"' and trailing '\' is missing") | |
499 | unless s#^([^\\]*)\s*\\\s*$#$1#; # strip trailing '\' plus any whitespace around | |
500 | goto LINE_FINISHED; | |
501 | } | |
502 | } | |
503 | ||
504 | # blind contents of character and string literals as @, preserving length (but not spaces) | |
505 | # this prevents confusing any of the matching below, e.g., of whitespace and comment delimiters | |
506 | s#('[^']*')#$1 =~ tr/'/@/cr#eg; # handle all intra-line character literals | |
507 | s#("[^"]*")#$1 =~ tr/"/@/cr#eg; # handle all intra-line string literals | |
508 | $in_multiline_string = # handle trailing string literal terminated by '\' | |
509 | s#^(([^"]*"[^"]*")*[^"]*)("[^"]*)\\(\s*)$#$1.($3 =~ tr/"/@/cr).'"'.$4#e; | |
510 | # its contents have been blinded and the trailing '\' replaced by '"' | |
511 | ||
512 | # strip any other trailing '\' along with any whitespace around it such that it does not interfere with various | |
513 | # matching below; the later handling of multi-line macro definitions uses $contents where it is not stripped | |
514 | s#^(.*?)\s*\\\s*$#$1#; # trailing '\' possibly preceded and/or followed by whitespace | |
515 | ||
516 | # comments @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | |
517 | ||
518 | # do/prepare checks within multi-line comments | |
519 | my $self_test_exception = $self_test ? "@" : ""; | |
e304aa87 | 520 | if ($in_comment > 0) { # this still includes the last line of multi-line comment |
99a16e04 DDO |
521 | my ($head, $any_symbol, $cmt_text) = m/^(\s*)(.?)(.*)$/; |
522 | if ($any_symbol eq "*") { | |
f35a9b6a | 523 | report("no space after leading '*' in multi-line comment") if $cmt_text =~ m|^[^/\s$self_test_exception]|; |
99a16e04 DDO |
524 | } else { |
525 | report("no leading '*' in multi-line comment"); | |
526 | } | |
527 | $in_comment++; | |
528 | } | |
529 | ||
530 | # detect end of comment, must be within multi-line comment, check if it is preceded by non-whitespace text | |
531 | if ((my ($head, $tail) = m|^(.*?)\*/(.*)$|) && $1 ne '/') { # ending comment: '*/' | |
f35a9b6a DDO |
532 | report("neither space nor '*' before '*/'") if $head =~ m/[^*\s]$/; |
533 | report("no space after '*/'") if $tail =~ m/^[^\s,;)}\]]/; # no space or ,;)}] after '*/' | |
99a16e04 DDO |
534 | if (!($head =~ m|/\*|)) { # not begin of comment '/*', which is is handled below |
535 | if ($in_comment == 0) { | |
536 | report("unexpected '*/' outside comment"); | |
537 | $_ = "$head@@".$tail; # blind the "*/" | |
538 | } else { | |
539 | report("text before '*/' in multi-line comment") if ($head =~ m/\S/); # non-SPC before '*/' | |
540 | $in_comment = -1; # indicate that multi-line comment ends on current line | |
541 | if ($count > 0) { | |
542 | # make indentation of end of multi-line comment appear like of leading intra-line comment | |
543 | $head =~ s/^(\s*)\s/$1@/; # replace the last leading space by '@' | |
544 | $count--; | |
545 | $in_comment = -2; # indicate that multi-line comment ends on current line, with tweak | |
546 | } | |
547 | my $cmt_text = $head; | |
548 | $_ = blind_nonspace($cmt_text)."@@".$tail; | |
549 | } | |
550 | } | |
551 | } | |
552 | ||
553 | # detect begin of comment, check if it is followed by non-space text | |
554 | MATCH_COMMENT: | |
555 | if (my ($head, $opt_minus, $tail) = m|^(.*?)/\*(-?)(.*)$|) { # begin of comment: '/*' | |
f35a9b6a | 556 | report("no space before '/*'") |
a77571c3 | 557 | if $head =~ m/[^\s(\*]$/; # not space, '(', or or '*' (needed to allow '*/') before comment delimiter |
f35a9b6a | 558 | report("neither space nor '*' after '/*' or '/*-'") if $tail =~ m/^[^\s*$self_test_exception]/; |
99a16e04 DDO |
559 | my $cmt_text = $opt_minus.$tail; # preliminary |
560 | if ($in_comment > 0) { | |
561 | report("unexpected '/*' inside multi-line comment"); | |
562 | } elsif ($tail =~ m|^(.*?)\*/(.*)$|) { # comment end: */ on same line | |
563 | report("unexpected '/*' inside intra-line comment") if $1 =~ /\/\*/; | |
564 | # blind comment text, preserving length and spaces | |
565 | ($cmt_text, my $rest) = ($opt_minus.$1, $2); | |
566 | $_ = "$head@@".blind_nonspace($cmt_text)."@@".$rest; | |
567 | goto MATCH_COMMENT; | |
568 | } else { # begin of multi-line comment | |
569 | my $self_test_exception = $self_test ? "(@\d?)?" : ""; | |
570 | report("text after '/*' in multi-line comment") | |
571 | unless $tail =~ m/^$self_test_exception.?\s*$/; | |
d8662f2f | 572 | # tail not essentially blank, first char already checked |
99a16e04 DDO |
573 | # adapt to actual indentation of first line |
574 | $comment_indent = length($head) + 1; | |
575 | $_ = "$head@@".blind_nonspace($cmt_text); | |
576 | $in_comment = 1; | |
577 | $leading_comment = $head =~ m/^\s*$/; # there is code before beginning delimiter | |
578 | $formatted_comment = $opt_minus eq "-"; | |
579 | } | |
580 | } | |
581 | ||
582 | if ($in_comment > 1) { # still inside multi-line comment (not at its begin or end) | |
583 | m/^(\s*)\*?(\s*)(.*)$/; | |
584 | $_ = $1."@".$2.blind_nonspace($3); | |
585 | } | |
586 | ||
587 | # handle special case of line after '#ifdef __cplusplus' (which typically appears in header files) | |
588 | if ($ifdef__cplusplus) { | |
589 | $ifdef__cplusplus = 0; | |
590 | $_ = "$1 $2" if $contents =~ m/^(\s*extern\s*"C"\s*)\{(\s*)$/; # ignore opening brace in 'extern "C" {' | |
591 | goto LINE_FINISHED if m/^\s*\}\s*$/; # ignore closing brace '}' | |
592 | } | |
593 | ||
594 | # check for over-long lines, | |
595 | # while allowing trailing (also multi-line) string literals to go past $max_length | |
596 | my $len = length; # total line length (without trailing '\n') | |
597 | if ($len > $max_length && | |
598 | !(m/^(.*)"[^"]*"\s*[\)\}\]]*[,;]?\s*$/ # string literal terminated by '"' (or '\'), then maybe )}],; | |
599 | && length($1) < $max_length) | |
600 | # this allows over-long trailing string literals with beginning col before $max_length | |
601 | ) { | |
602 | report("line length = $len > ".MAX_LINE_LENGTH); | |
603 | } | |
604 | ||
605 | # handle C++ / C99 - style end-of-line comments | |
606 | if (my ($head, $cmt_text) = m|^(.*?)//(.*$)|) { | |
607 | report("'//' end-of-line comment"); # the '//' comment style is not allowed for C90 | |
608 | # blind comment text, preserving length and spaces | |
609 | $_ = "$head@@".blind_nonspace($cmt_text); | |
610 | } | |
611 | ||
612 | # at this point all non-space portions of any types of comments have been blinded as @ | |
613 | ||
d8662f2f | 614 | goto LINE_FINISHED if m/^\s*$/; # essentially blank line: just whitespace (and maybe a trailing '\') |
99a16e04 DDO |
615 | |
616 | # intra-line whitespace nits @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | |
617 | ||
618 | my $in_multiline_comment = ($in_comment > 1 || $in_comment < 0); # $in_multiline_comment refers to line before | |
619 | if (!$sloppy_SPC && !($in_multiline_comment && $formatted_comment)) { | |
435e659a | 620 | sub extra_SPC { |
99a16e04 | 621 | my $intra_line = shift; |
f35a9b6a DDO |
622 | return "extra space".($intra_line =~ m/@\s\s/ ? |
623 | $in_comment != 0 ? " in multi-line comment" | |
624 | : " in intra-line comment" : ""); | |
99a16e04 | 625 | } |
eb9b5320 | 626 | sub split_line_head { # split line contents into header containing leading spaces and the first non-space char, and the rest of the line |
99a16e04 DDO |
627 | my $comment_symbol = |
628 | $in_comment != 0 ? "@" : ""; # '@' will match the blinded leading '*' in multi-line comment | |
629 | # $in_comment may pertain to the following line due to delayed check | |
435e659a | 630 | # do not check for extra SPC in leading spaces including any '#' (or '*' within multi-line comment) |
99a16e04 DDO |
631 | shift =~ m/^(\s*([#$comment_symbol]\s*)?)(.*?)\s*$/; |
632 | return ($1, $3); | |
633 | } | |
634 | my ($head , $intra_line ) = split_line_head($_); | |
635 | my ($head1, $intra_line1) = split_line_head($contents_before_ ) if $line_before > 0; | |
636 | my ($head2, $intra_line2) = split_line_head($contents_before_2) if $line_before2 > 0; | |
eb9b5320 DDO |
637 | if ($line_before > 0) { # check with one line delay, such that at least $contents_before is available |
638 | sub column_alignments_only { # return 1 if the given line has multiple consecutive spaces only at columns that match the reference line | |
639 | # all parameter strings are assumed to contain contents after blinding comments etc. | |
640 | my $head = shift; # leading spaces and the first non-space char | |
641 | my $intra = shift; # the rest of the line contents | |
642 | my $contents = shift; # reference line | |
435e659a | 643 | # check if all extra SPC in $intra is used only for multi-line column alignment with $contents |
99a16e04 DDO |
644 | my $offset = length($head); |
645 | for (my $col = 0; $col < length($intra) - 2; $col++) { | |
eb9b5320 DDO |
646 | my $substr = substr($intra, $col); |
647 | next unless $substr =~ m/^\s\s\S/; # extra SPC (but not in leading spaces of the line) | |
648 | next if !$eol_cmt && $substr =~ m/^[@\s]+$/; # end-of-line comment | |
649 | return 0 unless substr($contents, $col + $offset + 1, 2) =~ m/\s\S/; # reference line contents do not match | |
99a16e04 DDO |
650 | } |
651 | return 1; | |
652 | } | |
435e659a | 653 | report_flexibly($line_before, extra_SPC($intra_line1), $contents_before) if $intra_line1 =~ m/\s\s\S/ && |
99a16e04 DDO |
654 | !( column_alignments_only($head1, $intra_line1, $_ ) # compare with $line |
655 | || ($line_before2 > 0 && | |
656 | column_alignments_only($head1, $intra_line1, $contents_before_2))); # compare w/ $line_before2 | |
435e659a | 657 | report(extra_SPC($intra_line)) if $intra_line =~ m/\s\s\S/ && eof |
99a16e04 DDO |
658 | && ! column_alignments_only($head , $intra_line , $contents_before_ ) ; # compare w/ $line_before |
659 | } elsif (eof) { # special case: just one line exists | |
435e659a | 660 | report(extra_SPC($intra_line)) if $intra_line =~ m/\s\s\S/; |
99a16e04 DDO |
661 | } |
662 | # ignore paths in #include | |
663 | $intra_line =~ s/^(include\s*)(".*?"|<.*?>)/$1/e if $head =~ m/#/; | |
664 | # treat op= and comparison operators as simple '=', simplifying matching below | |
665 | $intra_line =~ s/([\+\-\*\/\/%\&\|\^\!<>=]|<<|>>)=/=/g; | |
666 | # treat (type) variables within macro, indicated by trailing '\', as 'int' simplifying matching below | |
667 | $intra_line =~ s/[A-Z_]+/int/g if $contents =~ m/^(.*?)\s*\\\s*$/; | |
668 | # treat double &&, ||, <<, and >> as single ones, simplifying matching below | |
669 | $intra_line =~ s/(&&|\|\||<<|>>)/substr($1, 0, 1)/eg; | |
a77571c3 DDO |
670 | # remove blinded comments etc. directly after [{( |
671 | while ($intra_line =~ s/([\[\{\(])@+\s?/$1/e) {} # /g does not work here | |
672 | # remove blinded comments etc. directly before ,;)}] | |
673 | while ($intra_line =~ s/\s?@+([,;\)\}\]])/$1/e) {} # /g does not work here | |
99a16e04 | 674 | # treat remaining blinded comments and string literal contents as (single) space during matching below |
435e659a | 675 | $intra_line =~ s/@+/ /g; # note that extra SPC has already been handled above |
99a16e04 | 676 | $intra_line =~ s/\s+$//; # strip any (resulting) space at EOL |
8e655da0 DDO |
677 | $intra_line =~ s/(for\s*\([^;]*);;(\))/"$1$2"/eg; # strip trailing ';;' in for (;;) |
678 | $intra_line =~ s/(for\s*\([^;]+;[^;]+);(\))/"$1$2"/eg; # strip trailing ';' in for (;;) | |
99a16e04 DDO |
679 | $intra_line =~ s/(=\s*)\{ /"$1@ "/eg; # do not report {SPC in initializers such as ' = { 0, };' |
680 | $intra_line =~ s/, \};/, @;/g; # do not report SPC} in initializers such as ' = { 0, };' | |
f35a9b6a DDO |
681 | report("space before '$1'") if $intra_line =~ m/[\w)\]]\s+(\+\+|--)/; # postfix ++/-- with preceding space |
682 | report("space after '$1'") if $intra_line =~ m/(\+\+|--)\s+[a-zA-Z_(]/; # prefix ++/-- with following space | |
99a16e04 | 683 | $intra_line =~ s/\.\.\./@/g; # blind '...' |
f35a9b6a DDO |
684 | report("space before '$1'") if $intra_line =~ m/\s(\.|->)/; # '.' or '->' with preceding space |
685 | report("space after '$1'") if $intra_line =~ m/(\.|->)\s/; # '.' or '->' with following space | |
99a16e04 | 686 | $intra_line =~ s/\-\>|\+\+|\-\-/@/g; # blind '->,', '++', and '--' |
c30bc4e2 | 687 | report("space before '$1'") if $intra_line =~ m/[^:)]\s+(;)/; # space before ';' but not after ':' or ')' |
f35a9b6a DDO |
688 | report("space before '$1'") if $intra_line =~ m/\s([,)\]])/; # space before ,)] |
689 | report("space after '$1'") if $intra_line =~ m/([(\[~!])\s/; # space after ([~! | |
690 | report("space after '$1'") if $intra_line =~ m/(defined)\s/; # space after 'defined' | |
691 | report("no space before '=' or '<op>='") if $intra_line =~ m/\S(=)/; # '=' etc. without preceding space | |
692 | report("no space before '$1'") if $intra_line =~ m/\S([|\/%<>^\?])/; # |/%<>^? without preceding space | |
99a16e04 | 693 | # TODO ternary ':' without preceding SPC, while allowing no SPC before ':' after 'case' |
d45c0e1a DDO |
694 | report("no space before binary '$2'") if $intra_line =~ m/([^\s{()\[e])([+\-])/; # '+'/'-' without preceding space or {()[e |
695 | # ')' may be used for type casts or before "->", 'e' may be used for numerical literals such as "1e-6" | |
f35a9b6a DDO |
696 | report("no space before binary '$1'") if $intra_line =~ m/[^\s{()\[*!]([*])/; # '*' without preceding space or {()[*! |
697 | report("no space before binary '$1'") if $intra_line =~ m/[^\s{()\[]([&])/; # '&' without preceding space or {()[ | |
698 | report("no space after ternary '$1'") if $intra_line =~ m/(:)[^\s\d]/; # ':' without following space or digit | |
699 | report("no space after '$1'") if $intra_line =~ m/([,;=|\/%<>^\?])\S/; # ,;=|/%<>^? without following space | |
700 | report("no space after binary '$1'") if $intra_line=~m/[^{(\[]([*])[^\sa-zA-Z_(),*]/;# '*' w/o space or \w(),* after | |
99a16e04 | 701 | # TODO unary '*' must not be followed by SPC |
f35a9b6a | 702 | report("no space after binary '$1'") if $intra_line=~m/([&])[^\sa-zA-Z_(]/; # '&' w/o following space or \w( |
99a16e04 | 703 | # TODO unary '&' must not be followed by SPC |
f35a9b6a | 704 | report("no space after binary '$1'") if $intra_line=~m/[^{(\[]([+\-])[^\s\d(]/; # +/- w/o following space or \d( |
99a16e04 | 705 | # TODO unary '+' and '-' must not be followed by SPC |
f35a9b6a DDO |
706 | report("no space after '$2'") if $intra_line =~ m/(^|\W)(if|while|for|switch|case)[^\w\s]/; # kw w/o SPC |
707 | report("no space after '$2'") if $intra_line =~ m/(^|\W)(return)[^\w\s;]/; # return w/o SPC or ';' | |
708 | report("space after function/macro name") | |
99a16e04 | 709 | if $intra_line =~ m/(\w+)\s+\(/ # fn/macro name with space before '(' |
d8662f2f | 710 | && !($1 =~ m/^(sizeof|if|else|while|do|for|switch|case|default|break|continue|goto|return|void|char|signed|unsigned|int|short|long|float|double|typedef|enum|struct|union|auto|extern|static|const|volatile|register)$/) # not keyword |
99a16e04 DDO |
711 | && !(m/^\s*#\s*define\s/); # we skip macro definitions here because macros |
712 | # without parameters but with body beginning with '(', e.g., '#define X (1)', | |
713 | # would lead to false positives - TODO also check for macros with parameters | |
f35a9b6a DDO |
714 | report("no space before '{'") if $intra_line =~ m/[^\s{(\[]\{/; # '{' without preceding space or {([ |
715 | report("no space after '}'") if $intra_line =~ m/\}[^\s,;\])}]/; # '}' without following space or ,;])} | |
99a16e04 DDO |
716 | } |
717 | ||
718 | # preprocessor directives @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | |
719 | ||
720 | # handle preprocessor directives | |
721 | if (m/^\s*#(\s*)(\w+)/) { # line beginning with '#' | |
722 | my $space_count = length($1); # maybe could also use indentation before '#' | |
723 | my $directive = $2; | |
724 | report("indent = $count != 0 for '#'") if $count != 0; | |
725 | $directive_nesting-- if $directive =~ m/^(else|elif|endif)$/; | |
726 | if ($directive_nesting < 0) { | |
727 | $directive_nesting = 0; | |
728 | report("unexpected '#$directive'"); | |
729 | } | |
730 | report("'#' directive nesting = $space_count != $directive_nesting") if $space_count != $directive_nesting; | |
731 | $directive_nesting++ if $directive =~ m/^if|ifdef|ifndef|else|elif$/; | |
732 | $ifdef__cplusplus = m/^\s*#\s*ifdef\s+__cplusplus\s*$/; | |
733 | goto POSTPROCESS_DIRECTIVE unless $directive =~ m/^define$/; # skip normal code handling except for #define | |
734 | # TODO improve handling of indents of preprocessor directives ('\', $in_directive != 0) vs. normal C code | |
735 | $count = -1; # do not check indentation of #define | |
736 | } | |
737 | ||
738 | # adapt required indentation @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | |
739 | ||
740 | s/(\w*ASN1_[A-Z_]+END\w*([^(]|\(.*?\)|$))/$1;/g; # treat *ASN1_*END*(..) macro calls as if followed by ';' | |
741 | ||
742 | my $nested_indents_position = 0; | |
743 | ||
744 | # update indents according to leading closing brace(s) '}' or label or switch case | |
745 | my $in_stmt = $in_expr || @nested_symbols != 0 || $in_typedecl != 0; | |
746 | if ($in_stmt) { # expr/stmt/type decl/var def/fn hdr, i.e., not at block level | |
d8662f2f DDO |
747 | if (m/^([\s@]*\})/) { # leading '}' within stmt, any preceding blinded comment must not be matched |
748 | $in_block_decls = -1; | |
99a16e04 DDO |
749 | my $head = $1; |
750 | update_nested_indents($head); | |
751 | $nested_indents_position = length($head); | |
752 | if (@nested_symbols >= 1) { | |
753 | $hanging_symbol = @nested_symbols[-1]; | |
754 | $expr_indent = @nested_indents[-1]; | |
755 | } else { # typically end of initialiizer expr or enum | |
756 | $expr_indent = 0; | |
757 | } | |
758 | } elsif (m/^([\s@]*)(static_)?ASN1_ITEM_TEMPLATE_END(\W|$)/) { # workaround for ASN1 macro indented as '}' | |
759 | $local_offset = -INDENT_LEVEL; | |
760 | $expr_indent = 0; | |
761 | } elsif (m/;.*?\}/) { # expr ends with ';' before '}' | |
762 | report("code before '}'"); | |
763 | } | |
764 | } | |
765 | if (@in_do_hanging_offsets != 0 && # note there is nothing like "unexpected 'while'" | |
766 | m/^[\s@]*while(\W|$)/) { # leading 'while' | |
767 | $hanging_offset = pop @in_do_hanging_offsets; | |
768 | } | |
769 | if ($if_maybe_terminated) { | |
770 | if (m/(^|\W)else(\W|$)/) { # (not necessarily leading) 'else' | |
771 | if (@in_if_hanging_offsets == 0) { | |
772 | report("unexpected 'else'"); | |
773 | } else { | |
774 | $hanging_offset = pop @in_if_hanging_offsets; | |
775 | } | |
776 | } else { | |
777 | @in_if_hanging_offsets = (); # note there is nothing like "unclosed 'if'" | |
778 | $hanging_offset = 0; | |
779 | } | |
780 | } | |
781 | if (!$in_stmt) { # at block level, i.e., outside expr/stmt/type decl/var def/fn hdr | |
782 | $if_maybe_terminated = 0; | |
783 | if (my ($head, $before, $tail) = m/^([\s@]*([^{}]*)\})[\s@]*(.*)$/) { # leading closing '}', but possibly | |
784 | # with non-whitespace non-'{' before | |
785 | report("code after '}'") unless $tail eq "" || $tail =~ m/(else|while|OSSL_TRACE_END)(\W|$)/; | |
786 | my $outermost_level = @nested_block_indents == 1 && @nested_block_indents[0] == 0; | |
787 | if (!$sloppy_bodylen && $outermost_level && $line_body_start != 0) { | |
788 | my $body_len = $line - $line_body_start - 1; | |
789 | report_flexibly($line_function_start, "function body length = $body_len > ".MAX_BODY_LENGTH." lines", | |
790 | $last_function_header) if $body_len > MAX_BODY_LENGTH; | |
791 | $line_body_start = 0; | |
792 | } | |
793 | if ($before ne "") { # non-whitespace non-'{' before '}' | |
794 | report("code before '}'"); | |
d8662f2f DDO |
795 | } else { # leading '}' outside stmt, any preceding blinded comment must not be matched |
796 | $in_block_decls = -1; | |
99a16e04 DDO |
797 | $local_offset = $block_indent + $hanging_offset - INDENT_LEVEL; |
798 | update_nested_indents($head); | |
799 | $nested_indents_position = length($head); | |
800 | $local_offset -= ($block_indent + $hanging_offset); | |
801 | # in effect $local_offset = -INDENT_LEVEL relative to $block_indent + $hanging_offset values before | |
802 | } | |
803 | } | |
804 | ||
805 | # handle opening brace '{' after if/else/while/for/switch/do on line before | |
806 | if ($hanging_offset > 0 && m/^[\s@]*{/ && # leading opening '{' | |
807 | $line_before > 0 && | |
808 | $contents_before_ =~ m/(^|^.*\W)(if|else|while|for|switch|do)(\W.*$|$)/) { | |
809 | $keyword_opening_brace = $1; | |
810 | $hanging_offset -= INDENT_LEVEL; # cancel newly hanging_offset | |
811 | } | |
812 | ||
813 | if (m/^[\s@]*(case|default)(\W.*$|$)/) { # leading 'case' or 'default' | |
814 | my $keyword = $1; | |
815 | report("code after $keyword: ") if $2 =~ /:.*[^\s@].*$/; | |
816 | $local_offset = -INDENT_LEVEL; | |
817 | } else { | |
818 | if (m/^([\s@]*)(\w+):/) { # (leading) label, cannot be "default" | |
13a574d8 | 819 | $local_offset = -INDENT_LEVEL; |
99a16e04 DDO |
820 | $has_label = 1; |
821 | } | |
822 | } | |
823 | } | |
824 | ||
825 | # potential adaptations of indent in first line of macro body in multi-line macro definition | |
826 | if ($in_directive > 0 && $in_macro_header > 0) { | |
827 | if ($in_macro_header > 1) { # still in macro definition header | |
828 | $in_macro_header += parens_balance($_); | |
829 | } else { # begin of macro body | |
830 | $in_macro_header = 0; | |
831 | if ($count == $block_indent - $directive_offset # body began with same indentation as preceding code | |
832 | && $sloppy_macro) { # workaround for this situation is enabled | |
833 | $block_indent -= $directive_offset; | |
834 | $directive_offset = 0; | |
835 | } | |
836 | } | |
837 | } | |
838 | ||
839 | # check required indentation @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | |
840 | ||
841 | check_indent() if $count >= 0; # not for #define and not if multi-line string literal is continued | |
842 | ||
d8662f2f DDO |
843 | # check for blank lines within/after local decls @@@@@@@@@@@@@@@@@@@@@@@@@@@ |
844 | ||
845 | if ($in_block_decls >= 0 && | |
846 | $in_comment == 0 && !m/^\s*\*?@/ && # not multi-line or intra-line comment | |
847 | !$in_expr && $in_typedecl == 0) { | |
848 | my $blank_line_before = $line > 1 && $code_contents_before =~ m/^\s*(\\\s*)?$/; | |
849 | # essentially blank line: just whitespace (and maybe a trailing '\') | |
850 | if (m/^\s*(void|char|signed|unsigned|int|short|long|float|double|typedef|enum|struct|union|auto|extern|static|const|volatile|register)(\W|$)/ || | |
851 | (m/[\w)]\s+[*]*\w/ && | |
852 | !m/^\s*(\}|sizeof|if|else|while|do|for|switch|case|default|break|continue|goto|return)(\W|$)/)) { | |
853 | report_flexibly($line - 1, "blank line within local decls, before", $contents) if $blank_line_before; | |
854 | $in_block_decls++; | |
855 | } elsif ($in_block_decls > 0) { | |
856 | report_flexibly($line, "missing blank line after local decls", "\n$contents_before$contents") | |
857 | unless $blank_line_before; | |
858 | $in_block_decls = -1; | |
859 | } | |
860 | } | |
861 | ||
99a16e04 DDO |
862 | $in_comment = 0 if $in_comment < 0; # multi-line comment has ended |
863 | ||
864 | # do some further checks @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | |
865 | ||
866 | my $outermost_level = $block_indent == 0 + ($in_directive > 0 ? $directive_offset : 0); | |
867 | ||
868 | report("more than one stmt") if !m/(^|\W)for(\W.*|$)/ && # no 'for' - TODO improve matching | |
869 | m/;.*;/; # two or more terminators ';', so more than one statement | |
870 | ||
871 | # check for code block containing a single line/statement | |
872 | if ($line_before2 > 0 && !$outermost_level && # within function body | |
a77571c3 | 873 | $in_typedecl == 0 && @nested_indents == 0 && # neither within type declaration nor inside stmt/expr |
99a16e04 DDO |
874 | m/^[\s@]*\}/) { # leading closing brace '}', any preceding blinded comment must not be matched |
875 | # TODO extend detection from single-line to potentially multi-line statement | |
876 | if ($line_opening_brace > 0 && | |
877 | ($line_opening_brace == $line_before2 || | |
878 | $line_opening_brace == $line_before) | |
879 | && $contents_before =~ m/;/) { # there is at least one terminator ';', so there is some stmt | |
880 | # TODO do not report cases where a further else branch | |
e304aa87 | 881 | # follows with a block containing more than one line/statement |
99a16e04 DDO |
882 | report_flexibly($line_before, "'$keyword_opening_brace' { 1 stmt }", $contents_before); |
883 | } | |
884 | } | |
885 | ||
ff381033 DDO |
886 | report("single-letter name '$2'") if (m/(^|.*\W)([IO])(\W.*|$)/); # single-letter name 'I' or 'O' # maybe re-add 'l'? |
887 | # constant on LHS of comparison or assignment, e.g., NULL != x or 'a' < c, but not a + 1 == b | |
2e6afe10 | 888 | report("constant on LHS of '$3'") |
15ae69fa DDO |
889 | if (m/(['"]|([\+\-\*\/\/%\&\|\^<>]\s*)?\W[0-9]+L?|\WNULL)\s*([\!<>=]=|[<=>])([<>]?)/ && |
890 | $2 eq "" && (($3 ne "<" && $3 ne "='" && $3 ne ">") || $4 eq "")); | |
ff381033 DDO |
891 | |
892 | # TODO report #if 0 and #if 1 | |
99a16e04 | 893 | |
99a16e04 DDO |
894 | # TODO report needless use of parentheses, while |
895 | # macro parameters should always be in parens (except when passed on), e.g., '#define ID(x) (x)' | |
896 | ||
897 | # adapt required indentation for following lines @@@@@@@@@@@@@@@@@@@@@@@@@@@ | |
898 | ||
899 | # set $in_expr, $in_paren_expr, and $hanging_offset for if/while/for/switch, return/enum, and assignment RHS | |
900 | my $paren_expr_start = 0; | |
901 | my $return_enum_start = 0; | |
902 | my $assignment_start = 0; | |
903 | my $tmp = $_; | |
904 | $tmp =~ s/[\!<>=]=/@@/g; # blind (in-)equality symbols like '<=' as '@@' to prevent matching them as '=' below | |
905 | if (m/^((^|.*\W)(if|while|for|switch))(\W.*|$)$/) { # (last) if/for/while/switch | |
906 | $paren_expr_start = 1; | |
907 | } elsif (m/^((^|.*\W)(return|enum))(\W.*|$)/ # (last) return/enum | |
908 | && !$in_expr && @nested_indents == 0 && parens_balance($1) == 0) { # not nested enum | |
909 | $return_enum_start = 1; | |
910 | } elsif ($tmp =~ m/^(([^=]*)(=))(.*)$/ # (last) '=', i.e., assignment | |
911 | && !$in_expr && @nested_indents == 0 && parens_balance($1) == 0) { # not nested assignment | |
912 | $assignment_start = 1; | |
913 | } | |
914 | if ($paren_expr_start || $return_enum_start || $assignment_start) | |
915 | { | |
916 | my ($head, $mid, $tail) = ($1, $3, $4); | |
38e49781 DDO |
917 | $keyword_opening_brace = $mid if $mid ne "="; |
918 | # to cope with multi-line expressions, do this also if !($tail =~ m/\{/) | |
99a16e04 DDO |
919 | push @in_if_hanging_offsets, $hanging_offset if $mid eq "if"; |
920 | ||
921 | # already handle $head, i.e., anything before expression | |
922 | update_nested_indents($head, $nested_indents_position); | |
923 | $nested_indents_position = length($head); | |
924 | # now can set $in_expr and $in_paren_expr | |
925 | $in_expr = 1; | |
926 | $in_paren_expr = 1 if $paren_expr_start; | |
927 | if ($mid eq "while" && @in_do_hanging_offsets != 0) { | |
928 | $hanging_offset = pop @in_do_hanging_offsets; | |
929 | } else { | |
930 | $hanging_offset += INDENT_LEVEL; # tentatively set hanging_offset, may be canceled by following '{' | |
931 | } | |
932 | } | |
933 | ||
934 | # set $hanging_offset and $keyword_opening_brace for do/else | |
935 | if (my ($head, $mid, $tail) = m/(^|^.*\W)(else|do)(\W.*|$)$/) { # last else/do, where 'do' is preferred | |
936 | my $code_before = $head =~ m/[^\s\@}]/; # leading non-whitespace non-comment non-'}' | |
937 | report("code before '$mid'") if $code_before; | |
938 | report("code after '$mid'" ) if $tail =~ m/[^\s\@{]/# trailing non-whitespace non-comment non-'{' (non-'\') | |
939 | && !($mid eq "else" && $tail =~ m/[\s@]*if(\W|$)/); | |
940 | if ($mid eq "do") { # workarounds for code before 'do' | |
941 | if ($head =~ m/(^|^.*\W)(else)(\W.*$|$)/) { # 'else' ... 'do' | |
942 | $hanging_offset += INDENT_LEVEL; # tentatively set hanging_offset, may be canceled by following '{' | |
943 | } | |
944 | if ($head =~ m/;/) { # terminator ';' ... 'do' | |
945 | @in_if_hanging_offsets = (); # note there is nothing like "unclosed 'if'" | |
946 | $hanging_offset = 0; | |
947 | } | |
948 | } | |
949 | push @in_do_hanging_offsets, $hanging_offset if $mid eq "do"; | |
950 | if ($code_before && $mid eq "do") { | |
951 | $hanging_offset = length($head) - $block_indent; | |
952 | } | |
953 | if (!$in_paren_expr) { | |
954 | $keyword_opening_brace = $mid if $tail =~ m/\{/; | |
955 | $hanging_offset += INDENT_LEVEL; | |
956 | } | |
957 | } | |
958 | ||
959 | # set $in_typedecl and potentially $hanging_offset for type declaration | |
d3fc80ab | 960 | if (!$in_expr && @nested_indents == 0 # not in expression |
d8662f2f | 961 | && m/(^|^.*\W)(typedef|enum|struct|union)(\W.*|$)$/ |
d3fc80ab DDO |
962 | && parens_balance($1) == 0 # not in newly started expression or function arg list |
963 | && ($2 eq "typedef" || !($3 =~ m/\s*\w++\s*(.)/ && $1 ne "{")) # 'struct'/'union'/'enum' <name> not followed by '{' | |
964 | # not needed: && $keyword_opening_brace = $2 if $3 =~ m/\{/; | |
965 | ) { | |
99a16e04 DDO |
966 | $in_typedecl++; |
967 | $hanging_offset += INDENT_LEVEL if m/\*.*\(/; # '*' followed by '(' - seems consistent with Emacs C mode | |
968 | } | |
969 | ||
970 | my $bak_in_expr = $in_expr; | |
971 | my $terminator_position = update_nested_indents($_, $nested_indents_position); | |
972 | ||
973 | if ($bak_in_expr) { | |
974 | # on end of non-if/while/for/switch (multi-line) expression (i.e., return/enum/assignment) and | |
975 | # on end of statement/type declaration/variable definition/function header | |
976 | if ($terminator_position >= 0 && ($in_typedecl == 0 || @nested_indents == 0)) { | |
977 | check_nested_nonblock_indents("expr"); | |
978 | $in_expr = 0; | |
979 | } | |
980 | } else { | |
981 | check_nested_nonblock_indents($in_typedecl == 0 ? "stmt" : "decl") if $terminator_position >= 0; | |
982 | } | |
983 | ||
984 | # on ';', which terminates the current statement/type declaration/variable definition/function declaration | |
985 | if ($terminator_position >= 0) { | |
986 | my $tail = substr($_, $terminator_position + 1); | |
987 | if (@in_if_hanging_offsets != 0) { | |
988 | if ($tail =~ m/\s*else(\W|$)/) { | |
989 | pop @in_if_hanging_offsets; | |
990 | $hanging_offset -= INDENT_LEVEL; | |
991 | } elsif ($tail =~ m/[^\s@]/) { # code (not just comment) follows | |
992 | @in_if_hanging_offsets = (); # note there is nothing like "unclosed 'if'" | |
993 | $hanging_offset = 0; | |
994 | } else { | |
995 | $if_maybe_terminated = 1; | |
996 | } | |
997 | } elsif ($tail =~ m/^[\s@]*$/) { # ';' has been trailing, i.e. there is nothing but whitespace and comments | |
998 | $hanging_offset = 0; # reset in case of terminated assignment ('=') etc. | |
999 | } | |
1000 | $in_typedecl-- if $in_typedecl != 0 && @nested_in_typedecl == 0; # TODO handle multiple type decls per line | |
1001 | m/(;[^;]*)$/; # match last ';' | |
1002 | $terminator_position = length($_) - length($1) if $1; | |
1003 | # new $terminator_position value may be after the earlier one in case multiple terminators on current line | |
1004 | # TODO check treatment in case of multiple terminators on current line | |
1005 | update_nested_indents($_, $terminator_position + 1); | |
1006 | } | |
1007 | ||
1008 | # set hanging expression indent according to nested indents - TODO maybe do better in update_nested_indents() | |
1009 | # also if $in_expr is 0: in statement/type declaration/variable definition/function header | |
1010 | $expr_indent = 0; | |
1011 | for (my $i = -1; $i >= -@nested_symbols; $i--) { | |
1012 | if (@nested_symbols[$i] ne "?") { # conditionals '?' ... ':' are treated specially in check_indent() | |
1013 | $hanging_symbol = @nested_symbols[$i]; | |
1014 | $expr_indent = $nested_indents[$i]; | |
1015 | # $expr_indent is guaranteed to be != 0 unless @nested_indents contains just outer conditionals | |
1016 | last; | |
1017 | } | |
1018 | } | |
1019 | ||
1020 | # remember line number and header containing name of last function defined for reports w.r.t. MAX_BODY_LENGTH | |
1021 | if ($outermost_level && m/(\w+)\s*\(/ && $1 ne "STACK_OF") { | |
1022 | $line_function_start = $line; | |
1023 | $last_function_header = $contents; | |
1024 | } | |
1025 | ||
1026 | # special checks for last, typically trailing opening brace '{' in line | |
1027 | if (my ($head, $tail) = m/^(.*)\{(.*)$/) { # match last ... '{' | |
1028 | if ($in_directive == 0 && !$in_expr && $in_typedecl == 0) { | |
1029 | if ($outermost_level) { | |
1030 | if (!$assignment_start && !$bak_in_expr) { | |
1031 | # at end of function definition header (or stmt or var definition) | |
1032 | report("'{' not at beginning") if $head ne ""; | |
1033 | $line_body_start = $contents =~ m/LONG BODY/ ? 0 : $line; | |
1034 | } | |
1035 | } else { | |
1036 | $line_opening_brace = $line if $keyword_opening_brace =~ m/do|while|for/; | |
1037 | # using, not assigning, $keyword_opening_brace here because it could be on an earlier line | |
1038 | $line_opening_brace = $line if $keyword_opening_brace =~ m/if|else/ && $extended_1_stmt && | |
1039 | # TODO prevent false positives for if/else where braces around single-statement branches | |
1040 | # should be avoided but only if all branches have just single statements | |
1041 | # The following helps detecting the exception when handling multiple 'if ... else' branches: | |
1042 | !($keyword_opening_brace eq "else" && $line_opening_brace < $line_before2); | |
1043 | } | |
1044 | report("code after '{'") if $tail=~ m/[^\s\@]/ && # trailing non-whitespace non-comment (non-'\') | |
1045 | !($tail=~ m/\}/); # no '}' after last '{' | |
1046 | } | |
1047 | } | |
1048 | ||
1049 | # check for opening brace after if/while/for/switch/do not on same line | |
1050 | # note that "no '{' on same line after '} else'" is handled further below | |
1051 | if (/^[\s@]*{/ && # leading '{' | |
90a7c905 | 1052 | $line_before > 0 && !($contents_before_ =~ m/^\s*#/) && # not preprocessor directive '#if |
99a16e04 DDO |
1053 | (my ($head, $mid, $tail) = ($contents_before_ =~ m/(^|^.*\W)(if|while|for|switch|do)(\W.*$|$)/))) { |
1054 | my $brace_after = $tail =~ /^[\s@]*{/; # any whitespace or comments then '{' | |
1055 | report("'{' not on same line as preceding '$mid'") if !$brace_after; | |
1056 | } | |
1057 | # check for closing brace on line before 'else' not followed by leading '{' | |
1058 | elsif (my ($head, $tail) = m/(^|^.*\W)else(\W.*$|$)/) { | |
1059 | if (parens_balance($tail) == 0 && # avoid false positive due to unfinished expr on current line | |
1060 | !($tail =~ m/{/) && # after 'else' no '{' on same line | |
1061 | !($head =~ m/}[\s@]*$/) && # not: '}' then any whitespace or comments before 'else' | |
1062 | $line_before > 0 && $contents_before_ =~ /}[\s@]*$/) { # trailing '}' on line before | |
1063 | report("no '{' after '} else'"); | |
1064 | } | |
1065 | } | |
1066 | ||
1067 | # check for closing brace before 'while' not on same line | |
1068 | if (my ($head, $tail) = m/(^|^.*\W)while(\W.*$|$)/) { | |
1069 | my $brace_before = $head =~ m/}[\s@]*$/; # '}' then any whitespace or comments | |
1070 | # possibly 'if (...)' (with potentially inner '(' and ')') then any whitespace or comments then '{' | |
1071 | if (!$brace_before && | |
1072 | # does not work here: @in_do_hanging_offsets != 0 && #'while' terminates loop | |
1073 | parens_balance($tail) == 0 && # avoid false positive due to unfinished expr on current line | |
1074 | $tail =~ /;/ && # 'while' terminates loop (by ';') | |
1075 | $line_before > 0 && | |
1076 | $contents_before_ =~ /}[\s@]*$/) { # on line before: '}' then any whitespace or comments | |
1077 | report("'while' not on same line as preceding '}'"); | |
1078 | } | |
1079 | } | |
1080 | ||
1081 | # check for missing brace on same line before or after 'else' | |
1082 | if (my ($head, $tail) = m/(^|^.*\W)else(\W.*$|$)/) { | |
1083 | my $brace_before = $head =~ /}[\s@]*$/; # '}' then any whitespace or comments | |
1084 | my $brace_after = $tail =~ /^[\s@]*if[\s@]*\(.*\)[\s@]*{|[\s@]*{/; | |
1085 | # possibly 'if (...)' (with potentially inner '(' and ')') then any whitespace or comments then '{' | |
1086 | if (!$brace_before) { | |
1087 | if ($line_before > 0 && $contents_before_ =~ /}[\s@]*$/) { | |
1088 | report("'else' not on same line as preceding '}'"); | |
1089 | } elsif (parens_balance($tail) == 0) { # avoid false positive due to unfinished expr on current line | |
1090 | report("no '}' on same line before 'else ... {'") if $brace_after; | |
1091 | } | |
1092 | } elsif (parens_balance($tail) == 0) { # avoid false positive due to unfinished expr on current line | |
1093 | report("no '{' on same line after '} else'") if $brace_before && !$brace_after; | |
1094 | } | |
1095 | } | |
1096 | ||
1097 | POSTPROCESS_DIRECTIVE: | |
1098 | # on begin of multi-line preprocessor directive, adapt indent | |
1099 | # need to use original line contents because trailing '\' may have been stripped above | |
1100 | if ($contents =~ m/^(.*?)[\s@]*\\[\s@]*$/) { # trailing '\' (which is not stripped from $contents), | |
1101 | # typically used in macro definitions (or other preprocessor directives) | |
1102 | if ($in_directive == 0) { | |
1103 | $in_macro_header = m/^\s*#\s*define(\W|$)?(.*)/ ? 1 + parens_balance($2) : 0; # '#define' is beginning | |
1104 | $directive_offset = INDENT_LEVEL; | |
1105 | $block_indent += $directive_offset; | |
1106 | } | |
1107 | $in_directive += 1; | |
1108 | } | |
1109 | ||
1110 | # post-processing at end of line @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | |
1111 | ||
1112 | LINE_FINISHED: | |
d8662f2f DDO |
1113 | $code_contents_before = $contents if |
1114 | !m/^\s*#(\s*)(\w+)/ && # not single-line directive | |
1115 | $in_comment == 0 && !m/^\s*\*?@/; # not multi-line or intra-line comment | |
1116 | ||
99a16e04 DDO |
1117 | # on end of multi-line preprocessor directive, adapt indent |
1118 | if ($in_directive > 0 && | |
1119 | # need to use original line contents because trailing \ may have been stripped | |
1120 | !($contents =~ m/^(.*?)[\s@]*\\[\s@]*$/)) { # no trailing '\' | |
1121 | $block_indent -= $directive_offset; | |
1122 | $in_directive = 0; | |
1123 | # macro body typically does not include terminating ';' | |
1124 | $hanging_offset = 0; # compensate for this in case macro ends, e.g., as 'while (0)' | |
1125 | } | |
1126 | ||
d8662f2f DDO |
1127 | if (m/^\s*$/) { # at begin of file essentially blank line: just whitespace (and maybe a '\') |
1128 | report("leading ".($1 eq "" ? "blank" :"whitespace")." line") if $line == 1 && !$sloppy_SPC; | |
dc187815 | 1129 | } else { |
43b2e9e0 DDO |
1130 | if ($line_before > 0) { |
1131 | my $linediff = $line - $line_before - 1; | |
d8662f2f | 1132 | report("$linediff blank lines before") if $linediff > 1 && !$sloppy_SPC; |
43b2e9e0 | 1133 | } |
99a16e04 DDO |
1134 | $line_before2 = $line_before; |
1135 | $contents_before2 = $contents_before; | |
1136 | $contents_before_2 = $contents_before_; | |
1137 | $line_before = $line; | |
1138 | $contents_before = $contents; | |
1139 | $contents_before_ = $_; | |
1140 | $count_before = $count; | |
1141 | } | |
1142 | ||
1143 | if ($self_test) { # debugging | |
1144 | my $should_report = $contents =~ m/\*@(\d)?/ ? 1 : 0; | |
1145 | $should_report = +$1 if $should_report != 0 && defined $1; | |
1146 | print("$ARGV:$line:$num_reports_line reports on:$contents") | |
1147 | if $num_reports_line != $should_report; | |
1148 | } | |
1149 | $num_reports_line = 0; | |
1150 | ||
1151 | # post-processing at end of file @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | |
1152 | ||
1153 | if (eof) { | |
d8662f2f DDO |
1154 | # check for essentially blank line (which may include a '\') just before EOF |
1155 | report(($1 eq "\n" ? "blank line" : $2 ne "" ? "'\\'" : "whitespace")." at EOF") | |
174f4a4d | 1156 | if $contents =~ m/^(\s*(\\?)\s*)$/ && !$sloppy_SPC; |
99a16e04 DDO |
1157 | |
1158 | # report unclosed expression-level nesting | |
1159 | check_nested_nonblock_indents("expr at EOF"); # also adapts @nested_block_indents | |
1160 | ||
1161 | # sanity-check balance of block-level { ... } via final $block_indent at end of file | |
1162 | report_flexibly($line, +@nested_block_indents." unclosed '{'", "(EOF)\n") if @nested_block_indents != 0; | |
1163 | ||
1164 | # sanity-check balance of #if ... #endif via final preprocessor directive indent at end of file | |
1165 | report_flexibly($line, "$directive_nesting unclosed '#if'", "(EOF)\n") if $directive_nesting != 0; | |
1166 | ||
1167 | reset_file_state(); | |
1168 | } | |
1169 | } | |
1170 | ||
1171 | # final summary report @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | |
1172 | ||
1173 | my $num_other_reports = $num_reports - $num_indent_reports - $num_nesting_issues | |
1174 | - $num_syntax_issues - $num_SPC_reports - $num_length_reports; | |
1175 | print "$num_reports ($num_indent_reports indentation, $num_nesting_issues directive nesting, ". | |
1176 | "$num_syntax_issues syntax, $num_SPC_reports whitespace, $num_length_reports length, $num_other_reports other)". | |
1177 | " issues have been found by $0\n" if $num_reports != 0 && !$self_test; |