Lines Matching refs:line

31 #  -l | --sloppy-len     increase accepted max line length from 80 to 84
35 # Otherwise for each multi-line comment the indentation of
40 # refer to the following line and may be indented equally.
43 # * same indentation as on line before
46 # just to fit contents within the line length limit
47 # -e | --eol-comment report needless intermediate multiple consecutive spaces also before end-of-line comments
52 # * When a line contains several issues of the same kind only one is reported.
54 # * When a line contains more than one statement this is (correctly) reported
70 # except when this is before end-of-line comments (unless the --eol-comment is given) and
76 # for a given line if in the non-blank line before or after (if existing)
77 # for each occurrence of " \S" (where \S means non-space) in the given line
78 # there is " \S" in the other line in the respective column position.
101 # command-line options
137 my $in_comment; # number of lines so far within multi-line comment, 0 if no comment, < 0 when end is on current line
138 my $leading_comment; # multi-line comment has no code before its beginning delimiter, if $in_comment != 0
139 my $formatted_comment; # multi-line comment beginning with "/*-", which indicates/allows special formatting, if $in_comment != 0
142 my $ifdef__cplusplus; # line before contained '#ifdef __cplusplus' (used in header files)
146 my $preproc_offset; # offset to $block_indent within multi-line preprocessor directive, else 0
147 my $in_macro_header; # number of open parentheses + 1 in (multi-line) header of #define, if $in_preproc != 0
149 my $line; # current line number
150 my $line_before; # number of previous not essentially blank line (containing at most whitespace and '\')
151 my $line_before2; # number of not essentially blank line before previous not essentially blank line
154 my $contents; # contents of current line (without blinding)
155 # $_ # current line, where comments etc. get blinded
156 my $code_contents_before; # contents of previous non-comment non-preprocessor-directive line (without blinding), initially ""
161 my $in_multiline_string; # line starts within multi-line string literal
162 my $count; # -1 or number of leading whitespace characters (except newline) in current line,
164 my $count_before; # number of leading whitespace characters (except line ending chars) in $contents_before
165 my $has_label; # current line contains label
167 my $line_body_start; # number of line where last function body started, or 0
168 my $line_function_start; # number of line where last function definition started, used for $line_body_start
170 my $line_opening_brace; # number of previous line with opening brace after do/while/for, optionally for if/else
177 my $if_maybe_terminated; # 'if' ends and $hanging_offset should be reset unless the next line starts with 'else'
184 my $expr_indent; # resulting hanging indent within (multi-line) expressions including type exprs, else 0
191 my $num_reports_line = 0; # number of issues found on current line
197 my $num_length_reports = 0;# total number of line length issues found
204 $line = 0;
301 my $line = shift;
307 print "$ARGV:$line:$msg:$contents" unless $self_test;
319 report_flexibly($line, $msg, $contents);
337 sub check_indent { # used for lines outside multi-line string literals
342 m/^\s*\/\*/ ? "intra-line comment" :
363 # this cannot happen for labels and overrides special treatment of ':', '&&' and '||' for this line
370 # check consistency of indentation within multi-line comment (i.e., between its first, inner, and last lines)
371 if ($in_comment != 0 && $in_comment != 1) { # in multi-line comment but not on its first line
374 report("indent = $count != $comment_indent within multi-line comment")
378 report("indent = ".($count + $tweak)." != $comment_indent at end of multi-line comment")
382 # do not check indentation of last line of non-leading multi-line comment
384 s/^(\s*)@/$1*/; # blind first '@' as '*' to prevent below delayed check for the line before
387 return if $in_comment > 0; # not on its last line
388 # $comment_indent will be checked by the below checks for end of multi-line comment
391 # else check indentation of entire-line comment or entire-line end of multi-line comment
392 # ... w.r.t. indent of the following line by delayed check for the line before
393 if (($in_comment == 0 || $in_comment == 1) # no comment, intra-line comment, or begin of multi-line comment
394 && $line_before > 0 # there is a line before
395 && $contents_before_ =~ m/^(\s*)@[\s@]*$/) { # line before begins with '@', no code follows (except '\')
396 report_flexibly($line_before, "entire-line comment indent = $count_before != $count (of following line)",
399 # ... but allow normal indentation for the current line, else above check will be done for the line before
400 if (($in_comment == 0 || $in_comment < 0) # (no comment,) intra-line comment or end of multi-line comment
401 && m/^(\s*)@[\s@]*$/) { # line begins with '@', no code follows (except '\')
402 if ($count == $ref_indent) { # indentation is like for (normal) code in this line
403 s/^(\s*)@/$1*/; # blind first '@' as '*' to prevent above delayed check for the line before
406 return if !eof; # defer check of entire-line comment to next line
409 # else check indentation of leading intra-line comment or end of multi-line comment
410 if (m/^(\s*)@/) { # line begins with '@', i.e., any (remaining type of) comment
412 report("intra-line comment indent = $count != $ref_indent") if $in_comment == 0;
413 report("multi-line comment indent = $count != $ref_indent") if $in_comment < 0;
419 # do not report same indentation as on the line before (potentially due to same violations)
426 # apparently aligned to the right in order to fit within line length limit
485 # # multi-line expr after 'case'
543 $_ = "" if $self_test && m/ blank line within local decls /;
544 $line++;
558 # assign to $count the actual indentation level of the current line
569 # handle multi-line string literals to avoid confusion on starting/ending '"' and trailing '\'
575 report("multi-line string literal not terminated by '\"' and trailing '\' is missing")
583 s#('[^']*')#$1 =~ tr/'/@/cr#eg; # handle all intra-line character literals
584 s#("[^"]*")#$1 =~ tr/"/@/cr#eg; # handle all intra-line string literals
595 # do/prepare checks within multi-line comments
597 if ($in_comment > 0) { # this still includes the last line of multi-line comment
600 report("missing space or '*' after leading '*' in multi-line comment") if $cmt_text =~ m|^[^*\s/$self_test_exception]|;
602 report("missing leading '*' in multi-line comment");
607 # detect end of comment, must be within multi-line comment, check if it is preceded by non-whitespace text
616 report("text before '*/' in multi-line comment") if ($head =~ m/[^*\s]/); # non-SPC before '*/'
617 $in_comment = -1; # indicate that multi-line comment ends on current line
619 # make indentation of end of multi-line comment appear like of leading intra-line comment
622 $in_comment = -2; # indicate that multi-line comment ends on current line, with tweak
638 report("unexpected '/*' inside multi-line comment");
639 } elsif ($tail =~ m|^(.*?)\*/(.*)$|) { # comment end: */ on same line
640 report("unexpected '/*' inside intra-line comment") if $1 =~ /\/\*/;
645 } else { # begin of multi-line comment
647 report("text after '/*' in multi-line comment")
650 # adapt to actual indentation of first line
660 if ($in_comment > 1) { # still inside multi-line comment (not at its begin or end)
665 # handle special case of line after '#ifdef __cplusplus' (which typically appears in header files)
673 # while allowing trailing (also multi-line) string literals to go past $max_length
674 my $len = length; # total line length (without trailing '\n')
680 report("line length = $len > ".MAX_LINE_LENGTH);
683 # handle C++ / C99 - style end-of-line comments
685 report("'//' end-of-line comment"); # the '//' comment style is not allowed for C90
696 if (s/^(\s*#)(\s*)(\w+)//) { # line beginning with '#' and directive name;
703 report("preprocessor directive within multi-line directive");
721 $count = -1; # do not check indentation of first line of preprocessor directive
726 # intra-line whitespace nits @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
728 my $in_multiline_comment = ($in_comment > 1 || $in_comment < 0); # $in_multiline_comment refers to line before
733 $in_comment != 0 ? " in multi-line comment"
734 : " in intra-line comment" : "");
736 sub split_line_head { # split line contents into header containing leading spaces and the first non-space char, and the rest of the line
738 $in_comment != 0 ? "@" : ""; # '@' will match the blinded leading '*' in multi-line comment
739 # $in_comment may pertain to the following line due to delayed check
740 # do not check for extra SPC in leading spaces including any '#' (or '*' within multi-line comment)
747 if ($line_before > 0) { # check with one line delay, such that at least $contents_before is available
748 sub column_alignments_only { # return 1 if the given line has multiple consecutive spaces only at columns that match the reference line
751 my $intra = shift; # the rest of the line contents
752 my $contents = shift; # reference line
753 # check if all extra SPC in $intra is used only for multi-line column alignment with $contents
757 next unless $substr =~ m/^\s\s\S/; # extra SPC (but not in leading spaces of the line)
758 next if !$eol_cmt && $substr =~ m/^[@\s]+$/; # end-of-line comment
759 return 0 unless substr($contents, $col + $offset + 1, 2) =~ m/\s\S/; # reference line contents do not match
764 !( column_alignments_only($head1, $intra_line1, $_ ) # compare with $line
769 } elsif (eof) { # special case: just one line exists
887 my $body_len = $line - $line_body_start - 1;
904 # handle opening brace '{' after if/else/while/for/switch/do on line before
924 # potential adaptations of indent in first line of macro body in multi-line macro definition
940 check_indent() if $count >= 0; # not for start of preprocessor directive and not if multi-line string literal is continued
945 $in_comment == 0 && !m/^\s*\*?@/ && # not in a multi-line or intra-line comment
947 my $blank_line_before = $line > 1 && $code_contents_before =~ m/^\s*(\\\s*)?$/;
948 # essentially blank line before: just whitespace and maybe a '\'
953 report_flexibly($line - 1, "blank line within local decls, before", $contents) if $blank_line_before;
955 report_flexibly($line, "missing blank line after local decls", "\n$contents_before$contents")
958 m/^\s*(\\\s*)?$/ # essentially blank line: just whitespace (and maybe a trailing '\')
959 || $in_comment != 0 || m/^\s*\*?@/; # in multi-line comment or an intra-line comment
963 $in_comment = 0 if $in_comment < 0; # multi-line comment has ended
972 # check for code block containing a single line/statement
976 # TODO extend detection from single-line to potentially multi-line statement
982 # follows with a block containing more than one line/statement
1017 # to cope with multi-line expressions, do this also if !($tail =~ m/\{/)
1073 # on end of non-if/while/for/switch (multi-line) expression (i.e., return/enum/assignment) and
1099 $in_typedecl-- if $in_typedecl != 0 && @nested_in_typedecl == 0; # TODO handle multiple type decls per line
1102 # new $terminator_position value may be after the earlier one in case multiple terminators on current line
1103 # TODO check treatment in case of multiple terminators on current line
1119 # remember line number and header containing name of last function defined for reports w.r.t. MAX_BODY_LENGTH
1121 $line_function_start = $line;
1125 # special checks for last, typically trailing opening brace '{' in line
1131 report("'{' not at line start") if length($head) != $preproc_offset && $head =~ m/\)\s*/; # at end of function definition header
1132 $line_body_start = $contents =~ m/LONG BODY/ ? 0 : $line if $line_function_start != 0;
1135 $line_opening_brace = $line if $keyword_opening_brace =~ m/do|while|for/;
1136 # using, not assigning, $keyword_opening_brace here because it could be on an earlier line
1137 $line_opening_brace = $line if $keyword_opening_brace =~ m/if|else/ && $extended_1_stmt &&
1148 # check for opening brace after if/while/for/switch/do not on same line
1149 # note that "missing '{' on same line after '} else'" is handled further below
1154 report("'{' not on same line as preceding '$mid'") if !$brace_after;
1156 # check for closing brace on line before 'else' not followed by leading '{'
1158 if (parens_balance($tail) == 0 && # avoid false positive due to unfinished expr on current line
1159 !($tail =~ m/{/) && # after 'else' missing '{' on same line
1161 $line_before > 0 && $contents_before_ =~ /}[\s@]*$/) { # trailing '}' on line before
1162 report("missing '{' on same line after '} else'");
1166 # check for closing brace before 'while' not on same line
1172 parens_balance($tail) == 0 && # avoid false positive due to unfinished expr on current line
1175 $contents_before_ =~ /}[\s@]*$/) { # on line before: '}' then any whitespace or comments
1176 report("'while' not on same line as preceding '}'");
1180 # check for missing brace on same line before or after 'else'
1187 report("'else' not on same line as preceding '}'");
1188 } elsif (parens_balance($tail) == 0) { # avoid false positive due to unfinished expr on current line
1189 report("missing '}' on same line before 'else ... {'") if $brace_after;
1191 } elsif (parens_balance($tail) == 0) { # avoid false positive due to unfinished expr on current line
1192 report("missing '{' on same line after '} else'") if $brace_before && !$brace_after;
1196 # on begin of multi-line preprocessor directive, adapt indent
1199 if ($in_preproc == 1) { # start of multi-line preprocessor directive
1208 # post-processing at end of line @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1212 !m/^\s*#(\s*)(\w+)/ && # not single-line preprocessor directive
1213 $in_comment == 0 && !m/^\s*\*?@/; # not in a multi-line comment nor in an intra-line comment
1215 # on end of (possibly multi-line) preprocessor directive, adapt indent
1223 report("leading ".($1 eq "" ? "blank" :"whitespace")." line") if $line == 1 && !$sloppy_SPC;
1226 my $linediff = $line - $line_before - 1;
1232 $line_before = $line;
1241 print("$ARGV:$line:$num_reports_line reports on:$contents")
1249 # check for essentially blank line (which may include a '\') just before EOF
1250 report(($1 eq "\n" ? "blank line" : $2 ne "" ? "'\\'" : "whitespace")." at EOF")
1257 report_flexibly($line, +@nested_block_indents." unclosed '{'", "(EOF)\n") if @nested_block_indents != 0;
1260 report_flexibly($line, "$preproc_if_nesting unclosed '#if'", "(EOF)\n") if $preproc_if_nesting != 0;