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)
| 3169 | |
| 3170 | |
| 3171 | def CheckSpacing(filename, clean_lines, linenum, nesting_state, error): |
| 3172 | """Checks for the correctness of various spacing issues in the code. |
| 3173 | |
| 3174 | Things we check for: spaces around operators, spaces after |
| 3175 | if/for/while/switch, no spaces around parens in function calls, two |
| 3176 | spaces between code and comment, don't start a block with a blank |
| 3177 | line, don't end a function with a blank line, don't add a blank line |
| 3178 | after public/protected/private, don't have too many blank lines in a row. |
| 3179 | |
| 3180 | Args: |
| 3181 | filename: The name of the current file. |
| 3182 | clean_lines: A CleansedLines instance containing the file. |
| 3183 | linenum: The number of the line to check. |
| 3184 | nesting_state: A NestingState instance which maintains information about |
| 3185 | the current stack of nested blocks being parsed. |
| 3186 | error: The function to call with any errors found. |
| 3187 | """ |
| 3188 | |
| 3189 | # Don't use "elided" lines here, otherwise we can't check commented lines. |
| 3190 | # Don't want to use "raw" either, because we don't want to check inside C++11 |
| 3191 | # raw strings, |
| 3192 | raw = clean_lines.lines_without_raw_strings |
| 3193 | line = raw[linenum] |
| 3194 | |
| 3195 | # Before nixing comments, check if the line is blank for no good |
| 3196 | # reason. This includes the first line after a block is opened, and |
| 3197 | # blank lines at the end of a function (ie, right before a line like '}' |
| 3198 | # |
| 3199 | # Skip all the blank line checks if we are immediately inside a |
| 3200 | # namespace body. In other words, don't issue blank line warnings |
| 3201 | # for this block: |
| 3202 | # namespace { |
| 3203 | # |
| 3204 | # } |
| 3205 | # |
| 3206 | # A warning about missing end of namespace comments will be issued instead. |
| 3207 | # |
| 3208 | # Also skip blank line checks for 'extern "C"' blocks, which are formatted |
| 3209 | # like namespaces. |
| 3210 | if (IsBlankLine(line) and |
| 3211 | not nesting_state.InNamespaceBody() and |
| 3212 | not nesting_state.InExternC()): |
| 3213 | elided = clean_lines.elided |
| 3214 | prev_line = elided[linenum - 1] |
| 3215 | prevbrace = prev_line.rfind('{') |
| 3216 | # TODO(unknown): Don't complain if line before blank line, and line after, |
| 3217 | # both start with alnums and are indented the same amount. |
| 3218 | # This ignores whitespace at the start of a namespace block |
| 3219 | # because those are not usually indented. |
| 3220 | if prevbrace != -1 and prev_line[prevbrace:].find('}') == -1: |
| 3221 | # OK, we have a blank line at the start of a code block. Before we |
| 3222 | # complain, we check if it is an exception to the rule: The previous |
| 3223 | # non-empty line has the parameters of a function header that are indented |
| 3224 | # 4 spaces (because they did not fit in a 80 column line when placed on |
| 3225 | # the same line as the function name). We also check for the case where |
| 3226 | # the previous line is indented 6 spaces, which may happen when the |
| 3227 | # initializers of a constructor do not fit into a 80 column line. |
| 3228 | exception = False |
no test coverage detected