Lines Matching +full:non +full:- +full:comment
3 # Copyright 2020-2025 The OpenSSL Project Authors. All Rights Reserved.
4 # Copyright Siemens AG 2019-2022
11 # check-format.pl
12 # - check formatting of C source according to OpenSSL coding style
15 # check-format.pl [-l|--strict-len] [-b|--sloppy-bodylen]
16 # [-s|--sloppy-space] [-c|--sloppy-comment]
17 # [-m|--sloppy-macro] [-h|--sloppy-hang]
18 # [-e|--eol-comment] [-1|--1-stmt]
21 # run self-tests:
22 # util/check-format.pl util/check-format-test-positives.c
23 # util/check-format.pl util/check-format-test-negatives.c
31 # -l | --strict-len decrease accepted max line length from 100 to 80
32 # -b | --sloppy-bodylen do not report function body length > 200
33 # -s | --sloppy-space do not report whitespace nits
34 # -c | --sloppy-comment do not report indentation of comments
35 # Otherwise for each multi-line comment the indentation of
36 # its lines is checked for consistency. For each comment
41 # -m | --sloppy-macro allow missing extra indentation of macro bodies
42 # -h | --sloppy-hang when checking hanging indentation, do not report
44 # * same indentation as non-hanging indent level
45 # * indentation moved left (not beyond non-hanging indent)
47 # -e | --eol-comment report needless intermediate multiple consecutive spaces also before end-o…
48 # -1 | --1-stmt do more aggressive checks for { 1 stmt } - see below
50 # There are non-trivial false positives and negatives such as the following.
67 # Yet with the --1-stmt option false positives are preferred over negatives.
68 # False negatives occur if the braces are more than two non-blank lines apart.
71 # except when this is before end-of-line comments (unless the --eol-comment is given) and
76 # This pattern is recognized - and consequently extra space not reported -
77 # for a given line if in the non-blank line before or after (if existing)
78 # for each occurrence of " \S" (where \S means non-space) in the given line
81 # and false positives (in case of more complex multi-column alignment).
103 # command-line options
113 while ($ARGV[0] =~ m/^-(\w|-[\w\-]+)$/) {
115 if ($arg =~ m/^(l|-strict-len)$/) {
117 } elsif ($arg =~ m/^(b|-sloppy-bodylen)$/) {
119 } elsif ($arg =~ m/^(s|-sloppy-space)$/) {
121 } elsif ($arg =~ m/^(c|-sloppy-comment)$/) {
123 } elsif ($arg =~ m/^(m|-sloppy-macro)$/) {
125 } elsif ($arg =~ m/^(h|-sloppy-hang)$/) {
127 } elsif ($arg =~ m/^(e|-eol-comment)$/) {
129 } elsif ($arg =~ m/^(1|-1-stmt)$/) {
132 die("unknown option: -$arg");
137 … # whether the current input file is regarded to contain (positive/negative) self-tests
139 my $in_comment; # number of lines so far within multi-line comment, 0 if no comment, < 0…
140 my $leading_comment; # multi-line comment has no code before its beginning delimiter, if $in_…
141 my $formatted_comment; # multi-line comment beginning with "/*-", which indicates/allows specia…
142 my $comment_indent; # comment indent, if $in_comment != 0
148 my $preproc_offset; # offset to $block_indent within multi-line preprocessor directive, else…
149 my $in_macro_header; # number of open parentheses + 1 in (multi-line) header of #define, if $…
158 my $code_contents_before; # contents of previous non-comment non-preprocessor-directive line (with…
163 my $in_multiline_string; # line starts within multi-line string literal
164 my $count; # -1 or number of leading whitespace characters (except newline) in curr…
172 …us line with opening brace after if/do/while/for, partly for 'else/else if' - used for detection o…
186 my $expr_indent; # resulting hanging indent within (multi-line) expressions including typ…
188 … of local declaration lines after block opening before normal statements, or -1 if no block opening
220 $in_block_decls = -1;
324 sub parens_balance { # count balance of opening parentheses - closing parentheses
326 return $str =~ tr/\(// - $str =~ tr/\)//;
329 sub blind_nonspace { # blind non-space text of comment as @, preserving length and spaces
331 …# comment text is not blinded to whitespace in order to be able to check extra SPC also in comments
339 sub check_indent { # used for lines outside multi-line string literals
344 m/^\s*\/\*/ ? "intra-line comment" :
352 # allow indent 1 for labels - this cannot happen for leading ':'
356 … # leading ':' within stmt/expr/decl - this cannot happen for labels, leading '&&', or leading '||'
358 ($alt_desc, $alt_indent) = ("leading ':'", @nested_conds_indents[-1]);
360 # allow extra indent offset leading '&&' or '||' - this cannot happen for leading ":"
363 …ndent < 0) { # implies @nested_symbols != 0 && @nested_symbols[0] eq "{" && @nested_indents[-1] < 0
368 …@nested_indents[-1] = $count == $stmt_indent ? $stmt_indent : -@nested_indents[-1]; # allow $stmt_…
369 $ref_indent = $expr_indent = @nested_indents[-1];
372 …# check consistency of indentation within multi-line comment (i.e., between its first, inner, and …
373 if ($in_comment != 0 && $in_comment != 1) { # in multi-line comment but not on its first line
376 report("indent = $count != $comment_indent within multi-line comment")
379 my $tweak = $in_comment == -2 ? 1 : 0;
380 … report("indent = ".($count + $tweak)." != $comment_indent at end of multi-line comment")
384 # do not check indentation of last line of non-leading multi-line comment
390 # $comment_indent will be checked by the below checks for end of multi-line comment
393 # else check indentation of entire-line comment or entire-line end of multi-line comment
395 …f (($in_comment == 0 || $in_comment == 1) # no comment, intra-line comment, or begin of multi-line…
398 …report_flexibly($line_before, "entire-line comment indent = $count_before != $count (of following …
399 $contents_before) if !$sloppy_cmt && $count_before != -1 && $count_before != $count;
402 …if (($in_comment == 0 || $in_comment < 0) # (no comment,) intra-line comment or end of multi-line …
408 return if !eof; # defer check of entire-line comment to next line
411 # else check indentation of leading intra-line comment or end of multi-line comment
412 if (m/^(\s*)@/) { # line begins with '@', i.e., any (remaining type of) comment
414 report("intra-line comment indent = $count != $ref_indent") if $in_comment == 0;
415 report("multi-line comment indent = $count != $ref_indent") if $in_comment < 0;
445 my $terminator_position = -1;
455 $i += length($1) + length($2) - 1;
464 … # cancel newly hanging_offset if opening brace '{' is after non-whitespace non-comment:
465 $hanging_offset -= INDENT_LEVEL if $hanging_offset > 0 && $head =~ m/[^\s\@]/;
477 ? $i + 1 + length($1) # actual indentation of following non-space non-comment
479 … : -($i + 1); # allow also $stmt_indent if '{' with only whitespace thereafter
489 # # multi-line expr after 'case'
496 … @nested_symbols[-1] == $opening_c) { # for $c there was a corresponding $opening_c
518 … if $tail =~ m/^([^{]*)/ && $1 =~ m/[^\s\@;]/; # non-space non-';' before any '{'
523 return -1;
546 $self_test = $ARGV =~ m/check-format-test/;
553 if (m/(.*?)([\x00-\x09\x0B-\x1F\x7F-\xFF])/) {
555 report(($2 eq "\x09" ? "TAB" : $2 eq "\x0D" ? "CR " : $2 =~ m/[\x00-\x1F]/ ? "non-printable"
556 : "non-7bit char") . " at column $col") ;
573 # handle multi-line string literals to avoid confusion on starting/ending '"' and trailing '\'
577 $count = -1; # do not check indentation
579 report("multi-line string literal not terminated by '\"' and trailing '\' is missing")
586 # this prevents confusing any of the matching below, e.g., of whitespace and comment delimiters
587 s#('[^']*')#$1 =~ tr/'/@/cr#eg; # handle all intra-line character literals
588 s#("[^"]*")#$1 =~ tr/"/@/cr#eg; # handle all intra-line string literals
599 # do/prepare checks within multi-line comments
601 if ($in_comment > 0) { # this still includes the last line of multi-line comment
604 …report("missing space or '*' after leading '*' in multi-line comment") if $cmt_text =~ m|^[^*\s/$s…
606 report("missing leading '*' in multi-line comment");
611 …# detect end of comment, must be within multi-line comment, check if it is preceded by non-whitesp…
612 if ((my ($head, $tail) = m|^(.*?)\*/(.*)$|) && $1 ne '/') { # ending comment: '*/'
615 if (!($head =~ m|/\*|)) { # not begin of comment '/*', which is is handled below
617 report("unexpected '*/' outside comment");
620 … report("text before '*/' in multi-line comment") if ($head =~ m/[^*\s]/); # non-SPC before '*/'
621 $in_comment = -1; # indicate that multi-line comment ends on current line
623 … # make indentation of end of multi-line comment appear like of leading intra-line comment
625 $count--;
626 … $in_comment = -2; # indicate that multi-line comment ends on current line, with tweak
634 # detect begin of comment, check if it is followed by non-space text
636 if (my ($head, $opt_minus, $tail) = m|^(.*?)/\*(-?)(.*)$|) { # begin of comment: '/*'
638 … $head =~ m/[^\s(\*]$/; # not space, '(', or or '*' (needed to allow '*/') before comment delimiter
642 report("unexpected '/*' inside multi-line comment");
643 } elsif ($tail =~ m|^(.*?)\*/(.*)$|) { # comment end: */ on same line
644 report("unexpected '/*' inside intra-line comment") if $1 =~ /\/\*/;
645 # blind comment text, preserving length and spaces
649 } else { # begin of multi-line comment
651 report("text after '/*' in multi-line comment")
659 $formatted_comment = $opt_minus eq "-";
661 } elsif (($head, $tail) = m|^\{-(.*)$|) { # begin of Perl pragma: '{-'
664 if ($in_comment > 1) { # still inside multi-line comment (not at its begin or end)
676 # check for over-long lines,
677 # while allowing trailing (also multi-line) string literals to go past $max_length
682 # this allows over-long trailing string literals with beginning col before $max_length
687 # handle C++ / C99 - style end-of-line comments
689 report("'//' end-of-line comment"); # the '//' comment style is not allowed for C90
690 # blind comment text, preserving length and spaces
694 # at this point all non-space portions of any types of comments have been blinded as @
701 # blank these portions to prevent confusion with C-level 'if', 'else', etc.
707 report("preprocessor directive within multi-line directive");
713 if $preproc_directive =~ m/^(if|elif)$/ && m/^[\W0-9]+$/ && !$trailing_backslash;
714 $preproc_if_nesting-- if $preproc_directive =~ m/^(else|elif|endif)$/;
725 $count = -1; # do not check indentation of first line of preprocessor directive
730 # intra-line whitespace nits @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
737 $in_comment != 0 ? " in multi-line comment"
738 : " in intra-line comment" : "");
740 …# split line contents into header containing leading spaces and the first non-space char, and the …
742 … $in_comment != 0 ? "@" : ""; # '@' will match the blinded leading '*' in multi-line comment
744 …# do not check for extra SPC in leading spaces including any '#' (or '*' within multi-line comment)
754 my $head = shift; # leading spaces and the first non-space char
757 … # check if all extra SPC in $intra is used only for multi-line column alignment with $contents
759 for (my $col = 0; $col < length($intra) - 2; $col++) {
762 next if !$eol_cmt && $substr =~ m/^[@\s]+$/; # end-of-line comment
780 || ($intra_line =~ m/(\S)([\+\-\*\/\/%\&\|\^\!<>=]=)/
783 && !($1 =~ m/[\+\-\*\/\/%\&\|\^\!<>=]/)
786 $intra_line =~ s/(<<|>>|[\+\-\*\/\/%\&\|\^\!<>=])=/=/g;
788 $intra_line =~ s/[A-Z_]+/int/g if $trailing_backslash;
806 …report("space before '$1'") if $intra_line =~ m/[\w)\]]\s+(\+\+|--)/; # postfix ++/-- with preced…
807 …report("space after '$1'") if $intra_line =~ m/(\+\+|--)\s+[a-zA-Z_(]/; # prefix ++/-- with follo…
809 …report("space before '$1'") if $intra_line =~ m/\s(\.|->)/; # '.' or '->' with precedin…
810 …report("space after '$1'") if $intra_line =~ m/(\.|->)\s/; # '.' or '->' with followin…
811 $intra_line =~ s/\-\>|\+\+|\-\-/@/g; # blind '->,', '++', and '--'
819 …ing space before binary '$2'") if $intra_line =~ m/([^\s{()\[e])([+\-])/; # '+'/'-' without prece…
820 …# ')' may be used for type casts or before "->", 'e' may be used for numerical literals such as "1…
825 …report("missing space after binary '$1'") if $intra_line=~m/[^{(\[]([*])[^\sa-zA-Z_(),*]/;# '*' w/…
827 …report("missing space after binary '$1'") if $intra_line=~m/([&])[^\sa-zA-Z_(]/; # '&' w/o follow…
829 …report("missing space after binary '$1'") if $intra_line=~m/[^{(\[]([+\-])[^\s\d(]/; # +/- w/o fo…
830 # TODO unary '+' and '-' must not be followed by SPC
844 …s/(\w*ASN1_[A-Z_]+END\w*([^(]|\(.*?\)|$))/$1;/g; # treat *ASN1_*END*(..) macro calls as if followe…
851 … if (m/^([\s@]*\})/) { # leading '}' within stmt, any preceding blinded comment must not be matched
852 $in_block_decls = -1;
857 $hanging_symbol = @nested_symbols[-1];
858 $expr_indent = @nested_indents[-1];
863 $local_offset = -INDENT_LEVEL;
888 … # with non-whitespace non-'{' before
892 my $body_len = $line - $line_body_start - 1;
897 if ($before ne "") { # non-whitespace non-'{' before '}'
899 } else { # leading '}' outside stmt, any preceding blinded comment must not be matched
900 $in_block_decls = -1;
901 $local_offset = $block_indent + $hanging_offset - INDENT_LEVEL;
904 $local_offset -= ($block_indent + $hanging_offset);
905 …# in effect $local_offset = -INDENT_LEVEL relative to $block_indent + $hanging_offset values before
914 $hanging_offset -= INDENT_LEVEL; # cancel newly hanging_offset
921 $local_offset = -INDENT_LEVEL;
924 $local_offset = -INDENT_LEVEL;
930 # potential adaptations of indent in first line of macro body in multi-line macro definition
936 … if ($count == $block_indent - $preproc_offset # body began with same indentation as preceding code
938 $block_indent -= $preproc_offset;
946 …check_indent() if $count >= 0; # not for start of preprocessor directive and not if multi-line str…
951 $in_comment == 0 && !m/^\s*\*?@/ && # not in a multi-line or intra-line comment
956 …)]))+?\s+[\*\(]*\w+(\s*(\)|\[[^\]]*\]))*\s*[;,=]/ # weak check for decl involving user-defined type
959 …report_flexibly($line - 1, "blank line within local decls, before", $contents) if $blank_line_befo…
963 $in_block_decls = -1 unless
965 || $in_comment != 0 || m/^\s*\*?@/; # in multi-line comment or an intra-line comment
969 $in_comment = 0 if $in_comment < 0; # multi-line comment has ended
973 my $outermost_level = $block_indent - $preproc_offset == 0;
975 …stmt") if !m/(^|\W)(for|(OSSL_)?LIST_FOREACH(_\w+)?)(\W.*|$)/ && # no 'for' - TODO improve matching
981 …m/^[\s@]*\}\s*(\w*)/) { # leading closing brace '}', any preceding blinded comment must not be mat…
982 # TODO extend detection from single-line to potentially multi-line statement
986 $extended_1_stmt || $next_word ne "else") && # --1-stmt or 'if' without 'else'
996 …report("single-letter name '$2'") if (m/(^|.*\W)([IO])(\W.*|$)/); # single-letter name 'I' or 'O' …
999 if (m/(['"]|([\+\-\*\/\/%\&\|\^<>]\s*)?\W[0-9]+L?|\WNULL)\s*([\!<>=]=|[<=>])([<>]?)/ &&
1012 …$tmp =~ s/[\!<>=]=/@@/g; # blind (in-)equality symbols like '<=' as '@@' to prevent matching them …
1026 …&& $mid eq "if" && !$extended_1_stmt; # prevent reporting "{ 1 stmt }" on "else if" unless --1-stmt
1027 # to cope with multi-line expressions, do this also if !($tail =~ m/\{/)
1045 my $code_before = $head =~ m/[^\s\@}]/; # leading non-whitespace non-comment non-'}'
1047 …"code after '$mid'" ) if $tail =~ m/[^\s\@{]/# trailing non-whitespace non-comment non-'{' (non-'\…
1060 $hanging_offset = length($head) - $block_indent;
1076 …$hanging_offset += INDENT_LEVEL if m/\*.*\(/; # '*' followed by '(' - seems consistent with Emacs …
1083 … # on end of non-if/while/for/switch (multi-line) expression (i.e., return/enum/assignment) and
1099 $hanging_offset -= INDENT_LEVEL;
1100 } elsif ($tail =~ m/[^\s@]/) { # code (not just comment) follows
1109 …$in_typedecl-- if $in_typedecl != 0 && @nested_in_typedecl == 0; # TODO handle multiple type decls…
1111 $terminator_position = length($_) - length($1) if $1;
1117 …# set hanging expression indent according to nested indents - TODO maybe do better in update_neste…
1120 for (my $i = -1; $i >= -@nested_symbols; $i--) {
1148 … # TODO prevent false positives for if/else where braces around single-statement branches
1153 … report("code after '{'") if $tail=~ m/[^\s\@]/ && # trailing non-whitespace non-comment (non-'\')
1206 # on begin of multi-line preprocessor directive, adapt indent
1209 if ($in_preproc == 1) { # start of multi-line preprocessor directive
1218 # post-processing at end of line @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1222 !m/^\s*#(\s*)(\w+)/ && # not single-line preprocessor directive
1223 $in_comment == 0 && !m/^\s*\*?@/; # not in a multi-line comment nor in an intra-line comment
1225 # on end of (possibly multi-line) preprocessor directive, adapt indent
1236 my $linediff = $line - $line_before - 1;
1256 # post-processing at end of file @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1263 # report unclosed expression-level nesting
1266 # sanity-check balance of block-level { ... } via final $block_indent at end of file
1269 … # sanity-check balance of #if ... #endif via final preprocessor directive indent at end of file
1278 my $num_other_reports = $num_reports - $num_indent_reports - $num_nesting_issues
1279 - $num_syntax_issues - $num_SPC_reports - $num_length_reports;