Changeset 46294 in webkit


Ignore:
Timestamp:
Jul 23, 2009 3:53:47 PM (15 years ago)
Author:
treat@webkit.org
Message:

2009-07-23 Jakob Petsovits <jakob.petsovits@torchmobile.com>

Reviewed by Adam Treat.

Fix false positives for switch statement indentation check in cpplint.
https://bugs.webkit.org/show_bug.cgi?id=27615

Makes one-line case statements (e.g. "case foo: bar();") work.
Also a few general improvements to the robustness and readability of
the check, and more test cases.

  • Scripts/modules/cpplint.py:
  • Scripts/modules/cpplint_unittest.py:
Location:
trunk/WebKitTools
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/WebKitTools/ChangeLog

    r46292 r46294  
     12009-07-23  Jakob Petsovits  <jakob.petsovits@torchmobile.com>
     2
     3         Reviewed by Adam Treat.
     4
     5         Fix false positives for switch statement indentation check in cpplint.
     6         https://bugs.webkit.org/show_bug.cgi?id=27615
     7
     8         Makes one-line case statements (e.g. "case foo: bar();") work.
     9         Also a few general improvements to the robustness and readability of
     10         the check, and more test cases.
     11
     12         * Scripts/modules/cpplint.py:
     13         * Scripts/modules/cpplint_unittest.py:
     14
    1152009-07-23  Jakob Petsovits  <jakob.petsovits@torchmobile.com>
    216
  • trunk/WebKitTools/Scripts/modules/cpplint.py

    r46292 r46294  
    17691769        remaining_line = current_indentation_match.group('remaining_line')
    17701770
    1771         if remaining_line.startswith('}'):
    1772             break # The end of the switch statement.
    1773         elif match(r'(default|case\s+.*)\s*:\s*$', remaining_line):
     1771        # End the check at the end of the switch statement.
     1772        if remaining_line.startswith('}') and current_indentation == switch_indentation:
     1773            break
     1774        # Case and default branches should not be indented. The regexp also
     1775        # catches single-line cases like "default: break;" but does not trigger
     1776        # on stuff like "Document::Foo();".
     1777        elif match(r'(default|case\s+.*)\s*:([^:].*)?$', remaining_line):
    17741778            if current_indentation != switch_indentation:
    17751779                error(filename, line_number + line_offset, 'whitespace/indent', 4,
     
    17781782                # one should be enough to figure out the problem.
    17791783                break
    1780         elif not match(r'\w+\s*:\s*$', remaining_line):
    1781             # It's not a goto label (which we don't care about), so check if
    1782             # it's indented at least as far as the switch plus 4 spaces.
    1783             if not current_indentation.startswith(inner_indentation):
    1784                 error(filename, line_number + line_offset, 'whitespace/indent', 4,
    1785                       'Non-label code inside switch statements should be indented.')
    1786                 # Don't throw an error for multiple badly indented statements,
    1787                 # one should be enough to figure out the problem.
    1788                 break
     1784        # We ignore goto labels at the very beginning of a line.
     1785        elif match(r'\w+\s*:\s*$', remaining_line):
     1786            continue
     1787        # It's not a goto label, so check if it's indented at least as far as
     1788        # the switch statement plus one more level of indentation.
     1789        elif not current_indentation.startswith(inner_indentation):
     1790            error(filename, line_number + line_offset, 'whitespace/indent', 4,
     1791                  'Non-label code inside switch statements should be indented.')
     1792            # Don't throw an error for multiple badly indented statements,
     1793            # one should be enough to figure out the problem.
     1794            break
    17891795
    17901796        if encountered_nested_switch:
  • trunk/WebKitTools/Scripts/modules/cpplint_unittest.py

    r46292 r46294  
    28302830        self.assert_multi_line_lint(
    28312831            '    switch (condition) {\n'
     2832            '    case fooCondition: break;\n'
     2833            '    default: return;\n'
     2834            '    }\n',
     2835            '')
     2836        self.assert_multi_line_lint(
     2837            '    switch (condition) {\n'
    28322838            '        case fooCondition:\n'
    28332839            '        case barCondition:\n'
     
    28352841            '            break;\n'
    28362842            '        default:\n'
     2843            '            i--;\n'
     2844            '    }\n',
     2845            'A case label should not be indented, but line up with its switch statement.'
     2846            '  [whitespace/indent] [4]')
     2847        self.assert_multi_line_lint(
     2848            '    switch (condition) {\n'
     2849            '        case fooCondition:\n'
     2850            '            break;\n'
     2851            '    default:\n'
    28372852            '            i--;\n'
    28382853            '    }\n',
Note: See TracChangeset for help on using the changeset viewer.