Checks for the correctness of various spacing issues in the code. Things we check for: spaces around operators, spaces after if/for/while/switch, no spaces around parens in function calls, two spaces between code and comment, don't start a block with a blank line, don't end a function with
(filename, clean_lines, linenum, nesting_state, error)
| 3665 | |
| 3666 | |
| 3667 | def CheckSpacing(filename, clean_lines, linenum, nesting_state, error): |
| 3668 | """Checks for the correctness of various spacing issues in the code. |
| 3669 | |
| 3670 | Things we check for: spaces around operators, spaces after |
| 3671 | if/for/while/switch, no spaces around parens in function calls, two |
| 3672 | spaces between code and comment, don't start a block with a blank |
| 3673 | line, don't end a function with a blank line, don't add a blank line |
| 3674 | after public/protected/private, don't have too many blank lines in a row. |
| 3675 | |
| 3676 | Args: |
| 3677 | filename: The name of the current file. |
| 3678 | clean_lines: A CleansedLines instance containing the file. |
| 3679 | linenum: The number of the line to check. |
| 3680 | nesting_state: A NestingState instance which maintains information about |
| 3681 | the current stack of nested blocks being parsed. |
| 3682 | error: The function to call with any errors found. |
| 3683 | """ |
| 3684 | |
| 3685 | # Don't use "elided" lines here, otherwise we can't check commented lines. |
| 3686 | # Don't want to use "raw" either, because we don't want to check inside C++11 |
| 3687 | # raw strings, |
| 3688 | raw = clean_lines.lines_without_raw_strings |
| 3689 | line = raw[linenum] |
| 3690 | |
| 3691 | # Before nixing comments, check if the line is blank for no good |
| 3692 | # reason. This includes the first line after a block is opened, and |
| 3693 | # blank lines at the end of a function (ie, right before a line like '}' |
| 3694 | # |
| 3695 | # Skip all the blank line checks if we are immediately inside a |
| 3696 | # namespace body. In other words, don't issue blank line warnings |
| 3697 | # for this block: |
| 3698 | # namespace { |
| 3699 | # |
| 3700 | # } |
| 3701 | # |
| 3702 | # A warning about missing end of namespace comments will be issued instead. |
| 3703 | # |
| 3704 | # Also skip blank line checks for 'extern "C"' blocks, which are formatted |
| 3705 | # like namespaces. |
| 3706 | if (IsBlankLine(line) and |
| 3707 | not nesting_state.InNamespaceBody() and |
| 3708 | not nesting_state.InExternC()): |
| 3709 | elided = clean_lines.elided |
| 3710 | prev_line = elided[linenum - 1] |
| 3711 | prevbrace = prev_line.rfind('{') |
| 3712 | # TODO(unknown): Don't complain if line before blank line, and line after, |
| 3713 | # both start with alnums and are indented the same amount. |
| 3714 | # This ignores whitespace at the start of a namespace block |
| 3715 | # because those are not usually indented. |
| 3716 | if prevbrace != -1 and prev_line[prevbrace:].find('}') == -1: |
| 3717 | # OK, we have a blank line at the start of a code block. Before we |
| 3718 | # complain, we check if it is an exception to the rule: The previous |
| 3719 | # non-empty line has the parameters of a function header that are indented |
| 3720 | # 4 spaces (because they did not fit in a 80 column line when placed on |
| 3721 | # the same line as the function name). We also check for the case where |
| 3722 | # the previous line is indented 6 spaces, which may happen when the |
| 3723 | # initializers of a constructor do not fit into a 80 column line. |
| 3724 | exception = False |
no test coverage detected