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)
| 2641 | |
| 2642 | |
| 2643 | def CheckSpacing(filename, clean_lines, linenum, nesting_state, error): |
| 2644 | """Checks for the correctness of various spacing issues in the code. |
| 2645 | |
| 2646 | Things we check for: spaces around operators, spaces after |
| 2647 | if/for/while/switch, no spaces around parens in function calls, two |
| 2648 | spaces between code and comment, don't start a block with a blank |
| 2649 | line, don't end a function with a blank line, don't add a blank line |
| 2650 | after public/protected/private, don't have too many blank lines in a row. |
| 2651 | |
| 2652 | Args: |
| 2653 | filename: The name of the current file. |
| 2654 | clean_lines: A CleansedLines instance containing the file. |
| 2655 | linenum: The number of the line to check. |
| 2656 | nesting_state: A _NestingState instance which maintains information about |
| 2657 | the current stack of nested blocks being parsed. |
| 2658 | error: The function to call with any errors found. |
| 2659 | """ |
| 2660 | |
| 2661 | # Don't use "elided" lines here, otherwise we can't check commented lines. |
| 2662 | # Don't want to use "raw" either, because we don't want to check inside C++11 |
| 2663 | # raw strings, |
| 2664 | raw = clean_lines.lines_without_raw_strings |
| 2665 | line = raw[linenum] |
| 2666 | |
| 2667 | # Before nixing comments, check if the line is blank for no good |
| 2668 | # reason. This includes the first line after a block is opened, and |
| 2669 | # blank lines at the end of a function (ie, right before a line like '}' |
| 2670 | # |
| 2671 | # Skip all the blank line checks if we are immediately inside a |
| 2672 | # namespace body. In other words, don't issue blank line warnings |
| 2673 | # for this block: |
| 2674 | # namespace { |
| 2675 | # |
| 2676 | # } |
| 2677 | # |
| 2678 | # A warning about missing end of namespace comments will be issued instead. |
| 2679 | if IsBlankLine(line) and not nesting_state.InNamespaceBody(): |
| 2680 | elided = clean_lines.elided |
| 2681 | prev_line = elided[linenum - 1] |
| 2682 | prevbrace = prev_line.rfind('{') |
| 2683 | # TODO(unknown): Don't complain if line before blank line, and line after, |
| 2684 | # both start with alnums and are indented the same amount. |
| 2685 | # This ignores whitespace at the start of a namespace block |
| 2686 | # because those are not usually indented. |
| 2687 | if prevbrace != -1 and prev_line[prevbrace:].find('}') == -1: |
| 2688 | # OK, we have a blank line at the start of a code block. Before we |
| 2689 | # complain, we check if it is an exception to the rule: The previous |
| 2690 | # non-empty line has the parameters of a function header that are indented |
| 2691 | # 4 spaces (because they did not fit in a 80 column line when placed on |
| 2692 | # the same line as the function name). We also check for the case where |
| 2693 | # the previous line is indented 6 spaces, which may happen when the |
| 2694 | # initializers of a constructor do not fit into a 80 column line. |
| 2695 | exception = False |
| 2696 | if Match(r' {6}\w', prev_line): # Initializer list? |
| 2697 | # We are looking for the opening column of initializer list, which |
| 2698 | # should be indented 4 spaces to cause 6 space indentation afterwards. |
| 2699 | search_position = linenum-2 |
| 2700 | while (search_position >= 0 |
no test coverage detected