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