@@ 3692-3777 (lines=86) @@ | ||
3689 | return False |
|
3690 | ||
3691 | ||
3692 | def CheckBracesSpacing(filename, clean_lines, linenum, nesting_state, error): |
|
3693 | """Checks for horizontal spacing near commas. |
|
3694 | ||
3695 | Args: |
|
3696 | filename: The name of the current file. |
|
3697 | clean_lines: A CleansedLines instance containing the file. |
|
3698 | linenum: The number of the line to check. |
|
3699 | nesting_state: A NestingState instance which maintains information about |
|
3700 | the current stack of nested blocks being parsed. |
|
3701 | error: The function to call with any errors found. |
|
3702 | """ |
|
3703 | line = clean_lines.elided[linenum] |
|
3704 | ||
3705 | # Except after an opening paren, or after another opening brace (in case of |
|
3706 | # an initializer list, for instance), you should have spaces before your |
|
3707 | # braces when they are delimiting blocks, classes, namespaces etc. |
|
3708 | # And since you should never have braces at the beginning of a line, |
|
3709 | # this is an easy test. Except that braces used for initialization don't |
|
3710 | # follow the same rule; we often don't want spaces before those. |
|
3711 | match = Match(r'^(.*[^ ({>]){', line) |
|
3712 | ||
3713 | if match: |
|
3714 | # Try a bit harder to check for brace initialization. This |
|
3715 | # happens in one of the following forms: |
|
3716 | # Constructor() : initializer_list_{} { ... } |
|
3717 | # Constructor{}.MemberFunction() |
|
3718 | # Type variable{}; |
|
3719 | # FunctionCall(type{}, ...); |
|
3720 | # LastArgument(..., type{}); |
|
3721 | # LOG(INFO) << type{} << " ..."; |
|
3722 | # map_of_type[{...}] = ...; |
|
3723 | # ternary = expr ? new type{} : nullptr; |
|
3724 | # OuterTemplate<InnerTemplateConstructor<Type>{}> |
|
3725 | # |
|
3726 | # We check for the character following the closing brace, and |
|
3727 | # silence the warning if it's one of those listed above, i.e. |
|
3728 | # "{.;,)<>]:". |
|
3729 | # |
|
3730 | # To account for nested initializer list, we allow any number of |
|
3731 | # closing braces up to "{;,)<". We can't simply silence the |
|
3732 | # warning on first sight of closing brace, because that would |
|
3733 | # cause false negatives for things that are not initializer lists. |
|
3734 | # Silence this: But not this: |
|
3735 | # Outer{ if (...) { |
|
3736 | # Inner{...} if (...){ // Missing space before { |
|
3737 | # }; } |
|
3738 | # |
|
3739 | # There is a false negative with this approach if people inserted |
|
3740 | # spurious semicolons, e.g. "if (cond){};", but we will catch the |
|
3741 | # spurious semicolon with a separate check. |
|
3742 | leading_text = match.group(1) |
|
3743 | (endline, endlinenum, endpos) = CloseExpression( |
|
3744 | clean_lines, linenum, len(match.group(1))) |
|
3745 | trailing_text = '' |
|
3746 | if endpos > -1: |
|
3747 | trailing_text = endline[endpos:] |
|
3748 | for offset in xrange(endlinenum + 1, |
|
3749 | min(endlinenum + 3, clean_lines.NumLines() - 1)): |
|
3750 | trailing_text += clean_lines.elided[offset] |
|
3751 | # We also suppress warnings for `uint64_t{expression}` etc., as the style |
|
3752 | # guide recommends brace initialization for integral types to avoid |
|
3753 | # overflow/truncation. |
|
3754 | if (not Match(r'^[\s}]*[{.;,)<>\]:]', trailing_text) |
|
3755 | and not _IsType(clean_lines, nesting_state, leading_text)): |
|
3756 | error(filename, linenum, 'whitespace/braces', 5, |
|
3757 | 'Missing space before {') |
|
3758 | ||
3759 | # Make sure '} else {' has spaces. |
|
3760 | if Search(r'}else', line): |
|
3761 | error(filename, linenum, 'whitespace/braces', 5, |
|
3762 | 'Missing space before else') |
|
3763 | ||
3764 | # You shouldn't have a space before a semicolon at the end of the line. |
|
3765 | # There's a special case for "for" since the style guide allows space before |
|
3766 | # the semicolon there. |
|
3767 | if Search(r':\s*;\s*$', line): |
|
3768 | error(filename, linenum, 'whitespace/semicolon', 5, |
|
3769 | 'Semicolon defining empty statement. Use {} instead.') |
|
3770 | elif Search(r'^\s*;\s*$', line): |
|
3771 | error(filename, linenum, 'whitespace/semicolon', 5, |
|
3772 | 'Line contains only semicolon. If this should be an empty statement, ' |
|
3773 | 'use {} instead.') |
|
3774 | elif (Search(r'\s+;\s*$', line) and |
|
3775 | not Search(r'\bfor\b', line)): |
|
3776 | error(filename, linenum, 'whitespace/semicolon', 5, |
|
3777 | 'Extra space before last semicolon. If this should be an empty ' |
|
3778 | 'statement, use {} instead.') |
|
3779 | ||
3780 |
@@ 3692-3777 (lines=86) @@ | ||
3689 | return False |
|
3690 | ||
3691 | ||
3692 | def CheckBracesSpacing(filename, clean_lines, linenum, nesting_state, error): |
|
3693 | """Checks for horizontal spacing near commas. |
|
3694 | ||
3695 | Args: |
|
3696 | filename: The name of the current file. |
|
3697 | clean_lines: A CleansedLines instance containing the file. |
|
3698 | linenum: The number of the line to check. |
|
3699 | nesting_state: A NestingState instance which maintains information about |
|
3700 | the current stack of nested blocks being parsed. |
|
3701 | error: The function to call with any errors found. |
|
3702 | """ |
|
3703 | line = clean_lines.elided[linenum] |
|
3704 | ||
3705 | # Except after an opening paren, or after another opening brace (in case of |
|
3706 | # an initializer list, for instance), you should have spaces before your |
|
3707 | # braces when they are delimiting blocks, classes, namespaces etc. |
|
3708 | # And since you should never have braces at the beginning of a line, |
|
3709 | # this is an easy test. Except that braces used for initialization don't |
|
3710 | # follow the same rule; we often don't want spaces before those. |
|
3711 | match = Match(r'^(.*[^ ({>]){', line) |
|
3712 | ||
3713 | if match: |
|
3714 | # Try a bit harder to check for brace initialization. This |
|
3715 | # happens in one of the following forms: |
|
3716 | # Constructor() : initializer_list_{} { ... } |
|
3717 | # Constructor{}.MemberFunction() |
|
3718 | # Type variable{}; |
|
3719 | # FunctionCall(type{}, ...); |
|
3720 | # LastArgument(..., type{}); |
|
3721 | # LOG(INFO) << type{} << " ..."; |
|
3722 | # map_of_type[{...}] = ...; |
|
3723 | # ternary = expr ? new type{} : nullptr; |
|
3724 | # OuterTemplate<InnerTemplateConstructor<Type>{}> |
|
3725 | # |
|
3726 | # We check for the character following the closing brace, and |
|
3727 | # silence the warning if it's one of those listed above, i.e. |
|
3728 | # "{.;,)<>]:". |
|
3729 | # |
|
3730 | # To account for nested initializer list, we allow any number of |
|
3731 | # closing braces up to "{;,)<". We can't simply silence the |
|
3732 | # warning on first sight of closing brace, because that would |
|
3733 | # cause false negatives for things that are not initializer lists. |
|
3734 | # Silence this: But not this: |
|
3735 | # Outer{ if (...) { |
|
3736 | # Inner{...} if (...){ // Missing space before { |
|
3737 | # }; } |
|
3738 | # |
|
3739 | # There is a false negative with this approach if people inserted |
|
3740 | # spurious semicolons, e.g. "if (cond){};", but we will catch the |
|
3741 | # spurious semicolon with a separate check. |
|
3742 | leading_text = match.group(1) |
|
3743 | (endline, endlinenum, endpos) = CloseExpression( |
|
3744 | clean_lines, linenum, len(match.group(1))) |
|
3745 | trailing_text = '' |
|
3746 | if endpos > -1: |
|
3747 | trailing_text = endline[endpos:] |
|
3748 | for offset in xrange(endlinenum + 1, |
|
3749 | min(endlinenum + 3, clean_lines.NumLines() - 1)): |
|
3750 | trailing_text += clean_lines.elided[offset] |
|
3751 | # We also suppress warnings for `uint64_t{expression}` etc., as the style |
|
3752 | # guide recommends brace initialization for integral types to avoid |
|
3753 | # overflow/truncation. |
|
3754 | if (not Match(r'^[\s}]*[{.;,)<>\]:]', trailing_text) |
|
3755 | and not _IsType(clean_lines, nesting_state, leading_text)): |
|
3756 | error(filename, linenum, 'whitespace/braces', 5, |
|
3757 | 'Missing space before {') |
|
3758 | ||
3759 | # Make sure '} else {' has spaces. |
|
3760 | if Search(r'}else', line): |
|
3761 | error(filename, linenum, 'whitespace/braces', 5, |
|
3762 | 'Missing space before else') |
|
3763 | ||
3764 | # You shouldn't have a space before a semicolon at the end of the line. |
|
3765 | # There's a special case for "for" since the style guide allows space before |
|
3766 | # the semicolon there. |
|
3767 | if Search(r':\s*;\s*$', line): |
|
3768 | error(filename, linenum, 'whitespace/semicolon', 5, |
|
3769 | 'Semicolon defining empty statement. Use {} instead.') |
|
3770 | elif Search(r'^\s*;\s*$', line): |
|
3771 | error(filename, linenum, 'whitespace/semicolon', 5, |
|
3772 | 'Line contains only semicolon. If this should be an empty statement, ' |
|
3773 | 'use {} instead.') |
|
3774 | elif (Search(r'\s+;\s*$', line) and |
|
3775 | not Search(r'\bfor\b', line)): |
|
3776 | error(filename, linenum, 'whitespace/semicolon', 5, |
|
3777 | 'Extra space before last semicolon. If this should be an empty ' |
|
3778 | 'statement, use {} instead.') |
|
3779 | ||
3780 |