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)
| 2997 | |
| 2998 | |
| 2999 | def CheckSpacing(filename, clean_lines, linenum, nesting_state, error): |
| 3000 | """Checks for the correctness of various spacing issues in the code. |
| 3001 | |
| 3002 | Things we check for: spaces around operators, spaces after |
| 3003 | if/for/while/switch, no spaces around parens in function calls, two |
| 3004 | spaces between code and comment, don't start a block with a blank |
| 3005 | line, don't end a function with a blank line, don't add a blank line |
| 3006 | after public/protected/private, don't have too many blank lines in a row. |
| 3007 | |
| 3008 | Args: |
| 3009 | filename: The name of the current file. |
| 3010 | clean_lines: A CleansedLines instance containing the file. |
| 3011 | linenum: The number of the line to check. |
| 3012 | nesting_state: A NestingState instance which maintains information about |
| 3013 | the current stack of nested blocks being parsed. |
| 3014 | error: The function to call with any errors found. |
| 3015 | """ |
| 3016 | |
| 3017 | # Don't use "elided" lines here, otherwise we can't check commented lines. |
| 3018 | # Don't want to use "raw" either, because we don't want to check inside C++11 |
| 3019 | # raw strings, |
| 3020 | raw = clean_lines.lines_without_raw_strings |
| 3021 | line = raw[linenum] |
| 3022 | |
| 3023 | # Before nixing comments, check if the line is blank for no good |
| 3024 | # reason. This includes the first line after a block is opened, and |
| 3025 | # blank lines at the end of a function (ie, right before a line like '}' |
| 3026 | # |
| 3027 | # Skip all the blank line checks if we are immediately inside a |
| 3028 | # namespace body. In other words, don't issue blank line warnings |
| 3029 | # for this block: |
| 3030 | # namespace { |
| 3031 | # |
| 3032 | # } |
| 3033 | # |
| 3034 | # A warning about missing end of namespace comments will be issued instead. |
| 3035 | # |
| 3036 | # Also skip blank line checks for 'extern "C"' blocks, which are formatted |
| 3037 | # like namespaces. |
| 3038 | if (IsBlankLine(line) and |
| 3039 | not nesting_state.InNamespaceBody() and |
| 3040 | not nesting_state.InExternC()): |
| 3041 | elided = clean_lines.elided |
| 3042 | prev_line = elided[linenum - 1] |
| 3043 | prevbrace = prev_line.rfind('{') |
| 3044 | # TODO(unknown): Don't complain if line before blank line, and line after, |
| 3045 | # both start with alnums and are indented the same amount. |
| 3046 | # This ignores whitespace at the start of a namespace block |
| 3047 | # because those are not usually indented. |
| 3048 | if prevbrace != -1 and prev_line[prevbrace:].find('}') == -1: |
| 3049 | # OK, we have a blank line at the start of a code block. Before we |
| 3050 | # complain, we check if it is an exception to the rule: The previous |
| 3051 | # non-empty line has the parameters of a function header that are indented |
| 3052 | # 4 spaces (because they did not fit in a 80 column line when placed on |
| 3053 | # the same line as the function name). We also check for the case where |
| 3054 | # the previous line is indented 6 spaces, which may happen when the |
| 3055 | # initializers of a constructor do not fit into a 80 column line. |
| 3056 | exception = False |
no test coverage detected