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 functi
(filename, clean_lines, linenum, nesting_state, error)
| 4211 | |
| 4212 | |
| 4213 | def CheckSpacing(filename, clean_lines, linenum, nesting_state, error): |
| 4214 | """Checks for the correctness of various spacing issues in the code. |
| 4215 | |
| 4216 | Things we check for: spaces around operators, spaces after |
| 4217 | if/for/while/switch, no spaces around parens in function calls, two |
| 4218 | spaces between code and comment, don't start a block with a blank |
| 4219 | line, don't end a function with a blank line, don't add a blank line |
| 4220 | after public/protected/private, don't have too many blank lines in a row. |
| 4221 | |
| 4222 | Args: |
| 4223 | filename: The name of the current file. |
| 4224 | clean_lines: A CleansedLines instance containing the file. |
| 4225 | linenum: The number of the line to check. |
| 4226 | nesting_state: A NestingState instance which maintains information about |
| 4227 | the current stack of nested blocks being parsed. |
| 4228 | error: The function to call with any errors found. |
| 4229 | """ |
| 4230 | |
| 4231 | # Don't use "elided" lines here, otherwise we can't check commented lines. |
| 4232 | # Don't want to use "raw" either, because we don't want to check inside C++11 |
| 4233 | # raw strings, |
| 4234 | raw = clean_lines.lines_without_raw_strings |
| 4235 | line = raw[linenum] |
| 4236 | |
| 4237 | # Before nixing comments, check if the line is blank for no good |
| 4238 | # reason. This includes the first line after a block is opened, and |
| 4239 | # blank lines at the end of a function (ie, right before a line like '}' |
| 4240 | # |
| 4241 | # Skip all the blank line checks if we are immediately inside a |
| 4242 | # namespace body. In other words, don't issue blank line warnings |
| 4243 | # for this block: |
| 4244 | # namespace { |
| 4245 | # |
| 4246 | # } |
| 4247 | # |
| 4248 | # A warning about missing end of namespace comments will be issued instead. |
| 4249 | # |
| 4250 | # Also skip blank line checks for 'extern "C"' blocks, which are formatted |
| 4251 | # like namespaces. |
| 4252 | if IsBlankLine(line) and not nesting_state.InNamespaceBody() and not nesting_state.InExternC(): |
| 4253 | elided = clean_lines.elided |
| 4254 | prev_line = elided[linenum - 1] |
| 4255 | prevbrace = prev_line.rfind("{") |
| 4256 | # TODO(google): Don't complain if line before blank line, and line after, |
| 4257 | # both start with alnums and are indented the same amount. |
| 4258 | # This ignores whitespace at the start of a namespace block |
| 4259 | # because those are not usually indented. |
| 4260 | if prevbrace != -1 and prev_line[prevbrace:].find("}") == -1: |
| 4261 | # OK, we have a blank line at the start of a code block. Before we |
| 4262 | # complain, we check if it is an exception to the rule: The previous |
| 4263 | # non-empty line has the parameters of a function header that are indented |
| 4264 | # 4 spaces (because they did not fit in a 80 column line when placed on |
| 4265 | # the same line as the function name). We also check for the case where |
| 4266 | # the previous line is indented 6 spaces, which may happen when the |
| 4267 | # initializers of a constructor do not fit into a 80 column line. |
| 4268 | exception = False |
| 4269 | if re.match(r" {6}\w", prev_line): # Initializer list? |
| 4270 | # We are looking for the opening column of initializer list, which |
no test coverage detected
searching dependent graphs…